unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: dalanicolai <dalanicolai@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: displaying margins leads to Emacs hanging
Date: Fri, 24 Feb 2023 22:43:20 +0100	[thread overview]
Message-ID: <CACJP=3kBRTz3Ha2QNzqeBuPo7GdMcgS7h9+HCcPc1d7mHNDSMQ@mail.gmail.com> (raw)
In-Reply-To: <CACJP=3mBb+MA4eSa95nY3rvQpryM6ZeCFmmYsV16UuRy+JsUCw@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 2704 bytes --]

Looks like this I forgot to attach the file

On Fri, 24 Feb 2023 at 22:19, dalanicolai <dalanicolai@gmail.com> wrote:

> Okay, in that attachment of my previous mail, I forgot to comment out the
> `(set-window-margins nil 4)`.
>
> So here I another mail (and attachment) with an extremely brief recipe:
>
> - load the file in the attachment
> - do `M-x baleen2`
> - type 'emacs' followed by RET
> - do `M-x find-function` `baleen2` and uncomment the line
> `(set-window-margins nil 4)` in the function
> - (re)evalute and repeat (do `M-x baleen2`, try typing 'emacs')
>
> notice the buffer update speed difference
>
> Any idea what might be causing this?
>
> Thanks!
>
> On Fri, 24 Feb 2023 at 21:53, dalanicolai <dalanicolai@gmail.com> wrote:
>
>> B.t.w if someone want to have a look, I'll attach a smaller file here
>> where
>> I have just removed about 200 pages of the book text data, so that it is
>> the file is much smaller, but there is still enough date to clearly show
>> the
>> 'undesired' behavior.
>>
>> So now, Emacs will not 'hang', but there will still be a clear difference
>> in time it
>> takes to update the buffer (between when window margins are displayed,
>> and when they are not).
>>
>> On Fri, 24 Feb 2023 at 21:41, dalanicolai <dalanicolai@gmail.com> wrote:
>>
>>> I hope you might have an idea what causes this behavior. And maybe you
>>> have a suggestion how to prevent it. Furthermore, I guess the behavior
>>> might
>>> be interesting for you to note/observe (but that is just a guess).
>>>
>>> On Fri, 24 Feb 2023 at 21:29, Eli Zaretskii <eliz@gnu.org> wrote:
>>>
>>>> > From: dalanicolai <dalanicolai@gmail.com>
>>>> > Date: Fri, 24 Feb 2023 21:26:05 +0100
>>>> > Cc: emacs-devel@gnu.org
>>>> >
>>>> > I have not really looked at it yet, but just as a quick thought:
>>>> > I am not sure if/why I should debug an infloop, because the
>>>> functionality
>>>> > works perfectly fine when not showing the margins (i.e. when just
>>>> setting
>>>> > the window margins to nil). So it seems to me its not about an
>>>> infinite loop.
>>>> >
>>>> > I was saying 'more or less' because it does not really hang, it just
>>>> takes
>>>> > extremely long to update the buffer text. When waiting long enough,
>>>> > then it seems to continue.
>>>> >
>>>> > Of course, I don't want to waste your time, but I think my
>>>> instructions
>>>> > for reproducing the error, are very clear and short this time (i.e.
>>>> take less
>>>> > than/about a minute). I think it would be most informative if you
>>>> reproduce the
>>>> > error there.
>>>>
>>>> What should I do after I reproduce it, though?  What are your
>>>> expectations from me (or whoever else tries your recipe)?
>>>>
>>>

