GNU Emacs Internals
*******************

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

Building Emacs
==============

This section explains the steps involved in building the Emacs executable.  You don't have to know this material to build and install Emacs, since the makefiles do all these things automatically.  This information is pertinent to Emacs developers.

Compilation of the C source files in the src directory produces an executable file called temacs, also called a bare impure Emacs.  It contains the Emacs Lisp interpreter and I/O routines, but not the editing commands.

The command temacs -l loadup would run temacs and direct it to load loadup.el.  The loadup library loads additional Lisp libraries, which set up the normal Emacs editing environment.  After this step, the Emacs executable is no longer bare.

Because it takes some time to load the standard Lisp files, the temacs executable usually isn't run directly by users. Instead, as one of the last steps of building Emacs, the command temacs -batch -l loadup dump is run.  The special dump argument causes temacs to dump out an executable program, called emacs, which has all the standard Lisp files preloaded. (The -batch argument prevents temacs from trying to initialize any of its data on the terminal, so that the tables of terminal information are empty in the dumped Emacs.)

The dumped emacs executable (also called a pure Emacs) is the one which is installed.  The variable preloaded-file-list stores a list of the Lisp files preloaded into the dumped Emacs.  If you port Emacs to a new operating system, and are not able to implement dumping, then Emacs must load loadup.el each time it starts.

You can specify additional files to preload by writing a library named site-load.el that loads them.  You may need to rebuild Emacs with an added definition

     #define SITELOAD_PURESIZE_EXTRA N

to make N added bytes of pure space to hold the additional files; see src/puresize.h. (Try adding increments of 20000 until it is big enough.)  However, the advantage of preloading additional files decreases as machines get faster.  On modern machines, it is usually not advisable.

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

You can specify other Lisp expressions to execute just before dumping by putting them in a library named site-init.el.  This file is executed after the documentation strings are found.

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

  * Arrange to scan these files when producing the etc/DOC file, and load them with site-load.el.

  * Load the files with site-init.el, then copy the files into the installation directory for Lisp files when you install Emacs.

  * Specify a nil value for byte-compile-dynamic-docstrings as a local variable in each of these files, and load them with either site-load.el or site-init.el.  (This method has the drawback that the documentation strings take up space in Emacs all the time.)

It is not advisable to put anything in site-load.el or site-init.el that would alter any of the features that users expect in an ordinary unmodified Emacs.  If you feel you must override normal features for your site, do it with default.el, so that users can override your changes if they wish.  See Startup Summary. Note that if either site-load.el or site-init.el changes load-path, the changes will be lost after dumping. See Library Search.  To make a permanent change to load-path, use the --enable-locallisppath option of configure.

In a package that can be preloaded, it is sometimes necessary (or useful) to delay certain evaluations until Emacs subsequently starts up.  The vast majority of such cases relate to the values of customizable variables.  For example, tutorial-directory is a variable defined in startup.el, which is preloaded.  The default value is set based on data-directory.  The variable needs to access the value of data-directory when Emacs starts, not when it is dumped, because the Emacs executable has probably been installed in a different location since it was dumped.

 -- Function: custom-initialize-delay symbol value
     This function delays the initialization of SYMBOL to the next Emacs start.  You normally use this function by specifying it as the :initialize property of a customizable variable.  (The argument VALUE is unused, and is provided only for compatibility with the form Custom expects.)

In the unlikely event that you need a more general functionality than custom-initialize-delay provides, you can use before-init-hook (see Startup Summary).

 -- Function: dump-emacs to-file from-file
     This function dumps the current state of Emacs into an executable file TO-FILE.  It takes symbols from FROM-FILE (this is normally the executable file temacs).

     If you want to use this function in an Emacs that was already dumped, you must run Emacs with -batch.

Pure Storage
============

Emacs Lisp uses two kinds of storage for user-created Lisp objects: normal storage and pure storage.  Normal storage is where all the new data created during an Emacs session are kept (see Garbage Collection).  Pure storage is used for certain data in the preloaded standard Lisp files---data that should never change during actual use of Emacs.

