internals.texi 60.2 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
3 4
@c Copyright (C) 1990-1993, 1998-1999, 2001-2013 Free Software
@c Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
5
@c See the file elisp.texi for copying conditions.
6
@node GNU Emacs Internals
Glenn Morris's avatar
Glenn Morris committed
7 8 9 10 11 12 13 14
@appendix GNU Emacs Internals

This chapter describes how the runnable Emacs executable is dumped with
the preloaded Lisp libraries in it, how storage is allocated, and some
internal aspects of GNU Emacs that may be of interest to C programmers.

@menu
* Building Emacs::      How the dumped Emacs is made.
Paul Eggert's avatar
Paul Eggert committed
15
* Pure Storage::        Kludge to make preloaded Lisp functions shareable.
Glenn Morris's avatar
Glenn Morris committed
16 17 18 19
* Garbage Collection::  Reclaiming space for Lisp objects no longer used.
* Memory Usage::        Info about total size of Lisp objects made so far.
* Writing Emacs Primitives::   Writing C code for Emacs.
* Object Internals::    Data formats of buffers, windows, processes.
20
* C Integer Types::     How C integer types are used inside Emacs.
Glenn Morris's avatar
Glenn Morris committed
21 22 23
@end menu

@node Building Emacs
24
@section Building Emacs
Glenn Morris's avatar
Glenn Morris committed
25 26 27 28 29 30
@cindex building Emacs
@pindex temacs

  This section explains the steps involved in building the Emacs
executable.  You don't have to know this material to build and install
Emacs, since the makefiles do all these things automatically.  This
31
information is pertinent to Emacs developers.
Glenn Morris's avatar
Glenn Morris committed
32 33 34

   Compilation of the C source files in the @file{src} directory
produces an executable file called @file{temacs}, also called a
35 36
@dfn{bare impure Emacs}.  It contains the Emacs Lisp interpreter and
I/O routines, but not the editing commands.
Glenn Morris's avatar
Glenn Morris committed
37 38

@cindex @file{loadup.el}
39 40 41 42 43
  The command @w{@command{temacs -l loadup}} would run @file{temacs}
and direct it to load @file{loadup.el}.  The @code{loadup} library
loads additional Lisp libraries, which set up the normal Emacs editing
environment.  After this step, the Emacs executable is no longer
@dfn{bare}.
Glenn Morris's avatar
Glenn Morris committed
44 45

@cindex dumping Emacs
46 47 48 49 50 51 52 53 54 55 56
  Because it takes some time to load the standard Lisp files, the
@file{temacs} executable usually isn't run directly by users.
Instead, as one of the last steps of building Emacs, the command
@samp{temacs -batch -l loadup dump} is run.  The special @samp{dump}
argument causes @command{temacs} to dump out an executable program,
called @file{emacs}, which has all the standard Lisp files preloaded.
(The @samp{-batch} argument prevents @file{temacs} from trying to
initialize any of its data on the terminal, so that the tables of
terminal information are empty in the dumped Emacs.)

@cindex preloaded Lisp files
57
@vindex preloaded-file-list
58 59 60 61 62 63
  The dumped @file{emacs} executable (also called a @dfn{pure} Emacs)
is the one which is installed.  The variable
@code{preloaded-file-list} stores a list of the Lisp files preloaded
into the dumped Emacs.  If you port Emacs to a new operating system,
and are not able to implement dumping, then Emacs must load
@file{loadup.el} each time it starts.
Glenn Morris's avatar
Glenn Morris committed
64 65 66

@cindex @file{site-load.el}
  You can specify additional files to preload by writing a library named
67 68
@file{site-load.el} that loads them.  You may need to rebuild Emacs
with an added definition
Glenn Morris's avatar
Glenn Morris committed
69 70 71 72 73 74

@example
#define SITELOAD_PURESIZE_EXTRA @var{n}
@end example

@noindent
75 76
to make @var{n} added bytes of pure space to hold the additional files;
see @file{src/puresize.h}.
Glenn Morris's avatar
Glenn Morris committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
(Try adding increments of 20000 until it is big enough.)  However, the
advantage of preloading additional files decreases as machines get
faster.  On modern machines, it is usually not advisable.

  After @file{loadup.el} reads @file{site-load.el}, it finds the
documentation strings for primitive and preloaded functions (and
variables) in the file @file{etc/DOC} where they are stored, by
calling @code{Snarf-documentation} (@pxref{Definition of
Snarf-documentation,, Accessing Documentation}).

@cindex @file{site-init.el}
@cindex preloading additional functions and variables
  You can specify other Lisp expressions to execute just before dumping
by putting them in a library named @file{site-init.el}.  This file is
executed after the documentation strings are found.

  If you want to preload function or variable definitions, there are
three ways you can do this and make their documentation strings
accessible when you subsequently run Emacs:

@itemize @bullet
@item
Arrange to scan these files when producing the @file{etc/DOC} file,
and load them with @file{site-load.el}.

@item
Load the files with @file{site-init.el}, then copy the files into the
installation directory for Lisp files when you install Emacs.

@item
107 108 109 110 111
Specify a @code{nil} value for @code{byte-compile-dynamic-docstrings}
as a local variable in each of these files, and load them with either
@file{site-load.el} or @file{site-init.el}.  (This method has the
drawback that the documentation strings take up space in Emacs all the
time.)
Glenn Morris's avatar
Glenn Morris committed
112 113 114 115 116 117 118
@end itemize

  It is not advisable to put anything in @file{site-load.el} or
@file{site-init.el} that would alter any of the features that users
expect in an ordinary unmodified Emacs.  If you feel you must override
normal features for your site, do it with @file{default.el}, so that
users can override your changes if they wish.  @xref{Startup Summary}.
119 120 121 122 123
Note that if either @file{site-load.el} or @file{site-init.el} changes
@code{load-path}, the changes will be lost after dumping.
@xref{Library Search}.  To make a permanent change to
@code{load-path}, use the @option{--enable-locallisppath} option
of @command{configure}.
Glenn Morris's avatar
Glenn Morris committed
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138
  In a package that can be preloaded, it is sometimes necessary (or
useful) to delay certain evaluations until Emacs subsequently starts
up.  The vast majority of such cases relate to the values of
customizable variables.  For example, @code{tutorial-directory} is a
variable defined in @file{startup.el}, which is preloaded.  The default
value is set based on @code{data-directory}.  The variable needs to
access the value of @code{data-directory} when Emacs starts, not when
it is dumped, because the Emacs executable has probably been installed
in a different location since it was dumped.

@defun custom-initialize-delay symbol value
This function delays the initialization of @var{symbol} to the next
Emacs start.  You normally use this function by specifying it as the
@code{:initialize} property of a customizable variable.  (The argument
Paul Eggert's avatar
Paul Eggert committed
139
@var{value} is unused, and is provided only for compatibility with the
140 141
form Custom expects.)
@end defun
Glenn Morris's avatar
Glenn Morris committed
142

143 144 145
In the unlikely event that you need a more general functionality than
@code{custom-initialize-delay} provides, you can use
@code{before-init-hook} (@pxref{Startup Summary}).
Glenn Morris's avatar
Glenn Morris committed
146 147 148 149 150 151 152 153 154 155 156 157

@defun dump-emacs to-file from-file
@cindex unexec
This function dumps the current state of Emacs into an executable file
@var{to-file}.  It takes symbols from @var{from-file} (this is normally
the executable file @file{temacs}).

