-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemacs-lisp-intro.texi
21974 lines (18312 loc) · 754 KB
/
emacs-lisp-intro.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\input texinfo @c -*-texinfo-*-
@comment %**start of header
@setfilename ../../info/eintr
@c setfilename emacs-lisp-intro.info
@c sethtmlfilename emacs-lisp-intro.html
@settitle Programming in Emacs Lisp
@syncodeindex vr cp
@syncodeindex fn cp
@finalout
@include emacsver.texi
@c ================ How to Print a Book in Various Sizes ================
@c This book can be printed in any of three different sizes.
@c Set the following @-commands appropriately.
@c 7 by 9.25 inches:
@c @smallbook
@c @clear largebook
@c 8.5 by 11 inches:
@c @c smallbook
@c @set largebook
@c European A4 size paper:
@c @c smallbook
@c @afourpaper
@c @set largebook
@c (Note: if you edit the book so as to change the length of the
@c table of contents, you may have to change the value of `pageno' below.)
@c <<<< For hard copy printing, this file is now
@c set for smallbook, which works for all sizes
@c of paper, and with PostScript figures >>>>
@set smallbook
@ifset smallbook
@smallbook
@clear largebook
@end ifset
@c ================ Included Figures ================
@c If you clear this, the figures will be printed as ASCII diagrams
@c rather than PostScript/PDF.
@c (This is not relevant to Info, since Info only handles ASCII.)
@set print-postscript-figures
@c clear print-postscript-figures
@comment %**end of header
@c per rms and peterb, use 10pt fonts for the main text, mostly to
@c save on paper cost.
@c Do this inside @tex for now, so current makeinfo does not complain.
@tex
@ifset smallbook
@fonttextsize 10
@end ifset
\global\hbadness=6666 % don't worry about not-too-underfull boxes
@end tex
@c These refer to the printed book sold by the FSF.
@set edition-number 3.10
@set update-date 28 October 2009
@c For next or subsequent edition:
@c create function using with-output-to-temp-buffer
@c create a major mode, with keymaps
@c run an asynchronous process, like grep or diff
@c For 8.5 by 11 inch format: do not use such a small amount of
@c whitespace between paragraphs as smallbook format
@ifset largebook
@tex
\global\parskip 6pt plus 1pt
@end tex
@end ifset
@c For all sized formats: print within-book cross
@c reference with ``...'' rather than [...]
@c This works with the texinfo.tex file, version 2003-05-04.08,
@c in the Texinfo version 4.6 of the 2003 Jun 13 distribution.
@tex
\if \xrefprintnodename
\global\def\xrefprintnodename#1{\unskip, ``#1''}
\else
\global\def\xrefprintnodename#1{ ``#1''}
\fi
% \global\def\xrefprintnodename#1{, ``#1''}
@end tex
@c ----------------------------------------------------
@dircategory GNU Emacs Lisp
@direntry
* Emacs Lisp Intro: (eintr).
A simple introduction to Emacs Lisp programming.
@end direntry
@copying
This is an @cite{Introduction to Programming in Emacs Lisp}, for
people who are not programmers.
@sp 1
@iftex
Edition @value{edition-number}, @value{update-date}
@end iftex
@ifnottex
Distributed with Emacs version @value{EMACSVER}.
@end ifnottex
@sp 1
Copyright @copyright{} 1990--1995, 1997, 2001--2013 Free Software
Foundation, Inc.
@sp 1
@iftex
Published by the:@*
GNU Press, @hfill @uref{http://www.fsf.org/licensing/gnu-press/}@*
a division of the @hfill email: @email{sales@@fsf.org}@*
Free Software Foundation, Inc. @hfill Tel: +1 (617) 542-5942@*
51 Franklin Street, Fifth Floor @hfill Fax: +1 (617) 542-2652@*
Boston, MA 02110-1301 USA
@end iftex
@ifnottex
Printed copies available from @uref{http://shop.fsf.org/}. Published by:
@example
GNU Press, http://www.fsf.org/licensing/gnu-press/
a division of the email: sales@@fsf.org
Free Software Foundation, Inc. Tel: +1 (617) 542-5942
51 Franklin Street, Fifth Floor Fax: +1 (617) 542-2652
Boston, MA 02110-1301 USA
@end example
@end ifnottex
@sp 1
ISBN 1-882114-43-4
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; there
being no Invariant Section, with the Front-Cover Texts being ``A GNU
Manual'', and with the Back-Cover Texts as in (a) below. A copy of
the license is included in the section entitled ``GNU Free
Documentation License''.
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''
@end copying
@c half title; two lines here, so do not use `shorttitlepage'
@tex
{\begingroup%
\hbox{}\vskip 1.5in \chaprm \centerline{An Introduction to}%
\endgroup}%
{\begingroup\hbox{}\vskip 0.25in \chaprm%
\centerline{Programming in Emacs Lisp}%
\endgroup\page\hbox{}\page}
@end tex
@titlepage
@sp 6
@center @titlefont{An Introduction to}
@sp 2
@center @titlefont{Programming in Emacs Lisp}
@sp 2
@center Revised Third Edition
@sp 4
@center by Robert J. Chassell
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@iftex
@headings off
@evenheading @thispage @| @| @thischapter
@oddheading @thissection @| @| @thispage
@end iftex
@ifnothtml
@c Keep T.O.C. short by tightening up for largebook
@ifset largebook
@tex
\global\parskip 2pt plus 1pt
\global\advance\baselineskip by -1pt
@end tex
@end ifset
@end ifnothtml
@shortcontents
@contents
@ifnottex
@node Top
@top An Introduction to Programming in Emacs Lisp
@ifset WWW_GNU_ORG
@html
<p>The homepage for GNU Emacs is at
<a href="/software/emacs/">http://www.gnu.org/software/emacs/</a>.<br>
To view this manual in other formats, click
<a href="/software/emacs/manual/eintr.html">here</a>.
@end html
@end ifset
@insertcopying
This master menu first lists each chapter and index; then it lists
every node in every chapter.
@end ifnottex
@c >>>> Set pageno appropriately <<<<
@c The first page of the Preface is a roman numeral; it is the first
@c right handed page after the Table of Contents; hence the following
@c setting must be for an odd negative number.
@c iftex
@c global@pageno = -11
@c end iftex
@set COUNT-WORDS count-words-example
@c Length of variable name chosen so that things still line up when expanded.
@menu
* Preface:: What to look for.
* List Processing:: What is Lisp?
* Practicing Evaluation:: Running several programs.
* Writing Defuns:: How to write function definitions.
* Buffer Walk Through:: Exploring a few buffer-related functions.
* More Complex:: A few, even more complex functions.
* Narrowing & Widening:: Restricting your and Emacs attention to
a region.
* car cdr & cons:: Fundamental functions in Lisp.
* Cutting & Storing Text:: Removing text and saving it.
* List Implementation:: How lists are implemented in the computer.
* Yanking:: Pasting stored text.
* Loops & Recursion:: How to repeat a process.
* Regexp Search:: Regular expression searches.
* Counting Words:: A review of repetition and regexps.
* Words in a defun:: Counting words in a @code{defun}.
* Readying a Graph:: A prototype graph printing function.
* Emacs Initialization:: How to write a @file{.emacs} file.
* Debugging:: How to run the Emacs Lisp debuggers.
* Conclusion:: Now you have the basics.
* the-the:: An appendix: how to find reduplicated words.
* Kill Ring:: An appendix: how the kill ring works.
* Full Graph:: How to create a graph with labeled axes.
* Free Software and Free Manuals::
* GNU Free Documentation License::
* Index::
* About the Author::
@detailmenu
--- The Detailed Node Listing ---
Preface
* Why:: Why learn Emacs Lisp?
* On Reading this Text:: Read, gain familiarity, pick up habits....
* Who You Are:: For whom this is written.
* Lisp History::
* Note for Novices:: You can read this as a novice.
* Thank You::
List Processing
* Lisp Lists:: What are lists?
* Run a Program:: Any list in Lisp is a program ready to run.
* Making Errors:: Generating an error message.
* Names & Definitions:: Names of symbols and function definitions.
* Lisp Interpreter:: What the Lisp interpreter does.
* Evaluation:: Running a program.
* Variables:: Returning a value from a variable.
* Arguments:: Passing information to a function.
* set & setq:: Setting the value of a variable.
* Summary:: The major points.
* Error Message Exercises::
Lisp Lists
* Numbers Lists:: List have numbers, other lists, in them.
* Lisp Atoms:: Elemental entities.
* Whitespace in Lists:: Formatting lists to be readable.
* Typing Lists:: How GNU Emacs helps you type lists.
The Lisp Interpreter
* Complications:: Variables, Special forms, Lists within.
* Byte Compiling:: Specially processing code for speed.
Evaluation
* How the Interpreter Acts:: Returns and Side Effects...
* Evaluating Inner Lists:: Lists within lists...
Variables
* fill-column Example::
* Void Function:: The error message for a symbol
without a function.
* Void Variable:: The error message for a symbol without a value.
Arguments
* Data types:: Types of data passed to a function.
* Args as Variable or List:: An argument can be the value
of a variable or list.
* Variable Number of Arguments:: Some functions may take a
variable number of arguments.
* Wrong Type of Argument:: Passing an argument of the wrong type
to a function.
* message:: A useful function for sending messages.
Setting the Value of a Variable
* Using set:: Setting values.
* Using setq:: Setting a quoted value.
* Counting:: Using @code{setq} to count.
Practicing Evaluation
* How to Evaluate:: Typing editing commands or @kbd{C-x C-e}
causes evaluation.
* Buffer Names:: Buffers and files are different.
* Getting Buffers:: Getting a buffer itself, not merely its name.
* Switching Buffers:: How to change to another buffer.
* Buffer Size & Locations:: Where point is located and the size of
the buffer.
* Evaluation Exercise::
How To Write Function Definitions
* Primitive Functions::
* defun:: The @code{defun} macro.
* Install:: Install a function definition.
* Interactive:: Making a function interactive.
* Interactive Options:: Different options for @code{interactive}.
* Permanent Installation:: Installing code permanently.
* let:: Creating and initializing local variables.
* if:: What if?
* else:: If--then--else expressions.
* Truth & Falsehood:: What Lisp considers false and true.
* save-excursion:: Keeping track of point, mark, and buffer.
* Review::
* defun Exercises::
Install a Function Definition
* Effect of installation::
* Change a defun:: How to change a function definition.
Make a Function Interactive
* Interactive multiply-by-seven:: An overview.
* multiply-by-seven in detail:: The interactive version.
@code{let}
* Prevent confusion::
* Parts of let Expression::
* Sample let Expression::
* Uninitialized let Variables::
The @code{if} Special Form
* if in more detail::
* type-of-animal in detail:: An example of an @code{if} expression.
Truth and Falsehood in Emacs Lisp
* nil explained:: @code{nil} has two meanings.
@code{save-excursion}
* Point and mark:: A review of various locations.
* Template for save-excursion::
A Few Buffer--Related Functions
* Finding More:: How to find more information.
* simplified-beginning-of-buffer:: Shows @code{goto-char},
@code{point-min}, and @code{push-mark}.
* mark-whole-buffer:: Almost the same as @code{beginning-of-buffer}.
* append-to-buffer:: Uses @code{save-excursion} and
@code{insert-buffer-substring}.
* Buffer Related Review:: Review.
* Buffer Exercises::
The Definition of @code{mark-whole-buffer}
* mark-whole-buffer overview::
* Body of mark-whole-buffer:: Only three lines of code.
The Definition of @code{append-to-buffer}
* append-to-buffer overview::
* append interactive:: A two part interactive expression.
* append-to-buffer body:: Incorporates a @code{let} expression.
* append save-excursion:: How the @code{save-excursion} works.
A Few More Complex Functions
* copy-to-buffer:: With @code{set-buffer}, @code{get-buffer-create}.
* insert-buffer:: Read-only, and with @code{or}.
* beginning-of-buffer:: Shows @code{goto-char},
@code{point-min}, and @code{push-mark}.
* Second Buffer Related Review::
* optional Exercise::
The Definition of @code{insert-buffer}
* insert-buffer code::
* insert-buffer interactive:: When you can read, but not write.
* insert-buffer body:: The body has an @code{or} and a @code{let}.
* if & or:: Using an @code{if} instead of an @code{or}.
* Insert or:: How the @code{or} expression works.
* Insert let:: Two @code{save-excursion} expressions.
* New insert-buffer::
The Interactive Expression in @code{insert-buffer}
* Read-only buffer:: When a buffer cannot be modified.
* b for interactive:: An existing buffer or else its name.
Complete Definition of @code{beginning-of-buffer}
* Optional Arguments::
* beginning-of-buffer opt arg:: Example with optional argument.
* beginning-of-buffer complete::
@code{beginning-of-buffer} with an Argument
* Disentangle beginning-of-buffer::
* Large buffer case::
* Small buffer case::
Narrowing and Widening
* Narrowing advantages:: The advantages of narrowing
* save-restriction:: The @code{save-restriction} special form.
* what-line:: The number of the line that point is on.
* narrow Exercise::
@code{car}, @code{cdr}, @code{cons}: Fundamental Functions
* Strange Names:: An historical aside: why the strange names?
* car & cdr:: Functions for extracting part of a list.
* cons:: Constructing a list.
* nthcdr:: Calling @code{cdr} repeatedly.
* nth::
* setcar:: Changing the first element of a list.
* setcdr:: Changing the rest of a list.
* cons Exercise::
@code{cons}
* Build a list::
* length:: How to find the length of a list.
Cutting and Storing Text
* Storing Text:: Text is stored in a list.
* zap-to-char:: Cutting out text up to a character.
* kill-region:: Cutting text out of a region.
* copy-region-as-kill:: A definition for copying text.
* Digression into C:: Minor note on C programming language macros.
* defvar:: How to give a variable an initial value.
* cons & search-fwd Review::
* search Exercises::
@code{zap-to-char}
* Complete zap-to-char:: The complete implementation.
* zap-to-char interactive:: A three part interactive expression.
* zap-to-char body:: A short overview.
* search-forward:: How to search for a string.
* progn:: The @code{progn} special form.
* Summing up zap-to-char:: Using @code{point} and @code{search-forward}.
@code{kill-region}
* Complete kill-region:: The function definition.
* condition-case:: Dealing with a problem.
* Lisp macro::
@code{copy-region-as-kill}
* Complete copy-region-as-kill:: The complete function definition.
* copy-region-as-kill body:: The body of @code{copy-region-as-kill}.
The Body of @code{copy-region-as-kill}
* last-command & this-command::
* kill-append function::
* kill-new function::
Initializing a Variable with @code{defvar}
* See variable current value::
* defvar and asterisk::
How Lists are Implemented
* Lists diagrammed::
* Symbols as Chest:: Exploring a powerful metaphor.
* List Exercise::
Yanking Text Back
* Kill Ring Overview::
* kill-ring-yank-pointer:: The kill ring is a list.
* yank nthcdr Exercises:: The @code{kill-ring-yank-pointer} variable.
Loops and Recursion
* while:: Causing a stretch of code to repeat.
* dolist dotimes::
* Recursion:: Causing a function to call itself.
* Looping exercise::
@code{while}
* Looping with while:: Repeat so long as test returns true.
* Loop Example:: A @code{while} loop that uses a list.
* print-elements-of-list:: Uses @code{while}, @code{car}, @code{cdr}.
* Incrementing Loop:: A loop with an incrementing counter.
* Incrementing Loop Details::
* Decrementing Loop:: A loop with a decrementing counter.
Details of an Incrementing Loop
* Incrementing Example:: Counting pebbles in a triangle.
* Inc Example parts:: The parts of the function definition.
* Inc Example altogether:: Putting the function definition together.
Loop with a Decrementing Counter
* Decrementing Example:: More pebbles on the beach.
* Dec Example parts:: The parts of the function definition.
* Dec Example altogether:: Putting the function definition together.
Save your time: @code{dolist} and @code{dotimes}
* dolist::
* dotimes::
Recursion
* Building Robots:: Same model, different serial number ...
* Recursive Definition Parts:: Walk until you stop ...
* Recursion with list:: Using a list as the test whether to recurse.
* Recursive triangle function::
* Recursion with cond::
* Recursive Patterns:: Often used templates.
* No Deferment:: Don't store up work ...
* No deferment solution::
Recursion in Place of a Counter
* Recursive Example arg of 1 or 2::
* Recursive Example arg of 3 or 4::
Recursive Patterns
* Every::
* Accumulate::
* Keep::
Regular Expression Searches
* sentence-end:: The regular expression for @code{sentence-end}.
* re-search-forward:: Very similar to @code{search-forward}.
* forward-sentence:: A straightforward example of regexp search.
* forward-paragraph:: A somewhat complex example.
* etags:: How to create your own @file{TAGS} table.
* Regexp Review::
* re-search Exercises::
@code{forward-sentence}
* Complete forward-sentence::
* fwd-sentence while loops:: Two @code{while} loops.
* fwd-sentence re-search:: A regular expression search.
@code{forward-paragraph}: a Goldmine of Functions
* forward-paragraph in brief:: Key parts of the function definition.
* fwd-para let:: The @code{let*} expression.
* fwd-para while:: The forward motion @code{while} loop.
Counting: Repetition and Regexps
* Why Count Words::
* @value{COUNT-WORDS}:: Use a regexp, but find a problem.
* recursive-count-words:: Start with case of no words in region.
* Counting Exercise::
The @code{@value{COUNT-WORDS}} Function
* Design @value{COUNT-WORDS}:: The definition using a @code{while} loop.
* Whitespace Bug:: The Whitespace Bug in @code{@value{COUNT-WORDS}}.
Counting Words in a @code{defun}
* Divide and Conquer::
* Words and Symbols:: What to count?
* Syntax:: What constitutes a word or symbol?
* count-words-in-defun:: Very like @code{@value{COUNT-WORDS}}.
* Several defuns:: Counting several defuns in a file.
* Find a File:: Do you want to look at a file?
* lengths-list-file:: A list of the lengths of many definitions.
* Several files:: Counting in definitions in different files.
* Several files recursively:: Recursively counting in different files.
* Prepare the data:: Prepare the data for display in a graph.
Count Words in @code{defuns} in Different Files
* lengths-list-many-files:: Return a list of the lengths of defuns.
* append:: Attach one list to another.
Prepare the Data for Display in a Graph
* Data for Display in Detail::
* Sorting:: Sorting lists.
* Files List:: Making a list of files.
* Counting function definitions::
Readying a Graph
* Columns of a graph::
* graph-body-print:: How to print the body of a graph.
* recursive-graph-body-print::
* Printed Axes::
* Line Graph Exercise::
Your @file{.emacs} File
* Default Configuration::
* Site-wide Init:: You can write site-wide init files.
* defcustom:: Emacs will write code for you.
* Beginning init File:: How to write a @file{.emacs} init file.
* Text and Auto-fill:: Automatically wrap lines.
* Mail Aliases:: Use abbreviations for email addresses.
* Indent Tabs Mode:: Don't use tabs with @TeX{}
* Keybindings:: Create some personal keybindings.
* Keymaps:: More about key binding.
* Loading Files:: Load (i.e., evaluate) files automatically.
* Autoload:: Make functions available.
* Simple Extension:: Define a function; bind it to a key.
* X11 Colors:: Colors in X.
* Miscellaneous::
* Mode Line:: How to customize your mode line.
Debugging
* debug:: How to use the built-in debugger.
* debug-on-entry:: Start debugging when you call a function.
* debug-on-quit:: Start debugging when you quit with @kbd{C-g}.
* edebug:: How to use Edebug, a source level debugger.
* Debugging Exercises::
Handling the Kill Ring
* What the Kill Ring Does::
* current-kill::
* yank:: Paste a copy of a clipped element.
* yank-pop:: Insert element pointed to.
* ring file::
The @code{current-kill} Function
* Code for current-kill::
* Understanding current-kill::
@code{current-kill} in Outline
* Body of current-kill::
* Digression concerning error:: How to mislead humans, but not computers.
* Determining the Element::
A Graph with Labeled Axes
* Labeled Example::
* print-graph Varlist:: @code{let} expression in @code{print-graph}.
* print-Y-axis:: Print a label for the vertical axis.
* print-X-axis:: Print a horizontal label.
* Print Whole Graph:: The function to print a complete graph.
The @code{print-Y-axis} Function
* print-Y-axis in Detail::
* Height of label:: What height for the Y axis?
* Compute a Remainder:: How to compute the remainder of a division.
* Y Axis Element:: Construct a line for the Y axis.
* Y-axis-column:: Generate a list of Y axis labels.
* print-Y-axis Penultimate:: A not quite final version.
The @code{print-X-axis} Function
* Similarities differences:: Much like @code{print-Y-axis}, but not exactly.
* X Axis Tic Marks:: Create tic marks for the horizontal axis.
Printing the Whole Graph
* The final version:: A few changes.
* Test print-graph:: Run a short test.
* Graphing words in defuns:: Executing the final code.
* lambda:: How to write an anonymous function.
* mapcar:: Apply a function to elements of a list.
* Another Bug:: Yet another bug @dots{} most insidious.
* Final printed graph:: The graph itself!
@end detailmenu
@end menu
@node Preface
@unnumbered Preface
Most of the GNU Emacs integrated environment is written in the programming
language called Emacs Lisp. The code written in this programming
language is the software---the sets of instructions---that tell the
computer what to do when you give it commands. Emacs is designed so
that you can write new code in Emacs Lisp and easily install it as an
extension to the editor.
(GNU Emacs is sometimes called an ``extensible editor'', but it does
much more than provide editing capabilities. It is better to refer to
Emacs as an ``extensible computing environment''. However, that
phrase is quite a mouthful. It is easier to refer to Emacs simply as
an editor. Moreover, everything you do in Emacs---find the Mayan date
and phases of the moon, simplify polynomials, debug code, manage
files, read letters, write books---all these activities are kinds of
editing in the most general sense of the word.)
@menu
* Why:: Why learn Emacs Lisp?
* On Reading this Text:: Read, gain familiarity, pick up habits....
* Who You Are:: For whom this is written.
* Lisp History::
* Note for Novices:: You can read this as a novice.
* Thank You::
@end menu
@ifnottex
@node Why
@unnumberedsec Why Study Emacs Lisp?
@end ifnottex
Although Emacs Lisp is usually thought of in association only with Emacs,
it is a full computer programming language. You can use Emacs Lisp as
you would any other programming language.
Perhaps you want to understand programming; perhaps you want to extend
Emacs; or perhaps you want to become a programmer. This introduction to
Emacs Lisp is designed to get you started: to guide you in learning the
fundamentals of programming, and more importantly, to show you how you
can teach yourself to go further.
@node On Reading this Text
@unnumberedsec On Reading this Text
All through this document, you will see little sample programs you can
run inside of Emacs. If you read this document in Info inside of GNU
Emacs, you can run the programs as they appear. (This is easy to do and
is explained when the examples are presented.) Alternatively, you can
read this introduction as a printed book while sitting beside a computer
running Emacs. (This is what I like to do; I like printed books.) If
you don't have a running Emacs beside you, you can still read this book,
but in this case, it is best to treat it as a novel or as a travel guide
to a country not yet visited: interesting, but not the same as being
there.
Much of this introduction is dedicated to walkthroughs or guided tours
of code used in GNU Emacs. These tours are designed for two purposes:
first, to give you familiarity with real, working code (code you use
every day); and, second, to give you familiarity with the way Emacs
works. It is interesting to see how a working environment is
implemented.
Also, I
hope that you will pick up the habit of browsing through source code.
You can learn from it and mine it for ideas. Having GNU Emacs is like
having a dragon's cave of treasures.
In addition to learning about Emacs as an editor and Emacs Lisp as a
programming language, the examples and guided tours will give you an
opportunity to get acquainted with Emacs as a Lisp programming
environment. GNU Emacs supports programming and provides tools that
you will want to become comfortable using, such as @kbd{M-.} (the key
which invokes the @code{find-tag} command). You will also learn about
buffers and other objects that are part of the environment.
Learning about these features of Emacs is like learning new routes
around your home town.
@ignore
In addition, I have written several programs as extended examples.
Although these are examples, the programs are real. I use them.
Other people use them. You may use them. Beyond the fragments of
programs used for illustrations, there is very little in here that is
`just for teaching purposes'; what you see is used. This is a great
advantage of Emacs Lisp: it is easy to learn to use it for work.
@end ignore
Finally, I hope to convey some of the skills for using Emacs to
learn aspects of programming that you don't know. You can often use
Emacs to help you understand what puzzles you or to find out how to do
something new. This self-reliance is not only a pleasure, but an
advantage.
@node Who You Are
@unnumberedsec For Whom This is Written
This text is written as an elementary introduction for people who are
not programmers. If you are a programmer, you may not be satisfied with
this primer. The reason is that you may have become expert at reading
reference manuals and be put off by the way this text is organized.
An expert programmer who reviewed this text said to me:
@quotation
@i{I prefer to learn from reference manuals. I ``dive into'' each
paragraph, and ``come up for air'' between paragraphs.}
@i{When I get to the end of a paragraph, I assume that that subject is
done, finished, that I know everything I need (with the
possible exception of the case when the next paragraph starts talking
about it in more detail). I expect that a well written reference manual
will not have a lot of redundancy, and that it will have excellent
pointers to the (one) place where the information I want is.}
@end quotation
This introduction is not written for this person!
Firstly, I try to say everything at least three times: first, to
introduce it; second, to show it in context; and third, to show it in a
different context, or to review it.
Secondly, I hardly ever put all the information about a subject in one
place, much less in one paragraph. To my way of thinking, that imposes
too heavy a burden on the reader. Instead I try to explain only what
you need to know at the time. (Sometimes I include a little extra
information so you won't be surprised later when the additional
information is formally introduced.)
When you read this text, you are not expected to learn everything the
first time. Frequently, you need only make, as it were, a `nodding
acquaintance' with some of the items mentioned. My hope is that I have
structured the text and given you enough hints that you will be alert to
what is important, and concentrate on it.
You will need to ``dive into'' some paragraphs; there is no other way
to read them. But I have tried to keep down the number of such
paragraphs. This book is intended as an approachable hill, rather than
as a daunting mountain.
This introduction to @cite{Programming in Emacs Lisp} has a companion
document,
@iftex
@cite{The GNU Emacs Lisp Reference Manual}.
@end iftex
@ifnottex
@ref{Top, , The GNU Emacs Lisp Reference Manual, elisp, The GNU
Emacs Lisp Reference Manual}.
@end ifnottex
The reference manual has more detail than this introduction. In the
reference manual, all the information about one topic is concentrated
in one place. You should turn to it if you are like the programmer
quoted above. And, of course, after you have read this
@cite{Introduction}, you will find the @cite{Reference Manual} useful
when you are writing your own programs.
@node Lisp History
@unnumberedsec Lisp History
@cindex Lisp history
Lisp was first developed in the late 1950s at the Massachusetts
Institute of Technology for research in artificial intelligence. The
great power of the Lisp language makes it superior for other purposes as
well, such as writing editor commands and integrated environments.
@cindex Maclisp
@cindex Common Lisp
GNU Emacs Lisp is largely inspired by Maclisp, which was written at MIT
in the 1960s. It is somewhat inspired by Common Lisp, which became a
standard in the 1980s. However, Emacs Lisp is much simpler than Common
Lisp. (The standard Emacs distribution contains an optional extensions
file, @file{cl.el}, that adds many Common Lisp features to Emacs Lisp.)
@node Note for Novices
@unnumberedsec A Note for Novices
If you don't know GNU Emacs, you can still read this document
profitably. However, I recommend you learn Emacs, if only to learn to
move around your computer screen. You can teach yourself how to use
Emacs with the on-line tutorial. To use it, type @kbd{C-h t}. (This
means you press and release the @key{CTRL} key and the @kbd{h} at the
same time, and then press and release @kbd{t}.)
Also, I often refer to one of Emacs's standard commands by listing the
keys which you press to invoke the command and then giving the name of
the command in parentheses, like this: @kbd{M-C-\}
(@code{indent-region}). What this means is that the
@code{indent-region} command is customarily invoked by typing
@kbd{M-C-\}. (You can, if you wish, change the keys that are typed to
invoke the command; this is called @dfn{rebinding}. @xref{Keymaps, ,
Keymaps}.) The abbreviation @kbd{M-C-\} means that you type your
@key{META} key, @key{CTRL} key and @key{\} key all at the same time.
(On many modern keyboards the @key{META} key is labeled
@key{ALT}.)
Sometimes a combination like this is called a keychord, since it is
similar to the way you play a chord on a piano. If your keyboard does
not have a @key{META} key, the @key{ESC} key prefix is used in place
of it. In this case, @kbd{M-C-\} means that you press and release your
@key{ESC} key and then type the @key{CTRL} key and the @key{\} key at
the same time. But usually @kbd{M-C-\} means press the @key{CTRL} key
along with the key that is labeled @key{ALT} and, at the same time,
press the @key{\} key.
In addition to typing a lone keychord, you can prefix what you type
with @kbd{C-u}, which is called the `universal argument'. The
@kbd{C-u} keychord passes an argument to the subsequent command.
Thus, to indent a region of plain text by 6 spaces, mark the region,
and then type @w{@kbd{C-u 6 M-C-\}}. (If you do not specify a number,
Emacs either passes the number 4 to the command or otherwise runs the
command differently than it would otherwise.) @xref{Arguments, ,
Numeric Arguments, emacs, The GNU Emacs Manual}.
If you are reading this in Info using GNU Emacs, you can read through
this whole document just by pressing the space bar, @key{SPC}.
(To learn about Info, type @kbd{C-h i} and then select Info.)
A note on terminology: when I use the word Lisp alone, I often am
referring to the various dialects of Lisp in general, but when I speak
of Emacs Lisp, I am referring to GNU Emacs Lisp in particular.
@node Thank You
@unnumberedsec Thank You
My thanks to all who helped me with this book. My especial thanks to
@r{Jim Blandy}, @r{Noah Friedman}, @w{Jim Kingdon}, @r{Roland
McGrath}, @w{Frank Ritter}, @w{Randy Smith}, @w{Richard M.
Stallman}, and @w{Melissa Weisshaus}. My thanks also go to both
@w{Philip Johnson} and @w{David Stampe} for their patient
encouragement. My mistakes are my own.
@flushright
Robert J. Chassell
@ifnothtml
@email{bob@@gnu.org}
@end ifnothtml
@ifhtml
bob@@gnu.org
@end ifhtml
@end flushright
@c ================ Beginning of main text ================
@c Start main text on right-hand (verso) page
@tex
\par\vfill\supereject
\headings off
\ifodd\pageno
\par\vfill\supereject
\else
\par\vfill\supereject
\page\hbox{}\page
\par\vfill\supereject
\fi
@end tex
@c Note: this resetting of the page number back to 1 causes TeX to gripe
@c about already having seen page numbers 1-4 before (in the preface):
@c pdfTeX warning (ext4): destination with the same identifier (name{1})
@c has been already used, duplicate ignored
@c I guess that is harmless (what happens if a later part of the text
@c makes a link to something in the first 4 pages though?).
@c E.g., note that the Emacs manual has a preface, but does not bother
@c resetting the page numbers back to 1 after that.
@iftex
@headings off
@evenheading @thispage @| @| @thischapter
@oddheading @thissection @| @| @thispage
@global@pageno = 1
@end iftex
@node List Processing
@chapter List Processing
To the untutored eye, Lisp is a strange programming language. In Lisp
code there are parentheses everywhere. Some people even claim that