[-- Attachment #1.2: Type: text/html, Size: 4267 bytes --]

[-- Attachment #2: baleen.el --]
[-- Type: text/x-emacs-lisp, Size: 307240 bytes --]

;;; baleen.el --- Search documents by filtering.     -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Daniel Nicolai

;; Author: Daniel Nicolai <dalanicolai@gmail.com>
;; Keywords: tools

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; 

;;; Code:
(require 'cl-lib)
(require 'page-break-lines nil t)

(defun baleen-render (data)
  (dolist (page data)
    (let* ((props (cdr page))
           (max (1- (length props))))
      (seq-do-indexed (lambda (match i)
                        (let ((o (make-overlay (point)
                                               (progn (progn (insert (nth 4 match))
                                                             (point))))))
                          (overlay-put o
                                       'before-string (propertize " "
                                                                  'display `((margin left-margin)
                                                                             ,(propertize (format " %3d" (car page))
                                                                                          'face 'bold))
                                                                  )))
                        ;; (unless (= i max) (insert "\n")))
                      (insert "\n"))
                      props))
    (insert "\f\n")))

(defun baleen-filter-page (data query)
  (seq-filter (lambda (c)
               (string-match-p query (nth 4 c)))
             (cdr data)))

;; (baleen-filter-page (car test) "ho")

(defun baleen-filter (data query)
  (nreverse
   (seq-reduce (lambda (v p)
                 (if-let (m (baleen-filter-page p query))
                     (cons (cons (car p) m)
                           v)
                   v))
               data
               nil)))

;; (baleen-filter test "ho")

(defun baleen-update ()
  (let* ((query (minibuffer-contents))
         (query-length (length query))
         (parent-results (unless (< query-length 2)
                             (alist-get (substring query 0 -1) baleen-results nil nil #'string=)))
         (current-results (if parent-results
                              (baleen-filter parent-results query)
                            (unless (string-empty-p query) (baleen-filter test query)))))
    (when current-results
      (with-current-buffer (get-buffer-create "*baleen*")
        (erase-buffer)
        (baleen-render current-results))
      (cl-pushnew (cons query current-results) baleen-results :test #'string= :key #'car))))

(defun baleen ()
  (interactive)
  (pop-to-buffer "*baleen*")
  (set-window-margins nil 4)
  (when (featurep 'page-break-lines)
    (page-break-lines-mode))
  (minibuffer-with-setup-hook
      (lambda ()
        (add-hook 'post-command-hook #'baleen-update nil t))
    (dlet (baleen-results)
      (read-string "Baleen: ")
      (pp baleen-results)))
  (kill-buffer "*baleen*"))

;; NOTE alternatively use doc-pymupdf-text-blocks
(setq test '((1 (1 2 3 4 "hello emac")
                (2 3 4 5 "hoi ema"))
             (2 (3 4 2 4 "how do emil"))
             (3 (2 4 5 9 "emacs"))
             (4 (3 5 3 5 "emoe"))))

(defun baleen-update2 ()
  ;; (let ((query (minibuffer-contents)))
  ;;   (with-current-buffer (get-buffer-create "*baleen*")
  ;;     (erase-buffer)
  ;;     (baleen-render (baleen-filter test2 query)))))
  (let* ((query (minibuffer-contents))
         (query-length (length query)))
    (if (> (length previous-query) query-length)
        (with-current-buffer (get-buffer-create "*baleen*")
          (erase-buffer)
          (baleen-render (cdr (alist-get query baleen-results nil nil #'string=))))
      (let* ((parent-results (unless (< query-length 2)
                               (alist-get (substring query 0 -1) baleen-results nil nil #'string=)))
             (current-results (if parent-results
                                  (baleen-filter parent-results query)
                                (unless (string-empty-p query) (baleen-filter test2 query)))))
        (when current-results
          (with-current-buffer (get-buffer-create "*baleen*")
            (erase-buffer)
            (baleen-render current-results))
          (cl-pushnew (cons query current-results) baleen-results :test #'string= :key #'car))

        (setq previous-query query)))))

(defun baleen2 ()
  (interactive)
  (pop-to-buffer "*baleen*")
  (buffer-disable-undo)
  ;; (set-window-margins nil 4)
  (when (featurep 'page-break-lines)
    (page-break-lines-mode))
  (minibuffer-with-setup-hook
      (lambda ()
        (add-hook 'post-command-hook #'baleen-update2 nil t))
    (dlet (previous-query
           baleen-results)
      (read-string "Baleen: ")
      (print (length baleen-results))))
  (kill-buffer "*baleen*"))

;; NOTE alternatively use doc-pymupdf-text-blocks
(setq test2
      '((1 (189.0 195.02845764160156 350.99603271484375 212.2610626220703 "An Introduction to
" 0 0) (149.28021240234375 237.02845764160156 390.6446228027344 254.2610626220703 "Programming in Emacs Lisp
" 1 0)) (3 (172.8000030517578 267.5661315917969 367.04559326171875 288.2452697753906 "An Introduction to
" 0 0) (125.160400390625 306.9261169433594 414.5801696777344 327.6052551269531 "Programming in Emacs Lisp
" 1 0) (233.8809051513672 347.60906982421875 305.9354553222656 360.9072570800781 "Second Edition
" 2 0) (218.40089416503906 407.48907470703125 321.4480895996094 420.7872619628906 "by Robert J. Chassell
" 3 0)) (4 (89.999755859375 389.84906005859375 449.3995666503906 415.38726806640625 "Copyright c⃝ 1990, 1991, 1992, 1993, 1994, 1995, 1997, 2001, 2002 Free
Software Foundation, Inc.
" 0 0) (89.99954223632812 440.00909423828125 321.0759582519531 477.1872863769531 "Published by the Free Software Foundation, Inc.
59 Temple Place, Suite 330
Boston, MA 02111-1307 USA
" 1 0) (89.99752807617188 489.8091125488281 449.4958190917969 628.6273803710938 "Edition 2.05, 2001 Jan 5
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.1 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 freedom to copy and modify
this GNU Manual, like GNU software. Copies published by the Free Software
Foundation raise funds for GNU development.”
" 2 0)) (5 (446.760009765625 49.213592529296875 450.0118103027344 61.180747985839844 "i
" 0 0) (90.0 75.14844512939453 219.12728881835938 92.38106536865234 "Short Contents
" 1 0) (89.99615478515625 106.16294860839844 449.7834167480469 510.0864562988281 "Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1
List Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2
Practicing Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3
How To Write Function Definitions . . . . . . . . . . . . . . . . . . 29
4
A Few Buffer–Related Functions . . . . . . . . . . . . . . . . . . . . 51
5
A Few More Complex Functions . . . . . . . . . . . . . . . . . . . . 63
6
Narrowing and Widening . . . . . . . . . . . . . . . . . . . . . . . . . 77
7
car, cdr, cons: Fundamental Functions . . . . . . . . . . . . . 81
8
Cutting and Storing Text. . . . . . . . . . . . . . . . . . . . . . . . . 89
9
How Lists are Implemented. . . . . . . . . . . . . . . . . . . . . . . 113
10
Yanking Text Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
11
Loops and Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
12
Regular Expression Searches . . . . . . . . . . . . . . . . . . . . . . 149
13
Counting: Repetition and Regexps. . . . . . . . . . . . . . . . . . 167
14
Counting Words in a defun. . . . . . . . . . . . . . . . . . . . . . 181
15
Readying a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
16
Your ‘.emacs’ File. . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
17
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
18
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Appendix A
The the-the Function . . . . . . . . . . . . . . . . . . 241
Appendix B
Handling the Kill Ring . . . . . . . . . . . . . . . . . . . 243
Appendix C
A Graph with Labelled Axes . . . . . . . . . . . . . . . 255
Appendix D
GNU Free Documentation License . . . . . . . . . . . 279
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
" 2 0)) (6 (90.0 49.213592529296875 96.4916763305664 61.180747985839844 "ii
" 0 0)) (7 (440.8800048828125 47.60907745361328 449.89093017578125 60.90726852416992 "iii
" 0 0) (90.0 75.14844512939453 240.67727661132812 92.38106536865234 "Table of Contents
" 1 0) (90.00009155273438 117.08299255371094 449.61639404296875 193.38731384277344 "Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
On Reading this Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
For Whom This is Written. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Lisp History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
A Note for Novices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Thank You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
" 2 0) (90.000732421875 207.6830291748047 449.66815185546875 535.1473999023438 "1
List Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1
Lisp Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1
Lisp Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2
Whitespace in Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3
GNU Emacs Helps You Type Lists . . . . . . . . . . . . . . . 3
1.2
Run a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3
Generate an Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4
Symbol Names and Function Definitions . . . . . . . . . . . . . . . . . . 6
1.5
The Lisp Interpreter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.1
Byte Compiling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6
Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6.1
Evaluating Inner Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7.1
Error Message for a Symbol Without a Function. . 11
1.7.2
Error Message for a Symbol Without a Value . . . . 11
1.8
Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.8.1
Arguments’ Data Types . . . . . . . . . . . . . . . . . . . . . . . . 13
1.8.2
An Argument as the Value of a Variable or List . . 13
1.8.3
Variable Number of Arguments . . . . . . . . . . . . . . . . . 14
1.8.4
Using the Wrong Type Object as an Argument . . 14
1.8.5
The message Function . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.9
Setting the Value of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.9.1
Using set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.9.2
Using setq. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.9.3
Counting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.10
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.11
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
" 3 0) (90.00164794921875 549.443115234375 449.662109375 625.7474365234375 "2
Practicing Evaluation . . . . . . . . . . . . . . . . . . . . . 23
2.1
Buffer Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2
Getting Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.3
Switching Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4
Buffer Size and the Location of Point . . . . . . . . . . . . . . . . . . . . 27
2.5
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
" 4 0)) (8 (90.0 47.60907745361328 98.74909210205078 60.90726852416992 "iv
" 0 0) (90.0 71.24296569824219 449.7541198730469 314.9472961425781 "3
How To Write Function Definitions . . . . . . . . 29
3.1
The defun Special Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2
Install a Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2.1
Change a Function Definition. . . . . . . . . . . . . . . . . . . 32
3.3
Make a Function Interactive . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.3.1
An Interactive multiply-by-seven .. . . . . . . . . . . . 34
3.4
Different Options for interactive . . . . . . . . . . . . . . . . . . . . . . 35
3.5
Install Code Permanently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.6
let . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.6.1
The Parts of a let Expression . . . . . . . . . . . . . . . . . . 37
3.6.2
Sample let Expression. . . . . . . . . . . . . . . . . . . . . . . . . 38
3.6.3
Uninitialized Variables in a let Statement. . . . . . . 39
3.7
The if Special Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.7.1
The type-of-animal Function in Detail. . . . . . . . . 41
3.8
If–then–else Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.9
Truth and Falsehood in Emacs Lisp . . . . . . . . . . . . . . . . . . . . . 43
3.10
save-excursion .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.10.1
Template for a save-excursion Expression . . . . 45
3.11
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.12
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
" 1 0) (90.00115966796875 329.24298095703125 449.7353210449219 465.30731201171875 "4
A Few Buffer–Related Functions. . . . . . . . . . . 51
4.1
Finding More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2
A Simplified beginning-of-buffer Definition. . . . . . . . . . . . 52
4.3
The Definition of mark-whole-buffer . . . . . . . . . . . . . . . . . . . 54
4.3.1
Body of mark-whole-buffer .. . . . . . . . . . . . . . . . . . 55
4.4
The Definition of append-to-buffer . . . . . . . . . . . . . . . . . . . . 56
4.4.1
The append-to-buffer Interactive Expression. . . 57
4.4.2
The Body of append-to-buffer .. . . . . . . . . . . . . . . 57
4.4.3
save-excursion in append-to-buffer.. . . . . . . . . 58
4.5
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.6
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
" 2 0)) (9 (444.239990234375 47.60907745361328 450.0 60.90726852416992 "v
" 0 0) (90.0 71.24296569824219 449.7479553222656 291.0672912597656 "5
A Few More Complex Functions . . . . . . . . . . . 63
5.1
The Definition of copy-to-buffer.. . . . . . . . . . . . . . . . . . . . . . 63
5.2
The Definition of insert-buffer.. . . . . . . . . . . . . . . . . . . . . . . 64
5.2.1
The Interactive Expression in insert-buffer.. . . 65
A Read-only Buffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
‘b’ in an Interactive Expression. . . . . . . . . . . . . . . . . 65
5.2.2
The Body of the insert-buffer Function . . . . . . . 65
5.2.3
insert-buffer With an if Instead of an or. . . . . 66
5.2.4
The or in the Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.2.5
The let Expression in insert-buffer .. . . . . . . . . 68
5.3
Complete Definition of beginning-of-buffer .. . . . . . . . . . . 69
5.3.1
Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.3.2
beginning-of-buffer with an Argument . . . . . . . 71
What happens in a large buffer. . . . . . . . . . . . . . . . . 71
What happens in a small buffer . . . . . . . . . . . . . . . . 72
5.3.3
The Complete beginning-of-buffer .. . . . . . . . . . 73
5.4
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
5.5
optional Argument Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
" 1 0) (89.99783325195312 305.3630065917969 449.7347106933594 357.7873229980469 "6
Narrowing and Widening. . . . . . . . . . . . . . . . . . 77
6.1
The save-restriction Special Form . . . . . . . . . . . . . . . . . . . . 77
6.2
what-line.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
6.3
Exercise with Narrowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
" 2 0) (89.9976806640625 372.0830078125 449.7032775878906 484.267333984375 "7
car, cdr, cons: Fundamental Functions . . . . . 81
7.1
car and cdr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.2
cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.2.1
Find the Length of a List: length . . . . . . . . . . . . . . 84
7.3
nthcdr .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7.4
nth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.5
setcar .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.6
setcdr .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7.7
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
" 3 0)) (10 (90.0 47.60907745361328 98.78182220458984 60.90726852416992 "vi
" 0 0) (90.0 71.24296569824219 449.7066650390625 302.9472961425781 "8
Cutting and Storing Text . . . . . . . . . . . . . . . . . 89
8.1
zap-to-char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.1.1
The interactive Expression . . . . . . . . . . . . . . . . . . . 90
8.1.2
The Body of zap-to-char.. . . . . . . . . . . . . . . . . . . . . 91
8.1.3
The search-forward Function . . . . . . . . . . . . . . . . . 92
8.1.4
The progn Special Form . . . . . . . . . . . . . . . . . . . . . . . 93
8.1.5
Summing up zap-to-char . . . . . . . . . . . . . . . . . . . . . 93
8.2
kill-region . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.2.1
condition-case.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.2.2
delete-and-extract-region . . . . . . . . . . . . . . . . . . 96
8.3
delete-and-extract-region: Digressing into C . . . . . . . . . 98
8.4
Initializing a Variable with defvar . . . . . . . . . . . . . . . . . . . . . 100
8.4.1
defvar and an asterisk. . . . . . . . . . . . . . . . . . . . . . . . 101
8.5
copy-region-as-kill.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
8.5.1
The Body of copy-region-as-kill.. . . . . . . . . . . 103
The kill-append function. . . . . . . . . . . . . . . . . . . . 104
The kill-new function . . . . . . . . . . . . . . . . . . . . . . . 105
8.6
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
8.7
Searching Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
" 1 0) (90.00201416015625 317.3630065917969 449.6243896484375 357.7873229980469 "9
How Lists are Implemented . . . . . . . . . . . . . . 113
9.1
Symbols as a Chest of Drawers . . . . . . . . . . . . . . . . . . . . . . . . . 115
9.2
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
" 2 0) (90.00247192382812 372.0830078125 449.5589599609375 424.50732421875 "10
Yanking Text Back . . . . . . . . . . . . . . . . . . . . . 117
10.1
Kill Ring Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10.2
The kill-ring-yank-pointer Variable . . . . . . . . . . . . . . . 117
10.3
Exercises with yank and nthcdr. . . . . . . . . . . . . . . . . . . . . . . 119
" 3 0) (90.00210571289062 438.9230041503906 449.5998229980469 622.7473754882812 "11
Loops and Recursion. . . . . . . . . . . . . . . . . . . . 121
11.1
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11.1.1
A while Loop and a List. . . . . . . . . . . . . . . . . . . . . 122
11.1.2
An Example: print-elements-of-list . . . . . . 123
11.1.3
A Loop with an Incrementing Counter . . . . . . . . 124
Example with incrementing counter . . . . . . . . . . . 125
The parts of the function definition. . . . . . . . . . . . 126
Putting the function definition together . . . . . . . . 127
11.1.4
Loop with a Decrementing Counter . . . . . . . . . . . 129
Example with decrementing counter . . . . . . . . . . . 129
The parts of the function definition. . . . . . . . . . . . 130
Putting the function definition together . . . . . . . . 130
11.2
Save your time: dolist and dotimes . . . . . . . . . . . . . . . . . . 131
The dolist Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
The dotimes Macro . . . . . . . . . . . . . . . . . . . . . . . . . . 133
" 4 0)) (11 (438.239990234375 47.60907745361328 450.0108947753906 60.90726852416992 "vii
" 0 0) (125.8800048828125 71.48908233642578 449.4754333496094 240.1873016357422 "11.3
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
11.3.1
Building Robots: Extending the Metaphor. . . . . 134
11.3.2
The Parts of a Recursive Definition . . . . . . . . . . . 135
11.3.3
Recursion with a List . . . . . . . . . . . . . . . . . . . . . . . . 136
11.3.4
Recursion in Place of a Counter . . . . . . . . . . . . . . 137
An argument of 3 or 4. . . . . . . . . . . . . . . . . . . . . . . . 138
11.3.5
Recursion Example Using cond . . . . . . . . . . . . . . . 139
11.3.6
Recursive Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Recursive Pattern: every . . . . . . . . . . . . . . . . . . . . . 141
Recursive Pattern: accumulate . . . . . . . . . . . . . . . . 142
Recursive Pattern: keep . . . . . . . . . . . . . . . . . . . . . . 143
11.3.7
Recursion without Deferments . . . . . . . . . . . . . . . . 143
11.3.8
No Deferment Solution. . . . . . . . . . . . . . . . . . . . . . . 145
11.4
Looping Exercise. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
" 1 0) (90.00149536132812 254.60301208496094 449.6435241699219 462.4273376464844 "12
Regular Expression Searches . . . . . . . . . . . . 149
12.1
The Regular Expression for sentence-end .. . . . . . . . . . . . 149
12.2
The re-search-forward Function. . . . . . . . . . . . . . . . . . . . . 150
12.3
forward-sentence .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
The while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The regular expression search . . . . . . . . . . . . . . . . . . . . . . . . 154
12.4
forward-paragraph: a Goldmine of Functions . . . . . . . . . 155
The let* expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
The forward motion while loop . . . . . . . . . . . . . . . . . . . . . . 158
Between paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Within paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
No fill prefix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
With a fill prefix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
12.5
Create Your Own ‘TAGS’ File. . . . . . . . . . . . . . . . . . . . . . . . . . 163
12.6
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
12.7
Exercises with re-search-forward.. . . . . . . . . . . . . . . . . . . 166
" 2 0) (90.00076293945312 476.7230224609375 449.64251708984375 541.02734375 "13
Counting: Repetition and Regexps . . . . . . 167
13.1
The count-words-region Function . . . . . . . . . . . . . . . . . . . 167
13.1.1
The Whitespace Bug in count-words-region.. 170
13.2
Count Words Recursively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
13.3
Exercise: Counting Punctuation . . . . . . . . . . . . . . . . . . . . . . . 179
" 3 0)) (12 (90.0 47.60907745361328 104.76000213623047 60.90726852416992 "viii
" 0 0) (90.0 71.24296569824219 449.6117858886719 243.1873016357422 "14
Counting Words in a defun . . . . . . . . . . . . . . 181
14.1
What to Count? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
14.2
What Constitutes a Word or Symbol? . . . . . . . . . . . . . . . . . 182
14.3
The count-words-in-defun Function . . . . . . . . . . . . . . . . . 183
14.4
Count Several defuns Within a File . . . . . . . . . . . . . . . . . . . 186
14.5
Find a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
14.6
lengths-list-file in Detail . . . . . . . . . . . . . . . . . . . . . . . . . 188
14.7
Count Words in defuns in Different Files . . . . . . . . . . . . . . 190
14.7.1
The append Function . . . . . . . . . . . . . . . . . . . . . . . . 191
14.8
Recursively Count Words in Different Files. . . . . . . . . . . . . 192
14.9
Prepare the Data for Display in a Graph . . . . . . . . . . . . . . . 193
14.9.1
Sorting Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
14.9.2
Making a List of Files. . . . . . . . . . . . . . . . . . . . . . . . 194
14.9.3
Counting function definitions . . . . . . . . . . . . . . . . . 197
" 1 0) (89.99942016601562 257.6029968261719 449.582275390625 321.9073181152344 "15
Readying a Graph . . . . . . . . . . . . . . . . . . . . . . 203
15.1
The graph-body-print Function. . . . . . . . . . . . . . . . . . . . . . 208
15.2
The recursive-graph-body-print Function. . . . . . . . . . . 210
15.3
Need for Printed Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
15.4
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
" 2 0) (89.99984741210938 336.2030029296875 449.5562744140625 520.1473388671875 "16
Your ‘.emacs’ File. . . . . . . . . . . . . . . . . . . . . . . 213
16.1
Site-wide Initialization Files. . . . . . . . . . . . . . . . . . . . . . . . . . . 213
16.2
Specifying Variables using defcustom.. . . . . . . . . . . . . . . . . 214
16.3
Beginning a ‘.emacs’ File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
16.4
Text and Auto Fill Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
16.5
Mail Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
16.6
Indent Tabs Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
16.7
Some Keybindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
16.8
Keymaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
16.9
Loading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
16.10
Autoloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
16.11
A Simple Extension: line-to-top-of-window . . . . . . . . 224
16.12
X11 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
16.13
Miscellaneous Settings for a ‘.emacs’ File . . . . . . . . . . . . . 227
16.14
A Modified Mode Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
" 3 0) (90.00198364257812 534.5630493164062 449.5606689453125 610.8673706054688 "17
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
17.1
debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
17.2
debug-on-entry .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
17.3
debug-on-quit and (debug).. . . . . . . . . . . . . . . . . . . . . . . . . 234
17.4
The edebug Source Level Debugger. . . . . . . . . . . . . . . . . . . . 235
17.5
Debugging Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
" 4 0)) (13 (441.239990234375 47.60907745361328 449.9891052246094 60.90726852416992 "ix
" 0 0) (90.0 71.24296569824219 449.5467834472656 85.66089630126953 "18
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
" 1 0) (90.0001220703125 102.08299255371094 449.6182556152344 116.51359558105469 "Appendix A
The the-the Function. . . . . . . . . . 241
" 2 0) (89.99960327148438 133.0430145263672 449.6458740234375 269.1073303222656 "Appendix B
Handling the Kill Ring . . . . . . . . 243
B.1
The rotate-yank-pointer Function . . . . . . . . . . . . . . . . . . . 243
B.1.1
The Body of rotate-yank-pointer .. . . . . . . . . . 244
The else-part of the if expression . . . . . . . . . . . . . 245
The % remainder function . . . . . . . . . . . . . . . . . . . . . 247
Using % in rotate-yank-pointer . . . . . . . . . . . . . 248
Pointing to the last element. . . . . . . . . . . . . . . . . . . 248
B.2
yank.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Passing the argument . . . . . . . . . . . . . . . . . . . . . . . . 250
Passing a negative argument . . . . . . . . . . . . . . . . . . 251
B.3
yank-pop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
" 3 0) (90.0001220703125 283.4030456542969 449.68988037109375 479.3473815917969 "Appendix C
A Graph with Labelled Axes. . . 255
C.1
The print-graph Varlist. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
C.2
The print-Y-axis Function. . . . . . . . . . . . . . . . . . . . . . . . . . . 256
C.2.1
Side Trip: Compute a Remainder. . . . . . . . . . . . . . 258
C.2.2
Construct a Y Axis Element . . . . . . . . . . . . . . . . . . 259
C.2.3
Create a Y Axis Column. . . . . . . . . . . . . . . . . . . . . . 261
C.2.4
The Not Quite Final Version of print-Y-axis.. 262
C.3
The print-X-axis Function. . . . . . . . . . . . . . . . . . . . . . . . . . . 263
C.3.1
X Axis Tic Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
C.4
Printing the Whole Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
C.4.1
Testing print-graph .. . . . . . . . . . . . . . . . . . . . . . . . 270
C.4.2
Graphing Numbers of Words and Symbols . . . . . 271
C.4.3
A lambda Expression: Useful Anonymity. . . . . . . 272
C.4.4
The mapcar Function. . . . . . . . . . . . . . . . . . . . . . . . . 274
C.4.5
Another Bug . . . Most Insidious . . . . . . . . . . . . . . 274
C.4.6
The Printed Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . 277
" 4 0) (89.99880981445312 493.7004699707031 449.52435302734375 524.02099609375 "Appendix D
GNU Free Documentation License
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
" 5 0) (89.99868774414062 540.443115234375 449.5042724609375 554.8610229492188 "Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
" 6 0)) (14 (90.0 47.60907745361328 95.76000213623047 60.90726852416992 "x
" 0 0)) (15 (90.0 47.60907745361328 449.83599853515625 60.90726852416992 "On Reading this Text
xi
" 0 0) (90.00021362304688 75.14844512939453 152.6470489501953 92.38106536865234 "Preface
" 1 0) (89.999267578125 102.08905792236328 449.4326477050781 360.4271545410156 "Most of the GNU Emacs integrated environment is written in the pro-
gramming language called Emacs Lisp. The code written in this program-
ming language is the software—the sets of instructions—that tell the com-
puter 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.)
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 ex-
tend Emacs; or perhaps you want to become a programmer. This introduc-
tion 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.
" 2 0) (90.00047302246094 376.2202453613281 243.05418395996094 390.5807800292969 "On Reading this Text
" 3 0) (89.99932861328125 398.24896240234375 449.6614685058594 630.7872314453125 "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 walk-throughs 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
" 4 0)) (16 (90.0 47.60907745361328 449.8251953125 60.90726852416992 "xii
Preface
" 0 0) (89.99826049804688 77.48908233642578 449.3992919921875 201.1873016357422 "opportunity to get acquainted with Emacs as a Lisp programming environ-
ment. GNU Emacs supports programming and provides tools that you will
want to become comfortable using, such as M-. (the key which invokes the
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.
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.
" 1 0) (89.99856567382812 221.06036376953125 279.07073974609375 235.4208984375 "For Whom This is Written
" 2 0) (89.99630737304688 244.049072265625 449.50616455078125 630.7874755859375 "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:
I prefer to learn from reference manuals. I “dive into” each para-
graph, and “come up for air” between paragraphs.
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.
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.
" 3 0)) (17 (90.0 47.60907745361328 449.7156982421875 60.90726852416992 "A Note for Novices
xiii
" 0 0) (89.99826049804688 77.48908233642578 449.42022705078125 188.34727478027344 "This book is intended as an approachable hill, rather than as a daunting
mountain.
This introduction to Programming in Emacs Lisp has a companion doc-
ument, The GNU Emacs Lisp Reference Manual. The reference manual has
more detail than this introduction. In the reference manual, all the infor-
mation 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 Introduction, you will find the Reference Manual useful when you
are writing your own programs.
" 1 0) (89.99887084960938 204.8603515625 177.66000366210938 219.22088623046875 "Lisp History
" 2 0) (89.999267578125 226.88909912109375 449.4208068847656 337.8672790527344 "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.
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,
‘cl.el’, that adds many Common Lisp features to Emacs Lisp.)
" 3 0) (89.99957275390625 354.2603759765625 226.11309814453125 368.62091064453125 "A Note for Novices
" 4 0) (89.99993896484375 376.4090881347656 449.44329833984375 443.3298034667969 "If you don’t know GNU Emacs, you can still read this document prof-
itably. 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 C-h t. (This means you press and
release the ⟨
" 5 0) (145.54800415039062 427.552001953125 169.30799865722656 428.0320129394531 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (145.55999755859375 428.7624206542969 169.3109130859375 436.7325134277344 "CTRL
" 7 0) (145.54800415039062 436.1919860839844 169.30799865722656 436.6719970703125 "<image: None, width: 1, height: 1, bpc: 1>" 8 1) (89.99853515625 423.80908203125 449.4320983886719 541.0098266601562 "⟩ key and the h at the same time, and then press and release
t.)
Also, I often refer to one of Emacs’ 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: M-C-\\ (indent-region). What this
means is that the indent-region command is customarily invoked by typing
M-C-\\. (You can, if you wish, change the keys that are typed to invoke the
command; this is called rebinding. See Section 16.8, “Keymaps”, page 221.)
The abbreviation M-C-\\ means that you type your ⟨
" 9 0) (344.26800537109375 525.2319946289062 369.5880126953125 525.7119750976562 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (344.2799987792969 526.4424438476562 369.5664978027344 534.41259765625 "META
" 11 0) (344.26800537109375 533.751953125 369.5880126953125 534.23193359375 "<image: None, width: 1, height: 1, bpc: 1>" 12 1) (369.0 521.4890747070312 403.426513671875 541.0098266601562 "⟩ key, ⟨
" 13 0) (402.7080078125 525.2319946289062 426.468017578125 525.7119750976562 "<image: None, width: 1, height: 1, bpc: 1>" 14 1) (402.7200012207031 526.4424438476562 426.4709167480469 534.41259765625 "CTRL
" 15 0) (402.7080078125 533.751953125 426.468017578125 534.23193359375 "<image: None, width: 1, height: 1, bpc: 1>" 16 1) (90.00009155273438 521.4890747070312 449.8581848144531 552.8898315429688 "⟩ key
and ⟨
" 17 0) (114.58800506591797 536.751953125 120.34800720214844 537.23193359375 "<image: None, width: 1, height: 1, bpc: 1>" 18 1) (114.5999984741211 535.5726928710938 120.32727813720703 546.8636474609375 "\\
" 19 0) (114.58800506591797 545.751953125 120.34800720214844 546.23193359375 "<image: None, width: 1, height: 1, bpc: 1>" 20 1) (119.76000213623047 533.3690795898438 422.3863830566406 552.8897705078125 "⟩ key all at the same time. (On many modern keyboards the ⟨
" 21 0) (421.66802978515625 537.1119995117188 446.988037109375 537.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 22 1) (421.67999267578125 538.3223876953125 446.9664001464844 546.2925415039062 "META
" 23 0) (421.66802978515625 545.751953125 446.988037109375 546.23193359375 "<image: None, width: 1, height: 1, bpc: 1>" 24 1) (90.0 536.97314453125 449.9865417480469 564.8897705078125 "⟩
key is labelled ⟨
" 25 0) (161.26800537109375 549.1119995117188 178.42800903320312 549.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 26 1) (161.27999877929688 550.3223876953125 178.3014678955078 558.2925415039062 "ALT
" 27 0) (161.26800537109375 557.6319580078125 178.42800903320312 558.1119384765625 "<image: None, width: 1, height: 1, bpc: 1>" 28 1) (89.99942016601562 545.7290649414062 449.3890380859375 588.769775390625 "⟩.) 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 ⟨
" 29 0) (174.58799743652344 572.991943359375 199.90798950195312 573.471923828125 "<image: None, width: 1, height: 1, bpc: 1>" 30 1) (174.60000610351562 574.202392578125 199.8865203857422 582.1725463867188 "META
" 31 0) (174.58799743652344 581.6319580078125 199.90798950195312 582.1119384765625 "<image: None, width: 1, height: 1, bpc: 1>" 32 1) (199.32000732421875 569.2490844726562 253.54656982421875 588.7698364257812 "⟩ key, the ⟨
" 33 0) (252.8280029296875 572.991943359375 269.50799560546875 573.471923828125 "<image: None, width: 1, height: 1, bpc: 1>" 34 1) (252.83999633789062 574.202392578125 269.3859558105469 582.1725463867188 "ESC
" 35 0) (252.8280029296875 581.6319580078125 269.50799560546875 582.1119384765625 "<image: None, width: 1, height: 1, bpc: 1>" 36 1) (90.00003051757812 569.2490844726562 449.7705993652344 600.7698364257812 "⟩ key prefix is used in place of it. In
this case, M-C-\\ means that you press and release your ⟨
" 37 0) (363.3479919433594 584.991943359375 380.0279846191406 585.471923828125 "<image: None, width: 1, height: 1, bpc: 1>" 38 1) (363.3599853515625 586.202392578125 379.90594482421875 594.1725463867188 "ESC
" 39 0) (363.3479919433594 593.5120239257812 380.0279846191406 593.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 40 1) (90.00039672851562 581.2490844726562 449.8471374511719 612.7698364257812 "⟩ key and then
type the ⟨
" 41 0) (137.7480010986328 596.9920043945312 161.50799560546875 597.4719848632812 "<image: None, width: 1, height: 1, bpc: 1>" 42 1) (137.75999450683594 598.202392578125 161.5109100341797 606.1725463867188 "CTRL
" 43 0) (137.7480010986328 605.5120239257812 161.50799560546875 605.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 44 1) (161.0399932861328 593.2490844726562 234.58642578125 612.7698364257812 "⟩ key and the ⟨
" 45 0) (233.9879913330078 596.5120239257812 239.74798583984375 596.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 46 1) (234.0 595.4526977539062 239.72727966308594 606.74365234375 "\\
" 47 0) (233.9879913330078 605.5120239257812 239.74798583984375 605.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 48 1) (90.00027465820312 593.2490844726562 449.7488708496094 624.6498413085938 "⟩ key at the same time. But usually M-C-\\
means press the ⟨
" 49 0) (172.42800903320312 608.8720092773438 196.18800354003906 609.3519897460938 "<image: None, width: 1, height: 1, bpc: 1>" 50 1) (172.44000244140625 610.0823974609375 196.19091796875 618.0525512695312 "CTRL
" 51 0) (172.42800903320312 617.5120239257812 196.18800354003906 617.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 52 1) (195.60000610351562 605.1290893554688 393.1064147949219 624.6497802734375 "⟩ key along with the key that is labelled ⟨
" 53 0) (392.38800048828125 608.8720092773438 409.5480041503906 609.3519897460938 "<image: None, width: 1, height: 1, bpc: 1>" 54 1) (392.3999938964844 610.0823974609375 409.42144775390625 618.0525512695312 "ALT
" 55 0) (392.38800048828125 617.5120239257812 409.5480041503906 617.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 56 1) (90.00039672851562 605.1290893554688 449.869140625 636.6497802734375 "⟩ and, at
the same time, press the ⟨
" 57 0) (213.10801696777344 620.3919677734375 218.86801147460938 620.8719482421875 "<image: None, width: 1, height: 1, bpc: 1>" 58 1) (213.1199951171875 619.3327026367188 218.84727478027344 630.6236572265625 "\\
" 59 0) (213.10801696777344 629.3919677734375 218.86801147460938 629.8719482421875 "<image: None, width: 1, height: 1, bpc: 1>" 60 1) (218.27999877929688 617.1290893554688 243.59999084472656 636.289794921875 "⟩ key.
" 61 0)) (18 (90.0 47.60907745361328 449.8251953125 60.90726852416992 "xiv
Preface
" 0 0) (89.9986572265625 77.48908233642578 449.4637756347656 194.2097930908203 "In addition to typing a lone keychord, you can prefix what you type with
C-u, which is called the ‘universal argument’. The 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 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.) See
section “Numeric Arguments” in 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, ⟨
" 1 0) (346.3080139160156 178.43194580078125 362.9880065917969 178.9119415283203 "<image: None, width: 1, height: 1, bpc: 1>" 2 1) (346.32000732421875 179.6424102783203 362.865966796875 187.61251831054688 "SPC
" 3 0) (346.3080139160156 187.072021484375 362.9880065917969 187.55201721191406 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (89.9993896484375 175.049072265625 449.90179443359375 238.2672576904297 "⟩. (To learn about
Info, type 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.
" 5 0) (89.99943542480469 254.66033935546875 168.7605743408203 269.0208740234375 "Thank You
" 6 0) (89.99978637695312 276.6890869140625 449.869384765625 352.26727294921875 "My thanks to all who helped me with this book. My especial thanks to
Jim Blandy, Noah Friedman, Jim Kingdon, Roland McGrath, Frank Ritter,
Randy Smith, Richard M. Stallman, and Melissa Weisshaus.
My thanks
also go to both Philip Johnson and David Stampe for their patient encour-
agement. My mistakes are my own.
Robert J. Chassell
" 7 0)) (19 (90.0 47.60907745361328 449.9559631347656 60.90726852416992 "Lisp Atoms
1
" 0 0) (89.9993896484375 75.14844512939453 237.94793701171875 92.38106536865234 "1 List Processing
" 1 0) (89.9981689453125 104.12909698486328 449.4855041503906 189.1873016357422 "To the untutored eye, Lisp is a strange programming language. In Lisp
code there are parentheses everywhere. Some people even claim that the
name stands for ‘Lots of Isolated Silly Parentheses’. But the claim is un-
warranted. Lisp stands for LISt Processing, and the programming language
handles lists (and lists of lists) by putting them between parentheses. The
parentheses mark the boundaries of the list. Sometimes a list is preceded by
a single apostrophe or quotation mark, ‘’’. Lists are the basis of Lisp.
" 2 0) (89.99810791015625 206.18035888671875 186.65130615234375 220.5408935546875 "1.1 Lisp Lists
" 3 0) (89.997314453125 228.4490966796875 449.47412109375 277.6272888183594 "In Lisp, a list looks like this: ’(rose violet daisy buttercup). This
list is preceded by a single apostrophe. It could just as well be written as
follows, which looks more like the kind of list you are likely to be familiar
with:
" 4 0) (89.99658203125 282.6976623535156 449.4080810546875 481.8672790527344 "’(rose
violet
daisy
buttercup)
The elements of this list are the names of the four different flowers, separated
from each other by whitespace and surrounded by parentheses, like flowers
in a field with a stone wall around them.
Lists can also have numbers in them, as in this list: (+ 2 2). This list
has a plus-sign, ‘+’, followed by two ‘2’s, each separated by whitespace.
In Lisp, both data and programs are represented the same way; that is,
they are both lists of words, numbers, or other lists, separated by white-
space and surrounded by parentheses. (Since a program looks like data, one
program may easily serve as data for another; this is a very powerful feature
of Lisp.) (Incidentally, these two parenthetical remarks are not Lisp lists,
because they contain ‘;’ and ‘.’ as punctuation marks.)
Here is another list, this time with a list inside of it:
" 5 0) (89.99627685546875 487.0576477050781 449.39642333984375 534.54736328125 "’(this list has (a list inside of it))
The components of this list are the words ‘this’, ‘list’, ‘has’, and the
list ‘(a list inside of it)’. The interior list is made up of the words ‘a’,
‘list’, ‘inside’, ‘of’, ‘it’.
" 6 0) (89.99640655517578 548.2818603515625 201.3193817138672 561.3858642578125 "1.1.1 Lisp Atoms
" 7 0) (89.99514770507812 569.609130859375 449.3634948730469 630.787353515625 "In Lisp, what we have been calling words are called atoms. This term
comes from the historical meaning of the word atom, which means ‘indivis-
ible’. As far as Lisp is concerned, the words we have been using in the lists
cannot be divided into any smaller parts and still mean the same thing as
part of a program; likewise with numbers and single character symbols like
" 8 0)) (20 (90.0 47.60907745361328 449.8034973144531 60.90726852416992 "2
Chapter 1: List Processing
" 0 0) (90.00003051757812 77.48908233642578 449.2689514160156 102.66727447509766 "‘+’. On the other hand, unlike an atom, a list can be split into parts. (See
Chapter 7, “car cdr & cons Fundamental Functions”, page 81.)
" 1 0) (89.99966430664062 117.68909454345703 449.32330322265625 142.86729431152344 "In a list, atoms are separated from each other by whitespace. They can
be right next to a parenthesis.
" 2 0) (89.9991455078125 157.88909912109375 449.6281433105469 230.9473114013672 "Technically speaking, a list in Lisp consists of parentheses surrounding
atoms separated by whitespace or surrounding other lists or surrounding
both atoms and other lists.
A list can have just one atom in it or have
nothing in it at all. A list with nothing in it looks like this: (), and is called
the empty list. Unlike anything else, an empty list is considered both an
atom and a list at the same time.
" 3 0) (89.99899291992188 245.84912109375 449.39892578125 318.9073181152344 "The printed representation of both atoms and lists are called symbolic
expressions or, more concisely, s-expressions. The word expression by itself
can refer to either the printed representation, or to the atom or list as it
is held internally in the computer. Often, people use the term expression
indiscriminately. (Also, in many texts, the word form is used as a synonym
for expression.)
" 4 0) (89.99847412109375 333.80914306640625 449.4320373535156 430.8673400878906 "Incidentally, the atoms that make up our universe were named such when
they were thought to be indivisible; but it has been found that physical atoms
are not indivisible. Parts can split off an atom or it can fission into two parts
of roughly equal size. Physical atoms were named prematurely, before their
truer nature was found. In Lisp, certain kinds of atom, such as an array, can
be separated into parts; but the mechanism for doing this is different from
the mechanism for splitting a list. As far as list operations are concerned,
the atoms of a list are unsplittable.
" 5 0) (89.99725341796875 445.7691650390625 449.41961669921875 494.9473571777344 "As in English, the meanings of the component letters of a Lisp atom are
different from the meaning the letters make as a word. For example, the
word for the South American sloth, the ‘ai’, is completely different from the
two words, ‘a’, and ‘i’.
" 6 0) (89.99642944335938 509.84918212890625 449.41912841796875 630.7874145507812 "There are many kinds of atom in nature but only a few in Lisp: for
example, numbers, such as 37, 511, or 1729, and symbols, such as ‘+’, ‘foo’,
or ‘forward-line’. The words we have listed in the examples above are
all symbols. In everyday Lisp conversation, the word “atom” is not often
used, because programmers usually try to be more specific about what kind
of atom they are dealing with. Lisp programming is mostly about symbols
(and sometimes numbers) within lists.
(Incidentally, the preceding three
word parenthetical remark is a proper list in Lisp, since it consists of atoms,
which in this case are symbols, separated by whitespace and enclosed by
parentheses, without any non-Lisp punctuation.)
" 7 0)) (21 (90.0 47.60907745361328 449.78131103515625 60.90726852416992 "GNU Emacs Helps You Type Lists
3
" 0 0) (89.99951171875 79.64905548095703 449.333984375 104.9472427368164 "In addition, text between double quotation marks—even sentences or
paragraphs—is an atom. Here is an example:
" 1 0) (89.998046875 110.13748168945312 449.41973876953125 181.5071258544922 "’(this list includes \"text between quotation marks.\")
In Lisp, all of the quoted text including the punctuation mark and the blank
spaces is a single atom. This kind of atom is called a string (for ‘string
of characters’) and is the sort of thing that is used for messages that a
computer can print for a human to read. Strings are a different kind of atom
than numbers or symbols and are used differently.
" 2 0) (89.99826049804688 195.24169921875 254.18441772460938 208.34568786621094 "1.1.2 Whitespace in Lists
" 3 0) (89.99853515625 216.68896484375 449.3758239746094 241.86717224121094 "The amount of whitespace in a list does not matter. From the point of
view of the Lisp language,
" 4 0) (89.9984359741211 247.05752563476562 218.55104064941406 284.2271423339844 "’(this list
looks like this)
is exactly the same as this:
" 5 0) (89.99649047851562 289.4173889160156 449.4737854003906 460.7470397949219 "’(this list looks like this)
Both examples show what to Lisp is the same list, the list made up of
the symbols ‘this’, ‘list’, ‘looks’, ‘like’, and ‘this’ in that order.
Extra whitespace and newlines are designed to make a list more readable
by humans. When Lisp reads the expression, it gets rid of all the extra
whitespace (but it needs to have at least one space between atoms in order
to tell them apart.)
Odd as it seems, the examples we have seen cover almost all of what
Lisp lists look like! Every other list in Lisp looks more or less like one of
these examples, except that the list may be longer and more complex. In
brief, a list is between parentheses, a string is between quotation marks, a
symbol looks like a word, and a number looks like a number. (For certain
situations, square brackets, dots and a few other special characters may be
used; however, we will go quite far without them.)
" 6 0) (89.99827575683594 474.4815979003906 352.74591064453125 487.5856018066406 "1.1.3 GNU Emacs Helps You Type Lists
" 7 0) (89.99734497070312 495.808837890625 449.5176086425781 550.8495483398438 "When you type a Lisp expression in GNU Emacs using either Lisp Interac-
tion mode or Emacs Lisp mode, you have available to you several commands
to format the Lisp expression so it is easy to read. For example, pressing
the ⟨
" 8 0) (112.66800689697266 535.072021484375 130.42800903320312 535.552001953125 "<image: None, width: 1, height: 1, bpc: 1>" 9 1) (112.68000030517578 536.2824096679688 130.4187774658203 544.2525634765625 "TAB
" 10 0) (112.66800689697266 543.5919799804688 130.42800903320312 544.0719604492188 "<image: None, width: 1, height: 1, bpc: 1>" 11 1) (90.00009155273438 531.3291015625 449.35589599609375 630.787353515625 "⟩ key automatically indents the line the cursor is on by the right
amount. A command to properly indent the code in a region is customarily
bound to M-C-\\. Indentation is designed so that you can see which elements
of a list belongs to which list—elements of a sub-list are indented more than
the elements of the enclosing list.
In addition, when you type a closing parenthesis, Emacs momentarily
jumps the cursor back to the matching opening parenthesis, so you can see
which one it is. This is very useful, since every list you type in Lisp must have
" 12 0)) (22 (90.0 47.60907745361328 449.8034973144531 60.90726852416992 "4
Chapter 1: List Processing
" 0 0) (90.00039672851562 77.48908233642578 449.3244323730469 114.66727447509766 "its closing parenthesis match its opening parenthesis. (See section “Major
Modes” in The GNU Emacs Manual, for more information about Emacs’
modes.)
" 1 0) (90.001220703125 131.30035400390625 228.78074645996094 145.660888671875 "1.2 Run a Program
" 2 0) (90.00100708007812 153.4490966796875 449.4663391113281 350.236083984375 "A list in Lisp—any list—is a program ready to run. If you run it (for
which the Lisp jargon is evaluate), the computer will do one of three things:
do nothing except return to you the list itself; send you an error message; or,
treat the first symbol in the list as a command to do something. (Usually,
of course, it is the last of these three things that you really want!)
The single apostrophe, ’, that I put in front of some of the example lists
in preceding sections is called a quote; when it precedes a list, it tells Lisp
to do nothing with the list, other than take it as it is written. But if there is
no quote preceding a list, the first item of the list is special: it is a command
for the computer to obey. (In Lisp, these commands are called functions.)
The list (+ 2 2) shown above did not have a quote in front of it, so Lisp
understands that the + is an instruction to do something with the rest of the
list: add the numbers that follow.
If you are reading this inside of GNU Emacs in Info, here is how you
can evaluate such a list: place your cursor immediately after the right hand
parenthesis of the following list and then type C-x C-e:
" 3 0) (90.00213623046875 355.297607421875 449.45660400390625 414.67608642578125 "(+ 2 2)
You will see the number 4 appear in the echo area. (In the jargon, what you
have just done is “evaluate the list.” The echo area is the line at the bottom
of the screen that displays or “echoes” text.) Now try the same thing with a
quoted list: place the cursor right after the following list and type C-x C-e:
" 4 0) (89.99981689453125 419.61761474609375 449.44403076171875 566.7073364257812 "’(this is a quoted list)
You will see (this is a quoted list) appear in the echo area.
In both cases, what you are doing is giving a command to the program
inside of GNU Emacs called the Lisp interpreter—giving the interpreter a
command to evaluate the expression. The name of the Lisp interpreter comes
from the word for the task done by a human who comes up with the meaning
of an expression—who “interprets” it.
You can also evaluate an atom that is not part of a list—one that is not
surrounded by parentheses; again, the Lisp interpreter translates from the
humanly readable expression to the language of the computer. But before
discussing this (see Section 1.7, “Variables”, page 10), we will discuss what
the Lisp interpreter does when you make an error.
" 5 0) (90.00013732910156 583.3403930664062 312.9887390136719 597.700927734375 "1.3 Generate an Error Message
" 6 0) (90.00048828125 605.4891357421875 449.4440612792969 630.787353515625 "Partly so you won’t worry if you do it accidentally, we will now give a
command to the Lisp interpreter that generates an error message. This is a
" 7 0)) (23 (90.0 47.60907745361328 449.8147888183594 60.90726852416992 "Generate an Error Message
5
" 0 0) (89.99969482421875 77.48908233642578 449.3667907714844 138.5472869873047 "harmless activity; and indeed, we will often try to generate error messages
intentionally. Once you understand the jargon, error messages can be in-
formative. Instead of being called “error” messages, they should be called
“help” messages. They are like signposts to a traveller in a strange country;
deciphering them can be hard, but once understood, they can point the way.
" 1 0) (89.99981689453125 141.80908203125 449.3234558105469 167.10728454589844 "The error message is generated by a built-in GNU Emacs debugger. We
will ‘enter the debugger’. You get out of the debugger by typing q.
" 2 0) (89.99853515625 170.24908447265625 449.3768005371094 219.43609619140625 "What we will do is evaluate a list that is not quoted and does not have
a meaningful command as its first element. Here is a list almost exactly the
same as the one we just used, but without the single-quote in front of it.
Position the cursor right after it and type C-x C-e:
" 3 0) (111.59806823730469 227.01760864257812 233.72633361816406 235.98399353027344 "(this is an unquoted list)
" 4 0) (89.9980697631836 239.72906494140625 449.3874206542969 288.9072570800781 "What you see depends on which version of Emacs you are running. GNU
Emacs version 21 provides more information than version 20 and before.
First, the more recent result of generating an error; then the earlier, version
20 result.
" 5 0) (89.998046875 292.1690673828125 449.3435974121094 317.4672546386719 "In GNU Emacs version 21, a ‘*Backtrace*’ window will open up and you
will see the following in it:
" 6 0) (111.59724426269531 325.0576171875 346.4422912597656 421.1441345214844 "---------- Buffer: *Backtrace* ----------
Debugger entered--Lisp error: (void-function this)
(this is an unquoted list)
eval((this is an unquoted list))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp)
---------- Buffer: *Backtrace* ----------
" 7 0) (89.99765014648438 426.689208984375 449.3754577636719 463.9873962402344 "Your cursor will be in this window (you may have to wait a few seconds
before it becomes visible). To quit the debugger and make the debugger
window go away, type:
" 8 0) (111.5979995727539 471.5777587890625 116.30535125732422 480.54412841796875 "q
" 9 0) (89.99800109863281 484.1692199707031 449.3653259277344 509.4762268066406 "Please type q right now, so you become confident that you can get out of
the debugger. Then, type C-x C-e again to re-enter it.
" 10 0) (104.99765014648438 512.729248046875 449.299072265625 526.0274658203125 "Based on what we already know, we can almost read this error message.
" 11 0) (89.99639892578125 529.169189453125 449.42919921875 590.347412109375 "You read the ‘*Backtrace*’ buffer from the bottom up; it tells you what
Emacs did. When you typed C-x C-e, you made an interactive call to the
command eval-last-sexp. eval is an abbreviation for ‘evaluate’ and sexp
is an abbreviation for ‘symbolic expression’. The command means ‘evaluate
last symbolic expression’, which is the expression just before your cursor.
" 12 0) (89.99649047851562 593.6092529296875 449.75628662109375 630.7874755859375 "Each line above tells you what the Lisp interpreter evaluated next. The
most recent action is at the top. The buffer is called the ‘*Backtrace*’
buffer because it enables you to track Emacs backwards.
" 13 0)) (24 (90.0 47.60907745361328 449.8034973144531 60.90726852416992 "6
Chapter 1: List Processing
" 0 0) (105.00039672851562 79.88904571533203 373.5820617675781 93.1872329711914 "At the top of the ‘*Backtrace*’ buffer, you see the line:
" 1 0) (89.99893188476562 98.61758422851562 449.7381286621094 363.90740966796875 "Debugger entered--Lisp error: (void-function this)
The Lisp interpreter tried to evaluate the first atom of the list, the word
‘this’. It is this action that generated the error message ‘void-function
this’.
The message contains the words ‘void-function’ and ‘this’.
The word ‘function’ was mentioned once before. It is a very important
word. For our purposes, we can define it by saying that a function is a set
of instructions to the computer that tell the computer to do something.
Now we can begin to understand the error message: ‘void-function
this’. The function (that is, the word ‘this’) does not have a definition of
any set of instructions for the computer to carry out.
The slightly odd word, ‘void-function’, is designed to cover the way
Emacs Lisp is implemented, which is that when a symbol does not have a
function definition attached to it, the place that should contain the instruc-
tions is ‘void’.
On the other hand, since we were able to add 2 plus 2 successfully, by
evaluating (+ 2 2), we can infer that the symbol + must have a set of in-
structions for the computer to obey and those instructions must be to add
the numbers that follow the +.
In GNU Emacs version 20, and in earlier versions, you will see only one
line of error message; it will appear in the echo area and look like this:
" 2 0) (89.99703979492188 369.2177734375 449.7022399902344 517.2674560546875 "Symbol’s function definition is void: this
(Also, your terminal may beep at you—some do, some don’t; and others
blink. This is just a device to get your attention.) The message goes away
as soon as you type another key, even just to move the cursor.
We know the meaning of the word ‘Symbol’. It refers to the first atom of
the list, the word ‘this’. The word ‘function’ refers to the instructions that
tell the computer what to do. (Technically, the symbol tells the computer
where to find the instructions, but this is a complication we can ignore for
the moment.)
The error message can be understood: ‘Symbol’s function definition
is void: this’. The symbol (that is, the word ‘this’) lacks instructions for
the computer to carry out.
" 3 0) (89.99691772460938 535.2205200195312 403.7975158691406 549.5810546875 "1.4 Symbol Names and Function Definitions
" 4 0) (89.996826171875 557.729248046875 449.39691162109375 630.7874755859375 "We can articulate another characteristic of Lisp based on what we have
discussed so far—an important characteristic: a symbol, like +, is not itself
the set of instructions for the computer to carry out. Instead, the symbol
is used, perhaps temporarily, as a way of locating the definition or set of
instructions. What we see is the name through which the instructions can
be found. Names of people work the same way.
I can be referred to as
" 5 0)) (25 (90.0 47.60907745361328 449.8582458496094 60.90726852416992 "The Lisp Interpreter
7
" 0 0) (89.99691772460938 77.48908233642578 449.4644775390625 305.9473571777344 "‘Bob’; however, I am not the letters ‘B’, ‘o’, ‘b’ but am the consciousness
consistently associated with a particular life-form. The name is not me, but
it can be used to refer to me.
In Lisp, one set of instructions can be attached to several names. For
example, the computer instructions for adding numbers can be linked to the
symbol plus as well as to the symbol + (and are in some dialects of Lisp).
Among humans, I can be referred to as ‘Robert’ as well as ‘Bob’ and by
other words as well.
On the other hand, a symbol can have only one function definition at-
tached to it at a time. Otherwise, the computer would be confused as to
which definition to use. If this were the case among people, only one person
in the world could be named ‘Bob’. However, the function definition to which
the name refers can be changed readily. (See Section 3.2, “Install a Function
Definition”, page 31.)
Since Emacs Lisp is large, it is customary to name symbols in a way that
identifies the part of Emacs to which the function belongs. Thus, all the
names for functions that deal with Texinfo start with ‘texinfo-’ and those
for functions that deal with reading mail start with ‘rmail-’.
" 1 0) (89.99710083007812 330.38043212890625 264.1138916015625 344.740966796875 "1.5 The Lisp Interpreter
" 2 0) (89.99496459960938 354.44915771484375 449.4619140625 630.787353515625 "Based on what we have seen, we can now start to figure out what the Lisp
interpreter does when we command it to evaluate a list. First, it looks to see
whether there is a quote before the list; if there is, the interpreter just gives
us the list. On the other hand, if there is no quote, the interpreter looks at
the first element in the list and sees whether it has a function definition. If
it does, the interpreter carries out the instructions in the function definition.
Otherwise, the interpreter prints an error message.
This is how Lisp works. Simple. There are added complications which
we will get to in a minute, but these are the fundamentals. Of course, to
write Lisp programs, you need to know how to write function definitions and
attach them to names, and how to do this without confusing either yourself
or the computer.
Now, for the first complication. In addition to lists, the Lisp interpreter
can evaluate a symbol that is not quoted and does not have parentheses
around it.
The Lisp interpreter will attempt to determine the symbol’s
value as a variable. This situation is described in the section on variables.
(See Section 1.7, “Variables”, page 10.)
The second complication occurs because some functions are unusual and
do not work in the usual manner. Those that don’t are called special forms.
They are used for special jobs, like defining a function, and there are not
many of them. In the next few chapters, you will be introduced to several
of the more important special forms.
" 3 0)) (26 (90.0 47.60907745361328 449.8034973144531 60.90726852416992 "8
Chapter 1: List Processing
" 0 0) (89.99853515625 77.48908233642578 449.49615478515625 213.4272918701172 "The third and final complication is this: if the function that the Lisp
interpreter is looking at is not a special form, and if it is part of a list, the
Lisp interpreter looks to see whether the list has a list inside of it. If there
is an inner list, the Lisp interpreter first figures out what it should do with
the inside list, and then it works on the outside list. If there is yet another
list embedded inside the inner list, it works on that one first, and so on.
It always works on the innermost list first. The interpreter works on the
innermost list first, to evaluate the result of that list. The result may be
used by the enclosing expression.
Otherwise, the interpreter works left to right, from one expression to the
next.
" 1 0) (89.99853515625 229.9217529296875 229.24867248535156 243.02574157714844 "1.5.1 Byte Compiling
" 2 0) (89.99526977539062 252.208984375 449.4936218261719 427.0271911621094 "One other aspect of interpreting: the Lisp interpreter is able to interpret
two kinds of entity: humanly readable code, on which we will focus exclu-
sively, and specially processed code, called byte compiled code, which is not
humanly readable. Byte compiled code runs faster than humanly readable
code.
You can transform humanly readable code into byte compiled code by
running one of the compile commands such as byte-compile-file. Byte
compiled code is usually stored in a file that ends with a ‘.elc’ exten-
sion rather than a ‘.el’ extension. You will see both kinds of file in the
‘emacs/lisp’ directory; the files to read are those with ‘.el’ extensions.
As a practical matter, for most things you might do to customize or
extend Emacs, you do not need to byte compile; and I will not discuss
the topic here. See section “Byte Compilation” in The GNU Emacs Lisp
Reference Manual, for a full description of byte compilation.
" 3 0) (89.99472045898438 447.74029541015625 193.54904174804688 462.100830078125 "1.6 Evaluation
" 4 0) (89.99298095703125 470.9690246582031 449.40380859375 630.7872314453125 "When the Lisp interpreter works on an expression, the term for the ac-
tivity is called evaluation. We say that the interpreter ‘evaluates the expres-
sion’. I’ve used this term several times before. The word comes from its
use in everyday language, ‘to ascertain the value or amount of; to appraise’,
according to Webster’s New Collegiate Dictionary.
After evaluating an expression, the Lisp interpreter will most likely return
the value that the computer produces by carrying out the instructions it
found in the function definition, or perhaps it will give up on that function
and produce an error message. (The interpreter may also find itself tossed,
so to speak, to a different function or it may attempt to repeat continually
what it is doing for ever and ever in what is called an ‘infinite loop’. These
actions are less common; and we can ignore them.) Most frequently, the
interpreter returns a value.
" 5 0)) (27 (90.0 47.60907745361328 449.83587646484375 60.90726852416992 "Evaluating Inner Lists
9
" 0 0) (89.99923706054688 77.48908233642578 449.3990783691406 188.58726501464844 "At the same time the interpreter returns a value, it may do something
else as well, such as move a cursor or copy a file; this other kind of action
is called a side effect. Actions that we humans think are important, such as
printing results, are often “side effects” to the Lisp interpreter. The jargon
can sound peculiar, but it turns out that it is fairly easy to learn to use side
effects.
In summary, evaluating a symbolic expression most commonly causes the
Lisp interpreter to return a value and perhaps carry out a side effect; or else
produce an error.
" 1 0) (89.99913024902344 202.56182861328125 270.7168884277344 215.6658172607422 "1.6.1 Evaluating Inner Lists
" 2 0) (89.99765014648438 223.88909912109375 449.3211669921875 299.2362060546875 "If evaluation applies to a list that is inside another list, the outer list may
use the value returned by the first evaluation as information when the outer
list is evaluated. This explains why inner expressions are evaluated first: the
values they return are used by the outer expressions.
We can investigate this process by evaluating another addition example.
Place your cursor after the following expression and type C-x C-e:
" 3 0) (89.99594116210938 304.4176330566406 449.461181640625 492.1872863769531 "(+ 2 (+ 3 3))
The number 8 will appear in the echo area.
What happens is that the Lisp interpreter first evaluates the inner ex-
pression, (+ 3 3), for which the value 6 is returned; then it evaluates the
outer expression as if it were written (+ 2 6), which returns the value 8.
Since there are no more enclosing expressions to evaluate, the interpreter
prints that value in the echo area.
Now it is easy to understand the name of the command invoked by the
keystrokes C-x C-e: the name is eval-last-sexp.
The letters sexp are
an abbreviation for ‘symbolic expression’, and eval is an abbreviation for
‘evaluate’. The command means ‘evaluate last symbolic expression’.
As an experiment, you can try evaluating the expression by putting the
cursor at the beginning of the next line immediately following the expression,
or inside the expression.
Here is another copy of the expression:
" 4 0) (89.99530029296875 497.3776550292969 449.4059143066406 630.787353515625 "(+ 2 (+ 3 3))
If you place the cursor at the beginning of the blank line that immediately
follows the expression and type C-x C-e, you will still get the value 8 printed
in the echo area. Now try putting the cursor inside the expression. If you
put it right after the next to last parenthesis (so it appears to sit on top
of the last parenthesis), you will get a 6 printed in the echo area! This is
because the command evaluates the expression (+ 3 3).
Now put the cursor immediately after a number. Type C-x C-e and you
will get the number itself. In Lisp, if you evaluate a number, you get the
number itself—this is how numbers differ from symbols. If you evaluate a
list starting with a symbol like +, you will get a value returned that is the
" 5 0)) (28 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "10
Chapter 1: List Processing
" 0 0) (89.999267578125 77.48908233642578 449.2245178222656 114.66727447509766 "result of the computer carrying out the instructions in the function definition
attached to that name. If a symbol by itself is evaluated, something different
happens, as we will see in the next section.
" 1 0) (89.9996337890625 137.54034423828125 183.30419921875 151.90087890625 "1.7 Variables
" 2 0) (89.9949951171875 161.24908447265625 449.5032958984375 436.5160827636719 "In Emacs Lisp, a symbol can have a value attached to it just as it can
have a function definition attached to it. The two are different. The function
definition is a set of instructions that a computer will obey. A value, on the
other hand, is something, such as number or a name, that can vary (which
is why such a symbol is called a variable). The value of a symbol can be any
expression in Lisp, such as a symbol, number, list, or string. A symbol that
has a value is often called a variable.
A symbol can have both a function definition and a value attached to it
at the same time. Or it can have just one or the other. The two are separate.
This is somewhat similar to the way the name Cambridge can refer to the
city in Massachusetts and have some information attached to the name as
well, such as “great programming center”.
Another way to think about this is to imagine a symbol as being a chest of
drawers. The function definition is put in one drawer, the value in another,
and so on. What is put in the drawer holding the value can be changed
without affecting the contents of the drawer holding the function definition,
and vice-versa.
The variable fill-column illustrates a symbol with a value attached to
it: in every GNU Emacs buffer, this symbol is set to some value, usually 72
or 70, but sometimes to some other value. To find the value of this symbol,
evaluate it by itself. If you are reading this in Info inside of GNU Emacs,
you can do this by putting the cursor after the symbol and typing C-x C-e:
" 3 0) (89.9940185546875 443.1376037597656 449.47039794921875 630.787353515625 "fill-column
After I typed C-x C-e, Emacs printed the number 72 in my echo area. This
is the value for which fill-column is set for me as I write this. It may
be different for you in your Info buffer. Notice that the value returned as
a variable is printed in exactly the same way as the value returned by a
function carrying out its instructions. From the point of view of the Lisp
interpreter, a value returned is a value returned. What kind of expression it
came from ceases to matter once the value is known.
A symbol can have any value attached to it or, to use the jargon, we can
bind the variable to a value: to a number, such as 72; to a string, \"such as
this\"; to a list, such as (spruce pine oak); we can even bind a variable to
a function definition.
A symbol can be bound to a value in several ways.
See Section 1.9,
“Setting the Value of a Variable”, page 17, for information about one way
to do this.
" 4 0)) (29 (90.0 47.60907745361328 449.5411682128906 60.90726852416992 "Error Message for a Symbol Without a Value
11
" 0 0) (89.99951171875 78.2418212890625 441.228759765625 91.34580993652344 "1.7.1 Error Message for a Symbol Without a Function
" 1 0) (89.99884033203125 99.68909454345703 449.4649353027344 175.02732849121094 "When we evaluated fill-column to find its value as a variable, we did
not place parentheses around the word. This is because we did not intend
to use it as a function name.
If fill-column were the first or only element of a list, the Lisp interpreter
would attempt to find the function definition attached to it.
But fill-
column has no function definition. Try evaluating this:
" 2 0) (89.99842071533203 180.33767700195312 449.25616455078125 204.06736755371094 "(fill-column)
In GNU Emacs version 21, you will create a ‘*Backtrace*’ buffer that says:
" 3 0) (89.99859619140625 209.37771606445312 449.3437194824219 359.34735107421875 "---------- Buffer: *Backtrace* ----------
Debugger entered--Lisp error: (void-function fill-column)
(fill-column)
eval((fill-column))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp)
---------- Buffer: *Backtrace* ----------
(Remember, to quit the debugger and make the debugger window go away,
type q in the ‘*Backtrace*’ buffer.)
In GNU Emacs 20 and before, you will produce an error message that
says:
" 4 0) (89.99845886230469 364.65771484375 449.4523620605469 400.2673645019531 "Symbol’s function definition is void: fill-column
(The message will go away away as soon as you move the cursor or type
another key.)
" 5 0) (89.99870300292969 414.6019287109375 421.0548095703125 427.7059326171875 "1.7.2 Error Message for a Symbol Without a Value
" 6 0) (89.99868774414062 436.0491943359375 449.5945739746094 485.2362060546875 "If you attempt to evaluate a symbol that does not have a value bound
to it, you will receive an error message. You can see this by experimenting
with our 2 plus 2 addition. In the following expression, put your cursor right
after the +, before the first number 2, type C-x C-e:
" 7 0) (89.99947357177734 490.4177551269531 416.4536437988281 514.2673950195312 "(+ 2 2)
In GNU Emacs 21, you will create a ‘*Backtrace*’ buffer that says:
" 8 0) (89.99969482421875 519.5778198242188 449.3229675292969 630.7874755859375 "---------- Buffer: *Backtrace* ----------
Debugger entered--Lisp error: (void-variable +)
eval(+)
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp)
---------- Buffer: *Backtrace* ----------
(As with the other times we entered the debugger, you can quit by typing q
in the ‘*Backtrace*’ buffer.)
" 9 0)) (30 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "12
Chapter 1: List Processing
" 0 0) (89.99711608886719 77.48908233642578 449.40985107421875 275.7072448730469 "This backtrace is different from the very first error message we saw,
which said, ‘Debugger entered--Lisp error: (void-function this)’. In
this case, the function does not have a value as a variable; while in the other
error message, the function (the word ‘this’) did not have a definition.
In this experiment with the +, what we did was cause the Lisp interpreter
to evaluate the + and look for the value of the variable instead of the function
definition. We did this by placing the cursor right after the symbol rather
than after the parenthesis of the enclosing list as we did before. As a conse-
quence, the Lisp interpreter evaluated the preceding s-expression, which in
this case was the + by itself.
Since + does not have a value bound to it, just the function definition,
the error message reported that the symbol’s value as a variable was void.
In GNU Emacs version 20 and before, your error message will say:
Symbol’s value as variable is void: +
The meaning is the same as in GNU Emacs 21.
" 1 0) (89.99661254882812 297.7403564453125 196.37869262695312 312.10089111328125 "1.8 Arguments
" 2 0) (89.9964599609375 321.2090759277344 449.287109375 346.50726318359375 "To see how information is passed to functions, let’s look again at our old
standby, the addition of two plus two. In Lisp, this is written as follows:
" 3 0) (89.99591064453125 352.8976135253906 449.4605712890625 503.9472351074219 "(+ 2 2)
If you evaluate this expression, the number 4 will appear in your echo
area. What the Lisp interpreter does is add the numbers that follow the +.
The numbers added by + are called the arguments of the function +.
These numbers are the information that is given to or passed to the function.
The word ‘argument’ comes from the way it is used in mathematics and
does not refer to a disputation between two people; instead it refers to the
information presented to the function, in this case, to the +. In Lisp, the
arguments to a function are the atoms or lists that follow the function. The
values returned by the evaluation of these atoms or lists are passed to the
function. Different functions require different numbers of arguments; some
functions require none at all.1
" 4 0) (89.98799896240234 517.9119873046875 233.98800659179688 518.3919677734375 "<image: None, width: 1, height: 1, bpc: 1>" 5 1) (95.87999725341797 518.8551635742188 449.8175354003906 630.3016357421875 "1 It is curious to track the path by which the word ‘argument’ came to have two differ-
ent meanings, one in mathematics and the other in everyday English. According to
the Oxford English Dictionary, the word derives from the Latin for ‘to make clear,
prove’; thus it came to mean, by one thread of derivation, ‘the evidence offered as
proof’, which is to say, ‘the information offered’, which led to its meaning in Lisp.
But in the other thread of derivation, it came to mean ‘to assert in a manner against
which others may make counter assertions’, which led to the meaning of the word as a
disputation. (Note here that the English word has two different definitions attached to
it at the same time. By contrast, in Emacs Lisp, a symbol cannot have two different
function definitions at the same time.)
" 6 0)) (31 (90.0 47.60907745361328 449.5195007324219 60.90726852416992 "An Argument as the Value of a Variable or List
13
" 0 0) (89.99984741210938 78.2418212890625 282.3642272949219 91.34580993652344 "1.8.1 Arguments’ Data Types
" 1 0) (89.99920654296875 99.56909942626953 449.4541320800781 198.6672821044922 "The type of data that should be passed to a function depends on what
kind of information it uses. The arguments to a function such as + must
have values that are numbers, since + adds numbers. Other functions use
different kinds of data for their arguments.
For example, the concat function links together or unites two or more
strings of text to produce a string. The arguments are strings. Concate-
nating the two character strings abc, def produces the single string abcdef.
This can be seen by evaluating the following:
" 2 0) (89.99880981445312 203.73764038085938 449.5189208984375 327.3072204589844 "(concat \"abc\" \"def\")
The value produced by evaluating this expression is \"abcdef\".
A function such as substring uses both a string and numbers as argu-
ments. The function returns a part of the string, a substring of the first
argument. This function takes three arguments. Its first argument is the
string of characters, the second and third arguments are numbers that in-
dicate the beginning and end of the substring. The numbers are a count
of the number of characters (including spaces and punctuations) from the
beginning of the string.
For example, if you evaluate the following:
" 3 0) (89.99746704101562 332.3775634765625 449.5065002441406 453.7873229980469 "(substring \"The quick brown fox jumped.\" 16 19)
you will see \"fox\" appear in the echo area. The arguments are the string
and the two numbers.
Note that the string passed to substring is a single atom even though
it is made up of several words separated by spaces. Lisp counts everything
between the two quotation marks as part of the string, including the spaces.
You can think of the substring function as a kind of ‘atom smasher’ since it
takes an otherwise indivisible atom and extracts a part. However, substring
is only able to extract a substring from an argument that is a string, not
from another type of atom such as a number or symbol.
" 4 0) (89.99795532226562 467.5218811035156 435.76593017578125 480.6258850097656 "1.8.2 An Argument as the Value of a Variable or List
" 5 0) (89.99832153320312 488.84912109375 449.4090576171875 540.1961669921875 "An argument can be a symbol that returns a value when it is evaluated.
For example, when the symbol fill-column by itself is evaluated, it returns
a number. This number can be used in an addition.
Position the cursor after the following expression and type C-x C-e:
" 6 0) (89.9979248046875 545.3777465820312 449.3211669921875 630.787353515625 "(+ 2 fill-column)
The value will be a number two more than what you get by evaluating
fill-column alone. For me, this is 74, because the value of fill-column is
72.
As we have just seen, an argument can be a symbol that returns a value
when evaluated. In addition, an argument can be a list that returns a value
when it is evaluated. For example, in the following expression, the arguments
" 7 0)) (32 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "14
Chapter 1: List Processing
" 0 0) (89.99893188476562 77.48908233642578 449.4317321777344 102.66727447509766 "to the function concat are the strings \"The \" and \" red foxes.\" and the
list (number-to-string (+ 2 fill-column)).
" 1 0) (89.9990234375 108.93759155273438 449.813720703125 193.3872528076172 "(concat \"The \" (number-to-string (+ 2 fill-column)) \" red foxes.\")
If you evaluate this expression—and if, as with my Emacs, fill-column
evaluates to 72—\"The 74 red foxes.\" will appear in the echo area. (Note
that you must put spaces after the word ‘The’ and before the word ‘red’
so they will appear in the final string.
The function number-to-string
converts the integer that the addition function returns to a string. number-
to-string is also known as int-to-string.)
" 2 0) (89.99903869628906 210.601806640625 331.18389892578125 223.70579528808594 "1.8.3 Variable Number of Arguments
" 3 0) (89.99853515625 233.009033203125 449.4313049316406 309.4272155761719 "Some functions, such as concat, + or *, take any number of arguments.
(The * is the symbol for multiplication.) This can be seen by evaluating
each of the following expressions in the usual way. What you will see in the
echo area is printed in this text after ‘⇒’, which you may read as ‘evaluates
to’.
In the first set, the functions have no arguments:
" 4 0) (111.59883117675781 314.8530578613281 177.5061798095703 330.40972900390625 "(+)
⇒ 0
" 5 0) (111.59883117675781 339.8130187988281 177.5061798095703 355.36968994140625 "(*)
⇒ 1
" 6 0) (104.99882507324219 353.2489929199219 343.0240783691406 366.54718017578125 "In this set, the functions have one argument each:
" 7 0) (111.59925842285156 371.9730224609375 177.50660705566406 387.5296936035156 "(+ 3)
⇒ 3
" 8 0) (111.59925842285156 396.8130187988281 177.50660705566406 412.36968994140625 "(* 3)
⇒ 3
" 9 0) (104.99925231933594 410.3689880371094 355.2318115234375 423.66717529296875 "In this set, the functions have three arguments each:
" 10 0) (111.5989990234375 429.093017578125 182.17779541015625 445.24969482421875 "(+ 3 4 5) ⇒ 12
" 11 0) (111.59896850585938 453.9330139160156 182.17776489257812 470.0896911621094 "(* 3 4 5) ⇒ 60
" 12 0) (89.99893188476562 480.4817199707031 431.9792785644531 493.5857238769531 "1.8.4 Using the Wrong Type Object as an Argument
" 13 0) (89.99838256835938 502.88897705078125 449.4961242675781 563.9559936523438 "When a function is passed an argument of the wrong type, the Lisp
interpreter produces an error message. For example, the + function expects
the values of its arguments to be numbers. As an experiment we can pass it
the quoted symbol hello instead of a number. Position the cursor after the
following expression and type C-x C-e:
" 14 0) (89.99658203125 570.2175903320312 449.3319091796875 630.7872314453125 "(+ 2 ’hello)
When you do this you will generate an error message. What has happened is
that + has tried to add the 2 to the value returned by ’hello, but the value
returned by ’hello is the symbol hello, not a number. Only numbers can
be added. So + could not carry out its addition.
" 15 0)) (33 (90.0 47.60907745361328 449.54150390625 60.90726852416992 "Using the Wrong Type Object as an Argument
15
" 0 0) (89.99951171875 85.52906036376953 449.80322265625 110.8272476196289 "In GNU Emacs version 21, you will create and enter a ‘*Backtrace*’
buffer that says:
" 1 0) (111.59939575195312 121.77761840820312 369.980712890625 230.46385192871094 "---------- Buffer: *Backtrace* ----------
Debugger entered--Lisp error:
(wrong-type-argument number-or-marker-p hello)
+(2 hello)
eval((+ 2 (quote hello)))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp)
---------- Buffer: *Backtrace* ----------
" 2 0) (90.00003051757812 238.7689208984375 449.30157470703125 264.0671081542969 "As usual, the error message tries to be helpful and makes sense after you
learn how to read it.
" 3 0) (89.99972534179688 270.68890380859375 449.4212341308594 307.8670959472656 "The first part of the error message is straightforward; it says ‘wrong type
argument’. Next comes the mysterious jargon word ‘number-or-marker-p’.
This word is trying to tell you what kind of argument the + expected.
" 4 0) (89.99972534179688 314.60888671875 449.43310546875 405.0495910644531 "The symbol number-or-marker-p says that the Lisp interpreter is try-
ing to determine whether the information presented it (the value of the
argument) is a number or a marker (a special object representing a buffer
position). What it does is test to see whether the + is being given numbers to
add. It also tests to see whether the argument is something called a marker,
which is a specific feature of Emacs Lisp. (In Emacs, locations in a buffer
are recorded as markers.
When the mark is set with the C-@ or C-⟨
" 5 0) (430.3080139160156 389.6319885253906 446.9880065917969 390.11199951171875 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (430.32000732421875 390.8424377441406 446.865966796875 398.8125305175781 "SPC
" 7 0) (430.3080139160156 398.2720031738281 446.9880065917969 398.75201416015625 "<image: None, width: 1, height: 1, bpc: 1>" 8 1) (89.99850463867188 389.49310302734375 449.9865417480469 447.42724609375 "⟩
command, its position is kept as a marker. The mark can be considered a
number—the number of characters the location is from the beginning of the
buffer.) In Emacs Lisp, + can be used to add the numeric value of marker
positions as numbers.
" 9 0) (89.99911499023438 454.049072265625 449.3556213378906 562.9873046875 "The ‘p’ of number-or-marker-p is the embodiment of a practice started
in the early days of Lisp programming. The ‘p’ stands for ‘predicate’. In the
jargon used by the early Lisp researchers, a predicate refers to a function
to determine whether some property is true or false. So the ‘p’ tells us that
number-or-marker-p is the name of a function that determines whether it
is true or false that the argument supplied is a number or a marker. Other
Lisp symbols that end in ‘p’ include zerop, a function that tests whether its
argument has the value of zero, and listp, a function that tests whether its
argument is a list.
" 10 0) (89.99859619140625 569.609130859375 449.3768005371094 630.787353515625 "Finally, the last part of the error message is the symbol hello. This is the
value of the argument that was passed to +. If the addition had been passed
the correct type of object, the value passed would have been a number, such
as 37, rather than a symbol like hello. But then you would not have got
the error message.
" 11 0)) (34 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "16
Chapter 1: List Processing
" 0 0) (89.99951171875 79.52906036376953 449.3662414550781 104.8272476196289 "In GNU Emacs version 20 and before, the echo area displays an error
message that says:
" 1 0) (89.99954223632812 109.77761840820312 449.72625732421875 145.1472625732422 "Wrong type argument: number-or-marker-p, hello
This says, in different words, the same as the top line of the ‘*Backtrace*’
buffer.
" 2 0) (89.99954223632812 157.92181396484375 265.9501037597656 171.03736877441406 "1.8.5 The message Function
" 3 0) (89.99853515625 179.00909423828125 449.47509765625 242.22727966308594 "Like +, the message function takes a variable number of arguments. It is
used to send messages to the user and is so useful that we will describe it
here.
A message is printed in the echo area.
For example, you can print a
message in your echo area by evaluating the following list:
" 4 0) (89.99856567382812 247.29763793945312 449.5411376953125 418.2760009765625 "(message \"This message appears in the echo area!\")
The whole string between double quotation marks is a single argument
and is printed in toto. (Note that in this example, the message itself will ap-
pear in the echo area within double quotes; that is because you see the value
returned by the message function. In most uses of message in programs that
you write, the text will be printed in the echo area as a side-effect, without
the quotes. See Section 3.3.1, “multiply-by-seven in detail”, page 34, for
an example of this.)
However, if there is a ‘%s’ in the quoted string of characters, the message
function does not print the ‘%s’ as such, but looks to the argument that
follows the string. It evaluates the second argument and prints the value at
the location in the string where the ‘%s’ is.
You can see this by positioning the cursor after the following expression
and typing C-x C-e:
" 5 0) (89.9979248046875 423.217529296875 449.4093017578125 508.38720703125 "(message \"The name of this buffer is: %s.\" (buffer-name))
In Info, \"The name of this buffer is: *info*.\" will appear in the echo
area. The function buffer-name returns the name of the buffer as a string,
which the message function inserts in place of %s.
To print a value as an integer, use ‘%d’ in the same way as ‘%s’. For
example, to print a message in the echo area that states the value of the
fill-column, evaluate the following:
" 6 0) (89.9979248046875 513.3375854492188 449.5290832519531 598.5072631835938 "(message \"The value of fill-column is %d.\" fill-column)
On my system, when I evaluate this list, \"The value of fill-column is
72.\" appears in my echo area2.
If there is more than one ‘%s’ in the quoted string, the value of the first
argument following the quoted string is printed at the location of the first
‘%s’ and the value of the second argument is printed at the location of the
second ‘%s’, and so on.
" 7 0) (89.98799896240234 605.5120239257812 233.98800659179688 605.9920043945312 "<image: None, width: 1, height: 1, bpc: 1>" 8 1) (95.87999725341797 606.5751953125 449.7224426269531 630.3015747070312 "2 Actually, you can use %s to print a number. It is non-specific. %d prints only the part
of a number left of a decimal point, and not anything that is not a number.
" 9 0)) (35 (90.0 47.60907745361328 449.8360595703125 60.90726852416992 "Using set
17
" 0 0) (104.9993896484375 80.60907745361328 307.3194274902344 93.90726470947266 "For example, if you evaluate the following,
" 1 0) (89.99725341796875 100.05758666992188 449.41925048828125 250.9872283935547 "(message \"There are %d %s in the office!\"
(- fill-column 14) \"pink elephants\")
a rather whimsical message will appear in your echo area. On my system it
says, \"There are 58 pink elephants in the office!\".
The expression (- fill-column 14) is evaluated and the resulting num-
ber is inserted in place of the ‘%d’; and the string in double quotes, \"pink
elephants\", is treated as a single argument and inserted in place of the
‘%s’. (That is to say, a string between double quotes evaluates to itself, like
a number.)
Finally, here is a somewhat complex example that not only illustrates
the computation of a number, but also shows how you can use an expression
within an expression to generate the text that is substituted for ‘%s’:
" 2 0) (89.99569702148438 257.1375427246094 449.5152893066406 418.9872131347656 "(message \"He saw %d %s\"
(- fill-column 34)
(concat \"red \"
(substring
\"The quick brown foxes jumped.\" 16 21)
\" leaping.\"))
In this example, message has three arguments: the string, \"He saw %d
%s\", the expression, (- fill-column 32), and the expression beginning with
the function concat. The value resulting from the evaluation of (- fill-
column 32) is inserted in place of the ‘%d’; and the value returned by the
expression beginning with concat is inserted in place of the ‘%s’.
When I evaluate the expression, the message \"He saw 38 red foxes
leaping.\" appears in my echo area.
" 3 0) (89.99574279785156 439.7003173828125 337.15985107421875 454.06085205078125 "1.9 Setting the Value of a Variable
" 4 0) (89.99496459960938 462.9290466308594 449.36309814453125 539.1072998046875 "There are several ways by which a variable can be given a value. One of
the ways is to use either the function set or the function setq. Another way
is to use let (see Section 3.6, “let”, page 36). (The jargon for this process
is to bind a variable to a value.)
The following sections not only describe how set and setq work but also
illustrate how arguments are passed.
" 5 0) (89.99526977539062 555.7218017578125 189.42724609375 568.8373413085938 "1.9.1 Using set
" 6 0) (89.99478149414062 578.009033203125 449.4709777832031 615.196044921875 "To set the value of the symbol flowers to the list ’(rose violet daisy
buttercup), evaluate the following expression by positioning the cursor after
the expression and typing C-x C-e.
" 7 0) (111.59518432617188 621.337646484375 322.8502197265625 630.3040161132812 "(set ’flowers ’(rose violet daisy buttercup))
" 8 0)) (36 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "18
Chapter 1: List Processing
" 0 0) (89.99588012695312 77.48908233642578 449.5401611328125 214.6361083984375 "The list (rose violet daisy buttercup) will appear in the echo area. This
is what is returned by the set function. As a side effect, the symbol flowers
is bound to the list ; that is, the symbol flowers, which can be viewed as a
variable, is given the list as its value. (This process, by the way, illustrates
how a side effect to the Lisp interpreter, setting the value, can be the primary
effect that we humans are interested in. This is because every Lisp function
must return a value if it does not get an error, but it will only have a side
effect if it is designed to have one.)
After evaluating the set expression, you can evaluate the symbol flowers
and it will return the value you just set. Here is the symbol. Place your
cursor after it and type C-x C-e.
" 1 0) (89.99557495117188 221.97763061523438 449.3955078125 299.8273010253906 "flowers
When you evaluate flowers, the list (rose violet daisy buttercup) ap-
pears in the echo area.
Incidentally, if you evaluate ’flowers, the variable with a quote in front
of it, what you will see in the echo area is the symbol itself, flowers. Here
is the quoted symbol, so you can try this:
" 2 0) (89.99444580078125 307.1776428222656 449.5155334472656 440.5873107910156 "’flowers
Note also, that when you use set, you need to quote both arguments to
set, unless you want them evaluated. Since we do not want either argument
evaluated, neither the variable flowers nor the list (rose violet daisy
buttercup), both are quoted. (When you use set without quoting its first
argument, the first argument is evaluated before anything else is done. If
you did this and flowers did not have a value already, you would get an
error message that the ‘Symbol’s value as variable is void’; on the other
hand, if flowers did return a value after it was evaluated, the set would
attempt to set the value that was returned. There are situations where this
is the right thing for the function to do; but such situations are rare.)
" 3 0) (89.99407958984375 460.921875 196.14169311523438 474.0374450683594 "1.9.2 Using setq
" 4 0) (89.99331665039062 484.40911865234375 449.50286865234375 585.787353515625 "As a practical matter, you almost always quote the first argument to set.
The combination of set and a quoted first argument is so common that it
has its own name: the special form setq. This special form is just like set
except that the first argument is quoted automatically, so you don’t need to
type the quote mark yourself. Also, as an added convenience, setq permits
you to set several different variables to different values, all in one expression.
To set the value of the variable carnivores to the list ’(lion tiger
leopard) using setq, the following expression is used:
" 5 0) (89.99374389648438 593.0177001953125 449.1311950683594 630.787353515625 "(setq carnivores ’(lion tiger leopard))
This is exactly the same as using set except the first argument is automat-
ically quoted by setq. (The ‘q’ in setq means quote.)
" 6 0)) (37 (90.0 47.60907745361328 449.8360595703125 60.90726852416992 "Counting
19
" 0 0) (104.9993896484375 79.52906036376953 324.09747314453125 92.8272476196289 "With set, the expression would look like this:
" 1 0) (89.99957275390625 97.89761352539062 449.4219665527344 169.1472625732422 "(set ’carnivores ’(lion tiger leopard))
Also, setq can be used to assign different values to different variables.
The first argument is bound to the value of the second argument, the third
argument is bound to the value of the fourth argument, and so on.
For
example, you could use the following to assign a list of trees to the symbol
trees and a list of herbivores to the symbol herbivores:
" 2 0) (89.99673461914062 174.09762573242188 449.462646484375 308.4672546386719 "(setq trees ’(pine fir oak maple)
herbivores ’(gazelle antelope zebra))
(The expression could just as well have been on one line, but it might not
have fit on a page; and humans find it easier to read nicely formatted lists.)
Although I have been using the term ‘assign’, there is another way of
thinking about the workings of set and setq; and that is to say that set
and setq make the symbol point to the list. This latter way of thinking is
very common and in forthcoming chapters we shall come upon at least one
symbol that has ‘pointer’ as part of its name. The name is chosen because the
symbol has a value, specifically a list, attached to it; or, expressed another
way, the symbol is set to “point” to the list.
" 3 0) (89.99659729003906 321.9618225097656 186.9347686767578 335.0658264160156 "1.9.3 Counting
" 4 0) (89.99603271484375 343.049072265625 449.38531494140625 428.1072692871094 "Here is an example that shows how to use setq in a counter. You might
use this to count how many times a part of your program repeats itself. First
set a variable to zero; then add one to the number each time the program
repeats itself. To do this, you need a variable that serves as a counter, and
two expressions: an initial setq expression that sets the counter variable to
zero; and a second setq expression that increments the counter each time it
is evaluated.
" 5 0) (111.59613800048828 433.1752014160156 385.1600646972656 442.14398193359375 "(setq counter 0)
; Let’s call this the initializer.
" 6 0) (111.59576416015625 458.01519775390625 367.5655212402344 466.9839782714844 "(setq counter (+ counter 1))
; This is the incrementer.
" 7 0) (89.99337768554688 482.9751892089844 449.4482727050781 630.7872924804688 "counter
; This is the counter.
(The text following the ‘;’ are comments.
See Section 3.2.1, “Change a
Function Definition”, page 32.)
If you evaluate the first of these expressions, the initializer, (setq
counter 0), and then evaluate the third expression, counter, the number 0
will appear in the echo area. If you then evaluate the second expression, the
incrementer, (setq counter (+ counter 1)), the counter will get the value
1. So if you again evaluate counter, the number 1 will appear in the echo
area. Each time you evaluate the second expression, the value of the counter
will be incremented.
When you evaluate the incrementer, (setq counter (+ counter 1)), the
Lisp interpreter first evaluates the innermost list; this is the addition. In
" 8 0)) (38 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "20
Chapter 1: List Processing
" 0 0) (89.99929809570312 77.48908233642578 449.4102478027344 150.5472869873047 "order to evaluate this list, it must evaluate the variable counter and the
number 1. When it evaluates the variable counter, it receives its current
value.
It passes this value and the number 1 to the + which adds them
together. The sum is then returned as the value of the inner list and passed
to the setq which sets the variable counter to this new value. Thus, the
value of the variable, counter, is changed.
" 1 0) (89.99888610839844 168.2603759765625 194.45069885253906 182.62091064453125 "1.10 Summary
" 2 0) (89.99853515625 190.64910888671875 449.4289855957031 550.1472778320312 "Learning Lisp is like climbing a hill in which the first part is the steepest.
You have now climbed the most difficult part; what remains becomes easier
as you progress onwards.
In summary,
• Lisp programs are made up of expressions, which are lists or single
atoms.
• Lists are made up of zero or more atoms or inner lists, separated by
whitespace and surrounded by parentheses. A list can be empty.
• Atoms are multi-character symbols, like forward-paragraph, single
character symbols like +, strings of characters between double quota-
tion marks, or numbers.
• A number evaluates to itself.
• A string between double quotes also evaluates to itself.
• When you evaluate a symbol by itself, its value is returned.
• When you evaluate a list, the Lisp interpreter looks at the first symbol
in the list and then at the function definition bound to that symbol.
Then the instructions in the function definition are carried out.
• A single-quote, ’, tells the Lisp interpreter that it should return the
following expression as written, and not evaluate it as it would if the
quote were not there.
• Arguments are the information passed to a function. The arguments to
a function are computed by evaluating the rest of the elements of the
list of which the function is the first element.
• A function always returns a value when it is evaluated (unless it gets
an error); in addition, it may also carry out some action called a “side
effect”. In many cases, a function’s primary purpose is to create a side
effect.
" 3 0) (89.99552154541016 567.8603515625 191.33419799804688 582.2208862304688 "1.11 Exercises
" 4 0) (98.99540710449219 590.2490844726562 449.3295593261719 630.7872924804688 "A few simple exercises:
• Generate an error message by evaluating an appropriate symbol that is
not within parentheses.
" 5 0)) (39 (90.0 47.60907745361328 449.8256530761719 60.90726852416992 "Exercises
21
" 0 0) (98.99981689453125 77.48908233642578 449.3341979980469 144.5472869873047 "• Generate an error message by evaluating an appropriate symbol that is
between parentheses.
• Create a counter that increments by two rather than one.
• Write an expression that prints a message in the echo area when evalu-
ated.
" 1 0)) (40 (90.0 47.60907745361328 449.7926025390625 60.90726852416992 "22
Chapter 1: List Processing
" 0 0)) (41 (90.0 47.60907745361328 449.8140563964844 60.90726852416992 "Buffer Names
23
" 0 0) (89.9993896484375 75.14844512939453 292.32098388671875 92.38106536865234 "2 Practicing Evaluation
" 1 0) (89.997802734375 102.08905792236328 449.6385498046875 434.1072692871094 "Before learning how to write a function definition in Emacs Lisp, it is
useful to spend a little time evaluating various expressions that have already
been written. These expressions will be lists with the functions as their first
(and often only) element. Since some of the functions associated with buffers
are both simple and interesting, we will start with those. In this section, we
will evaluate a few of these. In another section, we will study the code of
several other buffer-related functions, to see how they were written.
Whenever you give an editing command to Emacs Lisp, such as the com-
mand to move the cursor or to scroll the screen, you are evaluating an ex-
pression, the first element of which is a function. This is how Emacs works.
When you type keys, you cause the Lisp interpreter to evaluate an expres-
sion and that is how you get your results. Even typing plain text involves
evaluating an Emacs Lisp function, in this case, one that uses self-insert-
command, which simply inserts the character you typed. The functions you
evaluate by typing keystrokes are called interactive functions, or commands;
how you make a function interactive will be illustrated in the chapter on
how to write function definitions. See Section 3.3, “Making a Function In-
teractive”, page 33.
In addition to typing keyboard commands, we have seen a second way
to evaluate an expression: by positioning the cursor after a list and typing
C-x C-e. This is what we will do in the rest of this section. There are other
ways to evaluate an expression as well; these will be described as we come
to them.
Besides being used for practicing evaluation, the functions shown in the
next few sections are important in their own right. A study of these functions
makes clear the distinction between buffers and files, how to switch to a
buffer, and how to determine a location within it.
" 2 0) (89.9981689453125 449.9003601074219 215.32943725585938 464.2608947753906 "2.1 Buffer Names
" 3 0) (89.99673461914062 472.049072265625 449.4408264160156 630.787353515625 "The two functions, buffer-name and buffer-file-name, show the differ-
ence between a file and a buffer. When you evaluate the following expression,
(buffer-name), the name of the buffer appears in the echo area. When you
evaluate (buffer-file-name), the name of the file to which the buffer refers
appears in the echo area. Usually, the name returned by (buffer-name) is
the same as the name of the file to which it refers, and the name returned
by (buffer-file-name) is the full path-name of the file.
A file and a buffer are two different entities. A file is information recorded
permanently in the computer (unless you delete it). A buffer, on the other
hand, is information inside of Emacs that will vanish at the end of the editing
session (or when you kill the buffer). Usually, a buffer contains information
that you have copied from a file; we say the buffer is visiting that file. This
copy is what you work on and modify. Changes to the buffer do not change
" 4 0)) (42 (90.0 47.60907745361328 449.4980163574219 60.90726852416992 "24
Chapter 2: Practicing Evaluation
" 0 0) (89.99951171875 77.48908233642578 449.3558044433594 142.15606689453125 "the file, until you save the buffer. When you save the buffer, the buffer is
copied to the file and is thus saved permanently.
If you are reading this in Info inside of GNU Emacs, you can evaluate
each of the following expressions by positioning the cursor after it and typing
C-x C-e.
" 1 0) (111.60028076171875 148.53750610351562 172.3654022216797 157.50389099121094 "(buffer-name)
" 2 0) (89.99502563476562 173.49752807617188 450.076171875 630.7871704101562 "(buffer-file-name)
When I do this, ‘\"introduction.texinfo\"’ is the value returned by eval-
uating (buffer-name), and ‘\"/gnu/work/intro/introduction.texinfo\"’
is the value returned by evaluating (buffer-file-name). The former is the
name of the buffer and the latter is the name of the file. (In the expres-
sions, the parentheses tell the Lisp interpreter to treat buffer-name and
buffer-file-name as functions; without the parentheses, the interpreter
would attempt to evaluate the symbols as variables. See Section 1.7, “Vari-
ables”, page 10.)
In spite of the distinction between files and buffers, you will often find
that people refer to a file when they mean a buffer and vice-versa. Indeed,
most people say, “I am editing a file,” rather than saying, “I am editing a
buffer which I will soon save to a file.” It is almost always clear from context
what people mean. When dealing with computer programs, however, it is
important to keep the distinction in mind, since the computer is not as smart
as a person.
The word ‘buffer’, by the way, comes from the meaning of the word as
a cushion that deadens the force of a collision. In early computers, a buffer
cushioned the interaction between files and the computer’s central processing
unit. The drums or tapes that held a file and the central processing unit
were pieces of equipment that were very different from each other, working
at their own speeds, in spurts. The buffer made it possible for them to work
together effectively. Eventually, the buffer grew from being an intermediary,
a temporary holding place, to being the place where work is done. This
transformation is rather like that of a small seaport that grew into a great
city: once it was merely the place where cargo was warehoused temporarily
before being loaded onto ships; then it became a business and cultural center
in its own right.
Not all buffers are associated with files. For example, when you start
an Emacs session by typing the command emacs alone, without naming any
files, Emacs will start with the ‘*scratch*’ buffer on the screen. This buffer
is not visiting any file. Similarly, a ‘*Help*’ buffer is not associated with
any file.
If you switch to the ‘*scratch*’ buffer, type (buffer-name), position
the cursor after it, and type C-x C-e to evaluate the expression, the name
\"*scratch*\" is returned and will appear in the echo area. \"*scratch*\"
is the name of the buffer. However, if you type (buffer-file-name) in
" 3 0)) (43 (90.0 47.60907745361328 449.78125 60.90726852416992 "Getting Buffers
25
" 0 0) (89.99859619140625 77.48908233642578 449.4100036621094 176.46726989746094 "the ‘*scratch*’ buffer and evaluate that, nil will appear in the echo area.
nil is from the Latin word for ‘nothing’; in this case, it means that the
‘*scratch*’ buffer is not associated with any file. (In Lisp, nil is also used
to mean ‘false’ and is a synonym for the empty list, ().)
Incidentally, if you are in the ‘*scratch*’ buffer and want the value
returned by an expression to appear in the ‘*scratch*’ buffer itself rather
than in the echo area, type C-u C-x C-e instead of C-x C-e. This causes the
value returned to appear after the expression. The buffer will look like this:
" 1 0) (89.99822998046875 181.53762817382812 449.3982238769531 240.9072723388672 "(buffer-name)\"*scratch*\"
You cannot do this in Info since Info is read-only and it will not allow you
to change the contents of the buffer. But you can do this in any buffer you
can edit; and when you write code or documentation (such as this book),
this feature is very useful.
" 2 0) (89.99812316894531 257.54034423828125 228.2606201171875 271.90087890625 "2.2 Getting Buffers
" 3 0) (89.99639892578125 279.6890869140625 449.77862548828125 514.50732421875 "The buffer-name function returns the name of the buffer; to get the
buffer itself, a different function is needed: the current-buffer function. If
you use this function in code, what you get is the buffer itself.
A name and the object or entity to which the name refers are different
from each other. You are not your name. You are a person to whom others
refer by name. If you ask to speak to George and someone hands you a
card with the letters ‘G’, ‘e’, ‘o’, ‘r’, ‘g’, and ‘e’ written on it, you might be
amused, but you would not be satisfied. You do not want to speak to the
name, but to the person to whom the name refers. A buffer is similar: the
name of the scratch buffer is ‘*scratch*’, but the name is not the buffer.
To get a buffer itself, you need to use a function such as current-buffer.
However, there is a slight complication: if you evaluate current-buffer
in an expression on its own, as we will do here, what you see is a printed
representation of the name of the buffer without the contents of the buffer.
Emacs works this way for two reasons: the buffer may be thousands of lines
long—too long to be conveniently displayed; and, another buffer may have
the same contents but a different name, and it is important to distinguish
between them.
Here is an expression containing the function:
" 4 0) (89.99655151367188 519.4577026367188 449.4071044921875 630.787353515625 "(current-buffer)
If you evaluate the expression in the usual way, ‘#<buffer *info*>’ appears
in the echo area. The special format indicates that the buffer itself is being
returned, rather than just its name.
Incidentally, while you can type a number or symbol into a program, you
cannot do that with the printed representation of a buffer: the only way to
get a buffer itself is with a function such as current-buffer.
A related function is other-buffer. This returns the most recently se-
lected buffer other than the one you are in currently. If you have recently
" 5 0)) (44 (90.0 47.60907745361328 449.4980163574219 60.90726852416992 "26
Chapter 2: Practicing Evaluation
" 0 0) (89.99960327148438 77.48908233642578 449.38861083984375 117.30728912353516 "switched back and forth from the ‘*scratch*’ buffer, other-buffer will
return that buffer.
You can see this by evaluating the expression:
" 1 0) (89.99948120117188 122.85763549804688 449.3883972167969 158.7073211669922 "(other-buffer)
You should see ‘#<buffer *scratch*>’ appear in the echo area, or the name
of whatever other buffer you switched back from most recently1.
" 2 0) (89.99954223632812 177.2603759765625 242.77392578125 191.62091064453125 "2.3 Switching Buffers
" 3 0) (89.9981689453125 199.88909912109375 449.7664794921875 376.2673645019531 "The other-buffer function actually provides a buffer when it is used
as an argument to a function that requires one. We can see this by using
other-buffer and switch-to-buffer to switch to a different buffer.
But first, a brief introduction to the switch-to-buffer function. When
you switched back and forth from Info to the ‘*scratch*’ buffer to evaluate
(buffer-name), you most likely typed C-x b and then typed ‘*scratch*’2
when prompted in the minibuffer for the name of the buffer to which you
wanted to switch. The keystrokes, C-x b, cause the Lisp interpreter to eval-
uate the interactive function switch-to-buffer. As we said before, this is
how Emacs works: different keystrokes call or run different functions. For
example, C-f calls forward-char, M-e calls forward-sentence, and so on.
By writing switch-to-buffer in an expression, and giving it a buffer to
switch to, we can switch buffers just the way C-x b does.
Here is the Lisp expression:
" 4 0) (89.99703979492188 381.8177185058594 449.5504150390625 507.2898864746094 "(switch-to-buffer (other-buffer))
The symbol switch-to-buffer is the first element of the list, so the Lisp
interpreter will treat it as a function and carry out the instructions that
are attached to it.
But before doing that, the interpreter will note that
other-buffer is inside parentheses and work on that symbol first. other-
buffer is the first (and in this case, the only) element of this list, so the Lisp
interpreter calls or runs the function. It returns another buffer. Next, the
interpreter runs switch-to-buffer, passing to it, as an argument, the other
buffer, which is what Emacs will switch to. If you are reading this in Info,
try this now. Evaluate the expression. (To get back, type C-x b ⟨
" 5 0) (401.6280212402344 491.5119934082031 419.7480163574219 491.99200439453125 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (401.6400146484375 492.7224426269531 419.7401428222656 500.6925354003906 "RET
" 7 0) (401.6280212402344 500.0320129394531 419.7480163574219 500.51202392578125 "<image: None, width: 1, height: 1, bpc: 1>" 8 1) (419.2799987792969 488.1290588378906 434.0481262207031 506.9297790527344 "⟩.)3
" 9 0) (89.98799896240234 512.3919677734375 233.98800659179688 512.8719482421875 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (95.87995910644531 513.4552001953125 449.8567810058594 615.3016357421875 "1 Actually, by default, if the buffer from which you just switched is visible to you in
another window, other-buffer will choose the most recent buffer that you cannot see;
this is a subtlety that I often forget.
2 Or rather, to save typing, you probably typed just part of the name, such as *sc, and
then pressed your TAB key to cause it to expand to the full name; and then typed your
RET key.
3 Remember, this expression will move you to your most recent other buffer that you
cannot see. If you really want to go to your most recently selected buffer, even if you
can still see it, you need to evaluate the following more complex expression:
" 11 0) (111.60035705566406 621.3377075195312 355.89874267578125 630.3040771484375 "(switch-to-buffer (other-buffer (current-buffer) t))
" 12 0)) (45 (90.0 47.60907745361328 449.5959167480469 60.90726852416992 "Buffer Size and the Location of Point
27
" 0 0) (89.99728393554688 77.48908233642578 449.47625732421875 300.0672912597656 "In the programming examples in later sections of this document, you will
see the function set-buffer more often than switch-to-buffer. This is
because of a difference between computer programs and humans: humans
have eyes and expect to see the buffer on which they are working on their
computer terminals. This is so obvious, it almost goes without saying. How-
ever, programs do not have eyes. When a computer program works on a
buffer, that buffer does not need to be visible on the screen.
switch-to-buffer is designed for humans and does two different things:
it switches the buffer to which Emacs’ attention is directed; and it switches
the buffer displayed in the window to the new buffer. set-buffer, on the
other hand, does only one thing: it switches the attention of the computer
program to a different buffer. The buffer on the screen remains unchanged (of
course, normally nothing happens there until the command finishes running).
Also, we have just introduced another jargon term, the word call. When
you evaluate a list in which the first symbol is a function, you are calling
that function. The use of the term comes from the notion of the function as
an entity that can do something for you if you ‘call’ it—just as a plumber is
an entity who can fix a leak if you call him or her.
" 1 0) (89.99729919433594 320.7803955078125 382.8487243652344 335.14093017578125 "2.4 Buffer Size and the Location of Point
" 2 0) (89.99652099609375 344.0091247558594 449.4404602050781 408.18731689453125 "Finally, let’s look at several rather simple functions, buffer-size, point,
point-min, and point-max. These give information about the size of a buffer
and the location of point within it.
The function buffer-size tells you the size of the current buffer; that
is, the function returns a count of the number of characters in the buffer.
" 3 0) (89.99505615234375 414.2176818847656 449.3948669433594 528.54736328125 "(buffer-size)
You can evaluate this in the usual way, by positioning the cursor after the
expression and typing C-x C-e.
In Emacs, the current position of the cursor is called point. The expres-
sion (point) returns a number that tells you where the cursor is located as
a count of the number of characters from the beginning of the buffer up to
point.
You can see the character count for point in this buffer by evaluating the
following expression in the usual way:
" 4 0) (89.99472045898438 534.6976928710938 449.3069152832031 571.02734375 "(point)
As I write this, the value of point is 65724. The point function is frequently
used in some of the examples later in this book.
" 5 0) (89.98799896240234 583.6719970703125 233.98800659179688 584.1519775390625 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (105.0 588.4552001953125 449.8509216308594 630.308837890625 "In this case, the first argument to other-buffer tells it which buffer to skip—the
current one—and the second argument tells other-buffer it is OK to switch to a
visible buffer.
In regular use, switch-to-buffer takes you to an invisible window
since you would most likely use C-x o (other-window) to go to another visible buffer.
" 7 0)) (46 (90.0 47.60907745361328 449.4980163574219 60.90726852416992 "28
Chapter 2: Practicing Evaluation
" 0 0) (90.0001220703125 79.52906036376953 449.50830078125 104.8272476196289 "The value of point depends, of course, on its location within the buffer.
If you evaluate point in this spot, the number will be larger:
" 1 0) (89.99801635742188 109.77761840820312 449.4309997558594 230.82725524902344 "(point)
For me, the value of point in this location is 66043, which means that there
are 319 characters (including spaces) between the two expressions.
The function point-min is somewhat similar to point, but it returns the
value of the minimum permissible value of point in the current buffer. This
is the number 1 unless narrowing is in effect. (Narrowing is a mechanism
whereby you can restrict yourself, or a program, to operations on just a part
of a buffer. See Chapter 6, “Narrowing and Widening”, page 77.) Likewise,
the function point-max returns the value of the maximum permissible value
of point in the current buffer.
" 2 0) (89.99761199951172 247.2203369140625 176.9756317138672 261.58087158203125 "2.5 Exercise
" 3 0) (89.9976806640625 269.36907958984375 449.4519958496094 294.5472717285156 "Find a file with which you are working and move towards its middle.
Find its buffer name, file name, length, and your position in the file.
" 4 0)) (47 (90.0 47.60907745361328 449.7158508300781 60.90726852416992 "The defun Special Form
29
" 0 0) (89.99920654296875 75.14844512939453 408.7035217285156 92.38106536865234 "3 How To Write Function Definitions
" 1 0) (89.99887084960938 100.40906524658203 449.50750732421875 356.8272399902344 "When the Lisp interpreter evaluates a list, it looks to see whether the first
symbol on the list has a function definition attached to it; or, put another
way, whether the symbol points to a function definition.
If it does, the
computer carries out the instructions in the definition. A symbol that has a
function definition is called, simply, a function (although, properly speaking,
the definition is the function and the symbol refers to it.)
All functions are defined in terms of other functions, except for a few
primitive functions that are written in the C programming language. When
you write functions’ definitions, you will write them in Emacs Lisp and use
other functions as your building blocks. Some of the functions you will use
will themselves be written in Emacs Lisp (perhaps by you) and some will be
primitives written in C. The primitive functions are used exactly like those
written in Emacs Lisp and behave like them. They are written in C so we
can easily run GNU Emacs on any computer that has sufficient power and
can run C.
Let me re-emphasize this: when you write code in Emacs Lisp, you do
not distinguish between the use of functions written in C and the use of
functions written in Emacs Lisp. The difference is irrelevant.
I mention
the distinction only because it is interesting to know. Indeed, unless you
investigate, you won’t know whether an already-written function is written
in Emacs Lisp or C.
" 2 0) (89.99930572509766 371.66033935546875 285.83026123046875 386.0335693359375 "3.1 The defun Special Form
" 3 0) (89.99844360351562 393.8090515136719 449.5723876953125 630.787353515625 "In Lisp, a symbol such as mark-whole-buffer has code attached to it that
tells the computer what to do when the function is called. This code is called
the function definition and is created by evaluating a Lisp expression that
starts with the symbol defun (which is an abbreviation for define function).
Because defun does not evaluate its arguments in the usual way, it is called
a special form.
In subsequent sections, we will look at function definitions from the Emacs
source code, such as mark-whole-buffer. In this section, we will describe
a simple function definition so you can see how it looks.
This function
definition uses arithmetic because it makes for a simple example.
Some
people dislike examples using arithmetic; however, if you are such a person,
do not despair. Hardly any of the code we will study in the remainder of
this introduction involves arithmetic or mathematics. The examples mostly
involve text in one way or another.
A function definition has up to five parts following the word defun:
1. The name of the symbol to which the function definition should be
attached.
2. A list of the arguments that will be passed to the function. If no argu-
ments will be passed to the function, this is an empty list, ().
" 4 0)) (48 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "30
Chapter 3: How To Write Function Definitions
" 0 0) (95.8790512084961 77.48908233642578 449.5083923339844 170.10728454589844 "3. Documentation describing the function.
(Technically optional, but
strongly recommended.)
4. Optionally, an expression to make the function interactive so you can
use it by typing M-x and then the name of the function; or by typing an
appropriate key or keychord.
5. The code that instructs the computer what to do: the body of the
function definition.
" 1 0) (89.9984130859375 175.4090576171875 449.4312744140625 200.58726501464844 "It is helpful to think of the five parts of a function definition as being
organized in a template, with slots for each part:
" 2 0) (89.99749755859375 206.49517822265625 449.25555419921875 292.1471862792969 "(defun function-name (arguments...)
\"optional-documentation...\"
(interactive argument-passing-info)
; optional
body...)
As an example, here is the code for a function that multiplies its argument
by 7. (This example is not interactive. See Section 3.3, “Making a Function
Interactive”, page 33, for that information.)
" 3 0) (89.99642944335938 298.0575256347656 449.4833984375 630.7872314453125 "(defun multiply-by-seven (number)
\"Multiply NUMBER by seven.\"
(* 7 number))
This definition begins with a parenthesis and the symbol defun, followed
by the name of the function.
The name of the function is followed by a list that contains the arguments
that will be passed to the function. This list is called the argument list. In
this example, the list has only one element, the symbol, number. When the
function is used, the symbol will be bound to the value that is used as the
argument to the function.
Instead of choosing the word number for the name of the argument, I
could have picked any other name. For example, I could have chosen the
word multiplicand. I picked the word ‘number’ because it tells what kind
of value is intended for this slot; but I could just as well have chosen the
word ‘multiplicand’ to indicate the role that the value placed in this slot
will play in the workings of the function. I could have called it foogle, but
that would have been a bad choice because it would not tell humans what it
means. The choice of name is up to the programmer and should be chosen
to make the meaning of the function clear.
Indeed, you can choose any name you wish for a symbol in an argument
list, even the name of a symbol used in some other function: the name
you use in an argument list is private to that particular definition. In that
definition, the name refers to a different entity than any use of the same name
outside the function definition. Suppose you have a nick-name ‘Shorty’ in
your family; when your family members refer to ‘Shorty’, they mean you.
But outside your family, in a movie, for example, the name ‘Shorty’ refers to
someone else. Because a name in an argument list is private to the function
" 4 0)) (49 (90.0 47.60907745361328 449.6510314941406 60.90726852416992 "Install a Function Definition
31
" 0 0) (89.9986572265625 77.48908233642578 449.6072692871094 329.7073669433594 "definition, you can change the value of such a symbol inside the body of a
function without changing its value outside the function. The effect is similar
to that produced by a let expression. (See Section 3.6, “let”, page 36.)
The argument list is followed by the documentation string that describes
the function. This is what you see when you type C-h f and the name of a
function. Incidentally, when you write a documentation string like this, you
should make the first line a complete sentence since some commands, such as
apropos, print only the first line of a multi-line documentation string. Also,
you should not indent the second line of a documentation string, if you have
one, because that looks odd when you use C-h f (describe-function). The
documentation string is optional, but it is so useful, it should be included in
almost every function you write.
The third line of the example consists of the body of the function defini-
tion. (Most functions’ definitions, of course, are longer than this.) In this
function, the body is the list, (* 7 number), which says to multiply the value
of number by 7. (In Emacs Lisp, * is the function for multiplication, just as
+ is the function for addition.)
When you use the multiply-by-seven function, the argument number
evaluates to the actual number you want used. Here is an example that
shows how multiply-by-seven is used; but don’t try to evaluate this yet!
" 1 0) (89.99813842773438 336.6977233886719 449.4861755371094 509.4673767089844 "(multiply-by-seven 3)
The symbol number, specified in the function definition in the next section,
is given or “bound to” the value 3 in the actual use of the function. Note
that although number was inside parentheses in the function definition, the
argument passed to the multiply-by-seven function is not in parentheses.
The parentheses are written in the function definition so the computer can
figure out where the argument list ends and the rest of the function definition
begins.
If you evaluate this example, you are likely to get an error message. (Go
ahead, try it!) This is because we have written the function definition, but
not yet told the computer about the definition—we have not yet installed (or
‘loaded’) the function definition in Emacs. Installing a function is the process
that tells the Lisp interpreter the definition of the function. Installation is
described in the next section.
" 2 0) (89.99852752685547 533.6604614257812 318.361572265625 548.02099609375 "3.2 Install a Function Definition
" 3 0) (89.99853515625 557.7291870117188 449.4633483886719 630.7874145507812 "If you are reading this inside of Info in Emacs, you can try out the
multiply-by-seven function by first evaluating the function definition and
then evaluating (multiply-by-seven 3). A copy of the function definition
follows. Place the cursor after the last parenthesis of the function definition
and type C-x C-e. When you do this, multiply-by-seven will appear in the
echo area. (What this means is that when a function definition is evaluated,
" 4 0)) (50 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "32
Chapter 3: How To Write Function Definitions
" 0 0) (89.99884033203125 77.48908233642578 449.2569274902344 102.66727447509766 "the value it returns is the name of the defined function.) At the same time,
this action installs the function definition.
" 1 0) (89.99839782714844 108.33761596679688 449.6280212402344 244.03607177734375 "(defun multiply-by-seven (number)
\"Multiply NUMBER by seven.\"
(* 7 number))
By evaluating this defun, you have just installed multiply-by-seven in
Emacs. The function is now just as much a part of Emacs as forward-
word or any other editing function you use. (multiply-by-seven will stay
installed until you quit Emacs. To reload code automatically whenever you
start Emacs, see Section 3.5, “Installing Code Permanently”, page 36.)
You can see the effect of installing multiply-by-seven by evaluating the
following sample. Place the cursor after the following expression and type
C-x C-e. The number 21 will appear in the echo area.
" 2 0) (90.00173950195312 249.69760131835938 449.5650634765625 309.5472717285156 "(multiply-by-seven 3)
If you wish, you can read the documentation for the function by typing
C-h f (describe-function) and then the name of the function, multiply-
by-seven. When you do this, a ‘*Help*’ window will appear on your screen
that says:
" 3 0) (90.00132751464844 315.2176208496094 369.9828186035156 352.6361083984375 "multiply-by-seven:
Multiply NUMBER by seven.
(To return to a single window on your screen, type C-x 1.)
" 4 0) (90.00106811523438 368.0418395996094 317.6019287109375 381.1458435058594 "3.2.1 Change a Function Definition
" 5 0) (90.00045776367188 389.8490905761719 449.45574951171875 501.42730712890625 "If you want to change the code in multiply-by-seven, just rewrite it.
To install the new version in place of the old one, evaluate the function
definition again. This is how you modify code in Emacs. It is very simple.
As an example, you can change the multiply-by-seven function to add
the number to itself seven times instead of multiplying the number by seven.
It produces the same answer, but by a different path. At the same time, we
will add a comment to the code; a comment is text that the Lisp interpreter
ignores, but that a human reader may find useful or enlightening.
The
comment is that this is the “second version”.
" 6 0) (90.00033569335938 507.09527587890625 449.4554138183594 630.787353515625 "(defun multiply-by-seven (number)
; Second version.
\"Multiply NUMBER by seven.\"
(+ number number number number number number number))
The comment follows a semicolon, ‘;’. In Lisp, everything on a line that
follows a semicolon is a comment. The end of the line is the end of the
comment. To stretch a comment over two or more lines, begin each line
with a semicolon.
See Section 16.3, “Beginning a ‘.emacs’ File”, page 216, and section
“Comments” in The GNU Emacs Lisp Reference Manual, for more about
comments.
" 7 0)) (51 (90.0 47.60907745361328 449.6617126464844 60.90726852416992 "Make a Function Interactive
33
" 0 0) (90.00015258789062 77.48908233642578 449.4977111816406 152.9473114013672 "You can install this version of the multiply-by-seven function by eval-
uating it in the same way you evaluated the first function: place the cursor
after the last parenthesis and type C-x C-e.
In summary, this is how you write code in Emacs Lisp: you write a
function; install it; test it; and then make fixes or enhancements and install
it again.
" 1 0) (90.00015258789062 171.0203857421875 318.5445251464844 185.38092041015625 "3.3 Make a Function Interactive
" 2 0) (89.99545288085938 193.52911376953125 449.4504089355469 381.5473327636719 "You make a function interactive by placing a list that begins with the
special form interactive immediately after the documentation.
A user
can invoke an interactive function by typing M-x and then the name of the
function; or by typing the keys to which it is bound, for example, by typing
C-n for next-line or C-x h for mark-whole-buffer.
Interestingly, when you call an interactive function interactively, the value
returned is not automatically displayed in the echo area. This is because you
often call an interactive function for its side effects, such as moving forward
by a word or line, and not for the value returned. If the returned value
were displayed in the echo area each time you typed a key, it would be very
distracting.
Both the use of the special form interactive and one way to display a
value in the echo area can be illustrated by creating an interactive version
of multiply-by-seven.
Here is the code:
" 3 0) (89.9947509765625 386.97528076171875 449.7109375 490.1298828125 "(defun multiply-by-seven (number)
; Interactive version.
\"Multiply NUMBER by seven.\"
(interactive \"p\")
(message \"The result is %d\" (* 7 number)))
You can install this code by placing your cursor after it and typing C-x C-e.
The name of the function will appear in your echo area. Then, you can use
this code by typing C-u and a number and then typing M-x multiply-by-
seven and pressing ⟨
" 4 0) (188.98800659179688 474.35198974609375 207.10800170898438 474.8320007324219 "<image: None, width: 1, height: 1, bpc: 1>" 5 1) (189.0 475.56243896484375 207.10011291503906 483.53253173828125 "RET
" 6 0) (188.98800659179688 482.8719787597656 207.10800170898438 483.35198974609375 "<image: None, width: 1, height: 1, bpc: 1>" 7 1) (89.9998779296875 470.9690856933594 449.6286926269531 589.1560668945312 "⟩. The phrase ‘The result is ...’ followed by the
product will appear in the echo area.
Speaking more generally, you invoke a function like this in either of two
ways:
1. By typing a prefix argument that contains the number to be passed,
and then typing M-x and the name of the function, as with C-u 3 M-x
forward-sentence; or,
2. By typing whatever key or keychord the function is bound to, as with
C-u 3 M-e.
" 8 0) (89.99884796142578 593.6090698242188 449.2897033691406 630.7872924804688 "Both the examples just mentioned work identically to move point forward
three sentences. (Since multiply-by-seven is not bound to a key, it could
not be used as an example of key binding.)
" 9 0)) (52 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "34
Chapter 3: How To Write Function Definitions
" 0 0) (89.99887084960938 77.48908233642578 449.442626953125 134.2097930908203 "(See Section 16.7, “Some Keybindings”, page 220, to learn how to bind
a command to a key.)
A prefix argument is passed to an interactive function by typing the
⟨
" 1 0) (92.86800384521484 118.7919921875 118.18800354003906 119.2719955444336 "<image: None, width: 1, height: 1, bpc: 1>" 2 1) (92.87999725341797 120.00239562988281 118.16650390625 127.9725112915039 "META
" 3 0) (92.86800384521484 127.31201171875 118.18800354003906 127.7920150756836 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (90.00018310546875 115.04907989501953 449.5415344238281 152.58726501464844 "⟩ key followed by a number, for example, M-3 M-e, or by typing C-
u and then a number, for example, C-u 3 M-e (if you type C-u without a
number, it defaults to 4).
" 5 0) (90.00042724609375 166.081787109375 340.1502380371094 179.1973419189453 "3.3.1 An Interactive multiply-by-seven
" 6 0) (90.00039672851562 187.2890625 449.4432067871094 224.46726989746094 "Let’s look at the use of the special form interactive and then at the
function message in the interactive version of multiply-by-seven. You will
recall that the function definition looks like this:
" 7 0) (90.00021362304688 229.53521728515625 449.639892578125 340.5072326660156 "(defun multiply-by-seven (number)
; Interactive version.
\"Multiply NUMBER by seven.\"
(interactive \"p\")
(message \"The result is %d\" (* 7 number)))
In this function, the expression, (interactive \"p\"), is a list of two ele-
ments. The \"p\" tells Emacs to pass the prefix argument to the function and
use its value for the argument of the function.
The argument will be a number. This means that the symbol number
will be bound to a number in the line:
" 8 0) (90.00032043457031 345.57757568359375 449.1166076660156 381.0671081542969 "(message \"The result is %d\" (* 7 number))
For example, if your prefix argument is 5, the Lisp interpreter will evaluate
the line as if it were:
" 9 0) (89.99880981445312 386.0174560546875 449.5517883300781 630.7871704101562 "(message \"The result is %d\" (* 7 5))
(If you are reading this in GNU Emacs, you can evaluate this expression
yourself.) First, the interpreter will evaluate the inner list, which is (* 7
5). This returns a value of 35. Next, it will evaluate the outer list, passing
the values of the second and subsequent elements of the list to the function
message.
As we have seen, message is an Emacs Lisp function especially designed
for sending a one line message to a user. (See Section 1.8.5, “The message
function”, page 16.) In summary, the message function prints its first argu-
ment in the echo area as is, except for occurrences of ‘%d’, ‘%s’, or ‘%c’. When
it sees one of these control sequences, the function looks to the second and
subsequent arguments and prints the value of the argument in the location
in the string where the control sequence is located.
In the interactive multiply-by-seven function, the control string is ‘%d’,
which requires a number, and the value returned by evaluating (* 7 5) is
the number 35. Consequently, the number 35 is printed in place of the ‘%d’
and the message is ‘The result is 35’.
(Note that when you call the function multiply-by-seven, the message
is printed without quotes, but when you call message, the text is printed
in double quotes. This is because the value returned by message is what
" 10 0)) (53 (90.0 47.60907745361328 449.60723876953125 60.90726852416992 "Different Options for interactive
35
" 0 0) (89.99957275390625 77.48908233642578 449.4103088378906 114.66727447509766 "appears in the echo area when you evaluate an expression whose first element
is message; but when embedded in a function, message prints the text as a
side effect without quotes.)
" 1 0) (89.99964904785156 140.54034423828125 353.6011047363281 154.91355895996094 "3.4 Different Options for interactive
" 2 0) (90.0 165.08905029296875 449.3779296875 208.12977600097656 "In the example, multiply-by-seven used \"p\" as the argument to
interactive. This argument told Emacs to interpret your typing either
C-u followed by a number or ⟨
" 3 0) (237.8280029296875 192.35198974609375 263.14801025390625 192.8319854736328 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (237.83999633789062 193.56239318847656 263.1264953613281 201.53250122070312 "META
" 5 0) (237.8280029296875 200.87200927734375 263.14801025390625 201.3520050048828 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (89.99932861328125 188.60906982421875 449.7263488769531 262.0272521972656 "⟩ followed by a number as a command
to pass that number to the function as its argument. Emacs has more than
twenty characters predefined for use with interactive. In almost every
case, one of these options will enable you to pass the right information in-
teractively to a function. (See section “Code Characters for interactive”
in The GNU Emacs Lisp Reference Manual.)
" 7 0) (89.99850463867188 265.1689453125 449.4425048828125 302.3471374511719 "For example, the character ‘r’ causes Emacs to pass the beginning and
end of the region (the current values of point and mark) to the function as
two separate arguments. It is used as follows:
" 8 0) (89.99786376953125 309.6974792480469 449.4625549316406 400.7296447753906 "(interactive \"r\")
On the other hand, a ‘B’ tells Emacs to ask for the name of a buffer that
will be passed to the function. When it sees a ‘B’, Emacs will ask for the
name by prompting the user in the minibuffer, using a string that follows
the ‘B’, as in \"BAppend to buffer: \". Not only will Emacs prompt for the
name, but Emacs will complete the name if you type enough of it and press
⟨
" 9 0) (92.86800384521484 385.31201171875 110.62800598144531 385.7920227050781 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (92.87999725341797 386.5224304199219 110.6187744140625 394.4925231933594 "TAB
" 11 0) (92.86800384521484 393.9519958496094 110.62800598144531 394.4320068359375 "<image: None, width: 1, height: 1, bpc: 1>" 12 1) (89.9998779296875 381.92913818359375 449.7382507324219 535.5073852539062 "⟩.
A function with two or more arguments can have information passed
to each argument by adding parts to the string that follows interactive.
When you do this, the information is passed to each argument in the same
order it is specified in the interactive list.
In the string, each part is
separated from the next part by a ‘\\n’, which is a newline. For example,
you could follow \"BAppend to buffer: \" with a ‘\\n’) and an ‘r’. This would
cause Emacs to pass the values of point and mark to the function as well as
prompt you for the buffer—three arguments in all.
In this case, the function definition would look like the following, where
buffer, start, and end are the symbols to which interactive binds the
buffer and the current values of the beginning and ending of the region:
" 13 0) (111.60009765625 542.8552856445312 301.69366455078125 589.1441040039062 "(defun name-of-function (buffer start end)
\"documentation...\"
(interactive \"BAppend to buffer: \\nr\")
body-of-function...)
" 14 0) (89.99908447265625 593.6091918945312 449.36614990234375 630.7874145507812 "(The space after the colon in the prompt makes it look better when you
are prompted. The append-to-buffer function looks exactly like this. See
Section 4.4, “The Definition of append-to-buffer”, page 56.)
" 15 0)) (54 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "36
Chapter 3: How To Write Function Definitions
" 0 0) (89.9991455078125 77.48908233642578 449.42108154296875 164.46726989746094 "If a function does not have arguments, then interactive does not require
any. Such a function contains the simple expression (interactive). The
mark-whole-buffer function is like this.
Alternatively, if the special letter-codes are not right for your application,
you can pass your own arguments to interactive as a list.
See section
“Using Interactive” in The GNU Emacs Lisp Reference Manual, for more
information about this advanced technique.
" 1 0) (89.99888610839844 180.8603515625 299.9631652832031 195.22088623046875 "3.5 Install Code Permanently
" 2 0) (89.99822998046875 203.00909423828125 449.5183410644531 454.3872985839844 "When you install a function definition by evaluating it, it will stay in-
stalled until you quit Emacs.
The next time you start a new session of
Emacs, the function will not be installed unless you evaluate the function
definition again.
At some point, you may want to have code installed automatically when-
ever you start a new session of Emacs.
There are several ways of doing
this:
• If you have code that is just for yourself, you can put the code for the
function definition in your ‘.emacs’ initialization file. When you start
Emacs, your ‘.emacs’ file is automatically evaluated and all the function
definitions within it are installed. See Chapter 16, “Your ‘.emacs’ File”,
page 213.
• Alternatively, you can put the function definitions that you want in-
stalled in one or more files of their own and use the load function to
cause Emacs to evaluate and thereby install each of the functions in the
files. See Section 16.9, “Loading Files”, page 222.
• On the other hand, if you have code that your whole site will use,
it is usual to put it in a file called ‘site-init.el’ that is loaded when
Emacs is built. This makes the code available to everyone who uses your
machine. (See the ‘INSTALL’ file that is part of the Emacs distribution.)
" 3 0) (89.994384765625 458.0091247558594 449.3945007324219 566.9473876953125 "Finally, if you have code that everyone who uses Emacs may want, you
can post it on a computer network or send a copy to the Free Software Foun-
dation. (When you do this, please license the code and its documentation
under a license that permits other people to run, copy, study, modify, and
redistribute the code and which protects you from having your work taken
from you.) If you send a copy of your code to the Free Software Foundation,
and properly protect yourself and others, it may be included in the next
release of Emacs. In large part, this is how Emacs has grown over the past
years, by donations.
" 4 0) (89.99435424804688 583.46044921875 140.906005859375 597.8336791992188 "3.6 let
" 5 0) (89.99359130859375 605.4891357421875 449.3171691894531 630.787353515625 "The let expression is a special form in Lisp that you will need to use in
most function definitions.
" 6 0)) (55 (90.0 47.60907745361328 449.6617126464844 60.90726852416992 "The Parts of a let Expression
37
" 0 0) (89.99655151367188 77.48908233642578 449.54119873046875 534.3071899414062 "let is used to attach or bind a symbol to a value in such a way that
the Lisp interpreter will not confuse the variable with a variable of the same
name that is not part of the function.
To understand why the let special form is necessary, consider the situa-
tion in which you own a home that you generally refer to as ‘the house’, as
in the sentence, “The house needs painting.” If you are visiting a friend and
your host refers to ‘the house’, he is likely to be referring to his house, not
yours, that is, to a different house.
If your friend is referring to his house and you think he is referring to
your house, you may be in for some confusion. The same thing could happen
in Lisp if a variable that is used inside of one function has the same name
as a variable that is used inside of another function, and the two are not
intended to refer to the same value. The let special form prevents this kind
of confusion.
The let special form prevents confusion. let creates a name for a local
variable that overshadows any use of the same name outside the let ex-
pression. This is like understanding that whenever your host refers to ‘the
house’, he means his house, not yours. (Symbols used in argument lists work
the same way. See Section 3.1, “The defun Special Form”, page 29.)
Local variables created by a let expression retain their value only within
the let expression itself (and within expressions called within the let ex-
pression); the local variables have no effect outside the let expression.
Another way to think about let is that it is like a setq that is temporary
and local. The values set by let are automatically undone when the let is
finished. The setting only effects expressions that are inside the bounds of
the let expression. In computer science jargon, we would say “the binding
of a symbol is visible only in functions called in the let form; in Emacs Lisp,
scoping is dynamic, not lexical.”
let can create more than one variable at once.
Also, let gives each
variable it creates an initial value, either a value specified by you, or nil.
(In the jargon, this is called ‘binding the variable to the value’.) After let
has created and bound the variables, it executes the code in the body of the
let, and returns the value of the last expression in the body, as the value of
the whole let expression. (‘Execute’ is a jargon term that means to evaluate
a list; it comes from the use of the word meaning ‘to give practical effect to’
(Oxford English Dictionary). Since you evaluate an expression to perform
an action, ‘execute’ has evolved as a synonym to ‘evaluate’.)
" 1 0) (89.99635314941406 548.2816772460938 320.2282409667969 561.397216796875 "3.6.1 The Parts of a let Expression
" 2 0) (89.99591064453125 569.6089477539062 449.42822265625 630.7871704101562 "A let expression is a list of three parts. The first part is the symbol
let. The second part is a list, called a varlist, each element of which is
either a symbol by itself or a two-element list, the first element of which is
a symbol. The third part of the let expression is the body of the let. The
body usually consists of one or more lists.
" 3 0)) (56 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "38
Chapter 3: How To Write Function Definitions
" 0 0) (104.99932861328125 85.64905548095703 329.6063537597656 98.9472427368164 "A template for a let expression looks like this:
" 1 0) (111.59925842285156 110.13507843017578 204.64085388183594 119.10386657714844 "(let varlist body...)
" 2 0) (89.99923706054688 126.32892608642578 449.5193786621094 187.5071258544922 "The symbols in the varlist are the variables that are given initial values by
the let special form. Symbols by themselves are given the initial value of
nil; and each symbol that is the first element of a two-element list is bound
to the value that is returned when the Lisp interpreter evaluates the second
element.
" 3 0) (89.9986572265625 194.2489013671875 449.33294677734375 231.42710876464844 "Thus, a varlist might look like this: (thread (needles 3)). In this case,
in a let expression, Emacs binds the symbol thread to an initial value of
nil, and binds the symbol needles to an initial value of 3.
" 4 0) (89.99810791015625 238.28887939453125 449.45269775390625 263.4670715332031 "When you write a let expression, what you do is put the appropriate
expressions in the slots of the let expression template.
" 5 0) (89.99835205078125 270.3289794921875 449.3871765136719 295.5071716308594 "If the varlist is composed of two-element lists, as is often the case, the
template for the let expression looks like this:
" 6 0) (111.59841918945312 306.69512939453125 207.2140350341797 352.9839172363281 "(let ((variable value)
(variable value)
...)
body...)
" 7 0) (89.99833679199219 384.24176025390625 273.5873718261719 397.3573303222656 "3.6.2 Sample let Expression
" 8 0) (89.99783325195312 411.5690002441406 449.2343444824219 448.7471923828125 "The following expression creates and gives initial values to the two vari-
ables zebra and tiger. The body of the let expression is a list which calls
the message function.
" 9 0) (111.59782409667969 459.8175354003906 379.4499206542969 506.22393798828125 "(let ((zebra ’stripes)
(tiger ’fierce))
(message \"One kind of animal has %s and another is %s.\"
zebra tiger))
" 10 0) (104.99815368652344 513.6890258789062 387.94696044921875 526.9872436523438 "Here, the varlist is ((zebra ’stripes) (tiger ’fierce)).
" 11 0) (89.99758911132812 533.72900390625 449.4306640625 630.7872314453125 "The two variables are zebra and tiger. Each variable is the first element
of a two-element list and each value is the second element of its two-element
list. In the varlist, Emacs binds the variable zebra to the value stripes, and
binds the variable tiger to the value fierce. In this example, both values
are symbols preceded by a quote. The values could just as well have been
another list or a string. The body of the let follows after the list holding the
variables. In this example, the body is a list that uses the message function
to print a string in the echo area.
" 12 0)) (57 (90.0 47.60907745361328 449.7485656738281 60.90726852416992 "The if Special Form
39
" 0 0) (89.9990234375 80.12909698486328 449.4104309082031 117.42728424072266 "You may evaluate the example in the usual fashion, by placing the cur-
sor after the last parenthesis and typing C-x C-e. When you do this, the
following will appear in the echo area:
" 1 0) (89.99917602539062 122.97763061523438 449.27899169921875 182.8273162841797 "\"One kind of animal has stripes and another is fierce.\"
As we have seen before, the message function prints its first argument,
except for ‘%s’. In this example, the value of the variable zebra is printed
at the location of the first ‘%s’ and the value of the variable tiger is printed
at the location of the second ‘%s’.
" 2 0) (89.99952697753906 198.12188720703125 399.14581298828125 211.23744201660156 "3.6.3 Uninitialized Variables in a let Statement
" 3 0) (90.00033569335938 219.80914306640625 449.35638427734375 257.1073303222656 "If you do not bind the variables in a let statement to specific initial
values, they will automatically be bound to an initial value of nil, as in the
following expression:
" 4 0) (90.00006103515625 262.6576843261719 449.3996276855469 388.26739501953125 "(let ((birch 3)
pine
fir
(oak ’some))
(message
\"Here are %d variables with %s, %s, and %s value.\"
birch pine fir oak))
Here, the varlist is ((birch 3) pine fir (oak ’some)).
If you evaluate this expression in the usual way, the following will appear
in your echo area:
" 5 0) (89.99850463867188 393.8177490234375 449.52935791015625 528.0675048828125 "\"Here are 3 variables with nil, nil, and some value.\"
In this example, Emacs binds the symbol birch to the number 3, binds the
symbols pine and fir to nil, and binds the symbol oak to the value some.
Note that in the first part of the let, the variables pine and fir stand
alone as atoms that are not surrounded by parentheses; this is because they
are being bound to nil, the empty list. But oak is bound to some and so
is a part of the list (oak ’some). Similarly, birch is bound to the number
3 and so is in a list with that number. (Since a number evaluates to itself,
the number does not need to be quoted. Also, the number is printed in the
message using a ‘%d’ rather than a ‘%s’.) The four variables as a group are
put into a list to delimit them from the body of the let.
" 6 0) (89.99874877929688 546.9805297851562 263.7757263183594 561.353759765625 "3.7 The if Special Form
" 7 0) (89.9986572265625 569.6092529296875 449.37689208984375 630.7874755859375 "A third special form, in addition to defun and let, is the conditional if.
This form is used to instruct the computer to make decisions. You can write
function definitions without using if, but it is used often enough, and is
important enough, to be included here. It is used, for example, in the code
for the function beginning-of-buffer.
" 8 0)) (58 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "40
Chapter 3: How To Write Function Definitions
" 0 0) (89.9993896484375 77.48908233642578 449.3011169433594 126.66727447509766 "The basic idea behind an if, is that “if a test is true, then an expression
is evaluated.” If the test is not true, the expression is not evaluated. For
example, you might make a decision such as, “if it is warm and sunny, then
go to the beach!”
" 1 0) (90.00003051757812 133.2890625 449.541015625 182.46726989746094 "An if expression written in Lisp does not use the word ‘then’; the test
and the action are the second and third elements of the list whose first
element is if. Nonetheless, the test part of an if expression is often called
the if-part and the second argument is often called the then-part.
" 2 0) (89.99954223632812 189.08905029296875 449.38836669921875 238.2672576904297 "Also, when an if expression is written, the true-or-false-test is usually
written on the same line as the symbol if, but the action to carry out if the
test is true, the “then-part”, is written on the second and subsequent lines.
This makes the if expression easier to read.
" 3 0) (111.59953308105469 249.2152099609375 266.74188232421875 270.6639709472656 "(if true-or-false-test
action-to-carry-out-if-test-is-true)
" 4 0) (89.99954223632812 278.12908935546875 449.3883056640625 303.3072814941406 "The true-or-false-test will be an expression that is evaluated by the Lisp
interpreter.
" 5 0) (89.99847412109375 309.9290771484375 449.3443908691406 347.2272644042969 "Here is an example that you can evaluate in the usual manner. The test is
whether the number 5 is greater than the number 4. Since it is, the message
‘5 is greater than 4!’ will be printed.
" 6 0) (111.59870910644531 358.1752014160156 347.1021728515625 379.6239929199219 "(if (> 5 4)
; if-part
(message \"5 is greater than 4!\"))
; then-part
" 7 0) (89.99832153320312 387.569091796875 449.27825927734375 412.8672790527344 "(The function > tests whether its first argument is greater than its second
argument and returns true if it is.)
" 8 0) (89.99765014648438 419.4891052246094 449.45269775390625 480.66729736328125 "Of course, in actual use, the test in an if expression will not be fixed
for all time as it is by the expression (> 5 4). Instead, at least one of the
variables used in the test will be bound to a value that is not known ahead
of time. (If the value were known ahead of time, we would not need to run
the test!)
" 9 0) (89.99691772460938 487.28912353515625 449.77838134765625 548.3473510742188 "For example, the value may be bound to an argument of a function defi-
nition. In the following function definition, the character of the animal is a
value that is passed to the function. If the value bound to characteristic
is fierce, then the message, ‘It’s a tiger!’ will be printed; otherwise, nil
will be returned.
" 10 0) (111.59686279296875 559.4176635742188 383.8551025390625 630.6640625 "(defun type-of-animal (characteristic)
\"Print message in echo area depending on CHARACTERISTIC.
If the CHARACTERISTIC is the symbol ‘fierce’,
then warn of a tiger.\"
(if (equal characteristic ’fierce)
(message \"It’s a tiger!\")))
" 11 0)) (59 (90.0 47.60907745361328 449.5629577636719 60.90726852416992 "The type-of-animal Function in Detail
41
" 0 0) (89.99929809570312 79.76905059814453 449.2897033691406 117.0672378540039 "If you are reading this inside of GNU Emacs, you can evaluate the function
definition in the usual way to install it in Emacs, and then you can evaluate
the following two expressions to see the results:
" 1 0) (111.59971618652344 122.25759887695312 224.23960876464844 131.22398376464844 "(type-of-animal ’fierce)
" 2 0) (111.5997085571289 147.21762084960938 219.56813049316406 156.1840057373047 "(type-of-animal ’zebra)
" 3 0) (89.99862670898438 170.24908447265625 449.1593322753906 207.4272918701172 "When you evaluate (type-of-animal ’fierce), you will see the following
message printed in the echo area: \"It’s a tiger!\"; and when you evaluate
(type-of-animal ’zebra) you will see nil printed in the echo area.
" 4 0) (89.9989013671875 221.5218505859375 374.209228515625 234.6374053955078 "3.7.1 The type-of-animal Function in Detail
" 5 0) (89.998779296875 242.9691162109375 449.4207763671875 308.5874328613281 "Let’s look at the type-of-animal function in detail.
The function definition for type-of-animal was written by filling the
slots of two templates, one for a function definition as a whole, and a second
for an if expression.
The template for every function that is not interactive is:
" 6 0) (104.99870300292969 313.8953857421875 416.442138671875 363.6674499511719 "(defun name-of-function (argument-list)
\"documentation...\"
body...)
The parts of the function that match this template look like this:
" 7 0) (89.99978637695312 368.9779052734375 449.4326477050781 504.7875671386719 "(defun type-of-animal (characteristic)
\"Print message in echo area depending on CHARACTERISTIC.
If the CHARACTERISTIC is the symbol ‘fierce’,
then warn of a tiger.\"
body: the if expression)
The name of function is type-of-animal; it is passed the value of one
argument.
The argument list is followed by a multi-line documentation
string. The documentation string is included in the example because it is a
good habit to write documentation string for every function definition. The
body of the function definition consists of the if expression.
The template for an if expression looks like this:
" 8 0) (105.00108337402344 509.97552490234375 429.5027160644531 547.3876342773438 "(if true-or-false-test
action-to-carry-out-if-the-test-returns-true)
In the type-of-animal function, the code for the if looks like this:
" 9 0) (105.00122833251953 552.5780029296875 316.1464538574219 589.86767578125 "(if (equal characteristic ’fierce)
(message \"It’s a tiger!\")))
Here, the true-or-false-test is the expression:
" 10 0) (90.00041961669922 595.1780395507812 449.400390625 630.7876586914062 "(equal characteristic ’fierce)
In Lisp, equal is a function that determines whether its first argument is
equal to its second argument. The second argument is the quoted symbol
" 11 0)) (60 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "42
Chapter 3: How To Write Function Definitions
" 0 0) (89.99932861328125 77.48908233642578 449.8138427734375 102.66727447509766 "’fierce and the first argument is the value of the symbol characteristic—
in other words, the argument passed to this function.
" 1 0) (89.999267578125 110.00910186767578 449.5413513183594 171.0673065185547 "In the first exercise of type-of-animal, the argument fierce is passed to
type-of-animal. Since fierce is equal to fierce, the expression, (equal
characteristic ’fierce), returns a value of true. When this happens, the
if evaluates the second argument or then-part of the if: (message \"It’s
tiger!\").
" 2 0) (89.99838256835938 178.40911865234375 449.4090270996094 215.5873260498047 "On the other hand, in the second exercise of type-of-animal, the argu-
ment zebra is passed to type-of-animal. zebra is not equal to fierce, so
the then-part is not evaluated and nil is returned by the if expression.
" 3 0) (89.99838256835938 258.50042724609375 290.74395751953125 272.8609619140625 "3.8 If–then–else Expressions
" 4 0) (89.9981689453125 287.2491455078125 449.3325500488281 360.3073425292969 "An if expression may have an optional third argument, called the else-
part, for the case when the true-or-false-test returns false. When this hap-
pens, the second argument or then-part of the overall if expression is not
evaluated, but the third or else-part is evaluated. You might think of this
as the cloudy day alternative for the decision ‘if it is warm and sunny, then
go to the beach, else read a book!”.
" 5 0) (89.99774169921875 367.649169921875 449.4740905761719 416.8273620605469 "The word “else” is not written in the Lisp code; the else-part of an if
expression comes after the then-part. In the written Lisp, the else-part is
usually written to start on a line of its own and is indented less than the
then-part:
" 6 0) (111.59774017333984 428.37530517578125 300.1712341308594 462.3041076660156 "(if true-or-false-test
action-to-carry-out-if-the-test-returns-true
action-to-carry-out-if-the-test-returns-false)
" 7 0) (89.99734497070312 470.24920654296875 449.4627685546875 495.5473937988281 "For example, the following if expression prints the message ‘4 is not
greater than 5!’ when you evaluate it in the usual way:
" 8 0) (111.59687805175781 507.21533203125 347.1088562011719 541.024169921875 "(if (> 4 5)
; if-part
(message \"5 is greater than 4!\")
; then-part
(message \"4 is not greater than 5!\")) ; else-part
" 9 0) (89.99740600585938 549.0892333984375 449.3863220214844 598.2674560546875 "Note that the different levels of indentation make it easy to distinguish the
then-part from the else-part. (GNU Emacs has several commands that au-
tomatically indent if expressions correctly. See Section 1.1.3, “GNU Emacs
Helps You Type Lists”, page 3.)
" 10 0) (89.997314453125 605.4891967773438 449.3753356933594 630.7874145507812 "We can extend the type-of-animal function to include an else-part by
simply incorporating an additional part to the if expression.
" 11 0)) (61 (90.0 47.60907745361328 449.6175231933594 60.90726852416992 "Truth and Falsehood in Emacs Lisp
43
" 0 0) (89.99917602539062 79.52906036376953 449.3661804199219 128.70726013183594 "You can see the consequences of doing this if you evaluate the following
version of the type-of-animal function definition to install it and then
evaluate the two subsequent expressions to pass different arguments to the
function.
" 1 0) (111.59921264648438 133.77520751953125 383.85723876953125 229.86387634277344 "(defun type-of-animal (characteristic)
; Second version.
\"Print message in echo area depending on CHARACTERISTIC.
If the CHARACTERISTIC is the symbol ‘fierce’,
then warn of a tiger;
else say it’s not fierce.\"
(if (equal characteristic ’fierce)
(message \"It’s a tiger!\")
(message \"It’s not fierce!\")))
" 2 0) (111.5989761352539 246.33749389648438 224.23887634277344 255.3038787841797 "(type-of-animal ’fierce)
" 3 0) (111.5989761352539 271.2975158691406 219.56739807128906 280.2638854980469 "(type-of-animal ’zebra)
" 4 0) (89.9976806640625 294.0889892578125 449.4304504394531 381.06719970703125 "When you evaluate (type-of-animal ’fierce), you will see the following
message printed in the echo area: \"It’s a tiger!\"; but when you evaluate
(type-of-animal ’zebra), you will see \"It’s not fierce!\".
(Of course, if the characteristic were ferocious, the message \"It’s not
fierce!\" would be printed; and it would be misleading! When you write
code, you need to take into account the possibility that some such argument
will be tested by the if and write your program accordingly.)
" 5 0) (89.99810791015625 396.140380859375 370.0843200683594 410.50091552734375 "3.9 Truth and Falsehood in Emacs Lisp
" 6 0) (89.99664306640625 418.16900634765625 449.47314453125 630.7872924804688 "There is an important aspect to the truth test in an if expression. So
far, we have spoken of ‘true’ and ‘false’ as values of predicates as if they were
new kinds of Emacs Lisp objects. In fact, ‘false’ is just our old friend nil.
Anything else—anything at all—is ‘true’.
The expression that tests for truth is interpreted as true if the result of
evaluating it is a value that is not nil. In other words, the result of the test
is considered true if the value returned is a number such as 47, a string such
as \"hello\", or a symbol (other than nil) such as flowers, or a list, or even
a buffer!
Before illustrating a test for truth, we need an explanation of nil.
In Emacs Lisp, the symbol nil has two meanings. First, it means the
empty list. Second, it means false and is the value returned when a true-or-
false-test tests false. nil can be written as an empty list, (), or as nil. As
far as the Lisp interpreter is concerned, () and nil are the same. Humans,
however, tend to use nil for false and () for the empty list.
In Emacs Lisp, any value that is not nil—is not the empty list—is con-
sidered true. This means that if an evaluation returns something that is not
" 7 0)) (62 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "44
Chapter 3: How To Write Function Definitions
" 0 0) (89.99884033203125 77.48908233642578 449.50787353515625 227.1073455810547 "an empty list, an if expression will test true. For example, if a number is
put in the slot for the test, it will be evaluated and will return itself, since
that is what numbers do when evaluated. In this conditional, the if expres-
sion will test true. The expression tests false only when nil, an empty list,
is returned by evaluating the expression.
You can see this by evaluating the two expressions in the following ex-
amples.
In the first example, the number 4 is evaluated as the test in the if
expression and returns itself; consequently, the then-part of the expression
is evaluated and returned: ‘true’ appears in the echo area. In the second
example, the nil indicates false; consequently, the else-part of the expression
is evaluated and returned: ‘false’ appears in the echo area.
" 1 0) (111.59869384765625 232.53768920898438 153.8319549560547 266.4640197753906 "(if 4
’true
’false)
" 2 0) (89.99874877929688 274.5376281738281 449.2897033691406 360.4272155761719 "(if nil
’true
’false)
Incidentally, if some other useful value is not available for a test that
returns true, then the Lisp interpreter will return the symbol t for true. For
example, the expression (> 5 4) returns t when evaluated, as you can see
by evaluating it in the usual way:
" 3 0) (89.99907684326172 365.8575744628906 392.6931457519531 389.70721435546875 "(> 5 4)
On the other hand, this function returns nil if the test is false.
" 4 0) (111.59896850585938 395.1375732421875 144.47213745117188 404.10394287109375 "(> 4 5)
" 5 0) (89.99890899658203 422.7803039550781 230.6892547607422 437.1535339355469 "3.10 save-excursion
" 6 0) (89.99594116210938 445.2890319824219 449.5833740234375 630.7872314453125 "The save-excursion function is the fourth and final special form that
we will discuss in this chapter.
In Emacs Lisp programs used for editing, the save-excursion function
is very common. It saves the location of point and mark, executes the body
of the function, and then restores point and mark to their previous positions
if their locations were changed. Its primary purpose is to keep the user from
being surprised and disturbed by unexpected movement of point or mark.
Before discussing save-excursion, however, it may be useful first to
review what point and mark are in GNU Emacs. Point is the current location
of the cursor.
Wherever the cursor is, that is point.
More precisely, on
terminals where the cursor appears to be on top of a character, point is
immediately before the character. In Emacs Lisp, point is an integer. The
first character in a buffer is number one, the second is number two, and
so on. The function point returns the current position of the cursor as a
number. Each buffer has its own value for point.
" 7 0)) (63 (90.0 47.60907745361328 449.5309143066406 60.90726852416992 "Template for a save-excursion Expression
45
" 0 0) (89.999755859375 77.48908233642578 449.4543151855469 108.16980743408203 "The mark is another position in the buffer; its value can be set with a
command such as C-⟨
" 1 0) (190.9080047607422 92.75201416015625 207.5880126953125 93.23201751708984 "<image: None, width: 1, height: 1, bpc: 1>" 2 1) (190.9199981689453 93.96241760253906 207.4659423828125 101.93253326416016 "SPC
" 3 0) (190.9080047607422 101.3919677734375 207.5880126953125 101.8719711303711 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (89.99887084960938 89.00910186767578 449.5196533203125 168.04981994628906 "⟩ (set-mark-command). If a mark has been set, you
can use the command C-x C-x (exchange-point-and-mark) to cause the
cursor to jump to the mark and set the mark to be the previous position
of point. In addition, if you set another mark, the position of the previous
mark is saved in the mark ring. Many mark positions can be saved this way.
You can jump the cursor to a saved mark by typing C-u C-⟨
" 5 0) (371.14801025390625 152.6319580078125 387.8280029296875 153.11195373535156 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (371.1600036621094 153.84242248535156 387.7059631347656 161.81253051757812 "SPC
" 7 0) (371.14801025390625 161.1519775390625 387.8280029296875 161.63197326660156 "<image: None, width: 1, height: 1, bpc: 1>" 8 1) (89.994873046875 148.88909912109375 449.956298828125 483.3072814941406 "⟩ one or more
times.
The part of the buffer between point and mark is called the region. Nu-
merous commands work on the region, including center-region, count-
lines-region, kill-region, and print-region.
The save-excursion special form saves the locations of point and mark
and restores those positions after the code within the body of the special form
is evaluated by the Lisp interpreter. Thus, if point were in the beginning
of a piece of text and some code moved point to the end of the buffer, the
save-excursion would put point back to where it was before, after the
expressions in the body of the function were evaluated.
In Emacs, a function frequently moves point as part of its internal work-
ings even though a user would not expect this. For example, count-lines-
region moves point. To prevent the user from being bothered by jumps that
are both unexpected and (from the user’s point of view) unnecessary, save-
excursion is often used to keep point and mark in the location expected by
the user. The use of save-excursion is good housekeeping.
To make sure the house stays clean, save-excursion restores the values
of point and mark even if something goes wrong in the code inside of it (or,
to be more precise and to use the proper jargon, “in case of abnormal exit”).
This feature is very helpful.
In addition to recording the values of point and mark, save-excursion
keeps track of the current buffer, and restores it, too. This means you can
write code that will change the buffer and have save-excursion switch you
back to the original buffer. This is how save-excursion is used in append-
to-buffer.
(See Section 4.4, “The Definition of append-to-buffer”,
page 56.)
" 9 0) (89.994873046875 495.3618469238281 403.0967712402344 508.4774169921875 "3.10.1 Template for a save-excursion Expression
" 10 0) (104.99459838867188 516.569091796875 370.936279296875 529.8673095703125 "The template for code using save-excursion is simple:
" 11 0) (89.99346923828125 534.9376831054688 449.4045715332031 630.7872924804688 "(save-excursion
body...)
The body of the function is one or more expressions that will be evaluated
in sequence by the Lisp interpreter. If there is more than one expression
in the body, the value of the last one will be returned as the value of the
save-excursion function. The other expressions in the body are evaluated
only for their side effects; and save-excursion itself is used only for its side
effect (which is restoring the positions of point and mark).
" 12 0)) (64 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "46
Chapter 3: How To Write Function Definitions
" 0 0) (89.99957275390625 79.52906036376953 449.2681884765625 104.8272476196289 "In more detail, the template for a save-excursion expression looks like
this:
" 1 0) (89.99939727783203 109.77761840820312 449.3226013183594 221.5872039794922 "(save-excursion
first-expression-in-body
second-expression-in-body
third-expression-in-body
...
last-expression-in-body)
An expression, of course, may be a symbol on its own or a list.
In Emacs Lisp code, a save-excursion expression often occurs within
the body of a let expression. It looks like this:
" 2 0) (111.59974670410156 226.6551513671875 191.0677947998047 260.4638977050781 "(let varlist
(save-excursion
body...))
" 3 0) (89.99969482421875 275.18023681640625 177.9246826171875 289.540771484375 "3.11 Review
" 4 0) (89.99923706054688 297.32904052734375 449.355712890625 334.5072326660156 "In the last few chapters we have introduced a fair number of functions
and special forms. Here they are described in brief, along with a few similar
functions that have not been mentioned yet.
" 5 0) (89.99917602539062 339.8526611328125 449.5627746582031 411.0760498046875 "eval-last-sexp
Evaluate the last symbolic expression before the current location
of point. The value is printed in the echo area unless the function
is invoked with an argument; in that case, the output is printed
in the current buffer. This command is normally bound to C-x
C-e.
" 6 0) (89.99933624267578 414.56903076171875 449.5079650878906 478.2672119140625 "defun
Define function.
This special form has up to five parts: the
name, a template for the arguments that will be passed to the
function, documentation, an optional interactive declaration,
and the body of the definition.
For example:
" 7 0) (89.99830627441406 483.69757080078125 449.49609375 630.7872314453125 "(defun back-to-indentation ()
\"Move point to first visible character on line.\"
(interactive)
(beginning-of-line 1)
(skip-chars-forward \" \\t\"))
interactive
Declare to the interpreter that the function can be used interac-
tively. This special form may be followed by a string with one
or more parts that pass the information to the arguments of the
function, in sequence. These parts may also tell the interpreter
to prompt for information. Parts of the string are separated by
newlines, ‘\\n’.
" 8 0)) (65 (90.0 47.60907745361328 449.857666015625 60.90726852416992 "Review
47
" 0 0) (147.60000610351562 77.48908233642578 288.6217346191406 90.78726959228516 "Common code characters are:
" 1 0) (147.59974670410156 98.36908721923828 355.07952880859375 111.66727447509766 "b
The name of an existing buffer.
" 2 0) (147.59934997558594 119.12909698486328 341.30096435546875 132.4272918701172 "f
The name of an existing file.
" 3 0) (147.5989532470703 140.00909423828125 449.51861572265625 165.3072967529297 "p
The numeric prefix argument. (Note that this ‘p’ is
lower case.)
" 4 0) (147.59942626953125 172.76910400390625 449.55194091796875 210.0673065185547 "r
Point and the mark, as two numeric arguments,
smallest first. This is the only code letter that spec-
ifies two successive arguments rather than one.
" 5 0) (147.59896850585938 217.52911376953125 449.36614990234375 254.8273162841797 "See section “Code Characters for ‘interactive’” in The GNU
Emacs Lisp Reference Manual, for a complete list of code char-
acters.
" 6 0) (89.99897003173828 262.28912353515625 449.7151184082031 335.4673156738281 "let
Declare that a list of variables is for use within the body of the
let and give them an initial value, either nil or a specified
value; then evaluate the rest of the expressions in the body of
the let and return the value of the last one. Inside the body
of the let, the Lisp interpreter does not see the values of the
variables of the same names that are bound outside of the let.
" 7 0) (147.59890747070312 338.609130859375 209.08262634277344 351.9073181152344 "For example,
" 8 0) (169.19894409179688 359.377685546875 380.4963684082031 418.1440734863281 "(let ((foo (buffer-name))
(bar (buffer-size)))
(message
\"This buffer is %s and has %d characters.\"
foo bar))
" 9 0) (89.99915313720703 427.33282470703125 449.5732727050781 474.6673583984375 "save-excursion
Record the values of point and mark and the current buffer
before evaluating the body of this special form.
Restore the
values of point and mark and buffer afterward.
" 10 0) (147.59910583496094 477.9291687011719 209.08282470703125 491.22735595703125 "For example,
" 11 0) (169.1991424560547 498.6977233886719 399.27691650390625 544.984130859375 "(message \"We are %d characters into this buffer.\"
(- (point)
(save-excursion
(goto-char (point-min)) (point))))
" 12 0) (89.99880981445312 553.2891845703125 449.4642333984375 590.4674072265625 "if
Evaluate the first argument to the function; if it is true, evaluate
the second argument; else evaluate the third argument, if there
is one.
" 13 0) (147.59886169433594 593.6091918945312 449.3445739746094 630.7874145507812 "The if special form is called a conditional. There are other con-
ditionals in Emacs Lisp, but if is perhaps the most commonly
used.
" 14 0)) (66 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "48
Chapter 3: How To Write Function Definitions
" 0 0) (147.59933471679688 80.72907257080078 209.0830535888672 94.02725982666016 "For example,
" 1 0) (169.19937133789062 100.05758666992188 371.13720703125 158.8238983154297 "(if (string-equal
(number-to-string 21)
(substring (emacs-version) 10 12))
(message \"This is version 21 Emacs\")
(message \"This is not version 21 Emacs\"))
" 2 0) (89.99952697753906 165.3726348876953 449.58465576171875 357.9071960449219 "equal
eq
Test whether two objects are the same. equal uses one meaning
of the word ‘same’ and eq uses another: equal returns true if
the two objects have a similar structure and contents, such as
two copies of the same book. On the other hand, eq, returns
true if both arguments are actually the same object.
<
>
<=
>=
The < function tests whether its first argument is smaller than
its second argument. A corresponding function, >, tests whether
the first argument is greater than the second. Likewise, <= tests
whether the first argument is less than or equal to the second
and >= tests whether the first argument is greater than or equal
to the second. In all cases, both arguments must be numbers or
markers (markers indicate positions in buffers).
" 3 0) (90.000732421875 364.692626953125 449.51043701171875 552.7872314453125 "string<
string-lessp
string=
string-equal
The string-lessp function tests whether its first argument is
smaller than the second argument. A shorter, alternative name
for the same function (a defalias) is string<.
The arguments to string-lessp must be strings or symbols;
the ordering is lexicographic, so case is significant. The print
names of symbols are used instead of the symbols themselves.
An empty string, ‘\"\"’, a string with no characters in it, is smaller
than any string of characters.
string-equal provides the corresponding test for equality. Its
shorter, alternative name is string=. There are no string test
functions that correspond to >, >=, or <=.
" 4 0) (90.00051879882812 557.72900390625 449.6075134277344 630.7872314453125 "message
Print a message in the echo area. The first argument is a string
that can contain ‘%s’, ‘%d’, or ‘%c’ to print the value of arguments
that follow the string. The argument used by ‘%s’ must be a
string or a symbol; the argument used by ‘%d’ must be a number.
The argument used by ‘%c’ must be an ascii code number; it will
be printed as the character with that ascii code.
" 5 0)) (67 (90.0 47.60907745361328 449.857666015625 60.90726852416992 "Review
49
" 0 0) (90.0 79.33271789550781 449.48699951171875 162.5472869873047 "setq
set
The setq function sets the value of its first argument to the
value of the second argument. The first argument is automati-
cally quoted by setq. It does the same for succeeding pairs of
arguments. Another function, set, takes only two arguments
and evaluates both of them before setting the value returned by
its first argument to the value returned by its second argument.
" 1 0) (90.00051879882812 174.4927520751953 449.47625732421875 197.9473114013672 "buffer-name
Without an argument, return the name of the buffer, as a string.
" 2 0) (90.0 204.2527618408203 449.4541320800781 239.5873260498047 "buffer-file-name
Without an argument, return the name of the file the buffer is
visiting.
" 3 0) (89.9998550415039 251.6527862548828 449.5303955078125 286.9873352050781 "current-buffer
Return the buffer in which Emacs is active; it may not be the
buffer that is visible on the screen.
" 4 0) (89.9993896484375 299.05279541015625 449.5411376953125 346.3873596191406 "other-buffer
Return the most recently selected buffer (other than the buffer
passed to other-buffer as an argument and other than the
current buffer).
" 5 0) (89.99929809570312 358.33282470703125 449.51898193359375 393.7961730957031 "switch-to-buffer
Select a buffer for Emacs to be active in and display it in the
current window so users can look at it. Usually bound to C-x b.
" 6 0) (89.99923706054688 405.7327880859375 449.5954284667969 441.1873474121094 "set-buffer
Switch Emacs’ attention to a buffer on which programs will run.
Don’t alter what the window is showing.
" 7 0) (89.99992370605469 453.1328125 407.3122253417969 476.58734130859375 "buffer-size
Return the number of characters in the current buffer.
" 8 0) (90.00003051757812 486.80914306640625 449.39996337890625 523.9873657226562 "point
Return the value of the current position of the cursor, as an
integer counting the number of characters from the beginning of
the buffer.
" 9 0) (90.00067138671875 536.052734375 449.4986267089844 571.3873291015625 "point-min
Return the minimum permissible value of point in the current
buffer. This is 1, unless narrowing is in effect.
" 10 0) (90.00077819824219 583.4527587890625 449.47662353515625 630.787353515625 "point-max
Return the value of the maximum permissible value of point in
the current buffer. This is the end of the buffer, unless narrowing
is in effect.
" 11 0)) (68 (90.0 47.60907745361328 449.44342041015625 60.90726852416992 "50
Chapter 3: How To Write Function Definitions
" 0 0) (89.99932861328125 94.10033416748047 191.3380126953125 108.46088409423828 "3.12 Exercises
" 1 0) (98.99920654296875 114.20905303955078 449.69317626953125 178.2672576904297 "• Write a non-interactive function that doubles the value of its argument,
a number. Make that function interactive.
• Write a function that tests whether the current value of fill-column
is greater than the argument passed to the function, and if so, prints an
appropriate message.
" 2 0)) (69 (90.0 47.60907745361328 449.6835632324219 60.90726852416992 "Finding More Information
51
" 0 0) (89.99990844726562 75.14844512939453 385.9106140136719 92.38106536865234 "4 A Few Buffer–Related Functions
" 1 0) (89.99786376953125 106.64917755126953 449.40869140625 191.70738220214844 "In this chapter we study in detail several of the functions used in GNU
Emacs. This is called a “walk-through”. These functions are used as ex-
amples of Lisp code, but are not imaginary examples; with the exception of
the first, simplified function definition, these functions show the actual code
used in GNU Emacs. You can learn a great deal from these definitions. The
functions described here are all related to buffers. Later, we will study other
functions.
" 2 0) (89.9979248046875 210.740478515625 304.238525390625 225.10101318359375 "4.1 Finding More Information
" 3 0) (89.99746704101562 233.60919189453125 449.29949951171875 288.64990234375 "In this walk-through, I will describe each new function as we come to it,
sometimes in detail and sometimes briefly. If you are interested, you can get
the full documentation of any Emacs Lisp function at any time by typing
C-h f and then the name of the function (and then ⟨
" 4 0) (348.2279968261719 272.75201416015625 366.3479919433594 273.2320251464844 "<image: None, width: 1, height: 1, bpc: 1>" 5 1) (348.239990234375 274.0824279785156 366.3401184082031 282.0525207519531 "RET
" 6 0) (348.2279968261719 281.3919677734375 366.3479919433594 281.8719787597656 "<image: None, width: 1, height: 1, bpc: 1>" 7 1) (89.999755859375 269.48907470703125 449.7926940917969 312.52978515625 "⟩). Similarly, you
can get the full documentation for a variable by typing C-h v and then the
name of the variable (and then ⟨
" 8 0) (244.42800903320312 296.75201416015625 262.5480041503906 297.2320251464844 "<image: None, width: 1, height: 1, bpc: 1>" 9 1) (244.44000244140625 297.9624328613281 262.54010009765625 305.9325256347656 "RET
" 10 0) (244.42800903320312 305.27203369140625 262.5480041503906 305.7520446777344 "<image: None, width: 1, height: 1, bpc: 1>" 11 1) (90.00039672851562 293.36907958984375 449.4324951171875 351.0498046875 "⟩).
In versions 20 and higher, when a function is written in Emacs Lisp,
describe-function will also tell you the location of the function definition.
If you move point over the file name and press the ⟨
" 12 0) (341.7480163574219 335.2720031738281 359.8680114746094 335.75201416015625 "<image: None, width: 1, height: 1, bpc: 1>" 13 1) (341.760009765625 336.482421875 359.8601379394531 344.4525146484375 "RET
" 14 0) (341.7480163574219 343.7919921875 359.8680114746094 344.2720031738281 "<image: None, width: 1, height: 1, bpc: 1>" 15 1) (89.99880981445312 331.5290832519531 449.82550048828125 478.60980224609375 "⟩ key, which is this
case means help-follow rather than ‘return’ or ‘enter’, Emacs will take you
directly to the function definition.
More generally, if you want to see a function in its original source file,
you can use the find-tags function to jump to it. find-tags works with
a wide variety of languages, not just Lisp, and C, and it works with non-
programming text as well. For example, find-tags will jump to the various
nodes in the Texinfo source file of this document.
The find-tags function depends on ‘tags tables’ that record the locations
of the functions, variables, and other items to which find-tags jumps.
To use the find-tags command, type M-. (i.e., type the ⟨
" 16 0) (381.1080017089844 462.7120056152344 406.4280090332031 463.1920166015625 "<image: None, width: 1, height: 1, bpc: 1>" 17 1) (381.1199951171875 464.04241943359375 406.406494140625 472.01251220703125 "META
" 18 0) (381.1080017089844 471.35198974609375 406.4280090332031 471.8320007324219 "<image: None, width: 1, height: 1, bpc: 1>" 19 1) (90.00018310546875 459.0890808105469 450.0108337402344 490.48980712890625 "⟩ key and
the period key at the same time, or else type the ⟨
" 20 0) (337.4280090332031 474.7120056152344 354.1080017089844 475.1920166015625 "<image: None, width: 1, height: 1, bpc: 1>" 21 1) (337.44000244140625 475.92242431640625 353.9859619140625 483.89251708984375 "ESC
" 22 0) (337.4280090332031 483.35198974609375 354.1080017089844 483.8320007324219 "<image: None, width: 1, height: 1, bpc: 1>" 23 1) (90.00003051757812 470.9690856933594 449.90167236328125 526.3698120117188 "⟩ key and then type
the period key), and then, at the prompt, type in the name of the function
whose source code you want to see, such as mark-whole-buffer, and then
type ⟨
" 24 0) (118.30799865722656 510.59197998046875 136.42799377441406 511.0719909667969 "<image: None, width: 1, height: 1, bpc: 1>" 25 1) (118.31999969482422 511.8024597167969 136.4201202392578 519.7725830078125 "RET
" 26 0) (118.30799865722656 519.1119995117188 136.42799377441406 519.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 27 1) (90.00003051757812 507.2091064453125 449.4978332519531 549.8897705078125 "⟩. Emacs will switch buffers and display the source code for the
function on your screen. To switch back to your current buffer, type C-x b
⟨
" 28 0) (92.86800384521484 534.4719848632812 110.98800659179688 534.9519653320312 "<image: None, width: 1, height: 1, bpc: 1>" 29 1) (92.87999725341797 535.6824340820312 110.98011779785156 543.652587890625 "RET
" 30 0) (92.86800384521484 543.1119995117188 110.98800659179688 543.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 31 1) (110.5199966430664 531.089111328125 248.74658203125 550.2498168945312 "⟩. (On some keyboards, the ⟨
" 32 0) (248.0279998779297 534.4719848632812 273.3479919433594 534.9519653320312 "<image: None, width: 1, height: 1, bpc: 1>" 33 1) (248.0399932861328 535.6824340820312 273.3265075683594 543.652587890625 "META
" 34 0) (248.0279998779297 543.1119995117188 273.3479919433594 543.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 35 1) (272.760009765625 530.7290649414062 354.5865783691406 550.2498168945312 "⟩ key is labelled ⟨
" 36 0) (353.8680114746094 534.4719848632812 371.02801513671875 534.9519653320312 "<image: None, width: 1, height: 1, bpc: 1>" 37 1) (353.8800048828125 535.6824340820312 370.9014587402344 543.652587890625 "ALT
" 38 0) (353.8680114746094 543.1119995117188 371.02801513671875 543.5919799804688 "<image: None, width: 1, height: 1, bpc: 1>" 39 1) (89.99786376953125 531.089111328125 450.0957946777344 630.787353515625 "⟩.)
Depending on how the initial default values of your copy of Emacs are
set, you may also need to specify the location of your ‘tags table’, which
is a file called ‘TAGS’. For example, if you are interested in Emacs sources,
the tags table you will most likely want, if it has already been created for
you, will be in a subdirectory of the ‘/usr/local/share/emacs/’ direc-
tory; thus you would use the M-x visit-tags-table command and spec-
ify a pathname such as ‘/usr/local/share/emacs/21.0.100/lisp/TAGS’
" 40 0)) (70 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "52
Chapter 4: A Few Buffer–Related Functions
" 0 0) (89.99884033203125 77.48908233642578 449.21343994140625 102.66727447509766 "or ‘/usr/local/src/emacs/lisp/TAGS’. If the tags table has not already
been created, you will have to create it yourself.
" 1 0) (89.9990234375 107.12909698486328 449.3988342285156 156.1873016357422 "To create a ‘TAGS’ file in a specific directory, switch to that directory
in Emacs using M-x cd command, or list the directory with C-x d (dired).
Then run the compile command, with etags *.el as the command to exe-
cute
" 2 0) (111.59941864013672 164.85763549804688 252.54220581054688 173.8240203857422 "M-x compile RET etags *.el RET
" 3 0) (89.99945068359375 178.52911376953125 449.1486511230469 203.8273162841797 "For more information, see Section 12.5, “Create Your Own ‘TAGS’ File”,
page 163.
" 4 0) (89.99884033203125 208.04913330078125 449.3875732421875 245.2273406982422 "After you become more familiar with Emacs Lisp, you will find that you
will frequently use find-tags to navigate your way around source code; and
you will create your own ‘TAGS’ tables.
" 5 0) (89.99884033203125 249.56903076171875 449.3661193847656 370.3872375488281 "Incidentally, the files that contain Lisp code are conventionally called
libraries. The metaphor is derived from that of a specialized library, such as
a law library or an engineering library, rather than a general library. Each
library, or file, contains functions that relate to a particular topic or activity,
such as ‘abbrev.el’ for handling abbreviations and other typing shortcuts,
and ‘help.el’ for on-line help. (Sometimes several libraries provide code
for a single activity, as the various ‘rmail...’ files provide code for reading
electronic mail.) In The GNU Emacs Manual, you will see sentences such as
“The C-h p command lets you search the standard Emacs Lisp libraries by
topic keywords.”
" 6 0) (89.99909973144531 401.1803283691406 426.8679504394531 415.5535583496094 "4.2 A Simplified beginning-of-buffer Definition
" 7 0) (89.99945068359375 426.9290466308594 449.3887634277344 488.1160583496094 "The beginning-of-buffer command is a good function to start with
since you are likely to be familiar with it and it is easy to understand. Used
as an interactive command, beginning-of-buffer moves the cursor to the
beginning of the buffer, leaving the mark at the previous position.
It is
generally bound to M-<.
" 8 0) (89.99887084960938 492.32904052734375 449.34490966796875 553.3872680664062 "In this section, we will discuss a shortened version of the function that
shows how it is most frequently used.
This shortened function works as
written, but it does not contain the code for a complex option. In another
section, we will describe the entire function. (See Section 5.3, “Complete
Definition of beginning-of-buffer”, page 69.)
" 9 0) (89.99856567382812 557.7290649414062 449.453125 630.7872924804688 "Before looking at the code, let’s consider what the function definition has
to contain: it must include an expression that makes the function interactive
so it can be called by typing M-x beginning-of-buffer or by typing a
keychord such as C-<; it must include code to leave a mark at the original
position in the buffer; and it must include code to move the cursor to the
beginning of the buffer.
" 10 0)) (71 (90.0 47.60907745361328 449.5094299316406 60.90726852416992 "A Simplified beginning-of-buffer Definition
53
" 0 0) (105.00076293945312 80.36908721923828 420.15325927734375 93.66727447509766 "Here is the complete text of the shortened version of the function:
" 1 0) (90.00039672851562 99.57760620117188 449.3782043457031 277.2670593261719 "(defun simplified-beginning-of-buffer ()
\"Move point to the beginning of the buffer;
leave mark at previous position.\"
(interactive)
(push-mark)
(goto-char (point-min)))
Like all function definitions, this definition has five parts following the
special form defun:
1. The name: in this example, simplified-beginning-of-buffer.
2. A list of the arguments: in this example, an empty list, (),
3. The documentation string.
4. The interactive expression.
5. The body.
" 2 0) (89.99948120117188 282.56884765625 449.48712158203125 385.3870544433594 "In this function definition, the argument list is empty; this means that this
function does not require any arguments. (When we look at the definition
for the complete function, we will see that it may be passed an optional
argument.)
The interactive expression tells Emacs that the function is intended to be
used interactively. In this example, interactive does not have an argument
because simplified-beginning-of-buffer does not require one.
The body of the function consists of the two lines:
" 3 0) (89.99844360351562 391.1773986816406 449.5838317871094 636.6495971679688 "(push-mark)
(goto-char (point-min))
The first of these lines is the expression, (push-mark). When this ex-
pression is evaluated by the Lisp interpreter, it sets a mark at the current
position of the cursor, wherever that may be. The position of this mark is
saved in the mark ring.
The next line is (goto-char (point-min)). This expression jumps the
cursor to the minimum point in the buffer, that is, to the beginning of the
buffer (or to the beginning of the accessible portion of the buffer if it is
narrowed. See Chapter 6, “Narrowing and Widening”, page 77.)
The push-mark command sets a mark at the place where the cursor was
located before it was moved to the beginning of the buffer by the (goto-
char (point-min)) expression. Consequently, you can, if you wish, go back
to where you were originally by typing C-x C-x.
That is all there is to the function definition!
When you are reading code such as this and come upon an unfamiliar
function, such as goto-char, you can find out what it does by using the
describe-function command. To use this command, type C-h f and then
type in the name of the function and press ⟨
" 4 0) (300.947998046875 620.8720092773438 319.0679931640625 621.3519897460938 "<image: None, width: 1, height: 1, bpc: 1>" 5 1) (300.9599914550781 622.0823974609375 319.06011962890625 630.0525512695312 "RET
" 6 0) (300.947998046875 629.3919677734375 319.0679931640625 629.8719482421875 "<image: None, width: 1, height: 1, bpc: 1>" 7 1) (318.6000061035156 617.4890747070312 450.3489074707031 636.289794921875 "⟩. The describe-function
" 8 0)) (72 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "54
Chapter 4: A Few Buffer–Related Functions
" 0 0) (89.9993896484375 77.48908233642578 449.35546875 102.66727447509766 "command will print the function’s documentation string in a ‘*Help*’ win-
dow. For example, the documentation for goto-char is:
" 1 0) (89.99880981445312 108.81759643554688 449.3445739746094 188.9297637939453 "One arg, a number.
Set point to that number.
Beginning of buffer is position (point-min),
end is (point-max).
(The prompt for describe-function will offer you the symbol under or
preceding the cursor, so you can save typing by positioning the cursor right
over or after the function and then typing C-h f ⟨
" 2 0) (327.1080017089844 173.1519775390625 345.2279968261719 173.63197326660156 "<image: None, width: 1, height: 1, bpc: 1>" 3 1) (327.1199951171875 174.36244201660156 345.2201232910156 182.33255004882812 "RET
" 4 0) (327.1080017089844 181.6719970703125 345.2279968261719 182.15199279785156 "<image: None, width: 1, height: 1, bpc: 1>" 5 1) (89.99908447265625 169.76904296875 449.6611022949219 233.94725036621094 "⟩.)
The end-of-buffer function definition is written in the same way as
the beginning-of-buffer definition except that the body of the function
contains the expression (goto-char (point-max)) in place of (goto-char
(point-min)).
" 6 0) (89.99909210205078 254.90032958984375 371.3376159667969 269.2735595703125 "4.3 The Definition of mark-whole-buffer
" 7 0) (89.999267578125 278.1290283203125 449.4877014160156 381.5472106933594 "The mark-whole-buffer function is no harder to understand than the
simplified-beginning-of-buffer function. In this case, however, we will
look at the complete function, not a shortened version.
The mark-whole-buffer function is not as commonly used as the
beginning-of-buffer function, but is useful nonetheless: it marks a whole
buffer as a region by putting point at the beginning and a mark at the end
of the buffer. It is generally bound to C-x h.
In GNU Emacs 20, the code for the complete function looks like this:
" 8 0) (90.00177001953125 387.69757080078125 449.4015808105469 487.6272277832031 "(defun mark-whole-buffer ()
\"Put point at beginning and mark at end of buffer.\"
(interactive)
(push-mark (point))
(push-mark (point-max))
(goto-char (point-min)))
Like all other functions, the mark-whole-buffer function fits into the
template for a function definition. The template looks like this:
" 9 0) (90.00222778320312 493.7751770019531 449.6968078613281 630.7872314453125 "(defun name-of-function (argument-list)
\"documentation...\"
(interactive-expression...)
body...)
Here is how the function works: the name of the function is mark-whole-
buffer; it is followed by an empty argument list, ‘()’, which means that the
function does not require arguments. The documentation comes next.
The next line is an (interactive) expression that tells Emacs that
the function will be used interactively.
These details are similar to the
simplified-beginning-of-buffer function described in the previous sec-
tion.
" 10 0)) (73 (90.0 47.60907745361328 449.6732482910156 60.90726852416992 "Body of mark-whole-buffer
55
" 0 0) (90.00057983398438 78.2418212890625 297.62017822265625 91.35738372802734 "4.3.1 Body of mark-whole-buffer
" 1 0) (90.00100708007812 100.16907501220703 449.3463134765625 125.4672622680664 "The body of the mark-whole-buffer function consists of three lines of
code:
" 2 0) (89.99725341796875 131.13760375976562 449.5634460449219 445.369873046875 "(push-mark (point))
(push-mark (point-max))
(goto-char (point-min))
The first of these lines is the expression, (push-mark (point)).
This line does exactly the same job as the first line of the body of
the simplified-beginning-of-buffer function, which is written (push-
mark). In both cases, the Lisp interpreter sets a mark at the current position
of the cursor.
I don’t know why the expression in mark-whole-buffer is written (push-
mark (point)) and the expression in beginning-of-buffer is written
(push-mark). Perhaps whoever wrote the code did not know that the ar-
guments for push-mark are optional and that if push-mark is not passed an
argument, the function automatically sets mark at the location of point by
default. Or perhaps the expression was written so as to parallel the struc-
ture of the next line. In any case, the line causes Emacs to determine the
position of point and set a mark there.
The next line of mark-whole-buffer is (push-mark (point-max). This
expression sets a mark at the point in the buffer that has the highest number.
This will be the end of the buffer (or, if the buffer is narrowed, the end of the
accessible portion of the buffer. See Chapter 6, “Narrowing and Widening”,
page 77, for more about narrowing.)
After this mark has been set, the
previous mark, the one set at point, is no longer set, but Emacs remembers
its position, just as all other recent marks are always remembered. This
means that you can, if you wish, go back to that position by typing C-u
C-⟨
" 3 0) (104.38800048828125 429.9519958496094 121.06800079345703 430.4320068359375 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (104.4000015258789 431.1624450683594 120.9459457397461 439.1325378417969 "SPC
" 5 0) (104.38800048828125 438.47198486328125 121.06800079345703 438.9519958496094 "<image: None, width: 1, height: 1, bpc: 1>" 6 1) (89.99981689453125 426.2090759277344 449.3128356933594 466.5072937011719 "⟩ twice.
(In GNU Emacs 21, the (push-mark (point-max) is slightly more com-
plicated than shown here. The line reads
" 7 0) (89.99786376953125 472.2976379394531 449.976318359375 630.787353515625 "(push-mark (point-max) nil t)
(The expression works nearly the same as before.
It sets a mark at the
highest numbered place in the buffer that it can. However, in this version,
push-mark has two additional arguments. The second argument to push-
mark is nil. This tells the function it should display a message that says
‘Mark set’ when it pushes the mark. The third argument is t. This tells
push-mark to activate the mark when Transient Mark mode is turned on.
Transient Mark mode highlights the currently active region. It is usually
turned off.)
Finally, the last line of the function is (goto-char (point-min))). This
is written exactly the same way as it is written in beginning-of-buffer.
The expression moves the cursor to the minimum point in the buffer, that is,
to the beginning of the buffer (or to the beginning of the accessible portion
" 8 0)) (74 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "56
Chapter 4: A Few Buffer–Related Functions
" 0 0) (89.99887084960938 77.48908233642578 449.39886474609375 114.66727447509766 "of the buffer). As a result of this, point is placed at the beginning of the
buffer and mark is set at the end of the buffer. The whole buffer is, therefore,
the region.
" 1 0) (89.99887084960938 141.5003662109375 363.90606689453125 155.8735809326172 "4.4 The Definition of append-to-buffer
" 2 0) (89.99874877929688 166.1690673828125 449.48590087890625 215.34727478027344 "The append-to-buffer command is very nearly as simple as the mark-
whole-buffer command. What it does is copy the region (that is, the part
of the buffer between point and mark) from the current buffer to a specified
buffer.
" 3 0) (89.99859619140625 218.60906982421875 450.0857238769531 303.6672668457031 "The append-to-buffer command uses the insert-buffer-substring
function to copy the region. insert-buffer-substring is described by its
name: it takes a string of characters from part of a buffer, a “substring”, and
inserts them into another buffer. Most of append-to-buffer is concerned
with setting up the conditions for insert-buffer-substring to work: the
code must specify both the buffer to which the text will go and the region
that will be copied. Here is the complete text of the function:
" 4 0) (111.59902954101562 311.2575988769531 360.5087585449219 345.06396484375 "(defun append-to-buffer (buffer start end)
\"Append to specified buffer the text of the region.
It is inserted into that buffer before its point.
" 5 0) (111.59835815429688 353.2575988769531 388.7680358886719 449.4641418457031 "When calling from a program, give three arguments:
a buffer or the name of one, and two character numbers
specifying the portion of the current buffer to be copied.\"
(interactive \"BAppend to buffer: \\nr\")
(let ((oldbuf (current-buffer)))
(save-excursion
(set-buffer (get-buffer-create buffer))
(insert-buffer-substring oldbuf start end))))
" 6 0) (89.99880981445312 453.3292236328125 449.32196044921875 478.6274108886719 "The function can be understood by looking at it as a series of filled-in
templates.
" 7 0) (89.99908447265625 481.88922119140625 449.3765869140625 507.1874084472656 "The outermost template is for the function definition. In this function,
it looks like this (with several slots filled in):
" 8 0) (111.59933471679688 514.7777709960938 308.9515075683594 561.064208984375 "(defun append-to-buffer (buffer start end)
\"documentation...\"
(interactive \"BAppend to buffer: \\nr\")
body...)
" 9 0) (89.99948120117188 565.0491943359375 449.4534912109375 602.2274169921875 "The first line of the function includes its name and three arguments. The
arguments are the buffer to which the text will be copied, and the start
and end of the region in the current buffer that will be copied.
" 10 0) (89.99887084960938 605.4892578125 449.4425354003906 630.7874755859375 "The next part of the function is the documentation, which is clear and
complete.
" 11 0)) (75 (90.0 47.60907745361328 449.6504821777344 60.90726852416992 "The Body of append-to-buffer
57
" 0 0) (89.99981689453125 78.2418212890625 413.8818054199219 91.35738372802734 "4.4.1 The append-to-buffer Interactive Expression
" 1 0) (89.99908447265625 101.60907745361328 449.7373962402344 150.7872772216797 "Since the append-to-buffer function will be used interactively, the func-
tion must have an interactive expression. (For a review of interactive,
see Section 3.3, “Making a Function Interactive”, page 33.) The expression
reads as follows:
" 2 0) (89.99609375 158.01760864257812 449.4083251953125 323.5873107910156 "(interactive \"BAppend to buffer: \\nr\")
This expression has an argument inside of quotation marks and that argu-
ment has two parts, separated by ‘\\n’.
The first part is ‘BAppend to buffer: ’. Here, the ‘B’ tells Emacs to ask
for the name of the buffer that will be passed to the function. Emacs will
ask for the name by prompting the user in the minibuffer, using the string
following the ‘B’, which is the string ‘Append to buffer: ’.
Emacs then
binds the variable buffer in the function’s argument list to the specified
buffer.
The newline, ‘\\n’, separates the first part of the argument from the second
part. It is followed by an ‘r’ that tells Emacs to bind the two arguments
that follow the symbol buffer in the function’s argument list (that is, start
and end) to the values of point and mark.
" 3 0) (89.99641418457031 343.5618591308594 321.0094909667969 356.67742919921875 "4.4.2 The Body of append-to-buffer
" 4 0) (89.99420166015625 367.0491027832031 449.4703063964844 484.3872985839844 "The body of the append-to-buffer function begins with let.
As we have seen before (see Section 3.6, “let”, page 36), the purpose of
a let expression is to create and give initial values to one or more variables
that will only be used within the body of the let. This means that such
a variable will not be confused with any variable of the same name outside
the let expression.
We can see how the let expression fits into the function as a whole
by showing a template for append-to-buffer with the let expression in
outline:
" 5 0) (111.59420013427734 491.6176452636719 308.94635009765625 550.384033203125 "(defun append-to-buffer (buffer start end)
\"documentation...\"
(interactive \"BAppend to buffer: \\nr\")
(let ((variable value))
body...)
" 6 0) (104.99437713623047 554.0091552734375 291.63775634765625 567.307373046875 "The let expression has three elements:
" 7 0) (95.87387084960938 571.1691284179688 191.51376342773438 584.4673461914062 "1. The symbol let;
" 8 0) (95.87383270263672 588.3291015625 449.2298278808594 613.6273193359375 "2. A varlist containing, in this case, a single two-element list, (variable
value);
" 9 0) (95.87357330322266 617.4891357421875 265.2915954589844 630.787353515625 "3. The body of the let expression.
" 10 0)) (76 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "58
Chapter 4: A Few Buffer–Related Functions
" 0 0) (104.9993896484375 86.24909210205078 398.3338623046875 99.54727935791016 "In the append-to-buffer function, the varlist looks like this:
" 1 0) (111.59976196289062 111.21762084960938 228.67115783691406 120.18400573730469 "(oldbuf (current-buffer))
" 2 0) (89.99960327148438 128.00909423828125 449.46478271484375 177.1873016357422 "In this part of the let expression, the one variable, oldbuf, is bound to the
value returned by the (current-buffer) expression. The variable, oldbuf,
is used to keep track of the buffer in which you are working and from which
you will copy.
" 3 0) (89.99981689453125 184.52911376953125 449.464599609375 233.7073211669922 "The element or elements of a varlist are surrounded by a set of parentheses
so the Lisp interpreter can distinguish the varlist from the body of the let.
As a consequence, the two-element list within the varlist is surrounded by a
circumscribing set of parentheses. The line looks like this:
" 4 0) (111.60049438476562 245.37765502929688 261.50067138671875 266.8240051269531 "(let ((oldbuf (current-buffer)))
... )
" 5 0) (89.99920654296875 274.88909912109375 449.2578125 324.0672912597656 "The two parentheses before oldbuf might surprise you if you did not realize
that the first parenthesis before oldbuf marks the boundary of the varlist
and the second parenthesis marks the beginning of the two-element list,
(oldbuf (current-buffer)).
" 6 0) (89.99931335449219 357.60186767578125 351.45758056640625 370.7174377441406 "4.4.3 save-excursion in append-to-buffer
" 7 0) (89.99945068359375 385.40911865234375 449.45306396484375 410.7073059082031 "The body of the let expression in append-to-buffer consists of a save-
excursion expression.
" 8 0) (89.99969482421875 418.04913330078125 449.4207763671875 479.1073303222656 "The save-excursion function saves the locations of point and mark,
and restores them to those positions after the expressions in the body of the
save-excursion complete execution. In addition, save-excursion keeps
track of the original buffer, and restores it. This is how save-excursion is
used in append-to-buffer.
" 9 0) (90.00015258789062 486.56915283203125 449.4225158691406 547.7473754882812 "Incidentally, it is worth noting here that a Lisp function is normally for-
matted so that everything that is enclosed in a multi-line spread is indented
more to the right than the first symbol. In this function definition, the let
is indented more than the defun, and the save-excursion is indented more
than the let, like this:
" 10 0) (111.60150146484375 559.417724609375 200.54954528808594 630.6640625 "(defun ...
...
...
(let...
(save-excursion
...
" 11 0)) (77 (90.0 47.60907745361328 449.57415771484375 60.90726852416992 "save-excursion in append-to-buffer
59
" 0 0) (89.99896240234375 81.68909454345703 449.3990783691406 130.74729919433594 "This formatting convention makes it easy to see that the two lines in the
body of the save-excursion are enclosed by the parentheses associated
with save-excursion, just as the save-excursion itself is enclosed by the
parentheses associated with the let:
" 1 0) (111.59886169433594 137.85763549804688 341.7973327636719 184.1439666748047 "(let ((oldbuf (current-buffer)))
(save-excursion
(set-buffer (get-buffer-create buffer))
(insert-buffer-substring oldbuf start end))))
" 2 0) (89.99932861328125 188.60906982421875 449.3766174316406 213.9072723388672 "The use of the save-excursion function can be viewed as a process of
filling in the slots of a template:
" 3 0) (111.59921264648438 221.01760864257812 226.0409698486328 279.7839050292969 "(save-excursion
first-expression-in-body
second-expression-in-body
...
last-expression-in-body)
" 4 0) (89.99874877929688 284.2490234375 449.3550109863281 309.4272155761719 "In this function, the body of the save-excursion contains only two expres-
sions. The body looks like this:
" 5 0) (89.99810791015625 316.5375671386719 449.7251281738281 498.5472412109375 "(set-buffer (get-buffer-create buffer))
(insert-buffer-substring oldbuf start end)
When the append-to-buffer function is evaluated, the two expressions
in the body of the save-excursion are evaluated in sequence. The value of
the last expression is returned as the value of the save-excursion function;
the other expression is evaluated only for its side effects.
The first line in the body of the save-excursion uses the set-buffer
function to change the current buffer to the one specified in the first argument
to append-to-buffer. (Changing the buffer is the side effect; as we have
said before, in Lisp, a side effect is often the primary thing we want.) The
second line does the primary work of the function.
The set-buffer function changes Emacs’ attention to the buffer to which
the text will be copied and from which save-excursion will return.
The line looks like this:
" 6 0) (89.99884033203125 505.5376281738281 449.6275634765625 630.7872924804688 "(set-buffer (get-buffer-create buffer))
The innermost expression of this list is (get-buffer-create buffer).
This expression uses the get-buffer-create function, which either gets the
named buffer, or if it does not exist, creates one with the given name. This
means you can use append-to-buffer to put text into a buffer that did not
previously exist.
get-buffer-create also keeps set-buffer from getting an unnecessary
error: set-buffer needs a buffer to go to; if you were to specify a buffer that
does not exist, Emacs would baulk. Since get-buffer-create will create a
buffer if none exists, set-buffer is always provided with a buffer.
" 7 0)) (78 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "60
Chapter 4: A Few Buffer–Related Functions
" 0 0) (104.9993896484375 81.68909454345703 449.27874755859375 94.9872817993164 "The last line of append-to-buffer does the work of appending the text:
" 1 0) (89.99887084960938 102.21762084960938 449.83551025390625 231.90733337402344 "(insert-buffer-substring oldbuf start end)
The insert-buffer-substring function copies a string from the buffer
specified as its first argument and inserts the string into the present buffer.
In this case, the argument to insert-buffer-substring is the value of the
variable created and bound by the let, namely the value of oldbuf, which
was the current buffer when you gave the append-to-buffer command.
After insert-buffer-substring has done its work, save-excursion
will restore the action to the original buffer and append-to-buffer will
have done its job.
Written in skeletal form, the workings of the body look like this:
" 2 0) (111.5992431640625 239.0152587890625 392.71527099609375 285.3039855957031 "(let (bind-oldbuf-to-value-of-current-buffer)
(save-excursion
; Keep track of buffer.
change-buffer
insert-substring-from-oldbuf-into-buffer)
" 3 0) (111.59930419921875 301.29522705078125 337.39544677734375 322.7439880371094 "change-back-to-original-buffer-when-finished
let-the-local-meaning-of-oldbuf-disappear-when-finished
" 4 0) (89.99819946289062 338.72906494140625 449.51812744140625 451.8672790527344 "In summary, append-to-buffer works as follows: it saves the value of the
current buffer in the variable called oldbuf. It gets the new buffer, creating
one if need be, and switches Emacs to it. Using the value of oldbuf, it
inserts the region of text from the old buffer into the new buffer; and then
using save-excursion, it brings you back to your original buffer.
In looking at append-to-buffer, you have explored a fairly complex
function. It shows how to use let and save-excursion, and how to change
to and come back from another buffer. Many function definitions use let,
save-excursion, and set-buffer this way.
" 5 0) (89.99848937988281 476.9003601074219 169.8894805908203 491.2608947753906 "4.5 Review
" 6 0) (104.99848937988281 501.2090759277344 449.2343444824219 514.5072631835938 "Here is a brief summary of the various functions discussed in this chapter.
" 7 0) (89.99807739257812 525.252685546875 449.2018127441406 572.5960693359375 "describe-function
describe-variable
Print the documentation for a function or variable. Convention-
ally bound to C-h f and C-h v.
" 8 0) (89.9984130859375 581.609130859375 449.4854736328125 636.6498413085938 "find-tag
Find the file containing the source for a function or variable and
switch buffers to it, positioning point at the beginning of the
item. Conventionally bound to M-. (that’s a period following
the ⟨
" 9 0) (169.30799865722656 620.8720092773438 194.62799072265625 621.3519897460938 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (169.32000732421875 622.0823974609375 194.6065216064453 630.0525512695312 "META
" 11 0) (169.30799865722656 629.3919677734375 194.62799072265625 629.8719482421875 "<image: None, width: 1, height: 1, bpc: 1>" 12 1) (194.0399932861328 617.1290893554688 224.3890838623047 636.289794921875 "⟩ key).
" 13 0)) (79 (90.0 47.60907745361328 449.8256530761719 60.90726852416992 "Exercises
61
" 0 0) (90.0 79.33271789550781 449.3671569824219 126.66727447509766 "save-excursion
Save the location of point and mark and restore their values after
the arguments to save-excursion have been evaluated. Also,
remember the current buffer and return to it.
" 1 0) (89.99972534179688 133.0927276611328 449.4967346191406 192.4272918701172 "push-mark
Set mark at a location and record the value of the previous mark
on the mark ring. The mark is a location in the buffer that will
keep its relative position even if text is added to or removed
from the buffer.
" 2 0) (89.9984359741211 198.8527374267578 449.5509033203125 246.1873016357422 "goto-char
Set point to the location specified by the value of the argument,
which can be a number, a marker, or an expression that returns
the number of a position, such as (point-min).
" 3 0) (89.99832153320312 252.6127471923828 449.38671875 288.0672912597656 "insert-buffer-substring
Copy a region of text from a buffer that is passed to the function
as an argument and insert the region into the current buffer.
" 4 0) (89.99722290039062 294.49273681640625 441.1276550292969 317.95611572265625 "mark-whole-buffer
Mark the whole buffer as a region. Normally bound to C-x h.
" 5 0) (89.9969482421875 324.37274169921875 449.5276184082031 371.7073059082031 "set-buffer
Switch the attention of Emacs to another buffer, but do not
change the window being displayed. Used when the program
rather than a human is to work on a different buffer.
" 6 0) (89.99609375 378.13275146484375 449.46087646484375 437.4673156738281 "get-buffer-create
get-buffer
Find a named buffer or create one if a buffer of that name does
not exist. The get-buffer function returns nil if the named
buffer does not exist.
" 7 0) (89.99528503417969 455.660400390625 183.29995727539062 470.02093505859375 "4.6 Exercises
" 8 0) (98.99493408203125 475.7691345214844 449.3077697753906 550.6928100585938 "• Write your own simplified-end-of-buffer function definition; then
test it to see whether it works.
• Use if and get-buffer to write a function that prints a message telling
you whether a buffer exists.
• Using find-tag, find the source for the copy-to-buffer function.
" 9 0)) (80 (90.0 47.60907745361328 449.46527099609375 60.90726852416992 "62
Chapter 4: A Few Buffer–Related Functions
" 0 0)) (81 (90.0 47.60907745361328 449.6177673339844 60.90726852416992 "The Definition of copy-to-buffer
63
" 0 0) (90.0001220703125 75.14844512939453 384.7303466796875 92.38106536865234 "5 A Few More Complex Functions
" 1 0) (89.9990234375 107.36908721923828 449.5304260253906 180.4272918701172 "In this chapter, we build on what we have learned in previous chapters by
looking at more complex functions. The copy-to-buffer function illustrates
use of two save-excursion expressions in one definition, while the insert-
buffer function illustrates use of an asterisk in an interactive expression,
use of or, and the important distinction between a name and the object to
which the name refers.
" 2 0) (89.99909210205078 200.06036376953125 349.16363525390625 214.43357849121094 "5.1 The Definition of copy-to-buffer
" 3 0) (89.99795532226562 223.049072265625 449.5188903808594 322.8672790527344 "After understanding how append-to-buffer works, it is easy to under-
stand copy-to-buffer.
This function copies text into a buffer, but in-
stead of adding to the second buffer, it replaces the previous text in the
second buffer.
The code for the copy-to-buffer function is almost the
same as the code for append-to-buffer, except that erase-buffer and
a second save-excursion are used. (See Section 4.4, “The Definition of
append-to-buffer”, page 56, for the description of append-to-buffer.)
The body of copy-to-buffer looks like this
" 4 0) (89.998291015625 328.6576232910156 449.4534606933594 553.6273803710938 "...
(interactive \"BCopy to buffer: \\nr\")
(let ((oldbuf (current-buffer)))
(save-excursion
(set-buffer (get-buffer-create buffer))
(erase-buffer)
(save-excursion
(insert-buffer-substring oldbuf start end)))))
This code is similar to the code in append-to-buffer: it is only after
changing to the buffer to which the text will be copied that the definition
for this function diverges from the definition for append-to-buffer: the
copy-to-buffer function erases the buffer’s former contents. (This is what
is meant by ‘replacement’; to replace text, Emacs erases the previous text
and then inserts new text.) After erasing the previous contents of the buffer,
save-excursion is used for a second time and the new text is inserted.
Why is save-excursion used twice? Consider again what the function
does.
In outline, the body of copy-to-buffer looks like this:
" 5 0) (111.599365234375 559.415283203125 368.859130859375 630.6640625 "(let (bind-oldbuf-to-value-of-current-buffer)
(save-excursion
; First use of save-excursion.
change-buffer
(erase-buffer)
(save-excursion
; Second use of save-excursion.
insert-substring-from-oldbuf-into-buffer)))
" 6 0)) (82 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "64
Chapter 5: A Few More Complex Functions
" 0 0) (89.99850463867188 77.48908233642578 449.857421875 198.3072967529297 "The first use of save-excursion returns Emacs to the buffer from which
the text is being copied. That is clear, and is just like its use in append-
to-buffer.
Why the second use?
The reason is that insert-buffer-
substring always leaves point at the end of the region being inserted. The
second save-excursion causes Emacs to leave point at the beginning of the
text being inserted. In most circumstances, users prefer to find point at the
beginning of inserted text. (Of course, the copy-to-buffer function returns
the user to the original buffer when done—but if the user then switches to
the copied-to buffer, point will go to the beginning of the text. Thus, this
use of a second save-excursion is a little nicety.)
" 1 0) (89.99812316894531 224.06036376953125 341.7313537597656 238.43357849121094 "5.2 The Definition of insert-buffer
" 2 0) (89.9962158203125 248.48907470703125 449.5498352050781 337.8672790527344 "insert-buffer is yet another buffer-related function.
This command
copies another buffer into the current buffer. It is the reverse of append-
to-buffer or copy-to-buffer, since they copy a region of text from the
current buffer to another buffer.
In addition, this code illustrates the use of interactive with a buffer
that might be read-only and the important distinction between the name of
an object and the object actually referred to.
" 3 0) (104.99609375 341.00909423828125 184.80690002441406 354.3072814941406 "Here is the code:
" 4 0) (111.59607696533203 361.6576232910156 341.5971374511719 547.0241088867188 "(defun insert-buffer (buffer)
\"Insert after point the contents of BUFFER.
Puts mark after the inserted text.
BUFFER may be a buffer or a buffer name.\"
(interactive \"*bInsert buffer: \")
(or (bufferp buffer)
(setq buffer (get-buffer buffer)))
(let (start end newmark)
(save-excursion
(save-excursion
(set-buffer buffer)
(setq start (point-min) end (point-max)))
(insert-buffer-substring buffer start end)
(setq newmark (point)))
(push-mark newmark)))
" 5 0) (89.99786376953125 551.7291259765625 449.3534240722656 577.02734375 "As with other function definitions, you can use a template to see an
outline of the function:
" 6 0) (111.59786987304688 584.2577514648438 276.1382141113281 630.6640625 "(defun insert-buffer (buffer)
\"documentation...\"
(interactive \"*bInsert buffer: \")
body...)
" 7 0)) (83 (90.0 47.60907745361328 449.5639343261719 60.90726852416992 "The Body of the insert-buffer Function
65
" 0 0) (90.00027465820312 78.2418212890625 410.762451171875 91.35738372802734 "5.2.1 The Interactive Expression in insert-buffer
" 1 0) (89.99972534179688 101.48908233642578 449.36712646484375 126.78726959228516 "In insert-buffer, the argument to the interactive declaration has
two parts, an asterisk, ‘*’, and ‘bInsert buffer: ’.
" 2 0) (89.99920654296875 146.40179443359375 216.149658203125 159.5057830810547 "A Read-only Buffer
" 3 0) (89.99893188476562 169.6490478515625 449.48577880859375 242.82725524902344 "The asterisk is for the situation when the buffer is a read-only buffer—
a buffer that cannot be modified. If insert-buffer is called on a buffer
that is read-only, a message to this effect is printed in the echo area and
the terminal may beep or blink at you; you will not be permitted to insert
anything into current buffer. The asterisk does not need to be followed by a
newline to separate it from the next argument.
" 4 0) (89.99984741210938 262.4417724609375 290.81427001953125 275.5573425292969 "‘b’ in an Interactive Expression
" 5 0) (89.998291015625 285.68902587890625 449.85748291015625 406.6272277832031 "The next argument in the interactive expression starts with a lower case
‘b’. (This is different from the code for append-to-buffer, which uses an
upper-case ‘B’.
See Section 4.4, “The Definition of append-to-buffer”,
page 56.) The lower-case ‘b’ tells the Lisp interpreter that the argument for
insert-buffer should be an existing buffer or else its name. (The upper-
case ‘B’ option provides for the possibility that the buffer does not exist.)
Emacs will prompt you for the name of the buffer, offering you a default
buffer, with name completion enabled.
If the buffer does not exist, you
receive a message that says “No match”; your terminal may beep at you as
well.
" 6 0) (89.998291015625 426.3617858886719 387.6724853515625 439.47735595703125 "5.2.2 The Body of the insert-buffer Function
" 7 0) (89.997802734375 449.6090393066406 449.5071105957031 538.8673095703125 "The body of the insert-buffer function has two major parts: an or
expression and a let expression. The purpose of the or expression is to
ensure that the argument buffer is bound to a buffer and not just the name
of a buffer. The body of the let expression contains the code which copies
the other buffer into the current buffer.
In outline, the two expressions fit into the insert-buffer function like
this:
" 8 0) (111.59844207763672 545.8576049804688 276.1387634277344 630.6640014648438 "(defun insert-buffer (buffer)
\"documentation...\"
(interactive \"*bInsert buffer: \")
(or ...
...
(let (varlist)
body-of-let... )
" 9 0)) (84 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "66
Chapter 5: A Few More Complex Functions
" 0 0) (89.99993896484375 77.48908233642578 449.58526611328125 141.0673065185547 "To understand how the or expression ensures that the argument buffer
is bound to a buffer and not to the name of a buffer, it is first necessary to
understand the or function.
Before doing this, let me rewrite this part of the function using if so that
you can see what is done in a manner that will be familiar.
" 1 0) (89.99993896484375 156.0018310546875 399.27911376953125 169.1173858642578 "5.2.3 insert-buffer With an if Instead of an or
" 2 0) (89.99954223632812 177.6890869140625 449.4432678222656 279.7873229980469 "The job to be done is to make sure the value of buffer is a buffer itself
and not the name of a buffer. If the value is the name, then the buffer itself
must be got.
You can imagine yourself at a conference where an usher is wandering
around holding a list with your name on it and looking for you: the usher is
“bound” to your name, not to you; but when the usher finds you and takes
your arm, the usher becomes “bound” to you.
In Lisp, you might describe this situation like this:
" 3 0) (90.00009155273438 285.2176818847656 449.4981994628906 360.3073425292969 "(if (not (holding-on-to-guest))
(find-and-take-arm-of-guest))
We want to do the same thing with a buffer—if we do not have the buffer
itself, we want to get it.
Using a predicate called bufferp that tells us whether we have a buffer
(rather than its name), we can write the code like this:
" 4 0) (90.00030517578125 365.85528564453125 449.466064453125 527.107421875 "(if (not (bufferp buffer))
; if-part
(setq buffer (get-buffer buffer)))
; then-part
Here, the true-or-false-test of the if expression is (not (bufferp buffer));
and the then-part is the expression (setq buffer (get-buffer buffer)).
In the test, the function bufferp returns true if its argument is a buffer—
but false if its argument is the name of the buffer. (The last character of
the function name bufferp is the character ‘p’; as we saw earlier, such use
of ‘p’ is a convention that indicates that the function is a predicate, which is
a term that means that the function will determine whether some property
is true or false. See Section 1.8.4, “Using the Wrong Type Object as an
Argument”, page 14.)
The function not precedes the expression (bufferp buffer), so the true-
or-false-test looks like this:
" 5 0) (89.99822998046875 532.6577758789062 449.35491943359375 630.7874145507812 "(not (bufferp buffer))
not is a function that returns true if its argument is false and false if its
argument is true. So if (bufferp buffer) returns true, the not expression
returns false and vice-versa: what is “not true” is false and what is “not
false” is true.
Using this test, the if expression works as follows: when the value of
the variable buffer is actually a buffer rather then its name, the true-or-
false-test returns false and the if expression does not evaluate the then-part.
" 6 0)) (85 (90.0 47.60907745361328 449.7704772949219 60.90726852416992 "The or in the Body
67
" 0 0) (89.999755859375 77.48908233642578 449.2685241699219 102.66727447509766 "This is fine, since we do not need to do anything to the variable buffer if it
really is a buffer.
" 1 0) (89.99874877929688 108.68909454345703 449.5626525878906 193.74729919433594 "On the other hand, when the value of buffer is not a buffer itself, but
the name of a buffer, the true-or-false-test returns true and the then-part
of the expression is evaluated. In this case, the then-part is (setq buffer
(get-buffer buffer)). This expression uses the get-buffer function to
return an actual buffer itself, given its name. The setq then sets the variable
buffer to the value of the buffer itself, replacing its previous value (which
was the name of the buffer).
" 2 0) (89.9990234375 222.96185302734375 252.7234344482422 236.07740783691406 "5.2.4 The or in the Body
" 3 0) (89.99700927734375 249.4490966796875 449.4409484863281 310.6272888183594 "The purpose of the or expression in the insert-buffer function is to
ensure that the argument buffer is bound to a buffer and not just to the
name of a buffer. The previous section shows how the job could have been
done using an if expression. However, the insert-buffer function actually
uses or. To understand this, it is necessary to understand how or works.
" 4 0) (89.99600219726562 316.52911376953125 449.34100341796875 365.7073059082031 "An or function can have any number of arguments. It evaluates each
argument in turn and returns the value of the first of its arguments that is
not nil. Also, and this is a crucial feature of or, it does not evaluate any
subsequent arguments after returning the first non-nil value.
" 5 0) (104.99609375 371.84912109375 263.3631896972656 385.1473083496094 "The or expression looks like this:
" 6 0) (111.59611511230469 395.3776550292969 290.0478820800781 416.82403564453125 "(or (bufferp buffer)
(setq buffer (get-buffer buffer)))
" 7 0) (89.99453735351562 423.5691223144531 449.4162902832031 496.6273193359375 "The first argument to or is the expression (bufferp buffer). This expres-
sion returns true (a non-nil value) if the buffer is actually a buffer, and
not just the name of a buffer.
In the or expression, if this is the case,
the or expression returns this true value and does not evaluate the next
expression—and this is fine with us, since we do not want to do anything to
the value of buffer if it really is a buffer.
" 8 0) (89.99395751953125 502.5291748046875 449.535888671875 575.58740234375 "On the other hand, if the value of (bufferp buffer) is nil, which it will
be if the value of buffer is the name of a buffer, the Lisp interpreter evaluates
the next element of the or expression. This is the expression (setq buffer
(get-buffer buffer)). This expression returns a non-nil value, which is
the value to which it sets the variable buffer—and this value is a buffer
itself, not the name of a buffer.
" 9 0) (89.99322509765625 581.609130859375 449.42596435546875 630.787353515625 "The result of all this is that the symbol buffer is always bound to a buffer
itself rather than to the name of a buffer. All this is necessary because the
set-buffer function in a following line only works with a buffer itself, not
with the name to a buffer.
" 10 0)) (86 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "68
Chapter 5: A Few More Complex Functions
" 0 0) (90.00039672851562 80.48908233642578 449.2470397949219 105.66727447509766 "Incidentally, using or, the situation with the usher would be written like
this:
" 1 0) (111.60039520263672 111.57760620117188 369.3045349121094 120.54399108886719 "(or (holding-on-to-guest) (find-and-take-arm-of-guest))
" 2 0) (89.99978637695312 137.16180419921875 361.75616455078125 150.27735900878906 "5.2.5 The let Expression in insert-buffer
" 3 0) (89.99856567382812 159.20904541015625 449.4969787597656 271.1472473144531 "After ensuring that the variable buffer refers to a buffer itself and not
just to the name of a buffer, the insert-buffer function continues with a
let expression. This specifies three local variables, start, end, and newmark
and binds them to the initial value nil. These variables are used inside the
remainder of the let and temporarily hide any other occurrence of variables
of the same name in Emacs until the end of the let.
The body of the let contains two save-excursion expressions. First, we
will look at the inner save-excursion expression in detail. The expression
looks like this:
" 4 0) (89.99755859375 277.0575866699219 449.5391845703125 463.8672180175781 "(save-excursion
(set-buffer buffer)
(setq start (point-min) end (point-max)))
The expression (set-buffer buffer) changes Emacs’ attention from the
current buffer to the one from which the text will copied. In that buffer,
the variables start and end are set to the beginning and end of the buffer,
using the commands point-min and point-max. Note that we have here an
illustration of how setq is able to set two variables in the same expression.
The first argument of setq is set to the value of its second, and its third
argument is set to the value of its fourth.
After the body of the inner save-excursion is evaluated, the save-
excursion restores the original buffer, but start and end remain set to the
values of the beginning and end of the buffer from which the text will be
copied.
The outer save-excursion expression looks like this:
" 5 0) (89.99526977539062 469.7775573730469 449.42938232421875 630.7872314453125 "(save-excursion
(inner-save-excursion-expression
(go-to-new-buffer-and-set-start-and-end)
(insert-buffer-substring buffer start end)
(setq newmark (point)))
The insert-buffer-substring function copies the text into the current
buffer from the region indicated by start and end in buffer. Since the
whole of the second buffer lies between start and end, the whole of the
second buffer is copied into the buffer you are editing. Next, the value of
point, which will be at the end of the inserted text, is recorded in the variable
newmark.
After the body of the outer save-excursion is evaluated, point and mark
are relocated to their original places.
" 6 0)) (87 (90.0 47.60907745361328 449.73797607421875 60.90726852416992 "Optional Arguments
69
" 0 0) (89.9996337890625 77.48908233642578 449.650146484375 156.0497589111328 "However, it is convenient to locate a mark at the end of the newly inserted
text and locate point at its beginning. The newmark variable records the end
of the inserted text. In the last line of the let expression, the (push-mark
newmark) expression function sets a mark to this location. (The previous
location of the mark is still accessible; it is recorded on the mark ring and
you can go back to it with C-u C-⟨
" 1 0) (254.8679962158203 140.6319580078125 271.5480041503906 141.11195373535156 "<image: None, width: 1, height: 1, bpc: 1>" 2 1) (254.8800048828125 141.84242248535156 271.42596435546875 149.81253051757812 "SPC
" 3 0) (254.8679962158203 149.1519775390625 271.5480041503906 149.63197326660156 "<image: None, width: 1, height: 1, bpc: 1>" 4 1) (90.00015258789062 137.2491455078125 449.7162170410156 174.42735290527344 "⟩.) Meanwhile, point is located at the
beginning of the inserted text, which is where it was before you called the
insert function.
" 5 0) (105.00015258789062 180.80914306640625 299.9017028808594 194.1073455810547 "The whole let expression looks like this:
" 6 0) (111.599609375 204.57766723632812 332.24017333984375 300.7839050292969 "(let (start end newmark)
(save-excursion
(save-excursion
(set-buffer buffer)
(setq start (point-min) end (point-max)))
(insert-buffer-substring buffer start end)
(setq newmark (point)))
(push-mark newmark))
" 7 0) (90.00018310546875 307.64898681640625 449.41094970703125 356.8271789550781 "Like the append-to-buffer function, the insert-buffer function uses
let, save-excursion, and set-buffer. In addition, the function illustrates
one way to use or. All these functions are building blocks that we will find
and use again and again.
" 8 0) (90.00017547607422 395.4202880859375 426.1179504394531 409.79351806640625 "5.3 Complete Definition of beginning-of-buffer
" 9 0) (90.00003051757812 423.0890197753906 449.3235778808594 460.2672119140625 "The basic structure of the beginning-of-buffer function has already
been discussed. (See Section 4.2, “A Simplified beginning-of-buffer Def-
inition”, page 52.) This section describes the complex part of the definition.
" 10 0) (89.99850463867188 466.4090270996094 449.6725769042969 599.3472900390625 "As previously described, when invoked without an argument, beginning-
of-buffer moves the cursor to the beginning of the buffer, leaving the mark
at the previous position. However, when the command is invoked with a
number between one and ten, the function considers that number to be a
fraction of the length of the buffer, measured in tenths, and Emacs moves
the cursor that fraction of the way from the beginning of the buffer. Thus,
you can either call this function with the key command M-<, which will move
the cursor to the beginning of the buffer, or with a key command such as
C-u 7 M-< which will move the cursor to a point 70% of the way through the
buffer. If a number bigger than ten is used for the argument, it moves to
the end of the buffer.
" 11 0) (89.99862670898438 605.489013671875 449.34417724609375 630.7872314453125 "The beginning-of-buffer function can be called with or without an
argument. The use of the argument is optional.
" 12 0)) (88 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "70
Chapter 5: A Few More Complex Functions
" 0 0) (90.00021362304688 78.2418212890625 259.23736572265625 91.34580993652344 "5.3.1 Optional Arguments
" 1 0) (89.9998779296875 104.12909698486328 449.5845642089844 153.3072967529297 "Unless told otherwise, Lisp expects that a function with an argument in
its function definition will be called with a value for that argument. If that
does not happen, you get an error and a message that says ‘Wrong number
of arguments’.
" 2 0) (89.99942016601562 158.60906982421875 449.5632629394531 219.7872772216797 "However, optional arguments are a feature of Lisp: a keyword may be
used to tell the Lisp interpreter that an argument is optional. The keyword
is &optional. (The ‘&’ in front of ‘optional’ is part of the keyword.) In a
function definition, if an argument follows the keyword &optional, a value
does not need to be passed to that argument when the function is called.
" 3 0) (90.00006103515625 225.20904541015625 449.2474060058594 250.5072479248047 "The first line of the function definition of beginning-of-buffer therefore
looks like this:
" 4 0) (111.60009002685547 260.1376037597656 308.9436340332031 269.1039733886719 "(defun beginning-of-buffer (&optional arg)
" 5 0) (104.99986267089844 275.12908935546875 318.5670166015625 288.4272766113281 "In outline, the whole function looks like this:
" 6 0) (111.59992980957031 298.0576477050781 308.9434814453125 406.6239929199219 "(defun beginning-of-buffer (&optional arg)
\"documentation...\"
(interactive \"P\")
(push-mark)
(goto-char
(if-there-is-an-argument
figure-out-where-to-go
else-go-to
(point-min))))
" 7 0) (89.99911499023438 412.7690734863281 449.50860595703125 461.947265625 "The function is similar to the simplified-beginning-of-buffer func-
tion except that the interactive expression has \"P\" as an argument and
the goto-char function is followed by an if-then-else expression that figures
out where to put the cursor if there is an argument.
" 8 0) (89.9998779296875 467.24908447265625 449.541259765625 510.4097900390625 "The \"P\" in the interactive expression tells Emacs to pass a prefix
argument, if there is one, to the function. A prefix argument is made by
typing the ⟨
" 9 0) (146.86801147460938 494.6319885253906 172.18801879882812 495.11199951171875 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (146.8800048828125 495.8424377441406 172.16651916503906 503.8125305175781 "META
" 11 0) (146.86801147460938 503.1520080566406 172.18801879882812 503.63201904296875 "<image: None, width: 1, height: 1, bpc: 1>" 12 1) (90.00054931640625 490.8890686035156 449.60711669921875 516.4360961914062 "⟩ key followed by a number, or by typing C-u and then a
number (if you don’t type a number, C-u defaults to 4).
" 13 0) (89.99868774414062 521.84912109375 449.5188293457031 630.787353515625 "The true-or-false-test of the if expression is simple: it is simply the
argument arg. If arg has a value that is not nil, which will be the case if
beginning-of-buffer is called with an argument, then this true-or-false-
test will return true and the then-part of the if expression will be evaluated.
On the other hand, if beginning-of-buffer is not called with an argument,
the value of arg will be nil and the else-part of the if expression will be
evaluated. The else-part is simply point-min, and when this is the outcome,
the whole goto-char expression is (goto-char (point-min)), which is how
we saw the beginning-of-buffer function in its simplified form.
" 14 0)) (89 (90.0 47.60907745361328 449.6610107421875 60.90726852416992 "What happens in a large buffer
71
" 0 0) (89.99935913085938 78.2418212890625 379.2123718261719 91.35738372802734 "5.3.2 beginning-of-buffer with an Argument
" 1 0) (89.99972534179688 100.16907501220703 449.3883972167969 149.34727478027344 "When beginning-of-buffer is called with an argument, an expression is
evaluated which calculates what value to pass to goto-char. This expression
is rather complicated at first sight. It includes an inner if expression and
much arithmetic. It looks like this:
" 2 0) (90.0003662109375 155.13754272460938 449.4223937988281 290.2270812988281 "(if (> (buffer-size) 10000)
;; Avoid overflow for large buffer sizes!
(* (prefix-numeric-value arg) (/ (buffer-size) 10))
(/
(+ 10
(*
(buffer-size) (prefix-numeric-value arg))) 10))
Like other complex-looking expressions, the conditional expression within
beginning-of-buffer can be disentangled by looking at it as parts of a
template, in this case, the template for an if-then-else expression. In skeletal
form, the expression looks like this:
" 3 0) (90.00009155273438 296.01495361328125 449.466796875 443.8271179199219 "(if (buffer-is-large
divide-buffer-size-by-10-and-multiply-by-arg
else-use-alternate-calculation
The true-or-false-test of this inner if expression checks the size of the
buffer. The reason for this is that the old Version 18 Emacs used numbers
that are no bigger than eight million or so and in the computation that
followed, the programmer feared that Emacs might try to use over-large
numbers if the buffer were large.
The term ‘overflow’, mentioned in the
comment, means numbers that are over large. Version 21 Emacs uses larger
numbers, but this code has not been touched, if only because people now
look at buffers that are far, far larger than ever before.
There are two cases: if the buffer is large and if it is not.
" 4 0) (90.00003051757812 459.481689453125 291.9230041503906 472.585693359375 "What happens in a large buffer
" 5 0) (90.0006103515625 481.5289306640625 449.33489990234375 533.587158203125 "In beginning-of-buffer, the inner if expression tests whether the size
of the buffer is greater than 10,000 characters. To do this, it uses the >
function and the buffer-size function.
The line looks like this:
" 6 0) (89.99990844726562 539.3775024414062 449.3666687011719 575.587158203125 "(if (> (buffer-size) 10000)
When the buffer is large, the then-part of the if expression is evaluated. It
reads like this (after formatting for easy reading):
" 7 0) (89.9999771118164 581.3775024414062 442.1563720703125 630.7871704101562 "(*
(prefix-numeric-value arg)
(/ (buffer-size) 10))
This expression is a multiplication, with two arguments to the function *.
" 8 0)) (90 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "72
Chapter 5: A Few More Complex Functions
" 0 0) (89.9993896484375 77.48908233642578 449.4656066894531 138.5472869873047 "The first argument is (prefix-numeric-value arg). When \"P\" is used
as the argument for interactive, the value passed to the function as its
argument is passed a “raw prefix argument”, and not a number. (It is a
number in a list.) To perform the arithmetic, a conversion is necessary, and
prefix-numeric-value does the job.
" 1 0) (89.99932861328125 143.12908935546875 449.3666076660156 192.3072967529297 "The second argument is (/ (buffer-size) 10). This expression divides
the numeric value of the buffer by ten. This produces a number that tells
how many characters make up one tenth of the buffer size. (In Lisp, / is
used for division, just as * is used for multiplication.)
" 2 0) (89.99911499023438 196.88909912109375 449.3331298828125 222.0673065185547 "In the multiplication expression as a whole, this amount is multiplied by
the value of the prefix argument—the multiplication looks like this:
" 3 0) (111.59896850585938 230.9752197265625 320.8189697265625 252.4239959716797 "(* numeric-value-of-prefix-arg
number-of-characters-in-one-tenth-of-the-buffer)
" 4 0) (89.99862670898438 257.60906982421875 449.3546142578125 282.9072570800781 "If, for example, the prefix argument is ‘7’, the one-tenth value will be mul-
tiplied by 7 to give a position 70% of the way through the buffer.
" 5 0) (89.998291015625 287.48907470703125 449.2565612792969 312.6672668457031 "The result of all this is that if the buffer is large, the goto-char expression
reads like this:
" 6 0) (111.59825897216797 321.5777282714844 299.4787292480469 343.02410888671875 "(goto-char (* (prefix-numeric-value arg)
(/ (buffer-size) 10)))
" 7 0) (104.99807739257812 348.2091979980469 289.9832458496094 361.50738525390625 "This puts the cursor where we want it.
" 8 0) (89.99833679199219 386.4019470214844 294.0572814941406 399.5059509277344 "What happens in a small buffer
" 9 0) (89.99761962890625 411.4491882324219 449.4306335449219 472.50738525390625 "If the buffer contains fewer than 10,000 characters, a slightly different
computation is performed.
You might think this is not necessary, since
the first computation could do the job.
However, in a small buffer, the
first method may not put the cursor on exactly the desired line; the second
method does a better job.
" 10 0) (104.99754333496094 477.2091979980469 220.85206604003906 490.50738525390625 "The code looks like this:
" 11 0) (111.59766387939453 499.2977294921875 388.8701477050781 508.26409912109375 "(/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
" 12 0) (89.9962158203125 513.3292236328125 449.4185485839844 550.5074462890625 "This is code in which you figure out what happens by discovering how the
functions are embedded in parentheses. It is easier to read if you reformat
it with each expression indented more deeply than its enclosing expression:
" 13 0) (120.95587158203125 559.4177856445312 276.04156494140625 630.6641845703125 "(/
(+ 10
(*
(buffer-size)
(prefix-numeric-value arg)))
10))
" 14 0)) (91 (90.0 47.60907745361328 449.5965576171875 60.90726852416992 "The Complete beginning-of-buffer
73
" 0 0) (89.99990844726562 80.00910186767578 449.55206298828125 117.18729400634766 "Looking at parentheses, we see that the innermost operation is (prefix-
numeric-value arg), which converts the raw argument to a number. This
number is multiplied by the buffer size in the following expression:
" 1 0) (89.99945068359375 122.61764526367188 449.4430847167969 220.7473602294922 "(* (buffer-size) (prefix-numeric-value arg)
This multiplication creates a number that may be larger than the size of
the buffer—seven times larger if the argument is 7, for example.
Ten is
then added to this number and finally the large number is divided by ten to
provide a value that is one character larger than the percentage position in
the buffer.
The number that results from all this is passed to goto-char and the
cursor is moved to that point.
" 2 0) (90.000244140625 235.44189453125 351.8075256347656 248.5574493408203 "5.3.3 The Complete beginning-of-buffer
" 3 0) (105.00023651123047 257.129150390625 414.5348205566406 270.4273376464844 "Here is the complete text of the beginning-of-buffer function:
" 4 0) (111.59982299804688 275.8576965332031 323.009033203125 396.9041748046875 "(defun beginning-of-buffer (&optional arg)
\"Move point to the beginning of the buffer;
leave mark at previous position.
With arg N, put point N/10 of the way
from the true beginning.
Don’t use this in Lisp programs!
\\(goto-char (point-min)) is faster
and does not set the mark.\"
(interactive \"P\")
(push-mark)
" 5 0) (120.96048736572266 401.497802734375 323.555908203125 472.6241760253906 "(goto-char
(if arg
(if (> (buffer-size) 10000)
;; Avoid overflow for large buffer sizes!
(* (prefix-numeric-value arg)
(/ (buffer-size) 10))
" 6 0) (90.00030517578125 477.2178039550781 449.400146484375 589.5075073242188 "(/ (+ 10 (* (buffer-size)
(prefix-numeric-value arg)))
10))
(point-min)))
(if arg (forward-line 1)))
Except for two small points, the previous discussion shows how this function
works. The first point deals with a detail in the documentation string, and
the second point concerns the last line of the function.
In the documentation string, there is reference to an expression:
" 7 0) (90.00033569335938 594.9378662109375 449.37799072265625 630.7875366210938 "\\(goto-char (point-min))
A ‘\\\\u2019 is used before the first parenthesis of this expression. This ‘\\\\u2019 tells
the Lisp interpreter that the expression should be printed as shown in the
" 8 0)) (92 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "74
Chapter 5: A Few More Complex Functions
" 0 0) (90.00021362304688 77.48908233642578 449.34564208984375 141.5472869873047 "documentation rather than evaluated as a symbolic expression, which is what
it looks like.
Finally, the last line of the beginning-of-buffer command says to move
point to the beginning of the next line if the command is invoked with an
argument:
" 1 0) (90.00003051757812 147.45761108398438 449.4874267578125 219.4272918701172 "(if arg (forward-line 1)))
This puts the cursor at the beginning of the first line after the appropriate
tenths position in the buffer. This is a flourish that means that the cursor is
always located at least the requested tenths of the way through the buffer,
which is a nicety that is, perhaps, not necessary, but which, if it did not
occur, would be sure to draw complaints.
" 2 0) (89.99974060058594 239.30035400390625 169.89073181152344 253.660888671875 "5.4 Review
" 3 0) (104.99974060058594 262.2890625 437.1813049316406 275.5872497558594 "Here is a brief summary of some of the topics covered in this chapter.
" 4 0) (90.00003051757812 281.96905517578125 449.48760986328125 343.0272521972656 "or
Evaluate each argument in sequence, and return the value of
the first argument that is not nil; if none return a value that
is not nil, return nil. In brief, return the first true value of
the arguments; return a true value if one or any of the other are
true.
" 5 0) (90.00080108642578 349.4090576171875 449.5203552246094 398.5872497558594 "and
Evaluate each argument in sequence, and if any are nil, return
nil; if none are nil, return the value of the last argument. In
brief, return a true value only if all the arguments are true;
return a true value if one and each of the others is true.
" 6 0) (90.00076293945312 406.81268310546875 449.488037109375 454.1472473144531 "&optional
A keyword used to indicate that an argument to a function defi-
nition is optional; this means that the function can be evaluated
without the argument, if desired.
" 7 0) (90.00105285644531 462.252685546875 449.7936706542969 497.7072448730469 "prefix-numeric-value
Convert the ‘raw prefix argument’ produced by (interactive
\"P\") to a numeric value.
" 8 0) (90.00175476074219 505.81268310546875 449.7944641113281 577.0272827148438 "forward-line
Move point forward to the beginning of the next line, or if the
argument is greater than one, forward that many lines.
If it
can’t move as far forward as it is supposed to, forward-line
goes forward as far as it can and then returns a count of the
number of additional lines it was supposed to move but couldn’t.
" 9 0) (90.00173950195312 585.252685546875 373.3979187011719 608.707275390625 "erase-buffer
Delete the entire contents of the current buffer.
" 10 0) (90.00173950195312 615.0890502929688 424.12506103515625 628.3872680664062 "bufferp
Return t if its argument is a buffer; otherwise return nil.
" 11 0)) (93 (90.0 47.60907745361328 449.6620178222656 60.90726852416992 "optional Argument Exercise
75
" 0 0) (90.0003662109375 77.30034637451172 318.1007385253906 91.67356872558594 "5.5 optional Argument Exercise
" 1 0) (90.000244140625 99.32910919189453 449.43304443359375 148.50730895996094 "Write an interactive function with an optional argument that tests
whether its argument, a number, is greater or less than the value of fill-
column, and tells you which, in a message. However, if you do not pass an
argument to the function, use 56 as a default value.
" 2 0)) (94 (90.0 47.60907745361328 449.4870910644531 60.90726852416992 "76
Chapter 5: A Few More Complex Functions
" 0 0)) (95 (90.0 47.60907745361328 449.5958557128906 60.90726852416992 "The save-restriction Special Form
77
" 0 0) (89.99920654296875 75.14844512939453 324.1742858886719 92.38106536865234 "6 Narrowing and Widening
" 1 0) (89.9981689453125 117.92908477783203 449.35504150390625 155.10728454589844 "Narrowing is a feature of Emacs that makes it possible for you to focus
on a specific part of a buffer, and work without accidentally changing other
parts. Narrowing is normally disabled since it can confuse novices.
" 2 0) (89.99758911132812 158.72906494140625 449.4089660644531 255.67608642578125 "With narrowing, the rest of a buffer is made invisible, as if it weren’t
there. This is an advantage if, for example, you want to replace a word in
one part of a buffer but not in another: you narrow to the part you want
and the replacement is carried out only in that section, not in the rest of
the buffer. Searches will only work within a narrowed region, not outside of
one, so if you are fixing a part of a document, you can keep yourself from
accidentally finding parts you do not need to fix by narrowing just to the
region you want. (The key binding for narrow-to-region is C-x n n.)
" 3 0) (89.99505615234375 259.2890625 449.5495300292969 344.3560791015625 "However, narrowing does make the rest of the buffer invisible, which can
scare people who inadvertently invoke narrowing and think they have deleted
a part of their file. Moreover, the undo command (which is usually bound
to C-x u) does not turn off narrowing (nor should it), so people can become
quite desperate if they do not know that they can return the rest of a buffer
to visibility with the widen command. (The key binding for widen is C-x n
w.)
" 4 0) (89.99188232421875 347.84906005859375 449.5561218261719 456.7872619628906 "Narrowing is just as useful to the Lisp interpreter as to a human. Often,
an Emacs Lisp function is designed to work on just part of a buffer; or
conversely, an Emacs Lisp function needs to work on all of a buffer that
has been narrowed.
The what-line function, for example, removes the
narrowing from a buffer, if it has any narrowing and when it has finished its
job, restores the narrowing to what it was. On the other hand, the count-
lines function, which is called by what-line, uses narrowing to restrict
itself to just that portion of the buffer in which it is interested and then
restores the previous situation.
" 5 0) (89.99147033691406 484.8203430175781 366.9673156738281 499.1935729980469 "6.1 The save-restriction Special Form
" 6 0) (89.99102783203125 509.84906005859375 449.4242248535156 630.7872924804688 "In Emacs Lisp, you can use the save-restriction special form to keep
track of whatever narrowing is in effect, if any. When the Lisp interpreter
meets with save-restriction, it executes the code in the body of the save-
restriction expression, and then undoes any changes to narrowing that the
code caused. If, for example, the buffer is narrowed and the code that follows
save-restriction gets rid of the narrowing, save-restriction returns the
buffer to its narrowed region afterwards. In the what-line command, any
narrowing the buffer may have is undone by the widen command that im-
mediately follows the save-restriction command. Any original narrowing
is restored just before the completion of the function.
" 7 0)) (96 (90.0 47.60907745361328 449.7270202636719 60.90726852416992 "78
Chapter 6: Narrowing and Widening
" 0 0) (104.999755859375 80.60907745361328 390.3377380371094 93.90726470947266 "The template for a save-restriction expression is simple:
" 1 0) (89.99880981445312 99.81759643554688 449.5189208984375 223.6272430419922 "(save-restriction
body... )
The body of the save-restriction is one or more expressions that will be
evaluated in sequence by the Lisp interpreter.
Finally, a point to note: when you use both save-excursion and save-
restriction, one right after the other, you should use save-excursion out-
ermost. If you write them in reverse order, you may fail to record narrowing
in the buffer to which Emacs switches after calling save-excursion. Thus,
when written together, save-excursion and save-restriction should be
written like this:
" 2 0) (89.99862670898438 229.53756713867188 449.7917785644531 318.0671691894531 "(save-excursion
(save-restriction
body...))
In other circumstances, when not written together, the save-excursion
and save-restriction special forms must be written in the order appro-
priate to the function.
For example,
" 3 0) (120.95866394042969 323.9776306152344 200.5467071533203 370.384033203125 "(save-restriction
(widen)
(save-excursion
body...))
" 4 0) (89.99861145019531 389.42041015625 185.49827575683594 403.79364013671875 "6.2 what-line
" 5 0) (89.99871826171875 412.5291442871094 449.74725341796875 449.70733642578125 "The what-line command tells you the number of the line in which the
cursor is located. The function illustrates the use of the save-restriction
and save-excursion commands. Here is the text of the function in full:
" 6 0) (89.99655151367188 455.7377014160156 449.4836730957031 630.7874145507812 "(defun what-line ()
\"Print the current line number (in the buffer) of point.\"
(interactive)
(save-restriction
(widen)
(save-excursion
(beginning-of-line)
(message \"Line %d\"
(1+ (count-lines 1 (point)))))))
The function has a documentation line and is interactive, as you would
expect. The next two lines use the functions save-restriction and widen.
The save-restriction special form notes whatever narrowing is in ef-
fect, if any, in the current buffer and restores that narrowing after the code
in the body of the save-restriction has been evaluated.
" 7 0)) (97 (90.0 47.60907745361328 449.6940612792969 60.90726852416992 "Exercise with Narrowing
79
" 0 0) (89.99636840820312 77.48908233642578 449.9323425292969 288.1872253417969 "The save-restriction special form is followed by widen. This function
undoes any narrowing the current buffer may have had when what-line
was called.
(The narrowing that was there is the narrowing that save-
restriction remembers.)
This widening makes it possible for the line
counting commands to count from the beginning of the buffer. Otherwise,
they would have been limited to counting within the accessible region. Any
original narrowing is restored just before the completion of the function by
the save-restriction special form.
The call to widen is followed by save-excursion, which saves the loca-
tion of the cursor (i.e., of point) and of the mark, and restores them after
the code in the body of the save-excursion uses the beginning-of-line
function to move point.
(Note that the (widen) expression comes between the save-restriction
and save-excursion special forms.
When you write the two save- ...
expressions in sequence, write save-excursion outermost.)
The last two lines of the what-line function are functions to count the
number of lines in the buffer and then print the number in the echo area.
" 1 0) (89.99569702148438 293.1376037597656 449.43963623046875 403.02734375 "(message \"Line %d\"
(1+ (count-lines 1 (point)))))))
The message function prints a one-line message at the bottom of the
Emacs screen. The first argument is inside of quotation marks and is printed
as a string of characters. However, it may contain ‘%d’, ‘%s’, or ‘%c’ to print
arguments that follow the string. ‘%d’ prints the argument as a decimal, so
the message will say something such as ‘Line 243’.
The number that is printed in place of the ‘%d’ is computed by the last
line of the function:
" 2 0) (89.99578857421875 408.0976867675781 449.48358154296875 517.1473999023438 "(1+ (count-lines 1 (point)))
What this does is count the lines from the first position of the buffer, indi-
cated by the 1, up to (point), and then add one to that number. (The 1+
function adds one to its argument.) We add one to it because line 2 has only
one line before it, and count-lines counts only the lines before the current
line.
After count-lines has done its job, and the message has been printed in
the echo area, the save-excursion restores point and mark to their original
positions; and save-restriction restores the original narrowing, if any.
" 3 0) (89.99539184570312 533.5404663085938 293.41778564453125 547.9010009765625 "6.3 Exercise with Narrowing
" 4 0) (89.99453735351562 555.689208984375 449.53631591796875 616.7474365234375 "Write a function that will display the first 60 characters of the current
buffer, even if you have narrowed the buffer to its latter half so that the first
line is inaccessible. Restore point, mark, and narrowing. For this exercise,
you need to use save-restriction, widen, goto-char, point-min, buffer-
substring, message, and other functions, a whole potpourri.
" 5 0)) (98 (90.0 47.60907745361328 449.7270202636719 60.90726852416992 "80
Chapter 6: Narrowing and Widening
" 0 0)) (99 (90.0 47.60907745361328 449.8250427246094 60.90726852416992 "car and cdr
81
" 0 0) (89.9993896484375 75.14844512939453 434.5227966308594 92.39627838134766 "7 car, cdr, cons: Fundamental Functions
" 1 0) (89.99911499023438 124.28907012939453 449.2903747558594 161.46726989746094 "In Lisp, car, cdr, and cons are fundamental functions. The cons function
is used to construct lists, and the car and cdr functions are used to take
them apart.
" 2 0) (89.9986572265625 166.2890625 449.32257080078125 203.46726989746094 "In the walk through of the copy-region-as-kill function, we will see
cons as well as two variants on cdr, namely, setcdr and nthcdr.
(See
Section 8.5, “copy-region-as-kill”, page 102.)
" 3 0) (89.9974365234375 208.2890625 449.4090881347656 353.1072692871094 "The name of the cons function is not unreasonable: it is an abbreviation
of the word ‘construct’. The origins of the names for car and cdr, on the
other hand, are esoteric: car is an acronym from the phrase ‘Contents of
the Address part of the Register’; and cdr (pronounced ‘could-er’) is an
acronym from the phrase ‘Contents of the Decrement part of the Register’.
These phrases refer to specific pieces of hardware on the very early computer
on which the original Lisp was developed. Besides being obsolete, the phrases
have been completely irrelevant for more than 25 years to anyone thinking
about Lisp. Nonetheless, although a few brave scholars have begun to use
more reasonable names for these functions, the old terms are still in use. In
particular, since the terms are used in the Emacs Lisp source code, we will
use them in this introduction.
" 4 0) (89.99783325195312 386.18035888671875 199.543701171875 400.5535888671875 "7.1 car and cdr
" 5 0) (89.99740600585938 412.5291748046875 449.451904296875 437.7073669433594 "The car of a list is, quite simply, the first item in the list. Thus the car
of the list (rose violet daisy buttercup) is rose.
" 6 0) (89.99795532226562 442.649169921875 449.2989807128906 467.9473571777344 "If you are reading this in Info in GNU Emacs, you can see this by evalu-
ating the following:
" 7 0) (111.59794616699219 477.09771728515625 280.5611877441406 486.0640869140625 "(car ’(rose violet daisy buttercup))
" 8 0) (89.998046875 491.3691711425781 406.93951416015625 504.6673583984375 "After evaluating the expression, rose will appear in the echo area.
" 9 0) (89.9986572265625 509.4891357421875 449.4852600097656 534.787353515625 "Clearly, a more reasonable name for the car function would be first
and this is often suggested.
" 10 0) (89.9981689453125 539.609130859375 449.37591552734375 576.787353515625 "car does not remove the first item from the list; it only reports what it
is. After car has been applied to a list, the list is still the same as it was. In
the jargon, car is ‘non-destructive’. This feature turns out to be important.
" 11 0) (89.99746704101562 581.609130859375 449.451904296875 630.787353515625 "The cdr of a list is the rest of the list, that is, the cdr function returns
the part of the list that follows the first item. Thus, while the car of the
list ’(rose violet daisy buttercup) is rose, the rest of the list, the value
returned by the cdr function, is (violet daisy buttercup).
" 12 0)) (100 (90.0 47.60907745361328 449.3889465332031 60.90726852416992 "82
Chapter 7: car, cdr, cons: Fundamental Functions
" 0 0) (105.00006103515625 80.72907257080078 399.5671081542969 94.02725982666016 "You can see this by evaluating the following in the usual way:
" 1 0) (89.99603271484375 100.17758178710938 449.50537109375 379.5072326660156 "(cdr ’(rose violet daisy buttercup))
When you evaluate this, (violet daisy buttercup) will appear in the echo
area.
Like car, cdr does not remove any elements from the list—it just returns
a report of what the second and subsequent elements are.
Incidentally, in the example, the list of flowers is quoted. If it were not, the
Lisp interpreter would try to evaluate the list by calling rose as a function.
In this example, we do not want to do that.
Clearly, a more reasonable name for cdr would be rest.
(There is a lesson here: when you name new functions, consider very
carefully what you are doing, since you may be stuck with the names for far
longer than you expect. The reason this document perpetuates these names
is that the Emacs Lisp source code uses them, and if I did not use them,
you would have a hard time reading the code; but do, please, try to avoid
using these terms yourself. The people who come after you will be grateful
to you.)
When car and cdr are applied to a list made up of symbols, such as the
list (pine fir oak maple), the element of the list returned by the function
car is the symbol pine without any parentheses around it. pine is the first
element in the list. However, the cdr of the list is a list itself, (fir oak
maple), as you can see by evaluating the following expressions in the usual
way:
" 2 0) (111.59603118896484 385.6575927734375 238.39585876464844 394.62396240234375 "(car ’(pine fir oak maple))
" 3 0) (89.99554443359375 410.6175842285156 449.4280090332031 471.1872253417969 "(cdr ’(pine fir oak maple))
On the other hand, in a list of lists, the first element is itself a list. car
returns this first element as a list. For example, the following list contains
three sub-lists, a list of carnivores, a list of herbivores and a list of sea
mammals:
" 4 0) (89.995849609375 477.33758544921875 449.592041015625 550.9873046875 "(car ’((lion tiger cheetah)
(gazelle antelope zebra)
(whale dolphin seal)))
In this example, the first element or car of the list is the list of carnivores,
(lion tiger cheetah), and the rest of the list is ((gazelle antelope
zebra) (whale dolphin seal)).
" 5 0) (89.99578857421875 557.1376342773438 449.3960266113281 630.7872924804688 "(cdr ’((lion tiger cheetah)
(gazelle antelope zebra)
(whale dolphin seal)))
It is worth saying again that car and cdr are non-destructive—that is,
they do not modify or change lists to which they are applied. This is very
important for how they are used.
" 6 0)) (101 (90.0 47.60907745361328 449.8796691894531 60.90726852416992 "cons
83
" 0 0) (89.99908447265625 77.48908233642578 449.39910888671875 198.3072967529297 "Also, in the first chapter, in the discussion about atoms, I said that in
Lisp, “certain kinds of atom, such as an array, can be separated into parts;
but the mechanism for doing this is different from the mechanism for splitting
a list. As far as Lisp is concerned, the atoms of a list are unsplittable.”
(See Section 1.1.1, “Lisp Atoms”, page 1.) The car and cdr functions are
used for splitting lists and are considered fundamental to Lisp. Since they
cannot split or gain access to the parts of an array, an array is considered an
atom. Conversely, the other fundamental function, cons, can put together
or construct a list, but not an array. (Arrays are handled by array-specific
functions. See section “Arrays” in The GNU Emacs Lisp Reference Manual.)
" 1 0) (89.99948120117188 215.660400390625 148.34246826171875 230.0336151123047 "7.2 cons
" 2 0) (89.99945068359375 238.04913330078125 449.1919250488281 275.2273254394531 "The cons function constructs lists; it is the inverse of car and cdr. For
example, cons can be used to make a four element list from the three element
list, (fir oak maple):
" 3 0) (89.99906921386719 280.4175720214844 268.2425537109375 304.1472473144531 "(cons ’pine ’(fir oak maple))
After evaluating this list, you will see
" 4 0) (89.997802734375 309.4576110839844 449.58367919921875 426.0127258300781 "(pine fir oak maple)
appear in the echo area. cons puts a new element at the beginning of a list;
it attaches or pushes elements onto the list.
cons must have a list to attach to.1 You cannot start from absolutely
nothing. If you are building a list, you need to provide at least an empty
list at the beginning. Here is a series of cons expressions that build up a list
of flowers. If you are reading this in Info in GNU Emacs, you can evaluate
each of the expressions in the usual way; the value is printed in this text
after ‘⇒’, which you may read as ‘evaluates to’.
" 5 0) (111.59840393066406 424.1776428222656 205.56089782714844 451.36981201171875 "(cons ’buttercup ())
⇒ (buttercup)
" 6 0) (111.59838104248047 453.6976623535156 233.4697723388672 480.88983154296875 "(cons ’daisy ’(buttercup))
⇒ (daisy buttercup)
" 7 0) (111.59847259521484 483.3376770019531 266.4187927246094 510.52984619140625 "(cons ’violet ’(daisy buttercup))
⇒ (violet daisy buttercup)
" 8 0) (89.99783325195312 512.8577270507812 449.4844665527344 585.0673828125 "(cons ’rose ’(violet daisy buttercup))
⇒ (rose violet daisy buttercup)
In the first example, the empty list is shown as () and a list made up
of buttercup followed by the empty list is constructed. As you can see,
the empty list is not shown in the list that was constructed. All that you
see is (buttercup). The empty list is not counted as an element of a list
" 9 0) (89.98799896240234 594.5919799804688 233.98800659179688 595.0719604492188 "<image: None, width: 1, height: 1, bpc: 1>" 10 1) (95.87999725341797 595.5352172851562 449.7353515625 630.3015747070312 "1 Actually, you can cons an element to an atom to produce a dotted pair. Dotted pairs
are not discussed here; see section “Dotted Pair Notation” in The GNU Emacs Lisp
Reference Manual.
" 11 0)) (102 (90.0 47.60907745361328 449.3889465332031 60.90726852416992 "84
Chapter 7: car, cdr, cons: Fundamental Functions
" 0 0) (89.99978637695312 77.48908233642578 449.4430236816406 153.9072723388672 "because there is nothing in an empty list. Generally speaking, an empty list
is invisible.
The second example, (cons ’daisy ’(buttercup)) constructs a new,
two element list by putting daisy in front of buttercup; and the third
example constructs a three element list by putting violet in front of daisy
and buttercup.
" 1 0) (90.00016021728516 171.601806640625 342.6677551269531 184.7173614501953 "7.2.1 Find the Length of a List: length
" 2 0) (90.00006103515625 194.24908447265625 449.4648132324219 219.4272918701172 "You can find out how many elements there are in a list by using the Lisp
function length, as in the following examples:
" 3 0) (111.59971618652344 225.93765258789062 209.98521423339844 253.0098419189453 "(length ’(buttercup))
⇒ 1
" 4 0) (111.59981536865234 255.45761108398438 238.2627410888672 282.6497802734375 "(length ’(daisy buttercup))
⇒ 2
" 5 0) (90.00047302246094 284.9775695800781 449.4336242675781 361.9871826171875 "(length (cons ’violet ’(daisy buttercup)))
⇒ 3
In the third example, the cons function is used to construct a three element
list which is then passed to the length function as its argument.
We can also use length to count the number of elements in an empty
list:
" 6 0) (90.00161743164062 368.3775329589844 449.4566650390625 445.1471862792969 "(length ())
⇒ 0
As you would expect, the number of elements in an empty list is zero.
An interesting experiment is to find out what happens if you try to find
the length of no list at all; that is, if you try to call length without giving
it an argument, not even an empty list:
" 7 0) (90.0020751953125 451.65753173828125 353.7944641113281 476.5871887207031 "(length )
What you see, if you evaluate this, is the error message
" 8 0) (90.00070190429688 482.9775390625 449.422607421875 630.7872314453125 "Wrong number of arguments: #<subr length>, 0
This means that the function receives the wrong number of arguments, zero,
when it expects some other number of arguments. In this case, one argument
is expected, the argument being a list whose length the function is measuring.
(Note that one list is one argument, even if the list has many elements inside
it.)
The part of the error message that says ‘#<subr length>’ is the name of
the function. This is written with a special notation, ‘#<subr’, that indicates
that the function length is one of the primitive functions written in C rather
than in Emacs Lisp. (‘subr’ is an abbreviation for ‘subroutine’.) See section
“What Is a Function?” in The GNU Emacs Lisp Reference Manual, for more
about subroutines.
" 9 0)) (103 (90.0 47.60907745361328 449.857666015625 60.90726852416992 "nthcdr
85
" 0 0) (90.0 77.30034637451172 163.20565795898438 91.67356872558594 "7.3 nthcdr
" 1 0) (89.99871826171875 99.80908966064453 450.03179931640625 237.42735290527344 "The nthcdr function is associated with the cdr function. What it does
is take the cdr of a list repeatedly.
If you take the cdr of the list (pine fir oak maple), you will be returned
the list (fir oak maple).
If you repeat this on what was returned, you
will be returned the list (oak maple). (Of course, repeated cdring on the
original list will just give you the original cdr since the function does not
change the list. You need to evaluate the cdr of the cdr and so on.) If you
continue this, eventually you will be returned an empty list, which in this
case, instead of being shown as () is shown as nil.
For review, here is a series of repeated cdrs, the text following the ‘⇒’
shows what is returned.
" 2 0) (111.59909057617188 242.85769653320312 238.39891052246094 269.92987060546875 "(cdr ’(pine fir oak maple))
⇒(fir oak maple)
" 3 0) (111.59908294677734 272.3777770996094 214.7930145263672 299.5699462890625 "(cdr ’(fir oak maple))
⇒ (oak maple)
" 4 0) (111.59908294677734 301.8977355957031 196.09861755371094 329.08990478515625 "(cdr ’(oak maple))
⇒(maple)
" 5 0) (111.59907531738281 331.5377197265625 177.16415405273438 358.7298889160156 "(cdr ’(maple))
⇒ nil
" 6 0) (111.5990982055664 361.0577392578125 163.3294219970703 388.2499084472656 "(cdr ’nil)
⇒ nil
" 7 0) (89.99932861328125 390.69775390625 449.2679443359375 440.107421875 "(cdr ())
⇒ nil
You can also do several cdrs without printing the values in between, like
this:
" 8 0) (89.9970703125 445.41778564453125 449.45233154296875 603.907470703125 "(cdr (cdr ’(pine fir oak maple)))
⇒ (oak maple)
In this example, the Lisp interpreter evaluates the innermost list first. The
innermost list is quoted, so it just passes the list as it is to the innermost cdr.
This cdr passes a list made up of the second and subsequent elements of the
list to the outermost cdr, which produces a list composed of the third and
subsequent elements of the original list. In this example, the cdr function is
repeated and returns a list that consists of the original list without its first
two elements.
The nthcdr function does the same as repeating the call to cdr. In the
following example, the argument 2 is passed to the function nthcdr, along
with the list, and the value returned is the list without its first two items,
which is exactly the same as repeating cdr twice on the list:
" 9 0) (111.59686279296875 609.2178344726562 261.8825988769531 636.4099731445312 "(nthcdr 2 ’(pine fir oak maple))
⇒ (oak maple)
" 10 0)) (104 (90.0 47.60907745361328 449.3889465332031 60.90726852416992 "86
Chapter 7: car, cdr, cons: Fundamental Functions
" 0 0) (89.99990844726562 84.92908477783203 449.3125915527344 110.2272720336914 "Using the original four element list, we can see what happens when various
numeric arguments are passed to nthcdr, including 0, 1, and 5:
" 1 0) (111.60033416748047 120.45520782470703 261.8860778808594 160.12977600097656 ";; Leave the list as it was.
(nthcdr 0 ’(pine fir oak maple))
⇒ (pine fir oak maple)
" 2 0) (111.60026550292969 162.57513427734375 288.8662109375 202.1297149658203 ";; Return a copy without the first element.
(nthcdr 1 ’(pine fir oak maple))
⇒ (fir oak maple)
" 3 0) (111.60029602050781 204.5750732421875 322.9229736328125 244.24964904785156 ";; Return a copy of the list without three elements.
(nthcdr 3 ’(pine fir oak maple))
⇒ (maple)
" 4 0) (111.60054779052734 246.57501220703125 286.4632263183594 286.24957275390625 ";; Return a copy lacking all four elements.
(nthcdr 4 ’(pine fir oak maple))
⇒ nil
" 5 0) (111.60079193115234 288.574951171875 267.28466796875 328.24951171875 ";; Return a copy lacking all elements.
(nthcdr 5 ’(pine fir oak maple))
⇒ nil
" 6 0) (90.00125122070312 359.06005859375 140.91290283203125 373.43328857421875 "7.4 nth
" 7 0) (90.00137329101562 386.48876953125 449.30267333984375 423.7869567871094 "The nthcdr function takes the cdr of a list repeatedly. The nth function
takes the car of the result returned by nthcdr. It returns the Nth element
of the list.
" 8 0) (90.00167846679688 429.80877685546875 449.346435546875 455.1069641113281 "Thus, if it were not defined in C for speed, the definition of nth would
be:
" 9 0) (111.60151672363281 465.4573059082031 412.1624755859375 511.7436828613281 "(defun nth (n list)
\"Returns the Nth element of LIST.
N counts from zero.
If LIST is not that long, nil is returned.\"
(car (nthcdr n list)))
" 10 0) (90.00147247314453 519.0887451171875 449.281005859375 544.386962890625 "(Originally, nth was defined in Emacs Lisp in ‘subr.el’, but its definition
was redone in C in the 1980s.)
" 11 0) (90.00204467773438 550.4088134765625 449.4012145996094 575.5870361328125 "The nth function returns a single element of a list. This can be very
convenient.
" 12 0) (90.00192260742188 581.6087646484375 449.3797302246094 630.7869873046875 "Note that the elements are numbered from zero, not one.
That is to
say, the first element of a list, its car is the zeroth element. This is called
‘zero-based’ counting and often bothers people who are accustomed to the
first element in a list being number one, which is ‘one-based’.
" 13 0)) (105 (90.0 47.60907745361328 449.857666015625 60.90726852416992 "setcdr
87
" 0 0) (105.0 80.84906768798828 166.4837188720703 94.14725494384766 "For example:
" 1 0) (111.60000610351562 100.29757690429688 252.4142303466797 127.48975372314453 "(nth 0 ’(\"one\" \"two\" \"three\"))
⇒ \"one\"
" 2 0) (90.00054931640625 137.61758422851562 449.31256103515625 198.90721130371094 "(nth 1 ’(\"one\" \"two\" \"three\"))
⇒ \"two\"
It is worth mentioning that nth, like nthcdr and cdr, does not change
the original list—the function is non-destructive. This is in sharp contrast
to the setcar and setcdr functions.
" 3 0) (90.0003662109375 220.22027587890625 163.20602416992188 234.59349060058594 "7.5 setcar
" 4 0) (90.00042724609375 243.5689697265625 449.44384765625 331.8671569824219 "As you might guess from their names, the setcar and setcdr functions
set the car or the cdr of a list to a new value. They actually change the
original list, unlike car and cdr which leave the original list as it was. One
way to find out how this works is to experiment. We will start with the
setcar function.
First, we can make a list and then set the value of a variable to the list,
using the setq function. Here is a list of animals:
" 5 0) (89.99884033203125 338.13751220703125 449.4419250488281 437.8271484375 "(setq animals ’(antelope giraffe lion tiger))
If you are reading this in Info inside of GNU Emacs, you can evaluate this
expression in the usual fashion, by positioning the cursor after the expression
and typing C-x C-e.
(I’m doing this right here as I write this.
This is
one of the advantages of having the interpreter built into the computing
environment.)
When we evaluate the variable animals, we see that it is bound to the
list (antelope giraffe lion tiger):
" 6 0) (89.997314453125 443.9775085449219 449.5286865234375 544.2672119140625 "animals
⇒ (antelope giraffe lion tiger)
Put another way, the variable animals points to the list (antelope giraffe
lion tiger).
Next, evaluate the function setcar while passing it two arguments, the
variable animals and the quoted symbol hippopotamus; this is done by
writing the three element list (setcar animals ’hippopotamus) and then
evaluating it in the usual fashion:
" 7 0) (89.9971923828125 550.5375366210938 449.12408447265625 587.2271728515625 "(setcar animals ’hippopotamus)
After evaluating this expression, evaluate the variable animals again. You
will see that the list of animals has changed:
" 8 0) (89.99752807617188 593.4974975585938 420.6189880371094 630.7871704101562 "animals
⇒ (hippopotamus giraffe lion tiger)
The first element on the list, antelope is replaced by hippopotamus.
" 9 0)) (106 (90.0 47.60907745361328 449.3889465332031 60.90726852416992 "88
Chapter 7: car, cdr, cons: Fundamental Functions
" 0 0) (90.00006103515625 77.48908233642578 449.4975891113281 102.66727447509766 "So we can see that setcar did not add a new element to the list as cons
would have; it replaced giraffe with hippopotamus; it changed the list.
" 1 0) (90.0001220703125 119.18035125732422 163.20578002929688 133.55357360839844 "7.6 setcdr
" 2 0) (89.999755859375 141.2091064453125 449.41064453125 204.4272918701172 "The setcdr function is similar to the setcar function, except that the
function replaces the second and subsequent elements of a list rather than
the first element.
To see how this works, set the value of the variable to a list of domesti-
cated animals by evaluating the following expression:
" 3 0) (89.99969482421875 209.49765014648438 449.4212951660156 244.86729431152344 "(setq domesticated-animals ’(horse cow sheep goat))
If you now evaluate the list, you will be returned the list (horse cow sheep
goat):
" 4 0) (89.9986572265625 249.93765258789062 449.322509765625 310.8672790527344 "domesticated-animals
⇒ (horse cow sheep goat)
Next, evaluate setcdr with two arguments, the name of the variable
which has a list as its value, and the list to which the cdr of the first list
will be set;
" 5 0) (89.9986572265625 315.9376525878906 449.27886962890625 375.18731689453125 "(setcdr domesticated-animals ’(cat dog))
If you evaluate this expression, the list (cat dog) will appear in the echo
area. This is the value returned by the function. The result we are inter-
ested in is the “side effect”, which we can see by evaluating the variable
domesticated-animals:
" 6 0) (89.99832153320312 380.1376647949219 449.5284118652344 428.2274169921875 "domesticated-animals
⇒ (horse cat dog)
Indeed, the list is changed from (horse cow sheep goat) to (horse cat
dog). The cdr of the list is changed from (cow sheep goat) to (cat dog).
" 7 0) (89.99771118164062 444.6205139160156 176.97572326660156 458.9810485839844 "7.7 Exercise
" 8 0) (89.99710083007812 466.76922607421875 449.5281982421875 515.8274536132812 "Construct a list of four birds by evaluating several expressions with cons.
Find out what happens when you cons a list onto itself. Replace the first
element of the list of four birds with a fish. Replace the rest of that list with
a list of other fish.
" 9 0)) (107 (90.0 47.60907745361328 449.80364990234375 60.90726852416992 "zap-to-char
89
" 0 0) (90.0 75.14844512939453 325.3755187988281 92.38106536865234 "8 Cutting and Storing Text
" 1 0) (89.99948120117188 112.28907012939453 449.3558349609375 264.6672058105469 "Whenever you cut or clip text out of a buffer with a ‘kill’ command in
GNU Emacs, it is stored in a list and you can bring it back with a ‘yank’
command.
(The use of the word ‘kill’ in Emacs for processes which specifically do
not destroy the values of the entities is an unfortunate historical accident.
A much more appropriate word would be ‘clip’ since that is what the kill
commands do; they clip text out of a buffer and put it into storage from
which it can be brought back. I have often been tempted to replace globally
all occurrences of ‘kill’ in the Emacs sources with ‘clip’ and all occurrences
of ‘killed’ with ‘clipped’.)
When text is cut out of a buffer, it is stored on a list. Successive pieces
of text are stored on the list successively, so the list might look like this:
" 2 0) (90.00041198730469 271.4175720214844 440.9891357421875 296.7072448730469 "(\"a piece of text\" \"previous piece\")
The function cons can be used to add a piece of text to the list, like this:
" 3 0) (111.60003662109375 303.4576110839844 318.2345886230469 324.9039611816406 "(cons \"another piece\"
’(\"a piece of text\" \"previous piece\"))
" 4 0) (89.999755859375 329.0090637207031 449.3665771484375 354.3072509765625 "If you evaluate this expression, a list of three elements will appear in the
echo area:
" 5 0) (89.99966430664062 361.0576171875 449.464599609375 422.1072692871094 "(\"another piece\" \"a piece of text\" \"previous piece\")
With the car and nthcdr functions, you can retrieve whichever piece of
text you want. For example, in the following code, nthcdr 1 ... returns
the list with the first item removed; and the car returns the first element of
that remainder—the second element of the original list:
" 6 0) (89.99789428710938 428.9776306152344 449.49700927734375 630.787353515625 "(car (nthcdr 1 ’(\"another piece\"
\"a piece of text\"
\"previous piece\")))
⇒ \"a piece of text\"
The actual functions in Emacs are more complex than this, of course.
The code for cutting and retrieving text has to be written so that Emacs
can figure out which element in the list you want—the first, second, third,
or whatever. In addition, when you get to the end of the list, Emacs should
give you the first element of the list, rather than nothing at all.
The list that holds the pieces of text is called the kill ring. This chapter
leads up to a description of the kill ring and how it is used by first tracing how
the zap-to-char function works. This function uses (or ‘calls’) a function
that invokes a function that manipulates the kill ring. Thus, before reaching
the mountains, we climb the foothills.
A subsequent chapter describes how text that is cut from the buffer is
retrieved. See Chapter 10, “Yanking Text Back”, page 117.
" 7 0))))

(provide 'baleen)
;;; baleen.el ends here

  reply	other threads:[~2023-02-24 21:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 19:29 displaying margins leads to Emacs hanging dalanicolai
2023-02-24 19:41 ` Eli Zaretskii
2023-02-24 20:26   ` dalanicolai
2023-02-24 20:29     ` Eli Zaretskii
2023-02-24 20:41       ` dalanicolai
2023-02-24 20:53         ` dalanicolai
2023-02-24 21:19           ` dalanicolai
2023-02-24 21:43             ` dalanicolai [this message]
2023-02-25 10:51           ` Eli Zaretskii
2023-02-25 13:17             ` dalanicolai
2023-02-25 13:42               ` Eli Zaretskii
2023-03-06 22:51                 ` dalanicolai
2023-02-24 21:25         ` Eli Zaretskii
2023-02-24 21:41           ` dalanicolai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACJP=3kBRTz3Ha2QNzqeBuPo7GdMcgS7h9+HCcPc1d7mHNDSMQ@mail.gmail.com' \
    --to=dalanicolai@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).