If you want to use this function in an Emacs that was already dumped,
you must run Emacs with @samp{-batch}.
@end defun

@node Pure Storage
158
@section Pure Storage
Glenn Morris's avatar
Glenn Morris committed
159 160 161 162
@cindex pure storage

  Emacs Lisp uses two kinds of storage for user-created Lisp objects:
@dfn{normal storage} and @dfn{pure storage}.  Normal storage is where
163 164 165 166
all the new data created during an Emacs session are kept
(@pxref{Garbage Collection}).  Pure storage is used for certain data
in the preloaded standard Lisp files---data that should never change
during actual use of Emacs.
Glenn Morris's avatar
Glenn Morris committed
167

168
  Pure storage is allocated only while @command{temacs} is loading the
Glenn Morris's avatar
Glenn Morris committed
169 170 171 172 173 174
standard preloaded Lisp libraries.  In the file @file{emacs}, it is
marked as read-only (on operating systems that permit this), so that
the memory space can be shared by all the Emacs jobs running on the
machine at once.  Pure storage is not expandable; a fixed amount is
allocated when Emacs is compiled, and if that is not sufficient for
the preloaded libraries, @file{temacs} allocates dynamic memory for
175 176 177 178 179 180 181 182
the part that didn't fit.  The resulting image will work, but garbage
collection (@pxref{Garbage Collection}) is disabled in this situation,
causing a memory leak.  Such an overflow normally won't happen unless
you try to preload additional libraries or add features to the
standard ones.  Emacs will display a warning about the overflow when
it starts.  If this happens, you should increase the compilation
parameter @code{SYSTEM_PURESIZE_EXTRA} in the file
@file{src/puresize.h} and rebuild Emacs.
Glenn Morris's avatar
Glenn Morris committed
183 184 185 186 187 188 189 190 191 192

@defun purecopy object
This function makes a copy in pure storage of @var{object}, and returns
it.  It copies a string by simply making a new string with the same
characters, but without text properties, in pure storage.  It
recursively copies the contents of vectors and cons cells.  It does
not make copies of other objects such as symbols, but just returns
them unchanged.  It signals an error if asked to copy markers.

This function is a no-op except while Emacs is being built and dumped;
193
it is usually called only in preloaded Lisp files.
Glenn Morris's avatar
Glenn Morris committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
@end defun

@defvar pure-bytes-used
The value of this variable is the number of bytes of pure storage
allocated so far.  Typically, in a dumped Emacs, this number is very
close to the total amount of pure storage available---if it were not,
we would preallocate less.
@end defvar

@defvar purify-flag
This variable determines whether @code{defun} should make a copy of the
function definition in pure storage.  If it is non-@code{nil}, then the
function definition is copied into pure storage.

This flag is @code{t} while loading all of the basic functions for
Paul Eggert's avatar
Paul Eggert committed
209
building Emacs initially (allowing those functions to be shareable and
Glenn Morris's avatar
Glenn Morris committed
210 211 212 213 214 215 216 217
non-collectible).  Dumping Emacs as an executable always writes
@code{nil} in this variable, regardless of the value it actually has
before and after dumping.

You should not change this flag in a running Emacs.
@end defvar

@node Garbage Collection
218
@section Garbage Collection
Glenn Morris's avatar
Glenn Morris committed
219 220

@cindex memory allocation
221 222 223 224
  When a program creates a list or the user defines a new function
(such as by loading a library), that data is placed in normal storage.
If normal storage runs low, then Emacs asks the operating system to
allocate more memory.  Different types of Lisp objects, such as
225 226 227 228 229 230 231 232 233 234 235
symbols, cons cells, small vectors, markers, etc., are segregated in
distinct blocks in memory.  (Large vectors, long strings, buffers and
certain other editing types, which are fairly large, are allocated in
individual blocks, one per object; small strings are packed into blocks
of 8k bytes, and small vectors are packed into blocks of 4k bytes).

@cindex vector-like objects, storage
@cindex storage of vector-like Lisp objects
  Beyond the basic vector, a lot of objects like window, buffer, and
frame are managed as if they were vectors.  The corresponding C data
structures include the @code{struct vectorlike_header} field whose
236 237 238 239 240
@code{size} member contains the subtype enumerated by @code{enum pvec_type}
and an information about how many @code{Lisp_Object} fields this structure
contains and what the size of the rest data is.  This information is
needed to calculate the memory footprint of an object, and used
by the vector allocation code while iterating over the vector blocks.
Glenn Morris's avatar
Glenn Morris committed
241

242 243 244 245 246 247 248 249 250 251
@cindex garbage collection
  It is quite common to use some storage for a while, then release it
by (for example) killing a buffer or deleting the last pointer to an
object.  Emacs provides a @dfn{garbage collector} to reclaim this
abandoned storage.  The garbage collector operates by finding and
marking all Lisp objects that are still accessible to Lisp programs.
To begin with, it assumes all the symbols, their values and associated
function definitions, and any data presently on the stack, are
accessible.  Any objects that can be reached indirectly through other
accessible objects are also accessible.
Glenn Morris's avatar
Glenn Morris committed
252 253 254 255 256 257 258 259 260 261 262 263 264

  When marking is finished, all objects still unmarked are garbage.  No
matter what the Lisp program or the user does, it is impossible to refer
to them, since there is no longer a way to reach them.  Their space
might as well be reused, since no one will miss them.  The second
(``sweep'') phase of the garbage collector arranges to reuse them.

@c ??? Maybe add something describing weak hash tables here?

@cindex free list
  The sweep phase puts unused cons cells onto a @dfn{free list}
for future allocation; likewise for symbols and markers.  It compacts
the accessible strings so they occupy fewer 8k blocks; then it frees the
265 266 267 268 269 270
other 8k blocks.  Unreachable vectors from vector blocks are coalesced
to create largest possible free areas; if a free area spans a complete
4k block, that block is freed.  Otherwise, the free area is recorded
in a free list array, where each entry corresponds to a free list
of areas of the same size.  Large vectors, buffers, and other large
objects are allocated and freed individually.
Glenn Morris's avatar
Glenn Morris committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

@cindex CL note---allocate more storage
@quotation
@b{Common Lisp note:} Unlike other Lisps, GNU Emacs Lisp does not
call the garbage collector when the free list is empty.  Instead, it
simply requests the operating system to allocate more storage, and
processing continues until @code{gc-cons-threshold} bytes have been
used.

This means that you can make sure that the garbage collector will not
run during a certain portion of a Lisp program by calling the garbage
collector explicitly just before it (provided that portion of the
program does not use so much space as to force a second garbage
collection).
@end quotation

@deffn Command garbage-collect
This command runs a garbage collection, and returns information on
the amount of space in use.  (Garbage collection can also occur
spontaneously if you use more than @code{gc-cons-threshold} bytes of
Lisp data since the previous garbage collection.)

293 294 295 296 297 298 299 300
@code{garbage-collect} returns a list with information on amount of space in
use, where each entry has the form @samp{(@var{name} @var{size} @var{used})}
or @samp{(@var{name} @var{size} @var{used} @var{free})}.  In the entry,
@var{name} is a symbol describing the kind of objects this entry represents,
@var{size} is the number of bytes used by each one, @var{used} is the number
of those objects that were found live in the heap, and optional @var{free} is
the number of those objects that are not live but that Emacs keeps around for
future allocations.  So an overall result is:
Glenn Morris's avatar
Glenn Morris committed
301 302