Pure storage is allocated only while temacs is loading the standard preloaded Lisp libraries.  In the file emacs, it is marked as read-only (on operating systems that permit this), so that the memory space can be shared by all the Emacs jobs running on the machine at once.  Pure storage is not expandable; a fixed amount is allocated when Emacs is compiled, and if that is not sufficient for the preloaded libraries, temacs allocates dynamic memory for the part that didn't fit.  The resulting image will work, but garbage collection (see Garbage Collection) is disabled in this situation, causing a memory leak.  Such an overflow normally won't happen unless you try to preload additional libraries or add features to the standard ones.  Emacs will display a warning about the overflow when it starts.  If this happens, you should increase the compilation parameter SYSTEM_PURESIZE_EXTRA in the file src/puresize.h and rebuild Emacs.

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

     This function is a no-op except while Emacs is being built and dumped; it is usually called only in preloaded Lisp files.

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

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

     This flag is t while loading all of the basic functions for building Emacs initially (allowing those functions to be shareable and non-collectible).  Dumping Emacs as an executable always writes nil in this variable, regardless of the value it actually has before and after dumping.

     You should not change this flag in a running Emacs.

Garbage Collection
==================

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

Beyond the basic vector, a lot of objects like window, buffer, and frame are managed as if they were vectors.  The corresponding C data structures include the struct vectorlike_header field whose size member contains the subtype enumerated by enum pvec_type and an information about how many Lisp_Object fields this structure contains and what the size of the rest data is.  This information is needed to calculate the memory footprint of an object, and used by the vector allocation code while iterating over the vector blocks.

It is quite common to use some storage for a while, then release it by (for example) killing a buffer or deleting the last pointer to an object.  Emacs provides a garbage collector to reclaim this abandoned storage.  The garbage collector operates by finding and marking all Lisp objects that are still accessible to Lisp programs. To begin with, it assumes all the symbols, their values and associated function definitions, and any data presently on the stack, are accessible.  Any objects that can be reached indirectly through other accessible objects are also accessible.

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

The sweep phase puts unused cons cells onto a free list for future allocation; likewise for symbols and markers.  It compacts the accessible strings so they occupy fewer 8k blocks; then it frees the other 8k blocks.  Unreachable vectors from vector blocks are coalesced to create largest possible free areas; if a free area spans a complete 4k block, that block is freed.  Otherwise, the free area is recorded in a free list array, where each entry corresponds to a free list of areas of the same size.  Large vectors, buffers, and other large objects are allocated and freed individually.

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

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

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

     garbage-collect returns a list with information on amount of space in use, where each entry has the form (NAME SIZE USED) or (NAME SIZE USED FREE).  In the entry, NAME is a symbol describing the kind of objects this entry represents, SIZE is the number of bytes used by each one, USED is the number of those objects that were found live in the heap, and optional FREE is the number of those objects that are not live but that Emacs keeps around for future allocations.  So an overall result is:

          ((conses CONS-SIZE USED-CONSES FREE-CONSES)
           (symbols SYMBOL-SIZE USED-SYMBOLS FREE-SYMBOLS)
           (miscs MISC-SIZE USED-MISCS FREE-MISCS)
           (strings STRING-SIZE USED-STRINGS FREE-STRINGS)
           (string-bytes BYTE-SIZE USED-BYTES)
           (vectors VECTOR-SIZE USED-VECTORS)
           (vector-slots SLOT-SIZE USED-SLOTS FREE-SLOTS)
           (floats FLOAT-SIZE USED-FLOATS FREE-FLOATS)
           (intervals INTERVAL-SIZE USED-INTERVALS FREE-INTERVALS)
           (buffers BUFFER-SIZE USED-BUFFERS)
           (heap UNIT-SIZE TOTAL-SIZE FREE-SIZE))

     Here is an example:

          (garbage-collect)
                => ((conses 16 49126 8058) (symbols 48 14607 0)
                           (miscs 40 34 56) (strings 32 2942 2607)
                           (string-bytes 1 78607) (vectors 16 7247)
                           (vector-slots 8 341609 29474) (floats 8 71 102)
                           (intervals 56 27 26) (buffers 944 8)
                           (heap 1024 11715 2678))

     Below is a table explaining each element.  Note that last heap entry is optional and present only if an underlying malloc implementation provides mallinfo function.

     cons-size
          Internal size of a cons cell, i.e., sizeof (struct Lisp_Cons).

     used-conses
          The number of cons cells in use.

     free-conses
          The number of cons cells for which space has been obtained from the operating system, but that are not currently being used.

     symbol-size
          Internal size of a symbol, i.e., sizeof (struct Lisp_Symbol).

     used-symbols
          The number of symbols in use.

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

     misc-size
          Internal size of a miscellaneous entity, i.e., sizeof (union Lisp_Misc), which is a size of the largest type enumerated in enum Lisp_Misc_Type.

     used-miscs
          The number of miscellaneous objects in use.  These include markers and overlays, plus certain objects not visible to users.

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

     string-size
          Internal size of a string header, i.e., sizeof (struct Lisp_String).

     used-strings
          The number of string headers in use.

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

     byte-size
          This is used for convenience and equals to sizeof (char).

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

     vector-size
          Internal size of a vector header, i.e., sizeof (struct Lisp_Vector).

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

     slot-size
          Internal size of a vector slot, always equal to sizeof (Lisp_Object).

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

     free-slots
          The number of free slots in all vector blocks.

     float-size
          Internal size of a float object, i.e., sizeof (struct Lisp_Float). (Do not confuse it with the native platform float or double.)

     used-floats
          The number of floats in use.

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

     interval-size
          Internal size of an interval object, i.e., sizeof (struct interval).

     used-intervals
          The number of intervals in use.

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

     buffer-size
          Internal size of a buffer, i.e., sizeof (struct buffer). (Do not confuse with the value returned by buffer-size function.)

     used-buffers
          The number of buffer objects in use.  This includes killed buffers invisible to users, i.e., all buffers in all_buffers list.

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

     total-size
          Total heap size, in UNIT-SIZE units.

     free-size
          Heap space which is not currently used, in UNIT-SIZE units.

     If there was overflow in pure space (see Pure Storage), garbage-collect returns nil, because a real garbage collection cannot be done.

 -- User Option: garbage-collection-messages
     If this variable is non-nil, Emacs displays a message at the beginning and end of garbage collection.  The default value is nil.

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

 -- User Option: gc-cons-threshold
     The value of this variable is the number of bytes of storage that must be allocated for Lisp objects after one garbage collection in order to trigger another garbage collection.  You can use the result returned by garbage-collect to get an information about size of the particular object type; space allocated to the contents of buffers does not count. Note that the subsequent garbage collection does not happen immediately when the threshold is exhausted, but only the next time the Lisp interpreter is called.

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

     You can make collections more frequent by specifying a smaller value, down to 1/10th of GC_DEFAULT_THRESHOLD.  A value less than this minimum will remain in effect only until the subsequent garbage collection, at which time garbage-collect will set the threshold back to the minimum.

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

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

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

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

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

 -- Variable: memory-full
     This variable is t if Emacs is nearly out of memory for Lisp objects, and nil otherwise.

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

 -- Variable: gcs-done
     This variable contains the total number of garbage collections done so far in this Emacs session.

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

