GNU TeXmacsUser manualReferenceAboutEnglish
Inclusions

1.The two inclusion tags

The kernel knows two inclusion primitives, both with one argument, the name of the included file (

Data/Drd/drd_std.cpp

gives it the type URL):

include (INCLUDE)

Typeset by concater_rep::typeset_include (

Typeset/Concat/concat_macro.cpp

). The typesetter loads the file with load_inclusion, typesets the result with typeset_dynamic, and sets the current file name and the secure flag of the environment to those of the included file during that time.

include* (VAR_INCLUDE)

A rewriting primitive, like extern and with-package: the environment replaces it by the contents of the file (edit_env_rep::rewrite,

Typeset/Env/env_exec.cpp

), and the bridge typesets the rewritten tree (bridge_rewrite_rep::my_typeset,

Typeset/Bridge/bridge_rewrite.cpp

), again with the current file name and the secure flag of the included file. An inclusion which resolves to the base file name (the master, see below) is replaced by the error "invalid self include". Since it is a rewriting, the inclusion is expanded again whenever its bridge is retypeset (the file itself comes from the cache described below).

In practice, documents use include, but the standard style redefines it as a macro (

packages/standard/std-automatic.ts

):

<assign|include|<macro|name|<surround|<part-info|<arg|name>>||<include*|<arg|name>>>>>

so that every inclusion records its starting page and the current counters for projects, and then expands to include*. The primitive include and typeset_include are only used with styles which do not load std-automatic.

Neither form makes the included material part of the edit tree of the master: it is displayed, but the cursor cannot enter it and modifications have to be made in the included file itself, or in a part view.

2.Resolution of file names

The argument is evaluated to a string, interpreted as a Unix style relative or absolute name, and resolved with relative (env->base_file_name, file_name). The base file name is the master of the buffer (edit_typeset_rep::typeset_prepare), that is, its own name for an ordinary buffer.

The base file name is not changed while an included file is typeset; only the current file name is, and the latter is only used for the secure flag. Hence all relative names inside an included file (nested inclusions, images, sounds and videos, which are all resolved against base_file_name) are interpreted relative to the directory of the master, not of the included file. Books whose chapters live in subdirectories must therefore use names relative to the master in the chapters.

3.The inclusion cache

All inclusions go through load_inclusion (url) (

Texmacs/Data/new_buffer.cpp

), which keeps a global table document_inclusions from the file name (as a string) to the included tree:

  1. If the name is in the table, the cached tree is returned.

  2. Otherwise the file is imported with import_tree (name, "generic"), so that any format with a converter can be included, and reduced to a body by extract_document (

    Data/Convert/Texmacs/fromtm.cpp

    ). extract_document wraps the body in a with which sets the initial environment of the included file, except for the page layout variables and, when the included file belongs to a project, its page and sectional counters.

  3. The result is cached unless it is an error.

The cache is shared by all buffers and is never invalidated automatically. It is emptied by reset_inclusions, which is only called by tm_server_rep::inclusions_gc (inclusions-gc, Tools\(\rightarrow\)Update\(\rightarrow\)Inclusions and Document\(\rightarrow\)Update\(\rightarrow\)All); inclusions_gc then retypesets all documents. The routine reset_inclusion (url), which would forget a single file, exists but is not called anywhere. The same cache is used by the Scheme functions tree-load-inclusion and tree-inclusion (two glue names for load_inclusion).

4.Inclusions on the Scheme side

tm-get-includes, buffer-get-includes, buffer-contains-includes?

(

generic/document-part.scm

) List the names of the files included by a document, looking through document and with nodes. The list drives the Document\(\rightarrow\)Part menu of a master (document-master-menu in

part/part-menu.scm

), each entry of which opens the included file as a part view.

include-list, project-file-list

(

generic/document-menu.scm

) The same for the Document\(\rightarrow\)Project menu, but only at the top level of the master and with resolved URLs.

buffer-expand-includes

Tools\(\rightarrow\)Project\(\rightarrow\)Expand inclusions flattens a master into a single document: each include which is a paragraph of a document (at any depth) is replaced by the paragraphs of the included body (taken from the cache).

5.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".