@example
Paul Eggert's avatar
Paul Eggert committed
303
((@code{conses} @var{cons-size} @var{used-conses} @var{free-conses})
304 305 306 307 308 309 310 311 312 313
 (@code{symbols} @var{symbol-size} @var{used-symbols} @var{free-symbols})
 (@code{miscs} @var{misc-size} @var{used-miscs} @var{free-miscs})
 (@code{strings} @var{string-size} @var{used-strings} @var{free-strings})
 (@code{string-bytes} @var{byte-size} @var{used-bytes})
 (@code{vectors} @var{vector-size} @var{used-vectors})
 (@code{vector-slots} @var{slot-size} @var{used-slots} @var{free-slots})
 (@code{floats} @var{float-size} @var{used-floats} @var{free-floats})
 (@code{intervals} @var{interval-size} @var{used-intervals} @var{free-intervals})
 (@code{buffers} @var{buffer-size} @var{used-buffers})
 (@code{heap} @var{unit-size} @var{total-size} @var{free-size}))
Glenn Morris's avatar
Glenn Morris committed
314 315 316 317 318 319
@end example

Here is an example:

@example
(garbage-collect)
320 321 322 323 324 325
      @result{} ((conses 16 49126 8058) (symbols 48 14607 0)
                 (miscs 40 34 56) (strings 32 2942 2607)
                 (string-bytes 1 78607) (vectors 16 7247)
                 (vector-slots 8 341609 29474) (floats 8 71 102)
                 (intervals 56 27 26) (buffers 944 8)
                 (heap 1024 11715 2678))
Glenn Morris's avatar
Glenn Morris committed
326 327
@end example

328 329 330
Below is a table explaining each element.  Note that last @code{heap} entry
is optional and present only if an underlying @code{malloc} implementation
provides @code{mallinfo} function.
Glenn Morris's avatar
Glenn Morris committed
331 332

@table @var
333
@item cons-size
334
Internal size of a cons cell, i.e., @code{sizeof (struct Lisp_Cons)}.
335

Glenn Morris's avatar
Glenn Morris committed
336 337 338 339
@item used-conses
The number of cons cells in use.

@item free-conses
340 341
The number of cons cells for which space has been obtained from
the operating system, but that are not currently being used.
Glenn Morris's avatar
Glenn Morris committed
342

343
@item symbol-size
344
Internal size of a symbol, i.e., @code{sizeof (struct Lisp_Symbol)}.
345 346

@item used-symbols
Glenn Morris's avatar
Glenn Morris committed
347 348
The number of symbols in use.

349 350 351 352 353
@item free-symbols
The number of symbols for which space has been obtained from
the operating system, but that are not currently being used.

@item misc-size
354
Internal size of a miscellaneous entity, i.e.,
355 356
@code{sizeof (union Lisp_Misc)}, which is a size of the
largest type enumerated in @code{enum Lisp_Misc_Type}.
Glenn Morris's avatar
Glenn Morris committed
357 358

@item used-miscs
359 360
The number of miscellaneous objects in use.  These include markers
and overlays, plus certain objects not visible to users.
Glenn Morris's avatar
Glenn Morris committed
361 362 363 364 365

@item free-miscs
The number of miscellaneous objects for which space has been obtained
from the operating system, but that are not currently being used.

366
@item string-size
367
Internal size of a string header, i.e., @code{sizeof (struct Lisp_String)}.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

@item used-strings
The number of string headers in use.

@item free-strings
The number of string headers for which space has been obtained
from the operating system, but that are not currently being used.

@item byte-size
This is used for convenience and equals to @code{sizeof (char)}.

@item used-bytes
The total size of all string data in bytes.

@item vector-size
383
Internal size of a vector header, i.e., @code{sizeof (struct Lisp_Vector)}.
384 385 386 387 388 389 390 391 392 393 394 395

@item used-vectors
The number of vector headers allocated from the vector blocks.

@item slot-size
Internal size of a vector slot, always equal to @code{sizeof (Lisp_Object)}.

@item used-slots
The number of slots in all used vectors.

@item free-slots
The number of free slots in all vector blocks.
Glenn Morris's avatar
Glenn Morris committed
396

397
@item float-size
398
Internal size of a float object, i.e., @code{sizeof (struct Lisp_Float)}.
399
(Do not confuse it with the native platform @code{float} or @code{double}.)
Glenn Morris's avatar
Glenn Morris committed
400 401 402 403 404

@item used-floats
The number of floats in use.

@item free-floats
405 406 407 408
The number of floats for which space has been obtained from
the operating system, but that are not currently being used.

@item interval-size
409
Internal size of an interval object, i.e., @code{sizeof (struct interval)}.
Glenn Morris's avatar
Glenn Morris committed
410 411

@item used-intervals
412
The number of intervals in use.
Glenn Morris's avatar
Glenn Morris committed
413 414

@item free-intervals
415 416
The number of intervals for which space has been obtained from
the operating system, but that are not currently being used.
Glenn Morris's avatar
Glenn Morris committed
417

418
@item buffer-size
419
Internal size of a buffer, i.e., @code{sizeof (struct buffer)}.
420
(Do not confuse with the value returned by @code{buffer-size} function.)
Glenn Morris's avatar
Glenn Morris committed
421

422 423
@item used-buffers
The number of buffer objects in use.  This includes killed buffers
424
invisible to users, i.e., all buffers in @code{all_buffers} list.
425 426 427 428 429 430 431 432 433

@item unit-size
The unit of heap space measurement, always equal to 1024 bytes.

@item total-size
Total heap size, in @var{unit-size} units.

@item free-size
Heap space which is not currently used, in @var{unit-size} units.
Glenn Morris's avatar
Glenn Morris committed
434 435
@end table

436
If there was overflow in pure space (@pxref{Pure Storage}),
Glenn Morris's avatar
Glenn Morris committed
437
@code{garbage-collect} returns @code{nil}, because a real garbage
438
collection cannot be done.
Glenn Morris's avatar
Glenn Morris committed
439 440 441 442 443
@end deffn

@defopt garbage-collection-messages
If this variable is non-@code{nil}, Emacs displays a message at the
beginning and end of garbage collection.  The default value is
444
@code{nil}.
Glenn Morris's avatar
Glenn Morris committed
445 446 447 448 449 450 451 452 453 454 455
@end defopt

@defvar post-gc-hook
This is a normal hook that is run at the end of garbage collection.
Garbage collection is inhibited while the hook functions run, so be
careful writing them.
@end defvar

@defopt gc-cons-threshold
The value of this variable is the number of bytes of storage that must
be allocated for Lisp objects after one garbage collection in order to
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
trigger another garbage collection.  You can use the result returned by
@code{garbage-collect} to get an information about size of the particular
object type; space allocated to the contents of buffers does not count.
Note that the subsequent garbage collection does not happen immediately
when the threshold is exhausted, but only the next time the Lisp interpreter
is called.

The initial threshold value is @code{GC_DEFAULT_THRESHOLD}, defined in
@file{alloc.c}.  Since it's defined in @code{word_size} units, the value
is 400,000 for the default 32-bit configuration and 800,000 for the 64-bit
one.  If you specify a larger value, garbage collection will happen less
often.  This reduces the amount of time spent garbage collecting, but
increases total memory use.  You may want to do this when running a program
that creates lots of Lisp data.

