aboutsummaryrefslogtreecommitdiffstats
path: root/lispintro
diff options
context:
space:
mode:
authorRobert J. Chassell2006-10-30 22:15:21 +0000
committerRobert J. Chassell2006-10-30 22:15:21 +0000
commit5fb9c53c678be2bbfdf038eae7f80167014c8e87 (patch)
tree5e4e004a8a9de4879c55d173fc6ed142a4230e4e /lispintro
parent024ccae39937f2dda8bc2e6f383fd17eec4ac836 (diff)
downloademacs-5fb9c53c678be2bbfdf038eae7f80167014c8e87.tar.gz
emacs-5fb9c53c678be2bbfdf038eae7f80167014c8e87.zip
* emacs-lisp-intro.texi: Many changes since it turned out that
many `simple' functions were rewritten. Changes to the text regarding zap-to-char, mark-whole-buffer, append-to-buffer, copy-to-buffer, beginning-of-buffer, what-line, and possibly others. (I have not reviewed all yet.) This instance does build for Info and TeX.
Diffstat (limited to 'lispintro')
-rw-r--r--lispintro/ChangeLog9
-rw-r--r--lispintro/emacs-lisp-intro.texi5667
2 files changed, 3341 insertions, 2335 deletions
diff --git a/lispintro/ChangeLog b/lispintro/ChangeLog
index a210e21d98e..7313bfc613a 100644
--- a/lispintro/ChangeLog
+++ b/lispintro/ChangeLog
@@ -1,3 +1,12 @@
12006-10-30 Robert J. Chassell <bob@rattlesnake.com>
2
3 * emacs-lisp-intro.texi: Many changes since it turned out that
4 many `simple' functions were rewritten. Changes to the text
5 regarding zap-to-char, mark-whole-buffer, append-to-buffer,
6 copy-to-buffer, beginning-of-buffer, what-line, and possibly
7 others. (I have not reviewed all yet.) This instance does build
8 for Info and TeX.
9
12006-10-29 Chong Yidong <cyd@stupidchicken.com> 102006-10-29 Chong Yidong <cyd@stupidchicken.com>
2 11
3 * Makefile.in: Use relative paths to avoid advertising filesystem 12 * Makefile.in: Use relative paths to avoid advertising filesystem
diff --git a/lispintro/emacs-lisp-intro.texi b/lispintro/emacs-lisp-intro.texi
index cb04acc2062..0318b2b2c79 100644
--- a/lispintro/emacs-lisp-intro.texi
+++ b/lispintro/emacs-lisp-intro.texi
@@ -1,5 +1,6 @@
1\input ../man/texinfo @c -*-texinfo-*- 1\input ../man/texinfo @c -*-texinfo-*-
2@c change above to \input texinfo if building on own. 2@c \input texinfo @c -*-texinfo-*-
3@c change to \input texinfo if building on own.
3@comment %**start of header 4@comment %**start of header
4@setfilename ../info/eintr 5@setfilename ../info/eintr
5@c setfilename emacs-lisp-intro.info 6@c setfilename emacs-lisp-intro.info
@@ -23,19 +24,25 @@
23 24
24@comment %**end of header 25@comment %**end of header
25 26
26@set edition-number 2.14 27@set edition-number 2.15
27@set update-date 2004 Oct 12 28@set update-date 2006 Oct 30
28 29
29@ignore 30@ignore
30 ## Summary of shell commands to create various output formats: 31 ## Summary of shell commands to create various output formats:
31 32
32 pushd /usr/local/src/emacs/lispintro/ 33 pushd /usr/local/src/emacs/lispintro/
34 ## pushd /u/intro/
33 35
34 ## Info output 36 ## Info output
35 makeinfo --no-split --paragraph-indent=0 --verbose emacs-lisp-intro.texi 37 makeinfo --no-split --paragraph-indent=0 --verbose emacs-lisp-intro.texi
36 38
39 ## ;; (progn (when (bufferp (get-buffer "*info*")) (kill-buffer "*info*")) (info "/u/intro/emacs-lisp-intro.info"))
40
37 ## DVI output 41 ## DVI output
38 texi2dvi emacs-lisp-intro.texi 42 texi2dvi /u/intro/emacs-lisp-intro.texi
43
44 ## View DVI output; see below also
45 # xdvi -margins 24pt -topmargin 4pt -offsets 24pt -geometry 760x1140 -s 5 -useTeXpages -mousemode 1 /u/intro/emacs-lisp-intro.dvi &
39 46
40 ## HTML output 47 ## HTML output
41 makeinfo --html --no-split --verbose emacs-lisp-intro.texi 48 makeinfo --html --no-split --verbose emacs-lisp-intro.texi
@@ -52,6 +59,8 @@
52 makeinfo --xml --no-split --paragraph-indent=0 \ 59 makeinfo --xml --no-split --paragraph-indent=0 \
53 --verbose emacs-lisp-intro.texi 60 --verbose emacs-lisp-intro.texi
54 61
62 popd
63
55 #### (You must be in the same directory as the viewed file.) 64 #### (You must be in the same directory as the viewed file.)
56 65
57 ## View DVI output 66 ## View DVI output
@@ -63,7 +72,20 @@
63 ## View Info output with standalone reader 72 ## View Info output with standalone reader
64 info emacs-lisp-intro.info 73 info emacs-lisp-intro.info
65 74
66 ## popd 75 ## popd
76
77Need to explain defcustom see (elisp)Customization (elisp)Variable Definitions
78 defsubst
79 defconst
80
81
82
83# as user `root'
84# insert thumbdrive
85 mtusb # mount -v -t ext3 /dev/sda /mnt
86 cp -v /u/intro/emacs-lisp-intro.texi /mnt/backup/intro/emacs-lisp-intro.texi
87 umtusb # umount -v /mnt
88# remove thumbdrive
67 89
68@end ignore 90@end ignore
69 91
@@ -166,7 +188,8 @@
166@c in the Texinfo version 4.6 of the 2003 Jun 13 distribution. 188@c in the Texinfo version 4.6 of the 2003 Jun 13 distribution.
167 189
168@tex 190@tex
169\global\def\xrefprintnodename#1{``#1''} 191\global\def\xrefprintnodename#1{\unskip, ``#1''}
192% \global\def\xrefprintnodename#1{, ``#1''}
170@end tex 193@end tex
171 194
172@c ---------------------------------------------------- 195@c ----------------------------------------------------
@@ -280,6 +303,16 @@ This master menu first lists each chapter and index; then it lists
280every node in every chapter. 303every node in every chapter.
281@end ifnottex 304@end ifnottex
282 305
306@c >>>> Set pageno appropriately <<<<
307
308@c The first page of the Preface is a roman numeral; it is the first
309@c right handed page after the Table of Contents; hence the following
310@c setting must be for an odd negative number.
311
312@iftex
313@global@pageno = -11
314@end iftex
315
283@menu 316@menu
284* Preface:: What to look for. 317* Preface:: What to look for.
285* List Processing:: What is Lisp? 318* List Processing:: What is Lisp?
@@ -371,34 +404,34 @@ Arguments
371 404
372Setting the Value of a Variable 405Setting the Value of a Variable
373 406
374* Using set:: Setting values. 407* Using set:: Setting values.
375* Using setq:: Setting a quoted value. 408* Using setq:: Setting a quoted value.
376* Counting:: Using @code{setq} to count. 409* Counting:: Using @code{setq} to count.
377 410
378Practicing Evaluation 411Practicing Evaluation
379 412
380* How to Evaluate:: Typing editing commands or @kbd{C-x C-e} 413* How to Evaluate:: Typing editing commands or @kbd{C-x C-e}
381 causes evaluation. 414 causes evaluation.
382* Buffer Names:: Buffers and files are different. 415* Buffer Names:: Buffers and files are different.
383* Getting Buffers:: Getting a buffer itself, not merely its name. 416* Getting Buffers:: Getting a buffer itself, not merely its name.
384* Switching Buffers:: How to change to another buffer. 417* Switching Buffers:: How to change to another buffer.
385* Buffer Size & Locations:: Where point is located and the size of 418* Buffer Size & Locations:: Where point is located and the size of
386 the buffer. 419 the buffer.
387* Evaluation Exercise:: 420* Evaluation Exercise::
388 421
389How To Write Function Definitions 422How To Write Function Definitions
390 423
391* Primitive Functions:: 424* Primitive Functions::
392* defun:: The @code{defun} special form. 425* defun:: The @code{defun} special form.
393* Install:: Install a function definition. 426* Install:: Install a function definition.
394* Interactive:: Making a function interactive. 427* Interactive:: Making a function interactive.
395* Interactive Options:: Different options for @code{interactive}. 428* Interactive Options:: Different options for @code{interactive}.
396* Permanent Installation:: Installing code permanently. 429* Permanent Installation:: Installing code permanently.
397* let:: Creating and initializing local variables. 430* let:: Creating and initializing local variables.
398* if:: What if? 431* if:: What if?
399* else:: If--then--else expressions. 432* else:: If--then--else expressions.
400* Truth & Falsehood:: What Lisp considers false and true. 433* Truth & Falsehood:: What Lisp considers false and true.
401* save-excursion:: Keeping track of point, mark, and buffer. 434* save-excursion:: Keeping track of point, mark, and buffer.
402* Review:: 435* Review::
403* defun Exercises:: 436* defun Exercises::
404 437
@@ -473,6 +506,7 @@ The Definition of @code{insert-buffer}
473* if & or:: Using an @code{if} instead of an @code{or}. 506* if & or:: Using an @code{if} instead of an @code{or}.
474* Insert or:: How the @code{or} expression works. 507* Insert or:: How the @code{or} expression works.
475* Insert let:: Two @code{save-excursion} expressions. 508* Insert let:: Two @code{save-excursion} expressions.
509* New insert-buffer::
476 510
477The Interactive Expression in @code{insert-buffer} 511The Interactive Expression in @code{insert-buffer}
478 512
@@ -519,9 +553,9 @@ Cutting and Storing Text
519* Storing Text:: Text is stored in a list. 553* Storing Text:: Text is stored in a list.
520* zap-to-char:: Cutting out text up to a character. 554* zap-to-char:: Cutting out text up to a character.
521* kill-region:: Cutting text out of a region. 555* kill-region:: Cutting text out of a region.
556* copy-region-as-kill:: A definition for copying text.
522* Digression into C:: Minor note on C programming language macros. 557* Digression into C:: Minor note on C programming language macros.
523* defvar:: How to give a variable an initial value. 558* defvar:: How to give a variable an initial value.
524* copy-region-as-kill:: A definition for copying text.
525* cons & search-fwd Review:: 559* cons & search-fwd Review::
526* search Exercises:: 560* search Exercises::
527 561
@@ -538,12 +572,7 @@ Cutting and Storing Text
538 572
539* Complete kill-region:: The function definition. 573* Complete kill-region:: The function definition.
540* condition-case:: Dealing with a problem. 574* condition-case:: Dealing with a problem.
541* delete-and-extract-region:: Doing the work. 575* Lisp macro::
542
543Initializing a Variable with @code{defvar}
544
545* See variable current value::
546* defvar and asterisk:: An old-time convention.
547 576
548@code{copy-region-as-kill} 577@code{copy-region-as-kill}
549 578
@@ -556,6 +585,11 @@ The Body of @code{copy-region-as-kill}
556* kill-append function:: 585* kill-append function::
557* kill-new function:: 586* kill-new function::
558 587
588Initializing a Variable with @code{defvar}
589
590* See variable current value::
591* defvar and asterisk::
592
559How Lists are Implemented 593How Lists are Implemented
560 594
561* Lists diagrammed:: 595* Lists diagrammed::
@@ -564,9 +598,9 @@ How Lists are Implemented
564 598
565Yanking Text Back 599Yanking Text Back
566 600
567* Kill Ring Overview:: The kill ring is a list. 601* Kill Ring Overview::
568* kill-ring-yank-pointer:: The @code{kill-ring-yank-pointer} variable. 602* kill-ring-yank-pointer:: The kill ring is a list.
569* yank nthcdr Exercises:: 603* yank nthcdr Exercises:: The @code{kill-ring-yank-pointer} variable.
570 604
571Loops and Recursion 605Loops and Recursion
572 606
@@ -643,11 +677,6 @@ Regular Expression Searches
643* forward-paragraph in brief:: Key parts of the function definition. 677* forward-paragraph in brief:: Key parts of the function definition.
644* fwd-para let:: The @code{let*} expression. 678* fwd-para let:: The @code{let*} expression.
645* fwd-para while:: The forward motion @code{while} loop. 679* fwd-para while:: The forward motion @code{while} loop.
646* fwd-para between paragraphs:: Movement between paragraphs.
647* fwd-para within paragraph:: Movement within paragraphs.
648* fwd-para no fill prefix:: When there is no fill prefix.
649* fwd-para with fill prefix:: When there is a fill prefix.
650* fwd-para summary:: Summary of @code{forward-paragraph} code.
651 680
652Counting: Repetition and Regexps 681Counting: Repetition and Regexps
653 682
@@ -721,28 +750,19 @@ Debugging
721 750
722Handling the Kill Ring 751Handling the Kill Ring
723 752
724* rotate-yank-pointer:: Move a pointer along a list and around. 753* current-kill::
725* yank:: Paste a copy of a clipped element. 754* yank:: Paste a copy of a clipped element.
726* yank-pop:: Insert first element pointed to. 755* yank-pop:: Insert element pointed to.
727* ring file:: 756* ring file::
728 757
729The @code{rotate-yank-pointer} Function 758The @code{current-kill} Function
730 759
731* Understanding rotate-yk-ptr:: 760* Understanding current-kill::
732* rotate-yk-ptr body:: The body of @code{rotate-yank-pointer}.
733 761
734The Body of @code{rotate-yank-pointer} 762@code{current-kill} in Outline
735 763
736* Digression concerning error:: How to mislead humans, but not computers. 764* Digression concerning error:: How to mislead humans, but not computers.
737* rotate-yk-ptr else-part:: The else-part of the @code{if} expression. 765* Determining the Element::
738* Remainder Function:: The remainder, @code{%}, function.
739* rotate-yk-ptr remainder:: Using @code{%} in @code{rotate-yank-pointer}.
740* kill-rng-yk-ptr last elt:: Pointing to the last element.
741
742@code{yank}
743
744* rotate-yk-ptr arg:: Pass the argument to @code{rotate-yank-pointer}.
745* rotate-yk-ptr negative arg:: Pass a negative argument.
746 766
747A Graph with Labelled Axes 767A Graph with Labelled Axes
748 768
@@ -778,16 +798,6 @@ Printing the Whole Graph
778@end detailmenu 798@end detailmenu
779@end menu 799@end menu
780 800
781@c >>>> Set pageno appropriately <<<<
782
783@c The first page of the Preface is a roman numeral; it is the first
784@c right handed page after the Table of Contents; hence the following
785@c setting must be for an odd negative number.
786
787@iftex
788@global@pageno = -11
789@end iftex
790
791@node Preface, List Processing, Top, Top 801@node Preface, List Processing, Top, Top
792@comment node-name, next, previous, up 802@comment node-name, next, previous, up
793@unnumbered Preface 803@unnumbered Preface
@@ -809,11 +819,11 @@ files, read letters, write books---all these activities are kinds of
809editing in the most general sense of the word.) 819editing in the most general sense of the word.)
810 820
811@menu 821@menu
812* Why:: Why learn Emacs Lisp? 822* Why::
813* On Reading this Text:: Read, gain familiarity, pick up habits.... 823* On Reading this Text::
814* Who You Are:: For whom this is written. 824* Who You Are::
815* Lisp History:: 825* Lisp History::
816* Note for Novices:: You can read this as a novice. 826* Note for Novices::
817* Thank You:: 827* Thank You::
818@end menu 828@end menu
819 829
@@ -1066,16 +1076,16 @@ think about functions now; functions are defined in @ref{Making
1066Errors, , Generate an Error Message}.} Lists are the basis of Lisp. 1076Errors, , Generate an Error Message}.} Lists are the basis of Lisp.
1067 1077
1068@menu 1078@menu
1069* Lisp Lists:: What are lists? 1079* Lisp Lists::
1070* Run a Program:: Any list in Lisp is a program ready to run. 1080* Run a Program::
1071* Making Errors:: Generating an error message. 1081* Making Errors::
1072* Names & Definitions:: Names of symbols and function definitions. 1082* Names & Definitions::
1073* Lisp Interpreter:: What the Lisp interpreter does. 1083* Lisp Interpreter::
1074* Evaluation:: Running a program. 1084* Evaluation::
1075* Variables:: Returning a value from a variable. 1085* Variables::
1076* Arguments:: Passing information to a function. 1086* Arguments::
1077* set & setq:: Setting the value of a variable. 1087* set & setq::
1078* Summary:: The major points. 1088* Summary::
1079* Error Message Exercises:: 1089* Error Message Exercises::
1080@end menu 1090@end menu
1081 1091
@@ -1105,10 +1115,10 @@ like flowers in a field with a stone wall around them.
1105@cindex Flowers in a field 1115@cindex Flowers in a field
1106 1116
1107@menu 1117@menu
1108* Numbers Lists:: List have numbers, other lists, in them. 1118* Numbers Lists::
1109* Lisp Atoms:: Elemental entities. 1119* Lisp Atoms::
1110* Whitespace in Lists:: Formating lists to be readable. 1120* Whitespace in Lists::
1111* Typing Lists:: How GNU Emacs helps you type lists. 1121* Typing Lists::
1112@end menu 1122@end menu
1113 1123
1114@node Numbers Lists, Lisp Atoms, Lisp Lists, Lisp Lists 1124@node Numbers Lists, Lisp Atoms, Lisp Lists, Lisp Lists
@@ -1151,8 +1161,8 @@ term comes from the historical meaning of the word atom, which means
1151using in the lists cannot be divided into any smaller parts and still 1161using in the lists cannot be divided into any smaller parts and still
1152mean the same thing as part of a program; likewise with numbers and 1162mean the same thing as part of a program; likewise with numbers and
1153single character symbols like @samp{+}. On the other hand, unlike an 1163single character symbols like @samp{+}. On the other hand, unlike an
1154atom, a list can be split into parts. (@xref{car cdr & cons, , 1164ancient atom, a list can be split into parts. (@xref{car cdr & cons,
1155@code{car} @code{cdr} & @code{cons} Fundamental Functions}.) 1165, @code{car} @code{cdr} & @code{cons} Fundamental Functions}.)
1156 1166
1157In a list, atoms are separated from each other by whitespace. They can be 1167In a list, atoms are separated from each other by whitespace. They can be
1158right next to a parenthesis. 1168right next to a parenthesis.
@@ -1378,13 +1388,13 @@ C-e}:
1378 1388
1379@noindent 1389@noindent
1380What you see depends on which version of Emacs you are running. GNU 1390What you see depends on which version of Emacs you are running. GNU
1381Emacs version 21 provides more information than version 20 and before. 1391Emacs version 22 provides more information than version 20 and before.
1382First, the more recent result of generating an error; then the 1392First, the more recent result of generating an error; then the
1383earlier, version 20 result. 1393earlier, version 20 result.
1384 1394
1385@need 1250 1395@need 1250
1386@noindent 1396@noindent
1387In GNU Emacs version 21, a @file{*Backtrace*} window will open up and 1397In GNU Emacs version 22, a @file{*Backtrace*} window will open up and
1388you will see the following in it: 1398you will see the following in it:
1389 1399
1390@smallexample 1400@smallexample
@@ -1501,7 +1511,7 @@ carry out. Instead, the symbol is used, perhaps temporarily, as a way
1501of locating the definition or set of instructions. What we see is the 1511of locating the definition or set of instructions. What we see is the
1502name through which the instructions can be found. Names of people 1512name through which the instructions can be found. Names of people
1503work the same way. I can be referred to as @samp{Bob}; however, I am 1513work the same way. I can be referred to as @samp{Bob}; however, I am
1504not the letters @samp{B}, @samp{o}, @samp{b} but am, or were, the 1514not the letters @samp{B}, @samp{o}, @samp{b} but am, or was, the
1505consciousness consistently associated with a particular life-form. 1515consciousness consistently associated with a particular life-form.
1506The name is not me, but it can be used to refer to me. 1516The name is not me, but it can be used to refer to me.
1507 1517
@@ -1546,8 +1556,8 @@ and attach them to names, and how to do this without confusing either
1546yourself or the computer. 1556yourself or the computer.
1547 1557
1548@menu 1558@menu
1549* Complications:: Variables, Special forms, Lists within. 1559* Complications::
1550* Byte Compiling:: Specially processing code for speed. 1560* Byte Compiling::
1551@end menu 1561@end menu
1552 1562
1553@node Complications, Byte Compiling, Lisp Interpreter, Lisp Interpreter 1563@node Complications, Byte Compiling, Lisp Interpreter, Lisp Interpreter
@@ -1639,7 +1649,7 @@ Lisp interpreter to return a value and perhaps carry out a side effect;
1639or else produce an error. 1649or else produce an error.
1640 1650
1641@menu 1651@menu
1642* Evaluating Inner Lists:: Lists within lists... 1652* Evaluating Inner Lists::
1643@end menu 1653@end menu
1644 1654
1645@node Evaluating Inner Lists, , Evaluation, Evaluation 1655@node Evaluating Inner Lists, , Evaluation, Evaluation
@@ -1741,9 +1751,8 @@ function definition, and vice-versa.
1741 1751
1742@menu 1752@menu
1743* fill-column Example:: 1753* fill-column Example::
1744* Void Function:: The error message for a symbol 1754* Void Function::
1745 without a function. 1755* Void Variable::
1746* Void Variable:: The error message for a symbol without a value.
1747@end menu 1756@end menu
1748 1757
1749@node fill-column Example, Void Function, Variables, Variables 1758@node fill-column Example, Void Function, Variables, Variables
@@ -1805,7 +1814,7 @@ Try evaluating this:
1805 1814
1806@need 1250 1815@need 1250
1807@noindent 1816@noindent
1808In GNU Emacs version 21, you will create a @file{*Backtrace*} buffer 1817In GNU Emacs version 22, you will create a @file{*Backtrace*} buffer
1809that says: 1818that says:
1810 1819
1811@smallexample 1820@smallexample
@@ -1825,6 +1834,7 @@ Debugger entered--Lisp error: (void-function fill-column)
1825(Remember, to quit the debugger and make the debugger window go away, 1834(Remember, to quit the debugger and make the debugger window go away,
1826type @kbd{q} in the @file{*Backtrace*} buffer.) 1835type @kbd{q} in the @file{*Backtrace*} buffer.)
1827 1836
1837@ignore
1828@need 800 1838@need 800
1829In GNU Emacs 20 and before, you will produce an error message that says: 1839In GNU Emacs 20 and before, you will produce an error message that says:
1830 1840
@@ -1833,8 +1843,9 @@ Symbol's function definition is void:@: fill-column
1833@end smallexample 1843@end smallexample
1834 1844
1835@noindent 1845@noindent
1836(The message will go away away as soon as you move the cursor or type 1846(The message will go away as soon as you move the cursor or type
1837another key.) 1847another key.)
1848@end ignore
1838 1849
1839@node Void Variable, , Void Function, Variables 1850@node Void Variable, , Void Function, Variables
1840@comment node-name, next, previous, up 1851@comment node-name, next, previous, up
@@ -1854,7 +1865,7 @@ type @kbd{C-x C-e}:
1854 1865
1855@need 1500 1866@need 1500
1856@noindent 1867@noindent
1857In GNU Emacs 21, you will create a @file{*Backtrace*} buffer that 1868In GNU Emacs 22, you will create a @file{*Backtrace*} buffer that
1858says: 1869says:
1859 1870
1860@smallexample 1871@smallexample
@@ -1891,6 +1902,7 @@ Since @code{+} does not have a value bound to it, just the function
1891definition, the error message reported that the symbol's value as a 1902definition, the error message reported that the symbol's value as a
1892variable was void. 1903variable was void.
1893 1904
1905@ignore
1894@need 800 1906@need 800
1895In GNU Emacs version 20 and before, your error message will say: 1907In GNU Emacs version 20 and before, your error message will say:
1896 1908
@@ -1899,7 +1911,8 @@ Symbol's value as variable is void:@: +
1899@end example 1911@end example
1900 1912
1901@noindent 1913@noindent
1902The meaning is the same as in GNU Emacs 21. 1914The meaning is the same as in GNU Emacs 22.
1915@end ignore
1903 1916
1904@node Arguments, set & setq, Variables, List Processing 1917@node Arguments, set & setq, Variables, List Processing
1905@comment node-name, next, previous, up 1918@comment node-name, next, previous, up
@@ -1945,14 +1958,11 @@ to it at the same time. By contrast, in Emacs Lisp, a symbol cannot
1945have two different function definitions at the same time.)} 1958have two different function definitions at the same time.)}
1946 1959
1947@menu 1960@menu
1948* Data types:: Types of data passed to a function. 1961* Data types::
1949* Args as Variable or List:: An argument can be the value 1962* Args as Variable or List::
1950 of a variable or list. 1963* Variable Number of Arguments::
1951* Variable Number of Arguments:: Some functions may take a 1964* Wrong Type of Argument::
1952 variable number of arguments. 1965* message::
1953* Wrong Type of Argument:: Passing an argument of the wrong type
1954 to a function.
1955* message:: A useful function for sending messages.
1956@end menu 1966@end menu
1957 1967
1958@node Data types, Args as Variable or List, Arguments, Arguments 1968@node Data types, Args as Variable or List, Arguments, Arguments
@@ -2028,7 +2038,7 @@ C-e}:
2028 2038
2029@noindent 2039@noindent
2030The value will be a number two more than what you get by evaluating 2040The value will be a number two more than what you get by evaluating
2031@code{fill-column} alone. For me, this is 74, because the value of 2041@code{fill-column} alone. For me, this is 74, because my value of
2032@code{fill-column} is 72. 2042@code{fill-column} is 72.
2033 2043
2034As we have just seen, an argument can be a symbol that returns a value 2044As we have just seen, an argument can be a symbol that returns a value
@@ -2038,7 +2048,7 @@ the arguments to the function @code{concat} are the strings
2038@w{@code{"The "}} and @w{@code{" red foxes."}} and the list 2048@w{@code{"The "}} and @w{@code{" red foxes."}} and the list
2039@code{(number-to-string (+ 2 fill-column))}. 2049@code{(number-to-string (+ 2 fill-column))}.
2040 2050
2041@c For Emacs 21, need number-to-string 2051@c For GNU Emacs 22, need number-to-string
2042@smallexample 2052@smallexample
2043(concat "The " (number-to-string (+ 2 fill-column)) " red foxes.") 2053(concat "The " (number-to-string (+ 2 fill-column)) " red foxes.")
2044@end smallexample 2054@end smallexample
@@ -2122,7 +2132,7 @@ is that @code{+} has tried to add the 2 to the value returned by
2122could not carry out its addition. 2132could not carry out its addition.
2123 2133
2124@need 1250 2134@need 1250
2125In GNU Emacs version 21, you will create and enter a 2135In GNU Emacs version 22, you will create and enter a
2126@file{*Backtrace*} buffer that says: 2136@file{*Backtrace*} buffer that says:
2127 2137
2128@noindent 2138@noindent
@@ -2180,6 +2190,7 @@ addition had been passed the correct type of object, the value passed
2180would have been a number, such as 37, rather than a symbol like 2190would have been a number, such as 37, rather than a symbol like
2181@code{hello}. But then you would not have got the error message. 2191@code{hello}. But then you would not have got the error message.
2182 2192
2193@ignore
2183@need 1250 2194@need 1250
2184In GNU Emacs version 20 and before, the echo area displays an error 2195In GNU Emacs version 20 and before, the echo area displays an error
2185message that says: 2196message that says:
@@ -2190,6 +2201,7 @@ Wrong type argument:@: number-or-marker-p, hello
2190 2201
2191This says, in different words, the same as the top line of the 2202This says, in different words, the same as the top line of the
2192@file{*Backtrace*} buffer. 2203@file{*Backtrace*} buffer.
2204@end ignore
2193 2205
2194@node message, , Wrong Type of Argument, Arguments 2206@node message, , Wrong Type of Argument, Arguments
2195@comment node-name, next, previous, up 2207@comment node-name, next, previous, up
@@ -2300,8 +2312,9 @@ resulting from the evaluation of @code{(- fill-column 32)} is inserted
2300in place of the @samp{%d}; and the value returned by the expression 2312in place of the @samp{%d}; and the value returned by the expression
2301beginning with @code{concat} is inserted in place of the @samp{%s}. 2313beginning with @code{concat} is inserted in place of the @samp{%s}.
2302 2314
2303When I evaluate the expression, the message @code{"He saw 38 red 2315When your fill column is 70 and you evaluate the expression, the
2304foxes leaping."} appears in my echo area. 2316message @code{"He saw 38 red foxes leaping."} appears in your echo
2317area.
2305 2318
2306@node set & setq, Summary, Arguments, List Processing 2319@node set & setq, Summary, Arguments, List Processing
2307@comment node-name, next, previous, up 2320@comment node-name, next, previous, up
@@ -2319,9 +2332,9 @@ The following sections not only describe how @code{set} and @code{setq}
2319work but also illustrate how arguments are passed. 2332work but also illustrate how arguments are passed.
2320 2333
2321@menu 2334@menu
2322* Using set:: Setting values. 2335* Using set::
2323* Using setq:: Setting a quoted value. 2336* Using setq::
2324* Counting:: Using @code{setq} to count. 2337* Counting::
2325@end menu 2338@end menu
2326 2339
2327@node Using set, Using setq, set & setq, set & setq 2340@node Using set, Using setq, set & setq, set & setq
@@ -2340,7 +2353,7 @@ positioning the cursor after the expression and typing @kbd{C-x C-e}.
2340@noindent 2353@noindent
2341The list @code{(rose violet daisy buttercup)} will appear in the echo 2354The list @code{(rose violet daisy buttercup)} will appear in the echo
2342area. This is what is @emph{returned} by the @code{set} function. As a 2355area. This is what is @emph{returned} by the @code{set} function. As a
2343side effect, the symbol @code{flowers} is bound to the list ; that is, 2356side effect, the symbol @code{flowers} is bound to the list; that is,
2344the symbol @code{flowers}, which can be viewed as a variable, is given 2357the symbol @code{flowers}, which can be viewed as a variable, is given
2345the list as its value. (This process, by the way, illustrates how a 2358the list as its value. (This process, by the way, illustrates how a
2346side effect to the Lisp interpreter, setting the value, can be the 2359side effect to the Lisp interpreter, setting the value, can be the
@@ -2529,7 +2542,14 @@ in the list and then at the function definition bound to that symbol.
2529Then the instructions in the function definition are carried out. 2542Then the instructions in the function definition are carried out.
2530 2543
2531@item 2544@item
2532A single-quote, @code{'}, tells the Lisp interpreter that it should 2545A single quotation mark,
2546@ifinfo
2547'
2548@end ifinfo
2549@ifnotinfo
2550@code{'}
2551@end ifnotinfo
2552, tells the Lisp interpreter that it should
2533return the following expression as written, and not evaluate it as it 2553return the following expression as written, and not evaluate it as it
2534would if the quote were not there. 2554would if the quote were not there.
2535 2555
@@ -2584,13 +2604,11 @@ these. In another section, we will study the code of several other
2584buffer-related functions, to see how they were written. 2604buffer-related functions, to see how they were written.
2585 2605
2586@menu 2606@menu
2587* How to Evaluate:: Typing editing commands or @kbd{C-x C-e} 2607* How to Evaluate::
2588 causes evaluation. 2608* Buffer Names::
2589* Buffer Names:: Buffers and files are different. 2609* Getting Buffers::
2590* Getting Buffers:: Getting a buffer itself, not merely its name. 2610* Switching Buffers::
2591* Switching Buffers:: How to change to another buffer. 2611* Buffer Size & Locations::
2592* Buffer Size & Locations:: Where point is located and the size of
2593 the buffer.
2594* Evaluation Exercise:: 2612* Evaluation Exercise::
2595@end menu 2613@end menu
2596 2614
@@ -2656,24 +2674,42 @@ If you are reading this in Info inside of GNU Emacs, you can evaluate
2656each of the following expressions by positioning the cursor after it and 2674each of the following expressions by positioning the cursor after it and
2657typing @kbd{C-x C-e}. 2675typing @kbd{C-x C-e}.
2658 2676
2659@smallexample 2677@example
2660@group 2678@group
2661(buffer-name) 2679(buffer-name)
2662 2680
2663(buffer-file-name) 2681(buffer-file-name)
2664@end group 2682@end group
2665@end smallexample 2683@end example
2666 2684
2667@noindent 2685@noindent
2668When I do this, @file{"introduction.texinfo"} is the value returned by 2686When I do this in Info, the value returned by evaluating
2669evaluating @code{(buffer-name)}, and 2687@code{(buffer-name)} is @file{"*info*"}, and the value returned by
2670@file{"/gnu/work/intro/introduction.texinfo"} is the value returned by 2688evaluating @code{(buffer-file-name)} is @file{nil}.
2671evaluating @code{(buffer-file-name)}. The former is the name of the 2689
2672buffer and the latter is the name of the file. (In the expressions, the 2690On the other hand, while I am writing this Introduction, the value
2673parentheses tell the Lisp interpreter to treat @code{buffer-name} and 2691returned by evaluating @code{(buffer-name)} is
2674@code{buffer-file-name} as functions; without the parentheses, the 2692@file{"introduction.texinfo"}, and the value returned by evaluating
2675interpreter would attempt to evaluate the symbols as variables. 2693@code{(buffer-file-name)} is
2676@xref{Variables}.) 2694@file{"/gnu/work/intro/introduction.texinfo"}.
2695
2696@cindex @code{nil}, history of word
2697The former is the name of the buffer and the latter is the name of the
2698file. In Info, the buffer name is @file{"*info*"}. Info does not
2699point to any file, so the result of evaluating
2700@code{(buffer-file-name)} is @file{nil}. The symbol @code{nil} is
2701from the Latin word for `nothing'; in this case, it means that the
2702buffer is not associated with any file. (In Lisp, @code{nil} is also
2703used to mean `false' and is a synonym for the empty list, @code{()}.)
2704
2705When I am writing, the name of my buffer is
2706@file{"introduction.texinfo"}. The name of the file to which it
2707points is @file{"/gnu/work/intro/introduction.texinfo"}.
2708
2709(In the expressions, the parentheses tell the Lisp interpreter to
2710treat @w{@code{buffer-name}} and @w{@code{buffer-file-name}} as
2711functions; without the parentheses, the interpreter would attempt to
2712evaluate the symbols as variables. @xref{Variables}.)
2677 2713
2678In spite of the distinction between files and buffers, you will often 2714In spite of the distinction between files and buffers, you will often
2679find that people refer to a file when they mean a buffer and vice-versa. 2715find that people refer to a file when they mean a buffer and vice-versa.
@@ -2698,23 +2734,25 @@ great city: once it was merely the place where cargo was warehoused
2698temporarily before being loaded onto ships; then it became a business 2734temporarily before being loaded onto ships; then it became a business
2699and cultural center in its own right. 2735and cultural center in its own right.
2700 2736
2701Not all buffers are associated with files. For example, when you start 2737Not all buffers are associated with files. For example, a
2702an Emacs session by typing the command @code{emacs} alone, without 2738@file{*scratch*} buffer does not visit any file. Similarly, a
2703naming any files, Emacs will start with the @file{*scratch*} buffer on
2704the screen. This buffer is not visiting any file. Similarly, a
2705@file{*Help*} buffer is not associated with any file. 2739@file{*Help*} buffer is not associated with any file.
2706 2740
2707@cindex @code{nil}, history of word 2741In the old days, when you lacked a @file{~/.emacs} file and started an
2708If you switch to the @file{*scratch*} buffer, type @code{(buffer-name)}, 2742Emacs session by typing the command @code{emacs} alone, without naming
2709position the cursor after it, and type @kbd{C-x C-e} to evaluate the 2743any files, Emacs started with the @file{*scratch*} buffer visible.
2710expression, the name @code{"*scratch*"} is returned and will appear in 2744Nowadays, you will see a splash screen. You can follow one of the
2711the echo area. @code{"*scratch*"} is the name of the buffer. However, 2745commands suggested on the splash screen, visit a file, or press the
2712if you type @code{(buffer-file-name)} in the @file{*scratch*} buffer and 2746spacebar to reach the @file{*scratch*} buffer.
2713evaluate that, @code{nil} will appear in the echo area. @code{nil} is 2747
2714from the Latin word for `nothing'; in this case, it means that the 2748If you switch to the @file{*scratch*} buffer, type
2715@file{*scratch*} buffer is not associated with any file. (In Lisp, 2749@code{(buffer-name)}, position the cursor after it, and then type
2716@code{nil} is also used to mean `false' and is a synonym for the empty 2750@kbd{C-x C-e} to evaluate the expression. The name @code{"*scratch*"}
2717list, @code{()}.) 2751will be returned and will appear in the echo area. @code{"*scratch*"}
2752is the name of the buffer. When you type @code{(buffer-file-name)} in
2753the @file{*scratch*} buffer and evaluate that, @code{nil} will appear
2754in the echo area, just as it does when you evaluate
2755@code{(buffer-file-name)} in Info.
2718 2756
2719Incidentally, if you are in the @file{*scratch*} buffer and want the 2757Incidentally, if you are in the @file{*scratch*} buffer and want the
2720value returned by an expression to appear in the @file{*scratch*} 2758value returned by an expression to appear in the @file{*scratch*}
@@ -2771,18 +2809,20 @@ Here is an expression containing the function:
2771@end smallexample 2809@end smallexample
2772 2810
2773@noindent 2811@noindent
2774If you evaluate the expression in the usual way, @file{#<buffer *info*>} 2812If you evaluate this expression in Info in Emacs in the usual way,
2775appears in the echo area. The special format indicates that the 2813@file{#<buffer *info*>} will appear in the echo area. The special
2776buffer itself is being returned, rather than just its name. 2814format indicates that the buffer itself is being returned, rather than
2815just its name.
2777 2816
2778Incidentally, while you can type a number or symbol into a program, you 2817Incidentally, while you can type a number or symbol into a program, you
2779cannot do that with the printed representation of a buffer: the only way 2818cannot do that with the printed representation of a buffer: the only way
2780to get a buffer itself is with a function such as @code{current-buffer}. 2819to get a buffer itself is with a function such as @code{current-buffer}.
2781 2820
2782A related function is @code{other-buffer}. This returns the most 2821A related function is @code{other-buffer}. This returns the most
2783recently selected buffer other than the one you are in currently. If 2822recently selected buffer other than the one you are in currently, not
2784you have recently switched back and forth from the @file{*scratch*} 2823a printed representation of its name. If you have recently switched
2785buffer, @code{other-buffer} will return that buffer. 2824back and forth from the @file{*scratch*} buffer, @code{other-buffer}
2825will return that buffer.
2786 2826
2787@need 800 2827@need 800
2788You can see this by evaluating the expression: 2828You can see this by evaluating the expression:
@@ -2815,12 +2855,13 @@ But first, a brief introduction to the @code{switch-to-buffer}
2815function. When you switched back and forth from Info to the 2855function. When you switched back and forth from Info to the
2816@file{*scratch*} buffer to evaluate @code{(buffer-name)}, you most 2856@file{*scratch*} buffer to evaluate @code{(buffer-name)}, you most
2817likely typed @kbd{C-x b} and then typed @file{*scratch*}@footnote{Or 2857likely typed @kbd{C-x b} and then typed @file{*scratch*}@footnote{Or
2818rather, to save typing, you probably typed just part of the name, such 2858rather, to save typing, you probably only typed @kbd{RET} if the
2819as @code{*sc}, and then pressed your @kbd{TAB} key to cause it to 2859default buffer was @file{*scratch*}, or if it was different, then you
2820expand to the full name; and then typed your @kbd{RET} key.} when 2860typed just part of the name, such as @code{*sc}, pressed your
2821prompted in the minibuffer for the name of the buffer to which you 2861@kbd{TAB} key to cause it to expand to the full name, and then typed
2822wanted to switch. The keystrokes, @kbd{C-x b}, cause the Lisp 2862your @kbd{RET} key.} when prompted in the minibuffer for the name of
2823interpreter to evaluate the interactive function 2863the buffer to which you wanted to switch. The keystrokes, @kbd{C-x
2864b}, cause the Lisp interpreter to evaluate the interactive function
2824@code{switch-to-buffer}. As we said before, this is how Emacs works: 2865@code{switch-to-buffer}. As we said before, this is how Emacs works:
2825different keystrokes call or run different functions. For example, 2866different keystrokes call or run different functions. For example,
2826@kbd{C-f} calls @code{forward-char}, @kbd{M-e} calls 2867@kbd{C-f} calls @code{forward-char}, @kbd{M-e} calls
@@ -2980,16 +3021,16 @@ symbol refers to it.)
2980 3021
2981@menu 3022@menu
2982* Primitive Functions:: 3023* Primitive Functions::
2983* defun:: The @code{defun} special form. 3024* defun::
2984* Install:: Install a function definition. 3025* Install::
2985* Interactive:: Making a function interactive. 3026* Interactive::
2986* Interactive Options:: Different options for @code{interactive}. 3027* Interactive Options::
2987* Permanent Installation:: Installing code permanently. 3028* Permanent Installation::
2988* let:: Creating and initializing local variables. 3029* let::
2989* if:: What if? 3030* if::
2990* else:: If--then--else expressions. 3031* else::
2991* Truth & Falsehood:: What Lisp considers false and true. 3032* Truth & Falsehood::
2992* save-excursion:: Keeping track of point, mark, and buffer. 3033* save-excursion::
2993* Review:: 3034* Review::
2994* defun Exercises:: 3035* defun Exercises::
2995@end menu 3036@end menu
@@ -3226,10 +3267,9 @@ use. (@code{multiply-by-seven} will stay installed until you quit
3226Emacs. To reload code automatically whenever you start Emacs, see 3267Emacs. To reload code automatically whenever you start Emacs, see
3227@ref{Permanent Installation, , Installing Code Permanently}.) 3268@ref{Permanent Installation, , Installing Code Permanently}.)
3228 3269
3229
3230@menu 3270@menu
3231* Effect of installation:: 3271* Effect of installation::
3232* Change a defun:: How to change a function definition. 3272* Change a defun::
3233@end menu 3273@end menu
3234 3274
3235@node Effect of installation, Change a defun, Install, Install 3275@node Effect of installation, Change a defun, Install, Install
@@ -3254,7 +3294,9 @@ function, @code{multiply-by-seven}. When you do this, a
3254 3294
3255@smallexample 3295@smallexample
3256@group 3296@group
3257multiply-by-seven: 3297multiply-by-seven is a Lisp function.
3298(multiply-by-seven NUMBER)
3299
3258Multiply NUMBER by seven. 3300Multiply NUMBER by seven.
3259@end group 3301@end group
3260@end smallexample 3302@end smallexample
@@ -3329,8 +3371,8 @@ value returned. If the returned value were displayed in the echo area
3329each time you typed a key, it would be very distracting. 3371each time you typed a key, it would be very distracting.
3330 3372
3331@menu 3373@menu
3332* Interactive multiply-by-seven:: An overview. 3374* Interactive multiply-by-seven::
3333* multiply-by-seven in detail:: The interactive version. 3375* multiply-by-seven in detail::
3334@end menu 3376@end menu
3335 3377
3336@node Interactive multiply-by-seven, multiply-by-seven in detail, Interactive, Interactive 3378@node Interactive multiply-by-seven, multiply-by-seven in detail, Interactive, Interactive
@@ -3436,14 +3478,14 @@ will evaluate the outer list, passing the values of the second and
3436subsequent elements of the list to the function @code{message}. 3478subsequent elements of the list to the function @code{message}.
3437 3479
3438As we have seen, @code{message} is an Emacs Lisp function especially 3480As we have seen, @code{message} is an Emacs Lisp function especially
3439designed for sending a one line message to a user. (@xref{message, , The 3481designed for sending a one line message to a user. (@xref{message, ,
3440@code{message} function}.) 3482The @code{message} function}.) In summary, the @code{message}
3441In summary, the @code{message} function prints its first argument in the 3483function prints its first argument in the echo area as is, except for
3442echo area as is, except for occurrences of @samp{%d}, @samp{%s}, or 3484occurrences of @samp{%d} or @samp{%s} (and various other %-sequences
3443@samp{%c}. When it sees one of these control sequences, the function 3485which we have not mentioned). When it sees a control sequence, the
3444looks to the second and subsequent arguments and prints the value of the 3486function looks to the second or subsequent arguments and prints the
3445argument in the location in the string where the control sequence is 3487value of the argument in the location in the string where the control
3446located. 3488sequence is located.
3447 3489
3448In the interactive @code{multiply-by-seven} function, the control string 3490In the interactive @code{multiply-by-seven} function, the control string
3449is @samp{%d}, which requires a number, and the value returned by 3491is @samp{%d}, which requires a number, and the value returned by
@@ -3476,62 +3518,67 @@ a function. (@xref{Interactive Codes, , Code Characters for
3476@code{interactive}, elisp, The GNU Emacs Lisp Reference Manual}.) 3518@code{interactive}, elisp, The GNU Emacs Lisp Reference Manual}.)
3477 3519
3478@need 1250 3520@need 1250
3479For example, the character @samp{r} causes Emacs to pass the beginning 3521Consider the function @code{zap-to-char}. Its interactive expression
3480and end of the region (the current values of point and mark) to the 3522is
3481function as two separate arguments. It is used as follows:
3482 3523
3483@smallexample 3524@smallexample
3484(interactive "r") 3525(interactive "p\ncZap to char: ")
3485@end smallexample 3526@end smallexample
3486 3527
3487On the other hand, a @samp{B} tells Emacs to ask for the name of a 3528The first part of the argument to @code{interactive} is @samp{p}, with
3488buffer that will be passed to the function. When it sees a @samp{B}, 3529which you are already familiar. This argument tells Emacs to
3489Emacs will ask for the name by prompting the user in the minibuffer, 3530interpret a `prefix', as a number to be passed to the function. You
3490using a string that follows the @samp{B}, as in @code{"BAppend to 3531can specify a prefix either by typing @kbd{C-u} followed by a number
3491buffer:@: "}. Not only will Emacs prompt for the name, but Emacs will 3532or by typing @key{META} followed by a number. The prefix is the
3492complete the name if you type enough of it and press @key{TAB}. 3533number of specified characters. Thus, if your prefix is three and the
3534specified character is @samp{x}, then you will delete all the text up
3535to and including the third next @samp{x}. If you do not set a prefix,
3536then you delete all the text up to and including the specified
3537character, but no more.
3493 3538
3494A function with two or more arguments can have information passed to 3539The @samp{c} tells the function the name of the character to which to delete.
3495each argument by adding parts to the string that follows 3540
3496@code{interactive}. When you do this, the information is passed to 3541More formally, a function with two or more arguments can have
3497each argument in the same order it is specified in the 3542information passed to each argument by adding parts to the string that
3543follows @code{interactive}. When you do this, the information is
3544passed to each argument in the same order it is specified in the
3498@code{interactive} list. In the string, each part is separated from 3545@code{interactive} list. In the string, each part is separated from
3499the next part by a @samp{\n}, which is a newline. For example, you 3546the next part by a @samp{\n}, which is a newline. For example, you
3500could follow @code{"BAppend to buffer:@: "} with a @samp{\n} and an 3547can follow @samp{p} with a @samp{\n} and an @samp{cZap to char:@: }.
3501@samp{r}. This would cause Emacs to pass the values of point and mark 3548This causes Emacs to pass the value of the prefix argument (if there
3502to the function as well as prompt you for the buffer---three arguments 3549is one) and the character.
3503in all.
3504 3550
3505In this case, the function definition would look like the following, 3551In this case, the function definition looks like the following, where
3506where @code{buffer}, @code{start}, and @code{end} are the symbols to 3552@code{arg} and @code{char} are the symbols to which @code{interactive}
3507which @code{interactive} binds the buffer and the current values of the 3553binds the prefix argument and the specified character:
3508beginning and ending of the region:
3509 3554
3510@smallexample 3555@smallexample
3511@group 3556@group
3512(defun @var{name-of-function} (buffer start end) 3557(defun @var{name-of-function} (arg char)
3513 "@var{documentation}@dots{}" 3558 "@var{documentation}@dots{}"
3514 (interactive "BAppend to buffer:@: \nr") 3559 (interactive "p\ncZap to char: ")
3515 @var{body-of-function}@dots{}) 3560 @var{body-of-function}@dots{})
3516@end group 3561@end group
3517@end smallexample 3562@end smallexample
3518 3563
3519@noindent 3564@noindent
3520(The space after the colon in the prompt makes it look better when you 3565(The space after the colon in the prompt makes it look better when you
3521are prompted. The @code{append-to-buffer} function looks exactly like 3566are prompted. @xref{copy-to-buffer, , The Definition of
3522this. @xref{append-to-buffer, , The Definition of 3567@code{copy-to-buffer}}, for an example.)
3523@code{append-to-buffer}}.)
3524 3568
3525If a function does not have arguments, then @code{interactive} does not 3569When a function does not take arguments, @code{interactive} does not
3526require any. Such a function contains the simple expression 3570require any. Such a function contains the simple expression
3527@code{(interactive)}. The @code{mark-whole-buffer} function is like 3571@code{(interactive)}. The @code{mark-whole-buffer} function is like
3528this. 3572this.
3529 3573
3530Alternatively, if the special letter-codes are not right for your 3574Alternatively, if the special letter-codes are not right for your
3531application, you can pass your own arguments to @code{interactive} as 3575application, you can pass your own arguments to @code{interactive} as
3532a list. @xref{Using Interactive, , Using @code{Interactive}, elisp, The 3576a list.
3533GNU Emacs Lisp Reference Manual}, for more information about this advanced 3577
3534technique. 3578@xref{append-to-buffer, , The Definition of @code{append-to-buffer}},
3579for an example. @xref{Using Interactive, , Using @code{Interactive},
3580elisp, The GNU Emacs Lisp Reference Manual}, for a more complete
3581explanation about this technique.
3535 3582
3536@node Permanent Installation, let, Interactive Options, Writing Defuns 3583@node Permanent Installation, let, Interactive Options, Writing Defuns
3537@comment node-name, next, previous, up 3584@comment node-name, next, previous, up
@@ -3565,11 +3612,11 @@ functions in the files.
3565@xref{Loading Files, , Loading Files}. 3612@xref{Loading Files, , Loading Files}.
3566 3613
3567@item 3614@item
3568On the other hand, if you have code that your whole site will use, it 3615Thirdly, if you have code that your whole site will use, it is usual
3569is usual to put it in a file called @file{site-init.el} that is loaded 3616to put it in a file called @file{site-init.el} that is loaded when
3570when Emacs is built. This makes the code available to everyone who 3617Emacs is built. This makes the code available to everyone who uses
3571uses your machine. (See the @file{INSTALL} file that is part of the 3618your machine. (See the @file{INSTALL} file that is part of the Emacs
3572Emacs distribution.) 3619distribution.)
3573@end itemize 3620@end itemize
3574 3621
3575Finally, if you have code that everyone who uses Emacs may want, you 3622Finally, if you have code that everyone who uses Emacs may want, you
@@ -3621,6 +3668,7 @@ and the two are not intended to refer to the same value. The
3621@end ifnottex 3668@end ifnottex
3622 3669
3623@cindex @samp{local variable} defined 3670@cindex @samp{local variable} defined
3671@cindex @samp{variable, local}, defined
3624The @code{let} special form prevents confusion. @code{let} creates a 3672The @code{let} special form prevents confusion. @code{let} creates a
3625name for a @dfn{local variable} that overshadows any use of the same 3673name for a @dfn{local variable} that overshadows any use of the same
3626name outside the @code{let} expression. This is like understanding 3674name outside the @code{let} expression. This is like understanding
@@ -3726,13 +3774,17 @@ Here, the varlist is @code{((zebra 'stripes) (tiger 'fierce))}.
3726The two variables are @code{zebra} and @code{tiger}. Each variable is 3774The two variables are @code{zebra} and @code{tiger}. Each variable is
3727the first element of a two-element list and each value is the second 3775the first element of a two-element list and each value is the second
3728element of its two-element list. In the varlist, Emacs binds the 3776element of its two-element list. In the varlist, Emacs binds the
3729variable @code{zebra} to the value @code{stripes}, and binds the 3777variable @code{zebra} to the value @code{stripes}@footnote{According
3778to Jared Diamond in @cite{Guns, Germs, and Steel}, ``@dots{} zebras
3779become impossibly dangerous as they grow older'' but the claim here is
3780that they do not become fierce like a tiger. (1997, W. W. Norton and
3781Co., ISBN 0-393-03894-2, page 171)}, and binds the
3730variable @code{tiger} to the value @code{fierce}. In this example, 3782variable @code{tiger} to the value @code{fierce}. In this example,
3731both values are symbols preceded by a quote. The values could just as 3783both values are symbols preceded by a quote. The values could just as
3732well have been another list or a string. The body of the @code{let} 3784well have been another list or a string. The body of the @code{let}
3733follows after the list holding the variables. In this example, the body 3785follows after the list holding the variables. In this example, the
3734is a list that uses the @code{message} function to print a string in 3786body is a list that uses the @code{message} function to print a string
3735the echo area. 3787in the echo area.
3736 3788
3737@need 1500 3789@need 1500
3738You may evaluate the example in the usual fashion, by placing the 3790You may evaluate the example in the usual fashion, by placing the
@@ -3818,7 +3870,7 @@ such as, ``if it is warm and sunny, then go to the beach!''
3818 3870
3819@menu 3871@menu
3820* if in more detail:: 3872* if in more detail::
3821* type-of-animal in detail:: An example of an @code{if} expression. 3873* type-of-animal in detail::
3822@end menu 3874@end menu
3823 3875
3824@node if in more detail, type-of-animal in detail, if, if 3876@node if in more detail, type-of-animal in detail, if, if
@@ -4086,8 +4138,8 @@ when you evaluate @code{(type-of-animal 'zebra)}, you will see
4086(Of course, if the @var{characteristic} were @code{ferocious}, the 4138(Of course, if the @var{characteristic} were @code{ferocious}, the
4087message @code{"It's not fierce!"} would be printed; and it would be 4139message @code{"It's not fierce!"} would be printed; and it would be
4088misleading! When you write code, you need to take into account the 4140misleading! When you write code, you need to take into account the
4089possibility that some such argument will be tested by the @code{if} and 4141possibility that some such argument will be tested by the @code{if}
4090write your program accordingly.) 4142and write your program accordingly.)
4091 4143
4092@node Truth & Falsehood, save-excursion, else, Writing Defuns 4144@node Truth & Falsehood, save-excursion, else, Writing Defuns
4093@comment node-name, next, previous, up 4145@comment node-name, next, previous, up
@@ -4106,11 +4158,11 @@ The expression that tests for truth is interpreted as @dfn{true}
4106if the result of evaluating it is a value that is not @code{nil}. In 4158if the result of evaluating it is a value that is not @code{nil}. In
4107other words, the result of the test is considered true if the value 4159other words, the result of the test is considered true if the value
4108returned is a number such as 47, a string such as @code{"hello"}, or a 4160returned is a number such as 47, a string such as @code{"hello"}, or a
4109symbol (other than @code{nil}) such as @code{flowers}, or a list, or 4161symbol (other than @code{nil}) such as @code{flowers}, or a list (so
4110even a buffer! 4162long as it is not empty), or even a buffer!
4111 4163
4112@menu 4164@menu
4113* nil explained:: @code{nil} has two meanings. 4165* nil explained::
4114@end menu 4166@end menu
4115 4167
4116@node nil explained, , Truth & Falsehood, Truth & Falsehood 4168@node nil explained, , Truth & Falsehood, Truth & Falsehood
@@ -4198,7 +4250,7 @@ purpose is to keep the user from being surprised and disturbed by
4198unexpected movement of point or mark. 4250unexpected movement of point or mark.
4199 4251
4200@menu 4252@menu
4201* Point and mark:: A review of various locations. 4253* Point and mark::
4202* Template for save-excursion:: 4254* Template for save-excursion::
4203@end menu 4255@end menu
4204 4256
@@ -4256,8 +4308,8 @@ abnormal exit''). This feature is very helpful.
4256In addition to recording the values of point and mark, 4308In addition to recording the values of point and mark,
4257@code{save-excursion} keeps track of the current buffer, and restores 4309@code{save-excursion} keeps track of the current buffer, and restores
4258it, too. This means you can write code that will change the buffer and 4310it, too. This means you can write code that will change the buffer and
4259have @code{save-excursion} switch you back to the original buffer. This 4311have @code{save-excursion} switch you back to the original buffer.
4260is how @code{save-excursion} is used in @code{append-to-buffer}. 4312This is how @code{save-excursion} is used in @code{append-to-buffer}.
4261(@xref{append-to-buffer, , The Definition of @code{append-to-buffer}}.) 4313(@xref{append-to-buffer, , The Definition of @code{append-to-buffer}}.)
4262 4314
4263@node Template for save-excursion, , Point and mark, save-excursion 4315@node Template for save-excursion, , Point and mark, save-excursion
@@ -4334,7 +4386,9 @@ documentation, an optional interactive declaration, and the body of the
4334definition. 4386definition.
4335 4387
4336@need 1250 4388@need 1250
4337For example: 4389For example, in an early version of Emacs, the function definition was
4390as follows. (It is slightly more complex now that it seeks the first
4391non-whitespace character rather than the first visible character.)
4338 4392
4339@smallexample 4393@smallexample
4340@group 4394@group
@@ -4346,6 +4400,24 @@ For example:
4346@end group 4400@end group
4347@end smallexample 4401@end smallexample
4348 4402
4403@ignore
4404In GNU Emacs 22,
4405
4406(defun backward-to-indentation (&optional arg)
4407 "Move backward ARG lines and position at first nonblank character."
4408 (interactive "p")
4409 (forward-line (- (or arg 1)))
4410 (skip-chars-forward " \t"))
4411
4412(defun back-to-indentation ()
4413 "Move point to the first non-whitespace character on this line."
4414 (interactive)
4415 (beginning-of-line 1)
4416 (skip-syntax-forward " " (line-end-position))
4417 ;; Move back over chars that have whitespace syntax but have the p flag.
4418 (backward-prefix-chars))
4419@end ignore
4420
4349@item interactive 4421@item interactive
4350Declare to the interpreter that the function can be used 4422Declare to the interpreter that the function can be used
4351interactively. This special form may be followed by a string with one 4423interactively. This special form may be followed by a string with one
@@ -4430,10 +4502,10 @@ For example,
4430@smallexample 4502@smallexample
4431@group 4503@group
4432(if (string-equal 4504(if (string-equal
4433 (number-to-string 21) 4505 (number-to-string 22)
4434 (substring (emacs-version) 10 12)) 4506 (substring (emacs-version) 10 12))
4435 (message "This is version 21 Emacs") 4507 (message "This is version 22 Emacs")
4436 (message "This is not version 21 Emacs")) 4508 (message "This is not version 22 Emacs"))
4437@end group 4509@end group
4438@end smallexample 4510@end smallexample
4439 4511
@@ -4487,6 +4559,7 @@ arguments that follow the string. The argument used by @samp{%s} must
4487be a string or a symbol; the argument used by @samp{%d} must be a 4559be a string or a symbol; the argument used by @samp{%d} must be a
4488number. The argument used by @samp{%c} must be an @sc{ascii} code 4560number. The argument used by @samp{%c} must be an @sc{ascii} code
4489number; it will be printed as the character with that @sc{ascii} code. 4561number; it will be printed as the character with that @sc{ascii} code.
4562(Various other %-sequences have not been mentioned.)
4490 4563
4491@item setq 4564@item setq
4492@itemx set 4565@itemx set
@@ -4567,13 +4640,11 @@ these definitions. The functions described here are all related to
4567buffers. Later, we will study other functions. 4640buffers. Later, we will study other functions.
4568 4641
4569@menu 4642@menu
4570* Finding More:: How to find more information. 4643* Finding More::
4571* simplified-beginning-of-buffer:: Shows @code{goto-char}, 4644* simplified-beginning-of-buffer::
4572 @code{point-min}, and @code{push-mark}. 4645* mark-whole-buffer::
4573* mark-whole-buffer:: Almost the same as @code{beginning-of-buffer}. 4646* append-to-buffer::
4574* append-to-buffer:: Uses @code{save-excursion} and 4647* Buffer Related Review::
4575 @code{insert-buffer-substring}.
4576* Buffer Related Review:: Review.
4577* Buffer Exercises:: 4648* Buffer Exercises::
4578@end menu 4649@end menu
4579 4650
@@ -4591,12 +4662,23 @@ variable by typing @kbd{C-h v} and then the name of the variable (and
4591then @key{RET}). 4662then @key{RET}).
4592 4663
4593@cindex Find source of function 4664@cindex Find source of function
4594In versions 20 and higher, when a function is written in Emacs Lisp, 4665@c In version 22
4595@code{describe-function} will also tell you the location of the 4666When a function is written in Emacs Lisp, @code{describe-function}
4596function definition. If you move point over the file name and press 4667will also tell you the location of the function definition.
4668
4669Put point into the name of the file that contains the function and
4670press the @key{RET} key. In this case, @key{RET} means
4671@code{push-button} rather than `return' or `enter'. Emacs will take
4672you directly to the function definition.
4673
4674@ignore
4675Not In version 22
4676
4677If you move point over the file name and press
4597the @key{RET} key, which in this case means @code{help-follow} rather 4678the @key{RET} key, which in this case means @code{help-follow} rather
4598than `return' or `enter', Emacs will take you directly to the function 4679than `return' or `enter', Emacs will take you directly to the function
4599definition. 4680definition.
4681@end ignore
4600 4682
4601More generally, if you want to see a function in its original source 4683More generally, if you want to see a function in its original source
4602file, you can use the @code{find-tags} function to jump to it. 4684file, you can use the @code{find-tags} function to jump to it.
@@ -4619,7 +4701,7 @@ screen. To switch back to your current buffer, type @kbd{C-x b
4619@key{RET}}. (On some keyboards, the @key{META} key is labelled 4701@key{RET}}. (On some keyboards, the @key{META} key is labelled
4620@key{ALT}.) 4702@key{ALT}.)
4621 4703
4622@c !!! 21.0.100 tags table location in this paragraph 4704@c !!! 22.0.100 tags table location in this paragraph
4623@cindex TAGS table, specifying 4705@cindex TAGS table, specifying
4624@findex find-tags 4706@findex find-tags
4625Depending on how the initial default values of your copy of Emacs are 4707Depending on how the initial default values of your copy of Emacs are
@@ -4629,15 +4711,15 @@ interested in Emacs sources, the tags table you will most likely want,
4629if it has already been created for you, will be in a subdirectory of 4711if it has already been created for you, will be in a subdirectory of
4630the @file{/usr/local/share/emacs/} directory; thus you would use the 4712the @file{/usr/local/share/emacs/} directory; thus you would use the
4631@code{M-x visit-tags-table} command and specify a pathname such as 4713@code{M-x visit-tags-table} command and specify a pathname such as
4632@file{/usr/local/share/emacs/21.0.100/lisp/TAGS} or 4714@file{/usr/local/share/emacs/22.0.100/lisp/TAGS}. If the tags table
4633@file{/usr/local/src/emacs/src/TAGS}. If the tags table has 4715has not already been created, you will have to create it yourself. It
4634not already been created, you will have to create it yourself. 4716will in a file such as @file{/usr/local/src/emacs/src/TAGS}.
4635 4717
4636@need 1250 4718@need 1250
4637To create a @file{TAGS} file in a specific directory, switch to that 4719To create a @file{TAGS} file in a specific directory, switch to that
4638directory in Emacs using @kbd{M-x cd} command, or list the directory 4720directory in Emacs using @kbd{M-x cd} command, or list the directory
4639with @kbd{C-x d} (@code{dired}). Then run the compile command, with 4721with @kbd{C-x d} (@code{dired}). Then run the compile command, with
4640@w{@code{etags *.el}} as the command to execute 4722@w{@code{etags *.el}} as the command to execute:
4641 4723
4642@smallexample 4724@smallexample
4643M-x compile RET etags *.el RET 4725M-x compile RET etags *.el RET
@@ -4773,13 +4855,15 @@ example, the documentation for @code{goto-char} is:
4773 4855
4774@smallexample 4856@smallexample
4775@group 4857@group
4776One arg, a number. Set point to that number. 4858Set point to POSITION, a number or marker.
4777Beginning of buffer is position (point-min), 4859Beginning of buffer is position (point-min), end is (point-max).
4778end is (point-max).
4779@end group 4860@end group
4780@end smallexample 4861@end smallexample
4781 4862
4782@noindent 4863@noindent
4864The function's one argument is the desired position.
4865
4866@noindent
4783(The prompt for @code{describe-function} will offer you the symbol 4867(The prompt for @code{describe-function} will offer you the symbol
4784under or preceding the cursor, so you can save typing by positioning 4868under or preceding the cursor, so you can save typing by positioning
4785the cursor right over or after the function and then typing @kbd{C-h f 4869the cursor right over or after the function and then typing @kbd{C-h f
@@ -4805,28 +4889,29 @@ marks a whole buffer as a region by putting point at the beginning and
4805a mark at the end of the buffer. It is generally bound to @kbd{C-x 4889a mark at the end of the buffer. It is generally bound to @kbd{C-x
4806h}. 4890h}.
4807 4891
4808
4809@menu 4892@menu
4810* mark-whole-buffer overview:: 4893* mark-whole-buffer overview::
4811* Body of mark-whole-buffer:: Only three lines of code. 4894* Body of mark-whole-buffer::
4812@end menu 4895@end menu
4813 4896
4814
4815@node mark-whole-buffer overview, Body of mark-whole-buffer, mark-whole-buffer, mark-whole-buffer 4897@node mark-whole-buffer overview, Body of mark-whole-buffer, mark-whole-buffer, mark-whole-buffer
4816@ifnottex 4898@ifnottex
4817@unnumberedsubsec An overview of @code{mark-whole-buffer} 4899@unnumberedsubsec An overview of @code{mark-whole-buffer}
4818@end ifnottex 4900@end ifnottex
4819 4901
4820@need 1250 4902@need 1250
4821In GNU Emacs 20, the code for the complete function looks like this: 4903In GNU Emacs 22, the code for the complete function looks like this:
4822 4904
4823@smallexample 4905@smallexample
4824@group 4906@group
4825(defun mark-whole-buffer () 4907(defun mark-whole-buffer ()
4826 "Put point at beginning and mark at end of buffer." 4908 "Put point at beginning and mark at end of buffer.
4909You probably should not use this function in Lisp programs;
4910it is usually a mistake for a Lisp function to use any subroutine
4911that uses or sets the mark."
4827 (interactive) 4912 (interactive)
4828 (push-mark (point)) 4913 (push-mark (point))
4829 (push-mark (point-max)) 4914 (push-mark (point-max) nil t)
4830 (goto-char (point-min))) 4915 (goto-char (point-min)))
4831@end group 4916@end group
4832@end smallexample 4917@end smallexample
@@ -4863,10 +4948,11 @@ previous section.
4863The body of the @code{mark-whole-buffer} function consists of three 4948The body of the @code{mark-whole-buffer} function consists of three
4864lines of code: 4949lines of code:
4865 4950
4951@c GNU Emacs 22
4866@smallexample 4952@smallexample
4867@group 4953@group
4868(push-mark (point)) 4954(push-mark (point))
4869(push-mark (point-max)) 4955(push-mark (point-max) nil t)
4870(goto-char (point-min)) 4956(goto-char (point-min))
4871@end group 4957@end group
4872@end smallexample 4958@end smallexample
@@ -4889,34 +4975,36 @@ so as to parallel the structure of the next line. In any case, the
4889line causes Emacs to determine the position of point and set a mark 4975line causes Emacs to determine the position of point and set a mark
4890there. 4976there.
4891 4977
4892The next line of @code{mark-whole-buffer} is @code{(push-mark (point-max)}. 4978In earlier versions of GNU Emacs, the next line of
4893This expression sets a mark at the point in the buffer 4979@code{mark-whole-buffer} was @code{(push-mark (point-max))}. This
4894that has the highest number. This will be the end of the buffer (or, 4980expression sets a mark at the point in the buffer that has the highest
4895if the buffer is narrowed, the end of the accessible portion of the 4981number. This will be the end of the buffer (or, if the buffer is
4896buffer. @xref{Narrowing & Widening, , Narrowing and Widening}, for 4982narrowed, the end of the accessible portion of the buffer.
4897more about narrowing.) After this mark has been set, the previous 4983@xref{Narrowing & Widening, , Narrowing and Widening}, for more about
4898mark, the one set at point, is no longer set, but Emacs remembers its 4984narrowing.) After this mark has been set, the previous mark, the one
4899position, just as all other recent marks are always remembered. This 4985set at point, is no longer set, but Emacs remembers its position, just
4900means that you can, if you wish, go back to that position by typing 4986as all other recent marks are always remembered. This means that you
4901@kbd{C-u C-@key{SPC}} twice. 4987can, if you wish, go back to that position by typing @kbd{C-u
4988C-@key{SPC}} twice.
4902 4989
4903(In GNU Emacs 21, the @code{(push-mark (point-max)} is slightly more 4990@need 1250
4904complicated than shown here. The line reads 4991In GNU Emacs 22, the @code{(point-max)} is slightly more complicated.
4992The line reads
4905 4993
4906@smallexample 4994@smallexample
4907(push-mark (point-max) nil t) 4995(push-mark (point-max) nil t)
4908@end smallexample 4996@end smallexample
4909 4997
4910@noindent 4998@noindent
4911(The expression works nearly the same as before. It sets a mark at 4999The expression works nearly the same as before. It sets a mark at the
4912the highest numbered place in the buffer that it can. However, in 5000highest numbered place in the buffer that it can. However, in this
4913this version, @code{push-mark} has two additional arguments. The 5001version, @code{push-mark} has two additional arguments. The second
4914second argument to @code{push-mark} is @code{nil}. This tells the 5002argument to @code{push-mark} is @code{nil}. This tells the function
4915function it @emph{should} display a message that says `Mark set' when 5003it @emph{should} display a message that says `Mark set' when it pushes
4916it pushes the mark. The third argument is @code{t}. This tells 5004the mark. The third argument is @code{t}. This tells
4917@code{push-mark} to activate the mark when Transient Mark mode is 5005@code{push-mark} to activate the mark when Transient Mark mode is
4918turned on. Transient Mark mode highlights the currently active 5006turned on. Transient Mark mode highlights the currently active
4919region. It is usually turned off.) 5007region. It is often turned off.
4920 5008
4921Finally, the last line of the function is @code{(goto-char 5009Finally, the last line of the function is @code{(goto-char
4922(point-min)))}. This is written exactly the same way as it is written 5010(point-min)))}. This is written exactly the same way as it is written
@@ -4932,16 +5020,16 @@ region.
4932@section The Definition of @code{append-to-buffer} 5020@section The Definition of @code{append-to-buffer}
4933@findex append-to-buffer 5021@findex append-to-buffer
4934 5022
4935The @code{append-to-buffer} command is very nearly as simple as the 5023The @code{append-to-buffer} command is more complex than the
4936@code{mark-whole-buffer} command. What it does is copy the region (that 5024@code{mark-whole-buffer} command. What it does is copy the region
4937is, the part of the buffer between point and mark) from the current 5025(that is, the part of the buffer between point and mark) from the
4938buffer to a specified buffer. 5026current buffer to a specified buffer.
4939 5027
4940@menu 5028@menu
4941* append-to-buffer overview:: 5029* append-to-buffer overview::
4942* append interactive:: A two part interactive expression. 5030* append interactive::
4943* append-to-buffer body:: Incorporates a @code{let} expression. 5031* append-to-buffer body::
4944* append save-excursion:: How the @code{save-excursion} works. 5032* append save-excursion::
4945@end menu 5033@end menu
4946 5034
4947@node append-to-buffer overview, append interactive, append-to-buffer, append-to-buffer 5035@node append-to-buffer overview, append interactive, append-to-buffer, append-to-buffer
@@ -4954,10 +5042,15 @@ The @code{append-to-buffer} command uses the
4954@code{insert-buffer-substring} function to copy the region. 5042@code{insert-buffer-substring} function to copy the region.
4955@code{insert-buffer-substring} is described by its name: it takes a 5043@code{insert-buffer-substring} is described by its name: it takes a
4956string of characters from part of a buffer, a ``substring'', and 5044string of characters from part of a buffer, a ``substring'', and
4957inserts them into another buffer. Most of @code{append-to-buffer} is 5045inserts them into another buffer.
5046
5047Most of @code{append-to-buffer} is
4958concerned with setting up the conditions for 5048concerned with setting up the conditions for
4959@code{insert-buffer-substring} to work: the code must specify both the 5049@code{insert-buffer-substring} to work: the code must specify both the
4960buffer to which the text will go and the region that will be copied. 5050buffer to which the text will go, the window it comes from and goes
5051to, and the region that will be copied.
5052
5053@need 1250
4961Here is the complete text of the function: 5054Here is the complete text of the function:
4962 5055
4963@smallexample 5056@smallexample
@@ -4969,13 +5062,26 @@ It is inserted into that buffer before its point.
4969 5062
4970@group 5063@group
4971When calling from a program, give three arguments: 5064When calling from a program, give three arguments:
4972a buffer or the name of one, and two character numbers 5065BUFFER (or buffer name), START and END.
4973specifying the portion of the current buffer to be copied." 5066START and END specify the portion of the current buffer to be copied."
4974 (interactive "BAppend to buffer:@: \nr") 5067 (interactive
5068 (list (read-buffer "Append to buffer: " (other-buffer
5069 (current-buffer) t))
5070 (region-beginning) (region-end)))
5071@end group
5072@group
4975 (let ((oldbuf (current-buffer))) 5073 (let ((oldbuf (current-buffer)))
4976 (save-excursion 5074 (save-excursion
4977 (set-buffer (get-buffer-create buffer)) 5075 (let* ((append-to (get-buffer-create buffer))
4978 (insert-buffer-substring oldbuf start end)))) 5076 (windows (get-buffer-window-list append-to t t))
5077 point)
5078 (set-buffer append-to)
5079 (setq point (point))
5080 (barf-if-buffer-read-only)
5081 (insert-buffer-substring oldbuf start end)
5082 (dolist (window windows)
5083 (when (= (window-point window) point)
5084 (set-window-point window (point))))))))
4979@end group 5085@end group
4980@end smallexample 5086@end smallexample
4981 5087
@@ -4989,7 +5095,7 @@ function, it looks like this (with several slots filled in):
4989@group 5095@group
4990(defun append-to-buffer (buffer start end) 5096(defun append-to-buffer (buffer start end)
4991 "@var{documentation}@dots{}" 5097 "@var{documentation}@dots{}"
4992 (interactive "BAppend to buffer:@: \nr") 5098 (interactive @dots{})
4993 @var{body}@dots{}) 5099 @var{body}@dots{})
4994@end group 5100@end group
4995@end smallexample 5101@end smallexample
@@ -5000,7 +5106,12 @@ the @code{start} and @code{end} of the region in the current buffer that
5000will be copied. 5106will be copied.
5001 5107
5002The next part of the function is the documentation, which is clear and 5108The next part of the function is the documentation, which is clear and
5003complete. 5109complete. As is conventional, the three arguments are written in
5110upper case so you will notice them easily. Even better, they are
5111described in the same order as in the argument list.
5112
5113Note that the documentation distinguishes between a buffer and its
5114name. (The function can handle either.)
5004 5115
5005@node append interactive, append-to-buffer body, append-to-buffer overview, append-to-buffer 5116@node append interactive, append-to-buffer body, append-to-buffer overview, append-to-buffer
5006@comment node-name, next, previous, up 5117@comment node-name, next, previous, up
@@ -5012,30 +5123,94 @@ review of @code{interactive}, see @ref{Interactive, , Making a
5012Function Interactive}.) The expression reads as follows: 5123Function Interactive}.) The expression reads as follows:
5013 5124
5014@smallexample 5125@smallexample
5015(interactive "BAppend to buffer:@: \nr") 5126(interactive
5127 (list (read-buffer
5128 "Append to buffer: "
5129 (other-buffer (current-buffer) t))
5130 (region-beginning)
5131 (region-end)))
5016@end smallexample 5132@end smallexample
5017 5133
5018@noindent 5134@noindent
5019This expression has an argument inside of quotation marks and that 5135This expression is not one with letters standing for parts, as
5020argument has two parts, separated by @samp{\n}. 5136described earlier. Instead, it starts a list with thee parts.
5137
5138The first part of the list is an expression to read the name of a
5139buffer and return it as a string. That is @code{read-buffer}. The
5140function requires a prompt as its first argument, @samp{"Append to
5141buffer: "}. Its second argument tells the command what value to
5142provide if you don't specify anything.
5143
5144In this case that second argument is an expression containing the
5145function @code{other-buffer}, an exception, and a @samp{t}, standing
5146for true.
5147
5148The first argument to @code{other-buffer}, the exception, is yet
5149another function, @code{current-buffer}. That is not going to be
5150returned. The second argument is the symbol for true, @code{t}. that
5151tells @code{other-buffer} that it may show visible buffers (except in
5152this case, it will not show the current buffer, which makes sense).
5153
5154@need 1250
5155The expression looks like this:
5021 5156
5022The first part is @samp{BAppend to buffer:@: }. Here, the @samp{B} 5157@smallexample
5023tells Emacs to ask for the name of the buffer that will be passed to the 5158(other-buffer (current-buffer) t)
5024function. Emacs will ask for the name by prompting the user in the 5159@end smallexample
5025minibuffer, using the string following the @samp{B}, which is the string 5160
5026@samp{Append to buffer:@: }. Emacs then binds the variable @code{buffer} 5161The second and third arguments to the @code{list} expression are
5027in the function's argument list to the specified buffer. 5162@code{(region-beginning)} and @code{(region-end)}. These two
5163functions specify the beginning and end of the text to be appended.
5164
5165@need 1250
5166Originally, the command used the letters @samp{B} and @samp{r}.
5167The whole @code{interactive} expression looked like this:
5168
5169@smallexample
5170(interactive "BAppend to buffer:@: \nr")
5171@end smallexample
5172
5173@noindent
5174But when that was done, the default value of the buffer switched to
5175was invisible. That was not wanted.
5028 5176
5029The newline, @samp{\n}, separates the first part of the argument from 5177(The prompt was separated from the second argument with a newline,
5030the second part. It is followed by an @samp{r} that tells Emacs to bind 5178@samp{\n}. It was followed by an @samp{r} that told Emacs to bind the
5031the two arguments that follow the symbol @code{buffer} in the function's 5179two arguments that follow the symbol @code{buffer} in the function's
5032argument list (that is, @code{start} and @code{end}) to the values of 5180argument list (that is, @code{start} and @code{end}) to the values of
5033point and mark. 5181point and mark. That argument worked fine.)
5034 5182
5035@node append-to-buffer body, append save-excursion, append interactive, append-to-buffer 5183@node append-to-buffer body, append save-excursion, append interactive, append-to-buffer
5036@comment node-name, next, previous, up 5184@comment node-name, next, previous, up
5037@subsection The Body of @code{append-to-buffer} 5185@subsection The Body of @code{append-to-buffer}
5038 5186
5187@ignore
5188in GNU Emacs 22 in /usr/local/src/emacs/lisp/simple.el
5189
5190(defun append-to-buffer (buffer start end)
5191 "Append to specified buffer the text of the region.
5192It is inserted into that buffer before its point.
5193
5194When calling from a program, give three arguments:
5195BUFFER (or buffer name), START and END.
5196START and END specify the portion of the current buffer to be copied."
5197 (interactive
5198 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
5199 (region-beginning) (region-end)))
5200 (let ((oldbuf (current-buffer)))
5201 (save-excursion
5202 (let* ((append-to (get-buffer-create buffer))
5203 (windows (get-buffer-window-list append-to t t))
5204 point)
5205 (set-buffer append-to)
5206 (setq point (point))
5207 (barf-if-buffer-read-only)
5208 (insert-buffer-substring oldbuf start end)
5209 (dolist (window windows)
5210 (when (= (window-point window) point)
5211 (set-window-point window (point))))))))
5212@end ignore
5213
5039The body of the @code{append-to-buffer} function begins with @code{let}. 5214The body of the @code{append-to-buffer} function begins with @code{let}.
5040 5215
5041As we have seen before (@pxref{let, , @code{let}}), the purpose of a 5216As we have seen before (@pxref{let, , @code{let}}), the purpose of a
@@ -5052,7 +5227,7 @@ whole by showing a template for @code{append-to-buffer} with the
5052@group 5227@group
5053(defun append-to-buffer (buffer start end) 5228(defun append-to-buffer (buffer start end)
5054 "@var{documentation}@dots{}" 5229 "@var{documentation}@dots{}"
5055 (interactive "BAppend to buffer:@: \nr") 5230 (interactive @dots{})
5056 (let ((@var{variable} @var{value})) 5231 (let ((@var{variable} @var{value}))
5057 @var{body}@dots{}) 5232 @var{body}@dots{})
5058@end group 5233@end group
@@ -5142,7 +5317,7 @@ this:
5142 5317
5143@need 1500 5318@need 1500
5144@noindent 5319@noindent
5145This formatting convention makes it easy to see that the two lines in 5320This formatting convention makes it easy to see that the lines in
5146the body of the @code{save-excursion} are enclosed by the parentheses 5321the body of the @code{save-excursion} are enclosed by the parentheses
5147associated with @code{save-excursion}, just as the 5322associated with @code{save-excursion}, just as the
5148@code{save-excursion} itself is enclosed by the parentheses associated 5323@code{save-excursion} itself is enclosed by the parentheses associated
@@ -5152,8 +5327,10 @@ with the @code{let}:
5152@group 5327@group
5153(let ((oldbuf (current-buffer))) 5328(let ((oldbuf (current-buffer)))
5154 (save-excursion 5329 (save-excursion
5155 (set-buffer (get-buffer-create buffer)) 5330 @dots{}
5156 (insert-buffer-substring oldbuf start end)))) 5331 (set-buffer @dots{})
5332 (insert-buffer-substring oldbuf start end)
5333 @dots{}))
5157@end group 5334@end group
5158@end smallexample 5335@end smallexample
5159 5336
@@ -5174,70 +5351,84 @@ of filling in the slots of a template:
5174@need 1200 5351@need 1200
5175@noindent 5352@noindent
5176In this function, the body of the @code{save-excursion} contains only 5353In this function, the body of the @code{save-excursion} contains only
5177two expressions. The body looks like this: 5354one expression, the @code{let*} expression. You know about a
5355@code{let} function. The @code{let*} function is different. It has a
5356@samp{*} in its name. It enables Emacs to set each variable in its
5357varlist in sequence, one after another.
5178 5358
5179@smallexample 5359Its critical feature is that variables later in the varlist can make
5180@group 5360use of the values to which Emacs set variables earlier in the varlist.
5181(set-buffer (get-buffer-create buffer)) 5361@xref{fwd-para let, , The @code{let*} expression}.
5182(insert-buffer-substring oldbuf start end)
5183@end group
5184@end smallexample
5185 5362
5186When the @code{append-to-buffer} function is evaluated, the two 5363We will skip functions like @code{let*} and focus on two: the
5187expressions in the body of the @code{save-excursion} are evaluated in 5364@code{set-buffer} function and the @code{insert-buffer-substring}
5188sequence. The value of the last expression is returned as the value of 5365function.
5189the @code{save-excursion} function; the other expression is evaluated
5190only for its side effects.
5191 5366
5192The first line in the body of the @code{save-excursion} uses the 5367@need 1250
5193@code{set-buffer} function to change the current buffer to the one 5368In the old days, the @code{set-buffer} expression was simply
5194specified in the first argument to @code{append-to-buffer}. (Changing
5195the buffer is the side effect; as we have said before, in Lisp, a side
5196effect is often the primary thing we want.) The second line does the
5197primary work of the function.
5198 5369
5199The @code{set-buffer} function changes Emacs' attention to the buffer to 5370@smallexample
5200which the text will be copied and from which @code{save-excursion} will 5371(set-buffer (get-buffer-create buffer))
5201return. 5372@end smallexample
5202 5373
5203@need 800 5374@need 1250
5204The line looks like this: 5375@noindent
5376but now it is
5205 5377
5206@smallexample 5378@smallexample
5207(set-buffer (get-buffer-create buffer)) 5379(set-buffer append-to)
5208@end smallexample 5380@end smallexample
5209 5381
5210The innermost expression of this list is @code{(get-buffer-create 5382@noindent
5211buffer)}. This expression uses the @code{get-buffer-create} function, 5383@code{append-to} is bound to @code{(get-buffer-create buffer)} earlier
5212which either gets the named buffer, or if it does not exist, creates one 5384on in the @code{let*} expression. That extra binding would not be
5213with the given name. This means you can use @code{append-to-buffer} to 5385necessary except for that @code{append-to} is used later in the
5214put text into a buffer that did not previously exist. 5386varlist as an argument to @code{get-buffer-window-list}.
5387
5388@ignore
5389in GNU Emacs 22
5390
5391 (let ((oldbuf (current-buffer)))
5392 (save-excursion
5393 (let* ((append-to (get-buffer-create buffer))
5394 (windows (get-buffer-window-list append-to t t))
5395 point)
5396 (set-buffer append-to)
5397 (setq point (point))
5398 (barf-if-buffer-read-only)
5399 (insert-buffer-substring oldbuf start end)
5400 (dolist (window windows)
5401 (when (= (window-point window) point)
5402 (set-window-point window (point))))))))
5403@end ignore
5215 5404
5216@code{get-buffer-create} also keeps @code{set-buffer} from getting an 5405The @code{append-to-buffer} function definition inserts text from the
5217unnecessary error: @code{set-buffer} needs a buffer to go to; if you 5406buffer in which you are currently to a named buffer. It happens that
5218were to specify a buffer that does not exist, Emacs would baulk. 5407@code{insert-buffer-substring} copies text from another buffer to the
5219Since @code{get-buffer-create} will create a buffer if none exists, 5408current buffer, just the reverse---that is why the
5220@code{set-buffer} is always provided with a buffer. 5409@code{append-to-buffer} definition starts out with a @code{let} that
5410binds the local symbol @code{oldbuf} to the value returned by
5411@code{current-buffer}.
5221 5412
5222@need 1250 5413@need 1250
5223The last line of @code{append-to-buffer} does the work of appending 5414The @code{insert-buffer-substring} expression looks like this:
5224the text:
5225 5415
5226@smallexample 5416@smallexample
5227(insert-buffer-substring oldbuf start end) 5417(insert-buffer-substring oldbuf start end)
5228@end smallexample 5418@end smallexample
5229 5419
5230@noindent 5420@noindent
5231The @code{insert-buffer-substring} function copies a string @emph{from} 5421The @code{insert-buffer-substring} function copies a string
5232the buffer specified as its first argument and inserts the string into 5422@emph{from} the buffer specified as its first argument and inserts the
5233the present buffer. In this case, the argument to 5423string into the present buffer. In this case, the argument to
5234@code{insert-buffer-substring} is the value of the variable created and 5424@code{insert-buffer-substring} is the value of the variable created
5235bound by the @code{let}, namely the value of @code{oldbuf}, which was 5425and bound by the @code{let}, namely the value of @code{oldbuf}, which
5236the current buffer when you gave the @code{append-to-buffer} command. 5426was the current buffer when you gave the @code{append-to-buffer}
5427command.
5237 5428
5238After @code{insert-buffer-substring} has done its work, 5429After @code{insert-buffer-substring} has done its work,
5239@code{save-excursion} will restore the action to the original buffer and 5430@code{save-excursion} will restore the action to the original buffer
5240@code{append-to-buffer} will have done its job. 5431and @code{append-to-buffer} will have done its job.
5241 5432
5242@need 800 5433@need 800
5243Written in skeletal form, the workings of the body look like this: 5434Written in skeletal form, the workings of the body look like this:
@@ -5251,16 +5442,15 @@ Written in skeletal form, the workings of the body look like this:
5251 5442
5252 @var{change-back-to-original-buffer-when-finished} 5443 @var{change-back-to-original-buffer-when-finished}
5253@var{let-the-local-meaning-of-}@code{oldbuf}@var{-disappear-when-finished} 5444@var{let-the-local-meaning-of-}@code{oldbuf}@var{-disappear-when-finished}
5254
5255@end group 5445@end group
5256@end smallexample 5446@end smallexample
5257 5447
5258In summary, @code{append-to-buffer} works as follows: it saves the value 5448In summary, @code{append-to-buffer} works as follows: it saves the
5259of the current buffer in the variable called @code{oldbuf}. It gets the 5449value of the current buffer in the variable called @code{oldbuf}. It
5260new buffer, creating one if need be, and switches Emacs to it. Using 5450gets the new buffer (creating one if need be) and switches Emacs'
5261the value of @code{oldbuf}, it inserts the region of text from the old 5451attention to it. Using the value of @code{oldbuf}, it inserts the
5262buffer into the new buffer; and then using @code{save-excursion}, it 5452region of text from the old buffer into the new buffer; and then using
5263brings you back to your original buffer. 5453@code{save-excursion}, it brings you back to your original buffer.
5264 5454
5265In looking at @code{append-to-buffer}, you have explored a fairly 5455In looking at @code{append-to-buffer}, you have explored a fairly
5266complex function. It shows how to use @code{let} and 5456complex function. It shows how to use @code{let} and
@@ -5351,10 +5541,9 @@ use of an asterisk in an @code{interactive} expression, use of
5351to which the name refers. 5541to which the name refers.
5352 5542
5353@menu 5543@menu
5354* copy-to-buffer:: With @code{set-buffer}, @code{get-buffer-create}. 5544* copy-to-buffer::
5355* insert-buffer:: Read-only, and with @code{or}. 5545* insert-buffer::
5356* beginning-of-buffer:: Shows @code{goto-char}, 5546* beginning-of-buffer::
5357 @code{point-min}, and @code{push-mark}.
5358* Second Buffer Related Review:: 5547* Second Buffer Related Review::
5359* optional Exercise:: 5548* optional Exercise::
5360@end menu 5549@end menu
@@ -5366,42 +5555,63 @@ to which the name refers.
5366 5555
5367After understanding how @code{append-to-buffer} works, it is easy to 5556After understanding how @code{append-to-buffer} works, it is easy to
5368understand @code{copy-to-buffer}. This function copies text into a 5557understand @code{copy-to-buffer}. This function copies text into a
5369buffer, but instead of adding to the second buffer, it replaces the 5558buffer, but instead of adding to the second buffer, it replaces all the
5370previous text in the second buffer. The code for the 5559previous text in the second buffer.
5371@code{copy-to-buffer} function is almost the same as the code for
5372@code{append-to-buffer}, except that @code{erase-buffer} and a second
5373@code{save-excursion} are used. (@xref{append-to-buffer, , The
5374Definition of @code{append-to-buffer}}, for the description of
5375@code{append-to-buffer}.)
5376 5560
5377@need 800 5561@need 800
5378The body of @code{copy-to-buffer} looks like this 5562The body of @code{copy-to-buffer} looks like this,
5379 5563
5380@smallexample 5564@smallexample
5381@group 5565@group
5382@dots{} 5566@dots{}
5383(interactive "BCopy to buffer:@: \nr") 5567(interactive "BCopy to buffer: \nr")
5384 (let ((oldbuf (current-buffer))) 5568(let ((oldbuf (current-buffer)))
5569 (with-current-buffer (get-buffer-create buffer)
5570 (barf-if-buffer-read-only)
5571 (erase-buffer)
5385 (save-excursion 5572 (save-excursion
5386 (set-buffer (get-buffer-create buffer)) 5573 (insert-buffer-substring oldbuf start end)))))
5387 (erase-buffer)
5388 (save-excursion
5389 (insert-buffer-substring oldbuf start end)))))
5390@end group 5574@end group
5391@end smallexample 5575@end smallexample
5392 5576
5393This code is similar to the code in @code{append-to-buffer}: it is 5577The @code{copy-to-buffer} function has a simpler @code{interactive}
5394only after changing to the buffer to which the text will be copied 5578expression than @code{append-to-buffer}.
5395that the definition for this function diverges from the definition for 5579
5396@code{append-to-buffer}: the @code{copy-to-buffer} function erases the 5580@need 800
5397buffer's former contents. (This is what is meant by `replacement'; to 5581The definition then says
5398replace text, Emacs erases the previous text and then inserts new 5582
5399text.) After erasing the previous contents of the buffer, 5583@smallexample
5400@code{save-excursion} is used for a second time and the new text is 5584(with-current-buffer (get-buffer-create buffer) @dots{}
5401inserted. 5585@end smallexample
5586
5587First, look at the earliest inner expression; that is evaluated first.
5588That expression starts with @code{get-buffer-create buffer}. The
5589function tells the computer to use the buffer with the name specified
5590as the one to which you are copying, or if such a buffer does not
5591exist, to create it. Then, the @code{with-current-buffer} function
5592evaluates its body with that buffer temporarily current.
5402 5593
5403Why is @code{save-excursion} used twice? Consider again what the 5594(This demonstrates another way to shift the computer's attention but
5404function does. 5595not the user's. The @code{append-to-buffer} function showed how to do
5596the same with @code{save-excursion} and @code{set-buffer}.
5597@code{with-current-buffer} is a newer, and arguably easier,
5598mechanism.)
5599
5600The @code{barf-if-buffer-read-only} function sends you an error
5601message saying the buffer is read-only if you cannot modify it.
5602
5603The next line has the @code{erase-buffer} function as its sole
5604contents. That function erases the buffer.
5605
5606Finally, the last two lines contain the @code{save-excursion}
5607expression with @code{insert-buffer-substring} as its body.
5608The @code{insert-buffer-substring} expression copies the text from
5609the buffer you are in (and you have not seen the computer shift its
5610attention, so you don't know that that buffer is now called
5611@code{oldbuf}).
5612
5613Incidently, this is what is meant by `replacement'. To replace text,
5614Emacs erases the previous text and then inserts new text.
5405 5615
5406@need 1250 5616@need 1250
5407In outline, the body of @code{copy-to-buffer} looks like this: 5617In outline, the body of @code{copy-to-buffer} looks like this:
@@ -5409,27 +5619,14 @@ In outline, the body of @code{copy-to-buffer} looks like this:
5409@smallexample 5619@smallexample
5410@group 5620@group
5411(let (@var{bind-}@code{oldbuf}@var{-to-value-of-}@code{current-buffer}) 5621(let (@var{bind-}@code{oldbuf}@var{-to-value-of-}@code{current-buffer})
5412 (save-excursion ; @r{First use of @code{save-excursion}.} 5622 (@var{with-the-buffer-you-are-copying-to}
5413 @var{change-buffer} 5623 (@var{but-do-not-erase-or-copy-to-a-read-only-buffer})
5414 (erase-buffer) 5624 (erase-buffer)
5415 (save-excursion ; @r{Second use of @code{save-excursion}.} 5625 (save-excursion
5416 @var{insert-substring-from-}@code{oldbuf}@var{-into-buffer}))) 5626 @var{insert-substring-from-}@code{oldbuf}@var{-into-buffer})))
5417@end group 5627@end group
5418@end smallexample 5628@end smallexample
5419 5629
5420The first use of @code{save-excursion} returns Emacs to the buffer from
5421which the text is being copied. That is clear, and is just like its use
5422in @code{append-to-buffer}. Why the second use? The reason is that
5423@code{insert-buffer-substring} always leaves point at the @emph{end} of
5424the region being inserted. The second @code{save-excursion} causes
5425Emacs to leave point at the beginning of the text being inserted. In
5426most circumstances, users prefer to find point at the beginning of
5427inserted text. (Of course, the @code{copy-to-buffer} function returns
5428the user to the original buffer when done---but if the user @emph{then}
5429switches to the copied-to buffer, point will go to the beginning of the
5430text. Thus, this use of a second @code{save-excursion} is a little
5431nicety.)
5432
5433@node insert-buffer, beginning-of-buffer, copy-to-buffer, More Complex 5630@node insert-buffer, beginning-of-buffer, copy-to-buffer, More Complex
5434@comment node-name, next, previous, up 5631@comment node-name, next, previous, up
5435@section The Definition of @code{insert-buffer} 5632@section The Definition of @code{insert-buffer}
@@ -5443,17 +5640,21 @@ copy a region of text @emph{from} the current buffer to another buffer.
5443Here is a discussion based on the original code. The code was 5640Here is a discussion based on the original code. The code was
5444simplified in 2003 and is harder to understand. 5641simplified in 2003 and is harder to understand.
5445 5642
5643@xref{New insert-buffer, , New Body for @code{insert-buffer}}, to see
5644a discussion of the new body.)
5645
5446In addition, this code illustrates the use of @code{interactive} with a 5646In addition, this code illustrates the use of @code{interactive} with a
5447buffer that might be @dfn{read-only} and the important distinction 5647buffer that might be @dfn{read-only} and the important distinction
5448between the name of an object and the object actually referred to. 5648between the name of an object and the object actually referred to.
5449 5649
5450@menu 5650@menu
5451* insert-buffer code:: 5651* insert-buffer code::
5452* insert-buffer interactive:: When you can read, but not write. 5652* insert-buffer interactive::
5453* insert-buffer body:: The body has an @code{or} and a @code{let}. 5653* insert-buffer body::
5454* if & or:: Using an @code{if} instead of an @code{or}. 5654* if & or::
5455* Insert or:: How the @code{or} expression works. 5655* Insert or::
5456* Insert let:: Two @code{save-excursion} expressions. 5656* Insert let::
5657* New insert-buffer ::
5457@end menu 5658@end menu
5458 5659
5459@node insert-buffer code, insert-buffer interactive, insert-buffer, insert-buffer 5660@node insert-buffer code, insert-buffer interactive, insert-buffer, insert-buffer
@@ -5462,7 +5663,7 @@ between the name of an object and the object actually referred to.
5462@end ifnottex 5663@end ifnottex
5463 5664
5464@need 800 5665@need 800
5465Here is the code: 5666Here is the earlier code:
5466 5667
5467@smallexample 5668@smallexample
5468@group 5669@group
@@ -5511,8 +5712,8 @@ declaration has two parts, an asterisk, @samp{*}, and @samp{bInsert
5511buffer:@: }. 5712buffer:@: }.
5512 5713
5513@menu 5714@menu
5514* Read-only buffer:: When a buffer cannot be modified. 5715* Read-only buffer::
5515* b for interactive:: An existing buffer or else its name. 5716* b for interactive::
5516@end menu 5717@end menu
5517 5718
5518@node Read-only buffer, b for interactive, insert-buffer interactive, insert-buffer interactive 5719@node Read-only buffer, b for interactive, insert-buffer interactive, insert-buffer interactive
@@ -5546,6 +5747,11 @@ of the buffer, offering you a default buffer, with name completion
5546enabled. If the buffer does not exist, you receive a message that 5747enabled. If the buffer does not exist, you receive a message that
5547says ``No match''; your terminal may beep at you as well. 5748says ``No match''; your terminal may beep at you as well.
5548 5749
5750The new and simplified code generates a list for @code{interactive}.
5751It uses the @code{barf-if-buffer-read-only} and @code{read-buffer}
5752functions with which we are already familiar and the @code{progn}
5753special form with which we are not. (It will be described later.)
5754
5549@node insert-buffer body, if & or, insert-buffer interactive, insert-buffer 5755@node insert-buffer body, if & or, insert-buffer interactive, insert-buffer
5550@comment node-name, next, previous, up 5756@comment node-name, next, previous, up
5551@subsection The Body of the @code{insert-buffer} Function 5757@subsection The Body of the @code{insert-buffer} Function
@@ -5722,7 +5928,7 @@ written like this:
5722(or (holding-on-to-guest) (find-and-take-arm-of-guest)) 5928(or (holding-on-to-guest) (find-and-take-arm-of-guest))
5723@end smallexample 5929@end smallexample
5724 5930
5725@node Insert let, , Insert or, insert-buffer 5931@node Insert let, New insert-buffer , Insert or, insert-buffer
5726@comment node-name, next, previous, up 5932@comment node-name, next, previous, up
5727@subsection The @code{let} Expression in @code{insert-buffer} 5933@subsection The @code{let} Expression in @code{insert-buffer}
5728 5934
@@ -5821,6 +6027,45 @@ function uses @code{let}, @code{save-excursion}, and
5821use @code{or}. All these functions are building blocks that we will 6027use @code{or}. All these functions are building blocks that we will
5822find and use again and again. 6028find and use again and again.
5823 6029
6030@node New insert-buffer , , Insert let, insert-buffer
6031@comment node-name, next, previous, up
6032@subsection New Body for @code{insert-buffer}
6033@findex insert-buffer, new version body
6034@findex new version body for insert-buffer
6035
6036The body in the GNU Emacs 22 version is more confusing than the original.
6037
6038@need 1250
6039It consists of two expressions,
6040
6041@smallexample
6042@group
6043 (push-mark
6044 (save-excursion
6045 (insert-buffer-substring (get-buffer buffer))
6046 (point)))
6047
6048 nil
6049@end group
6050@end smallexample
6051
6052@noindent
6053except, and this is what confuses novices, very important work is done
6054inside the @code{push-mark} expression.
6055
6056The @code{get-buffer} function returns a buffer with the name
6057provided. You will note that the function is @emph{not} called
6058@code{get-buffer-create}; it does not create a buffer if one does not
6059already exist. The buffer returned by @code{get-buffer}, an existing
6060buffer, is passed to @code{insert-buffer-substring}, which inserts the
6061whole of the buffer (since you did not specify anything else).
6062
6063The location into which the buffer is inserted is recorded by
6064@code{push-mark}. Then the function returns @code{nil}, the value of
6065its last command. Put another way, the @code{insert-buffer} function
6066exists only to produce a side effect, inserting another buffer, not to
6067return any value.
6068
5824@node beginning-of-buffer, Second Buffer Related Review, insert-buffer, More Complex 6069@node beginning-of-buffer, Second Buffer Related Review, insert-buffer, More Complex
5825@comment node-name, next, previous, up 6070@comment node-name, next, previous, up
5826@section Complete Definition of @code{beginning-of-buffer} 6071@section Complete Definition of @code{beginning-of-buffer}
@@ -5833,23 +6078,24 @@ This section describes the complex part of the definition.
5833 6078
5834As previously described, when invoked without an argument, 6079As previously described, when invoked without an argument,
5835@code{beginning-of-buffer} moves the cursor to the beginning of the 6080@code{beginning-of-buffer} moves the cursor to the beginning of the
5836buffer, leaving the mark at the previous position. However, when the 6081buffer (in truth, the accessible portion of the buffer), leaving the
5837command is invoked with a number between one and ten, the function 6082mark at the previous position. However, when the command is invoked
5838considers that number to be a fraction of the length of the buffer, 6083with a number between one and ten, the function considers that number
5839measured in tenths, and Emacs moves the cursor that fraction of the way 6084to be a fraction of the length of the buffer, measured in tenths, and
5840from the beginning of the buffer. Thus, you can either call this 6085Emacs moves the cursor that fraction of the way from the beginning of
5841function with the key command @kbd{M-<}, which will move the cursor to 6086the buffer. Thus, you can either call this function with the key
5842the beginning of the buffer, or with a key command such as @kbd{C-u 7 6087command @kbd{M-<}, which will move the cursor to the beginning of the
5843M-<} which will move the cursor to a point 70% of the way through the 6088buffer, or with a key command such as @kbd{C-u 7 M-<} which will move
5844buffer. If a number bigger than ten is used for the argument, it moves 6089the cursor to a point 70% of the way through the buffer. If a number
5845to the end of the buffer. 6090bigger than ten is used for the argument, it moves to the end of the
6091buffer.
5846 6092
5847The @code{beginning-of-buffer} function can be called with or without an 6093The @code{beginning-of-buffer} function can be called with or without an
5848argument. The use of the argument is optional. 6094argument. The use of the argument is optional.
5849 6095
5850@menu 6096@menu
5851* Optional Arguments:: 6097* Optional Arguments::
5852* beginning-of-buffer opt arg:: Example with optional argument. 6098* beginning-of-buffer opt arg::
5853* beginning-of-buffer complete:: 6099* beginning-of-buffer complete::
5854@end menu 6100@end menu
5855 6101
@@ -5864,12 +6110,12 @@ If that does not happen, you get an error and a message that says
5864@cindex Optional arguments 6110@cindex Optional arguments
5865@cindex Keyword 6111@cindex Keyword
5866@findex optional 6112@findex optional
5867However, optional arguments are a feature of Lisp: a @dfn{keyword} may 6113However, optional arguments are a feature of Lisp: a particular
5868be used to tell the Lisp interpreter that an argument is optional. 6114@dfn{keyword} is used to tell the Lisp interpreter that an argument is
5869The keyword is @code{&optional}. (The @samp{&} in front of 6115optional. The keyword is @code{&optional}. (The @samp{&} in front of
5870@samp{optional} is part of the keyword.) In a function definition, if 6116@samp{optional} is part of the keyword.) In a function definition, if
5871an argument follows the keyword @code{&optional}, a value does not 6117an argument follows the keyword @code{&optional}, no value need be
5872need to be passed to that argument when the function is called. 6118passed to that argument when the function is called.
5873 6119
5874@need 1200 6120@need 1200
5875The first line of the function definition of @code{beginning-of-buffer} 6121The first line of the function definition of @code{beginning-of-buffer}
@@ -5887,12 +6133,16 @@ In outline, the whole function looks like this:
5887(defun beginning-of-buffer (&optional arg) 6133(defun beginning-of-buffer (&optional arg)
5888 "@var{documentation}@dots{}" 6134 "@var{documentation}@dots{}"
5889 (interactive "P") 6135 (interactive "P")
5890 (push-mark) 6136 (or (@var{is-the-argument-a-cons-cell} arg)
6137 (and @var{are-both-transient-mark-mode-and-mark-active-true})
6138 (push-mark))
6139 (let (@var{determine-size-and-set-it})
5891 (goto-char 6140 (goto-char
5892 (@var{if-there-is-an-argument} 6141 (@var{if-there-is-an-argument}
5893 @var{figure-out-where-to-go} 6142 @var{figure-out-where-to-go}
5894 @var{else-go-to} 6143 @var{else-go-to}
5895 (point-min)))) 6144 (point-min))))
6145 @var{do-nicety}
5896@end group 6146@end group
5897@end smallexample 6147@end smallexample
5898 6148
@@ -5900,26 +6150,37 @@ The function is similar to the @code{simplified-beginning-of-buffer}
5900function except that the @code{interactive} expression has @code{"P"} 6150function except that the @code{interactive} expression has @code{"P"}
5901as an argument and the @code{goto-char} function is followed by an 6151as an argument and the @code{goto-char} function is followed by an
5902if-then-else expression that figures out where to put the cursor if 6152if-then-else expression that figures out where to put the cursor if
5903there is an argument. 6153there is an argument that is not a cons cell.
5904 6154
5905The @code{"P"} in the @code{interactive} expression tells Emacs to pass 6155(Since I do not explain a cons cell for many more chapters, please
5906a prefix argument, if there is one, to the function. A prefix argument 6156consider ignoring the function @code{consp}. @xref{List
5907is made by typing the @key{META} key followed by a number, or by typing 6157Implementation, , How Lists are Implemented}, and @ref{Cons Cell Type,
5908@kbd{C-u} and then a number (if you don't type a number, @kbd{C-u} 6158, Cons Cell and List Types, elisp, The GNU Emacs Lisp Reference
5909defaults to 4). 6159Manual}.)
5910 6160
5911The true-or-false-test of the @code{if} expression is simple: it is 6161The @code{"P"} in the @code{interactive} expression tells Emacs to
5912simply the argument @code{arg}. If @code{arg} has a value that is not 6162pass a prefix argument, if there is one, to the function in raw form.
5913@code{nil}, which will be the case if @code{beginning-of-buffer} is 6163A prefix argument is made by typing the @key{META} key followed by a
5914called with an argument, then this true-or-false-test will return true 6164number, or by typing @kbd{C-u} and then a number. (If you don't type
5915and the then-part of the @code{if} expression will be evaluated. On the 6165a number, @kbd{C-u} defaults to a cons cell with a 4. A lowercase
6166@code{"p"} in the @code{interactive} expression causes the function to
6167convert a prefix arg to a number.)
6168
6169The true-or-false-test of the @code{if} expression looks complex, but
6170it is not: it checks whether @code{arg} has a value that is not
6171@code{nil} and whether it is a cons cell. (That is what @code{consp}
6172does; it checks whether its argument is a cons cell.) If @code{arg}
6173has a value that is not @code{nil} (and is not a cons cell), which
6174will be the case if @code{beginning-of-buffer} is called with a
6175numeric argument, then this true-or-false-test will return true and
6176the then-part of the @code{if} expression will be evaluated. On the
5916other hand, if @code{beginning-of-buffer} is not called with an 6177other hand, if @code{beginning-of-buffer} is not called with an
5917argument, the value of @code{arg} will be @code{nil} and the else-part 6178argument, the value of @code{arg} will be @code{nil} and the else-part
5918of the @code{if} expression will be evaluated. The else-part is simply 6179of the @code{if} expression will be evaluated. The else-part is
5919@code{point-min}, and when this is the outcome, the whole 6180simply @code{point-min}, and when this is the outcome, the whole
5920@code{goto-char} expression is @code{(goto-char (point-min))}, which is 6181@code{goto-char} expression is @code{(goto-char (point-min))}, which
5921how we saw the @code{beginning-of-buffer} function in its simplified 6182is how we saw the @code{beginning-of-buffer} function in its
5922form. 6183simplified form.
5923 6184
5924@node beginning-of-buffer opt arg, beginning-of-buffer complete, Optional Arguments, beginning-of-buffer 6185@node beginning-of-buffer opt arg, beginning-of-buffer complete, Optional Arguments, beginning-of-buffer
5925@subsection @code{beginning-of-buffer} with an Argument 6186@subsection @code{beginning-of-buffer} with an Argument
@@ -5934,11 +6195,12 @@ like this:
5934@group 6195@group
5935(if (> (buffer-size) 10000) 6196(if (> (buffer-size) 10000)
5936 ;; @r{Avoid overflow for large buffer sizes!} 6197 ;; @r{Avoid overflow for large buffer sizes!}
5937 (* (prefix-numeric-value arg) (/ (buffer-size) 10)) 6198 (* (prefix-numeric-value arg)
6199 (/ size 10))
5938 (/ 6200 (/
5939 (+ 10 6201 (+ 10
5940 (* 6202 (*
5941 (buffer-size) (prefix-numeric-value arg))) 10)) 6203 size (prefix-numeric-value arg))) 10)))
5942@end group 6204@end group
5943@end smallexample 6205@end smallexample
5944 6206
@@ -5984,13 +6246,21 @@ There are two cases: if the buffer is large and if it is not.
5984 6246
5985In @code{beginning-of-buffer}, the inner @code{if} expression tests 6247In @code{beginning-of-buffer}, the inner @code{if} expression tests
5986whether the size of the buffer is greater than 10,000 characters. To do 6248whether the size of the buffer is greater than 10,000 characters. To do
5987this, it uses the @code{>} function and the @code{buffer-size} function. 6249this, it uses the @code{>} function and the computation of @code{size}
6250that comes from the let expression.
6251
6252In the old days, the function @code{buffer-size} was used. Not only
6253was that function called several times, it gave the size of the whole
6254buffer, not the accessible part. The computation makes much more
6255sense when it handles just the accessible part. (@xref{Narrowing &
6256Widening, , Narrowing and Widening}, for more information on focusing
6257attention to an `accessible' part.)
5988 6258
5989@need 800 6259@need 800
5990The line looks like this: 6260The line looks like this:
5991 6261
5992@smallexample 6262@smallexample
5993(if (> (buffer-size) 10000) 6263(if (> size 10000)
5994@end smallexample 6264@end smallexample
5995 6265
5996@need 1200 6266@need 1200
@@ -6002,7 +6272,7 @@ evaluated. It reads like this (after formatting for easy reading):
6002@group 6272@group
6003(* 6273(*
6004 (prefix-numeric-value arg) 6274 (prefix-numeric-value arg)
6005 (/ (buffer-size) 10)) 6275 (/ size 10))
6006@end group 6276@end group
6007@end smallexample 6277@end smallexample
6008 6278
@@ -6019,11 +6289,12 @@ the arithmetic, a conversion is necessary, and
6019 6289
6020@findex / @r{(division)} 6290@findex / @r{(division)}
6021@cindex Division 6291@cindex Division
6022The second argument is @code{(/ (buffer-size) 10)}. This expression 6292The second argument is @code{(/ size 10)}. This expression divides
6023divides the numeric value of the buffer by ten. This produces a number 6293the numeric value by ten --- the numeric value of the size of the
6024that tells how many characters make up one tenth of the buffer size. 6294accessible portion of the buffer. This produces a number that tells
6025(In Lisp, @code{/} is used for division, just as @code{*} is 6295how many characters make up one tenth of the buffer size. (In Lisp,
6026used for multiplication.) 6296@code{/} is used for division, just as @code{*} is used for
6297multiplication.)
6027 6298
6028@need 1200 6299@need 1200
6029In the multiplication expression as a whole, this amount is multiplied 6300In the multiplication expression as a whole, this amount is multiplied
@@ -6032,23 +6303,22 @@ by the value of the prefix argument---the multiplication looks like this:
6032@smallexample 6303@smallexample
6033@group 6304@group
6034(* @var{numeric-value-of-prefix-arg} 6305(* @var{numeric-value-of-prefix-arg}
6035 @var{number-of-characters-in-one-tenth-of-the-buffer}) 6306 @var{number-of-characters-in-one-tenth-of-the-accessible-buffer})
6036@end group 6307@end group
6037@end smallexample 6308@end smallexample
6038 6309
6039@noindent 6310@noindent
6040If, for example, the prefix argument is @samp{7}, the one-tenth value 6311If, for example, the prefix argument is @samp{7}, the one-tenth value
6041will be multiplied by 7 to give a position 70% of the way through the 6312will be multiplied by 7 to give a position 70% of the way through.
6042buffer.
6043 6313
6044@need 1200 6314@need 1200
6045The result of all this is that if the buffer is large, the 6315The result of all this is that if the accessible portion of the buffer
6046@code{goto-char} expression reads like this: 6316is large, the @code{goto-char} expression reads like this:
6047 6317
6048@smallexample 6318@smallexample
6049@group 6319@group
6050(goto-char (* (prefix-numeric-value arg) 6320(goto-char (* (prefix-numeric-value arg)
6051 (/ (buffer-size) 10))) 6321 (/ size 10)))
6052@end group 6322@end group
6053@end smallexample 6323@end smallexample
6054 6324
@@ -6069,7 +6339,7 @@ The code looks like this:
6069 6339
6070@c Keep this on one line. 6340@c Keep this on one line.
6071@smallexample 6341@smallexample
6072(/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10)) 6342(/ (+ 10 (* size (prefix-numeric-value arg))) 10))
6073@end smallexample 6343@end smallexample
6074 6344
6075@need 1200 6345@need 1200
@@ -6084,7 +6354,7 @@ enclosing expression:
6084 (/ 6354 (/
6085 (+ 10 6355 (+ 10
6086 (* 6356 (*
6087 (buffer-size) 6357 size
6088 (prefix-numeric-value arg))) 6358 (prefix-numeric-value arg)))
6089 10)) 6359 10))
6090@end group 6360@end group
@@ -6093,12 +6363,12 @@ enclosing expression:
6093@need 1200 6363@need 1200
6094@noindent 6364@noindent
6095Looking at parentheses, we see that the innermost operation is 6365Looking at parentheses, we see that the innermost operation is
6096@code{(prefix-numeric-value arg)}, which converts the raw argument to a 6366@code{(prefix-numeric-value arg)}, which converts the raw argument to
6097number. This number is multiplied by the buffer size in the following 6367a number. In the following expression, this number is multiplied by
6098expression: 6368the size of the accessible portion of the buffer:
6099 6369
6100@smallexample 6370@smallexample
6101(* (buffer-size) (prefix-numeric-value arg)) 6371(* size (prefix-numeric-value arg))
6102@end smallexample 6372@end smallexample
6103 6373
6104@noindent 6374@noindent
@@ -6120,6 +6390,47 @@ the cursor is moved to that point.
6120Here is the complete text of the @code{beginning-of-buffer} function: 6390Here is the complete text of the @code{beginning-of-buffer} function:
6121@sp 1 6391@sp 1
6122 6392
6393@c In GNU Emacs 22
6394@smallexample
6395@group
6396(defun beginning-of-buffer (&optional arg)
6397 "Move point to the beginning of the buffer;
6398leave mark at previous position.
6399With \\[universal-argument] prefix,
6400do not set mark at previous position.
6401With numeric arg N,
6402put point N/10 of the way from the beginning.
6403
6404If the buffer is narrowed,
6405this command uses the beginning and size
6406of the accessible part of the buffer.
6407@end group
6408
6409@group
6410Don't use this command in Lisp programs!
6411\(goto-char (point-min)) is faster
6412and avoids clobbering the mark."
6413 (interactive "P")
6414 (or (consp arg)
6415 (and transient-mark-mode mark-active)
6416 (push-mark))
6417@end group
6418@group
6419 (let ((size (- (point-max) (point-min))))
6420 (goto-char (if (and arg (not (consp arg)))
6421 (+ (point-min)
6422 (if (> size 10000)
6423 ;; Avoid overflow for large buffer sizes!
6424 (* (prefix-numeric-value arg)
6425 (/ size 10))
6426 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
6427 (point-min))))
6428 (if arg (forward-line 1)))
6429@end group
6430@end smallexample
6431
6432@ignore
6433From before GNU Emacs 22
6123@smallexample 6434@smallexample
6124@group 6435@group
6125(defun beginning-of-buffer (&optional arg) 6436(defun beginning-of-buffer (&optional arg)
@@ -6151,6 +6462,7 @@ and does not set the mark."
6151 (if arg (forward-line 1))) 6462 (if arg (forward-line 1)))
6152@end group 6463@end group
6153@end smallexample 6464@end smallexample
6465@end ignore
6154 6466
6155@noindent 6467@noindent
6156Except for two small points, the previous discussion shows how this 6468Except for two small points, the previous discussion shows how this
@@ -6162,18 +6474,21 @@ the function.
6162In the documentation string, there is reference to an expression: 6474In the documentation string, there is reference to an expression:
6163 6475
6164@smallexample 6476@smallexample
6165\(goto-char (point-min)) 6477\\[universal-argument]
6166@end smallexample 6478@end smallexample
6167 6479
6168@noindent 6480@noindent
6169A @samp{\} is used before the first parenthesis of this expression. 6481A @samp{\\} is used before the first square bracket of this
6170This @samp{\} tells the Lisp interpreter that the expression should be 6482expression. This @samp{\\} tells the Lisp interpreter to substitute
6171printed as shown in the documentation rather than evaluated as a 6483whatever key is currently bound to the @samp{[@dots{}]}. In the case
6172symbolic expression, which is what it looks like. 6484of @code{universal-argument}, that is usually @kbd{C-u}, but it might
6485be different. (@xref{Documentation Tips, , Tips for Documentation
6486Strings, elisp, The GNU Emacs Lisp Reference Manual}, for more
6487information.)
6173 6488
6174@need 1200 6489@need 1200
6175Finally, the last line of the @code{beginning-of-buffer} command says to 6490Finally, the last line of the @code{beginning-of-buffer} command says
6176move point to the beginning of the next line if the command is 6491to move point to the beginning of the next line if the command is
6177invoked with an argument: 6492invoked with an argument:
6178 6493
6179@smallexample 6494@smallexample
@@ -6185,8 +6500,15 @@ This puts the cursor at the beginning of the first line after the
6185appropriate tenths position in the buffer. This is a flourish that 6500appropriate tenths position in the buffer. This is a flourish that
6186means that the cursor is always located @emph{at least} the requested 6501means that the cursor is always located @emph{at least} the requested
6187tenths of the way through the buffer, which is a nicety that is, 6502tenths of the way through the buffer, which is a nicety that is,
6188perhaps, not necessary, but which, if it did not occur, would be sure to 6503perhaps, not necessary, but which, if it did not occur, would be sure
6189draw complaints. 6504to draw complaints.
6505
6506On the other hand, it also means that if you specify the command with
6507a @kbd{C-u}, but without a number, that is to say, if the `raw prefix
6508argument' is simply a cons cell, then the command puts you at the
6509beginning of the second line @dots{} I don't know whether this is
6510intended or whether no one has dealt with the code to avoid this
6511happening.
6190 6512
6191@node Second Buffer Related Review, optional Exercise, beginning-of-buffer, More Complex 6513@node Second Buffer Related Review, optional Exercise, beginning-of-buffer, More Complex
6192@comment node-name, next, previous, up 6514@comment node-name, next, previous, up
@@ -6200,7 +6522,7 @@ Evaluate each argument in sequence, and return the value of the first
6200argument that is not @code{nil}; if none return a value that is not 6522argument that is not @code{nil}; if none return a value that is not
6201@code{nil}, return @code{nil}. In brief, return the first true value 6523@code{nil}, return @code{nil}. In brief, return the first true value
6202of the arguments; return a true value if one @emph{or} any of the 6524of the arguments; return a true value if one @emph{or} any of the
6203other are true. 6525others are true.
6204 6526
6205@item and 6527@item and
6206Evaluate each argument in sequence, and if any are @code{nil}, return 6528Evaluate each argument in sequence, and if any are @code{nil}, return
@@ -6254,9 +6576,9 @@ other parts. Narrowing is normally disabled since it can confuse
6254novices. 6576novices.
6255 6577
6256@menu 6578@menu
6257* Narrowing advantages:: The advantages of narrowing 6579* Narrowing advantages::
6258* save-restriction:: The @code{save-restriction} special form. 6580* save-restriction::
6259* what-line:: The number of the line that point is on. 6581* what-line::
6260* narrow Exercise:: 6582* narrow Exercise::
6261@end menu 6583@end menu
6262 6584
@@ -6359,6 +6681,60 @@ For example,
6359@end group 6681@end group
6360@end smallexample 6682@end smallexample
6361 6683
6684@ignore
6685Emacs 22
6686/usr/local/src/emacs/lisp/simple.el
6687
6688(defun what-line ()
6689 "Print the current buffer line number and narrowed line number of point."
6690 (interactive)
6691 (let ((start (point-min))
6692 (n (line-number-at-pos)))
6693 (if (= start 1)
6694 (message "Line %d" n)
6695 (save-excursion
6696 (save-restriction
6697 (widen)
6698 (message "line %d (narrowed line %d)"
6699 (+ n (line-number-at-pos start) -1) n))))))
6700
6701(defun line-number-at-pos (&optional pos)
6702 "Return (narrowed) buffer line number at position POS.
6703If POS is nil, use current buffer location.
6704Counting starts at (point-min), so the value refers
6705to the contents of the accessible portion of the buffer."
6706 (let ((opoint (or pos (point))) start)
6707 (save-excursion
6708 (goto-char (point-min))
6709 (setq start (point))
6710 (goto-char opoint)
6711 (forward-line 0)
6712 (1+ (count-lines start (point))))))
6713
6714(defun count-lines (start end)
6715 "Return number of lines between START and END.
6716This is usually the number of newlines between them,
6717but can be one more if START is not equal to END
6718and the greater of them is not at the start of a line."
6719 (save-excursion
6720 (save-restriction
6721 (narrow-to-region start end)
6722 (goto-char (point-min))
6723 (if (eq selective-display t)
6724 (save-match-data
6725 (let ((done 0))
6726 (while (re-search-forward "[\n\C-m]" nil t 40)
6727 (setq done (+ 40 done)))
6728 (while (re-search-forward "[\n\C-m]" nil t 1)
6729 (setq done (+ 1 done)))
6730 (goto-char (point-max))
6731 (if (and (/= start end)
6732 (not (bolp)))
6733 (1+ done)
6734 done)))
6735 (- (buffer-size) (forward-line (buffer-size)))))))
6736@end ignore
6737
6362@node what-line, narrow Exercise, save-restriction, Narrowing & Widening 6738@node what-line, narrow Exercise, save-restriction, Narrowing & Widening
6363@comment node-name, next, previous, up 6739@comment node-name, next, previous, up
6364@section @code{what-line} 6740@section @code{what-line}
@@ -6389,13 +6765,17 @@ been expanded to tell you your line number in a narrowed buffer as
6389well as your line number in a widened buffer. The recent version is 6765well as your line number in a widened buffer. The recent version is
6390more complex than the version shown here. If you feel adventurous, 6766more complex than the version shown here. If you feel adventurous,
6391you might want to look at it after figuring out how this version 6767you might want to look at it after figuring out how this version
6392works. The newer version uses a conditional to determine whether the 6768works. You will probably need to use @kbd{C-h f}
6393buffer has been narrowed, and rather than use @code{beginning-of-line} 6769(@code{describe-function}). The newer version uses a conditional to
6394to move point to the beginning of the current line, if need be, the 6770determine whether the buffer has been narrowed.
6395function uses @code{(forward-line 0)}.)
6396 6771
6397The function as shown here has a documentation line and is 6772(Also, it uses @code{line-number-at-pos}, which among other simple
6398interactive, as you would expect. The next two lines use the 6773expressions, such as @code{(goto-char (point-min))}, moves point to
6774the beginning of the current line with @code{(forward-line 0)} rather
6775than @code{beginning-of-line}.)
6776
6777The @code{what-line} function as shown here has a documentation line
6778and is interactive, as you would expect. The next two lines use the
6399functions @code{save-restriction} and @code{widen}. 6779functions @code{save-restriction} and @code{widen}.
6400 6780
6401The @code{save-restriction} special form notes whatever narrowing is in 6781The @code{save-restriction} special form notes whatever narrowing is in
@@ -6434,15 +6814,14 @@ echo area.
6434@end group 6814@end group
6435@end smallexample 6815@end smallexample
6436 6816
6437The @code{message} function prints a one-line message at the bottom of the 6817The @code{message} function prints a one-line message at the bottom of
6438Emacs screen. The first argument is inside of quotation marks and is 6818the Emacs screen. The first argument is inside of quotation marks and
6439printed as a string of characters. However, it may contain @samp{%d}, 6819is printed as a string of characters. However, it may contain a
6440@samp{%s}, or @samp{%c} to print arguments that follow the string. 6820@samp{%d} expression to print a following argument. @samp{%d} prints
6441@samp{%d} prints the argument as a decimal, so the message will say 6821the argument as a decimal, so the message will say something such as
6442something such as @samp{Line 243}. 6822@samp{Line 243}.
6443 6823
6444@need 1200 6824@need 1200
6445
6446The number that is printed in place of the @samp{%d} is computed by the 6825The number that is printed in place of the @samp{%d} is computed by the
6447last line of the function: 6826last line of the function:
6448 6827
@@ -6450,6 +6829,33 @@ last line of the function:
6450(1+ (count-lines 1 (point))) 6829(1+ (count-lines 1 (point)))
6451@end smallexample 6830@end smallexample
6452 6831
6832@ignore
6833GNU Emacs 22
6834
6835(defun count-lines (start end)
6836 "Return number of lines between START and END.
6837This is usually the number of newlines between them,
6838but can be one more if START is not equal to END
6839and the greater of them is not at the start of a line."
6840 (save-excursion
6841 (save-restriction
6842 (narrow-to-region start end)
6843 (goto-char (point-min))
6844 (if (eq selective-display t)
6845 (save-match-data
6846 (let ((done 0))
6847 (while (re-search-forward "[\n\C-m]" nil t 40)
6848 (setq done (+ 40 done)))
6849 (while (re-search-forward "[\n\C-m]" nil t 1)
6850 (setq done (+ 1 done)))
6851 (goto-char (point-max))
6852 (if (and (/= start end)
6853 (not (bolp)))
6854 (1+ done)
6855 done)))
6856 (- (buffer-size) (forward-line (buffer-size)))))))
6857@end ignore
6858
6453@noindent 6859@noindent
6454What this does is count the lines from the first position of the 6860What this does is count the lines from the first position of the
6455buffer, indicated by the @code{1}, up to @code{(point)}, and then add 6861buffer, indicated by the @code{1}, up to @code{(point)}, and then add
@@ -6477,12 +6883,14 @@ functions, including @code{save-restriction}, @code{widen},
6477@cindex Properties, mention of @code{buffer-substring-no-properties} 6883@cindex Properties, mention of @code{buffer-substring-no-properties}
6478(@code{buffer-substring} is a previously unmentioned function you will 6884(@code{buffer-substring} is a previously unmentioned function you will
6479have to investigate yourself; or perhaps you will have to use 6885have to investigate yourself; or perhaps you will have to use
6480@code{buffer-substring-no-properties} @dots{}, yet another function 6886@code{buffer-substring-no-properties} or
6481and one that introduces text properties, a feature otherwise not 6887@code{filter-buffer-substring} @dots{}, yet other functions. Text
6482discussed here. @xref{Text Properties, , Text Properties, elisp, The 6888properties are a feature otherwise not discussed here. @xref{Text
6483GNU Emacs Lisp Reference Manual}. Additionally, do you really need 6889Properties, , Text Properties, elisp, The GNU Emacs Lisp Reference
6484@code{goto-char} or @code{point-min}? Or can you write the function 6890Manual}.
6485without them?) 6891
6892Additionally, do you really need @code{goto-char} or @code{point-min}?
6893Or can you write the function without them?)
6486 6894
6487@node car cdr & cons, Cutting & Storing Text, Narrowing & Widening, Top 6895@node car cdr & cons, Cutting & Storing Text, Narrowing & Widening, Top
6488@comment node-name, next, previous, up 6896@comment node-name, next, previous, up
@@ -6499,13 +6907,13 @@ will see @code{cons} as well as two variants on @code{cdr},
6499namely, @code{setcdr} and @code{nthcdr}. (@xref{copy-region-as-kill}.) 6907namely, @code{setcdr} and @code{nthcdr}. (@xref{copy-region-as-kill}.)
6500 6908
6501@menu 6909@menu
6502* Strange Names:: An historical aside: why the strange names? 6910* Strange Names::
6503* car & cdr:: Functions for extracting part of a list. 6911* car & cdr::
6504* cons:: Constructing a list. 6912* cons::
6505* nthcdr:: Calling @code{cdr} repeatedly. 6913* nthcdr::
6506* nth:: 6914* nth::
6507* setcar:: Changing the first element of a list. 6915* setcar::
6508* setcdr:: Changing the rest of a list. 6916* setcdr::
6509* cons Exercise:: 6917* cons Exercise::
6510@end menu 6918@end menu
6511 6919
@@ -6687,7 +7095,7 @@ Like @code{car} and @code{cdr}, @code{cons} is non-destructive.
6687 7095
6688@menu 7096@menu
6689* Build a list:: 7097* Build a list::
6690* length:: How to find the length of a list. 7098* length::
6691@end menu 7099@end menu
6692 7100
6693@node Build a list, length, cons, cons 7101@node Build a list, length, cons, cons
@@ -6798,7 +7206,7 @@ without giving it an argument, not even an empty list:
6798What you see, if you evaluate this, is the error message 7206What you see, if you evaluate this, is the error message
6799 7207
6800@smallexample 7208@smallexample
6801Wrong number of arguments: #<subr length>, 0 7209Lisp error: (wrong-number-of-arguments length 0)
6802@end smallexample 7210@end smallexample
6803 7211
6804@noindent 7212@noindent
@@ -6808,13 +7216,20 @@ this case, one argument is expected, the argument being a list whose
6808length the function is measuring. (Note that @emph{one} list is 7216length the function is measuring. (Note that @emph{one} list is
6809@emph{one} argument, even if the list has many elements inside it.) 7217@emph{one} argument, even if the list has many elements inside it.)
6810 7218
6811The part of the error message that says @samp{#<subr length>} is the 7219The part of the error message that says @samp{length} is the name of
6812name of the function. This is written with a special notation, 7220the function.
6813@samp{#<subr}, that indicates that the function @code{length} is one 7221
6814of the primitive functions written in C rather than in Emacs Lisp. 7222@ignore
6815(@samp{subr} is an abbreviation for `subroutine'.) @xref{What Is a 7223@code{length} is still a subroutine, but you need C-h f to discover that.
6816Function, , What Is a Function?, elisp , The GNU Emacs Lisp Reference 7224
6817Manual}, for more about subroutines. 7225In an earlier version:
7226 This is written with a special notation, @samp{#<subr},
7227 that indicates that the function @code{length} is one of the primitive
7228 functions written in C rather than in Emacs Lisp. (@samp{subr} is an
7229 abbreviation for `subroutine'.) @xref{What Is a Function, , What Is a
7230 Function?, elisp , The GNU Emacs Lisp Reference Manual}, for more
7231 about subroutines.
7232@end ignore
6818 7233
6819@node nthcdr, nth, cons, car cdr & cons 7234@node nthcdr, nth, cons, car cdr & cons
6820@comment node-name, next, previous, up 7235@comment node-name, next, previous, up
@@ -7016,9 +7431,14 @@ list, using the @code{setq} function. Here is a list of animals:
7016@noindent 7431@noindent
7017If you are reading this in Info inside of GNU Emacs, you can evaluate 7432If you are reading this in Info inside of GNU Emacs, you can evaluate
7018this expression in the usual fashion, by positioning the cursor after 7433this expression in the usual fashion, by positioning the cursor after
7019the expression and typing @kbd{C-x C-e}. (I'm doing this right here as 7434the expression and typing @kbd{C-x C-e}. (I'm doing this right here
7020I write this. This is one of the advantages of having the interpreter 7435as I write this. This is one of the advantages of having the
7021built into the computing environment.) 7436interpreter built into the computing environment. Incidently, when
7437there is nothing on the line after the final parentheses, such as a
7438comment, point can be on the next line. Thus, if your cursor is in
7439the first column of the next line, you do not need to move it.
7440Indeed, Emacs permits any amount of white space after the final
7441parenthesis.)
7022 7442
7023@need 1200 7443@need 1200
7024When we evaluate the variable @code{animals}, we see that it is bound to 7444When we evaluate the variable @code{animals}, we see that it is bound to
@@ -7156,12 +7576,12 @@ been tempted to replace globally all occurrences of `kill' in the Emacs
7156sources with `clip' and all occurrences of `killed' with `clipped'.) 7576sources with `clip' and all occurrences of `killed' with `clipped'.)
7157 7577
7158@menu 7578@menu
7159* Storing Text:: Text is stored in a list. 7579* Storing Text::
7160* zap-to-char:: Cutting out text up to a character. 7580* zap-to-char::
7161* kill-region:: Cutting text out of a region. 7581* kill-region::
7162* Digression into C:: Minor note on C programming language macros. 7582* copy-region-as-kill::
7163* defvar:: How to give a variable an initial value. 7583* Digression into C::
7164* copy-region-as-kill:: A definition for copying text. 7584* defvar::
7165* cons & search-fwd Review:: 7585* cons & search-fwd Review::
7166* search Exercises:: 7586* search Exercises::
7167@end menu 7587@end menu
@@ -7181,9 +7601,9 @@ look like this:
7181 7601
7182@need 1200 7602@need 1200
7183@noindent 7603@noindent
7184The function @code{cons} can be used to to create a new list from a 7604The function @code{cons} can be used to create a new list from a piece
7185piece of text (an `atom', to use the jargon) and an existing list, 7605of text (an `atom', to use the jargon) and an existing list, like
7186like this: 7606this:
7187 7607
7188@smallexample 7608@smallexample
7189@group 7609@group
@@ -7238,27 +7658,27 @@ retrieved. @xref{Yanking, , Yanking Text Back}.
7238@section @code{zap-to-char} 7658@section @code{zap-to-char}
7239@findex zap-to-char 7659@findex zap-to-char
7240 7660
7241The @code{zap-to-char} function barely changed between GNU Emacs 7661The @code{zap-to-char} function changed a little between GNU Emacs
7242version 19 and GNU Emacs version 21. However, @code{zap-to-char} 7662version 19 and GNU Emacs version 22. However, @code{zap-to-char}
7243calls another function, @code{kill-region}, which enjoyed a major rewrite 7663calls another function, @code{kill-region}, which enjoyed a major
7244on the way to version 21. 7664rewrite.
7245 7665
7246The @code{kill-region} function in Emacs 19 is complex, but does not 7666The @code{kill-region} function in Emacs 19 is complex, but does not
7247use code that is important at this time. We will skip it. 7667use code that is important at this time. We will skip it.
7248 7668
7249The @code{kill-region} function in Emacs 21 is easier to read than the 7669The @code{kill-region} function in Emacs 22 is easier to read than the
7250same function in Emacs 19 and introduces a very important concept, 7670same function in Emacs 19 and introduces a very important concept,
7251that of error handling. We will walk through the function. 7671that of error handling. We will walk through the function.
7252 7672
7253But first, let us look at the interactive @code{zap-to-char} function. 7673But first, let us look at the interactive @code{zap-to-char} function.
7254 7674
7255@menu 7675@menu
7256* Complete zap-to-char:: The complete implementation. 7676* Complete zap-to-char::
7257* zap-to-char interactive:: A three part interactive expression. 7677* zap-to-char interactive::
7258* zap-to-char body:: A short overview. 7678* zap-to-char body::
7259* search-forward:: How to search for a string. 7679* search-forward::
7260* progn:: The @code{progn} special form. 7680* progn::
7261* Summing up zap-to-char:: Using @code{point} and @code{search-forward}. 7681* Summing up zap-to-char::
7262@end menu 7682@end menu
7263 7683
7264@node Complete zap-to-char, zap-to-char interactive, zap-to-char, zap-to-char 7684@node Complete zap-to-char, zap-to-char interactive, zap-to-char, zap-to-char
@@ -7288,12 +7708,8 @@ a search function. Searches are used extensively in code that
7288manipulates text, and we will focus attention on them as well as on the 7708manipulates text, and we will focus attention on them as well as on the
7289deletion command. 7709deletion command.
7290 7710
7291@need 800 7711@ignore
7292Here is the complete text of the version 19 implementation of the function: 7712@c GNU Emacs version 19
7293
7294@c v 19
7295@smallexample
7296@group
7297(defun zap-to-char (arg char) ; version 19 implementation 7713(defun zap-to-char (arg char) ; version 19 implementation
7298 "Kill up to and including ARG'th occurrence of CHAR. 7714 "Kill up to and including ARG'th occurrence of CHAR.
7299Goes backward if ARG is negative; error if CHAR not found." 7715Goes backward if ARG is negative; error if CHAR not found."
@@ -7303,6 +7719,24 @@ Goes backward if ARG is negative; error if CHAR not found."
7303 (search-forward 7719 (search-forward
7304 (char-to-string char) nil nil arg) 7720 (char-to-string char) nil nil arg)
7305 (point)))) 7721 (point))))
7722@end ignore
7723
7724@need 1250
7725Here is the complete text of the version 22 implementation of the function:
7726
7727@c GNU Emacs 22
7728@smallexample
7729@group
7730(defun zap-to-char (arg char)
7731 "Kill up to and including ARG'th occurrence of CHAR.
7732Case is ignored if `case-fold-search' is non-nil in the current buffer.
7733Goes backward if ARG is negative; error if CHAR not found."
7734 (interactive "p\ncZap to char: ")
7735 (if (char-table-p translation-table-for-input)
7736 (setq char (or (aref translation-table-for-input char) char)))
7737 (kill-region (point) (progn
7738 (search-forward (char-to-string char) nil nil arg)
7739 (point))))
7306@end group 7740@end group
7307@end smallexample 7741@end smallexample
7308 7742
@@ -7315,44 +7749,11 @@ The interactive expression in the @code{zap-to-char} command looks like
7315this: 7749this:
7316 7750
7317@smallexample 7751@smallexample
7318(interactive "*p\ncZap to char: ") 7752(interactive "p\ncZap to char: ")
7319@end smallexample 7753@end smallexample
7320
7321The part within quotation marks, @code{"*p\ncZap to char:@: "}, specifies
7322three different things. First, and most simply, the asterisk, @samp{*},
7323causes an error to be signaled if the buffer is read-only. This means that
7324if you try @code{zap-to-char} in a read-only buffer you will not be able to
7325remove text, and you will receive a message that says ``Buffer is
7326read-only''; your terminal may beep at you as well.
7327
7328The version 21 implementation does not have the asterisk, @samp{*}. The
7329function works the same as in version 19: in both cases, it cannot
7330remove text from a read-only buffer but the function does copy the
7331text that would have been removed to the kill ring. Also, in both
7332cases, you see an error message.
7333
7334However, the version 19 implementation copies text from a read-only
7335buffer only because of a mistake in the implementation of
7336@code{interactive}. According to the documentation for
7337@code{interactive}, the asterisk, @samp{*}, should prevent the
7338@code{zap-to-char} function from doing anything at all when the buffer
7339is read only. In version 19, the function should not copy the text to
7340the kill ring. It is a bug that it does.
7341
7342In version 21, the function is designed to copy the text to the kill
7343ring; moreover, @code{interactive} is implemented correctly. So the
7344asterisk, @samp{*}, had to be removed from the interactive
7345specification. However, if you insert an @samp{*} yourself and
7346evaluate the function definition, then the next time you run the
7347@code{zap-to-char} function on a read-only buffer, you will not copy
7348any text.
7349
7350That change aside, and a change to the documentation, the two versions
7351of the @code{zap-to-char} function are identical.
7352
7353Let us continue with the interactive specification.
7354 7754
7355The second part of @code{"*p\ncZap to char:@: "} is the @samp{p}. 7755The part within quotation marks, @code{"p\ncZap to char:@: "}, specifies
7756two different things. First, and most simply, is the @samp{p}.
7356This part is separated from the next part by a newline, @samp{\n}. 7757This part is separated from the next part by a newline, @samp{\n}.
7357The @samp{p} means that the first argument to the function will be 7758The @samp{p} means that the first argument to the function will be
7358passed the value of a `processed prefix'. The prefix argument is 7759passed the value of a `processed prefix'. The prefix argument is
@@ -7360,15 +7761,23 @@ passed by typing @kbd{C-u} and a number, or @kbd{M-} and a number. If
7360the function is called interactively without a prefix, 1 is passed to 7761the function is called interactively without a prefix, 1 is passed to
7361this argument. 7762this argument.
7362 7763
7363The third part of @code{"*p\ncZap to char:@: "} is @samp{cZap to char:@: 7764The second part of @code{"p\ncZap to char:@: "} is
7364}. In this part, the lower case @samp{c} indicates that 7765@samp{cZap to char:@: }. In this part, the lower case @samp{c}
7365@code{interactive} expects a prompt and that the argument will be a 7766indicates that @code{interactive} expects a prompt and that the
7366character. The prompt follows the @samp{c} and is the string @samp{Zap 7767argument will be a character. The prompt follows the @samp{c} and is
7367to char:@: } (with a space after the colon to make it look good). 7768the string @samp{Zap to char:@: } (with a space after the colon to
7769make it look good).
7368 7770
7369What all this does is prepare the arguments to @code{zap-to-char} so they 7771What all this does is prepare the arguments to @code{zap-to-char} so they
7370are of the right type, and give the user a prompt. 7772are of the right type, and give the user a prompt.
7371 7773
7774In a read-only buffer, the @code{zap-to-char} function copies the text
7775to the kill ring, but does not remove it. The echo area displays a
7776message saying that the buffer is read-ly. Also, the terminal may
7777beep or blink at you.
7778
7779Let us continue with the interactive specification.
7780
7372@node zap-to-char body, search-forward, zap-to-char interactive, zap-to-char 7781@node zap-to-char body, search-forward, zap-to-char interactive, zap-to-char
7373@comment node-name, next, previous, up 7782@comment node-name, next, previous, up
7374@subsection The Body of @code{zap-to-char} 7783@subsection The Body of @code{zap-to-char}
@@ -7376,13 +7785,31 @@ are of the right type, and give the user a prompt.
7376The body of the @code{zap-to-char} function contains the code that 7785The body of the @code{zap-to-char} function contains the code that
7377kills (that is, removes) the text in the region from the current 7786kills (that is, removes) the text in the region from the current
7378position of the cursor up to and including the specified character. 7787position of the cursor up to and including the specified character.
7788
7789The documentation is thorough. You do need to know the jargon meaning
7790of the word `kill'.
7791
7379The first part of the code looks like this: 7792The first part of the code looks like this:
7380 7793
7381@smallexample 7794@smallexample
7382(kill-region (point) @dots{} 7795(if (char-table-p translation-table-for-input)
7796 (setq char (or (aref translation-table-for-input char) char)))
7797(kill-region (point) (progn
7798 (search-forward (char-to-string char) nil nil arg)
7799 (point)))
7383@end smallexample 7800@end smallexample
7384 7801
7385@noindent 7802@noindent
7803@code{char-table-p} is an hitherto unseen function. It determines
7804whether its argument is a character table. When it is, it sets the
7805character passed to @code{zap-to-char} to one of them, if that
7806character exists, or to the character itself. (This becomes important
7807for certain characters in non-European languages. The @code{aref}
7808function extracts an element from an array. It is an array-specific
7809function that is not described in this document. @xref{Arrays, ,
7810Arrays, elisp, The GNU Emacs Lisp Reference Manual}.)
7811
7812@noindent
7386@code{(point)} is the current position of the cursor. 7813@code{(point)} is the current position of the cursor.
7387 7814
7388The next part of the code is an expression using @code{progn}. The body 7815The next part of the code is an expression using @code{progn}. The body
@@ -7402,10 +7829,13 @@ The @code{search-forward} function is used to locate the
7402zapped-for-character in @code{zap-to-char}. If the search is 7829zapped-for-character in @code{zap-to-char}. If the search is
7403successful, @code{search-forward} leaves point immediately after the 7830successful, @code{search-forward} leaves point immediately after the
7404last character in the target string. (In @code{zap-to-char}, the 7831last character in the target string. (In @code{zap-to-char}, the
7405target string is just one character long.) If the search is 7832target string is just one character long. @code{zap-to-char} uses the
7406backwards, @code{search-forward} leaves point just before the first 7833function @code{char-to-string} to ensure that the computer treats that
7407character in the target. Also, @code{search-forward} returns @code{t} 7834character as a string.) If the search is backwards,
7408for true. (Moving point is therefore a `side effect'.) 7835@code{search-forward} leaves point just before the first character in
7836the target. Also, @code{search-forward} returns @code{t} for true.
7837(Moving point is therefore a `side effect'.)
7838
7409 7839
7410@need 1250 7840@need 1250
7411In @code{zap-to-char}, the @code{search-forward} function looks like this: 7841In @code{zap-to-char}, the @code{search-forward} function looks like this:
@@ -7501,9 +7931,12 @@ character in the target. The movement of point is a side effect.
7501The second and last argument to @code{progn} is the expression 7931The second and last argument to @code{progn} is the expression
7502@code{(point)}. This expression returns the value of point, which in 7932@code{(point)}. This expression returns the value of point, which in
7503this case will be the location to which it has been moved by 7933this case will be the location to which it has been moved by
7504@code{search-forward}. This value is returned by the @code{progn} 7934@code{search-forward}. (In the source, a line that tells the function
7505expression and is passed to @code{kill-region} as @code{kill-region}'s 7935to go to the previous character, if it is going forward, was commented
7506second argument. 7936out in 1999; I don't remember whether that feature or mis-feature was
7937ever a part of the distributed source.) The value of @code{point} is
7938returned by the @code{progn} expression and is passed to
7939@code{kill-region} as @code{kill-region}'s second argument.
7507 7940
7508@node Summing up zap-to-char, , progn, zap-to-char 7941@node Summing up zap-to-char, , progn, zap-to-char
7509@comment node-name, next, previous, up 7942@comment node-name, next, previous, up
@@ -7520,14 +7953,14 @@ value of this location. The @code{kill-region} function puts together
7520these two values of point, the first one as the beginning of the region 7953these two values of point, the first one as the beginning of the region
7521and the second one as the end of the region, and removes the region. 7954and the second one as the end of the region, and removes the region.
7522 7955
7523The @code{progn} special form is necessary because the @code{kill-region} 7956The @code{progn} special form is necessary because the
7524command takes two arguments; and it would fail if @code{search-forward} 7957@code{kill-region} command takes two arguments; and it would fail if
7525and @code{point} expressions were written in sequence as two 7958@code{search-forward} and @code{point} expressions were written in
7526additional arguments. The @code{progn} expression is a single argument 7959sequence as two additional arguments. The @code{progn} expression is
7527to @code{kill-region} and returns the one value that @code{kill-region} 7960a single argument to @code{kill-region} and returns the one value that
7528needs for its second argument. 7961@code{kill-region} needs for its second argument.
7529 7962
7530@node kill-region, Digression into C, zap-to-char, Cutting & Storing Text 7963@node kill-region, copy-region-as-kill, zap-to-char, Cutting & Storing Text
7531@comment node-name, next, previous, up 7964@comment node-name, next, previous, up
7532@section @code{kill-region} 7965@section @code{kill-region}
7533@findex kill-region 7966@findex kill-region
@@ -7536,7 +7969,125 @@ The @code{zap-to-char} function uses the @code{kill-region} function.
7536This function clips text from a region and copies that text to 7969This function clips text from a region and copies that text to
7537the kill ring, from which it may be retrieved. 7970the kill ring, from which it may be retrieved.
7538 7971
7539The Emacs 21 version of that function uses @code{condition-case} and 7972@ignore
7973GNU Emacs 22:
7974
7975(defun kill-region (beg end &optional yank-handler)
7976 "Kill (\"cut\") text between point and mark.
7977This deletes the text from the buffer and saves it in the kill ring.
7978The command \\[yank] can retrieve it from there.
7979\(If you want to kill and then yank immediately, use \\[kill-ring-save].)
7980
7981If you want to append the killed region to the last killed text,
7982use \\[append-next-kill] before \\[kill-region].
7983
7984If the buffer is read-only, Emacs will beep and refrain from deleting
7985the text, but put the text in the kill ring anyway. This means that
7986you can use the killing commands to copy text from a read-only buffer.
7987
7988This is the primitive for programs to kill text (as opposed to deleting it).
7989Supply two arguments, character positions indicating the stretch of text
7990 to be killed.
7991Any command that calls this function is a \"kill command\".
7992If the previous command was also a kill command,
7993the text killed this time appends to the text killed last time
7994to make one entry in the kill ring.
7995
7996In Lisp code, optional third arg YANK-HANDLER, if non-nil,
7997specifies the yank-handler text property to be set on the killed
7998text. See `insert-for-yank'."
7999 ;; Pass point first, then mark, because the order matters
8000 ;; when calling kill-append.
8001 (interactive (list (point) (mark)))
8002 (unless (and beg end)
8003 (error "The mark is not set now, so there is no region"))
8004 (condition-case nil
8005 (let ((string (filter-buffer-substring beg end t)))
8006 (when string ;STRING is nil if BEG = END
8007 ;; Add that string to the kill ring, one way or another.
8008 (if (eq last-command 'kill-region)
8009 (kill-append string (< end beg) yank-handler)
8010 (kill-new string nil yank-handler)))
8011 (when (or string (eq last-command 'kill-region))
8012 (setq this-command 'kill-region))
8013 nil)
8014 ((buffer-read-only text-read-only)
8015 ;; The code above failed because the buffer, or some of the characters
8016 ;; in the region, are read-only.
8017 ;; We should beep, in case the user just isn't aware of this.
8018 ;; However, there's no harm in putting
8019 ;; the region's text in the kill ring, anyway.
8020 (copy-region-as-kill beg end)
8021 ;; Set this-command now, so it will be set even if we get an error.
8022 (setq this-command 'kill-region)
8023 ;; This should barf, if appropriate, and give us the correct error.
8024 (if kill-read-only-ok
8025 (progn (message "Read only text copied to kill ring") nil)
8026 ;; Signal an error if the buffer is read-only.
8027 (barf-if-buffer-read-only)
8028 ;; If the buffer isn't read-only, the text is.
8029 (signal 'text-read-only (list (current-buffer)))))))
8030
8031
8032kfstorm 18-Jan-03): (defun kill-region (beg end &optional yank-handler)
8033rms 11-Mar-06): "Kill (\"cut\") text between point and mark.
8034rms 11-Mar-06): This deletes the text from the buffer and saves it in the kill ring.
8035jimb 21-Dec-91): The command \\[yank] can retrieve it from there.
8036eliz 16-Feb-01): \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
8037eliz 16-Feb-01):
8038eliz 16-Feb-01): If you want to append the killed region to the last killed text,
8039eliz 16-Feb-01): use \\[append-next-kill] before \\[kill-region].
8040eliz 16-Feb-01):
8041jimb 01-Feb-93): If the buffer is read-only, Emacs will beep and refrain from deleting
8042jimb 01-Feb-93): the text, but put the text in the kill ring anyway. This means that
8043jimb 01-Feb-93): you can use the killing commands to copy text from a read-only buffer.
8044jimb 21-Dec-91):
8045jimb 21-Dec-91): This is the primitive for programs to kill text (as opposed to deleting it).
8046lektu 07-Jun-04): Supply two arguments, character positions indicating the stretch of text
8047jimb 21-Dec-91): to be killed.
8048jimb 21-Dec-91): Any command that calls this function is a \"kill command\".
8049jimb 21-Dec-91): If the previous command was also a kill command,
8050jimb 21-Dec-91): the text killed this time appends to the text killed last time
8051kfstorm 18-Jan-03): to make one entry in the kill ring.
8052kfstorm 18-Jan-03):
8053uid68472 18-Feb-04): In Lisp code, optional third arg YANK-HANDLER, if non-nil,
8054uid68472 18-Feb-04): specifies the yank-handler text property to be set on the killed
8055uid68472 18-Feb-04): text. See `insert-for-yank'."
8056rms 21-May-06): ;; Pass point first, then mark, because the order matters
8057rms 21-May-06): ;; when calling kill-append.
8058rms 21-May-06): (interactive (list (point) (mark)))
8059rms 04-Sep-06): (unless (and beg end)
8060rms 04-Sep-06): (error "The mark is not set now, so there is no region"))
8061kwzh 19-Nov-97): (condition-case nil
8062rms 29-Mar-05): (let ((string (filter-buffer-substring beg end t)))
8063monnier 07-Dec-99): (when string ;STRING is nil if BEG = END
8064monnier 07-Dec-99): ;; Add that string to the kill ring, one way or another.
8065monnier 07-Dec-99): (if (eq last-command 'kill-region)
8066kfstorm 18-Jan-03): (kill-append string (< end beg) yank-handler)
8067kfstorm 18-Jan-03): (kill-new string nil yank-handler)))
8068kai 28-May-03): (when (or string (eq last-command 'kill-region))
8069uid68472 18-Feb-04): (setq this-command 'kill-region))
8070uid68472 18-Feb-04): nil)
8071kwzh 19-Nov-97): ((buffer-read-only text-read-only)
8072kwzh 19-Nov-97): ;; The code above failed because the buffer, or some of the characters
8073kwzh 19-Nov-97): ;; in the region, are read-only.
8074kwzh 19-Nov-97): ;; We should beep, in case the user just isn't aware of this.
8075kwzh 19-Nov-97): ;; However, there's no harm in putting
8076kwzh 19-Nov-97): ;; the region's text in the kill ring, anyway.
8077kwzh 19-Nov-97): (copy-region-as-kill beg end)
8078rms 19-May-98): ;; Set this-command now, so it will be set even if we get an error.
8079rms 19-May-98): (setq this-command 'kill-region)
8080rms 19-May-98): ;; This should barf, if appropriate, and give us the correct error.
8081kwzh 19-Nov-97): (if kill-read-only-ok
8082uid68472 18-Feb-04): (progn (message "Read only text copied to kill ring") nil)
8083kwzh 19-Nov-97): ;; Signal an error if the buffer is read-only.
8084kwzh 19-Nov-97): (barf-if-buffer-read-only)
8085kwzh 19-Nov-97): ;; If the buffer isn't read-only, the text is.
8086kwzh 19-Nov-97): (signal 'text-read-only (list (current-buffer)))))))
8087
8088@end ignore
8089
8090The Emacs 22 version of that function uses @code{condition-case} and
7540@code{copy-region-as-kill}, both of which we will explain. 8091@code{copy-region-as-kill}, both of which we will explain.
7541@code{condition-case} is an important special form. 8092@code{condition-case} is an important special form.
7542 8093
@@ -7547,9 +8098,9 @@ code that does the work when all goes well. The third argument
7547contains the code that is called in the event of an error. 8098contains the code that is called in the event of an error.
7548 8099
7549@menu 8100@menu
7550* Complete kill-region:: The function definition. 8101* Complete kill-region::
7551* condition-case:: Dealing with a problem. 8102* condition-case::
7552* delete-and-extract-region:: Doing the work. 8103* Lisp macro::
7553@end menu 8104@end menu
7554 8105
7555@node Complete kill-region, condition-case, kill-region, kill-region 8106@node Complete kill-region, condition-case, kill-region, kill-region
@@ -7559,10 +8110,115 @@ contains the code that is called in the event of an error.
7559 8110
7560@need 1200 8111@need 1200
7561We will go through the @code{condition-case} code in a moment. First, 8112We will go through the @code{condition-case} code in a moment. First,
7562let us look at the original definition of @code{kill-region}, with 8113let us look at the definition of @code{kill-region}, with comments
7563comments added (the newer definition has an optional third argument 8114added:
7564and is more complex): 8115
8116@c GNU Emacs 22:
8117@smallexample
8118@group
8119(defun kill-region (beg end)
8120 "Kill (\"cut\") text between point and mark.
8121This deletes the text from the buffer and saves it in the kill ring.
8122The command \\[yank] can retrieve it from there. @dots{} "
8123@end group
8124
8125@group
8126 ;; @bullet{} Since order matters, pass point first.
8127 (interactive (list (point) (mark)))
8128 ;; @bullet{} And tell us if we cannot cut the text.
8129 (unless (and beg end)
8130 (error "The mark is not set now, so there is no region"))
8131@end group
8132
8133@group
8134 ;; @bullet{} `condition-case' takes three arguments.
8135 ;; If the first argument is nil, as it is here,
8136 ;; information about the error signal is not
8137 ;; stored for use by another function.
8138 (condition-case nil
8139@end group
8140
8141@group
8142 ;; @bullet{} The second argument to `condition-case' tells the
8143 ;; Lisp interpreter what to do when all goes well.
8144@end group
8145
8146@group
8147 ;; It starts with a `let' function that extracts the string
8148 ;; and tests whether it exists. If so (that is what the
8149 ;; `when' checks), it calls an `if' function that determines
8150 ;; whether the previous command was another call to
8151 ;; `kill-region'; if it was, then the new text is appended to
8152 ;; the previous text; if not, then a different function,
8153 ;; `kill-new', is called.
8154@end group
8155
8156@group
8157 ;; The `kill-append' function concatenates the new string and
8158 ;; the old. The `kill-new' function inserts text into a new
8159 ;; item in the kill ring.
8160@end group
8161
8162@group
8163 ;; `when' is an `if' without an else-part. The second `when'
8164 ;; again checks whether the current string exists; in
8165 ;; addition, it checks whether the previous command was
8166 ;; another call to `kill-region'. If one or the other
8167 ;; condition is true, then it sets the current command to
8168 ;; be `kill-region'.
8169@end group
8170@group
8171 (let ((string (filter-buffer-substring beg end t)))
8172 (when string ;STRING is nil if BEG = END
8173 ;; Add that string to the kill ring, one way or another.
8174 (if (eq last-command 'kill-region)
8175@end group
8176@group
8177 ;; @minus{} `yank-handler' is an optional argument to
8178 ;; `kill-region' that tells the `kill-append' and
8179 ;; `kill-new' functions how deal with properties
8180 ;; added to the text, such as `bold' or `italics'.
8181 (kill-append string (< end beg) yank-handler)
8182 (kill-new string nil yank-handler)))
8183 (when (or string (eq last-command 'kill-region))
8184 (setq this-command 'kill-region))
8185 nil)
8186@end group
8187
8188@group
8189 ;; @bullet{} The third argument to `condition-case' tells the interpreter
8190 ;; what to do with an error.
8191@end group
8192@group
8193 ;; The third argument has a conditions part and a body part.
8194 ;; If the conditions are met (in this case,
8195 ;; if text or buffer are read-only)
8196 ;; then the body is executed.
8197@end group
8198@group
8199 ;; The first part of the third argument is the following:
8200 ((buffer-read-only text-read-only) ;; the if-part
8201 ;; then @dots{}
8202 (copy-region-as-kill beg end)
8203@end group
8204@group
8205 ;; Next, also as part of the then-part, set this-command, so
8206 ;; it will be set in an error
8207 (setq this-command 'kill-region)
8208 ;; Finally, in the then-part, send a message if you may copy
8209 ;; the text to the kill ring without signally an error, but
8210 ;; don't if you may not.
8211@end group
8212@group
8213 (if kill-read-only-ok
8214 (progn (message "Read only text copied to kill ring") nil)
8215 (barf-if-buffer-read-only)
8216 ;; If the buffer isn't read-only, the text is.
8217 (signal 'text-read-only (list (current-buffer)))))
8218@end group
8219@end smallexample
7565 8220
8221@ignore
7566@c v 21 8222@c v 21
7567@smallexample 8223@smallexample
7568@group 8224@group
@@ -7615,7 +8271,7 @@ The text is deleted but saved in the kill ring."
7615@group 8271@group
7616 ;; The third argument has a conditions part and a body part. 8272 ;; The third argument has a conditions part and a body part.
7617 ;; If the conditions are met (in this case, 8273 ;; If the conditions are met (in this case,
7618 ;; if text or buffer is read-only) 8274 ;; if text or buffer are read-only)
7619 ;; then the body is executed. 8275 ;; then the body is executed.
7620@end group 8276@end group
7621@group 8277@group
@@ -7632,8 +8288,9 @@ The text is deleted but saved in the kill ring."
7632 (signal 'text-read-only (list (current-buffer))))))) 8288 (signal 'text-read-only (list (current-buffer)))))))
7633@end group 8289@end group
7634@end smallexample 8290@end smallexample
8291@end ignore
7635 8292
7636@node condition-case, delete-and-extract-region, Complete kill-region, kill-region 8293@node condition-case, Lisp macro, Complete kill-region, kill-region
7637@comment node-name, next, previous, up 8294@comment node-name, next, previous, up
7638@subsection @code{condition-case} 8295@subsection @code{condition-case}
7639@findex condition-case 8296@findex condition-case
@@ -7707,62 +8364,36 @@ In brief, in the @code{kill-region} function, the code
7707@end group 8364@end group
7708@end smallexample 8365@end smallexample
7709 8366
7710@node delete-and-extract-region, , condition-case, kill-region
7711@comment node-name, next, previous, up
7712@subsection @code{delete-and-extract-region}
7713@findex delete-and-extract-region
7714
7715A @code{condition-case} expression has two parts, a part that is
7716evaluated in the expectation that all will go well, but which may
7717generate an error; and a part that is evaluated when there is an
7718error.
7719
7720First, let us look at the code in @code{kill-region} that is run in
7721the expectation that all goes well. This is the core of the function.
7722The code looks like this:
7723
7724@smallexample
7725@group
7726(let ((string (delete-and-extract-region beg end)))
7727 (when string
7728 (if (eq last-command 'kill-region)
7729 (kill-append string (< end beg))
7730 (kill-new string)))
7731 (setq this-command 'kill-region))
7732@end group
7733@end smallexample
7734
7735It looks complicated because we have the new functions
7736@code{delete-and-extract-region}, @code{kill-append}, and
7737@code{kill-new} as well as the new variables,
7738@code{last-command} and @code{this-command}.
7739 8367
7740The @code{delete-and-extract-region} function is straightforward. It 8368@ignore
7741is a built-in function that deletes the text in a region (a side 83692006 Oct 24
7742effect) and also returns that text. This is the function that 8370In Emacs 22,
7743actually removes the text. (And if it cannot do that, it signals the 8371copy-region-as-kill is short, 12 lines, and uses
7744error.) 8372filter-buffer-substring, which is longer, 39 lines
8373and has delete-and-extract-region in it.
8374delete-and-extract-region is written in C.
8375
8376see Initializing a Variable with @code{defvar}
8377this is line 8054
8378Initializing a Variable with @code{defvar} includes line 8350
8379@end ignore
7745 8380
7746In this @code{let} expression, the text that 8381@node Lisp macro, , condition-case, kill-region
7747@code{delete-and-extract-region} returns is placed in the local 8382@comment node-name, next, previous, up
7748variable called @samp{string}. This is the text that is removed from 8383@subsection Lisp macro
7749the buffer. (To be more precise, the variable is set to point to the 8384@cindex Macro, lisp
7750address of the extracted text; to say it is `placed in' the variable 8385@cindex Lisp macro
7751is simply a shorthand.)
7752 8386
7753If the variable @samp{string} does point to text, that text is added 8387The part of the @code{condition-case} expression that is evaluated in
7754to the kill ring. The variable will have a @code{nil} value if no 8388the expectation that all goes well has a @code{when}. The code uses
7755text was removed. 8389@code{when} to determine whether the @code{string} variable points to
8390text that exists.
7756 8391
7757The code uses @code{when} to determine whether the variable 8392A @code{when} expression is simply a programmers' convenience. It is
7758@samp{string} points to text. A @code{when} statement is simply a 8393an @code{if} without the possibility of an else clause. In your mind,
7759programmers' convenience. A @code{when} statement is an @code{if} 8394you can replace @code{when} with @code{if} and understand what goes
7760statement without the possibility of an else clause. In your mind, you 8395on. That is what the Lisp interpreter does.
7761can replace @code{when} with @code{if} and understand what goes on.
7762That is what the Lisp interpreter does.
7763 8396
7764@cindex Macro, lisp
7765@cindex Lisp macro
7766Technically speaking, @code{when} is a Lisp macro. A Lisp @dfn{macro} 8397Technically speaking, @code{when} is a Lisp macro. A Lisp @dfn{macro}
7767enables you to define new control constructs and other language 8398enables you to define new control constructs and other language
7768features. It tells the interpreter how to compute another Lisp 8399features. It tells the interpreter how to compute another Lisp
@@ -7770,8 +8401,12 @@ expression which will in turn compute the value. In this case, the
7770`other expression' is an @code{if} expression. For more about Lisp 8401`other expression' is an @code{if} expression. For more about Lisp
7771macros, see @ref{Macros, , Macros, elisp, The GNU Emacs Lisp Reference 8402macros, see @ref{Macros, , Macros, elisp, The GNU Emacs Lisp Reference
7772Manual}. The C programming language also provides macros. These are 8403Manual}. The C programming language also provides macros. These are
7773different, but also useful. We will briefly look at C macros in 8404different, but also useful.
8405
8406@ignore
8407We will briefly look at C macros in
7774@ref{Digression into C}. 8408@ref{Digression into C}.
8409@end ignore
7775 8410
7776@need 1200 8411@need 1200
7777If the string has content, then another conditional expression is 8412If the string has content, then another conditional expression is
@@ -7780,14 +8415,18 @@ executed. This is an @code{if} with both a then-part and an else-part.
7780@smallexample 8415@smallexample
7781@group 8416@group
7782(if (eq last-command 'kill-region) 8417(if (eq last-command 'kill-region)
7783 (kill-append string (< end beg)) 8418 (kill-append string (< end beg) yank-handler)
7784 (kill-new string))) 8419 (kill-new string nil yank-handler))
7785@end group 8420@end group
7786@end smallexample 8421@end smallexample
7787 8422
7788The then-part is evaluated if the previous command was another call to 8423The then-part is evaluated if the previous command was another call to
7789@code{kill-region}; if not, the else-part is evaluated. 8424@code{kill-region}; if not, the else-part is evaluated.
7790 8425
8426@code{yank-handler} is an optional argument to @code{kill-region} that
8427tells the @code{kill-append} and @code{kill-new} functions how deal
8428with properties added to the text, such as `bold' or `italics'.
8429
7791@code{last-command} is a variable that comes with Emacs that we have 8430@code{last-command} is a variable that comes with Emacs that we have
7792not seen before. Normally, whenever a function is executed, Emacs 8431not seen before. Normally, whenever a function is executed, Emacs
7793sets the value of @code{last-command} to the previous command. 8432sets the value of @code{last-command} to the previous command.
@@ -7797,321 +8436,14 @@ In this segment of the definition, the @code{if} expression checks
7797whether the previous command was @code{kill-region}. If it was, 8436whether the previous command was @code{kill-region}. If it was,
7798 8437
7799@smallexample 8438@smallexample
7800(kill-append string (< end beg)) 8439(kill-append string (< end beg) yank-handler)
7801@end smallexample 8440@end smallexample
7802 8441
7803@noindent 8442@noindent
7804concatenates a copy of the newly clipped text to the just previously 8443concatenates a copy of the newly clipped text to the just previously
7805clipped text in the kill ring. (If the @w{@code{(< end beg))}} 8444clipped text in the kill ring.
7806expression is true, @code{kill-append} prepends the string to the just
7807previously clipped text. For a detailed discussion, see
7808@ref{kill-append function, , The @code{kill-append} function}.)
7809
7810If you then yank back the text, i.e., `paste' it, you get both
7811pieces of text at once. That way, if you delete two words in a row,
7812and then yank them back, you get both words, in their proper order,
7813with one yank. (The @w{@code{(< end beg))}} expression makes sure the
7814order is correct.)
7815
7816On the other hand, if the previous command is not @code{kill-region},
7817then the @code{kill-new} function is called, which adds the text to
7818the kill ring as the latest item, and sets the
7819@code{kill-ring-yank-pointer} variable to point to it.
7820 8445
7821@node Digression into C, defvar, kill-region, Cutting & Storing Text 8446@node copy-region-as-kill, Digression into C, kill-region, Cutting & Storing Text
7822@comment node-name, next, previous, up
7823@section Digression into C
7824@findex delete-and-extract-region
7825@cindex C, a digression into
7826@cindex Digression into C
7827
7828The @code{zap-to-char} command uses the
7829@code{delete-and-extract-region} function, which in turn uses two
7830other functions, @code{copy-region-as-kill} and
7831@code{del_range_1}. The @code{copy-region-as-kill} function will be
7832described in a following section; it puts a copy of the region in the
7833kill ring so it can be yanked back. (@xref{copy-region-as-kill, ,
7834@code{copy-region-as-kill}}.)
7835
7836The @code{delete-and-extract-region} function removes the contents of
7837a region and you cannot get them back.
7838
7839Unlike the other code discussed here, @code{delete-and-extract-region}
7840is not written in Emacs Lisp; it is written in C and is one of the
7841primitives of the GNU Emacs system. Since it is very simple, I will
7842digress briefly from Lisp and describe it here.
7843
7844@need 1500
7845Like many of the other Emacs primitives,
7846@code{delete-and-extract-region} is written as an instance of a C
7847macro, a macro being a template for code. The complete macro looks
7848like this:
7849
7850@c /usr/local/src/emacs/src/editfns.c
7851@smallexample
7852@group
7853DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
7854 Sdelete_and_extract_region, 2, 2, 0,
7855 "Delete the text between START and END and return it.")
7856 (start, end)
7857 Lisp_Object start, end;
7858@{
7859 validate_region (&start, &end);
7860 return del_range_1 (XINT (start), XINT (end), 1, 1);
7861@}
7862@end group
7863@end smallexample
7864
7865Without going into the details of the macro writing process, let me
7866point out that this macro starts with the word @code{DEFUN}. The word
7867@code{DEFUN} was chosen since the code serves the same purpose as
7868@code{defun} does in Lisp. The word @code{DEFUN} is followed by seven
7869parts inside of parentheses:
7870
7871@itemize @bullet
7872@item
7873The first part is the name given to the function in Lisp,
7874@code{delete-and-extract-region}.
7875
7876@item
7877The second part is the name of the function in C,
7878@code{Fdelete_and_extract_region}. By convention, it starts with
7879@samp{F}. Since C does not use hyphens in names, underscores are used
7880instead.
7881
7882@item
7883The third part is the name for the C constant structure that records
7884information on this function for internal use. It is the name of the
7885function in C but begins with an @samp{S} instead of an @samp{F}.
7886
7887@item
7888The fourth and fifth parts specify the minimum and maximum number of
7889arguments the function can have. This function demands exactly 2
7890arguments.
7891
7892@item
7893The sixth part is nearly like the argument that follows the
7894@code{interactive} declaration in a function written in Lisp: a letter
7895followed, perhaps, by a prompt. The only difference from the Lisp is
7896when the macro is called with no arguments. Then you write a @code{0}
7897(which is a `null string'), as in this macro.
7898
7899If you were to specify arguments, you would place them between
7900quotation marks. The C macro for @code{goto-char} includes
7901@code{"NGoto char: "} in this position to indicate that the function
7902expects a raw prefix, in this case, a numerical location in a buffer,
7903and provides a prompt.
7904
7905@item
7906The seventh part is a documentation string, just like the one for a
7907function written in Emacs Lisp, except that every newline must be
7908written explicitly as @samp{\n} followed by a backslash and carriage
7909return.
7910
7911@need 1000
7912Thus, the first two lines of documentation for @code{goto-char} are
7913written like this:
7914
7915@smallexample
7916@group
7917 "Set point to POSITION, a number or marker.\n\
7918Beginning of buffer is position (point-min), end is (point-max).
7919@end group
7920@end smallexample
7921@end itemize
7922
7923@need 1200
7924In a C macro, the formal parameters come next, with a statement of
7925what kind of object they are, followed by what might be called the `body'
7926of the macro. For @code{delete-and-extract-region} the `body'
7927consists of the following two lines:
7928
7929@smallexample
7930@group
7931validate_region (&start, &end);
7932return del_range_1 (XINT (start), XINT (end), 1, 1);
7933@end group
7934@end smallexample
7935
7936The first function, @code{validate_region} checks whether the values
7937passed as the beginning and end of the region are the proper type and
7938are within range. The second function, @code{del_range_1}, actually
7939deletes the text.
7940
7941@code{del_range_1} is a complex function we will not look into. It
7942updates the buffer and does other things.
7943
7944However, it is worth looking at the two arguments passed to
7945@code{del_range}. These are @w{@code{XINT (start)}} and @w{@code{XINT
7946(end)}}.
7947
7948As far as the C language is concerned, @code{start} and @code{end} are
7949two integers that mark the beginning and end of the region to be
7950deleted@footnote{More precisely, and requiring more expert knowledge
7951to understand, the two integers are of type `Lisp_Object', which can
7952also be a C union instead of an integer type.}.
7953
7954In early versions of Emacs, these two numbers were thirty-two bits
7955long, but the code is slowly being generalized to handle other
7956lengths. Three of the available bits are used to specify the type of
7957information and a fourth bit is used for handling the computer's
7958memory; the remaining bits are used as `content'.
7959
7960@samp{XINT} is a C macro that extracts the relevant number from the
7961longer collection of bits; the four other bits are discarded.
7962
7963@need 800
7964The command in @code{delete-and-extract-region} looks like this:
7965
7966@smallexample
7967del_range_1 (XINT (start), XINT (end), 1, 1);
7968@end smallexample
7969
7970@noindent
7971It deletes the region between the beginning position, @code{start},
7972and the ending position, @code{end}.
7973
7974From the point of view of the person writing Lisp, Emacs is all very
7975simple; but hidden underneath is a great deal of complexity to make it
7976all work.
7977
7978@node defvar, copy-region-as-kill, Digression into C, Cutting & Storing Text
7979@comment node-name, next, previous, up
7980@section Initializing a Variable with @code{defvar}
7981@findex defvar
7982@cindex Initializing a variable
7983@cindex Variable initialization
7984
7985Unlike the @code{delete-and-extract-region} function, the
7986@code{copy-region-as-kill} function is written in Emacs Lisp. Two
7987functions within it, @code{kill-append} and @code{kill-new}, copy a
7988region in a buffer and save it in a variable called the
7989@code{kill-ring}. This section describes how the @code{kill-ring}
7990variable is created and initialized using the @code{defvar} special
7991form.
7992
7993(Again we note that the term @code{kill-ring} is a misnomer. The text
7994that is clipped out of the buffer can be brought back; it is not a ring
7995of corpses, but a ring of resurrectable text.)
7996
7997In Emacs Lisp, a variable such as the @code{kill-ring} is created and
7998given an initial value by using the @code{defvar} special form. The
7999name comes from ``define variable''.
8000
8001The @code{defvar} special form is similar to @code{setq} in that it sets
8002the value of a variable. It is unlike @code{setq} in two ways: first,
8003it only sets the value of the variable if the variable does not already
8004have a value. If the variable already has a value, @code{defvar} does
8005not override the existing value. Second, @code{defvar} has a
8006documentation string.
8007
8008(Another special form, @code{defcustom}, is designed for variables
8009that people customize. It has more features than @code{defvar}.
8010(@xref{defcustom, , Setting Variables with @code{defcustom}}.)
8011
8012@menu
8013* See variable current value::
8014* defvar and asterisk:: An old-time convention.
8015@end menu
8016
8017@node See variable current value, defvar and asterisk, defvar, defvar
8018@ifnottex
8019@unnumberedsubsec Seeing the Current Value of a Variable
8020@end ifnottex
8021
8022You can see the current value of a variable, any variable, by using
8023the @code{describe-variable} function, which is usually invoked by
8024typing @kbd{C-h v}. If you type @kbd{C-h v} and then @code{kill-ring}
8025(followed by @key{RET}) when prompted, you will see what is in your
8026current kill ring---this may be quite a lot! Conversely, if you have
8027been doing nothing this Emacs session except read this document, you
8028may have nothing in it. Also, you will see the documentation for
8029@code{kill-ring}:
8030
8031@smallexample
8032@group
8033Documentation:
8034List of killed text sequences.
8035Since the kill ring is supposed to interact nicely with cut-and-paste
8036facilities offered by window systems, use of this variable should
8037@end group
8038@group
8039interact nicely with `interprogram-cut-function' and
8040`interprogram-paste-function'. The functions `kill-new',
8041`kill-append', and `current-kill' are supposed to implement this
8042interaction; you may want to use them instead of manipulating the kill
8043ring directly.
8044@end group
8045@end smallexample
8046
8047@need 800
8048The kill ring is defined by a @code{defvar} in the following way:
8049
8050@smallexample
8051@group
8052(defvar kill-ring nil
8053 "List of killed text sequences.
8054@dots{}")
8055@end group
8056@end smallexample
8057
8058@noindent
8059In this variable definition, the variable is given an initial value of
8060@code{nil}, which makes sense, since if you have saved nothing, you want
8061nothing back if you give a @code{yank} command. The documentation
8062string is written just like the documentation string of a @code{defun}.
8063As with the documentation string of the @code{defun}, the first line of
8064the documentation should be a complete sentence, since some commands,
8065like @code{apropos}, print only the first line of documentation.
8066Succeeding lines should not be indented; otherwise they look odd when
8067you use @kbd{C-h v} (@code{describe-variable}).
8068
8069@node defvar and asterisk, , See variable current value, defvar
8070@subsection @code{defvar} and an asterisk
8071@findex defvar @r{for a user customizable variable}
8072@findex defvar @r{with an asterisk}
8073
8074In the past, Emacs used the @code{defvar} special form both for
8075internal variables that you would not expect a user to change and for
8076variables that you do expect a user to change. Although you can still
8077use @code{defvar} for user customizable variables, please use
8078@code{defcustom} instead, since that special form provides a path into
8079the Customization commands. (@xref{defcustom, , Specifying Variables
8080using @code{defcustom}}.)
8081
8082When you specified a variable using the @code{defvar} special form,
8083you could distinguish a readily settable variable from others by
8084typing an asterisk, @samp{*}, in the first column of its documentation
8085string. For example:
8086
8087@smallexample
8088@group
8089(defvar shell-command-default-error-buffer nil
8090 "*Buffer name for `shell-command' @dots{} error output.
8091@dots{} ")
8092@end group
8093@end smallexample
8094
8095@findex set-variable
8096@noindent
8097You could (and still can) use the @code{set-variable} command to
8098change the value of @code{shell-command-default-error-buffer}
8099temporarily. However, options set using @code{set-variable} are set
8100only for the duration of your editing session. The new values are not
8101saved between sessions. Each time Emacs starts, it reads the original
8102value, unless you change the value within your @file{.emacs} file,
8103either by setting it manually or by using @code{customize}.
8104@xref{Emacs Initialization, , Your @file{.emacs} File}.
8105
8106For me, the major use of the @code{set-variable} command is to suggest
8107variables that I might want to set in my @file{.emacs} file. There
8108are now more than 700 such variables --- far too many to remember
8109readily. Fortunately, you can press @key{TAB} after calling the
8110@code{M-x set-variable} command to see the list of variables.
8111(@xref{Examining, , Examining and Setting Variables, emacs,
8112The GNU Emacs Manual}.)
8113
8114@node copy-region-as-kill, cons & search-fwd Review, defvar, Cutting & Storing Text
8115@comment node-name, next, previous, up 8447@comment node-name, next, previous, up
8116@section @code{copy-region-as-kill} 8448@section @code{copy-region-as-kill}
8117@findex copy-region-as-kill 8449@findex copy-region-as-kill
@@ -8129,8 +8461,8 @@ hand, if some other command precedes the @code{copy-region-as-kill},
8129the function copies the text into a separate entry in the kill ring. 8461the function copies the text into a separate entry in the kill ring.
8130 8462
8131@menu 8463@menu
8132* Complete copy-region-as-kill:: The complete function definition. 8464* Complete copy-region-as-kill::
8133* copy-region-as-kill body:: The body of @code{copy-region-as-kill}. 8465* copy-region-as-kill body::
8134@end menu 8466@end menu
8135 8467
8136@node Complete copy-region-as-kill, copy-region-as-kill body, copy-region-as-kill, copy-region-as-kill 8468@node Complete copy-region-as-kill, copy-region-as-kill body, copy-region-as-kill, copy-region-as-kill
@@ -8139,24 +8471,22 @@ the function copies the text into a separate entry in the kill ring.
8139@end ifnottex 8471@end ifnottex
8140 8472
8141@need 1200 8473@need 1200
8142Here is the complete text of the version 21 @code{copy-region-as-kill} 8474Here is the complete text of the version 22 @code{copy-region-as-kill}
8143function: 8475function:
8144 8476
8145@c !!! for no text properties, use buffer-substring-no-properties
8146
8147@smallexample 8477@smallexample
8148@group 8478@group
8149(defun copy-region-as-kill (beg end) 8479(defun copy-region-as-kill (beg end)
8150 "Save the region as if killed, but don't kill it. 8480 "Save the region as if killed, but don't kill it.
8151In Transient Mark mode, deactivate the mark. 8481In Transient Mark mode, deactivate the mark.
8152If `interprogram-cut-function' is non-nil, also save 8482If `interprogram-cut-function' is non-nil, also save the text for a window
8153the text for a window system cut and paste." 8483system cut and paste."
8154 (interactive "r") 8484 (interactive "r")
8155@end group 8485@end group
8156@group 8486@group
8157 (if (eq last-command 'kill-region) 8487 (if (eq last-command 'kill-region)
8158 (kill-append (buffer-substring beg end) (< end beg)) 8488 (kill-append (filter-buffer-substring beg end) (< end beg))
8159 (kill-new (buffer-substring beg end))) 8489 (kill-new (filter-buffer-substring beg end)))
8160@end group 8490@end group
8161@group 8491@group
8162 (if transient-mark-mode 8492 (if transient-mark-mode
@@ -8184,9 +8514,9 @@ document from the beginning, understanding these parts of a function is
8184almost becoming routine. 8514almost becoming routine.
8185 8515
8186The documentation is somewhat confusing unless you remember that the 8516The documentation is somewhat confusing unless you remember that the
8187word `kill' has a meaning different from its usual meaning. The 8517word `kill' has a meaning different from usual. The `Transient Mark'
8188`Transient Mark' and @code{interprogram-cut-function} comments explain 8518and @code{interprogram-cut-function} comments explain certain
8189certain side-effects. 8519side-effects.
8190 8520
8191After you once set a mark, a buffer always contains a region. If you 8521After you once set a mark, a buffer always contains a region. If you
8192wish, you can use Transient Mark mode to highlight the region 8522wish, you can use Transient Mark mode to highlight the region
@@ -8219,14 +8549,14 @@ The body of @code{copy-region-as-kill} merits discussion in detail.
8219@subsection The Body of @code{copy-region-as-kill} 8549@subsection The Body of @code{copy-region-as-kill}
8220 8550
8221The @code{copy-region-as-kill} function works in much the same way as 8551The @code{copy-region-as-kill} function works in much the same way as
8222the @code{kill-region} function (@pxref{kill-region, 8552the @code{kill-region} function. Both are written so that two or more
8223,@code{kill-region}}). Both are written so that two or more kills in 8553kills in a row combine their text into a single entry. If you yank
8224a row combine their text into a single entry. If you yank back the 8554back the text from the kill ring, you get it all in one piece.
8225text from the kill ring, you get it all in one piece. Moreover, kills 8555Moreover, kills that kill forward from the current position of the
8226that kill forward from the current position of the cursor are added to 8556cursor are added to the end of the previously copied text and commands
8227the end of the previously copied text and commands that copy text 8557that copy text backwards add it to the beginning of the previously
8228backwards add it to the beginning of the previously copied text. This 8558copied text. This way, the words in the text stay in the proper
8229way, the words in the text stay in the proper order. 8559order.
8230 8560
8231Like @code{kill-region}, the @code{copy-region-as-kill} function makes 8561Like @code{kill-region}, the @code{copy-region-as-kill} function makes
8232use of the @code{last-command} variable that keeps track of the 8562use of the @code{last-command} variable that keeps track of the
@@ -8268,12 +8598,20 @@ a function we have not yet seen:
8268@group 8598@group
8269 (if (eq last-command 'kill-region) 8599 (if (eq last-command 'kill-region)
8270 ;; @r{then-part} 8600 ;; @r{then-part}
8271 (kill-append (buffer-substring beg end) (< end beg)) 8601 (kill-append (filter-buffer-substring beg end) (< end beg))
8272 ;; @r{else-part} 8602 ;; @r{else-part}
8273 (kill-new (buffer-substring beg end))) 8603 (kill-new (filter-buffer-substring beg end)))
8274@end group 8604@end group
8275@end smallexample 8605@end smallexample
8276 8606
8607@findex filter-buffer-substring
8608(The @code{filter-buffer-substring} function returns a filtered
8609substring of the buffer, if any. Optionally---the arguments are not
8610here, so neither is done---the function may delete the initial text or
8611return the text without its properties; this function is a replacement
8612for the older @code{buffer-substring} function, which came before text
8613properties were implemented.)
8614
8277@findex eq @r{(example of use)} 8615@findex eq @r{(example of use)}
8278@noindent 8616@noindent
8279The @code{eq} function tests whether its first argument is the same Lisp 8617The @code{eq} function tests whether its first argument is the same Lisp
@@ -8294,8 +8632,23 @@ interpreter calls the @code{kill-append} function
8294@need 800 8632@need 800
8295The @code{kill-append} function looks like this: 8633The @code{kill-append} function looks like this:
8296 8634
8635@c in GNU Emacs 22
8297@smallexample 8636@smallexample
8298@group 8637@group
8638(defun kill-append (string before-p &optional yank-handler)
8639 "Append STRING to the end of the latest kill in the kill ring.
8640If BEFORE-P is non-nil, prepend STRING to the kill.
8641@dots{} "
8642 (let* ((cur (car kill-ring)))
8643 (kill-new (if before-p (concat string cur) (concat cur string))
8644 (or (= (length cur) 0)
8645 (equal yank-handler (get-text-property 0 'yank-handler cur)))
8646 yank-handler)))
8647@end group
8648@end smallexample
8649
8650@ignore
8651was:
8299(defun kill-append (string before-p) 8652(defun kill-append (string before-p)
8300 "Append STRING to the end of the latest kill in the kill ring. 8653 "Append STRING to the end of the latest kill in the kill ring.
8301If BEFORE-P is non-nil, prepend STRING to the kill. 8654If BEFORE-P is non-nil, prepend STRING to the kill.
@@ -8305,24 +8658,34 @@ it."
8305 (concat string (car kill-ring)) 8658 (concat string (car kill-ring))
8306 (concat (car kill-ring) string)) 8659 (concat (car kill-ring) string))
8307 t)) 8660 t))
8308@end group 8661@end ignore
8309@end smallexample
8310 8662
8311@noindent 8663@noindent
8312The @code{kill-append} function is fairly straightforward. It uses 8664The @code{kill-append} function is fairly straightforward. It uses
8313the @code{kill-new} function, which we will discuss in more detail in 8665the @code{kill-new} function, which we will discuss in more detail in
8314a moment. 8666a moment.
8315 8667
8316First, let us look at the conditional that is one of the two arguments 8668(Also, the function provides an optional argument called
8317to @code{kill-new}. It uses @code{concat} to concatenate the new text 8669@code{yank-handler}; when invoked, this argument tells the function
8318to the @sc{car} of the kill ring. Whether it prepends or appends the 8670how to deal with properties added to the text, such as `bold' or
8671`italics'.)
8672
8673@c !!! bug in GNU Emacs 22 version of kill-append ?
8674It has a @code{let*} function to set the value of the first element of
8675the kill ring to @code{cur}. (I do not know why the function does not
8676use @code{let} instead; only one value is set in the expression.
8677Perhaps this is a bug that produces no problems?)
8678
8679Consider the conditional that is one of the two arguments to
8680@code{kill-new}. It uses @code{concat} to concatenate the new text to
8681the @sc{car} of the kill ring. Whether it prepends or appends the
8319text depends on the results of an @code{if} expression: 8682text depends on the results of an @code{if} expression:
8320 8683
8321@smallexample 8684@smallexample
8322@group 8685@group
8323(if before-p ; @r{if-part} 8686(if before-p ; @r{if-part}
8324 (concat string (car kill-ring)) ; @r{then-part} 8687 (concat string cur) ; @r{then-part}
8325 (concat (car kill-ring) string)) ; @r{else-part} 8688 (concat cur string)) ; @r{else-part}
8326@end group 8689@end group
8327@end smallexample 8690@end smallexample
8328 8691
@@ -8355,7 +8718,7 @@ When the newly saved text will be prepended, then the string with the new
8355text will be concatenated before the old text: 8718text will be concatenated before the old text:
8356 8719
8357@smallexample 8720@smallexample
8358(concat string (car kill-ring)) 8721(concat string cur)
8359@end smallexample 8722@end smallexample
8360 8723
8361@need 1200 8724@need 1200
@@ -8364,7 +8727,7 @@ But if the text will be appended, it will be concatenated
8364after the old text: 8727after the old text:
8365 8728
8366@smallexample 8729@smallexample
8367(concat (car kill-ring) string)) 8730(concat cur string))
8368@end smallexample 8731@end smallexample
8369 8732
8370To understand how this works, we first need to review the 8733To understand how this works, we first need to review the
@@ -8397,28 +8760,51 @@ function which in turn uses the @code{setcar} function.
8397@unnumberedsubsubsec The @code{kill-new} function 8760@unnumberedsubsubsec The @code{kill-new} function
8398@findex kill-new 8761@findex kill-new
8399 8762
8763@c in GNU Emacs 22, additional documentation to kill-new:
8764@ignore
8765Optional third arguments YANK-HANDLER controls how the STRING is later
8766inserted into a buffer; see `insert-for-yank' for details.
8767When a yank handler is specified, STRING must be non-empty (the yank
8768handler, if non-nil, is stored as a `yank-handler' text property on STRING).
8769
8770When the yank handler has a non-nil PARAM element, the original STRING
8771argument is not used by `insert-for-yank'. However, since Lisp code
8772may access and use elements from the kill ring directly, the STRING
8773argument should still be a \"useful\" string for such uses."
8774@end ignore
8400@need 1200 8775@need 1200
8401The @code{kill-new} function looks like this: 8776The @code{kill-new} function looks like this:
8402 8777
8403@smallexample 8778@smallexample
8404@group 8779@group
8405(defun kill-new (string &optional replace) 8780(defun kill-new (string &optional replace yank-handler)
8406 "Make STRING the latest kill in the kill ring. 8781 "Make STRING the latest kill in the kill ring.
8407Set the kill-ring-yank pointer to point to it. 8782Set `kill-ring-yank-pointer' to point to it.
8783
8408If `interprogram-cut-function' is non-nil, apply it to STRING. 8784If `interprogram-cut-function' is non-nil, apply it to STRING.
8409Optional second argument REPLACE non-nil means that STRING will replace 8785Optional second argument REPLACE non-nil means that STRING will replace
8410the front of the kill ring, rather than being added to the list." 8786the front of the kill ring, rather than being added to the list.
8787@dots{}"
8411@end group 8788@end group
8412@group 8789@group
8413 (and (fboundp 'menu-bar-update-yank-menu) 8790 (if (> (length string) 0)
8414 (menu-bar-update-yank-menu string (and replace (car kill-ring)))) 8791 (if yank-handler
8792 (put-text-property 0 (length string)
8793 'yank-handler yank-handler string))
8794 (if yank-handler
8795 (signal 'args-out-of-range
8796 (list string "yank-handler specified for empty string"))))
8797@end group
8798@group
8799 (if (fboundp 'menu-bar-update-yank-menu)
8800 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
8415@end group 8801@end group
8416@group 8802@group
8417 (if (and replace kill-ring) 8803 (if (and replace kill-ring)
8418 (setcar kill-ring string) 8804 (setcar kill-ring string)
8419 (setq kill-ring (cons string kill-ring)) 8805 (push string kill-ring)
8420 (if (> (length kill-ring) kill-ring-max) 8806 (if (> (length kill-ring) kill-ring-max)
8421 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) 8807 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
8422@end group 8808@end group
8423@group 8809@group
8424 (setq kill-ring-yank-pointer kill-ring) 8810 (setq kill-ring-yank-pointer kill-ring)
@@ -8426,9 +8812,35 @@ the front of the kill ring, rather than being added to the list."
8426 (funcall interprogram-cut-function string (not replace)))) 8812 (funcall interprogram-cut-function string (not replace))))
8427@end group 8813@end group
8428@end smallexample 8814@end smallexample
8815@ignore
8816was:
8817(defun kill-new (string &optional replace)
8818 "Make STRING the latest kill in the kill ring.
8819Set the kill-ring-yank pointer to point to it.
8820If `interprogram-cut-function' is non-nil, apply it to STRING.
8821Optional second argument REPLACE non-nil means that STRING will replace
8822the front of the kill ring, rather than being added to the list."
8823 (and (fboundp 'menu-bar-update-yank-menu)
8824 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
8825 (if (and replace kill-ring)
8826 (setcar kill-ring string)
8827 (setq kill-ring (cons string kill-ring))
8828 (if (> (length kill-ring) kill-ring-max)
8829 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
8830 (setq kill-ring-yank-pointer kill-ring)
8831 (if interprogram-cut-function
8832 (funcall interprogram-cut-function string (not replace))))
8833@end ignore
8834
8835@noindent
8836(Notice that the function is not interactive.)
8429 8837
8430As usual, we can look at this function in parts. 8838As usual, we can look at this function in parts.
8431 8839
8840The function definition has an optional @code{yank-handler} argument,
8841which when invoked tells the function how to deal with properties
8842added to the text, such as `bold' or `italics'. We will skip that.
8843
8432@need 1200 8844@need 1200
8433The first line of the documentation makes sense: 8845The first line of the documentation makes sense:
8434 8846
@@ -8439,8 +8851,9 @@ Make STRING the latest kill in the kill ring.
8439@noindent 8851@noindent
8440Let's skip over the rest of the documentation for the moment. 8852Let's skip over the rest of the documentation for the moment.
8441 8853
8442Also, let's skip over the first two lines of code, those involving 8854Also, let's skip over the initial @code{if} expression and those lines
8443@code{menu-bar-update-yank-menu}. We will explain them below. 8855of code involving @code{menu-bar-update-yank-menu}. We will explain
8856them below.
8444 8857
8445@need 1200 8858@need 1200
8446The critical lines are these: 8859The critical lines are these:
@@ -8453,6 +8866,9 @@ The critical lines are these:
8453@end group 8866@end group
8454@group 8867@group
8455 ;; @r{else} 8868 ;; @r{else}
8869 (push string kill-ring)
8870@end group
8871@group
8456 (setq kill-ring (cons string kill-ring)) 8872 (setq kill-ring (cons string kill-ring))
8457 (if (> (length kill-ring) kill-ring-max) 8873 (if (> (length kill-ring) kill-ring-max)
8458 ;; @r{avoid overly long kill ring} 8874 ;; @r{avoid overly long kill ring}
@@ -8470,8 +8886,8 @@ This will be true when two conditions are met: the kill ring has
8470something in it, and the @code{replace} variable is true. 8886something in it, and the @code{replace} variable is true.
8471 8887
8472@need 1250 8888@need 1250
8473The @code{kill-append} function sets @code{replace} to be true; then, 8889When the @code{kill-append} function sets @code{replace} to be true
8474when the kill ring has at least one item in it, the @code{setcar} 8890and when the kill ring has at least one item in it, the @code{setcar}
8475expression is executed: 8891expression is executed:
8476 8892
8477@smallexample 8893@smallexample
@@ -8482,31 +8898,49 @@ The @code{setcar} function actually changes the first element of the
8482@code{kill-ring} list to the value of @code{string}. It replaces the 8898@code{kill-ring} list to the value of @code{string}. It replaces the
8483first element. 8899first element.
8484 8900
8901@need 1250
8485On the other hand, if the kill ring is empty, or replace is false, the 8902On the other hand, if the kill ring is empty, or replace is false, the
8486else-part of the condition is executed: 8903else-part of the condition is executed:
8487 8904
8488@smallexample 8905@smallexample
8489@group 8906(push string kill-ring)
8907@end smallexample
8908
8909@noindent
8910@need 1250
8911@code{push} puts its first argument onto the second. It is the same
8912as the older
8913
8914@smallexample
8490(setq kill-ring (cons string kill-ring)) 8915(setq kill-ring (cons string kill-ring))
8491(if (> (length kill-ring) kill-ring-max)
8492 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
8493@end group
8494@end smallexample 8916@end smallexample
8495 8917
8496@noindent 8918@noindent
8497This expression first constructs a new version of the kill ring by 8919@need 1250
8498prepending @code{string} to the existing kill ring as a new element. 8920or the newer
8499Then it executes a second @code{if} clause. This second @code{if} 8921
8500clause keeps the kill ring from growing too long. 8922@smallexample
8923(add-to-list kill-ring string)
8924@end smallexample
8925
8926@noindent
8927When it is false, the expression first constructs a new version of the
8928kill ring by prepending @code{string} to the existing kill ring as a
8929new element (that is what the @code{push} does). Then it executes a
8930second @code{if} clause. This second @code{if} clause keeps the kill
8931ring from growing too long.
8501 8932
8502Let's look at these two expressions in order. 8933Let's look at these two expressions in order.
8503 8934
8504The @code{setq} line of the else-part sets the new value of the kill 8935The @code{push} line of the else-part sets the new value of the kill
8505ring to what results from adding the string being killed to the old kill 8936ring to what results from adding the string being killed to the old
8506ring. 8937kill ring.
8938
8939
8940We can see how this works with an example.
8507 8941
8508@need 800 8942@need 800
8509We can see how this works with an example: 8943First,
8510 8944
8511@smallexample 8945@smallexample
8512(setq example-list '("here is a clause" "another clause")) 8946(setq example-list '("here is a clause" "another clause"))
@@ -8528,10 +8962,10 @@ example-list
8528@noindent 8962@noindent
8529Now, we can add a new element on to this list by evaluating the 8963Now, we can add a new element on to this list by evaluating the
8530following expression: 8964following expression:
8531@findex cons, @r{example} 8965@findex push, @r{example}
8532 8966
8533@smallexample 8967@smallexample
8534(setq example-list (cons "a third clause" example-list)) 8968(push "a third clause" example-list)
8535@end smallexample 8969@end smallexample
8536 8970
8537@need 800 8971@need 800
@@ -8546,15 +8980,7 @@ example-list
8546@end smallexample 8980@end smallexample
8547 8981
8548@noindent 8982@noindent
8549Thus, the third clause was added to the list by @code{cons}. 8983Thus, the third clause is added to the list by @code{push}.
8550
8551@need 1200
8552This is exactly similar to what the @code{setq} and @code{cons} do in
8553the function. Here is the line again:
8554
8555@smallexample
8556(setq kill-ring (cons string kill-ring))
8557@end smallexample
8558 8984
8559@need 1200 8985@need 1200
8560Now for the second part of the @code{if} clause. This expression 8986Now for the second part of the @code{if} clause. This expression
@@ -8587,13 +9013,15 @@ the last element of the kill ring.
8587The @code{nthcdr} function works by repeatedly taking the @sc{cdr} of a 9013The @code{nthcdr} function works by repeatedly taking the @sc{cdr} of a
8588list---it takes the @sc{cdr} of the @sc{cdr} of the @sc{cdr} 9014list---it takes the @sc{cdr} of the @sc{cdr} of the @sc{cdr}
8589@dots{} It does this @var{N} times and returns the results. 9015@dots{} It does this @var{N} times and returns the results.
9016(@xref{nthcdr, , @code{nthcdr}}.)
8590 9017
8591@findex setcdr, @r{example} 9018@findex setcdr, @r{example}
8592Thus, if we had a four element list that was supposed to be three 9019Thus, if we had a four element list that was supposed to be three
8593elements long, we could set the @sc{cdr} of the next to last element 9020elements long, we could set the @sc{cdr} of the next to last element
8594to @code{nil}, and thereby shorten the list. (If you sent the last 9021to @code{nil}, and thereby shorten the list. (If you sent the last
8595element to some other value than @code{nil}, which you could do, then 9022element to some other value than @code{nil}, which you could do, then
8596you would not have shortened the list.) 9023you would not have shortened the list. @xref{setcdr, ,
9024@code{setcdr}}.)
8597 9025
8598You can see shortening by evaluating the following three expressions 9026You can see shortening by evaluating the following three expressions
8599in turn. First set the value of @code{trees} to @code{(maple oak pine 9027in turn. First set the value of @code{trees} to @code{(maple oak pine
@@ -8621,9 +9049,9 @@ that is what the @sc{cdr} is set to.)
8621 9049
8622To repeat, in @code{kill-new}, the @code{nthcdr} function takes the 9050To repeat, in @code{kill-new}, the @code{nthcdr} function takes the
8623@sc{cdr} a number of times that is one less than the maximum permitted 9051@sc{cdr} a number of times that is one less than the maximum permitted
8624size of the kill ring and sets the @sc{cdr} of that element (which 9052size of the kill ring and @code{setcdr} sets the @sc{cdr} of that
8625will be the rest of the elements in the kill ring) to @code{nil}. 9053element (which will be the rest of the elements in the kill ring) to
8626This prevents the kill ring from growing too long. 9054@code{nil}. This prevents the kill ring from growing too long.
8627 9055
8628@need 800 9056@need 800
8629The next to last expression in the @code{kill-new} function is 9057The next to last expression in the @code{kill-new} function is
@@ -8642,28 +9070,17 @@ The variable is used in functions such as @code{yank} and
8642@code{yank-pop} (@pxref{Yanking, , Yanking Text Back}). 9070@code{yank-pop} (@pxref{Yanking, , Yanking Text Back}).
8643 9071
8644@need 1200 9072@need 1200
8645Now, to return to the first two lines in the body of the function: 9073Now, to return to an early expression in the body of the function:
8646 9074
8647@smallexample 9075@smallexample
8648@group 9076@group
8649 (and (fboundp 'menu-bar-update-yank-menu) 9077 (if (fboundp 'menu-bar-update-yank-menu)
8650 (menu-bar-update-yank-menu string (and replace (car kill-ring)))) 9078 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
8651@end group 9079@end group
8652@end smallexample 9080@end smallexample
8653 9081
8654@noindent 9082@noindent
8655This is an expression whose first element is the function @code{and}. 9083It starts with an @code{if} expression
8656
8657@findex and, @r{introduced}
8658The @code{and} special form evaluates each of its arguments until one of
8659the arguments returns a value of @code{nil}, in which case the
8660@code{and} expression returns @code{nil}; however, if none of the
8661arguments returns a value of @code{nil}, the value resulting from
8662evaluating the last argument is returned. (Since such a value is not
8663@code{nil}, it is considered true in Emacs Lisp.) In other words, an
8664@code{and} expression returns a true value only if all its arguments
8665are true.
8666@findex and
8667 9084
8668In this case, the expression tests first to see whether 9085In this case, the expression tests first to see whether
8669@code{menu-bar-update-yank-menu} exists as a function, and if so, 9086@code{menu-bar-update-yank-menu} exists as a function, and if so,
@@ -8673,28 +9090,38 @@ symbol's function definition were void, we would receive an error
8673message, as we did when we created errors intentionally (@pxref{Making 9090message, as we did when we created errors intentionally (@pxref{Making
8674Errors, , Generate an Error Message}). 9091Errors, , Generate an Error Message}).
8675 9092
8676@need 1200 9093@noindent
8677Essentially, the @code{and} is an @code{if} expression that reads like 9094The then-part contains an expression whose first element is the
8678this: 9095function @code{and}.
8679 9096
8680@smallexample 9097@findex and
8681@group 9098The @code{and} special form evaluates each of its arguments until one
8682if @var{the-menu-bar-function-exists} 9099of the arguments returns a value of @code{nil}, in which case the
8683 then @var{execute-it} 9100@code{and} expression returns @code{nil}; however, if none of the
8684@end group 9101arguments returns a value of @code{nil}, the value resulting from
8685@end smallexample 9102evaluating the last argument is returned. (Since such a value is not
9103@code{nil}, it is considered true in Emacs Lisp.) In other words, an
9104@code{and} expression returns a true value only if all its arguments
9105are true. (@xref{Second Buffer Related Review}.)
9106
9107The expression determines whether the second argument to
9108@code{menu-bar-update-yank-menu} is true or not.
9109@ignore
9110 ;; If we're supposed to be extending an existing string, and that
9111 ;; string really is at the front of the menu, then update it in place.
9112@end ignore
8686 9113
8687@code{menu-bar-update-yank-menu} is one of the functions that make it 9114@code{menu-bar-update-yank-menu} is one of the functions that make it
8688possible to use the `Select and Paste' menu in the Edit item of a menu 9115possible to use the `Select and Paste' menu in the Edit item of a menu
8689bar; using a mouse, you can look at the various pieces of text you 9116bar; using a mouse, you can look at the various pieces of text you
8690have saved and select one piece to paste. 9117have saved and select one piece to paste.
8691 9118
8692Finally, the last expression in the @code{kill-new} function adds the 9119The last expression in the @code{kill-new} function adds the newly
8693newly copied string to whatever facility exists for copying and 9120copied string to whatever facility exists for copying and pasting
8694pasting among different programs running in a windowing system. In 9121among different programs running in a windowing system. In the X
8695the X Windowing system, for example, the @code{x-select-text} function 9122Windowing system, for example, the @code{x-select-text} function takes
8696takes the string and stores it in memory operated by X. You can paste 9123the string and stores it in memory operated by X. You can paste the
8697the string in another program, such as an Xterm. 9124string in another program, such as an Xterm.
8698 9125
8699@need 1200 9126@need 1200
8700The expression looks like this: 9127The expression looks like this:
@@ -8723,8 +9150,359 @@ commands. However, before discussing the yank commands, it is better
8723to learn how lists are implemented in a computer. This will make 9150to learn how lists are implemented in a computer. This will make
8724clear such mysteries as the use of the term `pointer'. 9151clear such mysteries as the use of the term `pointer'.
8725 9152
9153
9154@ignore
9155@c is this true in Emacs 22? Does not seems to be
9156
9157 (If the @w{@code{(< end beg))}}
9158expression is true, @code{kill-append} prepends the string to the just
9159previously clipped text. For a detailed discussion, see
9160@ref{kill-append function, , The @code{kill-append} function}.)
9161
9162If you then yank back the text, i.e., `paste' it, you get both
9163pieces of text at once. That way, if you delete two words in a row,
9164and then yank them back, you get both words, in their proper order,
9165with one yank. (The @w{@code{(< end beg))}} expression makes sure the
9166order is correct.)
9167
9168On the other hand, if the previous command is not @code{kill-region},
9169then the @code{kill-new} function is called, which adds the text to
9170the kill ring as the latest item, and sets the
9171@code{kill-ring-yank-pointer} variable to point to it.
9172@end ignore
9173@ignore
9174
9175@c Evidently, changed for Emacs 22. The zap-to-char command does not
9176@c use the delete-and-extract-region function
9177
91782006 Oct 26, the Digression into C is now OK but should come after
9179copy-region-as-kill and filter-buffer-substring
9180
91812006 Oct 24
9182In Emacs 22,
9183copy-region-as-kill is short, 12 lines, and uses
9184filter-buffer-substring, which is longer, 39 lines
9185and has delete-and-extract-region in it.
9186delete-and-extract-region is written in C.
9187
9188see Initializing a Variable with @code{defvar}
9189@end ignore
9190
9191@node Digression into C, defvar, copy-region-as-kill, Cutting & Storing Text
9192@comment node-name, next, previous, up
9193@section Digression into C
9194@findex delete-and-extract-region
9195@cindex C, a digression into
9196@cindex Digression into C
9197
9198The @code{copy-region-as-kill} function (@pxref{copy-region-as-kill, ,
9199@code{copy-region-as-kill}}) uses the @code{filter-buffer-substring}
9200function, which in turn uses the @code{delete-and-extract-region}
9201function. It removes the contents of a region and you cannot get them
9202back.
9203
9204Unlike the other code discussed here, the
9205@code{delete-and-extract-region} function is not written in Emacs
9206Lisp; it is written in C and is one of the primitives of the GNU Emacs
9207system. Since it is very simple, I will digress briefly from Lisp and
9208describe it here.
9209
9210@c GNU Emacs 22 in /usr/local/src/emacs/src/editfns.c
9211@c the DEFUN for buffer-substring-no-properties
9212
9213@need 1500
9214Like many of the other Emacs primitives,
9215@code{delete-and-extract-region} is written as an instance of a C
9216macro, a macro being a template for code. The complete macro looks
9217like this:
9218
9219@smallexample
9220@group
9221DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
9222 Sbuffer_substring_no_properties, 2, 2, 0,
9223 doc: /* Return the characters of part of the buffer,
9224without the text properties.
9225The two arguments START and END are character positions;
9226they can be in either order. */)
9227 (start, end)
9228 Lisp_Object start, end;
9229@{
9230 register int b, e;
9231
9232 validate_region (&start, &end);
9233 b = XINT (start);
9234 e = XINT (end);
9235
9236 return make_buffer_string (b, e, 0);
9237@}
9238@end group
9239@end smallexample
9240
9241Without going into the details of the macro writing process, let me
9242point out that this macro starts with the word @code{DEFUN}. The word
9243@code{DEFUN} was chosen since the code serves the same purpose as
9244@code{defun} does in Lisp. (The @code{DEFUN} C macro is defined in
9245@file{emacs/src/lisp.h}.)
9246
9247The word @code{DEFUN} is followed by seven parts inside of
9248parentheses:
9249
9250@itemize @bullet
9251@item
9252The first part is the name given to the function in Lisp,
9253@code{delete-and-extract-region}.
9254
9255@item
9256The second part is the name of the function in C,
9257@code{Fdelete_and_extract_region}. By convention, it starts with
9258@samp{F}. Since C does not use hyphens in names, underscores are used
9259instead.
9260
9261@item
9262The third part is the name for the C constant structure that records
9263information on this function for internal use. It is the name of the
9264function in C but begins with an @samp{S} instead of an @samp{F}.
9265
9266@item
9267The fourth and fifth parts specify the minimum and maximum number of
9268arguments the function can have. This function demands exactly 2
9269arguments.
9270
9271@item
9272The sixth part is nearly like the argument that follows the
9273@code{interactive} declaration in a function written in Lisp: a letter
9274followed, perhaps, by a prompt. The only difference from the Lisp is
9275when the macro is called with no arguments. Then you write a @code{0}
9276(which is a `null string'), as in this macro.
9277
9278If you were to specify arguments, you would place them between
9279quotation marks. The C macro for @code{goto-char} includes
9280@code{"NGoto char: "} in this position to indicate that the function
9281expects a raw prefix, in this case, a numerical location in a buffer,
9282and provides a prompt.
9283
9284@item
9285The seventh part is a documentation string, just like the one for a
9286function written in Emacs Lisp, except that every newline must be
9287written explicitly as @samp{\n} followed by a backslash and carriage
9288return.
9289
9290@need 1000
9291Thus, the first two lines of documentation for @code{goto-char} are
9292written like this:
9293
9294@smallexample
9295@group
9296 "Set point to POSITION, a number or marker.\n\
9297Beginning of buffer is position (point-min), end is (point-max).
9298@end group
9299@end smallexample
9300@end itemize
9301
9302@need 1200
9303In a C macro, the formal parameters come next, with a statement of
9304what kind of object they are, followed by what might be called the `body'
9305of the macro. For @code{delete-and-extract-region} the `body'
9306consists of the following four lines:
9307
9308@smallexample
9309@group
9310validate_region (&start, &end);
9311if (XINT (start) == XINT (end))
9312 return build_string ("");
9313return del_range_1 (XINT (start), XINT (end), 1, 1);
9314@end group
9315@end smallexample
9316
9317The @code{validate_region} function checks whether the values
9318passed as the beginning and end of the region are the proper type and
9319are within range. If the beginning and end positions are the same,
9320then return and empty string.
9321
9322The @code{del_range_1} function actually deletes the text. It is a
9323complex function we will not look into. It updates the buffer and
9324does other things. However, it is worth looking at the two arguments
9325passed to @code{del_range}. These are @w{@code{XINT (start)}} and
9326@w{@code{XINT (end)}}.
9327
9328As far as the C language is concerned, @code{start} and @code{end} are
9329two integers that mark the beginning and end of the region to be
9330deleted@footnote{More precisely, and requiring more expert knowledge
9331to understand, the two integers are of type `Lisp_Object', which can
9332also be a C union instead of an integer type.}.
9333
9334In early versions of Emacs, these two numbers were thirty-two bits
9335long, but the code is slowly being generalized to handle other
9336lengths. Three of the available bits are used to specify the type of
9337information; the remaining bits are used as `content'.
9338
9339@samp{XINT} is a C macro that extracts the relevant number from the
9340longer collection of bits; the three other bits are discarded.
9341
9342@need 800
9343The command in @code{delete-and-extract-region} looks like this:
9344
9345@smallexample
9346del_range_1 (XINT (start), XINT (end), 1, 1);
9347@end smallexample
9348
9349@noindent
9350It deletes the region between the beginning position, @code{start},
9351and the ending position, @code{end}.
9352
9353From the point of view of the person writing Lisp, Emacs is all very
9354simple; but hidden underneath is a great deal of complexity to make it
9355all work.
9356
9357@node defvar, cons & search-fwd Review, Digression into C, Cutting & Storing Text
9358@comment node-name, next, previous, up
9359@section Initializing a Variable with @code{defvar}
9360@findex defvar
9361@cindex Initializing a variable
9362@cindex Variable initialization
9363
9364@ignore
93652006 Oct 24
9366In Emacs 22,
9367copy-region-as-kill is short, 12 lines, and uses
9368filter-buffer-substring, which is longer, 39 lines
9369and has delete-and-extract-region in it.
9370delete-and-extract-region is written in C.
9371
9372see Initializing a Variable with @code{defvar}
9373
9374@end ignore
9375
9376The @code{copy-region-as-kill} function is written in Emacs Lisp. Two
9377functions within it, @code{kill-append} and @code{kill-new}, copy a
9378region in a buffer and save it in a variable called the
9379@code{kill-ring}. This section describes how the @code{kill-ring}
9380variable is created and initialized using the @code{defvar} special
9381form.
9382
9383(Again we note that the term @code{kill-ring} is a misnomer. The text
9384that is clipped out of the buffer can be brought back; it is not a ring
9385of corpses, but a ring of resurrectable text.)
9386
9387In Emacs Lisp, a variable such as the @code{kill-ring} is created and
9388given an initial value by using the @code{defvar} special form. The
9389name comes from ``define variable''.
9390
9391The @code{defvar} special form is similar to @code{setq} in that it sets
9392the value of a variable. It is unlike @code{setq} in two ways: first,
9393it only sets the value of the variable if the variable does not already
9394have a value. If the variable already has a value, @code{defvar} does
9395not override the existing value. Second, @code{defvar} has a
9396documentation string.
9397
9398(Another special form, @code{defcustom}, is designed for variables
9399that people customize. It has more features than @code{defvar}.
9400(@xref{defcustom, , Setting Variables with @code{defcustom}}.)
9401
9402@menu
9403* See variable current value::
9404* defvar and asterisk::
9405@end menu
9406
9407@node See variable current value, defvar and asterisk, defvar, defvar
9408@ifnottex
9409@unnumberedsubsec Seeing the Current Value of a Variable
9410@end ifnottex
9411
9412You can see the current value of a variable, any variable, by using
9413the @code{describe-variable} function, which is usually invoked by
9414typing @kbd{C-h v}. If you type @kbd{C-h v} and then @code{kill-ring}
9415(followed by @key{RET}) when prompted, you will see what is in your
9416current kill ring---this may be quite a lot! Conversely, if you have
9417been doing nothing this Emacs session except read this document, you
9418may have nothing in it. Also, you will see the documentation for
9419@code{kill-ring}:
9420
9421@smallexample
9422@group
9423Documentation:
9424List of killed text sequences.
9425Since the kill ring is supposed to interact nicely with cut-and-paste
9426facilities offered by window systems, use of this variable should
9427@end group
9428@group
9429interact nicely with `interprogram-cut-function' and
9430`interprogram-paste-function'. The functions `kill-new',
9431`kill-append', and `current-kill' are supposed to implement this
9432interaction; you may want to use them instead of manipulating the kill
9433ring directly.
9434@end group
9435@end smallexample
9436
9437@need 800
9438The kill ring is defined by a @code{defvar} in the following way:
9439
9440@smallexample
9441@group
9442(defvar kill-ring nil
9443 "List of killed text sequences.
9444@dots{}")
9445@end group
9446@end smallexample
9447
9448@noindent
9449In this variable definition, the variable is given an initial value of
9450@code{nil}, which makes sense, since if you have saved nothing, you want
9451nothing back if you give a @code{yank} command. The documentation
9452string is written just like the documentation string of a @code{defun}.
9453As with the documentation string of the @code{defun}, the first line of
9454the documentation should be a complete sentence, since some commands,
9455like @code{apropos}, print only the first line of documentation.
9456Succeeding lines should not be indented; otherwise they look odd when
9457you use @kbd{C-h v} (@code{describe-variable}).
9458
9459@node defvar and asterisk, , See variable current value, defvar
9460@subsection @code{defvar} and an asterisk
9461@findex defvar @r{for a user customizable variable}
9462@findex defvar @r{with an asterisk}
9463
9464In the past, Emacs used the @code{defvar} special form both for
9465internal variables that you would not expect a user to change and for
9466variables that you do expect a user to change. Although you can still
9467use @code{defvar} for user customizable variables, please use
9468@code{defcustom} instead, since that special form provides a path into
9469the Customization commands. (@xref{defcustom, , Specifying Variables
9470using @code{defcustom}}.)
9471
9472When you specified a variable using the @code{defvar} special form,
9473you could distinguish a readily settable variable from others by
9474typing an asterisk, @samp{*}, in the first column of its documentation
9475string. For example:
9476
9477@smallexample
9478@group
9479(defvar shell-command-default-error-buffer nil
9480 "*Buffer name for `shell-command' @dots{} error output.
9481@dots{} ")
9482@end group
9483@end smallexample
9484
9485@findex set-variable
9486@noindent
9487You could (and still can) use the @code{set-variable} command to
9488change the value of @code{shell-command-default-error-buffer}
9489temporarily. However, options set using @code{set-variable} are set
9490only for the duration of your editing session. The new values are not
9491saved between sessions. Each time Emacs starts, it reads the original
9492value, unless you change the value within your @file{.emacs} file,
9493either by setting it manually or by using @code{customize}.
9494@xref{Emacs Initialization, , Your @file{.emacs} File}.
9495
9496For me, the major use of the @code{set-variable} command is to suggest
9497variables that I might want to set in my @file{.emacs} file. There
9498are now more than 700 such variables --- far too many to remember
9499readily. Fortunately, you can press @key{TAB} after calling the
9500@code{M-x set-variable} command to see the list of variables.
9501(@xref{Examining, , Examining and Setting Variables, emacs,
9502The GNU Emacs Manual}.)
9503
8726@need 1250 9504@need 1250
8727@node cons & search-fwd Review, search Exercises, copy-region-as-kill, Cutting & Storing Text 9505@node cons & search-fwd Review, search Exercises, defvar, Cutting & Storing Text
8728@comment node-name, next, previous, up 9506@comment node-name, next, previous, up
8729@section Review 9507@section Review
8730 9508
@@ -8856,14 +9634,15 @@ search goes backwards.
8856buffer and stores that text in the kill ring, so you can get it back 9634buffer and stores that text in the kill ring, so you can get it back
8857by yanking. 9635by yanking.
8858 9636
8859@code{delete-and-extract-region} removes the text between point and
8860mark from the buffer and throws it away. You cannot get it back.
8861
8862@code{copy-region-as-kill} copies the text between point and mark into 9637@code{copy-region-as-kill} copies the text between point and mark into
8863the kill ring, from which you can get it by yanking. The function 9638the kill ring, from which you can get it by yanking. The function
8864does not cut or remove the text from the buffer. 9639does not cut or remove the text from the buffer.
8865@end table 9640@end table
8866 9641
9642@code{delete-and-extract-region} removes the text between point and
9643mark from the buffer and throws it away. You cannot get it back.
9644(This is not an interactive command.)
9645
8867@need 1500 9646@need 1500
8868@node search Exercises, , cons & search-fwd Review, Cutting & Storing Text 9647@node search Exercises, , cons & search-fwd Review, Cutting & Storing Text
8869@section Searching Exercises 9648@section Searching Exercises
@@ -8904,7 +9683,7 @@ pointed to. Hence, a list is kept as a series of electronic addresses.
8904 9683
8905@menu 9684@menu
8906* Lists diagrammed:: 9685* Lists diagrammed::
8907* Symbols as Chest:: Exploring a powerful metaphor. 9686* Symbols as Chest::
8908* List Exercise:: 9687* List Exercise::
8909@end menu 9688@end menu
8910 9689
@@ -9385,8 +10164,8 @@ that holds the text is a list.
9385list is handled as a ring.) 10164list is handled as a ring.)
9386 10165
9387@menu 10166@menu
9388* Kill Ring Overview:: The kill ring is a list. 10167* Kill Ring Overview::
9389* kill-ring-yank-pointer:: The @code{kill-ring-yank-pointer} variable. 10168* kill-ring-yank-pointer::
9390* yank nthcdr Exercises:: 10169* yank nthcdr Exercises::
9391@end menu 10170@end menu
9392 10171
@@ -9422,6 +10201,15 @@ These functions refer to the kill ring through a variable called the
9422(insert (car kill-ring-yank-pointer)) 10201(insert (car kill-ring-yank-pointer))
9423@end smallexample 10202@end smallexample
9424 10203
10204@noindent
10205(Well, no more. In GNU Emacs 22, the function has been replaced by
10206@code{insert-for-yank} which calls @code{insert-for-yank-1}
10207repetitively for each @code{yank-handler} segment. In turn,
10208@code{insert-for-yank-1} strips text properties from the inserted text
10209according to @code{yank-excluded-properties}. Otherwise, it is just
10210like @code{insert}. We will stick with plain @code{insert} since it
10211is easier to understand.)
10212
9425To begin to understand how @code{yank} and @code{yank-pop} work, it is 10213To begin to understand how @code{yank} and @code{yank-pop} work, it is
9426first necessary to look at the @code{kill-ring-yank-pointer} variable 10214first necessary to look at the @code{kill-ring-yank-pointer} variable
9427and the @code{rotate-yank-pointer} function. 10215and the @code{rotate-yank-pointer} function.
@@ -9525,15 +10313,46 @@ on the other hand, serves to indicate---that is, to `point to'---that part
9525of the kill ring of which the first element (the @sc{car}) will be 10313of the kill ring of which the first element (the @sc{car}) will be
9526inserted. 10314inserted.
9527 10315
9528The @code{rotate-yank-pointer} function changes the element in the 10316
9529kill ring to which the @code{kill-ring-yank-pointer} points; when the 10317@ignore
9530pointer is set to point to the next element beyond the end of the kill 10318In GNU Emacs 22, the @code{kill-new} function calls
9531ring, it automatically sets it to point to the first element of the 10319
9532kill ring. This is how the list is transformed into a ring. The 10320@code{(setq kill-ring-yank-pointer kill-ring)}
9533@code{rotate-yank-pointer} function itself is not difficult, but 10321
9534contains many details. It and the much simpler @code{yank} and 10322(defun rotate-yank-pointer (arg)
9535@code{yank-pop} functions are described in an appendix. 10323 "Rotate the yanking point in the kill ring.
9536@xref{Kill Ring, , Handling the Kill Ring}. 10324With argument, rotate that many kills forward (or backward, if negative)."
10325 (interactive "p")
10326 (current-kill arg))
10327
10328(defun current-kill (n &optional do-not-move)
10329 "Rotate the yanking point by N places, and then return that kill.
10330If N is zero, `interprogram-paste-function' is set, and calling it
10331returns a string, then that string is added to the front of the
10332kill ring and returned as the latest kill.
10333If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
10334yanking point; just return the Nth kill forward."
10335 (let ((interprogram-paste (and (= n 0)
10336 interprogram-paste-function
10337 (funcall interprogram-paste-function))))
10338 (if interprogram-paste
10339 (progn
10340 ;; Disable the interprogram cut function when we add the new
10341 ;; text to the kill ring, so Emacs doesn't try to own the
10342 ;; selection, with identical text.
10343 (let ((interprogram-cut-function nil))
10344 (kill-new interprogram-paste))
10345 interprogram-paste)
10346 (or kill-ring (error "Kill ring is empty"))
10347 (let ((ARGth-kill-element
10348 (nthcdr (mod (- n (length kill-ring-yank-pointer))
10349 (length kill-ring))
10350 kill-ring)))
10351 (or do-not-move
10352 (setq kill-ring-yank-pointer ARGth-kill-element))
10353 (car ARGth-kill-element)))))
10354
10355@end ignore
9537 10356
9538@need 1500 10357@need 1500
9539@node yank nthcdr Exercises, , kill-ring-yank-pointer, Yanking 10358@node yank nthcdr Exercises, , kill-ring-yank-pointer, Yanking
@@ -9584,9 +10403,9 @@ increase the values of @code{max-specpdl-size} and
958415 and 30 times their default value.}. 1040315 and 30 times their default value.}.
9585 10404
9586@menu 10405@menu
9587* while:: Causing a stretch of code to repeat. 10406* while::
9588* dolist dotimes:: 10407* dolist dotimes::
9589* Recursion:: Causing a function to call itself. 10408* Recursion::
9590* Looping exercise:: 10409* Looping exercise::
9591@end menu 10410@end menu
9592 10411
@@ -9621,11 +10440,11 @@ The template for a @code{while} expression looks like this:
9621@end smallexample 10440@end smallexample
9622 10441
9623@menu 10442@menu
9624* Looping with while:: Repeat so long as test returns true. 10443* Looping with while::
9625* Loop Example:: A @code{while} loop that uses a list. 10444* Loop Example::
9626* print-elements-of-list:: Uses @code{while}, @code{car}, @code{cdr}. 10445* print-elements-of-list::
9627* Incrementing Loop:: A loop with an incrementing counter. 10446* Incrementing Loop::
9628* Decrementing Loop:: A loop with a decrementing counter. 10447* Decrementing Loop::
9629@end menu 10448@end menu
9630 10449
9631@node Looping with while, Loop Example, while, while 10450@node Looping with while, Loop Example, while, while
@@ -9776,8 +10595,9 @@ loop with a list.
9776 10595
9777@cindex @file{*scratch*} buffer 10596@cindex @file{*scratch*} buffer
9778The function requires several lines for its output. If you are 10597The function requires several lines for its output. If you are
9779reading this in Emacs 21 or a later version, you can evaluate the 10598reading this in a recent instance of GNU Emacs,
9780following expression inside of Info, as usual. 10599@c GNU Emacs 21, GNU Emacs 22, or a later version,
10600you can evaluate the following expression inside of Info, as usual.
9781 10601
9782If you are using an earlier version of Emacs, you need to copy the 10602If you are using an earlier version of Emacs, you need to copy the
9783necessary expressions to your @file{*scratch*} buffer and evaluate 10603necessary expressions to your @file{*scratch*} buffer and evaluate
@@ -9787,8 +10607,10 @@ earlier versions.
9787You can copy the expressions by marking the beginning of the region 10607You can copy the expressions by marking the beginning of the region
9788with @kbd{C-@key{SPC}} (@code{set-mark-command}), moving the cursor to 10608with @kbd{C-@key{SPC}} (@code{set-mark-command}), moving the cursor to
9789the end of the region and then copying the region using @kbd{M-w} 10609the end of the region and then copying the region using @kbd{M-w}
9790(@code{copy-region-as-kill}). In the @file{*scratch*} buffer, you can 10610(@code{kill-ring-save}, which calls @code{copy-region-as-kill} and
9791yank the expressions back by typing @kbd{C-y} (@code{yank}). 10611then provides visual feedback). In the @file{*scratch*}
10612buffer, you can yank the expressions back by typing @kbd{C-y}
10613(@code{yank}).
9792 10614
9793After you have copied the expressions to the @file{*scratch*} buffer, 10615After you have copied the expressions to the @file{*scratch*} buffer,
9794evaluate each expression in turn. Be sure to evaluate the last 10616evaluate each expression in turn. Be sure to evaluate the last
@@ -9801,7 +10623,7 @@ echo area: @code{^Jgazelle^J^Jgiraffe^J^Jlion^J^Jtiger^Jnil}, in which
9801each @samp{^J} stands for a `newline'.) 10623each @samp{^J} stands for a `newline'.)
9802 10624
9803@need 1500 10625@need 1500
9804If you are using Emacs 21 or later, you can evaluate these expressions 10626In a recent instance of GNU Emacs, you can evaluate these expressions
9805directly in the Info buffer, and the echo area will grow to show the 10627directly in the Info buffer, and the echo area will grow to show the
9806results. 10628results.
9807 10629
@@ -9882,9 +10704,9 @@ Note that you need to set the initial value of @code{count}; usually it
9882is set to 1. 10704is set to 1.
9883 10705
9884@menu 10706@menu
9885* Incrementing Example:: Counting pebbles in a triangle. 10707* Incrementing Example::
9886* Inc Example parts:: The parts of the function definition. 10708* Inc Example parts::
9887* Inc Example altogether:: Putting the function definition together. 10709* Inc Example altogether::
9888@end menu 10710@end menu
9889 10711
9890@node Incrementing Example, Inc Example parts, Incrementing Loop, Incrementing Loop 10712@node Incrementing Example, Inc Example parts, Incrementing Loop, Incrementing Loop
@@ -9967,9 +10789,9 @@ argument will be the total number of rows in the triangle. It can be
9967called @code{number-of-rows}. 10789called @code{number-of-rows}.
9968 10790
9969Finally, we need a variable to use as a counter. We could call this 10791Finally, we need a variable to use as a counter. We could call this
9970variable @code{counter}, but a better name is @code{row-number}. 10792variable @code{counter}, but a better name is @code{row-number}. That
9971That is because what the counter does is count rows, and a program 10793is because what the counter does in this function is count rows, and a
9972should be written to be as understandable as possible. 10794program should be written to be as understandable as possible.
9973 10795
9974When the Lisp interpreter first starts evaluating the expressions in the 10796When the Lisp interpreter first starts evaluating the expressions in the
9975function, the value of @code{total} should be set to zero, since we have 10797function, the value of @code{total} should be set to zero, since we have
@@ -10092,7 +10914,7 @@ In outline, the function will look like this:
10092 (let (@var{varlist}) 10914 (let (@var{varlist})
10093 (while (@var{true-or-false-test}) 10915 (while (@var{true-or-false-test})
10094 @var{body-of-while}@dots{} ) 10916 @var{body-of-while}@dots{} )
10095 @dots{} ) ; @r{Need final expression here.} 10917 @dots{} )) ; @r{Need final expression here.}
10096@end group 10918@end group
10097@end smallexample 10919@end smallexample
10098 10920
@@ -10190,9 +11012,9 @@ The template for a decrementing @code{while} loop looks like this:
10190@end smallexample 11012@end smallexample
10191 11013
10192@menu 11014@menu
10193* Decrementing Example:: More pebbles on the beach. 11015* Decrementing Example::
10194* Dec Example parts:: The parts of the function definition. 11016* Dec Example parts::
10195* Dec Example altogether:: Putting the function definition together. 11017* Dec Example altogether::
10196@end menu 11018@end menu
10197 11019
10198@node Decrementing Example, Dec Example parts, Decrementing Loop, Decrementing Loop 11020@node Decrementing Example, Dec Example parts, Decrementing Loop, Decrementing Loop
@@ -10547,13 +11369,13 @@ different arguments' will become sufficiently different from the first
10547arguments that the final instance will stop. 11369arguments that the final instance will stop.
10548 11370
10549@menu 11371@menu
10550* Building Robots:: Same model, different serial number ... 11372* Building Robots::
10551* Recursive Definition Parts:: Walk until you stop ... 11373* Recursive Definition Parts::
10552* Recursion with list:: Using a list as the test whether to recurse. 11374* Recursion with list::
10553* Recursive triangle function:: 11375* Recursive triangle function::
10554* Recursion with cond:: 11376* Recursion with cond::
10555* Recursive Patterns:: Often used templates. 11377* Recursive Patterns::
10556* No Deferment:: Don't store up work ... 11378* No Deferment::
10557* No deferment solution:: 11379* No deferment solution::
10558@end menu 11380@end menu
10559 11381
@@ -10668,8 +11490,8 @@ The example of a @code{while} loop that printed the elements of a list
10668of numbers can be written recursively. Here is the code, including 11490of numbers can be written recursively. Here is the code, including
10669an expression to set the value of the variable @code{animals} to a list. 11491an expression to set the value of the variable @code{animals} to a list.
10670 11492
10671If you are using Emacs 20 or before, this example must be copied to 11493If you are using GNU Emacs 20 or before, this example must be copied
10672the @file{*scratch*} buffer and each expression must be evaluated 11494to the @file{*scratch*} buffer and each expression must be evaluated
10673there. Use @kbd{C-u C-x C-e} to evaluate the 11495there. Use @kbd{C-u C-x C-e} to evaluate the
10674@code{(print-elements-recursively animals)} expression so that the 11496@code{(print-elements-recursively animals)} expression so that the
10675results are printed in the buffer; otherwise the Lisp interpreter will 11497results are printed in the buffer; otherwise the Lisp interpreter will
@@ -10679,8 +11501,8 @@ Also, place your cursor immediately after the last closing parenthesis
10679of the @code{print-elements-recursively} function, before the comment. 11501of the @code{print-elements-recursively} function, before the comment.
10680Otherwise, the Lisp interpreter will try to evaluate the comment. 11502Otherwise, the Lisp interpreter will try to evaluate the comment.
10681 11503
10682If you are using Emacs 21 or later, you can evaluate this expression 11504If you are using a more recent version, you can evaluate this
10683directly in Info. 11505expression directly in Info.
10684 11506
10685@findex print-elements-recursively 11507@findex print-elements-recursively
10686@smallexample 11508@smallexample
@@ -11432,7 +12254,7 @@ test whether @code{(> counter number)}:
11432@need 1200 12254@need 1200
11433@noindent 12255@noindent
11434and find that the result is false, so it will invoke 12256and find that the result is false, so it will invoke
11435the then-part of the @code{if} clause: 12257the else-part of the @code{if} clause:
11436 12258
11437@smallexample 12259@smallexample
11438@group 12260@group
@@ -11548,7 +12370,7 @@ For more information, see
11548@ref{Indicating, , Indicating, texinfo, Texinfo Manual}, which goes to 12370@ref{Indicating, , Indicating, texinfo, Texinfo Manual}, which goes to
11549a Texinfo manual in the current directory. Or, if you are on the 12371a Texinfo manual in the current directory. Or, if you are on the
11550Internet, see 12372Internet, see
11551@uref{http://www.gnu.org/manual/texinfo-4.6/html_node/Indicating.html} 12373@uref{http://www.gnu.org/software/texinfo/manual/texinfo/}
11552@end ifhtml 12374@end ifhtml
11553@iftex 12375@iftex
11554``Indicating Definitions, Commands, etc.'' in @cite{Texinfo, The GNU 12376``Indicating Definitions, Commands, etc.'' in @cite{Texinfo, The GNU
@@ -11594,11 +12416,11 @@ this chapter. @code{forward-paragraph} is a complex function that
11594introduces several new features. 12416introduces several new features.
11595 12417
11596@menu 12418@menu
11597* sentence-end:: The regular expression for @code{sentence-end}. 12419* sentence-end::
11598* re-search-forward:: Very similar to @code{search-forward}. 12420* re-search-forward::
11599* forward-sentence:: A straightforward example of regexp search. 12421* forward-sentence::
11600* forward-paragraph:: A somewhat complex example. 12422* forward-paragraph::
11601* etags:: How to create your own @file{TAGS} table. 12423* etags::
11602* Regexp Review:: 12424* Regexp Review::
11603* re-search Exercises:: 12425* re-search Exercises::
11604@end menu 12426@end menu
@@ -11697,8 +12519,20 @@ sentence-end
11697@end group 12519@end group
11698@end smallexample 12520@end smallexample
11699 12521
11700@ignore 12522@noindent
12523(Well, not in GNU Emacs 22; that is because of an effort to make the
12524process simpler. When its value is @code{nil}, then use the value
12525defined by the function @code{sentence-end}, and that returns a value
12526constructed from the variables @code{sentence-end-base},
12527@code{sentence-end-double-space}, @code{sentence-end-without-period},
12528and @code{sentence-end-without-space}. The critical variable is
12529@code{sentence-end-base}; its global value is similar to the one
12530described above but it also contains two additional quotation marks.
12531These have differing degrees of curliness. The
12532@code{sentence-end-without-period} variable, when true, tells Emacs
12533that a sentence may end without a period, such as text in Thai.)
11701 12534
12535@ignore
11702@noindent 12536@noindent
11703(Note that here the @key{TAB}, two spaces, and @key{RET} are shown 12537(Note that here the @key{TAB}, two spaces, and @key{RET} are shown
11704literally in the pattern.) 12538literally in the pattern.)
@@ -11739,7 +12573,6 @@ mark may, but need not, be followed by one or more carriage returns. In
11739the pattern, the carriage return is inserted as an actual carriage 12573the pattern, the carriage return is inserted as an actual carriage
11740return between square brackets but here it is shown as @key{RET}. 12574return between square brackets but here it is shown as @key{RET}.
11741@end table 12575@end table
11742
11743@end ignore 12576@end ignore
11744 12577
11745@node re-search-forward, forward-sentence, sentence-end, Regexp Search 12578@node re-search-forward, forward-sentence, sentence-end, Regexp Search
@@ -11802,7 +12635,7 @@ to.
11802 12635
11803@need 1200 12636@need 1200
11804In the @code{forward-sentence} function, the regular expression will be 12637In the @code{forward-sentence} function, the regular expression will be
11805the value of the variable @code{sentence-end}, namely: 12638the value of the variable @code{sentence-end}. In simple form, that is:
11806 12639
11807@smallexample 12640@smallexample
11808@group 12641@group
@@ -11831,8 +12664,8 @@ bound to the key command @kbd{M-e}.
11831 12664
11832@menu 12665@menu
11833* Complete forward-sentence:: 12666* Complete forward-sentence::
11834* fwd-sentence while loops:: Two @code{while} loops. 12667* fwd-sentence while loops::
11835* fwd-sentence re-search:: A regular expression search. 12668* fwd-sentence re-search::
11836@end menu 12669@end menu
11837 12670
11838@node Complete forward-sentence, fwd-sentence while loops, forward-sentence, forward-sentence 12671@node Complete forward-sentence, fwd-sentence while loops, forward-sentence, forward-sentence
@@ -11843,6 +12676,44 @@ bound to the key command @kbd{M-e}.
11843@need 1250 12676@need 1250
11844Here is the code for @code{forward-sentence}: 12677Here is the code for @code{forward-sentence}:
11845 12678
12679@c in GNU Emacs 22
12680@smallexample
12681@group
12682(defun forward-sentence (&optional arg)
12683 "Move forward to next `sentence-end'. With argument, repeat.
12684With negative argument, move backward repeatedly to `sentence-beginning'.
12685
12686The variable `sentence-end' is a regular expression that matches ends of
12687sentences. Also, every paragraph boundary terminates sentences as well."
12688@end group
12689@group
12690 (interactive "p")
12691 (or arg (setq arg 1))
12692 (let ((opoint (point))
12693 (sentence-end (sentence-end)))
12694 (while (< arg 0)
12695 (let ((pos (point))
12696 (par-beg (save-excursion (start-of-paragraph-text) (point))))
12697 (if (and (re-search-backward sentence-end par-beg t)
12698 (or (< (match-end 0) pos)
12699 (re-search-backward sentence-end par-beg t)))
12700 (goto-char (match-end 0))
12701 (goto-char par-beg)))
12702 (setq arg (1+ arg)))
12703@end group
12704@group
12705 (while (> arg 0)
12706 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
12707 (if (re-search-forward sentence-end par-end t)
12708 (skip-chars-backward " \t\n")
12709 (goto-char par-end)))
12710 (setq arg (1- arg)))
12711 (constrain-to-field nil opoint t)))
12712@end group
12713@end smallexample
12714
12715@ignore
12716GNU Emacs 21
11846@smallexample 12717@smallexample
11847@group 12718@group
11848(defun forward-sentence (&optional arg) 12719(defun forward-sentence (&optional arg)
@@ -11872,6 +12743,7 @@ terminates sentences as well."
11872 (setq arg (1- arg)))) 12743 (setq arg (1- arg))))
11873@end group 12744@end group
11874@end smallexample 12745@end smallexample
12746@end ignore
11875 12747
11876The function looks long at first sight and it is best to look at its 12748The function looks long at first sight and it is best to look at its
11877skeleton first, and then its muscle. The way to see the skeleton is to 12749skeleton first, and then its muscle. The way to see the skeleton is to
@@ -11883,16 +12755,21 @@ look at the expressions that start in the left-most columns:
11883 "@var{documentation}@dots{}" 12755 "@var{documentation}@dots{}"
11884 (interactive "p") 12756 (interactive "p")
11885 (or arg (setq arg 1)) 12757 (or arg (setq arg 1))
11886 (while (< arg 0) 12758 (let ((opoint (point)) (sentence-end (sentence-end)))
11887 @var{body-of-while-loop} 12759 (while (< arg 0)
11888 (while (> arg 0) 12760 (let ((pos (point))
11889 @var{body-of-while-loop} 12761 (par-beg (save-excursion (start-of-paragraph-text) (point))))
12762 @var{rest-of-body-of-while-loop-when-going-backwards}
12763 (while (> arg 0)
12764 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
12765 @var{rest-of-body-of-while-loop-when-going-forwards}
12766 @var{handle-forms-and-equivalent}
11890@end group 12767@end group
11891@end smallexample 12768@end smallexample
11892 12769
11893This looks much simpler! The function definition consists of 12770This looks much simpler! The function definition consists of
11894documentation, an @code{interactive} expression, an @code{or} 12771documentation, an @code{interactive} expression, an @code{or}
11895expression, and @code{while} loops. 12772expression, a @code{let} expression, and @code{while} loops.
11896 12773
11897Let's look at each of these parts in turn. 12774Let's look at each of these parts in turn.
11898 12775
@@ -11902,24 +12779,31 @@ The function has an @code{interactive "p"} declaration. This means
11902that the processed prefix argument, if any, is passed to the 12779that the processed prefix argument, if any, is passed to the
11903function as its argument. (This will be a number.) If the function 12780function as its argument. (This will be a number.) If the function
11904is not passed an argument (it is optional) then the argument 12781is not passed an argument (it is optional) then the argument
11905@code{arg} will be bound to 1. When @code{forward-sentence} is called 12782@code{arg} will be bound to 1.
11906non-interactively without an argument, @code{arg} is bound to 12783
12784When @code{forward-sentence} is called non-interactively without an
12785argument, @code{arg} is bound to @code{nil}. The @code{or} expression
12786handles this. What it does is either leave the value of @code{arg} as
12787it is, but only if @code{arg} is bound to a value; or it sets the
12788value of @code{arg} to 1, in the case when @code{arg} is bound to
11907@code{nil}. 12789@code{nil}.
11908 12790
11909The @code{or} expression handles the prefix argument. What it does is 12791Next is a @code{let}. That specifies the values of two local
11910either leave the value of @code{arg} as it is, but only if @code{arg} 12792variables, @code{point} and @code{sentence-end}. The local value of
11911is bound to a value; or it sets the value of @code{arg} to 1, in the 12793point, from before the search, is used in the
11912case when @code{arg} is bound to @code{nil}. 12794@code{constrain-to-field} function which handles forms and
12795equivalents. The @code{sentence-end} variable is set by the
12796@code{sentence-end} function.
11913 12797
11914@node fwd-sentence while loops, fwd-sentence re-search, Complete forward-sentence, forward-sentence 12798@node fwd-sentence while loops, fwd-sentence re-search, Complete forward-sentence, forward-sentence
11915@unnumberedsubsec The @code{while} loops 12799@unnumberedsubsec The @code{while} loops
11916 12800
11917Two @code{while} loops follow the @code{or} expression. The first 12801Two @code{while} loops follow. The first @code{while} has a
11918@code{while} has a true-or-false-test that tests true if the prefix 12802true-or-false-test that tests true if the prefix argument for
11919argument for @code{forward-sentence} is a negative number. This is for 12803@code{forward-sentence} is a negative number. This is for going
11920going backwards. The body of this loop is similar to the body of the 12804backwards. The body of this loop is similar to the body of the second
11921second @code{while} clause, but it is not exactly the same. We will 12805@code{while} clause, but it is not exactly the same. We will skip
11922skip this @code{while} loop and concentrate on the second @code{while} 12806this @code{while} loop and concentrate on the second @code{while}
11923loop. 12807loop.
11924 12808
11925@need 1500 12809@need 1500
@@ -12052,6 +12936,9 @@ false. The false then causes the @code{if} to evaluate its third
12052argument, which is @code{(goto-char par-end)}: it moves point to the 12936argument, which is @code{(goto-char par-end)}: it moves point to the
12053end of the paragraph. 12937end of the paragraph.
12054 12938
12939(And if the text is in a form or equivalent, and point may not move
12940fully, then the @code{constrain-to-field} function comes into play.)
12941
12055Regular expression searches are exceptionally useful and the pattern 12942Regular expression searches are exceptionally useful and the pattern
12056illustrated by @code{re-search-forward}, in which the search is the 12943illustrated by @code{re-search-forward}, in which the search is the
12057test of an @code{if} expression, is handy. You will see or write code 12944test of an @code{if} expression, is handy. You will see or write code
@@ -12062,6 +12949,151 @@ incorporating this pattern often.
12062@section @code{forward-paragraph}: a Goldmine of Functions 12949@section @code{forward-paragraph}: a Goldmine of Functions
12063@findex forward-paragraph 12950@findex forward-paragraph
12064 12951
12952@ignore
12953@c in GNU Emacs 22
12954(defun forward-paragraph (&optional arg)
12955 "Move forward to end of paragraph.
12956With argument ARG, do it ARG times;
12957a negative argument ARG = -N means move backward N paragraphs.
12958
12959A line which `paragraph-start' matches either separates paragraphs
12960\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
12961A paragraph end is the beginning of a line which is not part of the paragraph
12962to which the end of the previous line belongs, or the end of the buffer.
12963Returns the count of paragraphs left to move."
12964 (interactive "p")
12965 (or arg (setq arg 1))
12966 (let* ((opoint (point))
12967 (fill-prefix-regexp
12968 (and fill-prefix (not (equal fill-prefix ""))
12969 (not paragraph-ignore-fill-prefix)
12970 (regexp-quote fill-prefix)))
12971 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
12972 ;; These regexps shouldn't be anchored, because we look for them
12973 ;; starting at the left-margin. This allows paragraph commands to
12974 ;; work normally with indented text.
12975 ;; This hack will not find problem cases like "whatever\\|^something".
12976 (parstart (if (and (not (equal "" paragraph-start))
12977 (equal ?^ (aref paragraph-start 0)))
12978 (substring paragraph-start 1)
12979 paragraph-start))
12980 (parsep (if (and (not (equal "" paragraph-separate))
12981 (equal ?^ (aref paragraph-separate 0)))
12982 (substring paragraph-separate 1)
12983 paragraph-separate))
12984 (parsep
12985 (if fill-prefix-regexp
12986 (concat parsep "\\|"
12987 fill-prefix-regexp "[ \t]*$")
12988 parsep))
12989 ;; This is used for searching.
12990 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
12991 start found-start)
12992 (while (and (< arg 0) (not (bobp)))
12993 (if (and (not (looking-at parsep))
12994 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
12995 (looking-at parsep))
12996 (setq arg (1+ arg))
12997 (setq start (point))
12998 ;; Move back over paragraph-separating lines.
12999 (forward-char -1) (beginning-of-line)
13000 (while (and (not (bobp))
13001 (progn (move-to-left-margin)
13002 (looking-at parsep)))
13003 (forward-line -1))
13004 (if (bobp)
13005 nil
13006 (setq arg (1+ arg))
13007 ;; Go to end of the previous (non-separating) line.
13008 (end-of-line)
13009 ;; Search back for line that starts or separates paragraphs.
13010 (if (if fill-prefix-regexp
13011 ;; There is a fill prefix; it overrides parstart.
13012 (let (multiple-lines)
13013 (while (and (progn (beginning-of-line) (not (bobp)))
13014 (progn (move-to-left-margin)
13015 (not (looking-at parsep)))
13016 (looking-at fill-prefix-regexp))
13017 (unless (= (point) start)
13018 (setq multiple-lines t))
13019 (forward-line -1))
13020 (move-to-left-margin)
13021 ;; This deleted code caused a long hanging-indent line
13022 ;; not to be filled together with the following lines.
13023 ;; ;; Don't move back over a line before the paragraph
13024 ;; ;; which doesn't start with fill-prefix
13025 ;; ;; unless that is the only line we've moved over.
13026 ;; (and (not (looking-at fill-prefix-regexp))
13027 ;; multiple-lines
13028 ;; (forward-line 1))
13029 (not (bobp)))
13030 (while (and (re-search-backward sp-parstart nil 1)
13031 (setq found-start t)
13032 ;; Found a candidate, but need to check if it is a
13033 ;; REAL parstart.
13034 (progn (setq start (point))
13035 (move-to-left-margin)
13036 (not (looking-at parsep)))
13037 (not (and (looking-at parstart)
13038 (or (not use-hard-newlines)
13039 (bobp)
13040 (get-text-property
13041 (1- start) 'hard)))))
13042 (setq found-start nil)
13043 (goto-char start))
13044 found-start)
13045 ;; Found one.
13046 (progn
13047 ;; Move forward over paragraph separators.
13048 ;; We know this cannot reach the place we started
13049 ;; because we know we moved back over a non-separator.
13050 (while (and (not (eobp))
13051 (progn (move-to-left-margin)
13052 (looking-at parsep)))
13053 (forward-line 1))
13054 ;; If line before paragraph is just margin, back up to there.
13055 (end-of-line 0)
13056 (if (> (current-column) (current-left-margin))
13057 (forward-char 1)
13058 (skip-chars-backward " \t")
13059 (if (not (bolp))
13060 (forward-line 1))))
13061 ;; No starter or separator line => use buffer beg.
13062 (goto-char (point-min))))))
13063
13064 (while (and (> arg 0) (not (eobp)))
13065 ;; Move forward over separator lines...
13066 (while (and (not (eobp))
13067 (progn (move-to-left-margin) (not (eobp)))
13068 (looking-at parsep))
13069 (forward-line 1))
13070 (unless (eobp) (setq arg (1- arg)))
13071 ;; ... and one more line.
13072 (forward-line 1)
13073 (if fill-prefix-regexp
13074 ;; There is a fill prefix; it overrides parstart.
13075 (while (and (not (eobp))
13076 (progn (move-to-left-margin) (not (eobp)))
13077 (not (looking-at parsep))
13078 (looking-at fill-prefix-regexp))
13079 (forward-line 1))
13080 (while (and (re-search-forward sp-parstart nil 1)
13081 (progn (setq start (match-beginning 0))
13082 (goto-char start)
13083 (not (eobp)))
13084 (progn (move-to-left-margin)
13085 (not (looking-at parsep)))
13086 (or (not (looking-at parstart))
13087 (and use-hard-newlines
13088 (not (get-text-property (1- start) 'hard)))))
13089 (forward-char 1))
13090 (if (< (point) (point-max))
13091 (goto-char start))))
13092 (constrain-to-field nil opoint t)
13093 ;; Return the number of steps that could not be done.
13094 arg))
13095@end ignore
13096
12065The @code{forward-paragraph} function moves point forward to the end 13097The @code{forward-paragraph} function moves point forward to the end
12066of the paragraph. It is usually bound to @kbd{M-@}} and makes use of a 13098of the paragraph. It is usually bound to @kbd{M-@}} and makes use of a
12067number of functions that are important in themselves, including 13099number of functions that are important in themselves, including
@@ -12091,14 +13123,9 @@ exists, especially when blank lines separate paragraphs.
12091This is an added complication. 13123This is an added complication.
12092 13124
12093@menu 13125@menu
12094* forward-paragraph in brief:: Key parts of the function definition. 13126* forward-paragraph in brief::
12095* fwd-para let:: The @code{let*} expression. 13127* fwd-para let::
12096* fwd-para while:: The forward motion @code{while} loop. 13128* fwd-para while::
12097* fwd-para between paragraphs:: Movement between paragraphs.
12098* fwd-para within paragraph:: Movement within paragraphs.
12099* fwd-para no fill prefix:: When there is no fill prefix.
12100* fwd-para with fill prefix:: When there is a fill prefix.
12101* fwd-para summary:: Summary of @code{forward-paragraph} code.
12102@end menu 13129@end menu
12103 13130
12104@node forward-paragraph in brief, fwd-para let, forward-paragraph, forward-paragraph 13131@node forward-paragraph in brief, fwd-para let, forward-paragraph, forward-paragraph
@@ -12121,12 +13148,10 @@ In outline, the function looks like this:
12121 (or arg (setq arg 1)) 13148 (or arg (setq arg 1))
12122 (let* 13149 (let*
12123 @var{varlist} 13150 @var{varlist}
12124 (while (< arg 0) ; @r{backward-moving-code} 13151 (while (and (< arg 0) (not (bobp))) ; @r{backward-moving-code}
12125 @dots{} 13152 @dots{}
12126 (setq arg (1+ arg))) 13153 (while (and (> arg 0) (not (eobp))) ; @r{forward-moving-code}
12127 (while (> arg 0) ; @r{forward-moving-code}
12128 @dots{} 13154 @dots{}
12129 (setq arg (1- arg)))))
12130@end group 13155@end group
12131@end smallexample 13156@end smallexample
12132 13157
@@ -12147,22 +13172,34 @@ familiar part of this function.
12147@unnumberedsubsec The @code{let*} expression 13172@unnumberedsubsec The @code{let*} expression
12148 13173
12149The next line of the @code{forward-paragraph} function begins a 13174The next line of the @code{forward-paragraph} function begins a
12150@code{let*} expression. This is a different kind of expression than 13175@code{let*} expression. This is a different than @code{let}. The
12151we have seen so far. The symbol is @code{let*} not @code{let}. 13176symbol is @code{let*} not @code{let}.
12152 13177
12153The @code{let*} special form is like @code{let} except that Emacs sets 13178The @code{let*} special form is like @code{let} except that Emacs sets
12154each variable in sequence, one after another, and variables in the 13179each variable in sequence, one after another, and variables in the
12155latter part of the varlist can make use of the values to which Emacs 13180latter part of the varlist can make use of the values to which Emacs
12156set variables in the earlier part of the varlist. 13181set variables in the earlier part of the varlist.
12157 13182
12158In the @code{let*} expression in this function, Emacs binds two 13183@ignore
12159variables: @code{fill-prefix-regexp} and @code{paragraph-separate}. 13184( refappend save-excursion, , code save-excursion in code append-to-buffer .)
12160The value to which @code{paragraph-separate} is bound depends on the 13185@end ignore
12161value of @code{fill-prefix-regexp}.
12162 13186
12163@need 1200 13187(@ref{append save-excursion, , @code{save-excursion} in @code{append-to-buffer}}.)
12164Let's look at each in turn. The symbol @code{fill-prefix-regexp} is 13188
12165set to the value returned by evaluating the following list: 13189In the @code{let*} expression in this function, Emacs binds a total of
13190seven variables: @code{opoint}, @code{fill-prefix-regexp},
13191@code{parstart}, @code{parsep}, @code{sp-parstart}, @code{start}, and
13192@code{found-start}.
13193
13194The variable @code{parsep} appears twice, first, to remove instances
13195of @samp{^}, and second, to handle fill prefixes.
13196
13197The variable @code{opoint} is just the value of @code{point}. As you
13198can guess, it is used in a @code{constrain-to-field} expression, just
13199as in @code{forward-sentence}.
13200
13201The variable @code{fill-prefix-regexp} is set to the value returned by
13202evaluating the following list:
12166 13203
12167@smallexample 13204@smallexample
12168@group 13205@group
@@ -12226,49 +13263,44 @@ This means that @code{fill-prefix-regexp} will be set to a value that
12226will exactly match the fill prefix if the fill prefix exists. 13263will exactly match the fill prefix if the fill prefix exists.
12227Otherwise, the variable will be set to @code{nil}. 13264Otherwise, the variable will be set to @code{nil}.
12228 13265
12229The second local variable in the @code{let*} expression is 13266The next two local variables in the @code{let*} expression are
12230@code{paragraph-separate}. It is bound to the value returned by 13267designed to remove instances of @samp{^} from @code{parstart} and
12231evaluating the expression: 13268@code{parsep}, the local variables indicate the paragraph start and
12232 13269the paragraph separator. The next expression sets @code{parsep}
12233@smallexample 13270again. That is to handle fill prefixes.
12234@group
12235(if fill-prefix-regexp
12236 (concat paragraph-separate
12237 "\\|^" fill-prefix-regexp "[ \t]*$")
12238 paragraph-separate)))
12239@end group
12240@end smallexample
12241 13271
12242This expression shows why @code{let*} rather than @code{let} was used. 13272This is the setting that requires the definition call @code{let*}
12243The true-or-false-test for the @code{if} depends on whether the variable 13273rather than @code{let}. The true-or-false-test for the @code{if}
12244@code{fill-prefix-regexp} evaluates to @code{nil} or some other value. 13274depends on whether the variable @code{fill-prefix-regexp} evaluates to
13275@code{nil} or some other value.
12245 13276
12246If @code{fill-prefix-regexp} does not have a value, Emacs evaluates 13277If @code{fill-prefix-regexp} does not have a value, Emacs evaluates
12247the else-part of the @code{if} expression and binds 13278the else-part of the @code{if} expression and binds @code{parsep} to
12248@code{paragraph-separate} to its local value. 13279its local value. (@code{parsep} is a regular expression that matches
12249(@code{paragraph-separate} is a regular expression that matches what 13280what separates paragraphs.)
12250separates paragraphs.)
12251 13281
12252But if @code{fill-prefix-regexp} does have a value, Emacs evaluates 13282But if @code{fill-prefix-regexp} does have a value, Emacs evaluates
12253the then-part of the @code{if} expression and binds 13283the then-part of the @code{if} expression and binds @code{parsep} to a
12254@code{paragraph-separate} to a regular expression that includes the 13284regular expression that includes the @code{fill-prefix-regexp} as part
12255@code{fill-prefix-regexp} as part of the pattern. 13285of the pattern.
12256 13286
12257Specifically, @code{paragraph-separate} is set to the original value 13287Specifically, @code{parsep} is set to the original value of the
12258of the paragraph separate regular expression concatenated with an 13288paragraph separate regular expression concatenated with an alternative
12259alternative expression that consists of the @code{fill-prefix-regexp} 13289expression that consists of the @code{fill-prefix-regexp} followed by
12260followed by a blank line. The @samp{^} indicates that the 13290optional whitespace to the end of the line. The whitespace is defined
12261@code{fill-prefix-regexp} must begin a line, and the optional 13291by @w{@code{"[ \t]*$"}}.) The @samp{\\|} defines this portion of the
12262whitespace to the end of the line is defined by @w{@code{"[ \t]*$"}}.) 13292regexp as an alternative to @code{parsep}.
12263The @samp{\\|} defines this portion of the regexp as an alternative to 13293
12264@code{paragraph-separate}. 13294According to a comment in the code, the next local variable,
13295@code{sp-parstart}, is used for searching, and then the final two,
13296@code{start} and @code{found-start}, are set to @code{nil}.
12265 13297
12266Now we get into the body of the @code{let*}. The first part of the body 13298Now we get into the body of the @code{let*}. The first part of the body
12267of the @code{let*} deals with the case when the function is given a 13299of the @code{let*} deals with the case when the function is given a
12268negative argument and is therefore moving backwards. We will skip this 13300negative argument and is therefore moving backwards. We will skip this
12269section. 13301section.
12270 13302
12271@node fwd-para while, fwd-para between paragraphs, fwd-para let, forward-paragraph 13303@node fwd-para while, , fwd-para let, forward-paragraph
12272@unnumberedsubsec The forward motion @code{while} loop 13304@unnumberedsubsec The forward motion @code{while} loop
12273 13305
12274The second part of the body of the @code{let*} deals with forward 13306The second part of the body of the @code{let*} deals with forward
@@ -12278,349 +13310,260 @@ the function, the value of the argument is 1, so the body of the
12278@code{while} loop is evaluated exactly once, and the cursor moves 13310@code{while} loop is evaluated exactly once, and the cursor moves
12279forward one paragraph. 13311forward one paragraph.
12280 13312
13313@ignore
13314(while (and (> arg 0) (not (eobp)))
13315
13316 ;; Move forward over separator lines...
13317 (while (and (not (eobp))
13318 (progn (move-to-left-margin) (not (eobp)))
13319 (looking-at parsep))
13320 (forward-line 1))
13321 (unless (eobp) (setq arg (1- arg)))
13322 ;; ... and one more line.
13323 (forward-line 1)
13324
13325 (if fill-prefix-regexp
13326 ;; There is a fill prefix; it overrides parstart.
13327 (while (and (not (eobp))
13328 (progn (move-to-left-margin) (not (eobp)))
13329 (not (looking-at parsep))
13330 (looking-at fill-prefix-regexp))
13331 (forward-line 1))
13332
13333
13334 (while (and (re-search-forward sp-parstart nil 1)
13335 (progn (setq start (match-beginning 0))
13336 (goto-char start)
13337 (not (eobp)))
13338 (progn (move-to-left-margin)
13339 (not (looking-at parsep)))
13340 (or (not (looking-at parstart))
13341 (and use-hard-newlines
13342 (not (get-text-property (1- start) 'hard)))))
13343 (forward-char 1))
13344
13345 (if (< (point) (point-max))
13346 (goto-char start))))
13347@end ignore
13348
12281This part handles three situations: when point is between paragraphs, 13349This part handles three situations: when point is between paragraphs,
12282when point is within a paragraph and there is a fill prefix, and 13350when there is a fill prefix and when there is no fill prefix.
12283when point is within a paragraph and there is no fill prefix.
12284 13351
12285@need 800 13352@need 800
12286The @code{while} loop looks like this: 13353The @code{while} loop looks like this:
12287 13354
12288@smallexample 13355@smallexample
12289@group 13356@group
12290(while (> arg 0) 13357;; @r{going forwards and not at the end of the buffer}
12291 (beginning-of-line) 13358(while (and (> arg 0) (not (eobp)))
12292 13359
12293 ;; @r{between paragraphs} 13360 ;; @r{between paragraphs}
12294 (while (prog1 (and (not (eobp)) 13361 ;; Move forward over separator lines...
12295 (looking-at paragraph-separate)) 13362 (while (and (not (eobp))
12296 (forward-line 1))) 13363 (progn (move-to-left-margin) (not (eobp)))
13364 (looking-at parsep))
13365 (forward-line 1))
13366 ;; @r{This decrements the loop}
13367 (unless (eobp) (setq arg (1- arg)))
13368 ;; ... and one more line.
13369 (forward-line 1)
12297@end group 13370@end group
12298 13371
12299@group 13372@group
12300 ;; @r{within paragraphs, with a fill prefix}
12301 (if fill-prefix-regexp 13373 (if fill-prefix-regexp
12302 ;; @r{There is a fill prefix; it overrides paragraph-start.} 13374 ;; There is a fill prefix; it overrides parstart;
13375 ;; we go forward line by line
12303 (while (and (not (eobp)) 13376 (while (and (not (eobp))
12304 (not (looking-at paragraph-separate)) 13377 (progn (move-to-left-margin) (not (eobp)))
13378 (not (looking-at parsep))
12305 (looking-at fill-prefix-regexp)) 13379 (looking-at fill-prefix-regexp))
12306 (forward-line 1)) 13380 (forward-line 1))
12307@end group 13381@end group
12308 13382
12309@group 13383@group
12310 ;; @r{within paragraphs, no fill prefix} 13384 ;; There is no fill prefix;
12311 (if (re-search-forward paragraph-start nil t) 13385 ;; we go forward character by character
12312 (goto-char (match-beginning 0)) 13386 (while (and (re-search-forward sp-parstart nil 1)
12313 (goto-char (point-max)))) 13387 (progn (setq start (match-beginning 0))
13388 (goto-char start)
13389 (not (eobp)))
13390 (progn (move-to-left-margin)
13391 (not (looking-at parsep)))
13392 (or (not (looking-at parstart))
13393 (and use-hard-newlines
13394 (not (get-text-property (1- start) 'hard)))))
13395 (forward-char 1))
13396@end group
12314 13397
12315 (setq arg (1- arg))) 13398@group
13399 ;; and if there is no fill prefix and if we are not at the end,
13400 ;; go to whatever was found in the regular expression search
13401 ;; for sp-parstart
13402 (if (< (point) (point-max))
13403 (goto-char start))))
12316@end group 13404@end group
12317@end smallexample 13405@end smallexample
12318 13406
12319We can see immediately that this is a decrementing counter @code{while} 13407@findex eobp
12320loop, using the expression @code{(setq arg (1- arg))} as the decrementer. 13408We can see that this is a decrementing counter @code{while} loop,
13409using the expression @code{(setq arg (1- arg))} as the decrementer.
13410That expression is not far from the @code{while}, but is hidden in
13411another Lisp macro, an @code{unless} macro. Unless we are at the end
13412of the buffer --- that is what the @code{eobp} function determines; it
13413is an abbreviation of @samp{End Of Buffer P} --- we decrease the value
13414of @code{arg} by one.
13415
13416(If we are at the end of the buffer, we cannot go forward any more and
13417the next loop of the @code{while} expression will test false since the
13418test is an @code{and} with @code{(not (eobp))}. The @code{not}
13419function means exactly as you expect; it is another name for
13420@code{null}, a function that returns true when its argument is false.)
13421
13422Interestingly, the loop count is not decremented until we leave the
13423space between paragraphs, unless we come to the end of buffer or stop
13424seeing the local value of the paragraph separator.
13425
13426That second @code{while} also has a @code{(move-to-left-margin)}
13427expression. The function is self-explanatory. It is inside a
13428@code{progn} expression and not the last element of its body, so it is
13429only invoked for its side effect, which is to move point to the left
13430margin of the current line.
12321 13431
12322@need 800 13432@findex looking-at
12323The body of the loop consists of three expressions: 13433The @code{looking-at} function is also self-explanatory; it returns
13434true if the text after point matches the regular expression given as
13435its argument.
12324 13436
12325@smallexample 13437The rest of the body of the loop looks difficult at first, but makes
12326@group 13438sense as you come to understand it.
12327;; @r{between paragraphs}
12328(beginning-of-line)
12329(while
12330 @var{body-of-while})
12331@end group
12332 13439
12333@group 13440@need 800
12334;; @r{within paragraphs, with fill prefix} 13441First consider what happens if there is a fill prefix:
12335(if @var{true-or-false-test}
12336 @var{then-part}
12337@end group
12338 13442
13443@smallexample
12339@group 13444@group
12340;; @r{within paragraphs, no fill prefix} 13445 (if fill-prefix-regexp
12341 @var{else-part} 13446 ;; There is a fill prefix; it overrides parstart;
13447 ;; we go forward line by line
13448 (while (and (not (eobp))
13449 (progn (move-to-left-margin) (not (eobp)))
13450 (not (looking-at parsep))
13451 (looking-at fill-prefix-regexp))
13452 (forward-line 1))
12342@end group 13453@end group
12343@end smallexample 13454@end smallexample
12344 13455
12345@noindent 13456@noindent
12346When the Emacs Lisp interpreter evaluates the body of the 13457This expression moves point forward line by line so long
12347@code{while} loop, the first thing it does is evaluate the 13458as four conditions are true:
12348@code{(beginning-of-line)} expression and move point to the beginning
12349of the line. Then there is an inner @code{while} loop. This
12350@code{while} loop is designed to move the cursor out of the blank
12351space between paragraphs, if it should happen to be there. Finally,
12352there is an @code{if} expression that actually moves point to the end
12353of the paragraph.
12354
12355@node fwd-para between paragraphs, fwd-para within paragraph, fwd-para while, forward-paragraph
12356@unnumberedsubsec Between paragraphs
12357 13459
12358First, let us look at the inner @code{while} loop. This loop handles 13460@enumerate
12359the case when point is between paragraphs; it uses three functions
12360that are new to us: @code{prog1}, @code{eobp} and @code{looking-at}.
12361@findex prog1
12362@findex eobp
12363@findex looking-at
12364
12365@itemize @bullet
12366@item 13461@item
12367@code{prog1} is similar to the @code{progn} special form, 13462Point is not at the end of the buffer.
12368except that @code{prog1} evaluates its arguments in sequence and then
12369returns the value of its first argument as the value of the whole
12370expression. (@code{progn} returns the value of its last argument as the
12371value of the expression.) The second and subsequent arguments to
12372@code{prog1} are evaluated only for their side effects.
12373 13463
12374@item 13464@item
12375@code{eobp} is an abbreviation of @samp{End Of Buffer P} and is a 13465We can move to the left margin of the text and are
12376function that returns true if point is at the end of the buffer. 13466not at the end of the buffer.
12377 13467
12378@item 13468@item
12379@code{looking-at} is a function that returns true if the text following 13469The text following point does not separate paragraphs.
12380point matches the regular expression passed @code{looking-at} as its
12381argument.
12382@end itemize
12383 13470
12384@need 800 13471@item
12385The @code{while} loop we are studying looks like this: 13472The pattern following point is the fill prefix regular expression.
13473@end enumerate
12386 13474
12387@smallexample 13475The last condition may be puzzling, until you remember that point was
12388@group 13476moved to the beginning of the line early in the @code{forward-paragraph}
12389(while (prog1 (and (not (eobp)) 13477function. This means that if the text has a fill prefix, the
12390 (looking-at paragraph-separate)) 13478@code{looking-at} function will see it.
12391 (forward-line 1)))
12392@end group
12393@end smallexample
12394 13479
12395@need 1200 13480@need 1250
12396@noindent 13481Consider what happens when there is no fill prefix.
12397This is a @code{while} loop with no body! The true-or-false-test of the
12398loop is the expression:
12399 13482
12400@smallexample 13483@smallexample
12401@group 13484@group
12402(prog1 (and (not (eobp)) 13485 (while (and (re-search-forward sp-parstart nil 1)
12403 (looking-at paragraph-separate)) 13486 (progn (setq start (match-beginning 0))
12404 (forward-line 1)) 13487 (goto-char start)
13488 (not (eobp)))
13489 (progn (move-to-left-margin)
13490 (not (looking-at parsep)))
13491 (or (not (looking-at parstart))
13492 (and use-hard-newlines
13493 (not (get-text-property (1- start) 'hard)))))
13494 (forward-char 1))
12405@end group 13495@end group
12406@end smallexample 13496@end smallexample
12407 13497
12408@noindent 13498@noindent
12409The first argument to the @code{prog1} is the @code{and} expression. It 13499This @code{while} loop has us searching forward for
12410has within in it a test of whether point is at the end of the buffer and 13500@code{sp-parstart}, which is the combination of possible whitespace
12411also a test of whether the pattern following point matches the regular 13501with a the local value of the start of a paragraph or of a paragraph
12412expression for separating paragraphs. 13502separator. (The latter two are within an expression starting
12413 13503@code{\(?:} so that they are not referenced by the
12414If the cursor is not at the end of the buffer and if the characters 13504@code{match-beginning} function.)
12415following the cursor mark the separation between two paragraphs, then
12416the @code{and} expression is true. After evaluating the @code{and}
12417expression, the Lisp interpreter evaluates the second argument to
12418@code{prog1}, which is @code{forward-line}. This moves point forward
12419one line. The value returned by the @code{prog1} however, is the
12420value of its first argument, so the @code{while} loop continues so
12421long as point is not at the end of the buffer and is between
12422paragraphs. When, finally, point is moved to a paragraph, the
12423@code{and} expression tests false. Note however, that the
12424@code{forward-line} command is carried out anyhow. This means that
12425when point is moved from between paragraphs to a paragraph, it is left
12426at the beginning of the second line of the paragraph.
12427
12428@node fwd-para within paragraph, fwd-para no fill prefix, fwd-para between paragraphs, forward-paragraph
12429@unnumberedsubsec Within paragraphs
12430
12431The next expression in the outer @code{while} loop is an @code{if}
12432expression. The Lisp interpreter evaluates the then-part of the
12433@code{if} when the @code{fill-prefix-regexp} variable has a value other
12434than @code{nil}, and it evaluates the else-part when the value of
12435@code{if fill-prefix-regexp} is @code{nil}, that is, when there is no
12436fill prefix.
12437 13505
12438@node fwd-para no fill prefix, fwd-para with fill prefix, fwd-para within paragraph, forward-paragraph 13506@need 800
12439@unnumberedsubsec No fill prefix 13507The two expressions,
12440
12441It is simplest to look at the code for the case when there is no fill
12442prefix first. This code consists of yet another inner @code{if}
12443expression, and reads as follows:
12444 13508
12445@smallexample 13509@smallexample
12446@group 13510@group
12447(if (re-search-forward paragraph-start nil t) 13511(setq start (match-beginning 0))
12448 (goto-char (match-beginning 0)) 13512(goto-char start)
12449 (goto-char (point-max)))
12450@end group 13513@end group
12451@end smallexample 13514@end smallexample
12452 13515
12453@noindent 13516@noindent
12454This expression actually does the work that most people think of as 13517mean go to the start of the text matched by the regular expression
12455the primary purpose of the @code{forward-paragraph} command: it causes 13518search.
12456a regular expression search to occur that searches forward to the
12457start of the next paragraph and if it is found, moves point there; but
12458if the start of another paragraph if not found, it moves point to the
12459end of the accessible region of the buffer.
12460 13519
12461The only unfamiliar part of this is the use of @code{match-beginning}. 13520The @code{(match-beginning 0)} expression is new. It returns a number
12462This is another function that is new to us. The 13521specifying the location of the start of the text that was matched by
12463@code{match-beginning} function returns a number specifying the 13522the last search.
12464location of the start of the text that was matched by the last regular
12465expression search.
12466 13523
12467The @code{match-beginning} function is used here because of a 13524The @code{match-beginning} function is used here because of a
12468characteristic of a forward search: a successful forward search, 13525characteristic of a forward search: a successful forward search,
12469regardless of whether it is a plain search or a regular expression 13526regardless of whether it is a plain search or a regular expression
12470search, will move point to the end of the text that is found. In this 13527search, moves point to the end of the text that is found. In this
12471case, a successful search will move point to the end of the pattern for 13528case, a successful search moves point to the end of the pattern for
12472@code{paragraph-start}, which will be the beginning of the next 13529@code{sp-parstart}.
12473paragraph rather than the end of the current one.
12474 13530
12475However, we want to put point at the end of the current paragraph, not at 13531However, we want to put point at the end of the current paragraph, not
12476the beginning of the next one. The two positions may be different, 13532somewhere else. Indeed, since the search possibly includes the
12477because there may be several blank lines between paragraphs. 13533paragraph separator, point may end up at the beginning of the next one
13534unless we use an expression that includes @code{match-beginning}.
12478 13535
12479@findex match-beginning 13536@findex match-beginning
12480When given an argument of 0, @code{match-beginning} returns the position 13537When given an argument of 0, @code{match-beginning} returns the
12481that is the start of the text that the most recent regular 13538position that is the start of the text matched by the most recent
12482expression search matched. In this case, the most recent regular 13539search. In this case, the most recent search looks for
12483expression search is the one looking for @code{paragraph-start}, so 13540@code{sp-parstart}. The @code{(match-beginning 0)} expression returns
12484@code{match-beginning} returns the beginning position of the pattern, 13541the beginning position of that pattern, rather than the end position
12485rather than the end of the pattern. The beginning position is the end 13542of that pattern.
12486of the paragraph.
12487 13543
12488(Incidentally, when passed a positive number as an argument, the 13544(Incidentally, when passed a positive number as an argument, the
12489@code{match-beginning} function will place point at that parenthesized 13545@code{match-beginning} function returns the location of point at that
12490expression in the last regular expression. It is a useful function.) 13546parenthesized expression in the last search unless that parenthesized
12491 13547expression begins with @code{\(?:}. I don't know why @code{\(?:}
12492@node fwd-para with fill prefix, fwd-para summary, fwd-para no fill prefix, forward-paragraph 13548appears here since the argument is 0.)
12493@unnumberedsubsec With a fill prefix
12494 13549
12495The inner @code{if} expression just discussed is the else-part of an enclosing 13550@need 1250
12496@code{if} expression which tests whether there is a fill prefix. If 13551The last expression when there is no fill prefix is
12497there is a fill prefix, the then-part of this @code{if} is evaluated.
12498It looks like this:
12499 13552
12500@smallexample 13553@smallexample
12501@group 13554@group
12502(while (and (not (eobp)) 13555(if (< (point) (point-max))
12503 (not (looking-at paragraph-separate)) 13556 (goto-char start))))
12504 (looking-at fill-prefix-regexp))
12505 (forward-line 1))
12506@end group 13557@end group
12507@end smallexample 13558@end smallexample
12508 13559
12509@noindent 13560@noindent
12510What this expression does is move point forward line by line so long 13561This says that if there is no fill prefix and if we are not at the
12511as three conditions are true: 13562end, point should move to the beginning of whatever was found by the
12512 13563regular expression search for @code{sp-parstart}.
12513@enumerate
12514@item
12515Point is not at the end of the buffer.
12516
12517@item
12518The text following point does not separate paragraphs.
12519
12520@item
12521The pattern following point is the fill prefix regular expression.
12522@end enumerate
12523
12524The last condition may be puzzling, until you remember that point was
12525moved to the beginning of the line early in the @code{forward-paragraph}
12526function. This means that if the text has a fill prefix, the
12527@code{looking-at} function will see it.
12528
12529@node fwd-para summary, , fwd-para with fill prefix, forward-paragraph
12530@unnumberedsubsec Summary
12531
12532In summary, when moving forward, the @code{forward-paragraph} function
12533does the following:
12534
12535@itemize @bullet
12536@item
12537Move point to the beginning of the line.
12538
12539@item
12540Skip over lines between paragraphs.
12541
12542@item
12543Check whether there is a fill prefix, and if there is:
12544
12545@itemize ---
12546
12547@item
12548Go forward line by line so long as the line is not a paragraph
12549separating line.
12550@end itemize
12551
12552@item
12553But if there is no fill prefix,
12554
12555@itemize ---
12556
12557@item
12558Search for the next paragraph start pattern.
12559
12560@item
12561Go to the beginning of the paragraph start pattern, which will be the
12562end of the previous paragraph.
12563
12564@item
12565Or else go to the end of the accessible portion of the buffer.
12566@end itemize
12567@end itemize
12568
12569@need 1200
12570For review, here is the code we have just been discussing, formatted
12571for clarity:
12572
12573@smallexample
12574@group
12575(interactive "p")
12576(or arg (setq arg 1))
12577(let* (
12578 (fill-prefix-regexp
12579 (and fill-prefix (not (equal fill-prefix ""))
12580 (not paragraph-ignore-fill-prefix)
12581 (regexp-quote fill-prefix)))
12582@end group
12583
12584@group
12585 (paragraph-separate
12586 (if fill-prefix-regexp
12587 (concat paragraph-separate
12588 "\\|^"
12589 fill-prefix-regexp
12590 "[ \t]*$")
12591 paragraph-separate)))
12592
12593 @var{omitted-backward-moving-code} @dots{}
12594@end group
12595
12596@group
12597 (while (> arg 0) ; @r{forward-moving-code}
12598 (beginning-of-line)
12599
12600 (while (prog1 (and (not (eobp))
12601 (looking-at paragraph-separate))
12602 (forward-line 1)))
12603@end group
12604
12605@group
12606 (if fill-prefix-regexp
12607 (while (and (not (eobp)) ; @r{then-part}
12608 (not (looking-at paragraph-separate))
12609 (looking-at fill-prefix-regexp))
12610 (forward-line 1))
12611@end group
12612@group
12613 ; @r{else-part: the inner-if}
12614 (if (re-search-forward paragraph-start nil t)
12615 (goto-char (match-beginning 0))
12616 (goto-char (point-max))))
12617
12618 (setq arg (1- arg))))) ; @r{decrementer}
12619@end group
12620@end smallexample
12621 13564
12622The full definition for the @code{forward-paragraph} function not only 13565The full definition for the @code{forward-paragraph} function not only
12623includes this code for going forwards, but also code for going backwards. 13566includes code for going forwards, but also code for going backwards.
12624 13567
12625If you are reading this inside of GNU Emacs and you want to see the 13568If you are reading this inside of GNU Emacs and you want to see the
12626whole function, you can type @kbd{C-h f} (@code{describe-function}) 13569whole function, you can type @kbd{C-h f} (@code{describe-function})
@@ -12631,26 +13574,25 @@ key; you will be taken directly to the source. (Be sure to install
12631your sources! Without them, you are like a person who tries to drive 13574your sources! Without them, you are like a person who tries to drive
12632a car with his eyes shut!) 13575a car with his eyes shut!)
12633 13576
12634@c !!! again, 21.0.100 tags table location in this paragraph
12635Or -- a good habit to get into -- you can type @kbd{M-.}
12636(@code{find-tag}) and the name of the function when prompted for it.
12637This will take you directly to the source. If the @code{find-tag}
12638function first asks you for the name of a @file{TAGS} table, give it
12639the name of the @file{TAGS} file such as
12640@file{/usr/local/share/emacs/21.0.100/lisp/TAGS}. (The exact path to your
12641@file{TAGS} file depends on how your copy of Emacs was installed.)
12642
12643You can also create your own @file{TAGS} file for directories that
12644lack one.
12645@ifnottex
12646@xref{etags, , Create Your Own @file{TAGS} File}.
12647@end ifnottex
12648
12649@node etags, Regexp Review, forward-paragraph, Regexp Search 13577@node etags, Regexp Review, forward-paragraph, Regexp Search
12650@section Create Your Own @file{TAGS} File 13578@section Create Your Own @file{TAGS} File
12651@findex etags 13579@findex etags
12652@cindex @file{TAGS} file, create own 13580@cindex @file{TAGS} file, create own
12653 13581
13582Besides @kbd{C-h f} (@code{describe-function}), another way to see the
13583source of a function is to type @kbd{M-.} (@code{find-tag}) and the
13584name of the function when prompted for it. This is a good habit to
13585get into. This will take you directly to the source. If the
13586@code{find-tag} function first asks you for the name of a @file{TAGS}
13587table, give it the name of a @file{TAGS} file such as
13588@file{/usr/local/src/emacs/src/TAGS}. (The exact path to your
13589@file{TAGS} file depends on how your copy of Emacs was installed. I
13590just told you the location that provides both my C and my Emacs Lisp
13591sources.)
13592
13593You can also create your own @file{TAGS} file for directories that
13594lack one.
13595
12654The @kbd{M-.} (@code{find-tag}) command takes you directly to the 13596The @kbd{M-.} (@code{find-tag}) command takes you directly to the
12655source for a function, variable, node, or other source. The function 13597source for a function, variable, node, or other source. The function
12656depends on tags tables to tell it where to go. 13598depends on tags tables to tell it where to go.
@@ -12720,11 +13662,11 @@ If you think an appropriate @file{TAGS} file already exists for what
12720you want, but do not know where it is, you can use the @code{locate} 13662you want, but do not know where it is, you can use the @code{locate}
12721program to attempt to find it. 13663program to attempt to find it.
12722 13664
12723Type @w{@kbd{M-x locate RET TAGS RET}} and Emacs will list for you the 13665Type @w{@kbd{M-x locate @key{RET} TAGS @key{RET}}} and Emacs will list
12724full path names of all your @file{TAGS} files. On my system, this 13666for you the full path names of all your @file{TAGS} files. On my
12725command lists 34 @file{TAGS} files. On the other hand, a `plain 13667system, this command lists 34 @file{TAGS} files. On the other hand, a
12726vanilla' system I recently installed did not contain any @file{TAGS} 13668`plain vanilla' system I recently installed did not contain any
12727files. 13669@file{TAGS} files.
12728 13670
12729If the tags table you want has been created, you can use the @code{M-x 13671If the tags table you want has been created, you can use the @code{M-x
12730visit-tags-table} command to specify it. Otherwise, you will need to 13672visit-tags-table} command to specify it. Otherwise, you will need to
@@ -12784,6 +13726,7 @@ For example:
12784 nil 13726 nil
12785@end group 13727@end group
12786@end smallexample 13728@end smallexample
13729
12787@noindent 13730@noindent
12788(The @code{insert} function inserts its arguments at point; the 13731(The @code{insert} function inserts its arguments at point; the
12789@code{format} function returns a string formatted from its arguments 13732@code{format} function returns a string formatted from its arguments
@@ -12800,6 +13743,7 @@ Takes four arguments, like @code{search-forward}:
12800@enumerate 13743@enumerate
12801@item 13744@item
12802A regular expression that specifies the pattern to search for. 13745A regular expression that specifies the pattern to search for.
13746(Remember to put quotation marks around this argument!)
12803 13747
12804@item 13748@item
12805Optionally, the limit of the search. 13749Optionally, the limit of the search.
@@ -12844,20 +13788,6 @@ Return @code{t} for true if point is at the end of the accessible part
12844of a buffer. The end of the accessible part is the end of the buffer 13788of a buffer. The end of the accessible part is the end of the buffer
12845if the buffer is not narrowed; it is the end of the narrowed part if 13789if the buffer is not narrowed; it is the end of the narrowed part if
12846the buffer is narrowed. 13790the buffer is narrowed.
12847
12848@item prog1
12849Evaluate each argument in sequence and then return the value of the
12850@emph{first}.
12851
12852@need 1250
12853For example:
12854
12855@smallexample
12856@group
12857(prog1 1 2 3 4)
12858 @result{} 1
12859@end group
12860@end smallexample
12861@end table 13791@end table
12862 13792
12863@need 1500 13793@need 1500
@@ -12891,8 +13821,8 @@ word count commands using @code{while} loops and recursion.
12891 13821
12892@menu 13822@menu
12893* Why Count Words:: 13823* Why Count Words::
12894* count-words-region:: Use a regexp, but find a problem. 13824* count-words-region::
12895* recursive-count-words:: Start with case of no words in region. 13825* recursive-count-words::
12896* Counting Exercise:: 13826* Counting Exercise::
12897@end menu 13827@end menu
12898 13828
@@ -12928,8 +13858,8 @@ the Emacs tradition encourages flexibility---you may want to count
12928words in just a section, rather than all of a buffer. So it makes 13858words in just a section, rather than all of a buffer. So it makes
12929more sense to design the command to count the number of words in a 13859more sense to design the command to count the number of words in a
12930region. Once you have a @code{count-words-region} command, you can, 13860region. Once you have a @code{count-words-region} command, you can,
12931if you wish, count words in a whole buffer by marking it with @kbd{C-x 13861if you wish, count words in a whole buffer by marking it with
12932h} (@code{mark-whole-buffer}). 13862@w{@kbd{C-x h}} (@code{mark-whole-buffer}).
12933 13863
12934Clearly, counting words is a repetitive act: starting from the 13864Clearly, counting words is a repetitive act: starting from the
12935beginning of the region, you count the first word, then the second 13865beginning of the region, you count the first word, then the second
@@ -12938,8 +13868,8 @@ region. This means that word counting is ideally suited to recursion
12938or to a @code{while} loop. 13868or to a @code{while} loop.
12939 13869
12940@menu 13870@menu
12941* Design count-words-region:: The definition using a @code{while} loop. 13871* Design count-words-region::
12942* Whitespace Bug:: The Whitespace Bug in @code{count-words-region}. 13872* Whitespace Bug::
12943@end menu 13873@end menu
12944 13874
12945@node Design count-words-region, Whitespace Bug, count-words-region, count-words-region 13875@node Design count-words-region, Whitespace Bug, count-words-region, count-words-region
@@ -13043,11 +13973,13 @@ The search expression looks like this:
13043 13973
13044@noindent 13974@noindent
13045(Note that paired backslashes precede the @samp{w} and @samp{W}. A 13975(Note that paired backslashes precede the @samp{w} and @samp{W}. A
13046single backslash has special meaning to the Emacs Lisp interpreter. It 13976single backslash has special meaning to the Emacs Lisp interpreter.
13047indicates that the following character is interpreted differently than 13977It indicates that the following character is interpreted differently
13048usual. For example, the two characters, @samp{\n}, stand for 13978than usual. For example, the two characters, @samp{\n}, stand for
13049@samp{newline}, rather than for a backslash followed by @samp{n}. Two 13979@samp{newline}, rather than for a backslash followed by @samp{n}. Two
13050backslashes in a row stand for an ordinary, `unspecial' backslash.) 13980backslashes in a row stand for an ordinary, `unspecial' backslash,
13981which in this case is followed by a letter, the combination of which
13982is important to @code{re-search-forward}.)
13051 13983
13052We need a counter to count how many words there are; this variable 13984We need a counter to count how many words there are; this variable
13053must first be set to 0 and then incremented each time Emacs goes 13985must first be set to 0 and then incremented each time Emacs goes
@@ -13312,21 +14244,21 @@ the @code{while} loop as the true-or-false-test, like this:
13312@iftex 14244@iftex
13313@noindent 14245@noindent
13314(For information about @code{and}, see 14246(For information about @code{and}, see
13315@ref{forward-paragraph, , @code{forward-paragraph}: a Goldmine of 14247@ref{kill-new function, , The @code{kill-new} function}.)
13316Functions}.)
13317@end iftex 14248@end iftex
13318@ifinfo 14249@ifinfo
13319@noindent 14250@noindent
13320(@xref{forward-paragraph}, for information about @code{and}.) 14251(@xref{kill-new function, , The @code{kill-new} function}, for
14252information about @code{and}.)
13321@end ifinfo 14253@end ifinfo
13322 14254
13323The @code{re-search-forward} expression returns @code{t} if the search 14255The @code{re-search-forward} expression returns @code{t} if the search
13324succeeds and as a side effect moves point. Consequently, as words are 14256succeeds and as a side effect moves point. Consequently, as words are
13325found, point is moved through the region. When the search 14257found, point is moved through the region. When the search expression
13326expression fails to find another word, or when point reaches the end 14258fails to find another word, or when point reaches the end of the
13327of the region, the true-or-false-test tests false, the @code{while} 14259region, the true-or-false-test tests false, the @code{while} loop
13328loop exists, and the @code{count-words-region} function displays one 14260exits, and the @code{count-words-region} function displays one or
13329or other of its messages. 14261other of its messages.
13330 14262
13331After incorporating these final changes, the @code{count-words-region} 14263After incorporating these final changes, the @code{count-words-region}
13332works without bugs (or at least, without bugs that I have found!). 14264works without bugs (or at least, without bugs that I have found!).
@@ -13590,6 +14522,7 @@ point forward by one word, and since a recursive call is made for
13590each word, the counting mechanism must be an expression that adds one 14522each word, the counting mechanism must be an expression that adds one
13591to the value returned by a call to @code{recursive-count-words}. 14523to the value returned by a call to @code{recursive-count-words}.
13592 14524
14525@need 800
13593Consider several cases: 14526Consider several cases:
13594 14527
13595@itemize @bullet 14528@itemize @bullet
@@ -13776,15 +14709,15 @@ and this will tell.
13776 14709
13777@menu 14710@menu
13778* Divide and Conquer:: 14711* Divide and Conquer::
13779* Words and Symbols:: What to count? 14712* Words and Symbols::
13780* Syntax:: What constitutes a word or symbol? 14713* Syntax::
13781* count-words-in-defun:: Very like @code{count-words}. 14714* count-words-in-defun::
13782* Several defuns:: Counting several defuns in a file. 14715* Several defuns::
13783* Find a File:: Do you want to look at a file? 14716* Find a File::
13784* lengths-list-file:: A list of the lengths of many definitions. 14717* lengths-list-file::
13785* Several files:: Counting in definitions in different files. 14718* Several files::
13786* Several files recursively:: Recursively counting in different files. 14719* Several files recursively::
13787* Prepare the data:: Prepare the data for display in a graph. 14720* Prepare the data::
13788@end menu 14721@end menu
13789 14722
13790@node Divide and Conquer, Words and Symbols, Words in a defun, Words in a defun 14723@node Divide and Conquer, Words and Symbols, Words in a defun, Words in a defun
@@ -14161,7 +15094,7 @@ several definitions within a single file.
14161@node Several defuns, Find a File, count-words-in-defun, Words in a defun 15094@node Several defuns, Find a File, count-words-in-defun, Words in a defun
14162@section Count Several @code{defuns} Within a File 15095@section Count Several @code{defuns} Within a File
14163 15096
14164A file such as @file{simple.el} may have 80 or more function 15097A file such as @file{simple.el} may have a hundred or more function
14165definitions within it. Our long term goal is to collect statistics on 15098definitions within it. Our long term goal is to collect statistics on
14166many files, but as a first step, our immediate goal is to collect 15099many files, but as a first step, our immediate goal is to collect
14167statistics on one file. 15100statistics on one file.
@@ -14223,9 +15156,7 @@ command. This command is almost, but not quite right for the lengths
14223problem. 15156problem.
14224 15157
14225@need 1200 15158@need 1200
14226Let's look at the source for @code{find-file} (you can use the 15159Let's look at the source for @code{find-file}:
14227@code{find-tag} command or @kbd{C-h f} (@code{describe-function}) to
14228find the source of a function):
14229 15160
14230@smallexample 15161@smallexample
14231@group 15162@group
@@ -14238,18 +15169,54 @@ creating one if none already exists."
14238@end group 15169@end group
14239@end smallexample 15170@end smallexample
14240 15171
14241The definition possesses short but complete documentation and an 15172@noindent
14242interactive specification that prompts you for a file name when you 15173(The most recent version of the @code{find-file} function definition
14243use the command interactively. The body of the definition contains 15174permits you to specify optional wildcards visit multiple files; that
14244two functions, @code{find-file-noselect} and @code{switch-to-buffer}. 15175makes the definition more complex and we will not discuss it here,
15176since it is not relevant. You can see its source using either
15177@kbd{M-.} (@code{find-tag}) or @kbd{C-h f} (@code{describe-function}).)
15178
15179@ignore
15180In Emacs 22
15181(defun find-file (filename &optional wildcards)
15182 "Edit file FILENAME.
15183Switch to a buffer visiting file FILENAME,
15184creating one if none already exists.
15185Interactively, the default if you just type RET is the current directory,
15186but the visited file name is available through the minibuffer history:
15187type M-n to pull it into the minibuffer.
15188
15189Interactively, or if WILDCARDS is non-nil in a call from Lisp,
15190expand wildcards (if any) and visit multiple files. You can
15191suppress wildcard expansion by setting `find-file-wildcards' to nil.
15192
15193To visit a file without any kind of conversion and without
15194automatically choosing a major mode, use \\[find-file-literally]."
15195 (interactive (find-file-read-args "Find file: " nil))
15196 (let ((value (find-file-noselect filename nil nil wildcards)))
15197 (if (listp value)
15198 (mapcar 'switch-to-buffer (nreverse value))
15199 (switch-to-buffer value))))
15200@end ignore
15201
15202The definition I am showing possesses short but complete documentation
15203and an interactive specification that prompts you for a file name when
15204you use the command interactively. The body of the definition
15205contains two functions, @code{find-file-noselect} and
15206@code{switch-to-buffer}.
14245 15207
14246According to its documentation as shown by @kbd{C-h f} (the 15208According to its documentation as shown by @kbd{C-h f} (the
14247@code{describe-function} command), the @code{find-file-noselect} 15209@code{describe-function} command), the @code{find-file-noselect}
14248function reads the named file into a buffer and returns the buffer. 15210function reads the named file into a buffer and returns the buffer.
14249However, the buffer is not selected. Emacs does not switch its 15211(Its most recent version includes an optional wildcards argument,
14250attention (or yours if you are using @code{find-file-noselect}) to the 15212too, as well as another to read a file literally and an other you
14251named buffer. That is what @code{switch-to-buffer} does: it switches 15213suppress warning messages. These optional arguments are irrelevant.)
14252the buffer to which Emacs attention is directed; and it switches the 15214
15215However, the @code{find-file-noselect} function does not select the
15216buffer in which it puts the file. Emacs does not switch its attention
15217(or yours if you are using @code{find-file-noselect}) to the named
15218buffer. That is what @code{switch-to-buffer} does: it switches the
15219buffer to which Emacs attention is directed; and it switches the
14253buffer displayed in the window to the new buffer. We have discussed 15220buffer displayed in the window to the new buffer. We have discussed
14254buffer switching elsewhere. (@xref{Switching Buffers}.) 15221buffer switching elsewhere. (@xref{Switching Buffers}.)
14255 15222
@@ -14261,7 +15228,7 @@ program to a different buffer but does not redisplay it on the screen.
14261So instead of calling on @code{find-file} to do the job, we must write 15228So instead of calling on @code{find-file} to do the job, we must write
14262our own expression. 15229our own expression.
14263 15230
14264The task is easy: use @code{find-file-noselect} and @code{set-buffer}. 15231The task is easy: use @code{find-file-noselect} and @code{set-buffer}.
14265 15232
14266@node lengths-list-file, Several files, Find a File, Words in a defun 15233@node lengths-list-file, Several files, Find a File, Words in a defun
14267@section @code{lengths-list-file} in Detail 15234@section @code{lengths-list-file} in Detail
@@ -14345,10 +15312,10 @@ carried out. In the loop, Emacs determines the length of each
14345definition and constructs a lengths' list containing the information. 15312definition and constructs a lengths' list containing the information.
14346 15313
14347Emacs kills the buffer after working through it. This is to save 15314Emacs kills the buffer after working through it. This is to save
14348space inside of Emacs. My version of Emacs 19 contained over 300 15315space inside of Emacs. My version of GNU Emacs 19 contained over 300
14349source files of interest; Emacs 21 contains over 800 source files. 15316source files of interest; GNU Emacs 22 contains over a thousand source
14350Another function will apply @code{lengths-list-file} to each of the 15317files. Another function will apply @code{lengths-list-file} to each
14351files. 15318of the files.
14352 15319
14353Finally, the last expression within the @code{let} expression is the 15320Finally, the last expression within the @code{let} expression is the
14354@code{lengths-list} variable; its value is returned as the value of 15321@code{lengths-list} variable; its value is returned as the value of
@@ -14358,20 +15325,29 @@ You can try this function by installing it in the usual fashion. Then
14358place your cursor after the following expression and type @kbd{C-x 15325place your cursor after the following expression and type @kbd{C-x
14359C-e} (@code{eval-last-sexp}). 15326C-e} (@code{eval-last-sexp}).
14360 15327
14361@c !!! 21.0.100 lisp sources location here 15328@c !!! 22.0.100 lisp sources location here
14362@smallexample 15329@smallexample
14363(lengths-list-file 15330(lengths-list-file
14364 "/usr/local/share/emacs/21.0.100/lisp/emacs-lisp/debug.el") 15331 "/usr/local/share/emacs/22.0.100/lisp/emacs-lisp/debug.el")
14365@end smallexample 15332@end smallexample
14366 15333
14367@c was: (lengths-list-file "../lisp/debug.el") 15334@c was: (lengths-list-file "../lisp/debug.el")
14368@c !!! as of 21, Info file is in 15335@ignore
14369@c /usr/share/info/emacs-lisp-intro.info.gz 153362006 Oct 29
14370@c but debug.el is in /usr/local/share/emacs/21.0.100/lisp/emacs-lisp/debug.el 15337For Bob, as root,
15338 gunzip /usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el.gz
15339In GNU Emacs 22, in *info* eval
15340 count-words-in-defun
15341in The `count-words-in-defun' Function
15342 count-words-in-defun
15343in `lengths-list-file' in Detail
15344then eval
15345 (lengths-list-file "/usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el")
15346@end ignore
14371 15347
14372@noindent 15348@noindent
14373(You may need to change the pathname of the file; the one here worked 15349(You may need to change the pathname of the file; the one here is for
14374with GNU Emacs version 21.0.100. To change the expression, copy it to 15350GNU Emacs version 22.0.100. To change the expression, copy it to
14375the @file{*scratch*} buffer and edit it. 15351the @file{*scratch*} buffer and edit it.
14376 15352
14377@need 1200 15353@need 1200
@@ -14389,10 +15365,10 @@ Then evaluate the @code{lengths-list-file} expression.)
14389 15365
14390@need 1200 15366@need 1200
14391The lengths' list for @file{debug.el} takes less than a second to 15367The lengths' list for @file{debug.el} takes less than a second to
14392produce and looks like this: 15368produce and looks like this in GNY Emacs 22:
14393 15369
14394@smallexample 15370@smallexample
14395(77 95 85 87 131 89 50 25 44 44 68 35 64 45 17 34 167 457) 15371(83 113 105 144 289 22 30 97 48 89 25 52 52 88 28 29 77 49 43 290 232 587)
14396@end smallexample 15372@end smallexample
14397 15373
14398@need 1500 15374@need 1500
@@ -14403,7 +15379,7 @@ took seven seconds to produce and looked like this:
14403(75 41 80 62 20 45 44 68 45 12 34 235) 15379(75 41 80 62 20 45 44 68 45 12 34 235)
14404@end smallexample 15380@end smallexample
14405 15381
14406(The newer version of @file{debug.el} contains more defuns than the 15382(The newer version of @file{debug.el} contains more defuns than the
14407earlier one; and my new machine is much faster than the old one.) 15383earlier one; and my new machine is much faster than the old one.)
14408 15384
14409Note that the length of the last definition in the file is first in 15385Note that the length of the last definition in the file is first in
@@ -14421,8 +15397,8 @@ Working on each of a list of files is a repetitious act, so we can use
14421either a @code{while} loop or recursion. 15397either a @code{while} loop or recursion.
14422 15398
14423@menu 15399@menu
14424* lengths-list-many-files:: Return a list of the lengths of defuns. 15400* lengths-list-many-files::
14425* append:: Attach one list to another. 15401* append::
14426@end menu 15402@end menu
14427 15403
14428@node lengths-list-many-files, append, Several files, Several files 15404@node lengths-list-many-files, append, Several files, Several files
@@ -14499,11 +15475,19 @@ These considerations lead us directly to the function itself:
14499name to the absolute, long, path name form of the directory in which 15475name to the absolute, long, path name form of the directory in which
14500the function is called. 15476the function is called.
14501 15477
14502@c !!! 21.0.100 lisp sources location here 15478@ignore
154792006 Oct 29
15480For Bob, as root,
15481 gunzip /usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el.gz
15482In GNU Emacs 22, eval
15483(expand-file-name "/usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el")
15484@end ignore
15485
15486@c !!! 22.0.100 lisp sources location here
14503@need 1500 15487@need 1500
14504Thus, if @code{expand-file-name} is called on @code{debug.el} when 15488Thus, if @code{expand-file-name} is called on @code{debug.el} when
14505Emacs is visiting the 15489Emacs is visiting the
14506@file{/usr/local/share/emacs/21.0.100/lisp/emacs-lisp/} directory, 15490@file{/usr/local/share/emacs/22.0.100/lisp/emacs-lisp/} directory,
14507 15491
14508@smallexample 15492@smallexample
14509debug.el 15493debug.el
@@ -14513,9 +15497,9 @@ debug.el
14513@noindent 15497@noindent
14514becomes 15498becomes
14515 15499
14516@c !!! 21.0.100 lisp sources location here 15500@c !!! 22.0.100 lisp sources location here
14517@smallexample 15501@smallexample
14518/usr/local/share/emacs/21.0.100/lisp/emacs-lisp/debug.el 15502/usr/local/share/emacs/22.0.100/lisp/emacs-lisp/debug.el
14519@end smallexample 15503@end smallexample
14520 15504
14521The only other new element of this function definition is the as yet 15505The only other new element of this function definition is the as yet
@@ -14604,34 +15588,69 @@ in their customary places. To change the expressions, copy them to
14604the @file{*scratch*} buffer, edit them, and then evaluate them. 15588the @file{*scratch*} buffer, edit them, and then evaluate them.
14605 15589
14606The results are shown after the @samp{@result{}}. (These results are 15590The results are shown after the @samp{@result{}}. (These results are
14607for files from Emacs Version 21.0.100; files from other versions of 15591for files from Emacs Version 22.0.100; files from other versions of
14608Emacs may produce different results.) 15592Emacs may produce different results.)
14609 15593
14610@c !!! 21.0.100 lisp sources location here 15594@ignore
155952006 Oct 29
15596For Bob, as root,
15597 gunzip /usr/local/share/emacs/22.0.50/lisp/macros.el.gz
15598In GNU Emacs 22, eval
15599 (lengths-list-file "/usr/local/share/emacs/22.0.50/lisp/macros.el")
15600@end ignore
15601
15602@c !!! 22.0.100 lisp sources location here
14611@smallexample 15603@smallexample
14612@group 15604@group
14613(cd "/usr/local/share/emacs/21.0.100/") 15605(cd "/usr/local/share/emacs/22.0.100/")
14614 15606
14615(lengths-list-file "./lisp/macros.el") 15607(lengths-list-file "./lisp/macros.el")
14616 @result{} (273 263 456 90) 15608 @result{} (283 263 480 90)
14617@end group 15609@end group
14618 15610
15611@ignore
156122006 Oct 29
15613For Bob, as root,
15614 gunzip /usr/local/share/emacs/22.0.50/lisp/mail/mailalias.el.gz
15615In GNU Emacs 22, eval
15616 (lengths-list-file "/usr/local/share/emacs/22.0.50/lisp/mail/mailalias.el")
15617@end ignore
15618
14619@group 15619@group
14620(lengths-list-file "./lisp/mail/mailalias.el") 15620(lengths-list-file "./lisp/mail/mailalias.el")
14621 @result{} (38 32 26 77 174 180 321 198 324) 15621 @result{} (38 32 29 95 178 180 321 218 324)
14622@end group 15622@end group
14623 15623
15624@ignore
156252006 Oct 29
15626For Bob, as root,
15627 gunzip /usr/local/share/emacs/22.0.50/lisp/makesum.el
15628In GNU Emacs 22, eval
15629 (lengths-list-file "/usr/local/share/emacs/22.0.50/lisp/makesum.el")
15630@end ignore
15631
14624@group 15632@group
14625(lengths-list-file "./lisp/makesum.el") 15633(lengths-list-file "./lisp/makesum.el")
14626 @result{} (85 181) 15634 @result{} (85 181)
14627@end group 15635@end group
14628 15636
15637@ignore
156382006 Oct 29
15639In GNU Emacs 22, eval
15640(progn
15641 (cd "/usr/local/share/emacs/22.0.50/")
15642 (recursive-lengths-list-many-files
15643 '("./lisp/macros.el"
15644 "./lisp/mail/mailalias.el"
15645 "./lisp/makesum.el")))
15646@end ignore
15647
14629@group 15648@group
14630(recursive-lengths-list-many-files 15649 (recursive-lengths-list-many-files
14631 '("./lisp/macros.el" 15650 '("./lisp/macros.el"
14632 "./lisp/mail/mailalias.el" 15651 "./lisp/mail/mailalias.el"
14633 "./lisp/makesum.el")) 15652 "./lisp/makesum.el"))
14634 @result{} (273 263 456 90 38 32 26 77 174 180 321 198 324 85 181) 15653 @result{} (283 263 480 90 38 32 29 95 178 180 321 218 324 85 181)
14635@end group 15654@end group
14636@end smallexample 15655@end smallexample
14637 15656
@@ -14671,8 +15690,8 @@ number, and thereby determine the largest and smallest length range
14671that we will need. 15690that we will need.
14672 15691
14673@menu 15692@menu
14674* Sorting:: Sorting lists. 15693* Sorting::
14675* Files List:: Making a list of files. 15694* Files List::
14676* Counting function definitions:: 15695* Counting function definitions::
14677@end menu 15696@end menu
14678 15697
@@ -14717,13 +15736,27 @@ Sorting the list returned by the
14717@code{recursive-lengths-list-many-files} function is straightforward; 15736@code{recursive-lengths-list-many-files} function is straightforward;
14718it uses the @code{<} function: 15737it uses the @code{<} function:
14719 15738
15739@ignore
157402006 Oct 29
15741In GNU Emacs 22, eval
15742(progn
15743 (cd "/usr/local/share/emacs/22.0.50/")
15744 (sort
15745 (recursive-lengths-list-many-files
15746 '("./lisp/macros.el"
15747 "./lisp/mail/mailalias.el"
15748 "./lisp/makesum.el"))
15749 '<))
15750
15751@end ignore
15752
14720@smallexample 15753@smallexample
14721@group 15754@group
14722(sort 15755(sort
14723 (recursive-lengths-list-many-files 15756 (recursive-lengths-list-many-files
14724 '("../lisp/macros.el" 15757 '("./lisp/macros.el"
14725 "../lisp/mailalias.el" 15758 "./lisp/mailalias.el"
14726 "../lisp/makesum.el")) 15759 "./lisp/makesum.el"))
14727 '<) 15760 '<)
14728@end group 15761@end group
14729@end smallexample 15762@end smallexample
@@ -14733,7 +15766,7 @@ it uses the @code{<} function:
14733which produces: 15766which produces:
14734 15767
14735@smallexample 15768@smallexample
14736(85 86 116 122 154 176 179 265) 15769(29 32 38 85 90 95 178 180 181 218 263 283 321 324 480)
14737@end smallexample 15770@end smallexample
14738 15771
14739@noindent 15772@noindent
@@ -14779,9 +15812,9 @@ as a list that looks like this (but with more elements):
14779 15812
14780@smallexample 15813@smallexample
14781@group 15814@group
14782("../lisp/macros.el" 15815("./lisp/macros.el"
14783 "../lisp/mail/rmail.el" 15816 "./lisp/mail/rmail.el"
14784 "../lisp/makesum.el") 15817 "./lisp/makesum.el")
14785@end group 15818@end group
14786@end smallexample 15819@end smallexample
14787 15820
@@ -14793,55 +15826,76 @@ say, a file with the special attributes of a directory. The second
14793element of the list is @code{t} for a directory, a string 15826element of the list is @code{t} for a directory, a string
14794for symbolic link (the string is the name linked to), or @code{nil}. 15827for symbolic link (the string is the name linked to), or @code{nil}.
14795 15828
15829@ignore
15830(directory-files-and-attributes "/usr/local/src/emacs") -->
15831(... ("lisp" t 21 1000 100 (17733 259) (17732 36064) (17732 36064) 12288
15832"drwxr-xr-x" nil 2971606 773))
15833@end ignore
15834
15835
14796For example, the first @samp{.el} file in the @file{lisp/} directory 15836For example, the first @samp{.el} file in the @file{lisp/} directory
14797is @file{abbrev.el}. Its name is 15837is @file{abbrev.el}. Its name is
14798@file{/usr/local/share/emacs/21.0.100/lisp/abbrev.el} and it is not a 15838@file{/usr/local/share/emacs/22.0.100/lisp/abbrev.el} and it is not a
14799directory or a symbolic link. 15839directory or a symbolic link.
14800 15840
14801@need 1000 15841@need 1000
14802This is how @code{directory-files-and-attributes} lists that file and 15842This is how @code{directory-files-and-attributes} lists that file and
14803its attributes: 15843its attributes:
14804 15844
15845@ignore
15846(directory-files-and-attributes "/usr/local/src/emacs/lisp") -->
15847(... ("abbrev.el" nil 1 1000 100 (17733 259) (17491 28834) (17596 62124)
1584813157 "-rw-r--r--" nil 2971624 773) ...)
15849@end ignore
15850
14805@smallexample 15851@smallexample
14806@group 15852@group
14807("/usr/local/share/emacs/21.0.100/lisp/abbrev.el" 15853("abbrev.el"
14808nil 15854nil
148091 158551
148101000 158561000
14811100 15857100
14812@end group 15858@end group
14813@group 15859@group
14814(15019 32380) 15860(17733 259)
14815(14883 48041) 15861(17491 28834)
14816(15214 49336) 15862(17596 62124)
1481711583 1586313157
14818"-rw-rw-r--" 15864"-rw-rw-r--"
14819@end group 15865@end group
14820@group 15866@group
14821t 15867nil
14822341385 158682971624
14823776) 15869773)
14824@end group 15870@end group
14825@end smallexample 15871@end smallexample
14826 15872
15873@ignore
15874(directory-files-and-attributes "/usr/local/src/emacs/lisp/mail/") -->
15875(("." t 3 1000 100 (17733 1296) (17718 8756) (17718 8756) 4096 "drwxr-xr-x" nil 1273504 773))
15876
15877(directory-files-and-attributes "/usr/local/src/emacs/lisp/") -->
15878(... ("mail" t 3 1000 100 (17733 1296) (17718 8756) (17718 8756) 4096
15879"drwxr-xr-x" nil 1273504 773) ...)
15880@end ignore
15881
14827@need 1200 15882@need 1200
14828On the other hand, @file{mail/} is a directory within the @file{lisp/} 15883On the other hand, @file{mail/} is a directory within the @file{lisp/}
14829directory. The beginning of its listing looks like this: 15884directory. The beginning of its listing looks like this:
14830 15885
14831@smallexample 15886@smallexample
14832@group 15887@group
14833("/usr/local/share/emacs/21.0.100/lisp/mail" 15888("mail"
14834t 15889t
14835@dots{} 15890@dots{}
14836) 15891)
14837@end group 15892@end group
14838@end smallexample 15893@end smallexample
14839 15894
14840(Look at the documentation of @code{file-attributes} to learn about 15895(To learn about the different attributes, look at the documentation of
14841the different attributes. Bear in mind that the 15896@code{file-attributes}. Bear in mind that the @code{file-attributes}
14842@code{file-attributes} function does not list the filename, so its 15897function does not list the filename, so its first element is
14843first element is @code{directory-files-and-attributes}'s second 15898@code{directory-files-and-attributes}'s second element.)
14844element.)
14845 15899
14846We will want our new function, @code{files-in-below-directory}, to 15900We will want our new function, @code{files-in-below-directory}, to
14847list the @samp{.el} files in the directory it is told to check, and in 15901list the @samp{.el} files in the directory it is told to check, and in
@@ -14893,11 +15947,14 @@ pattern is `accumulate'
14893using @code{append} as the combiner. 15947using @code{append} as the combiner.
14894 15948
14895@ignore 15949@ignore
14896(directory-files "/usr/local/share/emacs/21.0.100/lisp/" t "\\.el$") 15950(directory-files "/usr/local/src/emacs/lisp/" t "\\.el$")
14897(shell-command "find /usr/local/share/emacs/21.0.100/lisp/ -name '*.el'") 15951(shell-command "find /usr/local/src/emacs/lisp/ -name '*.el'")
15952
15953(directory-files "/usr/local/share/emacs/22.0.100/lisp/" t "\\.el$")
15954(shell-command "find /usr/local/share/emacs/22.0.100/lisp/ -name '*.el'")
14898@end ignore 15955@end ignore
14899 15956
14900@c /usr/local/share/emacs/21.0.100/lisp/ 15957@c /usr/local/share/emacs/22.0.100/lisp/
14901 15958
14902@need 800 15959@need 800
14903Here is the function: 15960Here is the function:
@@ -14909,7 +15966,7 @@ Here is the function:
14909 ;; Although the function will be used non-interactively, 15966 ;; Although the function will be used non-interactively,
14910 ;; it will be easier to test if we make it interactive. 15967 ;; it will be easier to test if we make it interactive.
14911 ;; The directory will have a name such as 15968 ;; The directory will have a name such as
14912 ;; "/usr/local/share/emacs/21.0.100/lisp/" 15969 ;; "/usr/local/share/emacs/22.0.100/lisp/"
14913 (interactive "DDirectory name: ") 15970 (interactive "DDirectory name: ")
14914@end group 15971@end group
14915@group 15972@group
@@ -14953,7 +16010,8 @@ Here is the function:
14953@end group 16010@end group
14954@end smallexample 16011@end smallexample
14955 16012
14956@c (files-in-below-directory "/usr/local/share/emacs/21.0.100/lisp/") 16013@c (files-in-below-directory "/usr/local/src/emacs/lisp/")
16014@c (files-in-below-directory "/usr/local/share/emacs/22.0.100/lisp/")
14957 16015
14958The @code{files-in-below-directory} @code{directory-files} function 16016The @code{files-in-below-directory} @code{directory-files} function
14959takes one argument, the name of a directory. 16017takes one argument, the name of a directory.
@@ -14961,17 +16019,19 @@ takes one argument, the name of a directory.
14961@need 1250 16019@need 1250
14962Thus, on my system, 16020Thus, on my system,
14963 16021
14964@c !!! 21.0.100 lisp sources location here 16022@c (length (files-in-below-directory "/usr/local/src/emacs/lisp/"))
16023
16024@c !!! 22.0.100 lisp sources location here
14965@smallexample 16025@smallexample
14966@group 16026@group
14967(length 16027(length
14968 (files-in-below-directory "/usr/local/share/emacs/21.0.100/lisp/")) 16028 (files-in-below-directory "/usr/local/share/emacs/22.0.100/lisp/"))
14969@end group 16029@end group
14970@end smallexample 16030@end smallexample
14971 16031
14972@noindent 16032@noindent
14973tells me that my version 21.0.100 Lisp sources directory contains 754 16033tells me that my Lisp sources directory contains 1031 @samp{.el}
14974@samp{.el} files. 16034files.
14975 16035
14976@code{files-in-below-directory} returns a list in reverse alphabetical 16036@code{files-in-below-directory} returns a list in reverse alphabetical
14977order. An expression to sort the list in alphabetical order looks 16037order. An expression to sort the list in alphabetical order looks
@@ -14980,24 +16040,21 @@ like this:
14980@smallexample 16040@smallexample
14981@group 16041@group
14982(sort 16042(sort
14983 (files-in-below-directory "/usr/local/share/emacs/21.0.100/lisp/") 16043 (files-in-below-directory "/usr/local/share/emacs/22.0.100/lisp/")
14984 'string-lessp) 16044 'string-lessp)
14985@end group 16045@end group
14986@end smallexample 16046@end smallexample
14987 16047
14988@ignore 16048@ignore
14989(defun test () 16049(defun test ()
14990 "Test how long it takes to find lengths of all elisp defuns." 16050 "Test how long it takes to find lengths of all sorted elisp defuns."
14991 (insert "\n" (current-time-string) "\n") 16051 (insert "\n" (current-time-string) "\n")
14992 (sit-for 0) 16052 (sit-for 0)
14993 (sort 16053 (sort
14994 (recursive-lengths-list-many-files 16054 (recursive-lengths-list-many-files
14995 '("../lisp/macros.el" 16055 (files-in-below-directory "/usr/local/src/emacs/lisp/"))
14996 "../lisp/mailalias.el"
14997 "../lisp/makesum.el"))
14998 '<) 16056 '<)
14999 (insert (format "%s" (current-time-string)))) 16057 (insert (format "%s" (current-time-string))))
15000
15001@end ignore 16058@end ignore
15002 16059
15003@node Counting function definitions, , Files List, Prepare the data 16060@node Counting function definitions, , Files List, Prepare the data
@@ -15259,11 +16316,14 @@ of the @code{and} expression.
15259@c colon in printed section title causes problem in Info cross reference 16316@c colon in printed section title causes problem in Info cross reference
15260This way, we avoid an error. 16317This way, we avoid an error.
15261@iftex 16318@iftex
15262@xref{forward-paragraph, , @code{forward-paragraph}: a Goldmine of 16319@noindent
15263Functions}, for more information about @code{and}. 16320(For information about @code{and}, see
16321@ref{kill-new function, , The @code{kill-new} function}.)
15264@end iftex 16322@end iftex
15265@ifinfo 16323@ifinfo
15266@xref{forward-paragraph}, for more information about @code{and}. 16324@noindent
16325(@xref{kill-new function, , The @code{kill-new} function}, for
16326information about @code{and}.)
15267@end ifinfo 16327@end ifinfo
15268 16328
15269Here is a short test of the @code{defuns-per-range} function. First, 16329Here is a short test of the @code{defuns-per-range} function. First,
@@ -15301,7 +16361,6 @@ between 120 and 129, and so on. There are four elements with a value
15301of 200 or larger. 16361of 200 or larger.
15302 16362
15303@c The next step is to turn this numbers' list into a graph. 16363@c The next step is to turn this numbers' list into a graph.
15304
15305@node Readying a Graph, Emacs Initialization, Words in a defun, Top 16364@node Readying a Graph, Emacs Initialization, Words in a defun, Top
15306@chapter Readying a Graph 16365@chapter Readying a Graph
15307@cindex Readying a graph 16366@cindex Readying a graph
@@ -15328,7 +16387,7 @@ the function to label the axes automatically.
15328 16387
15329@menu 16388@menu
15330* Columns of a graph:: 16389* Columns of a graph::
15331* graph-body-print:: How to print the body of a graph. 16390* graph-body-print::
15332* recursive-graph-body-print:: 16391* recursive-graph-body-print::
15333* Printed Axes:: 16392* Printed Axes::
15334* Line Graph Exercise:: 16393* Line Graph Exercise::
@@ -15363,7 +16422,7 @@ line by line, by typing. We have two routes we can follow: write our
15363own column-insertion function or discover whether one exists in Emacs. 16422own column-insertion function or discover whether one exists in Emacs.
15364 16423
15365To see whether there is one in Emacs, we can use the @kbd{M-x apropos} 16424To see whether there is one in Emacs, we can use the @kbd{M-x apropos}
15366command. This command is like the @kbd{C-h a} (command-apropos) 16425command. This command is like the @kbd{C-h a} (@code{command-apropos})
15367command, except that the latter finds only those functions that are 16426command, except that the latter finds only those functions that are
15368commands. The @kbd{M-x apropos} command lists all symbols that match 16427commands. The @kbd{M-x apropos} command lists all symbols that match
15369a regular expression, including functions that are not interactive. 16428a regular expression, including functions that are not interactive.
@@ -15374,9 +16433,11 @@ columns. Very likely, the name of the function will contain either
15374the word `print' or the word `insert' or the word `column'. 16433the word `print' or the word `insert' or the word `column'.
15375Therefore, we can simply type @kbd{M-x apropos RET 16434Therefore, we can simply type @kbd{M-x apropos RET
15376print\|insert\|column RET} and look at the result. On my system, this 16435print\|insert\|column RET} and look at the result. On my system, this
15377command takes quite some time, and then produces a list of 79 16436command once too takes quite some time, and then produced a list of 79
15378functions and variables. Scanning down the list, the only function 16437functions and variables. Now it does not take much time at all and
15379that looks as if it might do the job is @code{insert-rectangle}. 16438produces a list of 211 functions and variables. Scanning down the
16439list, the only function that looks as if it might do the job is
16440@code{insert-rectangle}.
15380 16441
15381@need 1200 16442@need 1200
15382Indeed, this is the function we want; its documentation says: 16443Indeed, this is the function we want; its documentation says:
@@ -15388,6 +16449,8 @@ Insert text of RECTANGLE with upper left corner at point.
15388RECTANGLE's first line is inserted at point, 16449RECTANGLE's first line is inserted at point,
15389its second line is inserted at a point vertically under point, etc. 16450its second line is inserted at a point vertically under point, etc.
15390RECTANGLE should be a list of strings. 16451RECTANGLE should be a list of strings.
16452After this command, the mark is at the upper left corner
16453and point is at the lower right corner.
15391@end group 16454@end group
15392@end smallexample 16455@end smallexample
15393 16456
@@ -15403,8 +16466,7 @@ point. Also the function returns @code{nil}.
15403@group 16466@group
15404(insert-rectangle '("first" "second" "third"))first 16467(insert-rectangle '("first" "second" "third"))first
15405 second 16468 second
15406 third 16469 thirdnil
15407nil
15408@end group 16470@end group
15409@end smallexample 16471@end smallexample
15410 16472
@@ -15418,12 +16480,14 @@ column of strings.
15418 16480
15419If you are reading this in Info, you can see how this works by 16481If you are reading this in Info, you can see how this works by
15420switching to another buffer, such as the @file{*scratch*} buffer, 16482switching to another buffer, such as the @file{*scratch*} buffer,
15421placing point somewhere in the buffer, typing @kbd{M-:}, 16483placing point somewhere in the buffer, typing @kbd{M-:}, typing the
15422typing the @code{insert-rectangle} expression into the minibuffer at 16484@code{insert-rectangle} expression into the minibuffer at the prompt,
15423the prompt, and then typing @key{RET}. This causes Emacs to evaluate 16485and then typing @key{RET}. This causes Emacs to evaluate the
15424the expression in the minibuffer, but to use as the value of point the 16486expression in the minibuffer, but to use as the value of point the
15425position of point in the @file{*scratch*} buffer. (@kbd{M-:} 16487position of point in the @file{*scratch*} buffer. (@kbd{M-:} is the
15426is the keybinding for @code{eval-expression}.) 16488keybinding for @code{eval-expression}. Also, @code{nil} does not
16489appear in the @file{*scratch*} buffer since the expression is
16490evaluated in the minibuffer.)
15427 16491
15428We find when we do this that point ends up at the end of the last 16492We find when we do this that point ends up at the end of the last
15429inserted line---that is to say, this function moves point as a 16493inserted line---that is to say, this function moves point as a
@@ -15983,20 +17047,20 @@ expressions in Emacs Lisp you can change or extend Emacs.
15983 17047
15984@menu 17048@menu
15985* Default Configuration:: 17049* Default Configuration::
15986* Site-wide Init:: You can write site-wide init files. 17050* Site-wide Init::
15987* defcustom:: Emacs will write code for you. 17051* defcustom::
15988* Beginning a .emacs File:: How to write a @code{.emacs file}. 17052* Beginning a .emacs File::
15989* Text and Auto-fill:: Automatically wrap lines. 17053* Text and Auto-fill::
15990* Mail Aliases:: Use abbreviations for email addresses. 17054* Mail Aliases::
15991* Indent Tabs Mode:: Don't use tabs with @TeX{} 17055* Indent Tabs Mode::
15992* Keybindings:: Create some personal keybindings. 17056* Keybindings::
15993* Keymaps:: More about key binding. 17057* Keymaps::
15994* Loading Files:: Load (i.e., evaluate) files automatically. 17058* Loading Files::
15995* Autoload:: Make functions available. 17059* Autoload::
15996* Simple Extension:: Define a function; bind it to a key. 17060* Simple Extension::
15997* X11 Colors:: Colors in version 19 in X. 17061* X11 Colors::
15998* Miscellaneous:: 17062* Miscellaneous::
15999* Mode Line:: How to customize your mode line. 17063* Mode Line::
16000@end menu 17064@end menu
16001 17065
16002@node Default Configuration, Site-wide Init, Emacs Initialization, Emacs Initialization 17066@node Default Configuration, Site-wide Init, Emacs Initialization, Emacs Initialization
@@ -16011,7 +17075,11 @@ Fundamental mode when you edit an unadorned file. This all makes
16011sense, if you do not know who is going to use Emacs. Who knows what a 17075sense, if you do not know who is going to use Emacs. Who knows what a
16012person hopes to do with an unadorned file? Fundamental mode is the 17076person hopes to do with an unadorned file? Fundamental mode is the
16013right default for such a file, just as C mode is the right default for 17077right default for such a file, just as C mode is the right default for
16014editing C code. But when you do know who is going to use Emacs---you, 17078editing C code. (Enough programming languages have syntaxes
17079that enable them to share or nearly share features, so C mode is
17080now provided by by CC mode, the `C Collection'.)
17081
17082But when you do know who is going to use Emacs---you,
16015yourself---then it makes sense to customize Emacs. 17083yourself---then it makes sense to customize Emacs.
16016 17084
16017For example, I seldom want Fundamental mode when I edit an 17085For example, I seldom want Fundamental mode when I edit an
@@ -16169,8 +17237,8 @@ M-x customize
16169@noindent 17237@noindent
16170and find that the group for editing files of data is called `data'. 17238and find that the group for editing files of data is called `data'.
16171Enter that group. Text Mode Hook is the first member. You can click 17239Enter that group. Text Mode Hook is the first member. You can click
16172on its various options to set the values. After you click on the 17240on its various options, such as @code{turn-on-auto-fill}, to set the
16173button to 17241values. After you click on the button to
16174 17242
16175@smallexample 17243@smallexample
16176Save for Future Sessions 17244Save for Future Sessions
@@ -16183,24 +17251,22 @@ It will look like this:
16183@smallexample 17251@smallexample
16184@group 17252@group
16185(custom-set-variables 17253(custom-set-variables
16186 ;; custom-set-variables was added by Custom -- 17254 ;; custom-set-variables was added by Custom.
16187 ;; don't edit or cut/paste it! 17255 ;; If you edit it by hand, you could mess it up, so be careful.
16188 ;; Your init file should contain only one such instance. 17256 ;; Your init file should contain only one such instance.
17257 ;; If there is more than one, they won't work right.
16189 '(text-mode-hook (quote (turn-on-auto-fill text-mode-hook-identify)))) 17258 '(text-mode-hook (quote (turn-on-auto-fill text-mode-hook-identify))))
16190@end group 17259@end group
16191@end smallexample 17260@end smallexample
16192 17261
16193@noindent 17262@noindent
16194(The @code{text-mode-hook-identify} function tells 17263(The @code{text-mode-hook-identify} function tells
16195@code{toggle-text-mode-auto-fill} which buffers are in Text mode.) 17264@code{toggle-text-mode-auto-fill} which buffers are in Text mode.
16196 17265It comes on automatically.
16197In spite of the warning, you certainly may edit, cut, and paste the 17266)
16198expression! I do all time. The purpose of the warning is to scare
16199those who do not know what they are doing, so they do not
16200inadvertently generate an error.
16201 17267
16202The @code{custom-set-variables} function works somewhat differently 17268The @code{custom-set-variables} function works somewhat differently
16203than a @code{setq}. While I have never learned the differences, I do 17269than a @code{setq}. While I have never learned the differences, I
16204modify the @code{custom-set-variables} expressions in my @file{.emacs} 17270modify the @code{custom-set-variables} expressions in my @file{.emacs}
16205file by hand: I make the changes in what appears to me to be a 17271file by hand: I make the changes in what appears to me to be a
16206reasonable manner and have not had any problems. Others prefer to use 17272reasonable manner and have not had any problems. Others prefer to use
@@ -16221,7 +17287,7 @@ When you do this, and later use @code{customize}, you will see a
16221message that says 17287message that says
16222 17288
16223@smallexample 17289@smallexample
16224this option has been changed outside the customize buffer. 17290CHANGED outside Customize; operating on it here may be unreliable.
16225@end smallexample 17291@end smallexample
16226 17292
16227@need 800 17293@need 800
@@ -16338,12 +17404,11 @@ Auto Fill mode.
16338@smallexample 17404@smallexample
16339@group 17405@group
16340;;; Text mode and Auto Fill mode 17406;;; Text mode and Auto Fill mode
16341; The next three lines put Emacs into Text mode 17407; The next two lines put Emacs into Text mode
16342; and Auto Fill mode, and are for writers who 17408; and Auto Fill mode, and are for writers who
16343; want to start writing prose rather than code. 17409; want to start writing prose rather than code.
16344 17410
16345(setq default-major-mode 'text-mode) 17411(setq default-major-mode 'text-mode)
16346(add-hook 'text-mode-hook 'text-mode-hook-identify)
16347(add-hook 'text-mode-hook 'turn-on-auto-fill) 17412(add-hook 'text-mode-hook 'turn-on-auto-fill)
16348@end group 17413@end group
16349@end smallexample 17414@end smallexample
@@ -16404,33 +17469,28 @@ a value in your @file{.emacs} file and the procedure you use anywhere
16404else in Emacs. 17469else in Emacs.
16405 17470
16406@need 800 17471@need 800
16407Here are the next two lines: 17472Here is the next line:
16408 17473
16409@cindex Auto Fill mode turned on 17474@cindex Auto Fill mode turned on
16410@findex add-hook 17475@findex add-hook
16411@smallexample 17476@smallexample
16412(add-hook 'text-mode-hook 'text-mode-hook-identify)
16413(add-hook 'text-mode-hook 'turn-on-auto-fill) 17477(add-hook 'text-mode-hook 'turn-on-auto-fill)
16414@end smallexample 17478@end smallexample
16415 17479
16416@noindent 17480@noindent
16417In these two lines, the @code{add-hook} command first adds 17481In this line, the @code{add-hook} command adds
16418@code{text-mode-hook-identify} to the variable called 17482@code{turn-on-auto-fill} to the variable.
16419@code{text-mode-hook} and then adds @code{turn-on-auto-fill} to the
16420variable.
16421 17483
16422@code{turn-on-auto-fill} is the name of a program, that, you guessed 17484@code{turn-on-auto-fill} is the name of a program, that, you guessed
16423it!, turns on Auto Fill mode. @code{text-mode-hook-identify} is a 17485it!, turns on Auto Fill mode.
16424function that tells @code{toggle-text-mode-auto-fill} which buffers
16425are in Text mode.
16426 17486
16427Every time Emacs turns on Text mode, Emacs runs the commands `hooked' 17487Every time Emacs turns on Text mode, Emacs runs the commands `hooked'
16428onto Text mode. So every time Emacs turns on Text mode, Emacs also 17488onto Text mode. So every time Emacs turns on Text mode, Emacs also
16429turns on Auto Fill mode. 17489turns on Auto Fill mode.
16430 17490
16431In brief, the first line causes Emacs to enter Text mode when you edit 17491In brief, the first line causes Emacs to enter Text mode when you edit a
16432a file, unless the file name extension, first non-blank line, or local 17492file, unless the file name extension, a first non-blank line, or local
16433variables tell Emacs otherwise. 17493variables to tell Emacs otherwise.
16434 17494
16435Text mode among other actions, sets the syntax table to work 17495Text mode among other actions, sets the syntax table to work
16436conveniently for writers. In Text mode, Emacs considers an apostrophe 17496conveniently for writers. In Text mode, Emacs considers an apostrophe
@@ -16439,11 +17499,10 @@ or a space as part of a word. Thus, @kbd{M-f} moves you over
16439@samp{it's}. On the other hand, in C mode, @kbd{M-f} stops just after 17499@samp{it's}. On the other hand, in C mode, @kbd{M-f} stops just after
16440the @samp{t} of @samp{it's}. 17500the @samp{t} of @samp{it's}.
16441 17501
16442The second and third lines causes Emacs to turn on Auto Fill mode when 17502The second line causes Emacs to turn on Auto Fill mode when it turns
16443it turns on Text mode. In Auto Fill mode, Emacs automatically breaks 17503on Text mode. In Auto Fill mode, Emacs automatically breaks a line
16444a line that is too wide and brings the excessively wide part of the 17504that is too wide and brings the excessively wide part of the line down
16445line down to the next line. Emacs breaks lines between words, not 17505to the next line. Emacs breaks lines between words, not within them.
16446within them.
16447 17506
16448When Auto Fill mode is turned off, lines continue to the right as you 17507When Auto Fill mode is turned off, lines continue to the right as you
16449type them. Depending on how you set the value of 17508type them. Depending on how you set the value of
@@ -16785,7 +17844,10 @@ Incidentally, @code{load-library} is an interactive interface to the
16785(defun load-library (library) 17844(defun load-library (library)
16786 "Load the library named LIBRARY. 17845 "Load the library named LIBRARY.
16787This is an interface to the function `load'." 17846This is an interface to the function `load'."
16788 (interactive "sLoad library: ") 17847 (interactive
17848 (list (completing-read "Load library: "
17849 'locate-file-completion
17850 (cons load-path (get-load-suffixes)))))
16789 (load library)) 17851 (load library))
16790@end group 17852@end group
16791@end smallexample 17853@end smallexample
@@ -16822,12 +17884,12 @@ come to use a `rare' function frequently. When you do, you should
16822load that function's file with a @code{load} expression in your 17884load that function's file with a @code{load} expression in your
16823@file{.emacs} file. 17885@file{.emacs} file.
16824 17886
16825In my @file{.emacs} file for Emacs version 21, I load 12 libraries 17887In my @file{.emacs} file for Emacs version 22, I load 14 libraries
16826that contain functions that would otherwise be autoloaded. (Actually, 17888that contain functions that would otherwise be autoloaded. (Actually,
16827it would have been better to include these files in my `dumped' Emacs 17889it would have been better to include these files in my `dumped' Emacs,
16828when I built it, but I forgot. @xref{Building Emacs, , Building 17890but I forgot. @xref{Building Emacs, , Building Emacs, elisp, The GNU
16829Emacs, elisp, The GNU Emacs Lisp Reference Manual}, and the @file{INSTALL} 17891Emacs Lisp Reference Manual}, and the @file{INSTALL} file for more
16830file for more about dumping.) 17892about dumping.)
16831 17893
16832You may also want to include autoloaded expressions in your @file{.emacs} 17894You may also want to include autoloaded expressions in your @file{.emacs}
16833file. @code{autoload} is a built-in function that takes up to five 17895file. @code{autoload} is a built-in function that takes up to five
@@ -16916,18 +17978,18 @@ Your Init File, emacs, The GNU Emacs Manual}.
16916@cindex Conditional 'twixt two versions of Emacs 17978@cindex Conditional 'twixt two versions of Emacs
16917@cindex Version of Emacs, choosing 17979@cindex Version of Emacs, choosing
16918@cindex Emacs version, choosing 17980@cindex Emacs version, choosing
16919If you run two versions of GNU Emacs, such as versions 20 and 21, and 17981If you run two versions of GNU Emacs, such as versions 21 and 22, and
16920use one @file{.emacs} file, you can select which code to evaluate with 17982use one @file{.emacs} file, you can select which code to evaluate with
16921the following conditional: 17983the following conditional:
16922 17984
16923@smallexample 17985@smallexample
16924@group 17986@group
16925(cond 17987(cond
16926 ((string-equal (number-to-string 20) (substring (emacs-version) 10 12))
16927 ;; evaluate version 20 code
16928 ( @dots{} ))
16929 ((string-equal (number-to-string 21) (substring (emacs-version) 10 12)) 17988 ((string-equal (number-to-string 21) (substring (emacs-version) 10 12))
16930 ;; evaluate version 21 code 17989 ;; evaluate version 21 code
17990 ( @dots{} ))
17991 ((string-equal (number-to-string 22) (substring (emacs-version) 10 12))
17992 ;; evaluate version 22 code
16931 ( @dots{} ))) 17993 ( @dots{} )))
16932@end group 17994@end group
16933@end smallexample 17995@end smallexample
@@ -16940,6 +18002,10 @@ file@footnote{When I start instances of Emacs that do not load my
16940 18002
16941@smallexample 18003@smallexample
16942emacs -q --no-site-file -eval '(blink-cursor-mode nil)' 18004emacs -q --no-site-file -eval '(blink-cursor-mode nil)'
18005
18006@exdent Or nowadays, using an even more sophisticated set of options,
18007
18008emacs -Q - D
16943@end smallexample 18009@end smallexample
16944}: 18010}:
16945 18011
@@ -17055,16 +18121,12 @@ Emacs*pointerColor: white
17055@end smallexample 18121@end smallexample
17056 18122
17057In any event, since it is not part of Emacs, I set the root color of 18123In any event, since it is not part of Emacs, I set the root color of
17058my X window in my @file{~/.xinitrc} file, like this@footnote{I 18124my X window in my @file{~/.xinitrc} file, like this@footnote{I also
17059occasionally run more modern window managers, such as Sawfish with 18125run more modern window managers, such as Enlightenment, Gnome, or KDE;
17060GNOME, Enlightenment, SCWM, or KDE; in those cases, I often specify an 18126in those cases, I often specify an image rather than a plain color.}:
17061image rather than a plain color.}:
17062 18127
17063@smallexample 18128@smallexample
17064@group
17065# I use TWM for window manager.
17066xsetroot -solid Navy -fg white & 18129xsetroot -solid Navy -fg white &
17067@end group
17068@end smallexample 18130@end smallexample
17069 18131
17070@node Miscellaneous, Mode Line, X11 Colors, Emacs Initialization 18132@node Miscellaneous, Mode Line, X11 Colors, Emacs Initialization
@@ -17076,6 +18138,7 @@ Here are a few miscellaneous settings:
17076@itemize @minus 18138@itemize @minus
17077@item 18139@item
17078Set the shape and color of the mouse cursor: 18140Set the shape and color of the mouse cursor:
18141
17079@smallexample 18142@smallexample
17080@group 18143@group
17081; Cursor shapes are defined in 18144; Cursor shapes are defined in
@@ -17099,6 +18162,32 @@ Set the shape and color of the mouse cursor:
17099@end smallexample 18162@end smallexample
17100 18163
17101@item 18164@item
18165Or you can set the values of a variety of features in an alist, like
18166this:
18167
18168@smallexample
18169@group
18170(setq-default
18171 default-frame-alist
18172 '((cursor-color . "white")
18173 (mouse-color . "white")
18174 (foreground-color . "white")
18175 (background-color . "DodgerBlue4")
18176 ;; (cursor-type . bar)
18177 (cursor-type . box)
18178@end group
18179@group
18180 (tool-bar-lines . 0)
18181 (menu-bar-lines . 1)
18182 (width . 80)
18183 (height . 58)
18184 (font .
18185 "-Misc-Fixed-Medium-R-Normal--20-200-75-75-C-100-ISO8859-1")
18186 ))
18187@end group
18188@end smallexample
18189
18190@item
17102Convert @kbd{@key{CTL}-h} into @key{DEL} and @key{DEL} 18191Convert @kbd{@key{CTL}-h} into @key{DEL} and @key{DEL}
17103into @kbd{@key{CTL}-h}.@* 18192into @kbd{@key{CTL}-h}.@*
17104(Some older keyboards needed this, although I have not seen the 18193(Some older keyboards needed this, although I have not seen the
@@ -17123,6 +18212,9 @@ problem recently.)
17123@end group 18212@end group
17124@end smallexample 18213@end smallexample
17125 18214
18215@noindent
18216or start GNU Emacs with the command @code{emacs -nbc}.
18217
17126@item Ignore case when using `grep'@* 18218@item Ignore case when using `grep'@*
17127@samp{-n}@w{ } Prefix each line of output with line number@* 18219@samp{-n}@w{ } Prefix each line of output with line number@*
17128@samp{-i}@w{ } Ignore case distinctions@* 18220@samp{-i}@w{ } Ignore case distinctions@*
@@ -17132,11 +18224,16 @@ problem recently.)
17132(setq grep-command "grep -n -i -e ") 18224(setq grep-command "grep -n -i -e ")
17133@end smallexample 18225@end smallexample
17134 18226
17135@item Automatically uncompress compressed files when visiting them 18227@ignore
18228@c Evidently, no longer needed in GNU Emacs 22
17136 18229
17137@smallexample 18230item Automatically uncompress compressed files when visiting them
18231
18232smallexample
17138(load "uncompress") 18233(load "uncompress")
17139@end smallexample 18234end smallexample
18235
18236@end ignore
17140 18237
17141@item Find an existing buffer, even if it has a different name@* 18238@item Find an existing buffer, even if it has a different name@*
17142This avoids problems with symbolic links. 18239This avoids problems with symbolic links.
@@ -17305,8 +18402,8 @@ the old days, it would have been specified simply as @code{"-"}. But
17305nowadays, Emacs can add properties to a string, such as highlighting 18402nowadays, Emacs can add properties to a string, such as highlighting
17306or, as in this case, a help feature. If you place your mouse cursor 18403or, as in this case, a help feature. If you place your mouse cursor
17307over the hyphen, some help information appears (By default, you must 18404over the hyphen, some help information appears (By default, you must
17308wait one second before the information appears. You can change that 18405wait seven-tenths of a second before the information appears. You can
17309timing by changing the value of @code{tooltip-delay}.) 18406change that timing by changing the value of @code{tooltip-delay}.)
17310 18407
17311@need 1000 18408@need 1000
17312The new string format has a special syntax: 18409The new string format has a special syntax:
@@ -17344,7 +18441,7 @@ this number. (Buffer names can and often should be longer than 12
17344characters; this length works well in a typical 80 column wide 18441characters; this length works well in a typical 80 column wide
17345window.) 18442window.)
17346 18443
17347@code{:eval} is a new feature in GNU Emacs version 21. It says to 18444@code{:eval} was a new feature in GNU Emacs version 21. It says to
17348evaluate the following form and use the result as a string to display. 18445evaluate the following form and use the result as a string to display.
17349In this case, the expression displays the first component of the full 18446In this case, the expression displays the first component of the full
17350system name. The end of the first component is a @samp{.} (`period'), 18447system name. The end of the first component is a @samp{.} (`period'),
@@ -17399,10 +18496,10 @@ Debugging Lisp Programs, elisp, The GNU Emacs Lisp Reference Manual}.
17399In this chapter, I will walk through a short example of each. 18496In this chapter, I will walk through a short example of each.
17400 18497
17401@menu 18498@menu
17402* debug:: How to use the built-in debugger. 18499* debug::
17403* debug-on-entry:: Start debugging when you call a function. 18500* debug-on-entry::
17404* debug-on-quit:: Start debugging when you quit with @kbd{C-g}. 18501* debug-on-quit::
17405* edebug:: How to use Edebug, a source level debugger. 18502* edebug::
17406* Debugging Exercises:: 18503* Debugging Exercises::
17407@end menu 18504@end menu
17408 18505
@@ -17479,6 +18576,7 @@ In practice, for a bug as simple as this, the `Lisp error' line will
17479tell you what you need to know to correct the definition. The 18576tell you what you need to know to correct the definition. The
17480function @code{1=} is `void'. 18577function @code{1=} is `void'.
17481 18578
18579@ignore
17482@need 800 18580@need 800
17483In GNU Emacs 20 and before, you will see: 18581In GNU Emacs 20 and before, you will see:
17484 18582
@@ -17489,11 +18587,12 @@ Symbol's function definition is void:@: 1=
17489@noindent 18587@noindent
17490which has the same meaning as the @file{*Backtrace*} buffer line in 18588which has the same meaning as the @file{*Backtrace*} buffer line in
17491version 21. 18589version 21.
18590@end ignore
17492 18591
17493However, suppose you are not quite certain what is going on? 18592However, suppose you are not quite certain what is going on?
17494You can read the complete backtrace. 18593You can read the complete backtrace.
17495 18594
17496In this case, you need to run GNU Emacs 21, which automatically starts 18595In this case, you need to run GNU Emacs 22, which automatically starts
17497the debugger that puts you in the @file{*Backtrace*} buffer; or else, 18596the debugger that puts you in the @file{*Backtrace*} buffer; or else,
17498you need to start the debugger manually as described below. 18597you need to start the debugger manually as described below.
17499 18598
@@ -17535,14 +18634,18 @@ then run your test again.
17535@section @code{debug-on-entry} 18634@section @code{debug-on-entry}
17536@findex debug-on-entry 18635@findex debug-on-entry
17537 18636
17538GNU Emacs 21 starts the debugger automatically when your function has 18637GNU Emacs 22 starts the debugger automatically when your function has
17539an error. GNU Emacs version 20 and before did not; it simply 18638an error.
18639
18640@ignore
18641GNU Emacs version 20 and before did not; it simply
17540presented you with an error message. You had to start the debugger 18642presented you with an error message. You had to start the debugger
17541manually. 18643manually.
18644@end ignore
17542 18645
17543You can start the debugger manually for all versions of Emacs; the 18646Incidently, you can start the debugger manually for all versions of
17544advantage is that the debugger runs even if you do not have a bug in 18647Emacs; the advantage is that the debugger runs even if you do not have
17545your code. Sometimes your code will be free of bugs! 18648a bug in your code. Sometimes your code will be free of bugs!
17546 18649
17547You can enter the debugger when you call the function by calling 18650You can enter the debugger when you call the function by calling
17548@code{debug-on-entry}. 18651@code{debug-on-entry}.
@@ -17796,13 +18899,13 @@ that value will be displayed in the echo area. For example, after you
17796move point past @code{number}, you will see the following: 18899move point past @code{number}, you will see the following:
17797 18900
17798@smallexample 18901@smallexample
17799Result: 3 = C-c 18902Result: 3 (#o3, #x3, ?\C-c)
17800@end smallexample 18903@end smallexample
17801 18904
17802@noindent 18905@noindent
17803This means the value of @code{number} is 3, which is @sc{ascii} 18906This means the value of @code{number} is 3, which is octal three,
17804`control-c' (the third letter of the alphabet, in case you need to 18907hexadecimal three, and @sc{ascii} `control-c' (the third letter of the
17805know this information). 18908alphabet, in case you need to know this information).
17806 18909
17807You can continue moving through the code until you reach the line with 18910You can continue moving through the code until you reach the line with
17808the error. Before evaluation, that line looks like this: 18911the error. Before evaluation, that line looks like this:
@@ -17872,7 +18975,7 @@ In the Edebug debugging buffer, use the @kbd{p}
17872@code{count-words-region} is working. 18975@code{count-words-region} is working.
17873 18976
17874@item 18977@item
17875Move point to some spot further down function and then type the 18978Move point to some spot further down the function and then type the
17876@kbd{h} (@code{edebug-goto-here}) command to jump to that location. 18979@kbd{h} (@code{edebug-goto-here}) command to jump to that location.
17877 18980
17878@item 18981@item
@@ -17922,8 +19025,8 @@ introduction, it comes as a Texinfo source file, so you can read it
17922on-line and as a typeset, printed book.) 19025on-line and as a typeset, printed book.)
17923 19026
17924Go to the other on-line help that is part of GNU Emacs: the on-line 19027Go to the other on-line help that is part of GNU Emacs: the on-line
17925documentation for all functions, and @code{find-tags}, the program 19028documentation for all functions and variables, and @code{find-tags},
17926that takes you to sources. 19029the program that takes you to sources.
17927 19030
17928Here is an example of how I explore the sources. Because of its name, 19031Here is an example of how I explore the sources. Because of its name,
17929@file{simple.el} is the file I looked at first, a long time ago. As 19032@file{simple.el} is the file I looked at first, a long time ago. As
@@ -17932,21 +19035,16 @@ or at least look complicated at first sight. The @code{open-line}
17932function, for example, looks complicated. 19035function, for example, looks complicated.
17933 19036
17934You may want to walk through this function slowly, as we did with the 19037You may want to walk through this function slowly, as we did with the
17935@code{forward-sentence} function. 19038@code{forward-sentence} function. (@xref{forward-sentence, The
17936@ifnottex 19039@code{forward-sentence} function}.) Or you may want to skip that
17937(@xref{forward-sentence}.) 19040function and look at another, such as @code{split-line}. You don't
17938@end ifnottex 19041need to read all the functions. According to
17939@iftex 19042@code{count-words-in-defun}, the @code{split-line} function contains
17940(@xref{forward-sentence, , @code{forward-sentence}}.) 19043102 words and symbols.
17941@end iftex 19044
17942Or you may want to skip that function and look at another, such as 19045Even though it is short, @code{split-line} contains expressions
17943@code{split-line}. You don't need to read all the functions.
17944According to @code{count-words-in-defun}, the @code{split-line}
17945function contains 27 words and symbols.
17946
17947Even though it is short, @code{split-line} contains four expressions
17948we have not studied: @code{skip-chars-forward}, @code{indent-to}, 19046we have not studied: @code{skip-chars-forward}, @code{indent-to},
17949@code{current-column} and @samp{?\n}. 19047@code{current-column} and @code{insert-and-inherit}.
17950 19048
17951Consider the @code{skip-chars-forward} function. (It is part of the 19049Consider the @code{skip-chars-forward} function. (It is part of the
17952function definition for @code{back-to-indentation}, which is shown in 19050function definition for @code{back-to-indentation}, which is shown in
@@ -17974,25 +19072,17 @@ customize the @code{interactive} expression without using the standard
17974character codes; and it shows how to create a temporary buffer. 19072character codes; and it shows how to create a temporary buffer.
17975 19073
17976(The @code{indent-to} function is written in C rather than Emacs Lisp; 19074(The @code{indent-to} function is written in C rather than Emacs Lisp;
17977it is a `built-in' function. @code{help-follow} only provides you 19075it is a `built-in' function. @code{help-follow} takes you to its
17978with the documentation of a built-in function; it does not take you to 19076source as does @code{find-tag}, when properly set up.)
17979the source. But @code{find-tag} will take you to the source, if
17980properly set up.)
17981 19077
17982You can look at a function's source using @code{find-tag}, which is 19078You can look at a function's source using @code{find-tag}, which is
17983bound to @kbd{M-.} Finally, you can find out what the Reference 19079bound to @kbd{M-.} Finally, you can find out what the Reference
17984Manual has to say by visiting the manual in Info, and typing @kbd{i} 19080Manual has to say by visiting the manual in Info, and typing @kbd{i}
17985(@code{Info-index}) and the name of the function, or by looking up 19081(@code{Info-index}) and the name of the function, or by looking up the
17986@code{skip-chars-forward} in the index to a printed copy of the 19082function in the index to a printed copy of the manual.
17987manual. 19083
17988 19084Similarly, you can find out what is meant by
17989Similarly, you can find out what is meant by @samp{?\n}. You can try 19085@code{insert-and-inherit}.
17990using @code{Info-index} with @samp{?\n}. It turns out that this
17991action won't help; but don't give up. If you search the index for
17992@samp{\n} without the @samp{?}, you will be taken directly to the
17993relevant section of the manual. (@xref{Character Type, , Character
17994Type, elisp, The GNU Emacs Lisp Reference Manual}. @samp{?\n} stands
17995for the newline character.)
17996 19086
17997Other interesting source files include @file{paragraphs.el}, 19087Other interesting source files include @file{paragraphs.el},
17998@file{loaddefs.el}, and @file{loadup.el}. The @file{paragraphs.el} 19088@file{loaddefs.el}, and @file{loadup.el}. The @file{paragraphs.el}
@@ -18115,6 +19205,105 @@ five six seven
18115You can substitute the other regular expressions shown above in the 19205You can substitute the other regular expressions shown above in the
18116function definition and try each of them on this list. 19206function definition and try each of them on this list.
18117 19207
19208@c done til here 2006 Oct 29
19209
19210@ignore
19211
19212
19213
19214@end ignore
19215
19216
19217@ignore
19218as of GNU Emacs 22, this no longer seems to be the case
19219with yank yank-pop
19220
19221guts of rotate-yank-pointer moved to current-kill
19222in simple.el
19223
19224(defun current-kill (n &optional do-not-move)
19225 "Rotate the yanking point by N places, and then return that kill.
19226
19227critical part is:
19228
19229 (let ((ARGth-kill-element
19230 (nthcdr (mod (- n (length kill-ring-yank-pointer))
19231 (length kill-ring))
19232 kill-ring)))
19233 (or do-not-move
19234 (setq kill-ring-yank-pointer ARGth-kill-element))
19235 (car ARGth-kill-element))
19236
19237
19238
19239 (mod X Y)
19240 Return X modulo Y.
19241
19242 (% X Y)
19243 Return remainder of X divided by Y.
19244
19245 (mod 12 3) 0 (#o0, #x0, ?\C-@)
19246 (mod 4 3) 1
19247 (mod 13 3) 1
19248
19249 (% 12 3) 0
19250 (% 4 3) 1
19251 (% 13 3) 1
19252
19253 mod returns the value of DIVIDEND modulo DIVISOR; in
19254 other words, the remainder after division of DIVIDEND by DIVISOR,
19255 but with the same sign as DIVISOR. The arguments must be numbers
19256 or markers.
19257
19258 Unlike `%', `mod' returns a well-defined result for negative
19259 arguments. It also permits floating point arguments; it rounds the
19260 quotient downward (towards minus infinity) to an integer, and uses
19261 that quotient to compute the remainder.
19262
19263 % returns the integer remainder after division of
19264 DIVIDEND by DIVISOR. The arguments must be integers or markers.
19265
19266 For negative arguments, the remainder is in principle
19267 machine-dependent since the quotient is; but in practice, all
19268 known machines behave alike.
19269
19270
19271
19272rotate-yank-pointer has
19273
19274reformatted:
19275 (let ((length (length kill-ring)))
19276 ...
19277 (setq kill-ring-yank-pointer
19278 (nthcdr (% (+ arg
19279 (- length (length kill-ring-yank-pointer)))
19280 length)
19281 kill-ring)))))
19282
19283originally
19284 (setq kill-ring-yank-pointer
19285 (nthcdr (% (+ arg
19286 (- length
19287 (length
19288 kill-ring-yank-pointer)))
19289 length)
19290 kill-ring)))))
19291
19292
19293
19294
19295
19296
19297/usr/local/src/emacs/lisp/ChangeLog.3
192981992-05-21 Jim Blandy (jimb@pogo.cs.oberlin.edu)
19299simple.el
19300 (yank-pop): Use current-kill, rather than assuming that
19301 kill-ring-yank-pointer points to the text you should use.
19302 (yank): Use current-kill, instead of calling rotate-yank-pointer
19303 and then fetching through the kill-ring-yank-pointer.
19304
19305@end ignore
19306
18118@node Kill Ring, Full Graph, the-the, Top 19307@node Kill Ring, Full Graph, the-the, Top
18119@appendix Handling the Kill Ring 19308@appendix Handling the Kill Ring
18120@cindex Kill ring handling 19309@cindex Kill ring handling
@@ -18122,155 +19311,269 @@ function definition and try each of them on this list.
18122@cindex Ring, making a list like a 19311@cindex Ring, making a list like a
18123 19312
18124The kill ring is a list that is transformed into a ring by the 19313The kill ring is a list that is transformed into a ring by the
18125workings of the @code{rotate-yank-pointer} function. The @code{yank} 19314workings of the @code{current-kill} function. The @code{yank} and
18126and @code{yank-pop} commands use the @code{rotate-yank-pointer} 19315@code{yank-pop} commands use the @code{current-kill} function.
18127function. This appendix describes the @code{rotate-yank-pointer} 19316
18128function as well as both the @code{yank} and the @code{yank-pop} 19317This appendix describes the @code{current-kill} function as well as
18129commands. 19318both the @code{yank} and the @code{yank-pop} commands, but first,
19319consider the workings of the kill ring.
19320
19321@need 1250
19322The kill ring has a default maximum length of sixty items; this number
19323is too large for an explanation. Instead, set it to four. Please
19324evaluate the following:
19325
19326@smallexample
19327@group
19328(setq old-kill-ring-max kill-ring-max)
19329(setq kill-ring-max 4)
19330@end group
19331@end smallexample
19332
19333@noindent
19334Then, please copy each line of the following indented example into the
19335kill ring. You may kill each line with @kbd{C-k} or mark it and copy
19336it with @kbd{M-w}.
19337
19338@noindent
19339(In a read-only buffer, such as the @file{*info*} buffer, the kill
19340command, @kbd{C-k} (@code{kill-line}), will not remove the text,
19341merely copy it to the kill ring. However, your machine may beep at
19342you. Alternatively, for silence, you may copy the region of each line
19343with the @kbd{M-w} (@code{kill-ring-save}) command. You must mark
19344each line for this command to succeed, but it does not matter at which
19345end you put point or mark.)
19346
19347@need 1250
19348@noindent
19349Please invoke the calls in order, so that five elements attempt to
19350fill the kill ring:
19351
19352@smallexample
19353@group
19354first some text
19355second piece of text
19356third line
19357fourth line of text
19358fifth bit of text
19359@end group
19360@end smallexample
19361
19362@need 1250
19363@noindent
19364Then find the value of @code{kill-ring} by evaluating
19365
19366@smallexample
19367kill-ring
19368@end smallexample
19369
19370@need 800
19371@noindent
19372It is:
19373
19374@smallexample
19375@group
19376("fifth bit of text" "fourth line of text"
19377"third line" "second piece of text")
19378@end group
19379@end smallexample
19380
19381@noindent
19382The first element, @samp{first some text}, was dropped.
19383
19384@need 1250
19385To return to the old value for the length of the kill ring, evaluate:
19386
19387@smallexample
19388(setq kill-ring-max old-kill-ring-max)
19389@end smallexample
18130 19390
18131@menu 19391@menu
18132* rotate-yank-pointer:: Move a pointer along a list and around. 19392* current-kill::
18133* yank:: Paste a copy of a clipped element. 19393* yank::
18134* yank-pop:: Insert first element pointed to. 19394* yank-pop::
18135* ring file:: 19395* ring file::
18136@end menu 19396@end menu
18137 19397
18138@node rotate-yank-pointer, yank, Kill Ring, Kill Ring 19398@node current-kill, yank, Kill Ring, Kill Ring
18139@comment node-name, next, previous, up 19399@comment node-name, next, previous, up
18140@appendixsec The @code{rotate-yank-pointer} Function 19400@appendixsec The @code{current-kill} Function
18141@findex rotate-yank-pointer 19401@findex current-kill
18142 19402
18143The @code{rotate-yank-pointer} function changes the element in the kill 19403The @code{current-kill} function changes the element in the kill ring
18144ring to which @code{kill-ring-yank-pointer} points. For example, it can 19404to which @code{kill-ring-yank-pointer} points. (Also, the
18145change @code{kill-ring-yank-pointer} from pointing to the second 19405@code{kill-new} function sets @code{kill-ring-yank-pointer} to point
18146element to point to the third element. 19406to the latest element of the the kill ring.)
18147 19407
18148@need 800 19408@need 1500
18149Here is the code for @code{rotate-yank-pointer}: 19409The @code{current-kill} function is used by @code{yank} and by
19410@code{yank-pop}. Here is the code for @code{current-kill}:
18150 19411
18151@smallexample 19412@smallexample
18152@group 19413@group
18153(defun rotate-yank-pointer (arg) 19414(defun current-kill (n &optional do-not-move)
18154 "Rotate the yanking point in the kill ring." 19415 "Rotate the yanking point by N places, and then return that kill.
18155 (interactive "p") 19416If N is zero, `interprogram-paste-function' is set, and calling it
18156 (let ((length (length kill-ring))) 19417returns a string, then that string is added to the front of the
19418kill ring and returned as the latest kill.
18157@end group 19419@end group
18158@group 19420@group
18159 (if (zerop length) 19421If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
18160 ;; @r{then-part} 19422yanking point; just return the Nth kill forward."
18161 (error "Kill ring is empty") 19423 (let ((interprogram-paste (and (= n 0)
19424 interprogram-paste-function
19425 (funcall interprogram-paste-function))))
18162@end group 19426@end group
18163@group 19427@group
18164 ;; @r{else-part} 19428 (if interprogram-paste
18165 (setq kill-ring-yank-pointer 19429 (progn
18166 (nthcdr (% (+ arg 19430 ;; Disable the interprogram cut function when we add the new
18167 (- length 19431 ;; text to the kill ring, so Emacs doesn't try to own the
18168 (length 19432 ;; selection, with identical text.
18169 kill-ring-yank-pointer))) 19433 (let ((interprogram-cut-function nil))
18170 length) 19434 (kill-new interprogram-paste))
18171 kill-ring))))) 19435 interprogram-paste)
18172@end group 19436@end group
19437@group
19438 (or kill-ring (error "Kill ring is empty"))
19439 (let ((ARGth-kill-element
19440 (nthcdr (mod (- n (length kill-ring-yank-pointer))
19441 (length kill-ring))
19442 kill-ring)))
19443 (or do-not-move
19444 (setq kill-ring-yank-pointer ARGth-kill-element))
19445 (car ARGth-kill-element)))))
19446@end group
19447@end smallexample
19448
19449In addition, the @code{kill-new} function sets
19450@code{kill-ring-yank-pointer} to the latest element of the the kill
19451ring. And indirectly so does @code{kill-append}, since it calls
19452@code{kill-new}. In addition, @code{kill-region} and @code{kill-line}
19453call the @code{kill-new} function.
19454
19455@need 1500
19456Here is the line in @code{kill-new}, which is explained in
19457@ref{kill-new function, , The @code{kill-new} function}.
19458
19459@smallexample
19460(setq kill-ring-yank-pointer kill-ring)
18173@end smallexample 19461@end smallexample
18174 19462
18175@menu 19463@menu
18176* Understanding rotate-yk-ptr:: 19464* Understanding current-kill::
18177* rotate-yk-ptr body:: The body of @code{rotate-yank-pointer}.
18178@end menu 19465@end menu
18179 19466
18180@node Understanding rotate-yk-ptr, rotate-yk-ptr body, rotate-yank-pointer, rotate-yank-pointer 19467@node Understanding current-kill, , current-kill, current-kill
18181@ifnottex 19468@ifnottex
18182@unnumberedsubsec @code{rotate-yank-pointer} in Outline 19469@unnumberedsubsec @code{current-kill} in Outline
18183@end ifnottex 19470@end ifnottex
18184 19471
18185The @code{rotate-yank-pointer} function looks complex, but as usual, 19472The @code{current-kill} function looks complex, but as usual, it can
18186it can be understood by taking it apart piece by piece. First look at 19473be understood by taking it apart piece by piece. First look at it in
18187it in skeletal form: 19474skeletal form:
18188 19475
18189@smallexample 19476@smallexample
18190@group 19477@group
18191(defun rotate-yank-pointer (arg) 19478(defun current-kill (n &optional do-not-move)
18192 "Rotate the yanking point in the kill ring." 19479 "Rotate the yanking point by N places, and then return that kill.
18193 (interactive "p")
18194 (let @var{varlist} 19480 (let @var{varlist}
18195 @var{body}@dots{}) 19481 @var{body}@dots{})
18196@end group 19482@end group
18197@end smallexample 19483@end smallexample
18198 19484
18199This function takes one argument, called @code{arg}. It has a brief 19485This function takes two arguments, one of which is optional. It has a
18200documentation string; and it is interactive with a small @samp{p}, which 19486documentation string. It is @emph{not} interactive.
18201means that the argument must be a processed prefix passed to the
18202function as a number.
18203 19487
18204The body of the function definition is a @code{let} expression, which 19488The body of the function definition is a @code{let} expression, which
18205itself has a body as well as a @var{varlist}. 19489itself has a body as well as a @var{varlist}.
18206 19490
18207The @code{let} expression declares a variable that will be only usable 19491The @code{let} expression declares a variable that will be only usable
18208within the bounds of this function. This variable is called 19492within the bounds of this function. This variable is called
18209@code{length} and is bound to a value that is equal to the number of 19493@code{interprogram-paste} and is for copying to another program. It
18210items in the kill ring. This is done by using the function called 19494is not for copying within this instance of GNU Emacs. Most window
18211@code{length}. (Note that this function has the same name as the 19495systems provide a facility for interprogram pasting. Sadly, that
18212variable called @code{length}; but one use of the word is to name the 19496facility usually provides only for the lasted element. Most windowing
18213function and the other is to name the variable. The two are quite 19497systems have not adopted a ring of many possiblities, even though
18214distinct. Similarly, an English speaker will distinguish between the 19498Emacs has provided it for decades.
18215meanings of the word @samp{ship} when he says: "I must ship this package
18216immediately." and "I must get aboard the ship immediately.")
18217
18218The function @code{length} tells the number of items there are in a list,
18219so @code{(length kill-ring)} returns the number of items there are in the
18220kill ring.
18221 19499
18222@node rotate-yk-ptr body, , Understanding rotate-yk-ptr, rotate-yank-pointer 19500The @code{if} expression has two parts, one if there exists
18223@comment node-name, next, previous, up 19501@code{interprogram-paste} and one if not.
18224@appendixsubsec The Body of @code{rotate-yank-pointer} 19502
19503@need 2000
19504Let us consider the `if not' or else-part of the @code{current-kill}
19505function. (The then-part uses the the @code{kill-new} function, which
19506we have already described. (@xref{kill-new function, , The
19507@code{kill-new} function}.)
18225 19508
18226The body of @code{rotate-yank-pointer} is a @code{let} expression and 19509@smallexample
18227the body of the @code{let} expression is an @code{if} expression. 19510@group
19511(or kill-ring (error "Kill ring is empty"))
19512(let ((ARGth-kill-element
19513 (nthcdr (mod (- n (length kill-ring-yank-pointer))
19514 (length kill-ring))
19515 kill-ring)))
19516 (or do-not-move
19517 (setq kill-ring-yank-pointer ARGth-kill-element))
19518 (car ARGth-kill-element))
19519@end group
19520@end smallexample
18228 19521
18229The purpose of the @code{if} expression is to find out whether there is 19522@noindent
18230anything in the kill ring. If the kill ring is empty, the @code{error} 19523The code first checks whether the kill ring has content; otherwise it
18231function stops evaluation of the function and prints a message in the 19524signals an error.
18232echo area. On the other hand, if the kill ring has something in it, the
18233work of the function is done.
18234 19525
18235Here is the if-part and then-part of the @code{if} expression: 19526@need 1000
19527Note that the @code{or} expression is very similar to writing
18236 19528
18237@findex zerop 19529@findex zerop
18238@findex error 19530@findex error
18239@smallexample 19531@smallexample
18240@group 19532@group
18241(if (zerop length) ; @r{if-part} 19533(if (zerop (length kill-ring)) ; @r{if-part}
18242 (error "Kill ring is empty") ; @r{then-part} 19534 (error "Kill ring is empty")) ; @r{then-part}
18243 @dots{} 19535 ;; No else-part
18244@end group 19536@end group
18245@end smallexample 19537@end smallexample
18246 19538
18247@noindent 19539@noindent
18248If there is not anything in the kill ring, its length must be zero and 19540If there is not anything in the kill ring, its length must be zero and
18249an error message sent to the user: @samp{Kill ring is empty}. The 19541an error message sent to the user: @samp{Kill ring is empty}. The
18250@code{if} expression uses the function @code{zerop} which returns true 19542@code{current-kill} function uses an @code{or} expression which is
18251if the value it is testing is zero. When @code{zerop} tests true, the 19543simpler. But an @code{if} expression reminds us what goes on.
18252then-part of the @code{if} is evaluated. The then-part is a list 19544
18253starting with the function @code{error}, which is a function that is 19545This @code{if} expression uses the function @code{zerop} which returns
18254similar to the @code{message} function (@pxref{message}), in that it 19546true if the value it is testing is zero. When @code{zerop} tests
18255prints a one-line message in the echo area. However, in addition to 19547true, the then-part of the @code{if} is evaluated. The then-part is a
18256printing a message, @code{error} also stops evaluation of the function 19548list starting with the function @code{error}, which is a function that
18257within which it is embedded. This means that the rest of the function 19549is similar to the @code{message} function
18258will not be evaluated if the length of the kill ring is zero. 19550(@pxref{message, , The @code{message} Function}), in that
19551it prints a one-line message in the echo area. However, in addition
19552to printing a message, @code{error} also stops evaluation of the
19553function within which it is embedded. This means that the rest of the
19554function will not be evaluated if the length of the kill ring is zero.
19555
19556Then the @code{current-kill} function selects the element to return.
19557The selection depends on the number of places that @code{current-kill}
19558rotates and on where @code{kill-ring-yank-pointer} points.
19559
19560Next, either the optional @code{do-not-move} argument is true or the
19561current value of @code{kill-ring-yank-pointer} is set to point to the
19562list, the first element of which is returned even if the
19563@code{do-not-move} argument is true.
19564
18259 19565
18260@menu 19566@menu
18261* Digression concerning error:: How to mislead humans, but not computers. 19567* Digression concerning error::
18262* rotate-yk-ptr else-part:: The else-part of the @code{if} expression. 19568* Determining the Element ::
18263* Remainder Function:: The remainder, @code{%}, function.
18264* rotate-yk-ptr remainder:: Using @code{%} in @code{rotate-yank-pointer}.
18265* kill-rng-yk-ptr last elt:: Pointing to the last element.
18266@end menu 19569@end menu
18267 19570
18268@node Digression concerning error, rotate-yk-ptr else-part, rotate-yk-ptr body, rotate-yk-ptr body 19571@node Digression concerning error, Determining the Element , Understanding current-kill, Understanding current-kill
18269@ifnottex 19572@ifnottex
18270@unnumberedsubsubsec Digression about the word `error' 19573@unnumberedsubsubsec Digression about the word `error'
18271@end ifnottex 19574@end ifnottex
18272 19575
18273(In my opinion, it is slightly misleading, at least to humans, to use 19576In my opinion, it is slightly misleading, at least to humans, to use
18274the term `error' as the name of the @code{error} function. A better 19577the term `error' as the name of the @code{error} function. A better
18275term would be `cancel'. Strictly speaking, of course, you cannot 19578term would be `cancel'. Strictly speaking, of course, you cannot
18276point to, much less rotate a pointer to a list that has no length, so 19579point to, much less rotate a pointer to a list that has no length, so
@@ -18285,86 +19588,38 @@ even in the bowels of a computer. As it is, the code in Emacs implies
18285that a human who is acting virtuously, by exploring his or her 19588that a human who is acting virtuously, by exploring his or her
18286environment, is making an error. This is bad. Even though the computer 19589environment, is making an error. This is bad. Even though the computer
18287takes the same steps as it does when there is an `error', a term such as 19590takes the same steps as it does when there is an `error', a term such as
18288`cancel' would have a clearer connotation.) 19591`cancel' would have a clearer connotation.
18289
18290@node rotate-yk-ptr else-part, Remainder Function, Digression concerning error, rotate-yk-ptr body
18291@unnumberedsubsubsec The else-part of the @code{if} expression
18292
18293The else-part of the @code{if} expression is dedicated to setting the
18294value of @code{kill-ring-yank-pointer} when the kill ring has something
18295in it. The code looks like this:
18296
18297@smallexample
18298@group
18299(setq kill-ring-yank-pointer
18300 (nthcdr (% (+ arg
18301 (- length
18302 (length kill-ring-yank-pointer)))
18303 length)
18304 kill-ring)))))
18305@end group
18306@end smallexample
18307
18308This needs some examination. Clearly, @code{kill-ring-yank-pointer}
18309is being set to be equal to some @sc{cdr} of the kill ring, using the
18310@code{nthcdr} function that is described in an earlier section.
18311(@xref{copy-region-as-kill}.) But exactly how does it do this?
18312
18313Before looking at the details of the code let's first consider the
18314purpose of the @code{rotate-yank-pointer} function.
18315
18316The @code{rotate-yank-pointer} function changes what
18317@code{kill-ring-yank-pointer} points to. If
18318@code{kill-ring-yank-pointer} starts by pointing to the first element
18319of a list, a call to @code{rotate-yank-pointer} causes it to point to
18320the second element; and if @code{kill-ring-yank-pointer} points to the
18321second element, a call to @code{rotate-yank-pointer} causes it to
18322point to the third element. (And if @code{rotate-yank-pointer} is
18323given an argument greater than 1, it jumps the pointer that many
18324elements.)
18325
18326The @code{rotate-yank-pointer} function uses @code{setq} to reset what
18327the @code{kill-ring-yank-pointer} points to. If
18328@code{kill-ring-yank-pointer} points to the first element of the kill
18329ring, then, in the simplest case, the @code{rotate-yank-pointer}
18330function must cause it to point to the second element. Put another
18331way, @code{kill-ring-yank-pointer} must be reset to have a value equal
18332to the @sc{cdr} of the kill ring.
18333 19592
18334@need 1250 19593@node Determining the Element , , Digression concerning error, Understanding current-kill
18335That is, under these circumstances, 19594@ifnottex
18336 19595@unnumberedsubsubsec Determining the Element
18337@smallexample 19596@end ifnottex
18338@group
18339(setq kill-ring-yank-pointer
18340 ("some text" "a different piece of text" "yet more text"))
18341 19597
18342(setq kill-ring 19598Among other actions, the else-part of the @code{if} expression sets
18343 ("some text" "a different piece of text" "yet more text")) 19599the value of @code{kill-ring-yank-pointer} to
18344@end group 19600@code{ARGth-kill-element} when the kill ring has something in it and
18345@end smallexample 19601the value of @code{do-not-move} is @code{nil}.
18346 19602
18347@need 800 19603@need 800
18348@noindent 19604The code looks like this:
18349the code should do this:
18350
18351@smallexample
18352(setq kill-ring-yank-pointer (cdr kill-ring))
18353@end smallexample
18354
18355@need 1000
18356@noindent
18357As a result, the @code{kill-ring-yank-pointer} will look like this:
18358 19605
18359@smallexample 19606@smallexample
18360@group 19607@group
18361kill-ring-yank-pointer 19608(nthcdr (mod (- n (length kill-ring-yank-pointer))
18362 @result{} ("a different piece of text" "yet more text")) 19609 (length kill-ring))
19610 kill-ring)))
18363@end group 19611@end group
18364@end smallexample 19612@end smallexample
18365 19613
18366The actual @code{setq} expression uses the @code{nthcdr} function to do 19614This needs some examination. Unless it is not supposed to move the
18367the job. 19615pointer, the @code{current-kill} function changes where
19616@code{kill-ring-yank-pointer} points.
19617That is what the
19618@w{@code{(setq kill-ring-yank-pointer ARGth-kill-element))}}
19619expression does. Also, clearly, @code{ARGth-kill-element} is being
19620set to be equal to some @sc{cdr} of the kill ring, using the
19621@code{nthcdr} function that is described in an earlier section.
19622(@xref{copy-region-as-kill}.) How does it do this?
18368 19623
18369As we have seen before (@pxref{nthcdr}), the @code{nthcdr} function 19624As we have seen before (@pxref{nthcdr}), the @code{nthcdr} function
18370works by repeatedly taking the @sc{cdr} of a list---it takes the 19625works by repeatedly taking the @sc{cdr} of a list---it takes the
@@ -18381,477 +19636,222 @@ The two following expressions produce the same result:
18381@end group 19636@end group
18382@end smallexample 19637@end smallexample
18383 19638
18384In the @code{rotate-yank-pointer} function, however, the first 19639However, the @code{nthcdr} expression is more complicated. It uses
18385argument to @code{nthcdr} is a rather complex looking expression with 19640the @code{mod} function to determine which @sc{cdr} to select.
18386lots of arithmetic inside of it:
18387 19641
18388@smallexample 19642(You will remember to look at inner functions first; indeed, we will
18389@group 19643have to go inside the @code{mod}.)
18390(% (+ arg
18391 (- length
18392 (length kill-ring-yank-pointer)))
18393 length)
18394@end group
18395@end smallexample
18396
18397As usual, we need to look at the most deeply embedded expression first
18398and then work our way towards the light.
18399 19644
18400The most deeply embedded expression is @code{(length 19645The @code{mod} function returns the value of its first argument modulo
18401kill-ring-yank-pointer)}. This finds the length of the current value of 19646the second; that is to say, it returns the remainder after dividing
18402the @code{kill-ring-yank-pointer}. (Remember that the 19647the first argument by the second. The value returned has the same
18403@code{kill-ring-yank-pointer} is the name of a variable whose value is a 19648sign as the second argument.
18404list.)
18405 19649
18406@need 800 19650@need 800
18407The measurement of the length is inside the expression: 19651Thus,
18408
18409@smallexample
18410(- length (length kill-ring-yank-pointer))
18411@end smallexample
18412
18413@noindent
18414In this expression, the first @code{length} is the variable that was
18415assigned the length of the kill ring in the @code{let} statement at the
18416beginning of the function. (One might think this function would be
18417clearer if the variable @code{length} were named
18418@code{length-of-kill-ring} instead; but if you look at the text of the
18419whole function, you will see that it is so short that naming this
18420variable @code{length} is not a bother, unless you are pulling the
18421function apart into very tiny pieces as we are doing here.)
18422
18423So the line @code{(- length (length kill-ring-yank-pointer))} tells the
18424difference between the length of the kill ring and the length of the list
18425whose name is @code{kill-ring-yank-pointer}.
18426
18427To see how all this fits into the @code{rotate-yank-pointer}
18428function, let's begin by analyzing the case where
18429@code{kill-ring-yank-pointer} points to the first element of the kill
18430ring, just as @code{kill-ring} does, and see what happens when
18431@code{rotate-yank-pointer} is called with an argument of 1.
18432
18433The variable @code{length} and the value of the expression
18434@code{(length kill-ring-yank-pointer)} will be the same since the
18435variable @code{length} is the length of the kill ring and the
18436@code{kill-ring-yank-pointer} is pointing to the whole kill ring.
18437Consequently, the value of
18438
18439@smallexample
18440(- length (length kill-ring-yank-pointer))
18441@end smallexample
18442
18443@noindent
18444will be zero. Since the value of @code{arg} will be 1, this will mean
18445that the value of the whole expression
18446
18447@smallexample
18448(+ arg (- length (length kill-ring-yank-pointer)))
18449@end smallexample
18450
18451@noindent
18452will be 1.
18453
18454@need 1200
18455Consequently, the argument to @code{nthcdr} will be found as the result of
18456the expression
18457
18458@smallexample
18459(% 1 length)
18460@end smallexample
18461
18462@node Remainder Function, rotate-yk-ptr remainder, rotate-yk-ptr else-part, rotate-yk-ptr body
18463@unnumberedsubsubsec The @code{%} remainder function
18464
18465To understand @code{(% 1 length)}, we need to understand @code{%}.
18466According to its documentation (which I just found by typing @kbd{C-h
18467f @kbd{%} @key{RET}}), the @code{%} function returns the remainder of
18468its first argument divided by its second argument. For example, the
18469remainder of 5 divided by 2 is 1. (2 goes into 5 twice with a
18470remainder of 1.)
18471
18472What surprises people who don't often do arithmetic is that a smaller
18473number can be divided by a larger number and have a remainder. In the
18474example we just used, 5 was divided by 2. We can reverse that and ask,
18475what is the result of dividing 2 by 5? If you can use fractions, the
18476answer is obviously 2/5 or .4; but if, as here, you can only use whole
18477numbers, the result has to be something different. Clearly, 5 can go into
184782 zero times, but what of the remainder? To see what the answer is,
18479consider a case that has to be familiar from childhood:
18480
18481@itemize @bullet
18482@item
184835 divided by 5 is 1 with a remainder of 0;
18484
18485@item
184866 divided by 5 is 1 with a remainder of 1;
18487
18488@item
184897 divided by 5 is 1 with a remainder of 2.
18490
18491@item
18492Similarly, 10 divided by 5 is 2 with a remainder of 0;
18493
18494@item
1849511 divided by 5 is 2 with a remainder of 1;
18496
18497@item
1849812 divided by 5 is 1 with a remainder of 2.
18499@end itemize
18500
18501@need 1250
18502@noindent
18503By considering the cases as parallel, we can see that
18504
18505@itemize @bullet
18506@item
18507zero divided by 5 must be zero with a remainder of zero;
18508
18509@item
185101 divided by 5 must be zero with a remainder of 1;
18511
18512@item
185132 divided by 5 must be zero with a remainder of 2;
18514@end itemize
18515
18516@noindent
18517and so on.
18518
18519@need 1250
18520So, in this code, if the value of @code{length} is 5, then the result of
18521evaluating
18522
18523@smallexample
18524(% 1 5)
18525@end smallexample
18526
18527@noindent
18528is 1. (I just checked this by placing the cursor after the expression
18529and typing @kbd{C-x C-e}. Indeed, 1 is printed in the echo area.)
18530
18531@need 2000
18532@node rotate-yk-ptr remainder, kill-rng-yk-ptr last elt, Remainder Function, rotate-yk-ptr body
18533@unnumberedsubsubsec Using @code{%} in @code{rotate-yank-pointer}
18534
18535When the @code{kill-ring-yank-pointer} points to the
18536beginning of the kill ring, and the argument passed to
18537@code{rotate-yank-pointer} is 1, the @code{%} expression returns 1:
18538 19652
18539@smallexample 19653@smallexample
18540@group 19654@group
18541(- length (length kill-ring-yank-pointer)) 19655(mod 12 4)
18542 @result{} 0 19656 @result{} 0 ;; @r{because there is no remainder}
19657(mod 13 4)
19658 @result{} 1
18543@end group 19659@end group
18544@end smallexample 19660@end smallexample
18545 19661
18546@need 1250 19662@need 1250
18547@noindent 19663In this case, the first argument is often smaller than the second.
18548therefore, 19664That is fine.
18549 19665
18550@smallexample 19666@smallexample
18551@group 19667@group
18552(+ arg (- length (length kill-ring-yank-pointer))) 19668(mod 0 4)
18553 @result{} 1 19669 @result{} 0
19670(mod 1 4)
19671 @result{} 1
18554@end group 19672@end group
18555@end smallexample 19673@end smallexample
18556 19674
18557@need 1250 19675We can guess what the @code{-} function does. It is like @code{+} but
18558@noindent 19676substracts instead of adds; the @code{-} function subtracts its second
18559and consequently: 19677argument from its first. Also, we already know what the @code{length}
18560 19678function does (@pxref{length}). It returns the length of a list.
18561@smallexample
18562@group
18563(% (+ arg (- length (length kill-ring-yank-pointer)))
18564 length)
18565 @result{} 1
18566@end group
18567@end smallexample
18568 19679
18569@noindent 19680And @code{n} is the name of the required argument to the
18570regardless of the value of @code{length}. 19681@code{current-kill} function.
18571 19682
18572@need 1250 19683@need 1250
18573@noindent 19684So when the first argument to @code{nthcdr} is zero, the @code{nthcdr}
18574As a result of this, the @code{setq kill-ring-yank-pointer} expression 19685expression returns the whole list, as you can see by evaluating the
18575simplifies to: 19686following:
18576
18577@smallexample
18578(setq kill-ring-yank-pointer (nthcdr 1 kill-ring))
18579@end smallexample
18580
18581@noindent
18582What it does is now easy to understand. Instead of pointing as it did
18583to the first element of the kill ring, the
18584@code{kill-ring-yank-pointer} is set to point to the second element.
18585
18586Clearly, if the argument passed to @code{rotate-yank-pointer} is two, then
18587the @code{kill-ring-yank-pointer} is set to @code{(nthcdr 2 kill-ring)};
18588and so on for different values of the argument.
18589
18590Similarly, if the @code{kill-ring-yank-pointer} starts out pointing to
18591the second element of the kill ring, its length is shorter than the
18592length of the kill ring by 1, so the computation of the remainder is
18593based on the expression @code{(% (+ arg 1) length)}. This means that
18594the @code{kill-ring-yank-pointer} is moved from the second element of
18595the kill ring to the third element if the argument passed to
18596@code{rotate-yank-pointer} is 1.
18597
18598@node kill-rng-yk-ptr last elt, , rotate-yk-ptr remainder, rotate-yk-ptr body
18599@unnumberedsubsubsec Pointing to the last element
18600
18601The final question is, what happens if the @code{kill-ring-yank-pointer}
18602is set to the @emph{last} element of the kill ring? Will a call to
18603@code{rotate-yank-pointer} mean that nothing more can be taken from the
18604kill ring? The answer is no. What happens is different and useful.
18605The @code{kill-ring-yank-pointer} is set to point to the beginning of
18606the kill ring instead.
18607
18608Let's see how this works by looking at the code, assuming the length of the
18609kill ring is 5 and the argument passed to @code{rotate-yank-pointer} is 1.
18610When the @code{kill-ring-yank-pointer} points to the last element of
18611the kill ring, its length is 1. The code looks like this:
18612 19687
18613@smallexample 19688@smallexample
18614(% (+ arg (- length (length kill-ring-yank-pointer))) length) 19689@group
19690;; kill-ring-yank-pointer @r{and} kill-ring @r{have a length of four}
19691(nthcdr (mod (- 0 4) 4) ; (mod -4 4) @result{} 0
19692 '("fourth line of text"
19693 "third line"
19694 "second piece of text"
19695 "first some text"))
19696@end group
18615@end smallexample 19697@end smallexample
18616 19698
18617@need 1250 19699@need 1250
18618When the variables are replaced by their numeric values, the expression 19700When the first argument to the @code{current-kill} function is one,
18619looks like this: 19701the @code{nthcdr} expression returns the list without its first
19702element.
18620 19703
18621@smallexample 19704@smallexample
18622(% (+ 1 (- 5 1)) 5) 19705@group
18623@end smallexample 19706(nthcdr (mod (- 1 4) 4)
18624 19707 '("fourth line of text"
18625@noindent 19708 "third line"
18626This expression can be evaluated by looking at the most embedded inner 19709 "second piece of text"
18627expression first and working outwards: The value of @code{(- 5 1)} is 4; 19710 "first some text"))
18628the sum of @code{(+ 1 4)} is 5; and the remainder of dividing 5 by 5 is 19711@end group
18629zero. So what @code{rotate-yank-pointer} will do is
18630
18631@smallexample
18632(setq kill-ring-yank-pointer (nthcdr 0 kill-ring))
18633@end smallexample 19712@end smallexample
18634 19713
18635@noindent 19714@cindex @samp{global variable} defined
18636which will set the @code{kill-ring-yank-pointer} to point to the beginning 19715@cindex @samp{variable, global}, defined
18637of the kill ring. 19716Incidently, both @code{kill-ring} and @code{kill-ring-yank-pointer}
18638 19717are @dfn{global variables}. That means that any expression in Emacs
18639So what happens with successive calls to @code{rotate-yank-pointer} is that 19718Lisp can access them. They are not like the local variables set by
18640it moves the @code{kill-ring-yank-pointer} from element to element in the 19719@code{let} (@pxref{Prevent confusion}) or like the symbols in an
18641kill ring until it reaches the end; then it jumps back to the beginning. 19720argument list (@pxref{defun, , The @code{defun} Special Form}). Local
18642And this is why the kill ring is called a ring, since by jumping back to 19721variables can only be accessed within the @code{let} that defines them
18643the beginning, it is as if the list has no end! (And what is a ring, but 19722or the function that specifies them in an argument list (and within
18644an entity with no end?) 19723expressions called by them).
18645 19724
18646@node yank, yank-pop, rotate-yank-pointer, Kill Ring 19725@node yank, yank-pop, current-kill, Kill Ring
18647@comment node-name, next, previous, up 19726@comment node-name, next, previous, up
18648@appendixsec @code{yank} 19727@appendixsec @code{yank}
18649@findex yank 19728@findex yank
18650 19729
18651After learning about @code{rotate-yank-pointer}, the code for the 19730After learning about @code{current-kill}, the code for the
18652@code{yank} function is almost easy. It has only one tricky part, which is 19731@code{yank} function is almost easy. It has only one tricky part, which is
18653the computation of the argument to be passed to @code{rotate-yank-pointer}. 19732the computation of the argument to be passed to @code{rotate-yank-pointer}.
18654 19733
18655@need 1250 19734@need 1250
18656The code looks like this: 19735The code looks like this:
18657 19736
19737@c in GNU Emacs 22
18658@smallexample 19738@smallexample
18659@group 19739@group
18660(defun yank (&optional arg) 19740(defun yank (&optional arg)
18661 "Reinsert the last stretch of killed text. 19741 "Reinsert (\"paste\") the last stretch of killed text.
18662More precisely, reinsert the stretch of killed text most 19742More precisely, reinsert the stretch of killed text most recently
18663recently killed OR yanked. 19743killed OR yanked. Put point at end, and set mark at beginning.
18664With just C-U as argument, same but put point in front 19744With just \\[universal-argument] as argument, same but put point at
18665(and mark at end). With argument n, reinsert the nth 19745beginning (and mark at end). With argument N, reinsert the Nth most
18666most recently killed stretch of killed text. 19746recently killed stretch of killed text.
19747
19748When this command inserts killed text into the buffer, it honors
19749`yank-excluded-properties' and `yank-handler' as described in the
19750doc string for `insert-for-yank-1', which see.
19751
18667See also the command \\[yank-pop]." 19752See also the command \\[yank-pop]."
18668@end group 19753@end group
18669@group 19754@group
18670
18671 (interactive "*P") 19755 (interactive "*P")
18672 (rotate-yank-pointer (if (listp arg) 0 19756 (setq yank-window-start (window-start))
18673 (if (eq arg '-) -1 19757 ;; If we don't get all the way thru, make last-command indicate that
18674 (1- arg)))) 19758 ;; for the following command.
19759 (setq this-command t)
18675 (push-mark (point)) 19760 (push-mark (point))
18676 (insert (car kill-ring-yank-pointer))
18677 (if (consp arg)
18678 (exchange-point-and-mark)))
18679@end group 19761@end group
18680@end smallexample
18681
18682Glancing over this code, we can understand the last few lines readily
18683enough. The mark is pushed, that is, remembered; then the first element
18684(the @sc{car}) of what the @code{kill-ring-yank-pointer} points to is
18685inserted; and then, if the argument passed the function is a
18686@code{cons}, point and mark are exchanged so the point is put in the
18687front of the inserted text rather than at the end. This option is
18688explained in the documentation. The function itself is interactive with
18689@code{"*P"}. This means it will not work on a read-only buffer, and that
18690the unprocessed prefix argument is passed to the function.
18691
18692@menu
18693* rotate-yk-ptr arg:: Pass the argument to @code{rotate-yank-pointer}.
18694* rotate-yk-ptr negative arg:: Pass a negative argument.
18695@end menu
18696
18697@node rotate-yk-ptr arg, rotate-yk-ptr negative arg, yank, yank
18698@unnumberedsubsubsec Passing the argument
18699
18700The hard part of @code{yank} is understanding the computation that
18701determines the value of the argument passed to
18702@code{rotate-yank-pointer}. Fortunately, it is not so difficult as it
18703looks at first sight.
18704
18705What happens is that the result of evaluating one or both of the
18706@code{if} expressions will be a number and that number will be the
18707argument passed to @code{rotate-yank-pointer}.
18708
18709@need 1250
18710Laid out with comments, the code looks like this:
18711
18712@smallexample
18713@group 19762@group
18714(if (listp arg) ; @r{if-part} 19763 (insert-for-yank (current-kill (cond
18715 0 ; @r{then-part} 19764 ((listp arg) 0)
18716 (if (eq arg '-) ; @r{else-part, inner if} 19765 ((eq arg '-) -2)
18717 -1 ; @r{inner if's then-part} 19766 (t (1- arg)))))
18718 (1- arg)))) ; @r{inner if's else-part} 19767 (if (consp arg)
19768 ;; This is like exchange-point-and-mark,
19769 ;; but doesn't activate the mark.
19770 ;; It is cleaner to avoid activation, even though the command
19771 ;; loop would deactivate the mark because we inserted text.
19772 (goto-char (prog1 (mark t)
19773 (set-marker (mark-marker) (point) (current-buffer)))))
18719@end group 19774@end group
18720@end smallexample
18721
18722@noindent
18723This code consists of two @code{if} expression, one the else-part of
18724the other.
18725
18726The first or outer @code{if} expression tests whether the argument
18727passed to @code{yank} is a list. Oddly enough, this will be true if
18728@code{yank} is called without an argument---because then it will be
18729passed the value of @code{nil} for the optional argument and an
18730evaluation of @code{(listp nil)} returns true! So, if no argument is
18731passed to @code{yank}, the argument passed to
18732@code{rotate-yank-pointer} inside of @code{yank} is zero. This means
18733the pointer is not moved and the first element to which
18734@code{kill-ring-yank-pointer} points is inserted, as we expect.
18735Similarly, if the argument for @code{yank} is @kbd{C-u}, this will be
18736read as a list, so again, a zero will be passed to
18737@code{rotate-yank-pointer}. (@kbd{C-u} produces an unprocessed prefix
18738argument of @code{(4)}, which is a list of one element.) At the same
18739time, later in the function, this argument will be read as a
18740@code{cons} so point will be put in the front and mark at the end of
18741the insertion. (The @code{P} argument to @code{interactive} is
18742designed to provide these values for the case when an optional
18743argument is not provided or when it is @kbd{C-u}.)
18744
18745The then-part of the outer @code{if} expression handles the case when
18746there is no argument or when it is @kbd{C-u}. The else-part handles the
18747other situations. The else-part is itself another @code{if} expression.
18748
18749The inner @code{if} expression tests whether the argument is a minus
18750sign. (This is done by pressing the @key{META} and @kbd{-} keys at the
18751same time, or the @key{ESC} key and then the @kbd{-} key). In this
18752case, the @code{rotate-yank-pointer} function is passed @kbd{-1} as an
18753argument. This moves the @code{kill-ring-yank-pointer} backwards, which
18754is what is desired.
18755
18756If the true-or-false-test of the inner @code{if} expression is false
18757(that is, if the argument is not a minus sign), the else-part of the
18758expression is evaluated. This is the expression @code{(1- arg)}.
18759Because of the two @code{if} expressions, it will only occur when the
18760argument is a positive number or when it is a negative number (not
18761just a minus sign on its own). What @code{(1- arg)} does is decrement
18762the number and return it. (The @code{1-} function subtracts one from
18763its argument.) This means that if the argument to
18764@code{rotate-yank-pointer} is 1, it is reduced to zero, which means
18765the first element to which @code{kill-ring-yank-pointer} points is
18766yanked back, as you would expect.
18767
18768@node rotate-yk-ptr negative arg, , rotate-yk-ptr arg, yank
18769@unnumberedsubsubsec Passing a negative argument
18770
18771Finally, the question arises, what happens if either the remainder
18772function, @code{%}, or the @code{nthcdr} function is passed a negative
18773argument, as they quite well may?
18774
18775The answers can be found by a quick test. When @code{(% -1 5)} is
18776evaluated, a negative number is returned; and if @code{nthcdr} is
18777called with a negative number, it returns the same value as if it were
18778called with a first argument of zero. This can be seen by evaluating
18779the following code.
18780
18781Here the @samp{@result{}} points to the result of evaluating the code
18782preceding it. This was done by positioning the cursor after the code
18783and typing @kbd{C-x C-e} (@code{eval-last-sexp}) in the usual fashion.
18784You can do this if you are reading this in Info inside of GNU Emacs.
18785
18786@smallexample
18787@group 19775@group
18788(% -1 5) 19776 ;; If we do get all the way thru, make this-command indicate that.
18789 @result{} -1 19777 (if (eq this-command t)
19778 (setq this-command 'yank))
19779 nil)
18790@end group 19780@end group
19781@end smallexample
18791 19782
18792@group 19783The key expression is @code{insert-for-yank}, which inserts the string
18793(setq animals '(cats dogs elephants)) 19784returned by @code{current-kill}, but removes some text properties from
18794 @result{} (cats dogs elephants) 19785it.
18795@end group
18796 19786
18797@group 19787However, before getting to that expression, the function set the value
18798(nthcdr 1 animals) 19788of @code{yank-window-start} to the position returned by the
18799 @result{} (dogs elephants) 19789@code{(window-start)} expression, the position at which the display
18800@end group 19790currently starts. It also set @code{this-command} and pushed the
19791mark.
18801 19792
18802@group 19793After it yanks the appropriate element, if the optional argument is a
18803(nthcdr 0 animals) 19794@sc{cons} rather than a number or nothing, put point at beginning of
18804 @result{} (cats dogs elephants) 19795the yanked text and mark at its end. (The @code{prog1} function is
18805@end group 19796like @code{progn} but returns the value of its first argument rather
19797than the value of its last argument. Its first argument is forced to
19798return the buffer's mark as an integer. You can see the documentation
19799for these functions by placing point over them in this buffer and then
19800typing @kbd{C-h f} (@code{describe-function}) followed by a @kbd{RET};
19801the default is the function.)
18806 19802
18807@group 19803The last part of the function tells what to do when it succeeds.
18808(nthcdr -1 animals)
18809 @result{} (cats dogs elephants)
18810@end group
18811@end smallexample
18812 19804
18813So, if a minus sign or a negative number is passed to @code{yank}, the
18814@code{kill-ring-yank-point} is rotated backwards until it reaches the
18815beginning of the list. Then it stays there. Unlike the other case,
18816when it jumps from the end of the list to the beginning of the list,
18817making a ring, it stops. This makes sense. You often want to get back
18818to the most recently clipped out piece of text, but you don't usually
18819want to insert text from as many as thirty kill commands ago. So you
18820need to work through the ring to get to the end, but won't cycle around
18821it inadvertently if you are trying to come back to the beginning.
18822 19805
18823Incidentally, any number passed to @code{yank} with a minus sign
18824preceding it will be treated as @minus{}1. This is evidently a
18825simplification for writing the program. You don't need to jump back
18826towards the beginning of the kill ring more than one place at a time
18827and doing this is easier than writing a function to determine the
18828magnitude of the number that follows the minus sign.
18829 19806
18830@node yank-pop, ring file, yank, Kill Ring 19807@node yank-pop, ring file, yank, Kill Ring
18831@comment node-name, next, previous, up 19808@comment node-name, next, previous, up
18832@appendixsec @code{yank-pop} 19809@appendixsec @code{yank-pop}
18833@findex yank-pop 19810@findex yank-pop
18834 19811
18835After understanding @code{yank}, the @code{yank-pop} function is easy. 19812After understanding @code{yank} and @code{current-kill}, you know how
18836Leaving out the documentation to save space, it looks like this: 19813to approach the @code{yank-pop} function Leaving out the documentation
19814to save space, it looks like this:
18837 19815
19816@c GNU Emacs 22
18838@smallexample 19817@smallexample
18839@group 19818@group
18840(defun yank-pop (arg) 19819(defun yank-pop (&optional arg)
19820 "@dots{}"
18841 (interactive "*p") 19821 (interactive "*p")
18842 (if (not (eq last-command 'yank)) 19822 (if (not (eq last-command 'yank))
18843 (error "Previous command was not a yank")) 19823 (error "Previous command was not a yank"))
18844@end group 19824@end group
18845@group 19825@group
18846 (setq this-command 'yank) 19826 (setq this-command 'yank)
18847 (let ((before (< (point) (mark)))) 19827 (unless arg (setq arg 1))
18848 (delete-region (point) (mark)) 19828 (let ((inhibit-read-only t)
18849 (rotate-yank-pointer arg) 19829 (before (< (point) (mark t))))
19830@end group
19831@group
19832 (if before
19833 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
19834 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
19835 (setq yank-undo-function nil)
19836@end group
19837@group
19838 (set-marker (mark-marker) (point) (current-buffer))
19839 (insert-for-yank (current-kill arg))
19840 ;; Set the window start back where it was in the yank command,
19841 ;; if possible.
19842 (set-window-start (selected-window) yank-window-start t)
18850@end group 19843@end group
18851@group 19844@group
18852 (set-mark (point)) 19845 (if before
18853 (insert (car kill-ring-yank-pointer)) 19846 ;; This is like exchange-point-and-mark,
18854 (if before (exchange-point-and-mark)))) 19847 ;; but doesn't activate the mark.
19848 ;; It is cleaner to avoid activation, even though the command
19849 ;; loop would deactivate the mark because we inserted text.
19850 (goto-char (prog1 (mark t)
19851 (set-marker (mark-marker)
19852 (point)
19853 (current-buffer))))))
19854 nil)
18855@end group 19855@end group
18856@end smallexample 19856@end smallexample
18857 19857
@@ -18859,20 +19859,20 @@ The function is interactive with a small @samp{p} so the prefix
18859argument is processed and passed to the function. The command can 19859argument is processed and passed to the function. The command can
18860only be used after a previous yank; otherwise an error message is 19860only be used after a previous yank; otherwise an error message is
18861sent. This check uses the variable @code{last-command} which is 19861sent. This check uses the variable @code{last-command} which is
18862discussed elsewhere. (@xref{copy-region-as-kill}.) 19862set by @code{yank} and is discussed elsewhere. (@xref{copy-region-as-kill}.)
18863 19863
18864The @code{let} clause sets the variable @code{before} to true or false 19864The @code{let} clause sets the variable @code{before} to true or false
18865depending whether point is before or after mark and then the region 19865depending whether point is before or after mark and then the region
18866between point and mark is deleted. This is the region that was just 19866between point and mark is deleted. This is the region that was just
18867inserted by the previous yank and it is this text that will be 19867inserted by the previous yank and it is this text that will be
18868replaced. Next the @code{kill-ring-yank-pointer} is rotated so that 19868replaced.
18869the previously inserted text is not reinserted yet again. Mark is set 19869
18870at the beginning of the place the new text will be inserted and then 19870@code{funcall} calls its first argument as a function, passing
18871the first element to which @code{kill-ring-yank-pointer} points is 19871remaining arguments to it. The first argument is whatever the
18872inserted. This leaves point after the new text. If in the previous 19872@code{or} expression returns. The two remaining arguments are the
18873yank, point was left before the inserted text, point and mark are now 19873positions of point and mark set by the preceding @code{yank} command.
18874exchanged so point is again left in front of the newly inserted text. 19874
18875That is all there is to it! 19875There is more, but that is the hardest part.
18876 19876
18877@node ring file, , yank-pop, Kill Ring 19877@node ring file, , yank-pop, Kill Ring
18878@comment node-name, next, previous, up 19878@comment node-name, next, previous, up
@@ -18895,10 +19895,10 @@ body itself.
18895 19895
18896@menu 19896@menu
18897* Labelled Example:: 19897* Labelled Example::
18898* print-graph Varlist:: @code{let} expression in @code{print-graph}. 19898* print-graph Varlist::
18899* print-Y-axis:: Print a label for the vertical axis. 19899* print-Y-axis::
18900* print-X-axis:: Print a horizontal label. 19900* print-X-axis::
18901* Print Whole Graph:: The function to print a complete graph. 19901* Print Whole Graph::
18902@end menu 19902@end menu
18903 19903
18904@node Labelled Example, print-graph Varlist, Full Graph, Full Graph 19904@node Labelled Example, print-graph Varlist, Full Graph, Full Graph
@@ -19073,11 +20073,11 @@ the fifth line from the bottom and on every line that is a multiple of
19073five. 20073five.
19074 20074
19075@menu 20075@menu
19076* Height of label:: What height for the Y axis? 20076* Height of label::
19077* Compute a Remainder:: How to compute the remainder of a division. 20077* Compute a Remainder::
19078* Y Axis Element:: Construct a line for the Y axis. 20078* Y Axis Element::
19079* Y-axis-column:: Generate a list of Y axis labels. 20079* Y-axis-column::
19080* print-Y-axis Penultimate:: A not quite final version. 20080* print-Y-axis Penultimate::
19081@end menu 20081@end menu
19082 20082
19083@node Height of label, Compute a Remainder, print-Y-axis, print-Y-axis 20083@node Height of label, Compute a Remainder, print-Y-axis, print-Y-axis
@@ -19129,10 +20129,7 @@ second argument. As it happens, @code{%} is a function in Emacs Lisp
19129that you cannot discover using @code{apropos}: you find nothing if you 20129that you cannot discover using @code{apropos}: you find nothing if you
19130type @kbd{M-x apropos @key{RET} remainder @key{RET}}. The only way to 20130type @kbd{M-x apropos @key{RET} remainder @key{RET}}. The only way to
19131learn of the existence of @code{%} is to read about it in a book such 20131learn of the existence of @code{%} is to read about it in a book such
19132as this or in the Emacs Lisp sources. The @code{%} function is used 20132as this or in the Emacs Lisp sources.
19133in the code for @code{rotate-yank-pointer}, which is described in an
19134appendix. (@xref{rotate-yk-ptr body, , The Body of
19135@code{rotate-yank-pointer}}.)
19136 20133
19137You can try the @code{%} function by evaluating the following two 20134You can try the @code{%} function by evaluating the following two
19138expressions: 20135expressions:
@@ -19515,8 +20512,8 @@ the width of the symbols that you are using to print the body of the
19515graph without changing the ways the graph is labelled. 20512graph without changing the ways the graph is labelled.
19516 20513
19517@menu 20514@menu
19518* Similarities differences:: Much like @code{print-Y-axis}, but not exactly. 20515* Similarities differences::
19519* X Axis Tic Marks:: Create tic marks for the horizontal axis. 20516* X Axis Tic Marks::
19520@end menu 20517@end menu
19521 20518
19522@node Similarities differences, X Axis Tic Marks, print-X-axis, print-X-axis 20519@node Similarities differences, X Axis Tic Marks, print-X-axis, print-X-axis
@@ -19864,13 +20861,13 @@ Here is the outline:
19864@end smallexample 20861@end smallexample
19865 20862
19866@menu 20863@menu
19867* The final version:: A few changes. 20864* The final version::
19868* Test print-graph:: Run a short test. 20865* Test print-graph::
19869* Graphing words in defuns:: Executing the final code. 20866* Graphing words in defuns::
19870* lambda:: How to write an anonymous function. 20867* lambda::
19871* mapcar:: Apply a function to elements of a list. 20868* mapcar::
19872* Another Bug:: Yet another bug @dots{} most insidious. 20869* Another Bug::
19873* Final printed graph:: The graph itself! 20870* Final printed graph::
19874@end menu 20871@end menu
19875 20872
19876@node The final version, Test print-graph, Print Whole Graph, Print Whole Graph 20873@node The final version, Test print-graph, Print Whole Graph, Print Whole Graph