diff options
| author | Robert J. Chassell | 2006-10-30 22:15:21 +0000 |
|---|---|---|
| committer | Robert J. Chassell | 2006-10-30 22:15:21 +0000 |
| commit | 5fb9c53c678be2bbfdf038eae7f80167014c8e87 (patch) | |
| tree | 5e4e004a8a9de4879c55d173fc6ed142a4230e4e /lispintro | |
| parent | 024ccae39937f2dda8bc2e6f383fd17eec4ac836 (diff) | |
| download | emacs-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/ChangeLog | 9 | ||||
| -rw-r--r-- | lispintro/emacs-lisp-intro.texi | 5667 |
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 @@ | |||
| 1 | 2006-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 | |||
| 1 | 2006-10-29 Chong Yidong <cyd@stupidchicken.com> | 10 | 2006-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 | |||
| 77 | Need 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 | |||
| 280 | every node in every chapter. | 303 | every 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 | ||
| 372 | Setting the Value of a Variable | 405 | Setting 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 | ||
| 378 | Practicing Evaluation | 411 | Practicing 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 | ||
| 389 | How To Write Function Definitions | 422 | How 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 | ||
| 477 | The Interactive Expression in @code{insert-buffer} | 511 | The 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 | |||
| 543 | Initializing 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 | ||
| 588 | Initializing a Variable with @code{defvar} | ||
| 589 | |||
| 590 | * See variable current value:: | ||
| 591 | * defvar and asterisk:: | ||
| 592 | |||
| 559 | How Lists are Implemented | 593 | How Lists are Implemented |
| 560 | 594 | ||
| 561 | * Lists diagrammed:: | 595 | * Lists diagrammed:: |
| @@ -564,9 +598,9 @@ How Lists are Implemented | |||
| 564 | 598 | ||
| 565 | Yanking Text Back | 599 | Yanking 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 | ||
| 571 | Loops and Recursion | 605 | Loops 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 | ||
| 652 | Counting: Repetition and Regexps | 681 | Counting: Repetition and Regexps |
| 653 | 682 | ||
| @@ -721,28 +750,19 @@ Debugging | |||
| 721 | 750 | ||
| 722 | Handling the Kill Ring | 751 | Handling 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 | ||
| 729 | The @code{rotate-yank-pointer} Function | 758 | The @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 | ||
| 734 | The 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 | ||
| 747 | A Graph with Labelled Axes | 767 | A 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 | |||
| 809 | editing in the most general sense of the word.) | 819 | editing 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 | |||
| 1066 | Errors, , Generate an Error Message}.} Lists are the basis of Lisp. | 1076 | Errors, , 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 | |||
| 1151 | using in the lists cannot be divided into any smaller parts and still | 1161 | using in the lists cannot be divided into any smaller parts and still |
| 1152 | mean the same thing as part of a program; likewise with numbers and | 1162 | mean the same thing as part of a program; likewise with numbers and |
| 1153 | single character symbols like @samp{+}. On the other hand, unlike an | 1163 | single character symbols like @samp{+}. On the other hand, unlike an |
| 1154 | atom, a list can be split into parts. (@xref{car cdr & cons, , | 1164 | ancient 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 | ||
| 1157 | In a list, atoms are separated from each other by whitespace. They can be | 1167 | In a list, atoms are separated from each other by whitespace. They can be |
| 1158 | right next to a parenthesis. | 1168 | right next to a parenthesis. |
| @@ -1378,13 +1388,13 @@ C-e}: | |||
| 1378 | 1388 | ||
| 1379 | @noindent | 1389 | @noindent |
| 1380 | What you see depends on which version of Emacs you are running. GNU | 1390 | What you see depends on which version of Emacs you are running. GNU |
| 1381 | Emacs version 21 provides more information than version 20 and before. | 1391 | Emacs version 22 provides more information than version 20 and before. |
| 1382 | First, the more recent result of generating an error; then the | 1392 | First, the more recent result of generating an error; then the |
| 1383 | earlier, version 20 result. | 1393 | earlier, version 20 result. |
| 1384 | 1394 | ||
| 1385 | @need 1250 | 1395 | @need 1250 |
| 1386 | @noindent | 1396 | @noindent |
| 1387 | In GNU Emacs version 21, a @file{*Backtrace*} window will open up and | 1397 | In GNU Emacs version 22, a @file{*Backtrace*} window will open up and |
| 1388 | you will see the following in it: | 1398 | you 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 | |||
| 1501 | of locating the definition or set of instructions. What we see is the | 1511 | of locating the definition or set of instructions. What we see is the |
| 1502 | name through which the instructions can be found. Names of people | 1512 | name through which the instructions can be found. Names of people |
| 1503 | work the same way. I can be referred to as @samp{Bob}; however, I am | 1513 | work the same way. I can be referred to as @samp{Bob}; however, I am |
| 1504 | not the letters @samp{B}, @samp{o}, @samp{b} but am, or were, the | 1514 | not the letters @samp{B}, @samp{o}, @samp{b} but am, or was, the |
| 1505 | consciousness consistently associated with a particular life-form. | 1515 | consciousness consistently associated with a particular life-form. |
| 1506 | The name is not me, but it can be used to refer to me. | 1516 | The 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 | |||
| 1546 | yourself or the computer. | 1556 | yourself 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; | |||
| 1639 | or else produce an error. | 1649 | or 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 |
| 1808 | In GNU Emacs version 21, you will create a @file{*Backtrace*} buffer | 1817 | In GNU Emacs version 22, you will create a @file{*Backtrace*} buffer |
| 1809 | that says: | 1818 | that 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, |
| 1826 | type @kbd{q} in the @file{*Backtrace*} buffer.) | 1835 | type @kbd{q} in the @file{*Backtrace*} buffer.) |
| 1827 | 1836 | ||
| 1837 | @ignore | ||
| 1828 | @need 800 | 1838 | @need 800 |
| 1829 | In GNU Emacs 20 and before, you will produce an error message that says: | 1839 | In 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 |
| 1837 | another key.) | 1847 | another 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 |
| 1857 | In GNU Emacs 21, you will create a @file{*Backtrace*} buffer that | 1868 | In GNU Emacs 22, you will create a @file{*Backtrace*} buffer that |
| 1858 | says: | 1869 | says: |
| 1859 | 1870 | ||
| 1860 | @smallexample | 1871 | @smallexample |
| @@ -1891,6 +1902,7 @@ Since @code{+} does not have a value bound to it, just the function | |||
| 1891 | definition, the error message reported that the symbol's value as a | 1902 | definition, the error message reported that the symbol's value as a |
| 1892 | variable was void. | 1903 | variable was void. |
| 1893 | 1904 | ||
| 1905 | @ignore | ||
| 1894 | @need 800 | 1906 | @need 800 |
| 1895 | In GNU Emacs version 20 and before, your error message will say: | 1907 | In 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 |
| 1902 | The meaning is the same as in GNU Emacs 21. | 1914 | The 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 | |||
| 1945 | have two different function definitions at the same time.)} | 1958 | have 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 |
| 2030 | The value will be a number two more than what you get by evaluating | 2040 | The 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 | ||
| 2034 | As we have just seen, an argument can be a symbol that returns a value | 2044 | As 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 | |||
| 2122 | could not carry out its addition. | 2132 | could not carry out its addition. |
| 2123 | 2133 | ||
| 2124 | @need 1250 | 2134 | @need 1250 |
| 2125 | In GNU Emacs version 21, you will create and enter a | 2135 | In 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 | |||
| 2180 | would have been a number, such as 37, rather than a symbol like | 2190 | would 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 |
| 2184 | In GNU Emacs version 20 and before, the echo area displays an error | 2195 | In GNU Emacs version 20 and before, the echo area displays an error |
| 2185 | message that says: | 2196 | message that says: |
| @@ -2190,6 +2201,7 @@ Wrong type argument:@: number-or-marker-p, hello | |||
| 2190 | 2201 | ||
| 2191 | This says, in different words, the same as the top line of the | 2202 | This 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 | |||
| 2300 | in place of the @samp{%d}; and the value returned by the expression | 2312 | in place of the @samp{%d}; and the value returned by the expression |
| 2301 | beginning with @code{concat} is inserted in place of the @samp{%s}. | 2313 | beginning with @code{concat} is inserted in place of the @samp{%s}. |
| 2302 | 2314 | ||
| 2303 | When I evaluate the expression, the message @code{"He saw 38 red | 2315 | When your fill column is 70 and you evaluate the expression, the |
| 2304 | foxes leaping."} appears in my echo area. | 2316 | message @code{"He saw 38 red foxes leaping."} appears in your echo |
| 2317 | area. | ||
| 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} | |||
| 2319 | work but also illustrate how arguments are passed. | 2332 | work 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 |
| 2341 | The list @code{(rose violet daisy buttercup)} will appear in the echo | 2354 | The list @code{(rose violet daisy buttercup)} will appear in the echo |
| 2342 | area. This is what is @emph{returned} by the @code{set} function. As a | 2355 | area. This is what is @emph{returned} by the @code{set} function. As a |
| 2343 | side effect, the symbol @code{flowers} is bound to the list ; that is, | 2356 | side effect, the symbol @code{flowers} is bound to the list; that is, |
| 2344 | the symbol @code{flowers}, which can be viewed as a variable, is given | 2357 | the symbol @code{flowers}, which can be viewed as a variable, is given |
| 2345 | the list as its value. (This process, by the way, illustrates how a | 2358 | the list as its value. (This process, by the way, illustrates how a |
| 2346 | side effect to the Lisp interpreter, setting the value, can be the | 2359 | side 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. | |||
| 2529 | Then the instructions in the function definition are carried out. | 2542 | Then the instructions in the function definition are carried out. |
| 2530 | 2543 | ||
| 2531 | @item | 2544 | @item |
| 2532 | A single-quote, @code{'}, tells the Lisp interpreter that it should | 2545 | A single quotation mark, |
| 2546 | @ifinfo | ||
| 2547 | ' | ||
| 2548 | @end ifinfo | ||
| 2549 | @ifnotinfo | ||
| 2550 | @code{'} | ||
| 2551 | @end ifnotinfo | ||
| 2552 | , tells the Lisp interpreter that it should | ||
| 2533 | return the following expression as written, and not evaluate it as it | 2553 | return the following expression as written, and not evaluate it as it |
| 2534 | would if the quote were not there. | 2554 | would if the quote were not there. |
| 2535 | 2555 | ||
| @@ -2584,13 +2604,11 @@ these. In another section, we will study the code of several other | |||
| 2584 | buffer-related functions, to see how they were written. | 2604 | buffer-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 | |||
| 2656 | each of the following expressions by positioning the cursor after it and | 2674 | each of the following expressions by positioning the cursor after it and |
| 2657 | typing @kbd{C-x C-e}. | 2675 | typing @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 |
| 2668 | When I do this, @file{"introduction.texinfo"} is the value returned by | 2686 | When I do this in Info, the value returned by evaluating |
| 2669 | evaluating @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 | 2688 | evaluating @code{(buffer-file-name)} is @file{nil}. |
| 2671 | evaluating @code{(buffer-file-name)}. The former is the name of the | 2689 | |
| 2672 | buffer and the latter is the name of the file. (In the expressions, the | 2690 | On the other hand, while I am writing this Introduction, the value |
| 2673 | parentheses tell the Lisp interpreter to treat @code{buffer-name} and | 2691 | returned 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 |
| 2675 | interpreter 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 | ||
| 2697 | The former is the name of the buffer and the latter is the name of the | ||
| 2698 | file. In Info, the buffer name is @file{"*info*"}. Info does not | ||
| 2699 | point to any file, so the result of evaluating | ||
| 2700 | @code{(buffer-file-name)} is @file{nil}. The symbol @code{nil} is | ||
| 2701 | from the Latin word for `nothing'; in this case, it means that the | ||
| 2702 | buffer is not associated with any file. (In Lisp, @code{nil} is also | ||
| 2703 | used to mean `false' and is a synonym for the empty list, @code{()}.) | ||
| 2704 | |||
| 2705 | When I am writing, the name of my buffer is | ||
| 2706 | @file{"introduction.texinfo"}. The name of the file to which it | ||
| 2707 | points is @file{"/gnu/work/intro/introduction.texinfo"}. | ||
| 2708 | |||
| 2709 | (In the expressions, the parentheses tell the Lisp interpreter to | ||
| 2710 | treat @w{@code{buffer-name}} and @w{@code{buffer-file-name}} as | ||
| 2711 | functions; without the parentheses, the interpreter would attempt to | ||
| 2712 | evaluate the symbols as variables. @xref{Variables}.) | ||
| 2677 | 2713 | ||
| 2678 | In spite of the distinction between files and buffers, you will often | 2714 | In spite of the distinction between files and buffers, you will often |
| 2679 | find that people refer to a file when they mean a buffer and vice-versa. | 2715 | find 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 | |||
| 2698 | temporarily before being loaded onto ships; then it became a business | 2734 | temporarily before being loaded onto ships; then it became a business |
| 2699 | and cultural center in its own right. | 2735 | and cultural center in its own right. |
| 2700 | 2736 | ||
| 2701 | Not all buffers are associated with files. For example, when you start | 2737 | Not all buffers are associated with files. For example, a |
| 2702 | an Emacs session by typing the command @code{emacs} alone, without | 2738 | @file{*scratch*} buffer does not visit any file. Similarly, a |
| 2703 | naming any files, Emacs will start with the @file{*scratch*} buffer on | ||
| 2704 | the 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 | 2741 | In the old days, when you lacked a @file{~/.emacs} file and started an |
| 2708 | If you switch to the @file{*scratch*} buffer, type @code{(buffer-name)}, | 2742 | Emacs session by typing the command @code{emacs} alone, without naming |
| 2709 | position the cursor after it, and type @kbd{C-x C-e} to evaluate the | 2743 | any files, Emacs started with the @file{*scratch*} buffer visible. |
| 2710 | expression, the name @code{"*scratch*"} is returned and will appear in | 2744 | Nowadays, you will see a splash screen. You can follow one of the |
| 2711 | the echo area. @code{"*scratch*"} is the name of the buffer. However, | 2745 | commands suggested on the splash screen, visit a file, or press the |
| 2712 | if you type @code{(buffer-file-name)} in the @file{*scratch*} buffer and | 2746 | spacebar to reach the @file{*scratch*} buffer. |
| 2713 | evaluate that, @code{nil} will appear in the echo area. @code{nil} is | 2747 | |
| 2714 | from the Latin word for `nothing'; in this case, it means that the | 2748 | If 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*"} |
| 2717 | list, @code{()}.) | 2751 | will be returned and will appear in the echo area. @code{"*scratch*"} |
| 2752 | is the name of the buffer. When you type @code{(buffer-file-name)} in | ||
| 2753 | the @file{*scratch*} buffer and evaluate that, @code{nil} will appear | ||
| 2754 | in the echo area, just as it does when you evaluate | ||
| 2755 | @code{(buffer-file-name)} in Info. | ||
| 2718 | 2756 | ||
| 2719 | Incidentally, if you are in the @file{*scratch*} buffer and want the | 2757 | Incidentally, if you are in the @file{*scratch*} buffer and want the |
| 2720 | value returned by an expression to appear in the @file{*scratch*} | 2758 | value 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 |
| 2774 | If you evaluate the expression in the usual way, @file{#<buffer *info*>} | 2812 | If you evaluate this expression in Info in Emacs in the usual way, |
| 2775 | appears in the echo area. The special format indicates that the | 2813 | @file{#<buffer *info*>} will appear in the echo area. The special |
| 2776 | buffer itself is being returned, rather than just its name. | 2814 | format indicates that the buffer itself is being returned, rather than |
| 2815 | just its name. | ||
| 2777 | 2816 | ||
| 2778 | Incidentally, while you can type a number or symbol into a program, you | 2817 | Incidentally, while you can type a number or symbol into a program, you |
| 2779 | cannot do that with the printed representation of a buffer: the only way | 2818 | cannot do that with the printed representation of a buffer: the only way |
| 2780 | to get a buffer itself is with a function such as @code{current-buffer}. | 2819 | to get a buffer itself is with a function such as @code{current-buffer}. |
| 2781 | 2820 | ||
| 2782 | A related function is @code{other-buffer}. This returns the most | 2821 | A related function is @code{other-buffer}. This returns the most |
| 2783 | recently selected buffer other than the one you are in currently. If | 2822 | recently selected buffer other than the one you are in currently, not |
| 2784 | you have recently switched back and forth from the @file{*scratch*} | 2823 | a printed representation of its name. If you have recently switched |
| 2785 | buffer, @code{other-buffer} will return that buffer. | 2824 | back and forth from the @file{*scratch*} buffer, @code{other-buffer} |
| 2825 | will return that buffer. | ||
| 2786 | 2826 | ||
| 2787 | @need 800 | 2827 | @need 800 |
| 2788 | You can see this by evaluating the expression: | 2828 | You can see this by evaluating the expression: |
| @@ -2815,12 +2855,13 @@ But first, a brief introduction to the @code{switch-to-buffer} | |||
| 2815 | function. When you switched back and forth from Info to the | 2855 | function. 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 |
| 2817 | likely typed @kbd{C-x b} and then typed @file{*scratch*}@footnote{Or | 2857 | likely typed @kbd{C-x b} and then typed @file{*scratch*}@footnote{Or |
| 2818 | rather, to save typing, you probably typed just part of the name, such | 2858 | rather, to save typing, you probably only typed @kbd{RET} if the |
| 2819 | as @code{*sc}, and then pressed your @kbd{TAB} key to cause it to | 2859 | default buffer was @file{*scratch*}, or if it was different, then you |
| 2820 | expand to the full name; and then typed your @kbd{RET} key.} when | 2860 | typed just part of the name, such as @code{*sc}, pressed your |
| 2821 | prompted 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 |
| 2822 | wanted to switch. The keystrokes, @kbd{C-x b}, cause the Lisp | 2862 | your @kbd{RET} key.} when prompted in the minibuffer for the name of |
| 2823 | interpreter to evaluate the interactive function | 2863 | the buffer to which you wanted to switch. The keystrokes, @kbd{C-x |
| 2864 | b}, 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: |
| 2825 | different keystrokes call or run different functions. For example, | 2866 | different 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 | |||
| 3226 | Emacs. To reload code automatically whenever you start Emacs, see | 3267 | Emacs. 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 |
| 3257 | multiply-by-seven: | 3297 | multiply-by-seven is a Lisp function. |
| 3298 | (multiply-by-seven NUMBER) | ||
| 3299 | |||
| 3258 | Multiply NUMBER by seven. | 3300 | Multiply 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 | |||
| 3329 | each time you typed a key, it would be very distracting. | 3371 | each 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 | |||
| 3436 | subsequent elements of the list to the function @code{message}. | 3478 | subsequent elements of the list to the function @code{message}. |
| 3437 | 3479 | ||
| 3438 | As we have seen, @code{message} is an Emacs Lisp function especially | 3480 | As we have seen, @code{message} is an Emacs Lisp function especially |
| 3439 | designed for sending a one line message to a user. (@xref{message, , The | 3481 | designed for sending a one line message to a user. (@xref{message, , |
| 3440 | @code{message} function}.) | 3482 | The @code{message} function}.) In summary, the @code{message} |
| 3441 | In summary, the @code{message} function prints its first argument in the | 3483 | function prints its first argument in the echo area as is, except for |
| 3442 | echo area as is, except for occurrences of @samp{%d}, @samp{%s}, or | 3484 | occurrences of @samp{%d} or @samp{%s} (and various other %-sequences |
| 3443 | @samp{%c}. When it sees one of these control sequences, the function | 3485 | which we have not mentioned). When it sees a control sequence, the |
| 3444 | looks to the second and subsequent arguments and prints the value of the | 3486 | function looks to the second or subsequent arguments and prints the |
| 3445 | argument in the location in the string where the control sequence is | 3487 | value of the argument in the location in the string where the control |
| 3446 | located. | 3488 | sequence is located. |
| 3447 | 3489 | ||
| 3448 | In the interactive @code{multiply-by-seven} function, the control string | 3490 | In the interactive @code{multiply-by-seven} function, the control string |
| 3449 | is @samp{%d}, which requires a number, and the value returned by | 3491 | is @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 |
| 3479 | For example, the character @samp{r} causes Emacs to pass the beginning | 3521 | Consider the function @code{zap-to-char}. Its interactive expression |
| 3480 | and end of the region (the current values of point and mark) to the | 3522 | is |
| 3481 | function 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 | ||
| 3487 | On the other hand, a @samp{B} tells Emacs to ask for the name of a | 3528 | The first part of the argument to @code{interactive} is @samp{p}, with |
| 3488 | buffer that will be passed to the function. When it sees a @samp{B}, | 3529 | which you are already familiar. This argument tells Emacs to |
| 3489 | Emacs will ask for the name by prompting the user in the minibuffer, | 3530 | interpret a `prefix', as a number to be passed to the function. You |
| 3490 | using a string that follows the @samp{B}, as in @code{"BAppend to | 3531 | can specify a prefix either by typing @kbd{C-u} followed by a number |
| 3491 | buffer:@: "}. Not only will Emacs prompt for the name, but Emacs will | 3532 | or by typing @key{META} followed by a number. The prefix is the |
| 3492 | complete the name if you type enough of it and press @key{TAB}. | 3533 | number of specified characters. Thus, if your prefix is three and the |
| 3534 | specified character is @samp{x}, then you will delete all the text up | ||
| 3535 | to and including the third next @samp{x}. If you do not set a prefix, | ||
| 3536 | then you delete all the text up to and including the specified | ||
| 3537 | character, but no more. | ||
| 3493 | 3538 | ||
| 3494 | A function with two or more arguments can have information passed to | 3539 | The @samp{c} tells the function the name of the character to which to delete. |
| 3495 | each argument by adding parts to the string that follows | 3540 | |
| 3496 | @code{interactive}. When you do this, the information is passed to | 3541 | More formally, a function with two or more arguments can have |
| 3497 | each argument in the same order it is specified in the | 3542 | information passed to each argument by adding parts to the string that |
| 3543 | follows @code{interactive}. When you do this, the information is | ||
| 3544 | passed 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 |
| 3499 | the next part by a @samp{\n}, which is a newline. For example, you | 3546 | the next part by a @samp{\n}, which is a newline. For example, you |
| 3500 | could follow @code{"BAppend to buffer:@: "} with a @samp{\n} and an | 3547 | can 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 | 3548 | This causes Emacs to pass the value of the prefix argument (if there |
| 3502 | to the function as well as prompt you for the buffer---three arguments | 3549 | is one) and the character. |
| 3503 | in all. | ||
| 3504 | 3550 | ||
| 3505 | In this case, the function definition would look like the following, | 3551 | In this case, the function definition looks like the following, where |
| 3506 | where @code{buffer}, @code{start}, and @code{end} are the symbols to | 3552 | @code{arg} and @code{char} are the symbols to which @code{interactive} |
| 3507 | which @code{interactive} binds the buffer and the current values of the | 3553 | binds the prefix argument and the specified character: |
| 3508 | beginning 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 |
| 3521 | are prompted. The @code{append-to-buffer} function looks exactly like | 3566 | are prompted. @xref{copy-to-buffer, , The Definition of |
| 3522 | this. @xref{append-to-buffer, , The Definition of | 3567 | @code{copy-to-buffer}}, for an example.) |
| 3523 | @code{append-to-buffer}}.) | ||
| 3524 | 3568 | ||
| 3525 | If a function does not have arguments, then @code{interactive} does not | 3569 | When a function does not take arguments, @code{interactive} does not |
| 3526 | require any. Such a function contains the simple expression | 3570 | require 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 |
| 3528 | this. | 3572 | this. |
| 3529 | 3573 | ||
| 3530 | Alternatively, if the special letter-codes are not right for your | 3574 | Alternatively, if the special letter-codes are not right for your |
| 3531 | application, you can pass your own arguments to @code{interactive} as | 3575 | application, you can pass your own arguments to @code{interactive} as |
| 3532 | a list. @xref{Using Interactive, , Using @code{Interactive}, elisp, The | 3576 | a list. |
| 3533 | GNU Emacs Lisp Reference Manual}, for more information about this advanced | 3577 | |
| 3534 | technique. | 3578 | @xref{append-to-buffer, , The Definition of @code{append-to-buffer}}, |
| 3579 | for an example. @xref{Using Interactive, , Using @code{Interactive}, | ||
| 3580 | elisp, The GNU Emacs Lisp Reference Manual}, for a more complete | ||
| 3581 | explanation 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 |
| 3568 | On the other hand, if you have code that your whole site will use, it | 3615 | Thirdly, if you have code that your whole site will use, it is usual |
| 3569 | is usual to put it in a file called @file{site-init.el} that is loaded | 3616 | to put it in a file called @file{site-init.el} that is loaded when |
| 3570 | when Emacs is built. This makes the code available to everyone who | 3617 | Emacs is built. This makes the code available to everyone who uses |
| 3571 | uses your machine. (See the @file{INSTALL} file that is part of the | 3618 | your machine. (See the @file{INSTALL} file that is part of the Emacs |
| 3572 | Emacs distribution.) | 3619 | distribution.) |
| 3573 | @end itemize | 3620 | @end itemize |
| 3574 | 3621 | ||
| 3575 | Finally, if you have code that everyone who uses Emacs may want, you | 3622 | Finally, 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 | ||
| 3624 | The @code{let} special form prevents confusion. @code{let} creates a | 3672 | The @code{let} special form prevents confusion. @code{let} creates a |
| 3625 | name for a @dfn{local variable} that overshadows any use of the same | 3673 | name for a @dfn{local variable} that overshadows any use of the same |
| 3626 | name outside the @code{let} expression. This is like understanding | 3674 | name outside the @code{let} expression. This is like understanding |
| @@ -3726,13 +3774,17 @@ Here, the varlist is @code{((zebra 'stripes) (tiger 'fierce))}. | |||
| 3726 | The two variables are @code{zebra} and @code{tiger}. Each variable is | 3774 | The two variables are @code{zebra} and @code{tiger}. Each variable is |
| 3727 | the first element of a two-element list and each value is the second | 3775 | the first element of a two-element list and each value is the second |
| 3728 | element of its two-element list. In the varlist, Emacs binds the | 3776 | element of its two-element list. In the varlist, Emacs binds the |
| 3729 | variable @code{zebra} to the value @code{stripes}, and binds the | 3777 | variable @code{zebra} to the value @code{stripes}@footnote{According |
| 3778 | to Jared Diamond in @cite{Guns, Germs, and Steel}, ``@dots{} zebras | ||
| 3779 | become impossibly dangerous as they grow older'' but the claim here is | ||
| 3780 | that they do not become fierce like a tiger. (1997, W. W. Norton and | ||
| 3781 | Co., ISBN 0-393-03894-2, page 171)}, and binds the | ||
| 3730 | variable @code{tiger} to the value @code{fierce}. In this example, | 3782 | variable @code{tiger} to the value @code{fierce}. In this example, |
| 3731 | both values are symbols preceded by a quote. The values could just as | 3783 | both values are symbols preceded by a quote. The values could just as |
| 3732 | well have been another list or a string. The body of the @code{let} | 3784 | well have been another list or a string. The body of the @code{let} |
| 3733 | follows after the list holding the variables. In this example, the body | 3785 | follows after the list holding the variables. In this example, the |
| 3734 | is a list that uses the @code{message} function to print a string in | 3786 | body is a list that uses the @code{message} function to print a string |
| 3735 | the echo area. | 3787 | in the echo area. |
| 3736 | 3788 | ||
| 3737 | @need 1500 | 3789 | @need 1500 |
| 3738 | You may evaluate the example in the usual fashion, by placing the | 3790 | You 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 |
| 4087 | message @code{"It's not fierce!"} would be printed; and it would be | 4139 | message @code{"It's not fierce!"} would be printed; and it would be |
| 4088 | misleading! When you write code, you need to take into account the | 4140 | misleading! When you write code, you need to take into account the |
| 4089 | possibility that some such argument will be tested by the @code{if} and | 4141 | possibility that some such argument will be tested by the @code{if} |
| 4090 | write your program accordingly.) | 4142 | and 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} | |||
| 4106 | if the result of evaluating it is a value that is not @code{nil}. In | 4158 | if the result of evaluating it is a value that is not @code{nil}. In |
| 4107 | other words, the result of the test is considered true if the value | 4159 | other words, the result of the test is considered true if the value |
| 4108 | returned is a number such as 47, a string such as @code{"hello"}, or a | 4160 | returned is a number such as 47, a string such as @code{"hello"}, or a |
| 4109 | symbol (other than @code{nil}) such as @code{flowers}, or a list, or | 4161 | symbol (other than @code{nil}) such as @code{flowers}, or a list (so |
| 4110 | even a buffer! | 4162 | long 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 | |||
| 4198 | unexpected movement of point or mark. | 4250 | unexpected 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. | |||
| 4256 | In addition to recording the values of point and mark, | 4308 | In 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 |
| 4258 | it, too. This means you can write code that will change the buffer and | 4310 | it, too. This means you can write code that will change the buffer and |
| 4259 | have @code{save-excursion} switch you back to the original buffer. This | 4311 | have @code{save-excursion} switch you back to the original buffer. |
| 4260 | is how @code{save-excursion} is used in @code{append-to-buffer}. | 4312 | This 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 | |||
| 4334 | definition. | 4386 | definition. |
| 4335 | 4387 | ||
| 4336 | @need 1250 | 4388 | @need 1250 |
| 4337 | For example: | 4389 | For example, in an early version of Emacs, the function definition was |
| 4390 | as follows. (It is slightly more complex now that it seeks the first | ||
| 4391 | non-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 | ||
| 4404 | In 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 |
| 4350 | Declare to the interpreter that the function can be used | 4422 | Declare to the interpreter that the function can be used |
| 4351 | interactively. This special form may be followed by a string with one | 4423 | interactively. 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 | |||
| 4487 | be a string or a symbol; the argument used by @samp{%d} must be a | 4559 | be a string or a symbol; the argument used by @samp{%d} must be a |
| 4488 | number. The argument used by @samp{%c} must be an @sc{ascii} code | 4560 | number. The argument used by @samp{%c} must be an @sc{ascii} code |
| 4489 | number; it will be printed as the character with that @sc{ascii} code. | 4561 | number; 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 | |||
| 4567 | buffers. Later, we will study other functions. | 4640 | buffers. 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 | |||
| 4591 | then @key{RET}). | 4662 | then @key{RET}). |
| 4592 | 4663 | ||
| 4593 | @cindex Find source of function | 4664 | @cindex Find source of function |
| 4594 | In 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 | 4666 | When a function is written in Emacs Lisp, @code{describe-function} |
| 4596 | function definition. If you move point over the file name and press | 4667 | will also tell you the location of the function definition. |
| 4668 | |||
| 4669 | Put point into the name of the file that contains the function and | ||
| 4670 | press the @key{RET} key. In this case, @key{RET} means | ||
| 4671 | @code{push-button} rather than `return' or `enter'. Emacs will take | ||
| 4672 | you directly to the function definition. | ||
| 4673 | |||
| 4674 | @ignore | ||
| 4675 | Not In version 22 | ||
| 4676 | |||
| 4677 | If you move point over the file name and press | ||
| 4597 | the @key{RET} key, which in this case means @code{help-follow} rather | 4678 | the @key{RET} key, which in this case means @code{help-follow} rather |
| 4598 | than `return' or `enter', Emacs will take you directly to the function | 4679 | than `return' or `enter', Emacs will take you directly to the function |
| 4599 | definition. | 4680 | definition. |
| 4681 | @end ignore | ||
| 4600 | 4682 | ||
| 4601 | More generally, if you want to see a function in its original source | 4683 | More generally, if you want to see a function in its original source |
| 4602 | file, you can use the @code{find-tags} function to jump to it. | 4684 | file, 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 |
| 4625 | Depending on how the initial default values of your copy of Emacs are | 4707 | Depending 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, | |||
| 4629 | if it has already been created for you, will be in a subdirectory of | 4711 | if it has already been created for you, will be in a subdirectory of |
| 4630 | the @file{/usr/local/share/emacs/} directory; thus you would use the | 4712 | the @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 | 4715 | has not already been created, you will have to create it yourself. It |
| 4634 | not already been created, you will have to create it yourself. | 4716 | will in a file such as @file{/usr/local/src/emacs/src/TAGS}. |
| 4635 | 4717 | ||
| 4636 | @need 1250 | 4718 | @need 1250 |
| 4637 | To create a @file{TAGS} file in a specific directory, switch to that | 4719 | To create a @file{TAGS} file in a specific directory, switch to that |
| 4638 | directory in Emacs using @kbd{M-x cd} command, or list the directory | 4720 | directory in Emacs using @kbd{M-x cd} command, or list the directory |
| 4639 | with @kbd{C-x d} (@code{dired}). Then run the compile command, with | 4721 | with @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 |
| 4643 | M-x compile RET etags *.el RET | 4725 | M-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 |
| 4776 | One arg, a number. Set point to that number. | 4858 | Set point to POSITION, a number or marker. |
| 4777 | Beginning of buffer is position (point-min), | 4859 | Beginning of buffer is position (point-min), end is (point-max). |
| 4778 | end is (point-max). | ||
| 4779 | @end group | 4860 | @end group |
| 4780 | @end smallexample | 4861 | @end smallexample |
| 4781 | 4862 | ||
| 4782 | @noindent | 4863 | @noindent |
| 4864 | The 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 |
| 4784 | under or preceding the cursor, so you can save typing by positioning | 4868 | under or preceding the cursor, so you can save typing by positioning |
| 4785 | the cursor right over or after the function and then typing @kbd{C-h f | 4869 | the 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 | |||
| 4805 | a mark at the end of the buffer. It is generally bound to @kbd{C-x | 4889 | a mark at the end of the buffer. It is generally bound to @kbd{C-x |
| 4806 | h}. | 4890 | h}. |
| 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 |
| 4821 | In GNU Emacs 20, the code for the complete function looks like this: | 4903 | In 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. |
| 4909 | You probably should not use this function in Lisp programs; | ||
| 4910 | it is usually a mistake for a Lisp function to use any subroutine | ||
| 4911 | that 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. | |||
| 4863 | The body of the @code{mark-whole-buffer} function consists of three | 4948 | The body of the @code{mark-whole-buffer} function consists of three |
| 4864 | lines of code: | 4949 | lines 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 | |||
| 4889 | line causes Emacs to determine the position of point and set a mark | 4975 | line causes Emacs to determine the position of point and set a mark |
| 4890 | there. | 4976 | there. |
| 4891 | 4977 | ||
| 4892 | The next line of @code{mark-whole-buffer} is @code{(push-mark (point-max)}. | 4978 | In earlier versions of GNU Emacs, the next line of |
| 4893 | This expression sets a mark at the point in the buffer | 4979 | @code{mark-whole-buffer} was @code{(push-mark (point-max))}. This |
| 4894 | that has the highest number. This will be the end of the buffer (or, | 4980 | expression sets a mark at the point in the buffer that has the highest |
| 4895 | if the buffer is narrowed, the end of the accessible portion of the | 4981 | number. This will be the end of the buffer (or, if the buffer is |
| 4896 | buffer. @xref{Narrowing & Widening, , Narrowing and Widening}, for | 4982 | narrowed, the end of the accessible portion of the buffer. |
| 4897 | more about narrowing.) After this mark has been set, the previous | 4983 | @xref{Narrowing & Widening, , Narrowing and Widening}, for more about |
| 4898 | mark, the one set at point, is no longer set, but Emacs remembers its | 4984 | narrowing.) After this mark has been set, the previous mark, the one |
| 4899 | position, just as all other recent marks are always remembered. This | 4985 | set at point, is no longer set, but Emacs remembers its position, just |
| 4900 | means that you can, if you wish, go back to that position by typing | 4986 | as all other recent marks are always remembered. This means that you |
| 4901 | @kbd{C-u C-@key{SPC}} twice. | 4987 | can, if you wish, go back to that position by typing @kbd{C-u |
| 4988 | C-@key{SPC}} twice. | ||
| 4902 | 4989 | ||
| 4903 | (In GNU Emacs 21, the @code{(push-mark (point-max)} is slightly more | 4990 | @need 1250 |
| 4904 | complicated than shown here. The line reads | 4991 | In GNU Emacs 22, the @code{(point-max)} is slightly more complicated. |
| 4992 | The 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 | 4999 | The expression works nearly the same as before. It sets a mark at the |
| 4912 | the highest numbered place in the buffer that it can. However, in | 5000 | highest numbered place in the buffer that it can. However, in this |
| 4913 | this version, @code{push-mark} has two additional arguments. The | 5001 | version, @code{push-mark} has two additional arguments. The second |
| 4914 | second argument to @code{push-mark} is @code{nil}. This tells the | 5002 | argument to @code{push-mark} is @code{nil}. This tells the function |
| 4915 | function it @emph{should} display a message that says `Mark set' when | 5003 | it @emph{should} display a message that says `Mark set' when it pushes |
| 4916 | it pushes the mark. The third argument is @code{t}. This tells | 5004 | the 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 |
| 4918 | turned on. Transient Mark mode highlights the currently active | 5006 | turned on. Transient Mark mode highlights the currently active |
| 4919 | region. It is usually turned off.) | 5007 | region. It is often turned off. |
| 4920 | 5008 | ||
| 4921 | Finally, the last line of the function is @code{(goto-char | 5009 | Finally, 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 | ||
| 4935 | The @code{append-to-buffer} command is very nearly as simple as the | 5023 | The @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 |
| 4937 | is, 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 |
| 4938 | buffer to a specified buffer. | 5026 | current 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 |
| 4956 | string of characters from part of a buffer, a ``substring'', and | 5044 | string of characters from part of a buffer, a ``substring'', and |
| 4957 | inserts them into another buffer. Most of @code{append-to-buffer} is | 5045 | inserts them into another buffer. |
| 5046 | |||
| 5047 | Most of @code{append-to-buffer} is | ||
| 4958 | concerned with setting up the conditions for | 5048 | concerned 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 |
| 4960 | buffer to which the text will go and the region that will be copied. | 5050 | buffer to which the text will go, the window it comes from and goes |
| 5051 | to, and the region that will be copied. | ||
| 5052 | |||
| 5053 | @need 1250 | ||
| 4961 | Here is the complete text of the function: | 5054 | Here 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 |
| 4971 | When calling from a program, give three arguments: | 5064 | When calling from a program, give three arguments: |
| 4972 | a buffer or the name of one, and two character numbers | 5065 | BUFFER (or buffer name), START and END. |
| 4973 | specifying the portion of the current buffer to be copied." | 5066 | START 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 | |||
| 5000 | will be copied. | 5106 | will be copied. |
| 5001 | 5107 | ||
| 5002 | The next part of the function is the documentation, which is clear and | 5108 | The next part of the function is the documentation, which is clear and |
| 5003 | complete. | 5109 | complete. As is conventional, the three arguments are written in |
| 5110 | upper case so you will notice them easily. Even better, they are | ||
| 5111 | described in the same order as in the argument list. | ||
| 5112 | |||
| 5113 | Note that the documentation distinguishes between a buffer and its | ||
| 5114 | name. (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 | |||
| 5012 | Function Interactive}.) The expression reads as follows: | 5123 | Function 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 |
| 5019 | This expression has an argument inside of quotation marks and that | 5135 | This expression is not one with letters standing for parts, as |
| 5020 | argument has two parts, separated by @samp{\n}. | 5136 | described earlier. Instead, it starts a list with thee parts. |
| 5137 | |||
| 5138 | The first part of the list is an expression to read the name of a | ||
| 5139 | buffer and return it as a string. That is @code{read-buffer}. The | ||
| 5140 | function requires a prompt as its first argument, @samp{"Append to | ||
| 5141 | buffer: "}. Its second argument tells the command what value to | ||
| 5142 | provide if you don't specify anything. | ||
| 5143 | |||
| 5144 | In this case that second argument is an expression containing the | ||
| 5145 | function @code{other-buffer}, an exception, and a @samp{t}, standing | ||
| 5146 | for true. | ||
| 5147 | |||
| 5148 | The first argument to @code{other-buffer}, the exception, is yet | ||
| 5149 | another function, @code{current-buffer}. That is not going to be | ||
| 5150 | returned. The second argument is the symbol for true, @code{t}. that | ||
| 5151 | tells @code{other-buffer} that it may show visible buffers (except in | ||
| 5152 | this case, it will not show the current buffer, which makes sense). | ||
| 5153 | |||
| 5154 | @need 1250 | ||
| 5155 | The expression looks like this: | ||
| 5021 | 5156 | ||
| 5022 | The first part is @samp{BAppend to buffer:@: }. Here, the @samp{B} | 5157 | @smallexample |
| 5023 | tells Emacs to ask for the name of the buffer that will be passed to the | 5158 | (other-buffer (current-buffer) t) |
| 5024 | function. Emacs will ask for the name by prompting the user in the | 5159 | @end smallexample |
| 5025 | minibuffer, using the string following the @samp{B}, which is the string | 5160 | |
| 5026 | @samp{Append to buffer:@: }. Emacs then binds the variable @code{buffer} | 5161 | The second and third arguments to the @code{list} expression are |
| 5027 | in the function's argument list to the specified buffer. | 5162 | @code{(region-beginning)} and @code{(region-end)}. These two |
| 5163 | functions specify the beginning and end of the text to be appended. | ||
| 5164 | |||
| 5165 | @need 1250 | ||
| 5166 | Originally, the command used the letters @samp{B} and @samp{r}. | ||
| 5167 | The whole @code{interactive} expression looked like this: | ||
| 5168 | |||
| 5169 | @smallexample | ||
| 5170 | (interactive "BAppend to buffer:@: \nr") | ||
| 5171 | @end smallexample | ||
| 5172 | |||
| 5173 | @noindent | ||
| 5174 | But when that was done, the default value of the buffer switched to | ||
| 5175 | was invisible. That was not wanted. | ||
| 5028 | 5176 | ||
| 5029 | The newline, @samp{\n}, separates the first part of the argument from | 5177 | (The prompt was separated from the second argument with a newline, |
| 5030 | the 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 |
| 5031 | the two arguments that follow the symbol @code{buffer} in the function's | 5179 | two arguments that follow the symbol @code{buffer} in the function's |
| 5032 | argument list (that is, @code{start} and @code{end}) to the values of | 5180 | argument list (that is, @code{start} and @code{end}) to the values of |
| 5033 | point and mark. | 5181 | point 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 | ||
| 5188 | in 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. | ||
| 5192 | It is inserted into that buffer before its point. | ||
| 5193 | |||
| 5194 | When calling from a program, give three arguments: | ||
| 5195 | BUFFER (or buffer name), START and END. | ||
| 5196 | START 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 | |||
| 5039 | The body of the @code{append-to-buffer} function begins with @code{let}. | 5214 | The body of the @code{append-to-buffer} function begins with @code{let}. |
| 5040 | 5215 | ||
| 5041 | As we have seen before (@pxref{let, , @code{let}}), the purpose of a | 5216 | As 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 |
| 5145 | This formatting convention makes it easy to see that the two lines in | 5320 | This formatting convention makes it easy to see that the lines in |
| 5146 | the body of the @code{save-excursion} are enclosed by the parentheses | 5321 | the body of the @code{save-excursion} are enclosed by the parentheses |
| 5147 | associated with @code{save-excursion}, just as the | 5322 | associated 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 |
| 5176 | In this function, the body of the @code{save-excursion} contains only | 5353 | In this function, the body of the @code{save-excursion} contains only |
| 5177 | two expressions. The body looks like this: | 5354 | one 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 | ||
| 5357 | varlist in sequence, one after another. | ||
| 5178 | 5358 | ||
| 5179 | @smallexample | 5359 | Its critical feature is that variables later in the varlist can make |
| 5180 | @group | 5360 | use 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 | ||
| 5186 | When the @code{append-to-buffer} function is evaluated, the two | 5363 | We will skip functions like @code{let*} and focus on two: the |
| 5187 | expressions in the body of the @code{save-excursion} are evaluated in | 5364 | @code{set-buffer} function and the @code{insert-buffer-substring} |
| 5188 | sequence. The value of the last expression is returned as the value of | 5365 | function. |
| 5189 | the @code{save-excursion} function; the other expression is evaluated | ||
| 5190 | only for its side effects. | ||
| 5191 | 5366 | ||
| 5192 | The 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 | 5368 | In the old days, the @code{set-buffer} expression was simply |
| 5194 | specified in the first argument to @code{append-to-buffer}. (Changing | ||
| 5195 | the buffer is the side effect; as we have said before, in Lisp, a side | ||
| 5196 | effect is often the primary thing we want.) The second line does the | ||
| 5197 | primary work of the function. | ||
| 5198 | 5369 | ||
| 5199 | The @code{set-buffer} function changes Emacs' attention to the buffer to | 5370 | @smallexample |
| 5200 | which the text will be copied and from which @code{save-excursion} will | 5371 | (set-buffer (get-buffer-create buffer)) |
| 5201 | return. | 5372 | @end smallexample |
| 5202 | 5373 | ||
| 5203 | @need 800 | 5374 | @need 1250 |
| 5204 | The line looks like this: | 5375 | @noindent |
| 5376 | but 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 | ||
| 5210 | The innermost expression of this list is @code{(get-buffer-create | 5382 | @noindent |
| 5211 | buffer)}. This expression uses the @code{get-buffer-create} function, | 5383 | @code{append-to} is bound to @code{(get-buffer-create buffer)} earlier |
| 5212 | which either gets the named buffer, or if it does not exist, creates one | 5384 | on in the @code{let*} expression. That extra binding would not be |
| 5213 | with the given name. This means you can use @code{append-to-buffer} to | 5385 | necessary except for that @code{append-to} is used later in the |
| 5214 | put text into a buffer that did not previously exist. | 5386 | varlist as an argument to @code{get-buffer-window-list}. |
| 5387 | |||
| 5388 | @ignore | ||
| 5389 | in 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 | 5405 | The @code{append-to-buffer} function definition inserts text from the |
| 5217 | unnecessary error: @code{set-buffer} needs a buffer to go to; if you | 5406 | buffer in which you are currently to a named buffer. It happens that |
| 5218 | were to specify a buffer that does not exist, Emacs would baulk. | 5407 | @code{insert-buffer-substring} copies text from another buffer to the |
| 5219 | Since @code{get-buffer-create} will create a buffer if none exists, | 5408 | current 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 |
| 5410 | binds the local symbol @code{oldbuf} to the value returned by | ||
| 5411 | @code{current-buffer}. | ||
| 5221 | 5412 | ||
| 5222 | @need 1250 | 5413 | @need 1250 |
| 5223 | The last line of @code{append-to-buffer} does the work of appending | 5414 | The @code{insert-buffer-substring} expression looks like this: |
| 5224 | the 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 |
| 5231 | The @code{insert-buffer-substring} function copies a string @emph{from} | 5421 | The @code{insert-buffer-substring} function copies a string |
| 5232 | the buffer specified as its first argument and inserts the string into | 5422 | @emph{from} the buffer specified as its first argument and inserts the |
| 5233 | the present buffer. In this case, the argument to | 5423 | string 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 |
| 5235 | bound by the @code{let}, namely the value of @code{oldbuf}, which was | 5425 | and bound by the @code{let}, namely the value of @code{oldbuf}, which |
| 5236 | the current buffer when you gave the @code{append-to-buffer} command. | 5426 | was the current buffer when you gave the @code{append-to-buffer} |
| 5427 | command. | ||
| 5237 | 5428 | ||
| 5238 | After @code{insert-buffer-substring} has done its work, | 5429 | After @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. | 5431 | and @code{append-to-buffer} will have done its job. |
| 5241 | 5432 | ||
| 5242 | @need 800 | 5433 | @need 800 |
| 5243 | Written in skeletal form, the workings of the body look like this: | 5434 | Written 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 | ||
| 5258 | In summary, @code{append-to-buffer} works as follows: it saves the value | 5448 | In summary, @code{append-to-buffer} works as follows: it saves the |
| 5259 | of the current buffer in the variable called @code{oldbuf}. It gets the | 5449 | value of the current buffer in the variable called @code{oldbuf}. It |
| 5260 | new buffer, creating one if need be, and switches Emacs to it. Using | 5450 | gets the new buffer (creating one if need be) and switches Emacs' |
| 5261 | the value of @code{oldbuf}, it inserts the region of text from the old | 5451 | attention to it. Using the value of @code{oldbuf}, it inserts the |
| 5262 | buffer into the new buffer; and then using @code{save-excursion}, it | 5452 | region of text from the old buffer into the new buffer; and then using |
| 5263 | brings you back to your original buffer. | 5453 | @code{save-excursion}, it brings you back to your original buffer. |
| 5264 | 5454 | ||
| 5265 | In looking at @code{append-to-buffer}, you have explored a fairly | 5455 | In looking at @code{append-to-buffer}, you have explored a fairly |
| 5266 | complex function. It shows how to use @code{let} and | 5456 | complex function. It shows how to use @code{let} and |
| @@ -5351,10 +5541,9 @@ use of an asterisk in an @code{interactive} expression, use of | |||
| 5351 | to which the name refers. | 5541 | to 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 | ||
| 5367 | After understanding how @code{append-to-buffer} works, it is easy to | 5556 | After understanding how @code{append-to-buffer} works, it is easy to |
| 5368 | understand @code{copy-to-buffer}. This function copies text into a | 5557 | understand @code{copy-to-buffer}. This function copies text into a |
| 5369 | buffer, but instead of adding to the second buffer, it replaces the | 5558 | buffer, but instead of adding to the second buffer, it replaces all the |
| 5370 | previous text in the second buffer. The code for the | 5559 | previous 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 | ||
| 5374 | Definition of @code{append-to-buffer}}, for the description of | ||
| 5375 | @code{append-to-buffer}.) | ||
| 5376 | 5560 | ||
| 5377 | @need 800 | 5561 | @need 800 |
| 5378 | The body of @code{copy-to-buffer} looks like this | 5562 | The 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 | ||
| 5393 | This code is similar to the code in @code{append-to-buffer}: it is | 5577 | The @code{copy-to-buffer} function has a simpler @code{interactive} |
| 5394 | only after changing to the buffer to which the text will be copied | 5578 | expression than @code{append-to-buffer}. |
| 5395 | that 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 |
| 5397 | buffer's former contents. (This is what is meant by `replacement'; to | 5581 | The definition then says |
| 5398 | replace text, Emacs erases the previous text and then inserts new | 5582 | |
| 5399 | text.) 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{} |
| 5401 | inserted. | 5585 | @end smallexample |
| 5586 | |||
| 5587 | First, look at the earliest inner expression; that is evaluated first. | ||
| 5588 | That expression starts with @code{get-buffer-create buffer}. The | ||
| 5589 | function tells the computer to use the buffer with the name specified | ||
| 5590 | as the one to which you are copying, or if such a buffer does not | ||
| 5591 | exist, to create it. Then, the @code{with-current-buffer} function | ||
| 5592 | evaluates its body with that buffer temporarily current. | ||
| 5402 | 5593 | ||
| 5403 | Why is @code{save-excursion} used twice? Consider again what the | 5594 | (This demonstrates another way to shift the computer's attention but |
| 5404 | function does. | 5595 | not the user's. The @code{append-to-buffer} function showed how to do |
| 5596 | the same with @code{save-excursion} and @code{set-buffer}. | ||
| 5597 | @code{with-current-buffer} is a newer, and arguably easier, | ||
| 5598 | mechanism.) | ||
| 5599 | |||
| 5600 | The @code{barf-if-buffer-read-only} function sends you an error | ||
| 5601 | message saying the buffer is read-only if you cannot modify it. | ||
| 5602 | |||
| 5603 | The next line has the @code{erase-buffer} function as its sole | ||
| 5604 | contents. That function erases the buffer. | ||
| 5605 | |||
| 5606 | Finally, the last two lines contain the @code{save-excursion} | ||
| 5607 | expression with @code{insert-buffer-substring} as its body. | ||
| 5608 | The @code{insert-buffer-substring} expression copies the text from | ||
| 5609 | the buffer you are in (and you have not seen the computer shift its | ||
| 5610 | attention, so you don't know that that buffer is now called | ||
| 5611 | @code{oldbuf}). | ||
| 5612 | |||
| 5613 | Incidently, this is what is meant by `replacement'. To replace text, | ||
| 5614 | Emacs erases the previous text and then inserts new text. | ||
| 5405 | 5615 | ||
| 5406 | @need 1250 | 5616 | @need 1250 |
| 5407 | In outline, the body of @code{copy-to-buffer} looks like this: | 5617 | In 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 | ||
| 5420 | The first use of @code{save-excursion} returns Emacs to the buffer from | ||
| 5421 | which the text is being copied. That is clear, and is just like its use | ||
| 5422 | in @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 | ||
| 5424 | the region being inserted. The second @code{save-excursion} causes | ||
| 5425 | Emacs to leave point at the beginning of the text being inserted. In | ||
| 5426 | most circumstances, users prefer to find point at the beginning of | ||
| 5427 | inserted text. (Of course, the @code{copy-to-buffer} function returns | ||
| 5428 | the user to the original buffer when done---but if the user @emph{then} | ||
| 5429 | switches to the copied-to buffer, point will go to the beginning of the | ||
| 5430 | text. Thus, this use of a second @code{save-excursion} is a little | ||
| 5431 | nicety.) | ||
| 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. | |||
| 5443 | Here is a discussion based on the original code. The code was | 5640 | Here is a discussion based on the original code. The code was |
| 5444 | simplified in 2003 and is harder to understand. | 5641 | simplified in 2003 and is harder to understand. |
| 5445 | 5642 | ||
| 5643 | @xref{New insert-buffer, , New Body for @code{insert-buffer}}, to see | ||
| 5644 | a discussion of the new body.) | ||
| 5645 | |||
| 5446 | In addition, this code illustrates the use of @code{interactive} with a | 5646 | In addition, this code illustrates the use of @code{interactive} with a |
| 5447 | buffer that might be @dfn{read-only} and the important distinction | 5647 | buffer that might be @dfn{read-only} and the important distinction |
| 5448 | between the name of an object and the object actually referred to. | 5648 | between 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 |
| 5465 | Here is the code: | 5666 | Here 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 | |||
| 5511 | buffer:@: }. | 5712 | buffer:@: }. |
| 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 | |||
| 5546 | enabled. If the buffer does not exist, you receive a message that | 5747 | enabled. If the buffer does not exist, you receive a message that |
| 5547 | says ``No match''; your terminal may beep at you as well. | 5748 | says ``No match''; your terminal may beep at you as well. |
| 5548 | 5749 | ||
| 5750 | The new and simplified code generates a list for @code{interactive}. | ||
| 5751 | It uses the @code{barf-if-buffer-read-only} and @code{read-buffer} | ||
| 5752 | functions with which we are already familiar and the @code{progn} | ||
| 5753 | special 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 | |||
| 5821 | use @code{or}. All these functions are building blocks that we will | 6027 | use @code{or}. All these functions are building blocks that we will |
| 5822 | find and use again and again. | 6028 | find 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 | |||
| 6036 | The body in the GNU Emacs 22 version is more confusing than the original. | ||
| 6037 | |||
| 6038 | @need 1250 | ||
| 6039 | It 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 | ||
| 6053 | except, and this is what confuses novices, very important work is done | ||
| 6054 | inside the @code{push-mark} expression. | ||
| 6055 | |||
| 6056 | The @code{get-buffer} function returns a buffer with the name | ||
| 6057 | provided. 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 | ||
| 6059 | already exist. The buffer returned by @code{get-buffer}, an existing | ||
| 6060 | buffer, is passed to @code{insert-buffer-substring}, which inserts the | ||
| 6061 | whole of the buffer (since you did not specify anything else). | ||
| 6062 | |||
| 6063 | The location into which the buffer is inserted is recorded by | ||
| 6064 | @code{push-mark}. Then the function returns @code{nil}, the value of | ||
| 6065 | its last command. Put another way, the @code{insert-buffer} function | ||
| 6066 | exists only to produce a side effect, inserting another buffer, not to | ||
| 6067 | return 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 | ||
| 5834 | As previously described, when invoked without an argument, | 6079 | As 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 |
| 5836 | buffer, leaving the mark at the previous position. However, when the | 6081 | buffer (in truth, the accessible portion of the buffer), leaving the |
| 5837 | command is invoked with a number between one and ten, the function | 6082 | mark at the previous position. However, when the command is invoked |
| 5838 | considers that number to be a fraction of the length of the buffer, | 6083 | with a number between one and ten, the function considers that number |
| 5839 | measured in tenths, and Emacs moves the cursor that fraction of the way | 6084 | to be a fraction of the length of the buffer, measured in tenths, and |
| 5840 | from the beginning of the buffer. Thus, you can either call this | 6085 | Emacs moves the cursor that fraction of the way from the beginning of |
| 5841 | function with the key command @kbd{M-<}, which will move the cursor to | 6086 | the buffer. Thus, you can either call this function with the key |
| 5842 | the beginning of the buffer, or with a key command such as @kbd{C-u 7 | 6087 | command @kbd{M-<}, which will move the cursor to the beginning of the |
| 5843 | M-<} which will move the cursor to a point 70% of the way through the | 6088 | buffer, or with a key command such as @kbd{C-u 7 M-<} which will move |
| 5844 | buffer. If a number bigger than ten is used for the argument, it moves | 6089 | the cursor to a point 70% of the way through the buffer. If a number |
| 5845 | to the end of the buffer. | 6090 | bigger than ten is used for the argument, it moves to the end of the |
| 6091 | buffer. | ||
| 5846 | 6092 | ||
| 5847 | The @code{beginning-of-buffer} function can be called with or without an | 6093 | The @code{beginning-of-buffer} function can be called with or without an |
| 5848 | argument. The use of the argument is optional. | 6094 | argument. 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 |
| 5867 | However, optional arguments are a feature of Lisp: a @dfn{keyword} may | 6113 | However, optional arguments are a feature of Lisp: a particular |
| 5868 | be 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 |
| 5869 | The keyword is @code{&optional}. (The @samp{&} in front of | 6115 | optional. 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 |
| 5871 | an argument follows the keyword @code{&optional}, a value does not | 6117 | an argument follows the keyword @code{&optional}, no value need be |
| 5872 | need to be passed to that argument when the function is called. | 6118 | passed to that argument when the function is called. |
| 5873 | 6119 | ||
| 5874 | @need 1200 | 6120 | @need 1200 |
| 5875 | The first line of the function definition of @code{beginning-of-buffer} | 6121 | The 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} | |||
| 5900 | function except that the @code{interactive} expression has @code{"P"} | 6150 | function except that the @code{interactive} expression has @code{"P"} |
| 5901 | as an argument and the @code{goto-char} function is followed by an | 6151 | as an argument and the @code{goto-char} function is followed by an |
| 5902 | if-then-else expression that figures out where to put the cursor if | 6152 | if-then-else expression that figures out where to put the cursor if |
| 5903 | there is an argument. | 6153 | there is an argument that is not a cons cell. |
| 5904 | 6154 | ||
| 5905 | The @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 |
| 5906 | a prefix argument, if there is one, to the function. A prefix argument | 6156 | consider ignoring the function @code{consp}. @xref{List |
| 5907 | is made by typing the @key{META} key followed by a number, or by typing | 6157 | Implementation, , 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 |
| 5909 | defaults to 4). | 6159 | Manual}.) |
| 5910 | 6160 | ||
| 5911 | The true-or-false-test of the @code{if} expression is simple: it is | 6161 | The @code{"P"} in the @code{interactive} expression tells Emacs to |
| 5912 | simply the argument @code{arg}. If @code{arg} has a value that is not | 6162 | pass 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 | 6163 | A prefix argument is made by typing the @key{META} key followed by a |
| 5914 | called with an argument, then this true-or-false-test will return true | 6164 | number, or by typing @kbd{C-u} and then a number. (If you don't type |
| 5915 | and the then-part of the @code{if} expression will be evaluated. On the | 6165 | a 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 | ||
| 6167 | convert a prefix arg to a number.) | ||
| 6168 | |||
| 6169 | The true-or-false-test of the @code{if} expression looks complex, but | ||
| 6170 | it 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} | ||
| 6172 | does; it checks whether its argument is a cons cell.) If @code{arg} | ||
| 6173 | has a value that is not @code{nil} (and is not a cons cell), which | ||
| 6174 | will be the case if @code{beginning-of-buffer} is called with a | ||
| 6175 | numeric argument, then this true-or-false-test will return true and | ||
| 6176 | the then-part of the @code{if} expression will be evaluated. On the | ||
| 5916 | other hand, if @code{beginning-of-buffer} is not called with an | 6177 | other hand, if @code{beginning-of-buffer} is not called with an |
| 5917 | argument, the value of @code{arg} will be @code{nil} and the else-part | 6178 | argument, the value of @code{arg} will be @code{nil} and the else-part |
| 5918 | of the @code{if} expression will be evaluated. The else-part is simply | 6179 | of the @code{if} expression will be evaluated. The else-part is |
| 5919 | @code{point-min}, and when this is the outcome, the whole | 6180 | simply @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 |
| 5921 | how we saw the @code{beginning-of-buffer} function in its simplified | 6182 | is how we saw the @code{beginning-of-buffer} function in its |
| 5922 | form. | 6183 | simplified 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 | ||
| 5985 | In @code{beginning-of-buffer}, the inner @code{if} expression tests | 6247 | In @code{beginning-of-buffer}, the inner @code{if} expression tests |
| 5986 | whether the size of the buffer is greater than 10,000 characters. To do | 6248 | whether the size of the buffer is greater than 10,000 characters. To do |
| 5987 | this, it uses the @code{>} function and the @code{buffer-size} function. | 6249 | this, it uses the @code{>} function and the computation of @code{size} |
| 6250 | that comes from the let expression. | ||
| 6251 | |||
| 6252 | In the old days, the function @code{buffer-size} was used. Not only | ||
| 6253 | was that function called several times, it gave the size of the whole | ||
| 6254 | buffer, not the accessible part. The computation makes much more | ||
| 6255 | sense when it handles just the accessible part. (@xref{Narrowing & | ||
| 6256 | Widening, , Narrowing and Widening}, for more information on focusing | ||
| 6257 | attention to an `accessible' part.) | ||
| 5988 | 6258 | ||
| 5989 | @need 800 | 6259 | @need 800 |
| 5990 | The line looks like this: | 6260 | The 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 |
| 6022 | The second argument is @code{(/ (buffer-size) 10)}. This expression | 6292 | The second argument is @code{(/ size 10)}. This expression divides |
| 6023 | divides the numeric value of the buffer by ten. This produces a number | 6293 | the numeric value by ten --- the numeric value of the size of the |
| 6024 | that tells how many characters make up one tenth of the buffer size. | 6294 | accessible portion of the buffer. This produces a number that tells |
| 6025 | (In Lisp, @code{/} is used for division, just as @code{*} is | 6295 | how many characters make up one tenth of the buffer size. (In Lisp, |
| 6026 | used for multiplication.) | 6296 | @code{/} is used for division, just as @code{*} is used for |
| 6297 | multiplication.) | ||
| 6027 | 6298 | ||
| 6028 | @need 1200 | 6299 | @need 1200 |
| 6029 | In the multiplication expression as a whole, this amount is multiplied | 6300 | In 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 |
| 6040 | If, for example, the prefix argument is @samp{7}, the one-tenth value | 6311 | If, for example, the prefix argument is @samp{7}, the one-tenth value |
| 6041 | will be multiplied by 7 to give a position 70% of the way through the | 6312 | will be multiplied by 7 to give a position 70% of the way through. |
| 6042 | buffer. | ||
| 6043 | 6313 | ||
| 6044 | @need 1200 | 6314 | @need 1200 |
| 6045 | The result of all this is that if the buffer is large, the | 6315 | The result of all this is that if the accessible portion of the buffer |
| 6046 | @code{goto-char} expression reads like this: | 6316 | is 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 |
| 6095 | Looking at parentheses, we see that the innermost operation is | 6365 | Looking 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 |
| 6097 | number. This number is multiplied by the buffer size in the following | 6367 | a number. In the following expression, this number is multiplied by |
| 6098 | expression: | 6368 | the 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. | |||
| 6120 | Here is the complete text of the @code{beginning-of-buffer} function: | 6390 | Here 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; | ||
| 6398 | leave mark at previous position. | ||
| 6399 | With \\[universal-argument] prefix, | ||
| 6400 | do not set mark at previous position. | ||
| 6401 | With numeric arg N, | ||
| 6402 | put point N/10 of the way from the beginning. | ||
| 6403 | |||
| 6404 | If the buffer is narrowed, | ||
| 6405 | this command uses the beginning and size | ||
| 6406 | of the accessible part of the buffer. | ||
| 6407 | @end group | ||
| 6408 | |||
| 6409 | @group | ||
| 6410 | Don't use this command in Lisp programs! | ||
| 6411 | \(goto-char (point-min)) is faster | ||
| 6412 | and 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 | ||
| 6433 | From 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 |
| 6156 | Except for two small points, the previous discussion shows how this | 6468 | Except for two small points, the previous discussion shows how this |
| @@ -6162,18 +6474,21 @@ the function. | |||
| 6162 | In the documentation string, there is reference to an expression: | 6474 | In 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 |
| 6169 | A @samp{\} is used before the first parenthesis of this expression. | 6481 | A @samp{\\} is used before the first square bracket of this |
| 6170 | This @samp{\} tells the Lisp interpreter that the expression should be | 6482 | expression. This @samp{\\} tells the Lisp interpreter to substitute |
| 6171 | printed as shown in the documentation rather than evaluated as a | 6483 | whatever key is currently bound to the @samp{[@dots{}]}. In the case |
| 6172 | symbolic expression, which is what it looks like. | 6484 | of @code{universal-argument}, that is usually @kbd{C-u}, but it might |
| 6485 | be different. (@xref{Documentation Tips, , Tips for Documentation | ||
| 6486 | Strings, elisp, The GNU Emacs Lisp Reference Manual}, for more | ||
| 6487 | information.) | ||
| 6173 | 6488 | ||
| 6174 | @need 1200 | 6489 | @need 1200 |
| 6175 | Finally, the last line of the @code{beginning-of-buffer} command says to | 6490 | Finally, the last line of the @code{beginning-of-buffer} command says |
| 6176 | move point to the beginning of the next line if the command is | 6491 | to move point to the beginning of the next line if the command is |
| 6177 | invoked with an argument: | 6492 | invoked 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 | |||
| 6185 | appropriate tenths position in the buffer. This is a flourish that | 6500 | appropriate tenths position in the buffer. This is a flourish that |
| 6186 | means that the cursor is always located @emph{at least} the requested | 6501 | means that the cursor is always located @emph{at least} the requested |
| 6187 | tenths of the way through the buffer, which is a nicety that is, | 6502 | tenths of the way through the buffer, which is a nicety that is, |
| 6188 | perhaps, not necessary, but which, if it did not occur, would be sure to | 6503 | perhaps, not necessary, but which, if it did not occur, would be sure |
| 6189 | draw complaints. | 6504 | to draw complaints. |
| 6505 | |||
| 6506 | On the other hand, it also means that if you specify the command with | ||
| 6507 | a @kbd{C-u}, but without a number, that is to say, if the `raw prefix | ||
| 6508 | argument' is simply a cons cell, then the command puts you at the | ||
| 6509 | beginning of the second line @dots{} I don't know whether this is | ||
| 6510 | intended or whether no one has dealt with the code to avoid this | ||
| 6511 | happening. | ||
| 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 | |||
| 6200 | argument that is not @code{nil}; if none return a value that is not | 6522 | argument 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 |
| 6202 | of the arguments; return a true value if one @emph{or} any of the | 6524 | of the arguments; return a true value if one @emph{or} any of the |
| 6203 | other are true. | 6525 | others are true. |
| 6204 | 6526 | ||
| 6205 | @item and | 6527 | @item and |
| 6206 | Evaluate each argument in sequence, and if any are @code{nil}, return | 6528 | Evaluate 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 | |||
| 6254 | novices. | 6576 | novices. |
| 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 | ||
| 6685 | Emacs 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. | ||
| 6703 | If POS is nil, use current buffer location. | ||
| 6704 | Counting starts at (point-min), so the value refers | ||
| 6705 | to 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. | ||
| 6716 | This is usually the number of newlines between them, | ||
| 6717 | but can be one more if START is not equal to END | ||
| 6718 | and 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 | |||
| 6389 | well as your line number in a widened buffer. The recent version is | 6765 | well as your line number in a widened buffer. The recent version is |
| 6390 | more complex than the version shown here. If you feel adventurous, | 6766 | more complex than the version shown here. If you feel adventurous, |
| 6391 | you might want to look at it after figuring out how this version | 6767 | you might want to look at it after figuring out how this version |
| 6392 | works. The newer version uses a conditional to determine whether the | 6768 | works. You will probably need to use @kbd{C-h f} |
| 6393 | buffer has been narrowed, and rather than use @code{beginning-of-line} | 6769 | (@code{describe-function}). The newer version uses a conditional to |
| 6394 | to move point to the beginning of the current line, if need be, the | 6770 | determine whether the buffer has been narrowed. |
| 6395 | function uses @code{(forward-line 0)}.) | ||
| 6396 | 6771 | ||
| 6397 | The function as shown here has a documentation line and is | 6772 | (Also, it uses @code{line-number-at-pos}, which among other simple |
| 6398 | interactive, as you would expect. The next two lines use the | 6773 | expressions, such as @code{(goto-char (point-min))}, moves point to |
| 6774 | the beginning of the current line with @code{(forward-line 0)} rather | ||
| 6775 | than @code{beginning-of-line}.) | ||
| 6776 | |||
| 6777 | The @code{what-line} function as shown here has a documentation line | ||
| 6778 | and is interactive, as you would expect. The next two lines use the | ||
| 6399 | functions @code{save-restriction} and @code{widen}. | 6779 | functions @code{save-restriction} and @code{widen}. |
| 6400 | 6780 | ||
| 6401 | The @code{save-restriction} special form notes whatever narrowing is in | 6781 | The @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 | ||
| 6437 | The @code{message} function prints a one-line message at the bottom of the | 6817 | The @code{message} function prints a one-line message at the bottom of |
| 6438 | Emacs screen. The first argument is inside of quotation marks and is | 6818 | the Emacs screen. The first argument is inside of quotation marks and |
| 6439 | printed as a string of characters. However, it may contain @samp{%d}, | 6819 | is 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 | 6821 | the argument as a decimal, so the message will say something such as |
| 6442 | something such as @samp{Line 243}. | 6822 | @samp{Line 243}. |
| 6443 | 6823 | ||
| 6444 | @need 1200 | 6824 | @need 1200 |
| 6445 | |||
| 6446 | The number that is printed in place of the @samp{%d} is computed by the | 6825 | The number that is printed in place of the @samp{%d} is computed by the |
| 6447 | last line of the function: | 6826 | last 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 | ||
| 6833 | GNU Emacs 22 | ||
| 6834 | |||
| 6835 | (defun count-lines (start end) | ||
| 6836 | "Return number of lines between START and END. | ||
| 6837 | This is usually the number of newlines between them, | ||
| 6838 | but can be one more if START is not equal to END | ||
| 6839 | and 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 |
| 6454 | What this does is count the lines from the first position of the | 6860 | What this does is count the lines from the first position of the |
| 6455 | buffer, indicated by the @code{1}, up to @code{(point)}, and then add | 6861 | buffer, 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 |
| 6479 | have to investigate yourself; or perhaps you will have to use | 6885 | have 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 |
| 6481 | and one that introduces text properties, a feature otherwise not | 6887 | @code{filter-buffer-substring} @dots{}, yet other functions. Text |
| 6482 | discussed here. @xref{Text Properties, , Text Properties, elisp, The | 6888 | properties are a feature otherwise not discussed here. @xref{Text |
| 6483 | GNU Emacs Lisp Reference Manual}. Additionally, do you really need | 6889 | Properties, , Text Properties, elisp, The GNU Emacs Lisp Reference |
| 6484 | @code{goto-char} or @code{point-min}? Or can you write the function | 6890 | Manual}. |
| 6485 | without them?) | 6891 | |
| 6892 | Additionally, do you really need @code{goto-char} or @code{point-min}? | ||
| 6893 | Or 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}, | |||
| 6499 | namely, @code{setcdr} and @code{nthcdr}. (@xref{copy-region-as-kill}.) | 6907 | namely, @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: | |||
| 6798 | What you see, if you evaluate this, is the error message | 7206 | What you see, if you evaluate this, is the error message |
| 6799 | 7207 | ||
| 6800 | @smallexample | 7208 | @smallexample |
| 6801 | Wrong number of arguments: #<subr length>, 0 | 7209 | Lisp 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 | |||
| 6808 | length the function is measuring. (Note that @emph{one} list is | 7216 | length 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 | ||
| 6811 | The part of the error message that says @samp{#<subr length>} is the | 7219 | The part of the error message that says @samp{length} is the name of |
| 6812 | name of the function. This is written with a special notation, | 7220 | the function. |
| 6813 | @samp{#<subr}, that indicates that the function @code{length} is one | 7221 | |
| 6814 | of 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. |
| 6816 | Function, , What Is a Function?, elisp , The GNU Emacs Lisp Reference | 7224 | |
| 6817 | Manual}, for more about subroutines. | 7225 | In 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 |
| 7017 | If you are reading this in Info inside of GNU Emacs, you can evaluate | 7432 | If you are reading this in Info inside of GNU Emacs, you can evaluate |
| 7018 | this expression in the usual fashion, by positioning the cursor after | 7433 | this expression in the usual fashion, by positioning the cursor after |
| 7019 | the expression and typing @kbd{C-x C-e}. (I'm doing this right here as | 7434 | the expression and typing @kbd{C-x C-e}. (I'm doing this right here |
| 7020 | I write this. This is one of the advantages of having the interpreter | 7435 | as I write this. This is one of the advantages of having the |
| 7021 | built into the computing environment.) | 7436 | interpreter built into the computing environment. Incidently, when |
| 7437 | there is nothing on the line after the final parentheses, such as a | ||
| 7438 | comment, point can be on the next line. Thus, if your cursor is in | ||
| 7439 | the first column of the next line, you do not need to move it. | ||
| 7440 | Indeed, Emacs permits any amount of white space after the final | ||
| 7441 | parenthesis.) | ||
| 7022 | 7442 | ||
| 7023 | @need 1200 | 7443 | @need 1200 |
| 7024 | When we evaluate the variable @code{animals}, we see that it is bound to | 7444 | When 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 | |||
| 7156 | sources with `clip' and all occurrences of `killed' with `clipped'.) | 7576 | sources 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 |
| 7184 | The function @code{cons} can be used to to create a new list from a | 7604 | The function @code{cons} can be used to create a new list from a piece |
| 7185 | piece of text (an `atom', to use the jargon) and an existing list, | 7605 | of text (an `atom', to use the jargon) and an existing list, like |
| 7186 | like this: | 7606 | this: |
| 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 | ||
| 7241 | The @code{zap-to-char} function barely changed between GNU Emacs | 7661 | The @code{zap-to-char} function changed a little between GNU Emacs |
| 7242 | version 19 and GNU Emacs version 21. However, @code{zap-to-char} | 7662 | version 19 and GNU Emacs version 22. However, @code{zap-to-char} |
| 7243 | calls another function, @code{kill-region}, which enjoyed a major rewrite | 7663 | calls another function, @code{kill-region}, which enjoyed a major |
| 7244 | on the way to version 21. | 7664 | rewrite. |
| 7245 | 7665 | ||
| 7246 | The @code{kill-region} function in Emacs 19 is complex, but does not | 7666 | The @code{kill-region} function in Emacs 19 is complex, but does not |
| 7247 | use code that is important at this time. We will skip it. | 7667 | use code that is important at this time. We will skip it. |
| 7248 | 7668 | ||
| 7249 | The @code{kill-region} function in Emacs 21 is easier to read than the | 7669 | The @code{kill-region} function in Emacs 22 is easier to read than the |
| 7250 | same function in Emacs 19 and introduces a very important concept, | 7670 | same function in Emacs 19 and introduces a very important concept, |
| 7251 | that of error handling. We will walk through the function. | 7671 | that of error handling. We will walk through the function. |
| 7252 | 7672 | ||
| 7253 | But first, let us look at the interactive @code{zap-to-char} function. | 7673 | But 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 | |||
| 7288 | manipulates text, and we will focus attention on them as well as on the | 7708 | manipulates text, and we will focus attention on them as well as on the |
| 7289 | deletion command. | 7709 | deletion command. |
| 7290 | 7710 | ||
| 7291 | @need 800 | 7711 | @ignore |
| 7292 | Here 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. |
| 7299 | Goes backward if ARG is negative; error if CHAR not found." | 7715 | Goes 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 | ||
| 7725 | Here 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. | ||
| 7732 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | ||
| 7733 | Goes 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 | |||
| 7315 | this: | 7749 | this: |
| 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 | |||
| 7321 | The part within quotation marks, @code{"*p\ncZap to char:@: "}, specifies | ||
| 7322 | three different things. First, and most simply, the asterisk, @samp{*}, | ||
| 7323 | causes an error to be signaled if the buffer is read-only. This means that | ||
| 7324 | if you try @code{zap-to-char} in a read-only buffer you will not be able to | ||
| 7325 | remove text, and you will receive a message that says ``Buffer is | ||
| 7326 | read-only''; your terminal may beep at you as well. | ||
| 7327 | |||
| 7328 | The version 21 implementation does not have the asterisk, @samp{*}. The | ||
| 7329 | function works the same as in version 19: in both cases, it cannot | ||
| 7330 | remove text from a read-only buffer but the function does copy the | ||
| 7331 | text that would have been removed to the kill ring. Also, in both | ||
| 7332 | cases, you see an error message. | ||
| 7333 | |||
| 7334 | However, the version 19 implementation copies text from a read-only | ||
| 7335 | buffer 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 | ||
| 7339 | is read only. In version 19, the function should not copy the text to | ||
| 7340 | the kill ring. It is a bug that it does. | ||
| 7341 | |||
| 7342 | In version 21, the function is designed to copy the text to the kill | ||
| 7343 | ring; moreover, @code{interactive} is implemented correctly. So the | ||
| 7344 | asterisk, @samp{*}, had to be removed from the interactive | ||
| 7345 | specification. However, if you insert an @samp{*} yourself and | ||
| 7346 | evaluate 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 | ||
| 7348 | any text. | ||
| 7349 | |||
| 7350 | That change aside, and a change to the documentation, the two versions | ||
| 7351 | of the @code{zap-to-char} function are identical. | ||
| 7352 | |||
| 7353 | Let us continue with the interactive specification. | ||
| 7354 | 7754 | ||
| 7355 | The second part of @code{"*p\ncZap to char:@: "} is the @samp{p}. | 7755 | The part within quotation marks, @code{"p\ncZap to char:@: "}, specifies |
| 7756 | two different things. First, and most simply, is the @samp{p}. | ||
| 7356 | This part is separated from the next part by a newline, @samp{\n}. | 7757 | This part is separated from the next part by a newline, @samp{\n}. |
| 7357 | The @samp{p} means that the first argument to the function will be | 7758 | The @samp{p} means that the first argument to the function will be |
| 7358 | passed the value of a `processed prefix'. The prefix argument is | 7759 | passed 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 | |||
| 7360 | the function is called interactively without a prefix, 1 is passed to | 7761 | the function is called interactively without a prefix, 1 is passed to |
| 7361 | this argument. | 7762 | this argument. |
| 7362 | 7763 | ||
| 7363 | The third part of @code{"*p\ncZap to char:@: "} is @samp{cZap to char:@: | 7764 | The 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 | 7766 | indicates that @code{interactive} expects a prompt and that the |
| 7366 | character. The prompt follows the @samp{c} and is the string @samp{Zap | 7767 | argument will be a character. The prompt follows the @samp{c} and is |
| 7367 | to char:@: } (with a space after the colon to make it look good). | 7768 | the string @samp{Zap to char:@: } (with a space after the colon to |
| 7769 | make it look good). | ||
| 7368 | 7770 | ||
| 7369 | What all this does is prepare the arguments to @code{zap-to-char} so they | 7771 | What all this does is prepare the arguments to @code{zap-to-char} so they |
| 7370 | are of the right type, and give the user a prompt. | 7772 | are of the right type, and give the user a prompt. |
| 7371 | 7773 | ||
| 7774 | In a read-only buffer, the @code{zap-to-char} function copies the text | ||
| 7775 | to the kill ring, but does not remove it. The echo area displays a | ||
| 7776 | message saying that the buffer is read-ly. Also, the terminal may | ||
| 7777 | beep or blink at you. | ||
| 7778 | |||
| 7779 | Let 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. | |||
| 7376 | The body of the @code{zap-to-char} function contains the code that | 7785 | The body of the @code{zap-to-char} function contains the code that |
| 7377 | kills (that is, removes) the text in the region from the current | 7786 | kills (that is, removes) the text in the region from the current |
| 7378 | position of the cursor up to and including the specified character. | 7787 | position of the cursor up to and including the specified character. |
| 7788 | |||
| 7789 | The documentation is thorough. You do need to know the jargon meaning | ||
| 7790 | of the word `kill'. | ||
| 7791 | |||
| 7379 | The first part of the code looks like this: | 7792 | The 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 | ||
| 7804 | whether its argument is a character table. When it is, it sets the | ||
| 7805 | character passed to @code{zap-to-char} to one of them, if that | ||
| 7806 | character exists, or to the character itself. (This becomes important | ||
| 7807 | for certain characters in non-European languages. The @code{aref} | ||
| 7808 | function extracts an element from an array. It is an array-specific | ||
| 7809 | function that is not described in this document. @xref{Arrays, , | ||
| 7810 | Arrays, 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 | ||
| 7388 | The next part of the code is an expression using @code{progn}. The body | 7815 | The 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 | |||
| 7402 | zapped-for-character in @code{zap-to-char}. If the search is | 7829 | zapped-for-character in @code{zap-to-char}. If the search is |
| 7403 | successful, @code{search-forward} leaves point immediately after the | 7830 | successful, @code{search-forward} leaves point immediately after the |
| 7404 | last character in the target string. (In @code{zap-to-char}, the | 7831 | last character in the target string. (In @code{zap-to-char}, the |
| 7405 | target string is just one character long.) If the search is | 7832 | target string is just one character long. @code{zap-to-char} uses the |
| 7406 | backwards, @code{search-forward} leaves point just before the first | 7833 | function @code{char-to-string} to ensure that the computer treats that |
| 7407 | character in the target. Also, @code{search-forward} returns @code{t} | 7834 | character as a string.) If the search is backwards, |
| 7408 | for true. (Moving point is therefore a `side effect'.) | 7835 | @code{search-forward} leaves point just before the first character in |
| 7836 | the 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 |
| 7411 | In @code{zap-to-char}, the @code{search-forward} function looks like this: | 7841 | In @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. | |||
| 7501 | The second and last argument to @code{progn} is the expression | 7931 | The 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 |
| 7503 | this case will be the location to which it has been moved by | 7933 | this 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 |
| 7505 | expression and is passed to @code{kill-region} as @code{kill-region}'s | 7935 | to go to the previous character, if it is going forward, was commented |
| 7506 | second argument. | 7936 | out in 1999; I don't remember whether that feature or mis-feature was |
| 7937 | ever a part of the distributed source.) The value of @code{point} is | ||
| 7938 | returned 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 | |||
| 7520 | these two values of point, the first one as the beginning of the region | 7953 | these two values of point, the first one as the beginning of the region |
| 7521 | and the second one as the end of the region, and removes the region. | 7954 | and the second one as the end of the region, and removes the region. |
| 7522 | 7955 | ||
| 7523 | The @code{progn} special form is necessary because the @code{kill-region} | 7956 | The @code{progn} special form is necessary because the |
| 7524 | command takes two arguments; and it would fail if @code{search-forward} | 7957 | @code{kill-region} command takes two arguments; and it would fail if |
| 7525 | and @code{point} expressions were written in sequence as two | 7958 | @code{search-forward} and @code{point} expressions were written in |
| 7526 | additional arguments. The @code{progn} expression is a single argument | 7959 | sequence as two additional arguments. The @code{progn} expression is |
| 7527 | to @code{kill-region} and returns the one value that @code{kill-region} | 7960 | a single argument to @code{kill-region} and returns the one value that |
| 7528 | needs 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. | |||
| 7536 | This function clips text from a region and copies that text to | 7969 | This function clips text from a region and copies that text to |
| 7537 | the kill ring, from which it may be retrieved. | 7970 | the kill ring, from which it may be retrieved. |
| 7538 | 7971 | ||
| 7539 | The Emacs 21 version of that function uses @code{condition-case} and | 7972 | @ignore |
| 7973 | GNU Emacs 22: | ||
| 7974 | |||
| 7975 | (defun kill-region (beg end &optional yank-handler) | ||
| 7976 | "Kill (\"cut\") text between point and mark. | ||
| 7977 | This deletes the text from the buffer and saves it in the kill ring. | ||
| 7978 | The command \\[yank] can retrieve it from there. | ||
| 7979 | \(If you want to kill and then yank immediately, use \\[kill-ring-save].) | ||
| 7980 | |||
| 7981 | If you want to append the killed region to the last killed text, | ||
| 7982 | use \\[append-next-kill] before \\[kill-region]. | ||
| 7983 | |||
| 7984 | If the buffer is read-only, Emacs will beep and refrain from deleting | ||
| 7985 | the text, but put the text in the kill ring anyway. This means that | ||
| 7986 | you can use the killing commands to copy text from a read-only buffer. | ||
| 7987 | |||
| 7988 | This is the primitive for programs to kill text (as opposed to deleting it). | ||
| 7989 | Supply two arguments, character positions indicating the stretch of text | ||
| 7990 | to be killed. | ||
| 7991 | Any command that calls this function is a \"kill command\". | ||
| 7992 | If the previous command was also a kill command, | ||
| 7993 | the text killed this time appends to the text killed last time | ||
| 7994 | to make one entry in the kill ring. | ||
| 7995 | |||
| 7996 | In Lisp code, optional third arg YANK-HANDLER, if non-nil, | ||
| 7997 | specifies the yank-handler text property to be set on the killed | ||
| 7998 | text. 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 | |||
| 8032 | kfstorm 18-Jan-03): (defun kill-region (beg end &optional yank-handler) | ||
| 8033 | rms 11-Mar-06): "Kill (\"cut\") text between point and mark. | ||
| 8034 | rms 11-Mar-06): This deletes the text from the buffer and saves it in the kill ring. | ||
| 8035 | jimb 21-Dec-91): The command \\[yank] can retrieve it from there. | ||
| 8036 | eliz 16-Feb-01): \(If you want to kill and then yank immediately, use \\[kill-ring-save].) | ||
| 8037 | eliz 16-Feb-01): | ||
| 8038 | eliz 16-Feb-01): If you want to append the killed region to the last killed text, | ||
| 8039 | eliz 16-Feb-01): use \\[append-next-kill] before \\[kill-region]. | ||
| 8040 | eliz 16-Feb-01): | ||
| 8041 | jimb 01-Feb-93): If the buffer is read-only, Emacs will beep and refrain from deleting | ||
| 8042 | jimb 01-Feb-93): the text, but put the text in the kill ring anyway. This means that | ||
| 8043 | jimb 01-Feb-93): you can use the killing commands to copy text from a read-only buffer. | ||
| 8044 | jimb 21-Dec-91): | ||
| 8045 | jimb 21-Dec-91): This is the primitive for programs to kill text (as opposed to deleting it). | ||
| 8046 | lektu 07-Jun-04): Supply two arguments, character positions indicating the stretch of text | ||
| 8047 | jimb 21-Dec-91): to be killed. | ||
| 8048 | jimb 21-Dec-91): Any command that calls this function is a \"kill command\". | ||
| 8049 | jimb 21-Dec-91): If the previous command was also a kill command, | ||
| 8050 | jimb 21-Dec-91): the text killed this time appends to the text killed last time | ||
| 8051 | kfstorm 18-Jan-03): to make one entry in the kill ring. | ||
| 8052 | kfstorm 18-Jan-03): | ||
| 8053 | uid68472 18-Feb-04): In Lisp code, optional third arg YANK-HANDLER, if non-nil, | ||
| 8054 | uid68472 18-Feb-04): specifies the yank-handler text property to be set on the killed | ||
| 8055 | uid68472 18-Feb-04): text. See `insert-for-yank'." | ||
| 8056 | rms 21-May-06): ;; Pass point first, then mark, because the order matters | ||
| 8057 | rms 21-May-06): ;; when calling kill-append. | ||
| 8058 | rms 21-May-06): (interactive (list (point) (mark))) | ||
| 8059 | rms 04-Sep-06): (unless (and beg end) | ||
| 8060 | rms 04-Sep-06): (error "The mark is not set now, so there is no region")) | ||
| 8061 | kwzh 19-Nov-97): (condition-case nil | ||
| 8062 | rms 29-Mar-05): (let ((string (filter-buffer-substring beg end t))) | ||
| 8063 | monnier 07-Dec-99): (when string ;STRING is nil if BEG = END | ||
| 8064 | monnier 07-Dec-99): ;; Add that string to the kill ring, one way or another. | ||
| 8065 | monnier 07-Dec-99): (if (eq last-command 'kill-region) | ||
| 8066 | kfstorm 18-Jan-03): (kill-append string (< end beg) yank-handler) | ||
| 8067 | kfstorm 18-Jan-03): (kill-new string nil yank-handler))) | ||
| 8068 | kai 28-May-03): (when (or string (eq last-command 'kill-region)) | ||
| 8069 | uid68472 18-Feb-04): (setq this-command 'kill-region)) | ||
| 8070 | uid68472 18-Feb-04): nil) | ||
| 8071 | kwzh 19-Nov-97): ((buffer-read-only text-read-only) | ||
| 8072 | kwzh 19-Nov-97): ;; The code above failed because the buffer, or some of the characters | ||
| 8073 | kwzh 19-Nov-97): ;; in the region, are read-only. | ||
| 8074 | kwzh 19-Nov-97): ;; We should beep, in case the user just isn't aware of this. | ||
| 8075 | kwzh 19-Nov-97): ;; However, there's no harm in putting | ||
| 8076 | kwzh 19-Nov-97): ;; the region's text in the kill ring, anyway. | ||
| 8077 | kwzh 19-Nov-97): (copy-region-as-kill beg end) | ||
| 8078 | rms 19-May-98): ;; Set this-command now, so it will be set even if we get an error. | ||
| 8079 | rms 19-May-98): (setq this-command 'kill-region) | ||
| 8080 | rms 19-May-98): ;; This should barf, if appropriate, and give us the correct error. | ||
| 8081 | kwzh 19-Nov-97): (if kill-read-only-ok | ||
| 8082 | uid68472 18-Feb-04): (progn (message "Read only text copied to kill ring") nil) | ||
| 8083 | kwzh 19-Nov-97): ;; Signal an error if the buffer is read-only. | ||
| 8084 | kwzh 19-Nov-97): (barf-if-buffer-read-only) | ||
| 8085 | kwzh 19-Nov-97): ;; If the buffer isn't read-only, the text is. | ||
| 8086 | kwzh 19-Nov-97): (signal 'text-read-only (list (current-buffer))))))) | ||
| 8087 | |||
| 8088 | @end ignore | ||
| 8089 | |||
| 8090 | The 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 | |||
| 7547 | contains the code that is called in the event of an error. | 8098 | contains 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 |
| 7561 | We will go through the @code{condition-case} code in a moment. First, | 8112 | We will go through the @code{condition-case} code in a moment. First, |
| 7562 | let us look at the original definition of @code{kill-region}, with | 8113 | let us look at the definition of @code{kill-region}, with comments |
| 7563 | comments added (the newer definition has an optional third argument | 8114 | added: |
| 7564 | and 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. | ||
| 8121 | This deletes the text from the buffer and saves it in the kill ring. | ||
| 8122 | The 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 | |||
| 7715 | A @code{condition-case} expression has two parts, a part that is | ||
| 7716 | evaluated in the expectation that all will go well, but which may | ||
| 7717 | generate an error; and a part that is evaluated when there is an | ||
| 7718 | error. | ||
| 7719 | |||
| 7720 | First, let us look at the code in @code{kill-region} that is run in | ||
| 7721 | the expectation that all goes well. This is the core of the function. | ||
| 7722 | The 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 | |||
| 7735 | It 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 | ||
| 7740 | The @code{delete-and-extract-region} function is straightforward. It | 8368 | @ignore |
| 7741 | is a built-in function that deletes the text in a region (a side | 8369 | 2006 Oct 24 |
| 7742 | effect) and also returns that text. This is the function that | 8370 | In Emacs 22, |
| 7743 | actually removes the text. (And if it cannot do that, it signals the | 8371 | copy-region-as-kill is short, 12 lines, and uses |
| 7744 | error.) | 8372 | filter-buffer-substring, which is longer, 39 lines |
| 8373 | and has delete-and-extract-region in it. | ||
| 8374 | delete-and-extract-region is written in C. | ||
| 8375 | |||
| 8376 | see Initializing a Variable with @code{defvar} | ||
| 8377 | this is line 8054 | ||
| 8378 | Initializing a Variable with @code{defvar} includes line 8350 | ||
| 8379 | @end ignore | ||
| 7745 | 8380 | ||
| 7746 | In 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 |
| 7748 | variable called @samp{string}. This is the text that is removed from | 8383 | @subsection Lisp macro |
| 7749 | the buffer. (To be more precise, the variable is set to point to the | 8384 | @cindex Macro, lisp |
| 7750 | address of the extracted text; to say it is `placed in' the variable | 8385 | @cindex Lisp macro |
| 7751 | is simply a shorthand.) | ||
| 7752 | 8386 | ||
| 7753 | If the variable @samp{string} does point to text, that text is added | 8387 | The part of the @code{condition-case} expression that is evaluated in |
| 7754 | to the kill ring. The variable will have a @code{nil} value if no | 8388 | the expectation that all goes well has a @code{when}. The code uses |
| 7755 | text was removed. | 8389 | @code{when} to determine whether the @code{string} variable points to |
| 8390 | text that exists. | ||
| 7756 | 8391 | ||
| 7757 | The code uses @code{when} to determine whether the variable | 8392 | A @code{when} expression is simply a programmers' convenience. It is |
| 7758 | @samp{string} points to text. A @code{when} statement is simply a | 8393 | an @code{if} without the possibility of an else clause. In your mind, |
| 7759 | programmers' convenience. A @code{when} statement is an @code{if} | 8394 | you can replace @code{when} with @code{if} and understand what goes |
| 7760 | statement without the possibility of an else clause. In your mind, you | 8395 | on. That is what the Lisp interpreter does. |
| 7761 | can replace @code{when} with @code{if} and understand what goes on. | ||
| 7762 | That is what the Lisp interpreter does. | ||
| 7763 | 8396 | ||
| 7764 | @cindex Macro, lisp | ||
| 7765 | @cindex Lisp macro | ||
| 7766 | Technically speaking, @code{when} is a Lisp macro. A Lisp @dfn{macro} | 8397 | Technically speaking, @code{when} is a Lisp macro. A Lisp @dfn{macro} |
| 7767 | enables you to define new control constructs and other language | 8398 | enables you to define new control constructs and other language |
| 7768 | features. It tells the interpreter how to compute another Lisp | 8399 | features. 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 |
| 7771 | macros, see @ref{Macros, , Macros, elisp, The GNU Emacs Lisp Reference | 8402 | macros, see @ref{Macros, , Macros, elisp, The GNU Emacs Lisp Reference |
| 7772 | Manual}. The C programming language also provides macros. These are | 8403 | Manual}. The C programming language also provides macros. These are |
| 7773 | different, but also useful. We will briefly look at C macros in | 8404 | different, but also useful. |
| 8405 | |||
| 8406 | @ignore | ||
| 8407 | We 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 |
| 7777 | If the string has content, then another conditional expression is | 8412 | If 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 | ||
| 7788 | The then-part is evaluated if the previous command was another call to | 8423 | The 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 | ||
| 8427 | tells the @code{kill-append} and @code{kill-new} functions how deal | ||
| 8428 | with 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 |
| 7792 | not seen before. Normally, whenever a function is executed, Emacs | 8431 | not seen before. Normally, whenever a function is executed, Emacs |
| 7793 | sets the value of @code{last-command} to the previous command. | 8432 | sets 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 | |||
| 7797 | whether the previous command was @code{kill-region}. If it was, | 8436 | whether 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 |
| 7804 | concatenates a copy of the newly clipped text to the just previously | 8443 | concatenates a copy of the newly clipped text to the just previously |
| 7805 | clipped text in the kill ring. (If the @w{@code{(< end beg))}} | 8444 | clipped text in the kill ring. |
| 7806 | expression is true, @code{kill-append} prepends the string to the just | ||
| 7807 | previously clipped text. For a detailed discussion, see | ||
| 7808 | @ref{kill-append function, , The @code{kill-append} function}.) | ||
| 7809 | |||
| 7810 | If you then yank back the text, i.e., `paste' it, you get both | ||
| 7811 | pieces of text at once. That way, if you delete two words in a row, | ||
| 7812 | and then yank them back, you get both words, in their proper order, | ||
| 7813 | with one yank. (The @w{@code{(< end beg))}} expression makes sure the | ||
| 7814 | order is correct.) | ||
| 7815 | |||
| 7816 | On the other hand, if the previous command is not @code{kill-region}, | ||
| 7817 | then the @code{kill-new} function is called, which adds the text to | ||
| 7818 | the 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 | |||
| 7828 | The @code{zap-to-char} command uses the | ||
| 7829 | @code{delete-and-extract-region} function, which in turn uses two | ||
| 7830 | other functions, @code{copy-region-as-kill} and | ||
| 7831 | @code{del_range_1}. The @code{copy-region-as-kill} function will be | ||
| 7832 | described in a following section; it puts a copy of the region in the | ||
| 7833 | kill ring so it can be yanked back. (@xref{copy-region-as-kill, , | ||
| 7834 | @code{copy-region-as-kill}}.) | ||
| 7835 | |||
| 7836 | The @code{delete-and-extract-region} function removes the contents of | ||
| 7837 | a region and you cannot get them back. | ||
| 7838 | |||
| 7839 | Unlike the other code discussed here, @code{delete-and-extract-region} | ||
| 7840 | is not written in Emacs Lisp; it is written in C and is one of the | ||
| 7841 | primitives of the GNU Emacs system. Since it is very simple, I will | ||
| 7842 | digress briefly from Lisp and describe it here. | ||
| 7843 | |||
| 7844 | @need 1500 | ||
| 7845 | Like many of the other Emacs primitives, | ||
| 7846 | @code{delete-and-extract-region} is written as an instance of a C | ||
| 7847 | macro, a macro being a template for code. The complete macro looks | ||
| 7848 | like this: | ||
| 7849 | |||
| 7850 | @c /usr/local/src/emacs/src/editfns.c | ||
| 7851 | @smallexample | ||
| 7852 | @group | ||
| 7853 | DEFUN ("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 | |||
| 7865 | Without going into the details of the macro writing process, let me | ||
| 7866 | point 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 | ||
| 7869 | parts inside of parentheses: | ||
| 7870 | |||
| 7871 | @itemize @bullet | ||
| 7872 | @item | ||
| 7873 | The first part is the name given to the function in Lisp, | ||
| 7874 | @code{delete-and-extract-region}. | ||
| 7875 | |||
| 7876 | @item | ||
| 7877 | The 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 | ||
| 7880 | instead. | ||
| 7881 | |||
| 7882 | @item | ||
| 7883 | The third part is the name for the C constant structure that records | ||
| 7884 | information on this function for internal use. It is the name of the | ||
| 7885 | function in C but begins with an @samp{S} instead of an @samp{F}. | ||
| 7886 | |||
| 7887 | @item | ||
| 7888 | The fourth and fifth parts specify the minimum and maximum number of | ||
| 7889 | arguments the function can have. This function demands exactly 2 | ||
| 7890 | arguments. | ||
| 7891 | |||
| 7892 | @item | ||
| 7893 | The sixth part is nearly like the argument that follows the | ||
| 7894 | @code{interactive} declaration in a function written in Lisp: a letter | ||
| 7895 | followed, perhaps, by a prompt. The only difference from the Lisp is | ||
| 7896 | when the macro is called with no arguments. Then you write a @code{0} | ||
| 7897 | (which is a `null string'), as in this macro. | ||
| 7898 | |||
| 7899 | If you were to specify arguments, you would place them between | ||
| 7900 | quotation marks. The C macro for @code{goto-char} includes | ||
| 7901 | @code{"NGoto char: "} in this position to indicate that the function | ||
| 7902 | expects a raw prefix, in this case, a numerical location in a buffer, | ||
| 7903 | and provides a prompt. | ||
| 7904 | |||
| 7905 | @item | ||
| 7906 | The seventh part is a documentation string, just like the one for a | ||
| 7907 | function written in Emacs Lisp, except that every newline must be | ||
| 7908 | written explicitly as @samp{\n} followed by a backslash and carriage | ||
| 7909 | return. | ||
| 7910 | |||
| 7911 | @need 1000 | ||
| 7912 | Thus, the first two lines of documentation for @code{goto-char} are | ||
| 7913 | written like this: | ||
| 7914 | |||
| 7915 | @smallexample | ||
| 7916 | @group | ||
| 7917 | "Set point to POSITION, a number or marker.\n\ | ||
| 7918 | Beginning of buffer is position (point-min), end is (point-max). | ||
| 7919 | @end group | ||
| 7920 | @end smallexample | ||
| 7921 | @end itemize | ||
| 7922 | |||
| 7923 | @need 1200 | ||
| 7924 | In a C macro, the formal parameters come next, with a statement of | ||
| 7925 | what kind of object they are, followed by what might be called the `body' | ||
| 7926 | of the macro. For @code{delete-and-extract-region} the `body' | ||
| 7927 | consists of the following two lines: | ||
| 7928 | |||
| 7929 | @smallexample | ||
| 7930 | @group | ||
| 7931 | validate_region (&start, &end); | ||
| 7932 | return del_range_1 (XINT (start), XINT (end), 1, 1); | ||
| 7933 | @end group | ||
| 7934 | @end smallexample | ||
| 7935 | |||
| 7936 | The first function, @code{validate_region} checks whether the values | ||
| 7937 | passed as the beginning and end of the region are the proper type and | ||
| 7938 | are within range. The second function, @code{del_range_1}, actually | ||
| 7939 | deletes the text. | ||
| 7940 | |||
| 7941 | @code{del_range_1} is a complex function we will not look into. It | ||
| 7942 | updates the buffer and does other things. | ||
| 7943 | |||
| 7944 | However, 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 | |||
| 7948 | As far as the C language is concerned, @code{start} and @code{end} are | ||
| 7949 | two integers that mark the beginning and end of the region to be | ||
| 7950 | deleted@footnote{More precisely, and requiring more expert knowledge | ||
| 7951 | to understand, the two integers are of type `Lisp_Object', which can | ||
| 7952 | also be a C union instead of an integer type.}. | ||
| 7953 | |||
| 7954 | In early versions of Emacs, these two numbers were thirty-two bits | ||
| 7955 | long, but the code is slowly being generalized to handle other | ||
| 7956 | lengths. Three of the available bits are used to specify the type of | ||
| 7957 | information and a fourth bit is used for handling the computer's | ||
| 7958 | memory; the remaining bits are used as `content'. | ||
| 7959 | |||
| 7960 | @samp{XINT} is a C macro that extracts the relevant number from the | ||
| 7961 | longer collection of bits; the four other bits are discarded. | ||
| 7962 | |||
| 7963 | @need 800 | ||
| 7964 | The command in @code{delete-and-extract-region} looks like this: | ||
| 7965 | |||
| 7966 | @smallexample | ||
| 7967 | del_range_1 (XINT (start), XINT (end), 1, 1); | ||
| 7968 | @end smallexample | ||
| 7969 | |||
| 7970 | @noindent | ||
| 7971 | It deletes the region between the beginning position, @code{start}, | ||
| 7972 | and the ending position, @code{end}. | ||
| 7973 | |||
| 7974 | From the point of view of the person writing Lisp, Emacs is all very | ||
| 7975 | simple; but hidden underneath is a great deal of complexity to make it | ||
| 7976 | all 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 | |||
| 7985 | Unlike the @code{delete-and-extract-region} function, the | ||
| 7986 | @code{copy-region-as-kill} function is written in Emacs Lisp. Two | ||
| 7987 | functions within it, @code{kill-append} and @code{kill-new}, copy a | ||
| 7988 | region in a buffer and save it in a variable called the | ||
| 7989 | @code{kill-ring}. This section describes how the @code{kill-ring} | ||
| 7990 | variable is created and initialized using the @code{defvar} special | ||
| 7991 | form. | ||
| 7992 | |||
| 7993 | (Again we note that the term @code{kill-ring} is a misnomer. The text | ||
| 7994 | that is clipped out of the buffer can be brought back; it is not a ring | ||
| 7995 | of corpses, but a ring of resurrectable text.) | ||
| 7996 | |||
| 7997 | In Emacs Lisp, a variable such as the @code{kill-ring} is created and | ||
| 7998 | given an initial value by using the @code{defvar} special form. The | ||
| 7999 | name comes from ``define variable''. | ||
| 8000 | |||
| 8001 | The @code{defvar} special form is similar to @code{setq} in that it sets | ||
| 8002 | the value of a variable. It is unlike @code{setq} in two ways: first, | ||
| 8003 | it only sets the value of the variable if the variable does not already | ||
| 8004 | have a value. If the variable already has a value, @code{defvar} does | ||
| 8005 | not override the existing value. Second, @code{defvar} has a | ||
| 8006 | documentation string. | ||
| 8007 | |||
| 8008 | (Another special form, @code{defcustom}, is designed for variables | ||
| 8009 | that 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 | |||
| 8022 | You can see the current value of a variable, any variable, by using | ||
| 8023 | the @code{describe-variable} function, which is usually invoked by | ||
| 8024 | typing @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 | ||
| 8026 | current kill ring---this may be quite a lot! Conversely, if you have | ||
| 8027 | been doing nothing this Emacs session except read this document, you | ||
| 8028 | may have nothing in it. Also, you will see the documentation for | ||
| 8029 | @code{kill-ring}: | ||
| 8030 | |||
| 8031 | @smallexample | ||
| 8032 | @group | ||
| 8033 | Documentation: | ||
| 8034 | List of killed text sequences. | ||
| 8035 | Since the kill ring is supposed to interact nicely with cut-and-paste | ||
| 8036 | facilities offered by window systems, use of this variable should | ||
| 8037 | @end group | ||
| 8038 | @group | ||
| 8039 | interact 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 | ||
| 8042 | interaction; you may want to use them instead of manipulating the kill | ||
| 8043 | ring directly. | ||
| 8044 | @end group | ||
| 8045 | @end smallexample | ||
| 8046 | |||
| 8047 | @need 800 | ||
| 8048 | The 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 | ||
| 8059 | In 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 | ||
| 8061 | nothing back if you give a @code{yank} command. The documentation | ||
| 8062 | string is written just like the documentation string of a @code{defun}. | ||
| 8063 | As with the documentation string of the @code{defun}, the first line of | ||
| 8064 | the documentation should be a complete sentence, since some commands, | ||
| 8065 | like @code{apropos}, print only the first line of documentation. | ||
| 8066 | Succeeding lines should not be indented; otherwise they look odd when | ||
| 8067 | you 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 | |||
| 8074 | In the past, Emacs used the @code{defvar} special form both for | ||
| 8075 | internal variables that you would not expect a user to change and for | ||
| 8076 | variables that you do expect a user to change. Although you can still | ||
| 8077 | use @code{defvar} for user customizable variables, please use | ||
| 8078 | @code{defcustom} instead, since that special form provides a path into | ||
| 8079 | the Customization commands. (@xref{defcustom, , Specifying Variables | ||
| 8080 | using @code{defcustom}}.) | ||
| 8081 | |||
| 8082 | When you specified a variable using the @code{defvar} special form, | ||
| 8083 | you could distinguish a readily settable variable from others by | ||
| 8084 | typing an asterisk, @samp{*}, in the first column of its documentation | ||
| 8085 | string. 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 | ||
| 8097 | You could (and still can) use the @code{set-variable} command to | ||
| 8098 | change the value of @code{shell-command-default-error-buffer} | ||
| 8099 | temporarily. However, options set using @code{set-variable} are set | ||
| 8100 | only for the duration of your editing session. The new values are not | ||
| 8101 | saved between sessions. Each time Emacs starts, it reads the original | ||
| 8102 | value, unless you change the value within your @file{.emacs} file, | ||
| 8103 | either by setting it manually or by using @code{customize}. | ||
| 8104 | @xref{Emacs Initialization, , Your @file{.emacs} File}. | ||
| 8105 | |||
| 8106 | For me, the major use of the @code{set-variable} command is to suggest | ||
| 8107 | variables that I might want to set in my @file{.emacs} file. There | ||
| 8108 | are now more than 700 such variables --- far too many to remember | ||
| 8109 | readily. 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, | ||
| 8112 | The 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}, | |||
| 8129 | the function copies the text into a separate entry in the kill ring. | 8461 | the 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 |
| 8142 | Here is the complete text of the version 21 @code{copy-region-as-kill} | 8474 | Here is the complete text of the version 22 @code{copy-region-as-kill} |
| 8143 | function: | 8475 | function: |
| 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. |
| 8151 | In Transient Mark mode, deactivate the mark. | 8481 | In Transient Mark mode, deactivate the mark. |
| 8152 | If `interprogram-cut-function' is non-nil, also save | 8482 | If `interprogram-cut-function' is non-nil, also save the text for a window |
| 8153 | the text for a window system cut and paste." | 8483 | system 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 | |||
| 8184 | almost becoming routine. | 8514 | almost becoming routine. |
| 8185 | 8515 | ||
| 8186 | The documentation is somewhat confusing unless you remember that the | 8516 | The documentation is somewhat confusing unless you remember that the |
| 8187 | word `kill' has a meaning different from its usual meaning. The | 8517 | word `kill' has a meaning different from usual. The `Transient Mark' |
| 8188 | `Transient Mark' and @code{interprogram-cut-function} comments explain | 8518 | and @code{interprogram-cut-function} comments explain certain |
| 8189 | certain side-effects. | 8519 | side-effects. |
| 8190 | 8520 | ||
| 8191 | After you once set a mark, a buffer always contains a region. If you | 8521 | After you once set a mark, a buffer always contains a region. If you |
| 8192 | wish, you can use Transient Mark mode to highlight the region | 8522 | wish, 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 | ||
| 8221 | The @code{copy-region-as-kill} function works in much the same way as | 8551 | The @code{copy-region-as-kill} function works in much the same way as |
| 8222 | the @code{kill-region} function (@pxref{kill-region, | 8552 | the @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 | 8553 | kills in a row combine their text into a single entry. If you yank |
| 8224 | a row combine their text into a single entry. If you yank back the | 8554 | back the text from the kill ring, you get it all in one piece. |
| 8225 | text from the kill ring, you get it all in one piece. Moreover, kills | 8555 | Moreover, kills that kill forward from the current position of the |
| 8226 | that kill forward from the current position of the cursor are added to | 8556 | cursor are added to the end of the previously copied text and commands |
| 8227 | the end of the previously copied text and commands that copy text | 8557 | that copy text backwards add it to the beginning of the previously |
| 8228 | backwards add it to the beginning of the previously copied text. This | 8558 | copied text. This way, the words in the text stay in the proper |
| 8229 | way, the words in the text stay in the proper order. | 8559 | order. |
| 8230 | 8560 | ||
| 8231 | Like @code{kill-region}, the @code{copy-region-as-kill} function makes | 8561 | Like @code{kill-region}, the @code{copy-region-as-kill} function makes |
| 8232 | use of the @code{last-command} variable that keeps track of the | 8562 | use 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 | ||
| 8609 | substring of the buffer, if any. Optionally---the arguments are not | ||
| 8610 | here, so neither is done---the function may delete the initial text or | ||
| 8611 | return the text without its properties; this function is a replacement | ||
| 8612 | for the older @code{buffer-substring} function, which came before text | ||
| 8613 | properties were implemented.) | ||
| 8614 | |||
| 8277 | @findex eq @r{(example of use)} | 8615 | @findex eq @r{(example of use)} |
| 8278 | @noindent | 8616 | @noindent |
| 8279 | The @code{eq} function tests whether its first argument is the same Lisp | 8617 | The @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 |
| 8295 | The @code{kill-append} function looks like this: | 8633 | The @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. | ||
| 8640 | If 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 | ||
| 8651 | was: | ||
| 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. |
| 8301 | If BEFORE-P is non-nil, prepend STRING to the kill. | 8654 | If 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 |
| 8312 | The @code{kill-append} function is fairly straightforward. It uses | 8664 | The @code{kill-append} function is fairly straightforward. It uses |
| 8313 | the @code{kill-new} function, which we will discuss in more detail in | 8665 | the @code{kill-new} function, which we will discuss in more detail in |
| 8314 | a moment. | 8666 | a moment. |
| 8315 | 8667 | ||
| 8316 | First, let us look at the conditional that is one of the two arguments | 8668 | (Also, the function provides an optional argument called |
| 8317 | to @code{kill-new}. It uses @code{concat} to concatenate the new text | 8669 | @code{yank-handler}; when invoked, this argument tells the function |
| 8318 | to the @sc{car} of the kill ring. Whether it prepends or appends the | 8670 | how 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 ? | ||
| 8674 | It has a @code{let*} function to set the value of the first element of | ||
| 8675 | the kill ring to @code{cur}. (I do not know why the function does not | ||
| 8676 | use @code{let} instead; only one value is set in the expression. | ||
| 8677 | Perhaps this is a bug that produces no problems?) | ||
| 8678 | |||
| 8679 | Consider the conditional that is one of the two arguments to | ||
| 8680 | @code{kill-new}. It uses @code{concat} to concatenate the new text to | ||
| 8681 | the @sc{car} of the kill ring. Whether it prepends or appends the | ||
| 8319 | text depends on the results of an @code{if} expression: | 8682 | text 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 | |||
| 8355 | text will be concatenated before the old text: | 8718 | text 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 | |||
| 8364 | after the old text: | 8727 | after 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 | ||
| 8370 | To understand how this works, we first need to review the | 8733 | To 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 | ||
| 8765 | Optional third arguments YANK-HANDLER controls how the STRING is later | ||
| 8766 | inserted into a buffer; see `insert-for-yank' for details. | ||
| 8767 | When a yank handler is specified, STRING must be non-empty (the yank | ||
| 8768 | handler, if non-nil, is stored as a `yank-handler' text property on STRING). | ||
| 8769 | |||
| 8770 | When the yank handler has a non-nil PARAM element, the original STRING | ||
| 8771 | argument is not used by `insert-for-yank'. However, since Lisp code | ||
| 8772 | may access and use elements from the kill ring directly, the STRING | ||
| 8773 | argument should still be a \"useful\" string for such uses." | ||
| 8774 | @end ignore | ||
| 8400 | @need 1200 | 8775 | @need 1200 |
| 8401 | The @code{kill-new} function looks like this: | 8776 | The @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. |
| 8407 | Set the kill-ring-yank pointer to point to it. | 8782 | Set `kill-ring-yank-pointer' to point to it. |
| 8783 | |||
| 8408 | If `interprogram-cut-function' is non-nil, apply it to STRING. | 8784 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
| 8409 | Optional second argument REPLACE non-nil means that STRING will replace | 8785 | Optional second argument REPLACE non-nil means that STRING will replace |
| 8410 | the front of the kill ring, rather than being added to the list." | 8786 | the 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 | ||
| 8816 | was: | ||
| 8817 | (defun kill-new (string &optional replace) | ||
| 8818 | "Make STRING the latest kill in the kill ring. | ||
| 8819 | Set the kill-ring-yank pointer to point to it. | ||
| 8820 | If `interprogram-cut-function' is non-nil, apply it to STRING. | ||
| 8821 | Optional second argument REPLACE non-nil means that STRING will replace | ||
| 8822 | the 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 | ||
| 8430 | As usual, we can look at this function in parts. | 8838 | As usual, we can look at this function in parts. |
| 8431 | 8839 | ||
| 8840 | The function definition has an optional @code{yank-handler} argument, | ||
| 8841 | which when invoked tells the function how to deal with properties | ||
| 8842 | added to the text, such as `bold' or `italics'. We will skip that. | ||
| 8843 | |||
| 8432 | @need 1200 | 8844 | @need 1200 |
| 8433 | The first line of the documentation makes sense: | 8845 | The 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 |
| 8440 | Let's skip over the rest of the documentation for the moment. | 8852 | Let's skip over the rest of the documentation for the moment. |
| 8441 | 8853 | ||
| 8442 | Also, let's skip over the first two lines of code, those involving | 8854 | Also, let's skip over the initial @code{if} expression and those lines |
| 8443 | @code{menu-bar-update-yank-menu}. We will explain them below. | 8855 | of code involving @code{menu-bar-update-yank-menu}. We will explain |
| 8856 | them below. | ||
| 8444 | 8857 | ||
| 8445 | @need 1200 | 8858 | @need 1200 |
| 8446 | The critical lines are these: | 8859 | The 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 | |||
| 8470 | something in it, and the @code{replace} variable is true. | 8886 | something in it, and the @code{replace} variable is true. |
| 8471 | 8887 | ||
| 8472 | @need 1250 | 8888 | @need 1250 |
| 8473 | The @code{kill-append} function sets @code{replace} to be true; then, | 8889 | When the @code{kill-append} function sets @code{replace} to be true |
| 8474 | when the kill ring has at least one item in it, the @code{setcar} | 8890 | and when the kill ring has at least one item in it, the @code{setcar} |
| 8475 | expression is executed: | 8891 | expression 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 |
| 8483 | first element. | 8899 | first element. |
| 8484 | 8900 | ||
| 8901 | @need 1250 | ||
| 8485 | On the other hand, if the kill ring is empty, or replace is false, the | 8902 | On the other hand, if the kill ring is empty, or replace is false, the |
| 8486 | else-part of the condition is executed: | 8903 | else-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 | ||
| 8912 | as 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 |
| 8497 | This expression first constructs a new version of the kill ring by | 8919 | @need 1250 |
| 8498 | prepending @code{string} to the existing kill ring as a new element. | 8920 | or the newer |
| 8499 | Then it executes a second @code{if} clause. This second @code{if} | 8921 | |
| 8500 | clause keeps the kill ring from growing too long. | 8922 | @smallexample |
| 8923 | (add-to-list kill-ring string) | ||
| 8924 | @end smallexample | ||
| 8925 | |||
| 8926 | @noindent | ||
| 8927 | When it is false, the expression first constructs a new version of the | ||
| 8928 | kill ring by prepending @code{string} to the existing kill ring as a | ||
| 8929 | new element (that is what the @code{push} does). Then it executes a | ||
| 8930 | second @code{if} clause. This second @code{if} clause keeps the kill | ||
| 8931 | ring from growing too long. | ||
| 8501 | 8932 | ||
| 8502 | Let's look at these two expressions in order. | 8933 | Let's look at these two expressions in order. |
| 8503 | 8934 | ||
| 8504 | The @code{setq} line of the else-part sets the new value of the kill | 8935 | The @code{push} line of the else-part sets the new value of the kill |
| 8505 | ring to what results from adding the string being killed to the old kill | 8936 | ring to what results from adding the string being killed to the old |
| 8506 | ring. | 8937 | kill ring. |
| 8938 | |||
| 8939 | |||
| 8940 | We can see how this works with an example. | ||
| 8507 | 8941 | ||
| 8508 | @need 800 | 8942 | @need 800 |
| 8509 | We can see how this works with an example: | 8943 | First, |
| 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 |
| 8529 | Now, we can add a new element on to this list by evaluating the | 8963 | Now, we can add a new element on to this list by evaluating the |
| 8530 | following expression: | 8964 | following 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 |
| 8549 | Thus, the third clause was added to the list by @code{cons}. | 8983 | Thus, the third clause is added to the list by @code{push}. |
| 8550 | |||
| 8551 | @need 1200 | ||
| 8552 | This is exactly similar to what the @code{setq} and @code{cons} do in | ||
| 8553 | the 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 |
| 8560 | Now for the second part of the @code{if} clause. This expression | 8986 | Now for the second part of the @code{if} clause. This expression |
| @@ -8587,13 +9013,15 @@ the last element of the kill ring. | |||
| 8587 | The @code{nthcdr} function works by repeatedly taking the @sc{cdr} of a | 9013 | The @code{nthcdr} function works by repeatedly taking the @sc{cdr} of a |
| 8588 | list---it takes the @sc{cdr} of the @sc{cdr} of the @sc{cdr} | 9014 | list---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} |
| 8592 | Thus, if we had a four element list that was supposed to be three | 9019 | Thus, if we had a four element list that was supposed to be three |
| 8593 | elements long, we could set the @sc{cdr} of the next to last element | 9020 | elements long, we could set the @sc{cdr} of the next to last element |
| 8594 | to @code{nil}, and thereby shorten the list. (If you sent the last | 9021 | to @code{nil}, and thereby shorten the list. (If you sent the last |
| 8595 | element to some other value than @code{nil}, which you could do, then | 9022 | element to some other value than @code{nil}, which you could do, then |
| 8596 | you would not have shortened the list.) | 9023 | you would not have shortened the list. @xref{setcdr, , |
| 9024 | @code{setcdr}}.) | ||
| 8597 | 9025 | ||
| 8598 | You can see shortening by evaluating the following three expressions | 9026 | You can see shortening by evaluating the following three expressions |
| 8599 | in turn. First set the value of @code{trees} to @code{(maple oak pine | 9027 | in 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 | ||
| 8622 | To repeat, in @code{kill-new}, the @code{nthcdr} function takes the | 9050 | To 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 |
| 8624 | size of the kill ring and sets the @sc{cdr} of that element (which | 9052 | size of the kill ring and @code{setcdr} sets the @sc{cdr} of that |
| 8625 | will be the rest of the elements in the kill ring) to @code{nil}. | 9053 | element (which will be the rest of the elements in the kill ring) to |
| 8626 | This 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 |
| 8629 | The next to last expression in the @code{kill-new} function is | 9057 | The 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 |
| 8645 | Now, to return to the first two lines in the body of the function: | 9073 | Now, 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 |
| 8655 | This is an expression whose first element is the function @code{and}. | 9083 | It starts with an @code{if} expression |
| 8656 | |||
| 8657 | @findex and, @r{introduced} | ||
| 8658 | The @code{and} special form evaluates each of its arguments until one of | ||
| 8659 | the arguments returns a value of @code{nil}, in which case the | ||
| 8660 | @code{and} expression returns @code{nil}; however, if none of the | ||
| 8661 | arguments returns a value of @code{nil}, the value resulting from | ||
| 8662 | evaluating 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 | ||
| 8665 | are true. | ||
| 8666 | @findex and | ||
| 8667 | 9084 | ||
| 8668 | In this case, the expression tests first to see whether | 9085 | In 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 | |||
| 8673 | message, as we did when we created errors intentionally (@pxref{Making | 9090 | message, as we did when we created errors intentionally (@pxref{Making |
| 8674 | Errors, , Generate an Error Message}). | 9091 | Errors, , Generate an Error Message}). |
| 8675 | 9092 | ||
| 8676 | @need 1200 | 9093 | @noindent |
| 8677 | Essentially, the @code{and} is an @code{if} expression that reads like | 9094 | The then-part contains an expression whose first element is the |
| 8678 | this: | 9095 | function @code{and}. |
| 8679 | 9096 | ||
| 8680 | @smallexample | 9097 | @findex and |
| 8681 | @group | 9098 | The @code{and} special form evaluates each of its arguments until one |
| 8682 | if @var{the-menu-bar-function-exists} | 9099 | of 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 | 9101 | arguments returns a value of @code{nil}, the value resulting from |
| 8685 | @end smallexample | 9102 | evaluating 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 | ||
| 9105 | are true. (@xref{Second Buffer Related Review}.) | ||
| 9106 | |||
| 9107 | The 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 |
| 8688 | possible to use the `Select and Paste' menu in the Edit item of a menu | 9115 | possible to use the `Select and Paste' menu in the Edit item of a menu |
| 8689 | bar; using a mouse, you can look at the various pieces of text you | 9116 | bar; using a mouse, you can look at the various pieces of text you |
| 8690 | have saved and select one piece to paste. | 9117 | have saved and select one piece to paste. |
| 8691 | 9118 | ||
| 8692 | Finally, the last expression in the @code{kill-new} function adds the | 9119 | The last expression in the @code{kill-new} function adds the newly |
| 8693 | newly copied string to whatever facility exists for copying and | 9120 | copied string to whatever facility exists for copying and pasting |
| 8694 | pasting among different programs running in a windowing system. In | 9121 | among different programs running in a windowing system. In the X |
| 8695 | the X Windowing system, for example, the @code{x-select-text} function | 9122 | Windowing system, for example, the @code{x-select-text} function takes |
| 8696 | takes the string and stores it in memory operated by X. You can paste | 9123 | the string and stores it in memory operated by X. You can paste the |
| 8697 | the string in another program, such as an Xterm. | 9124 | string in another program, such as an Xterm. |
| 8698 | 9125 | ||
| 8699 | @need 1200 | 9126 | @need 1200 |
| 8700 | The expression looks like this: | 9127 | The expression looks like this: |
| @@ -8723,8 +9150,359 @@ commands. However, before discussing the yank commands, it is better | |||
| 8723 | to learn how lists are implemented in a computer. This will make | 9150 | to learn how lists are implemented in a computer. This will make |
| 8724 | clear such mysteries as the use of the term `pointer'. | 9151 | clear 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))}} | ||
| 9158 | expression is true, @code{kill-append} prepends the string to the just | ||
| 9159 | previously clipped text. For a detailed discussion, see | ||
| 9160 | @ref{kill-append function, , The @code{kill-append} function}.) | ||
| 9161 | |||
| 9162 | If you then yank back the text, i.e., `paste' it, you get both | ||
| 9163 | pieces of text at once. That way, if you delete two words in a row, | ||
| 9164 | and then yank them back, you get both words, in their proper order, | ||
| 9165 | with one yank. (The @w{@code{(< end beg))}} expression makes sure the | ||
| 9166 | order is correct.) | ||
| 9167 | |||
| 9168 | On the other hand, if the previous command is not @code{kill-region}, | ||
| 9169 | then the @code{kill-new} function is called, which adds the text to | ||
| 9170 | the 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 | |||
| 9178 | 2006 Oct 26, the Digression into C is now OK but should come after | ||
| 9179 | copy-region-as-kill and filter-buffer-substring | ||
| 9180 | |||
| 9181 | 2006 Oct 24 | ||
| 9182 | In Emacs 22, | ||
| 9183 | copy-region-as-kill is short, 12 lines, and uses | ||
| 9184 | filter-buffer-substring, which is longer, 39 lines | ||
| 9185 | and has delete-and-extract-region in it. | ||
| 9186 | delete-and-extract-region is written in C. | ||
| 9187 | |||
| 9188 | see 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 | |||
| 9198 | The @code{copy-region-as-kill} function (@pxref{copy-region-as-kill, , | ||
| 9199 | @code{copy-region-as-kill}}) uses the @code{filter-buffer-substring} | ||
| 9200 | function, which in turn uses the @code{delete-and-extract-region} | ||
| 9201 | function. It removes the contents of a region and you cannot get them | ||
| 9202 | back. | ||
| 9203 | |||
| 9204 | Unlike the other code discussed here, the | ||
| 9205 | @code{delete-and-extract-region} function is not written in Emacs | ||
| 9206 | Lisp; it is written in C and is one of the primitives of the GNU Emacs | ||
| 9207 | system. Since it is very simple, I will digress briefly from Lisp and | ||
| 9208 | describe 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 | ||
| 9214 | Like many of the other Emacs primitives, | ||
| 9215 | @code{delete-and-extract-region} is written as an instance of a C | ||
| 9216 | macro, a macro being a template for code. The complete macro looks | ||
| 9217 | like this: | ||
| 9218 | |||
| 9219 | @smallexample | ||
| 9220 | @group | ||
| 9221 | DEFUN ("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, | ||
| 9224 | without the text properties. | ||
| 9225 | The two arguments START and END are character positions; | ||
| 9226 | they 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 | |||
| 9241 | Without going into the details of the macro writing process, let me | ||
| 9242 | point 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 | |||
| 9247 | The word @code{DEFUN} is followed by seven parts inside of | ||
| 9248 | parentheses: | ||
| 9249 | |||
| 9250 | @itemize @bullet | ||
| 9251 | @item | ||
| 9252 | The first part is the name given to the function in Lisp, | ||
| 9253 | @code{delete-and-extract-region}. | ||
| 9254 | |||
| 9255 | @item | ||
| 9256 | The 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 | ||
| 9259 | instead. | ||
| 9260 | |||
| 9261 | @item | ||
| 9262 | The third part is the name for the C constant structure that records | ||
| 9263 | information on this function for internal use. It is the name of the | ||
| 9264 | function in C but begins with an @samp{S} instead of an @samp{F}. | ||
| 9265 | |||
| 9266 | @item | ||
| 9267 | The fourth and fifth parts specify the minimum and maximum number of | ||
| 9268 | arguments the function can have. This function demands exactly 2 | ||
| 9269 | arguments. | ||
| 9270 | |||
| 9271 | @item | ||
| 9272 | The sixth part is nearly like the argument that follows the | ||
| 9273 | @code{interactive} declaration in a function written in Lisp: a letter | ||
| 9274 | followed, perhaps, by a prompt. The only difference from the Lisp is | ||
| 9275 | when the macro is called with no arguments. Then you write a @code{0} | ||
| 9276 | (which is a `null string'), as in this macro. | ||
| 9277 | |||
| 9278 | If you were to specify arguments, you would place them between | ||
| 9279 | quotation marks. The C macro for @code{goto-char} includes | ||
| 9280 | @code{"NGoto char: "} in this position to indicate that the function | ||
| 9281 | expects a raw prefix, in this case, a numerical location in a buffer, | ||
| 9282 | and provides a prompt. | ||
| 9283 | |||
| 9284 | @item | ||
| 9285 | The seventh part is a documentation string, just like the one for a | ||
| 9286 | function written in Emacs Lisp, except that every newline must be | ||
| 9287 | written explicitly as @samp{\n} followed by a backslash and carriage | ||
| 9288 | return. | ||
| 9289 | |||
| 9290 | @need 1000 | ||
| 9291 | Thus, the first two lines of documentation for @code{goto-char} are | ||
| 9292 | written like this: | ||
| 9293 | |||
| 9294 | @smallexample | ||
| 9295 | @group | ||
| 9296 | "Set point to POSITION, a number or marker.\n\ | ||
| 9297 | Beginning of buffer is position (point-min), end is (point-max). | ||
| 9298 | @end group | ||
| 9299 | @end smallexample | ||
| 9300 | @end itemize | ||
| 9301 | |||
| 9302 | @need 1200 | ||
| 9303 | In a C macro, the formal parameters come next, with a statement of | ||
| 9304 | what kind of object they are, followed by what might be called the `body' | ||
| 9305 | of the macro. For @code{delete-and-extract-region} the `body' | ||
| 9306 | consists of the following four lines: | ||
| 9307 | |||
| 9308 | @smallexample | ||
| 9309 | @group | ||
| 9310 | validate_region (&start, &end); | ||
| 9311 | if (XINT (start) == XINT (end)) | ||
| 9312 | return build_string (""); | ||
| 9313 | return del_range_1 (XINT (start), XINT (end), 1, 1); | ||
| 9314 | @end group | ||
| 9315 | @end smallexample | ||
| 9316 | |||
| 9317 | The @code{validate_region} function checks whether the values | ||
| 9318 | passed as the beginning and end of the region are the proper type and | ||
| 9319 | are within range. If the beginning and end positions are the same, | ||
| 9320 | then return and empty string. | ||
| 9321 | |||
| 9322 | The @code{del_range_1} function actually deletes the text. It is a | ||
| 9323 | complex function we will not look into. It updates the buffer and | ||
| 9324 | does other things. However, it is worth looking at the two arguments | ||
| 9325 | passed to @code{del_range}. These are @w{@code{XINT (start)}} and | ||
| 9326 | @w{@code{XINT (end)}}. | ||
| 9327 | |||
| 9328 | As far as the C language is concerned, @code{start} and @code{end} are | ||
| 9329 | two integers that mark the beginning and end of the region to be | ||
| 9330 | deleted@footnote{More precisely, and requiring more expert knowledge | ||
| 9331 | to understand, the two integers are of type `Lisp_Object', which can | ||
| 9332 | also be a C union instead of an integer type.}. | ||
| 9333 | |||
| 9334 | In early versions of Emacs, these two numbers were thirty-two bits | ||
| 9335 | long, but the code is slowly being generalized to handle other | ||
| 9336 | lengths. Three of the available bits are used to specify the type of | ||
| 9337 | information; the remaining bits are used as `content'. | ||
| 9338 | |||
| 9339 | @samp{XINT} is a C macro that extracts the relevant number from the | ||
| 9340 | longer collection of bits; the three other bits are discarded. | ||
| 9341 | |||
| 9342 | @need 800 | ||
| 9343 | The command in @code{delete-and-extract-region} looks like this: | ||
| 9344 | |||
| 9345 | @smallexample | ||
| 9346 | del_range_1 (XINT (start), XINT (end), 1, 1); | ||
| 9347 | @end smallexample | ||
| 9348 | |||
| 9349 | @noindent | ||
| 9350 | It deletes the region between the beginning position, @code{start}, | ||
| 9351 | and the ending position, @code{end}. | ||
| 9352 | |||
| 9353 | From the point of view of the person writing Lisp, Emacs is all very | ||
| 9354 | simple; but hidden underneath is a great deal of complexity to make it | ||
| 9355 | all 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 | ||
| 9365 | 2006 Oct 24 | ||
| 9366 | In Emacs 22, | ||
| 9367 | copy-region-as-kill is short, 12 lines, and uses | ||
| 9368 | filter-buffer-substring, which is longer, 39 lines | ||
| 9369 | and has delete-and-extract-region in it. | ||
| 9370 | delete-and-extract-region is written in C. | ||
| 9371 | |||
| 9372 | see Initializing a Variable with @code{defvar} | ||
| 9373 | |||
| 9374 | @end ignore | ||
| 9375 | |||
| 9376 | The @code{copy-region-as-kill} function is written in Emacs Lisp. Two | ||
| 9377 | functions within it, @code{kill-append} and @code{kill-new}, copy a | ||
| 9378 | region in a buffer and save it in a variable called the | ||
| 9379 | @code{kill-ring}. This section describes how the @code{kill-ring} | ||
| 9380 | variable is created and initialized using the @code{defvar} special | ||
| 9381 | form. | ||
| 9382 | |||
| 9383 | (Again we note that the term @code{kill-ring} is a misnomer. The text | ||
| 9384 | that is clipped out of the buffer can be brought back; it is not a ring | ||
| 9385 | of corpses, but a ring of resurrectable text.) | ||
| 9386 | |||
| 9387 | In Emacs Lisp, a variable such as the @code{kill-ring} is created and | ||
| 9388 | given an initial value by using the @code{defvar} special form. The | ||
| 9389 | name comes from ``define variable''. | ||
| 9390 | |||
| 9391 | The @code{defvar} special form is similar to @code{setq} in that it sets | ||
| 9392 | the value of a variable. It is unlike @code{setq} in two ways: first, | ||
| 9393 | it only sets the value of the variable if the variable does not already | ||
| 9394 | have a value. If the variable already has a value, @code{defvar} does | ||
| 9395 | not override the existing value. Second, @code{defvar} has a | ||
| 9396 | documentation string. | ||
| 9397 | |||
| 9398 | (Another special form, @code{defcustom}, is designed for variables | ||
| 9399 | that 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 | |||
| 9412 | You can see the current value of a variable, any variable, by using | ||
| 9413 | the @code{describe-variable} function, which is usually invoked by | ||
| 9414 | typing @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 | ||
| 9416 | current kill ring---this may be quite a lot! Conversely, if you have | ||
| 9417 | been doing nothing this Emacs session except read this document, you | ||
| 9418 | may have nothing in it. Also, you will see the documentation for | ||
| 9419 | @code{kill-ring}: | ||
| 9420 | |||
| 9421 | @smallexample | ||
| 9422 | @group | ||
| 9423 | Documentation: | ||
| 9424 | List of killed text sequences. | ||
| 9425 | Since the kill ring is supposed to interact nicely with cut-and-paste | ||
| 9426 | facilities offered by window systems, use of this variable should | ||
| 9427 | @end group | ||
| 9428 | @group | ||
| 9429 | interact 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 | ||
| 9432 | interaction; you may want to use them instead of manipulating the kill | ||
| 9433 | ring directly. | ||
| 9434 | @end group | ||
| 9435 | @end smallexample | ||
| 9436 | |||
| 9437 | @need 800 | ||
| 9438 | The 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 | ||
| 9449 | In 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 | ||
| 9451 | nothing back if you give a @code{yank} command. The documentation | ||
| 9452 | string is written just like the documentation string of a @code{defun}. | ||
| 9453 | As with the documentation string of the @code{defun}, the first line of | ||
| 9454 | the documentation should be a complete sentence, since some commands, | ||
| 9455 | like @code{apropos}, print only the first line of documentation. | ||
| 9456 | Succeeding lines should not be indented; otherwise they look odd when | ||
| 9457 | you 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 | |||
| 9464 | In the past, Emacs used the @code{defvar} special form both for | ||
| 9465 | internal variables that you would not expect a user to change and for | ||
| 9466 | variables that you do expect a user to change. Although you can still | ||
| 9467 | use @code{defvar} for user customizable variables, please use | ||
| 9468 | @code{defcustom} instead, since that special form provides a path into | ||
| 9469 | the Customization commands. (@xref{defcustom, , Specifying Variables | ||
| 9470 | using @code{defcustom}}.) | ||
| 9471 | |||
| 9472 | When you specified a variable using the @code{defvar} special form, | ||
| 9473 | you could distinguish a readily settable variable from others by | ||
| 9474 | typing an asterisk, @samp{*}, in the first column of its documentation | ||
| 9475 | string. 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 | ||
| 9487 | You could (and still can) use the @code{set-variable} command to | ||
| 9488 | change the value of @code{shell-command-default-error-buffer} | ||
| 9489 | temporarily. However, options set using @code{set-variable} are set | ||
| 9490 | only for the duration of your editing session. The new values are not | ||
| 9491 | saved between sessions. Each time Emacs starts, it reads the original | ||
| 9492 | value, unless you change the value within your @file{.emacs} file, | ||
| 9493 | either by setting it manually or by using @code{customize}. | ||
| 9494 | @xref{Emacs Initialization, , Your @file{.emacs} File}. | ||
| 9495 | |||
| 9496 | For me, the major use of the @code{set-variable} command is to suggest | ||
| 9497 | variables that I might want to set in my @file{.emacs} file. There | ||
| 9498 | are now more than 700 such variables --- far too many to remember | ||
| 9499 | readily. 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, | ||
| 9502 | The 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. | |||
| 8856 | buffer and stores that text in the kill ring, so you can get it back | 9634 | buffer and stores that text in the kill ring, so you can get it back |
| 8857 | by yanking. | 9635 | by yanking. |
| 8858 | 9636 | ||
| 8859 | @code{delete-and-extract-region} removes the text between point and | ||
| 8860 | mark 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 |
| 8863 | the kill ring, from which you can get it by yanking. The function | 9638 | the kill ring, from which you can get it by yanking. The function |
| 8864 | does not cut or remove the text from the buffer. | 9639 | does 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 | ||
| 9643 | mark 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. | |||
| 9385 | list is handled as a ring.) | 10164 | list 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} | ||
| 10207 | repetitively for each @code{yank-handler} segment. In turn, | ||
| 10208 | @code{insert-for-yank-1} strips text properties from the inserted text | ||
| 10209 | according to @code{yank-excluded-properties}. Otherwise, it is just | ||
| 10210 | like @code{insert}. We will stick with plain @code{insert} since it | ||
| 10211 | is easier to understand.) | ||
| 10212 | |||
| 9425 | To begin to understand how @code{yank} and @code{yank-pop} work, it is | 10213 | To begin to understand how @code{yank} and @code{yank-pop} work, it is |
| 9426 | first necessary to look at the @code{kill-ring-yank-pointer} variable | 10214 | first necessary to look at the @code{kill-ring-yank-pointer} variable |
| 9427 | and the @code{rotate-yank-pointer} function. | 10215 | and the @code{rotate-yank-pointer} function. |
| @@ -9525,15 +10313,46 @@ on the other hand, serves to indicate---that is, to `point to'---that part | |||
| 9525 | of the kill ring of which the first element (the @sc{car}) will be | 10313 | of the kill ring of which the first element (the @sc{car}) will be |
| 9526 | inserted. | 10314 | inserted. |
| 9527 | 10315 | ||
| 9528 | The @code{rotate-yank-pointer} function changes the element in the | 10316 | |
| 9529 | kill ring to which the @code{kill-ring-yank-pointer} points; when the | 10317 | @ignore |
| 9530 | pointer is set to point to the next element beyond the end of the kill | 10318 | In GNU Emacs 22, the @code{kill-new} function calls |
| 9531 | ring, it automatically sets it to point to the first element of the | 10319 | |
| 9532 | kill 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 | |
| 9534 | contains 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}. | 10324 | With 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. | ||
| 10330 | If N is zero, `interprogram-paste-function' is set, and calling it | ||
| 10331 | returns a string, then that string is added to the front of the | ||
| 10332 | kill ring and returned as the latest kill. | ||
| 10333 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the | ||
| 10334 | yanking 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 | |||
| 9584 | 15 and 30 times their default value.}. | 10403 | 15 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 |
| 9778 | The function requires several lines for its output. If you are | 10597 | The function requires several lines for its output. If you are |
| 9779 | reading this in Emacs 21 or a later version, you can evaluate the | 10598 | reading this in a recent instance of GNU Emacs, |
| 9780 | following expression inside of Info, as usual. | 10599 | @c GNU Emacs 21, GNU Emacs 22, or a later version, |
| 10600 | you can evaluate the following expression inside of Info, as usual. | ||
| 9781 | 10601 | ||
| 9782 | If you are using an earlier version of Emacs, you need to copy the | 10602 | If you are using an earlier version of Emacs, you need to copy the |
| 9783 | necessary expressions to your @file{*scratch*} buffer and evaluate | 10603 | necessary expressions to your @file{*scratch*} buffer and evaluate |
| @@ -9787,8 +10607,10 @@ earlier versions. | |||
| 9787 | You can copy the expressions by marking the beginning of the region | 10607 | You can copy the expressions by marking the beginning of the region |
| 9788 | with @kbd{C-@key{SPC}} (@code{set-mark-command}), moving the cursor to | 10608 | with @kbd{C-@key{SPC}} (@code{set-mark-command}), moving the cursor to |
| 9789 | the end of the region and then copying the region using @kbd{M-w} | 10609 | the 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 |
| 9791 | yank the expressions back by typing @kbd{C-y} (@code{yank}). | 10611 | then provides visual feedback). In the @file{*scratch*} |
| 10612 | buffer, you can yank the expressions back by typing @kbd{C-y} | ||
| 10613 | (@code{yank}). | ||
| 9792 | 10614 | ||
| 9793 | After you have copied the expressions to the @file{*scratch*} buffer, | 10615 | After you have copied the expressions to the @file{*scratch*} buffer, |
| 9794 | evaluate each expression in turn. Be sure to evaluate the last | 10616 | evaluate 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 | |||
| 9801 | each @samp{^J} stands for a `newline'.) | 10623 | each @samp{^J} stands for a `newline'.) |
| 9802 | 10624 | ||
| 9803 | @need 1500 | 10625 | @need 1500 |
| 9804 | If you are using Emacs 21 or later, you can evaluate these expressions | 10626 | In a recent instance of GNU Emacs, you can evaluate these expressions |
| 9805 | directly in the Info buffer, and the echo area will grow to show the | 10627 | directly in the Info buffer, and the echo area will grow to show the |
| 9806 | results. | 10628 | results. |
| 9807 | 10629 | ||
| @@ -9882,9 +10704,9 @@ Note that you need to set the initial value of @code{count}; usually it | |||
| 9882 | is set to 1. | 10704 | is 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 | |||
| 9967 | called @code{number-of-rows}. | 10789 | called @code{number-of-rows}. |
| 9968 | 10790 | ||
| 9969 | Finally, we need a variable to use as a counter. We could call this | 10791 | Finally, we need a variable to use as a counter. We could call this |
| 9970 | variable @code{counter}, but a better name is @code{row-number}. | 10792 | variable @code{counter}, but a better name is @code{row-number}. That |
| 9971 | That is because what the counter does is count rows, and a program | 10793 | is because what the counter does in this function is count rows, and a |
| 9972 | should be written to be as understandable as possible. | 10794 | program should be written to be as understandable as possible. |
| 9973 | 10795 | ||
| 9974 | When the Lisp interpreter first starts evaluating the expressions in the | 10796 | When the Lisp interpreter first starts evaluating the expressions in the |
| 9975 | function, the value of @code{total} should be set to zero, since we have | 10797 | function, 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 | |||
| 10547 | arguments that the final instance will stop. | 11369 | arguments 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 | |||
| 10668 | of numbers can be written recursively. Here is the code, including | 11490 | of numbers can be written recursively. Here is the code, including |
| 10669 | an expression to set the value of the variable @code{animals} to a list. | 11491 | an expression to set the value of the variable @code{animals} to a list. |
| 10670 | 11492 | ||
| 10671 | If you are using Emacs 20 or before, this example must be copied to | 11493 | If you are using GNU Emacs 20 or before, this example must be copied |
| 10672 | the @file{*scratch*} buffer and each expression must be evaluated | 11494 | to the @file{*scratch*} buffer and each expression must be evaluated |
| 10673 | there. Use @kbd{C-u C-x C-e} to evaluate the | 11495 | there. 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 |
| 10675 | results are printed in the buffer; otherwise the Lisp interpreter will | 11497 | results are printed in the buffer; otherwise the Lisp interpreter will |
| @@ -10679,8 +11501,8 @@ Also, place your cursor immediately after the last closing parenthesis | |||
| 10679 | of the @code{print-elements-recursively} function, before the comment. | 11501 | of the @code{print-elements-recursively} function, before the comment. |
| 10680 | Otherwise, the Lisp interpreter will try to evaluate the comment. | 11502 | Otherwise, the Lisp interpreter will try to evaluate the comment. |
| 10681 | 11503 | ||
| 10682 | If you are using Emacs 21 or later, you can evaluate this expression | 11504 | If you are using a more recent version, you can evaluate this |
| 10683 | directly in Info. | 11505 | expression 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 |
| 11434 | and find that the result is false, so it will invoke | 12256 | and find that the result is false, so it will invoke |
| 11435 | the then-part of the @code{if} clause: | 12257 | the 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 |
| 11549 | a Texinfo manual in the current directory. Or, if you are on the | 12371 | a Texinfo manual in the current directory. Or, if you are on the |
| 11550 | Internet, see | 12372 | Internet, 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 | |||
| 11594 | introduces several new features. | 12416 | introduces 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 | ||
| 12524 | process simpler. When its value is @code{nil}, then use the value | ||
| 12525 | defined by the function @code{sentence-end}, and that returns a value | ||
| 12526 | constructed from the variables @code{sentence-end-base}, | ||
| 12527 | @code{sentence-end-double-space}, @code{sentence-end-without-period}, | ||
| 12528 | and @code{sentence-end-without-space}. The critical variable is | ||
| 12529 | @code{sentence-end-base}; its global value is similar to the one | ||
| 12530 | described above but it also contains two additional quotation marks. | ||
| 12531 | These have differing degrees of curliness. The | ||
| 12532 | @code{sentence-end-without-period} variable, when true, tells Emacs | ||
| 12533 | that 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 |
| 11704 | literally in the pattern.) | 12538 | literally in the pattern.) |
| @@ -11739,7 +12573,6 @@ mark may, but need not, be followed by one or more carriage returns. In | |||
| 11739 | the pattern, the carriage return is inserted as an actual carriage | 12573 | the pattern, the carriage return is inserted as an actual carriage |
| 11740 | return between square brackets but here it is shown as @key{RET}. | 12574 | return 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 |
| 11804 | In the @code{forward-sentence} function, the regular expression will be | 12637 | In the @code{forward-sentence} function, the regular expression will be |
| 11805 | the value of the variable @code{sentence-end}, namely: | 12638 | the 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 |
| 11844 | Here is the code for @code{forward-sentence}: | 12677 | Here 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. | ||
| 12684 | With negative argument, move backward repeatedly to `sentence-beginning'. | ||
| 12685 | |||
| 12686 | The variable `sentence-end' is a regular expression that matches ends of | ||
| 12687 | sentences. 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 | ||
| 12716 | GNU 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 | ||
| 11876 | The function looks long at first sight and it is best to look at its | 12748 | The function looks long at first sight and it is best to look at its |
| 11877 | skeleton first, and then its muscle. The way to see the skeleton is to | 12749 | skeleton 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 | ||
| 11893 | This looks much simpler! The function definition consists of | 12770 | This looks much simpler! The function definition consists of |
| 11894 | documentation, an @code{interactive} expression, an @code{or} | 12771 | documentation, an @code{interactive} expression, an @code{or} |
| 11895 | expression, and @code{while} loops. | 12772 | expression, a @code{let} expression, and @code{while} loops. |
| 11896 | 12773 | ||
| 11897 | Let's look at each of these parts in turn. | 12774 | Let'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 | |||
| 11902 | that the processed prefix argument, if any, is passed to the | 12779 | that the processed prefix argument, if any, is passed to the |
| 11903 | function as its argument. (This will be a number.) If the function | 12780 | function as its argument. (This will be a number.) If the function |
| 11904 | is not passed an argument (it is optional) then the argument | 12781 | is 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. |
| 11906 | non-interactively without an argument, @code{arg} is bound to | 12783 | |
| 12784 | When @code{forward-sentence} is called non-interactively without an | ||
| 12785 | argument, @code{arg} is bound to @code{nil}. The @code{or} expression | ||
| 12786 | handles this. What it does is either leave the value of @code{arg} as | ||
| 12787 | it is, but only if @code{arg} is bound to a value; or it sets the | ||
| 12788 | value of @code{arg} to 1, in the case when @code{arg} is bound to | ||
| 11907 | @code{nil}. | 12789 | @code{nil}. |
| 11908 | 12790 | ||
| 11909 | The @code{or} expression handles the prefix argument. What it does is | 12791 | Next is a @code{let}. That specifies the values of two local |
| 11910 | either leave the value of @code{arg} as it is, but only if @code{arg} | 12792 | variables, @code{point} and @code{sentence-end}. The local value of |
| 11911 | is bound to a value; or it sets the value of @code{arg} to 1, in the | 12793 | point, from before the search, is used in the |
| 11912 | case when @code{arg} is bound to @code{nil}. | 12794 | @code{constrain-to-field} function which handles forms and |
| 12795 | equivalents. 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 | ||
| 11917 | Two @code{while} loops follow the @code{or} expression. The first | 12801 | Two @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 | 12802 | true-or-false-test that tests true if the prefix argument for |
| 11919 | argument for @code{forward-sentence} is a negative number. This is for | 12803 | @code{forward-sentence} is a negative number. This is for going |
| 11920 | going backwards. The body of this loop is similar to the body of the | 12804 | backwards. The body of this loop is similar to the body of the second |
| 11921 | second @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 |
| 11922 | skip this @code{while} loop and concentrate on the second @code{while} | 12806 | this @code{while} loop and concentrate on the second @code{while} |
| 11923 | loop. | 12807 | loop. |
| 11924 | 12808 | ||
| 11925 | @need 1500 | 12809 | @need 1500 |
| @@ -12052,6 +12936,9 @@ false. The false then causes the @code{if} to evaluate its third | |||
| 12052 | argument, which is @code{(goto-char par-end)}: it moves point to the | 12936 | argument, which is @code{(goto-char par-end)}: it moves point to the |
| 12053 | end of the paragraph. | 12937 | end of the paragraph. |
| 12054 | 12938 | ||
| 12939 | (And if the text is in a form or equivalent, and point may not move | ||
| 12940 | fully, then the @code{constrain-to-field} function comes into play.) | ||
| 12941 | |||
| 12055 | Regular expression searches are exceptionally useful and the pattern | 12942 | Regular expression searches are exceptionally useful and the pattern |
| 12056 | illustrated by @code{re-search-forward}, in which the search is the | 12943 | illustrated by @code{re-search-forward}, in which the search is the |
| 12057 | test of an @code{if} expression, is handy. You will see or write code | 12944 | test 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. | ||
| 12956 | With argument ARG, do it ARG times; | ||
| 12957 | a negative argument ARG = -N means move backward N paragraphs. | ||
| 12958 | |||
| 12959 | A line which `paragraph-start' matches either separates paragraphs | ||
| 12960 | \(if `paragraph-separate' matches it also) or is the first line of a paragraph. | ||
| 12961 | A paragraph end is the beginning of a line which is not part of the paragraph | ||
| 12962 | to which the end of the previous line belongs, or the end of the buffer. | ||
| 12963 | Returns 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 | |||
| 12065 | The @code{forward-paragraph} function moves point forward to the end | 13097 | The @code{forward-paragraph} function moves point forward to the end |
| 12066 | of the paragraph. It is usually bound to @kbd{M-@}} and makes use of a | 13098 | of the paragraph. It is usually bound to @kbd{M-@}} and makes use of a |
| 12067 | number of functions that are important in themselves, including | 13099 | number of functions that are important in themselves, including |
| @@ -12091,14 +13123,9 @@ exists, especially when blank lines separate paragraphs. | |||
| 12091 | This is an added complication. | 13123 | This 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 | ||
| 12149 | The next line of the @code{forward-paragraph} function begins a | 13174 | The 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 |
| 12151 | we have seen so far. The symbol is @code{let*} not @code{let}. | 13176 | symbol is @code{let*} not @code{let}. |
| 12152 | 13177 | ||
| 12153 | The @code{let*} special form is like @code{let} except that Emacs sets | 13178 | The @code{let*} special form is like @code{let} except that Emacs sets |
| 12154 | each variable in sequence, one after another, and variables in the | 13179 | each variable in sequence, one after another, and variables in the |
| 12155 | latter part of the varlist can make use of the values to which Emacs | 13180 | latter part of the varlist can make use of the values to which Emacs |
| 12156 | set variables in the earlier part of the varlist. | 13181 | set variables in the earlier part of the varlist. |
| 12157 | 13182 | ||
| 12158 | In the @code{let*} expression in this function, Emacs binds two | 13183 | @ignore |
| 12159 | variables: @code{fill-prefix-regexp} and @code{paragraph-separate}. | 13184 | ( refappend save-excursion, , code save-excursion in code append-to-buffer .) |
| 12160 | The value to which @code{paragraph-separate} is bound depends on the | 13185 | @end ignore |
| 12161 | value of @code{fill-prefix-regexp}. | ||
| 12162 | 13186 | ||
| 12163 | @need 1200 | 13187 | (@ref{append save-excursion, , @code{save-excursion} in @code{append-to-buffer}}.) |
| 12164 | Let's look at each in turn. The symbol @code{fill-prefix-regexp} is | 13188 | |
| 12165 | set to the value returned by evaluating the following list: | 13189 | In the @code{let*} expression in this function, Emacs binds a total of |
| 13190 | seven variables: @code{opoint}, @code{fill-prefix-regexp}, | ||
| 13191 | @code{parstart}, @code{parsep}, @code{sp-parstart}, @code{start}, and | ||
| 13192 | @code{found-start}. | ||
| 13193 | |||
| 13194 | The variable @code{parsep} appears twice, first, to remove instances | ||
| 13195 | of @samp{^}, and second, to handle fill prefixes. | ||
| 13196 | |||
| 13197 | The variable @code{opoint} is just the value of @code{point}. As you | ||
| 13198 | can guess, it is used in a @code{constrain-to-field} expression, just | ||
| 13199 | as in @code{forward-sentence}. | ||
| 13200 | |||
| 13201 | The variable @code{fill-prefix-regexp} is set to the value returned by | ||
| 13202 | evaluating 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 | |||
| 12226 | will exactly match the fill prefix if the fill prefix exists. | 13263 | will exactly match the fill prefix if the fill prefix exists. |
| 12227 | Otherwise, the variable will be set to @code{nil}. | 13264 | Otherwise, the variable will be set to @code{nil}. |
| 12228 | 13265 | ||
| 12229 | The second local variable in the @code{let*} expression is | 13266 | The next two local variables in the @code{let*} expression are |
| 12230 | @code{paragraph-separate}. It is bound to the value returned by | 13267 | designed to remove instances of @samp{^} from @code{parstart} and |
| 12231 | evaluating the expression: | 13268 | @code{parsep}, the local variables indicate the paragraph start and |
| 12232 | 13269 | the paragraph separator. The next expression sets @code{parsep} | |
| 12233 | @smallexample | 13270 | again. 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 | ||
| 12242 | This expression shows why @code{let*} rather than @code{let} was used. | 13272 | This is the setting that requires the definition call @code{let*} |
| 12243 | The true-or-false-test for the @code{if} depends on whether the variable | 13273 | rather 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. | 13274 | depends on whether the variable @code{fill-prefix-regexp} evaluates to |
| 13275 | @code{nil} or some other value. | ||
| 12245 | 13276 | ||
| 12246 | If @code{fill-prefix-regexp} does not have a value, Emacs evaluates | 13277 | If @code{fill-prefix-regexp} does not have a value, Emacs evaluates |
| 12247 | the else-part of the @code{if} expression and binds | 13278 | the else-part of the @code{if} expression and binds @code{parsep} to |
| 12248 | @code{paragraph-separate} to its local value. | 13279 | its local value. (@code{parsep} is a regular expression that matches |
| 12249 | (@code{paragraph-separate} is a regular expression that matches what | 13280 | what separates paragraphs.) |
| 12250 | separates paragraphs.) | ||
| 12251 | 13281 | ||
| 12252 | But if @code{fill-prefix-regexp} does have a value, Emacs evaluates | 13282 | But if @code{fill-prefix-regexp} does have a value, Emacs evaluates |
| 12253 | the then-part of the @code{if} expression and binds | 13283 | the then-part of the @code{if} expression and binds @code{parsep} to a |
| 12254 | @code{paragraph-separate} to a regular expression that includes the | 13284 | regular expression that includes the @code{fill-prefix-regexp} as part |
| 12255 | @code{fill-prefix-regexp} as part of the pattern. | 13285 | of the pattern. |
| 12256 | 13286 | ||
| 12257 | Specifically, @code{paragraph-separate} is set to the original value | 13287 | Specifically, @code{parsep} is set to the original value of the |
| 12258 | of the paragraph separate regular expression concatenated with an | 13288 | paragraph separate regular expression concatenated with an alternative |
| 12259 | alternative expression that consists of the @code{fill-prefix-regexp} | 13289 | expression that consists of the @code{fill-prefix-regexp} followed by |
| 12260 | followed by a blank line. The @samp{^} indicates that the | 13290 | optional whitespace to the end of the line. The whitespace is defined |
| 12261 | @code{fill-prefix-regexp} must begin a line, and the optional | 13291 | by @w{@code{"[ \t]*$"}}.) The @samp{\\|} defines this portion of the |
| 12262 | whitespace to the end of the line is defined by @w{@code{"[ \t]*$"}}.) | 13292 | regexp as an alternative to @code{parsep}. |
| 12263 | The @samp{\\|} defines this portion of the regexp as an alternative to | 13293 | |
| 12264 | @code{paragraph-separate}. | 13294 | According 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 | ||
| 12266 | Now we get into the body of the @code{let*}. The first part of the body | 13298 | Now we get into the body of the @code{let*}. The first part of the body |
| 12267 | of the @code{let*} deals with the case when the function is given a | 13299 | of the @code{let*} deals with the case when the function is given a |
| 12268 | negative argument and is therefore moving backwards. We will skip this | 13300 | negative argument and is therefore moving backwards. We will skip this |
| 12269 | section. | 13301 | section. |
| 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 | ||
| 12274 | The second part of the body of the @code{let*} deals with forward | 13306 | The 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 |
| 12279 | forward one paragraph. | 13311 | forward 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 | |||
| 12281 | This part handles three situations: when point is between paragraphs, | 13349 | This part handles three situations: when point is between paragraphs, |
| 12282 | when point is within a paragraph and there is a fill prefix, and | 13350 | when there is a fill prefix and when there is no fill prefix. |
| 12283 | when point is within a paragraph and there is no fill prefix. | ||
| 12284 | 13351 | ||
| 12285 | @need 800 | 13352 | @need 800 |
| 12286 | The @code{while} loop looks like this: | 13353 | The @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 | ||
| 12319 | We can see immediately that this is a decrementing counter @code{while} | 13407 | @findex eobp |
| 12320 | loop, using the expression @code{(setq arg (1- arg))} as the decrementer. | 13408 | We can see that this is a decrementing counter @code{while} loop, |
| 13409 | using the expression @code{(setq arg (1- arg))} as the decrementer. | ||
| 13410 | That expression is not far from the @code{while}, but is hidden in | ||
| 13411 | another Lisp macro, an @code{unless} macro. Unless we are at the end | ||
| 13412 | of the buffer --- that is what the @code{eobp} function determines; it | ||
| 13413 | is an abbreviation of @samp{End Of Buffer P} --- we decrease the value | ||
| 13414 | of @code{arg} by one. | ||
| 13415 | |||
| 13416 | (If we are at the end of the buffer, we cannot go forward any more and | ||
| 13417 | the next loop of the @code{while} expression will test false since the | ||
| 13418 | test is an @code{and} with @code{(not (eobp))}. The @code{not} | ||
| 13419 | function means exactly as you expect; it is another name for | ||
| 13420 | @code{null}, a function that returns true when its argument is false.) | ||
| 13421 | |||
| 13422 | Interestingly, the loop count is not decremented until we leave the | ||
| 13423 | space between paragraphs, unless we come to the end of buffer or stop | ||
| 13424 | seeing the local value of the paragraph separator. | ||
| 13425 | |||
| 13426 | That second @code{while} also has a @code{(move-to-left-margin)} | ||
| 13427 | expression. The function is self-explanatory. It is inside a | ||
| 13428 | @code{progn} expression and not the last element of its body, so it is | ||
| 13429 | only invoked for its side effect, which is to move point to the left | ||
| 13430 | margin of the current line. | ||
| 12321 | 13431 | ||
| 12322 | @need 800 | 13432 | @findex looking-at |
| 12323 | The body of the loop consists of three expressions: | 13433 | The @code{looking-at} function is also self-explanatory; it returns |
| 13434 | true if the text after point matches the regular expression given as | ||
| 13435 | its argument. | ||
| 12324 | 13436 | ||
| 12325 | @smallexample | 13437 | The rest of the body of the loop looks difficult at first, but makes |
| 12326 | @group | 13438 | sense 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} | 13441 | First 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 |
| 12346 | When the Emacs Lisp interpreter evaluates the body of the | 13457 | This expression moves point forward line by line so long |
| 12347 | @code{while} loop, the first thing it does is evaluate the | 13458 | as four conditions are true: |
| 12348 | @code{(beginning-of-line)} expression and move point to the beginning | ||
| 12349 | of 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 | ||
| 12351 | space between paragraphs, if it should happen to be there. Finally, | ||
| 12352 | there is an @code{if} expression that actually moves point to the end | ||
| 12353 | of the paragraph. | ||
| 12354 | |||
| 12355 | @node fwd-para between paragraphs, fwd-para within paragraph, fwd-para while, forward-paragraph | ||
| 12356 | @unnumberedsubsec Between paragraphs | ||
| 12357 | 13459 | ||
| 12358 | First, let us look at the inner @code{while} loop. This loop handles | 13460 | @enumerate |
| 12359 | the case when point is between paragraphs; it uses three functions | ||
| 12360 | that 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, | 13462 | Point is not at the end of the buffer. |
| 12368 | except that @code{prog1} evaluates its arguments in sequence and then | ||
| 12369 | returns the value of its first argument as the value of the whole | ||
| 12370 | expression. (@code{progn} returns the value of its last argument as the | ||
| 12371 | value 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 | 13465 | We can move to the left margin of the text and are |
| 12376 | function that returns true if point is at the end of the buffer. | 13466 | not 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 | 13469 | The text following point does not separate paragraphs. |
| 12380 | point matches the regular expression passed @code{looking-at} as its | ||
| 12381 | argument. | ||
| 12382 | @end itemize | ||
| 12383 | 13470 | ||
| 12384 | @need 800 | 13471 | @item |
| 12385 | The @code{while} loop we are studying looks like this: | 13472 | The pattern following point is the fill prefix regular expression. |
| 13473 | @end enumerate | ||
| 12386 | 13474 | ||
| 12387 | @smallexample | 13475 | The last condition may be puzzling, until you remember that point was |
| 12388 | @group | 13476 | moved to the beginning of the line early in the @code{forward-paragraph} |
| 12389 | (while (prog1 (and (not (eobp)) | 13477 | function. 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 | 13481 | Consider what happens when there is no fill prefix. |
| 12397 | This is a @code{while} loop with no body! The true-or-false-test of the | ||
| 12398 | loop 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 |
| 12409 | The first argument to the @code{prog1} is the @code{and} expression. It | 13499 | This @code{while} loop has us searching forward for |
| 12410 | has 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 |
| 12411 | also a test of whether the pattern following point matches the regular | 13501 | with a the local value of the start of a paragraph or of a paragraph |
| 12412 | expression for separating paragraphs. | 13502 | separator. (The latter two are within an expression starting |
| 12413 | 13503 | @code{\(?:} so that they are not referenced by the | |
| 12414 | If the cursor is not at the end of the buffer and if the characters | 13504 | @code{match-beginning} function.) |
| 12415 | following the cursor mark the separation between two paragraphs, then | ||
| 12416 | the @code{and} expression is true. After evaluating the @code{and} | ||
| 12417 | expression, the Lisp interpreter evaluates the second argument to | ||
| 12418 | @code{prog1}, which is @code{forward-line}. This moves point forward | ||
| 12419 | one line. The value returned by the @code{prog1} however, is the | ||
| 12420 | value of its first argument, so the @code{while} loop continues so | ||
| 12421 | long as point is not at the end of the buffer and is between | ||
| 12422 | paragraphs. 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 | ||
| 12425 | when point is moved from between paragraphs to a paragraph, it is left | ||
| 12426 | at 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 | |||
| 12431 | The next expression in the outer @code{while} loop is an @code{if} | ||
| 12432 | expression. The Lisp interpreter evaluates the then-part of the | ||
| 12433 | @code{if} when the @code{fill-prefix-regexp} variable has a value other | ||
| 12434 | than @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 | ||
| 12436 | fill 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 | 13507 | The two expressions, |
| 12440 | |||
| 12441 | It is simplest to look at the code for the case when there is no fill | ||
| 12442 | prefix first. This code consists of yet another inner @code{if} | ||
| 12443 | expression, 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 |
| 12454 | This expression actually does the work that most people think of as | 13517 | mean go to the start of the text matched by the regular expression |
| 12455 | the primary purpose of the @code{forward-paragraph} command: it causes | 13518 | search. |
| 12456 | a regular expression search to occur that searches forward to the | ||
| 12457 | start of the next paragraph and if it is found, moves point there; but | ||
| 12458 | if the start of another paragraph if not found, it moves point to the | ||
| 12459 | end of the accessible region of the buffer. | ||
| 12460 | 13519 | ||
| 12461 | The only unfamiliar part of this is the use of @code{match-beginning}. | 13520 | The @code{(match-beginning 0)} expression is new. It returns a number |
| 12462 | This is another function that is new to us. The | 13521 | specifying the location of the start of the text that was matched by |
| 12463 | @code{match-beginning} function returns a number specifying the | 13522 | the last search. |
| 12464 | location of the start of the text that was matched by the last regular | ||
| 12465 | expression search. | ||
| 12466 | 13523 | ||
| 12467 | The @code{match-beginning} function is used here because of a | 13524 | The @code{match-beginning} function is used here because of a |
| 12468 | characteristic of a forward search: a successful forward search, | 13525 | characteristic of a forward search: a successful forward search, |
| 12469 | regardless of whether it is a plain search or a regular expression | 13526 | regardless of whether it is a plain search or a regular expression |
| 12470 | search, will move point to the end of the text that is found. In this | 13527 | search, moves point to the end of the text that is found. In this |
| 12471 | case, a successful search will move point to the end of the pattern for | 13528 | case, 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}. |
| 12473 | paragraph rather than the end of the current one. | ||
| 12474 | 13530 | ||
| 12475 | However, we want to put point at the end of the current paragraph, not at | 13531 | However, we want to put point at the end of the current paragraph, not |
| 12476 | the beginning of the next one. The two positions may be different, | 13532 | somewhere else. Indeed, since the search possibly includes the |
| 12477 | because there may be several blank lines between paragraphs. | 13533 | paragraph separator, point may end up at the beginning of the next one |
| 13534 | unless we use an expression that includes @code{match-beginning}. | ||
| 12478 | 13535 | ||
| 12479 | @findex match-beginning | 13536 | @findex match-beginning |
| 12480 | When given an argument of 0, @code{match-beginning} returns the position | 13537 | When given an argument of 0, @code{match-beginning} returns the |
| 12481 | that is the start of the text that the most recent regular | 13538 | position that is the start of the text matched by the most recent |
| 12482 | expression search matched. In this case, the most recent regular | 13539 | search. In this case, the most recent search looks for |
| 12483 | expression 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, | 13541 | the beginning position of that pattern, rather than the end position |
| 12485 | rather than the end of the pattern. The beginning position is the end | 13542 | of that pattern. |
| 12486 | of 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 |
| 12490 | expression in the last regular expression. It is a useful function.) | 13546 | parenthesized expression in the last search unless that parenthesized |
| 12491 | 13547 | expression 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 | 13548 | appears here since the argument is 0.) |
| 12493 | @unnumberedsubsec With a fill prefix | ||
| 12494 | 13549 | ||
| 12495 | The 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 | 13551 | The last expression when there is no fill prefix is |
| 12497 | there is a fill prefix, the then-part of this @code{if} is evaluated. | ||
| 12498 | It 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 |
| 12510 | What this expression does is move point forward line by line so long | 13561 | This says that if there is no fill prefix and if we are not at the |
| 12511 | as three conditions are true: | 13562 | end, point should move to the beginning of whatever was found by the |
| 12512 | 13563 | regular expression search for @code{sp-parstart}. | |
| 12513 | @enumerate | ||
| 12514 | @item | ||
| 12515 | Point is not at the end of the buffer. | ||
| 12516 | |||
| 12517 | @item | ||
| 12518 | The text following point does not separate paragraphs. | ||
| 12519 | |||
| 12520 | @item | ||
| 12521 | The pattern following point is the fill prefix regular expression. | ||
| 12522 | @end enumerate | ||
| 12523 | |||
| 12524 | The last condition may be puzzling, until you remember that point was | ||
| 12525 | moved to the beginning of the line early in the @code{forward-paragraph} | ||
| 12526 | function. 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 | |||
| 12532 | In summary, when moving forward, the @code{forward-paragraph} function | ||
| 12533 | does the following: | ||
| 12534 | |||
| 12535 | @itemize @bullet | ||
| 12536 | @item | ||
| 12537 | Move point to the beginning of the line. | ||
| 12538 | |||
| 12539 | @item | ||
| 12540 | Skip over lines between paragraphs. | ||
| 12541 | |||
| 12542 | @item | ||
| 12543 | Check whether there is a fill prefix, and if there is: | ||
| 12544 | |||
| 12545 | @itemize --- | ||
| 12546 | |||
| 12547 | @item | ||
| 12548 | Go forward line by line so long as the line is not a paragraph | ||
| 12549 | separating line. | ||
| 12550 | @end itemize | ||
| 12551 | |||
| 12552 | @item | ||
| 12553 | But if there is no fill prefix, | ||
| 12554 | |||
| 12555 | @itemize --- | ||
| 12556 | |||
| 12557 | @item | ||
| 12558 | Search for the next paragraph start pattern. | ||
| 12559 | |||
| 12560 | @item | ||
| 12561 | Go to the beginning of the paragraph start pattern, which will be the | ||
| 12562 | end of the previous paragraph. | ||
| 12563 | |||
| 12564 | @item | ||
| 12565 | Or else go to the end of the accessible portion of the buffer. | ||
| 12566 | @end itemize | ||
| 12567 | @end itemize | ||
| 12568 | |||
| 12569 | @need 1200 | ||
| 12570 | For review, here is the code we have just been discussing, formatted | ||
| 12571 | for 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 | ||
| 12622 | The full definition for the @code{forward-paragraph} function not only | 13565 | The full definition for the @code{forward-paragraph} function not only |
| 12623 | includes this code for going forwards, but also code for going backwards. | 13566 | includes code for going forwards, but also code for going backwards. |
| 12624 | 13567 | ||
| 12625 | If you are reading this inside of GNU Emacs and you want to see the | 13568 | If you are reading this inside of GNU Emacs and you want to see the |
| 12626 | whole function, you can type @kbd{C-h f} (@code{describe-function}) | 13569 | whole 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 | |||
| 12631 | your sources! Without them, you are like a person who tries to drive | 13574 | your sources! Without them, you are like a person who tries to drive |
| 12632 | a car with his eyes shut!) | 13575 | a car with his eyes shut!) |
| 12633 | 13576 | ||
| 12634 | @c !!! again, 21.0.100 tags table location in this paragraph | ||
| 12635 | Or -- 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. | ||
| 12637 | This will take you directly to the source. If the @code{find-tag} | ||
| 12638 | function first asks you for the name of a @file{TAGS} table, give it | ||
| 12639 | the 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 | |||
| 12643 | You can also create your own @file{TAGS} file for directories that | ||
| 12644 | lack 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 | ||
| 13582 | Besides @kbd{C-h f} (@code{describe-function}), another way to see the | ||
| 13583 | source of a function is to type @kbd{M-.} (@code{find-tag}) and the | ||
| 13584 | name of the function when prompted for it. This is a good habit to | ||
| 13585 | get 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} | ||
| 13587 | table, 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 | ||
| 13590 | just told you the location that provides both my C and my Emacs Lisp | ||
| 13591 | sources.) | ||
| 13592 | |||
| 13593 | You can also create your own @file{TAGS} file for directories that | ||
| 13594 | lack one. | ||
| 13595 | |||
| 12654 | The @kbd{M-.} (@code{find-tag}) command takes you directly to the | 13596 | The @kbd{M-.} (@code{find-tag}) command takes you directly to the |
| 12655 | source for a function, variable, node, or other source. The function | 13597 | source for a function, variable, node, or other source. The function |
| 12656 | depends on tags tables to tell it where to go. | 13598 | depends 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 | |||
| 12720 | you want, but do not know where it is, you can use the @code{locate} | 13662 | you want, but do not know where it is, you can use the @code{locate} |
| 12721 | program to attempt to find it. | 13663 | program to attempt to find it. |
| 12722 | 13664 | ||
| 12723 | Type @w{@kbd{M-x locate RET TAGS RET}} and Emacs will list for you the | 13665 | Type @w{@kbd{M-x locate @key{RET} TAGS @key{RET}}} and Emacs will list |
| 12724 | full path names of all your @file{TAGS} files. On my system, this | 13666 | for you the full path names of all your @file{TAGS} files. On my |
| 12725 | command lists 34 @file{TAGS} files. On the other hand, a `plain | 13667 | system, this command lists 34 @file{TAGS} files. On the other hand, a |
| 12726 | vanilla' system I recently installed did not contain any @file{TAGS} | 13668 | `plain vanilla' system I recently installed did not contain any |
| 12727 | files. | 13669 | @file{TAGS} files. |
| 12728 | 13670 | ||
| 12729 | If the tags table you want has been created, you can use the @code{M-x | 13671 | If the tags table you want has been created, you can use the @code{M-x |
| 12730 | visit-tags-table} command to specify it. Otherwise, you will need to | 13672 | visit-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 |
| 12802 | A regular expression that specifies the pattern to search for. | 13745 | A regular expression that specifies the pattern to search for. |
| 13746 | (Remember to put quotation marks around this argument!) | ||
| 12803 | 13747 | ||
| 12804 | @item | 13748 | @item |
| 12805 | Optionally, the limit of the search. | 13749 | Optionally, the limit of the search. |
| @@ -12844,20 +13788,6 @@ Return @code{t} for true if point is at the end of the accessible part | |||
| 12844 | of a buffer. The end of the accessible part is the end of the buffer | 13788 | of a buffer. The end of the accessible part is the end of the buffer |
| 12845 | if the buffer is not narrowed; it is the end of the narrowed part if | 13789 | if the buffer is not narrowed; it is the end of the narrowed part if |
| 12846 | the buffer is narrowed. | 13790 | the buffer is narrowed. |
| 12847 | |||
| 12848 | @item prog1 | ||
| 12849 | Evaluate each argument in sequence and then return the value of the | ||
| 12850 | @emph{first}. | ||
| 12851 | |||
| 12852 | @need 1250 | ||
| 12853 | For 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 | |||
| 12928 | words in just a section, rather than all of a buffer. So it makes | 13858 | words in just a section, rather than all of a buffer. So it makes |
| 12929 | more sense to design the command to count the number of words in a | 13859 | more sense to design the command to count the number of words in a |
| 12930 | region. Once you have a @code{count-words-region} command, you can, | 13860 | region. Once you have a @code{count-words-region} command, you can, |
| 12931 | if you wish, count words in a whole buffer by marking it with @kbd{C-x | 13861 | if you wish, count words in a whole buffer by marking it with |
| 12932 | h} (@code{mark-whole-buffer}). | 13862 | @w{@kbd{C-x h}} (@code{mark-whole-buffer}). |
| 12933 | 13863 | ||
| 12934 | Clearly, counting words is a repetitive act: starting from the | 13864 | Clearly, counting words is a repetitive act: starting from the |
| 12935 | beginning of the region, you count the first word, then the second | 13865 | beginning 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 | |||
| 12938 | or to a @code{while} loop. | 13868 | or 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 |
| 13046 | single backslash has special meaning to the Emacs Lisp interpreter. It | 13976 | single backslash has special meaning to the Emacs Lisp interpreter. |
| 13047 | indicates that the following character is interpreted differently than | 13977 | It indicates that the following character is interpreted differently |
| 13048 | usual. For example, the two characters, @samp{\n}, stand for | 13978 | than 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 |
| 13050 | backslashes in a row stand for an ordinary, `unspecial' backslash.) | 13980 | backslashes in a row stand for an ordinary, `unspecial' backslash, |
| 13981 | which in this case is followed by a letter, the combination of which | ||
| 13982 | is important to @code{re-search-forward}.) | ||
| 13051 | 13983 | ||
| 13052 | We need a counter to count how many words there are; this variable | 13984 | We need a counter to count how many words there are; this variable |
| 13053 | must first be set to 0 and then incremented each time Emacs goes | 13985 | must 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}.) |
| 13316 | Functions}.) | ||
| 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 |
| 14252 | information about @code{and}.) | ||
| 13321 | @end ifinfo | 14253 | @end ifinfo |
| 13322 | 14254 | ||
| 13323 | The @code{re-search-forward} expression returns @code{t} if the search | 14255 | The @code{re-search-forward} expression returns @code{t} if the search |
| 13324 | succeeds and as a side effect moves point. Consequently, as words are | 14256 | succeeds and as a side effect moves point. Consequently, as words are |
| 13325 | found, point is moved through the region. When the search | 14257 | found, point is moved through the region. When the search expression |
| 13326 | expression fails to find another word, or when point reaches the end | 14258 | fails to find another word, or when point reaches the end of the |
| 13327 | of the region, the true-or-false-test tests false, the @code{while} | 14259 | region, the true-or-false-test tests false, the @code{while} loop |
| 13328 | loop exists, and the @code{count-words-region} function displays one | 14260 | exits, and the @code{count-words-region} function displays one or |
| 13329 | or other of its messages. | 14261 | other of its messages. |
| 13330 | 14262 | ||
| 13331 | After incorporating these final changes, the @code{count-words-region} | 14263 | After incorporating these final changes, the @code{count-words-region} |
| 13332 | works without bugs (or at least, without bugs that I have found!). | 14264 | works 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 | |||
| 13590 | each word, the counting mechanism must be an expression that adds one | 14522 | each word, the counting mechanism must be an expression that adds one |
| 13591 | to the value returned by a call to @code{recursive-count-words}. | 14523 | to the value returned by a call to @code{recursive-count-words}. |
| 13592 | 14524 | ||
| 14525 | @need 800 | ||
| 13593 | Consider several cases: | 14526 | Consider 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 | ||
| 14164 | A file such as @file{simple.el} may have 80 or more function | 15097 | A file such as @file{simple.el} may have a hundred or more function |
| 14165 | definitions within it. Our long term goal is to collect statistics on | 15098 | definitions within it. Our long term goal is to collect statistics on |
| 14166 | many files, but as a first step, our immediate goal is to collect | 15099 | many files, but as a first step, our immediate goal is to collect |
| 14167 | statistics on one file. | 15100 | statistics on one file. |
| @@ -14223,9 +15156,7 @@ command. This command is almost, but not quite right for the lengths | |||
| 14223 | problem. | 15156 | problem. |
| 14224 | 15157 | ||
| 14225 | @need 1200 | 15158 | @need 1200 |
| 14226 | Let's look at the source for @code{find-file} (you can use the | 15159 | Let's look at the source for @code{find-file}: |
| 14227 | @code{find-tag} command or @kbd{C-h f} (@code{describe-function}) to | ||
| 14228 | find 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 | ||
| 14241 | The definition possesses short but complete documentation and an | 15172 | @noindent |
| 14242 | interactive specification that prompts you for a file name when you | 15173 | (The most recent version of the @code{find-file} function definition |
| 14243 | use the command interactively. The body of the definition contains | 15174 | permits you to specify optional wildcards visit multiple files; that |
| 14244 | two functions, @code{find-file-noselect} and @code{switch-to-buffer}. | 15175 | makes the definition more complex and we will not discuss it here, |
| 15176 | since 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 | ||
| 15180 | In Emacs 22 | ||
| 15181 | (defun find-file (filename &optional wildcards) | ||
| 15182 | "Edit file FILENAME. | ||
| 15183 | Switch to a buffer visiting file FILENAME, | ||
| 15184 | creating one if none already exists. | ||
| 15185 | Interactively, the default if you just type RET is the current directory, | ||
| 15186 | but the visited file name is available through the minibuffer history: | ||
| 15187 | type M-n to pull it into the minibuffer. | ||
| 15188 | |||
| 15189 | Interactively, or if WILDCARDS is non-nil in a call from Lisp, | ||
| 15190 | expand wildcards (if any) and visit multiple files. You can | ||
| 15191 | suppress wildcard expansion by setting `find-file-wildcards' to nil. | ||
| 15192 | |||
| 15193 | To visit a file without any kind of conversion and without | ||
| 15194 | automatically 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 | |||
| 15202 | The definition I am showing possesses short but complete documentation | ||
| 15203 | and an interactive specification that prompts you for a file name when | ||
| 15204 | you use the command interactively. The body of the definition | ||
| 15205 | contains two functions, @code{find-file-noselect} and | ||
| 15206 | @code{switch-to-buffer}. | ||
| 14245 | 15207 | ||
| 14246 | According to its documentation as shown by @kbd{C-h f} (the | 15208 | According 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} |
| 14248 | function reads the named file into a buffer and returns the buffer. | 15210 | function reads the named file into a buffer and returns the buffer. |
| 14249 | However, the buffer is not selected. Emacs does not switch its | 15211 | (Its most recent version includes an optional wildcards argument, |
| 14250 | attention (or yours if you are using @code{find-file-noselect}) to the | 15212 | too, as well as another to read a file literally and an other you |
| 14251 | named buffer. That is what @code{switch-to-buffer} does: it switches | 15213 | suppress warning messages. These optional arguments are irrelevant.) |
| 14252 | the buffer to which Emacs attention is directed; and it switches the | 15214 | |
| 15215 | However, the @code{find-file-noselect} function does not select the | ||
| 15216 | buffer 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 | ||
| 15218 | buffer. That is what @code{switch-to-buffer} does: it switches the | ||
| 15219 | buffer to which Emacs attention is directed; and it switches the | ||
| 14253 | buffer displayed in the window to the new buffer. We have discussed | 15220 | buffer displayed in the window to the new buffer. We have discussed |
| 14254 | buffer switching elsewhere. (@xref{Switching Buffers}.) | 15221 | buffer switching elsewhere. (@xref{Switching Buffers}.) |
| 14255 | 15222 | ||
| @@ -14261,7 +15228,7 @@ program to a different buffer but does not redisplay it on the screen. | |||
| 14261 | So instead of calling on @code{find-file} to do the job, we must write | 15228 | So instead of calling on @code{find-file} to do the job, we must write |
| 14262 | our own expression. | 15229 | our own expression. |
| 14263 | 15230 | ||
| 14264 | The task is easy: use @code{find-file-noselect} and @code{set-buffer}. | 15231 | The 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 | |||
| 14345 | definition and constructs a lengths' list containing the information. | 15312 | definition and constructs a lengths' list containing the information. |
| 14346 | 15313 | ||
| 14347 | Emacs kills the buffer after working through it. This is to save | 15314 | Emacs kills the buffer after working through it. This is to save |
| 14348 | space inside of Emacs. My version of Emacs 19 contained over 300 | 15315 | space inside of Emacs. My version of GNU Emacs 19 contained over 300 |
| 14349 | source files of interest; Emacs 21 contains over 800 source files. | 15316 | source files of interest; GNU Emacs 22 contains over a thousand source |
| 14350 | Another function will apply @code{lengths-list-file} to each of the | 15317 | files. Another function will apply @code{lengths-list-file} to each |
| 14351 | files. | 15318 | of the files. |
| 14352 | 15319 | ||
| 14353 | Finally, the last expression within the @code{let} expression is the | 15320 | Finally, 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 | |||
| 14358 | place your cursor after the following expression and type @kbd{C-x | 15325 | place your cursor after the following expression and type @kbd{C-x |
| 14359 | C-e} (@code{eval-last-sexp}). | 15326 | C-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 | 15336 | 2006 Oct 29 |
| 14370 | @c but debug.el is in /usr/local/share/emacs/21.0.100/lisp/emacs-lisp/debug.el | 15337 | For Bob, as root, |
| 15338 | gunzip /usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el.gz | ||
| 15339 | In GNU Emacs 22, in *info* eval | ||
| 15340 | count-words-in-defun | ||
| 15341 | in The `count-words-in-defun' Function | ||
| 15342 | count-words-in-defun | ||
| 15343 | in `lengths-list-file' in Detail | ||
| 15344 | then 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 |
| 14374 | with GNU Emacs version 21.0.100. To change the expression, copy it to | 15350 | GNU Emacs version 22.0.100. To change the expression, copy it to |
| 14375 | the @file{*scratch*} buffer and edit it. | 15351 | the @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 |
| 14391 | The lengths' list for @file{debug.el} takes less than a second to | 15367 | The lengths' list for @file{debug.el} takes less than a second to |
| 14392 | produce and looks like this: | 15368 | produce 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 |
| 14407 | earlier one; and my new machine is much faster than the old one.) | 15383 | earlier one; and my new machine is much faster than the old one.) |
| 14408 | 15384 | ||
| 14409 | Note that the length of the last definition in the file is first in | 15385 | Note 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 | |||
| 14421 | either a @code{while} loop or recursion. | 15397 | either 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: | |||
| 14499 | name to the absolute, long, path name form of the directory in which | 15475 | name to the absolute, long, path name form of the directory in which |
| 14500 | the function is called. | 15476 | the function is called. |
| 14501 | 15477 | ||
| 14502 | @c !!! 21.0.100 lisp sources location here | 15478 | @ignore |
| 15479 | 2006 Oct 29 | ||
| 15480 | For Bob, as root, | ||
| 15481 | gunzip /usr/local/share/emacs/22.0.50/lisp/emacs-lisp/debug.el.gz | ||
| 15482 | In 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 |
| 14504 | Thus, if @code{expand-file-name} is called on @code{debug.el} when | 15488 | Thus, if @code{expand-file-name} is called on @code{debug.el} when |
| 14505 | Emacs is visiting the | 15489 | Emacs 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 |
| 14509 | debug.el | 15493 | debug.el |
| @@ -14513,9 +15497,9 @@ debug.el | |||
| 14513 | @noindent | 15497 | @noindent |
| 14514 | becomes | 15498 | becomes |
| 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 | ||
| 14521 | The only other new element of this function definition is the as yet | 15505 | The 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 | |||
| 14604 | the @file{*scratch*} buffer, edit them, and then evaluate them. | 15588 | the @file{*scratch*} buffer, edit them, and then evaluate them. |
| 14605 | 15589 | ||
| 14606 | The results are shown after the @samp{@result{}}. (These results are | 15590 | The results are shown after the @samp{@result{}}. (These results are |
| 14607 | for files from Emacs Version 21.0.100; files from other versions of | 15591 | for files from Emacs Version 22.0.100; files from other versions of |
| 14608 | Emacs may produce different results.) | 15592 | Emacs may produce different results.) |
| 14609 | 15593 | ||
| 14610 | @c !!! 21.0.100 lisp sources location here | 15594 | @ignore |
| 15595 | 2006 Oct 29 | ||
| 15596 | For Bob, as root, | ||
| 15597 | gunzip /usr/local/share/emacs/22.0.50/lisp/macros.el.gz | ||
| 15598 | In 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 | ||
| 15612 | 2006 Oct 29 | ||
| 15613 | For Bob, as root, | ||
| 15614 | gunzip /usr/local/share/emacs/22.0.50/lisp/mail/mailalias.el.gz | ||
| 15615 | In 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 | ||
| 15625 | 2006 Oct 29 | ||
| 15626 | For Bob, as root, | ||
| 15627 | gunzip /usr/local/share/emacs/22.0.50/lisp/makesum.el | ||
| 15628 | In 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 | ||
| 15638 | 2006 Oct 29 | ||
| 15639 | In 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 | |||
| 14671 | that we will need. | 15690 | that 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; |
| 14718 | it uses the @code{<} function: | 15737 | it uses the @code{<} function: |
| 14719 | 15738 | ||
| 15739 | @ignore | ||
| 15740 | 2006 Oct 29 | ||
| 15741 | In 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: | |||
| 14733 | which produces: | 15766 | which 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 | |||
| 14793 | element of the list is @code{t} for a directory, a string | 15826 | element of the list is @code{t} for a directory, a string |
| 14794 | for symbolic link (the string is the name linked to), or @code{nil}. | 15827 | for 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 | |||
| 14796 | For example, the first @samp{.el} file in the @file{lisp/} directory | 15836 | For example, the first @samp{.el} file in the @file{lisp/} directory |
| 14797 | is @file{abbrev.el}. Its name is | 15837 | is @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 |
| 14799 | directory or a symbolic link. | 15839 | directory or a symbolic link. |
| 14800 | 15840 | ||
| 14801 | @need 1000 | 15841 | @need 1000 |
| 14802 | This is how @code{directory-files-and-attributes} lists that file and | 15842 | This is how @code{directory-files-and-attributes} lists that file and |
| 14803 | its attributes: | 15843 | its 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) | ||
| 15848 | 13157 "-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" |
| 14808 | nil | 15854 | nil |
| 14809 | 1 | 15855 | 1 |
| 14810 | 1000 | 15856 | 1000 |
| 14811 | 100 | 15857 | 100 |
| 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) |
| 14817 | 11583 | 15863 | 13157 |
| 14818 | "-rw-rw-r--" | 15864 | "-rw-rw-r--" |
| 14819 | @end group | 15865 | @end group |
| 14820 | @group | 15866 | @group |
| 14821 | t | 15867 | nil |
| 14822 | 341385 | 15868 | 2971624 |
| 14823 | 776) | 15869 | 773) |
| 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 |
| 14828 | On the other hand, @file{mail/} is a directory within the @file{lisp/} | 15883 | On the other hand, @file{mail/} is a directory within the @file{lisp/} |
| 14829 | directory. The beginning of its listing looks like this: | 15884 | directory. 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" |
| 14834 | t | 15889 | t |
| 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 |
| 14841 | the 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 | 15897 | function does not list the filename, so its first element is |
| 14843 | first element is @code{directory-files-and-attributes}'s second | 15898 | @code{directory-files-and-attributes}'s second element.) |
| 14844 | element.) | ||
| 14845 | 15899 | ||
| 14846 | We will want our new function, @code{files-in-below-directory}, to | 15900 | We will want our new function, @code{files-in-below-directory}, to |
| 14847 | list the @samp{.el} files in the directory it is told to check, and in | 15901 | list the @samp{.el} files in the directory it is told to check, and in |
| @@ -14893,11 +15947,14 @@ pattern is `accumulate' | |||
| 14893 | using @code{append} as the combiner. | 15947 | using @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 |
| 14903 | Here is the function: | 15960 | Here 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 | ||
| 14958 | The @code{files-in-below-directory} @code{directory-files} function | 16016 | The @code{files-in-below-directory} @code{directory-files} function |
| 14959 | takes one argument, the name of a directory. | 16017 | takes 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 |
| 14962 | Thus, on my system, | 16020 | Thus, 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 |
| 14973 | tells me that my version 21.0.100 Lisp sources directory contains 754 | 16033 | tells me that my Lisp sources directory contains 1031 @samp{.el} |
| 14974 | @samp{.el} files. | 16034 | files. |
| 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 |
| 14977 | order. An expression to sort the list in alphabetical order looks | 16037 | order. 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 |
| 15260 | This way, we avoid an error. | 16317 | This way, we avoid an error. |
| 15261 | @iftex | 16318 | @iftex |
| 15262 | @xref{forward-paragraph, , @code{forward-paragraph}: a Goldmine of | 16319 | @noindent |
| 15263 | Functions}, 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 | ||
| 16326 | information about @code{and}.) | ||
| 15267 | @end ifinfo | 16327 | @end ifinfo |
| 15268 | 16328 | ||
| 15269 | Here is a short test of the @code{defuns-per-range} function. First, | 16329 | Here 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 | |||
| 15301 | of 200 or larger. | 16361 | of 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 | |||
| 15363 | own column-insertion function or discover whether one exists in Emacs. | 16422 | own column-insertion function or discover whether one exists in Emacs. |
| 15364 | 16423 | ||
| 15365 | To see whether there is one in Emacs, we can use the @kbd{M-x apropos} | 16424 | To see whether there is one in Emacs, we can use the @kbd{M-x apropos} |
| 15366 | command. This command is like the @kbd{C-h a} (command-apropos) | 16425 | command. This command is like the @kbd{C-h a} (@code{command-apropos}) |
| 15367 | command, except that the latter finds only those functions that are | 16426 | command, except that the latter finds only those functions that are |
| 15368 | commands. The @kbd{M-x apropos} command lists all symbols that match | 16427 | commands. The @kbd{M-x apropos} command lists all symbols that match |
| 15369 | a regular expression, including functions that are not interactive. | 16428 | a regular expression, including functions that are not interactive. |
| @@ -15374,9 +16433,11 @@ columns. Very likely, the name of the function will contain either | |||
| 15374 | the word `print' or the word `insert' or the word `column'. | 16433 | the word `print' or the word `insert' or the word `column'. |
| 15375 | Therefore, we can simply type @kbd{M-x apropos RET | 16434 | Therefore, we can simply type @kbd{M-x apropos RET |
| 15376 | print\|insert\|column RET} and look at the result. On my system, this | 16435 | print\|insert\|column RET} and look at the result. On my system, this |
| 15377 | command takes quite some time, and then produces a list of 79 | 16436 | command once too takes quite some time, and then produced a list of 79 |
| 15378 | functions and variables. Scanning down the list, the only function | 16437 | functions and variables. Now it does not take much time at all and |
| 15379 | that looks as if it might do the job is @code{insert-rectangle}. | 16438 | produces a list of 211 functions and variables. Scanning down the |
| 16439 | list, 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 |
| 15382 | Indeed, this is the function we want; its documentation says: | 16443 | Indeed, this is the function we want; its documentation says: |
| @@ -15388,6 +16449,8 @@ Insert text of RECTANGLE with upper left corner at point. | |||
| 15388 | RECTANGLE's first line is inserted at point, | 16449 | RECTANGLE's first line is inserted at point, |
| 15389 | its second line is inserted at a point vertically under point, etc. | 16450 | its second line is inserted at a point vertically under point, etc. |
| 15390 | RECTANGLE should be a list of strings. | 16451 | RECTANGLE should be a list of strings. |
| 16452 | After this command, the mark is at the upper left corner | ||
| 16453 | and 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 |
| 15407 | nil | ||
| 15408 | @end group | 16470 | @end group |
| 15409 | @end smallexample | 16471 | @end smallexample |
| 15410 | 16472 | ||
| @@ -15418,12 +16480,14 @@ column of strings. | |||
| 15418 | 16480 | ||
| 15419 | If you are reading this in Info, you can see how this works by | 16481 | If you are reading this in Info, you can see how this works by |
| 15420 | switching to another buffer, such as the @file{*scratch*} buffer, | 16482 | switching to another buffer, such as the @file{*scratch*} buffer, |
| 15421 | placing point somewhere in the buffer, typing @kbd{M-:}, | 16483 | placing point somewhere in the buffer, typing @kbd{M-:}, typing the |
| 15422 | typing the @code{insert-rectangle} expression into the minibuffer at | 16484 | @code{insert-rectangle} expression into the minibuffer at the prompt, |
| 15423 | the prompt, and then typing @key{RET}. This causes Emacs to evaluate | 16485 | and then typing @key{RET}. This causes Emacs to evaluate the |
| 15424 | the expression in the minibuffer, but to use as the value of point the | 16486 | expression in the minibuffer, but to use as the value of point the |
| 15425 | position of point in the @file{*scratch*} buffer. (@kbd{M-:} | 16487 | position of point in the @file{*scratch*} buffer. (@kbd{M-:} is the |
| 15426 | is the keybinding for @code{eval-expression}.) | 16488 | keybinding for @code{eval-expression}. Also, @code{nil} does not |
| 16489 | appear in the @file{*scratch*} buffer since the expression is | ||
| 16490 | evaluated in the minibuffer.) | ||
| 15427 | 16491 | ||
| 15428 | We find when we do this that point ends up at the end of the last | 16492 | We find when we do this that point ends up at the end of the last |
| 15429 | inserted line---that is to say, this function moves point as a | 16493 | inserted 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 | |||
| 16011 | sense, if you do not know who is going to use Emacs. Who knows what a | 17075 | sense, if you do not know who is going to use Emacs. Who knows what a |
| 16012 | person hopes to do with an unadorned file? Fundamental mode is the | 17076 | person hopes to do with an unadorned file? Fundamental mode is the |
| 16013 | right default for such a file, just as C mode is the right default for | 17077 | right default for such a file, just as C mode is the right default for |
| 16014 | editing C code. But when you do know who is going to use Emacs---you, | 17078 | editing C code. (Enough programming languages have syntaxes |
| 17079 | that enable them to share or nearly share features, so C mode is | ||
| 17080 | now provided by by CC mode, the `C Collection'.) | ||
| 17081 | |||
| 17082 | But when you do know who is going to use Emacs---you, | ||
| 16015 | yourself---then it makes sense to customize Emacs. | 17083 | yourself---then it makes sense to customize Emacs. |
| 16016 | 17084 | ||
| 16017 | For example, I seldom want Fundamental mode when I edit an | 17085 | For example, I seldom want Fundamental mode when I edit an |
| @@ -16169,8 +17237,8 @@ M-x customize | |||
| 16169 | @noindent | 17237 | @noindent |
| 16170 | and find that the group for editing files of data is called `data'. | 17238 | and find that the group for editing files of data is called `data'. |
| 16171 | Enter that group. Text Mode Hook is the first member. You can click | 17239 | Enter that group. Text Mode Hook is the first member. You can click |
| 16172 | on its various options to set the values. After you click on the | 17240 | on its various options, such as @code{turn-on-auto-fill}, to set the |
| 16173 | button to | 17241 | values. After you click on the button to |
| 16174 | 17242 | ||
| 16175 | @smallexample | 17243 | @smallexample |
| 16176 | Save for Future Sessions | 17244 | Save 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 | 17265 | It comes on automatically. | |
| 16197 | In spite of the warning, you certainly may edit, cut, and paste the | 17266 | ) |
| 16198 | expression! I do all time. The purpose of the warning is to scare | ||
| 16199 | those who do not know what they are doing, so they do not | ||
| 16200 | inadvertently generate an error. | ||
| 16201 | 17267 | ||
| 16202 | The @code{custom-set-variables} function works somewhat differently | 17268 | The @code{custom-set-variables} function works somewhat differently |
| 16203 | than a @code{setq}. While I have never learned the differences, I do | 17269 | than a @code{setq}. While I have never learned the differences, I |
| 16204 | modify the @code{custom-set-variables} expressions in my @file{.emacs} | 17270 | modify the @code{custom-set-variables} expressions in my @file{.emacs} |
| 16205 | file by hand: I make the changes in what appears to me to be a | 17271 | file by hand: I make the changes in what appears to me to be a |
| 16206 | reasonable manner and have not had any problems. Others prefer to use | 17272 | reasonable 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 | |||
| 16221 | message that says | 17287 | message that says |
| 16222 | 17288 | ||
| 16223 | @smallexample | 17289 | @smallexample |
| 16224 | this option has been changed outside the customize buffer. | 17290 | CHANGED 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 | |||
| 16404 | else in Emacs. | 17469 | else in Emacs. |
| 16405 | 17470 | ||
| 16406 | @need 800 | 17471 | @need 800 |
| 16407 | Here are the next two lines: | 17472 | Here 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 |
| 16417 | In these two lines, the @code{add-hook} command first adds | 17481 | In 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 | ||
| 16420 | variable. | ||
| 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 |
| 16423 | it!, turns on Auto Fill mode. @code{text-mode-hook-identify} is a | 17485 | it!, turns on Auto Fill mode. |
| 16424 | function that tells @code{toggle-text-mode-auto-fill} which buffers | ||
| 16425 | are in Text mode. | ||
| 16426 | 17486 | ||
| 16427 | Every time Emacs turns on Text mode, Emacs runs the commands `hooked' | 17487 | Every time Emacs turns on Text mode, Emacs runs the commands `hooked' |
| 16428 | onto Text mode. So every time Emacs turns on Text mode, Emacs also | 17488 | onto Text mode. So every time Emacs turns on Text mode, Emacs also |
| 16429 | turns on Auto Fill mode. | 17489 | turns on Auto Fill mode. |
| 16430 | 17490 | ||
| 16431 | In brief, the first line causes Emacs to enter Text mode when you edit | 17491 | In brief, the first line causes Emacs to enter Text mode when you edit a |
| 16432 | a file, unless the file name extension, first non-blank line, or local | 17492 | file, unless the file name extension, a first non-blank line, or local |
| 16433 | variables tell Emacs otherwise. | 17493 | variables to tell Emacs otherwise. |
| 16434 | 17494 | ||
| 16435 | Text mode among other actions, sets the syntax table to work | 17495 | Text mode among other actions, sets the syntax table to work |
| 16436 | conveniently for writers. In Text mode, Emacs considers an apostrophe | 17496 | conveniently 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 |
| 16440 | the @samp{t} of @samp{it's}. | 17500 | the @samp{t} of @samp{it's}. |
| 16441 | 17501 | ||
| 16442 | The second and third lines causes Emacs to turn on Auto Fill mode when | 17502 | The second line causes Emacs to turn on Auto Fill mode when it turns |
| 16443 | it turns on Text mode. In Auto Fill mode, Emacs automatically breaks | 17503 | on Text mode. In Auto Fill mode, Emacs automatically breaks a line |
| 16444 | a line that is too wide and brings the excessively wide part of the | 17504 | that is too wide and brings the excessively wide part of the line down |
| 16445 | line down to the next line. Emacs breaks lines between words, not | 17505 | to the next line. Emacs breaks lines between words, not within them. |
| 16446 | within them. | ||
| 16447 | 17506 | ||
| 16448 | When Auto Fill mode is turned off, lines continue to the right as you | 17507 | When Auto Fill mode is turned off, lines continue to the right as you |
| 16449 | type them. Depending on how you set the value of | 17508 | type 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. |
| 16787 | This is an interface to the function `load'." | 17846 | This 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 | |||
| 16822 | load that function's file with a @code{load} expression in your | 17884 | load that function's file with a @code{load} expression in your |
| 16823 | @file{.emacs} file. | 17885 | @file{.emacs} file. |
| 16824 | 17886 | ||
| 16825 | In my @file{.emacs} file for Emacs version 21, I load 12 libraries | 17887 | In my @file{.emacs} file for Emacs version 22, I load 14 libraries |
| 16826 | that contain functions that would otherwise be autoloaded. (Actually, | 17888 | that contain functions that would otherwise be autoloaded. (Actually, |
| 16827 | it would have been better to include these files in my `dumped' Emacs | 17889 | it would have been better to include these files in my `dumped' Emacs, |
| 16828 | when I built it, but I forgot. @xref{Building Emacs, , Building | 17890 | but I forgot. @xref{Building Emacs, , Building Emacs, elisp, The GNU |
| 16829 | Emacs, elisp, The GNU Emacs Lisp Reference Manual}, and the @file{INSTALL} | 17891 | Emacs Lisp Reference Manual}, and the @file{INSTALL} file for more |
| 16830 | file for more about dumping.) | 17892 | about dumping.) |
| 16831 | 17893 | ||
| 16832 | You may also want to include autoloaded expressions in your @file{.emacs} | 17894 | You may also want to include autoloaded expressions in your @file{.emacs} |
| 16833 | file. @code{autoload} is a built-in function that takes up to five | 17895 | file. @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 |
| 16919 | If you run two versions of GNU Emacs, such as versions 20 and 21, and | 17981 | If you run two versions of GNU Emacs, such as versions 21 and 22, and |
| 16920 | use one @file{.emacs} file, you can select which code to evaluate with | 17982 | use one @file{.emacs} file, you can select which code to evaluate with |
| 16921 | the following conditional: | 17983 | the 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 |
| 16942 | emacs -q --no-site-file -eval '(blink-cursor-mode nil)' | 18004 | emacs -q --no-site-file -eval '(blink-cursor-mode nil)' |
| 18005 | |||
| 18006 | @exdent Or nowadays, using an even more sophisticated set of options, | ||
| 18007 | |||
| 18008 | emacs -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 | ||
| 17057 | In any event, since it is not part of Emacs, I set the root color of | 18123 | In any event, since it is not part of Emacs, I set the root color of |
| 17058 | my X window in my @file{~/.xinitrc} file, like this@footnote{I | 18124 | my X window in my @file{~/.xinitrc} file, like this@footnote{I also |
| 17059 | occasionally run more modern window managers, such as Sawfish with | 18125 | run more modern window managers, such as Enlightenment, Gnome, or KDE; |
| 17060 | GNOME, Enlightenment, SCWM, or KDE; in those cases, I often specify an | 18126 | in those cases, I often specify an image rather than a plain color.}: |
| 17061 | image rather than a plain color.}: | ||
| 17062 | 18127 | ||
| 17063 | @smallexample | 18128 | @smallexample |
| 17064 | @group | ||
| 17065 | # I use TWM for window manager. | ||
| 17066 | xsetroot -solid Navy -fg white & | 18129 | xsetroot -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 |
| 17078 | Set the shape and color of the mouse cursor: | 18140 | Set 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 |
| 18165 | Or you can set the values of a variety of features in an alist, like | ||
| 18166 | this: | ||
| 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 | ||
| 17102 | Convert @kbd{@key{CTL}-h} into @key{DEL} and @key{DEL} | 18191 | Convert @kbd{@key{CTL}-h} into @key{DEL} and @key{DEL} |
| 17103 | into @kbd{@key{CTL}-h}.@* | 18192 | into @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 | ||
| 18216 | or 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 | 18230 | item Automatically uncompress compressed files when visiting them |
| 18231 | |||
| 18232 | smallexample | ||
| 17138 | (load "uncompress") | 18233 | (load "uncompress") |
| 17139 | @end smallexample | 18234 | end 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@* |
| 17142 | This avoids problems with symbolic links. | 18239 | This avoids problems with symbolic links. |
| @@ -17305,8 +18402,8 @@ the old days, it would have been specified simply as @code{"-"}. But | |||
| 17305 | nowadays, Emacs can add properties to a string, such as highlighting | 18402 | nowadays, Emacs can add properties to a string, such as highlighting |
| 17306 | or, as in this case, a help feature. If you place your mouse cursor | 18403 | or, as in this case, a help feature. If you place your mouse cursor |
| 17307 | over the hyphen, some help information appears (By default, you must | 18404 | over the hyphen, some help information appears (By default, you must |
| 17308 | wait one second before the information appears. You can change that | 18405 | wait seven-tenths of a second before the information appears. You can |
| 17309 | timing by changing the value of @code{tooltip-delay}.) | 18406 | change that timing by changing the value of @code{tooltip-delay}.) |
| 17310 | 18407 | ||
| 17311 | @need 1000 | 18408 | @need 1000 |
| 17312 | The new string format has a special syntax: | 18409 | The new string format has a special syntax: |
| @@ -17344,7 +18441,7 @@ this number. (Buffer names can and often should be longer than 12 | |||
| 17344 | characters; this length works well in a typical 80 column wide | 18441 | characters; this length works well in a typical 80 column wide |
| 17345 | window.) | 18442 | window.) |
| 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 |
| 17348 | evaluate the following form and use the result as a string to display. | 18445 | evaluate the following form and use the result as a string to display. |
| 17349 | In this case, the expression displays the first component of the full | 18446 | In this case, the expression displays the first component of the full |
| 17350 | system name. The end of the first component is a @samp{.} (`period'), | 18447 | system 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}. | |||
| 17399 | In this chapter, I will walk through a short example of each. | 18496 | In 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 | |||
| 17479 | tell you what you need to know to correct the definition. The | 18576 | tell you what you need to know to correct the definition. The |
| 17480 | function @code{1=} is `void'. | 18577 | function @code{1=} is `void'. |
| 17481 | 18578 | ||
| 18579 | @ignore | ||
| 17482 | @need 800 | 18580 | @need 800 |
| 17483 | In GNU Emacs 20 and before, you will see: | 18581 | In 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 |
| 17490 | which has the same meaning as the @file{*Backtrace*} buffer line in | 18588 | which has the same meaning as the @file{*Backtrace*} buffer line in |
| 17491 | version 21. | 18589 | version 21. |
| 18590 | @end ignore | ||
| 17492 | 18591 | ||
| 17493 | However, suppose you are not quite certain what is going on? | 18592 | However, suppose you are not quite certain what is going on? |
| 17494 | You can read the complete backtrace. | 18593 | You can read the complete backtrace. |
| 17495 | 18594 | ||
| 17496 | In this case, you need to run GNU Emacs 21, which automatically starts | 18595 | In this case, you need to run GNU Emacs 22, which automatically starts |
| 17497 | the debugger that puts you in the @file{*Backtrace*} buffer; or else, | 18596 | the debugger that puts you in the @file{*Backtrace*} buffer; or else, |
| 17498 | you need to start the debugger manually as described below. | 18597 | you 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 | ||
| 17538 | GNU Emacs 21 starts the debugger automatically when your function has | 18637 | GNU Emacs 22 starts the debugger automatically when your function has |
| 17539 | an error. GNU Emacs version 20 and before did not; it simply | 18638 | an error. |
| 18639 | |||
| 18640 | @ignore | ||
| 18641 | GNU Emacs version 20 and before did not; it simply | ||
| 17540 | presented you with an error message. You had to start the debugger | 18642 | presented you with an error message. You had to start the debugger |
| 17541 | manually. | 18643 | manually. |
| 18644 | @end ignore | ||
| 17542 | 18645 | ||
| 17543 | You can start the debugger manually for all versions of Emacs; the | 18646 | Incidently, you can start the debugger manually for all versions of |
| 17544 | advantage is that the debugger runs even if you do not have a bug in | 18647 | Emacs; the advantage is that the debugger runs even if you do not have |
| 17545 | your code. Sometimes your code will be free of bugs! | 18648 | a bug in your code. Sometimes your code will be free of bugs! |
| 17546 | 18649 | ||
| 17547 | You can enter the debugger when you call the function by calling | 18650 | You 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 | |||
| 17796 | move point past @code{number}, you will see the following: | 18899 | move point past @code{number}, you will see the following: |
| 17797 | 18900 | ||
| 17798 | @smallexample | 18901 | @smallexample |
| 17799 | Result: 3 = C-c | 18902 | Result: 3 (#o3, #x3, ?\C-c) |
| 17800 | @end smallexample | 18903 | @end smallexample |
| 17801 | 18904 | ||
| 17802 | @noindent | 18905 | @noindent |
| 17803 | This means the value of @code{number} is 3, which is @sc{ascii} | 18906 | This 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 | 18907 | hexadecimal three, and @sc{ascii} `control-c' (the third letter of the |
| 17805 | know this information). | 18908 | alphabet, in case you need to know this information). |
| 17806 | 18909 | ||
| 17807 | You can continue moving through the code until you reach the line with | 18910 | You can continue moving through the code until you reach the line with |
| 17808 | the error. Before evaluation, that line looks like this: | 18911 | the 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 |
| 17875 | Move point to some spot further down function and then type the | 18978 | Move 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 | |||
| 17922 | on-line and as a typeset, printed book.) | 19025 | on-line and as a typeset, printed book.) |
| 17923 | 19026 | ||
| 17924 | Go to the other on-line help that is part of GNU Emacs: the on-line | 19027 | Go to the other on-line help that is part of GNU Emacs: the on-line |
| 17925 | documentation for all functions, and @code{find-tags}, the program | 19028 | documentation for all functions and variables, and @code{find-tags}, |
| 17926 | that takes you to sources. | 19029 | the program that takes you to sources. |
| 17927 | 19030 | ||
| 17928 | Here is an example of how I explore the sources. Because of its name, | 19031 | Here 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} | |||
| 17932 | function, for example, looks complicated. | 19035 | function, for example, looks complicated. |
| 17933 | 19036 | ||
| 17934 | You may want to walk through this function slowly, as we did with the | 19037 | You 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}.) | 19040 | function and look at another, such as @code{split-line}. You don't |
| 17938 | @end ifnottex | 19041 | need 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}}.) | 19043 | 102 words and symbols. |
| 17941 | @end iftex | 19044 | |
| 17942 | Or you may want to skip that function and look at another, such as | 19045 | Even though it is short, @code{split-line} contains expressions |
| 17943 | @code{split-line}. You don't need to read all the functions. | ||
| 17944 | According to @code{count-words-in-defun}, the @code{split-line} | ||
| 17945 | function contains 27 words and symbols. | ||
| 17946 | |||
| 17947 | Even though it is short, @code{split-line} contains four expressions | ||
| 17948 | we have not studied: @code{skip-chars-forward}, @code{indent-to}, | 19046 | we 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 | ||
| 17951 | Consider the @code{skip-chars-forward} function. (It is part of the | 19049 | Consider the @code{skip-chars-forward} function. (It is part of the |
| 17952 | function definition for @code{back-to-indentation}, which is shown in | 19050 | function definition for @code{back-to-indentation}, which is shown in |
| @@ -17974,25 +19072,17 @@ customize the @code{interactive} expression without using the standard | |||
| 17974 | character codes; and it shows how to create a temporary buffer. | 19072 | character 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; |
| 17977 | it is a `built-in' function. @code{help-follow} only provides you | 19075 | it is a `built-in' function. @code{help-follow} takes you to its |
| 17978 | with the documentation of a built-in function; it does not take you to | 19076 | source as does @code{find-tag}, when properly set up.) |
| 17979 | the source. But @code{find-tag} will take you to the source, if | ||
| 17980 | properly set up.) | ||
| 17981 | 19077 | ||
| 17982 | You can look at a function's source using @code{find-tag}, which is | 19078 | You can look at a function's source using @code{find-tag}, which is |
| 17983 | bound to @kbd{M-.} Finally, you can find out what the Reference | 19079 | bound to @kbd{M-.} Finally, you can find out what the Reference |
| 17984 | Manual has to say by visiting the manual in Info, and typing @kbd{i} | 19080 | Manual 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 | 19082 | function in the index to a printed copy of the manual. |
| 17987 | manual. | 19083 | |
| 17988 | 19084 | Similarly, you can find out what is meant by | |
| 17989 | Similarly, you can find out what is meant by @samp{?\n}. You can try | 19085 | @code{insert-and-inherit}. |
| 17990 | using @code{Info-index} with @samp{?\n}. It turns out that this | ||
| 17991 | action 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 | ||
| 17993 | relevant section of the manual. (@xref{Character Type, , Character | ||
| 17994 | Type, elisp, The GNU Emacs Lisp Reference Manual}. @samp{?\n} stands | ||
| 17995 | for the newline character.) | ||
| 17996 | 19086 | ||
| 17997 | Other interesting source files include @file{paragraphs.el}, | 19087 | Other 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 | |||
| 18115 | You can substitute the other regular expressions shown above in the | 19205 | You can substitute the other regular expressions shown above in the |
| 18116 | function definition and try each of them on this list. | 19206 | function 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 | ||
| 19218 | as of GNU Emacs 22, this no longer seems to be the case | ||
| 19219 | with yank yank-pop | ||
| 19220 | |||
| 19221 | guts of rotate-yank-pointer moved to current-kill | ||
| 19222 | in 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 | |||
| 19227 | critical 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 | |||
| 19272 | rotate-yank-pointer has | ||
| 19273 | |||
| 19274 | reformatted: | ||
| 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 | |||
| 19283 | originally | ||
| 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 | ||
| 19298 | 1992-05-21 Jim Blandy (jimb@pogo.cs.oberlin.edu) | ||
| 19299 | simple.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 | ||
| 18124 | The kill ring is a list that is transformed into a ring by the | 19313 | The kill ring is a list that is transformed into a ring by the |
| 18125 | workings of the @code{rotate-yank-pointer} function. The @code{yank} | 19314 | workings of the @code{current-kill} function. The @code{yank} and |
| 18126 | and @code{yank-pop} commands use the @code{rotate-yank-pointer} | 19315 | @code{yank-pop} commands use the @code{current-kill} function. |
| 18127 | function. This appendix describes the @code{rotate-yank-pointer} | 19316 | |
| 18128 | function as well as both the @code{yank} and the @code{yank-pop} | 19317 | This appendix describes the @code{current-kill} function as well as |
| 18129 | commands. | 19318 | both the @code{yank} and the @code{yank-pop} commands, but first, |
| 19319 | consider the workings of the kill ring. | ||
| 19320 | |||
| 19321 | @need 1250 | ||
| 19322 | The kill ring has a default maximum length of sixty items; this number | ||
| 19323 | is too large for an explanation. Instead, set it to four. Please | ||
| 19324 | evaluate 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 | ||
| 19334 | Then, please copy each line of the following indented example into the | ||
| 19335 | kill ring. You may kill each line with @kbd{C-k} or mark it and copy | ||
| 19336 | it with @kbd{M-w}. | ||
| 19337 | |||
| 19338 | @noindent | ||
| 19339 | (In a read-only buffer, such as the @file{*info*} buffer, the kill | ||
| 19340 | command, @kbd{C-k} (@code{kill-line}), will not remove the text, | ||
| 19341 | merely copy it to the kill ring. However, your machine may beep at | ||
| 19342 | you. Alternatively, for silence, you may copy the region of each line | ||
| 19343 | with the @kbd{M-w} (@code{kill-ring-save}) command. You must mark | ||
| 19344 | each line for this command to succeed, but it does not matter at which | ||
| 19345 | end you put point or mark.) | ||
| 19346 | |||
| 19347 | @need 1250 | ||
| 19348 | @noindent | ||
| 19349 | Please invoke the calls in order, so that five elements attempt to | ||
| 19350 | fill the kill ring: | ||
| 19351 | |||
| 19352 | @smallexample | ||
| 19353 | @group | ||
| 19354 | first some text | ||
| 19355 | second piece of text | ||
| 19356 | third line | ||
| 19357 | fourth line of text | ||
| 19358 | fifth bit of text | ||
| 19359 | @end group | ||
| 19360 | @end smallexample | ||
| 19361 | |||
| 19362 | @need 1250 | ||
| 19363 | @noindent | ||
| 19364 | Then find the value of @code{kill-ring} by evaluating | ||
| 19365 | |||
| 19366 | @smallexample | ||
| 19367 | kill-ring | ||
| 19368 | @end smallexample | ||
| 19369 | |||
| 19370 | @need 800 | ||
| 19371 | @noindent | ||
| 19372 | It 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 | ||
| 19382 | The first element, @samp{first some text}, was dropped. | ||
| 19383 | |||
| 19384 | @need 1250 | ||
| 19385 | To 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 | ||
| 18143 | The @code{rotate-yank-pointer} function changes the element in the kill | 19403 | The @code{current-kill} function changes the element in the kill ring |
| 18144 | ring to which @code{kill-ring-yank-pointer} points. For example, it can | 19404 | to which @code{kill-ring-yank-pointer} points. (Also, the |
| 18145 | change @code{kill-ring-yank-pointer} from pointing to the second | 19405 | @code{kill-new} function sets @code{kill-ring-yank-pointer} to point |
| 18146 | element to point to the third element. | 19406 | to the latest element of the the kill ring.) |
| 18147 | 19407 | ||
| 18148 | @need 800 | 19408 | @need 1500 |
| 18149 | Here is the code for @code{rotate-yank-pointer}: | 19409 | The @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") | 19416 | If N is zero, `interprogram-paste-function' is set, and calling it |
| 18156 | (let ((length (length kill-ring))) | 19417 | returns a string, then that string is added to the front of the |
| 19418 | kill ring and returned as the latest kill. | ||
| 18157 | @end group | 19419 | @end group |
| 18158 | @group | 19420 | @group |
| 18159 | (if (zerop length) | 19421 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the |
| 18160 | ;; @r{then-part} | 19422 | yanking 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 | |||
| 19449 | In addition, the @code{kill-new} function sets | ||
| 19450 | @code{kill-ring-yank-pointer} to the latest element of the the kill | ||
| 19451 | ring. And indirectly so does @code{kill-append}, since it calls | ||
| 19452 | @code{kill-new}. In addition, @code{kill-region} and @code{kill-line} | ||
| 19453 | call the @code{kill-new} function. | ||
| 19454 | |||
| 19455 | @need 1500 | ||
| 19456 | Here 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 | ||
| 18185 | The @code{rotate-yank-pointer} function looks complex, but as usual, | 19472 | The @code{current-kill} function looks complex, but as usual, it can |
| 18186 | it can be understood by taking it apart piece by piece. First look at | 19473 | be understood by taking it apart piece by piece. First look at it in |
| 18187 | it in skeletal form: | 19474 | skeletal 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 | ||
| 18199 | This function takes one argument, called @code{arg}. It has a brief | 19485 | This function takes two arguments, one of which is optional. It has a |
| 18200 | documentation string; and it is interactive with a small @samp{p}, which | 19486 | documentation string. It is @emph{not} interactive. |
| 18201 | means that the argument must be a processed prefix passed to the | ||
| 18202 | function as a number. | ||
| 18203 | 19487 | ||
| 18204 | The body of the function definition is a @code{let} expression, which | 19488 | The body of the function definition is a @code{let} expression, which |
| 18205 | itself has a body as well as a @var{varlist}. | 19489 | itself has a body as well as a @var{varlist}. |
| 18206 | 19490 | ||
| 18207 | The @code{let} expression declares a variable that will be only usable | 19491 | The @code{let} expression declares a variable that will be only usable |
| 18208 | within the bounds of this function. This variable is called | 19492 | within 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 |
| 18210 | items in the kill ring. This is done by using the function called | 19494 | is not for copying within this instance of GNU Emacs. Most window |
| 18211 | @code{length}. (Note that this function has the same name as the | 19495 | systems provide a facility for interprogram pasting. Sadly, that |
| 18212 | variable called @code{length}; but one use of the word is to name the | 19496 | facility usually provides only for the lasted element. Most windowing |
| 18213 | function and the other is to name the variable. The two are quite | 19497 | systems have not adopted a ring of many possiblities, even though |
| 18214 | distinct. Similarly, an English speaker will distinguish between the | 19498 | Emacs has provided it for decades. |
| 18215 | meanings of the word @samp{ship} when he says: "I must ship this package | ||
| 18216 | immediately." and "I must get aboard the ship immediately.") | ||
| 18217 | |||
| 18218 | The function @code{length} tells the number of items there are in a list, | ||
| 18219 | so @code{(length kill-ring)} returns the number of items there are in the | ||
| 18220 | kill ring. | ||
| 18221 | 19499 | ||
| 18222 | @node rotate-yk-ptr body, , Understanding rotate-yk-ptr, rotate-yank-pointer | 19500 | The @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 | ||
| 19504 | Let us consider the `if not' or else-part of the @code{current-kill} | ||
| 19505 | function. (The then-part uses the the @code{kill-new} function, which | ||
| 19506 | we have already described. (@xref{kill-new function, , The | ||
| 19507 | @code{kill-new} function}.) | ||
| 18225 | 19508 | ||
| 18226 | The body of @code{rotate-yank-pointer} is a @code{let} expression and | 19509 | @smallexample |
| 18227 | the 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 | ||
| 18229 | The purpose of the @code{if} expression is to find out whether there is | 19522 | @noindent |
| 18230 | anything in the kill ring. If the kill ring is empty, the @code{error} | 19523 | The code first checks whether the kill ring has content; otherwise it |
| 18231 | function stops evaluation of the function and prints a message in the | 19524 | signals an error. |
| 18232 | echo area. On the other hand, if the kill ring has something in it, the | ||
| 18233 | work of the function is done. | ||
| 18234 | 19525 | ||
| 18235 | Here is the if-part and then-part of the @code{if} expression: | 19526 | @need 1000 |
| 19527 | Note 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 |
| 18248 | If there is not anything in the kill ring, its length must be zero and | 19540 | If there is not anything in the kill ring, its length must be zero and |
| 18249 | an error message sent to the user: @samp{Kill ring is empty}. The | 19541 | an 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 |
| 18251 | if the value it is testing is zero. When @code{zerop} tests true, the | 19543 | simpler. But an @code{if} expression reminds us what goes on. |
| 18252 | then-part of the @code{if} is evaluated. The then-part is a list | 19544 | |
| 18253 | starting with the function @code{error}, which is a function that is | 19545 | This @code{if} expression uses the function @code{zerop} which returns |
| 18254 | similar to the @code{message} function (@pxref{message}), in that it | 19546 | true if the value it is testing is zero. When @code{zerop} tests |
| 18255 | prints a one-line message in the echo area. However, in addition to | 19547 | true, the then-part of the @code{if} is evaluated. The then-part is a |
| 18256 | printing a message, @code{error} also stops evaluation of the function | 19548 | list starting with the function @code{error}, which is a function that |
| 18257 | within which it is embedded. This means that the rest of the function | 19549 | is similar to the @code{message} function |
| 18258 | will not be evaluated if the length of the kill ring is zero. | 19550 | (@pxref{message, , The @code{message} Function}), in that |
| 19551 | it prints a one-line message in the echo area. However, in addition | ||
| 19552 | to printing a message, @code{error} also stops evaluation of the | ||
| 19553 | function within which it is embedded. This means that the rest of the | ||
| 19554 | function will not be evaluated if the length of the kill ring is zero. | ||
| 19555 | |||
| 19556 | Then the @code{current-kill} function selects the element to return. | ||
| 19557 | The selection depends on the number of places that @code{current-kill} | ||
| 19558 | rotates and on where @code{kill-ring-yank-pointer} points. | ||
| 19559 | |||
| 19560 | Next, either the optional @code{do-not-move} argument is true or the | ||
| 19561 | current value of @code{kill-ring-yank-pointer} is set to point to the | ||
| 19562 | list, 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 | 19576 | In my opinion, it is slightly misleading, at least to humans, to use |
| 18274 | the term `error' as the name of the @code{error} function. A better | 19577 | the term `error' as the name of the @code{error} function. A better |
| 18275 | term would be `cancel'. Strictly speaking, of course, you cannot | 19578 | term would be `cancel'. Strictly speaking, of course, you cannot |
| 18276 | point to, much less rotate a pointer to a list that has no length, so | 19579 | point 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 | |||
| 18285 | that a human who is acting virtuously, by exploring his or her | 19588 | that a human who is acting virtuously, by exploring his or her |
| 18286 | environment, is making an error. This is bad. Even though the computer | 19589 | environment, is making an error. This is bad. Even though the computer |
| 18287 | takes the same steps as it does when there is an `error', a term such as | 19590 | takes 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 | |||
| 18293 | The else-part of the @code{if} expression is dedicated to setting the | ||
| 18294 | value of @code{kill-ring-yank-pointer} when the kill ring has something | ||
| 18295 | in 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 | |||
| 18308 | This needs some examination. Clearly, @code{kill-ring-yank-pointer} | ||
| 18309 | is 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 | |||
| 18313 | Before looking at the details of the code let's first consider the | ||
| 18314 | purpose of the @code{rotate-yank-pointer} function. | ||
| 18315 | |||
| 18316 | The @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 | ||
| 18319 | of a list, a call to @code{rotate-yank-pointer} causes it to point to | ||
| 18320 | the second element; and if @code{kill-ring-yank-pointer} points to the | ||
| 18321 | second element, a call to @code{rotate-yank-pointer} causes it to | ||
| 18322 | point to the third element. (And if @code{rotate-yank-pointer} is | ||
| 18323 | given an argument greater than 1, it jumps the pointer that many | ||
| 18324 | elements.) | ||
| 18325 | |||
| 18326 | The @code{rotate-yank-pointer} function uses @code{setq} to reset what | ||
| 18327 | the @code{kill-ring-yank-pointer} points to. If | ||
| 18328 | @code{kill-ring-yank-pointer} points to the first element of the kill | ||
| 18329 | ring, then, in the simplest case, the @code{rotate-yank-pointer} | ||
| 18330 | function must cause it to point to the second element. Put another | ||
| 18331 | way, @code{kill-ring-yank-pointer} must be reset to have a value equal | ||
| 18332 | to the @sc{cdr} of the kill ring. | ||
| 18333 | 19592 | ||
| 18334 | @need 1250 | 19593 | @node Determining the Element , , Digression concerning error, Understanding current-kill |
| 18335 | That 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 | 19598 | Among other actions, the else-part of the @code{if} expression sets |
| 18343 | ("some text" "a different piece of text" "yet more text")) | 19599 | the 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 | 19601 | the value of @code{do-not-move} is @code{nil}. |
| 18346 | 19602 | ||
| 18347 | @need 800 | 19603 | @need 800 |
| 18348 | @noindent | 19604 | The code looks like this: |
| 18349 | the 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 | ||
| 18357 | As a result, the @code{kill-ring-yank-pointer} will look like this: | ||
| 18358 | 19605 | ||
| 18359 | @smallexample | 19606 | @smallexample |
| 18360 | @group | 19607 | @group |
| 18361 | kill-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 | ||
| 18366 | The actual @code{setq} expression uses the @code{nthcdr} function to do | 19614 | This needs some examination. Unless it is not supposed to move the |
| 18367 | the job. | 19615 | pointer, the @code{current-kill} function changes where |
| 19616 | @code{kill-ring-yank-pointer} points. | ||
| 19617 | That is what the | ||
| 19618 | @w{@code{(setq kill-ring-yank-pointer ARGth-kill-element))}} | ||
| 19619 | expression does. Also, clearly, @code{ARGth-kill-element} is being | ||
| 19620 | set 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 | ||
| 18369 | As we have seen before (@pxref{nthcdr}), the @code{nthcdr} function | 19624 | As we have seen before (@pxref{nthcdr}), the @code{nthcdr} function |
| 18370 | works by repeatedly taking the @sc{cdr} of a list---it takes the | 19625 | works 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 | ||
| 18384 | In the @code{rotate-yank-pointer} function, however, the first | 19639 | However, the @code{nthcdr} expression is more complicated. It uses |
| 18385 | argument to @code{nthcdr} is a rather complex looking expression with | 19640 | the @code{mod} function to determine which @sc{cdr} to select. |
| 18386 | lots of arithmetic inside of it: | ||
| 18387 | 19641 | ||
| 18388 | @smallexample | 19642 | (You will remember to look at inner functions first; indeed, we will |
| 18389 | @group | 19643 | have 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 | |||
| 18397 | As usual, we need to look at the most deeply embedded expression first | ||
| 18398 | and then work our way towards the light. | ||
| 18399 | 19644 | ||
| 18400 | The most deeply embedded expression is @code{(length | 19645 | The @code{mod} function returns the value of its first argument modulo |
| 18401 | kill-ring-yank-pointer)}. This finds the length of the current value of | 19646 | the second; that is to say, it returns the remainder after dividing |
| 18402 | the @code{kill-ring-yank-pointer}. (Remember that the | 19647 | the 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 | 19648 | sign as the second argument. |
| 18404 | list.) | ||
| 18405 | 19649 | ||
| 18406 | @need 800 | 19650 | @need 800 |
| 18407 | The measurement of the length is inside the expression: | 19651 | Thus, |
| 18408 | |||
| 18409 | @smallexample | ||
| 18410 | (- length (length kill-ring-yank-pointer)) | ||
| 18411 | @end smallexample | ||
| 18412 | |||
| 18413 | @noindent | ||
| 18414 | In this expression, the first @code{length} is the variable that was | ||
| 18415 | assigned the length of the kill ring in the @code{let} statement at the | ||
| 18416 | beginning of the function. (One might think this function would be | ||
| 18417 | clearer if the variable @code{length} were named | ||
| 18418 | @code{length-of-kill-ring} instead; but if you look at the text of the | ||
| 18419 | whole function, you will see that it is so short that naming this | ||
| 18420 | variable @code{length} is not a bother, unless you are pulling the | ||
| 18421 | function apart into very tiny pieces as we are doing here.) | ||
| 18422 | |||
| 18423 | So the line @code{(- length (length kill-ring-yank-pointer))} tells the | ||
| 18424 | difference between the length of the kill ring and the length of the list | ||
| 18425 | whose name is @code{kill-ring-yank-pointer}. | ||
| 18426 | |||
| 18427 | To see how all this fits into the @code{rotate-yank-pointer} | ||
| 18428 | function, let's begin by analyzing the case where | ||
| 18429 | @code{kill-ring-yank-pointer} points to the first element of the kill | ||
| 18430 | ring, just as @code{kill-ring} does, and see what happens when | ||
| 18431 | @code{rotate-yank-pointer} is called with an argument of 1. | ||
| 18432 | |||
| 18433 | The variable @code{length} and the value of the expression | ||
| 18434 | @code{(length kill-ring-yank-pointer)} will be the same since the | ||
| 18435 | variable @code{length} is the length of the kill ring and the | ||
| 18436 | @code{kill-ring-yank-pointer} is pointing to the whole kill ring. | ||
| 18437 | Consequently, the value of | ||
| 18438 | |||
| 18439 | @smallexample | ||
| 18440 | (- length (length kill-ring-yank-pointer)) | ||
| 18441 | @end smallexample | ||
| 18442 | |||
| 18443 | @noindent | ||
| 18444 | will be zero. Since the value of @code{arg} will be 1, this will mean | ||
| 18445 | that the value of the whole expression | ||
| 18446 | |||
| 18447 | @smallexample | ||
| 18448 | (+ arg (- length (length kill-ring-yank-pointer))) | ||
| 18449 | @end smallexample | ||
| 18450 | |||
| 18451 | @noindent | ||
| 18452 | will be 1. | ||
| 18453 | |||
| 18454 | @need 1200 | ||
| 18455 | Consequently, the argument to @code{nthcdr} will be found as the result of | ||
| 18456 | the 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 | |||
| 18465 | To understand @code{(% 1 length)}, we need to understand @code{%}. | ||
| 18466 | According to its documentation (which I just found by typing @kbd{C-h | ||
| 18467 | f @kbd{%} @key{RET}}), the @code{%} function returns the remainder of | ||
| 18468 | its first argument divided by its second argument. For example, the | ||
| 18469 | remainder of 5 divided by 2 is 1. (2 goes into 5 twice with a | ||
| 18470 | remainder of 1.) | ||
| 18471 | |||
| 18472 | What surprises people who don't often do arithmetic is that a smaller | ||
| 18473 | number can be divided by a larger number and have a remainder. In the | ||
| 18474 | example we just used, 5 was divided by 2. We can reverse that and ask, | ||
| 18475 | what is the result of dividing 2 by 5? If you can use fractions, the | ||
| 18476 | answer is obviously 2/5 or .4; but if, as here, you can only use whole | ||
| 18477 | numbers, the result has to be something different. Clearly, 5 can go into | ||
| 18478 | 2 zero times, but what of the remainder? To see what the answer is, | ||
| 18479 | consider a case that has to be familiar from childhood: | ||
| 18480 | |||
| 18481 | @itemize @bullet | ||
| 18482 | @item | ||
| 18483 | 5 divided by 5 is 1 with a remainder of 0; | ||
| 18484 | |||
| 18485 | @item | ||
| 18486 | 6 divided by 5 is 1 with a remainder of 1; | ||
| 18487 | |||
| 18488 | @item | ||
| 18489 | 7 divided by 5 is 1 with a remainder of 2. | ||
| 18490 | |||
| 18491 | @item | ||
| 18492 | Similarly, 10 divided by 5 is 2 with a remainder of 0; | ||
| 18493 | |||
| 18494 | @item | ||
| 18495 | 11 divided by 5 is 2 with a remainder of 1; | ||
| 18496 | |||
| 18497 | @item | ||
| 18498 | 12 divided by 5 is 1 with a remainder of 2. | ||
| 18499 | @end itemize | ||
| 18500 | |||
| 18501 | @need 1250 | ||
| 18502 | @noindent | ||
| 18503 | By considering the cases as parallel, we can see that | ||
| 18504 | |||
| 18505 | @itemize @bullet | ||
| 18506 | @item | ||
| 18507 | zero divided by 5 must be zero with a remainder of zero; | ||
| 18508 | |||
| 18509 | @item | ||
| 18510 | 1 divided by 5 must be zero with a remainder of 1; | ||
| 18511 | |||
| 18512 | @item | ||
| 18513 | 2 divided by 5 must be zero with a remainder of 2; | ||
| 18514 | @end itemize | ||
| 18515 | |||
| 18516 | @noindent | ||
| 18517 | and so on. | ||
| 18518 | |||
| 18519 | @need 1250 | ||
| 18520 | So, in this code, if the value of @code{length} is 5, then the result of | ||
| 18521 | evaluating | ||
| 18522 | |||
| 18523 | @smallexample | ||
| 18524 | (% 1 5) | ||
| 18525 | @end smallexample | ||
| 18526 | |||
| 18527 | @noindent | ||
| 18528 | is 1. (I just checked this by placing the cursor after the expression | ||
| 18529 | and 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 | |||
| 18535 | When the @code{kill-ring-yank-pointer} points to the | ||
| 18536 | beginning 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 | 19663 | In this case, the first argument is often smaller than the second. |
| 18548 | therefore, | 19664 | That 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 | 19675 | We can guess what the @code{-} function does. It is like @code{+} but |
| 18558 | @noindent | 19676 | substracts instead of adds; the @code{-} function subtracts its second |
| 18559 | and consequently: | 19677 | argument from its first. Also, we already know what the @code{length} |
| 18560 | 19678 | function 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 | 19680 | And @code{n} is the name of the required argument to the |
| 18570 | regardless of the value of @code{length}. | 19681 | @code{current-kill} function. |
| 18571 | 19682 | ||
| 18572 | @need 1250 | 19683 | @need 1250 |
| 18573 | @noindent | 19684 | So when the first argument to @code{nthcdr} is zero, the @code{nthcdr} |
| 18574 | As a result of this, the @code{setq kill-ring-yank-pointer} expression | 19685 | expression returns the whole list, as you can see by evaluating the |
| 18575 | simplifies to: | 19686 | following: |
| 18576 | |||
| 18577 | @smallexample | ||
| 18578 | (setq kill-ring-yank-pointer (nthcdr 1 kill-ring)) | ||
| 18579 | @end smallexample | ||
| 18580 | |||
| 18581 | @noindent | ||
| 18582 | What it does is now easy to understand. Instead of pointing as it did | ||
| 18583 | to the first element of the kill ring, the | ||
| 18584 | @code{kill-ring-yank-pointer} is set to point to the second element. | ||
| 18585 | |||
| 18586 | Clearly, if the argument passed to @code{rotate-yank-pointer} is two, then | ||
| 18587 | the @code{kill-ring-yank-pointer} is set to @code{(nthcdr 2 kill-ring)}; | ||
| 18588 | and so on for different values of the argument. | ||
| 18589 | |||
| 18590 | Similarly, if the @code{kill-ring-yank-pointer} starts out pointing to | ||
| 18591 | the second element of the kill ring, its length is shorter than the | ||
| 18592 | length of the kill ring by 1, so the computation of the remainder is | ||
| 18593 | based on the expression @code{(% (+ arg 1) length)}. This means that | ||
| 18594 | the @code{kill-ring-yank-pointer} is moved from the second element of | ||
| 18595 | the 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 | |||
| 18601 | The final question is, what happens if the @code{kill-ring-yank-pointer} | ||
| 18602 | is 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 | ||
| 18604 | kill ring? The answer is no. What happens is different and useful. | ||
| 18605 | The @code{kill-ring-yank-pointer} is set to point to the beginning of | ||
| 18606 | the kill ring instead. | ||
| 18607 | |||
| 18608 | Let's see how this works by looking at the code, assuming the length of the | ||
| 18609 | kill ring is 5 and the argument passed to @code{rotate-yank-pointer} is 1. | ||
| 18610 | When the @code{kill-ring-yank-pointer} points to the last element of | ||
| 18611 | the 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 |
| 18618 | When the variables are replaced by their numeric values, the expression | 19700 | When the first argument to the @code{current-kill} function is one, |
| 18619 | looks like this: | 19701 | the @code{nthcdr} expression returns the list without its first |
| 19702 | element. | ||
| 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" |
| 18626 | This expression can be evaluated by looking at the most embedded inner | 19709 | "second piece of text" |
| 18627 | expression first and working outwards: The value of @code{(- 5 1)} is 4; | 19710 | "first some text")) |
| 18628 | the sum of @code{(+ 1 4)} is 5; and the remainder of dividing 5 by 5 is | 19711 | @end group |
| 18629 | zero. 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 |
| 18636 | which will set the @code{kill-ring-yank-pointer} to point to the beginning | 19715 | @cindex @samp{variable, global}, defined |
| 18637 | of the kill ring. | 19716 | Incidently, both @code{kill-ring} and @code{kill-ring-yank-pointer} |
| 18638 | 19717 | are @dfn{global variables}. That means that any expression in Emacs | |
| 18639 | So what happens with successive calls to @code{rotate-yank-pointer} is that | 19718 | Lisp can access them. They are not like the local variables set by |
| 18640 | it 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 |
| 18641 | kill ring until it reaches the end; then it jumps back to the beginning. | 19720 | argument list (@pxref{defun, , The @code{defun} Special Form}). Local |
| 18642 | And this is why the kill ring is called a ring, since by jumping back to | 19721 | variables can only be accessed within the @code{let} that defines them |
| 18643 | the beginning, it is as if the list has no end! (And what is a ring, but | 19722 | or the function that specifies them in an argument list (and within |
| 18644 | an entity with no end?) | 19723 | expressions 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 | ||
| 18651 | After learning about @code{rotate-yank-pointer}, the code for the | 19730 | After 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 |
| 18653 | the computation of the argument to be passed to @code{rotate-yank-pointer}. | 19732 | the computation of the argument to be passed to @code{rotate-yank-pointer}. |
| 18654 | 19733 | ||
| 18655 | @need 1250 | 19734 | @need 1250 |
| 18656 | The code looks like this: | 19735 | The 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. |
| 18662 | More precisely, reinsert the stretch of killed text most | 19742 | More precisely, reinsert the stretch of killed text most recently |
| 18663 | recently killed OR yanked. | 19743 | killed OR yanked. Put point at end, and set mark at beginning. |
| 18664 | With just C-U as argument, same but put point in front | 19744 | With just \\[universal-argument] as argument, same but put point at |
| 18665 | (and mark at end). With argument n, reinsert the nth | 19745 | beginning (and mark at end). With argument N, reinsert the Nth most |
| 18666 | most recently killed stretch of killed text. | 19746 | recently killed stretch of killed text. |
| 19747 | |||
| 19748 | When this command inserts killed text into the buffer, it honors | ||
| 19749 | `yank-excluded-properties' and `yank-handler' as described in the | ||
| 19750 | doc string for `insert-for-yank-1', which see. | ||
| 19751 | |||
| 18667 | See also the command \\[yank-pop]." | 19752 | See 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 | |||
| 18682 | Glancing over this code, we can understand the last few lines readily | ||
| 18683 | enough. 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 | ||
| 18685 | inserted; 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 | ||
| 18687 | front of the inserted text rather than at the end. This option is | ||
| 18688 | explained 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 | ||
| 18690 | the 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 | |||
| 18700 | The hard part of @code{yank} is understanding the computation that | ||
| 18701 | determines the value of the argument passed to | ||
| 18702 | @code{rotate-yank-pointer}. Fortunately, it is not so difficult as it | ||
| 18703 | looks at first sight. | ||
| 18704 | |||
| 18705 | What 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 | ||
| 18707 | argument passed to @code{rotate-yank-pointer}. | ||
| 18708 | |||
| 18709 | @need 1250 | ||
| 18710 | Laid 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 | ||
| 18723 | This code consists of two @code{if} expression, one the else-part of | ||
| 18724 | the other. | ||
| 18725 | |||
| 18726 | The first or outer @code{if} expression tests whether the argument | ||
| 18727 | passed 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 | ||
| 18729 | passed the value of @code{nil} for the optional argument and an | ||
| 18730 | evaluation of @code{(listp nil)} returns true! So, if no argument is | ||
| 18731 | passed to @code{yank}, the argument passed to | ||
| 18732 | @code{rotate-yank-pointer} inside of @code{yank} is zero. This means | ||
| 18733 | the pointer is not moved and the first element to which | ||
| 18734 | @code{kill-ring-yank-pointer} points is inserted, as we expect. | ||
| 18735 | Similarly, if the argument for @code{yank} is @kbd{C-u}, this will be | ||
| 18736 | read as a list, so again, a zero will be passed to | ||
| 18737 | @code{rotate-yank-pointer}. (@kbd{C-u} produces an unprocessed prefix | ||
| 18738 | argument of @code{(4)}, which is a list of one element.) At the same | ||
| 18739 | time, 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 | ||
| 18741 | the insertion. (The @code{P} argument to @code{interactive} is | ||
| 18742 | designed to provide these values for the case when an optional | ||
| 18743 | argument is not provided or when it is @kbd{C-u}.) | ||
| 18744 | |||
| 18745 | The then-part of the outer @code{if} expression handles the case when | ||
| 18746 | there is no argument or when it is @kbd{C-u}. The else-part handles the | ||
| 18747 | other situations. The else-part is itself another @code{if} expression. | ||
| 18748 | |||
| 18749 | The inner @code{if} expression tests whether the argument is a minus | ||
| 18750 | sign. (This is done by pressing the @key{META} and @kbd{-} keys at the | ||
| 18751 | same time, or the @key{ESC} key and then the @kbd{-} key). In this | ||
| 18752 | case, the @code{rotate-yank-pointer} function is passed @kbd{-1} as an | ||
| 18753 | argument. This moves the @code{kill-ring-yank-pointer} backwards, which | ||
| 18754 | is what is desired. | ||
| 18755 | |||
| 18756 | If 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 | ||
| 18758 | expression is evaluated. This is the expression @code{(1- arg)}. | ||
| 18759 | Because of the two @code{if} expressions, it will only occur when the | ||
| 18760 | argument is a positive number or when it is a negative number (not | ||
| 18761 | just a minus sign on its own). What @code{(1- arg)} does is decrement | ||
| 18762 | the number and return it. (The @code{1-} function subtracts one from | ||
| 18763 | its argument.) This means that if the argument to | ||
| 18764 | @code{rotate-yank-pointer} is 1, it is reduced to zero, which means | ||
| 18765 | the first element to which @code{kill-ring-yank-pointer} points is | ||
| 18766 | yanked 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 | |||
| 18771 | Finally, the question arises, what happens if either the remainder | ||
| 18772 | function, @code{%}, or the @code{nthcdr} function is passed a negative | ||
| 18773 | argument, as they quite well may? | ||
| 18774 | |||
| 18775 | The answers can be found by a quick test. When @code{(% -1 5)} is | ||
| 18776 | evaluated, a negative number is returned; and if @code{nthcdr} is | ||
| 18777 | called with a negative number, it returns the same value as if it were | ||
| 18778 | called with a first argument of zero. This can be seen by evaluating | ||
| 18779 | the following code. | ||
| 18780 | |||
| 18781 | Here the @samp{@result{}} points to the result of evaluating the code | ||
| 18782 | preceding it. This was done by positioning the cursor after the code | ||
| 18783 | and typing @kbd{C-x C-e} (@code{eval-last-sexp}) in the usual fashion. | ||
| 18784 | You 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 | 19783 | The key expression is @code{insert-for-yank}, which inserts the string |
| 18793 | (setq animals '(cats dogs elephants)) | 19784 | returned by @code{current-kill}, but removes some text properties from |
| 18794 | @result{} (cats dogs elephants) | 19785 | it. |
| 18795 | @end group | ||
| 18796 | 19786 | ||
| 18797 | @group | 19787 | However, before getting to that expression, the function set the value |
| 18798 | (nthcdr 1 animals) | 19788 | of @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 | 19790 | currently starts. It also set @code{this-command} and pushed the |
| 19791 | mark. | ||
| 18801 | 19792 | ||
| 18802 | @group | 19793 | After 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) | 19795 | the yanked text and mark at its end. (The @code{prog1} function is |
| 18805 | @end group | 19796 | like @code{progn} but returns the value of its first argument rather |
| 19797 | than the value of its last argument. Its first argument is forced to | ||
| 19798 | return the buffer's mark as an integer. You can see the documentation | ||
| 19799 | for these functions by placing point over them in this buffer and then | ||
| 19800 | typing @kbd{C-h f} (@code{describe-function}) followed by a @kbd{RET}; | ||
| 19801 | the default is the function.) | ||
| 18806 | 19802 | ||
| 18807 | @group | 19803 | The 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 | ||
| 18813 | So, 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 | ||
| 18815 | beginning of the list. Then it stays there. Unlike the other case, | ||
| 18816 | when it jumps from the end of the list to the beginning of the list, | ||
| 18817 | making a ring, it stops. This makes sense. You often want to get back | ||
| 18818 | to the most recently clipped out piece of text, but you don't usually | ||
| 18819 | want to insert text from as many as thirty kill commands ago. So you | ||
| 18820 | need to work through the ring to get to the end, but won't cycle around | ||
| 18821 | it inadvertently if you are trying to come back to the beginning. | ||
| 18822 | 19805 | ||
| 18823 | Incidentally, any number passed to @code{yank} with a minus sign | ||
| 18824 | preceding it will be treated as @minus{}1. This is evidently a | ||
| 18825 | simplification for writing the program. You don't need to jump back | ||
| 18826 | towards the beginning of the kill ring more than one place at a time | ||
| 18827 | and doing this is easier than writing a function to determine the | ||
| 18828 | magnitude 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 | ||
| 18835 | After understanding @code{yank}, the @code{yank-pop} function is easy. | 19812 | After understanding @code{yank} and @code{current-kill}, you know how |
| 18836 | Leaving out the documentation to save space, it looks like this: | 19813 | to approach the @code{yank-pop} function Leaving out the documentation |
| 19814 | to 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 | |||
| 18859 | argument is processed and passed to the function. The command can | 19859 | argument is processed and passed to the function. The command can |
| 18860 | only be used after a previous yank; otherwise an error message is | 19860 | only be used after a previous yank; otherwise an error message is |
| 18861 | sent. This check uses the variable @code{last-command} which is | 19861 | sent. This check uses the variable @code{last-command} which is |
| 18862 | discussed elsewhere. (@xref{copy-region-as-kill}.) | 19862 | set by @code{yank} and is discussed elsewhere. (@xref{copy-region-as-kill}.) |
| 18863 | 19863 | ||
| 18864 | The @code{let} clause sets the variable @code{before} to true or false | 19864 | The @code{let} clause sets the variable @code{before} to true or false |
| 18865 | depending whether point is before or after mark and then the region | 19865 | depending whether point is before or after mark and then the region |
| 18866 | between point and mark is deleted. This is the region that was just | 19866 | between point and mark is deleted. This is the region that was just |
| 18867 | inserted by the previous yank and it is this text that will be | 19867 | inserted by the previous yank and it is this text that will be |
| 18868 | replaced. Next the @code{kill-ring-yank-pointer} is rotated so that | 19868 | replaced. |
| 18869 | the previously inserted text is not reinserted yet again. Mark is set | 19869 | |
| 18870 | at the beginning of the place the new text will be inserted and then | 19870 | @code{funcall} calls its first argument as a function, passing |
| 18871 | the first element to which @code{kill-ring-yank-pointer} points is | 19871 | remaining arguments to it. The first argument is whatever the |
| 18872 | inserted. This leaves point after the new text. If in the previous | 19872 | @code{or} expression returns. The two remaining arguments are the |
| 18873 | yank, point was left before the inserted text, point and mark are now | 19873 | positions of point and mark set by the preceding @code{yank} command. |
| 18874 | exchanged so point is again left in front of the newly inserted text. | 19874 | |
| 18875 | That is all there is to it! | 19875 | There 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 | |||
| 19073 | five. | 20073 | five. |
| 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 | |||
| 19129 | that you cannot discover using @code{apropos}: you find nothing if you | 20129 | that you cannot discover using @code{apropos}: you find nothing if you |
| 19130 | type @kbd{M-x apropos @key{RET} remainder @key{RET}}. The only way to | 20130 | type @kbd{M-x apropos @key{RET} remainder @key{RET}}. The only way to |
| 19131 | learn of the existence of @code{%} is to read about it in a book such | 20131 | learn of the existence of @code{%} is to read about it in a book such |
| 19132 | as this or in the Emacs Lisp sources. The @code{%} function is used | 20132 | as this or in the Emacs Lisp sources. |
| 19133 | in the code for @code{rotate-yank-pointer}, which is described in an | ||
| 19134 | appendix. (@xref{rotate-yk-ptr body, , The Body of | ||
| 19135 | @code{rotate-yank-pointer}}.) | ||
| 19136 | 20133 | ||
| 19137 | You can try the @code{%} function by evaluating the following two | 20134 | You can try the @code{%} function by evaluating the following two |
| 19138 | expressions: | 20135 | expressions: |
| @@ -19515,8 +20512,8 @@ the width of the symbols that you are using to print the body of the | |||
| 19515 | graph without changing the ways the graph is labelled. | 20512 | graph 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 |