You can make collections more frequent by specifying a smaller value, down
to 1/10th of @code{GC_DEFAULT_THRESHOLD}.  A value less than this minimum
will remain in effect only until the subsequent garbage collection, at which
time @code{garbage-collect} will set the threshold back to the minimum.
Glenn Morris's avatar
Glenn Morris committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
@end defopt

@defopt gc-cons-percentage
The value of this variable specifies the amount of consing before a
garbage collection occurs, as a fraction of the current heap size.
This criterion and @code{gc-cons-threshold} apply in parallel, and
garbage collection occurs only when both criteria are satisfied.

As the heap size increases, the time to perform a garbage collection
increases.  Thus, it can be desirable to do them less frequently in
proportion.
@end defopt

  The value returned by @code{garbage-collect} describes the amount of
memory used by Lisp data, broken down by data type.  By contrast, the
function @code{memory-limit} provides information on the total amount of
memory Emacs is currently using.

@defun memory-limit
This function returns the address of the last byte Emacs has allocated,
divided by 1024.  We divide the value by 1024 to make sure it fits in a
Lisp integer.

You can use this to get a general idea of how your actions affect the
memory usage.
@end defun

@defvar memory-full
503
This variable is @code{t} if Emacs is nearly out of memory for Lisp
Glenn Morris's avatar
Glenn Morris committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
objects, and @code{nil} otherwise.
@end defvar

@defun memory-use-counts
This returns a list of numbers that count the number of objects
created in this Emacs session.  Each of these counters increments for
a certain kind of object.  See the documentation string for details.
@end defun

@defvar gcs-done
This variable contains the total number of garbage collections
done so far in this Emacs session.
@end defvar

@defvar gc-elapsed
This variable contains the total number of seconds of elapsed time
during garbage collection so far in this Emacs session, as a floating
point number.
@end defvar

@node Memory Usage
@section Memory Usage
@cindex memory usage

  These functions and variables give information about the total amount
of memory allocation that Emacs has done, broken down by data type.
Note the difference between these and the values returned by
531
@code{garbage-collect}; those count objects that currently exist, but
Glenn Morris's avatar
Glenn Morris committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
these count the number or size of all allocations, including those for
objects that have since been freed.

@defvar cons-cells-consed
The total number of cons cells that have been allocated so far
in this Emacs session.
@end defvar

@defvar floats-consed
The total number of floats that have been allocated so far
in this Emacs session.
@end defvar

@defvar vector-cells-consed
The total number of vector cells that have been allocated so far
in this Emacs session.
@end defvar

@defvar symbols-consed
The total number of symbols that have been allocated so far
in this Emacs session.
@end defvar

@defvar string-chars-consed
The total number of string characters that have been allocated so far
557
in this session.
Glenn Morris's avatar
Glenn Morris committed
558 559 560 561
@end defvar

@defvar misc-objects-consed
The total number of miscellaneous objects that have been allocated so
562
far in this session.  These include markers and overlays, plus
Glenn Morris's avatar
Glenn Morris committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576
certain objects not visible to users.
@end defvar

@defvar intervals-consed
The total number of intervals that have been allocated so far
in this Emacs session.
@end defvar

@defvar strings-consed
The total number of strings that have been allocated so far in this
Emacs session.
@end defvar

@node Writing Emacs Primitives
577
@section Writing Emacs Primitives
Glenn Morris's avatar
Glenn Morris committed
578 579 580
@cindex primitive function internals
@cindex writing Emacs primitives

581
  Lisp primitives are Lisp functions implemented in C@.  The details of
Glenn Morris's avatar
Glenn Morris committed
582 583 584 585 586 587 588 589 590 591 592 593
interfacing the C function so that Lisp can call it are handled by a few
C macros.  The only way to really understand how to write new C code is
to read the source, but we can explain some things here.

  An example of a special form is the definition of @code{or}, from
@file{eval.c}.  (An ordinary function would have the same general
appearance.)

@cindex garbage collection protection
@smallexample
@group
DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
594 595 596
  doc: /* Eval args until one of them yields non-nil, then return
that value.
The remaining args are not evalled at all.
Glenn Morris's avatar
Glenn Morris committed
597 598 599 600
If all args return nil, return nil.
@end group
@group
usage: (or CONDITIONS ...)  */)
601
  (Lisp_Object args)
Glenn Morris's avatar
Glenn Morris committed
602 603 604 605 606 607 608 609 610 611 612 613
@{
  register Lisp_Object val = Qnil;
  struct gcpro gcpro1;
@end group

@group
  GCPRO1 (args);
@end group

@group
  while (CONSP (args))
    @{
614
      val = eval_sub (XCAR (args));
Glenn Morris's avatar
Glenn Morris committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
      if (!NILP (val))
        break;
      args = XCDR (args);
    @}
@end group

@group
  UNGCPRO;
  return val;
@}
@end group
@end smallexample

@cindex @code{DEFUN}, C macro to define Lisp primitives
  Let's start with a precise explanation of the arguments to the
@code{DEFUN} macro.  Here is a template for them:

@example
DEFUN (@var{lname}, @var{fname}, @var{sname}, @var{min}, @var{max}, @var{interactive}, @var{doc})
@end example

@table @var
@item lname
This is the name of the Lisp symbol to define as the function name; in
the example above, it is @code{or}.

@item fname
642 643 644 645 646
This is the C function name for this function.  This is the name that
is used in C code for calling the function.  The name is, by
convention, @samp{F} prepended to the Lisp name, with all dashes
(@samp{-}) in the Lisp name changed to underscores.  Thus, to call
this function from C code, call @code{For}.
Glenn Morris's avatar
Glenn Morris committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

@item sname
This is a C variable name to use for a structure that holds the data for
the subr object that represents the function in Lisp.  This structure
conveys the Lisp symbol name to the initialization routine that will
create the symbol and store the subr object as its definition.  By
convention, this name is always @var{fname} with @samp{F} replaced with
@samp{S}.

@item min
This is the minimum number of arguments that the function requires.  The
function @code{or} allows a minimum of zero arguments.

@item max
This is the maximum number of arguments that the function accepts, if
there is a fixed maximum.  Alternatively, it can be @code{UNEVALLED},
indicating a special form that receives unevaluated arguments, or
@code{MANY}, indicating an unlimited number of evaluated arguments (the
equivalent of @code{&rest}).  Both @code{UNEVALLED} and @code{MANY} are
666 667
macros.  If @var{max} is a number, it must be more than @var{min} but
less than 8.
Glenn Morris's avatar
Glenn Morris committed
668

669
@cindex interactive specification in primitives
Glenn Morris's avatar
Glenn Morris committed
670
@item interactive
671 672 673
This is an interactive specification, a string such as might be used
as the argument of @code{interactive} in a Lisp function.  In the case
of @code{or}, it is 0 (a null pointer), indicating that @code{or}
674 675
cannot be called interactively.  A value of @code{""} indicates a
function that should receive no arguments when called interactively.
676 677 678
If the value begins with a @samp{"(}, the string is evaluated as a
Lisp form.  For example:

679
@example
680
@group
681 682
DEFUN ("foo", Ffoo, Sfoo, 0, UNEVALLED,
       "(list (read-char-by-name \"Insert character: \")\
683 684 685 686
              (prefix-numeric-value current-prefix-arg)\
              t))",
  doc: /* @dots{} /*)
@end group
687
@end example
Glenn Morris's avatar
Glenn Morris committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

@item doc
This is the documentation string.  It uses C comment syntax rather
than C string syntax because comment syntax requires nothing special
to include multiple lines.  The @samp{doc:} identifies the comment
that follows as the documentation string.  The @samp{/*} and @samp{*/}
delimiters that begin and end the comment are not part of the
documentation string.

