aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispintro
diff options
context:
space:
mode:
authorStefan Monnier2010-12-10 19:13:08 -0500
committerStefan Monnier2010-12-10 19:13:08 -0500
commit2c302df3a13236bfbf8ea1b771d13618fcda8d71 (patch)
treef26dc9f22861dc37610de319d05255de058c221b /doc/lispintro
parent0c747cb143fa227e78f350ac353d703f489209df (diff)
parent175069efeb080517afefdd44a06f7a779ea8c25c (diff)
downloademacs-2c302df3a13236bfbf8ea1b771d13618fcda8d71.tar.gz
emacs-2c302df3a13236bfbf8ea1b771d13618fcda8d71.zip
Merge from trunk
Diffstat (limited to 'doc/lispintro')
-rw-r--r--doc/lispintro/ChangeLog5
-rw-r--r--doc/lispintro/emacs-lisp-intro.texi147
2 files changed, 80 insertions, 72 deletions
diff --git a/doc/lispintro/ChangeLog b/doc/lispintro/ChangeLog
index 101e5b1d8b7..d75bb003279 100644
--- a/doc/lispintro/ChangeLog
+++ b/doc/lispintro/ChangeLog
@@ -1,3 +1,8 @@
12010-11-13 Glenn Morris <rgm@gnu.org>
2
3 * emacs-lisp-intro.texi: Rename the `count-words-region' example,
4 since there is now a standard command of that name.
5
12010-10-11 Glenn Morris <rgm@gnu.org> 62010-10-11 Glenn Morris <rgm@gnu.org>
2 7
3 * Makefile.in (.dvi.ps): Remove unnecessary suffix rule. 8 * Makefile.in (.dvi.ps): Remove unnecessary suffix rule.
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi
index dfba68cc911..50b11a62fa0 100644
--- a/doc/lispintro/emacs-lisp-intro.texi
+++ b/doc/lispintro/emacs-lisp-intro.texi
@@ -704,23 +704,25 @@ Regular Expression Searches
704* fwd-para while:: The forward motion @code{while} loop. 704* fwd-para while:: The forward motion @code{while} loop.
705 705
706Counting: Repetition and Regexps 706Counting: Repetition and Regexps
707@set COUNT-WORDS count-words-example
708@c Length of variable name chosen so that things still line up when expanded.
707 709
708* Why Count Words:: 710* Why Count Words::
709* count-words-region:: Use a regexp, but find a problem. 711* @value{COUNT-WORDS}:: Use a regexp, but find a problem.
710* recursive-count-words:: Start with case of no words in region. 712* recursive-count-words:: Start with case of no words in region.
711* Counting Exercise:: 713* Counting Exercise::
712 714
713The @code{count-words-region} Function 715The @code{@value{COUNT-WORDS}} Function
714 716
715* Design count-words-region:: The definition using a @code{while} loop. 717* Design @value{COUNT-WORDS}:: The definition using a @code{while} loop.
716* Whitespace Bug:: The Whitespace Bug in @code{count-words-region}. 718* Whitespace Bug:: The Whitespace Bug in @code{@value{COUNT-WORDS}}.
717 719
718Counting Words in a @code{defun} 720Counting Words in a @code{defun}
719 721
720* Divide and Conquer:: 722* Divide and Conquer::
721* Words and Symbols:: What to count? 723* Words and Symbols:: What to count?
722* Syntax:: What constitutes a word or symbol? 724* Syntax:: What constitutes a word or symbol?
723* count-words-in-defun:: Very like @code{count-words}. 725* count-words-in-defun:: Very like @code{@value{COUNT-WORDS}}.
724* Several defuns:: Counting several defuns in a file. 726* Several defuns:: Counting several defuns in a file.
725* Find a File:: Do you want to look at a file? 727* Find a File:: Do you want to look at a file?
726* lengths-list-file:: A list of the lengths of many definitions. 728* lengths-list-file:: A list of the lengths of many definitions.
@@ -13829,35 +13831,37 @@ word count commands using @code{while} loops and recursion.
13829 13831
13830@menu 13832@menu
13831* Why Count Words:: 13833* Why Count Words::
13832* count-words-region:: Use a regexp, but find a problem. 13834* @value{COUNT-WORDS}:: Use a regexp, but find a problem.
13833* recursive-count-words:: Start with case of no words in region. 13835* recursive-count-words:: Start with case of no words in region.
13834* Counting Exercise:: 13836* Counting Exercise::
13835@end menu 13837@end menu
13836 13838
13837@node Why Count Words, count-words-region, Counting Words, Counting Words 13839@node Why Count Words, @value{COUNT-WORDS}, Counting Words, Counting Words
13838@ifnottex 13840@ifnottex
13839@unnumberedsec Counting words 13841@unnumberedsec Counting words
13840@end ifnottex 13842@end ifnottex
13841 13843
13842The standard Emacs distribution contains a function for counting the 13844The standard Emacs distribution contains functions for counting the
13843number of lines within a region. However, there is no corresponding 13845number of lines and words within a region.
13844function for counting words.
13845 13846
13846Certain types of writing ask you to count words. Thus, if you write 13847Certain types of writing ask you to count words. Thus, if you write
13847an essay, you may be limited to 800 words; if you write a novel, you 13848an essay, you may be limited to 800 words; if you write a novel, you
13848may discipline yourself to write 1000 words a day. It seems odd to me 13849may discipline yourself to write 1000 words a day. It seems odd, but
13849that Emacs lacks a word count command. Perhaps people use Emacs 13850for a long time, Emacs lacked a word count command. Perhaps people used
13850mostly for code or types of documentation that do not require word 13851Emacs mostly for code or types of documentation that did not require
13851counts; or perhaps they restrict themselves to the operating system 13852word counts; or perhaps they restricted themselves to the operating
13852word count command, @code{wc}. Alternatively, people may follow 13853system word count command, @code{wc}. Alternatively, people may have
13853the publishers' convention and compute a word count by dividing the 13854followed the publishers' convention and computed a word count by
13854number of characters in a document by five. In any event, here are 13855dividing the number of characters in a document by five.
13855commands to count words. 13856
13856 13857There are many ways to implement a command to count words. Here are
13857@node count-words-region, recursive-count-words, Why Count Words, Counting Words 13858some examples, which you may wish to compare with the standard Emacs
13859command, @code{count-words-region}.
13860
13861@node @value{COUNT-WORDS}, recursive-count-words, Why Count Words, Counting Words
13858@comment node-name, next, previous, up 13862@comment node-name, next, previous, up
13859@section The @code{count-words-region} Function 13863@section The @code{@value{COUNT-WORDS}} Function
13860@findex count-words-region 13864@findex @value{COUNT-WORDS}
13861 13865
13862A word count command could count words in a line, paragraph, region, 13866A word count command could count words in a line, paragraph, region,
13863or buffer. What should the command cover? You could design the 13867or buffer. What should the command cover? You could design the
@@ -13865,7 +13869,7 @@ command to count the number of words in a complete buffer. However,
13865the Emacs tradition encourages flexibility---you may want to count 13869the Emacs tradition encourages flexibility---you may want to count
13866words in just a section, rather than all of a buffer. So it makes 13870words in just a section, rather than all of a buffer. So it makes
13867more sense to design the command to count the number of words in a 13871more sense to design the command to count the number of words in a
13868region. Once you have a @code{count-words-region} command, you can, 13872region. Once you have a command to count words in a region, you can,
13869if you wish, count words in a whole buffer by marking it with 13873if you wish, count words in a whole buffer by marking it with
13870@w{@kbd{C-x h}} (@code{mark-whole-buffer}). 13874@w{@kbd{C-x h}} (@code{mark-whole-buffer}).
13871 13875
@@ -13876,13 +13880,13 @@ region. This means that word counting is ideally suited to recursion
13876or to a @code{while} loop. 13880or to a @code{while} loop.
13877 13881
13878@menu 13882@menu
13879* Design count-words-region:: The definition using a @code{while} loop. 13883* Design @value{COUNT-WORDS}:: The definition using a @code{while} loop.
13880* Whitespace Bug:: The Whitespace Bug in @code{count-words-region}. 13884* Whitespace Bug:: The Whitespace Bug in @code{@value{COUNT-WORDS}}.
13881@end menu 13885@end menu
13882 13886
13883@node Design count-words-region, Whitespace Bug, count-words-region, count-words-region 13887@node Design @value{COUNT-WORDS}, Whitespace Bug, @value{COUNT-WORDS}, @value{COUNT-WORDS}
13884@ifnottex 13888@ifnottex
13885@unnumberedsubsec Designing @code{count-words-region} 13889@unnumberedsubsec Designing @code{@value{COUNT-WORDS}}
13886@end ifnottex 13890@end ifnottex
13887 13891
13888First, we will implement the word count command with a @code{while} 13892First, we will implement the word count command with a @code{while}
@@ -13905,7 +13909,9 @@ What we need to do is fill in the slots.
13905 13909
13906The name of the function should be self-explanatory and similar to the 13910The name of the function should be self-explanatory and similar to the
13907existing @code{count-lines-region} name. This makes the name easier 13911existing @code{count-lines-region} name. This makes the name easier
13908to remember. @code{count-words-region} is a good choice. 13912to remember. @code{count-words-region} is the obvious choice. Since
13913that name is now used for the standard Emacs command to count words, we
13914will name our implementation @code{@value{COUNT-WORDS}}.
13909 13915
13910The function counts words within a region. This means that the 13916The function counts words within a region. This means that the
13911argument list must contain symbols that are bound to the two 13917argument list must contain symbols that are bound to the two
@@ -13923,7 +13929,7 @@ first, to set up conditions under which the @code{while} loop can
13923count words, second, to run the @code{while} loop, and third, to send 13929count words, second, to run the @code{while} loop, and third, to send
13924a message to the user. 13930a message to the user.
13925 13931
13926When a user calls @code{count-words-region}, point may be at the 13932When a user calls @code{@value{COUNT-WORDS}}, point may be at the
13927beginning or the end of the region. However, the counting process 13933beginning or the end of the region. However, the counting process
13928must start at the beginning of the region. This means we will want 13934must start at the beginning of the region. This means we will want
13929to put point there if it is not already there. Executing 13935to put point there if it is not already there. Executing
@@ -14015,7 +14021,7 @@ All this leads to the following function definition:
14015@smallexample 14021@smallexample
14016@group 14022@group
14017;;; @r{First version; has bugs!} 14023;;; @r{First version; has bugs!}
14018(defun count-words-region (beginning end) 14024(defun @value{COUNT-WORDS} (beginning end)
14019 "Print number of words in the region. 14025 "Print number of words in the region.
14020Words are defined as at least one word-constituent 14026Words are defined as at least one word-constituent
14021character followed by at least one character that 14027character followed by at least one character that
@@ -14056,14 +14062,14 @@ table determines which characters these are."
14056@noindent 14062@noindent
14057As written, the function works, but not in all circumstances. 14063As written, the function works, but not in all circumstances.
14058 14064
14059@node Whitespace Bug, , Design count-words-region, count-words-region 14065@node Whitespace Bug, , Design @value{COUNT-WORDS}, @value{COUNT-WORDS}
14060@comment node-name, next, previous, up 14066@comment node-name, next, previous, up
14061@subsection The Whitespace Bug in @code{count-words-region} 14067@subsection The Whitespace Bug in @code{@value{COUNT-WORDS}}
14062 14068
14063The @code{count-words-region} command described in the preceding 14069The @code{@value{COUNT-WORDS}} command described in the preceding
14064section has two bugs, or rather, one bug with two manifestations. 14070section has two bugs, or rather, one bug with two manifestations.
14065First, if you mark a region containing only whitespace in the middle 14071First, if you mark a region containing only whitespace in the middle
14066of some text, the @code{count-words-region} command tells you that the 14072of some text, the @code{@value{COUNT-WORDS}} command tells you that the
14067region contains one word! Second, if you mark a region containing 14073region contains one word! Second, if you mark a region containing
14068only whitespace at the end of the buffer or the accessible portion of 14074only whitespace at the end of the buffer or the accessible portion of
14069a narrowed buffer, the command displays an error message that looks 14075a narrowed buffer, the command displays an error message that looks
@@ -14084,7 +14090,7 @@ parenthesis and type @kbd{C-x C-e} to install it.
14084@smallexample 14090@smallexample
14085@group 14091@group
14086;; @r{First version; has bugs!} 14092;; @r{First version; has bugs!}
14087(defun count-words-region (beginning end) 14093(defun @value{COUNT-WORDS} (beginning end)
14088 "Print number of words in the region. 14094 "Print number of words in the region.
14089Words are defined as at least one word-constituent character followed 14095Words are defined as at least one word-constituent character followed
14090by at least one character that is not a word-constituent. The buffer's 14096by at least one character that is not a word-constituent. The buffer's
@@ -14123,12 +14129,12 @@ syntax table determines which characters these are."
14123If you wish, you can also install this keybinding by evaluating it: 14129If you wish, you can also install this keybinding by evaluating it:
14124 14130
14125@smallexample 14131@smallexample
14126(global-set-key "\C-c=" 'count-words-region) 14132(global-set-key "\C-c=" '@value{COUNT-WORDS})
14127@end smallexample 14133@end smallexample
14128 14134
14129To conduct the first test, set mark and point to the beginning and end 14135To conduct the first test, set mark and point to the beginning and end
14130of the following line and then type @kbd{C-c =} (or @kbd{M-x 14136of the following line and then type @kbd{C-c =} (or @kbd{M-x
14131count-words-region} if you have not bound @kbd{C-c =}): 14137@value{COUNT-WORDS}} if you have not bound @kbd{C-c =}):
14132 14138
14133@smallexample 14139@smallexample
14134 one two three 14140 one two three
@@ -14139,7 +14145,7 @@ Emacs will tell you, correctly, that the region has three words.
14139 14145
14140Repeat the test, but place mark at the beginning of the line and place 14146Repeat the test, but place mark at the beginning of the line and place
14141point just @emph{before} the word @samp{one}. Again type the command 14147point just @emph{before} the word @samp{one}. Again type the command
14142@kbd{C-c =} (or @kbd{M-x count-words-region}). Emacs should tell you 14148@kbd{C-c =} (or @kbd{M-x @value{COUNT-WORDS}}). Emacs should tell you
14143that the region has no words, since it is composed only of the 14149that the region has no words, since it is composed only of the
14144whitespace at the beginning of the line. But instead Emacs tells you 14150whitespace at the beginning of the line. But instead Emacs tells you
14145that the region has one word! 14151that the region has one word!
@@ -14148,7 +14154,7 @@ For the third test, copy the sample line to the end of the
14148@file{*scratch*} buffer and then type several spaces at the end of the 14154@file{*scratch*} buffer and then type several spaces at the end of the
14149line. Place mark right after the word @samp{three} and point at the 14155line. Place mark right after the word @samp{three} and point at the
14150end of line. (The end of the line will be the end of the buffer.) 14156end of line. (The end of the line will be the end of the buffer.)
14151Type @kbd{C-c =} (or @kbd{M-x count-words-region}) as you did before. 14157Type @kbd{C-c =} (or @kbd{M-x @value{COUNT-WORDS}}) as you did before.
14152Again, Emacs should tell you that the region has no words, since it is 14158Again, Emacs should tell you that the region has no words, since it is
14153composed only of the whitespace at the end of the line. Instead, 14159composed only of the whitespace at the end of the line. Instead,
14154Emacs displays an error message saying @samp{Search failed}. 14160Emacs displays an error message saying @samp{Search failed}.
@@ -14157,7 +14163,7 @@ The two bugs stem from the same problem.
14157 14163
14158Consider the first manifestation of the bug, in which the command 14164Consider the first manifestation of the bug, in which the command
14159tells you that the whitespace at the beginning of the line contains 14165tells you that the whitespace at the beginning of the line contains
14160one word. What happens is this: The @code{M-x count-words-region} 14166one word. What happens is this: The @code{M-x @value{COUNT-WORDS}}
14161command moves point to the beginning of the region. The @code{while} 14167command moves point to the beginning of the region. The @code{while}
14162tests whether the value of point is smaller than the value of 14168tests whether the value of point is smaller than the value of
14163@code{end}, which it is. Consequently, the regular expression search 14169@code{end}, which it is. Consequently, the regular expression search
@@ -14191,7 +14197,7 @@ an error if the search fails. The optional fourth argument is a
14191repeat count. (In Emacs, you can see a function's documentation by 14197repeat count. (In Emacs, you can see a function's documentation by
14192typing @kbd{C-h f}, the name of the function, and then @key{RET}.) 14198typing @kbd{C-h f}, the name of the function, and then @key{RET}.)
14193 14199
14194In the @code{count-words-region} definition, the value of the end of 14200In the @code{@value{COUNT-WORDS}} definition, the value of the end of
14195the region is held by the variable @code{end} which is passed as an 14201the region is held by the variable @code{end} which is passed as an
14196argument to the function. Thus, we can add @code{end} as an argument 14202argument to the function. Thus, we can add @code{end} as an argument
14197to the regular expression search expression: 14203to the regular expression search expression:
@@ -14200,7 +14206,7 @@ to the regular expression search expression:
14200(re-search-forward "\\w+\\W*" end) 14206(re-search-forward "\\w+\\W*" end)
14201@end smallexample 14207@end smallexample
14202 14208
14203However, if you make only this change to the @code{count-words-region} 14209However, if you make only this change to the @code{@value{COUNT-WORDS}}
14204definition and then test the new version of the definition on a 14210definition and then test the new version of the definition on a
14205stretch of whitespace, you will receive an error message saying 14211stretch of whitespace, you will receive an error message saying
14206@samp{Search failed}. 14212@samp{Search failed}.
@@ -14231,7 +14237,7 @@ true-or-false-test tests true because the value of point is still less
14231than the value of end, since the @code{re-search-forward} expression 14237than the value of end, since the @code{re-search-forward} expression
14232did not move point. @dots{} and the cycle repeats @dots{} 14238did not move point. @dots{} and the cycle repeats @dots{}
14233 14239
14234The @code{count-words-region} definition requires yet another 14240The @code{@value{COUNT-WORDS}} definition requires yet another
14235modification, to cause the true-or-false-test of the @code{while} loop 14241modification, to cause the true-or-false-test of the @code{while} loop
14236to test false if the search fails. Put another way, there are two 14242to test false if the search fails. Put another way, there are two
14237conditions that must be satisfied in the true-or-false-test before the 14243conditions that must be satisfied in the true-or-false-test before the
@@ -14265,17 +14271,17 @@ succeeds and as a side effect moves point. Consequently, as words are
14265found, point is moved through the region. When the search expression 14271found, point is moved through the region. When the search expression
14266fails to find another word, or when point reaches the end of the 14272fails to find another word, or when point reaches the end of the
14267region, the true-or-false-test tests false, the @code{while} loop 14273region, the true-or-false-test tests false, the @code{while} loop
14268exits, and the @code{count-words-region} function displays one or 14274exits, and the @code{@value{COUNT-WORDS}} function displays one or
14269other of its messages. 14275other of its messages.
14270 14276
14271After incorporating these final changes, the @code{count-words-region} 14277After incorporating these final changes, the @code{@value{COUNT-WORDS}}
14272works without bugs (or at least, without bugs that I have found!). 14278works without bugs (or at least, without bugs that I have found!).
14273Here is what it looks like: 14279Here is what it looks like:
14274 14280
14275@smallexample 14281@smallexample
14276@group 14282@group
14277;;; @r{Final version:} @code{while} 14283;;; @r{Final version:} @code{while}
14278(defun count-words-region (beginning end) 14284(defun @value{COUNT-WORDS} (beginning end)
14279 "Print number of words in the region." 14285 "Print number of words in the region."
14280 (interactive "r") 14286 (interactive "r")
14281 (message "Counting words in region ... ") 14287 (message "Counting words in region ... ")
@@ -14309,7 +14315,7 @@ Here is what it looks like:
14309@end group 14315@end group
14310@end smallexample 14316@end smallexample
14311 14317
14312@node recursive-count-words, Counting Exercise, count-words-region, Counting Words 14318@node recursive-count-words, Counting Exercise, @value{COUNT-WORDS}, Counting Words
14313@comment node-name, next, previous, up 14319@comment node-name, next, previous, up
14314@section Count Words Recursively 14320@section Count Words Recursively
14315@cindex Count words recursively 14321@cindex Count words recursively
@@ -14319,7 +14325,7 @@ Here is what it looks like:
14319You can write the function for counting words recursively as well as 14325You can write the function for counting words recursively as well as
14320with a @code{while} loop. Let's see how this is done. 14326with a @code{while} loop. Let's see how this is done.
14321 14327
14322First, we need to recognize that the @code{count-words-region} 14328First, we need to recognize that the @code{@value{COUNT-WORDS}}
14323function has three jobs: it sets up the appropriate conditions for 14329function has three jobs: it sets up the appropriate conditions for
14324counting to occur; it counts the words in the region; and it sends a 14330counting to occur; it counts the words in the region; and it sends a
14325message to the user telling how many words there are. 14331message to the user telling how many words there are.
@@ -14333,7 +14339,7 @@ other. One function will set up the conditions and display the
14333message; the other will return the word count. 14339message; the other will return the word count.
14334 14340
14335Let us start with the function that causes the message to be displayed. 14341Let us start with the function that causes the message to be displayed.
14336We can continue to call this @code{count-words-region}. 14342We can continue to call this @code{@value{COUNT-WORDS}}.
14337 14343
14338This is the function that the user will call. It will be interactive. 14344This is the function that the user will call. It will be interactive.
14339Indeed, it will be similar to our previous versions of this 14345Indeed, it will be similar to our previous versions of this
@@ -14347,7 +14353,7 @@ previous versions:
14347@smallexample 14353@smallexample
14348@group 14354@group
14349;; @r{Recursive version; uses regular expression search} 14355;; @r{Recursive version; uses regular expression search}
14350(defun count-words-region (beginning end) 14356(defun @value{COUNT-WORDS} (beginning end)
14351 "@var{documentation}@dots{}" 14357 "@var{documentation}@dots{}"
14352 (@var{interactive-expression}@dots{}) 14358 (@var{interactive-expression}@dots{})
14353@end group 14359@end group
@@ -14388,7 +14394,7 @@ Using @code{let}, the function definition looks like this:
14388 14394
14389@smallexample 14395@smallexample
14390@group 14396@group
14391(defun count-words-region (beginning end) 14397(defun @value{COUNT-WORDS} (beginning end)
14392 "Print number of words in the region." 14398 "Print number of words in the region."
14393 (interactive "r") 14399 (interactive "r")
14394@end group 14400@end group
@@ -14484,7 +14490,7 @@ Thus, the do-again-test should look like this:
14484Note that the search expression is part of the do-again-test---the 14490Note that the search expression is part of the do-again-test---the
14485function returns @code{t} if its search succeeds and @code{nil} if it 14491function returns @code{t} if its search succeeds and @code{nil} if it
14486fails. (@xref{Whitespace Bug, , The Whitespace Bug in 14492fails. (@xref{Whitespace Bug, , The Whitespace Bug in
14487@code{count-words-region}}, for an explanation of how 14493@code{@value{COUNT-WORDS}}}, for an explanation of how
14488@code{re-search-forward} works.) 14494@code{re-search-forward} works.)
14489 14495
14490The do-again-test is the true-or-false test of an @code{if} clause. 14496The do-again-test is the true-or-false test of an @code{if} clause.
@@ -14657,7 +14663,7 @@ The wrapper:
14657@smallexample 14663@smallexample
14658@group 14664@group
14659;;; @r{Recursive version} 14665;;; @r{Recursive version}
14660(defun count-words-region (beginning end) 14666(defun @value{COUNT-WORDS} (beginning end)
14661 "Print number of words in the region. 14667 "Print number of words in the region.
14662@end group 14668@end group
14663 14669
@@ -14702,11 +14708,11 @@ exclamation mark, and question mark. Do the same using recursion.
14702 14708
14703Our next project is to count the number of words in a function 14709Our next project is to count the number of words in a function
14704definition. Clearly, this can be done using some variant of 14710definition. Clearly, this can be done using some variant of
14705@code{count-word-region}. @xref{Counting Words, , Counting Words: 14711@code{@value{COUNT-WORDS}}. @xref{Counting Words, , Counting Words:
14706Repetition and Regexps}. If we are just going to count the words in 14712Repetition and Regexps}. If we are just going to count the words in
14707one definition, it is easy enough to mark the definition with the 14713one definition, it is easy enough to mark the definition with the
14708@kbd{C-M-h} (@code{mark-defun}) command, and then call 14714@kbd{C-M-h} (@code{mark-defun}) command, and then call
14709@code{count-word-region}. 14715@code{@value{COUNT-WORDS}}.
14710 14716
14711However, I am more ambitious: I want to count the words and symbols in 14717However, I am more ambitious: I want to count the words and symbols in
14712every definition in the Emacs sources and then print a graph that 14718every definition in the Emacs sources and then print a graph that
@@ -14719,7 +14725,7 @@ and this will tell.
14719* Divide and Conquer:: 14725* Divide and Conquer::
14720* Words and Symbols:: What to count? 14726* Words and Symbols:: What to count?
14721* Syntax:: What constitutes a word or symbol? 14727* Syntax:: What constitutes a word or symbol?
14722* count-words-in-defun:: Very like @code{count-words}. 14728* count-words-in-defun:: Very like @code{@value{COUNT-WORDS}}.
14723* Several defuns:: Counting several defuns in a file. 14729* Several defuns:: Counting several defuns in a file.
14724* Find a File:: Do you want to look at a file? 14730* Find a File:: Do you want to look at a file?
14725* lengths-list-file:: A list of the lengths of many definitions. 14731* lengths-list-file:: A list of the lengths of many definitions.
@@ -14793,11 +14799,11 @@ of ten words and symbols.
14793@noindent 14799@noindent
14794However, if we mark the @code{multiply-by-seven} definition with 14800However, if we mark the @code{multiply-by-seven} definition with
14795@kbd{C-M-h} (@code{mark-defun}), and then call 14801@kbd{C-M-h} (@code{mark-defun}), and then call
14796@code{count-words-region} on it, we will find that 14802@code{@value{COUNT-WORDS}} on it, we will find that
14797@code{count-words-region} claims the definition has eleven words, not 14803@code{@value{COUNT-WORDS}} claims the definition has eleven words, not
14798ten! Something is wrong! 14804ten! Something is wrong!
14799 14805
14800The problem is twofold: @code{count-words-region} does not count the 14806The problem is twofold: @code{@value{COUNT-WORDS}} does not count the
14801@samp{*} as a word, and it counts the single symbol, 14807@samp{*} as a word, and it counts the single symbol,
14802@code{multiply-by-seven}, as containing three words. The hyphens are 14808@code{multiply-by-seven}, as containing three words. The hyphens are
14803treated as if they were interword spaces rather than intraword 14809treated as if they were interword spaces rather than intraword
@@ -14805,8 +14811,8 @@ connectors: @samp{multiply-by-seven} is counted as if it were written
14805@samp{multiply by seven}. 14811@samp{multiply by seven}.
14806 14812
14807The cause of this confusion is the regular expression search within 14813The cause of this confusion is the regular expression search within
14808the @code{count-words-region} definition that moves point forward word 14814the @code{@value{COUNT-WORDS}} definition that moves point forward word
14809by word. In the canonical version of @code{count-words-region}, the 14815by word. In the canonical version of @code{@value{COUNT-WORDS}}, the
14810regexp is: 14816regexp is:
14811 14817
14812@smallexample 14818@smallexample
@@ -14839,8 +14845,8 @@ Syntax tables specify which characters belong to which categories.
14839Usually, a hyphen is not specified as a `word constituent character'. 14845Usually, a hyphen is not specified as a `word constituent character'.
14840Instead, it is specified as being in the `class of characters that are 14846Instead, it is specified as being in the `class of characters that are
14841part of symbol names but not words.' This means that the 14847part of symbol names but not words.' This means that the
14842@code{count-words-region} function treats it in the same way it treats 14848@code{@value{COUNT-WORDS}} function treats it in the same way it treats
14843an interword white space, which is why @code{count-words-region} 14849an interword white space, which is why @code{@value{COUNT-WORDS}}
14844counts @samp{multiply-by-seven} as three words. 14850counts @samp{multiply-by-seven} as three words.
14845 14851
14846There are two ways to cause Emacs to count @samp{multiply-by-seven} as 14852There are two ways to cause Emacs to count @samp{multiply-by-seven} as
@@ -14853,7 +14859,7 @@ most common character within symbols that is not typically a word
14853constituent character; there are others, too. 14859constituent character; there are others, too.
14854 14860
14855Alternatively, we can redefine the regular expression used in the 14861Alternatively, we can redefine the regular expression used in the
14856@code{count-words} definition so as to include symbols. This 14862@code{@value{COUNT-WORDS}} definition so as to include symbols. This
14857procedure has the merit of clarity, but the task is a little tricky. 14863procedure has the merit of clarity, but the task is a little tricky.
14858 14864
14859@need 1200 14865@need 1200
@@ -14910,7 +14916,7 @@ Here is the full regular expression:
14910@cindex Counting words in a @code{defun} 14916@cindex Counting words in a @code{defun}
14911 14917
14912We have seen that there are several ways to write a 14918We have seen that there are several ways to write a
14913@code{count-word-region} function. To write a 14919@code{count-words-region} function. To write a
14914@code{count-words-in-defun}, we need merely adapt one of these 14920@code{count-words-in-defun}, we need merely adapt one of these
14915versions. 14921versions.
14916 14922
@@ -15044,7 +15050,7 @@ Put together, the @code{count-words-in-defun} definition looks like this:
15044How to test this? The function is not interactive, but it is easy to 15050How to test this? The function is not interactive, but it is easy to
15045put a wrapper around the function to make it interactive; we can use 15051put a wrapper around the function to make it interactive; we can use
15046almost the same code as for the recursive version of 15052almost the same code as for the recursive version of
15047@code{count-words-region}: 15053@code{@value{COUNT-WORDS}}:
15048 15054
15049@smallexample 15055@smallexample
15050@group 15056@group
@@ -18885,7 +18891,7 @@ Lisp Reference Manual}.
18885 18891
18886@itemize @bullet 18892@itemize @bullet
18887@item 18893@item
18888Install the @code{count-words-region} function and then cause it to 18894Install the @code{@value{COUNT-WORDS}} function and then cause it to
18889enter the built-in debugger when you call it. Run the command on a 18895enter the built-in debugger when you call it. Run the command on a
18890region containing two words. You will need to press @kbd{d} a 18896region containing two words. You will need to press @kbd{d} a
18891remarkable number of times. On your system, is a `hook' called after 18897remarkable number of times. On your system, is a `hook' called after
@@ -18894,7 +18900,7 @@ Overview, , Command Loop Overview, elisp, The GNU Emacs Lisp Reference
18894Manual}.) 18900Manual}.)
18895 18901
18896@item 18902@item
18897Copy @code{count-words-region} into the @file{*scratch*} buffer, 18903Copy @code{@value{COUNT-WORDS}} into the @file{*scratch*} buffer,
18898instrument the function for Edebug, and walk through its execution. 18904instrument the function for Edebug, and walk through its execution.
18899The function does not need to have a bug, although you can introduce 18905The function does not need to have a bug, although you can introduce
18900one if you wish. If the function lacks a bug, the walk-through 18906one if you wish. If the function lacks a bug, the walk-through
@@ -18909,7 +18915,7 @@ for commands made outside of the Edebug debugging buffer.)
18909@item 18915@item
18910In the Edebug debugging buffer, use the @kbd{p} 18916In the Edebug debugging buffer, use the @kbd{p}
18911(@code{edebug-bounce-point}) command to see where in the region the 18917(@code{edebug-bounce-point}) command to see where in the region the
18912@code{count-words-region} is working. 18918@code{@value{COUNT-WORDS}} is working.
18913 18919
18914@item 18920@item
18915Move point to some spot further down the function and then type the 18921Move point to some spot further down the function and then type the
@@ -22272,6 +22278,3 @@ airplane.
22272 22278
22273@bye 22279@bye
22274 22280
22275@ignore
22276 arch-tag: da1a2154-531f-43a8-8e33-fc7faad10acf
22277@end ignore