1 This is ../info/internals.info, produced by makeinfo version 4.0 from
2 internals/internals.texi.
4 INFO-DIR-SECTION XEmacs Editor
6 * Internals: (internals). XEmacs Internals Manual.
9 Copyright (C) 1992 - 1996 Ben Wing. Copyright (C) 1996, 1997 Sun
10 Microsystems. Copyright (C) 1994 - 1998 Free Software Foundation.
11 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
13 Permission is granted to make and distribute verbatim copies of this
14 manual provided the copyright notice and this permission notice are
15 preserved on all copies.
17 Permission is granted to copy and distribute modified versions of
18 this manual under the conditions for verbatim copying, provided that the
19 entire resulting derived work is distributed under the terms of a
20 permission notice identical to this one.
22 Permission is granted to copy and distribute translations of this
23 manual into another language, under the above conditions for modified
24 versions, except that this permission notice may be stated in a
25 translation approved by the Foundation.
27 Permission is granted to copy and distribute modified versions of
28 this manual under the conditions for verbatim copying, provided also
29 that the section entitled "GNU General Public License" is included
30 exactly as in the original, and provided that the entire resulting
31 derived work is distributed under the terms of a permission notice
32 identical to this one.
34 Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that the section entitled "GNU General Public License"
37 may be included in a translation approved by the Free Software
38 Foundation instead of in the original English.
41 File: internals.info, Node: Working With Character and Byte Positions, Next: Conversion to and from External Data, Prev: Character-Related Data Types, Up: Coding for Mule
43 Working With Character and Byte Positions
44 -----------------------------------------
46 Now that we have defined the basic character-related types, we can
47 look at the macros and functions designed for work with them and for
48 conversion between them. Most of these macros are defined in
49 `buffer.h', and we don't discuss all of them here, but only the most
50 important ones. Examining the existing code is the best way to learn
54 This preprocessor constant is the maximum number of buffer bytes to
55 represent an Emacs character in the variable width internal
56 encoding. It is useful when allocating temporary strings to keep
57 a known number of characters. For instance:
63 /* Allocate place for CCLEN characters. */
64 Bufbyte *buf = (Bufbyte *)alloca (cclen * MAX_EMCHAR_LEN);
67 If you followed the previous section, you can guess that,
68 logically, multiplying a `Charcount' value with `MAX_EMCHAR_LEN'
69 produces a `Bytecount' value.
71 In the current Mule implementation, `MAX_EMCHAR_LEN' equals 4.
72 Without Mule, it is 1.
76 The `charptr_emchar' macro takes a `Bufbyte' pointer and returns
77 the `Emchar' stored at that position. If it were a function, its
80 Emchar charptr_emchar (Bufbyte *p);
82 `set_charptr_emchar' stores an `Emchar' to the specified byte
83 position. It returns the number of bytes stored:
85 Bytecount set_charptr_emchar (Bufbyte *p, Emchar c);
87 It is important to note that `set_charptr_emchar' is safe only for
88 appending a character at the end of a buffer, not for overwriting a
89 character in the middle. This is because the width of characters
90 varies, and `set_charptr_emchar' cannot resize the string if it
91 writes, say, a two-byte character where a single-byte character
94 A typical use of `set_charptr_emchar' can be demonstrated by this
95 example, which copies characters from buffer BUF to a temporary
100 for (pos = beg; pos < end; pos++)
102 Emchar c = BUF_FETCH_CHAR (buf, pos);
103 p += set_charptr_emchar (buf, c);
107 Note how `set_charptr_emchar' is used to store the `Emchar' and
108 increment the counter, at the same time.
112 These two macros increment and decrement a `Bufbyte' pointer,
113 respectively. They will adjust the pointer by the appropriate
114 number of bytes according to the byte length of the character
115 stored there. Both macros assume that the memory address is
116 located at the beginning of a valid character.
118 Without Mule support, `INC_CHARPTR (p)' and `DEC_CHARPTR (p)'
119 simply expand to `p++' and `p--', respectively.
121 `bytecount_to_charcount'
122 Given a pointer to a text string and a length in bytes, return the
123 equivalent length in characters.
125 Charcount bytecount_to_charcount (Bufbyte *p, Bytecount bc);
127 `charcount_to_bytecount'
128 Given a pointer to a text string and a length in characters,
129 return the equivalent length in bytes.
131 Bytecount charcount_to_bytecount (Bufbyte *p, Charcount cc);
134 Return a pointer to the beginning of the character offset CC (in
137 Bufbyte *charptr_n_addr (Bufbyte *p, Charcount cc);
140 File: internals.info, Node: Conversion to and from External Data, Next: General Guidelines for Writing Mule-Aware Code, Prev: Working With Character and Byte Positions, Up: Coding for Mule
142 Conversion to and from External Data
143 ------------------------------------
145 When an external function, such as a C library function, returns a
146 `char' pointer, you should almost never treat it as `Bufbyte'. This is
147 because these returned strings may contain 8bit characters which can be
148 misinterpreted by XEmacs, and cause a crash. Likewise, when exporting
149 a piece of internal text to the outside world, you should always
150 convert it to an appropriate external encoding, lest the internal stuff
151 (such as the infamous \201 characters) leak out.
153 The interface to conversion between the internal and external
154 representations of text are the numerous conversion macros defined in
155 `buffer.h'. There used to be a fixed set of external formats supported
156 by these macros, but now any coding system can be used with these
157 macros. The coding system alias mechanism is used to create the
158 following logical coding systems, which replace the fixed external
159 formats. The (dontusethis-set-symbol-value-handler) mechanism was
160 enhanced to make this possible (more work on that is needed - like
161 remove the `dontusethis-' prefix).
164 This is the simplest format and is what we use in the absence of a
165 more appropriate format. This converts according to the `binary'
168 a. On input, bytes 0-255 are converted into (implicitly Latin-1)
169 characters 0-255. A non-Mule xemacs doesn't really know about
170 different character sets and the fonts to display them, so
171 the bytes can be treated as text in different 1-byte
172 encodings by simply setting the appropriate fonts. So in a
173 sense, non-Mule xemacs is a multi-lingual editor if, for
174 example, different fonts are used to display text in
175 different buffers, faces, or windows. The specifier
176 mechanism gives the user complete control over this kind of
179 b. On output, characters 0-255 are converted into bytes 0-255
180 and other characters are converted into `~'.
183 Format used for filenames. This is user-definable via either the
184 `file-name-coding-system' or `pathname-coding-system' (now
188 Format used for the external Unix environment--`argv[]', stuff
189 from `getenv()', stuff from the `/etc/passwd' file, etc.
190 Currently this is the same as Qfile_name. The two should be
191 distinguished for clarity and possible future separation.
194 Compound-text format. This is the standard X11 format used for
195 data stored in properties, selections, and the like. This is an
196 8-bit no-lock-shift ISO2022 coding system. This is a real coding
197 system, unlike Qfile_name, which is user-definable.
199 There are two fundamental macros to convert between external and
202 `TO_INTERNAL_FORMAT' converts external data to internal format, and
203 `TO_EXTERNAL_FORMAT' converts the other way around. The arguments each
204 of these receives are a source type, a source, a sink type, a sink, and
205 a coding system (or a symbol naming a coding system).
207 A typical call looks like
208 TO_EXTERNAL_FORMAT (LISP_STRING, str, C_STRING_MALLOC, ptr, Qfile_name);
210 which means that the contents of the lisp string `str' are written
211 to a malloc'ed memory area which will be pointed to by `ptr', after the
212 function returns. The conversion will be done using the `file-name'
213 coding system, which will be controlled by the user indirectly by
214 setting or binding the variable `file-name-coding-system'.
216 Some sources and sinks require two C variables to specify. We use
217 some preprocessor magic to allow different source and sink types, and
218 even different numbers of arguments to specify different types of
221 So we can have a call that looks like
222 TO_INTERNAL_FORMAT (DATA, (ptr, len),
226 The parenthesized argument pairs are required to make the
227 preprocessor magic work.
229 Here are the different source and sink types:
231 ``DATA, (ptr, len),''
232 input data is a fixed buffer of size LEN at address PTR
234 ``ALLOCA, (ptr, len),''
235 output data is placed in an alloca()ed buffer of size LEN pointed
238 ``MALLOC, (ptr, len),''
239 output data is in a malloc()ed buffer of size LEN pointed to by PTR
241 ``C_STRING_ALLOCA, ptr,''
242 equivalent to `ALLOCA (ptr, len_ignored)' on output.
244 ``C_STRING_MALLOC, ptr,''
245 equivalent to `MALLOC (ptr, len_ignored)' on output
248 equivalent to `DATA, (ptr, strlen (ptr) + 1)' on input
250 ``LISP_STRING, string,''
251 input or output is a Lisp_Object of type string
253 ``LISP_BUFFER, buffer,''
254 output is written to `(point)' in lisp buffer BUFFER
256 ``LISP_LSTREAM, lstream,''
257 input or output is a Lisp_Object of type lstream
259 ``LISP_OPAQUE, object,''
260 input or output is a Lisp_Object of type opaque
262 Often, the data is being converted to a '\0'-byte-terminated string,
263 which is the format required by many external system C APIs. For these
264 purposes, a source type of `C_STRING' or a sink type of
265 `C_STRING_ALLOCA' or `C_STRING_MALLOC' is appropriate. Otherwise, we
266 should try to keep XEmacs '\0'-byte-clean, which means using (ptr, len)
269 The sinks to be specified must be lvalues, unless they are the lisp
270 object types `LISP_LSTREAM' or `LISP_BUFFER'.
272 For the sink types `ALLOCA' and `C_STRING_ALLOCA', the resulting
273 text is stored in a stack-allocated buffer, which is automatically
274 freed on returning from the function. However, the sink types `MALLOC'
275 and `C_STRING_MALLOC' return `xmalloc()'ed memory. The caller is
276 responsible for freeing this memory using `xfree()'.
278 Note that it doesn't make sense for `LISP_STRING' to be a source for
279 `TO_INTERNAL_FORMAT' or a sink for `TO_EXTERNAL_FORMAT'. You'll get an
280 assertion failure if you try.
283 File: internals.info, Node: General Guidelines for Writing Mule-Aware Code, Next: An Example of Mule-Aware Code, Prev: Conversion to and from External Data, Up: Coding for Mule
285 General Guidelines for Writing Mule-Aware Code
286 ----------------------------------------------
288 This section contains some general guidance on how to write
289 Mule-aware code, as well as some pitfalls you should avoid.
291 _Never use `char' and `char *'._
292 In XEmacs, the use of `char' and `char *' is almost always a
293 mistake. If you want to manipulate an Emacs character from "C",
294 use `Emchar'. If you want to examine a specific octet in the
295 internal format, use `Bufbyte'. If you want a Lisp-visible
296 character, use a `Lisp_Object' and `make_char'. If you want a
297 pointer to move through the internal text, use `Bufbyte *'. Also
298 note that you almost certainly do not need `Emchar *'.
300 _Be careful not to confuse `Charcount', `Bytecount', and `Bufpos'._
301 The whole point of using different types is to avoid confusion
302 about the use of certain variables. Lest this effect be
303 nullified, you need to be careful about using the right types.
305 _Always convert external data_
306 It is extremely important to always convert external data, because
307 XEmacs can crash if unexpected 8bit sequences are copied to its
308 internal buffers literally.
310 This means that when a system function, such as `readdir', returns
311 a string, you may need to convert it using one of the conversion
312 macros described in the previous chapter, before passing it
315 Actually, most of the basic system functions that accept
316 '\0'-terminated string arguments, like `stat()' and `open()', have
317 been *encapsulated* so that they are they `always' do internal to
318 external conversion themselves. This means you must pass
319 internally encoded data, typically the `XSTRING_DATA' of a
320 Lisp_String to these functions. This is actually a design bug,
321 since it unexpectedly changes the semantics of the system
322 functions. A better design would be to provide separate versions
323 of these system functions that accepted Lisp_Objects which were
324 lisp strings in place of their current `char *' arguments.
326 int stat_lisp (Lisp_Object path, struct stat *buf); /* Implement me */
328 Also note that many internal functions, such as `make_string',
329 accept Bufbytes, which removes the need for them to convert the
330 data they receive. This increases efficiency because that way
331 external data needs to be decoded only once, when it is read.
332 After that, it is passed around in internal format.
335 File: internals.info, Node: An Example of Mule-Aware Code, Prev: General Guidelines for Writing Mule-Aware Code, Up: Coding for Mule
337 An Example of Mule-Aware Code
338 -----------------------------
340 As an example of Mule-aware code, we will analyze the `string'
341 function, which conses up a Lisp string from the character arguments it
342 receives. Here is the definition, pasted from `alloc.c':
344 DEFUN ("string", Fstring, 0, MANY, 0, /*
345 Concatenate all the argument characters and make the result a string.
347 (int nargs, Lisp_Object *args))
349 Bufbyte *storage = alloca_array (Bufbyte, nargs * MAX_EMCHAR_LEN);
350 Bufbyte *p = storage;
352 for (; nargs; nargs--, args++)
354 Lisp_Object lisp_char = *args;
355 CHECK_CHAR_COERCE_INT (lisp_char);
356 p += set_charptr_emchar (p, XCHAR (lisp_char));
358 return make_string (storage, p - storage);
361 Now we can analyze the source line by line.
363 Obviously, string will be as long as there are arguments to the
364 function. This is why we allocate `MAX_EMCHAR_LEN' * NARGS bytes on
365 the stack, i.e. the worst-case number of bytes for NARGS `Emchar's to
368 Then, the loop checks that each element is a character, converting
369 integers in the process. Like many other functions in XEmacs, this
370 function silently accepts integers where characters are expected, for
371 historical and compatibility reasons. Unless you know what you are
372 doing, `CHECK_CHAR' will also suffice. `XCHAR (lisp_char)' extracts
373 the `Emchar' from the `Lisp_Object', and `set_charptr_emchar' stores it
374 to storage, increasing `p' in the process.
376 Other instructive examples of correct coding under Mule can be found
377 all over the XEmacs code. For starters, I recommend
378 `Fnormalize_menu_item_name' in `menubar.c'. After you have understood
379 this section of the manual and studied the examples, you can proceed
380 writing new Mule-aware code.
383 File: internals.info, Node: Techniques for XEmacs Developers, Prev: Coding for Mule, Up: Rules When Writing New C Code
385 Techniques for XEmacs Developers
386 ================================
388 To make a purified XEmacs, do: `make puremacs'. To make a
389 quantified XEmacs, do: `make quantmacs'.
391 You simply can't dump Quantified and Purified images (unless using
392 the portable dumper). Purify gets confused when xemacs frees memory in
393 one process that was allocated in a _different_ process on a different
394 machine!. Run it like so:
395 temacs -batch -l loadup.el run-temacs XEMACS-ARGS...
397 Before you go through the trouble, are you compiling with all
398 debugging and error-checking off? If not, try that first. Be warned
399 that while Quantify is directly responsible for quite a few
400 optimizations which have been made to XEmacs, doing a run which
401 generates results which can be acted upon is not necessarily a trivial
404 Also, if you're still willing to do some runs make sure you configure
405 with the `--quantify' flag. That will keep Quantify from starting to
406 record data until after the loadup is completed and will shut off
407 recording right before it shuts down (which generates enough bogus data
408 to throw most results off). It also enables three additional elisp
409 commands: `quantify-start-recording-data',
410 `quantify-stop-recording-data' and `quantify-clear-data'.
412 If you want to make XEmacs faster, target your favorite slow
413 benchmark, run a profiler like Quantify, `gprof', or `tcov', and figure
414 out where the cycles are going. Specific projects:
416 * Make the garbage collector faster. Figure out how to write an
417 incremental garbage collector.
419 * Write a compiler that takes bytecode and spits out C code.
420 Unfortunately, you will then need a C compiler and a more fully
421 developed module system.
423 * Speed up redisplay.
425 * Speed up syntax highlighting. Maybe moving some of the syntax
426 highlighting capabilities into C would make a difference.
428 * Implement tail recursion in Emacs Lisp (hard!).
430 Unfortunately, Emacs Lisp is slow, and is going to stay slow.
431 Function calls in elisp are especially expensive. Iterating over a
432 long list is going to be 30 times faster implemented in C than in Elisp.
434 Heavily used small code fragments need to be fast. The traditional
435 way to implement such code fragments in C is with macros. But macros
436 in C are known to be broken.
438 Macro arguments that are repeatedly evaluated may suffer from
439 repeated side effects or suboptimal performance.
441 Variable names used in macros may collide with caller's variables,
442 causing (at least) unwanted compiler warnings.
444 In order to solve these problems, and maintain statement semantics,
445 one should use the `do { ... } while (0)' trick while trying to
446 reference macro arguments exactly once using local variables.
448 Let's take a look at this poor macro definition:
450 #define MARK_OBJECT(obj) \
451 if (!marked_p (obj)) mark_object (obj), did_mark = 1
453 This macro evaluates its argument twice, and also fails if used like
455 if (flag) MARK_OBJECT (obj); else do_something();
457 A much better definition is
459 #define MARK_OBJECT(obj) do { \
460 Lisp_Object mo_obj = (obj); \
461 if (!marked_p (mo_obj)) \
463 mark_object (mo_obj); \
468 Notice the elimination of double evaluation by using the local
469 variable with the obscure name. Writing safe and efficient macros
470 requires great care. The one problem with macros that cannot be
471 portably worked around is, since a C block has no value, a macro used
472 as an expression rather than a statement cannot use the techniques just
473 described to avoid multiple evaluation.
475 In most cases where a macro has function semantics, an inline
476 function is a better implementation technique. Modern compiler
477 optimizers tend to inline functions even if they have no `inline'
478 keyword, and configure magic ensures that the `inline' keyword can be
479 safely used as an additional compiler hint. Inline functions used in a
480 single .c files are easy. The function must already be defined to be
481 `static'. Just add another `inline' keyword to the definition.
484 heavily_used_small_function (int arg)
489 Inline functions in header files are trickier, because we would like
490 to make the following optimization if the function is _not_ inlined
491 (for example, because we're compiling for debugging). We would like the
492 function to be defined externally exactly once, and each calling
493 translation unit would create an external reference to the function,
494 instead of including a definition of the inline function in the object
495 code of every translation unit that uses it. This optimization is
496 currently only available for gcc. But you don't have to worry about the
497 trickiness; just define your inline functions in header files using this
501 i_used_to_be_a_crufty_macro_but_look_at_me_now (int arg);
503 i_used_to_be_a_crufty_macro_but_look_at_me_now (int arg)
508 The declaration right before the definition is to prevent warnings
509 when compiling with `gcc -Wmissing-declarations'. I consider issuing
510 this warning for inline functions a gcc bug, but the gcc maintainers
513 Every header which contains inline functions, either directly by
514 using `INLINE_HEADER' or indirectly by using `DECLARE_LRECORD' must be
515 added to `inline.c''s includes to make the optimization described above
516 work. (Optimization note: if all INLINE_HEADER functions are in fact
517 inlined in all translation units, then the linker can just discard
518 `inline.o', since it contains only unreferenced code).
520 To get started debugging XEmacs, take a look at the `.gdbinit' and
521 `.dbxrc' files in the `src' directory. See the section in the XEmacs
522 FAQ on How to Debug an XEmacs problem with a debugger.
524 After making source code changes, run `make check' to ensure that
525 you haven't introduced any regressions. If you want to make xemacs more
526 reliable, please improve the test suite in `tests/automated'.
528 Did you make sure you didn't introduce any new compiler warnings?
530 Before submitting a patch, please try compiling at least once with
532 configure --with-mule --with-union-type --error-checking=all
534 Here are things to know when you create a new source file:
536 * All `.c' files should `#include <config.h>' first. Almost all
537 `.c' files should `#include "lisp.h"' second.
539 * Generated header files should be included using the `#include
540 <...>' syntax, not the `#include "..."' syntax. The generated
543 `config.h sheap-adjust.h paths.h Emacs.ad.h'
545 The basic rule is that you should assume builds using `--srcdir'
546 and the `#include <...>' syntax needs to be used when the
547 to-be-included generated file is in a potentially different
548 directory _at compile time_. The non-obvious C rule is that
549 `#include "..."' means to search for the included file in the same
550 directory as the including file, _not_ in the current directory.
552 * Header files should _not_ include `<config.h>' and `"lisp.h"'. It
553 is the responsibility of the `.c' files that use it to do so.
556 Here is a checklist of things to do when creating a new lisp object
563 3. add definitions of `syms_of_FOO', etc. to `FOO.c'
565 4. add declarations of `syms_of_FOO', etc. to `symsinit.h'
567 5. add calls to `syms_of_FOO', etc. to `emacs.c'
569 6. add definitions of macros like `CHECK_FOO' and `FOOP' to `FOO.h'
571 7. add the new type index to `enum lrecord_type'
573 8. add a DEFINE_LRECORD_IMPLEMENTATION call to `FOO.c'
575 9. add an INIT_LRECORD_IMPLEMENTATION call to `syms_of_FOO.c'
578 File: internals.info, Node: A Summary of the Various XEmacs Modules, Next: Allocation of Objects in XEmacs Lisp, Prev: Rules When Writing New C Code, Up: Top
580 A Summary of the Various XEmacs Modules
581 ***************************************
583 This is accurate as of XEmacs 20.0.
587 * Low-Level Modules::
588 * Basic Lisp Modules::
589 * Modules for Standard Editing Operations::
590 * Editor-Level Control Flow Modules::
591 * Modules for the Basic Displayable Lisp Objects::
592 * Modules for other Display-Related Lisp Objects::
593 * Modules for the Redisplay Mechanism::
594 * Modules for Interfacing with the File System::
595 * Modules for Other Aspects of the Lisp Interpreter and Object System::
596 * Modules for Interfacing with the Operating System::
597 * Modules for Interfacing with X Windows::
598 * Modules for Internationalization::
601 File: internals.info, Node: Low-Level Modules, Next: Basic Lisp Modules, Up: A Summary of the Various XEmacs Modules
608 This is automatically generated from `config.h.in' based on the
609 results of configure tests and user-selected optional features and
610 contains preprocessor definitions specifying the nature of the
611 environment in which XEmacs is being compiled.
615 This is automatically generated from `paths.h.in' based on supplied
616 configure values, and allows for non-standard installed configurations
617 of the XEmacs directories. It's currently broken, though.
622 `emacs.c' contains `main()' and other code that performs the most
623 basic environment initializations and handles shutting down the XEmacs
624 process (this includes `kill-emacs', the normal way that XEmacs is
625 exited; `dump-emacs', which is used during the build process to write
626 out the XEmacs executable; `run-emacs-from-temacs', which can be used
627 to start XEmacs directly when temacs has finished loading all the Lisp
628 code; and emergency code to handle crashes [XEmacs tries to auto-save
629 all files before it crashes]).
631 Low-level code that directly interacts with the Unix signal
632 mechanism, however, is in `signal.c'. Note that this code does not
633 handle system dependencies in interfacing to signals; that is handled
634 using the `syssignal.h' header file, described in section J below.
654 These modules contain code dumping out the XEmacs executable on
655 various different systems. (This process is highly machine-specific and
656 requires intimate knowledge of the executable format and the memory map
657 of the process.) Only one of these modules is actually used; this is
658 chosen by `configure'.
664 These modules are used in conjunction with the dump mechanism. On
665 some systems, an alternative version of the C startup code (the actual
666 code that receives control from the operating system when the process is
667 started, and which calls `main()') is required so that the dumping
668 process works properly; `crt0.c' provides this.
670 `pre-crt0.c' and `lastfile.c' should be the very first and very last
671 file linked, respectively. (Actually, this is not really true.
672 `lastfile.c' should be after all Emacs modules whose initialized data
673 should be made constant, and before all other Emacs files and all
674 libraries. In particular, the allocation modules `gmalloc.c',
675 `alloca.c', etc. are normally placed past `lastfile.c', and all of the
676 files that implement Xt widget classes _must_ be placed after
677 `lastfile.c' because they contain various structures that must be
678 statically initialized and into which Xt writes at various times.)
679 `pre-crt0.c' and `lastfile.c' contain exported symbols that are used to
680 determine the start and end of XEmacs' initialized data space when
692 These handle basic C allocation of memory. `alloca.c' is an
693 emulation of the stack allocation function `alloca()' on machines that
694 lack this. (XEmacs makes extensive use of `alloca()' in its code.)
696 `gmalloc.c' and `malloc.c' are two implementations of the standard C
697 functions `malloc()', `realloc()' and `free()'. They are often used in
698 place of the standard system-provided `malloc()' because they usually
699 provide a much faster implementation, at the expense of additional
700 memory use. `gmalloc.c' is a newer implementation that is much more
701 memory-efficient for large allocations than `malloc.c', and should
702 always be preferred if it works. (At one point, `gmalloc.c' didn't work
703 on some systems where `malloc.c' worked; but this should be fixed now.)
705 `ralloc.c' is the "relocating allocator". It provides functions
706 similar to `malloc()', `realloc()' and `free()' that allocate memory
707 that can be dynamically relocated in memory. The advantage of this is
708 that allocated memory can be shuffled around to place all the free
709 memory at the end of the heap, and the heap can then be shrunk,
710 releasing the memory back to the operating system. The use of this can
711 be controlled with the configure option `--rel-alloc'; if enabled,
712 memory allocated for buffers will be relocatable, so that if a very
713 large file is visited and the buffer is later killed, the memory can be
714 released to the operating system. (The disadvantage of this mechanism
715 is that it can be very slow. On systems with the `mmap()' system call,
716 the XEmacs version of `ralloc.c' uses this to move memory around
717 without actually having to block-copy it, which can speed things up;
718 but it can still cause noticeable performance degradation.)
720 `free-hook.c' contains some debugging functions for checking for
721 invalid arguments to `free()'.
723 `vm-limit.c' contains some functions that warn the user when memory
724 is getting low. These are callback functions that are called by
725 `gmalloc.c' and `malloc.c' at appropriate times.
727 `getpagesize.h' provides a uniform interface for retrieving the size
728 of a page in virtual memory. `mem-limits.h' provides a uniform
729 interface for retrieving the total amount of available virtual memory.
730 Both are similar in spirit to the `sys*.h' files described in section
737 These implement a couple of basic C data types to facilitate memory
738 allocation. The `Blocktype' type efficiently manages the allocation of
739 fixed-size blocks by minimizing the number of times that `malloc()' and
740 `free()' are called. It allocates memory in large chunks, subdivides
741 the chunks into blocks of the proper size, and returns the blocks as
742 requested. When blocks are freed, they are placed onto a linked list,
743 so they can be efficiently reused. This data type is not much used in
744 XEmacs currently, because it's a fairly new addition.
746 The `Dynarr' type implements a "dynamic array", which is similar to
747 a standard C array but has no fixed limit on the number of elements it
748 can contain. Dynamic arrays can hold elements of any type, and when
749 you add a new element, the array automatically resizes itself if it
750 isn't big enough. Dynarrs are extensively used in the redisplay
755 This module is used in connection with inline functions (available in
756 some compilers). Often, inline functions need to have a corresponding
757 non-inline function that does the same thing. This module is where they
758 reside. It contains no actual code, but defines some special flags that
759 cause inline functions defined in header files to be rendered as actual
760 functions. It then includes all header files that contain any inline
761 function definitions, so that each one gets a real function equivalent.
766 These functions provide a system for doing internal consistency
767 checks during code development. This system is not currently used;
768 instead the simpler `assert()' macro is used along with the various
769 checks provided by the `--error-check-*' configuration options.
773 This is not currently used.
776 File: internals.info, Node: Basic Lisp Modules, Next: Modules for Standard Editing Operations, Prev: Low-Level Modules, Up: A Summary of the Various XEmacs Modules
787 These are the basic header files for all XEmacs modules. Each module
788 includes `lisp.h', which brings the other header files in. `lisp.h'
789 contains the definitions of the structures and extractor and
790 constructor macros for the basic Lisp objects and various other basic
791 definitions for the Lisp environment, as well as some general-purpose
792 definitions (e.g. `min()' and `max()'). `lisp.h' includes either
793 `lisp-disunion.h' or `lisp-union.h', depending on whether
794 `USE_UNION_TYPE' is defined. These files define the typedef of the
795 Lisp object itself (as described above) and the low-level macros that
796 hide the actual implementation of the Lisp object. All extractor and
797 constructor macros for particular types of Lisp objects are defined in
798 terms of these low-level macros.
800 As a general rule, all typedefs should go into the typedefs section
801 of `lisp.h' rather than into a module-specific header file even if the
802 structure is defined elsewhere. This allows function prototypes that
803 use the typedef to be placed into other header files. Forward structure
804 declarations (i.e. a simple declaration like `struct foo;' where the
805 structure itself is defined elsewhere) should be placed into the
806 typedefs section as necessary.
808 `lrecord.h' contains the basic structures and macros that implement
809 all record-type Lisp objects--i.e. all objects whose type is a field in
810 their C structure, which includes all objects except the few most basic
813 `lisp.h' contains prototypes for most of the exported functions in
814 the various modules. Lisp primitives defined using `DEFUN' that need
815 to be called by C code should be declared using `EXFUN'. Other
816 function prototypes should be placed either into the appropriate
817 section of `lisp.h', or into a module-specific header file, depending
818 on how general-purpose the function is and whether it has
819 special-purpose argument types requiring definitions not in `lisp.h'.)
820 All initialization functions are prototyped in `symsinit.h'.
824 The large module `alloc.c' implements all of the basic allocation and
825 garbage collection for Lisp objects. The most commonly used Lisp
826 objects are allocated in chunks, similar to the Blocktype data type
827 described above; others are allocated in individually `malloc()'ed
828 blocks. This module provides the foundation on which all other aspects
829 of the Lisp environment sit, and is the first module initialized at
832 Note that `alloc.c' provides a series of generic functions that are
833 not dependent on any particular object type, and interfaces to
834 particular types of objects using a standardized interface of
835 type-specific methods. This scheme is a fundamental principle of
836 object-oriented programming and is heavily used throughout XEmacs. The
837 great advantage of this is that it allows for a clean separation of
838 functionality into different modules--new classes of Lisp objects, new
839 event interfaces, new device types, new stream interfaces, etc. can be
840 added transparently without affecting code anywhere else in XEmacs.
841 Because the different subsystems are divided into general and specific
842 code, adding a new subtype within a subsystem will in general not
843 require changes to the generic subsystem code or affect any of the other
844 subtypes in the subsystem; this provides a great deal of robustness to
850 This module contains all of the functions to handle the flow of
851 control. This includes the mechanisms of defining functions, calling
852 functions, traversing stack frames, and binding variables; the control
853 primitives and other special forms such as `while', `if', `eval',
854 `let', `and', `or', `progn', etc.; handling of non-local exits,
855 unwind-protects, and exception handlers; entering the debugger; methods
856 for the subr Lisp object type; etc. It does _not_ include the `read'
857 function, the `print' function, or the handling of symbols and obarrays.
859 `backtrace.h' contains some structures related to stack frames and
864 This module implements the Lisp reader and the `read' function,
865 which converts text into Lisp objects, according to the read syntax of
866 the objects, as described above. This is similar to the parser that is
867 a part of all compilers.
871 This module implements the Lisp print mechanism and the `print'
872 function and related functions. This is the inverse of the Lisp reader
873 - it converts Lisp objects to a printed, textual representation.
874 (Hopefully something that can be read back in using `read' to get an
881 `symbols.c' implements the handling of symbols, obarrays, and
882 retrieving the values of symbols. Much of the code is devoted to
883 handling the special "symbol-value-magic" objects that define special
884 types of variables--this includes buffer-local variables, variable
885 aliases, variables that forward into C variables, etc. This module is
886 initialized extremely early (right after `alloc.c'), because it is here
887 that the basic symbols `t' and `nil' are created, and those symbols are
888 used everywhere throughout XEmacs.
890 `symeval.h' contains the definitions of symbol structures and the
891 `DEFVAR_LISP()' and related macros for declaring variables.
897 These modules implement the methods and standard Lisp primitives for
898 all the basic Lisp object types other than symbols (which are described
899 above). `data.c' contains all the predicates (primitives that return
900 whether an object is of a particular type); the integer arithmetic
901 functions; and the basic accessor and mutator primitives for the various
902 object types. `fns.c' contains all the standard predicates for working
903 with sequences (where, abstractly speaking, a sequence is an ordered set
904 of objects, and can be represented by a list, string, vector, or
905 bit-vector); it also contains `equal', perhaps on the grounds that bulk
906 of the operation of `equal' is comparing sequences. `floatfns.c'
907 contains methods and primitives for floats and floating-point
913 `bytecode.c' implements the byte-code interpreter and
914 compiled-function objects, and `bytecode.h' contains associated
915 structures. Note that the byte-code _compiler_ is written in Lisp.
918 File: internals.info, Node: Modules for Standard Editing Operations, Next: Editor-Level Control Flow Modules, Prev: Basic Lisp Modules, Up: A Summary of the Various XEmacs Modules
920 Modules for Standard Editing Operations
921 =======================================
927 `buffer.c' implements the "buffer" Lisp object type. This includes
928 functions that create and destroy buffers; retrieve buffers by name or
929 by other properties; manipulate lists of buffers (remember that buffers
930 are permanent objects and stored in various ordered lists); retrieve or
931 change buffer properties; etc. It also contains the definitions of all
932 the built-in buffer-local variables (which can be viewed as buffer
933 properties). It does _not_ contain code to manipulate buffer-local
934 variables (that's in `symbols.c', described above); or code to
935 manipulate the text in a buffer.
937 `buffer.h' defines the structures associated with a buffer and the
938 various macros for retrieving text from a buffer and special buffer
939 positions (e.g. `point', the default location for text insertion). It
940 also contains macros for working with buffer positions and converting
941 between their representations as character offsets and as byte offsets
942 (under MULE, they are different, because characters can be multi-byte).
943 It is one of the largest header files.
945 `bufslots.h' defines the fields in the buffer structure that
946 correspond to the built-in buffer-local variables. It is its own
947 header file because it is included many times in `buffer.c', as a way
948 of iterating over all the built-in buffer-local variables.
953 `insdel.c' contains low-level functions for inserting and deleting
954 text in a buffer, keeping track of changed regions for use by
955 redisplay, and calling any before-change and after-change functions
956 that may have been registered for the buffer. It also contains the
957 actual functions that convert between byte offsets and character
960 `insdel.h' contains associated headers.
964 This module implements the "marker" Lisp object type, which
965 conceptually is a pointer to a text position in a buffer that moves
966 around as text is inserted and deleted, so as to remain in the same
967 relative position. This module doesn't actually move the markers around
968 - that's handled in `insdel.c'. This module just creates them and
969 implements the primitives for working with them. As markers are simple
970 objects, this does not entail much.
972 Note that the standard arithmetic primitives (e.g. `+') accept
973 markers in place of integers and automatically substitute the value of
974 `marker-position' for the marker, i.e. an integer describing the
975 current buffer position of the marker.
980 This module implements the "extent" Lisp object type, which is like
981 a marker that works over a range of text rather than a single position.
982 Extents are also much more complex and powerful than markers and have a
983 more efficient (and more algorithmically complex) implementation. The
984 implementation is described in detail in comments in `extents.c'.
986 The code in `extents.c' works closely with `insdel.c' so that
987 extents are properly moved around as text is inserted and deleted.
988 There is also code in `extents.c' that provides information needed by
989 the redisplay mechanism for efficient operation. (Remember that extents
990 can have display properties that affect [sometimes drastically, as in
991 the `invisible' property] the display of the text they cover.)
995 `editfns.c' contains the standard Lisp primitives for working with a
996 buffer's text, and calls the low-level functions in `insdel.c'. It
997 also contains primitives for working with `point' (the default buffer
1000 `editfns.c' also contains functions for retrieving various
1001 characteristics from the external environment: the current time, the
1002 process ID of the running XEmacs process, the name of the user who ran
1003 this XEmacs process, etc. It's not clear why this code is in
1010 These modules implement the basic "interactive" commands, i.e.
1011 user-callable functions. Commands, as opposed to other functions, have
1012 special ways of getting their parameters interactively (by querying the
1013 user), as opposed to having them passed in a normal function
1014 invocation. Many commands are not really meant to be called from other
1015 Lisp functions, because they modify global state in a way that's often
1016 undesired as part of other Lisp functions.
1018 `callint.c' implements the mechanism for querying the user for
1019 parameters and calling interactive commands. The bulk of this module is
1020 code that parses the interactive spec that is supplied with an
1021 interactive command.
1023 `cmds.c' implements the basic, most commonly used editing commands:
1024 commands to move around the current buffer and insert and delete
1025 characters. These commands are implemented using the Lisp primitives
1026 defined in `editfns.c'.
1028 `commands.h' contains associated structure definitions and
1035 `search.c' implements the Lisp primitives for searching for text in
1036 a buffer, and some of the low-level algorithms for doing this. In
1037 particular, the fast fixed-string Boyer-Moore search algorithm is
1038 implemented in `search.c'. The low-level algorithms for doing
1039 regular-expression searching, however, are implemented in `regex.c' and
1040 `regex.h'. These two modules are largely independent of XEmacs, and
1041 are similar to (and based upon) the regular-expression routines used in
1042 `grep' and other GNU utilities.
1046 `doprnt.c' implements formatted-string processing, similar to
1047 `printf()' command in C.
1051 This module implements the undo mechanism for tracking buffer
1052 changes. Most of this could be implemented in Lisp.
1055 File: internals.info, Node: Editor-Level Control Flow Modules, Next: Modules for the Basic Displayable Lisp Objects, Prev: Modules for Standard Editing Operations, Up: A Summary of the Various XEmacs Modules
1057 Editor-Level Control Flow Modules
1058 =================================
1070 These implement the handling of events (user input and other system
1073 `events.c' and `events.h' define the "event" Lisp object type and
1074 primitives for manipulating it.
1076 `event-stream.c' implements the basic functions for working with
1077 event queues, dispatching an event by looking it up in relevant keymaps
1078 and such, and handling timeouts; this includes the primitives
1079 `next-event' and `dispatch-event', as well as related primitives such
1080 as `sit-for', `sleep-for', and `accept-process-output'.
1081 (`event-stream.c' is one of the hairiest and trickiest modules in
1082 XEmacs. Beware! You can easily mess things up here.)
1084 `event-Xt.c' and `event-tty.c' implement the low-level interfaces
1085 onto retrieving events from Xt (the X toolkit) and from TTY's (using
1086 `read()' and `select()'), respectively. The event interface enforces a
1087 clean separation between the specific code for interfacing with the
1088 operating system and the generic code for working with events, by
1089 defining an API of basic, low-level event methods; `event-Xt.c' and
1090 `event-tty.c' are two different implementations of this API. To add
1091 support for a new operating system (e.g. NeXTstep), one merely needs to
1092 provide another implementation of those API functions.
1094 Note that the choice of whether to use `event-Xt.c' or `event-tty.c'
1095 is made at compile time! Or at the very latest, it is made at startup
1096 time. `event-Xt.c' handles events for _both_ X and TTY frames;
1097 `event-tty.c' is only used when X support is not compiled into XEmacs.
1098 The reason for this is that there is only one event loop in XEmacs:
1099 thus, it needs to be able to receive events from all different kinds of
1105 `keymap.c' and `keymap.h' define the "keymap" Lisp object type and
1106 associated methods and primitives. (Remember that keymaps are objects
1107 that associate event descriptions with functions to be called to
1108 "execute" those events; `dispatch-event' looks up events in the
1113 `cmdloop.c' contains functions that implement the actual editor
1114 command loop--i.e. the event loop that cyclically retrieves and
1115 dispatches events. This code is also rather tricky, just like
1121 These two modules contain the basic code for defining keyboard
1122 macros. These functions don't actually do much; most of the code that
1123 handles keyboard macros is mixed in with the event-handling code in
1128 This contains some miscellaneous code related to the minibuffer
1129 (most of the minibuffer code was moved into Lisp by Richard Mlynarik).
1130 This includes the primitives for completion (although filename
1131 completion is in `dired.c'), the lowest-level interface to the
1132 minibuffer (if the command loop were cleaned up, this too could be in
1133 Lisp), and code for dealing with the echo area (this, too, was mostly
1134 moved into Lisp, and the only code remaining is code to call out to
1135 Lisp or provide simple bootstrapping implementations early in temacs,
1136 before the echo-area Lisp code is loaded).
1139 File: internals.info, Node: Modules for the Basic Displayable Lisp Objects, Next: Modules for other Display-Related Lisp Objects, Prev: Editor-Level Control Flow Modules, Up: A Summary of the Various XEmacs Modules
1141 Modules for the Basic Displayable Lisp Objects
1142 ==============================================
1155 These modules implement the "console" Lisp object type. A console
1156 contains multiple display devices, but only one keyboard and mouse.
1157 Most of the time, a console will contain exactly one device.
1159 Consoles are the top of a lisp object inclusion hierarchy. Consoles
1160 contain devices, which contain frames, which contain windows.
1168 These modules implement the "device" Lisp object type. This
1169 abstracts a particular screen or connection on which frames are
1170 displayed. As with Lisp objects, event interfaces, and other
1171 subsystems, the device code is separated into a generic component that
1172 contains a standardized interface (in the form of a set of methods) onto
1173 particular device types.
1175 The device subsystem defines all the methods and provides method
1176 services for not only device operations but also for the frame, window,
1177 menubar, scrollbar, toolbar, and other displayable-object subsystems.
1178 The reason for this is that all of these subsystems have the same
1179 subtypes (X, TTY, NeXTstep, Microsoft Windows, etc.) as devices do.
1187 Each device contains one or more frames in which objects (e.g. text)
1188 are displayed. A frame corresponds to a window in the window system;
1189 usually this is a top-level window but it could potentially be one of a
1190 number of overlapping child windows within a top-level window, using the
1191 MDI (Multiple Document Interface) protocol in Microsoft Windows or a
1194 The `frame-*' files implement the "frame" Lisp object type and
1195 provide the generic and device-type-specific operations on frames (e.g.
1196 raising, lowering, resizing, moving, etc.).
1201 Each frame consists of one or more non-overlapping "windows" (better
1202 known as "panes" in standard window-system terminology) in which a
1203 buffer's text can be displayed. Windows can also have scrollbars
1204 displayed around their edges.
1206 `window.c' and `window.h' implement the "window" Lisp object type
1207 and provide code to manage windows. Since windows have no associated
1208 resources in the window system (the window system knows only about the
1209 frame; no child windows or anything are used for XEmacs windows), there
1210 is no device-type-specific code here; all of that code is part of the
1211 redisplay mechanism or the code for particular object types such as