If the last line of the documentation string begins with the keyword
@samp{usage:}, the rest of the line is treated as the argument list
for documentation purposes.  This way, you can use different argument
names in the documentation string from the ones used in the C code.
@samp{usage:} is required if the function has an unlimited number of
arguments.

All the usual rules for documentation strings in Lisp code
(@pxref{Documentation Tips}) apply to C code documentation strings
too.
@end table

709
  After the call to the @code{DEFUN} macro, you must write the
710 711 712 713 714 715 716 717 718
argument list for the C function, including the types for the
arguments.  If the primitive accepts a fixed maximum number of Lisp
arguments, there must be one C argument for each Lisp argument, and
each argument must be of type @code{Lisp_Object}.  (Various macros and
functions for creating values of type @code{Lisp_Object} are declared
in the file @file{lisp.h}.)  If the primitive has no upper limit on
the number of Lisp arguments, it must have exactly two C arguments:
the first is the number of Lisp arguments, and the second is the
address of a block containing their values.  These have types
719
@code{int} and @w{@code{Lisp_Object *}} respectively.  Since
720 721 722 723 724
@code{Lisp_Object} can hold any Lisp object of any data type, you
can determine the actual data type only at run time; so if you want
a primitive to accept only a certain type of argument, you must check
the type explicitly using a suitable predicate (@pxref{Type Predicates}).
@cindex type checking internals
Glenn Morris's avatar
Glenn Morris committed
725 726 727 728

@cindex @code{GCPRO} and @code{UNGCPRO}
@cindex protect C variables from garbage collection
  Within the function @code{For} itself, note the use of the macros
729 730 731 732 733 734 735 736
@code{GCPRO1} and @code{UNGCPRO}.  These macros are defined for the
sake of the few platforms which do not use Emacs' default
stack-marking garbage collector.  The @code{GCPRO1} macro ``protects''
a variable from garbage collection, explicitly informing the garbage
collector that that variable and all its contents must be as
accessible.  GC protection is necessary in any function which can
perform Lisp evaluation by calling @code{eval_sub} or @code{Feval} as
a subroutine, either directly or indirectly.
Glenn Morris's avatar
Glenn Morris committed
737 738

  It suffices to ensure that at least one pointer to each object is
739
GC-protected.  Thus, a particular local variable can do without
Glenn Morris's avatar
Glenn Morris committed
740
protection if it is certain that the object it points to will be
741
preserved by some other pointer (such as another local variable that
742 743
has a @code{GCPRO}).  Otherwise, the local variable needs a
@code{GCPRO}.
Glenn Morris's avatar
Glenn Morris committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

  The macro @code{GCPRO1} protects just one local variable.  If you
want to protect two variables, use @code{GCPRO2} instead; repeating
@code{GCPRO1} will not work.  Macros @code{GCPRO3}, @code{GCPRO4},
@code{GCPRO5}, and @code{GCPRO6} also exist.  All these macros
implicitly use local variables such as @code{gcpro1}; you must declare
these explicitly, with type @code{struct gcpro}.  Thus, if you use
@code{GCPRO2}, you must declare @code{gcpro1} and @code{gcpro2}.

  @code{UNGCPRO} cancels the protection of the variables that are
protected in the current function.  It is necessary to do this
explicitly.

  You must not use C initializers for static or global variables unless
the variables are never written once Emacs is dumped.  These variables
with initializers are allocated in an area of memory that becomes
read-only (on certain operating systems) as a result of dumping Emacs.
@xref{Pure Storage}.

@cindex @code{defsubr}, Lisp symbol for a primitive
  Defining the C function is not enough to make a Lisp primitive
available; you must also create the Lisp symbol for the primitive and
store a suitable subr object in its function cell.  The code looks like
this:

@example
770
defsubr (&@var{sname});
Glenn Morris's avatar
Glenn Morris committed
771 772 773
@end example

@noindent
774
Here @var{sname} is the name you used as the third argument to @code{DEFUN}.
Glenn Morris's avatar
Glenn Morris committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

  If you add a new primitive to a file that already has Lisp primitives
defined in it, find the function (near the end of the file) named
@code{syms_of_@var{something}}, and add the call to @code{defsubr}
there.  If the file doesn't have this function, or if you create a new
file, add to it a @code{syms_of_@var{filename}} (e.g.,
@code{syms_of_myfile}).  Then find the spot in @file{emacs.c} where all
of these functions are called, and add a call to
@code{syms_of_@var{filename}} there.

@anchor{Defining Lisp variables in C}
@vindex byte-boolean-vars
@cindex defining Lisp variables in C
@cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL}
  The function @code{syms_of_@var{filename}} is also the place to define
any C variables that are to be visible as Lisp variables.
@code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible
in Lisp.  @code{DEFVAR_INT} makes a C variable of type @code{int}
visible in Lisp with a value that is always an integer.
@code{DEFVAR_BOOL} makes a C variable of type @code{int} visible in Lisp
with a value that is either @code{t} or @code{nil}.  Note that variables
defined with @code{DEFVAR_BOOL} are automatically added to the list
@code{byte-boolean-vars} used by the byte compiler.

799 800 801 802 803
@cindex defining customization variables in C
  If you want to make a Lisp variables that is defined in C behave
like one declared with @code{defcustom}, add an appropriate entry to
@file{cus-start.el}.

Glenn Morris's avatar
Glenn Morris committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
@cindex @code{staticpro}, protection from GC
  If you define a file-scope C variable of type @code{Lisp_Object},
you must protect it from garbage-collection by calling @code{staticpro}
in @code{syms_of_@var{filename}}, like this:

@example
staticpro (&@var{variable});
@end example

  Here is another example function, with more complicated arguments.
This comes from the code in @file{window.c}, and it demonstrates the use
of macros and functions to manipulate Lisp objects.

@smallexample
@group
DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
820 821 822
  Scoordinates_in_window_p, 2, 2, 0,
  doc: /* Return non-nil if COORDINATES are in WINDOW.
  ...
Glenn Morris's avatar
Glenn Morris committed
823 824
@end group
@group
825 826
  or `right-margin' is returned.  */)
  (register Lisp_Object coordinates, Lisp_Object window)
