![]() |
Document parts and part views | ![]() |
The word part is used for two different features. In-buffer parts let the user show only some of the principal sections of a long document, or only its preamble. Part views are documents of the TeXmacs file system, tmfs://part/…, which present a file of a multi-file document together with the context of its master, and in which the inclusions are expanded and editable.
In-buffer parts are implemented entirely in Scheme, in
generic/document-part.scm
, as rewritings of the top level document of the buffer. The comment at the head of the file gives the representations of the four part modes:
(document (show-preamble preamble) (ignore body)): only the preamble (the macro definitions of the document) is visible and editable.
(document [(hide-preamble preamble)] parts…), where each part is (show-part id body alt-body) or (hide-part id body alt-body).
(document [(hide-preamble preamble)] body…): the ordinary form.
The macros are defined in
packages/standard/std-fold.ts
. show-part typesets its body inside set-part (which extends the variable current-part and resets auto-nr,
std-automatic.ts
); hide-part does the same inside hidden, choosing the body or the alt-body according to sectional-short-style. Hidden parts are still evaluated, so that counters and labels remain correct, and hide-part is declared with hidden child 1 in its DRD properties. hide-preamble shows nothing but still executes the definitions, through filter-style.
The parts are created by buffer-make-parts, which calls principal-sections-to-document-parts (
text/text-structure.scm
): the paragraphs are split at each principal section (chapters in a book, sections in an article), the pieces are numbered "1", "2", … as identifiers, and the alt-body is the section heading alone. Parts are referred to in the menus by the title of their first section (document-part-name, buffer-parts-list). buffer-flatten-parts undoes the splitting.
The main entry points are buffer-set-part-mode (with a check mark for the current mode), buffer-show-part (mode :one), buffer-toggle-part (mode :several), buffer-go-to-part, toggle-preamble-mode and buffer-make-preamble. They make up the Document\(\rightarrow\)Part menu (document-part-menu). When the cursor ends up inside a hidden part, for instance after a search, cursor-show-hidden (
utils/edit/variants.scm
) calls the tree-show-hidden overload of
document-part.scm
, which shows the part.
A part view is named
tmfs://part/master tmfs://part/master/file |
where master is the URL of the master (as a TeXmacs file system string) and file is either a name relative to the master, prefixed with here/, or an absolute one. part-url, part-master, part-file and part-open-name (
part/part-tmfs.scm
) build and decompose such names; the split is made at the first occurrence of .tm/, so the second form only works for masters with the suffix .tm (and in directories whose names do not contain .tm/). In a master with inclusions, Document\(\rightarrow\)Part lists the included files (document-master-menu,
part/part-menu.scm
), and choosing one opens (part-url master file).
The handlers registered for the part protocol are documented in general in the TeXmacs file system. The master handler returns the real file, so that links and relative names in the view are resolved as in the file; the title is master - file.
The load handler reads the file and the master and builds a document with part-expand:
In the body, every include or include* with a literal file name is replaced by (shared uid name body), where uid is a fresh unique identifier, name the Unix name of the included file and body its body. If the view is not the master itself, the whole body is also wrapped in a shared tag for the file.
The style, references and auxiliary sections are taken from the master, so that the view is typeset with the master's style and resolves references to the whole book.
The initial environment is that of the master, without preamble, mode, page-medium, page-printed and page-first, plus part-flag set to true, the counters recorded by part-info in the master's parts channel, the first page from the label part:file, and finally the file's own initial environment (master-inits). part-flag makes the editor use a copy of its references as global table; see projects.
The save handler performs the inverse transformation (part-compress): it removes the outer shared wrapper, turns each remaining shared back into an include with a name relative to the file, stores only the differences between the edited initial environment and the inherited one, and takes the style, references and auxiliary sections from the file as it is on disk. The result is written to the real file, which is then marked as saved (or modified, if writing failed).
The shared macro (
std-automatic.ts
) wraps its body in a locus with the identifier name and an observer which calls the Scheme function mirror-notify (
part/part-shared.scm
) on each modification. The same machinery serves the mirror tag of
packages/utilities/relate.ts
(live copies of a piece of document, made with make-mirror) and several comment tags. The general link and observer mechanism is described in the link kernel; here is what happens on top of it:
When the body of a shared tag is modified, mirror-notify calls buffer-initialize-shared. If a buffer with the included file's name is open, it attaches a notifier to that buffer (buffer-attach-notifier, see buffers) and marks it for an initial copy.
The modification is queued for that buffer (buffer-notify-shared) and, at the next idle moment, buffer-treat-pending applies the queued modifications to the buffer body with modification-apply!. If one of them does not apply, the buffer body is replaced by a copy of the shared tree (buffer-restore).
Conversely, modifications of the open file buffer reach buffer-notify through its notifier and are queued, with mirror-list-notify, for the other trees carrying the same identifier, that is, for the shared tags in the open part views.
Between copies of a mirror or shared body, the same queue is used (mirror-treat-pending). Copies which fall out of step are put on a black list, separated by giving them new unique identifiers if needed (mirror-separate) and resynchronized by copying (mirror-synchronize). All these changes are made under a dedicated author (mirror-author), with mirror-idle? set to false so that they are not echoed back.
Edits of included material can be lost. Modifications made to an expanded inclusion in a part view are forwarded only to an open buffer of the included file. If that file is not open, buffer-initialize-shared does nothing (the code which would load it in the background is commented out), and saving the view turns the shared tag back into a plain include, so the changes are silently discarded. If the file is open, its buffer has to be saved separately. (Found by reading the code, not tested.)
Initial synchronization of shared never runs. The shared macro passes <quote-arg|xbody> to mirror-initialize (
std-automatic.ts:329
), but the macro has no argument xbody; the mirror macro passes <quote-arg|body> (
relate.ts:31
). mirror-initialize therefore never recognizes the body of a shared tag, and copies which differ when a view is opened are not brought in sync until they are modified.
Style and references of a part view are not saved. The save handler takes these sections from the file on disk, so changing the style in a part view has no effect on the file.
The part mode is global. part-mode is a single variable of
document-part.scm
, shared by all buffers: choosing :several in one document changes the meaning of the part commands in all others.
Two naming schemes for in-buffer parts. Parts are identified by their number in the show-part and hide-part tags, but by their title in the menus and in buffer-show-part and buffer-toggle-part; show-hidden-part, in contrast, expects the number. Two parts with the same title cannot be selected separately from the menu.