annotate info/eintr-2 @ 73591:b214bd8be620

info/eintr-2: Updated Info file to Third Edition for `Introduction to Programming in Emacs Lisp'
author Robert J. Chassell <bob@rattlesnake.com>
date Tue, 31 Oct 2006 17:00:32 +0000
parents
children f93366072a0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
13 Edition 3.00, 2006 Oct 31
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
42 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
43
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
44 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
45 ------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
46
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
47 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
48 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
49 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
50 `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
51 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
52 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
53
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
54 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
55 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
56 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
57 example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
58
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
59 (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
60 "*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
61 ... ")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
62
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
63 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
64 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
65 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
66 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
67 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
68 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
69 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
70
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
71 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
72 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
73 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
74 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
75 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
76 Variables: (emacs)Examining.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
77
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
78 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
79 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
80
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
81 8.6 Review
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
82 ==========
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 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
85
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
86 `car'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
87 `cdr'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
88 `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
89 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
90
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
91 For example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
92
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
93 (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
94 => 1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
95 (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
96 => (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
97
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
98 `cons'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
99 `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
100 second argument.
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 For example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
103
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
104 (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
105 => (1 2 3 4)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
106
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
107 `nthcdr'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
108 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
109 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
110
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
111 For example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
112
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
113 (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
114 => (4 5 6 7)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
115
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
116 `setcar'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
117 `setcdr'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
118 `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
119 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
120
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
121 For example:
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 (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
124
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
125 (setcar triple '37)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
126
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
127 triple
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
128 => (37 2 3)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
129
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
130 (setcdr triple '("foo" "bar"))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
131
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
132 triple
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
133 => (37 "foo" "bar")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
134
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
135 `progn'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
136 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
137 last.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
138
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
139 For example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
140
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
141 (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
142 => 4
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 `save-restriction'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
145 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
146 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
147
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
148 `search-forward'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
149 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
150
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
151 Takes four arguments:
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 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
154
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
155 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
156
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
157 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
158 error message.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
159
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
160 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
161 the search goes backwards.
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 `kill-region'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
164 `delete-and-extract-region'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
165 `copy-region-as-kill'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
166 `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
167 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
168 yanking.
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 `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
171 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
172 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
173
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
174 `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
175 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
176 not an interactive command.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
177
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 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
180
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
181 8.7 Searching Exercises
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
182 =======================
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 * 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
185 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
186 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
187 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
188 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
189 `test-search' instead.)
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 * 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
192 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
193 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
194
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
195 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
196 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
197
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
198 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
199 ***************************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
200
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
212 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
213 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
214
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
215 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
216
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
217 * Lists diagrammed::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
218 * Symbols as Chest::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
219 * List Exercise::
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
222 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
223
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
224 Lists diagrammed
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
225 ================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
226
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
227 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
228 `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
229 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
230 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
231 `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
232 `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
233 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
234
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
235 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
236
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 |___|___|--> |___|___|--> |___|___|--> nil
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 --> rose --> violet --> buttercup
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
242
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
243
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
244
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
255 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
256 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
257 of the expression
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
258
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
259 (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
260
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
261 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
262
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
263 bouquet
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
264 |
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 --> |___|___|--> |___|___|--> |___|___|--> nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
267 | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
268 | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
269 --> rose --> violet --> buttercup
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
270
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
272
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
273 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
274 pair of boxes.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
275
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
276 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
277 like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
278
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
279 bouquet
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 | -------------- --------------- ----------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
282 | | 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
283 -->| 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
284 | | | | | | | cup | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
285 -------------- --------------- ----------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
286
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
287
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
288
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
289 (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
290 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
291 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
292 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
293 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
294 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
295 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
296 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
297 list.)
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 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
300 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
301 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
302 the following expression
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
303
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
304 (setq flowers (cdr bouquet))
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 produces this:
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 bouquet flowers
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 |___|___|----> |___|___|--> |___|___|--> nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
314 | | |
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 --> rose --> violet --> buttercup
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
317
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
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 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
322 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
323 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
324 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
325
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
326 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
327 *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
328 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
329 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
330
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
331 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
332 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
333 expression
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
334
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
335 (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
336
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
337 produces:
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
340 bouquet flowers
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
341 | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
342 | ___ ___ ___ ___ | ___ ___ ___ ___
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 |___|___|----> |___|___|----> |___|___|---->|___|___|--> nil
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 | | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
347 --> lily --> rose --> violet --> buttercup
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
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 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
353 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
354
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
355 (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
356
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
357 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
358
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
359 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
360 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
361 `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
362 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
363
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
364 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
365 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
366 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
367 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
368 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
369
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
370 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
371 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
372
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
373 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
374 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
375
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 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
378
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
379 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
380 =================================
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 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
383 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
384 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
385 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
386 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
387
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
388 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
389 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
390 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
391 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
392
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
393 (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
394 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
395 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
396 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
397
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
398 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
399
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
400
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
401 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
402
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
403 __ o0O0o __
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
404 / \
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
405 ---------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
406 | directions to | [map to]
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
407 | symbol name | bouquet
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
408 | |
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 | directions to |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
411 | symbol definition | [none]
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 +---------------------+
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
414 | directions to | [map to]
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
415 | 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
416 | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
417 +---------------------+
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
418 | directions to |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
419 | 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
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 |/ \|
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
423
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
424
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
425
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
426
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 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
429
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
430 9.2 Exercise
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
431 ============
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 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
434 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
435 `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
436
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 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
439
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
440 10 Yanking Text Back
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
441 ********************
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 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
444 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
445 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
446 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
447 (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
448
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
449 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
450 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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458 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
459
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
460 * Menu:
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 * Kill Ring Overview::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
463 * kill-ring-yank-pointer::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
464 * yank nthcdr Exercises::
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 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
468
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
469 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
470 =======================
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 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
473
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
474 ("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
475
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
476 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
477 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
478 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
479
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
480 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
481 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
482 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
483
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
484 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
485 `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
486 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
487 other functions.
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 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
490 `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
491 `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
492
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
493 (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
494
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
495 (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
496 `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
497 `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
498 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
499 `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
500 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
501
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
502 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
503 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
504 `rotate-yank-pointer' function.
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-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
508
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
509 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
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 `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
513 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
514 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
515
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
516 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
517
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
518 ("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
519
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
520 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
521 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
522
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
523 ("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
524
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
525 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
526 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
527 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
528 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
529 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
530 is a diagram:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
531
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
532 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
533 | |
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 ---> | | | --> | | | | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
536 |___|___|----> |___|___|--> |___|___|--> nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
537 | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
538 | | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
539 | | --> "yet more text"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
540 | |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
541 | --> "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
542 |
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
543 --> "some text"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
544
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
548 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
549 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
550 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
551 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
552 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
553 list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
554
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
555 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
556 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
557 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
558 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
559 `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
560 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
561 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
562
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
563 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
564 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
565
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
566 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
567 =======================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
568
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
569 * 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
570 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
571 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
572 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
573 `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
574 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
575
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
576 * 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
577 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
578
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
579 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
580 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
581
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
582 11 Loops and Recursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
583 **********************
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 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
586 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
587 the other uses "recursion".
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
588
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
589 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
590 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
591 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
592 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
593 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
594 have on humans.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
595
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
596 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
597 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
598 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
599
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
600 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
601
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
602 * while::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
603 * dolist dotimes::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
604 * Recursion::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
605 * Looping exercise::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
606
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
607 ---------- Footnotes ----------
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 (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
610 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
611 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
612 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
613 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
614 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
615 `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
616 times their default value.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
617
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 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
620
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
621 11.1 `while'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
622 ============
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 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
625 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
626 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
627 is different.
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 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
630 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
631 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
632 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
633 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
634 `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
635 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
636 body of the expression.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
637
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
638 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
639
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
640 (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
641 BODY...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
642
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
643 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
644
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
645 * Looping with while::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
646 * Loop Example::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
647 * print-elements-of-list::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
648 * Incrementing Loop::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
649 * Decrementing Loop::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
650
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
651 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
652 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
653
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
654 Looping with `while'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
655 --------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
656
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
657 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
658 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
659 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
660 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
661 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
662 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
663 `exits the loop'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
664
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
665 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
666 `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
667 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
668 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
669 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
670 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
671 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
672 test return false.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
673
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
674 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
675 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
676 `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
677 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
678 `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
679 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
680 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
681 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
682 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
683 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
684
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
685 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
686 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
687
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
688 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
689 --------------------------------
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 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
692 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
693 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
694 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
695
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
696 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
697 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
698 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
699 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
700 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
701 `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
702
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
703 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
704 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
705
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
706 (setq empty-list ())
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
707
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
708 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
709 `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
710 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
711
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
712 empty-list
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 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
715 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
716 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
717
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
718 (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
719
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
720 animals
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
721
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
722 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
723 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
724 this:
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 (while animals
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
729 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
730 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
731 `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
732 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
733
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
734 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
735 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
736 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
737 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
738 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
739 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
740 `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
741 no longer be evaluated.
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, 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
744 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
745
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
746 (setq animals (cdr animals))
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 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
749 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
750 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
751 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
752 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
753
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
754 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
755 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
756 this:
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 (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
759 BODY...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
760 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
761
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
762 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
763 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
764
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 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
767
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
768 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
769 -------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
770
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
771 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
772 list.
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 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
775 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
776 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
777
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
778 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
779 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
780 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
781 versions.
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 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
784 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
785 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
786 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
787 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
788 `C-y' (`yank').
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
789
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
790 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
791 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
792 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
793 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
794 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
795 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
796 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
797 `^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
798 for a `newline'.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
799
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
800 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
801 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
802 results.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
803
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
804 (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
805
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
806 (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
807 "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
808 (while list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
809 (print (car list))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
810 (setq list (cdr list))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
811
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
812 (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
813
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
814 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
815
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
816 gazelle
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 giraffe
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
819
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
820 lion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
821
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
822 tiger
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
823 nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
824
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
825 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
826 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
827 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
828 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
829 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
830
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
831 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
832 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
833
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
834 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
835 ------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
836
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
837 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
838 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
839 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
840 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
841 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
842 itself.
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 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
845 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
846 `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
847 `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
848 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
849 `(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
850 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
851 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
852
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
853 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
854 looks like 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 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
857 (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
858 BODY...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
859 (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
860
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
861 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
862 set to 1.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
863
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
864 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
865
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
866 * Incrementing Example::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
867 * Inc Example parts::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
868 * Inc Example altogether::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
869
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 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
872
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
873 Example with incrementing counter
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
874 .................................
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 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
877 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
878 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
879
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
880
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
881 *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
882 * *
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 * * * *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
885
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
886
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
887 (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
888 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
889
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
890 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
891 triangle with 7 rows?
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 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
894 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
895 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
896 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
897 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
898 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
899 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
900
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
901 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
902 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
903 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
904 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
905 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
906 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
907
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
908 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
909 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
910 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
911 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
912 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
913
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
914 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
915 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
916 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
917 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
918 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
919 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
920 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
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 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
924
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
925 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
926 ....................................
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
927
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
928 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
929 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
930 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
931 function.
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 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
934 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
935 called `number-of-rows'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
936
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
937 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
938 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
939 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
940 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
941
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
942 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
943 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
944 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
945 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
946 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
947 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
948 left to add.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
949
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
950 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
951 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
952 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
953 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
954 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
955
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
956 (let ((total 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
957 (row-number 1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
958 BODY...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
959
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
960 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
961 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
962 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
963 `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
964 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
965 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
966 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
967 number of rows.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
968
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
969 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
970 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
971 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
972 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
973
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
974 (<= 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
975
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
976 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
977 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
978 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
979 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
980 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
981 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
982 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
983
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
984 (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
985
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
986 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
987 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
988
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
989 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
990 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
991 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
992 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
993 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
994 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
995 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
996 `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
997 loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
998
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
999 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
1000 `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
1001
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1002 (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
1003
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1004 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1005 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
1006
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1007 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
1008 ........................................
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1009
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1010 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
1011 put them together.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1012
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1013 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
1014
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1015 (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
1016 (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
1017 (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
1018
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1019 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
1020 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
1021 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
1022
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1023 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
1024 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
1025 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
1026 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
1027 which is always `nil'.
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 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
1030 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
1031 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
1032 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
1033 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
1034
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1035 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
1036
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1037 (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
1038 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1039 (let (VARLIST)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1040 (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
1041 BODY-OF-WHILE... )
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1042 ... )) ; Need final expression here.
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 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
1045 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
1046 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
1047 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
1048 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
1049 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
1050 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
1051 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
1052 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
1053
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1054 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
1055 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
1056 `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
1057 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
1058
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1059 (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
1060
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1061 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
1062 like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1063
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1064 (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
1065 ; incrementing counter.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1066 "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
1067 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
1068 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
1069 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
1070 (let ((total 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1071 (row-number 1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1072 (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
1073 (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
1074 (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
1075 total))
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 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
1078 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
1079
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1080 (triangle 4)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1081
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1082 (triangle 7)
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 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
1085 numbers is 28.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1086
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1087 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1088 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
1089
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1090 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
1091 ---------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1092
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1093 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
1094 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
1095 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
1096 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
1097 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
1098 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
1099
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1100 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
1101 `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
1102 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
1103 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
1104 `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
1105 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
1106
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1107 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
1108
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1109 (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
1110 BODY...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1111 (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
1112
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1113 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1114
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1115 * Decrementing Example::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1116 * Dec Example parts::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1117 * Dec Example altogether::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1118
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 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
1121
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1122 Example with decrementing counter
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1125 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
1126 `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
1127
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1128 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
1129 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
1130 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
1131 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
1132 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
1133
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1134 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
1135 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
1136 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
1137 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
1138 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
1139 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
1140 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
1141 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
1142
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1143 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
1144 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
1145 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
1146 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
1147 the row.
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1150 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
1151
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1152 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
1153 ....................................
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 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
1156 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
1157 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
1158 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
1159 respectively.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1160
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1161 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
1162 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
1163 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
1164 `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
1165 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
1166
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1167 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
1168 this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1169
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1170 (let ((total 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1171 (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
1172 BODY...)
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 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
1175 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
1176 evaluating the following expression:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1177
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1178 (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
1179
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1180 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
1181 `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
1182 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
1183
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1184 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
1185 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
1186 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
1187 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
1188
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1189 (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
1190 (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
1191
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1192 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
1193 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
1194 `while' loop is simply:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1195
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1196 (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
1197
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1198 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1199 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
1200
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1201 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
1202 ........................................
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1203
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1204 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
1205 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
1206 variables is unneeded!
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1207
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1208 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
1209
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1210 ;;; First subtractive version.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1211 (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
1212 "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
1213 (let ((total 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1214 (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
1215 (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
1216 (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
1217 (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
1218 (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
1219 total))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1220
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1221 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
1222
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1223 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
1224
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1225 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
1226 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
1227 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
1228 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
1229 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
1230 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
1231 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
1232
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1233 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
1234
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1235 (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
1236 "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
1237 (let ((total 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1238 (while (> number 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1239 (setq total (+ total number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1240 (setq number (1- number)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1241 total))
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 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
1244
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1245 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
1246 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
1247
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1248 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
1249 after being repeatedly evaluated.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1250
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1251 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
1252 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
1253 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
1254
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1255 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1256 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
1257
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1258 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
1259 ===========================================
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 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
1262 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
1263 `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
1264
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1265 `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
1266 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
1267 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
1268 first of its arguments.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1269
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1270 `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
1271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1272 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1273
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1274 * dolist::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1275 * dotimes::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1276
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1277 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1278 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
1279
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1280 The `dolist' Macro
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1281 ..................
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 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
1284 "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
1285
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1286 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
1287
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1288 (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
1289
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1290 (reverse animals)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1291
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1292 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
1293
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1294 (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
1295
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1296 (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
1297 "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
1298 (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
1299 (while list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1300 (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
1301 (setq list (cdr list)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1302 value))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1303
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1304 (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
1305
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1306 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
1307
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1308 (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
1309
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1310 (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
1311 "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
1312 (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
1313 (dolist (element list value)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1314 (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
1315
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1316 (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
1317
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1318 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
1319 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
1320
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1321 (tiger lion giraffe gazelle)
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 in the echo area.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1324
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1325 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
1326 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
1327 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
1328 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
1329 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
1330 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
1331 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
1332 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
1333
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1334 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
1335 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
1336 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
1337 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
1338
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1339 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
1340 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
1341 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
1342
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1343 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
1344 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
1345 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
1346 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
1347
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1348 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
1349 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
1350 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
1351 expression is the body.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1352
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1353 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
1354 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
1355 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
1356
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 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
1359
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1360 The `dotimes' Macro
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1361 ...................
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 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
1364 specific number of times.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1365
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1366 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
1367 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
1368 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
1369 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
1370
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1371 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
1372 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
1373 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
1374 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
1375 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
1376
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1377 (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
1378 (dotimes (number 3 value)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1379 (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
1380
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1381 => (2 1 0)
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 `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
1384 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
1385 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
1386
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1387 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
1388 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
1389
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1390 (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
1391 "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
1392 (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
1393 (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
1394 (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
1395
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1396 (triangle-using-dotimes 4)
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1399 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
1400
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1401 11.3 Recursion
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1404 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
1405 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
1406 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
1407 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
1408 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
1409 different `instance'.
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 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
1412 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
1413 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
1414
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1415 * Menu:
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 * Building Robots::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1418 * Recursive Definition Parts::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1419 * Recursion with list::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1420 * Recursive triangle function::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1421 * Recursion with cond::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1422 * Recursive Patterns::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1423 * No Deferment::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1424 * No deferment solution::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1425
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 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
1428
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1429 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
1430 ----------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1431
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1432 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
1433 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
1434 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
1435 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
1436 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
1437
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1438 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
1439 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
1440 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
1441
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1442 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
1443 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
1444 to stop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1445
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1446 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
1447
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1448 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
1449 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
1450 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
1451 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
1452 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
1453 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
1454 number'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1455
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1456 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
1457 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
1458 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
1459 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
1460
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1461 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
1462 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
1463
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1464 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1465 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
1466
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1467 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
1468 ------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1469
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1470 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
1471 has three parts:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1472
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1473 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
1474 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
1475
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1476 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
1477 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
1478 told what to do.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1479
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1480 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
1481 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
1482 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
1483 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
1484 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
1485 "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
1486 repetitions.
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 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
1489 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
1490 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
1491 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
1492 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
1493
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1494 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
1495 pattern looks like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1496
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1497 (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
1498 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1499 (if DO-AGAIN-TEST
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1500 BODY...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1501 (NAME-OF-RECURSIVE-FUNCTION
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1502 NEXT-STEP-EXPRESSION)))
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 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
1505 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
1506 do.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1507
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1508 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
1509 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
1510
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1511 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
1512
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1513 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
1514 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
1515 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
1516 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
1517 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
1518
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1519 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
1520 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
1521
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1522 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1523 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
1524
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1525 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
1526 ----------------------------
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 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
1529 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
1530 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
1531
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1532 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
1533 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
1534 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
1535 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
1536 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
1537 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
1538
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1539 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
1540 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
1541 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
1542
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1543 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
1544 expression directly in Info.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1545
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1546 (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
1547
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1548 (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
1549 "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
1550 Uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1551 (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
1552 (progn
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1553 (print (car list)) ; body
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1554 (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
1555 (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
1556
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1557 (print-elements-recursively animals)
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 `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
1560 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
1561 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
1562 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
1563 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
1564
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1565 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
1566 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
1567 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
1568 instance.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1569
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1570 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
1571 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
1572 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
1573
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1574 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
1575 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
1576 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
1577 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
1578 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
1579 list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1580
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1581 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
1582 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
1583 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
1584 is a separate individual.
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 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
1587 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
1588 a shorter list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1589
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1590 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
1591 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
1592 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
1593 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
1594 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
1595
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1596 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
1597 `*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
1598
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1599 gazelle
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1600
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1601 giraffe
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1602
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1603 lion
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 tiger
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1606 nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1607
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1608 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1609 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
1610
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1611 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
1612 --------------------------------------
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 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
1615 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
1616
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1617 (defun triangle-recursively (number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1618 "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
1619 Uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1620 (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
1621 1 ; then-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1622 (+ number ; else-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1623 (triangle-recursively ; recursive call
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1624 (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
1625
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1626 (triangle-recursively 7)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1627
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1628 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
1629 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
1630 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
1631 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
1632
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1633 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
1634 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
1635 value of its argument.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1636
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1637 * Menu:
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 * 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
1640 * 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
1641
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 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
1644
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1645 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
1646 .....................
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 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
1649
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1650 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
1651 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
1652 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
1653 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
1654 one pebble in it.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1655
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1656 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
1657 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
1658
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1659 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
1660 `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
1661 this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1662
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1663 (+ 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
1664
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1665 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
1666 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
1667 in detail:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1668
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1669 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
1670 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
1671 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
1672
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1673 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
1674 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
1675 `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
1676 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
1677 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
1678 function
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1679
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1680 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
1681 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
1682 `triangle-recursively' returns 1.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1683
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1684 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
1685 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
1686 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
1687
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1688 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
1689 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
1690 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
1691 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
1692
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1693 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
1694 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
1695 three pebbles in it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1696
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1697 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1698 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
1699
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1700 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
1701 .....................
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 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
1704
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1705 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
1706 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
1707 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
1708 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
1709 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
1710 true.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1711
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1712 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
1713 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
1714 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
1715
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1716 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
1717 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
1718
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1719 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
1720 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
1721 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
1722 what will happen here.
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 4 Evaluate the addition.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1725 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
1726 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
1727
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1728 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
1729
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1730 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
1731 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
1732 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
1733
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1734 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
1735
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1736 (triangle-recursively (1- 4))
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 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
1739
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1740 (triangle-recursively 3)
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 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
1743 third line.
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 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
1746
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1747 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
1748 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
1749 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
1750
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1751 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
1752 operations be deferred.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1753
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1754 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
1755 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
1756 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
1757 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
1758 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
1759 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
1760 `(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
1761
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1762 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
1763 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
1764 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
1765
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1766 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
1767 *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
1768
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 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
1771
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1772 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
1773 -------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1774
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1775 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
1776 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
1777 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
1778 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
1779
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1780 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
1781 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
1782
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1783 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
1784
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1785 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1786 BODY...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1787
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1788 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
1789
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1790 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
1791
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1792 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1793 (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
1794 (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
1795 (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
1796 ...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1797
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1798 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
1799 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
1800 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
1801
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1802 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
1803 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
1804 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
1805 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
1806 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
1807 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
1808 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
1809 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
1810 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
1811
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1812 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
1813 returns `nil'.
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 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
1816
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1817 (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
1818 (cond ((<= number 0) 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1819 ((= number 1) 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1820 ((> number 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1821 (+ 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
1822
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1823 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
1824 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
1825 (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
1826
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 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
1829
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1830 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
1831 -------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1832
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1833 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
1834 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
1835 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
1836
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1837 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1838
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1839 * Every::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1840 * Accumulate::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1841 * Keep::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1842
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1843 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1844 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
1845
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1846 Recursive Pattern: _every_
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1847 ..........................
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1848
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1849 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
1850 element of a list.
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 The basic pattern is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1853
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1854 * 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
1855
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1856 * 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
1857 - 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
1858 CDR) of the list,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1859
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1860 - 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
1861 `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
1862
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1863 Here is example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1864
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1865 (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
1866 "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
1867 (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
1868 nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1869 (cons
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1870 (* (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
1871 (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
1872
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1873 (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
1874 => (1 4 9)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1875
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1876 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
1877 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
1878 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
1879
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1880 (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
1881 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
1882 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
1883
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1884 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
1885 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
1886 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
1887 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
1888
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1889 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
1890
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1891 (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
1892
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1893 (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
1894 "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
1895 Uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1896 (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
1897 (progn
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1898 (print (car list)) ; body
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1899 (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
1900 (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
1901
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1902 (print-elements-recursively animals)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1903
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1904 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
1905
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1906 * 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
1907
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1908 * 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
1909 - 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
1910
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1911 - 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
1912 list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1913
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1914 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1915 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
1916
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1917 Recursive Pattern: _accumulate_
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1918 ...............................
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 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
1921 `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
1922 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
1923 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
1924
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1925 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
1926 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
1927
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1928 The pattern is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1929
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1930 * 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
1931
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1932 * 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
1933 - 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
1934 other combining function, with
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1935
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1936 - 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
1937 the list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1938
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1939 Here is an example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1940
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1941 (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
1942 "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
1943 (if (not numbers-list)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1944 0
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1945 (+ (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
1946
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1947 (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
1948 => 10
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1949
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1950 *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
1951 accumulate pattern.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1952
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 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
1955
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1956 Recursive Pattern: _keep_
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1957 .........................
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 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
1960 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
1961 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
1962
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1963 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
1964 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
1965
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1966 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
1967
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1968 * 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
1969
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1970 * 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
1971 a test
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1972 - 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
1973
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1974 - 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
1975 the list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1976
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1977 * 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
1978 the test
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1979 - skip on that element,
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 - 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
1982 of the list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1983
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1984 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
1985
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1986 (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
1987 "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
1988 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1989 ;; 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
1990 ((not word-list) nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1991
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1992 ;; 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
1993 ((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
1994 ;; 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
1995 (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
1996
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
1997 ;; 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
1998 ;; 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
1999 (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
2000
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2001 (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
2002 => (one two six)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2003
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2004 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
2005 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
2006
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 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
2009
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2010 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
2011 -----------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2012
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2013 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
2014 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
2015 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
2016
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2017 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
2018
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2019 (defun triangle-recursively (number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2020 "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
2021 Uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2022 (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
2023 1 ; then-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2024 (+ number ; else-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2025 (triangle-recursively ; recursive call
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2026 (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
2027
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2028 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
2029
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2030 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
2031 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
2032 `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
2033 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
2034
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2035 (+ 7 (triangle-recursively 6))
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 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
2038 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
2039 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
2040 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
2041 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
2042 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
2043 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
2044 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
2045
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2046 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
2047 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
2048 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
2049 to help it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2050
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2051 Now the total is:
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 (+ 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
2054
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2055 And what happens next?
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 (+ 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
2058
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2059 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
2060 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
2061 to make a calculation.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2062
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2063 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
2064
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2065 (+ 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
2066
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2067 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
2068 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
2069 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
2070 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
2071 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
2072 steps.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2073
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 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
2076
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2077 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
2078 ----------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2079
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2080 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
2081 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
2082 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
2083 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
2084
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2085 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
2086 does the work.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2087
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2088 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
2089 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
2090
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2091 (defun triangle-initialization (number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2092 "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
2093 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
2094 duo that uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2095 (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
2096
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2097 (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
2098 "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
2099 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
2100 that uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2101 (if (> counter number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2102 sum
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2103 (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
2104 (1+ counter) ; counter
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2105 number))) ; number
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 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
2108 `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
2109
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2110 (triangle-initialization 2)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2111 => 3
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2112
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2113 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
2114 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
2115 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
2116
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2117 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
2118 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
2119 `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
2120
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2121 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
2122 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
2123
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2124 `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
2125 `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
2126 counter number)':
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 (> 0 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2129
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2130 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
2131 the `if' clause:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2132
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2133 (triangle-recursive-helper
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2134 (+ 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
2135 (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
2136 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
2137
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2138 which will first compute:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2139
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2140 (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
2141 (1+ 0) ; counter
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2142 1) ; number
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2143 which is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2144
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2145 (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
2146
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2147 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
2148 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
2149 instance with new arguments.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2150
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2151 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
2152
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2153 (triangle-recursive-helper
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2154 (+ 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
2155 (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
2156 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
2157
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2158 which is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2159
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2160 (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
2161
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2162 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
2163 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
2164
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2165 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
2166 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
2167
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2168 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
2169
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2170 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
2171
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2172 sum counter number
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2173 (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
2174
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2175 (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
2176
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2177 (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
2178
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2179 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
2180 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
2181
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2182 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
2183 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
2184
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2185 ---------- Footnotes ----------
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 (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
2188 that uses `constant space'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2189
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2190 (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
2191 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
2192 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
2193 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
2194 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
2195 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
2196 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
2197 contexts.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2198
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 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
2201
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2202 11.4 Looping Exercise
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2203 =====================
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 * 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
2206 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
2207
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2208 * 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
2209 adds the values.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2210
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2211 * 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
2212 using `cond'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2213
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2214 * 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
2215 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
2216 (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
2217 written in Texinfo.)
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 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
2220 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
2221 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
2222 `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
2223 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
2224 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
2225
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2226 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
2227 (texinfo)Indicating.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2228
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 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
2231
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2232 12 Regular Expression Searches
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2233 ******************************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2234
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2235 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
2236 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
2237 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
2238 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
2239
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2240 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
2241 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
2242 (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
2243 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
2244 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
2245 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
2246 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
2247 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
2248 that spot.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2249
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2250 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
2251 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
2252 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
2253 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
2254 `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
2255 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
2256 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
2257 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
2258
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2259 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2260
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2261 * sentence-end::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2262 * re-search-forward::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2263 * forward-sentence::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2264 * forward-paragraph::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2265 * etags::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2266 * Regexp Review::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2267 * re-search Exercises::
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: 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
2271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2272 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
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 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
2276 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
2277
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2278 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
2279 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
2280 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
2281 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
2282
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2283 [.?!]
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2284
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2285 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
2286 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
2287 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
2288 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
2289
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2290 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
2291 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
2292 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
2293 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
2294 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
2295 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
2296 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
2297
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2298 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
2299
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 ^ ^^
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2302 TAB SPC
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2303
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2304 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
2305 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
2306 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
2307
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2308 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
2309 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
2310 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
2311 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
2312
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2313 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
2314 this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2315
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2316 [
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2319 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
2320 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
2321 <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
2322
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2323 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
2324 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
2325 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
2326 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
2327 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
2328
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2331 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
2332 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
2333 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
2334 `'', `)', and `}'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2335
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2336 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
2337 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
2338 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
2339
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2340 sentence-end
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 ]*"
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 (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
2345 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
2346 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
2347 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
2348 `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
2349 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
2350 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
2351 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
2352 `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
2353 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
2354
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 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
2357
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2358 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
2359 =====================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2360
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2361 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
2362 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
2363
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2364 `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
2365 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
2366 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
2367 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
2368 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
2369
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2370 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
2371 arguments:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2372
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2373 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
2374 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
2375 quotations marks.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2376
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2377 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
2378 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
2379 buffer.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2380
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2381 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
2382 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
2383 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
2384 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
2385 search succeeds.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2386
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2387 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
2388 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
2389
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2390 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
2391
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2392 (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
2393 LIMIT-OF-SEARCH
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2394 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
2395 REPEAT-COUNT)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2396
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2397 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
2398 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
2399 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
2400 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
2401 to.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2402
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2403 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
2404 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
2405
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2406 "[.?!][]\"')}]*\\($\\| \\| \\)[
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2407 ]*"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2408
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2409 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
2410 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
2411 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
2412 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
2413
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2414 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2415 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
2416
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2417 12.3 `forward-sentence'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2418 =======================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2419
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2420 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
2421 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
2422 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
2423 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
2424 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
2425 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
2426
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2427 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2428
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2429 * Complete forward-sentence::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2430 * fwd-sentence while loops::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2431 * fwd-sentence re-search::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2432
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2433 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2434 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
2435
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2436 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
2437 -----------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2438
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2439 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
2440
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2441 (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
2442 "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
2443 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
2444
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2445 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
2446 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
2447 (interactive "p")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2448 (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
2449 (let ((opoint (point))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2450 (sentence-end (sentence-end)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2451 (while (< arg 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2452 (let ((pos (point))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2453 (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
2454 (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
2455 (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
2456 (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
2457 (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
2458 (goto-char par-beg)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2459 (setq arg (1+ arg)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2460 (while (> arg 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2461 (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
2462 (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
2463 (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
2464 (goto-char par-end)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2465 (setq arg (1- arg)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2466 (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
2467
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2468 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
2469 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
2470 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
2471
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2472 (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
2473 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2474 (interactive "p")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2475 (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
2476 (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
2477 (while (< arg 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2478 (let ((pos (point))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2479 (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
2480 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
2481 (while (> arg 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2482 (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
2483 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
2484 HANDLE-FORMS-AND-EQUIVALENT
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2485
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2486 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
2487 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
2488 expression, and `while' loops.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2489
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2490 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
2491
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2492 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
2493
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2494 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
2495 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
2496 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
2497 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
2498
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2499 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
2500 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
2501 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
2502 `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
2503 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
2504
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2505 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
2506 `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
2507 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
2508 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
2509 `sentence-end' function.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2510
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 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
2513
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2514 The `while' loops
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2515 -----------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2516
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2517 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
2518 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
2519 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
2520 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
2521 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
2522 the second `while' loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2523
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2524 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
2525 looks like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2526
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2527 (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
2528 (let VARLIST
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2529 (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
2530 THEN-PART
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2531 ELSE-PART
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2532 (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
2533
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2534 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
2535 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
2536 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
2537 `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
2538 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
2539
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2540 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
2541 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
2542 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
2543
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2544 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
2545 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
2546 expression.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2547
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2548 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
2549
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2550 (let ((par-end
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2551 (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
2552 (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
2553 (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
2554 (goto-char par-end)))
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 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
2557 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
2558 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
2559 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
2560 end of the paragraph.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2561
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2562 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
2563 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
2564 `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
2565 expression
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 (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
2568
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2569 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
2570 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
2571 `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
2572 `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
2573 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
2574 `(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
2575 will discuss shortly.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2576
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2577 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
2578 that looks like this:
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 (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
2581 (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
2582 (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
2583
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2584 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
2585 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
2586 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
2587 regular expression search.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2588
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2589 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
2590 `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
2591 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
2592
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2593 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2594 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
2595
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2596 The regular expression search
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2597 -----------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2598
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2599 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
2600 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
2601 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
2602 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
2603
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2604 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
2605 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
2606
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2607 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
2608 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
2609 successful.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2610
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2611 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
2612 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
2613 the search.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2614
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2615 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
2616 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
2617 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
2618 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
2619 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
2620 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
2621 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
2622 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
2623
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2624 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
2625 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
2626 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
2627 `(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
2628
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2629 (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
2630 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
2631
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2632 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
2633 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
2634 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
2635 this pattern often.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2636
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 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
2639
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2640 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
2641 =================================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2642
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2643 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
2644 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
2645 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
2646 `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
2647
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2648 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
2649 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
2650 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
2651
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2652 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
2653 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
2654 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
2655 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
2656 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
2657 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
2658
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2659 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
2660 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
2661 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
2662 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
2663 fill prefix.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2664
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2665 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
2666 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
2667 added complication.
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 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2670
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2671 * forward-paragraph in brief::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2672 * fwd-para let::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2673 * fwd-para while::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2674
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2675 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2676 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
2677
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2678 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
2679 -------------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2680
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2681 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
2682 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
2683 daunting!
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2684
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2685 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
2686
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2687 (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
2688 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2689 (interactive "p")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2690 (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
2691 (let*
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2692 VARLIST
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2693 (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
2694 ...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2695 (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
2696 ...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2697
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2698 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
2699 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
2700
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2701 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
2702 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
2703 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
2704 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
2705 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
2706 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
2707 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
2708 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
2709 function.
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2712 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
2713
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2714 The `let*' expression
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2717 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
2718 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
2719 `let'.
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 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
2722 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
2723 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
2724 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
2725
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2726 (*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
2727
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2728 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
2729 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
2730 `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
2731
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2732 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
2733 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
2734
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2735 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
2736 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
2737 `forward-sentence'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2738
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2739 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
2740 evaluating the following list:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2741
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2742 (and fill-prefix
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2743 (not (equal fill-prefix ""))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2744 (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
2745 (regexp-quote fill-prefix))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2746
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2747 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
2748
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2749 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
2750 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
2751 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
2752 `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
2753 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
2754 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
2755 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
2756 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
2757
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2758 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
2759 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
2760 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
2761 `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
2762
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2763 `fill-prefix'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2764 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
2765 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
2766 returns `nil'.
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 `(not (equal fill-prefix "")'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2769 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
2770 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
2771 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
2772
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2773 `(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
2774 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
2775 `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
2776 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
2777
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2778 `(regexp-quote fill-prefix)'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2779 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
2780 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
2781 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
2782 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
2783
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2784 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
2785 `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
2786 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
2787 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
2788 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
2789 `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
2790 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
2791 set to `nil'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2792
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2793 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
2794 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
2795 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
2796 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
2797 prefixes.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2798
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2799 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
2800 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
2801 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
2802
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2803 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
2804 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
2805 (`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
2806 paragraphs.)
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 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
2809 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
2810 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
2811 pattern.
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 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
2814 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
2815 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
2816 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
2817 `"[ \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
2818 alternative to `parsep'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2819
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2820 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
2821 `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
2822 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
2823
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2824 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
2825 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
2826 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
2827
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2828 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2829 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
2830
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2831 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
2832 -------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2833
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2834 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
2835 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
2836 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
2837 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
2838 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
2839
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2840 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
2841 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
2842
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2843 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
2844
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2845 ;; 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
2846 (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
2847
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2848 ;; between paragraphs
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2849 ;; 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
2850 (while (and (not (eobp))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2851 (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
2852 (looking-at parsep))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2853 (forward-line 1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2854 ;; This decrements the loop
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2855 (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
2856 ;; ... and one more line.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2857 (forward-line 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2858
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2859 (if fill-prefix-regexp
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2860 ;; 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
2861 ;; 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
2862 (while (and (not (eobp))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2863 (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
2864 (not (looking-at parsep))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2865 (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
2866 (forward-line 1))
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 ;; 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
2869 ;; 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
2870 (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
2871 (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
2872 (goto-char start)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2873 (not (eobp)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2874 (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
2875 (not (looking-at parsep)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2876 (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
2877 (and use-hard-newlines
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2878 (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
2879 (forward-char 1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2880
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2881 ;; 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
2882 ;; 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
2883 ;; for sp-parstart
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2884 (if (< (point) (point-max))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2885 (goto-char start))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2886
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2887 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
2888 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
2889 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
2890 `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
2891 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
2892 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
2893
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2894 (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
2895 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
2896 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
2897 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
2898 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
2899
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2900 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
2901 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
2902 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
2903
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2904 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
2905 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
2906 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
2907 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
2908
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2909 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
2910 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
2911 argument.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2912
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2913 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
2914 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
2915
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2916 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
2917
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2918 (if fill-prefix-regexp
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2919 ;; 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
2920 ;; 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
2921 (while (and (not (eobp))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2922 (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
2923 (not (looking-at parsep))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2924 (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
2925 (forward-line 1))
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 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
2928 conditions are true:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2929
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2930 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
2931
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2932 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
2933 of the buffer.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2934
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2935 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
2936
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2937 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
2938
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2939 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
2940 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
2941 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
2942 `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
2943
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2944 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
2945
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2946 (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
2947 (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
2948 (goto-char start)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2949 (not (eobp)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2950 (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
2951 (not (looking-at parsep)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2952 (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
2953 (and use-hard-newlines
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2954 (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
2955 (forward-char 1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2956
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2957 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
2958 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
2959 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
2960 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
2961 the `match-beginning' function.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2962
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2963 The two expressions,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2964
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2965 (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
2966 (goto-char start)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2967
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2968 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
2969 search.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2970
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2971 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
2972 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
2973 the last search.
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 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
2976 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
2977 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
2978 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
2979 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
2980
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2981 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
2982 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
2983 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
2984 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
2985
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2986 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
2987 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
2988 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
2989 `(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
2990 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
2991
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2992 (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
2993 `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
2994 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
2995 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
2996 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
2997
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
2998 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
2999
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3000 (if (< (point) (point-max))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3001 (goto-char start))))
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 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
3004 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
3005 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
3006
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3007 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
3008 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
3009
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3010 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
3011 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
3012 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
3013 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
3014 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
3015 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
3016 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
3017 shut!)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3018
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3019 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3020 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
3021
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3022 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
3023 ================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3024
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3025 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
3026 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
3027 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
3028 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
3029 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
3030 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
3031 `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
3032 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
3033 sources.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3034
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3035 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
3036
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3037 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
3038 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
3039 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
3040
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3041 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
3042 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
3043 in upper case letters.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3044
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3045 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
3046 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
3047 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
3048 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
3049
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3050 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
3051 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
3052 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
3053 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
3054 `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
3055
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3056 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
3057
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3058 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
3059
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3060 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
3061 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
3062 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
3063 directory.
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 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
3066 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
3067 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
3068 directory:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3069
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3070 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
3071
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3072 Type
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3073
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3074 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
3075
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3076 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
3077 supported languages.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3078
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3079 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
3080 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
3081 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
3082 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
3083 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
3084
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3085 `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
3086 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
3087 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
3088 the `TAGS' file.
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 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
3091 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
3092 attempt to find it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3093
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3094 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
3095 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
3096 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
3097 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
3098
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3099 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
3100 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
3101 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
3102
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3103 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
3104 ..................................
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3105
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3106 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
3107 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
3108 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
3109 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
3110 Emacs source directory.
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 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
3113 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
3114
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3115 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
3116
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3117 (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
3118 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
3119
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3120 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
3121
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3122 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3123 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
3124
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3125 12.6 Review
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3126 ===========
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3127
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3128 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
3129
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3130 `while'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3131 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
3132 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
3133 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
3134
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3135 For example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3136
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3137 (let ((foo 2))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3138 (while (> foo 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3139 (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
3140 (setq foo (1- foo))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3141
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3142 => foo is 2.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3143 foo is 1.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3144 nil
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 `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
3147 `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
3148 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
3149
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3150 `re-search-forward'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3151 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
3152 rest just after it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3153
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3154 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
3155
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3156 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
3157 (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
3158
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3159 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
3160
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3161 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
3162 error message.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3163
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3164 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
3165 the search goes backwards.
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 `let*'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3168 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
3169 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
3170 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
3171 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
3172
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3173 For example:
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 (let* ((foo 7)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3176 (bar (* 3 foo)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3177 (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
3178 => `bar' is 21.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3179
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3180 `match-beginning'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3181 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
3182 regular expression search.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3183
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3184 `looking-at'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3185 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
3186 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
3187
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3188 `eobp'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3189 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
3190 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
3191 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
3192 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
3193
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3194 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3195 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
3196
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3197 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
3198 =======================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3199
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3200 * 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
3201 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
3202
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3203 * 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
3204 *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
3205 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
3206 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
3207 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
3208 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
3209 *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
3210
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3211 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3212 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
3213
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3214 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
3215 ***********************************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3216
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3217 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
3218 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
3219 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
3220 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
3221
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3222 * Menu:
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 * Why Count Words::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3225 * count-words-region::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3226 * recursive-count-words::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3227 * Counting Exercise::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3228
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3229 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3230 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
3231
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3232 Counting words
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 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
3236 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
3237 function for counting words.
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 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
3240 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
3241 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
3242 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
3243 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
3244 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
3245 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
3246 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
3247 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
3248 count words.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3249
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 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
3252
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3253 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
3254 ======================================
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 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
3257 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
3258 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
3259 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
3260 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
3261 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
3262 `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
3263 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
3264
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3265 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
3266 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
3267 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
3268 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
3269 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
3270
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3271 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3272
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3273 * Design count-words-region::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3274 * Whitespace Bug::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3275
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3276 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3277 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
3278
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3279 Designing `count-words-region'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3280 ------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3281
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3282 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
3283 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
3284
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3285 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
3286
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3287 (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
3288 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3289 (INTERACTIVE-EXPRESSION...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3290 BODY...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3291
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3292 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
3293
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3294 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
3295 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
3296 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
3297
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3298 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
3299 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
3300 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
3301 `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
3302 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
3303 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
3304 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
3305 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
3306 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
3307
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3308 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
3309 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
3310 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
3311 user.
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 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
3314 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
3315 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
3316 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
3317 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
3318 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
3319 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
3320
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3321 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
3322 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
3323 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
3324 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
3325 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
3326
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3327 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
3328 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
3329 `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
3330
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3331 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
3332 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
3333 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
3334 word by word.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3335
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3336 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
3337 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
3338 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
3339 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
3340 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
3341 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
3342 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
3343 optional.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3344
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3345 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
3346 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
3347 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
3348 this is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3349
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3350 \w+\W*
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3351
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3352 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
3353 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
3354 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
3355 *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
3356
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3357 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
3358
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3359 (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
3360
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3361 (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
3362 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
3363 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
3364 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
3365 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
3366 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
3367 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
3368 `re-search-forward'.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3369
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3370 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
3371 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
3372 `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
3373
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3374 (setq count (1+ count))
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 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
3377 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
3378 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
3379 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
3380 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
3381 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
3382 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
3383 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
3384 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
3385 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
3386 appropriate.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3387
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3388 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
3389
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3390 ;;; First version; has bugs!
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3391 (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
3392 "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
3393 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
3394 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
3395 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
3396 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
3397 (interactive "r")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3398 (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
3399
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3400 ;;; 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
3401 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3402 (goto-char beginning)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3403 (let ((count 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3404
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3405 ;;; 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
3406 (while (< (point) end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3407 (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
3408 (setq count (1+ count)))
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 ;;; 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
3411 (cond ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3412 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3413 "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
3414 ((= 1 count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3415 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3416 "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
3417 (t
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3418 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3419 "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
3420
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3421 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
3422
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3423 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3424 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
3425
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3426 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
3427 -------------------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3428
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3429 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
3430 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
3431 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
3432 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
3433 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
3434 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
3435 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
3436
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3437 Search failed: "\\w+\\W*"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3438
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3439 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
3440 bugs yourself.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3441
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3442 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
3443 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
3444 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
3445
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3446 ;; First version; has bugs!
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3447 (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
3448 "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
3449 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
3450 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
3451 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
3452 (interactive "r")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3453 (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
3454
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3455 ;;; 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
3456 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3457 (goto-char beginning)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3458 (let ((count 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3459
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3460 ;;; 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
3461 (while (< (point) end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3462 (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
3463 (setq count (1+ count)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3464
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3465 ;;; 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
3466 (cond ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3467 (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
3468 ((= 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
3469 (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
3470
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3471 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
3472
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3473 (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
3474
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3475 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
3476 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
3477 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
3478
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3479 one two three
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3480
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3481 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
3482
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3483 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
3484 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
3485 `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
3486 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
3487 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
3488
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3489 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
3490 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
3491 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
3492 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
3493 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
3494 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
3495 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
3496 message saying `Search failed'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3497
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3498 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
3499
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3500 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
3501 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
3502 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
3503 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
3504 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
3505 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
3506 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
3507 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
3508 `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
3509 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
3510 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
3511 the marked region.
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 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
3514 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
3515 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
3516 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
3517 buffer, the search fails.
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 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
3520 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
3521
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3522 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
3523 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
3524 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
3525
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3526 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
3527 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
3528 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
3529 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
3530 `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
3531 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
3532 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
3533 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
3534
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3535 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
3536 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
3537 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
3538 expression search expression:
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 (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
3541
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3542 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
3543 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
3544 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
3545
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3546 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
3547 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
3548 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
3549 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
3550 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
3551
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3552 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
3553 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
3554 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
3555
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3556 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
3557 "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
3558 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
3559
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3560 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
3561 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
3562 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
3563 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
3564 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
3565 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
3566 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
3567 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
3568 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
3569 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
3570 and the cycle repeats ...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3571
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3572 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
3573 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
3574 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
3575 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
3576 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
3577 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
3578
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3579 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
3580 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
3581 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
3582 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
3583
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3584 (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
3585
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3586 (*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
3587 about `and'.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3588
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3589 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
3590 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
3591 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
3592 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
3593 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
3594 `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
3595
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3596 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
3597 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
3598 what it looks like:
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 ;;; Final version: `while'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3601 (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
3602 "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
3603 (interactive "r")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3604 (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
3605
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3606 ;;; 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
3607 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3608 (let ((count 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3609 (goto-char beginning)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3610
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3611 ;;; 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
3612 (while (and (< (point) end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3613 (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
3614 (setq count (1+ count)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3615
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3616 ;;; 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
3617 (cond ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3618 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3619 "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
3620 ((= 1 count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3621 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3622 "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
3623 (t
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3624 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3625 "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
3626
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3627 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3628 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
3629
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3630 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
3631 ============================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3632
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3633 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
3634 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
3635
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3636 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
3637 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
3638 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
3639 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
3640
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3641 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
3642 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
3643 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
3644 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
3645 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
3646 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
3647 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
3648
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3649 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
3650 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
3651
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3652 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
3653 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
3654 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
3655 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
3656
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3657 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
3658 previous versions:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3659
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3660 ;; 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
3661 (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
3662 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3663 (INTERACTIVE-EXPRESSION...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3664
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3665 ;;; 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
3666 (EXPLANATORY MESSAGE)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3667 (SET-UP FUNCTIONS...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3668
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3669 ;;; 2. Count the words.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3670 RECURSIVE CALL
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 ;;; 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
3673 MESSAGE PROVIDING WORD COUNT))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3674
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3675 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
3676 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
3677 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
3678 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
3679 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
3680 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
3681 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
3682
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3683 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
3684 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
3685 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
3686 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
3687
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3688 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
3689
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3690 (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
3691 "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
3692 (interactive "r")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3693
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3694 ;;; 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
3695 (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
3696 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3697 (goto-char beginning)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3698
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3699 ;;; 2. Count the words.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3700 (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
3701
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3702 ;;; 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
3703 (cond ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3704 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3705 "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
3706 ((= 1 count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3707 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3708 "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
3709 (t
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3710 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3711 "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
3712
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3713 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
3714
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3715 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
3716 `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
3717
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3718 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
3719 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
3720 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
3721 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
3722 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
3723 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
3724 `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
3725 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
3726
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3727 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
3728 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
3729 again.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3730
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3731 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
3732 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
3733 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
3734 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
3735 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
3736 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
3737
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3738 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
3739
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3740 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
3741 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
3742
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3743 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
3744
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3745 (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
3746 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3747 DO-AGAIN-TEST
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3748 NEXT-STEP-EXPRESSION
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3749 RECURSIVE CALL)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3750
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3751 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
3752 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
3753 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
3754 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
3755 should return zero.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3756
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3757 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
3758 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
3759
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3760 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
3761
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3762 (and (< (point) region-end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3763 (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
3764
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3765 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
3766 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
3767 (*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
3768 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
3769
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3770 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
3771 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
3772 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
3773 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
3774 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
3775
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3776 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
3777 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
3778 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
3779
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3780 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
3781 `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
3782 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
3783 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
3784 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
3785 `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
3786
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3787 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
3788 looks like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3789
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3790 (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
3791 ;; then
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3792 RECURSIVE-CALL-RETURNING-COUNT
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3793 ;; else
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3794 RETURN-ZERO)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3795
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3796 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
3797
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3798 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
3799 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
3800 systematically.
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 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
3803 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
3804 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
3805 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
3806 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
3807
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3808 Consider several cases:
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 * 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
3811 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
3812 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
3813 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
3814
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3815 * 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
3816 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
3817 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
3818 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
3819
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3820 * 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
3821 zero.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3822
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3823 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
3824 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
3825 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
3826 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
3827
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3828 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
3829 one to its argument.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3830
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3831 (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
3832
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3833 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
3834
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3835 (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
3836 "DOCUMENTATION..."
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 ;;; 1. do-again-test
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3839 (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
3840 (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
3841
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3842 ;;; 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
3843 (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
3844
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3845 ;;; 3. else-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3846 0))
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 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
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 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
3851 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
3852
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3853 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
3854 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
3855 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
3856 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
3857 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
3858 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
3859 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
3860
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3861 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
3862 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
3863 `(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
3864 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
3865 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
3866 `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
3867 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
3868 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
3869
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3870 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
3871 `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
3872 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
3873 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
3874 amount.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3875
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3876 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
3877 `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
3878 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
3879 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
3880
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3881 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
3882
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3883 The recursive function:
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 (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
3886 "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
3887
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3888 ;;; 1. do-again-test
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3889 (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
3890 (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
3891
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3892 ;;; 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
3893 (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
3894
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3895 ;;; 3. else-part
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3896 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3897
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3898 The wrapper:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3899
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3900 ;;; Recursive version
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3901 (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
3902 "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
3903
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3904 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
3905 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
3906 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
3907 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
3908 (interactive "r")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3909 (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
3910 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3911 (goto-char beginning)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3912 (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
3913 (cond ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3914 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3915 "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
3916 ((= 1 count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3917 (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
3918 (t
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3919 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3920 "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
3921
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 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
3924
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3925 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
3926 ===================================
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 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
3929 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
3930 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
3931
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3932 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3933 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
3934
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3935 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
3936 ******************************
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 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
3939 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
3940 `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
3941 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
3942 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
3943 (`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
3944
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3945 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
3946 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
3947 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
3948 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
3949 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
3950 will tell.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3951
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3952 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3953
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3954 * Divide and Conquer::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3955 * Words and Symbols::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3956 * Syntax::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3957 * count-words-in-defun::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3958 * Several defuns::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3959 * Find a File::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3960 * lengths-list-file::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3961 * Several files::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3962 * Several files recursively::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3963 * Prepare the data::
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3966 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
3967
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3968 Divide and Conquer
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3969 ==================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3970
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3971 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
3972 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
3973 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
3974
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3975 * 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
3976 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
3977
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3978 * 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
3979 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
3980 `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
3981
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3982 * 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
3983 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
3984 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
3985 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
3986
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3987 * 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
3988 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
3989 as a graph.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3990
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3991 * 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
3992
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3993 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
3994 be difficult.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3995
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3996 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3997 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
3998
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
3999 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
4000 ===================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4001
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4002 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
4003 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
4004 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
4005 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
4006 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
4007 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
4008 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
4009 `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
4010 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
4011
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4012 (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
4013 "Multiply NUMBER by seven."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4014 (* 7 number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4015
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4016 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
4017 (`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
4018 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
4019 ten! Something is wrong!
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4020
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4021 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
4022 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
4023 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
4024 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
4025 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
4026
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4027 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
4028 `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
4029 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
4030
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4031 "\\w+\\W*"
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 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
4034 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
4035 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
4036 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
4037 of its own.
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4040 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
4041
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4042 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
4043 =======================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4044
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4045 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
4046 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
4047 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
4048 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
4049 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
4050 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
4051 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
4052 (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
4053
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4054 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
4055 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
4056 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
4057 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
4058 `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
4059 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
4060 `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
4061
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4062 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
4063 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
4064
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4065 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
4066 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
4067 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
4068 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
4069 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
4070
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4071 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
4072 `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
4073 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
4074
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4075 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
4076 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
4077
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4078 "\\(\\w\\|\\s_\\)+"
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 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
4081 `\\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
4082 `\\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
4083 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
4084 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
4085 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
4086
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4087 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
4088 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
4089 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
4090 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
4091
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4092 "\\(\\W\\|\\S_\\)*"
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 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
4095 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
4096 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
4097 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
4098
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4099 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
4100 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
4101 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
4102 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
4103 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
4104 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
4105 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
4106 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
4107 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
4108
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4109 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
4110
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4111 "\\(\\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
4112
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4113 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4114 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
4115
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4116 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
4117 ========================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4118
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4119 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
4120 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
4121 of these versions.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4122
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4123 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
4124 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
4125 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
4126 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
4127 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
4128
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4129 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
4130 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
4131 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
4132 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
4133 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
4134 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
4135
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4136 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
4137
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4138 (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
4139 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4140 (SET UP...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4141 (WHILE LOOP...)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4142 RETURN COUNT)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4143
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4144 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
4145
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4146 First, the set up.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4147
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4148 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
4149 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
4150 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
4151 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
4152 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
4153 definition.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4154
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4155 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
4156 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
4157 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
4158 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
4159 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
4160 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
4161 wish to start.
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 `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
4164 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
4165 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
4166 zero.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4167
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4168 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
4169 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
4170 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
4171 of the definition.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4172
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4173 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
4174 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
4175 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
4176 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
4177 looping.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4178
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4179 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
4180
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4181 (beginning-of-defun)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4182 (let ((count 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4183 (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
4184
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4185 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
4186 `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
4187 `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
4188 `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
4189
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4190 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
4191 `while' loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4192
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4193 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
4194 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
4195 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
4196 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
4197 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
4198 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
4199
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4200 (while (and (< (point) end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4201 (re-search-forward
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4202 "\\(\\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
4203 (setq count (1+ count)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4204
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4205 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
4206 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
4207 `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
4208 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
4209
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4210 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
4211
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4212 (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
4213 "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
4214 (beginning-of-defun)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4215 (let ((count 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4216 (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
4217 (while
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4218 (and (< (point) end)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4219 (re-search-forward
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4220 "\\(\\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
4221 end t))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4222 (setq count (1+ count)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4223 count))
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 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
4226 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
4227 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
4228 `count-words-region':
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 ;;; Interactive version.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4231 (defun count-words-defun ()
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4232 "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
4233 (interactive)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4234 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4235 "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
4236 (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
4237 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4238 ((zerop count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4239 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4240 "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
4241 ((= 1 count)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4242 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4243 "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
4244 (t
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4245 (message
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4246 "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
4247
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4248 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
4249
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4250 (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
4251
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4252 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
4253 `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
4254 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
4255
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4256 (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
4257 "Multiply NUMBER by seven."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4258 (* 7 number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4259 => 10
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4260
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4261 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
4262
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4263 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
4264 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
4265
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4266 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4267 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
4268
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4269 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
4270 =========================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4272 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
4273 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
4274 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
4275 statistics on one file.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4276
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4277 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
4278 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
4279
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4280 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
4281 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
4282 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
4283 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
4284 messages.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4285
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4286 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
4287 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
4288 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
4289 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
4290 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
4291
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4292 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
4293 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
4294 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
4295 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
4296 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
4297 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
4298 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
4299 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
4300 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
4301
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4302 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
4303
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4304 (goto-char (point-min))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4305 (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
4306 (setq lengths-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4307 (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
4308
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4309 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
4310 contains the function definitions.
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 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
4313 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
4314 buffer.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4315
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4316 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
4317
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4318 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4319 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
4320
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4321 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
4322 ================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4323
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4324 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
4325 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
4326
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4327 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
4328
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4329 (defun find-file (filename)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4330 "Edit file FILENAME.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4331 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
4332 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
4333 (interactive "FFind file: ")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4334 (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
4335
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4336 (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
4337 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
4338 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
4339 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
4340 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
4341
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4342 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
4343 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
4344 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
4345 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
4346
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4347 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
4348 `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
4349 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
4350 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
4351 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
4352 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
4353
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4354 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
4355 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
4356 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
4357 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
4358 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
4359 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
4360 elsewhere. (*Note Switching Buffers::.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4361
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4362 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
4363 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
4364 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
4365 `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
4366 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
4367 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
4368 expression.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4369
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4370 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
4371
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4372 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4373 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
4374
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4375 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
4376 ==================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4377
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4378 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
4379 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
4380 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
4381 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
4382 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
4383 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
4384
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4385 (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
4386 "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
4387 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
4388 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
4389 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
4390 (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
4391 (save-excursion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4392 (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
4393 (lengths-list))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4394 (set-buffer buffer)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4395 (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
4396 (widen)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4397 (goto-char (point-min))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4398 (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
4399 (setq lengths-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4400 (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
4401 (kill-buffer buffer)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4402 lengths-list)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4403
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4404 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
4405 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
4406 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
4407 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
4408
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4409 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
4410 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
4411 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
4412 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
4413
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4414 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
4415 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
4416 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
4417
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4418 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
4419
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4420 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
4421 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
4422 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
4423 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
4424 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
4425 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
4426 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
4427 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
4428 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
4429 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
4430
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4431 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
4432 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
4433 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
4434 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
4435 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
4436 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
4437
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4438 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
4439 of the buffer.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4440
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4441 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
4442 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
4443 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
4444
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4445 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
4446 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
4447 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
4448 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
4449
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4450 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
4451 `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
4452 whole function.
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 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
4455 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
4456 (`eval-last-sexp').
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 (lengths-list-file
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4459 "/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
4460
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4461 (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
4462 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
4463 `*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
4464
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4465 (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
4466 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
4467
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4468 (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
4469
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4470 (*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
4471 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
4472
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4473 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
4474 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
4475
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4476 (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
4477
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4478 (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
4479 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
4480
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4481 (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
4482
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4483 (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
4484 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
4485
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4486 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
4487 list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4488
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 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
4491
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4492 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
4493 ===============================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4494
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4495 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
4496 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
4497 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
4498 list of files.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4499
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4500 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
4501 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
4502
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4503 * Menu:
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 * lengths-list-many-files::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4506 * append::
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4509 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
4510
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4511 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
4512 ---------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4513
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4514 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
4515 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
4516 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
4517 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
4518 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
4519 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
4520 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
4521 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
4522 body is evaluated.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4523
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4524 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
4525
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4526 (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
4527 BODY...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4528 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
4529
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4530 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
4531 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
4532 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
4533 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
4534 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
4535 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
4536 `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
4537 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
4538 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
4539 Example.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4540
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4541 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
4542
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4543 ;;; Use `while' loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4544 (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
4545 "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
4546 (let (lengths-list)
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 ;;; true-or-false-test
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4549 (while list-of-files
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4550 (setq lengths-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4551 (append
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4552 lengths-list
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 ;;; Generate a lengths' list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4555 (lengths-list-file
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4556 (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
4557
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4558 ;;; Make files' list shorter.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4559 (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
4560
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4561 ;;; 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
4562 lengths-list))
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 `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
4565 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
4566 function is called.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4567
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4568 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
4569 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
4570 directory,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4571
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4572 debug.el
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4573
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4574 becomes
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4575
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4576 /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
4577
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4578 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
4579 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
4580
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4581 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4582 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
4583
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4584 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
4585 ----------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4586
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4587 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
4588
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4589 (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
4590
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4591 produces the list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4592
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4593 (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
4594
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4595 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
4596 `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
4597
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4598 (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
4599
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4600 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
4601 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
4602
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4603 ((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
4604
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4605 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4606 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
4607
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4608 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
4609 ===============================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4610
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4611 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
4612 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
4613 and simple.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4614
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4615 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
4616 `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
4617 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
4618 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
4619 `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
4620 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
4621 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
4622 description!
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 (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
4625 "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
4626 (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
4627 (append
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4628 (lengths-list-file
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4629 (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
4630 (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
4631 (cdr list-of-files)))))
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 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
4634 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
4635 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
4636
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4637 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
4638 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
4639 individually.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4640
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4641 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
4642 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
4643 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
4644 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
4645 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
4646 evaluate them.
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 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
4649 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
4650 produce different results.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4651
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4652 (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
4653
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4654 (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
4655 => (283 263 480 90)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4656
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4657 (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
4658 => (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
4659
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4660 (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
4661 => (85 181)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4662
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4663 (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
4664 '("./lisp/macros.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4665 "./lisp/mail/mailalias.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4666 "./lisp/makesum.el"))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4667 => (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
4668
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4669 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
4670 want.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4671
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4672 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
4673
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4674 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4675 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
4676
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4677 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
4678 ============================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4679
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4680 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
4681 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
4682 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
4683 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
4684 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
4685 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
4686 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
4687
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4688 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
4689 `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
4690 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
4691 numbers.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4692
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4693 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
4694 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
4695 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
4696 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
4697
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4698 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
4699 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
4700 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
4701 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
4702 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
4703 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
4704 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
4705 that we will need.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4706
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4707 * Menu:
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 * Sorting::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4710 * Files List::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4711 * Counting function definitions::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4712
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 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
4715
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4716 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
4717 --------------------
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 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
4720 `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
4721 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
4722 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
4723
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4724 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
4725 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
4726 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
4727 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
4728 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
4729 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
4730
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4731 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
4732
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4733 (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
4734
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4735 produces this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4736
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4737 (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
4738
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4739 (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
4740 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
4741
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4742 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
4743 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
4744
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4745 (sort
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4746 (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
4747 '("./lisp/macros.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4748 "./lisp/mailalias.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4749 "./lisp/makesum.el"))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4750 '<)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4751
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4752 which produces:
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 (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
4755
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4756 (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
4757 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
4758 is passed to `sort'.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4759
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4760 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4761 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
4762
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4763 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
4764 -----------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4765
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4766 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
4767 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
4768 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
4769 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
4770 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
4771 call.
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 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
4774 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
4775 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
4776 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
4777 directory.
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 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
4780 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
4781 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
4782 `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
4783 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
4784 into the sub-directories.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4785
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4786 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
4787 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
4788 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
4789 `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
4790 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
4791 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
4792
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4793 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
4794 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
4795 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
4796
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4797 ("./lisp/macros.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4798 "./lisp/mail/rmail.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4799 "./lisp/makesum.el")
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 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
4802 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
4803 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
4804 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
4805 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
4806 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
4807 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
4808
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4809 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
4810 `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
4811 `/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
4812 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
4813
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4814 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
4815 attributes:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4816
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4817 ("abbrev.el"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4818 nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4819 1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4820 1000
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4821 100
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4822 (17733 259)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4823 (17491 28834)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4824 (17596 62124)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4825 13157
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4826 "-rw-rw-r--"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4827 nil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4828 2971624
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4829 773)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4830
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4831 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
4832 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
4833
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4834 ("mail"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4835 t
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 )
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4838
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4839 (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
4840 `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
4841 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
4842 `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
4843
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4844 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
4845 `.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
4846 directories below that directory.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4847
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4848 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
4849 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
4850 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
4851 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
4852
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4853 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
4854 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
4855 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
4856 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
4857 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
4858 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
4859 current directory.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4860
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4861 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
4862 tasks:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4863
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4864 * 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
4865 `.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
4866
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4867 * 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
4868 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
4869
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4870 - 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
4871 skip it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4872
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4873 - 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
4874
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4875 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
4876 `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
4877 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
4878 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
4879 `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
4880 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
4881
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4882 Here is the function:
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 (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
4885 "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
4886 ;; 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
4887 ;; 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
4888 ;; 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
4889 ;; "/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
4890 (interactive "DDirectory name: ")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4891 (let (el-files-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4892 (current-directory-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4893 (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
4894 ;; 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
4895 (while current-directory-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4896 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4897 ;; 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
4898 ;; 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
4899 ((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
4900 (setq el-files-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4901 (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
4902 ;; 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
4903 ((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
4904 ;; 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
4905 (if
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4906 (equal "."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4907 (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
4908 ;; 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
4909 ;; 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
4910 ()
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4911 ;; 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
4912 (setq el-files-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4913 (append
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4914 (files-in-below-directory
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4915 (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
4916 el-files-list)))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4917 ;; 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
4918 ;; 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
4919 (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
4920 ;; return the filenames
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4921 el-files-list))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4922
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4923 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
4924 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
4925
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4926 Thus, on my system,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4927
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4928 (length
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4929 (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
4930
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4931 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
4932
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4933 `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
4934 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
4935 this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4936
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4937 (sort
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4938 (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
4939 'string-lessp)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4940
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4941 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4942 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
4943
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4944 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
4945 ------------------------------------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4946
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4947 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
4948 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
4949 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
4950 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
4951
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4952 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
4953 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
4954 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
4955 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
4956 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
4957 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
4958 `top-of-ranges' list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4959
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4960 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
4961 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
4962
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4963 (defvar top-of-ranges
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4964 '(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
4965 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
4966 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
4967 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
4968 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
4969 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
4970 "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
4971
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4972 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
4973
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4974 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
4975 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
4976 `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
4977
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4978 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
4979 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
4980 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
4981 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
4982 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
4983 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
4984 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
4985 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
4986
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
4987 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
4988 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
4989 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
4990 a big gear.
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 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
4993 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
4994 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
4995 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
4996 `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
4997 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
4998 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
4999
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5000 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
5001
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5002 (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
5003 (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
5004 (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
5005
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5006 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
5007 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
5008 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
5009
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5010 (while top-of-ranges
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5011 BODY-OF-LOOP...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5012 (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
5013
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5014 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
5015
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5016 (while top-of-ranges
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 ;; 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
5019 (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
5020 (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
5021 (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
5022
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5023 ;; Move to next range.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5024 (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
5025
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5026 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
5027 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
5028 `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
5029 (*Note `cons': cons.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5030
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5031 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
5032 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
5033 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
5034 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
5035 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
5036 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
5037 `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
5038 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
5039 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
5040 `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
5041 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
5042
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5043 For example,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5044
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5045 (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
5046
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5047 produces:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5048
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5049 (4 3 2 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5050
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5051 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
5052 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
5053 `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
5054 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
5055 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
5056 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
5057
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5058 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
5059
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5060 (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
5061 "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
5062 (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
5063 (number-within-range 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5064 defuns-per-range-list)
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 ;; Outer loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5067 (while top-of-ranges
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5068
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5069 ;; Inner loop.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5070 (while (and
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5071 ;; 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
5072 (car sorted-lengths)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5073 (< (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
5074
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5075 ;; 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
5076 (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
5077 (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
5078
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5079 ;; 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
5080
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5081 (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
5082 (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
5083 (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
5084
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5085 ;; Move to next range.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5086 (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
5087 ;; 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
5088 (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
5089
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5090 ;; 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
5091 ;; 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
5092 (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
5093 (cons
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5094 (length sorted-lengths)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5095 defuns-per-range-list))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5096
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5097 ;; 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
5098 ;; smallest to largest.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5099 (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
5100
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5101 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
5102 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
5103
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5104 (and (car sorted-lengths)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5105 (< (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
5106
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5107 instead of like this:
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 (< (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
5110
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5111 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
5112 `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
5113
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5114 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
5115 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
5116 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
5117 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
5118 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
5119
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5120 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
5121 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
5122 `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
5123 fail.
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 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
5126 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
5127 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
5128 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
5129 `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
5130 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
5131 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
5132 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
5133 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
5134
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5135 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
5136 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
5137
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5138 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
5139 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
5140 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
5141 `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
5142 function.
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 ;; (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
5145 (setq top-of-ranges
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5146 '(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
5147 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
5148
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5149 (setq sorted-lengths
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5150 '(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
5151
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5152 (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
5153
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5154 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
5155
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5156 (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
5157
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5158 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
5159 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
5160 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
5161 larger.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5162
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5163 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5164 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
5165
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5166 15 Readying a Graph
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5167 *******************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5168
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5169 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
5170 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
5171
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5172 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
5173 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
5174 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
5175 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
5176 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
5177
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5178 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
5179 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
5180 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
5181 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
5182 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
5183 the axes automatically.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5184
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5185 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5186
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5187 * Columns of a graph::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5188 * graph-body-print::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5189 * recursive-graph-body-print::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5190 * Printed Axes::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5191 * Line Graph Exercise::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5192
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 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
5195
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5196 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
5197 ===============================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5198
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5199 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
5200 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
5201 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
5202 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
5203 symbol a user option.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5204
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5205 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
5206 `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
5207 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
5208
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5209 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
5210 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
5211 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
5212
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5213 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
5214 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
5215
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5216 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
5217 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
5218 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
5219 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
5220
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5221 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
5222 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
5223 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
5224 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
5225 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
5226
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5227 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
5228 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
5229 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
5230 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
5231 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
5232 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
5233 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
5234 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
5235 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
5236
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5237 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
5238
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5239 insert-rectangle:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5240 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
5241 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
5242 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
5243 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
5244 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
5245 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
5246
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5247 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
5248
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5249 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
5250 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
5251 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
5252 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
5253
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5254 (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
5255 second
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5256 thirdnil
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5257
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5258 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
5259 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
5260 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
5261 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
5262 `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
5263
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5264 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
5265 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
5266 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
5267 `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
5268 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
5269 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
5270 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
5271 `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
5272 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
5273
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5274 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
5275 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
5276 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
5277 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
5278 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
5279 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
5280
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5281 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
5282 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
5283 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
5284 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
5285 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
5286 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
5287 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
5288
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5289 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
5290 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
5291 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
5292 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
5293 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
5294 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
5295 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
5296
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5297 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
5298 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
5299 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
5300 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
5301 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
5302 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
5303 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
5304 like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5305
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5306 (" " " " "*" "*" "*")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5307
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5308 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
5309 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
5310 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
5311 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
5312 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
5313 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
5314 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
5315 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
5316 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
5317 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
5318
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5319 (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
5320
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5321 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
5322 of all its arguments.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5323
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5324 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
5325 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
5326 the following expression,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5327
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5328 (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
5329
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5330 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
5331
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5332 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
5333
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5334 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
5335 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
5336 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
5337
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5338 For example,
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5339
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5340 (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
5341
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5342 returns 8.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5343
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5344 (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
5345 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
5346 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
5347 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
5348 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
5349 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
5350 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
5351 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
5352
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5353 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
5354 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
5355 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
5356
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5357 (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
5358
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5359 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
5360 `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
5361 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
5362 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
5363
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5364 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
5365 this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5366
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5367 (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
5368
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5369 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
5370 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
5371 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
5372 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
5373 insert.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5374
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5375 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
5376 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
5377 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
5378 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
5379 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
5380 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
5381
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5382 ;;; First version.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5383 (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
5384 "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
5385 (let ((insert-list nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5386 (number-of-top-blanks
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5387 (- 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
5388
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5389 ;; Fill in asterisks.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5390 (while (> actual-height 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5391 (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
5392 (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
5393
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5394 ;; Fill in blanks.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5395 (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
5396 (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
5397 (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
5398 (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
5399
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5400 ;; Return whole list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5401 insert-list))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5402
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5403 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
5404 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
5405
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5406 (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
5407
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5408 returns
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5409
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5410 (" " " " "*" "*" "*")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5411
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5412 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
5413 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
5414 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
5415 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
5416 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
5417 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
5418 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
5419 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
5420 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
5421 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
5422 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
5423 `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
5424
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5425 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
5426 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
5427
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5428 (defvar graph-symbol "*"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5429 "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
5430
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5431 (defvar graph-blank " "
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5432 "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
5433 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
5434 as graph-symbol.")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5435
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5436 (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
5437 `defvar': defvar.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5438
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5439 ;;; Second version.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5440 (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
5441 "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
5442 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
5443 of the list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5444 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
5445 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
5446
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5447 (let ((insert-list nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5448 (number-of-top-blanks
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5449 (- 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
5450
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5451 ;; Fill in `graph-symbols'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5452 (while (> actual-height 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5453 (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
5454 (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
5455
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5456 ;; Fill in `graph-blanks'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5457 (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
5458 (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
5459 (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
5460 (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
5461
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5462 ;; Return whole list.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5463 insert-list))
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 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
5466 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
5467 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
5468 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
5469 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
5470 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
5471 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
5472 `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
5473
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5474 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
5475 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
5476 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
5477 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
5478 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
5479
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5480 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
5481 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
5482 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
5483
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5484 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5485 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
5486
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5487 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
5488 ====================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5489
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5490 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
5491 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
5492 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
5493 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
5494 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
5495 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
5496 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
5497
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5498 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
5499 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
5500
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5501 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
5502 this function:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5503
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5504 (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
5505 "DOCUMENTATION..."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5506 (let ((height ...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5507 ...))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5508
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5509 (while numbers-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5510 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
5511 (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
5512
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5513 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
5514
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5515 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
5516 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
5517
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5518 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
5519 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
5520 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
5521 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
5522
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5523 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
5524 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
5525 `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
5526 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
5527 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
5528 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
5529 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
5530
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5531 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
5532 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
5533 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
5534 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
5535 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
5536 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
5537 `(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
5538 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
5539 the `let' expression.
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 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
5542
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5543 (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
5544 "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
5545 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
5546
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5547 (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
5548 (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
5549 from-position)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5550
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5551 (while numbers-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5552 (setq from-position (point))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5553 (insert-rectangle
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5554 (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
5555 (goto-char from-position)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5556 (forward-char symbol-width)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5557 ;; 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
5558 (sit-for 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5559 (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
5560 ;; 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
5561 (forward-line height)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5562 (insert "\n")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5563 ))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5564
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5565 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
5566 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
5567 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
5568 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
5569 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
5570 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
5571 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
5572
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5573 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
5574
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5575 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
5576 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
5577
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5578 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
5579
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5580 (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
5581
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5582 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
5583 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
5584
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5585 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
5586
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5587 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
5588 `C-y' (`yank)'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5589
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5590 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
5591
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5592 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
5593
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5594 *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5595 * **
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5596 * ****
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5597 *** ****
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5598 ********* *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5599 ************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5600 *************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5601
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5602 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5603 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
5604
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5605 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
5606 ==============================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5607
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5608 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
5609 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
5610 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
5611 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
5612 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
5613
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5614 The `wrapper' is uncomplicated:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5615
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5616 (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
5617 "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
5618 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
5619 (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
5620 (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
5621 from-position)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5622 (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
5623 numbers-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5624 height
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5625 symbol-width)))
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 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
5628 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
5629 `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
5630 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
5631 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
5632 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
5633 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
5634 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
5635
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5636 (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
5637 (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
5638 "Print a bar graph.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5639 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
5640
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5641 (if numbers-list
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5642 (progn
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5643 (setq from-position (point))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5644 (insert-rectangle
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5645 (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
5646 (goto-char from-position)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5647 (forward-char symbol-width)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5648 (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
5649 (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
5650 (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
5651
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5652 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
5653
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5654 (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
5655
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5656 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
5657
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5658 *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5659 ** *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5660 **** *
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5661 **** ***
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5662 * *********
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5663 ************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5664 *************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5665
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5666 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
5667 `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
5668
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5669 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5670 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
5671
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5672 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
5673 ==========================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5674
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5675 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
5676 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
5677 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
5678
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5679 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
5680 `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
5681 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
5682 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
5683 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
5684
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5685 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5686 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
5687
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5688 15.4 Exercise
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5689 =============
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5690
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5691 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
5692
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 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
5695
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5696 16 Your `.emacs' File
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 "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
5700 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
5701 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
5702 themselves.
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 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
5705 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
5706
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5707 * Menu:
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 * Default Configuration::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5710 * Site-wide Init::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5711 * defcustom::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5712 * Beginning a .emacs File::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5713 * Text and Auto-fill::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5714 * Mail Aliases::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5715 * Indent Tabs Mode::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5716 * Keybindings::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5717 * Keymaps::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5718 * Loading Files::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5719 * Autoload::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5720 * Simple Extension::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5721 * X11 Colors::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5722 * Miscellaneous::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5723 * Mode Line::
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: 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
5727
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5728 Emacs' Default Configuration
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 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
5732 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
5733 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
5734 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
5735 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
5736 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
5737 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
5738 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
5739 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
5740 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
5741
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5742 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
5743 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
5744
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5745 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
5746 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
5747 so it suits me.
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 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
5750 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
5751 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
5752
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5753 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
5754 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
5755 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
5756 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
5757
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5758 (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
5759 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
5760 `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
5761
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5762 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
5763 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
5764 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
5765
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5766 ---------- Footnotes ----------
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 (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
5769 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
5770 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
5771 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
5772 format saves typing.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5773
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5774 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5775 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
5776
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5777 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
5778 ===================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5779
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5780 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
5781 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
5782 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
5783
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5784 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
5785 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
5786 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
5787 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
5788 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
5789 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
5790 `INSTALL' file.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5791
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5792 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
5793 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
5794 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
5795 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
5796
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5797 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
5798 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
5799 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
5800 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
5801 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
5802 `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
5803
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5804 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
5805 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
5806
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5807 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
5808 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
5809 are worth perusing.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5810
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5811 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
5812 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
5813 file.
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5816 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
5817
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5818 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
5819 ===========================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5820
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5821 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
5822 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
5823 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
5824 `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
5825 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
5826
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5827 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
5828 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
5829 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
5830
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5831 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
5832 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
5833 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
5834 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
5835 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
5836
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5837 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
5838 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
5839 optional.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5840
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5841 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
5842 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
5843
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5844 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
5845 looks like this:
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 (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
5848 "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
5849 :type 'hook
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5850 :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
5851 :group 'data)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5852
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5853 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
5854 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
5855
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5856 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
5857 `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
5858 Customization buffer.
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 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
5861 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
5862 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
5863 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
5864 `: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
5865
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5866 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
5867 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
5868
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5869 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
5870 (elisp)Customization.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5871
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5872 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
5873
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5874 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
5875 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
5876
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5877 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
5878
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5879 M-x customize
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 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
5882 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
5883 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
5884 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
5885
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5886 Save for Future Sessions
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5887
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5888 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
5889 like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5890
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5891 (custom-set-variables
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5892 ;; 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
5893 ;; 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
5894 ;; 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
5895 ;; 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
5896 '(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
5897
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5898 (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
5899 `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
5900 on automatically. )
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5901
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5902 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
5903 `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
5904 `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
5905 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
5906 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
5907 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
5908
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5909 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
5910 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
5911 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
5912 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
5913 `.emacs' file itself.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5914
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5915 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
5916 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
5917 `custom-set-...' functions.
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 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
5920 that says
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5921
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5922 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
5923
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5924 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
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 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
5929 `.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
5930 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
5931 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
5932 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
5933 yourself.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5934
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5935 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
5936 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
5937 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
5938
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5939 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
5940 expressions myself.
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 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
5943 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
5944 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
5945 value set by `defconst'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5946
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5947 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5948 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
5949
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5950 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
5951 ==============================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5952
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5953 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
5954 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
5955 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
5956
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5957 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
5958 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
5959
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5960 *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
5961 description of initialization files.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5962
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5963 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
5964 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
5965
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5966 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
5967 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
5968 not.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5969
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5970 ;;;; Bob's .emacs file
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5971 ; Robert J. Chassell
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5972 ; 26 September 1985
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5973
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5974 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
5975 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
5976
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5977 ; 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
5978 ; 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
5979 ; 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
5980 ; three semicolons.
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 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
5983 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
5984 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
5985 (*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
5986
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5987 ;;;; The Help Key
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5988 ; 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
5989 ; 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
5990 ; 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
5991 ; 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
5992 ; 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
5993
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5994 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
5995
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
5996 ; 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
5997 ; 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
5998 ; 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
5999 ; control-h m.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6000
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6001 `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
6002 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
6003
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6004 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
6005 `.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
6006 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
6007 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
6008
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 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
6011
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6012 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
6013 ============================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6014
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6015 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
6016
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6017 ;;; 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
6018 ; 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
6019 ; 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
6020 ; 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
6021
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6022 (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
6023 (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
6024
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6025 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
6026 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
6027
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6028 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
6029 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
6030 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
6031
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6032 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
6033 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
6034 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
6035 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
6036 `-*- 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
6037 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
6038 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
6039 list", if any.
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 *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
6042
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6043 *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
6044
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6045 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
6046
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6047 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
6048
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6049 (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
6050
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6051 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
6052
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6053 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
6054 `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
6055 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
6056 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
6057 *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
6058 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
6059 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
6060 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
6061
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6062 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
6063
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6064 (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
6065
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6066 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
6067 variable.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6068
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6069 `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
6070 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
6071
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6072 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
6073 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
6074 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
6075
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6076 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
6077 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
6078 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
6079
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6080 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
6081 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
6082 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
6083 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
6084 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
6085
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6086 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
6087 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
6088 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
6089 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
6090
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6091 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
6092 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
6093 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
6094 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
6095 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
6096
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6097 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
6098 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
6099
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6100 (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
6101
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6102 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6103 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
6104
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6105 16.5 Mail Aliases
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6106 =================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6107
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6108 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
6109 reminders.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6110
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6111 ;;; Mail mode
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6112 ; 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
6113 ; 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
6114 ; type `M-x rmail'
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 (setq mail-aliases t)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6117
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6118 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
6119 `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
6120 aliases."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6121
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6122 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
6123 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
6124 `~/.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
6125
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6126 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
6127
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6128 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
6129 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
6130
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6131 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6132 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
6133
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6134 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
6135 =====================
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 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
6138 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
6139 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
6140 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
6141 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
6142
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6143 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
6144
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6145 ;;; Prevent Extraneous Tabs
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6146 (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
6147
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6148 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
6149 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
6150 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
6151
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6152 *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
6153
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6154 *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
6155
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6156 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6157 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
6158
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6159 16.7 Some Keybindings
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6160 =====================
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 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
6163
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6164 ;;; Compare windows
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6165 (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
6166
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6167 `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
6168 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
6169 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
6170 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
6171
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6172 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
6173
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6174 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
6175 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
6176 `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
6177 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
6178 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
6179 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
6180 `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
6181 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
6182
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6183 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
6184 `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
6185 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
6186
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6187 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
6188 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
6189 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
6190 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
6191
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6192 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
6193 `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
6194 `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
6195 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
6196 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
6197 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
6198 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
6199 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
6200 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
6201
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6202 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
6203
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6204 ;;; Keybinding for `occur'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6205 ; 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
6206 (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
6207
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6208 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
6209 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
6210 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
6211 occurrences.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6212
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6213 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
6214
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6215 ;;; Unbind `C-x f'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6216 (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
6217
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6218 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
6219 `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
6220 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
6221 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
6222 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
6223
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6224 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
6225
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6226 ;;; 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
6227 (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
6228
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6229 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
6230 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
6231 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
6232 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
6233
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6234 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6235 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
6236
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6237 16.8 Keymaps
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6238 ============
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6239
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6240 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
6241 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
6242 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
6243 `current-global-map'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6244
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6245 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
6246 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
6247 buffers.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6248
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6249 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
6250 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
6251 `buffer-menu':
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 (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
6254
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6255 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
6256 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
6257 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
6258 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
6259
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6260 (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
6261
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6262 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
6263 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
6264 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
6265 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
6266 `@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
6267 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
6268 ... @end group'.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6269
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6270 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
6271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6272 (defun texinfo-insert-@group ()
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6273 "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
6274 (interactive)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6275 (beginning-of-line)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6276 (insert "@group\n"))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6277
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6278 (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
6279 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
6280 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
6281
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6282 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
6283 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
6284 `lisp-mode.el'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6285
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6286 *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
6287 (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
6288
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6289 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6290 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
6291
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6292 16.9 Loading Files
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6293 ==================
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 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
6296 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
6297 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
6298 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
6299
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6300 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
6301 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
6302 example:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6303
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6304 (load "~/emacs/slowsplit")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6305
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6306 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
6307 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
6308 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
6309 `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
6310
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6311 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
6312 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
6313 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
6314 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
6315 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
6316 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
6317
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6318 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
6319 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
6320 `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
6321
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6322 (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
6323 (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
6324
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6325 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
6326 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
6327 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
6328 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
6329 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
6330 built.)
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 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
6333 load path:
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 ;;; Emacs Load Path
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6336 (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
6337
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6338 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
6339 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
6340
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6341 (defun load-library (library)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6342 "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
6343 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
6344 (interactive
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6345 (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
6346 'locate-file-completion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6347 (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
6348 (load library))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6349
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6350 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
6351 `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
6352 `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
6353
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6354 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
6355 `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
6356 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
6357 and this command.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6358
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6359 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6360 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
6361
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6362 16.10 Autoloading
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6363 =================
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 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
6366 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
6367 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
6368 is called "autoloading".
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6369
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6370 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
6371 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
6372
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6373 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
6374 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
6375 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
6376
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6377 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
6378 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
6379 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
6380 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
6381 `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
6382
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6383 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
6384 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
6385 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
6386 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
6387 `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
6388
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6389 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
6390 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
6391 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
6392 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
6393 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
6394 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
6395 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
6396 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
6397 (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
6398
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6399 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
6400
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6401 (autoload 'html-helper-mode
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6402 "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
6403
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6404 (`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
6405 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
6406
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6407 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
6408 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
6409 `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
6410 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
6411 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
6412 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
6413 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
6414 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
6415 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
6416
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6417 *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
6418
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6419 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6420 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
6421
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6422 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
6423 =================================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6424
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6425 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
6426 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
6427 read.
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 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
6430 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
6431 file.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6432
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6433 Here is the definition:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6434
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6435 ;;; 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
6436 ;;; 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
6437 (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
6438 "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
6439 (interactive)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6440 (recenter 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6441
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6442 Now for the keybinding.
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 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
6445 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
6446 (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
6447 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
6448
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6449 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
6450
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6451 (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
6452
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6453 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
6454 (emacs)Init Rebinding.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6455
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6456 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
6457 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
6458 following conditional:
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 (cond
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6461 ((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
6462 ;; evaluate version 21 code
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 ((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
6465 ;; evaluate version 22 code
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6466 ( ... )))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6467
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6468 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
6469 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
6470 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
6471
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6472 (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
6473 (progn
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6474 (blink-cursor-mode 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6475 ;; 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
6476 ;; 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
6477 (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
6478 ;; Turn on image viewing
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6479 (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
6480 ;; 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
6481 ;; (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
6482 (menu-bar-mode 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6483 ;; 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
6484 ;; (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
6485 (tool-bar-mode nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6486 ;; 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
6487 ;; (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
6488 ;; (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
6489 (tooltip-mode nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6490 ;; 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
6491 (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
6492 ))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6493
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6494 (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
6495 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
6496 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
6497 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
6498 `(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
6499 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
6500 function will be needed.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6501
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6502 ---------- Footnotes ----------
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 (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
6505 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
6506
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6507 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
6508
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6509 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
6510
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6511 emacs -Q - D
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6512
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6513 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6514 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
6515
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6516 16.12 X11 Colors
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6517 ================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6518
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6519 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
6520 system.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6521
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6522 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
6523
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6524 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
6525
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6526 ;; Set cursor color
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6527 (set-cursor-color "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6528
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6529 ;; Set mouse color
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6530 (set-mouse-color "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6531
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6532 ;; Set foreground and background
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6533 (set-foreground-color "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6534 (set-background-color "darkblue")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6535
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6536 ;;; 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
6537 (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
6538 (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
6539
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6540 (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
6541 (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
6542
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6543 (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
6544 (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
6545
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6546 ;; Set calendar highlighting colors
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6547 (setq calendar-load-hook
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6548 '(lambda ()
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6549 (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
6550 (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
6551 (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
6552
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6553 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
6554 screen flicker.
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 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
6557 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
6558 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
6559 `~/.Xresources' file like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6560
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6561 Emacs*foreground: white
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6562 Emacs*background: darkblue
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6563 Emacs*cursorColor: white
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6564 Emacs*pointerColor: white
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 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
6567 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
6568
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6569 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
6570
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6571 ---------- Footnotes ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6572
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6573 (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
6574 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
6575 plain color.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6576
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6577 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6578 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
6579
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6580 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
6581 ================================================
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 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
6584
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6585 - 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
6586
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6587 ; 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
6588 ; `/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
6589 ; 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
6590 ; 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
6591
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6592 (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
6593 "*emacs*mpointer")))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6594 ;; 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
6595 ;; 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
6596 (if (eq mpointer nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6597 (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
6598 (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
6599 (set-mouse-color "white"))
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 - 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
6602 like this:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6603
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6604 (setq-default
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6605 default-frame-alist
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6606 '((cursor-color . "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6607 (mouse-color . "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6608 (foreground-color . "white")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6609 (background-color . "DodgerBlue4")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6610 ;; (cursor-type . bar)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6611 (cursor-type . box)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6612 (tool-bar-lines . 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6613 (menu-bar-lines . 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6614 (width . 80)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6615 (height . 58)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6616 (font .
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6617 "-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
6618 ))
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 - 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
6621 (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
6622 problem recently.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6623
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6624 ;; 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
6625 ; (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
6626
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6627 ;; 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
6628 (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
6629
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6630 - 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
6631
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6632 (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
6633 (blink-cursor-mode -1))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6634
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6635 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
6636
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6637 - 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
6638 `-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
6639 `-i' Ignore case distinctions
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6640 `-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
6641
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6642 (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
6643
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6644 - 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
6645 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
6646
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6647 (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
6648
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6649 - 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
6650
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6651 (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
6652 ;; 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
6653 ;; 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
6654 (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
6655
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6656 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
6657 instead:
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 (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
6660 (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
6661
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6662 Fixing Unpleasant Key Bindings
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6665 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
6666 <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
6667 the home row.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6668
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6669 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
6670 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
6671 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
6672 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
6673 `.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
6674
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6675 For a boot script:
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 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
6678
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6679 or
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6680
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6681 install-keymap emacs2
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6682
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6683 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
6684 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
6685
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6686 # 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
6687 # (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
6688 # 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
6689
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6690 xmodmap -e "clear Lock"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6691 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
6692
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6693 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
6694 <META> key:
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 # 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
6697 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
6698
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6699 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6700 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
6701
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6702 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
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 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
6706
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6707 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
6708 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
6709
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6710 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
6711
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6712 -:-- 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
6713
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6714 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
6715 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
6716 top of the buffer.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6717
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6718 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
6719
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6720 ;; 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
6721 ;; 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
6722 (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
6723 (quote
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6724 (#("-" 0 1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6725 (help-echo
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6726 "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
6727 mode-line-mule-info
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6728 mode-line-modified
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6729 mode-line-frame-identification
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6730 " "
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6731 mode-line-buffer-identification
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 (:eval (substring
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6734 (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
6735 ":"
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6736 default-directory
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6737 #(" " 0 1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6738 (help-echo
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6739 "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
6740 (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
6741 global-mode-string
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6742 #(" %[(" 0 6
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6743 (help-echo
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6744 "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
6745 (: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
6746 mode-line-process
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6747 minor-mode-alist
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6748 #("%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
6749 ")%] "
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6750 (-3 . "%P")
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 )))
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 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
6755 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
6756 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
6757
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6758 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
6759 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
6760 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
6761 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
6762
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6763 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
6764 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
6765 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
6766 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
6767 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
6768 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
6769 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
6770
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6771 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
6772
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6773 #("-" 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
6774
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6775 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
6776 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
6777 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
6778 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
6779 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
6780 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
6781 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
6782 `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
6783 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
6784
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6785 *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
6786 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
6787
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6788 `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
6789 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
6790
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6791 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
6792 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
6793 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
6794 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
6795 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
6796 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
6797
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6798 `:eval' was a new feature in GNU Emacs version 21. It says to evaluate
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6799 the following form and use the result as a string to display. In this
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6800 case, the expression displays the first component of the full system
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6801 name. The end of the first component is a `.' (`period'), so I use the
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6802 `string-match' function to tell me the length of the first component.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6803 The substring from the zeroth character to that length is the name of
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6804 the machine.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6805
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6806 This is the expression:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6807
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6808 (:eval (substring
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6809 (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
6810
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6811 `%[' 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
6812 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
6813 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
6814 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
6815 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
6816 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
6817
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6818 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
6819 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
6820 a default Emacs.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6821
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6822 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
6823 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
6824
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6825 emacs -q
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6826
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6827 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
6828 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
6829
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 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
6832
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6833 17 Debugging
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6834 ************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6835
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6836 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
6837 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
6838 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
6839
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6840 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
6841 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
6842 short example of each.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6843
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6844 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6845
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6846 * debug::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6847 * debug-on-entry::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6848 * debug-on-quit::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6849 * edebug::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6850 * Debugging Exercises::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6851
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6852 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6853 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
6854
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6855 17.1 `debug'
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6858 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
6859 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
6860 `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
6861 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
6862
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6863 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
6864 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
6865
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6866 (defun triangle-bugged (number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6867 "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
6868 (let ((total 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6869 (while (> number 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6870 (setq total (+ total number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6871 (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
6872 total))
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 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
6875 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
6876 area.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6877
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6878 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
6879
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6880 (triangle-bugged 4)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6881
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6882 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
6883 buffer that says:
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
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6886 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6887 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
6888 (1= number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6889 (setq number (1= number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6890 (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
6891 (setq number (1= number)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6892 (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
6893 (setq number ...)) total)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6894 triangle-bugged(4)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6895 eval((triangle-bugged 4))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6896 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
6897 eval-last-sexp(nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6898 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
6899 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6900
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6901 (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
6902 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
6903 `*Backtrace*' buffer.)
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 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
6906 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
6907 `1=' is `void'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6908
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6909 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
6910 read the complete backtrace.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6911
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6912 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
6913 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
6914 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
6915
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6916 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
6917 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
6918 `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
6919 `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
6920 interpreter evaluated next.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6921
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6922 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
6923
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6924 (setq number (1= number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6925
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6926 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
6927 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
6928
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6929 (1= number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6930
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6931 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
6932
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6933 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
6934
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6935 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
6936 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
6937
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6938 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6939 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
6940
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6941 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
6942 =====================
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 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
6945 an error.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6946
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6947 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
6948 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
6949 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
6950
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6951 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
6952 `debug-on-entry'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6953
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6954 Type:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6955
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6956 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
6957
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6958 Now, evaluate the following:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6959
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6960 (triangle-bugged 5)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6961
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6962 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
6963 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
6964
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6965 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6966 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
6967 * triangle-bugged(5)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6968 eval((triangle-bugged 5))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6969 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
6970 eval-last-sexp(nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6971 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
6972 ---------- Buffer: *Backtrace* ----------
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 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
6975 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
6976
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6977 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6978 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
6979 * (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
6980 (setq number ...)) total)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6981 * triangle-bugged(5)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6982 eval((triangle-bugged 5))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6983 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
6984 eval-last-sexp(nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6985 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
6986 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6987
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6988 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
6989 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
6990
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6991 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
6992
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6993 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6994 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
6995 * (setq number (1= number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6996 * (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
6997 (setq number (1= number)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
6998 * (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
6999 (setq number ...)) total)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7000 * triangle-bugged(5)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7001 eval((triangle-bugged 5))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7002 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
7003 eval-last-sexp(nil)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7004 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
7005 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7006
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7007 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
7008 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
7009
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7010 ---------- Buffer: *Backtrace* ----------
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7011 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
7012 * (1= number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7013 ...
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7014 ---------- Buffer: *Backtrace* ----------
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 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
7017
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7018 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
7019 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
7020
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7021 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
7022 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
7023
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7024 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
7025
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7026 (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
7027
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7028 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7029 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
7030
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7031 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
7032 ==================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7033
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7034 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
7035 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
7036
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7037 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
7038 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
7039 debugging infinite loops.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7040
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7041 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
7042 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
7043
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7044 (defun triangle-bugged (number)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7045 "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
7046 (let ((total 0))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7047 (while (> number 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7048 (setq total (+ total number))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7049 (debug) ; Start debugger.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7050 (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
7051 total))
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 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
7054 (elisp)Debugger.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7055
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 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
7058
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7059 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
7060 =======================================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7061
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7062 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
7063 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
7064 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
7065
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7066 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
7067 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
7068
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7069 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
7070
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7071 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
7072 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
7073 review of it.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7074
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7075 (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
7076 "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
7077 Uses recursion."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7078 (if (= number 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7079 1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7080 (+ number
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7081 (triangle-recursively-bugged
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7082 (1= number))))) ; Error here.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7083
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7084 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
7085 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
7086 (`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
7087 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
7088 `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
7089 Interactive mode.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7090
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7091 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
7092 "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
7093 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
7094
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7095 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
7096
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7097 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
7098 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
7099
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7100 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
7101 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
7102
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7103 (triangle-recursively-bugged 3)
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 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
7106 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
7107 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
7108 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
7109 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
7110 `=>'; 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
7111 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
7112
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7113 =>-!-(if (= number 1)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7114
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7115 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
7116 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
7117
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7118 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
7119 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
7120
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7121 =>(if -!-(= number 1)
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 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
7124 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
7125 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
7126 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
7127
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7128 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
7129
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7130 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
7131 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
7132 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
7133
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7134 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
7135 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
7136
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7137 => -!-(1= number))))) ; Error here.
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 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
7140 says:
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 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
7143
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7144 This is the bug.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7145
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7146 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
7147
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7148 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
7149 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
7150 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
7151 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
7152
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7153 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
7154 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
7155 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
7156 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
7157 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
7158
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7159 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
7160
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 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
7163
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7164 17.5 Debugging Exercises
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7165 ========================
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 * 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
7168 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
7169 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
7170 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
7171 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
7172 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
7173
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7174 * 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
7175 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
7176 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
7177 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
7178 completes without problems.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7179
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7180 * 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
7181 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
7182 `<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
7183 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
7184
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7185 * 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
7186 (`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
7187 `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
7188
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7189 * 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
7190 `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
7191
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7192 * 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
7193 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
7194 `edebug-Trace-fast-mode'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7195
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7196 * 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
7197 the stopping point.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7198
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 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
7201
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7202 18 Conclusion
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7203 *************
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 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
7206 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
7207 `.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
7208 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
7209
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7210 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
7211 teach yourself.
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 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
7214 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
7215 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
7216
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7217 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
7218 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
7219
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7220 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
7221 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
7222 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
7223
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7224 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
7225 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
7226 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
7227 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
7228 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
7229 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
7230
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7231 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
7232 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
7233 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
7234
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7235 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
7236 `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
7237 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
7238 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
7239 example, looks complicated.
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 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
7242 `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
7243 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
7244 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
7245 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
7246 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
7247
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7248 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
7249 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
7250 `insert-and-inherit'.
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 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
7253 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
7254 Review: Review.)
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 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
7257 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
7258 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
7259
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7260 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
7261 `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
7262 `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
7263 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
7264 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
7265
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7266 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
7267 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
7268 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
7269 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
7270 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
7271
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7272 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
7273 `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
7274 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
7275
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7276 (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
7277 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
7278 `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
7279
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7280 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
7281 `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
7282 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
7283 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
7284 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
7285
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7286 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
7287
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7288 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
7289 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
7290 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
7291 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
7292 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
7293 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
7294 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
7295 building.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7296
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7297 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
7298 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
7299 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
7300 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
7301 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
7302 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
7303 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
7304
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7305 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
7306 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
7307 beginning.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7308
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7309 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7310 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
7311
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7312 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
7313 *********************************
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 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
7316 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
7317 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
7318 words, `the-the'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7319
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7320 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
7321 search for duplicates:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7322
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7323 \\(\\w+[ \t\n]+\\)\\1
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 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
7326 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
7327 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
7328 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
7329 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
7330 *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
7331 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
7332 Expressions: (elisp)Regular Expressions.)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7333
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7334 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
7335 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
7336 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
7337
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7338 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
7339 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
7340 `\\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
7341 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
7342
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7343 \\(\\(\\w+\\)\\W*\\)\\1
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7344
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7345 Again, not useful.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7346
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7347 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
7348 `\\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
7349 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
7350 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
7351
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7352 \\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
7353
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7354 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
7355 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
7356
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7357 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
7358 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
7359
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7360 (defun the-the ()
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7361 "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
7362 (interactive)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7363 (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
7364 (push-mark)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7365 ;; 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
7366 ;; 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
7367 (if (re-search-forward
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7368 "\\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
7369 (message "Found duplicated word.")
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7370 (message "End of buffer")))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7371
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7372 ;; 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
7373 (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
7374
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7375
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7376 Here is test text:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7377
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7378 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
7379 five six seven
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7380
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7381 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
7382 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
7383
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7384 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7385 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
7386
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7387 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
7388 *********************************
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7389
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7390 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
7391 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
7392 the `current-kill' function.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7393
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7394 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
7395 `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
7396 the kill ring.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7397
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7398 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
7399 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
7400 evaluate the following:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7401
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7402 (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
7403 (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
7404
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7405 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
7406 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
7407 with `M-w'.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7408
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7409 (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
7410 `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
7411 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
7412 `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
7413 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
7414 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
7415 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
7416
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7417 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
7418 the kill ring:
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 first some text
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7421 second piece of text
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7422 third line
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7423 fourth line of text
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7424 fifth bit of text
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 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
7427
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7428 kill-ring
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7429
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7430 It is:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7431
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7432 ("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
7433 "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
7434
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7435 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
7436
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7437 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
7438
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7439 (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
7440
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7441 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7442
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7443 * current-kill::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7444 * yank::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7445 * yank-pop::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7446 * ring file::
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 
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7449 File: eintr, Node: current-kill, Next: yank, Prev: Kill Ring, Up: Kill Ring
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7450
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7451 B.1 The `current-kill' Function
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7452 ===============================
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7453
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7454 The `current-kill' function changes the element in the kill ring to
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7455 which `kill-ring-yank-pointer' points. (Also, the `kill-new' function
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7456 sets `kill-ring-yank-pointer' to point to the latest element of the the
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7457 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 The `current-kill' function is used by `yank' and by `yank-pop'. Here
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7460 is the code for `current-kill':
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7461
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7462 (defun current-kill (n &optional do-not-move)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7463 "Rotate the yanking point by N places, and then return that kill.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7464 If N is zero, `interprogram-paste-function' is set, and calling it
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7465 returns a string, then that string is added to the front of the
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7466 kill ring and returned as the latest kill.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7467 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7468 yanking point; just return the Nth kill forward."
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7469 (let ((interprogram-paste (and (= n 0)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7470 interprogram-paste-function
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7471 (funcall interprogram-paste-function))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7472 (if interprogram-paste
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7473 (progn
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7474 ;; Disable the interprogram cut function when we add the new
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7475 ;; text to the kill ring, so Emacs doesn't try to own the
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7476 ;; selection, with identical text.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7477 (let ((interprogram-cut-function nil))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7478 (kill-new interprogram-paste))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7479 interprogram-paste)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7480 (or kill-ring (error "Kill ring is empty"))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7481 (let ((ARGth-kill-element
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7482 (nthcdr (mod (- n (length kill-ring-yank-pointer))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7483 (length kill-ring))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7484 kill-ring)))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7485 (or do-not-move
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7486 (setq kill-ring-yank-pointer ARGth-kill-element))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7487 (car ARGth-kill-element)))))
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7488
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7489 In addition, the `kill-new' function sets `kill-ring-yank-pointer' to
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7490 the latest element of the the kill ring. And indirectly so does
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7491 `kill-append', since it calls `kill-new'. In addition, `kill-region'
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7492 and `kill-line' call the `kill-new' function.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7493
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7494 Here is the line in `kill-new', which is explained in *Note The
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7495 `kill-new' function: kill-new function.
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7496
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7497 (setq kill-ring-yank-pointer kill-ring)
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7498
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7499 * Menu:
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7500
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7501 * Understanding current-kill::
b214bd8be620 info/eintr-2: Updated Info file to Third Edition for
Robert J. Chassell <bob@rattlesnake.com>
parents:
diff changeset
7502