diff options
Diffstat (limited to 'doc/lispintro')
| -rw-r--r-- | doc/lispintro/ChangeLog | 5 | ||||
| -rw-r--r-- | doc/lispintro/emacs-lisp-intro.texi | 147 |
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 @@ | |||
| 1 | 2010-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 | |||
| 1 | 2010-10-11 Glenn Morris <rgm@gnu.org> | 6 | 2010-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 | ||
| 706 | Counting: Repetition and Regexps | 706 | Counting: 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 | ||
| 713 | The @code{count-words-region} Function | 715 | The @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 | ||
| 718 | Counting Words in a @code{defun} | 720 | Counting 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 | ||
| 13842 | The standard Emacs distribution contains a function for counting the | 13844 | The standard Emacs distribution contains functions for counting the |
| 13843 | number of lines within a region. However, there is no corresponding | 13845 | number of lines and words within a region. |
| 13844 | function for counting words. | ||
| 13845 | 13846 | ||
| 13846 | Certain types of writing ask you to count words. Thus, if you write | 13847 | Certain types of writing ask you to count words. Thus, if you write |
| 13847 | an essay, you may be limited to 800 words; if you write a novel, you | 13848 | an essay, you may be limited to 800 words; if you write a novel, you |
| 13848 | may discipline yourself to write 1000 words a day. It seems odd to me | 13849 | may discipline yourself to write 1000 words a day. It seems odd, but |
| 13849 | that Emacs lacks a word count command. Perhaps people use Emacs | 13850 | for a long time, Emacs lacked a word count command. Perhaps people used |
| 13850 | mostly for code or types of documentation that do not require word | 13851 | Emacs mostly for code or types of documentation that did not require |
| 13851 | counts; or perhaps they restrict themselves to the operating system | 13852 | word counts; or perhaps they restricted themselves to the operating |
| 13852 | word count command, @code{wc}. Alternatively, people may follow | 13853 | system word count command, @code{wc}. Alternatively, people may have |
| 13853 | the publishers' convention and compute a word count by dividing the | 13854 | followed the publishers' convention and computed a word count by |
| 13854 | number of characters in a document by five. In any event, here are | 13855 | dividing the number of characters in a document by five. |
| 13855 | commands to count words. | 13856 | |
| 13856 | 13857 | There 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 | 13858 | some examples, which you may wish to compare with the standard Emacs |
| 13859 | command, @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 | ||
| 13862 | A word count command could count words in a line, paragraph, region, | 13866 | A word count command could count words in a line, paragraph, region, |
| 13863 | or buffer. What should the command cover? You could design the | 13867 | or 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, | |||
| 13865 | the Emacs tradition encourages flexibility---you may want to count | 13869 | the Emacs tradition encourages flexibility---you may want to count |
| 13866 | words in just a section, rather than all of a buffer. So it makes | 13870 | words in just a section, rather than all of a buffer. So it makes |
| 13867 | more sense to design the command to count the number of words in a | 13871 | more sense to design the command to count the number of words in a |
| 13868 | region. Once you have a @code{count-words-region} command, you can, | 13872 | region. Once you have a command to count words in a region, you can, |
| 13869 | if you wish, count words in a whole buffer by marking it with | 13873 | if 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 | |||
| 13876 | or to a @code{while} loop. | 13880 | or 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 | ||
| 13888 | First, we will implement the word count command with a @code{while} | 13892 | First, 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 | ||
| 13906 | The name of the function should be self-explanatory and similar to the | 13910 | The name of the function should be self-explanatory and similar to the |
| 13907 | existing @code{count-lines-region} name. This makes the name easier | 13911 | existing @code{count-lines-region} name. This makes the name easier |
| 13908 | to remember. @code{count-words-region} is a good choice. | 13912 | to remember. @code{count-words-region} is the obvious choice. Since |
| 13913 | that name is now used for the standard Emacs command to count words, we | ||
| 13914 | will name our implementation @code{@value{COUNT-WORDS}}. | ||
| 13909 | 13915 | ||
| 13910 | The function counts words within a region. This means that the | 13916 | The function counts words within a region. This means that the |
| 13911 | argument list must contain symbols that are bound to the two | 13917 | argument 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 | |||
| 13923 | count words, second, to run the @code{while} loop, and third, to send | 13929 | count words, second, to run the @code{while} loop, and third, to send |
| 13924 | a message to the user. | 13930 | a message to the user. |
| 13925 | 13931 | ||
| 13926 | When a user calls @code{count-words-region}, point may be at the | 13932 | When a user calls @code{@value{COUNT-WORDS}}, point may be at the |
| 13927 | beginning or the end of the region. However, the counting process | 13933 | beginning or the end of the region. However, the counting process |
| 13928 | must start at the beginning of the region. This means we will want | 13934 | must start at the beginning of the region. This means we will want |
| 13929 | to put point there if it is not already there. Executing | 13935 | to 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. |
| 14020 | Words are defined as at least one word-constituent | 14026 | Words are defined as at least one word-constituent |
| 14021 | character followed by at least one character that | 14027 | character followed by at least one character that |
| @@ -14056,14 +14062,14 @@ table determines which characters these are." | |||
| 14056 | @noindent | 14062 | @noindent |
| 14057 | As written, the function works, but not in all circumstances. | 14063 | As 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 | ||
| 14063 | The @code{count-words-region} command described in the preceding | 14069 | The @code{@value{COUNT-WORDS}} command described in the preceding |
| 14064 | section has two bugs, or rather, one bug with two manifestations. | 14070 | section has two bugs, or rather, one bug with two manifestations. |
| 14065 | First, if you mark a region containing only whitespace in the middle | 14071 | First, if you mark a region containing only whitespace in the middle |
| 14066 | of some text, the @code{count-words-region} command tells you that the | 14072 | of some text, the @code{@value{COUNT-WORDS}} command tells you that the |
| 14067 | region contains one word! Second, if you mark a region containing | 14073 | region contains one word! Second, if you mark a region containing |
| 14068 | only whitespace at the end of the buffer or the accessible portion of | 14074 | only whitespace at the end of the buffer or the accessible portion of |
| 14069 | a narrowed buffer, the command displays an error message that looks | 14075 | a 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. |
| 14089 | Words are defined as at least one word-constituent character followed | 14095 | Words are defined as at least one word-constituent character followed |
| 14090 | by at least one character that is not a word-constituent. The buffer's | 14096 | by at least one character that is not a word-constituent. The buffer's |
| @@ -14123,12 +14129,12 @@ syntax table determines which characters these are." | |||
| 14123 | If you wish, you can also install this keybinding by evaluating it: | 14129 | If 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 | ||
| 14129 | To conduct the first test, set mark and point to the beginning and end | 14135 | To conduct the first test, set mark and point to the beginning and end |
| 14130 | of the following line and then type @kbd{C-c =} (or @kbd{M-x | 14136 | of the following line and then type @kbd{C-c =} (or @kbd{M-x |
| 14131 | count-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 | ||
| 14140 | Repeat the test, but place mark at the beginning of the line and place | 14146 | Repeat the test, but place mark at the beginning of the line and place |
| 14141 | point just @emph{before} the word @samp{one}. Again type the command | 14147 | point 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 |
| 14143 | that the region has no words, since it is composed only of the | 14149 | that the region has no words, since it is composed only of the |
| 14144 | whitespace at the beginning of the line. But instead Emacs tells you | 14150 | whitespace at the beginning of the line. But instead Emacs tells you |
| 14145 | that the region has one word! | 14151 | that 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 |
| 14149 | line. Place mark right after the word @samp{three} and point at the | 14155 | line. Place mark right after the word @samp{three} and point at the |
| 14150 | end of line. (The end of the line will be the end of the buffer.) | 14156 | end of line. (The end of the line will be the end of the buffer.) |
| 14151 | Type @kbd{C-c =} (or @kbd{M-x count-words-region}) as you did before. | 14157 | Type @kbd{C-c =} (or @kbd{M-x @value{COUNT-WORDS}}) as you did before. |
| 14152 | Again, Emacs should tell you that the region has no words, since it is | 14158 | Again, Emacs should tell you that the region has no words, since it is |
| 14153 | composed only of the whitespace at the end of the line. Instead, | 14159 | composed only of the whitespace at the end of the line. Instead, |
| 14154 | Emacs displays an error message saying @samp{Search failed}. | 14160 | Emacs displays an error message saying @samp{Search failed}. |
| @@ -14157,7 +14163,7 @@ The two bugs stem from the same problem. | |||
| 14157 | 14163 | ||
| 14158 | Consider the first manifestation of the bug, in which the command | 14164 | Consider the first manifestation of the bug, in which the command |
| 14159 | tells you that the whitespace at the beginning of the line contains | 14165 | tells you that the whitespace at the beginning of the line contains |
| 14160 | one word. What happens is this: The @code{M-x count-words-region} | 14166 | one word. What happens is this: The @code{M-x @value{COUNT-WORDS}} |
| 14161 | command moves point to the beginning of the region. The @code{while} | 14167 | command moves point to the beginning of the region. The @code{while} |
| 14162 | tests whether the value of point is smaller than the value of | 14168 | tests 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 | |||
| 14191 | repeat count. (In Emacs, you can see a function's documentation by | 14197 | repeat count. (In Emacs, you can see a function's documentation by |
| 14192 | typing @kbd{C-h f}, the name of the function, and then @key{RET}.) | 14198 | typing @kbd{C-h f}, the name of the function, and then @key{RET}.) |
| 14193 | 14199 | ||
| 14194 | In the @code{count-words-region} definition, the value of the end of | 14200 | In the @code{@value{COUNT-WORDS}} definition, the value of the end of |
| 14195 | the region is held by the variable @code{end} which is passed as an | 14201 | the region is held by the variable @code{end} which is passed as an |
| 14196 | argument to the function. Thus, we can add @code{end} as an argument | 14202 | argument to the function. Thus, we can add @code{end} as an argument |
| 14197 | to the regular expression search expression: | 14203 | to 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 | ||
| 14203 | However, if you make only this change to the @code{count-words-region} | 14209 | However, if you make only this change to the @code{@value{COUNT-WORDS}} |
| 14204 | definition and then test the new version of the definition on a | 14210 | definition and then test the new version of the definition on a |
| 14205 | stretch of whitespace, you will receive an error message saying | 14211 | stretch 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 | |||
| 14231 | than the value of end, since the @code{re-search-forward} expression | 14237 | than the value of end, since the @code{re-search-forward} expression |
| 14232 | did not move point. @dots{} and the cycle repeats @dots{} | 14238 | did not move point. @dots{} and the cycle repeats @dots{} |
| 14233 | 14239 | ||
| 14234 | The @code{count-words-region} definition requires yet another | 14240 | The @code{@value{COUNT-WORDS}} definition requires yet another |
| 14235 | modification, to cause the true-or-false-test of the @code{while} loop | 14241 | modification, to cause the true-or-false-test of the @code{while} loop |
| 14236 | to test false if the search fails. Put another way, there are two | 14242 | to test false if the search fails. Put another way, there are two |
| 14237 | conditions that must be satisfied in the true-or-false-test before the | 14243 | conditions 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 | |||
| 14265 | found, point is moved through the region. When the search expression | 14271 | found, point is moved through the region. When the search expression |
| 14266 | fails to find another word, or when point reaches the end of the | 14272 | fails to find another word, or when point reaches the end of the |
| 14267 | region, the true-or-false-test tests false, the @code{while} loop | 14273 | region, the true-or-false-test tests false, the @code{while} loop |
| 14268 | exits, and the @code{count-words-region} function displays one or | 14274 | exits, and the @code{@value{COUNT-WORDS}} function displays one or |
| 14269 | other of its messages. | 14275 | other of its messages. |
| 14270 | 14276 | ||
| 14271 | After incorporating these final changes, the @code{count-words-region} | 14277 | After incorporating these final changes, the @code{@value{COUNT-WORDS}} |
| 14272 | works without bugs (or at least, without bugs that I have found!). | 14278 | works without bugs (or at least, without bugs that I have found!). |
| 14273 | Here is what it looks like: | 14279 | Here 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: | |||
| 14319 | You can write the function for counting words recursively as well as | 14325 | You can write the function for counting words recursively as well as |
| 14320 | with a @code{while} loop. Let's see how this is done. | 14326 | with a @code{while} loop. Let's see how this is done. |
| 14321 | 14327 | ||
| 14322 | First, we need to recognize that the @code{count-words-region} | 14328 | First, we need to recognize that the @code{@value{COUNT-WORDS}} |
| 14323 | function has three jobs: it sets up the appropriate conditions for | 14329 | function has three jobs: it sets up the appropriate conditions for |
| 14324 | counting to occur; it counts the words in the region; and it sends a | 14330 | counting to occur; it counts the words in the region; and it sends a |
| 14325 | message to the user telling how many words there are. | 14331 | message to the user telling how many words there are. |
| @@ -14333,7 +14339,7 @@ other. One function will set up the conditions and display the | |||
| 14333 | message; the other will return the word count. | 14339 | message; the other will return the word count. |
| 14334 | 14340 | ||
| 14335 | Let us start with the function that causes the message to be displayed. | 14341 | Let us start with the function that causes the message to be displayed. |
| 14336 | We can continue to call this @code{count-words-region}. | 14342 | We can continue to call this @code{@value{COUNT-WORDS}}. |
| 14337 | 14343 | ||
| 14338 | This is the function that the user will call. It will be interactive. | 14344 | This is the function that the user will call. It will be interactive. |
| 14339 | Indeed, it will be similar to our previous versions of this | 14345 | Indeed, 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: | |||
| 14484 | Note that the search expression is part of the do-again-test---the | 14490 | Note that the search expression is part of the do-again-test---the |
| 14485 | function returns @code{t} if its search succeeds and @code{nil} if it | 14491 | function returns @code{t} if its search succeeds and @code{nil} if it |
| 14486 | fails. (@xref{Whitespace Bug, , The Whitespace Bug in | 14492 | fails. (@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 | ||
| 14490 | The do-again-test is the true-or-false test of an @code{if} clause. | 14496 | The 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 | ||
| 14703 | Our next project is to count the number of words in a function | 14709 | Our next project is to count the number of words in a function |
| 14704 | definition. Clearly, this can be done using some variant of | 14710 | definition. 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: |
| 14706 | Repetition and Regexps}. If we are just going to count the words in | 14712 | Repetition and Regexps}. If we are just going to count the words in |
| 14707 | one definition, it is easy enough to mark the definition with the | 14713 | one 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 | ||
| 14711 | However, I am more ambitious: I want to count the words and symbols in | 14717 | However, I am more ambitious: I want to count the words and symbols in |
| 14712 | every definition in the Emacs sources and then print a graph that | 14718 | every 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 |
| 14794 | However, if we mark the @code{multiply-by-seven} definition with | 14800 | However, 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 |
| 14798 | ten! Something is wrong! | 14804 | ten! Something is wrong! |
| 14799 | 14805 | ||
| 14800 | The problem is twofold: @code{count-words-region} does not count the | 14806 | The 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 |
| 14803 | treated as if they were interword spaces rather than intraword | 14809 | treated 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 | ||
| 14807 | The cause of this confusion is the regular expression search within | 14813 | The cause of this confusion is the regular expression search within |
| 14808 | the @code{count-words-region} definition that moves point forward word | 14814 | the @code{@value{COUNT-WORDS}} definition that moves point forward word |
| 14809 | by word. In the canonical version of @code{count-words-region}, the | 14815 | by word. In the canonical version of @code{@value{COUNT-WORDS}}, the |
| 14810 | regexp is: | 14816 | regexp is: |
| 14811 | 14817 | ||
| 14812 | @smallexample | 14818 | @smallexample |
| @@ -14839,8 +14845,8 @@ Syntax tables specify which characters belong to which categories. | |||
| 14839 | Usually, a hyphen is not specified as a `word constituent character'. | 14845 | Usually, a hyphen is not specified as a `word constituent character'. |
| 14840 | Instead, it is specified as being in the `class of characters that are | 14846 | Instead, it is specified as being in the `class of characters that are |
| 14841 | part of symbol names but not words.' This means that the | 14847 | part 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 |
| 14843 | an interword white space, which is why @code{count-words-region} | 14849 | an interword white space, which is why @code{@value{COUNT-WORDS}} |
| 14844 | counts @samp{multiply-by-seven} as three words. | 14850 | counts @samp{multiply-by-seven} as three words. |
| 14845 | 14851 | ||
| 14846 | There are two ways to cause Emacs to count @samp{multiply-by-seven} as | 14852 | There 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 | |||
| 14853 | constituent character; there are others, too. | 14859 | constituent character; there are others, too. |
| 14854 | 14860 | ||
| 14855 | Alternatively, we can redefine the regular expression used in the | 14861 | Alternatively, 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 |
| 14857 | procedure has the merit of clarity, but the task is a little tricky. | 14863 | procedure 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 | ||
| 14912 | We have seen that there are several ways to write a | 14918 | We 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 |
| 14915 | versions. | 14921 | versions. |
| 14916 | 14922 | ||
| @@ -15044,7 +15050,7 @@ Put together, the @code{count-words-in-defun} definition looks like this: | |||
| 15044 | How to test this? The function is not interactive, but it is easy to | 15050 | How to test this? The function is not interactive, but it is easy to |
| 15045 | put a wrapper around the function to make it interactive; we can use | 15051 | put a wrapper around the function to make it interactive; we can use |
| 15046 | almost the same code as for the recursive version of | 15052 | almost 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 |
| 18888 | Install the @code{count-words-region} function and then cause it to | 18894 | Install the @code{@value{COUNT-WORDS}} function and then cause it to |
| 18889 | enter the built-in debugger when you call it. Run the command on a | 18895 | enter the built-in debugger when you call it. Run the command on a |
| 18890 | region containing two words. You will need to press @kbd{d} a | 18896 | region containing two words. You will need to press @kbd{d} a |
| 18891 | remarkable number of times. On your system, is a `hook' called after | 18897 | remarkable 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 | |||
| 18894 | Manual}.) | 18900 | Manual}.) |
| 18895 | 18901 | ||
| 18896 | @item | 18902 | @item |
| 18897 | Copy @code{count-words-region} into the @file{*scratch*} buffer, | 18903 | Copy @code{@value{COUNT-WORDS}} into the @file{*scratch*} buffer, |
| 18898 | instrument the function for Edebug, and walk through its execution. | 18904 | instrument the function for Edebug, and walk through its execution. |
| 18899 | The function does not need to have a bug, although you can introduce | 18905 | The function does not need to have a bug, although you can introduce |
| 18900 | one if you wish. If the function lacks a bug, the walk-through | 18906 | one 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 |
| 18910 | In the Edebug debugging buffer, use the @kbd{p} | 18916 | In 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 |
| 18915 | Move point to some spot further down the function and then type the | 18921 | Move 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 | ||