Mercurial > emacs
annotate info/eintr-2 @ 73595:f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
author | Robert J. Chassell <bob@rattlesnake.com> |
---|---|
date | Tue, 31 Oct 2006 18:04:34 +0000 |
parents | b214bd8be620 |
children |
rev | line source |
---|---|
73591
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1 This is ../info/eintr, produced by makeinfo version 4.8 from |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2 emacs-lisp-intro.texi. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4 INFO-DIR-SECTION Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5 START-INFO-DIR-ENTRY |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6 * Emacs Lisp Intro: (eintr). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7 A simple introduction to Emacs Lisp programming. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
8 END-INFO-DIR-ENTRY |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
9 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
10 This is an `Introduction to Programming in Emacs Lisp', for people who |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
11 are not programmers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
12 |
73595
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
13 Edition 3.01, 2006 Oct 31 |
73591
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
14 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
15 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1997, 2001, 2002, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
16 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
17 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
18 Published by the: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
19 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
20 GNU Press, Website: http://www.gnupress.org |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
21 a division of the General: press@gnu.org |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
22 Free Software Foundation, Inc. Orders: sales@gnu.org |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
23 51 Franklin Street, Fifth Floor Tel: +1 (617) 542-5942 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
24 Boston, MA 02110-1301 USA Fax: +1 (617) 542-2652 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
25 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
26 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
27 ISBN 1-882114-43-4 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
28 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
29 Permission is granted to copy, distribute and/or modify this document |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
30 under the terms of the GNU Free Documentation License, Version 1.2 or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
31 any later version published by the Free Software Foundation; there |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
32 being no Invariant Section, with the Front-Cover Texts being "A GNU |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
33 Manual", and with the Back-Cover Texts as in (a) below. A copy of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
34 license is included in the section entitled "GNU Free Documentation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
35 License". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
36 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
37 (a) The FSF's Back-Cover Text is: "You have freedom to copy and modify |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
38 this GNU Manual, like GNU software. Copies published by the Free |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
39 Software Foundation raise funds for GNU development." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
40 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
41 |
73595
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
42 File: eintr, Node: See variable current value, Next: defvar and asterisk, Prev: defvar, Up: defvar |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
43 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
44 Seeing the Current Value of a Variable |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
45 -------------------------------------- |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
46 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
47 You can see the current value of a variable, any variable, by using the |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
48 `describe-variable' function, which is usually invoked by typing `C-h |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
49 v'. If you type `C-h v' and then `kill-ring' (followed by <RET>) when |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
50 prompted, you will see what is in your current kill ring--this may be |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
51 quite a lot! Conversely, if you have been doing nothing this Emacs |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
52 session except read this document, you may have nothing in it. Also, |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
53 you will see the documentation for `kill-ring': |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
54 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
55 Documentation: |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
56 List of killed text sequences. |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
57 Since the kill ring is supposed to interact nicely with cut-and-paste |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
58 facilities offered by window systems, use of this variable should |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
59 interact nicely with `interprogram-cut-function' and |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
60 `interprogram-paste-function'. The functions `kill-new', |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
61 `kill-append', and `current-kill' are supposed to implement this |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
62 interaction; you may want to use them instead of manipulating the kill |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
63 ring directly. |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
64 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
65 The kill ring is defined by a `defvar' in the following way: |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
66 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
67 (defvar kill-ring nil |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
68 "List of killed text sequences. |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
69 ...") |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
70 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
71 In this variable definition, the variable is given an initial value of |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
72 `nil', which makes sense, since if you have saved nothing, you want |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
73 nothing back if you give a `yank' command. The documentation string is |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
74 written just like the documentation string of a `defun'. As with the |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
75 documentation string of the `defun', the first line of the |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
76 documentation should be a complete sentence, since some commands, like |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
77 `apropos', print only the first line of documentation. Succeeding |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
78 lines should not be indented; otherwise they look odd when you use `C-h |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
79 v' (`describe-variable'). |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
80 |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
81 |
73591
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
82 File: eintr, Node: defvar and asterisk, Prev: See variable current value, Up: defvar |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
83 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
84 8.5.1 `defvar' and an asterisk |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
85 ------------------------------ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
86 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
87 In the past, Emacs used the `defvar' special form both for internal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
88 variables that you would not expect a user to change and for variables |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
89 that you do expect a user to change. Although you can still use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
90 `defvar' for user customizable variables, please use `defcustom' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
91 instead, since that special form provides a path into the Customization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
92 commands. (*Note Specifying Variables using `defcustom': defcustom.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
93 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
94 When you specified a variable using the `defvar' special form, you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
95 could distinguish a readily settable variable from others by typing an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
96 asterisk, `*', in the first column of its documentation string. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
97 example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
98 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
99 (defvar shell-command-default-error-buffer nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
100 "*Buffer name for `shell-command' ... error output. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
101 ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
102 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
103 You could (and still can) use the `set-variable' command to change the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
104 value of `shell-command-default-error-buffer' temporarily. However, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
105 options set using `set-variable' are set only for the duration of your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
106 editing session. The new values are not saved between sessions. Each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
107 time Emacs starts, it reads the original value, unless you change the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
108 value within your `.emacs' file, either by setting it manually or by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
109 using `customize'. *Note Your `.emacs' File: Emacs Initialization. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
110 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
111 For me, the major use of the `set-variable' command is to suggest |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
112 variables that I might want to set in my `.emacs' file. There are now |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
113 more than 700 such variables -- far too many to remember readily. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
114 Fortunately, you can press <TAB> after calling the `M-x set-variable' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
115 command to see the list of variables. (*Note Examining and Setting |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
116 Variables: (emacs)Examining.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
117 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
118 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
119 File: eintr, Node: cons & search-fwd Review, Next: search Exercises, Prev: defvar, Up: Cutting & Storing Text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
120 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
121 8.6 Review |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
122 ========== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
123 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
124 Here is a brief summary of some recently introduced functions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
125 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
126 `car' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
127 `cdr' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
128 `car' returns the first element of a list; `cdr' returns the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
129 second and subsequent elements of a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
130 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
131 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
132 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
133 (car '(1 2 3 4 5 6 7)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
134 => 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
135 (cdr '(1 2 3 4 5 6 7)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
136 => (2 3 4 5 6 7) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
137 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
138 `cons' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
139 `cons' constructs a list by prepending its first argument to its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
140 second argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
141 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
142 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
143 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
144 (cons 1 '(2 3 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
145 => (1 2 3 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
146 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
147 `nthcdr' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
148 Return the result of taking CDR `n' times on a list. The `rest of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
149 the rest', as it were. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
151 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
152 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
153 (nthcdr 3 '(1 2 3 4 5 6 7)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
154 => (4 5 6 7) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
155 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
156 `setcar' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
157 `setcdr' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
158 `setcar' changes the first element of a list; `setcdr' changes the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
159 second and subsequent elements of a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
160 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
161 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
162 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
163 (setq triple '(1 2 3)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
164 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
165 (setcar triple '37) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
166 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
167 triple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
168 => (37 2 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
169 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
170 (setcdr triple '("foo" "bar")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
171 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
172 triple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
173 => (37 "foo" "bar") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
174 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
175 `progn' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
176 Evaluate each argument in sequence and then return the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
177 last. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
178 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
179 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
180 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
181 (progn 1 2 3 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
182 => 4 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
183 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
184 `save-restriction' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
185 Record whatever narrowing is in effect in the current buffer, if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
186 any, and restore that narrowing after evaluating the arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
187 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
188 `search-forward' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
189 Search for a string, and if the string is found, move point. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
190 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
191 Takes four arguments: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
192 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
193 1. The string to search for. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
194 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
195 2. Optionally, the limit of the search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
196 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
197 3. Optionally, what to do if the search fails, return `nil' or an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
198 error message. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
199 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
200 4. Optionally, how many times to repeat the search; if negative, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
201 the search goes backwards. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
202 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
203 `kill-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
204 `delete-and-extract-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
205 `copy-region-as-kill' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
206 `kill-region' cuts the text between point and mark from the buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
207 and stores that text in the kill ring, so you can get it back by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
208 yanking. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
209 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
210 `copy-region-as-kill' copies the text between point and mark into |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
211 the kill ring, from which you can get it by yanking. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
212 does not cut or remove the text from the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
213 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
214 `delete-and-extract-region' removes the text between point and mark |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
215 from the buffer and throws it away. You cannot get it back. (This is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
216 not an interactive command.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
217 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
218 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
219 File: eintr, Node: search Exercises, Prev: cons & search-fwd Review, Up: Cutting & Storing Text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
220 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
221 8.7 Searching Exercises |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
222 ======================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
223 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
224 * Write an interactive function that searches for a string. If the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
225 search finds the string, leave point after it and display a message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
226 that says "Found!". (Do not use `search-forward' for the name of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
227 this function; if you do, you will overwrite the existing version |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
228 of `search-forward' that comes with Emacs. Use a name such as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
229 `test-search' instead.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
230 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
231 * Write a function that prints the third element of the kill ring in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
232 the echo area, if any; if the kill ring does not contain a third |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
233 element, print an appropriate message. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
234 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
235 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
236 File: eintr, Node: List Implementation, Next: Yanking, Prev: Cutting & Storing Text, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
237 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
238 9 How Lists are Implemented |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
239 *************************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
240 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
241 In Lisp, atoms are recorded in a straightforward fashion; if the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
242 implementation is not straightforward in practice, it is, nonetheless, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
243 straightforward in theory. The atom `rose', for example, is recorded |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
244 as the four contiguous letters `r', `o', `s', `e'. A list, on the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
245 other hand, is kept differently. The mechanism is equally simple, but |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
246 it takes a moment to get used to the idea. A list is kept using a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
247 series of pairs of pointers. In the series, the first pointer in each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
248 pair points to an atom or to another list, and the second pointer in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
249 each pair points to the next pair, or to the symbol `nil', which marks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
250 the end of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
251 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
252 A pointer itself is quite simply the electronic address of what is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
253 pointed to. Hence, a list is kept as a series of electronic addresses. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
254 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
255 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
256 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
257 * Lists diagrammed:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
258 * Symbols as Chest:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
259 * List Exercise:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
260 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
261 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
262 File: eintr, Node: Lists diagrammed, Next: Symbols as Chest, Prev: List Implementation, Up: List Implementation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
263 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
264 Lists diagrammed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
265 ================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
266 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
267 For example, the list `(rose violet buttercup)' has three elements, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
268 `rose', `violet', and `buttercup'. In the computer, the electronic |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
269 address of `rose' is recorded in a segment of computer memory along |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
270 with the address that gives the electronic address of where the atom |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
271 `violet' is located; and that address (the one that tells where |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
272 `violet' is located) is kept along with an address that tells where the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
273 address for the atom `buttercup' is located. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
274 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
275 This sounds more complicated than it is and is easier seen in a diagram: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
276 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
277 ___ ___ ___ ___ ___ ___ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
278 |___|___|--> |___|___|--> |___|___|--> nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
279 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
280 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
281 --> rose --> violet --> buttercup |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
282 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
283 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
284 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
285 In the diagram, each box represents a word of computer memory that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
286 holds a Lisp object, usually in the form of a memory address. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
287 boxes, i.e. the addresses, are in pairs. Each arrow points to what the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
288 address is the address of, either an atom or another pair of addresses. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
289 The first box is the electronic address of `rose' and the arrow points |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
290 to `rose'; the second box is the address of the next pair of boxes, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
291 first part of which is the address of `violet' and the second part of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
292 which is the address of the next pair. The very last box points to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
293 symbol `nil', which marks the end of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
294 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
295 When a variable is set to a list with a function such as `setq', it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
296 stores the address of the first box in the variable. Thus, evaluation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
297 of the expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
298 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
299 (setq bouquet '(rose violet buttercup)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
300 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
301 creates a situation like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
302 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
303 bouquet |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
304 | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
305 | ___ ___ ___ ___ ___ ___ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
306 --> |___|___|--> |___|___|--> |___|___|--> nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
307 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
308 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
309 --> rose --> violet --> buttercup |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
310 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
311 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
312 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
313 In this example, the symbol `bouquet' holds the address of the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
314 pair of boxes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
315 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
316 This same list can be illustrated in a different sort of box notation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
317 like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
318 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
319 bouquet |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
320 | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
321 | -------------- --------------- ---------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
322 | | car | cdr | | car | cdr | | car | cdr | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
323 -->| rose | o------->| violet | o------->| butter- | nil | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
324 | | | | | | | cup | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
325 -------------- --------------- ---------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
326 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
327 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
328 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
329 (Symbols consist of more than pairs of addresses, but the structure of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
330 a symbol is made up of addresses. Indeed, the symbol `bouquet' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
331 consists of a group of address-boxes, one of which is the address of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
332 the printed word `bouquet', a second of which is the address of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
333 function definition attached to the symbol, if any, a third of which is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
334 the address of the first pair of address-boxes for the list `(rose |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
335 violet buttercup)', and so on. Here we are showing that the symbol's |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
336 third address-box points to the first pair of address-boxes for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
337 list.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
338 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
339 If a symbol is set to the CDR of a list, the list itself is not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
340 changed; the symbol simply has an address further down the list. (In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
341 the jargon, CAR and CDR are `non-destructive'.) Thus, evaluation of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
342 the following expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
344 (setq flowers (cdr bouquet)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
345 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
346 produces this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
348 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
349 bouquet flowers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
350 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
351 | ___ ___ | ___ ___ ___ ___ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
352 --> | | | --> | | | | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
353 |___|___|----> |___|___|--> |___|___|--> nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
354 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
355 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
356 --> rose --> violet --> buttercup |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
357 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
358 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
359 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
360 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
361 The value of `flowers' is `(violet buttercup)', which is to say, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
362 symbol `flowers' holds the address of the pair of address-boxes, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
363 first of which holds the address of `violet', and the second of which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
364 holds the address of `buttercup'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
365 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
366 A pair of address-boxes is called a "cons cell" or "dotted pair". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
367 *Note Cons Cell and List Types: (elisp)Cons Cell Type, and *Note Dotted |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
368 Pair Notation: (elisp)Dotted Pair Notation, for more information about |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
369 cons cells and dotted pairs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
370 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
371 The function `cons' adds a new pair of addresses to the front of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
372 series of addresses like that shown above. For example, evaluating the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
373 expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
374 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
375 (setq bouquet (cons 'lily bouquet)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
376 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
377 produces: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
378 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
379 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
380 bouquet flowers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
381 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
382 | ___ ___ ___ ___ | ___ ___ ___ ___ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
383 --> | | | | | | --> | | | | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
384 |___|___|----> |___|___|----> |___|___|---->|___|___|--> nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
385 | | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
386 | | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
387 --> lily --> rose --> violet --> buttercup |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
388 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
389 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
390 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
391 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
392 However, this does not change the value of the symbol `flowers', as you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
393 can see by evaluating the following, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
394 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
395 (eq (cdr (cdr bouquet)) flowers) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
396 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
397 which returns `t' for true. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
398 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
399 Until it is reset, `flowers' still has the value `(violet buttercup)'; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
400 that is, it has the address of the cons cell whose first address is of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
401 `violet'. Also, this does not alter any of the pre-existing cons |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
402 cells; they are all still there. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
403 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
404 Thus, in Lisp, to get the CDR of a list, you just get the address of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
405 the next cons cell in the series; to get the CAR of a list, you get the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
406 address of the first element of the list; to `cons' a new element on a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
407 list, you add a new cons cell to the front of the list. That is all |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
408 there is to it! The underlying structure of Lisp is brilliantly simple! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
409 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
410 And what does the last address in a series of cons cells refer to? It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
411 is the address of the empty list, of `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
412 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
413 In summary, when a Lisp variable is set to a value, it is provided with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
414 the address of the list to which the variable refers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
415 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
416 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
417 File: eintr, Node: Symbols as Chest, Next: List Exercise, Prev: Lists diagrammed, Up: List Implementation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
418 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
419 9.1 Symbols as a Chest of Drawers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
420 ================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
421 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
422 In an earlier section, I suggested that you might imagine a symbol as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
423 being a chest of drawers. The function definition is put in one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
424 drawer, the value in another, and so on. What is put in the drawer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
425 holding the value can be changed without affecting the contents of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
426 drawer holding the function definition, and vice-verse. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
427 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
428 Actually, what is put in each drawer is the address of the value or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
429 function definition. It is as if you found an old chest in the attic, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
430 and in one of its drawers you found a map giving you directions to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
431 where the buried treasure lies. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
432 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
433 (In addition to its name, symbol definition, and variable value, a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
434 symbol has a `drawer' for a "property list" which can be used to record |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
435 other information. Property lists are not discussed here; see *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
436 Property Lists: (elisp)Property Lists.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
437 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
438 Here is a fanciful representation: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
439 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
440 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
441 Chest of Drawers Contents of Drawers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
442 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
443 __ o0O0o __ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
444 / \ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
445 --------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
446 | directions to | [map to] |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
447 | symbol name | bouquet |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
448 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
449 +---------------------+ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
450 | directions to | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
451 | symbol definition | [none] |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
452 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
453 +---------------------+ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
454 | directions to | [map to] |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
455 | variable value | (rose violet buttercup) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
456 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
457 +---------------------+ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
458 | directions to | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
459 | property list | [not described here] |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
460 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
461 +---------------------+ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
462 |/ \| |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
463 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
464 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
465 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
466 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
467 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
468 File: eintr, Node: List Exercise, Prev: Symbols as Chest, Up: List Implementation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
469 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
470 9.2 Exercise |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
471 ============ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
472 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
473 Set `flowers' to `violet' and `buttercup'. Cons two more flowers on to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
474 this list and set this new list to `more-flowers'. Set the CAR of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
475 `flowers' to a fish. What does the `more-flowers' list now contain? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
476 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
477 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
478 File: eintr, Node: Yanking, Next: Loops & Recursion, Prev: List Implementation, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
479 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
480 10 Yanking Text Back |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
481 ******************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
482 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
483 Whenever you cut text out of a buffer with a `kill' command in GNU |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
484 Emacs, you can bring it back with a `yank' command. The text that is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
485 cut out of the buffer is put in the kill ring and the yank commands |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
486 insert the appropriate contents of the kill ring back into a buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
487 (not necessarily the original buffer). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
488 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
489 A simple `C-y' (`yank') command inserts the first item from the kill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
490 ring into the current buffer. If the `C-y' command is followed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
491 immediately by `M-y', the first element is replaced by the second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
492 element. Successive `M-y' commands replace the second element with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
493 third, fourth, or fifth element, and so on. When the last element in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
494 the kill ring is reached, it is replaced by the first element and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
495 cycle is repeated. (Thus the kill ring is called a `ring' rather than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
496 just a `list'. However, the actual data structure that holds the text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
497 is a list. *Note Handling the Kill Ring: Kill Ring, for the details of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
498 how the list is handled as a ring.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
499 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
500 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
501 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
502 * Kill Ring Overview:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
503 * kill-ring-yank-pointer:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
504 * yank nthcdr Exercises:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
505 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
506 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
507 File: eintr, Node: Kill Ring Overview, Next: kill-ring-yank-pointer, Prev: Yanking, Up: Yanking |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
508 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
509 10.1 Kill Ring Overview |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
510 ======================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
511 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
512 The kill ring is a list of textual strings. This is what it looks like: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
513 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
514 ("some text" "a different piece of text" "yet more text") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
515 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
516 If this were the contents of my kill ring and I pressed `C-y', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
517 string of characters saying `some text' would be inserted in this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
518 buffer where my cursor is located. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
519 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
520 The `yank' command is also used for duplicating text by copying it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
521 The copied text is not cut from the buffer, but a copy of it is put on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
522 the kill ring and is inserted by yanking it back. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
523 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
524 Three functions are used for bringing text back from the kill ring: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
525 `yank', which is usually bound to `C-y'; `yank-pop', which is usually |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
526 bound to `M-y'; and `rotate-yank-pointer', which is used by the two |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
527 other functions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
528 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
529 These functions refer to the kill ring through a variable called the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
530 `kill-ring-yank-pointer'. Indeed, the insertion code for both the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
531 `yank' and `yank-pop' functions is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
532 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
533 (insert (car kill-ring-yank-pointer)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
534 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
535 (Well, no more. In GNU Emacs 22, the function has been replaced by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
536 `insert-for-yank' which calls `insert-for-yank-1' repetitively for each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
537 `yank-handler' segment. In turn, `insert-for-yank-1' strips text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
538 properties from the inserted text according to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
539 `yank-excluded-properties'. Otherwise, it is just like `insert'. We |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
540 will stick with plain `insert' since it is easier to understand.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
541 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
542 To begin to understand how `yank' and `yank-pop' work, it is first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
543 necessary to look at the `kill-ring-yank-pointer' variable and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
544 `rotate-yank-pointer' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
545 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
546 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
547 File: eintr, Node: kill-ring-yank-pointer, Next: yank nthcdr Exercises, Prev: Kill Ring Overview, Up: Yanking |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
548 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
549 10.2 The `kill-ring-yank-pointer' Variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
550 ========================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
551 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
552 `kill-ring-yank-pointer' is a variable, just as `kill-ring' is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
553 variable. It points to something by being bound to the value of what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
554 it points to, like any other Lisp variable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
555 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
556 Thus, if the value of the kill ring is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
557 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
558 ("some text" "a different piece of text" "yet more text") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
559 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
560 and the `kill-ring-yank-pointer' points to the second clause, the value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
561 of `kill-ring-yank-pointer' is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
562 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
563 ("a different piece of text" "yet more text") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
564 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
565 As explained in the previous chapter (*note List Implementation::), the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
566 computer does not keep two different copies of the text being pointed to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
567 by both the `kill-ring' and the `kill-ring-yank-pointer'. The words "a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
568 different piece of text" and "yet more text" are not duplicated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
569 Instead, the two Lisp variables point to the same pieces of text. Here |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
570 is a diagram: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
571 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
572 kill-ring kill-ring-yank-pointer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
573 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
574 | ___ ___ | ___ ___ ___ ___ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
575 ---> | | | --> | | | | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
576 |___|___|----> |___|___|--> |___|___|--> nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
577 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
578 | | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
579 | | --> "yet more text" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
580 | | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
581 | --> "a different piece of text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
582 | |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
583 --> "some text" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
584 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
585 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
586 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
587 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
588 Both the variable `kill-ring' and the variable `kill-ring-yank-pointer' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
589 are pointers. But the kill ring itself is usually described as if it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
590 were actually what it is composed of. The `kill-ring' is spoken of as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
591 if it were the list rather than that it points to the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
592 Conversely, the `kill-ring-yank-pointer' is spoken of as pointing to a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
593 list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
594 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
595 These two ways of talking about the same thing sound confusing at first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
596 but make sense on reflection. The kill ring is generally thought of as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
597 the complete structure of data that holds the information of what has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
598 recently been cut out of the Emacs buffers. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
599 `kill-ring-yank-pointer' on the other hand, serves to indicate--that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
600 is, to `point to'--that part of the kill ring of which the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
601 element (the CAR) will be inserted. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
602 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
603 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
604 File: eintr, Node: yank nthcdr Exercises, Prev: kill-ring-yank-pointer, Up: Yanking |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
605 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
606 10.3 Exercises with `yank' and `nthcdr' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
607 ======================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
608 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
609 * Using `C-h v' (`describe-variable'), look at the value of your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
610 kill ring. Add several items to your kill ring; look at its value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
611 again. Using `M-y' (`yank-pop)', move all the way around the kill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
612 ring. How many items were in your kill ring? Find the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
613 `kill-ring-max'. Was your kill ring full, or could you have kept |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
614 more blocks of text within it? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
615 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
616 * Using `nthcdr' and `car', construct a series of expressions to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
617 return the first, second, third, and fourth elements of a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
618 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
619 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
620 File: eintr, Node: Loops & Recursion, Next: Regexp Search, Prev: Yanking, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
621 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
622 11 Loops and Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
623 ********************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
624 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
625 Emacs Lisp has two primary ways to cause an expression, or a series of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
626 expressions, to be evaluated repeatedly: one uses a `while' loop, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
627 the other uses "recursion". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
628 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
629 Repetition can be very valuable. For example, to move forward four |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
630 sentences, you need only write a program that will move forward one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
631 sentence and then repeat the process four times. Since a computer does |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
632 not get bored or tired, such repetitive action does not have the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
633 deleterious effects that excessive or the wrong kinds of repetition can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
634 have on humans. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
635 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
636 People mostly write Emacs Lisp functions using `while' loops and their |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
637 kin; but you can use recursion, which provides a very powerful way to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
638 think about and then to solve problems(1). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
639 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
640 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
641 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
642 * while:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
643 * dolist dotimes:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
644 * Recursion:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
645 * Looping exercise:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
646 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
647 ---------- Footnotes ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
648 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
649 (1) You can write recursive functions to be frugal or wasteful of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
650 mental or computer resources; as it happens, methods that people find |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
651 easy--that are frugal of `mental resources'--sometimes use considerable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
652 computer resources. Emacs was designed to run on machines that we now |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
653 consider limited and its default settings are conservative. You may |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
654 want to increase the values of `max-specpdl-size' and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
655 `max-lisp-eval-depth'. In my `.emacs' file, I set them to 15 and 30 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
656 times their default value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
657 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
658 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
659 File: eintr, Node: while, Next: dolist dotimes, Prev: Loops & Recursion, Up: Loops & Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
660 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
661 11.1 `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
662 ============ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
663 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
664 The `while' special form tests whether the value returned by evaluating |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
665 its first argument is true or false. This is similar to what the Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
666 interpreter does with an `if'; what the interpreter does next, however, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
667 is different. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
668 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
669 In a `while' expression, if the value returned by evaluating the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
670 argument is false, the Lisp interpreter skips the rest of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
671 expression (the "body" of the expression) and does not evaluate it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
672 However, if the value is true, the Lisp interpreter evaluates the body |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
673 of the expression and then again tests whether the first argument to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
674 `while' is true or false. If the value returned by evaluating the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
675 first argument is again true, the Lisp interpreter again evaluates the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
676 body of the expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
677 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
678 The template for a `while' expression looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
679 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
680 (while TRUE-OR-FALSE-TEST |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
681 BODY...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
682 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
683 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
684 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
685 * Looping with while:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
686 * Loop Example:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
687 * print-elements-of-list:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
688 * Incrementing Loop:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
689 * Decrementing Loop:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
690 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
691 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
692 File: eintr, Node: Looping with while, Next: Loop Example, Prev: while, Up: while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
693 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
694 Looping with `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
695 -------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
696 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
697 So long as the true-or-false-test of the `while' expression returns a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
698 true value when it is evaluated, the body is repeatedly evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
699 This process is called a loop since the Lisp interpreter repeats the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
700 same thing again and again, like an airplane doing a loop. When the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
701 result of evaluating the true-or-false-test is false, the Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
702 interpreter does not evaluate the rest of the `while' expression and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
703 `exits the loop'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
704 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
705 Clearly, if the value returned by evaluating the first argument to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
706 `while' is always true, the body following will be evaluated again and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
707 again ... and again ... forever. Conversely, if the value returned is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
708 never true, the expressions in the body will never be evaluated. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
709 craft of writing a `while' loop consists of choosing a mechanism such |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
710 that the true-or-false-test returns true just the number of times that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
711 you want the subsequent expressions to be evaluated, and then have the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
712 test return false. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
713 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
714 The value returned by evaluating a `while' is the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
715 true-or-false-test. An interesting consequence of this is that a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
716 `while' loop that evaluates without error will return `nil' or false |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
717 regardless of whether it has looped 1 or 100 times or none at all. A |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
718 `while' expression that evaluates successfully never returns a true |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
719 value! What this means is that `while' is always evaluated for its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
720 side effects, which is to say, the consequences of evaluating the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
721 expressions within the body of the `while' loop. This makes sense. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
722 is not the mere act of looping that is desired, but the consequences of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
723 what happens when the expressions in the loop are repeatedly evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
724 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
725 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
726 File: eintr, Node: Loop Example, Next: print-elements-of-list, Prev: Looping with while, Up: while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
727 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
728 11.1.1 A `while' Loop and a List |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
729 -------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
730 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
731 A common way to control a `while' loop is to test whether a list has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
732 any elements. If it does, the loop is repeated; but if it does not, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
733 the repetition is ended. Since this is an important technique, we will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
734 create a short example to illustrate it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
735 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
736 A simple way to test whether a list has elements is to evaluate the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
737 list: if it has no elements, it is an empty list and will return the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
738 empty list, `()', which is a synonym for `nil' or false. On the other |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
739 hand, a list with elements will return those elements when it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
740 evaluated. Since Emacs Lisp considers as true any value that is not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
741 `nil', a list that returns elements will test true in a `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
742 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
743 For example, you can set the variable `empty-list' to `nil' by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
744 evaluating the following `setq' expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
745 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
746 (setq empty-list ()) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
747 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
748 After evaluating the `setq' expression, you can evaluate the variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
749 `empty-list' in the usual way, by placing the cursor after the symbol |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
750 and typing `C-x C-e'; `nil' will appear in your echo area: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
751 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
752 empty-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
753 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
754 On the other hand, if you set a variable to be a list with elements, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
755 list will appear when you evaluate the variable, as you can see by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
756 evaluating the following two expressions: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
757 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
758 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
759 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
760 animals |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
761 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
762 Thus, to create a `while' loop that tests whether there are any items |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
763 in the list `animals', the first part of the loop will be written like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
764 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
765 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
766 (while animals |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
767 ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
768 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
769 When the `while' tests its first argument, the variable `animals' is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
770 evaluated. It returns a list. So long as the list has elements, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
771 `while' considers the results of the test to be true; but when the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
772 is empty, it considers the results of the test to be false. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
773 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
774 To prevent the `while' loop from running forever, some mechanism needs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
775 to be provided to empty the list eventually. An oft-used technique is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
776 to have one of the subsequent forms in the `while' expression set the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
777 value of the list to be the CDR of the list. Each time the `cdr' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
778 function is evaluated, the list will be made shorter, until eventually |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
779 only the empty list will be left. At this point, the test of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
780 `while' loop will return false, and the arguments to the `while' will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
781 no longer be evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
782 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
783 For example, the list of animals bound to the variable `animals' can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
784 set to be the CDR of the original list with the following expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
785 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
786 (setq animals (cdr animals)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
787 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
788 If you have evaluated the previous expressions and then evaluate this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
789 expression, you will see `(giraffe lion tiger)' appear in the echo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
790 area. If you evaluate the expression again, `(lion tiger)' will appear |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
791 in the echo area. If you evaluate it again and yet again, `(tiger)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
792 appears and then the empty list, shown by `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
793 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
794 A template for a `while' loop that uses the `cdr' function repeatedly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
795 to cause the true-or-false-test eventually to test false looks like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
796 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
797 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
798 (while TEST-WHETHER-LIST-IS-EMPTY |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
799 BODY... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
800 SET-LIST-TO-CDR-OF-LIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
801 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
802 This test and use of `cdr' can be put together in a function that goes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
803 through a list and prints each element of the list on a line of its own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
804 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
805 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
806 File: eintr, Node: print-elements-of-list, Next: Incrementing Loop, Prev: Loop Example, Up: while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
807 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
808 11.1.2 An Example: `print-elements-of-list' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
809 ------------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
810 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
811 The `print-elements-of-list' function illustrates a `while' loop with a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
812 list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
813 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
814 The function requires several lines for its output. If you are reading |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
815 this in a recent instance of GNU Emacs, you can evaluate the following |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
816 expression inside of Info, as usual. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
817 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
818 If you are using an earlier version of Emacs, you need to copy the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
819 necessary expressions to your `*scratch*' buffer and evaluate them |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
820 there. This is because the echo area had only one line in the earlier |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
821 versions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
822 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
823 You can copy the expressions by marking the beginning of the region |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
824 with `C-<SPC>' (`set-mark-command'), moving the cursor to the end of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
825 the region and then copying the region using `M-w' (`kill-ring-save', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
826 which calls `copy-region-as-kill' and then provides visual feedback). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
827 In the `*scratch*' buffer, you can yank the expressions back by typing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
828 `C-y' (`yank'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
829 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
830 After you have copied the expressions to the `*scratch*' buffer, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
831 evaluate each expression in turn. Be sure to evaluate the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
832 expression, `(print-elements-of-list animals)', by typing `C-u C-x |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
833 C-e', that is, by giving an argument to `eval-last-sexp'. This will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
834 cause the result of the evaluation to be printed in the `*scratch*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
835 buffer instead of being printed in the echo area. (Otherwise you will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
836 see something like this in your echo area: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
837 `^Jgazelle^J^Jgiraffe^J^Jlion^J^Jtiger^Jnil', in which each `^J' stands |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
838 for a `newline'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
839 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
840 In a recent instance of GNU Emacs, you can evaluate these expressions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
841 directly in the Info buffer, and the echo area will grow to show the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
842 results. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
843 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
844 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
845 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
846 (defun print-elements-of-list (list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
847 "Print each element of LIST on a line of its own." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
848 (while list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
849 (print (car list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
850 (setq list (cdr list)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
851 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
852 (print-elements-of-list animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
853 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
854 When you evaluate the three expressions in sequence, you will see this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
855 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
856 gazelle |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
857 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
858 giraffe |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
859 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
860 lion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
861 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
862 tiger |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
863 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
864 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
865 Each element of the list is printed on a line of its own (that is what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
866 the function `print' does) and then the value returned by the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
867 is printed. Since the last expression in the function is the `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
868 loop, and since `while' loops always return `nil', a `nil' is printed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
869 after the last element of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
870 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
871 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
872 File: eintr, Node: Incrementing Loop, Next: Decrementing Loop, Prev: print-elements-of-list, Up: while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
873 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
874 11.1.3 A Loop with an Incrementing Counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
875 ------------------------------------------ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
876 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
877 A loop is not useful unless it stops when it ought. Besides |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
878 controlling a loop with a list, a common way of stopping a loop is to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
879 write the first argument as a test that returns false when the correct |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
880 number of repetitions are complete. This means that the loop must have |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
881 a counter--an expression that counts how many times the loop repeats |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
882 itself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
883 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
884 The test can be an expression such as `(< count desired-number)' which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
885 returns `t' for true if the value of `count' is less than the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
886 `desired-number' of repetitions and `nil' for false if the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
887 `count' is equal to or is greater than the `desired-number'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
888 expression that increments the count can be a simple `setq' such as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
889 `(setq count (1+ count))', where `1+' is a built-in function in Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
890 Lisp that adds 1 to its argument. (The expression `(1+ count)' has the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
891 same result as `(+ count 1)', but is easier for a human to read.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
892 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
893 The template for a `while' loop controlled by an incrementing counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
894 looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
895 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
896 SET-COUNT-TO-INITIAL-VALUE |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
897 (while (< count desired-number) ; true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
898 BODY... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
899 (setq count (1+ count))) ; incrementer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
900 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
901 Note that you need to set the initial value of `count'; usually it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
902 set to 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
903 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
904 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
905 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
906 * Incrementing Example:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
907 * Inc Example parts:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
908 * Inc Example altogether:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
909 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
910 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
911 File: eintr, Node: Incrementing Example, Next: Inc Example parts, Prev: Incrementing Loop, Up: Incrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
912 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
913 Example with incrementing counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
914 ................................. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
915 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
916 Suppose you are playing on the beach and decide to make a triangle of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
917 pebbles, putting one pebble in the first row, two in the second row, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
918 three in the third row and so on, like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
919 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
920 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
921 * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
922 * * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
923 * * * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
924 * * * * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
925 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
926 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
927 (About 2500 years ago, Pythagoras and others developed the beginnings of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
928 number theory by considering questions such as this.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
929 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
930 Suppose you want to know how many pebbles you will need to make a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
931 triangle with 7 rows? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
932 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
933 Clearly, what you need to do is add up the numbers from 1 to 7. There |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
934 are two ways to do this; start with the smallest number, one, and add up |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
935 the list in sequence, 1, 2, 3, 4 and so on; or start with the largest |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
936 number and add the list going down: 7, 6, 5, 4 and so on. Because both |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
937 mechanisms illustrate common ways of writing `while' loops, we will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
938 create two examples, one counting up and the other counting down. In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
939 this first example, we will start with 1 and add 2, 3, 4 and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
940 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
941 If you are just adding up a short list of numbers, the easiest way to do |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
942 it is to add up all the numbers at once. However, if you do not know |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
943 ahead of time how many numbers your list will have, or if you want to be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
944 prepared for a very long list, then you need to design your addition so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
945 that what you do is repeat a simple process many times instead of doing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
946 a more complex process once. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
947 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
948 For example, instead of adding up all the pebbles all at once, what you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
949 can do is add the number of pebbles in the first row, 1, to the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
950 in the second row, 2, and then add the total of those two rows to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
951 third row, 3. Then you can add the number in the fourth row, 4, to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
952 total of the first three rows; and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
953 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
954 The critical characteristic of the process is that each repetitive |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
955 action is simple. In this case, at each step we add only two numbers, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
956 the number of pebbles in the row and the total already found. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
957 process of adding two numbers is repeated again and again until the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
958 row has been added to the total of all the preceding rows. In a more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
959 complex loop the repetitive action might not be so simple, but it will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
960 be simpler than doing everything all at once. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
961 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
962 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
963 File: eintr, Node: Inc Example parts, Next: Inc Example altogether, Prev: Incrementing Example, Up: Incrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
964 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
965 The parts of the function definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
966 .................................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
968 The preceding analysis gives us the bones of our function definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
969 first, we will need a variable that we can call `total' that will be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
970 the total number of pebbles. This will be the value returned by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
971 function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
972 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
973 Second, we know that the function will require an argument: this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
974 argument will be the total number of rows in the triangle. It can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
975 called `number-of-rows'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
976 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
977 Finally, we need a variable to use as a counter. We could call this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
978 variable `counter', but a better name is `row-number'. That is because |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
979 what the counter does in this function is count rows, and a program |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
980 should be written to be as understandable as possible. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
981 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
982 When the Lisp interpreter first starts evaluating the expressions in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
983 function, the value of `total' should be set to zero, since we have not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
984 added anything to it. Then the function should add the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
985 pebbles in the first row to the total, and then add the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
986 pebbles in the second to the total, and then add the number of pebbles |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
987 in the third row to the total, and so on, until there are no more rows |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
988 left to add. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
989 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
990 Both `total' and `row-number' are used only inside the function, so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
991 they can be declared as local variables with `let' and given initial |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
992 values. Clearly, the initial value for `total' should be 0. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
993 initial value of `row-number' should be 1, since we start with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
994 first row. This means that the `let' statement will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
995 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
996 (let ((total 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
997 (row-number 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
998 BODY...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
999 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1000 After the internal variables are declared and bound to their initial |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1001 values, we can begin the `while' loop. The expression that serves as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1002 the test should return a value of `t' for true so long as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1003 `row-number' is less than or equal to the `number-of-rows'. (If the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1004 expression tests true only so long as the row number is less than the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1005 number of rows in the triangle, the last row will never be added to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1006 total; hence the row number has to be either less than or equal to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1007 number of rows.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1008 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1009 Lisp provides the `<=' function that returns true if the value of its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1010 first argument is less than or equal to the value of its second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1011 argument and false otherwise. So the expression that the `while' will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1012 evaluate as its test should look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1013 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1014 (<= row-number number-of-rows) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1015 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1016 The total number of pebbles can be found by repeatedly adding the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1017 of pebbles in a row to the total already found. Since the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1018 pebbles in the row is equal to the row number, the total can be found by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1019 adding the row number to the total. (Clearly, in a more complex |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1020 situation, the number of pebbles in the row might be related to the row |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1021 number in a more complicated way; if this were the case, the row number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1022 would be replaced by the appropriate expression.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1023 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1024 (setq total (+ total row-number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1025 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1026 What this does is set the new value of `total' to be equal to the sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1027 of adding the number of pebbles in the row to the previous total. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1028 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1029 After setting the value of `total', the conditions need to be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1030 established for the next repetition of the loop, if there is one. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1031 is done by incrementing the value of the `row-number' variable, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1032 serves as a counter. After the `row-number' variable has been |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1033 incremented, the true-or-false-test at the beginning of the `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1034 loop tests whether its value is still less than or equal to the value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1035 of the `number-of-rows' and if it is, adds the new value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1036 `row-number' variable to the `total' of the previous repetition of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1037 loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1038 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1039 The built-in Emacs Lisp function `1+' adds 1 to a number, so the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1040 `row-number' variable can be incremented with this expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1041 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1042 (setq row-number (1+ row-number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1043 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1044 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1045 File: eintr, Node: Inc Example altogether, Prev: Inc Example parts, Up: Incrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1046 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1047 Putting the function definition together |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1048 ........................................ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1049 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1050 We have created the parts for the function definition; now we need to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1051 put them together. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1052 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1053 First, the contents of the `while' expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1054 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1055 (while (<= row-number number-of-rows) ; true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1056 (setq total (+ total row-number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1057 (setq row-number (1+ row-number))) ; incrementer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1058 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1059 Along with the `let' expression varlist, this very nearly completes the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1060 body of the function definition. However, it requires one final |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1061 element, the need for which is somewhat subtle. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1062 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1063 The final touch is to place the variable `total' on a line by itself |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1064 after the `while' expression. Otherwise, the value returned by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1065 whole function is the value of the last expression that is evaluated in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1066 the body of the `let', and this is the value returned by the `while', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1067 which is always `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1068 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1069 This may not be evident at first sight. It almost looks as if the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1070 incrementing expression is the last expression of the whole function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1071 But that expression is part of the body of the `while'; it is the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1072 element of the list that starts with the symbol `while'. Moreover, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1073 whole of the `while' loop is a list within the body of the `let'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1074 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1075 In outline, the function will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1076 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1077 (defun NAME-OF-FUNCTION (ARGUMENT-LIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1078 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1079 (let (VARLIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1080 (while (TRUE-OR-FALSE-TEST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1081 BODY-OF-WHILE... ) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1082 ... )) ; Need final expression here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1083 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1084 The result of evaluating the `let' is what is going to be returned by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1085 the `defun' since the `let' is not embedded within any containing list, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1086 except for the `defun' as a whole. However, if the `while' is the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1087 element of the `let' expression, the function will always return `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1088 This is not what we want! Instead, what we want is the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1089 variable `total'. This is returned by simply placing the symbol as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1090 last element of the list starting with `let'. It gets evaluated after |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1091 the preceding elements of the list are evaluated, which means it gets |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1092 evaluated after it has been assigned the correct value for the total. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1093 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1094 It may be easier to see this by printing the list starting with `let' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1095 all on one line. This format makes it evident that the VARLIST and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1096 `while' expressions are the second and third elements of the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1097 starting with `let', and the `total' is the last element: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1098 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1099 (let (VARLIST) (while (TRUE-OR-FALSE-TEST) BODY-OF-WHILE... ) total) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1100 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1101 Putting everything together, the `triangle' function definition looks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1102 like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1103 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1104 (defun triangle (number-of-rows) ; Version with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1105 ; incrementing counter. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1106 "Add up the number of pebbles in a triangle. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1107 The first row has one pebble, the second row two pebbles, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1108 the third row three pebbles, and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1109 The argument is NUMBER-OF-ROWS." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1110 (let ((total 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1111 (row-number 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1112 (while (<= row-number number-of-rows) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1113 (setq total (+ total row-number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1114 (setq row-number (1+ row-number))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1115 total)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1116 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1117 After you have installed `triangle' by evaluating the function, you can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1118 try it out. Here are two examples: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1119 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1120 (triangle 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1121 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1122 (triangle 7) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1123 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1124 The sum of the first four numbers is 10 and the sum of the first seven |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1125 numbers is 28. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1126 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1127 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1128 File: eintr, Node: Decrementing Loop, Prev: Incrementing Loop, Up: while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1129 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1130 11.1.4 Loop with a Decrementing Counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1131 --------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1132 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1133 Another common way to write a `while' loop is to write the test so that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1134 it determines whether a counter is greater than zero. So long as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1135 counter is greater than zero, the loop is repeated. But when the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1136 counter is equal to or less than zero, the loop is stopped. For this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1137 to work, the counter has to start out greater than zero and then be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1138 made smaller and smaller by a form that is evaluated repeatedly. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1139 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1140 The test will be an expression such as `(> counter 0)' which returns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1141 `t' for true if the value of `counter' is greater than zero, and `nil' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1142 for false if the value of `counter' is equal to or less than zero. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1143 expression that makes the number smaller and smaller can be a simple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1144 `setq' such as `(setq counter (1- counter))', where `1-' is a built-in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1145 function in Emacs Lisp that subtracts 1 from its argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1146 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1147 The template for a decrementing `while' loop looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1148 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1149 (while (> counter 0) ; true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1150 BODY... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1151 (setq counter (1- counter))) ; decrementer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1152 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1153 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1154 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1155 * Decrementing Example:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1156 * Dec Example parts:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1157 * Dec Example altogether:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1158 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1159 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1160 File: eintr, Node: Decrementing Example, Next: Dec Example parts, Prev: Decrementing Loop, Up: Decrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1161 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1162 Example with decrementing counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1163 ................................. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1164 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1165 To illustrate a loop with a decrementing counter, we will rewrite the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1166 `triangle' function so the counter decreases to zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1167 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1168 This is the reverse of the earlier version of the function. In this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1169 case, to find out how many pebbles are needed to make a triangle with 3 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1170 rows, add the number of pebbles in the third row, 3, to the number in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1171 the preceding row, 2, and then add the total of those two rows to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1172 row that precedes them, which is 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1173 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1174 Likewise, to find the number of pebbles in a triangle with 7 rows, add |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1175 the number of pebbles in the seventh row, 7, to the number in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1176 preceding row, which is 6, and then add the total of those two rows to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1177 the row that precedes them, which is 5, and so on. As in the previous |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1178 example, each addition only involves adding two numbers, the total of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1179 the rows already added up and the number of pebbles in the row that is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1180 being added to the total. This process of adding two numbers is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1181 repeated again and again until there are no more pebbles to add. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1182 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1183 We know how many pebbles to start with: the number of pebbles in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1184 last row is equal to the number of rows. If the triangle has seven |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1185 rows, the number of pebbles in the last row is 7. Likewise, we know how |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1186 many pebbles are in the preceding row: it is one less than the number in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1187 the row. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1188 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1189 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1190 File: eintr, Node: Dec Example parts, Next: Dec Example altogether, Prev: Decrementing Example, Up: Decrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1191 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1192 The parts of the function definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1193 .................................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1194 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1195 We start with three variables: the total number of rows in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1196 triangle; the number of pebbles in a row; and the total number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1197 pebbles, which is what we want to calculate. These variables can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1198 named `number-of-rows', `number-of-pebbles-in-row', and `total', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1199 respectively. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1200 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1201 Both `total' and `number-of-pebbles-in-row' are used only inside the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1202 function and are declared with `let'. The initial value of `total' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1203 should, of course, be zero. However, the initial value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1204 `number-of-pebbles-in-row' should be equal to the number of rows in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1205 triangle, since the addition will start with the longest row. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1206 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1207 This means that the beginning of the `let' expression will look like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1208 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1209 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1210 (let ((total 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1211 (number-of-pebbles-in-row number-of-rows)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1212 BODY...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1213 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1214 The total number of pebbles can be found by repeatedly adding the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1215 of pebbles in a row to the total already found, that is, by repeatedly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1216 evaluating the following expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1217 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1218 (setq total (+ total number-of-pebbles-in-row)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1219 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1220 After the `number-of-pebbles-in-row' is added to the `total', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1221 `number-of-pebbles-in-row' should be decremented by one, since the next |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1222 time the loop repeats, the preceding row will be added to the total. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1223 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1224 The number of pebbles in a preceding row is one less than the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1225 pebbles in a row, so the built-in Emacs Lisp function `1-' can be used |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1226 to compute the number of pebbles in the preceding row. This can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1227 done with the following expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1228 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1229 (setq number-of-pebbles-in-row |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1230 (1- number-of-pebbles-in-row)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1231 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1232 Finally, we know that the `while' loop should stop making repeated |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1233 additions when there are no pebbles in a row. So the test for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1234 `while' loop is simply: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1235 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1236 (while (> number-of-pebbles-in-row 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1237 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1238 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1239 File: eintr, Node: Dec Example altogether, Prev: Dec Example parts, Up: Decrementing Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1240 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1241 Putting the function definition together |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1242 ........................................ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1243 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1244 We can put these expressions together to create a function definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1245 that works. However, on examination, we find that one of the local |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1246 variables is unneeded! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1247 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1248 The function definition looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1249 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1250 ;;; First subtractive version. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1251 (defun triangle (number-of-rows) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1252 "Add up the number of pebbles in a triangle." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1253 (let ((total 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1254 (number-of-pebbles-in-row number-of-rows)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1255 (while (> number-of-pebbles-in-row 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1256 (setq total (+ total number-of-pebbles-in-row)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1257 (setq number-of-pebbles-in-row |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1258 (1- number-of-pebbles-in-row))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1259 total)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1260 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1261 As written, this function works. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1262 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1263 However, we do not need `number-of-pebbles-in-row'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1264 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1265 When the `triangle' function is evaluated, the symbol `number-of-rows' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1266 will be bound to a number, giving it an initial value. That number can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1267 be changed in the body of the function as if it were a local variable, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1268 without any fear that such a change will effect the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1269 variable outside of the function. This is a very useful characteristic |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1270 of Lisp; it means that the variable `number-of-rows' can be used |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1271 anywhere in the function where `number-of-pebbles-in-row' is used. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1272 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1273 Here is a second version of the function written a bit more cleanly: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1274 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1275 (defun triangle (number) ; Second version. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1276 "Return sum of numbers 1 through NUMBER inclusive." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1277 (let ((total 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1278 (while (> number 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1279 (setq total (+ total number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1280 (setq number (1- number))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1281 total)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1282 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1283 In brief, a properly written `while' loop will consist of three parts: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1284 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1285 1. A test that will return false after the loop has repeated itself |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1286 the correct number of times. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1287 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1288 2. An expression the evaluation of which will return the value desired |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1289 after being repeatedly evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1290 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1291 3. An expression to change the value passed to the true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1292 so that the test returns false after the loop has repeated itself |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1293 the right number of times. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1294 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1295 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1296 File: eintr, Node: dolist dotimes, Next: Recursion, Prev: while, Up: Loops & Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1297 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1298 11.2 Save your time: `dolist' and `dotimes' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1299 =========================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1300 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1301 In addition to `while', both `dolist' and `dotimes' provide for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1302 looping. Sometimes these are quicker to write than the equivalent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1303 `while' loop. Both are Lisp macros. (*Note Macros: (elisp)Macros. ) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1304 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1305 `dolist' works like a `while' loop that `CDRs down a list': `dolist' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1306 automatically shortens the list each time it loops--takes the CDR of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1307 the list--and binds the CAR of each shorter version of the list to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1308 first of its arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1309 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1310 `dotimes' loops a specific number of times: you specify the number. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1311 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1312 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1313 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1314 * dolist:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1315 * dotimes:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1316 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1317 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1318 File: eintr, Node: dolist, Next: dotimes, Prev: dolist dotimes, Up: dolist dotimes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1319 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1320 The `dolist' Macro |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1321 .................. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1322 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1323 Suppose, for example, you want to reverse a list, so that "first" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1324 "second" "third" becomes "third" "second" "first". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1325 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1326 In practice, you would use the `reverse' function, like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1327 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1328 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1329 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1330 (reverse animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1331 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1332 Here is how you could reverse the list using a `while' loop: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1333 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1334 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1335 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1336 (defun reverse-list-with-while (list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1337 "Using while, reverse the order of LIST." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1338 (let (value) ; make sure list starts empty |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1339 (while list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1340 (setq value (cons (car list) value)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1341 (setq list (cdr list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1342 value)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1344 (reverse-list-with-while animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1345 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1346 And here is how you could use the `dolist' macro: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1348 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1349 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1350 (defun reverse-list-with-dolist (list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1351 "Using dolist, reverse the order of LIST." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1352 (let (value) ; make sure list starts empty |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1353 (dolist (element list value) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1354 (setq value (cons element value))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1355 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1356 (reverse-list-with-dolist animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1357 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1358 In Info, you can place your cursor after the closing parenthesis of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1359 each expression and type `C-x C-e'; in each case, you should see |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1360 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1361 (tiger lion giraffe gazelle) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1362 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1363 in the echo area. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1364 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1365 For this example, the existing `reverse' function is obviously best. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1366 The `while' loop is just like our first example (*note A `while' Loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1367 and a List: Loop Example.). The `while' first checks whether the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1368 has elements; if so, it constructs a new list by adding the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1369 element of the list to the existing list (which in the first iteration |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1370 of the loop is `nil'). Since the second element is prepended in front |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1371 of the first element, and the third element is prepended in front of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1372 the second element, the list is reversed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1373 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1374 In the expression using a `while' loop, the `(setq list (cdr list))' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1375 expression shortens the list, so the `while' loop eventually stops. In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1376 addition, it provides the `cons' expression with a new first element by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1377 creating a new and shorter list at each repetition of the loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1378 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1379 The `dolist' expression does very much the same as the `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1380 expression, except that the `dolist' macro does some of the work you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1381 have to do when writing a `while' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1382 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1383 Like a `while' loop, a `dolist' loops. What is different is that it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1384 automatically shortens the list each time it loops -- it `CDRs down the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1385 list' on its own -- and it automatically binds the CAR of each shorter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1386 version of the list to the first of its arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1387 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1388 In the example, the CAR of each shorter version of the list is referred |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1389 to using the symbol `element', the list itself is called `list', and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1390 the value returned is called `value'. The remainder of the `dolist' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1391 expression is the body. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1392 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1393 The `dolist' expression binds the CAR of each shorter version of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1394 list to `element' and then evaluates the body of the expression; and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1395 repeats the loop. The result is returned in `value'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1396 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1397 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1398 File: eintr, Node: dotimes, Prev: dolist, Up: dolist dotimes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1399 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1400 The `dotimes' Macro |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1401 ................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1402 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1403 The `dotimes' macro is similar to `dolist', except that it loops a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1404 specific number of times. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1405 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1406 The first argument to `dotimes' is assigned the numbers 0, 1, 2 and so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1407 forth each time around the loop, and the value of the third argument is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1408 returned. You need to provide the value of the second argument, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1409 is how many times the macro loops. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1410 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1411 For example, the following binds the numbers from 0 up to, but not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1412 including, the number 3 to the first argument, NUMBER, and then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1413 constructs a list of the three numbers. (The first number is 0, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1414 second number is 1, and the third number is 2; this makes a total of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1415 three numbers in all, starting with zero as the first number.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1416 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1417 (let (value) ; otherwise a value is a void variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1418 (dotimes (number 3 value) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1419 (setq value (cons number value)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1420 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1421 => (2 1 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1422 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1423 `dotimes' returns `value', so the way to use `dotimes' is to operate on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1424 some expression NUMBER number of times and then return the result, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1425 either as a list or an atom. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1426 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1427 Here is an example of a `defun' that uses `dotimes' to add up the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1428 number of pebbles in a triangle. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1429 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1430 (defun triangle-using-dotimes (number-of-rows) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1431 "Using dotimes, add up the number of pebbles in a triangle." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1432 (let ((total 0)) ; otherwise a total is a void variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1433 (dotimes (number number-of-rows total) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1434 (setq total (+ total (1+ number)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1435 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1436 (triangle-using-dotimes 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1437 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1438 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1439 File: eintr, Node: Recursion, Next: Looping exercise, Prev: dolist dotimes, Up: Loops & Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1440 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1441 11.3 Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1442 ============== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1443 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1444 A recursive function contains code that tells the Lisp interpreter to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1445 call a program that runs exactly like itself, but with slightly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1446 different arguments. The code runs exactly the same because it has the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1447 same name. However, even though the program has the same name, it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1448 not the same entity. It is different. In the jargon, it is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1449 different `instance'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1450 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1451 Eventually, if the program is written correctly, the `slightly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1452 different arguments' will become sufficiently different from the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1453 arguments that the final instance will stop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1454 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1455 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1456 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1457 * Building Robots:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1458 * Recursive Definition Parts:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1459 * Recursion with list:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1460 * Recursive triangle function:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1461 * Recursion with cond:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1462 * Recursive Patterns:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1463 * No Deferment:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1464 * No deferment solution:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1465 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1466 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1467 File: eintr, Node: Building Robots, Next: Recursive Definition Parts, Prev: Recursion, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1468 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1469 11.3.1 Building Robots: Extending the Metaphor |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1470 ---------------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1471 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1472 It is sometimes helpful to think of a running program as a robot that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1473 does a job. In doing its job, a recursive function calls on a second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1474 robot to help it. The second robot is identical to the first in every |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1475 way, except that the second robot helps the first and has been passed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1476 different arguments than the first. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1477 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1478 In a recursive function, the second robot may call a third; and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1479 third may call a fourth, and so on. Each of these is a different |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1480 entity; but all are clones. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1481 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1482 Since each robot has slightly different instructions--the arguments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1483 will differ from one robot to the next--the last robot should know when |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1484 to stop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1485 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1486 Let's expand on the metaphor in which a computer program is a robot. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1487 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1488 A function definition provides the blueprints for a robot. When you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1489 install a function definition, that is, when you evaluate a `defun' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1490 special form, you install the necessary equipment to build robots. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1491 is as if you were in a factory, setting up an assembly line. Robots |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1492 with the same name are built according to the same blueprints. So they |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1493 have, as it were, the same `model number', but a different `serial |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1494 number'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1495 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1496 We often say that a recursive function `calls itself'. What we mean is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1497 that the instructions in a recursive function cause the Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1498 interpreter to run a different function that has the same name and does |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1499 the same job as the first, but with different arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1500 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1501 It is important that the arguments differ from one instance to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1502 next; otherwise, the process will never stop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1503 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1504 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1505 File: eintr, Node: Recursive Definition Parts, Next: Recursion with list, Prev: Building Robots, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1506 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1507 11.3.2 The Parts of a Recursive Definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1508 ------------------------------------------ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1509 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1510 A recursive function typically contains a conditional expression which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1511 has three parts: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1512 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1513 1. A true-or-false-test that determines whether the function is called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1514 again, here called the "do-again-test". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1515 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1516 2. The name of the function. When this name is called, a new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1517 instance of the function--a new robot, as it were--is created and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1518 told what to do. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1519 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1520 3. An expression that returns a different value each time the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1521 function is called, here called the "next-step-expression". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1522 Consequently, the argument (or arguments) passed to the new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1523 instance of the function will be different from that passed to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1524 previous instance. This causes the conditional expression, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1525 "do-again-test", to test false after the correct number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1526 repetitions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1527 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1528 Recursive functions can be much simpler than any other kind of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1529 function. Indeed, when people first start to use them, they often look |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1530 so mysteriously simple as to be incomprehensible. Like riding a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1531 bicycle, reading a recursive function definition takes a certain knack |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1532 which is hard at first but then seems simple. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1533 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1534 There are several different common recursive patterns. A very simple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1535 pattern looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1536 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1537 (defun NAME-OF-RECURSIVE-FUNCTION (ARGUMENT-LIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1538 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1539 (if DO-AGAIN-TEST |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1540 BODY... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1541 (NAME-OF-RECURSIVE-FUNCTION |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1542 NEXT-STEP-EXPRESSION))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1543 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1544 Each time a recursive function is evaluated, a new instance of it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1545 created and told what to do. The arguments tell the instance what to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1546 do. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1547 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1548 An argument is bound to the value of the next-step-expression. Each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1549 instance runs with a different value of the next-step-expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1550 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1551 The value in the next-step-expression is used in the do-again-test. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1552 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1553 The value returned by the next-step-expression is passed to the new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1554 instance of the function, which evaluates it (or some |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1555 transmogrification of it) to determine whether to continue or stop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1556 The next-step-expression is designed so that the do-again-test returns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1557 false when the function should no longer be repeated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1558 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1559 The do-again-test is sometimes called the "stop condition", since it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1560 stops the repetitions when it tests false. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1561 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1562 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1563 File: eintr, Node: Recursion with list, Next: Recursive triangle function, Prev: Recursive Definition Parts, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1564 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1565 11.3.3 Recursion with a List |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1566 ---------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1567 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1568 The example of a `while' loop that printed the elements of a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1569 numbers can be written recursively. Here is the code, including an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1570 expression to set the value of the variable `animals' to a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1571 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1572 If you are using GNU Emacs 20 or before, this example must be copied to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1573 the `*scratch*' buffer and each expression must be evaluated there. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1574 Use `C-u C-x C-e' to evaluate the `(print-elements-recursively |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1575 animals)' expression so that the results are printed in the buffer; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1576 otherwise the Lisp interpreter will try to squeeze the results into the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1577 one line of the echo area. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1578 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1579 Also, place your cursor immediately after the last closing parenthesis |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1580 of the `print-elements-recursively' function, before the comment. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1581 Otherwise, the Lisp interpreter will try to evaluate the comment. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1582 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1583 If you are using a more recent version, you can evaluate this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1584 expression directly in Info. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1585 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1586 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1587 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1588 (defun print-elements-recursively (list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1589 "Print each element of LIST on a line of its own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1590 Uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1591 (if list ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1592 (progn |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1593 (print (car list)) ; body |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1594 (print-elements-recursively ; recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1595 (cdr list))))) ; next-step-expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1596 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1597 (print-elements-recursively animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1598 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1599 The `print-elements-recursively' function first tests whether there is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1600 any content in the list; if there is, the function prints the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1601 element of the list, the CAR of the list. Then the function `invokes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1602 itself', but gives itself as its argument, not the whole list, but the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1603 second and subsequent elements of the list, the CDR of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1604 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1605 Put another way, if the list is not empty, the function invokes another |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1606 instance of code that is similar to the initial code, but is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1607 different thread of execution, with different arguments than the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1608 instance. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1609 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1610 Put in yet another way, if the list is not empty, the first robot |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1611 assemblies a second robot and tells it what to do; the second robot is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1612 a different individual from the first, but is the same model. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1613 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1614 When the second evaluation occurs, the `if' expression is evaluated and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1615 if true, prints the first element of the list it receives as its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1616 argument (which is the second element of the original list). Then the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1617 function `calls itself' with the CDR of the list it is invoked with, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1618 which (the second time around) is the CDR of the CDR of the original |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1619 list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1620 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1621 Note that although we say that the function `calls itself', what we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1622 mean is that the Lisp interpreter assembles and instructs a new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1623 instance of the program. The new instance is a clone of the first, but |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1624 is a separate individual. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1625 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1626 Each time the function `invokes itself', it invokes itself on a shorter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1627 version of the original list. It creates a new instance that works on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1628 a shorter list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1629 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1630 Eventually, the function invokes itself on an empty list. It creates a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1631 new instance whose argument is `nil'. The conditional expression tests |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1632 the value of `list'. Since the value of `list' is `nil', the `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1633 expression tests false so the then-part is not evaluated. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1634 as a whole then returns `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1635 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1636 When you evaluate `(print-elements-recursively animals)' in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1637 `*scratch*' buffer, you see this result: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1638 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1639 gazelle |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1640 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1641 giraffe |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1642 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1643 lion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1644 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1645 tiger |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1646 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1647 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1648 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1649 File: eintr, Node: Recursive triangle function, Next: Recursion with cond, Prev: Recursion with list, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1650 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1651 11.3.4 Recursion in Place of a Counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1652 -------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1653 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1654 The `triangle' function described in a previous section can also be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1655 written recursively. It looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1656 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1657 (defun triangle-recursively (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1658 "Return the sum of the numbers 1 through NUMBER inclusive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1659 Uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1660 (if (= number 1) ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1661 1 ; then-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1662 (+ number ; else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1663 (triangle-recursively ; recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1664 (1- number))))) ; next-step-expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1665 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1666 (triangle-recursively 7) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1667 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1668 You can install this function by evaluating it and then try it by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1669 evaluating `(triangle-recursively 7)'. (Remember to put your cursor |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1670 immediately after the last parenthesis of the function definition, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1671 before the comment.) The function evaluates to 28. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1672 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1673 To understand how this function works, let's consider what happens in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1674 the various cases when the function is passed 1, 2, 3, or 4 as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1675 value of its argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1676 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1677 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1678 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1679 * Recursive Example arg of 1 or 2:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1680 * Recursive Example arg of 3 or 4:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1681 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1682 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1683 File: eintr, Node: Recursive Example arg of 1 or 2, Next: Recursive Example arg of 3 or 4, Prev: Recursive triangle function, Up: Recursive triangle function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1684 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1685 An argument of 1 or 2 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1686 ..................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1687 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1688 First, what happens if the value of the argument is 1? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1689 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1690 The function has an `if' expression after the documentation string. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1691 tests whether the value of `number' is equal to 1; if so, Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1692 evaluates the then-part of the `if' expression, which returns the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1693 number 1 as the value of the function. (A triangle with one row has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1694 one pebble in it.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1695 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1696 Suppose, however, that the value of the argument is 2. In this case, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1697 Emacs evaluates the else-part of the `if' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1698 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1699 The else-part consists of an addition, the recursive call to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1700 `triangle-recursively' and a decrementing action; and it looks like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1701 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1702 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1703 (+ number (triangle-recursively (1- number))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1704 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1705 When Emacs evaluates this expression, the innermost expression is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1706 evaluated first; then the other parts in sequence. Here are the steps |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1707 in detail: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1709 Step 1 Evaluate the innermost expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1710 The innermost expression is `(1- number)' so Emacs decrements the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1711 value of `number' from 2 to 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1712 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1713 Step 2 Evaluate the `triangle-recursively' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1714 The Lisp interpreter creates an individual instance of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1715 `triangle-recursively'. It does not matter that this function is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1716 contained within itself. Emacs passes the result Step 1 as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1717 argument used by this instance of the `triangle-recursively' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1718 function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1719 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1720 In this case, Emacs evaluates `triangle-recursively' with an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1721 argument of 1. This means that this evaluation of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1722 `triangle-recursively' returns 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1723 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1724 Step 3 Evaluate the value of `number'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1725 The variable `number' is the second element of the list that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1726 starts with `+'; its value is 2. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1727 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1728 Step 4 Evaluate the `+' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1729 The `+' expression receives two arguments, the first from the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1730 evaluation of `number' (Step 3) and the second from the evaluation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1731 of `triangle-recursively' (Step 2). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1732 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1733 The result of the addition is the sum of 2 plus 1, and the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1734 3 is returned, which is correct. A triangle with two rows has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1735 three pebbles in it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1736 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1737 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1738 File: eintr, Node: Recursive Example arg of 3 or 4, Prev: Recursive Example arg of 1 or 2, Up: Recursive triangle function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1739 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1740 An argument of 3 or 4 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1741 ..................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1742 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1743 Suppose that `triangle-recursively' is called with an argument of 3. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1744 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1745 Step 1 Evaluate the do-again-test. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1746 The `if' expression is evaluated first. This is the do-again test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1747 and returns false, so the else-part of the `if' expression is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1748 evaluated. (Note that in this example, the do-again-test causes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1749 the function to call itself when it tests false, not when it tests |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1750 true.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1751 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1752 Step 2 Evaluate the innermost expression of the else-part. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1753 The innermost expression of the else-part is evaluated, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1754 decrements 3 to 2. This is the next-step-expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1755 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1756 Step 3 Evaluate the `triangle-recursively' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1757 The number 2 is passed to the `triangle-recursively' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1758 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1759 We know what happens when Emacs evaluates `triangle-recursively' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1760 with an argument of 2. After going through the sequence of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1761 actions described earlier, it returns a value of 3. So that is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1762 what will happen here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1763 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1764 Step 4 Evaluate the addition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1765 3 will be passed as an argument to the addition and will be added |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1766 to the number with which the function was called, which is 3. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1767 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1768 The value returned by the function as a whole will be 6. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1769 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1770 Now that we know what will happen when `triangle-recursively' is called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1771 with an argument of 3, it is evident what will happen if it is called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1772 with an argument of 4: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1773 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1774 In the recursive call, the evaluation of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1775 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1776 (triangle-recursively (1- 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1777 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1778 will return the value of evaluating |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1779 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1780 (triangle-recursively 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1781 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1782 which is 6 and this value will be added to 4 by the addition in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1783 third line. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1784 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1785 The value returned by the function as a whole will be 10. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1786 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1787 Each time `triangle-recursively' is evaluated, it evaluates a version |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1788 of itself--a different instance of itself--with a smaller argument, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1789 until the argument is small enough so that it does not evaluate itself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1790 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1791 Note that this particular design for a recursive function requires that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1792 operations be deferred. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1793 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1794 Before `(triangle-recursively 7)' can calculate its answer, it must |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1795 call `(triangle-recursively 6)'; and before `(triangle-recursively 6)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1796 can calculate its answer, it must call `(triangle-recursively 5)'; and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1797 so on. That is to say, the calculation that `(triangle-recursively 7)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1798 makes must be deferred until `(triangle-recursively 6)' makes its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1799 calculation; and `(triangle-recursively 6)' must defer until |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1800 `(triangle-recursively 5)' completes; and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1801 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1802 If each of these instances of `triangle-recursively' are thought of as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1803 different robots, the first robot must wait for the second to complete |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1804 its job, which must wait until the third completes, and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1805 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1806 There is a way around this kind of waiting, which we will discuss in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1807 *Note Recursion without Deferments: No Deferment. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1808 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1809 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1810 File: eintr, Node: Recursion with cond, Next: Recursive Patterns, Prev: Recursive triangle function, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1811 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1812 11.3.5 Recursion Example Using `cond' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1813 ------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1814 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1815 The version of `triangle-recursively' described earlier is written with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1816 the `if' special form. It can also be written using another special |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1817 form called `cond'. The name of the special form `cond' is an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1818 abbreviation of the word `conditional'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1819 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1820 Although the `cond' special form is not used as often in the Emacs Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1821 sources as `if', it is used often enough to justify explaining it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1822 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1823 The template for a `cond' expression looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1824 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1825 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1826 BODY...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1827 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1828 where the BODY is a series of lists. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1829 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1830 Written out more fully, the template looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1831 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1832 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1833 (FIRST-TRUE-OR-FALSE-TEST FIRST-CONSEQUENT) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1834 (SECOND-TRUE-OR-FALSE-TEST SECOND-CONSEQUENT) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1835 (THIRD-TRUE-OR-FALSE-TEST THIRD-CONSEQUENT) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1836 ...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1837 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1838 When the Lisp interpreter evaluates the `cond' expression, it evaluates |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1839 the first element (the CAR or true-or-false-test) of the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1840 expression in a series of expressions within the body of the `cond'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1841 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1842 If the true-or-false-test returns `nil' the rest of that expression, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1843 the consequent, is skipped and the true-or-false-test of the next |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1844 expression is evaluated. When an expression is found whose |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1845 true-or-false-test returns a value that is not `nil', the consequent of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1846 that expression is evaluated. The consequent can be one or more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1847 expressions. If the consequent consists of more than one expression, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1848 the expressions are evaluated in sequence and the value of the last one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1849 is returned. If the expression does not have a consequent, the value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1850 of the true-or-false-test is returned. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1851 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1852 If none of the true-or-false-tests test true, the `cond' expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1853 returns `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1854 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1855 Written using `cond', the `triangle' function looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1856 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1857 (defun triangle-using-cond (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1858 (cond ((<= number 0) 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1859 ((= number 1) 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1860 ((> number 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1861 (+ number (triangle-using-cond (1- number)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1862 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1863 In this example, the `cond' returns 0 if the number is less than or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1864 equal to 0, it returns 1 if the number is 1 and it evaluates `(+ number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1865 (triangle-using-cond (1- number)))' if the number is greater than 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1866 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1867 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1868 File: eintr, Node: Recursive Patterns, Next: No Deferment, Prev: Recursion with cond, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1869 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1870 11.3.6 Recursive Patterns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1871 ------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1872 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1873 Here are three common recursive patterns. Each involves a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1874 Recursion does not need to involve lists, but Lisp is designed for lists |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1875 and this provides a sense of its primal capabilities. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1876 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1877 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1878 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1879 * Every:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1880 * Accumulate:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1881 * Keep:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1882 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1883 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1884 File: eintr, Node: Every, Next: Accumulate, Prev: Recursive Patterns, Up: Recursive Patterns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1885 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1886 Recursive Pattern: _every_ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1887 .......................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1888 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1889 In the `every' recursive pattern, an action is performed on every |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1890 element of a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1891 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1892 The basic pattern is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1893 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1894 * If a list be empty, return `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1895 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1896 * Else, act on the beginning of the list (the CAR of the list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1897 - through a recursive call by the function on the rest (the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1898 CDR) of the list, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1899 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1900 - and, optionally, combine the acted-on element, using |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1901 `cons', with the results of acting on the rest. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1902 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1903 Here is example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1904 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1905 (defun square-each (numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1906 "Square each of a NUMBERS LIST, recursively." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1907 (if (not numbers-list) ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1908 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1909 (cons |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1910 (* (car numbers-list) (car numbers-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1911 (square-each (cdr numbers-list))))) ; next-step-expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1912 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1913 (square-each '(1 2 3)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1914 => (1 4 9) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1915 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1916 If `numbers-list' is empty, do nothing. But if it has content, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1917 construct a list combining the square of the first number in the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1918 with the result of the recursive call. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1919 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1920 (The example follows the pattern exactly: `nil' is returned if the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1921 numbers' list is empty. In practice, you would write the conditional |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1922 so it carries out the action when the numbers' list is not empty.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1923 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1924 The `print-elements-recursively' function (*note Recursion with a List: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1925 Recursion with list.) is another example of an `every' pattern, except |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1926 in this case, rather than bring the results together using `cons', we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1927 print each element of output. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1928 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1929 The `print-elements-recursively' function looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1930 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1931 (setq animals '(gazelle giraffe lion tiger)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1932 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1933 (defun print-elements-recursively (list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1934 "Print each element of LIST on a line of its own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1935 Uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1936 (if list ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1937 (progn |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1938 (print (car list)) ; body |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1939 (print-elements-recursively ; recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1940 (cdr list))))) ; next-step-expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1941 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1942 (print-elements-recursively animals) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1943 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1944 The pattern for `print-elements-recursively' is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1945 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1946 * If the list be empty, do nothing. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1947 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1948 * But if the list has at least one element, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1949 - act on the beginning of the list (the CAR of the list), |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1950 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1951 - and make a recursive call on the rest (the CDR) of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1952 list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1953 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1954 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1955 File: eintr, Node: Accumulate, Next: Keep, Prev: Every, Up: Recursive Patterns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1956 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1957 Recursive Pattern: _accumulate_ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1958 ............................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1959 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1960 Another recursive pattern is called the `accumulate' pattern. In the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1961 `accumulate' recursive pattern, an action is performed on every element |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1962 of a list and the result of that action is accumulated with the results |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1963 of performing the action on the other elements. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1964 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1965 This is very like the `every' pattern using `cons', except that `cons' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1966 is not used, but some other combiner. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1968 The pattern is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1969 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1970 * If a list be empty, return zero or some other constant. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1971 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1972 * Else, act on the beginning of the list (the CAR of the list), |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1973 - and combine that acted-on element, using `+' or some |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1974 other combining function, with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1975 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1976 - a recursive call by the function on the rest (the CDR) of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1977 the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1978 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1979 Here is an example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1980 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1981 (defun add-elements (numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1982 "Add the elements of NUMBERS-LIST together." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1983 (if (not numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1984 0 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1985 (+ (car numbers-list) (add-elements (cdr numbers-list))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1986 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1987 (add-elements '(1 2 3 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1988 => 10 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1989 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1990 *Note Making a List of Files: Files List, for an example of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1991 accumulate pattern. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1992 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1993 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1994 File: eintr, Node: Keep, Prev: Accumulate, Up: Recursive Patterns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1995 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1996 Recursive Pattern: _keep_ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1997 ......................... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1998 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
1999 A third recursive pattern is called the `keep' pattern. In the `keep' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2000 recursive pattern, each element of a list is tested; the element is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2001 acted on and the results are kept only if the element meets a criterion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2002 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2003 Again, this is very like the `every' pattern, except the element is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2004 skipped unless it meets a criterion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2005 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2006 The pattern has three parts: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2007 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2008 * If a list be empty, return `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2009 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2010 * Else, if the beginning of the list (the CAR of the list) passes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2011 a test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2012 - act on that element and combine it, using `cons' with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2013 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2014 - a recursive call by the function on the rest (the CDR) of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2015 the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2016 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2017 * Otherwise, if the beginning of the list (the CAR of the list) fails |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2018 the test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2019 - skip on that element, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2020 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2021 - and, recursively call the function on the rest (the CDR) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2022 of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2023 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2024 Here is an example that uses `cond': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2025 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2026 (defun keep-three-letter-words (word-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2027 "Keep three letter words in WORD-LIST." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2028 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2029 ;; First do-again-test: stop-condition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2030 ((not word-list) nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2031 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2032 ;; Second do-again-test: when to act |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2033 ((eq 3 (length (symbol-name (car word-list)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2034 ;; combine acted-on element with recursive call on shorter list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2035 (cons (car word-list) (keep-three-letter-words (cdr word-list)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2036 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2037 ;; Third do-again-test: when to skip element; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2038 ;; recursively call shorter list with next-step expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2039 (t (keep-three-letter-words (cdr word-list))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2040 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2041 (keep-three-letter-words '(one two three four five six)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2042 => (one two six) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2043 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2044 It goes without saying that you need not use `nil' as the test for when |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2045 to stop; and you can, of course, combine these patterns. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2046 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2047 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2048 File: eintr, Node: No Deferment, Next: No deferment solution, Prev: Recursive Patterns, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2049 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2050 11.3.7 Recursion without Deferments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2051 ----------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2052 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2053 Let's consider again what happens with the `triangle-recursively' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2054 function. We will find that the intermediate calculations are deferred |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2055 until all can be done. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2056 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2057 Here is the function definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2058 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2059 (defun triangle-recursively (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2060 "Return the sum of the numbers 1 through NUMBER inclusive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2061 Uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2062 (if (= number 1) ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2063 1 ; then-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2064 (+ number ; else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2065 (triangle-recursively ; recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2066 (1- number))))) ; next-step-expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2067 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2068 What happens when we call this function with a argument of 7? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2069 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2070 The first instance of the `triangle-recursively' function adds the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2071 number 7 to the value returned by a second instance of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2072 `triangle-recursively', an instance that has been passed an argument of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2073 6. That is to say, the first calculation is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2074 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2075 (+ 7 (triangle-recursively 6)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2076 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2077 The first instance of `triangle-recursively'--you may want to think of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2078 it as a little robot--cannot complete its job. It must hand off the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2079 calculation for `(triangle-recursively 6)' to a second instance of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2080 program, to a second robot. This second individual is completely |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2081 different from the first one; it is, in the jargon, a `different |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2082 instantiation'. Or, put another way, it is a different robot. It is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2083 the same model as the first; it calculates triangle numbers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2084 recursively; but it has a different serial number. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2085 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2086 And what does `(triangle-recursively 6)' return? It returns the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2087 6 added to the value returned by evaluating `triangle-recursively' with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2088 an argument of 5. Using the robot metaphor, it asks yet another robot |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2089 to help it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2090 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2091 Now the total is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2092 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2093 (+ 7 6 (triangle-recursively 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2094 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2095 And what happens next? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2096 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2097 (+ 7 6 5 (triangle-recursively 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2098 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2099 Each time `triangle-recursively' is called, except for the last time, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2100 it creates another instance of the program--another robot--and asks it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2101 to make a calculation. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2102 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2103 Eventually, the full addition is set up and performed: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2104 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2105 (+ 7 6 5 4 3 2 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2106 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2107 This design for the function defers the calculation of the first step |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2108 until the second can be done, and defers that until the third can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2109 done, and so on. Each deferment means the computer must remember what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2110 is being waited on. This is not a problem when there are only a few |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2111 steps, as in this example. But it can be a problem when there are more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2112 steps. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2113 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2114 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2115 File: eintr, Node: No deferment solution, Prev: No Deferment, Up: Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2116 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2117 11.3.8 No Deferment Solution |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2118 ---------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2119 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2120 The solution to the problem of deferred operations is to write in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2121 manner that does not defer operations(1). This requires writing to a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2122 different pattern, often one that involves writing two function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2123 definitions, an `initialization' function and a `helper' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2124 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2125 The `initialization' function sets up the job; the `helper' function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2126 does the work. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2127 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2128 Here are the two function definitions for adding up numbers. They are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2129 so simple, I find them hard to understand. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2130 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2131 (defun triangle-initialization (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2132 "Return the sum of the numbers 1 through NUMBER inclusive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2133 This is the `initialization' component of a two function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2134 duo that uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2135 (triangle-recursive-helper 0 0 number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2136 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2137 (defun triangle-recursive-helper (sum counter number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2138 "Return SUM, using COUNTER, through NUMBER inclusive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2139 This is the `helper' component of a two function duo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2140 that uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2141 (if (> counter number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2142 sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2143 (triangle-recursive-helper (+ sum counter) ; sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2144 (1+ counter) ; counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2145 number))) ; number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2146 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2147 Install both function definitions by evaluating them, then call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2148 `triangle-initialization' with 2 rows: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2149 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2150 (triangle-initialization 2) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2151 => 3 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2152 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2153 The `initialization' function calls the first instance of the `helper' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2154 function with three arguments: zero, zero, and a number which is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2155 number of rows in the triangle. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2156 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2157 The first two arguments passed to the `helper' function are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2158 initialization values. These values are changed when |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2159 `triangle-recursive-helper' invokes new instances.(2) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2160 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2161 Let's see what happens when we have a triangle that has one row. (This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2162 triangle will have one pebble in it!) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2163 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2164 `triangle-initialization' will call its helper with the arguments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2165 `0 0 1'. That function will run the conditional test whether `(> |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2166 counter number)': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2167 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2168 (> 0 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2169 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2170 and find that the result is false, so it will invoke the else-part of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2171 the `if' clause: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2172 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2173 (triangle-recursive-helper |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2174 (+ sum counter) ; sum plus counter => sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2175 (1+ counter) ; increment counter => counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2176 number) ; number stays the same |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2177 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2178 which will first compute: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2179 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2180 (triangle-recursive-helper (+ 0 0) ; sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2181 (1+ 0) ; counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2182 1) ; number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2183 which is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2184 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2185 (triangle-recursive-helper 0 1 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2186 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2187 Again, `(> counter number)' will be false, so again, the Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2188 interpreter will evaluate `triangle-recursive-helper', creating a new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2189 instance with new arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2190 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2191 This new instance will be; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2192 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2193 (triangle-recursive-helper |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2194 (+ sum counter) ; sum plus counter => sum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2195 (1+ counter) ; increment counter => counter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2196 number) ; number stays the same |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2197 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2198 which is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2199 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2200 (triangle-recursive-helper 1 2 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2201 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2202 In this case, the `(> counter number)' test will be true! So the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2203 instance will return the value of the sum, which will be 1, as expected. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2204 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2205 Now, let's pass `triangle-initialization' an argument of 2, to find out |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2206 how many pebbles there are in a triangle with two rows. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2207 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2208 That function calls `(triangle-recursive-helper 0 0 2)'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2209 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2210 In stages, the instances called will be: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2211 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2212 sum counter number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2213 (triangle-recursive-helper 0 1 2) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2214 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2215 (triangle-recursive-helper 1 2 2) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2216 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2217 (triangle-recursive-helper 3 3 2) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2218 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2219 When the last instance is called, the `(> counter number)' test will be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2220 true, so the instance will return the value of `sum', which will be 3. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2221 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2222 This kind of pattern helps when you are writing functions that can use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2223 many resources in a computer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2224 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2225 ---------- Footnotes ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2226 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2227 (1) The phrase "tail recursive" is used to describe such a process, one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2228 that uses `constant space'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2229 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2230 (2) The jargon is mildly confusing: `triangle-recursive-helper' uses a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2231 process that is iterative in a procedure that is recursive. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2232 process is called iterative because the computer need only record the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2233 three values, `sum', `counter', and `number'; the procedure is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2234 recursive because the function `calls itself'. On the other hand, both |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2235 the process and the procedure used by `triangle-recursively' are called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2236 recursive. The word `recursive' has different meanings in the two |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2237 contexts. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2238 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2239 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2240 File: eintr, Node: Looping exercise, Prev: Recursion, Up: Loops & Recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2241 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2242 11.4 Looping Exercise |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2243 ===================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2244 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2245 * Write a function similar to `triangle' in which each row has a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2246 value which is the square of the row number. Use a `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2247 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2248 * Write a function similar to `triangle' that multiplies instead of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2249 adds the values. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2250 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2251 * Rewrite these two functions recursively. Rewrite these functions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2252 using `cond'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2253 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2254 * Write a function for Texinfo mode that creates an index entry at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2255 the beginning of a paragraph for every `@dfn' within the paragraph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2256 (In a Texinfo file, `@dfn' marks a definition. This book is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2257 written in Texinfo.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2258 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2259 Many of the functions you will need are described in two of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2260 previous chapters, *Note Cutting and Storing Text: Cutting & |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2261 Storing Text, and *Note Yanking Text Back: Yanking. If you use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2262 `forward-paragraph' to put the index entry at the beginning of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2263 paragraph, you will have to use `C-h f' (`describe-function') to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2264 find out how to make the command go backwards. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2265 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2266 For more information, see *Note Indicating Definitions: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2267 (texinfo)Indicating. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2268 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2269 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2270 File: eintr, Node: Regexp Search, Next: Counting Words, Prev: Loops & Recursion, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2271 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2272 12 Regular Expression Searches |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2273 ****************************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2274 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2275 Regular expression searches are used extensively in GNU Emacs. The two |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2276 functions, `forward-sentence' and `forward-paragraph', illustrate these |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2277 searches well. They use regular expressions to find where to move |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2278 point. The phrase `regular expression' is often written as `regexp'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2279 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2280 Regular expression searches are described in *Note Regular Expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2281 Search: (emacs)Regexp Search, as well as in *Note Regular Expressions: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2282 (elisp)Regular Expressions. In writing this chapter, I am presuming |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2283 that you have at least a mild acquaintance with them. The major point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2284 to remember is that regular expressions permit you to search for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2285 patterns as well as for literal strings of characters. For example, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2286 the code in `forward-sentence' searches for the pattern of possible |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2287 characters that could mark the end of a sentence, and moves point to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2288 that spot. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2289 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2290 Before looking at the code for the `forward-sentence' function, it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2291 worth considering what the pattern that marks the end of a sentence |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2292 must be. The pattern is discussed in the next section; following that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2293 is a description of the regular expression search function, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2294 `re-search-forward'. The `forward-sentence' function is described in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2295 the section following. Finally, the `forward-paragraph' function is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2296 described in the last section of this chapter. `forward-paragraph' is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2297 a complex function that introduces several new features. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2298 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2299 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2300 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2301 * sentence-end:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2302 * re-search-forward:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2303 * forward-sentence:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2304 * forward-paragraph:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2305 * etags:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2306 * Regexp Review:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2307 * re-search Exercises:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2308 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2309 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2310 File: eintr, Node: sentence-end, Next: re-search-forward, Prev: Regexp Search, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2311 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2312 12.1 The Regular Expression for `sentence-end' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2313 ============================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2314 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2315 The symbol `sentence-end' is bound to the pattern that marks the end of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2316 a sentence. What should this regular expression be? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2317 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2318 Clearly, a sentence may be ended by a period, a question mark, or an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2319 exclamation mark. Indeed, only clauses that end with one of those three |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2320 characters should be considered the end of a sentence. This means that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2321 the pattern should include the character set: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2322 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2323 [.?!] |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2324 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2325 However, we do not want `forward-sentence' merely to jump to a period, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2326 a question mark, or an exclamation mark, because such a character might |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2327 be used in the middle of a sentence. A period, for example, is used |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2328 after abbreviations. So other information is needed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2329 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2330 According to convention, you type two spaces after every sentence, but |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2331 only one space after a period, a question mark, or an exclamation mark |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2332 in the body of a sentence. So a period, a question mark, or an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2333 exclamation mark followed by two spaces is a good indicator of an end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2334 of sentence. However, in a file, the two spaces may instead be a tab |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2335 or the end of a line. This means that the regular expression should |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2336 include these three items as alternatives. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2337 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2338 This group of alternatives will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2339 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2340 \\($\\| \\| \\) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2341 ^ ^^ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2342 TAB SPC |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2344 Here, `$' indicates the end of the line, and I have pointed out where |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2345 the tab and two spaces are inserted in the expression. Both are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2346 inserted by putting the actual characters into the expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2348 Two backslashes, `\\', are required before the parentheses and vertical |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2349 bars: the first backslash quotes the following backslash in Emacs; and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2350 the second indicates that the following character, the parenthesis or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2351 the vertical bar, is special. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2352 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2353 Also, a sentence may be followed by one or more carriage returns, like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2354 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2355 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2356 [ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2357 ]* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2358 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2359 Like tabs and spaces, a carriage return is inserted into a regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2360 expression by inserting it literally. The asterisk indicates that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2361 <RET> is repeated zero or more times. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2362 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2363 But a sentence end does not consist only of a period, a question mark or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2364 an exclamation mark followed by appropriate space: a closing quotation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2365 mark or a closing brace of some kind may precede the space. Indeed more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2366 than one such mark or brace may precede the space. These require a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2367 expression that looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2368 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2369 []\"')}]* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2370 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2371 In this expression, the first `]' is the first character in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2372 expression; the second character is `"', which is preceded by a `\' to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2373 tell Emacs the `"' is _not_ special. The last three characters are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2374 `'', `)', and `}'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2375 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2376 All this suggests what the regular expression pattern for matching the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2377 end of a sentence should be; and, indeed, if we evaluate `sentence-end' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2378 we find that it returns the following value: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2379 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2380 sentence-end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2381 => "[.?!][]\"')}]*\\($\\| \\| \\)[ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2382 ]*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2383 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2384 (Well, not in GNU Emacs 22; that is because of an effort to make the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2385 process simpler. When its value is `nil', then use the value defined |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2386 by the function `sentence-end', and that returns a value constructed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2387 from the variables `sentence-end-base', `sentence-end-double-space', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2388 `sentence-end-without-period', and `sentence-end-without-space'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2389 critical variable is `sentence-end-base'; its global value is similar |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2390 to the one described above but it also contains two additional |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2391 quotation marks. These have differing degrees of curliness. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2392 `sentence-end-without-period' variable, when true, tells Emacs that a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2393 sentence may end without a period, such as text in Thai.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2394 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2395 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2396 File: eintr, Node: re-search-forward, Next: forward-sentence, Prev: sentence-end, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2397 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2398 12.2 The `re-search-forward' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2399 ===================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2400 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2401 The `re-search-forward' function is very like the `search-forward' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2402 function. (*Note The `search-forward' Function: search-forward.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2403 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2404 `re-search-forward' searches for a regular expression. If the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2405 is successful, it leaves point immediately after the last character in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2406 the target. If the search is backwards, it leaves point just before |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2407 the first character in the target. You may tell `re-search-forward' to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2408 return `t' for true. (Moving point is therefore a `side effect'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2409 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2410 Like `search-forward', the `re-search-forward' function takes four |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2411 arguments: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2412 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2413 1. The first argument is the regular expression that the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2414 searches for. The regular expression will be a string between |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2415 quotations marks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2416 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2417 2. The optional second argument limits how far the function will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2418 search; it is a bound, which is specified as a position in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2419 buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2420 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2421 3. The optional third argument specifies how the function responds to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2422 failure: `nil' as the third argument causes the function to signal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2423 an error (and print a message) when the search fails; any other |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2424 value causes it to return `nil' if the search fails and `t' if the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2425 search succeeds. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2426 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2427 4. The optional fourth argument is the repeat count. A negative |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2428 repeat count causes `re-search-forward' to search backwards. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2429 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2430 The template for `re-search-forward' looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2431 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2432 (re-search-forward "REGULAR-EXPRESSION" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2433 LIMIT-OF-SEARCH |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2434 WHAT-TO-DO-IF-SEARCH-FAILS |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2435 REPEAT-COUNT) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2436 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2437 The second, third, and fourth arguments are optional. However, if you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2438 want to pass a value to either or both of the last two arguments, you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2439 must also pass a value to all the preceding arguments. Otherwise, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2440 Lisp interpreter will mistake which argument you are passing the value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2441 to. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2442 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2443 In the `forward-sentence' function, the regular expression will be the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2444 value of the variable `sentence-end'. In simple form, that is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2445 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2446 "[.?!][]\"')}]*\\($\\| \\| \\)[ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2447 ]*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2448 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2449 The limit of the search will be the end of the paragraph (since a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2450 sentence cannot go beyond a paragraph). If the search fails, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2451 function will return `nil'; and the repeat count will be provided by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2452 the argument to the `forward-sentence' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2453 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2454 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2455 File: eintr, Node: forward-sentence, Next: forward-paragraph, Prev: re-search-forward, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2456 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2457 12.3 `forward-sentence' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2458 ======================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2459 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2460 The command to move the cursor forward a sentence is a straightforward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2461 illustration of how to use regular expression searches in Emacs Lisp. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2462 Indeed, the function looks longer and more complicated than it is; this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2463 is because the function is designed to go backwards as well as forwards; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2464 and, optionally, over more than one sentence. The function is usually |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2465 bound to the key command `M-e'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2466 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2467 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2468 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2469 * Complete forward-sentence:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2470 * fwd-sentence while loops:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2471 * fwd-sentence re-search:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2472 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2473 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2474 File: eintr, Node: Complete forward-sentence, Next: fwd-sentence while loops, Prev: forward-sentence, Up: forward-sentence |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2475 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2476 Complete `forward-sentence' function definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2477 ----------------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2478 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2479 Here is the code for `forward-sentence': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2480 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2481 (defun forward-sentence (&optional arg) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2482 "Move forward to next `sentence-end'. With argument, repeat. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2483 With negative argument, move backward repeatedly to `sentence-beginning'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2484 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2485 The variable `sentence-end' is a regular expression that matches ends of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2486 sentences. Also, every paragraph boundary terminates sentences as well." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2487 (interactive "p") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2488 (or arg (setq arg 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2489 (let ((opoint (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2490 (sentence-end (sentence-end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2491 (while (< arg 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2492 (let ((pos (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2493 (par-beg (save-excursion (start-of-paragraph-text) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2494 (if (and (re-search-backward sentence-end par-beg t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2495 (or (< (match-end 0) pos) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2496 (re-search-backward sentence-end par-beg t))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2497 (goto-char (match-end 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2498 (goto-char par-beg))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2499 (setq arg (1+ arg))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2500 (while (> arg 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2501 (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2502 (if (re-search-forward sentence-end par-end t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2503 (skip-chars-backward " \t\n") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2504 (goto-char par-end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2505 (setq arg (1- arg))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2506 (constrain-to-field nil opoint t))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2507 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2508 The function looks long at first sight and it is best to look at its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2509 skeleton first, and then its muscle. The way to see the skeleton is to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2510 look at the expressions that start in the left-most columns: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2511 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2512 (defun forward-sentence (&optional arg) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2513 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2514 (interactive "p") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2515 (or arg (setq arg 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2516 (let ((opoint (point)) (sentence-end (sentence-end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2517 (while (< arg 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2518 (let ((pos (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2519 (par-beg (save-excursion (start-of-paragraph-text) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2520 REST-OF-BODY-OF-WHILE-LOOP-WHEN-GOING-BACKWARDS |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2521 (while (> arg 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2522 (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2523 REST-OF-BODY-OF-WHILE-LOOP-WHEN-GOING-FORWARDS |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2524 HANDLE-FORMS-AND-EQUIVALENT |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2525 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2526 This looks much simpler! The function definition consists of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2527 documentation, an `interactive' expression, an `or' expression, a `let' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2528 expression, and `while' loops. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2529 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2530 Let's look at each of these parts in turn. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2531 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2532 We note that the documentation is thorough and understandable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2533 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2534 The function has an `interactive "p"' declaration. This means that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2535 processed prefix argument, if any, is passed to the function as its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2536 argument. (This will be a number.) If the function is not passed an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2537 argument (it is optional) then the argument `arg' will be bound to 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2538 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2539 When `forward-sentence' is called non-interactively without an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2540 argument, `arg' is bound to `nil'. The `or' expression handles this. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2541 What it does is either leave the value of `arg' as it is, but only if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2542 `arg' is bound to a value; or it sets the value of `arg' to 1, in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2543 case when `arg' is bound to `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2544 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2545 Next is a `let'. That specifies the values of two local variables, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2546 `point' and `sentence-end'. The local value of point, from before the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2547 search, is used in the `constrain-to-field' function which handles |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2548 forms and equivalents. The `sentence-end' variable is set by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2549 `sentence-end' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2550 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2551 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2552 File: eintr, Node: fwd-sentence while loops, Next: fwd-sentence re-search, Prev: Complete forward-sentence, Up: forward-sentence |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2553 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2554 The `while' loops |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2555 ----------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2556 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2557 Two `while' loops follow. The first `while' has a true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2558 that tests true if the prefix argument for `forward-sentence' is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2559 negative number. This is for going backwards. The body of this loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2560 is similar to the body of the second `while' clause, but it is not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2561 exactly the same. We will skip this `while' loop and concentrate on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2562 the second `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2563 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2564 The second `while' loop is for moving point forward. Its skeleton |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2565 looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2566 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2567 (while (> arg 0) ; true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2568 (let VARLIST |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2569 (if (TRUE-OR-FALSE-TEST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2570 THEN-PART |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2571 ELSE-PART |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2572 (setq arg (1- arg)))) ; `while' loop decrementer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2573 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2574 The `while' loop is of the decrementing kind. (*Note A Loop with a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2575 Decrementing Counter: Decrementing Loop.) It has a true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2576 that tests true so long as the counter (in this case, the variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2577 `arg') is greater than zero; and it has a decrementer that subtracts 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2578 from the value of the counter every time the loop repeats. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2579 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2580 If no prefix argument is given to `forward-sentence', which is the most |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2581 common way the command is used, this `while' loop will run once, since |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2582 the value of `arg' will be 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2583 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2584 The body of the `while' loop consists of a `let' expression, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2585 creates and binds a local variable, and has, as its body, an `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2586 expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2587 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2588 The body of the `while' loop looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2589 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2590 (let ((par-end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2591 (save-excursion (end-of-paragraph-text) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2592 (if (re-search-forward sentence-end par-end t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2593 (skip-chars-backward " \t\n") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2594 (goto-char par-end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2595 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2596 The `let' expression creates and binds the local variable `par-end'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2597 As we shall see, this local variable is designed to provide a bound or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2598 limit to the regular expression search. If the search fails to find a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2599 proper sentence ending in the paragraph, it will stop on reaching the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2600 end of the paragraph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2601 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2602 But first, let us examine how `par-end' is bound to the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2603 end of the paragraph. What happens is that the `let' sets the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2604 `par-end' to the value returned when the Lisp interpreter evaluates the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2605 expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2606 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2607 (save-excursion (end-of-paragraph-text) (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2608 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2609 In this expression, `(end-of-paragraph-text)' moves point to the end of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2610 the paragraph, `(point)' returns the value of point, and then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2611 `save-excursion' restores point to its original position. Thus, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2612 `let' binds `par-end' to the value returned by the `save-excursion' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2613 expression, which is the position of the end of the paragraph. (The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2614 `(end-of-paragraph-text)' function uses `forward-paragraph', which we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2615 will discuss shortly.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2616 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2617 Emacs next evaluates the body of the `let', which is an `if' expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2618 that looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2619 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2620 (if (re-search-forward sentence-end par-end t) ; if-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2621 (skip-chars-backward " \t\n") ; then-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2622 (goto-char par-end))) ; else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2623 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2624 The `if' tests whether its first argument is true and if so, evaluates |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2625 its then-part; otherwise, the Emacs Lisp interpreter evaluates the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2626 else-part. The true-or-false-test of the `if' expression is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2627 regular expression search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2628 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2629 It may seem odd to have what looks like the `real work' of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2630 `forward-sentence' function buried here, but this is a common way this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2631 kind of operation is carried out in Lisp. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2632 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2633 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2634 File: eintr, Node: fwd-sentence re-search, Prev: fwd-sentence while loops, Up: forward-sentence |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2635 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2636 The regular expression search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2637 ----------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2638 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2639 The `re-search-forward' function searches for the end of the sentence, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2640 that is, for the pattern defined by the `sentence-end' regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2641 expression. If the pattern is found--if the end of the sentence is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2642 found--then the `re-search-forward' function does two things: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2643 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2644 1. The `re-search-forward' function carries out a side effect, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2645 is to move point to the end of the occurrence found. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2646 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2647 2. The `re-search-forward' function returns a value of true. This is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2648 the value received by the `if', and means that the search was |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2649 successful. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2650 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2651 The side effect, the movement of point, is completed before the `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2652 function is handed the value returned by the successful conclusion of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2653 the search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2654 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2655 When the `if' function receives the value of true from a successful |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2656 call to `re-search-forward', the `if' evaluates the then-part, which is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2657 the expression `(skip-chars-backward " \t\n")'. This expression moves |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2658 backwards over any blank spaces, tabs or carriage returns until a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2659 printed character is found and then leaves point after the character. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2660 Since point has already been moved to the end of the pattern that marks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2661 the end of the sentence, this action leaves point right after the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2662 closing printed character of the sentence, which is usually a period. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2663 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2664 On the other hand, if the `re-search-forward' function fails to find a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2665 pattern marking the end of the sentence, the function returns false. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2666 The false then causes the `if' to evaluate its third argument, which is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2667 `(goto-char par-end)': it moves point to the end of the paragraph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2668 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2669 (And if the text is in a form or equivalent, and point may not move |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2670 fully, then the `constrain-to-field' function comes into play.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2671 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2672 Regular expression searches are exceptionally useful and the pattern |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2673 illustrated by `re-search-forward', in which the search is the test of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2674 an `if' expression, is handy. You will see or write code incorporating |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2675 this pattern often. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2676 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2677 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2678 File: eintr, Node: forward-paragraph, Next: etags, Prev: forward-sentence, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2679 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2680 12.4 `forward-paragraph': a Goldmine of Functions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2681 ================================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2682 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2683 The `forward-paragraph' function moves point forward to the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2684 paragraph. It is usually bound to `M-}' and makes use of a number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2685 functions that are important in themselves, including `let*', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2686 `match-beginning', and `looking-at'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2687 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2688 The function definition for `forward-paragraph' is considerably longer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2689 than the function definition for `forward-sentence' because it works |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2690 with a paragraph, each line of which may begin with a fill prefix. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2691 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2692 A fill prefix consists of a string of characters that are repeated at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2693 the beginning of each line. For example, in Lisp code, it is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2694 convention to start each line of a paragraph-long comment with `;;; '. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2695 In Text mode, four blank spaces make up another common fill prefix, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2696 creating an indented paragraph. (*Note Fill Prefix: (emacs)Fill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2697 Prefix, for more information about fill prefixes.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2698 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2699 The existence of a fill prefix means that in addition to being able to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2700 find the end of a paragraph whose lines begin on the left-most column, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2701 the `forward-paragraph' function must be able to find the end of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2702 paragraph when all or many of the lines in the buffer begin with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2703 fill prefix. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2704 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2705 Moreover, it is sometimes practical to ignore a fill prefix that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2706 exists, especially when blank lines separate paragraphs. This is an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2707 added complication. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2709 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2710 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2711 * forward-paragraph in brief:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2712 * fwd-para let:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2713 * fwd-para while:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2714 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2715 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2716 File: eintr, Node: forward-paragraph in brief, Next: fwd-para let, Prev: forward-paragraph, Up: forward-paragraph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2717 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2718 Shortened `forward-paragraph' function definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2719 ------------------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2720 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2721 Rather than print all of the `forward-paragraph' function, we will only |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2722 print parts of it. Read without preparation, the function can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2723 daunting! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2724 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2725 In outline, the function looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2726 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2727 (defun forward-paragraph (&optional arg) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2728 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2729 (interactive "p") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2730 (or arg (setq arg 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2731 (let* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2732 VARLIST |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2733 (while (and (< arg 0) (not (bobp))) ; backward-moving-code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2734 ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2735 (while (and (> arg 0) (not (eobp))) ; forward-moving-code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2736 ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2737 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2738 The first parts of the function are routine: the function's argument |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2739 list consists of one optional argument. Documentation follows. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2740 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2741 The lower case `p' in the `interactive' declaration means that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2742 processed prefix argument, if any, is passed to the function. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2743 will be a number, and is the repeat count of how many paragraphs point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2744 will move. The `or' expression in the next line handles the common |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2745 case when no argument is passed to the function, which occurs if the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2746 function is called from other code rather than interactively. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2747 case was described earlier. (*Note The `forward-sentence' function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2748 forward-sentence.) Now we reach the end of the familiar part of this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2749 function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2750 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2751 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2752 File: eintr, Node: fwd-para let, Next: fwd-para while, Prev: forward-paragraph in brief, Up: forward-paragraph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2753 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2754 The `let*' expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2755 --------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2756 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2757 The next line of the `forward-paragraph' function begins a `let*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2758 expression. This is a different than `let'. The symbol is `let*' not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2759 `let'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2760 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2761 The `let*' special form is like `let' except that Emacs sets each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2762 variable in sequence, one after another, and variables in the latter |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2763 part of the varlist can make use of the values to which Emacs set |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2764 variables in the earlier part of the varlist. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2765 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2766 (*Note `save-excursion' in `append-to-buffer': append save-excursion.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2767 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2768 In the `let*' expression in this function, Emacs binds a total of seven |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2769 variables: `opoint', `fill-prefix-regexp', `parstart', `parsep', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2770 `sp-parstart', `start', and `found-start'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2771 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2772 The variable `parsep' appears twice, first, to remove instances of `^', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2773 and second, to handle fill prefixes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2774 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2775 The variable `opoint' is just the value of `point'. As you can guess, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2776 it is used in a `constrain-to-field' expression, just as in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2777 `forward-sentence'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2778 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2779 The variable `fill-prefix-regexp' is set to the value returned by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2780 evaluating the following list: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2781 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2782 (and fill-prefix |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2783 (not (equal fill-prefix "")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2784 (not paragraph-ignore-fill-prefix) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2785 (regexp-quote fill-prefix)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2786 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2787 This is an expression whose first element is the `and' special form. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2788 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2789 As we learned earlier (*note The `kill-new' function: kill-new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2790 function.), the `and' special form evaluates each of its arguments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2791 until one of the arguments returns a value of `nil', in which case the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2792 `and' expression returns `nil'; however, if none of the arguments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2793 returns a value of `nil', the value resulting from evaluating the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2794 argument is returned. (Since such a value is not `nil', it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2795 considered true in Lisp.) In other words, an `and' expression returns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2796 a true value only if all its arguments are true. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2797 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2798 In this case, the variable `fill-prefix-regexp' is bound to a non-`nil' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2799 value only if the following four expressions produce a true (i.e., a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2800 non-`nil') value when they are evaluated; otherwise, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2801 `fill-prefix-regexp' is bound to `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2802 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2803 `fill-prefix' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2804 When this variable is evaluated, the value of the fill prefix, if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2805 any, is returned. If there is no fill prefix, this variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2806 returns `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2807 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2808 `(not (equal fill-prefix "")' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2809 This expression checks whether an existing fill prefix is an empty |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2810 string, that is, a string with no characters in it. An empty |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2811 string is not a useful fill prefix. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2812 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2813 `(not paragraph-ignore-fill-prefix)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2814 This expression returns `nil' if the variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2815 `paragraph-ignore-fill-prefix' has been turned on by being set to a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2816 true value such as `t'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2817 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2818 `(regexp-quote fill-prefix)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2819 This is the last argument to the `and' special form. If all the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2820 arguments to the `and' are true, the value resulting from |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2821 evaluating this expression will be returned by the `and' expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2822 and bound to the variable `fill-prefix-regexp', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2823 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2824 The result of evaluating this `and' expression successfully is that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2825 `fill-prefix-regexp' will be bound to the value of `fill-prefix' as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2826 modified by the `regexp-quote' function. What `regexp-quote' does is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2827 read a string and return a regular expression that will exactly match |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2828 the string and match nothing else. This means that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2829 `fill-prefix-regexp' will be set to a value that will exactly match the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2830 fill prefix if the fill prefix exists. Otherwise, the variable will be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2831 set to `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2832 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2833 The next two local variables in the `let*' expression are designed to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2834 remove instances of `^' from `parstart' and `parsep', the local |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2835 variables indicate the paragraph start and the paragraph separator. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2836 The next expression sets `parsep' again. That is to handle fill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2837 prefixes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2838 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2839 This is the setting that requires the definition call `let*' rather |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2840 than `let'. The true-or-false-test for the `if' depends on whether the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2841 variable `fill-prefix-regexp' evaluates to `nil' or some other value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2842 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2843 If `fill-prefix-regexp' does not have a value, Emacs evaluates the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2844 else-part of the `if' expression and binds `parsep' to its local value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2845 (`parsep' is a regular expression that matches what separates |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2846 paragraphs.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2847 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2848 But if `fill-prefix-regexp' does have a value, Emacs evaluates the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2849 then-part of the `if' expression and binds `parsep' to a regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2850 expression that includes the `fill-prefix-regexp' as part of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2851 pattern. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2852 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2853 Specifically, `parsep' is set to the original value of the paragraph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2854 separate regular expression concatenated with an alternative expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2855 that consists of the `fill-prefix-regexp' followed by optional |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2856 whitespace to the end of the line. The whitespace is defined by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2857 `"[ \t]*$"'.) The `\\|' defines this portion of the regexp as an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2858 alternative to `parsep'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2859 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2860 According to a comment in the code, the next local variable, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2861 `sp-parstart', is used for searching, and then the final two, `start' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2862 and `found-start', are set to `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2863 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2864 Now we get into the body of the `let*'. The first part of the body of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2865 the `let*' deals with the case when the function is given a negative |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2866 argument and is therefore moving backwards. We will skip this section. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2867 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2868 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2869 File: eintr, Node: fwd-para while, Prev: fwd-para let, Up: forward-paragraph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2870 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2871 The forward motion `while' loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2872 ------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2873 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2874 The second part of the body of the `let*' deals with forward motion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2875 It is a `while' loop that repeats itself so long as the value of `arg' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2876 is greater than zero. In the most common use of the function, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2877 value of the argument is 1, so the body of the `while' loop is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2878 evaluated exactly once, and the cursor moves forward one paragraph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2879 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2880 This part handles three situations: when point is between paragraphs, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2881 when there is a fill prefix and when there is no fill prefix. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2882 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2883 The `while' loop looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2884 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2885 ;; going forwards and not at the end of the buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2886 (while (and (> arg 0) (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2887 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2888 ;; between paragraphs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2889 ;; Move forward over separator lines... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2890 (while (and (not (eobp)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2891 (progn (move-to-left-margin) (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2892 (looking-at parsep)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2893 (forward-line 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2894 ;; This decrements the loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2895 (unless (eobp) (setq arg (1- arg))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2896 ;; ... and one more line. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2897 (forward-line 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2898 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2899 (if fill-prefix-regexp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2900 ;; There is a fill prefix; it overrides parstart; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2901 ;; we go forward line by line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2902 (while (and (not (eobp)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2903 (progn (move-to-left-margin) (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2904 (not (looking-at parsep)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2905 (looking-at fill-prefix-regexp)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2906 (forward-line 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2907 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2908 ;; There is no fill prefix; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2909 ;; we go forward character by character |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2910 (while (and (re-search-forward sp-parstart nil 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2911 (progn (setq start (match-beginning 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2912 (goto-char start) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2913 (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2914 (progn (move-to-left-margin) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2915 (not (looking-at parsep))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2916 (or (not (looking-at parstart)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2917 (and use-hard-newlines |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2918 (not (get-text-property (1- start) 'hard))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2919 (forward-char 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2920 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2921 ;; and if there is no fill prefix and if we are not at the end, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2922 ;; go to whatever was found in the regular expression search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2923 ;; for sp-parstart |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2924 (if (< (point) (point-max)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2925 (goto-char start)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2926 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2927 We can see that this is a decrementing counter `while' loop, using the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2928 expression `(setq arg (1- arg))' as the decrementer. That expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2929 is not far from the `while', but is hidden in another Lisp macro, an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2930 `unless' macro. Unless we are at the end of the buffer -- that is what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2931 the `eobp' function determines; it is an abbreviation of `End Of Buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2932 P' -- we decrease the value of `arg' by one. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2933 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2934 (If we are at the end of the buffer, we cannot go forward any more and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2935 the next loop of the `while' expression will test false since the test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2936 is an `and' with `(not (eobp))'. The `not' function means exactly as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2937 you expect; it is another name for `null', a function that returns true |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2938 when its argument is false.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2939 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2940 Interestingly, the loop count is not decremented until we leave the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2941 space between paragraphs, unless we come to the end of buffer or stop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2942 seeing the local value of the paragraph separator. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2943 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2944 That second `while' also has a `(move-to-left-margin)' expression. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2945 function is self-explanatory. It is inside a `progn' expression and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2946 not the last element of its body, so it is only invoked for its side |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2947 effect, which is to move point to the left margin of the current line. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2948 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2949 The `looking-at' function is also self-explanatory; it returns true if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2950 the text after point matches the regular expression given as its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2951 argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2952 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2953 The rest of the body of the loop looks difficult at first, but makes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2954 sense as you come to understand it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2955 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2956 First consider what happens if there is a fill prefix: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2957 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2958 (if fill-prefix-regexp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2959 ;; There is a fill prefix; it overrides parstart; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2960 ;; we go forward line by line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2961 (while (and (not (eobp)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2962 (progn (move-to-left-margin) (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2963 (not (looking-at parsep)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2964 (looking-at fill-prefix-regexp)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2965 (forward-line 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2966 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2967 This expression moves point forward line by line so long as four |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2968 conditions are true: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2969 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2970 1. Point is not at the end of the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2971 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2972 2. We can move to the left margin of the text and are not at the end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2973 of the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2974 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2975 3. The text following point does not separate paragraphs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2976 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2977 4. The pattern following point is the fill prefix regular expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2978 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2979 The last condition may be puzzling, until you remember that point was |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2980 moved to the beginning of the line early in the `forward-paragraph' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2981 function. This means that if the text has a fill prefix, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2982 `looking-at' function will see it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2983 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2984 Consider what happens when there is no fill prefix. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2985 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2986 (while (and (re-search-forward sp-parstart nil 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2987 (progn (setq start (match-beginning 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2988 (goto-char start) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2989 (not (eobp))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2990 (progn (move-to-left-margin) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2991 (not (looking-at parsep))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2992 (or (not (looking-at parstart)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2993 (and use-hard-newlines |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2994 (not (get-text-property (1- start) 'hard))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2995 (forward-char 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2996 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2997 This `while' loop has us searching forward for `sp-parstart', which is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2998 the combination of possible whitespace with a the local value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
2999 start of a paragraph or of a paragraph separator. (The latter two are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3000 within an expression starting `\(?:' so that they are not referenced by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3001 the `match-beginning' function.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3002 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3003 The two expressions, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3004 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3005 (setq start (match-beginning 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3006 (goto-char start) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3007 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3008 mean go to the start of the text matched by the regular expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3009 search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3010 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3011 The `(match-beginning 0)' expression is new. It returns a number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3012 specifying the location of the start of the text that was matched by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3013 the last search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3014 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3015 The `match-beginning' function is used here because of a characteristic |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3016 of a forward search: a successful forward search, regardless of whether |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3017 it is a plain search or a regular expression search, moves point to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3018 end of the text that is found. In this case, a successful search moves |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3019 point to the end of the pattern for `sp-parstart'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3020 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3021 However, we want to put point at the end of the current paragraph, not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3022 somewhere else. Indeed, since the search possibly includes the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3023 paragraph separator, point may end up at the beginning of the next one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3024 unless we use an expression that includes `match-beginning'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3025 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3026 When given an argument of 0, `match-beginning' returns the position |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3027 that is the start of the text matched by the most recent search. In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3028 this case, the most recent search looks for `sp-parstart'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3029 `(match-beginning 0)' expression returns the beginning position of that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3030 pattern, rather than the end position of that pattern. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3031 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3032 (Incidentally, when passed a positive number as an argument, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3033 `match-beginning' function returns the location of point at that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3034 parenthesized expression in the last search unless that parenthesized |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3035 expression begins with `\(?:'. I don't know why `\(?:' appears here |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3036 since the argument is 0.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3037 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3038 The last expression when there is no fill prefix is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3039 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3040 (if (< (point) (point-max)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3041 (goto-char start)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3042 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3043 This says that if there is no fill prefix and if we are not at the end, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3044 point should move to the beginning of whatever was found by the regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3045 expression search for `sp-parstart'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3046 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3047 The full definition for the `forward-paragraph' function not only |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3048 includes code for going forwards, but also code for going backwards. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3049 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3050 If you are reading this inside of GNU Emacs and you want to see the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3051 whole function, you can type `C-h f' (`describe-function') and the name |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3052 of the function. This gives you the function documentation and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3053 name of the library containing the function's source. Place point over |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3054 the name of the library and press the RET key; you will be taken |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3055 directly to the source. (Be sure to install your sources! Without |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3056 them, you are like a person who tries to drive a car with his eyes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3057 shut!) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3058 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3059 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3060 File: eintr, Node: etags, Next: Regexp Review, Prev: forward-paragraph, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3061 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3062 12.5 Create Your Own `TAGS' File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3063 ================================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3064 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3065 Besides `C-h f' (`describe-function'), another way to see the source of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3066 a function is to type `M-.' (`find-tag') and the name of the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3067 when prompted for it. This is a good habit to get into. This will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3068 take you directly to the source. If the `find-tag' function first asks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3069 you for the name of a `TAGS' table, give it the name of a `TAGS' file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3070 such as `/usr/local/src/emacs/src/TAGS'. (The exact path to your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3071 `TAGS' file depends on how your copy of Emacs was installed. I just |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3072 told you the location that provides both my C and my Emacs Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3073 sources.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3074 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3075 You can also create your own `TAGS' file for directories that lack one. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3076 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3077 The `M-.' (`find-tag') command takes you directly to the source for a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3078 function, variable, node, or other source. The function depends on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3079 tags tables to tell it where to go. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3080 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3081 You often need to build and install tags tables yourself. They are not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3082 built automatically. A tags table is called a `TAGS' file; the name is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3083 in upper case letters. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3084 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3085 You can create a `TAGS' file by calling the `etags' program that comes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3086 as a part of the Emacs distribution. Usually, `etags' is compiled and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3087 installed when Emacs is built. (`etags' is not an Emacs Lisp function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3088 or a part of Emacs; it is a C program.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3089 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3090 To create a `TAGS' file, first switch to the directory in which you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3091 want to create the file. In Emacs you can do this with the `M-x cd' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3092 command, or by visiting a file in the directory, or by listing the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3093 directory with `C-x d' (`dired'). Then run the compile command, with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3094 `etags *.el' as the command to execute |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3095 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3096 M-x compile RET etags *.el RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3097 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3098 to create a `TAGS' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3099 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3100 For example, if you have a large number of files in your `~/emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3101 directory, as I do--I have 137 `.el' files in it, of which I load |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3102 12--you can create a `TAGS' file for the Emacs Lisp files in that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3103 directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3104 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3105 The `etags' program takes all the usual shell `wildcards'. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3106 example, if you have two directories for which you want a single `TAGS |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3107 file', type `etags *.el ../elisp/*.el', where `../elisp/' is the second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3108 directory: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3109 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3110 M-x compile RET etags *.el ../elisp/*.el RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3111 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3112 Type |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3113 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3114 M-x compile RET etags --help RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3115 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3116 to see a list of the options accepted by `etags' as well as a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3117 supported languages. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3118 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3119 The `etags' program handles more than 20 languages, including Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3120 Lisp, Common Lisp, Scheme, C, C++, Ada, Fortran, Java, LaTeX, Pascal, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3121 Perl, Python, Texinfo, makefiles, and most assemblers. The program has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3122 no switches for specifying the language; it recognizes the language in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3123 an input file according to its file name and contents. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3124 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3125 `etags' is very helpful when you are writing code yourself and want to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3126 refer back to functions you have already written. Just run `etags' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3127 again at intervals as you write new functions, so they become part of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3128 the `TAGS' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3129 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3130 If you think an appropriate `TAGS' file already exists for what you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3131 want, but do not know where it is, you can use the `locate' program to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3132 attempt to find it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3133 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3134 Type `M-x locate <RET> TAGS <RET>' and Emacs will list for you the full |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3135 path names of all your `TAGS' files. On my system, this command lists |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3136 34 `TAGS' files. On the other hand, a `plain vanilla' system I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3137 recently installed did not contain any `TAGS' files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3138 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3139 If the tags table you want has been created, you can use the `M-x |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3140 visit-tags-table' command to specify it. Otherwise, you will need to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3141 create the tag table yourself and then use `M-x visit-tags-table'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3142 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3143 Building Tags in the Emacs sources |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3144 .................................. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3145 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3146 The GNU Emacs sources come with a `Makefile' that contains a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3147 sophisticated `etags' command that creates, collects, and merges tags |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3148 tables from all over the Emacs sources and puts the information into |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3149 one `TAGS' file in the `src/' directory below the top level of your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3150 Emacs source directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3151 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3152 To build this `TAGS' file, go to the top level of your Emacs source |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3153 directory and run the compile command `make tags': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3154 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3155 M-x compile RET make tags RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3156 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3157 (The `make tags' command works well with the GNU Emacs sources, as well |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3158 as with some other source packages.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3159 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3160 For more information, see *Note Tag Tables: (emacs)Tags. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3161 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3162 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3163 File: eintr, Node: Regexp Review, Next: re-search Exercises, Prev: etags, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3164 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3165 12.6 Review |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3166 =========== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3167 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3168 Here is a brief summary of some recently introduced functions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3169 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3170 `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3171 Repeatedly evaluate the body of the expression so long as the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3172 element of the body tests true. Then return `nil'. (The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3173 expression is evaluated only for its side effects.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3174 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3175 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3176 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3177 (let ((foo 2)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3178 (while (> foo 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3179 (insert (format "foo is %d.\n" foo)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3180 (setq foo (1- foo)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3181 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3182 => foo is 2. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3183 foo is 1. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3184 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3185 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3186 (The `insert' function inserts its arguments at point; the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3187 `format' function returns a string formatted from its arguments |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3188 the way `message' formats its arguments; `\n' produces a new line.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3189 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3190 `re-search-forward' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3191 Search for a pattern, and if the pattern is found, move point to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3192 rest just after it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3193 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3194 Takes four arguments, like `search-forward': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3195 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3196 1. A regular expression that specifies the pattern to search for. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3197 (Remember to put quotation marks around this argument!) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3198 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3199 2. Optionally, the limit of the search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3200 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3201 3. Optionally, what to do if the search fails, return `nil' or an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3202 error message. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3203 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3204 4. Optionally, how many times to repeat the search; if negative, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3205 the search goes backwards. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3206 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3207 `let*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3208 Bind some variables locally to particular values, and then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3209 evaluate the remaining arguments, returning the value of the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3210 one. While binding the local variables, use the local values of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3211 variables bound earlier, if any. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3212 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3213 For example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3214 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3215 (let* ((foo 7) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3216 (bar (* 3 foo))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3217 (message "`bar' is %d." bar)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3218 => `bar' is 21. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3219 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3220 `match-beginning' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3221 Return the position of the start of the text found by the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3222 regular expression search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3223 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3224 `looking-at' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3225 Return `t' for true if the text after point matches the argument, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3226 which should be a regular expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3227 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3228 `eobp' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3229 Return `t' for true if point is at the end of the accessible part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3230 of a buffer. The end of the accessible part is the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3231 buffer if the buffer is not narrowed; it is the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3232 narrowed part if the buffer is narrowed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3233 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3234 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3235 File: eintr, Node: re-search Exercises, Prev: Regexp Review, Up: Regexp Search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3236 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3237 12.7 Exercises with `re-search-forward' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3238 ======================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3239 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3240 * Write a function to search for a regular expression that matches |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3241 two or more blank lines in sequence. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3242 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3243 * Write a function to search for duplicated words, such as `the the'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3244 *Note Syntax of Regular Expressions: (emacs)Regexps, for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3245 information on how to write a regexp (a regular expression) to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3246 match a string that is composed of two identical halves. You can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3247 devise several regexps; some are better than others. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3248 I use is described in an appendix, along with several regexps. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3249 *Note `the-the' Duplicated Words Function: the-the. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3250 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3251 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3252 File: eintr, Node: Counting Words, Next: Words in a defun, Prev: Regexp Search, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3253 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3254 13 Counting: Repetition and Regexps |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3255 *********************************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3256 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3257 Repetition and regular expression searches are powerful tools that you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3258 often use when you write code in Emacs Lisp. This chapter illustrates |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3259 the use of regular expression searches through the construction of word |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3260 count commands using `while' loops and recursion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3261 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3262 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3263 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3264 * Why Count Words:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3265 * count-words-region:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3266 * recursive-count-words:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3267 * Counting Exercise:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3268 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3269 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3270 File: eintr, Node: Why Count Words, Next: count-words-region, Prev: Counting Words, Up: Counting Words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3271 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3272 Counting words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3273 ============== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3274 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3275 The standard Emacs distribution contains a function for counting the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3276 number of lines within a region. However, there is no corresponding |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3277 function for counting words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3278 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3279 Certain types of writing ask you to count words. Thus, if you write an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3280 essay, you may be limited to 800 words; if you write a novel, you may |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3281 discipline yourself to write 1000 words a day. It seems odd to me that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3282 Emacs lacks a word count command. Perhaps people use Emacs mostly for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3283 code or types of documentation that do not require word counts; or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3284 perhaps they restrict themselves to the operating system word count |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3285 command, `wc'. Alternatively, people may follow the publishers' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3286 convention and compute a word count by dividing the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3287 characters in a document by five. In any event, here are commands to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3288 count words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3289 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3290 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3291 File: eintr, Node: count-words-region, Next: recursive-count-words, Prev: Why Count Words, Up: Counting Words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3292 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3293 13.1 The `count-words-region' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3294 ====================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3295 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3296 A word count command could count words in a line, paragraph, region, or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3297 buffer. What should the command cover? You could design the command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3298 to count the number of words in a complete buffer. However, the Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3299 tradition encourages flexibility--you may want to count words in just a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3300 section, rather than all of a buffer. So it makes more sense to design |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3301 the command to count the number of words in a region. Once you have a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3302 `count-words-region' command, you can, if you wish, count words in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3303 whole buffer by marking it with `C-x h' (`mark-whole-buffer'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3304 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3305 Clearly, counting words is a repetitive act: starting from the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3306 beginning of the region, you count the first word, then the second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3307 word, then the third word, and so on, until you reach the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3308 region. This means that word counting is ideally suited to recursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3309 or to a `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3310 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3311 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3312 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3313 * Design count-words-region:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3314 * Whitespace Bug:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3315 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3316 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3317 File: eintr, Node: Design count-words-region, Next: Whitespace Bug, Prev: count-words-region, Up: count-words-region |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3318 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3319 Designing `count-words-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3320 ------------------------------ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3321 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3322 First, we will implement the word count command with a `while' loop, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3323 then with recursion. The command will, of course, be interactive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3324 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3325 The template for an interactive function definition is, as always: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3326 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3327 (defun NAME-OF-FUNCTION (ARGUMENT-LIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3328 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3329 (INTERACTIVE-EXPRESSION...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3330 BODY...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3331 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3332 What we need to do is fill in the slots. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3333 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3334 The name of the function should be self-explanatory and similar to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3335 existing `count-lines-region' name. This makes the name easier to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3336 remember. `count-words-region' is a good choice. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3337 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3338 The function counts words within a region. This means that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3339 argument list must contain symbols that are bound to the two positions, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3340 the beginning and end of the region. These two positions can be called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3341 `beginning' and `end' respectively. The first line of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3342 documentation should be a single sentence, since that is all that is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3343 printed as documentation by a command such as `apropos'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3344 interactive expression will be of the form `(interactive "r")', since |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3345 that will cause Emacs to pass the beginning and end of the region to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3346 the function's argument list. All this is routine. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3348 The body of the function needs to be written to do three tasks: first, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3349 to set up conditions under which the `while' loop can count words, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3350 second, to run the `while' loop, and third, to send a message to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3351 user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3352 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3353 When a user calls `count-words-region', point may be at the beginning |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3354 or the end of the region. However, the counting process must start at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3355 the beginning of the region. This means we will want to put point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3356 there if it is not already there. Executing `(goto-char beginning)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3357 ensures this. Of course, we will want to return point to its expected |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3358 position when the function finishes its work. For this reason, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3359 body must be enclosed in a `save-excursion' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3360 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3361 The central part of the body of the function consists of a `while' loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3362 in which one expression jumps point forward word by word, and another |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3363 expression counts those jumps. The true-or-false-test of the `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3364 loop should test true so long as point should jump forward, and false |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3365 when point is at the end of the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3366 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3367 We could use `(forward-word 1)' as the expression for moving point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3368 forward word by word, but it is easier to see what Emacs identifies as a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3369 `word' if we use a regular expression search. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3370 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3371 A regular expression search that finds the pattern for which it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3372 searching leaves point after the last character matched. This means |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3373 that a succession of successful word searches will move point forward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3374 word by word. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3375 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3376 As a practical matter, we want the regular expression search to jump |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3377 over whitespace and punctuation between words as well as over the words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3378 themselves. A regexp that refuses to jump over interword whitespace |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3379 would never jump more than one word! This means that the regexp should |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3380 include the whitespace and punctuation that follows a word, if any, as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3381 well as the word itself. (A word may end a buffer and not have any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3382 following whitespace or punctuation, so that part of the regexp must be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3383 optional.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3384 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3385 Thus, what we want for the regexp is a pattern defining one or more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3386 word constituent characters followed, optionally, by one or more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3387 characters that are not word constituents. The regular expression for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3388 this is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3389 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3390 \w+\W* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3391 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3392 The buffer's syntax table determines which characters are and are not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3393 word constituents. (*Note What Constitutes a Word or Symbol?: Syntax, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3394 for more about syntax. Also, see *Note Syntax: (emacs)Syntax, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3395 *Note Syntax Tables: (elisp)Syntax Tables.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3396 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3397 The search expression looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3398 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3399 (re-search-forward "\\w+\\W*") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3400 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3401 (Note that paired backslashes precede the `w' and `W'. A single |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3402 backslash has special meaning to the Emacs Lisp interpreter. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3403 indicates that the following character is interpreted differently than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3404 usual. For example, the two characters, `\n', stand for `newline', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3405 rather than for a backslash followed by `n'. Two backslashes in a row |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3406 stand for an ordinary, `unspecial' backslash, which in this case is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3407 followed by a letter, the combination of which is important to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3408 `re-search-forward'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3409 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3410 We need a counter to count how many words there are; this variable must |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3411 first be set to 0 and then incremented each time Emacs goes around the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3412 `while' loop. The incrementing expression is simply: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3413 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3414 (setq count (1+ count)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3415 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3416 Finally, we want to tell the user how many words there are in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3417 region. The `message' function is intended for presenting this kind of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3418 information to the user. The message has to be phrased so that it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3419 reads properly regardless of how many words there are in the region: we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3420 don't want to say that "there are 1 words in the region". The conflict |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3421 between singular and plural is ungrammatical. We can solve this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3422 problem by using a conditional expression that evaluates different |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3423 messages depending on the number of words in the region. There are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3424 three possibilities: no words in the region, one word in the region, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3425 and more than one word. This means that the `cond' special form is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3426 appropriate. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3427 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3428 All this leads to the following function definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3429 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3430 ;;; First version; has bugs! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3431 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3432 "Print number of words in the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3433 Words are defined as at least one word-constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3434 character followed by at least one character that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3435 is not a word-constituent. The buffer's syntax |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3436 table determines which characters these are." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3437 (interactive "r") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3438 (message "Counting words in region ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3439 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3440 ;;; 1. Set up appropriate conditions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3441 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3442 (goto-char beginning) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3443 (let ((count 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3444 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3445 ;;; 2. Run the while loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3446 (while (< (point) end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3447 (re-search-forward "\\w+\\W*") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3448 (setq count (1+ count))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3449 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3450 ;;; 3. Send a message to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3451 (cond ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3452 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3453 "The region does NOT have any words.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3454 ((= 1 count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3455 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3456 "The region has 1 word.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3457 (t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3458 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3459 "The region has %d words." count)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3460 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3461 As written, the function works, but not in all circumstances. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3462 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3463 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3464 File: eintr, Node: Whitespace Bug, Prev: Design count-words-region, Up: count-words-region |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3465 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3466 13.1.1 The Whitespace Bug in `count-words-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3467 ------------------------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3468 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3469 The `count-words-region' command described in the preceding section has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3470 two bugs, or rather, one bug with two manifestations. First, if you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3471 mark a region containing only whitespace in the middle of some text, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3472 the `count-words-region' command tells you that the region contains one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3473 word! Second, if you mark a region containing only whitespace at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3474 end of the buffer or the accessible portion of a narrowed buffer, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3475 command displays an error message that looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3476 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3477 Search failed: "\\w+\\W*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3478 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3479 If you are reading this in Info in GNU Emacs, you can test for these |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3480 bugs yourself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3481 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3482 First, evaluate the function in the usual manner to install it. Here |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3483 is a copy of the definition. Place your cursor after the closing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3484 parenthesis and type `C-x C-e' to install it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3485 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3486 ;; First version; has bugs! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3487 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3488 "Print number of words in the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3489 Words are defined as at least one word-constituent character followed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3490 by at least one character that is not a word-constituent. The buffer's |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3491 syntax table determines which characters these are." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3492 (interactive "r") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3493 (message "Counting words in region ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3494 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3495 ;;; 1. Set up appropriate conditions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3496 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3497 (goto-char beginning) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3498 (let ((count 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3499 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3500 ;;; 2. Run the while loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3501 (while (< (point) end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3502 (re-search-forward "\\w+\\W*") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3503 (setq count (1+ count))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3504 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3505 ;;; 3. Send a message to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3506 (cond ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3507 (message "The region does NOT have any words.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3508 ((= 1 count) (message "The region has 1 word.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3509 (t (message "The region has %d words." count)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3510 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3511 If you wish, you can also install this keybinding by evaluating it: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3512 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3513 (global-set-key "\C-c=" 'count-words-region) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3514 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3515 To conduct the first test, set mark and point to the beginning and end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3516 of the following line and then type `C-c =' (or `M-x |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3517 count-words-region' if you have not bound `C-c ='): |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3518 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3519 one two three |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3520 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3521 Emacs will tell you, correctly, that the region has three words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3522 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3523 Repeat the test, but place mark at the beginning of the line and place |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3524 point just _before_ the word `one'. Again type the command `C-c =' (or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3525 `M-x count-words-region'). Emacs should tell you that the region has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3526 no words, since it is composed only of the whitespace at the beginning |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3527 of the line. But instead Emacs tells you that the region has one word! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3528 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3529 For the third test, copy the sample line to the end of the `*scratch*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3530 buffer and then type several spaces at the end of the line. Place mark |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3531 right after the word `three' and point at the end of line. (The end of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3532 the line will be the end of the buffer.) Type `C-c =' (or `M-x |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3533 count-words-region') as you did before. Again, Emacs should tell you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3534 that the region has no words, since it is composed only of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3535 whitespace at the end of the line. Instead, Emacs displays an error |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3536 message saying `Search failed'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3537 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3538 The two bugs stem from the same problem. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3539 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3540 Consider the first manifestation of the bug, in which the command tells |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3541 you that the whitespace at the beginning of the line contains one word. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3542 What happens is this: The `M-x count-words-region' command moves point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3543 to the beginning of the region. The `while' tests whether the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3544 point is smaller than the value of `end', which it is. Consequently, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3545 the regular expression search looks for and finds the first word. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3546 leaves point after the word. `count' is set to one. The `while' loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3547 repeats; but this time the value of point is larger than the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3548 `end', the loop is exited; and the function displays a message saying |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3549 the number of words in the region is one. In brief, the regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3550 expression search looks for and finds the word even though it is outside |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3551 the marked region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3552 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3553 In the second manifestation of the bug, the region is whitespace at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3554 end of the buffer. Emacs says `Search failed'. What happens is that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3555 the true-or-false-test in the `while' loop tests true, so the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3556 expression is executed. But since there are no more words in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3557 buffer, the search fails. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3558 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3559 In both manifestations of the bug, the search extends or attempts to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3560 extend outside of the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3561 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3562 The solution is to limit the search to the region--this is a fairly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3563 simple action, but as you may have come to expect, it is not quite as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3564 simple as you might think. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3565 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3566 As we have seen, the `re-search-forward' function takes a search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3567 pattern as its first argument. But in addition to this first, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3568 mandatory argument, it accepts three optional arguments. The optional |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3569 second argument bounds the search. The optional third argument, if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3570 `t', causes the function to return `nil' rather than signal an error if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3571 the search fails. The optional fourth argument is a repeat count. (In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3572 Emacs, you can see a function's documentation by typing `C-h f', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3573 name of the function, and then <RET>.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3574 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3575 In the `count-words-region' definition, the value of the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3576 region is held by the variable `end' which is passed as an argument to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3577 the function. Thus, we can add `end' as an argument to the regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3578 expression search expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3579 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3580 (re-search-forward "\\w+\\W*" end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3581 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3582 However, if you make only this change to the `count-words-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3583 definition and then test the new version of the definition on a stretch |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3584 of whitespace, you will receive an error message saying `Search failed'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3585 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3586 What happens is this: the search is limited to the region, and fails as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3587 you expect because there are no word-constituent characters in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3588 region. Since it fails, we receive an error message. But we do not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3589 want to receive an error message in this case; we want to receive the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3590 message that "The region does NOT have any words." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3591 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3592 The solution to this problem is to provide `re-search-forward' with a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3593 third argument of `t', which causes the function to return `nil' rather |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3594 than signal an error if the search fails. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3595 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3596 However, if you make this change and try it, you will see the message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3597 "Counting words in region ... " and ... you will keep on seeing that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3598 message ..., until you type `C-g' (`keyboard-quit'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3599 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3600 Here is what happens: the search is limited to the region, as before, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3601 and it fails because there are no word-constituent characters in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3602 region, as expected. Consequently, the `re-search-forward' expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3603 returns `nil'. It does nothing else. In particular, it does not move |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3604 point, which it does as a side effect if it finds the search target. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3605 After the `re-search-forward' expression returns `nil', the next |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3606 expression in the `while' loop is evaluated. This expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3607 increments the count. Then the loop repeats. The true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3608 tests true because the value of point is still less than the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3609 end, since the `re-search-forward' expression did not move point. ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3610 and the cycle repeats ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3611 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3612 The `count-words-region' definition requires yet another modification, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3613 to cause the true-or-false-test of the `while' loop to test false if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3614 the search fails. Put another way, there are two conditions that must |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3615 be satisfied in the true-or-false-test before the word count variable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3616 is incremented: point must still be within the region and the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3617 expression must have found a word to count. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3618 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3619 Since both the first condition and the second condition must be true |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3620 together, the two expressions, the region test and the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3621 expression, can be joined with an `and' special form and embedded in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3622 the `while' loop as the true-or-false-test, like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3623 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3624 (and (< (point) end) (re-search-forward "\\w+\\W*" end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3625 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3626 (*Note The `kill-new' function: kill-new function, for information |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3627 about `and'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3628 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3629 The `re-search-forward' expression returns `t' if the search succeeds |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3630 and as a side effect moves point. Consequently, as words are found, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3631 point is moved through the region. When the search expression fails to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3632 find another word, or when point reaches the end of the region, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3633 true-or-false-test tests false, the `while' loop exits, and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3634 `count-words-region' function displays one or other of its messages. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3635 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3636 After incorporating these final changes, the `count-words-region' works |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3637 without bugs (or at least, without bugs that I have found!). Here is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3638 what it looks like: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3639 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3640 ;;; Final version: `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3641 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3642 "Print number of words in the region." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3643 (interactive "r") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3644 (message "Counting words in region ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3645 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3646 ;;; 1. Set up appropriate conditions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3647 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3648 (let ((count 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3649 (goto-char beginning) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3650 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3651 ;;; 2. Run the while loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3652 (while (and (< (point) end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3653 (re-search-forward "\\w+\\W*" end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3654 (setq count (1+ count))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3655 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3656 ;;; 3. Send a message to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3657 (cond ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3658 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3659 "The region does NOT have any words.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3660 ((= 1 count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3661 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3662 "The region has 1 word.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3663 (t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3664 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3665 "The region has %d words." count)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3666 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3667 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3668 File: eintr, Node: recursive-count-words, Next: Counting Exercise, Prev: count-words-region, Up: Counting Words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3669 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3670 13.2 Count Words Recursively |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3671 ============================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3672 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3673 You can write the function for counting words recursively as well as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3674 with a `while' loop. Let's see how this is done. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3675 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3676 First, we need to recognize that the `count-words-region' function has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3677 three jobs: it sets up the appropriate conditions for counting to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3678 occur; it counts the words in the region; and it sends a message to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3679 user telling how many words there are. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3680 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3681 If we write a single recursive function to do everything, we will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3682 receive a message for every recursive call. If the region contains 13 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3683 words, we will receive thirteen messages, one right after the other. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3684 We don't want this! Instead, we must write two functions to do the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3685 job, one of which (the recursive function) will be used inside of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3686 other. One function will set up the conditions and display the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3687 message; the other will return the word count. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3688 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3689 Let us start with the function that causes the message to be displayed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3690 We can continue to call this `count-words-region'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3691 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3692 This is the function that the user will call. It will be interactive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3693 Indeed, it will be similar to our previous versions of this function, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3694 except that it will call `recursive-count-words' to determine how many |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3695 words are in the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3696 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3697 We can readily construct a template for this function, based on our |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3698 previous versions: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3699 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3700 ;; Recursive version; uses regular expression search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3701 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3702 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3703 (INTERACTIVE-EXPRESSION...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3704 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3705 ;;; 1. Set up appropriate conditions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3706 (EXPLANATORY MESSAGE) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3707 (SET-UP FUNCTIONS... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3709 ;;; 2. Count the words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3710 RECURSIVE CALL |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3711 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3712 ;;; 3. Send a message to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3713 MESSAGE PROVIDING WORD COUNT)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3714 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3715 The definition looks straightforward, except that somehow the count |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3716 returned by the recursive call must be passed to the message displaying |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3717 the word count. A little thought suggests that this can be done by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3718 making use of a `let' expression: we can bind a variable in the varlist |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3719 of a `let' expression to the number of words in the region, as returned |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3720 by the recursive call; and then the `cond' expression, using binding, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3721 can display the value to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3722 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3723 Often, one thinks of the binding within a `let' expression as somehow |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3724 secondary to the `primary' work of a function. But in this case, what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3725 you might consider the `primary' job of the function, counting words, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3726 is done within the `let' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3727 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3728 Using `let', the function definition looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3729 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3730 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3731 "Print number of words in the region." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3732 (interactive "r") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3733 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3734 ;;; 1. Set up appropriate conditions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3735 (message "Counting words in region ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3736 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3737 (goto-char beginning) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3738 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3739 ;;; 2. Count the words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3740 (let ((count (recursive-count-words end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3741 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3742 ;;; 3. Send a message to the user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3743 (cond ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3744 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3745 "The region does NOT have any words.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3746 ((= 1 count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3747 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3748 "The region has 1 word.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3749 (t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3750 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3751 "The region has %d words." count)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3752 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3753 Next, we need to write the recursive counting function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3754 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3755 A recursive function has at least three parts: the `do-again-test', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3756 `next-step-expression', and the recursive call. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3757 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3758 The do-again-test determines whether the function will or will not be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3759 called again. Since we are counting words in a region and can use a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3760 function that moves point forward for every word, the do-again-test can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3761 check whether point is still within the region. The do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3762 should find the value of point and determine whether point is before, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3763 at, or after the value of the end of the region. We can use the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3764 `point' function to locate point. Clearly, we must pass the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3765 the end of the region to the recursive counting function as an argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3766 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3767 In addition, the do-again-test should also test whether the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3768 finds a word. If it does not, the function should not call itself |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3769 again. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3770 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3771 The next-step-expression changes a value so that when the recursive |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3772 function is supposed to stop calling itself, it stops. More precisely, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3773 the next-step-expression changes a value so that at the right time, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3774 do-again-test stops the recursive function from calling itself again. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3775 In this case, the next-step-expression can be the expression that moves |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3776 point forward, word by word. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3777 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3778 The third part of a recursive function is the recursive call. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3779 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3780 Somewhere, also, we also need a part that does the `work' of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3781 function, a part that does the counting. A vital part! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3782 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3783 But already, we have an outline of the recursive counting function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3784 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3785 (defun recursive-count-words (region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3786 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3787 DO-AGAIN-TEST |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3788 NEXT-STEP-EXPRESSION |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3789 RECURSIVE CALL) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3790 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3791 Now we need to fill in the slots. Let's start with the simplest cases |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3792 first: if point is at or beyond the end of the region, there cannot be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3793 any words in the region, so the function should return zero. Likewise, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3794 if the search fails, there are no words to count, so the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3795 should return zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3796 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3797 On the other hand, if point is within the region and the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3798 succeeds, the function should call itself again. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3799 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3800 Thus, the do-again-test should look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3801 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3802 (and (< (point) region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3803 (re-search-forward "\\w+\\W*" region-end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3804 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3805 Note that the search expression is part of the do-again-test--the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3806 function returns `t' if its search succeeds and `nil' if it fails. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3807 (*Note The Whitespace Bug in `count-words-region': Whitespace Bug, for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3808 an explanation of how `re-search-forward' works.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3809 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3810 The do-again-test is the true-or-false test of an `if' clause. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3811 Clearly, if the do-again-test succeeds, the then-part of the `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3812 clause should call the function again; but if it fails, the else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3813 should return zero since either point is outside the region or the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3814 search failed because there were no words to find. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3815 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3816 But before considering the recursive call, we need to consider the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3817 next-step-expression. What is it? Interestingly, it is the search |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3818 part of the do-again-test. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3819 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3820 In addition to returning `t' or `nil' for the do-again-test, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3821 `re-search-forward' moves point forward as a side effect of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3822 successful search. This is the action that changes the value of point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3823 so that the recursive function stops calling itself when point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3824 completes its movement through the region. Consequently, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3825 `re-search-forward' expression is the next-step-expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3826 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3827 In outline, then, the body of the `recursive-count-words' function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3828 looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3829 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3830 (if DO-AGAIN-TEST-AND-NEXT-STEP-COMBINED |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3831 ;; then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3832 RECURSIVE-CALL-RETURNING-COUNT |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3833 ;; else |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3834 RETURN-ZERO) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3835 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3836 How to incorporate the mechanism that counts? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3837 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3838 If you are not used to writing recursive functions, a question like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3839 this can be troublesome. But it can and should be approached |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3840 systematically. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3841 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3842 We know that the counting mechanism should be associated in some way |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3843 with the recursive call. Indeed, since the next-step-expression moves |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3844 point forward by one word, and since a recursive call is made for each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3845 word, the counting mechanism must be an expression that adds one to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3846 value returned by a call to `recursive-count-words'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3847 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3848 Consider several cases: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3849 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3850 * If there are two words in the region, the function should return a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3851 value resulting from adding one to the value returned when it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3852 counts the first word, plus the number returned when it counts the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3853 remaining words in the region, which in this case is one. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3854 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3855 * If there is one word in the region, the function should return a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3856 value resulting from adding one to the value returned when it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3857 counts that word, plus the number returned when it counts the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3858 remaining words in the region, which in this case is zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3859 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3860 * If there are no words in the region, the function should return |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3861 zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3862 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3863 From the sketch we can see that the else-part of the `if' returns zero |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3864 for the case of no words. This means that the then-part of the `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3865 must return a value resulting from adding one to the value returned |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3866 from a count of the remaining words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3867 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3868 The expression will look like this, where `1+' is a function that adds |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3869 one to its argument. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3870 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3871 (1+ (recursive-count-words region-end)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3872 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3873 The whole `recursive-count-words' function will then look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3874 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3875 (defun recursive-count-words (region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3876 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3877 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3878 ;;; 1. do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3879 (if (and (< (point) region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3880 (re-search-forward "\\w+\\W*" region-end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3881 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3882 ;;; 2. then-part: the recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3883 (1+ (recursive-count-words region-end)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3884 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3885 ;;; 3. else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3886 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3887 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3888 Let's examine how this works: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3889 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3890 If there are no words in the region, the else part of the `if' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3891 expression is evaluated and consequently the function returns zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3892 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3893 If there is one word in the region, the value of point is less than the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3894 value of `region-end' and the search succeeds. In this case, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3895 true-or-false-test of the `if' expression tests true, and the then-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3896 of the `if' expression is evaluated. The counting expression is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3897 evaluated. This expression returns a value (which will be the value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3898 returned by the whole function) that is the sum of one added to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3899 value returned by a recursive call. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3900 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3901 Meanwhile, the next-step-expression has caused point to jump over the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3902 first (and in this case only) word in the region. This means that when |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3903 `(recursive-count-words region-end)' is evaluated a second time, as a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3904 result of the recursive call, the value of point will be equal to or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3905 greater than the value of region end. So this time, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3906 `recursive-count-words' will return zero. The zero will be added to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3907 one, and the original evaluation of `recursive-count-words' will return |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3908 one plus zero, which is one, which is the correct amount. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3909 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3910 Clearly, if there are two words in the region, the first call to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3911 `recursive-count-words' returns one added to the value returned by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3912 calling `recursive-count-words' on a region containing the remaining |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3913 word--that is, it adds one to one, producing two, which is the correct |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3914 amount. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3915 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3916 Similarly, if there are three words in the region, the first call to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3917 `recursive-count-words' returns one added to the value returned by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3918 calling `recursive-count-words' on a region containing the remaining |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3919 two words--and so on and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3920 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3921 With full documentation the two functions look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3922 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3923 The recursive function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3924 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3925 (defun recursive-count-words (region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3926 "Number of words between point and REGION-END." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3927 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3928 ;;; 1. do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3929 (if (and (< (point) region-end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3930 (re-search-forward "\\w+\\W*" region-end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3931 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3932 ;;; 2. then-part: the recursive call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3933 (1+ (recursive-count-words region-end)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3934 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3935 ;;; 3. else-part |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3936 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3937 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3938 The wrapper: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3939 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3940 ;;; Recursive version |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3941 (defun count-words-region (beginning end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3942 "Print number of words in the region. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3943 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3944 Words are defined as at least one word-constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3945 character followed by at least one character that is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3946 not a word-constituent. The buffer's syntax table |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3947 determines which characters these are." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3948 (interactive "r") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3949 (message "Counting words in region ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3950 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3951 (goto-char beginning) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3952 (let ((count (recursive-count-words end))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3953 (cond ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3954 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3955 "The region does NOT have any words.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3956 ((= 1 count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3957 (message "The region has 1 word.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3958 (t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3959 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3960 "The region has %d words." count)))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3961 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3962 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3963 File: eintr, Node: Counting Exercise, Prev: recursive-count-words, Up: Counting Words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3964 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3965 13.3 Exercise: Counting Punctuation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3966 =================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3968 Using a `while' loop, write a function to count the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3969 punctuation marks in a region--period, comma, semicolon, colon, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3970 exclamation mark, and question mark. Do the same using recursion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3971 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3972 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3973 File: eintr, Node: Words in a defun, Next: Readying a Graph, Prev: Counting Words, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3974 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3975 14 Counting Words in a `defun' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3976 ****************************** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3977 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3978 Our next project is to count the number of words in a function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3979 definition. Clearly, this can be done using some variant of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3980 `count-word-region'. *Note Counting Words: Repetition and Regexps: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3981 Counting Words. If we are just going to count the words in one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3982 definition, it is easy enough to mark the definition with the `C-M-h' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3983 (`mark-defun') command, and then call `count-word-region'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3984 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3985 However, I am more ambitious: I want to count the words and symbols in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3986 every definition in the Emacs sources and then print a graph that shows |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3987 how many functions there are of each length: how many contain 40 to 49 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3988 words or symbols, how many contain 50 to 59 words or symbols, and so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3989 on. I have often been curious how long a typical function is, and this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3990 will tell. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3991 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3992 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3993 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3994 * Divide and Conquer:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3995 * Words and Symbols:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3996 * Syntax:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3997 * count-words-in-defun:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3998 * Several defuns:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
3999 * Find a File:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4000 * lengths-list-file:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4001 * Several files:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4002 * Several files recursively:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4003 * Prepare the data:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4004 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4005 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4006 File: eintr, Node: Divide and Conquer, Next: Words and Symbols, Prev: Words in a defun, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4007 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4008 Divide and Conquer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4009 ================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4010 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4011 Described in one phrase, the histogram project is daunting; but divided |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4012 into numerous small steps, each of which we can take one at a time, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4013 project becomes less fearsome. Let us consider what the steps must be: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4014 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4015 * First, write a function to count the words in one definition. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4016 includes the problem of handling symbols as well as words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4017 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4018 * Second, write a function to list the numbers of words in each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4019 function in a file. This function can use the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4020 `count-words-in-defun' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4021 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4022 * Third, write a function to list the numbers of words in each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4023 function in each of several files. This entails automatically |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4024 finding the various files, switching to them, and counting the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4025 words in the definitions within them. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4026 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4027 * Fourth, write a function to convert the list of numbers that we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4028 created in step three to a form that will be suitable for printing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4029 as a graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4030 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4031 * Fifth, write a function to print the results as a graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4032 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4033 This is quite a project! But if we take each step slowly, it will not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4034 be difficult. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4035 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4036 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4037 File: eintr, Node: Words and Symbols, Next: Syntax, Prev: Divide and Conquer, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4038 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4039 14.1 What to Count? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4040 =================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4041 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4042 When we first start thinking about how to count the words in a function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4043 definition, the first question is (or ought to be) what are we going to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4044 count? When we speak of `words' with respect to a Lisp function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4045 definition, we are actually speaking, in large part, of `symbols'. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4046 example, the following `multiply-by-seven' function contains the five |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4047 symbols `defun', `multiply-by-seven', `number', `*', and `7'. In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4048 addition, in the documentation string, it contains the four words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4049 `Multiply', `NUMBER', `by', and `seven'. The symbol `number' is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4050 repeated, so the definition contains a total of ten words and symbols. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4051 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4052 (defun multiply-by-seven (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4053 "Multiply NUMBER by seven." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4054 (* 7 number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4055 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4056 However, if we mark the `multiply-by-seven' definition with `C-M-h' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4057 (`mark-defun'), and then call `count-words-region' on it, we will find |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4058 that `count-words-region' claims the definition has eleven words, not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4059 ten! Something is wrong! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4060 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4061 The problem is twofold: `count-words-region' does not count the `*' as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4062 a word, and it counts the single symbol, `multiply-by-seven', as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4063 containing three words. The hyphens are treated as if they were |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4064 interword spaces rather than intraword connectors: `multiply-by-seven' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4065 is counted as if it were written `multiply by seven'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4066 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4067 The cause of this confusion is the regular expression search within the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4068 `count-words-region' definition that moves point forward word by word. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4069 In the canonical version of `count-words-region', the regexp is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4070 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4071 "\\w+\\W*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4072 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4073 This regular expression is a pattern defining one or more word |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4074 constituent characters possibly followed by one or more characters that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4075 are not word constituents. What is meant by `word constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4076 characters' brings us to the issue of syntax, which is worth a section |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4077 of its own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4078 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4079 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4080 File: eintr, Node: Syntax, Next: count-words-in-defun, Prev: Words and Symbols, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4081 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4082 14.2 What Constitutes a Word or Symbol? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4083 ======================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4084 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4085 Emacs treats different characters as belonging to different "syntax |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4086 categories". For example, the regular expression, `\\w+', is a pattern |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4087 specifying one or more _word constituent_ characters. Word constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4088 characters are members of one syntax category. Other syntax categories |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4089 include the class of punctuation characters, such as the period and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4090 comma, and the class of whitespace characters, such as the blank space |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4091 and the tab character. (For more information, see *Note Syntax: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4092 (emacs)Syntax, and *Note Syntax Tables: (elisp)Syntax Tables.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4093 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4094 Syntax tables specify which characters belong to which categories. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4095 Usually, a hyphen is not specified as a `word constituent character'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4096 Instead, it is specified as being in the `class of characters that are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4097 part of symbol names but not words.' This means that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4098 `count-words-region' function treats it in the same way it treats an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4099 interword white space, which is why `count-words-region' counts |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4100 `multiply-by-seven' as three words. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4101 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4102 There are two ways to cause Emacs to count `multiply-by-seven' as one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4103 symbol: modify the syntax table or modify the regular expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4104 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4105 We could redefine a hyphen as a word constituent character by modifying |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4106 the syntax table that Emacs keeps for each mode. This action would |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4107 serve our purpose, except that a hyphen is merely the most common |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4108 character within symbols that is not typically a word constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4109 character; there are others, too. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4110 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4111 Alternatively, we can redefine the regular expression used in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4112 `count-words' definition so as to include symbols. This procedure has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4113 the merit of clarity, but the task is a little tricky. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4114 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4115 The first part is simple enough: the pattern must match "at least one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4116 character that is a word or symbol constituent". Thus: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4117 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4118 "\\(\\w\\|\\s_\\)+" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4119 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4120 The `\\(' is the first part of the grouping construct that includes the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4121 `\\w' and the `\\s_' as alternatives, separated by the `\\|'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4122 `\\w' matches any word-constituent character and the `\\s_' matches any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4123 character that is part of a symbol name but not a word-constituent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4124 character. The `+' following the group indicates that the word or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4125 symbol constituent characters must be matched at least once. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4126 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4127 However, the second part of the regexp is more difficult to design. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4128 What we want is to follow the first part with "optionally one or more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4129 characters that are not constituents of a word or symbol". At first, I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4130 thought I could define this with the following: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4131 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4132 "\\(\\W\\|\\S_\\)*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4133 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4134 The upper case `W' and `S' match characters that are _not_ word or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4135 symbol constituents. Unfortunately, this expression matches any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4136 character that is either not a word constituent or not a symbol |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4137 constituent. This matches any character! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4138 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4139 I then noticed that every word or symbol in my test region was followed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4140 by white space (blank space, tab, or newline). So I tried placing a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4141 pattern to match one or more blank spaces after the pattern for one or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4142 more word or symbol constituents. This failed, too. Words and symbols |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4143 are often separated by whitespace, but in actual code parentheses may |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4144 follow symbols and punctuation may follow words. So finally, I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4145 designed a pattern in which the word or symbol constituents are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4146 followed optionally by characters that are not white space and then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4147 followed optionally by white space. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4148 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4149 Here is the full regular expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4151 "\\(\\w\\|\\s_\\)+[^ \t\n]*[ \t\n]*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4152 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4153 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4154 File: eintr, Node: count-words-in-defun, Next: Several defuns, Prev: Syntax, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4155 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4156 14.3 The `count-words-in-defun' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4157 ======================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4158 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4159 We have seen that there are several ways to write a `count-word-region' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4160 function. To write a `count-words-in-defun', we need merely adapt one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4161 of these versions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4162 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4163 The version that uses a `while' loop is easy to understand, so I am |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4164 going to adapt that. Because `count-words-in-defun' will be part of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4165 more complex program, it need not be interactive and it need not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4166 display a message but just return the count. These considerations |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4167 simplify the definition a little. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4168 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4169 On the other hand, `count-words-in-defun' will be used within a buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4170 that contains function definitions. Consequently, it is reasonable to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4171 ask that the function determine whether it is called when point is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4172 within a function definition, and if it is, to return the count for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4173 that definition. This adds complexity to the definition, but saves us |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4174 from needing to pass arguments to the function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4175 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4176 These considerations lead us to prepare the following template: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4177 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4178 (defun count-words-in-defun () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4179 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4180 (SET UP... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4181 (WHILE LOOP...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4182 RETURN COUNT) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4183 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4184 As usual, our job is to fill in the slots. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4185 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4186 First, the set up. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4187 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4188 We are presuming that this function will be called within a buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4189 containing function definitions. Point will either be within a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4190 function definition or not. For `count-words-in-defun' to work, point |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4191 must move to the beginning of the definition, a counter must start at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4192 zero, and the counting loop must stop when point reaches the end of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4193 definition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4194 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4195 The `beginning-of-defun' function searches backwards for an opening |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4196 delimiter such as a `(' at the beginning of a line, and moves point to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4197 that position, or else to the limit of the search. In practice, this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4198 means that `beginning-of-defun' moves point to the beginning of an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4199 enclosing or preceding function definition, or else to the beginning of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4200 the buffer. We can use `beginning-of-defun' to place point where we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4201 wish to start. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4202 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4203 The `while' loop requires a counter to keep track of the words or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4204 symbols being counted. A `let' expression can be used to create a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4205 local variable for this purpose, and bind it to an initial value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4206 zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4207 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4208 The `end-of-defun' function works like `beginning-of-defun' except that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4209 it moves point to the end of the definition. `end-of-defun' can be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4210 used as part of an expression that determines the position of the end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4211 of the definition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4212 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4213 The set up for `count-words-in-defun' takes shape rapidly: first we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4214 move point to the beginning of the definition, then we create a local |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4215 variable to hold the count, and finally, we record the position of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4216 end of the definition so the `while' loop will know when to stop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4217 looping. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4218 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4219 The code looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4220 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4221 (beginning-of-defun) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4222 (let ((count 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4223 (end (save-excursion (end-of-defun) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4224 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4225 The code is simple. The only slight complication is likely to concern |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4226 `end': it is bound to the position of the end of the definition by a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4227 `save-excursion' expression that returns the value of point after |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4228 `end-of-defun' temporarily moves it to the end of the definition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4229 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4230 The second part of the `count-words-in-defun', after the set up, is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4231 `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4232 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4233 The loop must contain an expression that jumps point forward word by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4234 word and symbol by symbol, and another expression that counts the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4235 jumps. The true-or-false-test for the `while' loop should test true so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4236 long as point should jump forward, and false when point is at the end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4237 of the definition. We have already redefined the regular expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4238 for this (*note Syntax::), so the loop is straightforward: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4239 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4240 (while (and (< (point) end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4241 (re-search-forward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4242 "\\(\\w\\|\\s_\\)+[^ \t\n]*[ \t\n]*" end t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4243 (setq count (1+ count))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4244 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4245 The third part of the function definition returns the count of words |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4246 and symbols. This part is the last expression within the body of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4247 `let' expression, and can be, very simply, the local variable `count', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4248 which when evaluated returns the count. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4249 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4250 Put together, the `count-words-in-defun' definition looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4251 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4252 (defun count-words-in-defun () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4253 "Return the number of words and symbols in a defun." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4254 (beginning-of-defun) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4255 (let ((count 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4256 (end (save-excursion (end-of-defun) (point)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4257 (while |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4258 (and (< (point) end) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4259 (re-search-forward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4260 "\\(\\w\\|\\s_\\)+[^ \t\n]*[ \t\n]*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4261 end t)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4262 (setq count (1+ count))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4263 count)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4264 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4265 How to test this? The function is not interactive, but it is easy to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4266 put a wrapper around the function to make it interactive; we can use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4267 almost the same code as for the recursive version of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4268 `count-words-region': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4269 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4270 ;;; Interactive version. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4271 (defun count-words-defun () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4272 "Number of words and symbols in a function definition." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4273 (interactive) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4274 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4275 "Counting words and symbols in function definition ... ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4276 (let ((count (count-words-in-defun))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4277 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4278 ((zerop count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4279 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4280 "The definition does NOT have any words or symbols.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4281 ((= 1 count) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4282 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4283 "The definition has 1 word or symbol.")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4284 (t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4285 (message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4286 "The definition has %d words or symbols." count))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4287 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4288 Let's re-use `C-c =' as a convenient keybinding: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4289 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4290 (global-set-key "\C-c=" 'count-words-defun) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4291 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4292 Now we can try out `count-words-defun': install both |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4293 `count-words-in-defun' and `count-words-defun', and set the keybinding, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4294 and then place the cursor within the following definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4295 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4296 (defun multiply-by-seven (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4297 "Multiply NUMBER by seven." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4298 (* 7 number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4299 => 10 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4300 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4301 Success! The definition has 10 words and symbols. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4302 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4303 The next problem is to count the numbers of words and symbols in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4304 several definitions within a single file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4305 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4306 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4307 File: eintr, Node: Several defuns, Next: Find a File, Prev: count-words-in-defun, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4308 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4309 14.4 Count Several `defuns' Within a File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4310 ========================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4311 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4312 A file such as `simple.el' may have a hundred or more function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4313 definitions within it. Our long term goal is to collect statistics on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4314 many files, but as a first step, our immediate goal is to collect |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4315 statistics on one file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4316 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4317 The information will be a series of numbers, each number being the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4318 length of a function definition. We can store the numbers in a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4319 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4320 We know that we will want to incorporate the information regarding one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4321 file with information about many other files; this means that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4322 function for counting definition lengths within one file need only |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4323 return the list of lengths. It need not and should not display any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4324 messages. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4325 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4326 The word count commands contain one expression to jump point forward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4327 word by word and another expression to count the jumps. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4328 to return the lengths of definitions can be designed to work the same |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4329 way, with one expression to jump point forward definition by definition |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4330 and another expression to construct the lengths' list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4331 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4332 This statement of the problem makes it elementary to write the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4333 definition. Clearly, we will start the count at the beginning of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4334 file, so the first command will be `(goto-char (point-min))'. Next, we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4335 start the `while' loop; and the true-or-false test of the loop can be a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4336 regular expression search for the next function definition--so long as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4337 the search succeeds, point is moved forward and then the body of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4338 loop is evaluated. The body needs an expression that constructs the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4339 lengths' list. `cons', the list construction command, can be used to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4340 create the list. That is almost all there is to it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4341 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4342 Here is what this fragment of code looks like: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4344 (goto-char (point-min)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4345 (while (re-search-forward "^(defun" nil t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4346 (setq lengths-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4347 (cons (count-words-in-defun) lengths-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4348 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4349 What we have left out is the mechanism for finding the file that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4350 contains the function definitions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4351 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4352 In previous examples, we either used this, the Info file, or we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4353 switched back and forth to some other buffer, such as the `*scratch*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4354 buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4355 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4356 Finding a file is a new process that we have not yet discussed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4357 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4358 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4359 File: eintr, Node: Find a File, Next: lengths-list-file, Prev: Several defuns, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4360 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4361 14.5 Find a File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4362 ================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4363 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4364 To find a file in Emacs, you use the `C-x C-f' (`find-file') command. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4365 This command is almost, but not quite right for the lengths problem. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4366 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4367 Let's look at the source for `find-file': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4368 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4369 (defun find-file (filename) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4370 "Edit file FILENAME. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4371 Switch to a buffer visiting file FILENAME, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4372 creating one if none already exists." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4373 (interactive "FFind file: ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4374 (switch-to-buffer (find-file-noselect filename))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4375 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4376 (The most recent version of the `find-file' function definition permits |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4377 you to specify optional wildcards visit multiple files; that makes the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4378 definition more complex and we will not discuss it here, since it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4379 not relevant. You can see its source using either `M-.' (`find-tag') |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4380 or `C-h f' (`describe-function').) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4381 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4382 The definition I am showing possesses short but complete documentation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4383 and an interactive specification that prompts you for a file name when |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4384 you use the command interactively. The body of the definition contains |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4385 two functions, `find-file-noselect' and `switch-to-buffer'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4386 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4387 According to its documentation as shown by `C-h f' (the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4388 `describe-function' command), the `find-file-noselect' function reads |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4389 the named file into a buffer and returns the buffer. (Its most recent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4390 version includes an optional wildcards argument, too, as well as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4391 another to read a file literally and an other you suppress warning |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4392 messages. These optional arguments are irrelevant.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4393 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4394 However, the `find-file-noselect' function does not select the buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4395 in which it puts the file. Emacs does not switch its attention (or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4396 yours if you are using `find-file-noselect') to the named buffer. That |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4397 is what `switch-to-buffer' does: it switches the buffer to which Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4398 attention is directed; and it switches the buffer displayed in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4399 window to the new buffer. We have discussed buffer switching |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4400 elsewhere. (*Note Switching Buffers::.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4401 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4402 In this histogram project, we do not need to display each file on the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4403 screen as the program determines the length of each definition within |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4404 it. Instead of employing `switch-to-buffer', we can work with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4405 `set-buffer', which redirects the attention of the computer program to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4406 a different buffer but does not redisplay it on the screen. So instead |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4407 of calling on `find-file' to do the job, we must write our own |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4408 expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4409 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4410 The task is easy: use `find-file-noselect' and `set-buffer'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4411 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4412 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4413 File: eintr, Node: lengths-list-file, Next: Several files, Prev: Find a File, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4414 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4415 14.6 `lengths-list-file' in Detail |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4416 ================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4417 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4418 The core of the `lengths-list-file' function is a `while' loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4419 containing a function to move point forward `defun by defun' and a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4420 function to count the number of words and symbols in each defun. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4421 core must be surrounded by functions that do various other tasks, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4422 including finding the file, and ensuring that point starts out at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4423 beginning of the file. The function definition looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4424 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4425 (defun lengths-list-file (filename) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4426 "Return list of definitions' lengths within FILE. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4427 The returned list is a list of numbers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4428 Each number is the number of words or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4429 symbols in one function definition." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4430 (message "Working on `%s' ... " filename) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4431 (save-excursion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4432 (let ((buffer (find-file-noselect filename)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4433 (lengths-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4434 (set-buffer buffer) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4435 (setq buffer-read-only t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4436 (widen) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4437 (goto-char (point-min)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4438 (while (re-search-forward "^(defun" nil t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4439 (setq lengths-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4440 (cons (count-words-in-defun) lengths-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4441 (kill-buffer buffer) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4442 lengths-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4443 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4444 The function is passed one argument, the name of the file on which it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4445 will work. It has four lines of documentation, but no interactive |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4446 specification. Since people worry that a computer is broken if they |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4447 don't see anything going on, the first line of the body is a message. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4448 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4449 The next line contains a `save-excursion' that returns Emacs' attention |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4450 to the current buffer when the function completes. This is useful in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4451 case you embed this function in another function that presumes point is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4452 restored to the original buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4453 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4454 In the varlist of the `let' expression, Emacs finds the file and binds |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4455 the local variable `buffer' to the buffer containing the file. At the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4456 same time, Emacs creates `lengths-list' as a local variable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4457 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4458 Next, Emacs switches its attention to the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4459 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4460 In the following line, Emacs makes the buffer read-only. Ideally, this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4461 line is not necessary. None of the functions for counting words and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4462 symbols in a function definition should change the buffer. Besides, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4463 the buffer is not going to be saved, even if it were changed. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4464 line is entirely the consequence of great, perhaps excessive, caution. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4465 The reason for the caution is that this function and those it calls |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4466 work on the sources for Emacs and it is very inconvenient if they are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4467 inadvertently modified. It goes without saying that I did not realize |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4468 a need for this line until an experiment went awry and started to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4469 modify my Emacs source files ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4470 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4471 Next comes a call to widen the buffer if it is narrowed. This function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4472 is usually not needed--Emacs creates a fresh buffer if none already |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4473 exists; but if a buffer visiting the file already exists Emacs returns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4474 that one. In this case, the buffer may be narrowed and must be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4475 widened. If we wanted to be fully `user-friendly', we would arrange to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4476 save the restriction and the location of point, but we won't. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4477 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4478 The `(goto-char (point-min))' expression moves point to the beginning |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4479 of the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4480 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4481 Then comes a `while' loop in which the `work' of the function is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4482 carried out. In the loop, Emacs determines the length of each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4483 definition and constructs a lengths' list containing the information. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4484 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4485 Emacs kills the buffer after working through it. This is to save space |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4486 inside of Emacs. My version of GNU Emacs 19 contained over 300 source |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4487 files of interest; GNU Emacs 22 contains over a thousand source files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4488 Another function will apply `lengths-list-file' to each of the files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4489 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4490 Finally, the last expression within the `let' expression is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4491 `lengths-list' variable; its value is returned as the value of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4492 whole function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4493 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4494 You can try this function by installing it in the usual fashion. Then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4495 place your cursor after the following expression and type `C-x C-e' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4496 (`eval-last-sexp'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4497 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4498 (lengths-list-file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4499 "/usr/local/share/emacs/22.0.100/lisp/emacs-lisp/debug.el") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4500 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4501 (You may need to change the pathname of the file; the one here is for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4502 GNU Emacs version 22.0.100. To change the expression, copy it to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4503 `*scratch*' buffer and edit it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4504 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4505 (Also, to see the full length of the list, rather than a truncated |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4506 version, you may have to evaluate the following: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4507 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4508 (custom-set-variables '(eval-expression-print-length nil)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4509 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4510 (*Note Specifying Variables using `defcustom': defcustom.) Then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4511 evaluate the `lengths-list-file' expression.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4512 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4513 The lengths' list for `debug.el' takes less than a second to produce |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4514 and looks like this in GNU Emacs 22: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4515 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4516 (83 113 105 144 289 22 30 97 48 89 25 52 52 88 28 29 77 49 43 290 232 587) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4517 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4518 (Using my old machine, the version 19 lengths' list for `debug.el' took |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4519 seven seconds to produce and looked like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4520 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4521 (75 41 80 62 20 45 44 68 45 12 34 235) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4522 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4523 (The newer version of `debug.el' contains more defuns than the earlier |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4524 one; and my new machine is much faster than the old one.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4525 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4526 Note that the length of the last definition in the file is first in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4527 list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4528 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4529 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4530 File: eintr, Node: Several files, Next: Several files recursively, Prev: lengths-list-file, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4531 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4532 14.7 Count Words in `defuns' in Different Files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4533 =============================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4534 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4535 In the previous section, we created a function that returns a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4536 the lengths of each definition in a file. Now, we want to define a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4537 function to return a master list of the lengths of the definitions in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4538 list of files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4539 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4540 Working on each of a list of files is a repetitious act, so we can use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4541 either a `while' loop or recursion. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4542 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4543 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4544 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4545 * lengths-list-many-files:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4546 * append:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4547 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4548 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4549 File: eintr, Node: lengths-list-many-files, Next: append, Prev: Several files, Up: Several files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4550 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4551 Determine the lengths of `defuns' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4552 --------------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4553 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4554 The design using a `while' loop is routine. The argument passed the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4555 function is a list of files. As we saw earlier (*note Loop Example::), |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4556 you can write a `while' loop so that the body of the loop is evaluated |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4557 if such a list contains elements, but to exit the loop if the list is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4558 empty. For this design to work, the body of the loop must contain an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4559 expression that shortens the list each time the body is evaluated, so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4560 that eventually the list is empty. The usual technique is to set the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4561 value of the list to the value of the CDR of the list each time the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4562 body is evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4563 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4564 The template looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4565 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4566 (while TEST-WHETHER-LIST-IS-EMPTY |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4567 BODY... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4568 SET-LIST-TO-CDR-OF-LIST) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4569 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4570 Also, we remember that a `while' loop returns `nil' (the result of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4571 evaluating the true-or-false-test), not the result of any evaluation |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4572 within its body. (The evaluations within the body of the loop are done |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4573 for their side effects.) However, the expression that sets the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4574 lengths' list is part of the body--and that is the value that we want |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4575 returned by the function as a whole. To do this, we enclose the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4576 `while' loop within a `let' expression, and arrange that the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4577 element of the `let' expression contains the value of the lengths' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4578 list. (*Note Loop Example with an Incrementing Counter: Incrementing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4579 Example.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4580 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4581 These considerations lead us directly to the function itself: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4582 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4583 ;;; Use `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4584 (defun lengths-list-many-files (list-of-files) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4585 "Return list of lengths of defuns in LIST-OF-FILES." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4586 (let (lengths-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4587 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4588 ;;; true-or-false-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4589 (while list-of-files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4590 (setq lengths-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4591 (append |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4592 lengths-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4593 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4594 ;;; Generate a lengths' list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4595 (lengths-list-file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4596 (expand-file-name (car list-of-files))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4597 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4598 ;;; Make files' list shorter. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4599 (setq list-of-files (cdr list-of-files))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4600 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4601 ;;; Return final value of lengths' list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4602 lengths-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4603 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4604 `expand-file-name' is a built-in function that converts a file name to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4605 the absolute, long, path name form of the directory in which the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4606 function is called. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4607 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4608 Thus, if `expand-file-name' is called on `debug.el' when Emacs is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4609 visiting the `/usr/local/share/emacs/22.0.100/lisp/emacs-lisp/' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4610 directory, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4611 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4612 debug.el |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4613 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4614 becomes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4615 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4616 /usr/local/share/emacs/22.0.100/lisp/emacs-lisp/debug.el |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4617 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4618 The only other new element of this function definition is the as yet |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4619 unstudied function `append', which merits a short section for itself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4620 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4621 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4622 File: eintr, Node: append, Prev: lengths-list-many-files, Up: Several files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4623 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4624 14.7.1 The `append' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4625 ---------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4626 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4627 The `append' function attaches one list to another. Thus, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4628 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4629 (append '(1 2 3 4) '(5 6 7 8)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4630 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4631 produces the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4632 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4633 (1 2 3 4 5 6 7 8) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4634 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4635 This is exactly how we want to attach two lengths' lists produced by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4636 `lengths-list-file' to each other. The results contrast with `cons', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4637 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4638 (cons '(1 2 3 4) '(5 6 7 8)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4639 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4640 which constructs a new list in which the first argument to `cons' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4641 becomes the first element of the new list: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4642 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4643 ((1 2 3 4) 5 6 7 8) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4644 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4645 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4646 File: eintr, Node: Several files recursively, Next: Prepare the data, Prev: Several files, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4647 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4648 14.8 Recursively Count Words in Different Files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4649 =============================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4650 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4651 Besides a `while' loop, you can work on each of a list of files with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4652 recursion. A recursive version of `lengths-list-many-files' is short |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4653 and simple. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4654 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4655 The recursive function has the usual parts: the `do-again-test', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4656 `next-step-expression', and the recursive call. The `do-again-test' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4657 determines whether the function should call itself again, which it will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4658 do if the `list-of-files' contains any remaining elements; the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4659 `next-step-expression' resets the `list-of-files' to the CDR of itself, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4660 so eventually the list will be empty; and the recursive call calls |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4661 itself on the shorter list. The complete function is shorter than this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4662 description! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4663 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4664 (defun recursive-lengths-list-many-files (list-of-files) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4665 "Return list of lengths of each defun in LIST-OF-FILES." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4666 (if list-of-files ; do-again-test |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4667 (append |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4668 (lengths-list-file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4669 (expand-file-name (car list-of-files))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4670 (recursive-lengths-list-many-files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4671 (cdr list-of-files))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4672 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4673 In a sentence, the function returns the lengths' list for the first of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4674 the `list-of-files' appended to the result of calling itself on the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4675 rest of the `list-of-files'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4676 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4677 Here is a test of `recursive-lengths-list-many-files', along with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4678 results of running `lengths-list-file' on each of the files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4679 individually. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4680 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4681 Install `recursive-lengths-list-many-files' and `lengths-list-file', if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4682 necessary, and then evaluate the following expressions. You may need |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4683 to change the files' pathnames; those here work when this Info file and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4684 the Emacs sources are located in their customary places. To change the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4685 expressions, copy them to the `*scratch*' buffer, edit them, and then |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4686 evaluate them. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4687 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4688 The results are shown after the `=>'. (These results are for files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4689 from Emacs Version 22.0.100; files from other versions of Emacs may |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4690 produce different results.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4691 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4692 (cd "/usr/local/share/emacs/22.0.100/") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4693 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4694 (lengths-list-file "./lisp/macros.el") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4695 => (283 263 480 90) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4696 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4697 (lengths-list-file "./lisp/mail/mailalias.el") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4698 => (38 32 29 95 178 180 321 218 324) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4699 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4700 (lengths-list-file "./lisp/makesum.el") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4701 => (85 181) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4702 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4703 (recursive-lengths-list-many-files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4704 '("./lisp/macros.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4705 "./lisp/mail/mailalias.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4706 "./lisp/makesum.el")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4707 => (283 263 480 90 38 32 29 95 178 180 321 218 324 85 181) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4709 The `recursive-lengths-list-many-files' function produces the output we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4710 want. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4711 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4712 The next step is to prepare the data in the list for display in a graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4713 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4714 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4715 File: eintr, Node: Prepare the data, Prev: Several files recursively, Up: Words in a defun |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4716 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4717 14.9 Prepare the Data for Display in a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4718 ============================================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4719 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4720 The `recursive-lengths-list-many-files' function returns a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4721 numbers. Each number records the length of a function definition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4722 What we need to do now is transform this data into a list of numbers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4723 suitable for generating a graph. The new list will tell how many |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4724 functions definitions contain less than 10 words and symbols, how many |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4725 contain between 10 and 19 words and symbols, how many contain between |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4726 20 and 29 words and symbols, and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4727 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4728 In brief, we need to go through the lengths' list produced by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4729 `recursive-lengths-list-many-files' function and count the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4730 defuns within each range of lengths, and produce a list of those |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4731 numbers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4732 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4733 Based on what we have done before, we can readily foresee that it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4734 should not be too hard to write a function that `CDRs' down the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4735 lengths' list, looks at each element, determines which length range it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4736 is in, and increments a counter for that range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4737 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4738 However, before beginning to write such a function, we should consider |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4739 the advantages of sorting the lengths' list first, so the numbers are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4740 ordered from smallest to largest. First, sorting will make it easier |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4741 to count the numbers in each range, since two adjacent numbers will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4742 either be in the same length range or in adjacent ranges. Second, by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4743 inspecting a sorted list, we can discover the highest and lowest |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4744 number, and thereby determine the largest and smallest length range |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4745 that we will need. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4746 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4747 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4748 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4749 * Sorting:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4750 * Files List:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4751 * Counting function definitions:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4752 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4753 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4754 File: eintr, Node: Sorting, Next: Files List, Prev: Prepare the data, Up: Prepare the data |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4755 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4756 14.9.1 Sorting Lists |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4757 -------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4758 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4759 Emacs contains a function to sort lists, called (as you might guess) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4760 `sort'. The `sort' function takes two arguments, the list to be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4761 sorted, and a predicate that determines whether the first of two list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4762 elements is "less" than the second. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4763 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4764 As we saw earlier (*note Using the Wrong Type Object as an Argument: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4765 Wrong Type of Argument.), a predicate is a function that determines |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4766 whether some property is true or false. The `sort' function will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4767 reorder a list according to whatever property the predicate uses; this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4768 means that `sort' can be used to sort non-numeric lists by non-numeric |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4769 criteria--it can, for example, alphabetize a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4770 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4771 The `<' function is used when sorting a numeric list. For example, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4772 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4773 (sort '(4 8 21 17 33 7 21 7) '<) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4774 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4775 produces this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4776 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4777 (4 7 7 8 17 21 21 33) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4778 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4779 (Note that in this example, both the arguments are quoted so that the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4780 symbols are not evaluated before being passed to `sort' as arguments.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4781 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4782 Sorting the list returned by the `recursive-lengths-list-many-files' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4783 function is straightforward; it uses the `<' function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4784 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4785 (sort |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4786 (recursive-lengths-list-many-files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4787 '("./lisp/macros.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4788 "./lisp/mailalias.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4789 "./lisp/makesum.el")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4790 '<) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4791 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4792 which produces: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4793 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4794 (29 32 38 85 90 95 178 180 181 218 263 283 321 324 480) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4795 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4796 (Note that in this example, the first argument to `sort' is not quoted, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4797 since the expression must be evaluated so as to produce the list that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4798 is passed to `sort'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4799 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4800 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4801 File: eintr, Node: Files List, Next: Counting function definitions, Prev: Sorting, Up: Prepare the data |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4802 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4803 14.9.2 Making a List of Files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4804 ----------------------------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4805 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4806 The `recursive-lengths-list-many-files' function requires a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4807 files as its argument. For our test examples, we constructed such a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4808 list by hand; but the Emacs Lisp source directory is too large for us |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4809 to do for that. Instead, we will write a function to do the job for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4810 us. In this function, we will use both a `while' loop and a recursive |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4811 call. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4812 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4813 We did not have to write a function like this for older versions of GNU |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4814 Emacs, since they placed all the `.el' files in one directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4815 Instead, we were able to use the `directory-files' function, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4816 lists the names of files that match a specified pattern within a single |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4817 directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4818 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4819 However, recent versions of Emacs place Emacs Lisp files in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4820 sub-directories of the top level `lisp' directory. This re-arrangement |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4821 eases navigation. For example, all the mail related files are in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4822 `lisp' sub-directory called `mail'. But at the same time, this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4823 arrangement forces us to create a file listing function that descends |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4824 into the sub-directories. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4825 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4826 We can create this function, called `files-in-below-directory', using |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4827 familiar functions such as `car', `nthcdr', and `substring' in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4828 conjunction with an existing function called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4829 `directory-files-and-attributes'. This latter function not only lists |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4830 all the filenames in a directory, including the names of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4831 sub-directories, but also their attributes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4832 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4833 To restate our goal: to create a function that will enable us to feed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4834 filenames to `recursive-lengths-list-many-files' as a list that looks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4835 like this (but with more elements): |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4836 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4837 ("./lisp/macros.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4838 "./lisp/mail/rmail.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4839 "./lisp/makesum.el") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4840 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4841 The `directory-files-and-attributes' function returns a list of lists. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4842 Each of the lists within the main list consists of 13 elements. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4843 first element is a string that contains the name of the file - which, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4844 in GNU/Linux, may be a `directory file', that is to say, a file with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4845 the special attributes of a directory. The second element of the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4846 is `t' for a directory, a string for symbolic link (the string is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4847 name linked to), or `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4848 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4849 For example, the first `.el' file in the `lisp/' directory is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4850 `abbrev.el'. Its name is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4851 `/usr/local/share/emacs/22.0.100/lisp/abbrev.el' and it is not a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4852 directory or a symbolic link. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4853 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4854 This is how `directory-files-and-attributes' lists that file and its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4855 attributes: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4856 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4857 ("abbrev.el" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4858 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4859 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4860 1000 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4861 100 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4862 (17733 259) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4863 (17491 28834) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4864 (17596 62124) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4865 13157 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4866 "-rw-rw-r--" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4867 nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4868 2971624 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4869 773) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4870 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4871 On the other hand, `mail/' is a directory within the `lisp/' directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4872 The beginning of its listing looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4873 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4874 ("mail" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4875 t |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4876 ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4877 ) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4878 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4879 (To learn about the different attributes, look at the documentation of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4880 `file-attributes'. Bear in mind that the `file-attributes' function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4881 does not list the filename, so its first element is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4882 `directory-files-and-attributes''s second element.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4883 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4884 We will want our new function, `files-in-below-directory', to list the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4885 `.el' files in the directory it is told to check, and in any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4886 directories below that directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4887 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4888 This gives us a hint on how to construct `files-in-below-directory': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4889 within a directory, the function should add `.el' filenames to a list; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4890 and if, within a directory, the function comes upon a sub-directory, it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4891 should go into that sub-directory and repeat its actions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4892 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4893 However, we should note that every directory contains a name that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4894 refers to itself, called `.', ("dot") and a name that refers to its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4895 parent directory, called `..' ("double dot"). (In `/', the root |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4896 directory, `..' refers to itself, since `/' has no parent.) Clearly, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4897 we do not want our `files-in-below-directory' function to enter those |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4898 directories, since they always lead us, directly or indirectly, to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4899 current directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4900 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4901 Consequently, our `files-in-below-directory' function must do several |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4902 tasks: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4903 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4904 * Check to see whether it is looking at a filename that ends in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4905 `.el'; and if so, add its name to a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4906 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4907 * Check to see whether it is looking at a filename that is the name |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4908 of a directory; and if so, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4909 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4910 - Check to see whether it is looking at `.' or `..'; and if so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4911 skip it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4912 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4913 - Or else, go into that directory and repeat the process. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4914 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4915 Let's write a function definition to do these tasks. We will use a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4916 `while' loop to move from one filename to another within a directory, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4917 checking what needs to be done; and we will use a recursive call to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4918 repeat the actions on each sub-directory. The recursive pattern is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4919 `accumulate' (*note Recursive Pattern: _accumulate_: Accumulate.), |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4920 using `append' as the combiner. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4921 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4922 Here is the function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4923 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4924 (defun files-in-below-directory (directory) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4925 "List the .el files in DIRECTORY and in its sub-directories." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4926 ;; Although the function will be used non-interactively, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4927 ;; it will be easier to test if we make it interactive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4928 ;; The directory will have a name such as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4929 ;; "/usr/local/share/emacs/22.0.100/lisp/" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4930 (interactive "DDirectory name: ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4931 (let (el-files-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4932 (current-directory-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4933 (directory-files-and-attributes directory t))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4934 ;; while we are in the current directory |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4935 (while current-directory-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4936 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4937 ;; check to see whether filename ends in `.el' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4938 ;; and if so, append its name to a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4939 ((equal ".el" (substring (car (car current-directory-list)) -3)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4940 (setq el-files-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4941 (cons (car (car current-directory-list)) el-files-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4942 ;; check whether filename is that of a directory |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4943 ((eq t (car (cdr (car current-directory-list)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4944 ;; decide whether to skip or recurse |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4945 (if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4946 (equal "." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4947 (substring (car (car current-directory-list)) -1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4948 ;; then do nothing since filename is that of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4949 ;; current directory or parent, "." or ".." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4950 () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4951 ;; else descend into the directory and repeat the process |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4952 (setq el-files-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4953 (append |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4954 (files-in-below-directory |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4955 (car (car current-directory-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4956 el-files-list))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4957 ;; move to the next filename in the list; this also |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4958 ;; shortens the list so the while loop eventually comes to an end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4959 (setq current-directory-list (cdr current-directory-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4960 ;; return the filenames |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4961 el-files-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4962 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4963 The `files-in-below-directory' `directory-files' function takes one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4964 argument, the name of a directory. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4965 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4966 Thus, on my system, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4968 (length |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4969 (files-in-below-directory "/usr/local/share/emacs/22.0.100/lisp/")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4970 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4971 tells me that my Lisp sources directory contains 1031 `.el' files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4972 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4973 `files-in-below-directory' returns a list in reverse alphabetical |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4974 order. An expression to sort the list in alphabetical order looks like |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4975 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4976 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4977 (sort |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4978 (files-in-below-directory "/usr/local/share/emacs/22.0.100/lisp/") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4979 'string-lessp) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4980 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4981 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4982 File: eintr, Node: Counting function definitions, Prev: Files List, Up: Prepare the data |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4983 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4984 14.9.3 Counting function definitions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4985 ------------------------------------ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4986 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4987 Our immediate goal is to generate a list that tells us how many |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4988 function definitions contain fewer than 10 words and symbols, how many |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4989 contain between 10 and 19 words and symbols, how many contain between |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4990 20 and 29 words and symbols, and so on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4991 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4992 With a sorted list of numbers, this is easy: count how many elements of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4993 the list are smaller than 10, then, after moving past the numbers just |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4994 counted, count how many are smaller than 20, then, after moving past |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4995 the numbers just counted, count how many are smaller than 30, and so |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4996 on. Each of the numbers, 10, 20, 30, 40, and the like, is one larger |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4997 than the top of that range. We can call the list of such numbers the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4998 `top-of-ranges' list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
4999 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5000 If we wished, we could generate this list automatically, but it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5001 simpler to write a list manually. Here it is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5002 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5003 (defvar top-of-ranges |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5004 '(10 20 30 40 50 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5005 60 70 80 90 100 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5006 110 120 130 140 150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5007 160 170 180 190 200 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5008 210 220 230 240 250 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5009 260 270 280 290 300) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5010 "List specifying ranges for `defuns-per-range'.") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5011 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5012 To change the ranges, we edit this list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5013 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5014 Next, we need to write the function that creates the list of the number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5015 of definitions within each range. Clearly, this function must take the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5016 `sorted-lengths' and the `top-of-ranges' lists as arguments. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5017 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5018 The `defuns-per-range' function must do two things again and again: it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5019 must count the number of definitions within a range specified by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5020 current top-of-range value; and it must shift to the next higher value |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5021 in the `top-of-ranges' list after counting the number of definitions in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5022 the current range. Since each of these actions is repetitive, we can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5023 use `while' loops for the job. One loop counts the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5024 definitions in the range defined by the current top-of-range value, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5025 the other loop selects each of the top-of-range values in turn. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5026 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5027 Several entries of the `sorted-lengths' list are counted for each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5028 range; this means that the loop for the `sorted-lengths' list will be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5029 inside the loop for the `top-of-ranges' list, like a small gear inside |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5030 a big gear. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5031 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5032 The inner loop counts the number of definitions within the range. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5033 is a simple counting loop of the type we have seen before. (*Note A |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5034 loop with an incrementing counter: Incrementing Loop.) The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5035 true-or-false test of the loop tests whether the value from the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5036 `sorted-lengths' list is smaller than the current value of the top of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5037 the range. If it is, the function increments the counter and tests the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5038 next value from the `sorted-lengths' list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5039 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5040 The inner loop looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5041 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5042 (while LENGTH-ELEMENT-SMALLER-THAN-TOP-OF-RANGE |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5043 (setq number-within-range (1+ number-within-range)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5044 (setq sorted-lengths (cdr sorted-lengths))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5045 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5046 The outer loop must start with the lowest value of the `top-of-ranges' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5047 list, and then be set to each of the succeeding higher values in turn. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5048 This can be done with a loop like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5049 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5050 (while top-of-ranges |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5051 BODY-OF-LOOP... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5052 (setq top-of-ranges (cdr top-of-ranges))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5053 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5054 Put together, the two loops look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5055 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5056 (while top-of-ranges |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5057 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5058 ;; Count the number of elements within the current range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5059 (while LENGTH-ELEMENT-SMALLER-THAN-TOP-OF-RANGE |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5060 (setq number-within-range (1+ number-within-range)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5061 (setq sorted-lengths (cdr sorted-lengths))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5062 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5063 ;; Move to next range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5064 (setq top-of-ranges (cdr top-of-ranges))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5065 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5066 In addition, in each circuit of the outer loop, Emacs should record the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5067 number of definitions within that range (the value of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5068 `number-within-range') in a list. We can use `cons' for this purpose. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5069 (*Note `cons': cons.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5070 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5071 The `cons' function works fine, except that the list it constructs will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5072 contain the number of definitions for the highest range at its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5073 beginning and the number of definitions for the lowest range at its |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5074 end. This is because `cons' attaches new elements of the list to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5075 beginning of the list, and since the two loops are working their way |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5076 through the lengths' list from the lower end first, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5077 `defuns-per-range-list' will end up largest number first. But we will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5078 want to print our graph with smallest values first and the larger |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5079 later. The solution is to reverse the order of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5080 `defuns-per-range-list'. We can do this using the `nreverse' function, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5081 which reverses the order of a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5082 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5083 For example, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5084 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5085 (nreverse '(1 2 3 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5086 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5087 produces: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5088 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5089 (4 3 2 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5090 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5091 Note that the `nreverse' function is "destructive"--that is, it changes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5092 the list to which it is applied; this contrasts with the `car' and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5093 `cdr' functions, which are non-destructive. In this case, we do not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5094 want the original `defuns-per-range-list', so it does not matter that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5095 it is destroyed. (The `reverse' function provides a reversed copy of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5096 list, leaving the original list as is.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5097 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5098 Put all together, the `defuns-per-range' looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5099 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5100 (defun defuns-per-range (sorted-lengths top-of-ranges) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5101 "SORTED-LENGTHS defuns in each TOP-OF-RANGES range." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5102 (let ((top-of-range (car top-of-ranges)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5103 (number-within-range 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5104 defuns-per-range-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5105 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5106 ;; Outer loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5107 (while top-of-ranges |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5108 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5109 ;; Inner loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5110 (while (and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5111 ;; Need number for numeric test. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5112 (car sorted-lengths) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5113 (< (car sorted-lengths) top-of-range)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5114 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5115 ;; Count number of definitions within current range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5116 (setq number-within-range (1+ number-within-range)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5117 (setq sorted-lengths (cdr sorted-lengths))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5118 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5119 ;; Exit inner loop but remain within outer loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5120 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5121 (setq defuns-per-range-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5122 (cons number-within-range defuns-per-range-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5123 (setq number-within-range 0) ; Reset count to zero. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5124 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5125 ;; Move to next range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5126 (setq top-of-ranges (cdr top-of-ranges)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5127 ;; Specify next top of range value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5128 (setq top-of-range (car top-of-ranges))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5129 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5130 ;; Exit outer loop and count the number of defuns larger than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5131 ;; the largest top-of-range value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5132 (setq defuns-per-range-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5133 (cons |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5134 (length sorted-lengths) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5135 defuns-per-range-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5136 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5137 ;; Return a list of the number of definitions within each range, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5138 ;; smallest to largest. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5139 (nreverse defuns-per-range-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5140 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5141 The function is straightforward except for one subtle feature. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5142 true-or-false test of the inner loop looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5143 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5144 (and (car sorted-lengths) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5145 (< (car sorted-lengths) top-of-range)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5146 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5147 instead of like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5148 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5149 (< (car sorted-lengths) top-of-range) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5151 The purpose of the test is to determine whether the first item in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5152 `sorted-lengths' list is less than the value of the top of the range. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5153 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5154 The simple version of the test works fine unless the `sorted-lengths' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5155 list has a `nil' value. In that case, the `(car sorted-lengths)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5156 expression function returns `nil'. The `<' function cannot compare a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5157 number to `nil', which is an empty list, so Emacs signals an error and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5158 stops the function from attempting to continue to execute. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5159 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5160 The `sorted-lengths' list always becomes `nil' when the counter reaches |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5161 the end of the list. This means that any attempt to use the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5162 `defuns-per-range' function with the simple version of the test will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5163 fail. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5164 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5165 We solve the problem by using the `(car sorted-lengths)' expression in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5166 conjunction with the `and' expression. The `(car sorted-lengths)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5167 expression returns a non-`nil' value so long as the list has at least |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5168 one number within it, but returns `nil' if the list is empty. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5169 `and' expression first evaluates the `(car sorted-lengths)' expression, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5170 and if it is `nil', returns false _without_ evaluating the `<' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5171 expression. But if the `(car sorted-lengths)' expression returns a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5172 non-`nil' value, the `and' expression evaluates the `<' expression, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5173 returns that value as the value of the `and' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5174 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5175 This way, we avoid an error. (*Note The `kill-new' function: kill-new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5176 function, for information about `and'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5177 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5178 Here is a short test of the `defuns-per-range' function. First, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5179 evaluate the expression that binds (a shortened) `top-of-ranges' list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5180 to the list of values, then evaluate the expression for binding the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5181 `sorted-lengths' list, and then evaluate the `defuns-per-range' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5182 function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5183 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5184 ;; (Shorter list than we will use later.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5185 (setq top-of-ranges |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5186 '(110 120 130 140 150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5187 160 170 180 190 200)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5188 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5189 (setq sorted-lengths |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5190 '(85 86 110 116 122 129 154 176 179 200 265 300 300)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5191 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5192 (defuns-per-range sorted-lengths top-of-ranges) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5193 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5194 The list returned looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5195 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5196 (2 2 2 0 0 1 0 2 0 0 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5197 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5198 Indeed, there are two elements of the `sorted-lengths' list smaller |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5199 than 110, two elements between 110 and 119, two elements between 120 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5200 and 129, and so on. There are four elements with a value of 200 or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5201 larger. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5202 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5203 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5204 File: eintr, Node: Readying a Graph, Next: Emacs Initialization, Prev: Words in a defun, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5205 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5206 15 Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5207 ******************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5208 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5209 Our goal is to construct a graph showing the numbers of function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5210 definitions of various lengths in the Emacs lisp sources. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5211 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5212 As a practical matter, if you were creating a graph, you would probably |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5213 use a program such as `gnuplot' to do the job. (`gnuplot' is nicely |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5214 integrated into GNU Emacs.) In this case, however, we create one from |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5215 scratch, and in the process we will re-acquaint ourselves with some of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5216 what we learned before and learn more. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5217 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5218 In this chapter, we will first write a simple graph printing function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5219 This first definition will be a "prototype", a rapidly written function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5220 that enables us to reconnoiter this unknown graph-making territory. We |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5221 will discover dragons, or find that they are myth. After scouting the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5222 terrain, we will feel more confident and enhance the function to label |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5223 the axes automatically. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5224 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5225 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5226 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5227 * Columns of a graph:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5228 * graph-body-print:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5229 * recursive-graph-body-print:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5230 * Printed Axes:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5231 * Line Graph Exercise:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5232 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5233 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5234 File: eintr, Node: Columns of a graph, Next: graph-body-print, Prev: Readying a Graph, Up: Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5235 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5236 Printing the Columns of a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5237 =============================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5238 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5239 Since Emacs is designed to be flexible and work with all kinds of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5240 terminals, including character-only terminals, the graph will need to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5241 be made from one of the `typewriter' symbols. An asterisk will do; as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5242 we enhance the graph-printing function, we can make the choice of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5243 symbol a user option. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5244 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5245 We can call this function `graph-body-print'; it will take a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5246 `numbers-list' as its only argument. At this stage, we will not label |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5247 the graph, but only print its body. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5248 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5249 The `graph-body-print' function inserts a vertical column of asterisks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5250 for each element in the `numbers-list'. The height of each line is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5251 determined by the value of that element of the `numbers-list'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5252 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5253 Inserting columns is a repetitive act; that means that this function can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5254 be written either with a `while' loop or recursively. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5255 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5256 Our first challenge is to discover how to print a column of asterisks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5257 Usually, in Emacs, we print characters onto a screen horizontally, line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5258 by line, by typing. We have two routes we can follow: write our own |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5259 column-insertion function or discover whether one exists in Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5260 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5261 To see whether there is one in Emacs, we can use the `M-x apropos' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5262 command. This command is like the `C-h a' (`command-apropos') command, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5263 except that the latter finds only those functions that are commands. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5264 The `M-x apropos' command lists all symbols that match a regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5265 expression, including functions that are not interactive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5266 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5267 What we want to look for is some command that prints or inserts |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5268 columns. Very likely, the name of the function will contain either the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5269 word `print' or the word `insert' or the word `column'. Therefore, we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5270 can simply type `M-x apropos RET print\|insert\|column RET' and look at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5271 the result. On my system, this command once too takes quite some time, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5272 and then produced a list of 79 functions and variables. Now it does |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5273 not take much time at all and produces a list of 211 functions and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5274 variables. Scanning down the list, the only function that looks as if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5275 it might do the job is `insert-rectangle'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5276 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5277 Indeed, this is the function we want; its documentation says: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5278 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5279 insert-rectangle: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5280 Insert text of RECTANGLE with upper left corner at point. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5281 RECTANGLE's first line is inserted at point, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5282 its second line is inserted at a point vertically under point, etc. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5283 RECTANGLE should be a list of strings. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5284 After this command, the mark is at the upper left corner |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5285 and point is at the lower right corner. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5286 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5287 We can run a quick test, to make sure it does what we expect of it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5288 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5289 Here is the result of placing the cursor after the `insert-rectangle' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5290 expression and typing `C-u C-x C-e' (`eval-last-sexp'). The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5291 inserts the strings `"first"', `"second"', and `"third"' at and below |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5292 point. Also the function returns `nil'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5293 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5294 (insert-rectangle '("first" "second" "third"))first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5295 second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5296 thirdnil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5297 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5298 Of course, we won't be inserting the text of the `insert-rectangle' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5299 expression itself into the buffer in which we are making the graph, but |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5300 will call the function from our program. We shall, however, have to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5301 make sure that point is in the buffer at the place where the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5302 `insert-rectangle' function will insert its column of strings. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5303 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5304 If you are reading this in Info, you can see how this works by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5305 switching to another buffer, such as the `*scratch*' buffer, placing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5306 point somewhere in the buffer, typing `M-:', typing the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5307 `insert-rectangle' expression into the minibuffer at the prompt, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5308 then typing <RET>. This causes Emacs to evaluate the expression in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5309 minibuffer, but to use as the value of point the position of point in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5310 the `*scratch*' buffer. (`M-:' is the keybinding for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5311 `eval-expression'. Also, `nil' does not appear in the `*scratch*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5312 buffer since the expression is evaluated in the minibuffer.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5313 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5314 We find when we do this that point ends up at the end of the last |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5315 inserted line--that is to say, this function moves point as a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5316 side-effect. If we were to repeat the command, with point at this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5317 position, the next insertion would be below and to the right of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5318 previous insertion. We don't want this! If we are going to make a bar |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5319 graph, the columns need to be beside each other. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5320 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5321 So we discover that each cycle of the column-inserting `while' loop |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5322 must reposition point to the place we want it, and that place will be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5323 at the top, not the bottom, of the column. Moreover, we remember that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5324 when we print a graph, we do not expect all the columns to be the same |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5325 height. This means that the top of each column may be at a different |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5326 height from the previous one. We cannot simply reposition point to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5327 same line each time, but moved over to the right--or perhaps we can... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5328 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5329 We are planning to make the columns of the bar graph out of asterisks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5330 The number of asterisks in the column is the number specified by the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5331 current element of the `numbers-list'. We need to construct a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5332 asterisks of the right length for each call to `insert-rectangle'. If |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5333 this list consists solely of the requisite number of asterisks, then we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5334 will have position point the right number of lines above the base for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5335 the graph to print correctly. This could be difficult. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5336 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5337 Alternatively, if we can figure out some way to pass `insert-rectangle' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5338 a list of the same length each time, then we can place point on the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5339 same line each time, but move it over one column to the right for each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5340 new column. If we do this, however, some of the entries in the list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5341 passed to `insert-rectangle' must be blanks rather than asterisks. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5342 example, if the maximum height of the graph is 5, but the height of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5343 column is 3, then `insert-rectangle' requires an argument that looks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5344 like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5345 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5346 (" " " " "*" "*" "*") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5348 This last proposal is not so difficult, so long as we can determine the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5349 column height. There are two ways for us to specify the column height: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5350 we can arbitrarily state what it will be, which would work fine for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5351 graphs of that height; or we can search through the list of numbers and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5352 use the maximum height of the list as the maximum height of the graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5353 If the latter operation were difficult, then the former procedure would |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5354 be easiest, but there is a function built into Emacs that determines |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5355 the maximum of its arguments. We can use that function. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5356 is called `max' and it returns the largest of all its arguments, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5357 must be numbers. Thus, for example, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5358 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5359 (max 3 4 6 5 7 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5360 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5361 returns 7. (A corresponding function called `min' returns the smallest |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5362 of all its arguments.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5363 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5364 However, we cannot simply call `max' on the `numbers-list'; the `max' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5365 function expects numbers as its argument, not a list of numbers. Thus, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5366 the following expression, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5367 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5368 (max '(3 4 6 5 7 3)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5369 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5370 produces the following error message; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5371 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5372 Wrong type of argument: number-or-marker-p, (3 4 6 5 7 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5373 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5374 We need a function that passes a list of arguments to a function. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5375 function is `apply'. This function `applies' its first argument (a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5376 function) to its remaining arguments, the last of which may be a list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5377 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5378 For example, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5379 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5380 (apply 'max 3 4 7 3 '(4 8 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5381 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5382 returns 8. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5383 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5384 (Incidentally, I don't know how you would learn of this function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5385 without a book such as this. It is possible to discover other |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5386 functions, like `search-forward' or `insert-rectangle', by guessing at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5387 a part of their names and then using `apropos'. Even though its base |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5388 in metaphor is clear--`apply' its first argument to the rest--I doubt a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5389 novice would come up with that particular word when using `apropos' or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5390 other aid. Of course, I could be wrong; after all, the function was |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5391 first named by someone who had to invent it.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5392 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5393 The second and subsequent arguments to `apply' are optional, so we can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5394 use `apply' to call a function and pass the elements of a list to it, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5395 like this, which also returns 8: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5396 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5397 (apply 'max '(4 8 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5398 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5399 This latter way is how we will use `apply'. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5400 `recursive-lengths-list-many-files' function returns a numbers' list to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5401 which we can apply `max' (we could also apply `max' to the sorted |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5402 numbers' list; it does not matter whether the list is sorted or not.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5403 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5404 Hence, the operation for finding the maximum height of the graph is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5405 this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5406 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5407 (setq max-graph-height (apply 'max numbers-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5408 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5409 Now we can return to the question of how to create a list of strings |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5410 for a column of the graph. Told the maximum height of the graph and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5411 the number of asterisks that should appear in the column, the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5412 should return a list of strings for the `insert-rectangle' command to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5413 insert. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5414 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5415 Each column is made up of asterisks or blanks. Since the function is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5416 passed the value of the height of the column and the number of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5417 asterisks in the column, the number of blanks can be found by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5418 subtracting the number of asterisks from the height of the column. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5419 Given the number of blanks and the number of asterisks, two `while' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5420 loops can be used to construct the list: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5421 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5422 ;;; First version. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5423 (defun column-of-graph (max-graph-height actual-height) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5424 "Return list of strings that is one column of a graph." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5425 (let ((insert-list nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5426 (number-of-top-blanks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5427 (- max-graph-height actual-height))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5428 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5429 ;; Fill in asterisks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5430 (while (> actual-height 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5431 (setq insert-list (cons "*" insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5432 (setq actual-height (1- actual-height))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5433 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5434 ;; Fill in blanks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5435 (while (> number-of-top-blanks 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5436 (setq insert-list (cons " " insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5437 (setq number-of-top-blanks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5438 (1- number-of-top-blanks))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5439 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5440 ;; Return whole list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5441 insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5442 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5443 If you install this function and then evaluate the following expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5444 you will see that it returns the list as desired: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5445 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5446 (column-of-graph 5 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5447 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5448 returns |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5449 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5450 (" " " " "*" "*" "*") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5451 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5452 As written, `column-of-graph' contains a major flaw: the symbols used |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5453 for the blank and for the marked entries in the column are `hard-coded' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5454 as a space and asterisk. This is fine for a prototype, but you, or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5455 another user, may wish to use other symbols. For example, in testing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5456 the graph function, you many want to use a period in place of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5457 space, to make sure the point is being repositioned properly each time |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5458 the `insert-rectangle' function is called; or you might want to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5459 substitute a `+' sign or other symbol for the asterisk. You might even |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5460 want to make a graph-column that is more than one display column wide. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5461 The program should be more flexible. The way to do that is to replace |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5462 the blank and the asterisk with two variables that we can call |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5463 `graph-blank' and `graph-symbol' and define those variables separately. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5464 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5465 Also, the documentation is not well written. These considerations lead |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5466 us to the second version of the function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5467 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5468 (defvar graph-symbol "*" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5469 "String used as symbol in graph, usually an asterisk.") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5470 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5471 (defvar graph-blank " " |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5472 "String used as blank in graph, usually a blank space. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5473 graph-blank must be the same number of columns wide |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5474 as graph-symbol.") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5475 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5476 (For an explanation of `defvar', see *Note Initializing a Variable with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5477 `defvar': defvar.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5478 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5479 ;;; Second version. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5480 (defun column-of-graph (max-graph-height actual-height) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5481 "Return MAX-GRAPH-HEIGHT strings; ACTUAL-HEIGHT are graph-symbols. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5482 The graph-symbols are contiguous entries at the end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5483 of the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5484 The list will be inserted as one column of a graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5485 The strings are either graph-blank or graph-symbol." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5486 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5487 (let ((insert-list nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5488 (number-of-top-blanks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5489 (- max-graph-height actual-height))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5490 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5491 ;; Fill in `graph-symbols'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5492 (while (> actual-height 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5493 (setq insert-list (cons graph-symbol insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5494 (setq actual-height (1- actual-height))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5495 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5496 ;; Fill in `graph-blanks'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5497 (while (> number-of-top-blanks 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5498 (setq insert-list (cons graph-blank insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5499 (setq number-of-top-blanks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5500 (1- number-of-top-blanks))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5501 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5502 ;; Return whole list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5503 insert-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5504 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5505 If we wished, we could rewrite `column-of-graph' a third time to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5506 provide optionally for a line graph as well as for a bar graph. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5507 would not be hard to do. One way to think of a line graph is that it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5508 is no more than a bar graph in which the part of each bar that is below |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5509 the top is blank. To construct a column for a line graph, the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5510 first constructs a list of blanks that is one shorter than the value, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5511 then it uses `cons' to attach a graph symbol to the list; then it uses |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5512 `cons' again to attach the `top blanks' to the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5513 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5514 It is easy to see how to write such a function, but since we don't need |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5515 it, we will not do it. But the job could be done, and if it were done, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5516 it would be done with `column-of-graph'. Even more important, it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5517 worth noting that few changes would have to be made anywhere else. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5518 enhancement, if we ever wish to make it, is simple. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5519 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5520 Now, finally, we come to our first actual graph printing function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5521 This prints the body of a graph, not the labels for the vertical and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5522 horizontal axes, so we can call this `graph-body-print'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5523 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5524 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5525 File: eintr, Node: graph-body-print, Next: recursive-graph-body-print, Prev: Columns of a graph, Up: Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5526 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5527 15.1 The `graph-body-print' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5528 ==================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5529 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5530 After our preparation in the preceding section, the `graph-body-print' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5531 function is straightforward. The function will print column after |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5532 column of asterisks and blanks, using the elements of a numbers' list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5533 to specify the number of asterisks in each column. This is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5534 repetitive act, which means we can use a decrementing `while' loop or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5535 recursive function for the job. In this section, we will write the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5536 definition using a `while' loop. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5537 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5538 The `column-of-graph' function requires the height of the graph as an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5539 argument, so we should determine and record that as a local variable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5540 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5541 This leads us to the following template for the `while' loop version of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5542 this function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5543 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5544 (defun graph-body-print (numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5545 "DOCUMENTATION..." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5546 (let ((height ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5547 ...)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5548 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5549 (while numbers-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5550 INSERT-COLUMNS-AND-REPOSITION-POINT |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5551 (setq numbers-list (cdr numbers-list))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5552 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5553 We need to fill in the slots of the template. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5554 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5555 Clearly, we can use the `(apply 'max numbers-list)' expression to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5556 determine the height of the graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5557 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5558 The `while' loop will cycle through the `numbers-list' one element at a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5559 time. As it is shortened by the `(setq numbers-list (cdr |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5560 numbers-list))' expression, the CAR of each instance of the list is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5561 value of the argument for `column-of-graph'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5562 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5563 At each cycle of the `while' loop, the `insert-rectangle' function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5564 inserts the list returned by `column-of-graph'. Since the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5565 `insert-rectangle' function moves point to the lower right of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5566 inserted rectangle, we need to save the location of point at the time |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5567 the rectangle is inserted, move back to that position after the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5568 rectangle is inserted, and then move horizontally to the next place |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5569 from which `insert-rectangle' is called. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5570 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5571 If the inserted columns are one character wide, as they will be if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5572 single blanks and asterisks are used, the repositioning command is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5573 simply `(forward-char 1)'; however, the width of a column may be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5574 greater than one. This means that the repositioning command should be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5575 written `(forward-char symbol-width)'. The `symbol-width' itself is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5576 the length of a `graph-blank' and can be found using the expression |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5577 `(length graph-blank)'. The best place to bind the `symbol-width' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5578 variable to the value of the width of graph column is in the varlist of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5579 the `let' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5580 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5581 These considerations lead to the following function definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5582 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5583 (defun graph-body-print (numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5584 "Print a bar graph of the NUMBERS-LIST. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5585 The numbers-list consists of the Y-axis values." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5586 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5587 (let ((height (apply 'max numbers-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5588 (symbol-width (length graph-blank)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5589 from-position) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5590 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5591 (while numbers-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5592 (setq from-position (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5593 (insert-rectangle |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5594 (column-of-graph height (car numbers-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5595 (goto-char from-position) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5596 (forward-char symbol-width) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5597 ;; Draw graph column by column. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5598 (sit-for 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5599 (setq numbers-list (cdr numbers-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5600 ;; Place point for X axis labels. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5601 (forward-line height) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5602 (insert "\n") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5603 )) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5604 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5605 The one unexpected expression in this function is the `(sit-for 0)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5606 expression in the `while' loop. This expression makes the graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5607 printing operation more interesting to watch than it would be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5608 otherwise. The expression causes Emacs to `sit' or do nothing for a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5609 zero length of time and then redraw the screen. Placed here, it causes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5610 Emacs to redraw the screen column by column. Without it, Emacs would |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5611 not redraw the screen until the function exits. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5612 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5613 We can test `graph-body-print' with a short list of numbers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5614 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5615 1. Install `graph-symbol', `graph-blank', `column-of-graph', which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5616 are in *Note Columns of a graph::, and `graph-body-print'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5617 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5618 2. Copy the following expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5619 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5620 (graph-body-print '(1 2 3 4 6 4 3 5 7 6 5 2 3)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5621 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5622 3. Switch to the `*scratch*' buffer and place the cursor where you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5623 want the graph to start. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5624 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5625 4. Type `M-:' (`eval-expression'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5626 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5627 5. Yank the `graph-body-print' expression into the minibuffer with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5628 `C-y' (`yank)'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5629 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5630 6. Press <RET> to evaluate the `graph-body-print' expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5631 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5632 Emacs will print a graph like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5633 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5634 * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5635 * ** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5636 * **** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5637 *** **** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5638 ********* * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5639 ************ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5640 ************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5641 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5642 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5643 File: eintr, Node: recursive-graph-body-print, Next: Printed Axes, Prev: graph-body-print, Up: Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5644 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5645 15.2 The `recursive-graph-body-print' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5646 ============================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5647 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5648 The `graph-body-print' function may also be written recursively. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5649 recursive solution is divided into two parts: an outside `wrapper' that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5650 uses a `let' expression to determine the values of several variables |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5651 that need only be found once, such as the maximum height of the graph, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5652 and an inside function that is called recursively to print the graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5653 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5654 The `wrapper' is uncomplicated: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5655 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5656 (defun recursive-graph-body-print (numbers-list) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5657 "Print a bar graph of the NUMBERS-LIST. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5658 The numbers-list consists of the Y-axis values." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5659 (let ((height (apply 'max numbers-list)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5660 (symbol-width (length graph-blank)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5661 from-position) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5662 (recursive-graph-body-print-internal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5663 numbers-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5664 height |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5665 symbol-width))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5666 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5667 The recursive function is a little more difficult. It has four parts: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5668 the `do-again-test', the printing code, the recursive call, and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5669 `next-step-expression'. The `do-again-test' is an `if' expression that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5670 determines whether the `numbers-list' contains any remaining elements; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5671 if it does, the function prints one column of the graph using the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5672 printing code and calls itself again. The function calls itself again |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5673 according to the value produced by the `next-step-expression' which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5674 causes the call to act on a shorter version of the `numbers-list'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5675 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5676 (defun recursive-graph-body-print-internal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5677 (numbers-list height symbol-width) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5678 "Print a bar graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5679 Used within recursive-graph-body-print function." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5680 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5681 (if numbers-list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5682 (progn |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5683 (setq from-position (point)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5684 (insert-rectangle |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5685 (column-of-graph height (car numbers-list))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5686 (goto-char from-position) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5687 (forward-char symbol-width) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5688 (sit-for 0) ; Draw graph column by column. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5689 (recursive-graph-body-print-internal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5690 (cdr numbers-list) height symbol-width)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5691 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5692 After installation, this expression can be tested; here is a sample: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5693 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5694 (recursive-graph-body-print '(3 2 5 6 7 5 3 4 6 4 3 2 1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5695 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5696 Here is what `recursive-graph-body-print' produces: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5697 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5698 * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5699 ** * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5700 **** * |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5701 **** *** |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5702 * ********* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5703 ************ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5704 ************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5705 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5706 Either of these two functions, `graph-body-print' or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5707 `recursive-graph-body-print', create the body of a graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5709 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5710 File: eintr, Node: Printed Axes, Next: Line Graph Exercise, Prev: recursive-graph-body-print, Up: Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5711 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5712 15.3 Need for Printed Axes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5713 ========================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5714 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5715 A graph needs printed axes, so you can orient yourself. For a do-once |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5716 project, it may be reasonable to draw the axes by hand using Emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5717 Picture mode; but a graph drawing function may be used more than once. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5718 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5719 For this reason, I have written enhancements to the basic |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5720 `print-graph-body' function that automatically print labels for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5721 horizontal and vertical axes. Since the label printing functions do |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5722 not contain much new material, I have placed their description in an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5723 appendix. *Note A Graph with Labelled Axes: Full Graph. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5724 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5725 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5726 File: eintr, Node: Line Graph Exercise, Prev: Printed Axes, Up: Readying a Graph |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5727 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5728 15.4 Exercise |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5729 ============= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5730 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5731 Write a line graph version of the graph printing functions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5732 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5733 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5734 File: eintr, Node: Emacs Initialization, Next: Debugging, Prev: Readying a Graph, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5735 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5736 16 Your `.emacs' File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5737 ********************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5738 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5739 "You don't have to like Emacs to like it" - this seemingly paradoxical |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5740 statement is the secret of GNU Emacs. The plain, `out of the box' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5741 Emacs is a generic tool. Most people who use it, customize it to suit |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5742 themselves. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5743 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5744 GNU Emacs is mostly written in Emacs Lisp; this means that by writing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5745 expressions in Emacs Lisp you can change or extend Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5746 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5747 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5748 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5749 * Default Configuration:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5750 * Site-wide Init:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5751 * defcustom:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5752 * Beginning a .emacs File:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5753 * Text and Auto-fill:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5754 * Mail Aliases:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5755 * Indent Tabs Mode:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5756 * Keybindings:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5757 * Keymaps:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5758 * Loading Files:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5759 * Autoload:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5760 * Simple Extension:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5761 * X11 Colors:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5762 * Miscellaneous:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5763 * Mode Line:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5764 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5765 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5766 File: eintr, Node: Default Configuration, Next: Site-wide Init, Prev: Emacs Initialization, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5767 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5768 Emacs' Default Configuration |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5769 ============================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5770 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5771 There are those who appreciate Emacs' default configuration. After |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5772 all, Emacs starts you in C mode when you edit a C file, starts you in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5773 Fortran mode when you edit a Fortran file, and starts you in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5774 Fundamental mode when you edit an unadorned file. This all makes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5775 sense, if you do not know who is going to use Emacs. Who knows what a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5776 person hopes to do with an unadorned file? Fundamental mode is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5777 right default for such a file, just as C mode is the right default for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5778 editing C code. (Enough programming languages have syntaxes that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5779 enable them to share or nearly share features, so C mode is now |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5780 provided by by CC mode, the `C Collection'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5781 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5782 But when you do know who is going to use Emacs--you, yourself--then it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5783 makes sense to customize Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5784 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5785 For example, I seldom want Fundamental mode when I edit an otherwise |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5786 undistinguished file; I want Text mode. This is why I customize Emacs: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5787 so it suits me. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5788 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5789 You can customize and extend Emacs by writing or adapting a `~/.emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5790 file. This is your personal initialization file; its contents, written |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5791 in Emacs Lisp, tell Emacs what to do.(1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5792 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5793 A `~/.emacs' file contains Emacs Lisp code. You can write this code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5794 yourself; or you can use Emacs' `customize' feature to write the code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5795 for you. You can combine your own expressions and auto-written |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5796 Customize expressions in your `.emacs' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5797 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5798 (I myself prefer to write my own expressions, except for those, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5799 particularly fonts, that I find easier to manipulate using the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5800 `customize' command. I combine the two methods.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5801 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5802 Most of this chapter is about writing expressions yourself. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5803 describes a simple `.emacs' file; for more information, see *Note The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5804 Init File: (emacs)Init File, and *Note The Init File: (elisp)Init File. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5805 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5806 ---------- Footnotes ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5807 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5808 (1) You may also add `.el' to `~/.emacs' and call it a `~/.emacs.el' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5809 file. In the past, you were forbidden to type the extra keystrokes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5810 that the name `~/.emacs.el' requires, but now you may. The new format |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5811 is consistent with the Emacs Lisp file naming conventions; the old |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5812 format saves typing. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5813 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5814 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5815 File: eintr, Node: Site-wide Init, Next: defcustom, Prev: Default Configuration, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5816 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5817 16.1 Site-wide Initialization Files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5818 =================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5819 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5820 In addition to your personal initialization file, Emacs automatically |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5821 loads various site-wide initialization files, if they exist. These |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5822 have the same form as your `.emacs' file, but are loaded by everyone. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5823 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5824 Two site-wide initialization files, `site-load.el' and `site-init.el', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5825 are loaded into Emacs and then `dumped' if a `dumped' version of Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5826 is created, as is most common. (Dumped copies of Emacs load more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5827 quickly. However, once a file is loaded and dumped, a change to it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5828 does not lead to a change in Emacs unless you load it yourself or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5829 re-dump Emacs. *Note Building Emacs: (elisp)Building Emacs, and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5830 `INSTALL' file.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5831 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5832 Three other site-wide initialization files are loaded automatically |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5833 each time you start Emacs, if they exist. These are `site-start.el', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5834 which is loaded _before_ your `.emacs' file, and `default.el', and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5835 terminal type file, which are both loaded _after_ your `.emacs' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5836 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5837 Settings and definitions in your `.emacs' file will overwrite |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5838 conflicting settings and definitions in a `site-start.el' file, if it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5839 exists; but the settings and definitions in a `default.el' or terminal |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5840 type file will overwrite those in your `.emacs' file. (You can prevent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5841 interference from a terminal type file by setting `term-file-prefix' to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5842 `nil'. *Note A Simple Extension: Simple Extension.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5843 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5844 The `INSTALL' file that comes in the distribution contains descriptions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5845 of the `site-init.el' and `site-load.el' files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5846 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5847 The `loadup.el', `startup.el', and `loaddefs.el' files control loading. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5848 These files are in the `lisp' directory of the Emacs distribution and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5849 are worth perusing. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5850 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5851 The `loaddefs.el' file contains a good many suggestions as to what to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5852 put into your own `.emacs' file, or into a site-wide initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5853 file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5854 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5855 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5856 File: eintr, Node: defcustom, Next: Beginning a .emacs File, Prev: Site-wide Init, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5857 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5858 16.2 Specifying Variables using `defcustom' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5859 =========================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5860 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5861 You can specify variables using `defcustom' so that you and others can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5862 then use Emacs' `customize' feature to set their values. (You cannot |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5863 use `customize' to write function definitions; but you can write |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5864 `defuns' in your `.emacs' file. Indeed, you can write any Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5865 expression in your `.emacs' file.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5866 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5867 The `customize' feature depends on the `defcustom' special form. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5868 Although you can use `defvar' or `setq' for variables that users set, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5869 the `defcustom' special form is designed for the job. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5870 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5871 You can use your knowledge of `defvar' for writing the first three |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5872 arguments for `defcustom'. The first argument to `defcustom' is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5873 name of the variable. The second argument is the variable's initial |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5874 value, if any; and this value is set only if the value has not already |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5875 been set. The third argument is the documentation. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5876 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5877 The fourth and subsequent arguments to `defcustom' specify types and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5878 options; these are not featured in `defvar'. (These arguments are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5879 optional.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5880 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5881 Each of these arguments consists of a keyword followed by a value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5882 Each keyword starts with the colon character `:'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5883 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5884 For example, the customizable user option variable `text-mode-hook' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5885 looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5886 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5887 (defcustom text-mode-hook nil |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5888 "Normal hook run when entering Text mode and many related modes." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5889 :type 'hook |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5890 :options '(turn-on-auto-fill flyspell-mode) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5891 :group 'data) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5892 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5893 The name of the variable is `text-mode-hook'; it has no default value; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5894 and its documentation string tells you what it does. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5895 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5896 The `:type' keyword tells Emacs the kind of data to which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5897 `text-mode-hook' should be set and how to display the value in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5898 Customization buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5899 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5900 The `:options' keyword specifies a suggested list of values for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5901 variable. Currently, you can use `:options' only for a hook. The list |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5902 is only a suggestion; it is not exclusive; a person who sets the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5903 variable may set it to other values; the list shown following the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5904 `:options' keyword is intended to offer convenient choices to a user. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5905 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5906 Finally, the `:group' keyword tells the Emacs Customization command in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5907 which group the variable is located. This tells where to find it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5908 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5909 For more information, see *Note Writing Customization Definitions: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5910 (elisp)Customization. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5911 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5912 Consider `text-mode-hook' as an example. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5913 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5914 There are two ways to customize this variable. You can use the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5915 customization command or write the appropriate expressions yourself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5916 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5917 Using the customization command, you can type: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5918 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5919 M-x customize |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5920 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5921 and find that the group for editing files of data is called `data'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5922 Enter that group. Text Mode Hook is the first member. You can click |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5923 on its various options, such as `turn-on-auto-fill', to set the values. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5924 After you click on the button to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5925 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5926 Save for Future Sessions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5927 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5928 Emacs will write an expression into your `.emacs' file. It will look |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5929 like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5930 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5931 (custom-set-variables |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5932 ;; custom-set-variables was added by Custom. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5933 ;; If you edit it by hand, you could mess it up, so be careful. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5934 ;; Your init file should contain only one such instance. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5935 ;; If there is more than one, they won't work right. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5936 '(text-mode-hook (quote (turn-on-auto-fill text-mode-hook-identify)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5937 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5938 (The `text-mode-hook-identify' function tells |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5939 `toggle-text-mode-auto-fill' which buffers are in Text mode. It comes |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5940 on automatically. ) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5941 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5942 The `custom-set-variables' function works somewhat differently than a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5943 `setq'. While I have never learned the differences, I modify the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5944 `custom-set-variables' expressions in my `.emacs' file by hand: I make |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5945 the changes in what appears to me to be a reasonable manner and have |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5946 not had any problems. Others prefer to use the Customization command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5947 and let Emacs do the work for them. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5948 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5949 Another `custom-set-...' function is `custom-set-faces'. This function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5950 sets the various font faces. Over time, I have set a considerable |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5951 number of faces. Some of the time, I re-set them using `customize'; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5952 other times, I simply edit the `custom-set-faces' expression in my |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5953 `.emacs' file itself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5954 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5955 The second way to customize your `text-mode-hook' is to set it yourself |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5956 in your `.emacs' file using code that has nothing to do with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5957 `custom-set-...' functions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5958 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5959 When you do this, and later use `customize', you will see a message |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5960 that says |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5961 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5962 CHANGED outside Customize; operating on it here may be unreliable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5963 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5964 This message is only a warning. If you click on the button to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5965 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5966 Save for Future Sessions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5968 Emacs will write a `custom-set-...' expression near the end of your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5969 `.emacs' file that will be evaluated after your hand-written |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5970 expression. It will, therefore, overrule your hand-written expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5971 No harm will be done. When you do this, however, be careful to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5972 remember which expression is active; if you forget, you may confuse |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5973 yourself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5974 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5975 So long as you remember where the values are set, you will have no |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5976 trouble. In any event, the values are always set in your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5977 initialization file, which is usually called `.emacs'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5978 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5979 I myself use `customize' for hardly anything. Mostly, I write |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5980 expressions myself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5981 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5982 Incidentally, `defsubst' defines an inline function. The syntax is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5983 just like that of `defun'. `defconst' defines a symbol as a constant. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5984 The intent is that neither programs nor users should ever change a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5985 value set by `defconst' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5986 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5987 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5988 File: eintr, Node: Beginning a .emacs File, Next: Text and Auto-fill, Prev: defcustom, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5989 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5990 16.3 Beginning a `.emacs' File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5991 ============================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5992 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5993 When you start Emacs, it loads your `.emacs' file unless you tell it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5994 not to by specifying `-q' on the command line. (The `emacs -q' command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5995 gives you a plain, out-of-the-box Emacs.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5996 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5997 A `.emacs' file contains Lisp expressions. Often, these are no more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5998 than expressions to set values; sometimes they are function definitions. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
5999 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6000 *Note The Init File `~/.emacs': (emacs)Init File, for a short |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6001 description of initialization files. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6002 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6003 This chapter goes over some of the same ground, but is a walk among |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6004 extracts from a complete, long-used `.emacs' file--my own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6005 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6006 The first part of the file consists of comments: reminders to myself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6007 By now, of course, I remember these things, but when I started, I did |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6008 not. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6009 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6010 ;;;; Bob's .emacs file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6011 ; Robert J. Chassell |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6012 ; 26 September 1985 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6013 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6014 Look at that date! I started this file a long time ago. I have been |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6015 adding to it ever since. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6016 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6017 ; Each section in this file is introduced by a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6018 ; line beginning with four semicolons; and each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6019 ; entry is introduced by a line beginning with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6020 ; three semicolons. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6021 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6022 This describes the usual conventions for comments in Emacs Lisp. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6023 Everything on a line that follows a semicolon is a comment. Two, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6024 three, and four semicolons are used as section and subsection markers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6025 (*Note Comments: (elisp)Comments, for more about comments.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6026 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6027 ;;;; The Help Key |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6028 ; Control-h is the help key; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6029 ; after typing control-h, type a letter to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6030 ; indicate the subject about which you want help. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6031 ; For an explanation of the help facility, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6032 ; type control-h two times in a row. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6033 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6034 Just remember: type `C-h' two times for help. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6035 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6036 ; To find out about any mode, type control-h m |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6037 ; while in that mode. For example, to find out |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6038 ; about mail mode, enter mail mode and then type |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6039 ; control-h m. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6040 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6041 `Mode help', as I call this, is very helpful. Usually, it tells you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6042 all you need to know. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6043 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6044 Of course, you don't need to include comments like these in your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6045 `.emacs' file. I included them in mine because I kept forgetting about |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6046 Mode help or the conventions for comments--but I was able to remember |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6047 to look here to remind myself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6048 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6049 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6050 File: eintr, Node: Text and Auto-fill, Next: Mail Aliases, Prev: Beginning a .emacs File, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6051 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6052 16.4 Text and Auto Fill Mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6053 ============================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6054 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6055 Now we come to the part that `turns on' Text mode and Auto Fill mode. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6056 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6057 ;;; Text mode and Auto Fill mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6058 ; The next two lines put Emacs into Text mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6059 ; and Auto Fill mode, and are for writers who |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6060 ; want to start writing prose rather than code. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6061 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6062 (setq default-major-mode 'text-mode) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6063 (add-hook 'text-mode-hook 'turn-on-auto-fill) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6064 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6065 Here is the first part of this `.emacs' file that does something |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6066 besides remind a forgetful human! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6067 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6068 The first of the two lines in parentheses tells Emacs to turn on Text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6069 mode when you find a file, _unless_ that file should go into some other |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6070 mode, such as C mode. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6071 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6072 When Emacs reads a file, it looks at the extension to the file name, if |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6073 any. (The extension is the part that comes after a `.'.) If the file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6074 ends with a `.c' or `.h' extension then Emacs turns on C mode. Also, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6075 Emacs looks at first nonblank line of the file; if the line says |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6076 `-*- C -*-', Emacs turns on C mode. Emacs possesses a list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6077 extensions and specifications that it uses automatically. In addition, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6078 Emacs looks near the last page for a per-buffer, "local variables |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6079 list", if any. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6080 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6081 *Note How Major Modes are Chosen: (emacs)Choosing Modes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6082 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6083 *Note Local Variables in Files: (emacs)File Variables. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6084 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6085 Now, back to the `.emacs' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6086 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6087 Here is the line again; how does it work? |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6088 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6089 (setq default-major-mode 'text-mode) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6090 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6091 This line is a short, but complete Emacs Lisp expression. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6092 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6093 We are already familiar with `setq'. It sets the following variable, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6094 `default-major-mode', to the subsequent value, which is `text-mode'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6095 The single quote mark before `text-mode' tells Emacs to deal directly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6096 with the `text-mode' variable, not with whatever it might stand for. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6097 *Note Setting the Value of a Variable: set & setq, for a reminder of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6098 how `setq' works. The main point is that there is no difference |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6099 between the procedure you use to set a value in your `.emacs' file and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6100 the procedure you use anywhere else in Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6101 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6102 Here is the next line: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6103 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6104 (add-hook 'text-mode-hook 'turn-on-auto-fill) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6105 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6106 In this line, the `add-hook' command adds `turn-on-auto-fill' to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6107 variable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6108 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6109 `turn-on-auto-fill' is the name of a program, that, you guessed it!, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6110 turns on Auto Fill mode. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6111 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6112 Every time Emacs turns on Text mode, Emacs runs the commands `hooked' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6113 onto Text mode. So every time Emacs turns on Text mode, Emacs also |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6114 turns on Auto Fill mode. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6115 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6116 In brief, the first line causes Emacs to enter Text mode when you edit a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6117 file, unless the file name extension, a first non-blank line, or local |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6118 variables to tell Emacs otherwise. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6119 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6120 Text mode among other actions, sets the syntax table to work |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6121 conveniently for writers. In Text mode, Emacs considers an apostrophe |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6122 as part of a word like a letter; but Emacs does not consider a period |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6123 or a space as part of a word. Thus, `M-f' moves you over `it's'. On |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6124 the other hand, in C mode, `M-f' stops just after the `t' of `it's'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6125 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6126 The second line causes Emacs to turn on Auto Fill mode when it turns on |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6127 Text mode. In Auto Fill mode, Emacs automatically breaks a line that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6128 is too wide and brings the excessively wide part of the line down to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6129 the next line. Emacs breaks lines between words, not within them. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6130 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6131 When Auto Fill mode is turned off, lines continue to the right as you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6132 type them. Depending on how you set the value of `truncate-lines', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6133 words you type either disappear off the right side of the screen, or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6134 else are shown, in a rather ugly and unreadable manner, as a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6135 continuation line on the screen. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6136 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6137 In addition, in this part of my `.emacs' file, I tell the Emacs fill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6138 commands to insert two spaces after a colon: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6139 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6140 (setq colon-double-space t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6141 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6142 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6143 File: eintr, Node: Mail Aliases, Next: Indent Tabs Mode, Prev: Text and Auto-fill, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6144 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6145 16.5 Mail Aliases |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6146 ================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6147 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6148 Here is a `setq' that `turns on' mail aliases, along with more |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6149 reminders. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6150 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6151 ;;; Mail mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6152 ; To enter mail mode, type `C-x m' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6153 ; To enter RMAIL (for reading mail), |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6154 ; type `M-x rmail' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6155 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6156 (setq mail-aliases t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6157 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6158 This `setq' command sets the value of the variable `mail-aliases' to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6159 `t'. Since `t' means true, the line says, in effect, "Yes, use mail |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6160 aliases." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6161 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6162 Mail aliases are convenient short names for long email addresses or for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6163 lists of email addresses. The file where you keep your `aliases' is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6164 `~/.mailrc'. You write an alias like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6165 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6166 alias geo george@foobar.wiz.edu |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6167 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6168 When you write a message to George, address it to `geo'; the mailer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6169 will automatically expand `geo' to the full address. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6170 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6171 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6172 File: eintr, Node: Indent Tabs Mode, Next: Keybindings, Prev: Mail Aliases, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6173 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6174 16.6 Indent Tabs Mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6175 ===================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6176 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6177 By default, Emacs inserts tabs in place of multiple spaces when it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6178 formats a region. (For example, you might indent many lines of text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6179 all at once with the `indent-region' command.) Tabs look fine on a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6180 terminal or with ordinary printing, but they produce badly indented |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6181 output when you use TeX or Texinfo since TeX ignores tabs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6182 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6183 The following turns off Indent Tabs mode: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6184 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6185 ;;; Prevent Extraneous Tabs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6186 (setq-default indent-tabs-mode nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6187 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6188 Note that this line uses `setq-default' rather than the `setq' command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6189 that we have seen before. The `setq-default' command sets values only |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6190 in buffers that do not have their own local values for the variable. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6191 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6192 *Note Tabs vs. Spaces: (emacs)Just Spaces. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6193 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6194 *Note Local Variables in Files: (emacs)File Variables. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6195 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6196 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6197 File: eintr, Node: Keybindings, Next: Keymaps, Prev: Indent Tabs Mode, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6198 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6199 16.7 Some Keybindings |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6200 ===================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6201 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6202 Now for some personal keybindings: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6203 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6204 ;;; Compare windows |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6205 (global-set-key "\C-cw" 'compare-windows) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6206 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6207 `compare-windows' is a nifty command that compares the text in your |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6208 current window with text in the next window. It makes the comparison |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6209 by starting at point in each window, moving over text in each window as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6210 far as they match. I use this command all the time. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6211 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6212 This also shows how to set a key globally, for all modes. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6213 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6214 The command is `global-set-key'. It is followed by the keybinding. In |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6215 a `.emacs' file, the keybinding is written as shown: `\C-c' stands for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6216 `control-c', which means `press the control key and the `c' key at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6217 same time'. The `w' means `press the `w' key'. The keybinding is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6218 surrounded by double quotation marks. In documentation, you would |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6219 write this as `C-c w'. (If you were binding a <META> key, such as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6220 `M-c', rather than a <CTRL> key, you would write `\M-c'. *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6221 Rebinding Keys in Your Init File: (emacs)Init Rebinding, for details.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6222 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6223 The command invoked by the keys is `compare-windows'. Note that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6224 `compare-windows' is preceded by a single quote; otherwise, Emacs would |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6225 first try to evaluate the symbol to determine its value. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6226 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6227 These three things, the double quotation marks, the backslash before |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6228 the `C', and the single quote mark are necessary parts of keybinding |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6229 that I tend to forget. Fortunately, I have come to remember that I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6230 should look at my existing `.emacs' file, and adapt what is there. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6231 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6232 As for the keybinding itself: `C-c w'. This combines the prefix key, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6233 `C-c', with a single character, in this case, `w'. This set of keys, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6234 `C-c' followed by a single character, is strictly reserved for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6235 individuals' own use. (I call these `own' keys, since these are for my |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6236 own use.) You should always be able to create such a keybinding for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6237 your own use without stomping on someone else's keybinding. If you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6238 ever write an extension to Emacs, please avoid taking any of these keys |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6239 for public use. Create a key like `C-c C-w' instead. Otherwise, we |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6240 will run out of `own' keys. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6241 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6242 Here is another keybinding, with a comment: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6243 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6244 ;;; Keybinding for `occur' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6245 ; I use occur a lot, so let's bind it to a key: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6246 (global-set-key "\C-co" 'occur) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6247 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6248 The `occur' command shows all the lines in the current buffer that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6249 contain a match for a regular expression. Matching lines are shown in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6250 a buffer called `*Occur*'. That buffer serves as a menu to jump to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6251 occurrences. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6252 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6253 Here is how to unbind a key, so it does not work: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6254 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6255 ;;; Unbind `C-x f' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6256 (global-unset-key "\C-xf") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6257 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6258 There is a reason for this unbinding: I found I inadvertently typed |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6259 `C-x f' when I meant to type `C-x C-f'. Rather than find a file, as I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6260 intended, I accidentally set the width for filled text, almost always |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6261 to a width I did not want. Since I hardly ever reset my default width, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6262 I simply unbound the key. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6263 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6264 The following rebinds an existing key: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6265 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6266 ;;; Rebind `C-x C-b' for `buffer-menu' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6267 (global-set-key "\C-x\C-b" 'buffer-menu) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6268 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6269 By default, `C-x C-b' runs the `list-buffers' command. This command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6270 lists your buffers in _another_ window. Since I almost always want to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6271 do something in that window, I prefer the `buffer-menu' command, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6272 not only lists the buffers, but moves point into that window. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6273 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6274 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6275 File: eintr, Node: Keymaps, Next: Loading Files, Prev: Keybindings, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6276 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6277 16.8 Keymaps |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6278 ============ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6279 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6280 Emacs uses "keymaps" to record which keys call which commands. When |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6281 you use `global-set-key' to set the keybinding for a single command in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6282 all parts of Emacs, you are specifying the keybinding in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6283 `current-global-map'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6284 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6285 Specific modes, such as C mode or Text mode, have their own keymaps; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6286 the mode-specific keymaps override the global map that is shared by all |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6287 buffers. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6288 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6289 The `global-set-key' function binds, or rebinds, the global keymap. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6290 For example, the following binds the key `C-x C-b' to the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6291 `buffer-menu': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6292 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6293 (global-set-key "\C-x\C-b" 'buffer-menu) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6294 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6295 Mode-specific keymaps are bound using the `define-key' function, which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6296 takes a specific keymap as an argument, as well as the key and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6297 command. For example, my `.emacs' file contains the following |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6298 expression to bind the `texinfo-insert-@group' command to `C-c C-c g': |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6299 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6300 (define-key texinfo-mode-map "\C-c\C-cg" 'texinfo-insert-@group) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6301 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6302 The `texinfo-insert-@group' function itself is a little extension to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6303 Texinfo mode that inserts `@group' into a Texinfo file. I use this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6304 command all the time and prefer to type the three strokes `C-c C-c g' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6305 rather than the six strokes `@ g r o u p'. (`@group' and its matching |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6306 `@end group' are commands that keep all enclosed text together on one |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6307 page; many multi-line examples in this book are surrounded by `@group |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6308 ... @end group'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6309 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6310 Here is the `texinfo-insert-@group' function definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6311 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6312 (defun texinfo-insert-@group () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6313 "Insert the string @group in a Texinfo buffer." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6314 (interactive) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6315 (beginning-of-line) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6316 (insert "@group\n")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6317 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6318 (Of course, I could have used Abbrev mode to save typing, rather than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6319 write a function to insert a word; but I prefer key strokes consistent |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6320 with other Texinfo mode key bindings.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6321 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6322 You will see numerous `define-key' expressions in `loaddefs.el' as well |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6323 as in the various mode libraries, such as `cc-mode.el' and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6324 `lisp-mode.el'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6325 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6326 *Note Customizing Key Bindings: (emacs)Key Bindings, and *Note Keymaps: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6327 (elisp)Keymaps, for more information about keymaps. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6328 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6329 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6330 File: eintr, Node: Loading Files, Next: Autoload, Prev: Keymaps, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6331 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6332 16.9 Loading Files |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6333 ================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6334 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6335 Many people in the GNU Emacs community have written extensions to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6336 Emacs. As time goes by, these extensions are often included in new |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6337 releases. For example, the Calendar and Diary packages are now part of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6338 the standard GNU Emacs, as is Calc. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6339 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6340 You can use a `load' command to evaluate a complete file and thereby |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6341 install all the functions and variables in the file into Emacs. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6342 example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6344 (load "~/emacs/slowsplit") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6345 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6346 This evaluates, i.e. loads, the `slowsplit.el' file or if it exists, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6347 the faster, byte compiled `slowsplit.elc' file from the `emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6348 sub-directory of your home directory. The file contains the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6349 `split-window-quietly', which John Robinson wrote in 1989. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6350 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6351 The `split-window-quietly' function splits a window with the minimum of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6352 redisplay. I installed it in 1989 because it worked well with the slow |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6353 1200 baud terminals I was then using. Nowadays, I only occasionally |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6354 come across such a slow connection, but I continue to use the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6355 because I like the way it leaves the bottom half of a buffer in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6356 lower of the new windows and the top half in the upper window. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6357 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6358 To replace the key binding for the default `split-window-vertically', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6359 you must also unset that key and bind the keys to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6360 `split-window-quietly', like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6361 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6362 (global-unset-key "\C-x2") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6363 (global-set-key "\C-x2" 'split-window-quietly) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6364 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6365 If you load many extensions, as I do, then instead of specifying the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6366 exact location of the extension file, as shown above, you can specify |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6367 that directory as part of Emacs' `load-path'. Then, when Emacs loads a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6368 file, it will search that directory as well as its default list of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6369 directories. (The default list is specified in `paths.h' when Emacs is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6370 built.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6371 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6372 The following command adds your `~/emacs' directory to the existing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6373 load path: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6374 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6375 ;;; Emacs Load Path |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6376 (setq load-path (cons "~/emacs" load-path)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6377 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6378 Incidentally, `load-library' is an interactive interface to the `load' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6379 function. The complete function looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6380 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6381 (defun load-library (library) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6382 "Load the library named LIBRARY. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6383 This is an interface to the function `load'." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6384 (interactive |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6385 (list (completing-read "Load library: " |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6386 'locate-file-completion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6387 (cons load-path (get-load-suffixes))))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6388 (load library)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6389 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6390 The name of the function, `load-library', comes from the use of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6391 `library' as a conventional synonym for `file'. The source for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6392 `load-library' command is in the `files.el' library. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6393 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6394 Another interactive command that does a slightly different job is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6395 `load-file'. *Note Libraries of Lisp Code for Emacs: (emacs)Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6396 Libraries, for information on the distinction between `load-library' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6397 and this command. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6398 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6399 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6400 File: eintr, Node: Autoload, Next: Simple Extension, Prev: Loading Files, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6401 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6402 16.10 Autoloading |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6403 ================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6404 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6405 Instead of installing a function by loading the file that contains it, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6406 or by evaluating the function definition, you can make the function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6407 available but not actually install it until it is first called. This |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6408 is called "autoloading". |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6409 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6410 When you execute an autoloaded function, Emacs automatically evaluates |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6411 the file that contains the definition, and then calls the function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6412 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6413 Emacs starts quicker with autoloaded functions, since their libraries |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6414 are not loaded right away; but you need to wait a moment when you first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6415 use such a function, while its containing file is evaluated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6416 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6417 Rarely used functions are frequently autoloaded. The `loaddefs.el' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6418 library contains hundreds of autoloaded functions, from `bookmark-set' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6419 to `wordstar-mode'. Of course, you may come to use a `rare' function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6420 frequently. When you do, you should load that function's file with a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6421 `load' expression in your `.emacs' file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6422 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6423 In my `.emacs' file for Emacs version 22, I load 14 libraries that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6424 contain functions that would otherwise be autoloaded. (Actually, it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6425 would have been better to include these files in my `dumped' Emacs, but |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6426 I forgot. *Note Building Emacs: (elisp)Building Emacs, and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6427 `INSTALL' file for more about dumping.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6428 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6429 You may also want to include autoloaded expressions in your `.emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6430 file. `autoload' is a built-in function that takes up to five |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6431 arguments, the final three of which are optional. The first argument |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6432 is the name of the function to be autoloaded; the second is the name of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6433 the file to be loaded. The third argument is documentation for the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6434 function, and the fourth tells whether the function can be called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6435 interactively. The fifth argument tells what type of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6436 object--`autoload' can handle a keymap or macro as well as a function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6437 (the default is a function). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6438 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6439 Here is a typical example: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6440 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6441 (autoload 'html-helper-mode |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6442 "html-helper-mode" "Edit HTML documents" t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6443 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6444 (`html-helper-mode' is an alternative to `html-mode', which is a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6445 standard part of the distribution). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6446 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6447 This expression autoloads the `html-helper-mode' function. It takes it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6448 from the `html-helper-mode.el' file (or from the byte compiled file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6449 `html-helper-mode.elc', if it exists.) The file must be located in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6450 directory specified by `load-path'. The documentation says that this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6451 is a mode to help you edit documents written in the HyperText Markup |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6452 Language. You can call this mode interactively by typing `M-x |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6453 html-helper-mode'. (You need to duplicate the function's regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6454 documentation in the autoload expression because the regular function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6455 is not yet loaded, so its documentation is not available.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6456 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6457 *Note Autoload: (elisp)Autoload, for more information. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6458 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6459 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6460 File: eintr, Node: Simple Extension, Next: X11 Colors, Prev: Autoload, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6461 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6462 16.11 A Simple Extension: `line-to-top-of-window' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6463 ================================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6464 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6465 Here is a simple extension to Emacs that moves the line point is on to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6466 the top of the window. I use this all the time, to make text easier to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6467 read. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6468 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6469 You can put the following code into a separate file and then load it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6470 from your `.emacs' file, or you can include it within your `.emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6471 file. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6472 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6473 Here is the definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6474 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6475 ;;; Line to top of window; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6476 ;;; replace three keystroke sequence C-u 0 C-l |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6477 (defun line-to-top-of-window () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6478 "Move the line point is on to top of window." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6479 (interactive) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6480 (recenter 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6481 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6482 Now for the keybinding. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6483 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6484 Nowadays, function keys as well as mouse button events and non-ASCII |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6485 characters are written within square brackets, without quotation marks. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6486 (In Emacs version 18 and before, you had to write different function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6487 key bindings for each different make of terminal.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6488 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6489 I bind `line-to-top-of-window' to my <F6> function key like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6490 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6491 (global-set-key [f6] 'line-to-top-of-window) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6492 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6493 For more information, see *Note Rebinding Keys in Your Init File: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6494 (emacs)Init Rebinding. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6495 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6496 If you run two versions of GNU Emacs, such as versions 21 and 22, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6497 use one `.emacs' file, you can select which code to evaluate with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6498 following conditional: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6499 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6500 (cond |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6501 ((string-equal (number-to-string 21) (substring (emacs-version) 10 12)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6502 ;; evaluate version 21 code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6503 ( ... )) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6504 ((string-equal (number-to-string 22) (substring (emacs-version) 10 12)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6505 ;; evaluate version 22 code |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6506 ( ... ))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6507 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6508 For example, in contrast to version 20, version 21 blinks its cursor by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6509 default. I hate such blinking, as well as some other features in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6510 version 21, so I placed the following in my `.emacs' file(1): |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6511 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6512 (if (string-equal "21" (substring (emacs-version) 10 12)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6513 (progn |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6514 (blink-cursor-mode 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6515 ;; Insert newline when you press `C-n' (next-line) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6516 ;; at the end of the buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6517 (setq next-line-add-newlines t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6518 ;; Turn on image viewing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6519 (auto-image-file-mode t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6520 ;; Turn on menu bar (this bar has text) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6521 ;; (Use numeric argument to turn on) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6522 (menu-bar-mode 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6523 ;; Turn off tool bar (this bar has icons) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6524 ;; (Use numeric argument to turn on) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6525 (tool-bar-mode nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6526 ;; Turn off tooltip mode for tool bar |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6527 ;; (This mode causes icon explanations to pop up) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6528 ;; (Use numeric argument to turn on) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6529 (tooltip-mode nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6530 ;; If tooltips turned on, make tips appear promptly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6531 (setq tooltip-delay 0.1) ; default is one second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6532 )) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6533 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6534 (You will note that instead of typing `(number-to-string 21)', I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6535 decided to save typing and wrote `21' as a string, `"21"', rather than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6536 convert it from an integer to a string. In this instance, this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6537 expression is better than the longer, but more general |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6538 `(number-to-string 21)'. However, if you do not know ahead of time |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6539 what type of information will be returned, then the `number-to-string' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6540 function will be needed.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6541 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6542 ---------- Footnotes ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6543 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6544 (1) When I start instances of Emacs that do not load my `.emacs' file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6545 or any site file, I also turn off blinking: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6546 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6547 emacs -q --no-site-file -eval '(blink-cursor-mode nil)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6548 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6549 Or nowadays, using an even more sophisticated set of options, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6550 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6551 emacs -Q - D |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6552 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6553 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6554 File: eintr, Node: X11 Colors, Next: Miscellaneous, Prev: Simple Extension, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6555 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6556 16.12 X11 Colors |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6557 ================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6558 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6559 You can specify colors when you use Emacs with the MIT X Windowing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6560 system. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6561 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6562 I dislike the default colors and specify my own. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6563 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6564 Here are the expressions in my `.emacs' file that set values: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6565 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6566 ;; Set cursor color |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6567 (set-cursor-color "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6568 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6569 ;; Set mouse color |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6570 (set-mouse-color "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6571 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6572 ;; Set foreground and background |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6573 (set-foreground-color "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6574 (set-background-color "darkblue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6575 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6576 ;;; Set highlighting colors for isearch and drag |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6577 (set-face-foreground 'highlight "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6578 (set-face-background 'highlight "blue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6579 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6580 (set-face-foreground 'region "cyan") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6581 (set-face-background 'region "blue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6582 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6583 (set-face-foreground 'secondary-selection "skyblue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6584 (set-face-background 'secondary-selection "darkblue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6585 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6586 ;; Set calendar highlighting colors |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6587 (setq calendar-load-hook |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6588 '(lambda () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6589 (set-face-foreground 'diary-face "skyblue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6590 (set-face-background 'holiday-face "slate blue") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6591 (set-face-foreground 'holiday-face "white"))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6592 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6593 The various shades of blue soothe my eye and prevent me from seeing the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6594 screen flicker. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6595 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6596 Alternatively, I could have set my specifications in various X |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6597 initialization files. For example, I could set the foreground, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6598 background, cursor, and pointer (i.e., mouse) colors in my |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6599 `~/.Xresources' file like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6600 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6601 Emacs*foreground: white |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6602 Emacs*background: darkblue |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6603 Emacs*cursorColor: white |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6604 Emacs*pointerColor: white |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6605 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6606 In any event, since it is not part of Emacs, I set the root color of my |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6607 X window in my `~/.xinitrc' file, like this(1): |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6608 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6609 xsetroot -solid Navy -fg white & |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6610 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6611 ---------- Footnotes ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6612 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6613 (1) I also run more modern window managers, such as Enlightenment, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6614 Gnome, or KDE; in those cases, I often specify an image rather than a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6615 plain color. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6616 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6617 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6618 File: eintr, Node: Miscellaneous, Next: Mode Line, Prev: X11 Colors, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6619 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6620 16.13 Miscellaneous Settings for a `.emacs' File |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6621 ================================================ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6622 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6623 Here are a few miscellaneous settings: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6624 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6625 - Set the shape and color of the mouse cursor: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6626 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6627 ; Cursor shapes are defined in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6628 ; `/usr/include/X11/cursorfont.h'; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6629 ; for example, the `target' cursor is number 128; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6630 ; the `top_left_arrow' cursor is number 132. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6631 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6632 (let ((mpointer (x-get-resource "*mpointer" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6633 "*emacs*mpointer"))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6634 ;; If you have not set your mouse pointer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6635 ;; then set it, otherwise leave as is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6636 (if (eq mpointer nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6637 (setq mpointer "132")) ; top_left_arrow |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6638 (setq x-pointer-shape (string-to-int mpointer)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6639 (set-mouse-color "white")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6640 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6641 - Or you can set the values of a variety of features in an alist, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6642 like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6643 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6644 (setq-default |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6645 default-frame-alist |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6646 '((cursor-color . "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6647 (mouse-color . "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6648 (foreground-color . "white") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6649 (background-color . "DodgerBlue4") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6650 ;; (cursor-type . bar) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6651 (cursor-type . box) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6652 (tool-bar-lines . 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6653 (menu-bar-lines . 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6654 (width . 80) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6655 (height . 58) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6656 (font . |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6657 "-Misc-Fixed-Medium-R-Normal--20-200-75-75-C-100-ISO8859-1") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6658 )) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6659 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6660 - Convert `<CTRL>-h' into <DEL> and <DEL> into `<CTRL>-h'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6661 (Some older keyboards needed this, although I have not seen the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6662 problem recently.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6663 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6664 ;; Translate `C-h' to <DEL>. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6665 ; (keyboard-translate ?\C-h ?\C-?) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6666 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6667 ;; Translate <DEL> to `C-h'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6668 (keyboard-translate ?\C-? ?\C-h) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6669 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6670 - Turn off a blinking cursor! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6671 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6672 (if (fboundp 'blink-cursor-mode) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6673 (blink-cursor-mode -1)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6674 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6675 or start GNU Emacs with the command `emacs -nbc'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6676 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6677 - Ignore case when using `grep' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6678 `-n' Prefix each line of output with line number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6679 `-i' Ignore case distinctions |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6680 `-e' Protect patterns beginning with a hyphen character, `-' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6681 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6682 (setq grep-command "grep -n -i -e ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6683 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6684 - Find an existing buffer, even if it has a different name |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6685 This avoids problems with symbolic links. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6686 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6687 (setq find-file-existing-other-name t) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6688 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6689 - Set your language environment and default input method |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6690 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6691 (set-language-environment "latin-1") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6692 ;; Remember you can enable or disable multilingual text input |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6693 ;; with the `toggle-input-method'' (C-\) command |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6694 (setq default-input-method "latin-1-prefix") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6695 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6696 If you want to write with Chinese `GB' characters, set this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6697 instead: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6698 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6699 (set-language-environment "Chinese-GB") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6700 (setq default-input-method "chinese-tonepy") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6701 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6702 Fixing Unpleasant Key Bindings |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6703 .............................. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6704 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6705 Some systems bind keys unpleasantly. Sometimes, for example, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6706 <CTRL> key appears in an awkward spot rather than at the far left of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6707 the home row. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6708 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6709 Usually, when people fix these sorts of keybindings, they do not change |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6710 their `~/.emacs' file. Instead, they bind the proper keys on their |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6711 consoles with the `loadkeys' or `install-keymap' commands in their boot |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6712 script and then include `xmodmap' commands in their `.xinitrc' or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6713 `.Xsession' file for X Windows. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6714 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6715 For a boot script: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6716 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6717 loadkeys /usr/share/keymaps/i386/qwerty/emacs2.kmap.gz |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6718 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6719 or |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6720 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6721 install-keymap emacs2 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6722 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6723 For a `.xinitrc' or `.Xsession' file when the <Caps Lock> key is at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6724 far left of the home row: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6725 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6726 # Bind the key labeled `Caps Lock' to `Control' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6727 # (Such a broken user interface suggests that keyboard manufacturers |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6728 # think that computers are typewriters from 1885.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6729 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6730 xmodmap -e "clear Lock" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6731 xmodmap -e "add Control = Caps_Lock" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6732 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6733 In a `.xinitrc' or `.Xsession' file, to convert an <ALT> key to a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6734 <META> key: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6735 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6736 # Some ill designed keyboards have a key labeled ALT and no Meta |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6737 xmodmap -e "keysym Alt_L = Meta_L Alt_L" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6738 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6739 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6740 File: eintr, Node: Mode Line, Prev: Miscellaneous, Up: Emacs Initialization |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6741 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6742 16.14 A Modified Mode Line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6743 ========================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6744 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6745 Finally, a feature I really like: a modified mode line. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6746 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6747 When I work over a network, I forget which machine I am using. Also, I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6748 tend to I lose track of where I am, and which line point is on. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6749 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6750 So I reset my mode line to look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6751 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6752 -:-- foo.texi rattlesnake:/home/bob/ Line 1 (Texinfo Fill) Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6753 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6754 I am visiting a file called `foo.texi', on my machine `rattlesnake' in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6755 my `/home/bob' buffer. I am on line 1, in Texinfo mode, and am at the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6756 top of the buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6757 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6758 My `.emacs' file has a section that looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6759 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6760 ;; Set a Mode Line that tells me which machine, which directory, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6761 ;; and which line I am on, plus the other customary information. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6762 (setq default-mode-line-format |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6763 (quote |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6764 (#("-" 0 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6765 (help-echo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6766 "mouse-1: select window, mouse-2: delete others ...")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6767 mode-line-mule-info |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6768 mode-line-modified |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6769 mode-line-frame-identification |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6770 " " |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6771 mode-line-buffer-identification |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6772 " " |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6773 (:eval (substring |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6774 (system-name) 0 (string-match "\\..+" (system-name)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6775 ":" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6776 default-directory |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6777 #(" " 0 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6778 (help-echo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6779 "mouse-1: select window, mouse-2: delete others ...")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6780 (line-number-mode " Line %l ") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6781 global-mode-string |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6782 #(" %[(" 0 6 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6783 (help-echo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6784 "mouse-1: select window, mouse-2: delete others ...")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6785 (:eval (mode-line-mode-name)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6786 mode-line-process |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6787 minor-mode-alist |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6788 #("%n" 0 2 (help-echo "mouse-2: widen" local-map (keymap ...))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6789 ")%] " |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6790 (-3 . "%P") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6791 ;; "-%-" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6792 ))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6793 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6794 Here, I redefine the default mode line. Most of the parts are from the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6795 original; but I make a few changes. I set the _default_ mode line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6796 format so as to permit various modes, such as Info, to override it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6797 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6798 Many elements in the list are self-explanatory: `mode-line-modified' is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6799 a variable that tells whether the buffer has been modified, `mode-name' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6800 tells the name of the mode, and so on. However, the format looks |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6801 complicated because of two features we have not discussed. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6802 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6803 The first string in the mode line is a dash or hyphen, `-'. In the old |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6804 days, it would have been specified simply as `"-"'. But nowadays, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6805 Emacs can add properties to a string, such as highlighting or, as in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6806 this case, a help feature. If you place your mouse cursor over the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6807 hyphen, some help information appears (By default, you must wait |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6808 seven-tenths of a second before the information appears. You can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6809 change that timing by changing the value of `tooltip-delay'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6810 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6811 The new string format has a special syntax: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6812 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6813 #("-" 0 1 (help-echo "mouse-1: select window, ...")) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6814 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6815 The `#(' begins a list. The first element of the list is the string |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6816 itself, just one `-'. The second and third elements specify the range |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6817 over which the fourth element applies. A range starts _after_ a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6818 character, so a zero means the range starts just before the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6819 character; a 1 means that the range ends just after the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6820 character. The third element is the property for the range. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6821 consists of a property list, a property name, in this case, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6822 `help-echo', followed by a value, in this case, a string. The second, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6823 third, and fourth elements of this new string format can be repeated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6824 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6825 *Note Text Properties: (elisp)Text Properties, and see *Note Mode Line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6826 Format: (elisp)Mode Line Format, for more information. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6827 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6828 `mode-line-buffer-identification' displays the current buffer name. It |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6829 is a list beginning `(#("%12b" 0 4 ...'. The `#(' begins the list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6830 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6831 The `"%12b"' displays the current buffer name, using the `buffer-name' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6832 function with which we are familiar; the `12' specifies the maximum |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6833 number of characters that will be displayed. When a name has fewer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6834 characters, whitespace is added to fill out to this number. (Buffer |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6835 names can and often should be longer than 12 characters; this length |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6836 works well in a typical 80 column wide window.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6837 |
73595
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6838 `:eval' says to evaluate the following form and use the result as a |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6839 string to display. In this case, the expression displays the first |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6840 component of the full system name. The end of the first component is a |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6841 `.' (`period'), so I use the `string-match' function to tell me the |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6842 length of the first component. The substring from the zeroth character |
f93366072a0b
* eintr-2: updated `Introduction to Programming in Emacs Lisp'
Robert J. Chassell <bob@rattlesnake.com>
parents:
73591
diff
changeset
|
6843 to that length is the name of the machine. |
73591
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6844 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6845 This is the expression: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6846 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6847 (:eval (substring |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6848 (system-name) 0 (string-match "\\..+" (system-name)))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6849 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6850 `%[' and `%]' cause a pair of square brackets to appear for each |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6851 recursive editing level. `%n' says `Narrow' when narrowing is in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6852 effect. `%P' tells you the percentage of the buffer that is above the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6853 bottom of the window, or `Top', `Bottom', or `All'. (A lower case `p' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6854 tell you the percentage above the _top_ of the window.) `%-' inserts |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6855 enough dashes to fill out the line. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6856 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6857 Remember, "You don't have to like Emacs to like it" -- your own Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6858 can have different colors, different commands, and different keys than |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6859 a default Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6860 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6861 On the other hand, if you want to bring up a plain `out of the box' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6862 Emacs, with no customization, type: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6863 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6864 emacs -q |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6865 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6866 This will start an Emacs that does _not_ load your `~/.emacs' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6867 initialization file. A plain, default Emacs. Nothing more. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6868 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6869 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6870 File: eintr, Node: Debugging, Next: Conclusion, Prev: Emacs Initialization, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6871 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6872 17 Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6873 ************ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6874 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6875 GNU Emacs has two debuggers, `debug' and `edebug'. The first is built |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6876 into the internals of Emacs and is always with you; the second requires |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6877 that you instrument a function before you can use it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6878 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6879 Both debuggers are described extensively in *Note Debugging Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6880 Programs: (elisp)Debugging. In this chapter, I will walk through a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6881 short example of each. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6882 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6883 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6884 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6885 * debug:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6886 * debug-on-entry:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6887 * debug-on-quit:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6888 * edebug:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6889 * Debugging Exercises:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6890 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6891 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6892 File: eintr, Node: debug, Next: debug-on-entry, Prev: Debugging, Up: Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6893 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6894 17.1 `debug' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6895 ============ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6896 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6897 Suppose you have written a function definition that is intended to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6898 return the sum of the numbers 1 through a given number. (This is the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6899 `triangle' function discussed earlier. *Note Example with Decrementing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6900 Counter: Decrementing Example, for a discussion.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6901 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6902 However, your function definition has a bug. You have mistyped `1=' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6903 for `1-'. Here is the broken definition: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6904 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6905 (defun triangle-bugged (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6906 "Return sum of numbers 1 through NUMBER inclusive." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6907 (let ((total 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6908 (while (> number 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6909 (setq total (+ total number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6910 (setq number (1= number))) ; Error here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6911 total)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6912 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6913 If you are reading this in Info, you can evaluate this definition in |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6914 the normal fashion. You will see `triangle-bugged' appear in the echo |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6915 area. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6916 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6917 Now evaluate the `triangle-bugged' function with an argument of 4: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6918 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6919 (triangle-bugged 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6920 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6921 In GNU Emacs version 21, you will create and enter a `*Backtrace*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6922 buffer that says: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6923 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6924 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6925 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6926 Debugger entered--Lisp error: (void-function 1=) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6927 (1= number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6928 (setq number (1= number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6929 (while (> number 0) (setq total (+ total number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6930 (setq number (1= number))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6931 (let ((total 0)) (while (> number 0) (setq total ...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6932 (setq number ...)) total) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6933 triangle-bugged(4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6934 eval((triangle-bugged 4)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6935 eval-last-sexp-1(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6936 eval-last-sexp(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6937 call-interactively(eval-last-sexp) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6938 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6939 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6940 (I have reformatted this example slightly; the debugger does not fold |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6941 long lines. As usual, you can quit the debugger by typing `q' in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6942 `*Backtrace*' buffer.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6943 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6944 In practice, for a bug as simple as this, the `Lisp error' line will |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6945 tell you what you need to know to correct the definition. The function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6946 `1=' is `void'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6947 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6948 However, suppose you are not quite certain what is going on? You can |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6949 read the complete backtrace. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6950 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6951 In this case, you need to run GNU Emacs 22, which automatically starts |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6952 the debugger that puts you in the `*Backtrace*' buffer; or else, you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6953 need to start the debugger manually as described below. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6954 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6955 Read the `*Backtrace*' buffer from the bottom up; it tells you what |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6956 Emacs did that led to the error. Emacs made an interactive call to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6957 `C-x C-e' (`eval-last-sexp'), which led to the evaluation of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6958 `triangle-bugged' expression. Each line above tells you what the Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6959 interpreter evaluated next. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6960 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6961 The third line from the top of the buffer is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6962 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6963 (setq number (1= number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6964 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6965 Emacs tried to evaluate this expression; in order to do so, it tried to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6966 evaluate the inner expression shown on the second line from the top: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6967 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6968 (1= number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6969 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6970 This is where the error occurred; as the top line says: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6971 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6972 Debugger entered--Lisp error: (void-function 1=) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6973 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6974 You can correct the mistake, re-evaluate the function definition, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6975 then run your test again. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6976 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6977 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6978 File: eintr, Node: debug-on-entry, Next: debug-on-quit, Prev: debug, Up: Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6979 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6980 17.2 `debug-on-entry' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6981 ===================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6982 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6983 GNU Emacs 22 starts the debugger automatically when your function has |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6984 an error. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6985 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6986 Incidentally, you can start the debugger manually for all versions of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6987 Emacs; the advantage is that the debugger runs even if you do not have |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6988 a bug in your code. Sometimes your code will be free of bugs! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6989 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6990 You can enter the debugger when you call the function by calling |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6991 `debug-on-entry'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6992 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6993 Type: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6994 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6995 M-x debug-on-entry RET triangle-bugged RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6996 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6997 Now, evaluate the following: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6998 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
6999 (triangle-bugged 5) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7000 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7001 All versions of Emacs will create a `*Backtrace*' buffer and tell you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7002 that it is beginning to evaluate the `triangle-bugged' function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7003 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7004 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7005 Debugger entered--entering a function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7006 * triangle-bugged(5) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7007 eval((triangle-bugged 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7008 eval-last-sexp-1(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7009 eval-last-sexp(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7010 call-interactively(eval-last-sexp) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7011 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7012 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7013 In the `*Backtrace*' buffer, type `d'. Emacs will evaluate the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7014 expression in `triangle-bugged'; the buffer will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7015 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7016 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7017 Debugger entered--beginning evaluation of function call form: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7018 * (let ((total 0)) (while (> number 0) (setq total ...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7019 (setq number ...)) total) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7020 * triangle-bugged(5) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7021 eval((triangle-bugged 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7022 eval-last-sexp-1(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7023 eval-last-sexp(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7024 call-interactively(eval-last-sexp) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7025 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7026 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7027 Now, type `d' again, eight times, slowly. Each time you type `d', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7028 Emacs will evaluate another expression in the function definition. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7029 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7030 Eventually, the buffer will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7031 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7032 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7033 Debugger entered--beginning evaluation of function call form: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7034 * (setq number (1= number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7035 * (while (> number 0) (setq total (+ total number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7036 (setq number (1= number))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7037 * (let ((total 0)) (while (> number 0) (setq total ...) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7038 (setq number ...)) total) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7039 * triangle-bugged(5) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7040 eval((triangle-bugged 5)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7041 eval-last-sexp-1(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7042 eval-last-sexp(nil) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7043 call-interactively(eval-last-sexp) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7044 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7045 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7046 Finally, after you type `d' two more times, Emacs will reach the error, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7047 and the top two lines of the `*Backtrace*' buffer will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7048 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7049 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7050 Debugger entered--Lisp error: (void-function 1=) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7051 * (1= number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7052 ... |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7053 ---------- Buffer: *Backtrace* ---------- |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7054 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7055 By typing `d', you were able to step through the function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7056 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7057 You can quit a `*Backtrace*' buffer by typing `q' in it; this quits the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7058 trace, but does not cancel `debug-on-entry'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7059 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7060 To cancel the effect of `debug-on-entry', call `cancel-debug-on-entry' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7061 and the name of the function, like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7062 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7063 M-x cancel-debug-on-entry RET triangle-bugged RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7064 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7065 (If you are reading this in Info, cancel `debug-on-entry' now.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7066 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7067 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7068 File: eintr, Node: debug-on-quit, Next: edebug, Prev: debug-on-entry, Up: Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7069 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7070 17.3 `debug-on-quit' and `(debug)' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7071 ================================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7072 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7073 In addition to setting `debug-on-error' or calling `debug-on-entry', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7074 there are two other ways to start `debug'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7075 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7076 You can start `debug' whenever you type `C-g' (`keyboard-quit') by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7077 setting the variable `debug-on-quit' to `t'. This is useful for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7078 debugging infinite loops. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7079 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7080 Or, you can insert a line that says `(debug)' into your code where you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7081 want the debugger to start, like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7082 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7083 (defun triangle-bugged (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7084 "Return sum of numbers 1 through NUMBER inclusive." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7085 (let ((total 0)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7086 (while (> number 0) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7087 (setq total (+ total number)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7088 (debug) ; Start debugger. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7089 (setq number (1= number))) ; Error here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7090 total)) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7091 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7092 The `debug' function is described in detail in *Note The Lisp Debugger: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7093 (elisp)Debugger. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7094 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7095 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7096 File: eintr, Node: edebug, Next: Debugging Exercises, Prev: debug-on-quit, Up: Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7097 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7098 17.4 The `edebug' Source Level Debugger |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7099 ======================================= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7100 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7101 Edebug is a source level debugger. Edebug normally displays the source |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7102 of the code you are debugging, with an arrow at the left that shows |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7103 which line you are currently executing. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7104 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7105 You can walk through the execution of a function, line by line, or run |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7106 quickly until reaching a "breakpoint" where execution stops. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7107 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7108 Edebug is described in *Note Edebug: (elisp)edebug. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7109 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7110 Here is a bugged function definition for `triangle-recursively'. *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7111 Recursion in place of a counter: Recursive triangle function, for a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7112 review of it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7113 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7114 (defun triangle-recursively-bugged (number) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7115 "Return sum of numbers 1 through NUMBER inclusive. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7116 Uses recursion." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7117 (if (= number 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7118 1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7119 (+ number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7120 (triangle-recursively-bugged |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7121 (1= number))))) ; Error here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7122 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7123 Normally, you would install this definition by positioning your cursor |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7124 after the function's closing parenthesis and typing `C-x C-e' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7125 (`eval-last-sexp') or else by positioning your cursor within the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7126 definition and typing `C-M-x' (`eval-defun'). (By default, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7127 `eval-defun' command works only in Emacs Lisp mode or in Lisp |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7128 Interactive mode.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7129 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7130 However, to prepare this function definition for Edebug, you must first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7131 "instrument" the code using a different command. You can do this by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7132 positioning your cursor within the definition and typing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7133 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7134 M-x edebug-defun RET |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7135 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7136 This will cause Emacs to load Edebug automatically if it is not already |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7137 loaded, and properly instrument the function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7138 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7139 After instrumenting the function, place your cursor after the following |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7140 expression and type `C-x C-e' (`eval-last-sexp'): |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7141 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7142 (triangle-recursively-bugged 3) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7143 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7144 You will be jumped back to the source for `triangle-recursively-bugged' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7145 and the cursor positioned at the beginning of the `if' line of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7146 function. Also, you will see an arrowhead at the left hand side of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7147 that line. The arrowhead marks the line where the function is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7148 executing. (In the following examples, we show the arrowhead with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7149 `=>'; in a windowing system, you may see the arrowhead as a solid |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7150 triangle in the window `fringe'.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7151 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7152 =>-!-(if (= number 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7153 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7154 In the example, the location of point is displayed as `-!-' (in a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7155 printed book, it is displayed with a five pointed star). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7156 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7157 If you now press <SPC>, point will move to the next expression to be |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7158 executed; the line will look like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7159 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7160 =>(if -!-(= number 1) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7161 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7162 As you continue to press <SPC>, point will move from expression to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7163 expression. At the same time, whenever an expression returns a value, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7164 that value will be displayed in the echo area. For example, after you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7165 move point past `number', you will see the following: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7166 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7167 Result: 3 (#o3, #x3, ?\C-c) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7168 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7169 This means the value of `number' is 3, which is octal three, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7170 hexadecimal three, and ASCII `control-c' (the third letter of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7171 alphabet, in case you need to know this information). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7172 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7173 You can continue moving through the code until you reach the line with |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7174 the error. Before evaluation, that line looks like this: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7175 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7176 => -!-(1= number))))) ; Error here. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7177 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7178 When you press <SPC> once again, you will produce an error message that |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7179 says: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7180 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7181 Symbol's function definition is void: 1= |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7182 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7183 This is the bug. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7184 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7185 Press `q' to quit Edebug. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7186 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7187 To remove instrumentation from a function definition, simply |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7188 re-evaluate it with a command that does not instrument it. For |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7189 example, you could place your cursor after the definition's closing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7190 parenthesis and type `C-x C-e'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7191 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7192 Edebug does a great deal more than walk with you through a function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7193 You can set it so it races through on its own, stopping only at an |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7194 error or at specified stopping points; you can cause it to display the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7195 changing values of various expressions; you can find out how many times |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7196 a function is called, and more. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7197 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7198 Edebug is described in *Note Edebug: (elisp)edebug. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7199 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7200 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7201 File: eintr, Node: Debugging Exercises, Prev: edebug, Up: Debugging |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7202 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7203 17.5 Debugging Exercises |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7204 ======================== |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7205 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7206 * Install the `count-words-region' function and then cause it to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7207 enter the built-in debugger when you call it. Run the command on a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7208 region containing two words. You will need to press `d' a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7209 remarkable number of times. On your system, is a `hook' called |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7210 after the command finishes? (For information on hooks, see *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7211 Command Loop Overview: (elisp)Command Overview.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7212 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7213 * Copy `count-words-region' into the `*scratch*' buffer, instrument |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7214 the function for Edebug, and walk through its execution. The |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7215 function does not need to have a bug, although you can introduce |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7216 one if you wish. If the function lacks a bug, the walk-through |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7217 completes without problems. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7218 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7219 * While running Edebug, type `?' to see a list of all the Edebug |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7220 commands. (The `global-edebug-prefix' is usually `C-x X', i.e. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7221 `<CTRL>-x' followed by an upper case `X'; use this prefix for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7222 commands made outside of the Edebug debugging buffer.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7223 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7224 * In the Edebug debugging buffer, use the `p' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7225 (`edebug-bounce-point') command to see where in the region the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7226 `count-words-region' is working. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7227 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7228 * Move point to some spot further down the function and then type the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7229 `h' (`edebug-goto-here') command to jump to that location. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7230 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7231 * Use the `t' (`edebug-trace-mode') command to cause Edebug to walk |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7232 through the function on its own; use an upper case `T' for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7233 `edebug-Trace-fast-mode'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7234 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7235 * Set a breakpoint, then run Edebug in Trace mode until it reaches |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7236 the stopping point. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7237 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7238 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7239 File: eintr, Node: Conclusion, Next: the-the, Prev: Debugging, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7240 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7241 18 Conclusion |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7242 ************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7243 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7244 We have now reached the end of this Introduction. You have now learned |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7245 enough about programming in Emacs Lisp to set values, to write simple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7246 `.emacs' files for yourself and your friends, and write simple |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7247 customizations and extensions to Emacs. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7248 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7249 This is a place to stop. Or, if you wish, you can now go onward, and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7250 teach yourself. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7251 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7252 You have learned some of the basic nuts and bolts of programming. But |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7253 only some. There are a great many more brackets and hinges that are |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7254 easy to use that we have not touched. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7255 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7256 A path you can follow right now lies among the sources to GNU Emacs and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7257 in *Note The GNU Emacs Lisp Reference Manual: (elisp)Top. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7258 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7259 The Emacs Lisp sources are an adventure. When you read the sources and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7260 come across a function or expression that is unfamiliar, you need to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7261 figure out or find out what it does. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7262 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7263 Go to the Reference Manual. It is a thorough, complete, and fairly |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7264 easy-to-read description of Emacs Lisp. It is written not only for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7265 experts, but for people who know what you know. (The `Reference |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7266 Manual' comes with the standard GNU Emacs distribution. Like this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7267 introduction, it comes as a Texinfo source file, so you can read it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7268 on-line and as a typeset, printed book.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7269 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7270 Go to the other on-line help that is part of GNU Emacs: the on-line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7271 documentation for all functions and variables, and `find-tags', the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7272 program that takes you to sources. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7273 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7274 Here is an example of how I explore the sources. Because of its name, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7275 `simple.el' is the file I looked at first, a long time ago. As it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7276 happens some of the functions in `simple.el' are complicated, or at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7277 least look complicated at first sight. The `open-line' function, for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7278 example, looks complicated. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7279 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7280 You may want to walk through this function slowly, as we did with the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7281 `forward-sentence' function. (*Note The `forward-sentence' function: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7282 forward-sentence.) Or you may want to skip that function and look at |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7283 another, such as `split-line'. You don't need to read all the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7284 functions. According to `count-words-in-defun', the `split-line' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7285 function contains 102 words and symbols. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7286 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7287 Even though it is short, `split-line' contains expressions we have not |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7288 studied: `skip-chars-forward', `indent-to', `current-column' and |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7289 `insert-and-inherit'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7290 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7291 Consider the `skip-chars-forward' function. (It is part of the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7292 function definition for `back-to-indentation', which is shown in *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7293 Review: Review.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7294 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7295 In GNU Emacs, you can find out more about `skip-chars-forward' by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7296 typing `C-h f' (`describe-function') and the name of the function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7297 This gives you the function documentation. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7298 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7299 You may be able to guess what is done by a well named function such as |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7300 `indent-to'; or you can look it up, too. Incidentally, the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7301 `describe-function' function itself is in `help.el'; it is one of those |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7302 long, but decipherable functions. You can look up `describe-function' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7303 using the `C-h f' command! |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7304 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7305 In this instance, since the code is Lisp, the `*Help*' buffer contains |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7306 the name of the library containing the function's source. You can put |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7307 point over the name of the library and press the RET key, which in this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7308 situation is bound to `help-follow', and be taken directly to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7309 source, in the same way as `M-.' (`find-tag'). |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7310 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7311 The definition for `describe-function' illustrates how to customize the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7312 `interactive' expression without using the standard character codes; |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7313 and it shows how to create a temporary buffer. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7314 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7315 (The `indent-to' function is written in C rather than Emacs Lisp; it is |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7316 a `built-in' function. `help-follow' takes you to its source as does |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7317 `find-tag', when properly set up.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7318 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7319 You can look at a function's source using `find-tag', which is bound to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7320 `M-.' Finally, you can find out what the Reference Manual has to say |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7321 by visiting the manual in Info, and typing `i' (`Info-index') and the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7322 name of the function, or by looking up the function in the index to a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7323 printed copy of the manual. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7324 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7325 Similarly, you can find out what is meant by `insert-and-inherit'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7326 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7327 Other interesting source files include `paragraphs.el', `loaddefs.el', |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7328 and `loadup.el'. The `paragraphs.el' file includes short, easily |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7329 understood functions as well as longer ones. The `loaddefs.el' file |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7330 contains the many standard autoloads and many keymaps. I have never |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7331 looked at it all; only at parts. `loadup.el' is the file that loads |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7332 the standard parts of Emacs; it tells you a great deal about how Emacs |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7333 is built. (*Note Building Emacs: (elisp)Building Emacs, for more about |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7334 building.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7335 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7336 As I said, you have learned some nuts and bolts; however, and very |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7337 importantly, we have hardly touched major aspects of programming; I |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7338 have said nothing about how to sort information, except to use the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7339 predefined `sort' function; I have said nothing about how to store |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7340 information, except to use variables and lists; I have said nothing |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7341 about how to write programs that write programs. These are topics for |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7342 another, and different kind of book, a different kind of learning. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7343 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7344 What you have done is learn enough for much practical work with GNU |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7345 Emacs. What you have done is get started. This is the end of a |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7346 beginning. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7347 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7348 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7349 File: eintr, Node: the-the, Next: Kill Ring, Prev: Conclusion, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7350 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7351 Appendix A The `the-the' Function |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7352 ********************************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7353 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7354 Sometimes when you you write text, you duplicate words--as with "you |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7355 you" near the beginning of this sentence. I find that most frequently, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7356 I duplicate "the"; hence, I call the function for detecting duplicated |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7357 words, `the-the'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7358 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7359 As a first step, you could use the following regular expression to |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7360 search for duplicates: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7361 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7362 \\(\\w+[ \t\n]+\\)\\1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7363 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7364 This regexp matches one or more word-constituent characters followed by |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7365 one or more spaces, tabs, or newlines. However, it does not detect |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7366 duplicated words on different lines, since the ending of the first |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7367 word, the end of the line, is different from the ending of the second |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7368 word, a space. (For more information about regular expressions, see |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7369 *Note Regular Expression Searches: Regexp Search, as well as *Note |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7370 Syntax of Regular Expressions: (emacs)Regexps, and *Note Regular |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7371 Expressions: (elisp)Regular Expressions.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7372 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7373 You might try searching just for duplicated word-constituent characters |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7374 but that does not work since the pattern detects doubles such as the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7375 two occurrences of `th' in `with the'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7376 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7377 Another possible regexp searches for word-constituent characters |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7378 followed by non-word-constituent characters, reduplicated. Here, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7379 `\\w+' matches one or more word-constituent characters and `\\W*' |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7380 matches zero or more non-word-constituent characters. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7381 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7382 \\(\\(\\w+\\)\\W*\\)\\1 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7383 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7384 Again, not useful. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7385 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7386 Here is the pattern that I use. It is not perfect, but good enough. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7387 `\\b' matches the empty string, provided it is at the beginning or end |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7388 of a word; `[^@ \n\t]+' matches one or more occurrences of any |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7389 characters that are _not_ an @-sign, space, newline, or tab. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7390 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7391 \\b\\([^@ \n\t]+\\)[ \n\t]+\\1\\b |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7392 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7393 One can write more complicated expressions, but I found that this |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7394 expression is good enough, so I use it. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7395 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7396 Here is the `the-the' function, as I include it in my `.emacs' file, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7397 along with a handy global key binding: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7398 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7399 (defun the-the () |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7400 "Search forward for for a duplicated word." |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7401 (interactive) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7402 (message "Searching for for duplicated words ...") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7403 (push-mark) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7404 ;; This regexp is not perfect |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7405 ;; but is fairly good over all: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7406 (if (re-search-forward |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7407 "\\b\\([^@ \n\t]+\\)[ \n\t]+\\1\\b" nil 'move) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7408 (message "Found duplicated word.") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7409 (message "End of buffer"))) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7410 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7411 ;; Bind `the-the' to C-c \ |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7412 (global-set-key "\C-c\\" 'the-the) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7413 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7414 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7415 Here is test text: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7416 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7417 one two two three four five |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7418 five six seven |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7419 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7420 You can substitute the other regular expressions shown above in the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7421 function definition and try each of them on this list. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7422 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7423 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7424 File: eintr, Node: Kill Ring, Next: Full Graph, Prev: the-the, Up: Top |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7425 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7426 Appendix B Handling the Kill Ring |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7427 ********************************* |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7428 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7429 The kill ring is a list that is transformed into a ring by the workings |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7430 of the `current-kill' function. The `yank' and `yank-pop' commands use |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7431 the `current-kill' function. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7432 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7433 This appendix describes the `current-kill' function as well as both the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7434 `yank' and the `yank-pop' commands, but first, consider the workings of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7435 the kill ring. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7436 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7437 The kill ring has a default maximum length of sixty items; this number |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7438 is too large for an explanation. Instead, set it to four. Please |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7439 evaluate the following: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7440 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7441 (setq old-kill-ring-max kill-ring-max) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7442 (setq kill-ring-max 4) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7443 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7444 Then, please copy each line of the following indented example into the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7445 kill ring. You may kill each line with `C-k' or mark it and copy it |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7446 with `M-w'. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7447 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7448 (In a read-only buffer, such as the `*info*' buffer, the kill command, |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7449 `C-k' (`kill-line'), will not remove the text, merely copy it to the |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7450 kill ring. However, your machine may beep at you. (`kill-line' calls |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7451 `kill-region'.) Alternatively, for silence, you may copy the region of |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7452 each line with the `M-w' (`kill-ring-save') command. You must mark |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7453 each line for this command to succeed, but it does not matter at which |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7454 end you put point or mark.) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7455 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7456 Please invoke the calls in order, so that five elements attempt to fill |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7457 the kill ring: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7458 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7459 first some text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7460 second piece of text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7461 third line |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7462 fourth line of text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7463 fifth bit of text |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7464 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7465 Then find the value of `kill-ring' by evaluating |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7466 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7467 kill-ring |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7468 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7469 It is: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7470 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7471 ("fifth bit of text" "fourth line of text" |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7472 "third line" "second piece of text") |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7473 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7474 The first element, `first some text', was dropped. |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7475 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7476 To return to the old value for the length of the kill ring, evaluate: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7477 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7478 (setq kill-ring-max old-kill-ring-max) |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7479 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7480 * Menu: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7481 |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7482 * current-kill:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7483 * yank:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7484 * yank-pop:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7485 * ring file:: |
b214bd8be620
info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff
changeset
|
7486 |