Memory Usage
============

These functions and variables give information about the total amount of memory allocation that Emacs has done, broken down by data type. Note the difference between these and the values returned by garbage-collect; those count objects that currently exist, but these count the number or size of all allocations, including those for objects that have since been freed.

 -- Variable: cons-cells-consed
     The total number of cons cells that have been allocated so far in this Emacs session.

 -- Variable: floats-consed
     The total number of floats that have been allocated so far in this Emacs session.

 -- Variable: vector-cells-consed
     The total number of vector cells that have been allocated so far in this Emacs session.

 -- Variable: symbols-consed
     The total number of symbols that have been allocated so far in this Emacs session.

 -- Variable: string-chars-consed
     The total number of string characters that have been allocated so far in this session.

 -- Variable: misc-objects-consed
     The total number of miscellaneous objects that have been allocated so far in this session.  These include markers and overlays, plus certain objects not visible to users.

 -- Variable: intervals-consed
     The total number of intervals that have been allocated so far in this Emacs session.

 -- Variable: strings-consed
     The total number of strings that have been allocated so far in this Emacs session.

Writing Emacs Primitives
========================

Lisp primitives are Lisp functions implemented in C.  The details of interfacing the C function so that Lisp can call it are handled by a few C macros.  The only way to really understand how to write new C code is to read the source, but we can explain some things here.

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

     DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
       doc: /* Eval args until one of them yields non-nil, then return
     that value.
     The remaining args are not evalled at all.
     If all args return nil, return nil.

     usage: (or CONDITIONS ...)  */)
       (Lisp_Object args)
     {
       register Lisp_Object val = Qnil;
       struct gcpro gcpro1;

       GCPRO1 (args);

       while (CONSP (args))
         {
           val = eval_sub (XCAR (args));
           if (!NILP (val))
             break;
           args = XCDR (args);
         }

       UNGCPRO;
       return val;
     }