Glenn Morris's avatar
Glenn Morris committed
827
@{
828 829
  struct window *w;
  struct frame *f;
Glenn Morris's avatar
Glenn Morris committed
830
  int x, y;
831
  Lisp_Object lx, ly;
Glenn Morris's avatar
Glenn Morris committed
832 833 834
@end group

@group
835 836 837 838 839 840 841 842 843 844
  CHECK_LIVE_WINDOW (window);
  w = XWINDOW (window);
  f = XFRAME (w->frame);
  CHECK_CONS (coordinates);
  lx = Fcar (coordinates);
  ly = Fcdr (coordinates);
  CHECK_NUMBER_OR_FLOAT (lx);
  CHECK_NUMBER_OR_FLOAT (ly);
  x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH(f);
  y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH(f);
Glenn Morris's avatar
Glenn Morris committed
845 846 847
@end group

@group
848
  switch (coordinates_in_window (w, x, y))
Glenn Morris's avatar
Glenn Morris committed
849
    @{
850
    case ON_NOTHING:            /* NOT in window at all. */
Glenn Morris's avatar
Glenn Morris committed
851 852 853
      return Qnil;
@end group

854
    ...
Glenn Morris's avatar
Glenn Morris committed
855 856

@group
857
    case ON_MODE_LINE:          /* In mode line of window. */
Glenn Morris's avatar
Glenn Morris committed
858 859 860
      return Qmode_line;
@end group

861 862
    ...

Glenn Morris's avatar
Glenn Morris committed
863
@group
864 865 866
    case ON_SCROLL_BAR:         /* On scroll-bar of window.  */
      /* Historically we are supposed to return nil in this case.  */
      return Qnil;
Glenn Morris's avatar
Glenn Morris committed
867 868 869 870 871 872 873 874 875 876 877
@end group

@group
    default:
      abort ();
    @}
@}
@end group
@end smallexample

  Note that C code cannot call functions by name unless they are defined
878
in C@.  The way to call a function written in Lisp is to use
Glenn Morris's avatar
Glenn Morris committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
@code{Ffuncall}, which embodies the Lisp function @code{funcall}.  Since
the Lisp function @code{funcall} accepts an unlimited number of
arguments, in C it takes two: the number of Lisp-level arguments, and a
one-dimensional array containing their values.  The first Lisp-level
argument is the Lisp function to call, and the rest are the arguments to
pass to it.  Since @code{Ffuncall} can call the evaluator, you must
protect pointers from garbage collection around the call to
@code{Ffuncall}.

  The C functions @code{call0}, @code{call1}, @code{call2}, and so on,
provide handy ways to call a Lisp function conveniently with a fixed
number of arguments.  They work by calling @code{Ffuncall}.

  @file{eval.c} is a very good file to look through for examples;
@file{lisp.h} contains the definitions for some important macros and
functions.

  If you define a function which is side-effect free, update the code
897
in @file{byte-opt.el} that binds @code{side-effect-free-fns} and
Glenn Morris's avatar
Glenn Morris committed
898 899 900 901
@code{side-effect-and-error-free-fns} so that the compiler optimizer
knows about it.

@node Object Internals
902
@section Object Internals
Glenn Morris's avatar
Glenn Morris committed
903 904
@cindex object internals

905
  Emacs Lisp provides a rich set of the data types.  Some of them, like cons
Paul Eggert's avatar
Paul Eggert committed
906
cells, integers and strings, are common to nearly all Lisp dialects.  Some
907 908 909 910 911 912 913 914 915 916 917 918 919
others, like markers and buffers, are quite special and needed to provide
the basic support to write editor commands in Lisp.  To implement such
a variety of object types and provide an efficient way to pass objects between
the subsystems of an interpreter, there is a set of C data structures and
a special type to represent the pointers to all of them, which is known as
@dfn{tagged pointer}.

  In C, the tagged pointer is an object of type @code{Lisp_Object}.  Any
initialized variable of such a type always holds the value of one of the
following basic data types: integer, symbol, string, cons cell, float,
vectorlike or miscellaneous object.  Each of these data types has the
corresponding tag value.  All tags are enumerated by @code{enum Lisp_Type}
and placed into a 3-bit bitfield of the @code{Lisp_Object}.  The rest of the
920
bits is the value itself.  Integer values are immediate, i.e., directly
921 922 923
represented by those @dfn{value bits}, and all other objects are represented
by the C pointers to a corresponding object allocated from the heap.  Width
of the @code{Lisp_Object} is platform- and configuration-dependent: usually
924
it's equal to the width of an underlying platform pointer (i.e., 32-bit on
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
a 32-bit machine and 64-bit on a 64-bit one), but also there is a special
configuration where @code{Lisp_Object} is 64-bit but all pointers are 32-bit.
The latter trick was designed to overcome the limited range of values for
Lisp integers on a 32-bit system by using 64-bit @code{long long} type for
@code{Lisp_Object}.

  The following C data structures are defined in @file{lisp.h} to represent
the basic data types beyond integers:

@table @code
@item struct Lisp_Cons
Cons cell, an object used to construct lists.

@item struct Lisp_String
String, the basic object to represent a sequence of characters.

@item struct Lisp_Vector
Array, a fixed-size set of Lisp objects which may be accessed by an index.

@item struct Lisp_Symbol
Symbol, the unique-named entity commonly used as an identifier.

@item struct Lisp_Float
Floating point value.

@item union Lisp_Misc
Miscellaneous kinds of objects which don't fit into any of the above.
@end table

  These types are the first-class citizens of an internal type system.
Since the tag space is limited, all other types are the subtypes of either
@code{Lisp_Vectorlike} or @code{Lisp_Misc}.  Vector subtypes are enumerated
by @code{enum pvec_type}, and nearly all complex objects like windows, buffers,
frames, and processes fall into this category.  The rest of special types,
including markers and overlays, are enumerated by @code{enum Lisp_Misc_Type}
and form the set of subtypes of @code{Lisp_Misc}.

  Below there is a description of a few subtypes of @code{Lisp_Vectorlike}.
Buffer object represents the text to display and edit.  Window is the part
of display structure which shows the buffer or used as a container to
recursively place other windows on the same frame.  (Do not confuse Emacs Lisp
window object with the window as an entity managed by the user interface
system like X; in Emacs terminology, the latter is called frame.)  Finally,
process object is used to manage the subprocesses.
Glenn Morris's avatar
Glenn Morris committed
969 970 971 972 973 974 975 976

@menu
* Buffer Internals::    Components of a buffer structure.
* Window Internals::    Components of a window structure.
* Process Internals::   Components of a process structure.
@end menu

@node Buffer Internals
977
@subsection Buffer Internals
Glenn Morris's avatar
Glenn Morris committed
978 979 980
@cindex internals, of buffer
@cindex buffer internals

981
  Two structures (see @file{buffer.h}) are used to represent buffers
982
in C@.  The @code{buffer_text} structure contains fields describing the
983 984 985
text of a buffer; the @code{buffer} structure holds other fields.  In
the case of indirect buffers, two or more @code{buffer} structures
reference the same @code{buffer_text} structure.
Glenn Morris's avatar
Glenn Morris committed
986

987
Here are some of the fields in @code{struct buffer_text}:
Glenn Morris's avatar
Glenn Morris committed
988 989 990

@table @code
@item beg
991
The address of the buffer contents.
Glenn Morris's avatar
Glenn Morris committed
992 993

@item gpt
994 995 996
@itemx gpt_byte
The character and byte positions of the buffer gap.  @xref{Buffer
Gap}.
Glenn Morris's avatar
Glenn Morris committed
997 998

@item z
999 1000
@itemx z_byte
The character and byte positions of the end of the buffer text.
Glenn Morris's avatar
Glenn Morris committed
1001 1002

@item gap_size
1003
The size of buffer's gap.  @xref{Buffer Gap}.
Glenn Morris's avatar
Glenn Morris committed
1004 1005

@item modiff
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
@itemx save_modiff
@itemx chars_modiff
@itemx overlay_modiff
These fields count the number of buffer-modification events performed
in this buffer.  @code{modiff} is incremented after each
buffer-modification event, and is never otherwise changed;
@code{save_modiff} contains the value of @code{modiff} the last time
the buffer was visited or saved; @code{chars_modiff} counts only
modifications to the characters in the buffer, ignoring all other
kinds of changes; and @code{overlay_modiff} counts only modifications
to the overlays.
Glenn Morris's avatar
Glenn Morris committed
1017 1018

