]> code.delx.au - gnu-emacs/blob - doc/lispref/internals.texi
e620da0b4ff8b0add9885b46e6e536c07c691b71
[gnu-emacs] / doc / lispref / internals.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1993, 1998-1999, 2001-2015 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node GNU Emacs Internals
7 @appendix GNU Emacs Internals
8
9 This chapter describes how the runnable Emacs executable is dumped with
10 the preloaded Lisp libraries in it, how storage is allocated, and some
11 internal aspects of GNU Emacs that may be of interest to C programmers.
12
13 @menu
14 * Building Emacs:: How the dumped Emacs is made.
15 * Pure Storage:: Kludge to make preloaded Lisp functions shareable.
16 * Garbage Collection:: Reclaiming space for Lisp objects no longer used.
17 * Stack-allocated Objects:: Temporary conses and strings on C stack.
18 * Memory Usage:: Info about total size of Lisp objects made so far.
19 * C Dialect:: What C variant Emacs is written in.
20 * Writing Emacs Primitives:: Writing C code for Emacs.
21 * Object Internals:: Data formats of buffers, windows, processes.
22 * C Integer Types:: How C integer types are used inside Emacs.
23 @end menu
24
25 @node Building Emacs
26 @section Building Emacs
27 @cindex building Emacs
28 @pindex temacs
29
30 This section explains the steps involved in building the Emacs
31 executable. You don't have to know this material to build and install
32 Emacs, since the makefiles do all these things automatically. This
33 information is pertinent to Emacs developers.
34
35 Compilation of the C source files in the @file{src} directory
36 produces an executable file called @file{temacs}, also called a
37 @dfn{bare impure Emacs}. It contains the Emacs Lisp interpreter and
38 I/O routines, but not the editing commands.
39
40 @cindex @file{loadup.el}
41 The command @w{@command{temacs -l loadup}} would run @file{temacs}
42 and direct it to load @file{loadup.el}. The @code{loadup} library
43 loads additional Lisp libraries, which set up the normal Emacs editing
44 environment. After this step, the Emacs executable is no longer
45 @dfn{bare}.
46
47 @cindex dumping Emacs
48 Because it takes some time to load the standard Lisp files, the
49 @file{temacs} executable usually isn't run directly by users.
50 Instead, as one of the last steps of building Emacs, the command
51 @samp{temacs -batch -l loadup dump} is run. The special @samp{dump}
52 argument causes @command{temacs} to dump out an executable program,
53 called @file{emacs}, which has all the standard Lisp files preloaded.
54 (The @samp{-batch} argument prevents @file{temacs} from trying to
55 initialize any of its data on the terminal, so that the tables of
56 terminal information are empty in the dumped Emacs.)
57
58 @cindex preloaded Lisp files
59 @vindex preloaded-file-list
60 The dumped @file{emacs} executable (also called a @dfn{pure} Emacs)
61 is the one which is installed. The variable
62 @code{preloaded-file-list} stores a list of the Lisp files preloaded
63 into the dumped Emacs. If you port Emacs to a new operating system,
64 and are not able to implement dumping, then Emacs must load
65 @file{loadup.el} each time it starts.
66
67 @cindex @file{site-load.el}
68 You can specify additional files to preload by writing a library named
69 @file{site-load.el} that loads them. You may need to rebuild Emacs
70 with an added definition
71
72 @example
73 #define SITELOAD_PURESIZE_EXTRA @var{n}
74 @end example
75
76 @noindent
77 to make @var{n} added bytes of pure space to hold the additional files;
78 see @file{src/puresize.h}.
79 (Try adding increments of 20000 until it is big enough.) However, the
80 advantage of preloading additional files decreases as machines get
81 faster. On modern machines, it is usually not advisable.
82
83 After @file{loadup.el} reads @file{site-load.el}, it finds the
84 documentation strings for primitive and preloaded functions (and
85 variables) in the file @file{etc/DOC} where they are stored, by
86 calling @code{Snarf-documentation} (@pxref{Definition of
87 Snarf-documentation,, Accessing Documentation}).
88
89 @cindex @file{site-init.el}
90 @cindex preloading additional functions and variables
91 You can specify other Lisp expressions to execute just before dumping
92 by putting them in a library named @file{site-init.el}. This file is
93 executed after the documentation strings are found.
94
95 If you want to preload function or variable definitions, there are
96 three ways you can do this and make their documentation strings
97 accessible when you subsequently run Emacs:
98
99 @itemize @bullet
100 @item
101 Arrange to scan these files when producing the @file{etc/DOC} file,
102 and load them with @file{site-load.el}.
103
104 @item
105 Load the files with @file{site-init.el}, then copy the files into the
106 installation directory for Lisp files when you install Emacs.
107
108 @item
109 Specify a @code{nil} value for @code{byte-compile-dynamic-docstrings}
110 as a local variable in each of these files, and load them with either
111 @file{site-load.el} or @file{site-init.el}. (This method has the
112 drawback that the documentation strings take up space in Emacs all the
113 time.)
114 @end itemize
115
116 @cindex change @code{load-path} at configure time
117 @cindex @option{--enable-locallisppath} option to @command{configure}
118 It is not advisable to put anything in @file{site-load.el} or
119 @file{site-init.el} that would alter any of the features that users
120 expect in an ordinary unmodified Emacs. If you feel you must override
121 normal features for your site, do it with @file{default.el}, so that
122 users can override your changes if they wish. @xref{Startup Summary}.
123 Note that if either @file{site-load.el} or @file{site-init.el} changes
124 @code{load-path}, the changes will be lost after dumping.
125 @xref{Library Search}. To make a permanent change to
126 @code{load-path}, use the @option{--enable-locallisppath} option
127 of @command{configure}.
128
129 In a package that can be preloaded, it is sometimes necessary (or
130 useful) to delay certain evaluations until Emacs subsequently starts
131 up. The vast majority of such cases relate to the values of
132 customizable variables. For example, @code{tutorial-directory} is a
133 variable defined in @file{startup.el}, which is preloaded. The default
134 value is set based on @code{data-directory}. The variable needs to
135 access the value of @code{data-directory} when Emacs starts, not when
136 it is dumped, because the Emacs executable has probably been installed
137 in a different location since it was dumped.
138
139 @defun custom-initialize-delay symbol value
140 This function delays the initialization of @var{symbol} to the next
141 Emacs start. You normally use this function by specifying it as the
142 @code{:initialize} property of a customizable variable. (The argument
143 @var{value} is unused, and is provided only for compatibility with the
144 form Custom expects.)
145 @end defun
146
147 In the unlikely event that you need a more general functionality than
148 @code{custom-initialize-delay} provides, you can use
149 @code{before-init-hook} (@pxref{Startup Summary}).
150
151 @defun dump-emacs to-file from-file
152 @cindex unexec
153 This function dumps the current state of Emacs into an executable file
154 @var{to-file}. It takes symbols from @var{from-file} (this is normally
155 the executable file @file{temacs}).
156
157 If you want to use this function in an Emacs that was already dumped,
158 you must run Emacs with @samp{-batch}.
159 @end defun
160
161 @node Pure Storage
162 @section Pure Storage
163 @cindex pure storage
164
165 Emacs Lisp uses two kinds of storage for user-created Lisp objects:
166 @dfn{normal storage} and @dfn{pure storage}. Normal storage is where
167 all the new data created during an Emacs session are kept
168 (@pxref{Garbage Collection}). Pure storage is used for certain data
169 in the preloaded standard Lisp files---data that should never change
170 during actual use of Emacs.
171
172 Pure storage is allocated only while @command{temacs} is loading the
173 standard preloaded Lisp libraries. In the file @file{emacs}, it is
174 marked as read-only (on operating systems that permit this), so that
175 the memory space can be shared by all the Emacs jobs running on the
176 machine at once. Pure storage is not expandable; a fixed amount is
177 allocated when Emacs is compiled, and if that is not sufficient for
178 the preloaded libraries, @file{temacs} allocates dynamic memory for
179 the part that didn't fit. The resulting image will work, but garbage
180 collection (@pxref{Garbage Collection}) is disabled in this situation,
181 causing a memory leak. Such an overflow normally won't happen unless
182 you try to preload additional libraries or add features to the
183 standard ones. Emacs will display a warning about the overflow when
184 it starts. If this happens, you should increase the compilation
185 parameter @code{SYSTEM_PURESIZE_EXTRA} in the file
186 @file{src/puresize.h} and rebuild Emacs.
187
188 @defun purecopy object
189 This function makes a copy in pure storage of @var{object}, and returns
190 it. It copies a string by simply making a new string with the same
191 characters, but without text properties, in pure storage. It
192 recursively copies the contents of vectors and cons cells. It does
193 not make copies of other objects such as symbols, but just returns
194 them unchanged. It signals an error if asked to copy markers.
195
196 This function is a no-op except while Emacs is being built and dumped;
197 it is usually called only in preloaded Lisp files.
198 @end defun
199
200 @defvar pure-bytes-used
201 The value of this variable is the number of bytes of pure storage
202 allocated so far. Typically, in a dumped Emacs, this number is very
203 close to the total amount of pure storage available---if it were not,
204 we would preallocate less.
205 @end defvar
206
207 @defvar purify-flag
208 This variable determines whether @code{defun} should make a copy of the
209 function definition in pure storage. If it is non-@code{nil}, then the
210 function definition is copied into pure storage.
211
212 This flag is @code{t} while loading all of the basic functions for
213 building Emacs initially (allowing those functions to be shareable and
214 non-collectible). Dumping Emacs as an executable always writes
215 @code{nil} in this variable, regardless of the value it actually has
216 before and after dumping.
217
218 You should not change this flag in a running Emacs.
219 @end defvar
220
221 @node Garbage Collection
222 @section Garbage Collection
223
224 @cindex memory allocation
225 When a program creates a list or the user defines a new function
226 (such as by loading a library), that data is placed in normal storage.
227 If normal storage runs low, then Emacs asks the operating system to
228 allocate more memory. Different types of Lisp objects, such as
229 symbols, cons cells, small vectors, markers, etc., are segregated in
230 distinct blocks in memory. (Large vectors, long strings, buffers and
231 certain other editing types, which are fairly large, are allocated in
232 individual blocks, one per object; small strings are packed into blocks
233 of 8k bytes, and small vectors are packed into blocks of 4k bytes).
234
235 @cindex vector-like objects, storage
236 @cindex storage of vector-like Lisp objects
237 Beyond the basic vector, a lot of objects like window, buffer, and
238 frame are managed as if they were vectors. The corresponding C data
239 structures include the @code{struct vectorlike_header} field whose
240 @code{size} member contains the subtype enumerated by @code{enum pvec_type}
241 and an information about how many @code{Lisp_Object} fields this structure
242 contains and what the size of the rest data is. This information is
243 needed to calculate the memory footprint of an object, and used
244 by the vector allocation code while iterating over the vector blocks.
245
246 @cindex garbage collection
247 It is quite common to use some storage for a while, then release it
248 by (for example) killing a buffer or deleting the last pointer to an
249 object. Emacs provides a @dfn{garbage collector} to reclaim this
250 abandoned storage. The garbage collector operates by finding and
251 marking all Lisp objects that are still accessible to Lisp programs.
252 To begin with, it assumes all the symbols, their values and associated
253 function definitions, and any data presently on the stack, are
254 accessible. Any objects that can be reached indirectly through other
255 accessible objects are also accessible.
256
257 When marking is finished, all objects still unmarked are garbage. No
258 matter what the Lisp program or the user does, it is impossible to refer
259 to them, since there is no longer a way to reach them. Their space
260 might as well be reused, since no one will miss them. The second
261 (sweep) phase of the garbage collector arranges to reuse them.
262
263 @c ??? Maybe add something describing weak hash tables here?
264
265 @cindex free list
266 The sweep phase puts unused cons cells onto a @dfn{free list}
267 for future allocation; likewise for symbols and markers. It compacts
268 the accessible strings so they occupy fewer 8k blocks; then it frees the
269 other 8k blocks. Unreachable vectors from vector blocks are coalesced
270 to create largest possible free areas; if a free area spans a complete
271 4k block, that block is freed. Otherwise, the free area is recorded
272 in a free list array, where each entry corresponds to a free list
273 of areas of the same size. Large vectors, buffers, and other large
274 objects are allocated and freed individually.
275
276 @cindex CL note---allocate more storage
277 @quotation
278 @b{Common Lisp note:} Unlike other Lisps, GNU Emacs Lisp does not
279 call the garbage collector when the free list is empty. Instead, it
280 simply requests the operating system to allocate more storage, and
281 processing continues until @code{gc-cons-threshold} bytes have been
282 used.
283
284 This means that you can make sure that the garbage collector will not
285 run during a certain portion of a Lisp program by calling the garbage
286 collector explicitly just before it (provided that portion of the
287 program does not use so much space as to force a second garbage
288 collection).
289 @end quotation
290
291 @deffn Command garbage-collect
292 This command runs a garbage collection, and returns information on
293 the amount of space in use. (Garbage collection can also occur
294 spontaneously if you use more than @code{gc-cons-threshold} bytes of
295 Lisp data since the previous garbage collection.)
296
297 @code{garbage-collect} returns a list with information on amount of space in
298 use, where each entry has the form @samp{(@var{name} @var{size} @var{used})}
299 or @samp{(@var{name} @var{size} @var{used} @var{free})}. In the entry,
300 @var{name} is a symbol describing the kind of objects this entry represents,
301 @var{size} is the number of bytes used by each one, @var{used} is the number
302 of those objects that were found live in the heap, and optional @var{free} is
303 the number of those objects that are not live but that Emacs keeps around for
304 future allocations. So an overall result is:
305
306 @example
307 ((@code{conses} @var{cons-size} @var{used-conses} @var{free-conses})
308 (@code{symbols} @var{symbol-size} @var{used-symbols} @var{free-symbols})
309 (@code{miscs} @var{misc-size} @var{used-miscs} @var{free-miscs})
310 (@code{strings} @var{string-size} @var{used-strings} @var{free-strings})
311 (@code{string-bytes} @var{byte-size} @var{used-bytes})
312 (@code{vectors} @var{vector-size} @var{used-vectors})
313 (@code{vector-slots} @var{slot-size} @var{used-slots} @var{free-slots})
314 (@code{floats} @var{float-size} @var{used-floats} @var{free-floats})
315 (@code{intervals} @var{interval-size} @var{used-intervals} @var{free-intervals})
316 (@code{buffers} @var{buffer-size} @var{used-buffers})
317 (@code{heap} @var{unit-size} @var{total-size} @var{free-size}))
318 @end example
319
320 Here is an example:
321
322 @example
323 (garbage-collect)
324 @result{} ((conses 16 49126 8058) (symbols 48 14607 0)
325 (miscs 40 34 56) (strings 32 2942 2607)
326 (string-bytes 1 78607) (vectors 16 7247)
327 (vector-slots 8 341609 29474) (floats 8 71 102)
328 (intervals 56 27 26) (buffers 944 8)
329 (heap 1024 11715 2678))
330 @end example
331
332 Below is a table explaining each element. Note that last @code{heap} entry
333 is optional and present only if an underlying @code{malloc} implementation
334 provides @code{mallinfo} function.
335
336 @table @var
337 @item cons-size
338 Internal size of a cons cell, i.e., @code{sizeof (struct Lisp_Cons)}.
339
340 @item used-conses
341 The number of cons cells in use.
342
343 @item free-conses
344 The number of cons cells for which space has been obtained from
345 the operating system, but that are not currently being used.
346
347 @item symbol-size
348 Internal size of a symbol, i.e., @code{sizeof (struct Lisp_Symbol)}.
349
350 @item used-symbols
351 The number of symbols in use.
352
353 @item free-symbols
354 The number of symbols for which space has been obtained from
355 the operating system, but that are not currently being used.
356
357 @item misc-size
358 Internal size of a miscellaneous entity, i.e.,
359 @code{sizeof (union Lisp_Misc)}, which is a size of the
360 largest type enumerated in @code{enum Lisp_Misc_Type}.
361
362 @item used-miscs
363 The number of miscellaneous objects in use. These include markers
364 and overlays, plus certain objects not visible to users.
365
366 @item free-miscs
367 The number of miscellaneous objects for which space has been obtained
368 from the operating system, but that are not currently being used.
369
370 @item string-size
371 Internal size of a string header, i.e., @code{sizeof (struct Lisp_String)}.
372
373 @item used-strings
374 The number of string headers in use.
375
376 @item free-strings
377 The number of string headers for which space has been obtained
378 from the operating system, but that are not currently being used.
379
380 @item byte-size
381 This is used for convenience and equals to @code{sizeof (char)}.
382
383 @item used-bytes
384 The total size of all string data in bytes.
385
386 @item vector-size
387 Internal size of a vector header, i.e., @code{sizeof (struct Lisp_Vector)}.
388
389 @item used-vectors
390 The number of vector headers allocated from the vector blocks.
391
392 @item slot-size
393 Internal size of a vector slot, always equal to @code{sizeof (Lisp_Object)}.
394
395 @item used-slots
396 The number of slots in all used vectors.
397
398 @item free-slots
399 The number of free slots in all vector blocks.
400
401 @item float-size
402 Internal size of a float object, i.e., @code{sizeof (struct Lisp_Float)}.
403 (Do not confuse it with the native platform @code{float} or @code{double}.)
404
405 @item used-floats
406 The number of floats in use.
407
408 @item free-floats
409 The number of floats for which space has been obtained from
410 the operating system, but that are not currently being used.
411
412 @item interval-size
413 Internal size of an interval object, i.e., @code{sizeof (struct interval)}.
414
415 @item used-intervals
416 The number of intervals in use.
417
418 @item free-intervals
419 The number of intervals for which space has been obtained from
420 the operating system, but that are not currently being used.
421
422 @item buffer-size
423 Internal size of a buffer, i.e., @code{sizeof (struct buffer)}.
424 (Do not confuse with the value returned by @code{buffer-size} function.)
425
426 @item used-buffers
427 The number of buffer objects in use. This includes killed buffers
428 invisible to users, i.e., all buffers in @code{all_buffers} list.
429
430 @item unit-size
431 The unit of heap space measurement, always equal to 1024 bytes.
432
433 @item total-size
434 Total heap size, in @var{unit-size} units.
435
436 @item free-size
437 Heap space which is not currently used, in @var{unit-size} units.
438 @end table
439
440 If there was overflow in pure space (@pxref{Pure Storage}),
441 @code{garbage-collect} returns @code{nil}, because a real garbage
442 collection cannot be done.
443 @end deffn
444
445 @defopt garbage-collection-messages
446 If this variable is non-@code{nil}, Emacs displays a message at the
447 beginning and end of garbage collection. The default value is
448 @code{nil}.
449 @end defopt
450
451 @defvar post-gc-hook
452 This is a normal hook that is run at the end of garbage collection.
453 Garbage collection is inhibited while the hook functions run, so be
454 careful writing them.
455 @end defvar
456
457 @defopt gc-cons-threshold
458 The value of this variable is the number of bytes of storage that must
459 be allocated for Lisp objects after one garbage collection in order to
460 trigger another garbage collection. You can use the result returned by
461 @code{garbage-collect} to get an information about size of the particular
462 object type; space allocated to the contents of buffers does not count.
463 Note that the subsequent garbage collection does not happen immediately
464 when the threshold is exhausted, but only the next time the Lisp interpreter
465 is called.
466
467 The initial threshold value is @code{GC_DEFAULT_THRESHOLD}, defined in
468 @file{alloc.c}. Since it's defined in @code{word_size} units, the value
469 is 400,000 for the default 32-bit configuration and 800,000 for the 64-bit
470 one. If you specify a larger value, garbage collection will happen less
471 often. This reduces the amount of time spent garbage collecting, but
472 increases total memory use. You may want to do this when running a program
473 that creates lots of Lisp data.
474
475 You can make collections more frequent by specifying a smaller value, down
476 to 1/10th of @code{GC_DEFAULT_THRESHOLD}. A value less than this minimum
477 will remain in effect only until the subsequent garbage collection, at which
478 time @code{garbage-collect} will set the threshold back to the minimum.
479 @end defopt
480
481 @defopt gc-cons-percentage
482 The value of this variable specifies the amount of consing before a
483 garbage collection occurs, as a fraction of the current heap size.
484 This criterion and @code{gc-cons-threshold} apply in parallel, and
485 garbage collection occurs only when both criteria are satisfied.
486
487 As the heap size increases, the time to perform a garbage collection
488 increases. Thus, it can be desirable to do them less frequently in
489 proportion.
490 @end defopt
491
492 The value returned by @code{garbage-collect} describes the amount of
493 memory used by Lisp data, broken down by data type. By contrast, the
494 function @code{memory-limit} provides information on the total amount of
495 memory Emacs is currently using.
496
497 @defun memory-limit
498 This function returns the address of the last byte Emacs has allocated,
499 divided by 1024. We divide the value by 1024 to make sure it fits in a
500 Lisp integer.
501
502 You can use this to get a general idea of how your actions affect the
503 memory usage.
504 @end defun
505
506 @defvar memory-full
507 This variable is @code{t} if Emacs is nearly out of memory for Lisp
508 objects, and @code{nil} otherwise.
509 @end defvar
510
511 @defun memory-use-counts
512 This returns a list of numbers that count the number of objects
513 created in this Emacs session. Each of these counters increments for
514 a certain kind of object. See the documentation string for details.
515 @end defun
516
517 @defun memory-info
518 This functions returns an amount of total system memory and how much
519 of it is free. On an unsupported system, the value may be @code{nil}.
520 @end defun
521
522 @defvar gcs-done
523 This variable contains the total number of garbage collections
524 done so far in this Emacs session.
525 @end defvar
526
527 @defvar gc-elapsed
528 This variable contains the total number of seconds of elapsed time
529 during garbage collection so far in this Emacs session, as a
530 floating-point number.
531 @end defvar
532
533 @node Stack-allocated Objects
534 @section Stack-allocated Objects
535
536 @cindex stack allocated Lisp objects
537 @cindex Lisp objects, stack-allocated
538 The garbage collector described above is used to manage data visible
539 from Lisp programs, as well as most of the data internally used by the
540 Lisp interpreter. Sometimes it may be useful to allocate temporary
541 internal objects using the C stack of the interpreter. This can help
542 performance, as stack allocation is typically faster than using heap
543 memory to allocate and the garbage collector to free. The downside is
544 that using such objects after they are freed results in undefined
545 behavior, so uses should be well thought out and carefully debugged by
546 using the @code{GC_CHECK_MARKED_OBJECTS} feature (see
547 @file{src/alloc.c}). In particular, stack-allocated objects should
548 never be made visible to user Lisp code.
549
550 Currently, cons cells and strings can be allocated this way. This
551 is implemented by C macros like @code{AUTO_CONS} and
552 @code{AUTO_STRING} that define a named @code{Lisp_Object} with block
553 lifetime. These objects are not freed by the garbage collector;
554 instead, they have automatic storage duration, i.e., they are
555 allocated like local variables and are automatically freed at the end
556 of execution of the C block that defined the object.
557
558 For performance reasons, stack-allocated strings are limited to
559 @acronym{ASCII} characters, and many of these strings are immutable,
560 i.e., calling @code{ASET} on them produces undefined behavior.
561
562 @node Memory Usage
563 @section Memory Usage
564 @cindex memory usage
565
566 These functions and variables give information about the total amount
567 of memory allocation that Emacs has done, broken down by data type.
568 Note the difference between these and the values returned by
569 @code{garbage-collect}; those count objects that currently exist, but
570 these count the number or size of all allocations, including those for
571 objects that have since been freed.
572
573 @defvar cons-cells-consed
574 The total number of cons cells that have been allocated so far
575 in this Emacs session.
576 @end defvar
577
578 @defvar floats-consed
579 The total number of floats that have been allocated so far
580 in this Emacs session.
581 @end defvar
582
583 @defvar vector-cells-consed
584 The total number of vector cells that have been allocated so far
585 in this Emacs session.
586 @end defvar
587
588 @defvar symbols-consed
589 The total number of symbols that have been allocated so far
590 in this Emacs session.
591 @end defvar
592
593 @defvar string-chars-consed
594 The total number of string characters that have been allocated so far
595 in this session.
596 @end defvar
597
598 @defvar misc-objects-consed
599 The total number of miscellaneous objects that have been allocated so
600 far in this session. These include markers and overlays, plus
601 certain objects not visible to users.
602 @end defvar
603
604 @defvar intervals-consed
605 The total number of intervals that have been allocated so far
606 in this Emacs session.
607 @end defvar
608
609 @defvar strings-consed
610 The total number of strings that have been allocated so far in this
611 Emacs session.
612 @end defvar
613
614 @node C Dialect
615 @section C Dialect
616 @cindex C programming language
617
618 The C part of Emacs is portable to C99 or later: C11-specific features such
619 as @samp{<stdalign.h>} and @samp{_Noreturn} are not used without a check,
620 typically at configuration time, and the Emacs build procedure
621 provides a substitute implementation if necessary. Some C11 features,
622 such as anonymous structures and unions, are too difficult to emulate,
623 so they are avoided entirely.
624
625 At some point in the future the base C dialect will no doubt change to C11.
626
627 @node Writing Emacs Primitives
628 @section Writing Emacs Primitives
629 @cindex primitive function internals
630 @cindex writing Emacs primitives
631
632 Lisp primitives are Lisp functions implemented in C@. The details of
633 interfacing the C function so that Lisp can call it are handled by a few
634 C macros. The only way to really understand how to write new C code is
635 to read the source, but we can explain some things here.
636
637 An example of a special form is the definition of @code{or}, from
638 @file{eval.c}. (An ordinary function would have the same general
639 appearance.)
640
641 @smallexample
642 @group
643 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
644 doc: /* Eval args until one of them yields non-nil, then return
645 that value.
646 The remaining args are not evalled at all.
647 If all args return nil, return nil.
648 @end group
649 @group
650 usage: (or CONDITIONS...) */)
651 (Lisp_Object args)
652 @{
653 Lisp_Object val = Qnil;
654 @end group
655
656 @group
657 while (CONSP (args))
658 @{
659 val = eval_sub (XCAR (args));
660 if (!NILP (val))
661 break;
662 args = XCDR (args);
663 QUIT;
664 @}
665 @end group
666
667 @group
668 return val;
669 @}
670 @end group
671 @end smallexample
672
673 @cindex @code{DEFUN}, C macro to define Lisp primitives
674 Let's start with a precise explanation of the arguments to the
675 @code{DEFUN} macro. Here is a template for them:
676
677 @example
678 DEFUN (@var{lname}, @var{fname}, @var{sname}, @var{min}, @var{max}, @var{interactive}, @var{doc})
679 @end example
680
681 @table @var
682 @item lname
683 This is the name of the Lisp symbol to define as the function name; in
684 the example above, it is @code{or}.
685
686 @item fname
687 This is the C function name for this function. This is the name that
688 is used in C code for calling the function. The name is, by
689 convention, @samp{F} prepended to the Lisp name, with all dashes
690 (@samp{-}) in the Lisp name changed to underscores. Thus, to call
691 this function from C code, call @code{For}.
692
693 @item sname
694 This is a C variable name to use for a structure that holds the data for
695 the subr object that represents the function in Lisp. This structure
696 conveys the Lisp symbol name to the initialization routine that will
697 create the symbol and store the subr object as its definition. By
698 convention, this name is always @var{fname} with @samp{F} replaced with
699 @samp{S}.
700
701 @item min
702 This is the minimum number of arguments that the function requires. The
703 function @code{or} allows a minimum of zero arguments.
704
705 @item max
706 This is the maximum number of arguments that the function accepts, if
707 there is a fixed maximum. Alternatively, it can be @code{UNEVALLED},
708 indicating a special form that receives unevaluated arguments, or
709 @code{MANY}, indicating an unlimited number of evaluated arguments (the
710 equivalent of @code{&rest}). Both @code{UNEVALLED} and @code{MANY} are
711 macros. If @var{max} is a number, it must be more than @var{min} but
712 less than 8.
713
714 @cindex interactive specification in primitives
715 @item interactive
716 This is an interactive specification, a string such as might be used
717 as the argument of @code{interactive} in a Lisp function. In the case
718 of @code{or}, it is 0 (a null pointer), indicating that @code{or}
719 cannot be called interactively. A value of @code{""} indicates a
720 function that should receive no arguments when called interactively.
721 If the value begins with a @samp{"(}, the string is evaluated as a
722 Lisp form. For example:
723
724 @example
725 @group
726 DEFUN ("foo", Ffoo, Sfoo, 0, UNEVALLED,
727 "(list (read-char-by-name \"Insert character: \")\
728 (prefix-numeric-value current-prefix-arg)\
729 t))",
730 doc: /* @dots{} /*)
731 @end group
732 @end example
733
734 @item doc
735 This is the documentation string. It uses C comment syntax rather
736 than C string syntax because comment syntax requires nothing special
737 to include multiple lines. The @samp{doc:} identifies the comment
738 that follows as the documentation string. The @samp{/*} and @samp{*/}
739 delimiters that begin and end the comment are not part of the
740 documentation string.
741
742 If the last line of the documentation string begins with the keyword
743 @samp{usage:}, the rest of the line is treated as the argument list
744 for documentation purposes. This way, you can use different argument
745 names in the documentation string from the ones used in the C code.
746 @samp{usage:} is required if the function has an unlimited number of
747 arguments.
748
749 All the usual rules for documentation strings in Lisp code
750 (@pxref{Documentation Tips}) apply to C code documentation strings
751 too.
752 @end table
753
754 After the call to the @code{DEFUN} macro, you must write the
755 argument list for the C function, including the types for the
756 arguments. If the primitive accepts a fixed maximum number of Lisp
757 arguments, there must be one C argument for each Lisp argument, and
758 each argument must be of type @code{Lisp_Object}. (Various macros and
759 functions for creating values of type @code{Lisp_Object} are declared
760 in the file @file{lisp.h}.) If the primitive has no upper limit on
761 the number of Lisp arguments, it must have exactly two C arguments:
762 the first is the number of Lisp arguments, and the second is the
763 address of a block containing their values. These have types
764 @code{int} and @w{@code{Lisp_Object *}} respectively. Since
765 @code{Lisp_Object} can hold any Lisp object of any data type, you
766 can determine the actual data type only at run time; so if you want
767 a primitive to accept only a certain type of argument, you must check
768 the type explicitly using a suitable predicate (@pxref{Type Predicates}).
769 @cindex type checking internals
770
771 @cindex garbage collection protection
772 @cindex protect C variables from garbage collection
773 Within the function @code{For} itself, the local variable
774 @code{args} refers to objects controlled by Emacs's stack-marking
775 garbage collector. Although the garbage collector does not reclaim
776 objects reachable from C @code{Lisp_Object} stack variables, it may
777 move non-object components of an object, such as string contents; so
778 functions that access non-object components must take care to refetch
779 their addresses after performing Lisp evaluation. Lisp evaluation can
780 occur via calls to @code{eval_sub} or @code{Feval}, either directly or
781 indirectly.
782
783 @cindex @code{QUIT}, use in Lisp primitives
784 Note the call to the @code{QUIT} macro inside the loop: this macro
785 checks whether the user pressed @kbd{C-g}, and if so, aborts the
786 processing. You should do that in any loop that can potentially
787 require a large number of iterations; in this case, the list of
788 arguments could be very long. This increases Emacs responsiveness and
789 improves user experience.
790
791 You must not use C initializers for static or global variables unless
792 the variables are never written once Emacs is dumped. These variables
793 with initializers are allocated in an area of memory that becomes
794 read-only (on certain operating systems) as a result of dumping Emacs.
795 @xref{Pure Storage}.
796
797 @cindex @code{defsubr}, Lisp symbol for a primitive
798 Defining the C function is not enough to make a Lisp primitive
799 available; you must also create the Lisp symbol for the primitive and
800 store a suitable subr object in its function cell. The code looks like
801 this:
802
803 @example
804 defsubr (&@var{sname});
805 @end example
806
807 @noindent
808 Here @var{sname} is the name you used as the third argument to @code{DEFUN}.
809
810 If you add a new primitive to a file that already has Lisp primitives
811 defined in it, find the function (near the end of the file) named
812 @code{syms_of_@var{something}}, and add the call to @code{defsubr}
813 there. If the file doesn't have this function, or if you create a new
814 file, add to it a @code{syms_of_@var{filename}} (e.g.,
815 @code{syms_of_myfile}). Then find the spot in @file{emacs.c} where all
816 of these functions are called, and add a call to
817 @code{syms_of_@var{filename}} there.
818
819 @anchor{Defining Lisp variables in C}
820 @vindex byte-boolean-vars
821 @cindex defining Lisp variables in C
822 @cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL}
823 The function @code{syms_of_@var{filename}} is also the place to define
824 any C variables that are to be visible as Lisp variables.
825 @code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible
826 in Lisp. @code{DEFVAR_INT} makes a C variable of type @code{int}
827 visible in Lisp with a value that is always an integer.
828 @code{DEFVAR_BOOL} makes a C variable of type @code{int} visible in Lisp
829 with a value that is either @code{t} or @code{nil}. Note that variables
830 defined with @code{DEFVAR_BOOL} are automatically added to the list
831 @code{byte-boolean-vars} used by the byte compiler.
832
833 @cindex defining customization variables in C
834 If you want to make a Lisp variables that is defined in C behave
835 like one declared with @code{defcustom}, add an appropriate entry to
836 @file{cus-start.el}.
837
838 @cindex @code{staticpro}, protection from GC
839 If you define a file-scope C variable of type @code{Lisp_Object},
840 you must protect it from garbage-collection by calling @code{staticpro}
841 in @code{syms_of_@var{filename}}, like this:
842
843 @example
844 staticpro (&@var{variable});
845 @end example
846
847 Here is another example function, with more complicated arguments.
848 This comes from the code in @file{window.c}, and it demonstrates the use
849 of macros and functions to manipulate Lisp objects.
850
851 @smallexample
852 @group
853 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
854 Scoordinates_in_window_p, 2, 2, 0,
855 doc: /* Return non-nil if COORDINATES are in WINDOW.
856 ...
857 @end group
858 @group
859 or `right-margin' is returned. */)
860 (register Lisp_Object coordinates, Lisp_Object window)
861 @{
862 struct window *w;
863 struct frame *f;
864 int x, y;
865 Lisp_Object lx, ly;
866 @end group
867
868 @group
869 CHECK_LIVE_WINDOW (window);
870 w = XWINDOW (window);
871 f = XFRAME (w->frame);
872 CHECK_CONS (coordinates);
873 lx = Fcar (coordinates);
874 ly = Fcdr (coordinates);
875 CHECK_NUMBER_OR_FLOAT (lx);
876 CHECK_NUMBER_OR_FLOAT (ly);
877 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH(f);
878 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH(f);
879 @end group
880
881 @group
882 switch (coordinates_in_window (w, x, y))
883 @{
884 case ON_NOTHING: /* NOT in window at all. */
885 return Qnil;
886 @end group
887
888 ...
889
890 @group
891 case ON_MODE_LINE: /* In mode line of window. */
892 return Qmode_line;
893 @end group
894
895 ...
896
897 @group
898 case ON_SCROLL_BAR: /* On scroll-bar of window. */
899 /* Historically we are supposed to return nil in this case. */
900 return Qnil;
901 @end group
902
903 @group
904 default:
905 abort ();
906 @}
907 @}
908 @end group
909 @end smallexample
910
911 Note that C code cannot call functions by name unless they are defined
912 in C@. The way to call a function written in Lisp is to use
913 @code{Ffuncall}, which embodies the Lisp function @code{funcall}. Since
914 the Lisp function @code{funcall} accepts an unlimited number of
915 arguments, in C it takes two: the number of Lisp-level arguments, and a
916 one-dimensional array containing their values. The first Lisp-level
917 argument is the Lisp function to call, and the rest are the arguments to
918 pass to it.
919
920 The C functions @code{call0}, @code{call1}, @code{call2}, and so on,
921 provide handy ways to call a Lisp function conveniently with a fixed
922 number of arguments. They work by calling @code{Ffuncall}.
923
924 @file{eval.c} is a very good file to look through for examples;
925 @file{lisp.h} contains the definitions for some important macros and
926 functions.
927
928 If you define a function which is side-effect free, update the code
929 in @file{byte-opt.el} that binds @code{side-effect-free-fns} and
930 @code{side-effect-and-error-free-fns} so that the compiler optimizer
931 knows about it.
932
933 @node Object Internals
934 @section Object Internals
935 @cindex object internals
936
937 Emacs Lisp provides a rich set of the data types. Some of them, like cons
938 cells, integers and strings, are common to nearly all Lisp dialects. Some
939 others, like markers and buffers, are quite special and needed to provide
940 the basic support to write editor commands in Lisp. To implement such
941 a variety of object types and provide an efficient way to pass objects between
942 the subsystems of an interpreter, there is a set of C data structures and
943 a special type to represent the pointers to all of them, which is known as
944 @dfn{tagged pointer}.
945
946 In C, the tagged pointer is an object of type @code{Lisp_Object}. Any
947 initialized variable of such a type always holds the value of one of the
948 following basic data types: integer, symbol, string, cons cell, float,
949 vectorlike or miscellaneous object. Each of these data types has the
950 corresponding tag value. All tags are enumerated by @code{enum Lisp_Type}
951 and placed into a 3-bit bitfield of the @code{Lisp_Object}. The rest of the
952 bits is the value itself. Integers are immediate, i.e., directly
953 represented by those @dfn{value bits}, and all other objects are represented
954 by the C pointers to a corresponding object allocated from the heap. Width
955 of the @code{Lisp_Object} is platform- and configuration-dependent: usually
956 it's equal to the width of an underlying platform pointer (i.e., 32-bit on
957 a 32-bit machine and 64-bit on a 64-bit one), but also there is a special
958 configuration where @code{Lisp_Object} is 64-bit but all pointers are 32-bit.
959 The latter trick was designed to overcome the limited range of values for
960 Lisp integers on a 32-bit system by using 64-bit @code{long long} type for
961 @code{Lisp_Object}.
962
963 The following C data structures are defined in @file{lisp.h} to represent
964 the basic data types beyond integers:
965
966 @table @code
967 @item struct Lisp_Cons
968 Cons cell, an object used to construct lists.
969
970 @item struct Lisp_String
971 String, the basic object to represent a sequence of characters.
972
973 @item struct Lisp_Vector
974 Array, a fixed-size set of Lisp objects which may be accessed by an index.
975
976 @item struct Lisp_Symbol
977 Symbol, the unique-named entity commonly used as an identifier.
978
979 @item struct Lisp_Float
980 Floating-point value.
981
982 @item union Lisp_Misc
983 Miscellaneous kinds of objects which don't fit into any of the above.
984 @end table
985
986 These types are the first-class citizens of an internal type system.
987 Since the tag space is limited, all other types are the subtypes of either
988 @code{Lisp_Vectorlike} or @code{Lisp_Misc}. Vector subtypes are enumerated
989 by @code{enum pvec_type}, and nearly all complex objects like windows, buffers,
990 frames, and processes fall into this category. The rest of special types,
991 including markers and overlays, are enumerated by @code{enum Lisp_Misc_Type}
992 and form the set of subtypes of @code{Lisp_Misc}.
993
994 Below there is a description of a few subtypes of @code{Lisp_Vectorlike}.
995 Buffer object represents the text to display and edit. Window is the part
996 of display structure which shows the buffer or used as a container to
997 recursively place other windows on the same frame. (Do not confuse Emacs Lisp
998 window object with the window as an entity managed by the user interface
999 system like X; in Emacs terminology, the latter is called frame.) Finally,
1000 process object is used to manage the subprocesses.
1001
1002 @menu
1003 * Buffer Internals:: Components of a buffer structure.
1004 * Window Internals:: Components of a window structure.
1005 * Process Internals:: Components of a process structure.
1006 @end menu
1007
1008 @node Buffer Internals
1009 @subsection Buffer Internals
1010 @cindex internals, of buffer
1011 @cindex buffer internals
1012
1013 Two structures (see @file{buffer.h}) are used to represent buffers
1014 in C@. The @code{buffer_text} structure contains fields describing the
1015 text of a buffer; the @code{buffer} structure holds other fields. In
1016 the case of indirect buffers, two or more @code{buffer} structures
1017 reference the same @code{buffer_text} structure.
1018
1019 Here are some of the fields in @code{struct buffer_text}:
1020
1021 @table @code
1022 @item beg
1023 The address of the buffer contents.
1024
1025 @item gpt
1026 @itemx gpt_byte
1027 The character and byte positions of the buffer gap. @xref{Buffer
1028 Gap}.
1029
1030 @item z
1031 @itemx z_byte
1032 The character and byte positions of the end of the buffer text.
1033
1034 @item gap_size
1035 The size of buffer's gap. @xref{Buffer Gap}.
1036
1037 @item modiff
1038 @itemx save_modiff
1039 @itemx chars_modiff
1040 @itemx overlay_modiff
1041 These fields count the number of buffer-modification events performed
1042 in this buffer. @code{modiff} is incremented after each
1043 buffer-modification event, and is never otherwise changed;
1044 @code{save_modiff} contains the value of @code{modiff} the last time
1045 the buffer was visited or saved; @code{chars_modiff} counts only
1046 modifications to the characters in the buffer, ignoring all other
1047 kinds of changes; and @code{overlay_modiff} counts only modifications
1048 to the overlays.
1049
1050 @item beg_unchanged
1051 @itemx end_unchanged
1052 The number of characters at the start and end of the text that are
1053 known to be unchanged since the last complete redisplay.
1054
1055 @item unchanged_modified
1056 @itemx overlay_unchanged_modified
1057 The values of @code{modiff} and @code{overlay_modiff}, respectively,
1058 after the last complete redisplay. If their current values match
1059 @code{modiff} or @code{overlay_modiff}, that means
1060 @code{beg_unchanged} and @code{end_unchanged} contain no useful
1061 information.
1062
1063 @item markers
1064 The markers that refer to this buffer. This is actually a single
1065 marker, and successive elements in its marker @code{chain} are the other
1066 markers referring to this buffer text.
1067
1068 @item intervals
1069 The interval tree which records the text properties of this buffer.
1070 @end table
1071
1072 Some of the fields of @code{struct buffer} are:
1073
1074 @table @code
1075 @item header
1076 A header of type @code{struct vectorlike_header} is common to all
1077 vectorlike objects.
1078
1079 @item own_text
1080 A @code{struct buffer_text} structure that ordinarily holds the buffer
1081 contents. In indirect buffers, this field is not used.
1082
1083 @item text
1084 A pointer to the @code{buffer_text} structure for this buffer. In an
1085 ordinary buffer, this is the @code{own_text} field above. In an
1086 indirect buffer, this is the @code{own_text} field of the base buffer.
1087
1088 @item next
1089 A pointer to the next buffer, in the chain of all buffers, including
1090 killed buffers. This chain is used only for allocation and garbage
1091 collection, in order to collect killed buffers properly.
1092
1093 @item pt
1094 @itemx pt_byte
1095 The character and byte positions of point in a buffer.
1096
1097 @item begv
1098 @itemx begv_byte
1099 The character and byte positions of the beginning of the accessible
1100 range of text in the buffer.
1101
1102 @item zv
1103 @itemx zv_byte
1104 The character and byte positions of the end of the accessible range of
1105 text in the buffer.
1106
1107 @item base_buffer
1108 In an indirect buffer, this points to the base buffer. In an ordinary
1109 buffer, it is null.
1110
1111 @item local_flags
1112 This field contains flags indicating that certain variables are local
1113 in this buffer. Such variables are declared in the C code using
1114 @code{DEFVAR_PER_BUFFER}, and their buffer-local bindings are stored
1115 in fields in the buffer structure itself. (Some of these fields are
1116 described in this table.)
1117
1118 @item modtime
1119 The modification time of the visited file. It is set when the file is
1120 written or read. Before writing the buffer into a file, this field is
1121 compared to the modification time of the file to see if the file has
1122 changed on disk. @xref{Buffer Modification}.
1123
1124 @item auto_save_modified
1125 The time when the buffer was last auto-saved.
1126
1127 @item last_window_start
1128 The @code{window-start} position in the buffer as of the last time the
1129 buffer was displayed in a window.
1130
1131 @item clip_changed
1132 This flag indicates that narrowing has changed in the buffer.
1133 @xref{Narrowing}.
1134
1135 @item prevent_redisplay_optimizations_p
1136 This flag indicates that redisplay optimizations should not be used to
1137 display this buffer.
1138
1139 @item overlay_center
1140 This field holds the current overlay center position. @xref{Managing
1141 Overlays}.
1142
1143 @item overlays_before
1144 @itemx overlays_after
1145 These fields hold, respectively, a list of overlays that end at or
1146 before the current overlay center, and a list of overlays that end
1147 after the current overlay center. @xref{Managing Overlays}.
1148 @code{overlays_before} is sorted in order of decreasing end position,
1149 and @code{overlays_after} is sorted in order of increasing beginning
1150 position.
1151
1152 @c FIXME? the following are now all Lisp_Object BUFFER_INTERNAL_FIELD (foo).
1153
1154 @item name
1155 A Lisp string that names the buffer. It is guaranteed to be unique.
1156 @xref{Buffer Names}.
1157
1158 @item save_length
1159 The length of the file this buffer is visiting, when last read or
1160 saved. This and other fields concerned with saving are not kept in
1161 the @code{buffer_text} structure because indirect buffers are never
1162 saved.
1163
1164 @item directory
1165 The directory for expanding relative file names. This is the value of
1166 the buffer-local variable @code{default-directory} (@pxref{File Name Expansion}).
1167
1168 @item filename
1169 The name of the file visited in this buffer, or @code{nil}. This is
1170 the value of the buffer-local variable @code{buffer-file-name}
1171 (@pxref{Buffer File Name}).
1172
1173 @item undo_list
1174 @itemx backed_up
1175 @itemx auto_save_file_name
1176 @itemx auto_save_file_format
1177 @itemx read_only
1178 @itemx file_format
1179 @itemx file_truename
1180 @itemx invisibility_spec
1181 @itemx display_count
1182 @itemx display_time
1183 These fields store the values of Lisp variables that are automatically
1184 buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
1185 variable names have the additional prefix @code{buffer-} and have
1186 underscores replaced with dashes. For instance, @code{undo_list}
1187 stores the value of @code{buffer-undo-list}.
1188
1189 @item mark
1190 The mark for the buffer. The mark is a marker, hence it is also
1191 included on the list @code{markers}. @xref{The Mark}.
1192
1193 @item local_var_alist
1194 The association list describing the buffer-local variable bindings of
1195 this buffer, not including the built-in buffer-local bindings that
1196 have special slots in the buffer object. (Those slots are omitted
1197 from this table.) @xref{Buffer-Local Variables}.
1198
1199 @item major_mode
1200 Symbol naming the major mode of this buffer, e.g., @code{lisp-mode}.
1201
1202 @item mode_name
1203 Pretty name of the major mode, e.g., @code{"Lisp"}.
1204
1205 @item keymap
1206 @itemx abbrev_table
1207 @itemx syntax_table
1208 @itemx category_table
1209 @itemx display_table
1210 These fields store the buffer's local keymap (@pxref{Keymaps}), abbrev
1211 table (@pxref{Abbrev Tables}), syntax table (@pxref{Syntax Tables}),
1212 category table (@pxref{Categories}), and display table (@pxref{Display
1213 Tables}).
1214
1215 @item downcase_table
1216 @itemx upcase_table
1217 @itemx case_canon_table
1218 These fields store the conversion tables for converting text to lower
1219 case, upper case, and for canonicalizing text for case-fold search.
1220 @xref{Case Tables}.
1221
1222 @item minor_modes
1223 An alist of the minor modes of this buffer.
1224
1225 @item pt_marker
1226 @itemx begv_marker
1227 @itemx zv_marker
1228 These fields are only used in an indirect buffer, or in a buffer that
1229 is the base of an indirect buffer. Each holds a marker that records
1230 @code{pt}, @code{begv}, and @code{zv} respectively, for this buffer
1231 when the buffer is not current.
1232
1233 @item mode_line_format
1234 @itemx header_line_format
1235 @itemx case_fold_search
1236 @itemx tab_width
1237 @itemx fill_column
1238 @itemx left_margin
1239 @itemx auto_fill_function
1240 @itemx truncate_lines
1241 @itemx word_wrap
1242 @itemx ctl_arrow
1243 @itemx bidi_display_reordering
1244 @itemx bidi_paragraph_direction
1245 @itemx selective_display
1246 @itemx selective_display_ellipses
1247 @itemx overwrite_mode
1248 @itemx abbrev_mode
1249 @itemx mark_active
1250 @itemx enable_multibyte_characters
1251 @itemx buffer_file_coding_system
1252 @itemx cache_long_line_scans
1253 @itemx point_before_scroll
1254 @itemx left_fringe_width
1255 @itemx right_fringe_width
1256 @itemx fringes_outside_margins
1257 @itemx scroll_bar_width
1258 @itemx indicate_empty_lines
1259 @itemx indicate_buffer_boundaries
1260 @itemx fringe_indicator_alist
1261 @itemx fringe_cursor_alist
1262 @itemx scroll_up_aggressively
1263 @itemx scroll_down_aggressively
1264 @itemx cursor_type
1265 @itemx cursor_in_non_selected_windows
1266 These fields store the values of Lisp variables that are automatically
1267 buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
1268 variable names have underscores replaced with dashes. For instance,
1269 @code{mode_line_format} stores the value of @code{mode-line-format}.
1270
1271 @item last_selected_window
1272 This is the last window that was selected with this buffer in it, or @code{nil}
1273 if that window no longer displays this buffer.
1274 @end table
1275
1276 @node Window Internals
1277 @subsection Window Internals
1278 @cindex internals, of window
1279 @cindex window internals
1280
1281 The fields of a window (for a complete list, see the definition of
1282 @code{struct window} in @file{window.h}) include:
1283
1284 @table @code
1285 @item frame
1286 The frame that this window is on.
1287
1288 @item mini_p
1289 Non-@code{nil} if this window is a minibuffer window.
1290
1291 @item parent
1292 Internally, Emacs arranges windows in a tree; each group of siblings has
1293 a parent window whose area includes all the siblings. This field points
1294 to a window's parent.
1295
1296 Parent windows do not display buffers, and play little role in display
1297 except to shape their child windows. Emacs Lisp programs usually have
1298 no access to the parent windows; they operate on the windows at the
1299 leaves of the tree, which actually display buffers.
1300
1301 @c FIXME: These two slots and the 'buffer' slot below were replaced
1302 @c with a single slot 'contents' on 2013-03-28. --xfq
1303 @item hchild
1304 @itemx vchild
1305 These fields contain the window's leftmost child and its topmost child
1306 respectively. @code{hchild} is used if the window is subdivided
1307 horizontally by child windows, and @code{vchild} if it is subdivided
1308 vertically. In a live window, only one of @code{hchild}, @code{vchild},
1309 and @code{buffer} (q.v.@:) is non-@code{nil}.
1310
1311 @item next
1312 @itemx prev
1313 The next sibling and previous sibling of this window. @code{next} is
1314 @code{nil} if the window is the right-most or bottom-most in its group;
1315 @code{prev} is @code{nil} if it is the left-most or top-most in its
1316 group.
1317
1318 @item left_col
1319 The left-hand edge of the window, measured in columns, relative to the
1320 leftmost column in the frame (column 0).
1321
1322 @item top_line
1323 The top edge of the window, measured in lines, relative to the topmost
1324 line in the frame (line 0).
1325
1326 @item total_cols
1327 @itemx total_lines
1328 The width and height of the window, measured in columns and lines
1329 respectively. The width includes the scroll bar and fringes, and/or
1330 the separator line on the right of the window (if any).
1331
1332 @item buffer
1333 The buffer that the window is displaying.
1334
1335 @item start
1336 A marker pointing to the position in the buffer that is the first
1337 character displayed in the window.
1338
1339 @item pointm
1340 @cindex window point internals
1341 This is the value of point in the current buffer when this window is
1342 selected; when it is not selected, it retains its previous value.
1343
1344 @item force_start
1345 If this flag is non-@code{nil}, it says that the window has been
1346 scrolled explicitly by the Lisp program. This affects what the next
1347 redisplay does if point is off the screen: instead of scrolling the
1348 window to show the text around point, it moves point to a location that
1349 is on the screen.
1350
1351 @item frozen_window_start_p
1352 This field is set temporarily to 1 to indicate to redisplay that
1353 @code{start} of this window should not be changed, even if point
1354 gets invisible.
1355
1356 @item start_at_line_beg
1357 Non-@code{nil} means current value of @code{start} was the beginning of a line
1358 when it was chosen.
1359
1360 @item use_time
1361 This is the last time that the window was selected. The function
1362 @code{get-lru-window} uses this field.
1363
1364 @item sequence_number
1365 A unique number assigned to this window when it was created.
1366
1367 @item last_modified
1368 The @code{modiff} field of the window's buffer, as of the last time
1369 a redisplay completed in this window.
1370
1371 @item last_overlay_modified
1372 The @code{overlay_modiff} field of the window's buffer, as of the last
1373 time a redisplay completed in this window.
1374
1375 @item last_point
1376 The buffer's value of point, as of the last time a redisplay completed
1377 in this window.
1378
1379 @item last_had_star
1380 A non-@code{nil} value means the window's buffer was modified when the
1381 window was last updated.
1382
1383 @item vertical_scroll_bar
1384 This window's vertical scroll bar.
1385
1386 @item left_margin_cols
1387 @itemx right_margin_cols
1388 The widths of the left and right margins in this window. A value of
1389 @code{nil} means no margin.
1390
1391 @item left_fringe_width
1392 @itemx right_fringe_width
1393 The widths of the left and right fringes in this window. A value of
1394 @code{nil} or @code{t} means use the values of the frame.
1395
1396 @item fringes_outside_margins
1397 A non-@code{nil} value means the fringes outside the display margins;
1398 othersize they are between the margin and the text.
1399
1400 @item window_end_pos
1401 This is computed as @code{z} minus the buffer position of the last glyph
1402 in the current matrix of the window. The value is only valid if
1403 @code{window_end_valid} is not @code{nil}.
1404
1405 @item window_end_bytepos
1406 The byte position corresponding to @code{window_end_pos}.
1407
1408 @item window_end_vpos
1409 The window-relative vertical position of the line containing
1410 @code{window_end_pos}.
1411
1412 @item window_end_valid
1413 This field is set to a non-@code{nil} value if @code{window_end_pos} is truly
1414 valid. This is @code{nil} if nontrivial redisplay is pre-empted, since in that
1415 case the display that @code{window_end_pos} was computed for did not get
1416 onto the screen.
1417
1418 @item cursor
1419 A structure describing where the cursor is in this window.
1420
1421 @item last_cursor
1422 The value of @code{cursor} as of the last redisplay that finished.
1423
1424 @item phys_cursor
1425 A structure describing where the cursor of this window physically is.
1426
1427 @item phys_cursor_type
1428 @c FIXME What is this?
1429 @c itemx phys_cursor_ascent
1430 @itemx phys_cursor_height
1431 @itemx phys_cursor_width
1432 The type, height, and width of the cursor that was last displayed on
1433 this window.
1434
1435 @item phys_cursor_on_p
1436 This field is non-zero if the cursor is physically on.
1437
1438 @item cursor_off_p
1439 Non-zero means the cursor in this window is logically off. This is
1440 used for blinking the cursor.
1441
1442 @item last_cursor_off_p
1443 This field contains the value of @code{cursor_off_p} as of the time of
1444 the last redisplay.
1445
1446 @item must_be_updated_p
1447 This is set to 1 during redisplay when this window must be updated.
1448
1449 @item hscroll
1450 This is the number of columns that the display in the window is scrolled
1451 horizontally to the left. Normally, this is 0.
1452
1453 @item vscroll
1454 Vertical scroll amount, in pixels. Normally, this is 0.
1455
1456 @item dedicated
1457 Non-@code{nil} if this window is dedicated to its buffer.
1458
1459 @item display_table
1460 The window's display table, or @code{nil} if none is specified for it.
1461
1462 @item update_mode_line
1463 Non-@code{nil} means this window's mode line needs to be updated.
1464
1465 @item base_line_number
1466 The line number of a certain position in the buffer, or @code{nil}.
1467 This is used for displaying the line number of point in the mode line.
1468
1469 @item base_line_pos
1470 The position in the buffer for which the line number is known, or
1471 @code{nil} meaning none is known. If it is a buffer, don't display
1472 the line number as long as the window shows that buffer.
1473
1474 @item column_number_displayed
1475 The column number currently displayed in this window's mode line, or @code{nil}
1476 if column numbers are not being displayed.
1477
1478 @item current_matrix
1479 @itemx desired_matrix
1480 Glyph matrices describing the current and desired display of this window.
1481 @end table
1482
1483 @node Process Internals
1484 @subsection Process Internals
1485 @cindex internals, of process
1486 @cindex process internals
1487
1488 The fields of a process (for a complete list, see the definition of
1489 @code{struct Lisp_Process} in @file{process.h}) include:
1490
1491 @table @code
1492 @item name
1493 A string, the name of the process.
1494
1495 @item command
1496 A list containing the command arguments that were used to start this
1497 process. For a network or serial process, it is @code{nil} if the
1498 process is running or @code{t} if the process is stopped.
1499
1500 @item filter
1501 A function used to accept output from the process.
1502
1503 @item sentinel
1504 A function called whenever the state of the process changes.
1505
1506 @item buffer
1507 The associated buffer of the process.
1508
1509 @item pid
1510 An integer, the operating system's process @acronym{ID}.
1511 Pseudo-processes such as network or serial connections use a value of 0.
1512
1513 @item childp
1514 A flag, @code{t} if this is really a child process. For a network or
1515 serial connection, it is a plist based on the arguments to
1516 @code{make-network-process} or @code{make-serial-process}.
1517
1518 @item mark
1519 A marker indicating the position of the end of the last output from this
1520 process inserted into the buffer. This is often but not always the end
1521 of the buffer.
1522
1523 @item kill_without_query
1524 If this is non-zero, killing Emacs while this process is still running
1525 does not ask for confirmation about killing the process.
1526
1527 @item raw_status
1528 The raw process status, as returned by the @code{wait} system call.
1529
1530 @item status
1531 The process status, as @code{process-status} should return it.
1532
1533 @item tick
1534 @itemx update_tick
1535 If these two fields are not equal, a change in the status of the process
1536 needs to be reported, either by running the sentinel or by inserting a
1537 message in the process buffer.
1538
1539 @item pty_flag
1540 Non-@code{nil} if communication with the subprocess uses a pty;
1541 @code{nil} if it uses a pipe.
1542
1543 @item infd
1544 The file descriptor for input from the process.
1545
1546 @item outfd
1547 The file descriptor for output to the process.
1548
1549 @item tty_name
1550 The name of the terminal that the subprocess is using,
1551 or @code{nil} if it is using pipes.
1552
1553 @item decode_coding_system
1554 Coding-system for decoding the input from this process.
1555
1556 @item decoding_buf
1557 A working buffer for decoding.
1558
1559 @item decoding_carryover
1560 Size of carryover in decoding.
1561
1562 @item encode_coding_system
1563 Coding-system for encoding the output to this process.
1564
1565 @item encoding_buf
1566 A working buffer for encoding.
1567
1568 @item inherit_coding_system_flag
1569 Flag to set @code{coding-system} of the process buffer from the
1570 coding system used to decode process output.
1571
1572 @item type
1573 Symbol indicating the type of process: @code{real}, @code{network},
1574 @code{serial}.
1575
1576 @end table
1577
1578 @node C Integer Types
1579 @section C Integer Types
1580 @cindex integer types (C programming language)
1581
1582 Here are some guidelines for use of integer types in the Emacs C
1583 source code. These guidelines sometimes give competing advice; common
1584 sense is advised.
1585
1586 @itemize @bullet
1587 @item
1588 Avoid arbitrary limits. For example, avoid @code{int len = strlen
1589 (s);} unless the length of @code{s} is required for other reasons to
1590 fit in @code{int} range.
1591
1592 @item
1593 Do not assume that signed integer arithmetic wraps around on overflow.
1594 This is no longer true of Emacs porting targets: signed integer
1595 overflow has undefined behavior in practice, and can dump core or
1596 even cause earlier or later code to behave illogically. Unsigned
1597 overflow does wrap around reliably, modulo a power of two.
1598
1599 @item
1600 Prefer signed types to unsigned, as code gets confusing when signed
1601 and unsigned types are combined. Many other guidelines assume that
1602 types are signed; in the rarer cases where unsigned types are needed,
1603 similar advice may apply to the unsigned counterparts (e.g.,
1604 @code{size_t} instead of @code{ptrdiff_t}, or @code{uintptr_t} instead
1605 of @code{intptr_t}).
1606
1607 @item
1608 Prefer @code{int} for Emacs character codes, in the range 0 ..@: 0x3FFFFF@.
1609 More generally, prefer @code{int} for integers known to be in
1610 @code{int} range, e.g., screen column counts.
1611
1612 @item
1613 Prefer @code{ptrdiff_t} for sizes, i.e., for integers bounded by the
1614 maximum size of any individual C object or by the maximum number of
1615 elements in any C array. This is part of Emacs's general preference
1616 for signed types. Using @code{ptrdiff_t} limits objects to
1617 @code{PTRDIFF_MAX} bytes, but larger objects would cause trouble
1618 anyway since they would break pointer subtraction, so this does not
1619 impose an arbitrary limit.
1620
1621 @item
1622 Avoid @code{ssize_t} except when communicating to low-level APIs that
1623 have @code{ssize_t}-related limitations. Although it's equivalent to
1624 @code{ptrdiff_t} on typical platforms, @code{ssize_t} is occasionally
1625 narrower, so using it for size-related calculations could overflow.
1626 Also, @code{ptrdiff_t} is more ubiquitous and better-standardized, has
1627 standard @code{printf} formats, and is the basis for Emacs's internal
1628 size-overflow checking. When using @code{ssize_t}, please note that
1629 POSIX requires support only for values in the range @minus{}1 ..@:
1630 @code{SSIZE_MAX}.
1631
1632 @item
1633 Prefer @code{intptr_t} for internal representations of pointers, or
1634 for integers bounded only by the number of objects that can exist at
1635 any given time or by the total number of bytes that can be allocated.
1636 Currently Emacs sometimes uses other types when @code{intptr_t} would
1637 be better; fixing this is lower priority, as the code works as-is on
1638 Emacs's current porting targets.
1639
1640 @item
1641 Prefer the Emacs-defined type @code{EMACS_INT} for representing values
1642 converted to or from Emacs Lisp fixnums, as fixnum arithmetic is based
1643 on @code{EMACS_INT}.
1644
1645 @item
1646 When representing a system value (such as a file size or a count of
1647 seconds since the Epoch), prefer the corresponding system type (e.g.,
1648 @code{off_t}, @code{time_t}). Do not assume that a system type is
1649 signed, unless this assumption is known to be safe. For example,
1650 although @code{off_t} is always signed, @code{time_t} need not be.
1651
1652 @item
1653 Prefer the Emacs-defined type @code{printmax_t} for representing
1654 values that might be any signed integer that can be printed,
1655 using a @code{printf}-family function.
1656
1657 @item
1658 Prefer @code{intmax_t} for representing values that might be any
1659 signed integer value.
1660
1661 @item
1662 Prefer @code{bool}, @code{false} and @code{true} for booleans.
1663 Using @code{bool} can make programs easier to read and a bit faster than
1664 using @code{int}. Although it is also OK to use @code{int}, @code{0}
1665 and @code{1}, this older style is gradually being phased out. When
1666 using @code{bool}, respect the limitations of the replacement
1667 implementation of @code{bool}, as documented in the source file
1668 @file{lib/stdbool.in.h}. In particular, boolean bitfields should be of type
1669 @code{bool_bf}, not @code{bool}, so that they work correctly even when
1670 compiling Objective C with standard GCC.
1671
1672 @item
1673 In bitfields, prefer @code{unsigned int} or @code{signed int} to
1674 @code{int}, as @code{int} is less portable: it might be signed, and
1675 might not be. Single-bit bit fields should be @code{unsigned int} or
1676 @code{bool_bf} so that their values are 0 or 1.
1677 @end itemize
1678
1679 @c FIXME Mention src/globals.h somewhere in this file?