Let's start with a precise explanation of the arguments to the DEFUN macro.  Here is a template for them:

     DEFUN (LNAME, FNAME, SNAME, MIN, MAX, INTERACTIVE, DOC)

lname
     This is the name of the Lisp symbol to define as the function name; in the example above, it is or.

fname
     This is the C function name for this function.  This is the name that is used in C code for calling the function.  The name is, by convention, F prepended to the Lisp name, with all dashes (-) in the Lisp name changed to underscores.  Thus, to call this function from C code, call For.

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

min
     This is the minimum number of arguments that the function requires.  The function or allows a minimum of zero arguments.

max
     This is the maximum number of arguments that the function accepts, if there is a fixed maximum.  Alternatively, it can be UNEVALLED, indicating a special form that receives unevaluated arguments, or MANY, indicating an unlimited number of evaluated arguments (the equivalent of &rest).  Both UNEVALLED and MANY are macros.  If MAX is a number, it must be more than MIN but less than 8.

interactive
     This is an interactive specification, a string such as might be used as the argument of interactive in a Lisp function.  In the case of or, it is 0 (a null pointer), indicating that or cannot be called interactively.  A value of "" indicates a function that should receive no arguments when called interactively. If the value begins with a "(, the string is evaluated as a Lisp form.  For example:

          DEFUN ("foo", Ffoo, Sfoo, 0, UNEVALLED,
                 "(list (read-char-by-name \"Insert character: \")\
                        (prefix-numeric-value current-prefix-arg)\
                        t))",
            doc: /* ... /*)

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

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

     All the usual rules for documentation strings in Lisp code (see Documentation Tips) apply to C code documentation strings too.

After the call to the DEFUN macro, you must write the argument list for the C function, including the types for the arguments.  If the primitive accepts a fixed maximum number of Lisp arguments, there must be one C argument for each Lisp argument, and each argument must be of type Lisp_Object.  (Various macros and functions for creating values of type Lisp_Object are declared in the file lisp.h.)  If the primitive has no upper limit on the number of Lisp arguments, it must have exactly two C arguments: the first is the number of Lisp arguments, and the second is the address of a block containing their values.  These have types int and Lisp_Object * respectively.  Since Lisp_Object can hold any Lisp object of any data type, you can determine the actual data type only at run time; so if you want a primitive to accept only a certain type of argument, you must check the type explicitly using a suitable predicate (see Type Predicates).

Within the function For itself, note the use of the macros GCPRO1 and UNGCPRO.  These macros are defined for the sake of the few platforms which do not use Emacs' default stack-marking garbage collector.  The GCPRO1 macro ``protects'' a variable from garbage collection, explicitly informing the garbage collector that that variable and all its contents must be as accessible.  GC protection is necessary in any function which can perform Lisp evaluation by calling eval_sub or Feval as a subroutine, either directly or indirectly.

It suffices to ensure that at least one pointer to each object is GC-protected.  Thus, a particular local variable can do without protection if it is certain that the object it points to will be preserved by some other pointer (such as another local variable that has a GCPRO).  Otherwise, the local variable needs a GCPRO.

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

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

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

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

     defsubr (&SNAME);

Here SNAME is the name you used as the third argument to DEFUN.

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

The function syms_of_FILENAME is also the place to define any C variables that are to be visible as Lisp variables. DEFVAR_LISP makes a C variable of type Lisp_Object visible in Lisp.  DEFVAR_INT makes a C variable of type int visible in Lisp with a value that is always an integer. DEFVAR_BOOL makes a C variable of type int visible in Lisp with a value that is either t or nil.  Note that variables defined with DEFVAR_BOOL are automatically added to the list byte-boolean-vars used by the byte compiler.

If you want to make a Lisp variables that is defined in C behave like one declared with defcustom, add an appropriate entry to cus-start.el.

If you define a file-scope C variable of type Lisp_Object, you must protect it from garbage-collection by calling staticpro in syms_of_FILENAME, like this:

     staticpro (&VARIABLE);

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

     DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
       Scoordinates_in_window_p, 2, 2, 0,
       doc: /* Return non-nil if COORDINATES are in WINDOW.
       ...

       or `right-margin' is returned.  */)
       (register Lisp_Object coordinates, Lisp_Object window)
     {
       struct window *w;
       struct frame *f;
       int x, y;
       Lisp_Object lx, ly;

       CHECK_LIVE_WINDOW (window);
       w = XWINDOW (window);
       f = XFRAME (w->frame);
       CHECK_CONS (coordinates);
       lx = Fcar (coordinates);
       ly = Fcdr (coordinates);
       CHECK_NUMBER_OR_FLOAT (lx);
       CHECK_NUMBER_OR_FLOAT (ly);
       x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH(f);
       y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH(f);

       switch (coordinates_in_window (w, x, y))
         {
         case ON_NOTHING:            /* NOT in window at all. */
           return Qnil;

         ...

         case ON_MODE_LINE:          /* In mode line of window. */
           return Qmode_line;

         ...

         case ON_SCROLL_BAR:         /* On scroll-bar of window.  */
           /* Historically we are supposed to return nil in this case.  */
           return Qnil;

         default:
           abort ();
         }
     }

