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