ebook img

Programming in Emacs Lisp : an introduction PDF

262 Pages·1997·0.925 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming in Emacs Lisp : an introduction

Programming in Emacs Lisp An Introduction Programming in Emacs Lisp An Introduction Edition 1.05, 21 October 1997 by Robert J. Chassell Copyright (cid:13)c 1990, ’91, ’92, ’93, ’94, ’95, ’97 Free Software Foundation, Inc. Published by the Free Software Foundation, Inc. 59 Temple Place, Suite 330 Boston, MA 02111-1307 USA ISBN-1882114-41-8 Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permissionisgrantedtocopyanddistributemodifiedversionsofthismanualunderthecon- ditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Free Software Foundation. i Short Contents Preface......................................... ix 1 List Processing................................. 1 2 Practicing Evaluation ........................... 19 3 How To Write Function Definitions .................. 25 4 A Few Buffer–Related Functions.................... 45 5 A Few More Complex Functions .................... 55 6 Narrowing and Widening......................... 67 7 car, cdr, cons: Fundamental Functions ............. 71 8 Cutting and Storing Text......................... 79 9 How Lists are Implemented ....................... 97 10 Yanking Text Back ............................ 101 11 Loops and Recursion ........................... 105 12 Regular Expression Searches...................... 121 13 Counting: Repetition and Regexps.................. 137 14 Counting Words in a defun...................... 149 15 Readying a Graph............................. 167 16 Your ‘.emacs’ File............................ 177 17 Debugging .................................. 191 18 Conclusion.................................. 197 Appendix A The the-the Function .................. 199 Appendix B Handling the Kill Ring ................... 201 Appendix C A Graph with Labelled Axes ............... 211 Index......................................... 233 ii Programming in Emacs Lisp iii Table of Contents Preface ...................................... ix For Whom This is Written.................................... x Lisp History ................................................ xi A Note for Novices.......................................... xi Thank You ................................................. xi 1 List Processing ............................ 1 1.1 Lisp Lists............................................... 1 1.1.1 Lisp Atoms .................................... 1 1.1.2 Whitespace in Lists............................. 2 1.1.3 GNU Emacs Helps You Type Lists............... 3 1.2 Run a Program......................................... 3 1.3 Generate an Error Message .............................. 4 1.4 Symbol Names and Function Definitions .................. 5 1.5 The Lisp Interpreter..................................... 5 1.5.1 Byte Compiling................................. 6 1.6 Evaluation.............................................. 6 1.6.1 Evaluating Inner Lists .......................... 7 1.7 Variables............................................... 8 1.7.1 Error Message for a Symbol Without a Value..... 9 1.8 Arguments.............................................. 9 1.8.1 Arguments’ Data Types........................ 10 1.8.2 An Argument as the Value of a Variable or List.. 11 1.8.3 Variable Number of Arguments................. 11 1.8.4 Using the Wrong Type Object as an Argument .. 11 1.8.5 The message Function......................... 12 1.9 Setting the Value of a Variable.......................... 14 1.9.1 Using set..................................... 14 1.9.2 Using setq.................................... 15 1.9.3 Counting...................................... 15 1.10 Summary............................................. 16 1.11 Exercises............................................. 17 2 Practicing Evaluation ..................... 19 2.1 Buffer Names.......................................... 19 2.2 Getting Buffers ........................................ 21 2.3 Switching Buffers ...................................... 22 2.4 Buffer Size and the Location of Point.................... 23 2.5 Exercise............................................... 23 iv Programming in Emacs Lisp 3 How To Write Function Definitions ........ 25 3.1 The defun Special Form................................ 25 3.2 Install a Function Definition ............................ 27 3.2.1 Change a Function Definition................... 28 3.3 Make a Function Interactive ............................ 28 3.3.1 An Interactive multiply-by-seven.............. 29 3.4 Different Options for interactive ...................... 30 3.5 Install Code Permanently............................... 31 3.6 let ................................................... 32 3.6.1 The Parts of a let Expression.................. 32 3.6.2 Sample let Expression......................... 33 3.6.3 Uninitialized Variables in a let Statement....... 34 3.7 The if Special Form ................................... 34 3.7.1 The type-of-animal Function in Detail......... 35 3.8 If–then–else Expressions................................ 36 3.9 Truth and Falsehood in Lisp............................ 37 3.10 save-excursion...................................... 38 3.10.1 Template for a save-excursion Expression .... 39 3.11 Review............................................... 40 3.12 Exercises............................................. 43 4 A Few Buffer–Related Functions........... 45 4.1 Finding More Information .............................. 45 4.2 A Simplified beginning-of-buffer Definition............ 45 4.3 The Definition of mark-whole-buffer................... 47 4.3.1 Body of mark-whole-buffer................... 48 4.4 The Definition of append-to-buffer .................... 48 4.4.1 The append-to-buffer Interactive Expression... 49 4.4.2 The Body of append-to-buffer................ 50 4.4.3 save-excursion in append-to-buffer.......... 50 4.5 Review................................................ 52 4.6 Exercises.............................................. 53 5 A Few More Complex Functions........... 55 5.1 The Definition of copy-to-buffer....................... 55 5.2 The Definition of insert-buffer........................ 56 5.2.1 The Interactive Expression in insert-buffer.... 56 A Read-only Buffer............................ 56 ‘b’ in an Interactive Expression................. 57 5.2.2 The Body of the insert-buffer Function....... 57 5.2.3 insert-buffer With an if Instead of an or..... 57 5.2.4 The or in the Body............................ 58 5.2.5 The let Expression in insert-buffer.......... 59 5.3 Complete Definition of beginning-of-buffer............ 60 5.3.1 Optional Arguments........................... 61 5.3.2 beginning-of-buffer with an Argument....... 62 What happens in a large buffer................. 62 What happens in a small buffer................ 63 5.3.3 The Complete beginning-of-buffer........... 64 5.4 Review................................................ 65 5.5 &optional Argument Exercise .......................... 65 v 6 Narrowing and Widening.................. 67 6.1 The save-restriction Special Form.................... 67 6.2 what-line............................................. 68 6.3 Exercise with Narrowing................................ 69 7 car, cdr, cons: Fundamental Functions ..... 71 7.1 car and cdr........................................... 71 7.2 cons.................................................. 72 7.2.1 Find the Length of a List: length.............. 73 7.3 nthcdr................................................ 74 7.4 setcar................................................ 76 7.5 setcdr................................................ 76 7.6 Exercise............................................... 77 8 Cutting and Storing Text ................. 79 8.1 zap-to-char .......................................... 80 8.1.1 The interactive Expression................... 80 8.1.2 The Body of zap-to-char...................... 81 8.1.3 The search-forward Function ................. 81 8.1.4 The progn Function ........................... 82 8.1.5 Summing up zap-to-char ..................... 83 8.1.6 The Version 18 Implementation................. 83 The body of the progn expression.............. 84 8.2 kill-region .......................................... 86 8.3 delete-region: A Digression into C .................... 86 8.4 Initializing a Variable with defvar ...................... 88 8.5 copy-region-as-kill.................................. 89 8.5.1 The Body of copy-region-as-kill............. 90 The kill-append function..................... 91 The else-part of copy-region-as-kill......... 92 8.6 Review................................................ 94 8.7 Searching Exercises..................................... 96 9 How Lists are Implemented ............... 97 9.1 Exercise.............................................. 100 10 Yanking Text Back ..................... 101 10.1 Kill Ring Overview................................... 101 10.2 The kill-ring-yank-pointer Variable ............... 101 10.3 Exercises with yank and nthcdr....................... 103 vi Programming in Emacs Lisp 11 Loops and Recursion.................... 105 11.1 while............................................... 105 11.1.1 A while Loop and a List..................... 106 11.1.2 An Example: print-elements-of-list ...... 107 11.1.3 A Loop with an Incrementing Counter........ 108 Example with incrementing counter ........... 108 The parts of the function definition............ 109 Putting the function definition together........ 110 11.1.4 Loop with a Decrementing Counter........... 112 Example with decrementing counter........... 112 The parts of the function definition............ 113 Putting the function definition together........ 113 11.2 Recursion ........................................... 114 11.2.1 Recursion with a List........................ 115 11.2.2 Recursion in Place of a Counter .............. 116 An argument of 3............................ 117 11.2.3 Recursion Example Using cond............... 118 11.3 Looping Exercise..................................... 119 12 Regular Expression Searches ............ 121 12.1 The Regular Expression for sentence-end............. 121 12.2 The re-search-forward Function..................... 122 12.3 forward-sentence................................... 123 The while loops............................. 124 The regular expression search................. 126 12.4 forward-paragraph: a Goldmine of Functions ......... 126 The let* expression.......................... 127 The forward motion while loop............... 129 Between paragraphs.......................... 130 Within paragraphs........................... 131 No fill prefix................................. 131 With a fill prefix............................. 132 Summary.................................... 132 12.5 Create Your Own ‘TAGS’ File.......................... 134 12.6 Review.............................................. 134 12.7 Exercises with re-search-forward.................... 136 13 Counting: Repetition and Regexps ...... 137 13.1 The count-words-region Function ................... 137 13.1.1 The Whitespace Bug in count-words-region.. 140 13.2 Count Words Recursively............................. 142 13.3 Exercise: Counting Punctuation....................... 147

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.