Note that C code cannot call functions by name unless they are defined in C.  The way to call a function written in Lisp is to use Ffuncall, which embodies the Lisp function funcall.  Since the Lisp function funcall accepts an unlimited number of arguments, in C it takes two: the number of Lisp-level arguments, and a one-dimensional array containing their values.  The first Lisp-level argument is the Lisp function to call, and the rest are the arguments to pass to it.  Since Ffuncall can call the evaluator, you must protect pointers from garbage collection around the call to Ffuncall.

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

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

If you define a function which is side-effect free, update the code in byte-opt.el that binds side-effect-free-fns and side-effect-and-error-free-fns so that the compiler optimizer knows about it.

Object Internals
================

Emacs Lisp provides a rich set of the data types.  Some of them, like cons cells, integers and strings, are common to nearly all Lisp dialects.  Some others, like markers and buffers, are quite special and needed to provide the basic support to write editor commands in Lisp.  To implement such a variety of object types and provide an efficient way to pass objects between the subsystems of an interpreter, there is a set of C data structures and a special type to represent the pointers to all of them, which is known as tagged pointer.

In C, the tagged pointer is an object of type Lisp_Object.  Any initialized variable of such a type always holds the value of one of the following basic data types: integer, symbol, string, cons cell, float, vectorlike or miscellaneous object.  Each of these data types has the corresponding tag value.  All tags are enumerated by enum Lisp_Type and placed into a 3-bit bitfield of the Lisp_Object.  The rest of the bits is the value itself.  Integer values are immediate, i.e., directly represented by those value bits, and all other objects are represented by the C pointers to a corresponding object allocated from the heap.  Width of the Lisp_Object is platform- and configuration-dependent: usually it's equal to the width of an underlying platform pointer (i.e., 32-bit on a 32-bit machine and 64-bit on a 64-bit one), but also there is a special configuration where Lisp_Object is 64-bit but all pointers are 32-bit. The latter trick was designed to overcome the limited range of values for Lisp integers on a 32-bit system by using 64-bit long long type for Lisp_Object.

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

struct Lisp_Cons
     Cons cell, an object used to construct lists.

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

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

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

struct Lisp_Float
     Floating point value.

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

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

Below there is a description of a few subtypes of Lisp_Vectorlike. Buffer object represents the text to display and edit.  Window is the part of display structure which shows the buffer or used as a container to recursively place other windows on the same frame.  (Do not confuse Emacs Lisp window object with the window as an entity managed by the user interface system like X; in Emacs terminology, the latter is called frame.)  Finally, process object is used to manage the subprocesses.

Buffer Internals
----------------

Two structures (see buffer.h) are used to represent buffers in C.  The buffer_text structure contains fields describing the text of a buffer; the buffer structure holds other fields.  In the case of indirect buffers, two or more buffer structures reference the same buffer_text structure.

Here are some of the fields in struct buffer_text:

beg
     The address of the buffer contents.

gpt
gpt_byte
     The character and byte positions of the buffer gap.  See Buffer Gap.

z
z_byte
     The character and byte positions of the end of the buffer text.

gap_size
     The size of buffer's gap.  See Buffer Gap.

modiff
save_modiff
chars_modiff
overlay_modiff
     These fields count the number of buffer-modification events performed in this buffer.  modiff is incremented after each buffer-modification event, and is never otherwise changed; save_modiff contains the value of modiff the last time the buffer was visited or saved; chars_modiff counts only modifications to the characters in the buffer, ignoring all other kinds of changes; and overlay_modiff counts only modifications to the overlays.

beg_unchanged
end_unchanged
     The number of characters at the start and end of the text that are known to be unchanged since the last complete redisplay.