@item beg_unchanged
1019 1020 1021
@itemx end_unchanged
The number of characters at the start and end of the text that are
known to be unchanged since the last complete redisplay.
Glenn Morris's avatar
Glenn Morris committed
1022 1023

@item unchanged_modified
1024 1025
@itemx overlay_unchanged_modified
The values of @code{modiff} and @code{overlay_modiff}, respectively,
Paul Eggert's avatar
Paul Eggert committed
1026
after the last complete redisplay.  If their current values match
1027
@code{modiff} or @code{overlay_modiff}, that means
Glenn Morris's avatar
Glenn Morris committed
1028 1029 1030 1031 1032 1033 1034 1035 1036
@code{beg_unchanged} and @code{end_unchanged} contain no useful
information.

@item markers
The markers that refer to this buffer.  This is actually a single
marker, and successive elements in its marker @code{chain} are the other
markers referring to this buffer text.

@item intervals
1037
The interval tree which records the text properties of this buffer.
Glenn Morris's avatar
Glenn Morris committed
1038 1039
@end table

1040
Some of the fields of @code{struct buffer} are:
Glenn Morris's avatar
Glenn Morris committed
1041 1042

@table @code
1043
@item header
1044 1045
A header of type @code{struct vectorlike_header} is common to all
vectorlike objects.
Glenn Morris's avatar
Glenn Morris committed
1046 1047

@item own_text
1048 1049
A @code{struct buffer_text} structure that ordinarily holds the buffer
contents.  In indirect buffers, this field is not used.
Glenn Morris's avatar
Glenn Morris committed
1050 1051

@item text
1052 1053 1054
A pointer to the @code{buffer_text} structure for this buffer.  In an
ordinary buffer, this is the @code{own_text} field above.  In an
indirect buffer, this is the @code{own_text} field of the base buffer.
Glenn Morris's avatar
Glenn Morris committed
1055

1056 1057 1058 1059 1060
@item next
A pointer to the next buffer, in the chain of all buffers, including
killed buffers.  This chain is used only for allocation and garbage
collection, in order to collect killed buffers properly.

Glenn Morris's avatar
Glenn Morris committed
1061
@item pt
1062 1063
@itemx pt_byte
The character and byte positions of point in a buffer.
Glenn Morris's avatar
Glenn Morris committed
1064 1065

@item begv
1066 1067 1068
@itemx begv_byte
The character and byte positions of the beginning of the accessible
range of text in the buffer.
Glenn Morris's avatar
Glenn Morris committed
1069 1070

@item zv
1071 1072 1073
@itemx zv_byte
The character and byte positions of the end of the accessible range of
text in the buffer.
Glenn Morris's avatar
Glenn Morris committed
1074 1075 1076 1077 1078

@item base_buffer
In an indirect buffer, this points to the base buffer.  In an ordinary
buffer, it is null.

1079 1080 1081 1082 1083
@item local_flags
This field contains flags indicating that certain variables are local
in this buffer.  Such variables are declared in the C code using
@code{DEFVAR_PER_BUFFER}, and their buffer-local bindings are stored
in fields in the buffer structure itself.  (Some of these fields are
Glenn Morris's avatar
Glenn Morris committed
1084 1085 1086
described in this table.)

@item modtime
1087 1088 1089 1090
The modification time of the visited file.  It is set when the file is
written or read.  Before writing the buffer into a file, this field is
compared to the modification time of the file to see if the file has
changed on disk.  @xref{Buffer Modification}.
Glenn Morris's avatar
Glenn Morris committed
1091 1092

@item auto_save_modified
1093
The time when the buffer was last auto-saved.
Glenn Morris's avatar
Glenn Morris committed
1094 1095

@item last_window_start
1096 1097
The @code{window-start} position in the buffer as of the last time the
buffer was displayed in a window.
Glenn Morris's avatar
Glenn Morris committed
1098 1099

@item clip_changed
1100 1101
This flag indicates that narrowing has changed in the buffer.
@xref{Narrowing}.
Glenn Morris's avatar
Glenn Morris committed
1102 1103

@item prevent_redisplay_optimizations_p
1104 1105
This flag indicates that redisplay optimizations should not be used to
display this buffer.
Glenn Morris's avatar
Glenn Morris committed
1106

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
@item overlay_center
This field holds the current overlay center position.  @xref{Managing
Overlays}.

@item overlays_before
@itemx overlays_after
These fields hold, respectively, a list of overlays that end at or
before the current overlay center, and a list of overlays that end
after the current overlay center.  @xref{Managing Overlays}.
@code{overlays_before} is sorted in order of decreasing end position,
and @code{overlays_after} is sorted in order of increasing beginning
position.
Glenn Morris's avatar
Glenn Morris committed
1119

1120 1121
@c FIXME? the following are now all Lisp_Object BUFFER_INTERNAL_FIELD (foo).

Glenn Morris's avatar
Glenn Morris committed
1122
@item name
1123 1124
A Lisp string that names the buffer.  It is guaranteed to be unique.
@xref{Buffer Names}.
Glenn Morris's avatar
Glenn Morris committed
1125

1126 1127 1128 1129 1130
@item save_length
The length of the file this buffer is visiting, when last read or
saved.  This and other fields concerned with saving are not kept in
the @code{buffer_text} structure because indirect buffers are never
saved.
Glenn Morris's avatar
Glenn Morris committed
1131 1132

@item directory
1133 1134
The directory for expanding relative file names.  This is the value of
the buffer-local variable @code{default-directory} (@pxref{File Name Expansion}).
Glenn Morris's avatar
Glenn Morris committed
1135

1136 1137 1138 1139
@item filename
The name of the file visited in this buffer, or @code{nil}.  This is
the value of the buffer-local variable @code{buffer-file-name}
(@pxref{Buffer File Name}).
Glenn Morris's avatar
Glenn Morris committed
1140

1141 1142 1143
@item undo_list
@itemx backed_up
@itemx auto_save_file_name
1144
@itemx auto_save_file_format
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
@itemx read_only
@itemx file_format
@itemx file_truename
@itemx invisibility_spec
@itemx display_count
@itemx display_time
These fields store the values of Lisp variables that are automatically
buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
variable names have the additional prefix @code{buffer-} and have
underscores replaced with dashes.  For instance, @code{undo_list}
Glenn Morris's avatar
Glenn Morris committed
1155
stores the value of @code{buffer-undo-list}.
Glenn Morris's avatar
Glenn Morris committed
1156 1157

@item mark
1158 1159
The mark for the buffer.  The mark is a marker, hence it is also
included on the list @code{markers}.  @xref{The Mark}.
Glenn Morris's avatar
Glenn Morris committed
1160 1161

@item local_var_alist
1162 1163 1164 1165
The association list describing the buffer-local variable bindings of
this buffer, not including the built-in buffer-local bindings that
have special slots in the buffer object.  (Those slots are omitted
from this table.)  @xref{Buffer-Local Variables}.
Glenn Morris's avatar
Glenn Morris committed
1166 1167 1168 1169 1170

@item major_mode
Symbol naming the major mode of this buffer, e.g., @code{lisp-mode}.

