1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990--1993, 1998--1999, 2001--2021 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
9This chapter describes how the runnable Emacs executable is dumped with
10the preloaded Lisp libraries in it, how storage is allocated, and some
11internal 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* Writing Dynamic Modules::    Writing loadable modules for Emacs.
22* Object Internals::    Data formats of buffers, windows, processes.
23* C Integer Types::     How C integer types are used inside Emacs.
24@end menu
25
26@node Building Emacs
27@section Building Emacs
28@cindex building Emacs
29@pindex temacs
30
31  This section explains the steps involved in building the Emacs
32executable.  You don't have to know this material to build and install
33Emacs, since the makefiles do all these things automatically.  This
34information is pertinent to Emacs developers.
35
36  Building Emacs requires GNU Make version 3.81 or later.
37
38  Compilation of the C source files in the @file{src} directory
39produces an executable file called @file{temacs}, also called a
40@dfn{bare impure Emacs}.  It contains the Emacs Lisp interpreter and
41I/O routines, but not the editing commands.
42
43@cindex @file{loadup.el}
44  The command @w{@command{temacs -l loadup}} would run @file{temacs}
45and direct it to load @file{loadup.el}.  The @code{loadup} library
46loads additional Lisp libraries, which set up the normal Emacs editing
47environment.  After this step, the Emacs executable is no longer
48@dfn{bare}.
49
50@cindex dumping Emacs
51@cindex @option{--temacs} option, and dumping method
52  Because it takes some time to load the standard Lisp files, the
53@file{temacs} executable usually isn't run directly by users.
54Instead, one of the last steps of building Emacs runs the command
55@w{@samp{temacs -batch -l loadup --temacs=@var{dump-method}}}.  The
56special option @option{--temacs} tells @command{temacs} how to record
57all the standard preloaded Lisp functions and variables, so that when
58you subsequently run Emacs, it will start much faster.  The
59@option{--temacs} option requires an argument @var{dump-method}, which
60can be one of the following:
61
62@table @samp
63@item pdump
64@cindex dump file
65Record the preloaded Lisp data in a @dfn{dump file}.  This
66method produces an additional data file which Emacs will load at
67startup.  The produced dump file is usually called @file{emacs.pdmp},
68and is installed in the Emacs @code{exec-directory} (@pxref{Help
69Functions}).  This method is the most preferred one, as it does not
70require Emacs to employ any special techniques of memory allocation,
71which might get in the way of various memory-layout techniques used by
72modern systems to enhance security and privacy.
73
74@item pbootstrap
75@cindex bootstrapping Emacs
76Like @samp{pdump}, but used while @dfn{bootstrapping} Emacs, when no
77previous Emacs binary and no @file{*.elc} byte-compiled Lisp files are
78available.  The produced dump file is usually named
79@file{bootstrap-emacs.pdmp} in this case.
80
81@item dump
82@cindex unexec
83This method causes @command{temacs} to dump out an executable program,
84called @file{emacs}, which has all the standard Lisp files already
85preloaded into it.  (The @samp{-batch} argument prevents
86@command{temacs} from trying to initialize any of its data on the
87terminal, so that the tables of terminal information are empty in the
88dumped Emacs.)  This method is also known as @dfn{unexec}, because it
89produces a program file from a running process, and thus is in some
90sense the opposite of executing a program to start a process.
91Although this method was the way that Emacs traditionally saved its
92state, it is now deprecated.
93
94@item bootstrap
95Like @samp{dump}, but used when bootstrapping Emacs with the
96@code{unexec} method.
97@end table
98
99@cindex preloaded Lisp files
100@vindex preloaded-file-list
101  The dumped @file{emacs} executable (also called a @dfn{pure} Emacs)
102is the one which is installed.  If the portable dumper was used to
103build Emacs, the @file{emacs} executable is actually an exact copy of
104@file{temacs}, and the corresponding @file{emacs.pdmp} file is
105installed as well.  The variable @code{preloaded-file-list} stores a
106list of the preloaded Lisp files recorded in the dump file or
107in the dumped Emacs executable.  If you port Emacs to a new operating
108system, and are not able to implement dumping of any kind, then Emacs
109must load @file{loadup.el} each time it starts.
110
111@cindex build details
112@cindex deterministic build
113@cindex @option{--disable-build-details} option to @command{configure}
114  By default the dumped @file{emacs} executable records details such
115as the build time and host name.  Use the
116@option{--disable-build-details} option of @command{configure} to
117suppress these details, so that building and installing Emacs twice
118from the same sources is more likely to result in identical copies of
119Emacs.
120
121@cindex @file{site-load.el}
122  You can specify additional files to preload by writing a library named
123@file{site-load.el} that loads them.  You may need to rebuild Emacs
124with an added definition
125
126@example
127#define SITELOAD_PURESIZE_EXTRA @var{n}
128@end example
129
130@noindent
131to make @var{n} added bytes of pure space to hold the additional files;
132see @file{src/puresize.h}.
133(Try adding increments of 20000 until it is big enough.)  However, the
134advantage of preloading additional files decreases as machines get
135faster.  On modern machines, it is usually not advisable.
136
137  After @file{loadup.el} reads @file{site-load.el}, it finds the
138documentation strings for primitive and preloaded functions (and
139variables) in the file @file{etc/DOC} where they are stored, by
140calling @code{Snarf-documentation} (@pxref{Definition of
141Snarf-documentation,, Accessing Documentation}).
142
143@cindex @file{site-init.el}
144@cindex preloading additional functions and variables
145  You can specify other Lisp expressions to execute just before dumping
146by putting them in a library named @file{site-init.el}.  This file is
147executed after the documentation strings are found.
148
149  If you want to preload function or variable definitions, there are
150three ways you can do this and make their documentation strings
151accessible when you subsequently run Emacs:
152
153@itemize @bullet
154@item
155Arrange to scan these files when producing the @file{etc/DOC} file,
156and load them with @file{site-load.el}.
157
158@item
159Load the files with @file{site-init.el}, then copy the files into the
160installation directory for Lisp files when you install Emacs.
161
162@item
163Specify a @code{nil} value for @code{byte-compile-dynamic-docstrings}
164as a local variable in each of these files, and load them with either
165@file{site-load.el} or @file{site-init.el}.  (This method has the
166drawback that the documentation strings take up space in Emacs all the
167time.)
168@end itemize
169
170@cindex change @code{load-path} at configure time
171@cindex @option{--enable-locallisppath} option to @command{configure}
172  It is not advisable to put anything in @file{site-load.el} or
173@file{site-init.el} that would alter any of the features that users
174expect in an ordinary unmodified Emacs.  If you feel you must override
175normal features for your site, do it with @file{default.el}, so that
176users can override your changes if they wish.  @xref{Startup Summary}.
177Note that if either @file{site-load.el} or @file{site-init.el} changes
178@code{load-path}, the changes will be lost after dumping.
179@xref{Library Search}.  To make a permanent change to
180@code{load-path}, use the @option{--enable-locallisppath} option
181of @command{configure}.
182
183  In a package that can be preloaded, it is sometimes necessary (or
184useful) to delay certain evaluations until Emacs subsequently starts
185up.  The vast majority of such cases relate to the values of
186customizable variables.  For example, @code{tutorial-directory} is a
187variable defined in @file{startup.el}, which is preloaded.  The default
188value is set based on @code{data-directory}.  The variable needs to
189access the value of @code{data-directory} when Emacs starts, not when
190it is dumped, because the Emacs executable has probably been installed
191in a different location since it was dumped.
192
193@defun custom-initialize-delay symbol value
194This function delays the initialization of @var{symbol} to the next
195Emacs start.  You normally use this function by specifying it as the
196@code{:initialize} property of a customizable variable.  (The argument
197@var{value} is unused, and is provided only for compatibility with the
198form Custom expects.)
199@end defun
200
201In the unlikely event that you need a more general functionality than
202@code{custom-initialize-delay} provides, you can use
203@code{before-init-hook} (@pxref{Startup Summary}).
204
205@defun dump-emacs-portable to-file &optional track-referrers
206This function dumps the current state of Emacs into a dump
207file @var{to-file}, using the @code{pdump} method.  Normally, the
208dump file is called @file{@var{emacs-name}.dmp}, where
209@var{emacs-name} is the name of the Emacs executable file.  The
210optional argument @var{track-referrers}, if non-@code{nil}, causes the
211portable dumper to keep additional information to help track
212down the provenance of object types that are not yet supported by the
213@code{pdump} method.
214
215Although the portable dumper code can run on many platforms, the dump
216files that it produces are not portable---they can be loaded only by
217the Emacs executable that dumped them.
218
219If you want to use this function in an Emacs that was already dumped,
220you must run Emacs with the @samp{-batch} option.
221
222@vindex after-pdump-load-hook
223If you're including @samp{.el} files in the dumped Emacs and that
224@samp{.el} file has code that is normally run at load time, that code
225won't be run when Emacs starts after dumping.  To help work around
226that problem, you can put functions on the
227@code{after-pdump-load-hook} hook.  This hook is run when starting
228Emacs.
229@end defun
230
231@defun dump-emacs to-file from-file
232@cindex unexec
233This function dumps the current state of Emacs into an executable file
234@var{to-file}, using the @code{unexec} method.  It takes symbols from
235@var{from-file} (this is normally the executable file @file{temacs}).
236
237This function cannot be used in an Emacs that was already dumped.
238This function is deprecated, and by default Emacs is built without
239@code{unexec} support so this function is not available.
240@end defun
241
242@defun pdumper-stats
243If the current Emacs session restored its state from a dump
244file, this function returns information about the dump file and the
245time it took to restore the Emacs state.  The value is an alist
246@w{@code{((dumped-with-pdumper . t) (load-time . @var{time})
247(dump-file-name . @var{file}))}},
248where @var{file} is the name of the dump file, and @var{time} is the
249time in seconds it took to restore the state from the dump file.
250If the current session was not restored from a dump file, the
251value is nil.
252@end defun
253
254@node Pure Storage
255@section Pure Storage
256@cindex pure storage
257
258  Emacs Lisp uses two kinds of storage for user-created Lisp objects:
259@dfn{normal storage} and @dfn{pure storage}.  Normal storage is where
260all the new data created during an Emacs session are kept
261(@pxref{Garbage Collection}).  Pure storage is used for certain data
262in the preloaded standard Lisp files---data that should never change
263during actual use of Emacs.
264
265  Pure storage is allocated only while @command{temacs} is loading the
266standard preloaded Lisp libraries.  In the file @file{emacs}, it is
267marked as read-only (on operating systems that permit this), so that
268the memory space can be shared by all the Emacs jobs running on the
269machine at once.  Pure storage is not expandable; a fixed amount is
270allocated when Emacs is compiled, and if that is not sufficient for
271the preloaded libraries, @file{temacs} allocates dynamic memory for
272the part that didn't fit.  If Emacs will be dumped using the
273@code{pdump} method (@pxref{Building Emacs}), the pure-space overflow
274is of no special importance (it just means some of the preloaded stuff
275cannot be shared with other Emacs jobs).  However, if Emacs will be
276dumped using the now obsolete @code{unexec} method, the resulting
277image will work, but garbage collection (@pxref{Garbage Collection})
278is disabled in this situation, causing a memory leak.  Such an
279overflow normally won't happen unless you try to preload additional
280libraries or add features to the standard ones.  Emacs will display a
281warning about the overflow when it starts, if it was dumped using
282@code{unexec}.  If this happens, you should increase the compilation
283parameter @code{SYSTEM_PURESIZE_EXTRA} in the file
284@file{src/puresize.h} and rebuild Emacs.
285
286@defun purecopy object
287This function makes a copy in pure storage of @var{object}, and returns
288it.  It copies a string by simply making a new string with the same
289characters, but without text properties, in pure storage.  It
290recursively copies the contents of vectors and cons cells.  It does
291not make copies of other objects such as symbols, but just returns
292them unchanged.  It signals an error if asked to copy markers.
293
294This function is a no-op except while Emacs is being built and dumped;
295it is usually called only in preloaded Lisp files.
296@end defun
297
298@defvar pure-bytes-used
299The value of this variable is the number of bytes of pure storage
300allocated so far.  Typically, in a dumped Emacs, this number is very
301close to the total amount of pure storage available---if it were not,
302we would preallocate less.
303@end defvar
304
305@defvar purify-flag
306This variable determines whether @code{defun} should make a copy of the
307function definition in pure storage.  If it is non-@code{nil}, then the
308function definition is copied into pure storage.
309
310This flag is @code{t} while loading all of the basic functions for
311building Emacs initially (allowing those functions to be shareable and
312non-collectible).  Dumping Emacs as an executable always writes
313@code{nil} in this variable, regardless of the value it actually has
314before and after dumping.
315
316You should not change this flag in a running Emacs.
317@end defvar
318
319@node Garbage Collection
320@section Garbage Collection
321
322@cindex memory allocation
323  When a program creates a list or the user defines a new function
324(such as by loading a library), that data is placed in normal storage.
325If normal storage runs low, then Emacs asks the operating system to
326allocate more memory.  Different types of Lisp objects, such as
327symbols, cons cells, small vectors, markers, etc., are segregated in
328distinct blocks in memory.  (Large vectors, long strings, buffers and
329certain other editing types, which are fairly large, are allocated in
330individual blocks, one per object; small strings are packed into blocks
331of 8k bytes, and small vectors are packed into blocks of 4k bytes).
332
333@cindex vector-like objects, storage
334@cindex storage of vector-like Lisp objects
335  Beyond the basic vector, a lot of objects like markers, overlays and
336buffers are managed as if they were vectors.  The corresponding C data
337structures include the @code{union vectorlike_header} field whose
338@code{size} member contains the subtype enumerated by @code{enum pvec_type}
339and an information about how many @code{Lisp_Object} fields this structure
340contains and what the size of the rest data is.  This information is
341needed to calculate the memory footprint of an object, and used
342by the vector allocation code while iterating over the vector blocks.
343
344@cindex garbage collection
345  It is quite common to use some storage for a while, then release it
346by (for example) killing a buffer or deleting the last pointer to an
347object.  Emacs provides a @dfn{garbage collector} to reclaim this
348abandoned storage.  The garbage collector operates, in essence, by
349finding and marking all Lisp objects that are still accessible to Lisp
350programs.  To begin with, it assumes all the symbols, their values and
351associated function definitions, and any data presently on the stack,
352are accessible.  Any objects that can be reached indirectly through
353other accessible objects are also accessible, but this calculation is
354done ``conservatively'', so it may slightly overestimate how many
355objects that are accessible.
356
357  When marking is finished, all objects still unmarked are garbage.  No
358matter what the Lisp program or the user does, it is impossible to refer
359to them, since there is no longer a way to reach them.  Their space
360might as well be reused, since no one will miss them.  The second
361(sweep) phase of the garbage collector arranges to reuse them.  (But
362since the marking was done ``conservatively'', not all unused objects
363are guaranteed to be garbage-collected by any one sweep.)
364
365@c ??? Maybe add something describing weak hash tables here?
366
367@cindex free list
368  The sweep phase puts unused cons cells onto a @dfn{free list}
369for future allocation; likewise for symbols and markers.  It compacts
370the accessible strings so they occupy fewer 8k blocks; then it frees the
371other 8k blocks.  Unreachable vectors from vector blocks are coalesced
372to create largest possible free areas; if a free area spans a complete
3734k block, that block is freed.  Otherwise, the free area is recorded
374in a free list array, where each entry corresponds to a free list
375of areas of the same size.  Large vectors, buffers, and other large
376objects are allocated and freed individually.
377
378@cindex CL note---allocate more storage
379@quotation
380@b{Common Lisp note:} Unlike other Lisps, GNU Emacs Lisp does not
381call the garbage collector when the free list is empty.  Instead, it
382simply requests the operating system to allocate more storage, and
383processing continues until @code{gc-cons-threshold} bytes have been
384used.
385
386This means that you can make sure that the garbage collector will not
387run during a certain portion of a Lisp program by calling the garbage
388collector explicitly just before it (provided that portion of the
389program does not use so much space as to force a second garbage
390collection).
391@end quotation
392
393@deffn Command garbage-collect
394This command runs a garbage collection, and returns information on
395the amount of space in use.  (Garbage collection can also occur
396spontaneously if you use more than @code{gc-cons-threshold} bytes of
397Lisp data since the previous garbage collection.)
398
399@code{garbage-collect} returns a list with information on amount of space in
400use, where each entry has the form @samp{(@var{name} @var{size} @var{used})}
401or @samp{(@var{name} @var{size} @var{used} @var{free})}.  In the entry,
402@var{name} is a symbol describing the kind of objects this entry represents,
403@var{size} is the number of bytes used by each one, @var{used} is the number
404of those objects that were found live in the heap, and optional @var{free} is
405the number of those objects that are not live but that Emacs keeps around for
406future allocations.  So an overall result is:
407
408@example
409((@code{conses} @var{cons-size} @var{used-conses} @var{free-conses})
410 (@code{symbols} @var{symbol-size} @var{used-symbols} @var{free-symbols})
411 (@code{strings} @var{string-size} @var{used-strings} @var{free-strings})
412 (@code{string-bytes} @var{byte-size} @var{used-bytes})
413 (@code{vectors} @var{vector-size} @var{used-vectors})
414 (@code{vector-slots} @var{slot-size} @var{used-slots} @var{free-slots})
415 (@code{floats} @var{float-size} @var{used-floats} @var{free-floats})
416 (@code{intervals} @var{interval-size} @var{used-intervals} @var{free-intervals})
417 (@code{buffers} @var{buffer-size} @var{used-buffers})
418 (@code{heap} @var{unit-size} @var{total-size} @var{free-size}))
419@end example
420
421Here is an example:
422
423@example
424(garbage-collect)
425      @result{} ((conses 16 49126 8058) (symbols 48 14607 0)
426                 (strings 32 2942 2607)
427                 (string-bytes 1 78607) (vectors 16 7247)
428                 (vector-slots 8 341609 29474) (floats 8 71 102)
429                 (intervals 56 27 26) (buffers 944 8)
430                 (heap 1024 11715 2678))
431@end example
432
433Below is a table explaining each element.  Note that last @code{heap} entry
434is optional and present only if an underlying @code{malloc} implementation
435provides @code{mallinfo} function.
436
437@table @var
438@item cons-size
439Internal size of a cons cell, i.e., @code{sizeof (struct Lisp_Cons)}.
440
441@item used-conses
442The number of cons cells in use.
443
444@item free-conses
445The number of cons cells for which space has been obtained from
446the operating system, but that are not currently being used.
447
448@item symbol-size
449Internal size of a symbol, i.e., @code{sizeof (struct Lisp_Symbol)}.
450
451@item used-symbols
452The number of symbols in use.
453
454@item free-symbols
455The number of symbols for which space has been obtained from
456the operating system, but that are not currently being used.
457
458@item string-size
459Internal size of a string header, i.e., @code{sizeof (struct Lisp_String)}.
460
461@item used-strings
462The number of string headers in use.
463
464@item free-strings
465The number of string headers for which space has been obtained
466from the operating system, but that are not currently being used.
467
468@item byte-size
469This is used for convenience and equals to @code{sizeof (char)}.
470
471@item used-bytes
472The total size of all string data in bytes.
473
474@item vector-size
475Size in bytes of a vector of length 1, including its header.
476
477@item used-vectors
478The number of vector headers allocated from the vector blocks.
479
480@item slot-size
481Internal size of a vector slot, always equal to @code{sizeof (Lisp_Object)}.
482
483@item used-slots
484The number of slots in all used vectors.
485Slot counts might include some or all overhead from vector headers,
486depending on the platform.
487
488@item free-slots
489The number of free slots in all vector blocks.
490
491@item float-size
492Internal size of a float object, i.e., @code{sizeof (struct Lisp_Float)}.
493(Do not confuse it with the native platform @code{float} or @code{double}.)
494
495@item used-floats
496The number of floats in use.
497
498@item free-floats
499The number of floats for which space has been obtained from
500the operating system, but that are not currently being used.
501
502@item interval-size
503Internal size of an interval object, i.e., @code{sizeof (struct interval)}.
504
505@item used-intervals
506The number of intervals in use.
507
508@item free-intervals
509The number of intervals for which space has been obtained from
510the operating system, but that are not currently being used.
511
512@item buffer-size
513Internal size of a buffer, i.e., @code{sizeof (struct buffer)}.
514(Do not confuse with the value returned by @code{buffer-size} function.)
515
516@item used-buffers
517The number of buffer objects in use.  This includes killed buffers
518invisible to users, i.e., all buffers in @code{all_buffers} list.
519
520@item unit-size
521The unit of heap space measurement, always equal to 1024 bytes.
522
523@item total-size
524Total heap size, in @var{unit-size} units.
525
526@item free-size
527Heap space which is not currently used, in @var{unit-size} units.
528@end table
529
530If there was overflow in pure space (@pxref{Pure Storage}), and Emacs
531was dumped using the (now obsolete) @code{unexec} method
532(@pxref{Building Emacs}), then @code{garbage-collect} returns
533@code{nil}, because a real garbage collection cannot be done in that
534case.
535@end deffn
536
537@defopt garbage-collection-messages
538If this variable is non-@code{nil}, Emacs displays a message at the
539beginning and end of garbage collection.  The default value is
540@code{nil}.
541@end defopt
542
543@defvar post-gc-hook
544This is a normal hook that is run at the end of garbage collection.
545Garbage collection is inhibited while the hook functions run, so be
546careful writing them.
547@end defvar
548
549@defopt gc-cons-threshold
550The value of this variable is the number of bytes of storage that must
551be allocated for Lisp objects after one garbage collection in order to
552trigger another garbage collection.  You can use the result returned by
553@code{garbage-collect} to get an information about size of the particular
554object type; space allocated to the contents of buffers does not count.
555
556The initial threshold value is @code{GC_DEFAULT_THRESHOLD}, defined in
557@file{alloc.c}.  Since it's defined in @code{word_size} units, the value
558is 400,000 for the default 32-bit configuration and 800,000 for the 64-bit
559one.  If you specify a larger value, garbage collection will happen less
560often.  This reduces the amount of time spent garbage collecting, but
561increases total memory use.  You may want to do this when running a program
562that creates lots of Lisp data.
563
564You can make collections more frequent by specifying a smaller value, down
565to 1/10th of @code{GC_DEFAULT_THRESHOLD}.  A value less than this minimum
566will remain in effect only until the subsequent garbage collection, at which
567time @code{garbage-collect} will set the threshold back to the minimum.
568@end defopt
569
570@defopt gc-cons-percentage
571The value of this variable specifies the amount of consing before a
572garbage collection occurs, as a fraction of the current heap size.
573This criterion and @code{gc-cons-threshold} apply in parallel, and
574garbage collection occurs only when both criteria are satisfied.
575
576As the heap size increases, the time to perform a garbage collection
577increases.  Thus, it can be desirable to do them less frequently in
578proportion.
579@end defopt
580
581  Control over the garbage collector via @code{gc-cons-threshold} and
582@code{gc-cons-percentage} is only approximate.  Although Emacs checks
583for threshold exhaustion regularly, for efficiency reasons it does not
584do so immediately after every change to the heap or to
585@code{gc-cons-threshold} or @code{gc-cons-percentage}, so exhausting
586the threshold does not immediately trigger garbage collection.  Also,
587for efficiency in threshold calculations Emacs approximates the heap
588size, which counts the bytes used by currently-accessible objects in
589the heap.
590
591  The value returned by @code{garbage-collect} describes the amount of
592memory used by Lisp data, broken down by data type.  By contrast, the
593function @code{memory-limit} provides information on the total amount of
594memory Emacs is currently using.
595
596@defun memory-limit
597This function returns an estimate of the total amount of bytes of
598virtual memory that Emacs is currently using, divided by 1024.
599You can use this to get a general idea of how your actions affect the
600memory usage.
601@end defun
602
603@defvar memory-full
604This variable is @code{t} if Emacs is nearly out of memory for Lisp
605objects, and @code{nil} otherwise.
606@end defvar
607
608@defun memory-use-counts
609This returns a list of numbers that count the number of objects
610created in this Emacs session.  Each of these counters increments for
611a certain kind of object.  See the documentation string for details.
612@end defun
613
614@defun memory-info
615This functions returns an amount of total system memory and how much
616of it is free.  On an unsupported system, the value may be @code{nil}.
617@end defun
618
619@defvar gcs-done
620This variable contains the total number of garbage collections
621done so far in this Emacs session.
622@end defvar
623
624@defvar gc-elapsed
625This variable contains the total number of seconds of elapsed time
626during garbage collection so far in this Emacs session, as a
627floating-point number.
628@end defvar
629
630@defun memory-report
631It can sometimes be useful to see where Emacs is using memory (in
632various variables, buffers, and caches).  This command will open a new
633buffer (called @samp{"*Memory Report*"}) that will give an overview,
634in addition to listing the ``largest'' buffers and variables.
635
636All the data here is approximate, because there's really no consistent
637way to compute the size of a variable.  For instance, two variables
638may share parts of a data structure, and this will be counted twice,
639but this command may still give a useful high-level overview of which
640parts of Emacs are using memory.
641@end defun
642
643@node Stack-allocated Objects
644@section Stack-allocated Objects
645
646@cindex stack allocated Lisp objects
647@cindex Lisp objects, stack-allocated
648  The garbage collector described above is used to manage data visible
649from Lisp programs, as well as most of the data internally used by the
650Lisp interpreter.  Sometimes it may be useful to allocate temporary
651internal objects using the C stack of the interpreter.  This can help
652performance, as stack allocation is typically faster than using heap
653memory to allocate and the garbage collector to free.  The downside is
654that using such objects after they are freed results in undefined
655behavior, so uses should be well thought out and carefully debugged by
656using the @code{GC_CHECK_MARKED_OBJECTS} feature (see
657@file{src/alloc.c}).  In particular, stack-allocated objects should
658never be made visible to user Lisp code.
659
660  Currently, cons cells and strings can be allocated this way.  This
661is implemented by C macros like @code{AUTO_CONS} and
662@code{AUTO_STRING} that define a named @code{Lisp_Object} with block
663lifetime.  These objects are not freed by the garbage collector;
664instead, they have automatic storage duration, i.e., they are
665allocated like local variables and are automatically freed at the end
666of execution of the C block that defined the object.
667
668  For performance reasons, stack-allocated strings are limited to
669@acronym{ASCII} characters, and many of these strings are immutable,
670i.e., calling @code{ASET} on them produces undefined behavior.
671
672@node Memory Usage
673@section Memory Usage
674@cindex memory usage
675
676  These functions and variables give information about the total amount
677of memory allocation that Emacs has done, broken down by data type.
678Note the difference between these and the values returned by
679@code{garbage-collect}; those count objects that currently exist, but
680these count the number or size of all allocations, including those for
681objects that have since been freed.
682
683@defvar cons-cells-consed
684The total number of cons cells that have been allocated so far
685in this Emacs session.
686@end defvar
687
688@defvar floats-consed
689The total number of floats that have been allocated so far
690in this Emacs session.
691@end defvar
692
693@defvar vector-cells-consed
694The total number of vector cells that have been allocated so far
695in this Emacs session.
696This includes vector-like objects such as markers and overlays, plus
697certain objects not visible to users.
698@end defvar
699
700@defvar symbols-consed
701The total number of symbols that have been allocated so far
702in this Emacs session.
703@end defvar
704
705@defvar string-chars-consed
706The total number of string characters that have been allocated so far
707in this session.
708@end defvar
709
710@defvar intervals-consed
711The total number of intervals that have been allocated so far
712in this Emacs session.
713@end defvar
714
715@defvar strings-consed
716The total number of strings that have been allocated so far in this
717Emacs session.
718@end defvar
719
720@node C Dialect
721@section C Dialect
722@cindex C programming language
723
724The C part of Emacs is portable to C99 or later: C11-specific features such
725as @samp{<stdalign.h>} and @samp{_Noreturn} are not used without a check,
726typically at configuration time, and the Emacs build procedure
727provides a substitute implementation if necessary.  Some C11 features,
728such as anonymous structures and unions, are too difficult to emulate,
729so they are avoided entirely.
730
731At some point in the future the base C dialect will no doubt change to C11.
732
733@node Writing Emacs Primitives
734@section Writing Emacs Primitives
735@cindex primitive function internals
736@cindex writing Emacs primitives
737
738  Lisp primitives are Lisp functions implemented in C@.  The details of
739interfacing the C function so that Lisp can call it are handled by a few
740C macros.  The only way to really understand how to write new C code is
741to read the source, but we can explain some things here.
742
743  An example of a special form is the definition of @code{or}, from
744@file{eval.c}.  (An ordinary function would have the same general
745appearance.)
746
747@smallexample
748@group
749DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
750       doc: /* Eval args until one of them yields non-nil,
751then return that value.
752The remaining args are not evalled at all.
753If all args return nil, return nil.
754@end group
755@group
756usage: (or CONDITIONS...)  */)
757  (Lisp_Object args)
758@{
759  Lisp_Object val = Qnil;
760@end group
761
762@group
763  while (CONSP (args))
764    @{
765      val = eval_sub (XCAR (args));
766      if (!NILP (val))
767        break;
768      args = XCDR (args);
769      maybe_quit ();
770    @}
771@end group
772
773@group
774  return val;
775@}
776@end group
777@end smallexample
778
779@cindex @code{DEFUN}, C macro to define Lisp primitives
780  Let's start with a precise explanation of the arguments to the
781@code{DEFUN} macro.  Here is a template for them:
782
783@example
784DEFUN (@var{lname}, @var{fname}, @var{sname}, @var{min}, @var{max}, @var{interactive}, @var{doc})
785@end example
786
787@table @var
788@item lname
789This is the name of the Lisp symbol to define as the function name; in
790the example above, it is @code{or}.
791
792@item fname
793This is the C function name for this function.  This is the name that
794is used in C code for calling the function.  The name is, by
795convention, @samp{F} prepended to the Lisp name, with all dashes
796(@samp{-}) in the Lisp name changed to underscores.  Thus, to call
797this function from C code, call @code{For}.
798
799@item sname
800This is a C variable name to use for a structure that holds the data for
801the subr object that represents the function in Lisp.  This structure
802conveys the Lisp symbol name to the initialization routine that will
803create the symbol and store the subr object as its definition.  By
804convention, this name is always @var{fname} with @samp{F} replaced with
805@samp{S}.
806
807@item min
808This is the minimum number of arguments that the function requires.  The
809function @code{or} allows a minimum of zero arguments.
810
811@item max
812This is the maximum number of arguments that the function accepts, if
813there is a fixed maximum.  Alternatively, it can be @code{UNEVALLED},
814indicating a special form that receives unevaluated arguments, or
815@code{MANY}, indicating an unlimited number of evaluated arguments (the
816equivalent of @code{&rest}).  Both @code{UNEVALLED} and @code{MANY} are
817macros.  If @var{max} is a number, it must be more than @var{min} but
818less than 8.
819
820@cindex interactive specification in primitives
821@item interactive
822This is an interactive specification, a string such as might be used
823as the argument of @code{interactive} in a Lisp function
824(@pxref{Using Interactive}).  In the case
825of @code{or}, it is @code{0} (a null pointer), indicating that @code{or}
826cannot be called interactively.  A value of @code{""} indicates a
827function that should receive no arguments when called interactively.
828If the value begins with a @samp{"(}, the string is evaluated as a
829Lisp form.  For example:
830
831@example
832@group
833DEFUN ("foo", Ffoo, Sfoo, 0, 3,
834       "(list (read-char-by-name \"Insert character: \")\
835              (prefix-numeric-value current-prefix-arg)\
836              t)",
837       doc: /* @dots{} */)
838@end group
839@end example
840
841@item doc
842This is the documentation string.  It uses C comment syntax rather
843than C string syntax because comment syntax requires nothing special
844to include multiple lines.  The @samp{doc:} identifies the comment
845that follows as the documentation string.  The @samp{/*} and @samp{*/}
846delimiters that begin and end the comment are not part of the
847documentation string.
848
849If the last line of the documentation string begins with the keyword
850@samp{usage:}, the rest of the line is treated as the argument list
851for documentation purposes.  This way, you can use different argument
852names in the documentation string from the ones used in the C code.
853@samp{usage:} is required if the function has an unlimited number of
854arguments.
855
856Some primitives have multiple definitions, one per platform (e.g.,
857@code{x-create-frame}).  In such cases, rather than writing the
858same documentation string in each definition, only one definition has
859the actual documentation.  The others have placeholders beginning with
860@samp{SKIP}, which are ignored by the function that parses the
861@file{DOC} file.
862
863All the usual rules for documentation strings in Lisp code
864(@pxref{Documentation Tips}) apply to C code documentation strings
865too.
866
867The documentation string can be followed by a list of C function
868attributes for the C function that implements the primitive, like
869this:
870
871@example
872@group
873DEFUN ("bar", Fbar, Sbar, 0, UNEVALLED, 0
874       doc: /* @dots{} */
875       attributes: @var{attr1} @var{attr2} @dots{})
876@end group
877@end example
878
879@noindent
880You can specify more than a single attribute, one after the other.
881Currently, only the following attributes are recognized:
882
883@table @code
884@item noreturn
885Declares the C function as one that never returns.  This corresponds
886to the C11 keyword @code{_Noreturn} and to @w{@code{__attribute__
887((__noreturn__))}} attribute of GCC (@pxref{Function Attributes,,,
888gcc, Using the GNU Compiler Collection}).
889
890@item const
891Declares that the function does not examine any values except its
892arguments, and has no effects except the return value.  This
893corresponds to @w{@code{__attribute__ ((__const__))}} attribute of
894GCC.
895
896@item noinline
897This corresponds to @w{@code{__attribute__ ((__noinline__))}}
898attribute of GCC, which prevents the function from being considered
899for inlining.  This might be needed, e.g., to countermand effects of
900link-time optimizations on stack-based variables.
901@end table
902
903@end table
904
905  After the call to the @code{DEFUN} macro, you must write the
906argument list for the C function, including the types for the
907arguments.  If the primitive accepts a fixed maximum number of Lisp
908arguments, there must be one C argument for each Lisp argument, and
909each argument must be of type @code{Lisp_Object}.  (Various macros and
910functions for creating values of type @code{Lisp_Object} are declared
911in the file @file{lisp.h}.)  If the primitive is a special form, it
912must accept a Lisp list containing its unevaluated Lisp arguments as a
913single argument of type @code{Lisp_Object}.  If the primitive has no
914upper limit on the number of evaluated Lisp arguments, it must have
915exactly two C arguments: the first is the number of Lisp arguments,
916and the second is the address of a block containing their values.
917These have types @code{ptrdiff_t} and @w{@code{Lisp_Object *}},
918respectively.  Since @code{Lisp_Object} can hold any Lisp object of
919any data type, you can determine the actual data type only at run
920time; so if you want a primitive to accept only a certain type of
921argument, you must check the type explicitly using a suitable
922predicate (@pxref{Type Predicates}).
923@cindex type checking internals
924
925@cindex garbage collection protection
926@cindex protect C variables from garbage collection
927  Within the function @code{For} itself, the local variable
928@code{args} refers to objects controlled by Emacs's stack-marking
929garbage collector.  Although the garbage collector does not reclaim
930objects reachable from C @code{Lisp_Object} stack variables, it may
931move some of the components of an object, such as the contents of a
932string or the text of a buffer.  Therefore, functions that access
933these components must take care to refetch their addresses after
934performing Lisp evaluation.  This means that instead of keeping C
935pointers to string contents or buffer text, the code should keep the
936buffer or string position, and recompute the C pointer from the
937position after performing Lisp evaluation.  Lisp evaluation can occur
938via calls to @code{eval_sub} or @code{Feval}, either directly or
939indirectly.
940
941@cindex @code{maybe_quit}, use in Lisp primitives
942  Note the call to @code{maybe_quit} inside the loop: this function
943checks whether the user pressed @kbd{C-g}, and if so, aborts the
944processing.  You should do that in any loop that can potentially
945require a large number of iterations; in this case, the list of
946arguments could be very long.  This increases Emacs responsiveness and
947improves user experience.
948
949  You must not use C initializers for static or global variables unless
950the variables are never written once Emacs is dumped.  These variables
951with initializers are allocated in an area of memory that becomes
952read-only (on certain operating systems) as a result of dumping Emacs.
953@xref{Pure Storage}.
954
955@cindex @code{defsubr}, Lisp symbol for a primitive
956  Defining the C function is not enough to make a Lisp primitive
957available; you must also create the Lisp symbol for the primitive and
958store a suitable subr object in its function cell.  The code looks like
959this:
960
961@example
962defsubr (&@var{sname});
963@end example
964
965@noindent
966Here @var{sname} is the name you used as the third argument to @code{DEFUN}.
967
968  If you add a new primitive to a file that already has Lisp primitives
969defined in it, find the function (near the end of the file) named
970@code{syms_of_@var{something}}, and add the call to @code{defsubr}
971there.  If the file doesn't have this function, or if you create a new
972file, add to it a @code{syms_of_@var{filename}} (e.g.,
973@code{syms_of_myfile}).  Then find the spot in @file{emacs.c} where all
974of these functions are called, and add a call to
975@code{syms_of_@var{filename}} there.
976
977@anchor{Defining Lisp variables in C}
978@vindex byte-boolean-vars
979@cindex defining Lisp variables in C
980@cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL}, @code{DEFSYM}
981  The function @code{syms_of_@var{filename}} is also the place to define
982any C variables that are to be visible as Lisp variables.
983@code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible
984in Lisp.  @code{DEFVAR_INT} makes a C variable of type @code{int}
985visible in Lisp with a value that is always an integer.
986@code{DEFVAR_BOOL} makes a C variable of type @code{int} visible in Lisp
987with a value that is either @code{t} or @code{nil}.  Note that variables
988defined with @code{DEFVAR_BOOL} are automatically added to the list
989@code{byte-boolean-vars} used by the byte compiler.
990
991  These macros all expect three arguments:
992
993@table @code
994@item lname
995The name of the variable to be used by Lisp programs.
996@item vname
997The name of the variable in the C sources.
998@item doc
999The documentation for the variable, as a C
1000comment.  @xref{Documentation Basics}, for more details.
1001@end table
1002
1003  By convention, when defining variables of a ``native'' type
1004(@code{int} and @code{bool}), the name of the C variable is the name
1005of the Lisp variable with @code{-} replaced by @code{_}.  When the
1006variable has type @code{Lisp_Object}, the convention is to also prefix
1007the C variable name with @code{V}.  i.e.
1008
1009@smallexample
1010DEFVAR_INT ("my-int-variable", my_int_variable,
1011           doc: /* An integer variable.  */);
1012
1013DEFVAR_LISP ("my-lisp-variable", Vmy_lisp_variable,
1014           doc: /* A Lisp variable.  */);
1015@end smallexample
1016
1017  There are situations in Lisp where you need to refer to the symbol
1018itself rather than the value of that symbol.  One such case is when
1019temporarily overriding the value of a variable, which in Lisp is done
1020with @code{let}.  In C sources, this is done by defining a
1021corresponding, constant symbol, and using @code{specbind}.  By
1022convention, @code{Qmy_lisp_variable} corresponds to
1023@code{Vmy_lisp_variable}; to define it, use the @code{DEFSYM} macro.
1024i.e.
1025
1026@smallexample
1027DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
1028@end smallexample
1029
1030  To perform the actual binding:
1031
1032@smallexample
1033specbind (Qmy_lisp_variable, Qt);
1034@end smallexample
1035
1036  In Lisp symbols sometimes need to be quoted, to achieve the same
1037effect in C you again use the corresponding constant symbol
1038@code{Qmy_lisp_variable}.  For example, when creating a buffer-local
1039variable (@pxref{Buffer-Local Variables}) in Lisp you would write:
1040
1041@smallexample
1042(make-variable-buffer-local 'my-lisp-variable)
1043@end smallexample
1044
1045In C the corresponding code uses @code{Fmake_variable_buffer_local} in
1046combination with @code{DEFSYM}, i.e.
1047
1048@smallexample
1049DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
1050Fmake_variable_buffer_local (Qmy_lisp_variable);
1051@end smallexample
1052
1053@cindex defining customization variables in C
1054  If you want to make a Lisp variable that is defined in C behave
1055like one declared with @code{defcustom}, add an appropriate entry to
1056@file{cus-start.el}.  @xref{Variable Definitions}, for a description of
1057the format to use.
1058
1059@cindex @code{staticpro}, protection from GC
1060  If you directly define a file-scope C variable of type
1061@code{Lisp_Object}, you must protect it from garbage-collection by
1062calling @code{staticpro} in @code{syms_of_@var{filename}}, like this:
1063
1064@example
1065staticpro (&@var{variable});
1066@end example
1067
1068  Here is another example function, with more complicated arguments.
1069This comes from the code in @file{window.c}, and it demonstrates the use
1070of macros and functions to manipulate Lisp objects.
1071
1072@smallexample
1073@group
1074DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1075       Scoordinates_in_window_p, 2, 2, 0,
1076       doc: /* Return non-nil if COORDINATES are in WINDOW.
1077  @dots{}
1078@end group
1079@group
1080  or `right-margin' is returned.  */)
1081  (register Lisp_Object coordinates, Lisp_Object window)
1082@{
1083  struct window *w;
1084  struct frame *f;
1085  int x, y;
1086  Lisp_Object lx, ly;
1087@end group
1088
1089@group
1090  w = decode_live_window (window);
1091  f = XFRAME (w->frame);
1092  CHECK_CONS (coordinates);
1093  lx = Fcar (coordinates);
1094  ly = Fcdr (coordinates);
1095  CHECK_NUMBER (lx);
1096  CHECK_NUMBER (ly);
1097  x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1098  y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1099@end group
1100
1101@group
1102  switch (coordinates_in_window (w, x, y))
1103    @{
1104    case ON_NOTHING:            /* NOT in window at all.  */
1105      return Qnil;
1106@end group
1107
1108    @dots{}
1109
1110@group
1111    case ON_MODE_LINE:          /* In mode line of window.  */
1112      return Qmode_line;
1113@end group
1114
1115    @dots{}
1116
1117@group
1118    case ON_SCROLL_BAR:         /* On scroll-bar of window.  */
1119      /* Historically we are supposed to return nil in this case.  */
1120      return Qnil;
1121@end group
1122
1123@group
1124    default:
1125      emacs_abort ();
1126    @}
1127@}
1128@end group
1129@end smallexample
1130
1131  Note that C code cannot call functions by name unless they are defined
1132in C@.  The way to call a function written in Lisp is to use
1133@code{Ffuncall}, which embodies the Lisp function @code{funcall}.  Since
1134the Lisp function @code{funcall} accepts an unlimited number of
1135arguments, in C it takes two: the number of Lisp-level arguments, and a
1136one-dimensional array containing their values.  The first Lisp-level
1137argument is the Lisp function to call, and the rest are the arguments to
1138pass to it.
1139
1140  The C functions @code{call0}, @code{call1}, @code{call2}, and so on,
1141provide handy ways to call a Lisp function conveniently with a fixed
1142number of arguments.  They work by calling @code{Ffuncall}.
1143
1144  @file{eval.c} is a very good file to look through for examples;
1145@file{lisp.h} contains the definitions for some important macros and
1146functions.
1147
1148  If you define a function which is side-effect free or pure, give it
1149a non-@code{nil} @code{side-effect-free} or @code{pure} property,
1150respectively (@pxref{Standard Properties}).
1151
1152@node Writing Dynamic Modules
1153@section Writing Dynamically-Loaded Modules
1154@cindex writing emacs modules
1155@cindex dynamic modules, writing
1156
1157@cindex module @acronym{API}
1158  This section describes the Emacs module @acronym{API} and how to use
1159it as part of writing extension modules for Emacs.  The module
1160@acronym{API} is defined in the C programming language, therefore the
1161description and the examples in this section assume the module is
1162written in C@.  For other programming languages, you will need to use
1163the appropriate bindings, interfaces and facilities for calling C code.
1164Emacs C code requires a C99 or later compiler (@pxref{C Dialect}), and
1165so the code examples in this section also follow that standard.
1166
1167Writing a module and integrating it into Emacs comprises the following
1168tasks:
1169
1170@itemize @bullet
1171@item
1172Writing initialization code for the module.
1173
1174@item
1175Writing one or more module functions.
1176
1177@item
1178Communicating values and objects between Emacs and your module
1179functions.
1180
1181@item
1182Handling of error conditions and nonlocal exits.
1183@end itemize
1184
1185@noindent
1186The following subsections describe these tasks and the @acronym{API}
1187itself in more detail.
1188
1189Once your module is written, compile it to produce a shared library,
1190according to the conventions of the underlying platform.  Then place
1191the shared library in a directory mentioned in @code{load-path}
1192(@pxref{Library Search}), where Emacs will find it.
1193
1194If you wish to verify the conformance of a module to the Emacs dynamic
1195module @acronym{API}, invoke Emacs with the @kbd{--module-assertions}
1196option.  @xref{Initial Options,,,emacs, The GNU Emacs Manual}.
1197
1198@menu
1199* Module Initialization::
1200* Module Functions::
1201* Module Values::
1202* Module Misc::
1203* Module Nonlocal::
1204@end menu
1205
1206@node Module Initialization
1207@subsection Module Initialization Code
1208@cindex module initialization
1209
1210  Begin your module by including the header file @file{emacs-module.h}
1211and defining the GPL compatibility symbol:
1212
1213@example
1214#include <emacs-module.h>
1215
1216int plugin_is_GPL_compatible;
1217@end example
1218
1219The @file{emacs-module.h} file is installed into your system's include
1220tree as part of the Emacs installation.  Alternatively, you can find
1221it in the Emacs source tree.
1222
1223@anchor{module initialization function}
1224Next, write an initialization function for the module.
1225
1226@deftypefn Function int emacs_module_init (struct emacs_runtime *@var{runtime})
1227Emacs calls this function when it loads a module.  If a module does
1228not export a function named @code{emacs_module_init}, trying to load
1229the module will signal an error.  The initialization function should
1230return zero if the initialization succeeds, non-zero otherwise.  In
1231the latter case, Emacs will signal an error, and the loading of the
1232module will fail.  If the user presses @kbd{C-g} during the
1233initialization, Emacs ignores the return value of the initialization
1234function and quits (@pxref{Quitting}).  (If needed, you can catch user
1235quitting inside the initialization function, @pxref{should_quit}.)
1236
1237The argument @var{runtime} is a pointer to a C @code{struct} that
1238includes 2 public fields: @code{size}, which provides the size of the
1239structure in bytes; and @code{get_environment}, which provides a
1240pointer to a function that allows the module initialization function
1241access to the Emacs environment object and its interfaces.
1242
1243The initialization function should perform whatever initialization is
1244required for the module.  In addition, it can perform the following
1245tasks:
1246
1247@table @asis
1248@cindex compatibility, between modules and Emacs
1249@item Compatibility verification
1250A module can verify that the Emacs executable which loads the module
1251is compatible with the module, by comparing the @code{size} member of
1252the @var{runtime} structure with the value compiled into the module:
1253
1254@example
1255int
1256emacs_module_init (struct emacs_runtime *runtime)
1257@{
1258  if (runtime->size < sizeof (*runtime))
1259    return 1;
1260@}
1261@end example
1262
1263@noindent
1264If the size of the runtime object passed to the module is smaller than
1265what it expects, it means the module was compiled for an Emacs version
1266newer (later) than the one which attempts to load it, i.e.@: the
1267module might be incompatible with the Emacs binary.
1268
1269In addition, a module can verify the compatibility of the module
1270@acronym{API} with what the module expects.  The following sample code
1271assumes it is part of the @code{emacs_module_init} function shown
1272above:
1273
1274@example
1275  emacs_env *env = runtime->get_environment (runtime);
1276  if (env->size < sizeof (*env))
1277    return 2;
1278@end example
1279
1280@noindent
1281@cindex module runtime environment
1282This calls the @code{get_environment} function using the pointer
1283provided in the @code{runtime} structure to retrieve a pointer to the
1284@acronym{API}'s @dfn{environment}, a C @code{struct} which also has a
1285@code{size} field holding the size of the structure in bytes.
1286
1287Finally, you can write a module that will work with older versions of
1288Emacs, by comparing the size of the environment passed by Emacs with
1289known sizes, like this:
1290
1291@example
1292  emacs_env *env = runtime->get_environment (runtime);
1293  if (env->size >= sizeof (struct emacs_env_26))
1294    emacs_version = 26;  /* Emacs 26 or later.  */
1295  else if (env->size >= sizeof (struct emacs_env_25))
1296    emacs_version = 25;
1297  else
1298    return 2; /* Unknown or unsupported version.  */
1299@end example
1300
1301@noindent
1302This works because later Emacs versions always @emph{add} members to
1303the environment, never @emph{remove} any members, so the size can only
1304grow with new Emacs releases.  Given the version of Emacs, the module
1305can use only the parts of the module @acronym{API} that existed in
1306that version, since those parts are identical in later versions.
1307
1308@file{emacs-module.h} defines a preprocessor macro
1309@code{EMACS_MAJOR_VERSION}.  It expands to an integer literal which is
1310the latest major version of Emacs supported by the header.
1311@xref{Version Info}.  Note that the value of
1312@code{EMACS_MAJOR_VERSION} is a compile-time constant and does not
1313represent the version of Emacs that is currently running and has
1314loaded your module.  If you want your module to be compatible with
1315various versions of @file{emacs-module.h} as well as various versions
1316of Emacs, you can use conditional compilation based on
1317@code{EMACS_MAJOR_VERSION}.
1318
1319We recommend that modules always perform the compatibility
1320verification, unless they do their job entirely in the initialization
1321function, and don't access any Lisp objects or use any Emacs functions
1322accessible through the environment structure.
1323
1324@item Binding module functions to Lisp symbols
1325This gives the module functions names so that Lisp code could call it
1326by that name.  We describe how to do this in @ref{Module Functions}
1327below.
1328@end table
1329@end deftypefn
1330
1331@node Module Functions
1332@subsection Writing Module Functions
1333@cindex writing module functions
1334@cindex module functions
1335
1336  The main reason for writing an Emacs module is to make additional
1337functions available to Lisp programs that load the module.  This
1338subsection describes how to write such @dfn{module functions}.
1339
1340A module function has the following general form and signature:
1341
1342@deftypefn Function emacs_value emacs_function (emacs_env *@var{env}, ptrdiff_t @var{nargs}, emacs_value *@var{args}, void *@var{data})
1343@tindex emacs_function
1344The @var{env} argument provides a pointer to the @acronym{API}
1345environment, needed to access Emacs objects and functions.  The
1346@var{nargs} argument is the required number of arguments, which can be
1347zero (see @code{make_function} below for more flexible specification
1348of the argument number), and @var{args} is a pointer to the array of
1349the function arguments.  The argument @var{data} points to additional
1350data required by the function, which was arranged when
1351@code{make_function} (see below) was called to create an Emacs
1352function from @code{emacs_function}.
1353
1354Module functions use the type @code{emacs_value} to communicate Lisp
1355objects between Emacs and the module (@pxref{Module Values}).  The
1356@acronym{API}, described below and in the following subsections,
1357provides facilities for conversion between basic C data types and the
1358corresponding @code{emacs_value} objects.
1359
1360A module function always returns a value.  If the function returns
1361normally, the Lisp code which called it will see the Lisp object
1362corresponding to the @code{emacs_value} value the function returned.
1363However, if the user typed @kbd{C-g}, or if the module function or its
1364callees signaled an error or exited nonlocally (@pxref{Module
1365Nonlocal}), Emacs will ignore the returned value and quit or throw as
1366it does when Lisp code encounters the same situations.
1367
1368The header @file{emacs-module.h} provides the type
1369@code{emacs_function} as an alias type for a function pointer to a
1370module function.
1371@end deftypefn
1372
1373After writing your C code for a module function, you should make a
1374Lisp function object from it using the @code{make_function} function,
1375whose pointer is provided in the environment (recall that the pointer
1376to the environment is returned by @code{get_environment}).  This is
1377normally done in the module initialization function (@pxref{module
1378initialization function}), after verifying the @acronym{API}
1379compatibility.
1380
1381@deftypefn Function emacs_value make_function (emacs_env *@var{env}, ptrdiff_t @var{min_arity}, ptrdiff_t @var{max_arity}, emacs_function @var{func}, const char *@var{docstring}, void *@var{data})
1382@vindex emacs_variadic_function
1383This returns an Emacs function created from the C function @var{func},
1384whose signature is as described for @code{emacs_function} above.
1385The arguments
1386@var{min_arity} and @var{max_arity} specify the minimum and maximum
1387number of arguments that @var{func} can accept.  The @var{max_arity}
1388argument can have the special value @code{emacs_variadic_function},
1389which makes the function accept an unlimited number of arguments, like
1390the @code{&rest} keyword in Lisp (@pxref{Argument List}).
1391
1392The argument @var{data} is a way to arrange for arbitrary additional
1393data to be passed to @var{func} when it is called.  Whatever pointer
1394is passed to @code{make_function} will be passed unaltered to
1395@var{func}.
1396
1397The argument @var{docstring} specifies the documentation string for
1398the function.  It should be either an @acronym{ASCII} string, or a
1399UTF-8 encoded non-@acronym{ASCII} string, or a @code{NULL} pointer; in
1400the latter case the function will have no documentation.  The
1401documentation string can end with a line that specifies the advertised
1402calling convention, see @ref{Function Documentation}.
1403
1404Since every module function must accept the pointer to the environment
1405as its first argument, the call to @code{make_function} could be made
1406from any module function, but you will normally want to do that from
1407the module initialization function, so that all the module functions
1408are known to Emacs once the module is loaded.
1409@end deftypefn
1410
1411Finally, you should bind the Lisp function to a symbol, so that Lisp
1412code could call your function by name.  For that, use the module
1413@acronym{API} function @code{intern} (@pxref{intern}) whose pointer is
1414also provided in the environment that module functions can access.
1415
1416Combining the above steps, code that arranges for a C function
1417@code{module_func} to be callable as @code{module-func} from Lisp will
1418look like this, as part of the module initialization function:
1419
1420@example
1421 emacs_env *env = runtime->get_environment (runtime);
1422 emacs_value func = env->make_function (env, min_arity, max_arity,
1423                                        module_func, docstring, data);
1424 emacs_value symbol = env->intern (env, "module-func");
1425 emacs_value args[] = @{symbol, func@};
1426 env->funcall (env, env->intern (env, "defalias"), 2, args);
1427@end example
1428
1429@noindent
1430This makes the symbol @code{module-func} known to Emacs by calling
1431@code{env->intern}, then invokes @code{defalias} from Emacs to bind
1432the function to that symbol.  Note that it is possible to use
1433@code{fset} instead of @code{defalias}; the differences are described
1434in @ref{Defining Functions, defalias}.
1435
1436Module functions including the @code{emacs_module_init} function
1437(@pxref{module initialization function}) may only interact with Emacs
1438by calling environment functions from some live @code{emacs_env}
1439pointer while being called directly or indirectly from Emacs.  In
1440other words, if a module function wants to call Lisp functions or
1441Emacs primitives, convert @code{emacs_value} objects to and from C
1442datatypes (@pxref{Module Values}), or interact with Emacs in any other
1443way, some call from Emacs to @code{emacs_module_init} or to a module
1444function must be in the call stack.  Module functions may not interact
1445with Emacs while garbage collection is running; @pxref{Garbage
1446Collection}.  They may only interact with Emacs from Lisp interpreter
1447threads (including the main thread) created by Emacs; @pxref{Threads}.
1448The @kbd{--module-assertions} command-line option can detect some
1449violations of the above requirements.  @xref{Initial Options,,,emacs,
1450The GNU Emacs Manual}.
1451
1452Using the module @acronym{API}, it is possible to define more complex
1453function and data types: inline functions, macros, etc.  However, the
1454resulting C code will be cumbersome and hard to read.  Therefore, we
1455recommend that you limit the module code which creates functions and
1456data structures to the absolute minimum, and leave the rest for a Lisp
1457package that will accompany your module, because doing these
1458additional tasks in Lisp is much easier, and will produce a much more
1459readable code.  For example, given a module function
1460@code{module-func} defined as above, one way of making a macro
1461@code{module-macro} based on it is with the following simple Lisp
1462wrapper:
1463
1464@lisp
1465(defmacro module-macro (&rest args)
1466  "Documentation string for the macro."
1467  (module-func args))
1468@end lisp
1469
1470The Lisp package which goes with your module could then load the
1471module using the @code{load} primitive (@pxref{Dynamic Modules}) when
1472the package is loaded into Emacs.
1473
1474By default, module functions created by @code{make_function} are not
1475interactive.  To make them interactive, you can use the following
1476function.
1477
1478@deftypefun void make_interactive (emacs_env *@var{env}, emacs_value @var{function}, emacs_value @var{spec})
1479This function, which is available since Emacs 28, makes the function
1480@var{function} interactive using the interactive specification
1481@var{spec}.  Emacs interprets @var{spec} like the argument to the
1482@code{interactive} form.  @ref{Using Interactive}, and
1483@pxref{Interactive Codes}.  @var{function} must be an Emacs module
1484function returned by @code{make_function}.
1485@end deftypefun
1486
1487Note that there is no native module support for retrieving the
1488interactive specification of a module function.  Use the function
1489@code{interactive-form} for that.  @ref{Using Interactive}.  It is not
1490possible to make a module function non-interactive once you have made
1491it interactive using @code{make_interactive}.
1492
1493@anchor{Module Function Finalizers}
1494If you want to run some code when a module function object (i.e., an
1495object returned by @code{make_function}) is garbage-collected, you can
1496install a @dfn{function finalizer}.  Function finalizers are available
1497since Emacs 28.  For example, if you have passed some heap-allocated
1498structure to the @var{data} argument of @code{make_function}, you can
1499use the finalizer to deallocate the structure.  @xref{Basic
1500Allocation,,,libc}, and @pxref{Freeing after Malloc,,,libc}.  The
1501finalizer function has the following signature:
1502
1503@example
1504void finalizer (void *@var{data})
1505@end example
1506
1507Here, @var{data} receives the value passed to @var{data} when calling
1508@code{make_function}.  Note that the finalizer can't interact with
1509Emacs in any way.
1510
1511Directly after calling @code{make_function}, the newly-created
1512function doesn't have a finalizer.  Use @code{set_function_finalizer}
1513to add one, if desired.
1514
1515@deftypefun void emacs_finalizer (void *@var{ptr})
1516The header @file{emacs-module.h} provides the type
1517@code{emacs_finalizer} as a type alias for an Emacs finalizer
1518function.
1519@end deftypefun
1520
1521@deftypefun emacs_finalizer get_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg})
1522This function, which is available since Emacs 28, returns the function
1523finalizer associated with the module function represented by
1524@var{arg}.  @var{arg} must refer to a module function, that is, an
1525object returned by @code{make_function}.  If no finalizer is
1526associated with the function, @code{NULL} is returned.
1527@end deftypefun
1528
1529@deftypefun void set_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg}, emacs_finalizer @var{fin})
1530This function, which is available since Emacs 28, sets the function
1531finalizer associated with the module function represented by @var{arg}
1532to @var{fin}.  @var{arg} must refer to a module function, that is, an
1533object returned by @code{make_function}.  @var{fin} can either be
1534@code{NULL} to clear @var{arg}'s function finalizer, or a pointer to a
1535function to be called when the object represented by @var{arg} is
1536garbage-collected.  At most one function finalizer can be set per
1537function; if @var{arg} already has a finalizer, it is replaced by
1538@var{fin}.
1539@end deftypefun
1540
1541@node Module Values
1542@subsection Conversion Between Lisp and Module Values
1543@cindex module values, conversion
1544
1545@cindex @code{emacs_value} data type
1546  With very few exceptions, most modules need to exchange data with
1547Lisp programs that call them: accept arguments to module functions and
1548return values from module functions.  For this purpose, the module
1549@acronym{API} provides the @code{emacs_value} type, which represents
1550Emacs Lisp objects communicated via the @acronym{API}; it is the
1551functional equivalent of the @code{Lisp_Object} type used in Emacs C
1552primitives (@pxref{Writing Emacs Primitives}).  This section describes
1553the parts of the module @acronym{API} that allow to create
1554@code{emacs_value} objects corresponding to basic Lisp data types, and
1555how to access from C data in @code{emacs_value} objects that
1556correspond to Lisp objects.
1557
1558All of the functions described below are actually @emph{function
1559pointers} provided via the pointer to the environment which every
1560module function accepts.  Therefore, module code should call these
1561functions through the environment pointer, like this:
1562
1563@example
1564emacs_env *env;  /* the environment pointer */
1565env->some_function (arguments@dots{});
1566@end example
1567
1568@noindent
1569The @code{emacs_env} pointer will usually come from the first argument
1570to the module function, or from the call to @code{get_environment} if
1571you need the environment in the module initialization function.
1572
1573Most of the functions described below became available in Emacs 25,
1574the first Emacs release that supported dynamic modules.  For the few
1575functions that became available in later Emacs releases, we mention
1576the first Emacs version that supported them.
1577
1578The following @acronym{API} functions extract values of various C data
1579types from @code{emacs_value} objects.  They all raise the
1580@code{wrong-type-argument} error condition (@pxref{Type Predicates})
1581if the argument @code{emacs_value} object is not of the type expected
1582by the function.  @xref{Module Nonlocal}, for details of how signaling
1583errors works in Emacs modules, and how to catch error conditions
1584inside the module before they are reported to Emacs.  The
1585@acronym{API} function @code{type_of} (@pxref{Module Misc, type_of})
1586can be used to obtain the type of a @code{emacs_value} object.
1587
1588@deftypefn Function intmax_t extract_integer (emacs_env *@var{env}, emacs_value @var{arg})
1589This function returns the value of a Lisp integer specified by
1590@var{arg}.  The C data type of the return value, @code{intmax_t}, is
1591the widest integer data type supported by the C compiler, typically
1592@w{@code{long long}}.  If the value of @var{arg} doesn't fit into an
1593@code{intmax_t}, the function signals an error using the error symbol
1594@code{overflow-error}.
1595@end deftypefn
1596
1597@deftypefn Function bool extract_big_integer (emacs_env *@var{env}, emacs_value @var{arg}, int *@var{sign}, ptrdiff_t *@var{count}, emacs_limb_t *@var{magnitude})
1598This function, which is available since Emacs 27, extracts the
1599integer value of @var{arg}.  The value of @var{arg} must be an
1600integer (fixnum or bignum).  If @var{sign} is not @code{NULL}, it
1601stores the sign of @var{arg} (-1, 0, or +1) into @code{*sign}.  The
1602magnitude is stored into @var{magnitude} as follows.  If @var{count}
1603and @var{magnitude} are both non-@code{NULL}, then @var{magnitude} must
1604point to an array of at least @code{*count} @code{unsigned long}
1605elements.  If @var{magnitude} is large enough to hold the magnitude of
1606@var{arg}, then this function writes the magnitude into the
1607@var{magnitude} array in little-endian form, stores the number of
1608array elements written into @code{*count}, and returns @code{true}.
1609If @var{magnitude} is not large enough, it stores the required array
1610size into @code{*count}, signals an error, and returns @code{false}.
1611If @var{count} is not @code{NULL} and @var{magnitude} is @code{NULL},
1612then the function stores the required array size into @code{*count}
1613and returns @code{true}.
1614
1615Emacs guarantees that the maximum required value of @code{*count}
1616never exceeds @code{min (PTRDIFF_MAX, SIZE_MAX) / sizeof
1617(emacs_limb_t)}, so you can use @code{malloc (*count * sizeof *magnitude)}
1618to allocate the @code{magnitude} array without worrying about integer
1619overflow in the size calculation.
1620@end deftypefn
1621
1622@deftp {Type alias} emacs_limb_t
1623This is an unsigned integer type, used as the element type for the
1624magnitude arrays for the big integer conversion functions.  The type
1625is guaranteed to have unique object representations, i.e., no padding
1626bits.
1627@end deftp
1628
1629@defvr Macro EMACS_LIMB_MAX
1630This macro expands to a constant expression specifying the maximum
1631possible value for an @code{emacs_limb_t} object.
1632The expression is suitable for use in @code{#if}.
1633@end defvr
1634
1635@deftypefn Function double extract_float (emacs_env *@var{env}, emacs_value @var{arg})
1636This function returns the value of a Lisp float specified by
1637@var{arg}, as a C @code{double} value.
1638@end deftypefn
1639
1640@deftypefn Function struct timespec extract_time (emacs_env *@var{env}, emacs_value @var{arg})
1641This function, which is available since Emacs 27, interprets @var{arg}
1642as an Emacs Lisp time value and returns the corresponding @code{struct
1643timespec}.  @xref{Time of Day}.  @code{struct timespec} represents a
1644timestamp with nanosecond precision.  It has the following members:
1645
1646@table @code
1647@item time_t tv_sec
1648Whole number of seconds.
1649@item long tv_nsec
1650Fractional seconds as a number of nanoseconds.
1651For timestamps returned by @code{extract_time},
1652this is always nonnegative and less than one billion.
1653(Although POSIX requires the type of @code{tv_nsec} to be @code{long},
1654the type is @code{long long} on some nonstandard platforms.)
1655@end table
1656
1657@noindent
1658@xref{Elapsed Time,,,libc}.
1659
1660If @var{time} has higher precision than nanoseconds, then this
1661function truncates it to nanosecond precision towards negative
1662infinity.  This function signals an error if @var{time} (truncated to
1663nanoseconds) cannot be represented by @code{struct timespec}.  For
1664example, if @code{time_t} is a 32-bit integer type, then a @var{time}
1665value of ten billion seconds would signal an error, but a @var{time}
1666value of 600 picoseconds would get truncated to zero.
1667
1668If you need to deal with time values that are not representable by
1669@code{struct timespec}, or if you want higher precision, call the Lisp
1670function @code{encode-time} and work with its return value.
1671@xref{Time Conversion}.
1672@end deftypefn
1673
1674@deftypefn Function bool copy_string_contents (emacs_env *@var{env}, emacs_value @var{arg}, char *@var{buf}, ptrdiff_t *@var{len})
1675This function stores the UTF-8 encoded text of a Lisp string specified
1676by @var{arg} in the array of @code{char} pointed by @var{buf}, which
1677should have enough space to hold at least @code{*@var{len}} bytes,
1678including the terminating null byte.  The argument @var{len} must not
1679be a @code{NULL} pointer, and, when the function is called, it should
1680point to a value that specifies the size of @var{buf} in bytes.
1681
1682If the buffer size specified by @code{*@var{len}} is large enough to
1683hold the string's text, the function stores in @code{*@var{len}} the
1684actual number of bytes copied to @var{buf}, including the terminating
1685null byte, and returns @code{true}.  If the buffer is too small, the
1686function raises the @code{args-out-of-range} error condition, stores
1687the required number of bytes in @code{*@var{len}}, and returns
1688@code{false}.  @xref{Module Nonlocal}, for how to handle pending error
1689conditions.
1690
1691The argument @var{buf} can be a @code{NULL} pointer, in which case the
1692function stores in @code{*@var{len}} the number of bytes required for
1693storing the contents of @var{arg}, and returns @code{true}.  This is
1694how you can determine the size of @var{buf} needed to store a
1695particular string: first call @code{copy_string_contents} with
1696@code{NULL} as @var{buf}, then allocate enough memory to hold the
1697number of bytes stored by the function in @code{*@var{len}}, and call
1698the function again with non-@code{NULL} @var{buf} to actually perform
1699the text copying.
1700@end deftypefn
1701
1702@deftypefn Function emacs_value vec_get (emacs_env *@var{env}, emacs_value @var{vector}, ptrdiff_t @var{index})
1703This function returns the element of @var{vector} at @var{index}.  The
1704@var{index} of the first vector element is zero.  The function raises
1705the @code{args-out-of-range} error condition if the value of
1706@var{index} is invalid.  To extract C data from the value the function
1707returns, use the other extraction functions described here, as
1708appropriate for the Lisp data type stored in that element of the
1709vector.
1710@end deftypefn
1711
1712@deftypefn Function ptrdiff_t vec_size (emacs_env *@var{env}, emacs_value @var{vector})
1713This function returns the number of elements in @var{vector}.
1714@end deftypefn
1715
1716@deftypefn Function void vec_set (emacs_env *@var{env}, emacs_value @var{vector}, ptrdiff_t @var{index}, emacs_value @var{value})
1717This function stores @var{value} in the element of @var{vector} whose
1718index is @var{index}.  It raises the @code{args-out-of-range} error
1719condition if the value of @var{index} is invalid.
1720@end deftypefn
1721
1722The following @acronym{API} functions create @code{emacs_value}
1723objects from basic C data types.  They all return the created
1724@code{emacs_value} object.
1725
1726@deftypefn Function emacs_value make_integer (emacs_env *@var{env}, intmax_t @var{n})
1727This function takes an integer argument @var{n} and returns the
1728corresponding @code{emacs_value} object.  It returns either a fixnum
1729or a bignum depending on whether the value of @var{n} is inside the
1730limits set by @code{most-negative-fixnum} and
1731@code{most-positive-fixnum} (@pxref{Integer Basics}).
1732@end deftypefn
1733
1734@deftypefn Function emacs_value make_big_integer (emacs_env *@var{env}, int sign, ptrdiff_t count, const emacs_limb_t *magnitude)
1735This function, which is available since Emacs 27, takes an
1736arbitrary-sized integer argument and returns a corresponding
1737@code{emacs_value} object.  The @var{sign} argument gives the sign of
1738the return value.  If @var{sign} is nonzero, then @var{magnitude} must
1739point to an array of at least @var{count} elements specifying the
1740little-endian magnitude of the return value.
1741@end deftypefn
1742
1743The following example uses the GNU Multiprecision Library (GMP) to
1744calculate the next probable prime after a given integer.
1745@xref{Top,,,gmp}, for a general overview of GMP, and @pxref{Integer
1746Import and Export,,,gmp} for how to convert the @code{magnitude} array
1747to and from GMP @code{mpz_t} values.
1748
1749@example
1750#include <emacs-module.h>
1751int plugin_is_GPL_compatible;
1752
1753#include <assert.h>
1754#include <limits.h>
1755#include <stdint.h>
1756#include <stdlib.h>
1757#include <string.h>
1758
1759#include <gmp.h>
1760
1761static void
1762memory_full (emacs_env *env)
1763@{
1764  static const char message[] = "Memory exhausted";
1765  emacs_value data = env->make_string (env, message,
1766                                       strlen (message));
1767  env->non_local_exit_signal
1768    (env, env->intern (env, "error"),
1769     env->funcall (env, env->intern (env, "list"), 1, &data));
1770@}
1771
1772enum
1773@{
1774  order = -1, endian = 0, nails = 0,
1775  limb_size = sizeof (emacs_limb_t),
1776  max_nlimbs = ((SIZE_MAX < PTRDIFF_MAX ? SIZE_MAX : PTRDIFF_MAX)
1777                / limb_size)
1778@};
1779
1780static bool
1781extract_big_integer (emacs_env *env, emacs_value arg, mpz_t result)
1782@{
1783  ptrdiff_t nlimbs;
1784  bool ok = env->extract_big_integer (env, arg, NULL, &nlimbs, NULL);
1785  if (!ok)
1786    return false;
1787  assert (0 < nlimbs && nlimbs <= max_nlimbs);
1788  emacs_limb_t *magnitude = malloc (nlimbs * limb_size);
1789  if (magnitude == NULL)
1790    @{
1791      memory_full (env);
1792      return false;
1793    @}
1794  int sign;
1795  ok = env->extract_big_integer (env, arg, &sign, &nlimbs, magnitude);
1796  assert (ok);
1797  mpz_import (result, nlimbs, order, limb_size, endian, nails, magnitude);
1798  free (magnitude);
1799  if (sign < 0)
1800    mpz_neg (result, result);
1801  return true;
1802@}
1803
1804static emacs_value
1805make_big_integer (emacs_env *env, const mpz_t value)
1806@{
1807  size_t nbits = mpz_sizeinbase (value, 2);
1808  int bitsperlimb = CHAR_BIT * limb_size - nails;
1809  size_t nlimbs = nbits / bitsperlimb + (nbits % bitsperlimb != 0);
1810  emacs_limb_t *magnitude
1811    = nlimbs <= max_nlimbs ? malloc (nlimbs * limb_size) : NULL;
1812  if (magnitude == NULL)
1813    @{
1814      memory_full (env);
1815      return NULL;
1816    @}
1817  size_t written;
1818  mpz_export (magnitude, &written, order, limb_size, endian, nails, value);
1819  assert (written == nlimbs);
1820  assert (nlimbs <= PTRDIFF_MAX);
1821  emacs_value result = env->make_big_integer (env, mpz_sgn (value),
1822                                              nlimbs, magnitude);
1823  free (magnitude);
1824  return result;
1825@}
1826
1827static emacs_value
1828next_prime (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
1829            void *data)
1830@{
1831  assert (nargs == 1);
1832  mpz_t p;
1833  mpz_init (p);
1834  extract_big_integer (env, args[0], p);
1835  mpz_nextprime (p, p);
1836  emacs_value result = make_big_integer (env, p);
1837  mpz_clear (p);
1838  return result;
1839@}
1840
1841int
1842emacs_module_init (struct emacs_runtime *runtime)
1843@{
1844  emacs_env *env = runtime->get_environment (runtime);
1845  emacs_value symbol = env->intern (env, "next-prime");
1846  emacs_value func
1847    = env->make_function (env, 1, 1, next_prime, NULL, NULL);
1848  emacs_value args[] = @{symbol, func@};
1849  env->funcall (env, env->intern (env, "defalias"), 2, args);
1850  return 0;
1851@}
1852@end example
1853
1854@deftypefn Function emacs_value make_float (emacs_env *@var{env}, double @var{d})
1855This function takes a @code{double} argument @var{d} and returns the
1856corresponding Emacs floating-point value.
1857@end deftypefn
1858
1859@deftypefn Function emacs_value make_time (emacs_env *@var{env}, struct timespec @var{time})
1860This function, which is available since Emacs 27, takes a @code{struct
1861timespec} argument @var{time} and returns the corresponding Emacs
1862timestamp as a pair @code{(@var{ticks} . @var{hz})}.  @xref{Time of
1863Day}.  The return value represents exactly the same timestamp as
1864@var{time}: all input values are representable, and there is never a
1865loss of precision.  @code{@var{time}.tv_sec} and
1866@code{@var{time}.tv_nsec} can be arbitrary values.  In particular,
1867there's no requirement that @var{time} be normalized.  This means that
1868@code{@var{time}.tv_nsec} can be negative or larger than 999,999,999.
1869@end deftypefn
1870
1871@deftypefn Function emacs_value make_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{len})
1872This function creates an Emacs string from C text string pointed by
1873@var{str} whose length in bytes, not including the terminating null
1874byte, is @var{len}.  The original string in @var{str} can be either an
1875@acronym{ASCII} string or a UTF-8 encoded non-@acronym{ASCII} string;
1876it can include embedded null bytes, and doesn't have to end in a
1877terminating null byte at @code{@var{str}[@var{len}]}.  The function
1878raises the @code{overflow-error} error condition if @var{len} is
1879negative or exceeds the maximum length of an Emacs string.  If
1880@var{len} is zero, then @var{str} can be @code{NULL}, otherwise it
1881must point to valid memory.  For nonzero @var{len}, @code{make_string}
1882returns unique mutable string objects.
1883@end deftypefn
1884
1885@deftypefn Function emacs_value make_unibyte_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{len})
1886This function is like @code{make_string}, but has no restrictions on
1887the values of the bytes in the C string, and can be used to pass
1888binary data to Emacs in the form of a unibyte string.
1889@end deftypefn
1890
1891The @acronym{API} does not provide functions to manipulate Lisp data
1892structures, for example, create lists with @code{cons} and @code{list}
1893(@pxref{Building Lists}), extract list members with @code{car} and
1894@code{cdr} (@pxref{List Elements}), create vectors with @code{vector}
1895(@pxref{Vector Functions}), etc.  For these, use @code{intern} and
1896@code{funcall}, described in the next subsection, to call the
1897corresponding Lisp functions.
1898
1899Normally, @code{emacs_value} objects have a rather short lifetime: it
1900ends when the @code{emacs_env} pointer used for their creation goes
1901out of scope.  Occasionally, you may need to create @dfn{global
1902references}: @code{emacs_value} objects that live as long as you
1903wish.  Use the following two functions to manage such objects.
1904
1905@deftypefn Function emacs_value make_global_ref (emacs_env *@var{env}, emacs_value @var{value})
1906This function returns a global reference for @var{value}.
1907@end deftypefn
1908
1909@deftypefn Function void free_global_ref (emacs_env *@var{env}, emacs_value @var{global_value})
1910This function frees the @var{global_value} previously created by
1911@code{make_global_ref}.  The @var{global_value} is no longer valid
1912after the call.  Your module code should pair each call to
1913@code{make_global_ref} with the corresponding @code{free_global_ref}.
1914@end deftypefn
1915
1916@cindex user pointer, using in module functions
1917An alternative to keeping around C data structures that need to be
1918passed to module functions later is to create @dfn{user pointer}
1919objects.  A user pointer, or @code{user-ptr}, object is a Lisp object
1920that encapsulates a C pointer and can have an associated finalizer
1921function, which is called when the object is garbage-collected
1922(@pxref{Garbage Collection}).  The module @acronym{API} provides
1923functions to create and access @code{user-ptr} objects.  These
1924functions raise the @code{wrong-type-argument} error condition if they
1925are called on @code{emacs_value} that doesn't represent a
1926@code{user-ptr} object.
1927
1928@deftypefn Function emacs_value make_user_ptr (emacs_env *@var{env}, emacs_finalizer @var{fin}, void *@var{ptr})
1929This function creates and returns a @code{user-ptr} object which wraps
1930the C pointer @var{ptr}.  The finalizer function @var{fin} can be a
1931@code{NULL} pointer (meaning no finalizer), or it can be a function of
1932the following signature:
1933
1934@example
1935typedef void (*emacs_finalizer) (void *@var{ptr});
1936@end example
1937
1938@noindent
1939If @var{fin} is not a @code{NULL} pointer, it will be called with the
1940@var{ptr} as the argument when the @code{user-ptr} object is
1941garbage-collected.  Don't run any expensive code in a finalizer,
1942because GC must finish quickly to keep Emacs responsive.
1943@end deftypefn
1944
1945@deftypefn Function void *get_user_ptr (emacs_env *@var{env}, emacs_value @var{arg})
1946This function extracts the C pointer from the Lisp object represented
1947by @var{arg}.
1948@end deftypefn
1949
1950@deftypefn Function void set_user_ptr (emacs_env *@var{env}, emacs_value @var{arg}, void *@var{ptr})
1951This function sets the C pointer embedded in the @code{user-ptr}
1952object represented by @var{arg} to @var{ptr}.
1953@end deftypefn
1954
1955@deftypefn Function emacs_finalizer get_user_finalizer (emacs_env *@var{env}, emacs_value @var{arg})
1956This function returns the finalizer of the @code{user-ptr} object
1957represented by @var{arg}, or @code{NULL} if it doesn't have a
1958finalizer.
1959@end deftypefn
1960
1961@deftypefn Function void set_user_finalizer (emacs_env *@var{env}, emacs_value @var{arg}, emacs_finalizer @var{fin})
1962This function changes the finalizer of the @code{user-ptr} object
1963represented by @var{arg} to be @var{fin}.  If @var{fin} is a
1964@code{NULL} pointer, the @code{user-ptr} object will have no
1965finalizer.
1966@end deftypefn
1967
1968Note that the @code{emacs_finalizer} type works for both user pointer
1969an module function finalizers.  @xref{Module Function Finalizers}.
1970
1971@node Module Misc
1972@subsection Miscellaneous Convenience Functions for Modules
1973
1974  This subsection describes a few convenience functions provided by
1975the module @acronym{API}.  Like the functions described in previous
1976subsections, all of them are actually function pointers, and need to
1977be called via the @code{emacs_env} pointer.  Description of functions
1978that were introduced after Emacs 25 calls out the first version where
1979they became available.
1980
1981@deftypefn Function bool eq (emacs_env *@var{env}, emacs_value @var{a}, emacs_value @var{b})
1982This function returns @code{true} if the Lisp objects represented by
1983@var{a} and @var{b} are identical, @code{false} otherwise.  This is
1984the same as the Lisp function @code{eq} (@pxref{Equality Predicates}),
1985but avoids the need to intern the objects represented by the
1986arguments.
1987
1988There are no @acronym{API} functions for other equality predicates, so
1989you will need to use @code{intern} and @code{funcall}, described
1990below, to perform more complex equality tests.
1991@end deftypefn
1992
1993@deftypefn Function bool is_not_nil (emacs_env *@var{env}, emacs_value @var{arg})
1994This function tests whether the Lisp object represented by @var{arg}
1995is non-@code{nil}; it returns @code{true} or @code{false} accordingly.
1996
1997Note that you could implement an equivalent test by using
1998@code{intern} to get an @code{emacs_value} representing @code{nil},
1999then use @code{eq}, described above, to test for equality.  But using
2000this function is more convenient.
2001@end deftypefn
2002
2003@deftypefn Function emacs_value type_of (emacs_env *@var{env}, emacs_value @code{arg})
2004This function returns the type of @var{arg} as a value that represents
2005a symbol: @code{string} for a string, @code{integer} for an integer,
2006@code{process} for a process, etc.  @xref{Type Predicates}.  You can
2007use @code{intern} and @code{eq} to compare against known type symbols,
2008if your code needs to depend on the object type.
2009@end deftypefn
2010
2011@anchor{intern}
2012@deftypefn Function emacs_value intern (emacs_env *@var{env}, const char *name)
2013This function returns an interned Emacs symbol whose name is
2014@var{name}, which should be an @acronym{ASCII} null-terminated string.
2015It creates a new symbol if one does not already exist.
2016
2017Together with @code{funcall}, described below, this function provides
2018a means for invoking any Lisp-callable Emacs function, provided that
2019its name is a pure @acronym{ASCII} string.  For example, here's how to
2020intern a symbol whose name @code{name_str} is non-@acronym{ASCII}, by
2021calling the more powerful Emacs @code{intern} function
2022(@pxref{Creating Symbols}):
2023
2024@example
2025emacs_value fintern = env->intern (env, "intern");
2026emacs_value sym_name =
2027  env->make_string (env, name_str, strlen (name_str));
2028emacs_value symbol = env->funcall (env, fintern, 1, &sym_name);
2029@end example
2030
2031@end deftypefn
2032
2033@deftypefn Function emacs_value funcall (emacs_env *@var{env}, emacs_value @var{func}, ptrdiff_t @var{nargs}, emacs_value *@var{args})
2034This function calls the specified @var{func} passing it @var{nargs}
2035arguments from the array pointed to by @var{args}.  The argument
2036@var{func} can be a function symbol (e.g., returned by @code{intern}
2037described above), a module function returned by @code{make_function}
2038(@pxref{Module Functions}), a subroutine written in C, etc.  If
2039@var{nargs} is zero, @var{args} can be a @code{NULL} pointer.
2040
2041The function returns the value that @var{func} returned.
2042@end deftypefn
2043
2044If your module includes potentially long-running code, it is a good
2045idea to check from time to time in that code whether the user wants to
2046quit, e.g., by typing @kbd{C-g} (@pxref{Quitting}).  The following
2047function, which is available since Emacs 26.1, is provided for that
2048purpose.
2049
2050@anchor{should_quit}
2051@deftypefn Function bool should_quit (emacs_env *@var{env})
2052This function returns @code{true} if the user wants to quit.  In that
2053case, we recommend that your module function aborts any on-going
2054processing and returns as soon as possible.  In most cases, use
2055@code{process_input} instead.
2056@end deftypefn
2057
2058To process input events in addition to checking whether the user wants
2059to quit, use the following function, which is available since Emacs
206027.1.
2061
2062@anchor{process_input}
2063@deftypefn Function enum emacs_process_input_result process_input (emacs_env *@var{env})
2064This function processes pending input events.  It returns
2065@code{emacs_process_input_quit} if the user wants to quit or an error
2066occurred while processing signals.  In that case, we recommend that
2067your module function aborts any on-going processing and returns as
2068soon as possible.  If the module code may continue running,
2069@code{process_input} returns @code{emacs_process_input_continue}.  The
2070return value is @code{emacs_process_input_continue} if and only if
2071there is no pending nonlocal exit in @code{env}.  If the module
2072continues after calling @code{process_input}, global state such as
2073variable values and buffer content may have been modified in arbitrary
2074ways.
2075@end deftypefn
2076
2077@anchor{open_channel}
2078@deftypefun int open_channel (emacs_env *@var{env}, emacs_value @var{pipe_process})
2079This function, which is available since Emacs 28, opens a channel to
2080an existing pipe process.  @var{pipe_process} must refer to an
2081existing pipe process created by @code{make-pipe-process}.  @ref{Pipe
2082Processes}.  If successful, the return value will be a new file
2083descriptor that you can use to write to the pipe.  Unlike all other
2084module functions, you can use the returned file descriptor from
2085arbitrary threads, even if no module environment is active.  You can
2086use the @code{write} function to write to the file descriptor.  Once
2087done, close the file descriptor using @code{close}.  @ref{Low-Level
2088I/O,,,libc}.
2089@end deftypefun
2090
2091@node Module Nonlocal
2092@subsection Nonlocal Exits in Modules
2093@cindex nonlocal exits, in modules
2094
2095  Emacs Lisp supports nonlocal exits, whereby program control is
2096transferred from one point in a program to another remote point.
2097@xref{Nonlocal Exits}.  Thus, Lisp functions called by your module
2098might exit nonlocally by calling @code{signal} or @code{throw}, and
2099your module functions must handle such nonlocal exits properly.  Such
2100handling is needed because C programs will not automatically release
2101resources and perform other cleanups in these cases; your module code
2102must itself do it.  The module @acronym{API} provides facilities for
2103that, described in this subsection.  They are generally available
2104since Emacs 25; those of them that became available in later releases
2105explicitly call out the first Emacs version where they became part of
2106the @acronym{API}.
2107
2108When some Lisp code called by a module function signals an error or
2109throws, the nonlocal exit is trapped, and the pending exit and its
2110associated data are stored in the environment.  Whenever a nonlocal
2111exit is pending in the environment, any module @acronym{API} function
2112called with a pointer to that environment will return immediately
2113without any processing (the functions @code{non_local_exit_check},
2114@code{non_local_exit_get}, and @code{non_local_exit_clear} are
2115exceptions from this rule).  If your module function then does nothing
2116and returns to Emacs, a pending nonlocal exit will cause Emacs to act
2117on it: signal an error or throw to the corresponding @code{catch}.
2118
2119So the simplest ``handling'' of nonlocal exits in module functions is
2120to do nothing special and let the rest of your code to run as if
2121nothing happened.  However, this can cause two classes of problems:
2122
2123@itemize @minus
2124@item
2125Your module function might use uninitialized or undefined values,
2126since @acronym{API} functions return immediately without producing the
2127expected results.
2128
2129@item
2130Your module might leak resources, because it might not have the
2131opportunity to release them.
2132@end itemize
2133
2134Therefore, we recommend that your module functions check for nonlocal
2135exit conditions and recover from them, using the functions described
2136below.
2137
2138@deftypefn Function enum emacs_funcall_exit non_local_exit_check (emacs_env *@var{env})
2139This function returns the kind of nonlocal exit condition stored in
2140@var{env}.  The possible values are:
2141
2142@vindex emacs_funcall_exit@r{, enumeration}
2143@vtable @code
2144@item emacs_funcall_exit_return
2145The last @acronym{API} function exited normally.
2146@item emacs_funcall_exit_signal
2147The last @acronym{API} function signaled an error.
2148@item emacs_funcall_exit_throw
2149The last @acronym{API} function exited via @code{throw}.
2150@end vtable
2151@end deftypefn
2152
2153@deftypefn Function enum emacs_funcall_exit non_local_exit_get (emacs_env *@var{env}, emacs_value *@var{symbol}, emacs_value *@var{data})
2154This function returns the kind of nonlocal exit condition stored in
2155@var{env}, like @code{non_local_exit_check} does, but it also returns
2156the full information about the nonlocal exit, if any.  If the return
2157value is @code{emacs_funcall_exit_signal}, the function stores the
2158error symbol in @code{*@var{symbol}} and the error data in
2159@code{*@var{data}} (@pxref{Signaling Errors}).  If the return value is
2160@code{emacs_funcall_exit_throw}, the function stores the @code{catch}
2161tag symbol in @code{*@var{symbol}} and the @code{throw} value in
2162@code{*@var{data}}.  The function doesn't store anything in memory
2163pointed by these arguments when the return value is
2164@code{emacs_funcall_exit_return}.
2165@end deftypefn
2166
2167You should check nonlocal exit conditions where it matters: before you
2168allocated some resource or after you allocated a resource that might
2169need freeing, or where a failure means further processing is
2170impossible or infeasible.
2171
2172Once your module function detected that a nonlocal exit is pending, it
2173can either return to Emacs (after performing the necessary local
2174cleanup), or it can attempt to recover from the nonlocal exit.  The
2175following @acronym{API} functions will help with these tasks.
2176
2177@deftypefn Function void non_local_exit_clear (emacs_env *@var{env})
2178This function clears the pending nonlocal exit conditions and data
2179from @var{env}.  After calling it, the module @acronym{API} functions
2180will work normally.  Use this function if your module function can
2181recover from nonlocal exits of the Lisp functions it calls and
2182continue, and also before calling any of the following two functions
2183(or any other @acronym{API} functions, if you want them to perform
2184their intended processing when a nonlocal exit is pending).
2185@end deftypefn
2186
2187@deftypefn Function void non_local_exit_throw (emacs_env *@var{env}, emacs_value @var{tag}, emacs_value @var{value})
2188This function throws to the Lisp @code{catch} symbol represented by
2189@var{tag}, passing it @var{value} as the value to return.  Your module
2190function should in general return soon after calling this function.
2191One use of this function is when you want to re-throw a non-local exit
2192from one of the called @acronym{API} or Lisp functions.
2193@end deftypefn
2194
2195@deftypefn Function void non_local_exit_signal (emacs_env *@var{env}, emacs_value @var{symbol}, emacs_value @var{data})
2196This function signals the error represented by the error symbol
2197@var{symbol} with the specified error data @var{data}.  The module
2198function should return soon after calling this function.  This
2199function could be useful, e.g., for signaling errors from module
2200functions to Emacs.
2201@end deftypefn
2202
2203
2204@node Object Internals
2205@section Object Internals
2206@cindex object internals
2207
2208  Emacs Lisp provides a rich set of the data types.  Some of them, like cons
2209cells, integers and strings, are common to nearly all Lisp dialects.  Some
2210others, like markers and buffers, are quite special and needed to provide
2211the basic support to write editor commands in Lisp.  To implement such
2212a variety of object types and provide an efficient way to pass objects between
2213the subsystems of an interpreter, there is a set of C data structures and
2214a special type to represent the pointers to all of them, which is known as
2215@dfn{tagged pointer}.
2216
2217  In C, the tagged pointer is an object of type @code{Lisp_Object}.  Any
2218initialized variable of such a type always holds the value of one of the
2219following basic data types: integer, symbol, string, cons cell, float,
2220or vectorlike object.  Each of these data types has the
2221corresponding tag value.  All tags are enumerated by @code{enum Lisp_Type}
2222and placed into a 3-bit bitfield of the @code{Lisp_Object}.  The rest of the
2223bits is the value itself.  Integers are immediate, i.e., directly
2224represented by those @dfn{value bits}, and all other objects are represented
2225by the C pointers to a corresponding object allocated from the heap.  Width
2226of the @code{Lisp_Object} is platform- and configuration-dependent: usually
2227it's equal to the width of an underlying platform pointer (i.e., 32-bit on
2228a 32-bit machine and 64-bit on a 64-bit one), but also there is a special
2229configuration where @code{Lisp_Object} is 64-bit but all pointers are 32-bit.
2230The latter trick was designed to overcome the limited range of values for
2231Lisp integers on a 32-bit system by using 64-bit @code{long long} type for
2232@code{Lisp_Object}.
2233
2234  The following C data structures are defined in @file{lisp.h} to represent
2235the basic data types beyond integers:
2236
2237@table @code
2238@item struct Lisp_Cons
2239Cons cell, an object used to construct lists.
2240
2241@item struct Lisp_String
2242String, the basic object to represent a sequence of characters.
2243
2244@item struct Lisp_Vector
2245Array, a fixed-size set of Lisp objects which may be accessed by an index.
2246
2247@item struct Lisp_Symbol
2248Symbol, the unique-named entity commonly used as an identifier.
2249
2250@item struct Lisp_Float
2251Floating-point value.
2252@end table
2253
2254  These types are the first-class citizens of an internal type system.
2255Since the tag space is limited, all other types are the subtypes of
2256@code{Lisp_Vectorlike}.  Vector subtypes are enumerated
2257by @code{enum pvec_type}, and nearly all complex objects like windows, buffers,
2258frames, and processes fall into this category.
2259
2260  Below there is a description of a few subtypes of @code{Lisp_Vectorlike}.
2261Buffer object represents the text to display and edit.  Window is the part
2262of display structure which shows the buffer or is used as a container to
2263recursively place other windows on the same frame.  (Do not confuse Emacs Lisp
2264window object with the window as an entity managed by the user interface
2265system like X; in Emacs terminology, the latter is called frame.)  Finally,
2266process object is used to manage the subprocesses.
2267
2268@menu
2269* Buffer Internals::    Components of a buffer structure.
2270* Window Internals::    Components of a window structure.
2271* Process Internals::   Components of a process structure.
2272@end menu
2273
2274@node Buffer Internals
2275@subsection Buffer Internals
2276@cindex internals, of buffer
2277@cindex buffer internals
2278
2279  Two structures (see @file{buffer.h}) are used to represent buffers
2280in C@.  The @code{buffer_text} structure contains fields describing the
2281text of a buffer; the @code{buffer} structure holds other fields.  In
2282the case of indirect buffers, two or more @code{buffer} structures
2283reference the same @code{buffer_text} structure.
2284
2285Here are some of the fields in @code{struct buffer_text}:
2286
2287@table @code
2288@item beg
2289The address of the buffer contents.  The buffer contents is a linear C
2290array of @code{char}, with the gap somewhere in its midst.
2291
2292@item gpt
2293@itemx gpt_byte
2294The character and byte positions of the buffer gap.  @xref{Buffer
2295Gap}.
2296
2297@item z
2298@itemx z_byte
2299The character and byte positions of the end of the buffer text.
2300
2301@item gap_size
2302The size of buffer's gap.  @xref{Buffer Gap}.
2303
2304@item modiff
2305@itemx save_modiff
2306@itemx chars_modiff
2307@itemx overlay_modiff
2308These fields count the number of buffer-modification events performed
2309in this buffer.  @code{modiff} is incremented after each
2310buffer-modification event, and is never otherwise changed;
2311@code{save_modiff} contains the value of @code{modiff} the last time
2312the buffer was visited or saved; @code{chars_modiff} counts only
2313modifications to the characters in the buffer, ignoring all other
2314kinds of changes (such as text properties); and @code{overlay_modiff}
2315counts only modifications to the buffer's overlays.
2316
2317@item beg_unchanged
2318@itemx end_unchanged
2319The number of characters at the start and end of the text that are
2320known to be unchanged since the last complete redisplay.
2321
2322@item unchanged_modified
2323@itemx overlay_unchanged_modified
2324The values of @code{modiff} and @code{overlay_modiff}, respectively,
2325after the last complete redisplay.  If their current values match
2326@code{modiff} or @code{overlay_modiff}, that means
2327@code{beg_unchanged} and @code{end_unchanged} contain no useful
2328information.
2329
2330@item markers
2331The markers that refer to this buffer.  This is actually a single
2332marker, and successive elements in its marker @dfn{chain} (a linked
2333list) are the other markers referring to this buffer text.
2334
2335@item intervals
2336The interval tree which records the text properties of this buffer.
2337@end table
2338
2339Some of the fields of @code{struct buffer} are:
2340
2341@table @code
2342@item header
2343A header of type @code{union vectorlike_header} is common to all
2344vectorlike objects.
2345
2346@item own_text
2347A @code{struct buffer_text} structure that ordinarily holds the buffer
2348contents.  In indirect buffers, this field is not used.
2349
2350@item text
2351A pointer to the @code{buffer_text} structure for this buffer.  In an
2352ordinary buffer, this is the @code{own_text} field above.  In an
2353indirect buffer, this is the @code{own_text} field of the base buffer.
2354
2355@item next
2356A pointer to the next buffer, in the chain of all buffers, including
2357killed buffers.  This chain is used only for allocation and garbage
2358collection, in order to collect killed buffers properly.
2359
2360@item pt
2361@itemx pt_byte
2362The character and byte positions of point in a buffer.
2363
2364@item begv
2365@itemx begv_byte
2366The character and byte positions of the beginning of the accessible
2367range of text in the buffer.
2368
2369@item zv
2370@itemx zv_byte
2371The character and byte positions of the end of the accessible range of
2372text in the buffer.
2373
2374@item base_buffer
2375In an indirect buffer, this points to the base buffer.  In an ordinary
2376buffer, it is null.
2377
2378@item local_flags
2379This field contains flags indicating that certain variables are local
2380in this buffer.  Such variables are declared in the C code using
2381@code{DEFVAR_PER_BUFFER}, and their buffer-local bindings are stored
2382in fields in the buffer structure itself.  (Some of these fields are
2383described in this table.)
2384
2385@item modtime
2386The modification time of the visited file.  It is set when the file is
2387written or read.  Before writing the buffer into a file, this field is
2388compared to the modification time of the file to see if the file has
2389changed on disk.  @xref{Buffer Modification}.
2390
2391@item auto_save_modified
2392The time when the buffer was last auto-saved.
2393
2394@item last_window_start
2395The @code{window-start} position in the buffer as of the last time the
2396buffer was displayed in a window.
2397
2398@item clip_changed
2399This flag indicates that narrowing has changed in the buffer.
2400@xref{Narrowing}.
2401
2402@item prevent_redisplay_optimizations_p
2403This flag indicates that redisplay optimizations should not be used to
2404display this buffer.
2405
2406@item inhibit_buffer_hooks
2407This flag indicates that the buffer should not run the hooks
2408@code{kill-buffer-hook}, @code{kill-buffer-query-functions}
2409(@pxref{Killing Buffers}), and @code{buffer-list-update-hook}
2410(@pxref{Buffer List}).  It is set at buffer creation (@pxref{Creating
2411Buffers}), and avoids slowing down internal or temporary buffers, such
2412as those created by @code{with-temp-buffer} (@pxref{Definition of
2413with-temp-buffer,, Current Buffer}).
2414
2415@item overlay_center
2416This field holds the current overlay center position.  @xref{Managing
2417Overlays}.
2418
2419@item overlays_before
2420@itemx overlays_after
2421These fields hold, respectively, a list of overlays that end at or
2422before the current overlay center, and a list of overlays that end
2423after the current overlay center.  @xref{Managing Overlays}.
2424@code{overlays_before} is sorted in order of decreasing end position,
2425and @code{overlays_after} is sorted in order of increasing beginning
2426position.
2427
2428@item name
2429A Lisp string that names the buffer.  It is guaranteed to be unique.
2430@xref{Buffer Names}.  This and the following fields have their names
2431in the C struct definition end in a @code{_} to indicate that they
2432should not be accessed directly, but via the @code{BVAR} macro, like
2433this:
2434
2435@example
2436  Lisp_Object buf_name = BVAR (buffer, name);
2437@end example
2438
2439@item save_length
2440The length of the file this buffer is visiting, when last read or
2441saved.  It can have 2 special values: @minus{}1 means auto-saving was
2442turned off in this buffer, and @minus{}2 means don't turn off
2443auto-saving if buffer text shrinks a lot.  This and other fields
2444concerned with saving are not kept in the @code{buffer_text} structure
2445because indirect buffers are never saved.
2446
2447@item directory
2448The directory for expanding relative file names.  This is the value of
2449the buffer-local variable @code{default-directory} (@pxref{File Name Expansion}).
2450
2451@item filename
2452The name of the file visited in this buffer, or @code{nil}.  This is
2453the value of the buffer-local variable @code{buffer-file-name}
2454(@pxref{Buffer File Name}).
2455
2456@item undo_list
2457@itemx backed_up
2458@itemx auto_save_file_name
2459@itemx auto_save_file_format
2460@itemx read_only
2461@itemx file_format
2462@itemx file_truename
2463@itemx invisibility_spec
2464@itemx display_count
2465@itemx display_time
2466These fields store the values of Lisp variables that are automatically
2467buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
2468variable names have the additional prefix @code{buffer-} and have
2469underscores replaced with dashes.  For instance, @code{undo_list}
2470stores the value of @code{buffer-undo-list}.
2471
2472@item mark
2473The mark for the buffer.  The mark is a marker, hence it is also
2474included on the list @code{markers}.  @xref{The Mark}.
2475
2476@item local_var_alist
2477The association list describing the buffer-local variable bindings of
2478this buffer, not including the built-in buffer-local bindings that
2479have special slots in the buffer object.  (Those slots are omitted
2480from this table.)  @xref{Buffer-Local Variables}.
2481
2482@item major_mode
2483Symbol naming the major mode of this buffer, e.g., @code{lisp-mode}.
2484
2485@item mode_name
2486Pretty name of the major mode, e.g., @code{"Lisp"}.
2487
2488@item keymap
2489@itemx abbrev_table
2490@itemx syntax_table
2491@itemx category_table
2492@itemx display_table
2493These fields store the buffer's local keymap (@pxref{Keymaps}), abbrev
2494table (@pxref{Abbrev Tables}), syntax table (@pxref{Syntax Tables}),
2495category table (@pxref{Categories}), and display table (@pxref{Display
2496Tables}).
2497
2498@item downcase_table
2499@itemx upcase_table
2500@itemx case_canon_table
2501These fields store the conversion tables for converting text to lower
2502case, upper case, and for canonicalizing text for case-fold search.
2503@xref{Case Tables}.
2504
2505@item minor_modes
2506An alist of the minor modes of this buffer.
2507
2508@item pt_marker
2509@itemx begv_marker
2510@itemx zv_marker
2511These fields are only used in an indirect buffer, or in a buffer that
2512is the base of an indirect buffer.  Each holds a marker that records
2513@code{pt}, @code{begv}, and @code{zv} respectively, for this buffer
2514when the buffer is not current.
2515
2516@item mode_line_format
2517@itemx header_line_format
2518@itemx case_fold_search
2519@itemx tab_width
2520@itemx fill_column
2521@itemx left_margin
2522@itemx auto_fill_function
2523@itemx truncate_lines
2524@itemx word_wrap
2525@itemx ctl_arrow
2526@itemx bidi_display_reordering
2527@itemx bidi_paragraph_direction
2528@itemx selective_display
2529@itemx selective_display_ellipses
2530@itemx overwrite_mode
2531@itemx abbrev_mode
2532@itemx mark_active
2533@itemx enable_multibyte_characters
2534@itemx buffer_file_coding_system
2535@itemx cache_long_line_scans
2536@itemx point_before_scroll
2537@itemx left_fringe_width
2538@itemx right_fringe_width
2539@itemx fringes_outside_margins
2540@itemx scroll_bar_width
2541@itemx indicate_empty_lines
2542@itemx indicate_buffer_boundaries
2543@itemx fringe_indicator_alist
2544@itemx fringe_cursor_alist
2545@itemx scroll_up_aggressively
2546@itemx scroll_down_aggressively
2547@itemx cursor_type
2548@itemx cursor_in_non_selected_windows
2549These fields store the values of Lisp variables that are automatically
2550buffer-local (@pxref{Buffer-Local Variables}), whose corresponding
2551variable names have underscores replaced with dashes.  For instance,
2552@code{mode_line_format} stores the value of @code{mode-line-format}.
2553
2554@item last_selected_window
2555This is the last window that was selected with this buffer in it, or @code{nil}
2556if that window no longer displays this buffer.
2557@end table
2558
2559@node Window Internals
2560@subsection Window Internals
2561@cindex internals, of window
2562@cindex window internals
2563
2564  The fields of a window (for a complete list, see the definition of
2565@code{struct window} in @file{window.h}) include:
2566
2567@table @code
2568@item frame
2569The frame that this window is on, as a Lisp object.
2570
2571@item mini
2572Non-zero if this window is a minibuffer window, a window showing the
2573minibuffer or the echo area.
2574
2575@item pseudo_window_p
2576@cindex pseudo window
2577Non-zero if this window is a @dfn{pseudo window}.  A pseudo window is
2578either a window used to display the menu bar or the tool bar (when
2579Emacs uses toolkits that don't display their own menu bar and tool
2580bar) or the tab bar or a window showing a tooltip on a tooltip frame.
2581Pseudo windows are in general not accessible from Lisp code.
2582
2583@item parent
2584Internally, Emacs arranges windows in a tree; each group of siblings
2585has a parent window whose area includes all the siblings.  This field
2586points to the window's parent in that tree, as a Lisp object.  For the
2587root window of the tree and a minibuffer window this is always
2588@code{nil}.
2589
2590Parent windows do not display buffers, and play little role in display
2591except to shape their child windows.  Emacs Lisp programs cannot
2592directly manipulate parent windows; they operate on the windows at the
2593leaves of the tree, which actually display buffers.
2594
2595@item contents
2596For a leaf window and windows showing a tooltip, this is the buffer,
2597as a Lisp object, that the window is displaying.  For an internal
2598(``parent'') window, this is its first child window.  For a pseudo
2599window showing a menu or tool bar this is @code{nil}.  It is also
2600@code{nil} for a window that has been deleted.
2601
2602@item next
2603@itemx prev
2604The next and previous sibling of this window as Lisp objects.
2605@code{next} is @code{nil} if the window is the right-most or
2606bottom-most in its group; @code{prev} is @code{nil} if it is the
2607left-most or top-most in its group.  Whether the sibling is left/right
2608or up/down is determined by the @code{horizontal} field of the
2609sibling's parent: if it's non-zero, the siblings are arranged
2610horizontally.
2611
2612As a special case, @code{next} of a frame's root window points to the
2613frame's minibuffer window, provided this is not a minibuffer-only or
2614minibuffer-less frame.  On such frames @code{prev} of the minibuffer
2615window points to that frame's root window.  In any other case, the
2616root window's @code{next} and the minibuffer window's (if present)
2617@code{prev} fields are @code{nil}.
2618
2619@item left_col
2620The left-hand edge of the window, measured in columns, relative to the
2621leftmost column (column 0) of the window's native frame.
2622
2623@item top_line
2624The top edge of the window, measured in lines, relative to the topmost
2625line (line 0) of the window's native frame.
2626
2627@item pixel_left
2628@itemx pixel_top
2629The left-hand and top edges of this window, measured in pixels,
2630relative to the top-left corner (0, 0) of the window's native frame.
2631
2632@item total_cols
2633@itemx total_lines
2634The total width and height of the window, measured in columns and
2635lines respectively.  The values include scroll bars and fringes,
2636dividers and/or the separator line on the right of the window (if
2637any).
2638
2639@item pixel_width;
2640@itemx pixel_height;
2641The total width and height of the window measured in pixels.
2642
2643@item start
2644A marker pointing to the position in the buffer that is the first
2645character (in the logical order, @pxref{Bidirectional Display})
2646displayed in the window.
2647
2648@item pointm
2649@cindex window point internals
2650This is the value of point in the current buffer when this window is
2651selected; when it is not selected, it retains its previous value.
2652
2653@item old_pointm
2654The value of @code{pointm} at the last redisplay time.
2655
2656@item force_start
2657If this flag is non-@code{nil}, it says that the window has been
2658scrolled explicitly by the Lisp program, and the value of the
2659window's @code{start} was set for redisplay to honor.  This affects
2660what the next redisplay does if point is off the screen: instead of
2661scrolling the window to show the text around point, it moves point to
2662a location that is on the screen.
2663
2664@item optional_new_start
2665This is similar to @code{force_start}, but the next redisplay will
2666only obey it if point stays visible.
2667
2668@item start_at_line_beg
2669Non-@code{nil} means current value of @code{start} was the beginning of a line
2670when it was chosen.
2671
2672@item use_time
2673This is the last time that the window was selected.  The function
2674@code{get-lru-window} uses this field.
2675
2676@item sequence_number
2677A unique number assigned to this window when it was created.
2678
2679@item last_modified
2680The @code{modiff} field of the window's buffer, as of the last time
2681a redisplay completed in this window.
2682
2683@item last_overlay_modified
2684The @code{overlay_modiff} field of the window's buffer, as of the last
2685time a redisplay completed in this window.
2686
2687@item last_point
2688The buffer's value of point, as of the last time a redisplay completed
2689in this window.
2690
2691@item last_had_star
2692A non-zero value means the window's buffer was modified when the
2693window was last updated.
2694
2695@item vertical_scroll_bar_type
2696@itemx horizontal_scroll_bar_type
2697The types of this window's vertical and horizontal scroll bars.
2698
2699@item scroll_bar_width
2700@itemx scroll_bar_height
2701The width of this window's vertical scroll bar and the height of this
2702window's horizontal scroll bar, in pixels.
2703
2704@item left_margin_cols
2705@itemx right_margin_cols
2706The widths of the left and right margins in this window.  A value of
2707zero means no margin.
2708
2709@item left_fringe_width
2710@itemx right_fringe_width
2711The pixel widths of the left and right fringes in this window.  A
2712value of @minus{}1 means use the values of the frame.
2713
2714@item fringes_outside_margins
2715A non-zero value means the fringes outside the display margins;
2716othersize they are between the margin and the text.
2717
2718@item window_end_pos
2719This is computed as @code{z} minus the buffer position of the last glyph
2720in the current matrix of the window.  The value is only valid if
2721@code{window_end_valid} is non-zero.
2722
2723@item window_end_bytepos
2724The byte position corresponding to @code{window_end_pos}.
2725
2726@item window_end_vpos
2727The window-relative vertical position of the line containing
2728@code{window_end_pos}.
2729
2730@item window_end_valid
2731This field is set to a non-zero value if @code{window_end_pos} and
2732@code{window_end_vpos} are truly valid.  This is zero if nontrivial
2733redisplay is pre-empted, since in that case the display that
2734@code{window_end_pos} was computed for did not get onto the screen.
2735
2736@item cursor
2737A structure describing where the cursor is in this window.
2738
2739@item last_cursor_vpos
2740The window-relative vertical position of the line showing the cursor
2741as of the last redisplay that finished.
2742
2743@item phys_cursor
2744A structure describing where the cursor of this window physically is.
2745
2746@item phys_cursor_type
2747@c FIXME What is this?
2748@c itemx phys_cursor_ascent
2749@itemx phys_cursor_height
2750@itemx phys_cursor_width
2751The type, height, and width of the cursor that was last displayed on
2752this window.
2753
2754@item phys_cursor_on_p
2755This field is non-zero if the cursor is physically on.
2756
2757@item cursor_off_p
2758Non-zero means the cursor in this window is logically off.  This is
2759used for blinking the cursor.
2760
2761@item last_cursor_off_p
2762This field contains the value of @code{cursor_off_p} as of the time of
2763the last redisplay.
2764
2765@item must_be_updated_p
2766This is set to 1 during redisplay when this window must be updated.
2767
2768@item hscroll
2769This is the number of columns that the display in the window is
2770scrolled horizontally to the left.  Normally, this is 0.  When only
2771the current line is hscrolled, this describes how much the current
2772line is scrolled.
2773
2774@item min_hscroll
2775Minimum value of @code{hscroll}, set by the user via
2776@code{set-window-hscroll} (@pxref{Horizontal Scrolling}).  When only
2777the current line is hscrolled, this describes the horizontal scrolling
2778of lines other than the current one.
2779
2780@item vscroll
2781Vertical scroll amount, in pixels.  Normally, this is 0.
2782
2783@item dedicated
2784Non-@code{nil} if this window is dedicated to its buffer.
2785
2786@item combination_limit
2787This window's combination limit, meaningful only for a parent window.
2788If this is @code{t}, then it is not allowed to delete this window and
2789recombine its child windows with other siblings of this window.
2790
2791@item window_parameters
2792The alist of this window's parameters.
2793
2794@item display_table
2795The window's display table, or @code{nil} if none is specified for it.
2796
2797@item update_mode_line
2798Non-zero means this window's mode line needs to be updated.
2799
2800@item mode_line_height
2801@itemx header_line_height
2802The height in pixels of the mode line and the header line, or
2803@minus{}1 if not known.
2804
2805@item base_line_number
2806The line number of a certain position in the buffer, or zero.
2807This is used for displaying the line number of point in the mode line.
2808
2809@item base_line_pos
2810The position in the buffer for which the line number is known, or
2811zero meaning none is known.  If it is @minus{}1, don't display
2812the line number as long as the window shows that buffer.
2813
2814@item column_number_displayed
2815The column number currently displayed in this window's mode line, or
2816@minus{}1 if column numbers are not being displayed.
2817
2818@item current_matrix
2819@itemx desired_matrix
2820Glyph matrices describing the current and desired display of this window.
2821@end table
2822
2823@node Process Internals
2824@subsection Process Internals
2825@cindex internals, of process
2826@cindex process internals
2827
2828  The fields of a process (for a complete list, see the definition of
2829@code{struct Lisp_Process} in @file{process.h}) include:
2830
2831@table @code
2832@item name
2833A Lisp string, the name of the process.
2834
2835@item command
2836A list containing the command arguments that were used to start this
2837process.  For a network or serial process, it is @code{nil} if the
2838process is running or @code{t} if the process is stopped.
2839
2840@item filter
2841A Lisp function used to accept output from the process.
2842
2843@item sentinel
2844A Lisp function called whenever the state of the process changes.
2845
2846@item buffer
2847The associated buffer of the process.
2848
2849@item pid
2850An integer, the operating system's process @acronym{ID}.
2851Pseudo-processes such as network or serial connections use a value of 0.
2852
2853@item childp
2854A flag, @code{t} if this is really a child process.  For a network or
2855serial connection, it is a plist based on the arguments to
2856@code{make-network-process} or @code{make-serial-process}.
2857
2858@item mark
2859A marker indicating the position of the end of the last output from this
2860process inserted into the buffer.  This is often but not always the end
2861of the buffer.
2862
2863@item kill_without_query
2864If this is non-zero, killing Emacs while this process is still running
2865does not ask for confirmation about killing the process.
2866
2867@item raw_status
2868The raw process status, as returned by the @code{wait} system call.
2869
2870@item status
2871The process status, as @code{process-status} should return it.  This
2872is a Lisp symbol, a cons cell, or a list.
2873
2874@item tick
2875@itemx update_tick
2876If these two fields are not equal, a change in the status of the process
2877needs to be reported, either by running the sentinel or by inserting a
2878message in the process buffer.
2879
2880@item pty_flag
2881Non-zero if communication with the subprocess uses a pty; zero if it
2882uses a pipe.
2883
2884@item infd
2885The file descriptor for input from the process.
2886
2887@item outfd
2888The file descriptor for output to the process.
2889
2890@item tty_name
2891The name of the terminal that the subprocess is using,
2892or @code{nil} if it is using pipes.
2893
2894@item decode_coding_system
2895Coding-system for decoding the input from this process.
2896
2897@item decoding_buf
2898A working buffer for decoding.
2899
2900@item decoding_carryover
2901Size of carryover in decoding.
2902
2903@item encode_coding_system
2904Coding-system for encoding the output to this process.
2905
2906@item encoding_buf
2907A working buffer for encoding.
2908
2909@item inherit_coding_system_flag
2910Flag to set @code{coding-system} of the process buffer from the
2911coding system used to decode process output.
2912
2913@item type
2914Symbol indicating the type of process: @code{real}, @code{network},
2915@code{serial}.
2916
2917@end table
2918
2919@node C Integer Types
2920@section C Integer Types
2921@cindex integer types (C programming language)
2922
2923Here are some guidelines for use of integer types in the Emacs C
2924source code.  These guidelines sometimes give competing advice; common
2925sense is advised.
2926
2927@itemize @bullet
2928@item
2929Avoid arbitrary limits.  For example, avoid @code{int len = strlen
2930(s);} unless the length of @code{s} is required for other reasons to
2931fit in @code{int} range.
2932
2933@item
2934Do not assume that signed integer arithmetic wraps around on overflow.
2935This is no longer true of Emacs porting targets: signed integer
2936overflow has undefined behavior in practice, and can dump core or
2937even cause earlier or later code to behave illogically.  Unsigned
2938overflow does wrap around reliably, modulo a power of two.
2939
2940@item
2941Prefer signed types to unsigned, as code gets confusing when signed
2942and unsigned types are combined.  Many other guidelines assume that
2943types are signed; in the rarer cases where unsigned types are needed,
2944similar advice may apply to the unsigned counterparts (e.g.,
2945@code{size_t} instead of @code{ptrdiff_t}, or @code{uintptr_t} instead
2946of @code{intptr_t}).
2947
2948@item
2949Prefer @code{int} for Emacs character codes, in the range 0 ..@: 0x3FFFFF@.
2950More generally, prefer @code{int} for integers known to be in
2951@code{int} range, e.g., screen column counts.
2952
2953@item
2954Prefer @code{ptrdiff_t} for sizes, i.e., for integers bounded by the
2955maximum size of any individual C object or by the maximum number of
2956elements in any C array.  This is part of Emacs's general preference
2957for signed types.  Using @code{ptrdiff_t} limits objects to
2958@code{PTRDIFF_MAX} bytes, but larger objects would cause trouble
2959anyway since they would break pointer subtraction, so this does not
2960impose an arbitrary limit.
2961
2962@item
2963Avoid @code{ssize_t} except when communicating to low-level APIs that
2964have @code{ssize_t}-related limitations.  Although it's equivalent to
2965@code{ptrdiff_t} on typical platforms, @code{ssize_t} is occasionally
2966narrower, so using it for size-related calculations could overflow.
2967Also, @code{ptrdiff_t} is more ubiquitous and better-standardized, has
2968standard @code{printf} formats, and is the basis for Emacs's internal
2969size-overflow checking.  When using @code{ssize_t}, please note that
2970POSIX requires support only for values in the range @minus{}1 ..@:
2971@code{SSIZE_MAX}.
2972
2973@item
2974Normally, prefer @code{intptr_t} for internal representations of pointers, or
2975for integers bounded only by the number of objects that can exist at
2976any given time or by the total number of bytes that can be allocated.
2977However, prefer @code{uintptr_t} to represent pointer arithmetic that
2978could cross page boundaries.  For example, on a machine with a 32-bit
2979address space an array could cross the 0x7fffffff/0x80000000 boundary,
2980which would cause an integer overflow when adding 1 to
2981@code{(intptr_t) 0x7fffffff}.
2982
2983@item
2984Prefer the Emacs-defined type @code{EMACS_INT} for representing values
2985converted to or from Emacs Lisp fixnums, as fixnum arithmetic is based
2986on @code{EMACS_INT}.
2987
2988@item
2989When representing a system value (such as a file size or a count of
2990seconds since the Epoch), prefer the corresponding system type (e.g.,
2991@code{off_t}, @code{time_t}).  Do not assume that a system type is
2992signed, unless this assumption is known to be safe.  For example,
2993although @code{off_t} is always signed, @code{time_t} need not be.
2994
2995@item
2996Prefer @code{intmax_t} for representing values that might be any
2997signed integer value.
2998A @code{printf}-family function can print such a value
2999via a format like @code{"%"PRIdMAX}.
3000
3001@item
3002Prefer @code{bool}, @code{false} and @code{true} for booleans.
3003Using @code{bool} can make programs easier to read and a bit faster than
3004using @code{int}.  Although it is also OK to use @code{int}, @code{0}
3005and @code{1}, this older style is gradually being phased out.  When
3006using @code{bool}, respect the limitations of the replacement
3007implementation of @code{bool}, as documented in the source file
3008@file{lib/stdbool.in.h}.  In particular, boolean bitfields should be of type
3009@code{bool_bf}, not @code{bool}, so that they work correctly even when
3010compiling Objective C with standard GCC.
3011
3012@item
3013In bitfields, prefer @code{unsigned int} or @code{signed int} to
3014@code{int}, as @code{int} is less portable: it might be signed, and
3015might not be.  Single-bit bit fields should be @code{unsigned int} or
3016@code{bool_bf} so that their values are 0 or 1.
3017@end itemize
3018
3019@c FIXME Mention src/globals.h somewhere in this file?
3020