unchanged_modified
overlay_unchanged_modified
     The values of modiff and overlay_modiff, respectively, after the last complete redisplay.  If their current values match modiff or overlay_modiff, that means beg_unchanged and end_unchanged contain no useful information.

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

intervals
     The interval tree which records the text properties of this buffer.

Some of the fields of struct buffer are:

header
     A header of type struct vectorlike_header is common to all vectorlike objects.

own_text
     A struct buffer_text structure that ordinarily holds the buffer contents.  In indirect buffers, this field is not used.

text
     A pointer to the buffer_text structure for this buffer.  In an ordinary buffer, this is the own_text field above.  In an indirect buffer, this is the own_text field of the base buffer.

next
     A pointer to the next buffer, in the chain of all buffers, including killed buffers.  This chain is used only for allocation and garbage collection, in order to collect killed buffers properly.

pt
pt_byte
     The character and byte positions of point in a buffer.

begv
begv_byte
     The character and byte positions of the beginning of the accessible range of text in the buffer.

zv
zv_byte
     The character and byte positions of the end of the accessible range of text in the buffer.

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

local_flags
     This field contains flags indicating that certain variables are local in this buffer.  Such variables are declared in the C code using DEFVAR_PER_BUFFER, and their buffer-local bindings are stored in fields in the buffer structure itself.  (Some of these fields are described in this table.)

modtime
     The modification time of the visited file.  It is set when the file is written or read.  Before writing the buffer into a file, this field is compared to the modification time of the file to see if the file has changed on disk.  See Buffer Modification.

auto_save_modified
     The time when the buffer was last auto-saved.

last_window_start
     The window-start position in the buffer as of the last time the buffer was displayed in a window.

clip_changed
     This flag indicates that narrowing has changed in the buffer. See Narrowing.

prevent_redisplay_optimizations_p
     This flag indicates that redisplay optimizations should not be used to display this buffer.

overlay_center
     This field holds the current overlay center position.  See Managing Overlays.

overlays_before
overlays_after
     These fields hold, respectively, a list of overlays that end at or before the current overlay center, and a list of overlays that end after the current overlay center.  See Managing Overlays. overlays_before is sorted in order of decreasing end position, and overlays_after is sorted in order of increasing beginning position.

name
     A Lisp string that names the buffer.  It is guaranteed to be unique. See Buffer Names.

save_length
     The length of the file this buffer is visiting, when last read or saved.  This and other fields concerned with saving are not kept in the buffer_text structure because indirect buffers are never saved.

directory
     The directory for expanding relative file names.  This is the value of the buffer-local variable default-directory (see File Name Expansion).

filename
     The name of the file visited in this buffer, or nil.  This is the value of the buffer-local variable buffer-file-name (see Buffer File Name).

undo_list
backed_up
auto_save_file_name
auto_save_file_format
read_only
file_format
file_truename
invisibility_spec
display_count
display_time
     These fields store the values of Lisp variables that are automatically buffer-local (see Buffer-Local Variables), whose corresponding variable names have the additional prefix buffer- and have underscores replaced with dashes.  For instance, undo_list stores the value of buffer-undo-list.

mark
     The mark for the buffer.  The mark is a marker, hence it is also included on the list markers.  See The Mark.

local_var_alist
     The association list describing the buffer-local variable bindings of this buffer, not including the built-in buffer-local bindings that have special slots in the buffer object.  (Those slots are omitted from this table.)  See Buffer-Local Variables.

major_mode
     Symbol naming the major mode of this buffer, e.g., lisp-mode.

mode_name
     Pretty name of the major mode, e.g., "Lisp".

keymap
abbrev_table
syntax_table
category_table
display_table
     These fields store the buffer's local keymap (see Keymaps), abbrev table (see Abbrev Tables), syntax table (see Syntax Tables), category table (see Categories), and display table (see Display Tables).

downcase_table
upcase_table
case_canon_table
     These fields store the conversion tables for converting text to lower case, upper case, and for canonicalizing text for case-fold search. See Case Tables.

minor_modes
     An alist of the minor modes of this buffer.

pt_marker
begv_marker
zv_marker
     These fields are only used in an indirect buffer, or in a buffer that is the base of an indirect buffer.  Each holds a marker that records pt, begv, and zv respectively, for this buffer when the buffer is not current.