@item mode_name
1171
Pretty name of the major mode, e.g., @code{"Lisp"}.
Glenn Morris's avatar
Glenn Morris committed
1172 1173

@item keymap
1174 1175 1176 1177 1178 1179 1180 1181
@itemx abbrev_table
@itemx syntax_table
@itemx category_table
@itemx display_table
These fields store the buffer's local keymap (@pxref{Keymaps}), abbrev
table (@pxref{Abbrev Tables}), syntax table (@pxref{Syntax Tables}),
category table (@pxref{Categories}), and display table (@pxref{Display
Tables}).
Glenn Morris's avatar
Glenn Morris committed
1182 1183

@item downcase_table
1184 1185 1186 1187
@itemx upcase_table
@itemx case_canon_table
These fields store the conversion tables for converting text to lower
case, upper case, and for canonicalizing text for case-fold search.
Glenn Morris's avatar
Glenn Morris committed
1188 1189 1190 1191 1192 1193
@xref{Case Tables}.

@item minor_modes
An alist of the minor modes of this buffer.

@item pt_marker
1194 1195 1196 1197 1198
@itemx begv_marker
@itemx zv_marker
These fields are only used in an indirect buffer, or in a buffer that
is the base of an indirect buffer.  Each holds a marker that records
@code{pt}, @code{begv}, and @code{zv} respectively, for this buffer
Glenn Morris's avatar
Glenn Morris committed
1199 1200
when the buffer is not current.

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
@item mode_line_format
@itemx header_line_format
@itemx case_fold_search
@itemx tab_width
@itemx fill_column
@itemx left_margin
@itemx auto_fill_function
@itemx truncate_lines
@itemx word_wrap
@itemx ctl_arrow
1211 1212
@itemx bidi_display_reordering
@itemx bidi_paragraph_direction
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
@itemx selective_display
@itemx selective_display_ellipses
@itemx overwrite_mode
@itemx abbrev_mode
@itemx mark_active
@itemx enable_multibyte_characters
@itemx buffer_file_coding_system
@itemx cache_long_line_scans
@itemx point_before_scroll
@itemx left_fringe_width
@itemx right_fringe_width
@itemx fringes_outside_margins
@itemx scroll_bar_width
@itemx indicate_empty_lines
@itemx indicate_buffer_boundaries
@itemx fringe_indicator_alist
@itemx fringe_cursor_alist
@itemx scroll_up_aggressively
@itemx scroll_down_aggressively
@itemx cursor_type
@itemx cursor_in_non_selected_windows
These fields store the values of Lisp variables that are automatically
buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
variable names have underscores replaced with dashes.  For instance,
@code{mode_line_format} stores the value of @code{mode-line-format}.
Glenn Morris's avatar
Glenn Morris committed
1238 1239 1240 1241 1242 1243 1244

@item last_selected_window
This is the last window that was selected with this buffer in it, or @code{nil}
if that window no longer displays this buffer.
@end table

@node Window Internals
1245
@subsection Window Internals
Glenn Morris's avatar
Glenn Morris committed
1246 1247 1248
@cindex internals, of window
@cindex window internals

1249 1250
  The fields of a window (for a complete list, see the definition of
@code{struct window} in @file{window.h}) include:
Glenn Morris's avatar
Glenn Morris committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

@table @code
@item frame
The frame that this window is on.

@item mini_p
Non-@code{nil} if this window is a minibuffer window.

@item parent
Internally, Emacs arranges windows in a tree; each group of siblings has
a parent window whose area includes all the siblings.  This field points
to a window's parent.

Parent windows do not display buffers, and play little role in display
except to shape their child windows.  Emacs Lisp programs usually have
no access to the parent windows; they operate on the windows at the
leaves of the tree, which actually display buffers.

@item hchild
1270 1271 1272 1273
@itemx vchild
These fields contain the window's leftmost child and its topmost child
respectively.  @code{hchild} is used if the window is subdivided
horizontally by child windows, and @code{vchild} if it is subdivided
1274
vertically.  In a live window, only one of @code{hchild}, @code{vchild},
1275
and @code{buffer} (q.v.@:) is non-@code{nil}.
Glenn Morris's avatar
Glenn Morris committed
1276 1277

@item next
1278 1279
@itemx prev
The next sibling and previous sibling of this window.  @code{next} is
1280 1281
@code{nil} if the window is the right-most or bottom-most in its group;
@code{prev} is @code{nil} if it is the left-most or top-most in its
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
group.

@item left_col
The left-hand edge of the window, measured in columns, relative to the
leftmost column in the frame (column 0).

@item top_line
The top edge of the window, measured in lines, relative to the topmost
line in the frame (line 0).

@item total_cols
@itemx total_lines
The width and height of the window, measured in columns and lines
respectively.  The width includes the scroll bar and fringes, and/or
the separator line on the right of the window (if any).
Glenn Morris's avatar
Glenn Morris committed
1297 1298

@item buffer
1299
The buffer that the window is displaying.
Glenn Morris's avatar
Glenn Morris committed
1300 1301

@item start
1302 1303
A marker pointing to the position in the buffer that is the first
character displayed in the window.
Glenn Morris's avatar
Glenn Morris committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

@item pointm
@cindex window point internals
This is the value of point in the current buffer when this window is
selected; when it is not selected, it retains its previous value.

@item force_start
If this flag is non-@code{nil}, it says that the window has been
scrolled explicitly by the Lisp program.  This affects what the next
redisplay does if point is off the screen: instead of scrolling the
window to show the text around point, it moves point to a location that
is on the screen.

@item frozen_window_start_p
This field is set temporarily to 1 to indicate to redisplay that
@code{start} of this window should not be changed, even if point
gets invisible.

@item start_at_line_beg
Non-@code{nil} means current value of @code{start} was the beginning of a line
when it was chosen.

@item use_time
This is the last time that the window was selected.  The function
@code{get-lru-window} uses this field.

@item sequence_number
A unique number assigned to this window when it was created.

@item last_modified
The @code{modiff} field of the window's buffer, as of the last time
a redisplay completed in this window.

@item last_overlay_modified
The @code{overlay_modiff} field of the window's buffer, as of the last
time a redisplay completed in this window.

@item last_point
The buffer's value of point, as of the last time a redisplay completed
in this window.

@item last_had_star
A non-@code{nil} value means the window's buffer was ``modified'' when the
window was last updated.

@item vertical_scroll_bar
This window's vertical scroll bar.

1352 1353
@item left_margin_cols
@itemx right_margin_cols
1354
The widths of the left and right margins in this window.  A value of
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
@code{nil} means no margin.

@item left_fringe_width
@itemx right_fringe_width
The widths of the left and right fringes in this window.  A value of
@code{nil} or @code{t} means use the values of the frame.

@item fringes_outside_margins
A non-@code{nil} value means the fringes outside the display margins;
othersize they are between the margin and the text.
Glenn Morris's avatar
Glenn Morris committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379

@item window_end_pos
This is computed as @code{z} minus the buffer position of the last glyph
in the current matrix of the window.  The value is only valid if
@code{window_end_valid} is not @code{nil}.

@item window_end_bytepos
The byte position corresponding to @code{window_end_pos}.

@item window_end_vpos
The window-relative vertical position of the line containing
@code{window_end_pos}.

@item window_end_valid
This field is set to a non-@code{nil} value if @code{window_end_pos} is truly
1380
valid.  This is @code{nil} if nontrivial redisplay is pre-empted, since in that