mode_line_format
header_line_format
case_fold_search
tab_width
fill_column
left_margin
auto_fill_function
truncate_lines
word_wrap
ctl_arrow
bidi_display_reordering
bidi_paragraph_direction
selective_display
selective_display_ellipses
overwrite_mode
abbrev_mode
mark_active
enable_multibyte_characters
buffer_file_coding_system
cache_long_line_scans
point_before_scroll
left_fringe_width
right_fringe_width
fringes_outside_margins
scroll_bar_width
indicate_empty_lines
indicate_buffer_boundaries
fringe_indicator_alist
fringe_cursor_alist
scroll_up_aggressively
scroll_down_aggressively
cursor_type
cursor_in_non_selected_windows
     These fields store the values of Lisp variables that are automatically buffer-local (see Buffer-Local Variables), whose corresponding variable names have underscores replaced with dashes.  For instance, mode_line_format stores the value of mode-line-format.

last_selected_window
     This is the last window that was selected with this buffer in it, or nil if that window no longer displays this buffer.

Window Internals
----------------

The fields of a window (for a complete list, see the definition of struct window in window.h) include:

frame
     The frame that this window is on.

mini_p
     Non-nil if this window is a minibuffer window.

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

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

hchild
vchild
     These fields contain the window's leftmost child and its topmost child respectively.  hchild is used if the window is subdivided horizontally by child windows, and vchild if it is subdivided vertically.  In a live window, only one of hchild, vchild, and buffer (q.v.) is non-nil.

next
prev
     The next sibling and previous sibling of this window.  next is nil if the window is the right-most or bottom-most in its group; prev is nil if it is the left-most or top-most in its group.

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

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

total_cols
total_lines
     The width and height of the window, measured in columns and lines respectively.  The width includes the scroll bar and fringes, and/or the separator line on the right of the window (if any).

buffer
     The buffer that the window is displaying.

start
     A marker pointing to the position in the buffer that is the first character displayed in the window.

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

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

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

start_at_line_beg
     Non-nil means current value of start was the beginning of a line when it was chosen.

use_time
     This is the last time that the window was selected.  The function get-lru-window uses this field.

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

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

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

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

last_had_star
     A non-nil value means the window's buffer was ``modified'' when the window was last updated.

vertical_scroll_bar
     This window's vertical scroll bar.

left_margin_cols
right_margin_cols
     The widths of the left and right margins in this window.  A value of nil means no margin.

left_fringe_width
right_fringe_width
     The widths of the left and right fringes in this window.  A value of nil or t means use the values of the frame.

fringes_outside_margins
     A non-nil value means the fringes outside the display margins; othersize they are between the margin and the text.

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

window_end_bytepos
     The byte position corresponding to window_end_pos.

window_end_vpos
     The window-relative vertical position of the line containing window_end_pos.

window_end_valid
     This field is set to a non-nil value if window_end_pos is truly valid.  This is nil if nontrivial redisplay is pre-empted, since in that case the display that window_end_pos was computed for did not get onto the screen.

cursor
     A structure describing where the cursor is in this window.

last_cursor
     The value of cursor as of the last redisplay that finished.

phys_cursor
     A structure describing where the cursor of this window physically is.

phys_cursor_type
phys_cursor_height
phys_cursor_width
     The type, height, and width of the cursor that was last displayed on this window.

phys_cursor_on_p
     This field is non-zero if the cursor is physically on.

cursor_off_p
     Non-zero means the cursor in this window is logically off.  This is used for blinking the cursor.

last_cursor_off_p
     This field contains the value of cursor_off_p as of the time of the last redisplay.

must_be_updated_p
     This is set to 1 during redisplay when this window must be updated.

hscroll
     This is the number of columns that the display in the window is scrolled horizontally to the left.  Normally, this is 0.

vscroll
     Vertical scroll amount, in pixels.  Normally, this is 0.

dedicated
     Non-nil if this window is dedicated to its buffer.

display_table
     The window's display table, or nil if none is specified for it.

update_mode_line
     Non-nil means this window's mode line needs to be updated.

base_line_number
     The line number of a certain position in the buffer, or nil. This is used for displaying the line number of point in the mode line.

base_line_pos
     The position in the buffer for which the line number is known, or nil meaning none is known.  If it is a buffer, don't display the line number as long as the window shows that buffer.

column_number_displayed
     The column number currently displayed in this window's mode line, or nil if column numbers are not being displayed.

current_matrix
desired_matrix
     Glyph matrices describing the current and desired display of this window.

Process Internals
-----------------

The fields of a process (for a complete list, see the definition of struct Lisp_Process in process.h) include:

name
     A string, the name of the process.

command
     A list containing the command arguments that were used to start this process.  For a network or serial process, it is nil if the process is running or t if the process is stopped.

filter
     A function used to accept output from the process.

sentinel
     A function called whenever the state of the process changes.

buffer
     The associated buffer of the process.

pid
     An integer, the operating system's process ID. Pseudo-processes such as network or serial connections use a value of 0.

childp
     A flag, t if this is really a child process.  For a network or serial connection, it is a plist based on the arguments to make-network-process or make-serial-process.

mark
     A marker indicating the position of the end of the last output from this process inserted into the buffer.  This is often but not always the end of the buffer.

kill_without_query
     If this is non-zero, killing Emacs while this process is still running does not ask for confirmation about killing the process.

raw_status
     The raw process status, as returned by the wait system call.

status
     The process status, as process-status should return it.

tick
update_tick
     If these two fields are not equal, a change in the status of the process needs to be reported, either by running the sentinel or by inserting a message in the process buffer.

pty_flag
     Non-nil if communication with the subprocess uses a pty; nil if it uses a pipe.

infd
     The file descriptor for input from the process.

outfd
     The file descriptor for output to the process.

tty_name
     The name of the terminal that the subprocess is using, or nil if it is using pipes.

decode_coding_system
     Coding-system for decoding the input from this process.

decoding_buf
     A working buffer for decoding.

decoding_carryover
     Size of carryover in decoding.

encode_coding_system
     Coding-system for encoding the output to this process.

encoding_buf
     A working buffer for encoding.

inherit_coding_system_flag
     Flag to set coding-system of the process buffer from the coding system used to decode process output.

type
     Symbol indicating the type of process: real, network, serial.

C Integer Types
===============

Here are some guidelines for use of integer types in the Emacs C source code.  These guidelines sometimes give competing advice; common sense is advised.

  * Avoid arbitrary limits.  For example, avoid int len = strlen (s); unless the length of s is required for other reasons to fit in int range.

  * Do not assume that signed integer arithmetic wraps around on overflow. This is no longer true of Emacs porting targets: signed integer overflow has undefined behavior in practice, and can dump core or even cause earlier or later code to behave ``illogically''.  Unsigned overflow does wrap around reliably, modulo a power of two.

  * Prefer signed types to unsigned, as code gets confusing when signed and unsigned types are combined.  Many other guidelines assume that types are signed; in the rarer cases where unsigned types are needed, similar advice may apply to the unsigned counterparts (e.g., size_t instead of ptrdiff_t, or uintptr_t instead of intptr_t).

  * Prefer int for Emacs character codes, in the range 0 .. 0x3FFFFF.

  * Prefer ptrdiff_t for sizes, i.e., for integers bounded by the maximum size of any individual C object or by the maximum number of elements in any C array.  This is part of Emacs's general preference for signed types.  Using ptrdiff_t limits objects to PTRDIFF_MAX bytes, but larger objects would cause trouble anyway since they would break pointer subtraction, so this does not impose an arbitrary limit.

  * Prefer intptr_t for internal representations of pointers, or for integers bounded only by the number of objects that can exist at any given time or by the total number of bytes that can be allocated. Currently Emacs sometimes uses other types when intptr_t would be better; fixing this is lower priority, as the code works as-is on Emacs's current porting targets.

  * Prefer the Emacs-defined type EMACS_INT for representing values converted to or from Emacs Lisp fixnums, as fixnum arithmetic is based on EMACS_INT.

  * When representing a system value (such as a file size or a count of seconds since the Epoch), prefer the corresponding system type (e.g., off_t, time_t).  Do not assume that a system type is signed, unless this assumption is known to be safe.  For example, although off_t is always signed, time_t need not be.

  * Prefer the Emacs-defined type printmax_t for representing values that might be any signed integer value that can be printed, using a printf-family function.

  * Prefer intmax_t for representing values that might be any signed integer value.

  * In bitfields, prefer unsigned int or signed int to int, as int is less portable: it might be signed, and might not be.  Single-bit bit fields are invariably unsigned int so that their values are 0 and 1.

  * In C, Emacs commonly uses bool, 1, and 0 for boolean values. Using bool for booleans can make programs easier to read and a bit faster than using int.  Although it is also OK to use int, this older style is gradually being phased out.  When using bool, respect the limitations of the replacement implementation of bool, as documented in the source file lib/stdbool.in.h, so that Emacs remains portable to pre-C99 platforms.