# Chapter 10: Theory of Operation

This chapter discusses how Leo's code works, paying particular attention to topics that have caused difficulties in design or implementation. This chapter will be of use primarily to those wanting to change Leo's code.

Contents

- Chapter 10: Theory of Operation
  - Overview
  - Nodes
  - Drawing and events
  - Clones
  - Find and change commands
  - Tangle and untangle commands
  - Unicode
  - Unlimited undo
  - Key bindings

## Overview

All versions of Leo are organized as a collection of classes. The general organization of Leo has remained remarkably stable throughout all versions of Leo, although the names of classes are different in different versions. Smalltalk's Model/View/Controller terminology is a good way think about Leo's classes. Model classes represent the fundamental data. The vnode and tnode classes are Leo's primary model classes.

View classes draw the screen. The main view classes are leoFrame.py and leoTree.py. The colorizer class in leoColor.py handles syntax coloring in the body pane. Leo's view classes know about data stored in the vnode class. Most events (keystrokes and mouse actions) in the outline and body pane are handled in the leoTree class. The leoFrame class also creates the Leo window, including menus, and dispatches the appropriate members of the controller classes in response to menu commands.

Controller classes (aka commanders) control the application. In Leo, controllers mostly handle menu commands. Commanders create subcommanders to handle complex commands. The atFile class reads and writes files derived from `@file` trees. The LeoFind class handles the Find and Change commands. The leoImportCommands class handles the Import and Export commands, the tangleCommands class handles the Tangle and Untangle commands and the undoer class handles the Undo command. Other classes could be considered controller classes.

Each Leo window has its own commander and subcommanders. Subcommanders are not subclasses of their commander. Instead, subcommanders know the commander that created them, and call that commander as needed. Commanders and subcommanders call the model and view classes as needed. For example, the Commands class handles outline commands. To move a headline, the commander for the window calls a vnode move routine to alter the data, then calls the view class to redraw the screen based on the new data.

A singleton instance of the LeoApp class represents the application itself. All code uses the app() global function to gain access to this singleton member. The ivars of the LeoApp object are the equivalent of Leo's global variables. leo.py uses no global Python variables, except the gApp variable returned by app(). leoGlobals.py defines all application constants. Naturally, most constants are local to the class that uses them.

Several classes combine aspects of model, view and controller. For example, the LeoPrefs class represents user preferences (model), the Preference Panel (view) and the Preferences menu command (controller). Similarly, the LeoFind class represents find settings, the Find/Change dialog, and the Find/Change commands.

We use the following convention throughout this documentation. Any variable named `c` is a commander, i.e., an instance of the Commands class in `leoCommands.py`. Variables named `v` and `t` are vnodes and tnodes respectively. These classes are defined in `leoNodes.py`.

## Nodes

The vnode and tnode classes represent most of the data contained in the outline. These classes are Leo's fundamental Model classes. A vnode (visual node) represents a headline at a particular location on the screen. When a headline is cloned, vnodes must be copied. vnodes persist even if they are not drawn on the screen. Commanders call vnode routines to insert, delete and move headlines.

The vnode contains data associated with a headline, except the body text data which is contained in tnodes. A vnode contains headline text, a link to its tnode and other information. Vnodes contain structure links: parent, firstChild, next and back ivars. To insert, delete, move or clone a vnode the vnode class just alters those links. The Commands class calls the leoTree class to redraw the outline pane whenever it changes. The leoTree class knows about these structure links; in effect, the leoTree and vnode classes work together.

A tnode, (text node) represents body text. All vnodes that are clones of each other share the same tnode. In other words, tnodes are the unit of sharing of body text. The tnode class is more private than the vnode class. Most commanders deal only with vnodes, though there are exceptions.

Because Leo has unlimited Undo commands, Leo deletes vnodes and tnodes only when a window closes. Leo deletes nodes indirectly using `destroy` methods. Several classes, including the vnode, tnode, leoFrame and leoTree classes, have `destroy` methods. `destroy` methods merely clear links so that Python's and Tkinter's reference counting mechanisms will eventually delete vnodes, tnodes and other data when a window closes.

Leo's XML file format uses tnode indices to indicate which tnodes (t elements) belong to which vnodes (v elements). Such indices are required. Even if we duplicated the body text of shared tnodes within the file, the file format would still need an unambiguous way to denote that tnodes are shared. Present versions of Leo use gnx's (global node indices) as node indices. These indices do not change once a node has created. This reduces cvs conflicts.

## Drawing and events

Leo must redraw the outline pane when commands are executed and as the result of mouse and keyboard events. The main challenges are eliminating flicker and handling events properly. These topics are interrelated.

Eliminating flicker. Leo must update the outline pane with minimum flicker. Various versions of Leo have approached this problem in different ways. The drawing code in leo.py is robust, flexible, relatively simple and should work in almost any conceivable environment. Leo assumes that all code that changes the outline pane will be enclosed in matching calls to the c.beginUpdate and c.endUpdate methods of the Commands class. c.beginUpdate() inhibits drawing until the matching c.endUpdate(). These calls may be nested; only the outermost call to c.endUpdate() calls c.redraw() to force a redraw of the outline pane.

Code may call `c.endUpdate(flag)` instead of `c.endUpdate()`. Leo redraws the screen only if flag is true. This allows code to suppress redrawing entirely when needed. For example, here is how the idle_body_key event handler in leoTree.py conditionally redraws the outline pane:

    redraw_flag = false
    c.beginUpdate()
    val = v.computeIcon()
    if val != v.iconVal:
            v.iconVal = val
            redraw_flag = true
    c.endUpdate(redraw_flag) # redraw only if necessary

The leoTree class redraws all icons automatically when `c.redraw()` is called. This is a major simplification compared to previous versions of Leo. The entire machinery of drawing icons in the vnode class has been eliminated. The `v.computeIcon` method tells what the icon should be. The `v.iconVal` ivar tells what the present icon is. The event handler simply compares these two values and sets `redraw_flag` if they don't match.

Handling events. Besides redrawing the screen, Leo must handle events or commands that change the text in the outline or body panes. It is surprisingly difficult to ensure that headline and body text corresponds to the vnode and tnode corresponding to presently selected outline, and vice versa. For example, when the user selects a new headline in the outline pane, we must ensure that 1) the vnode and tnode of the previously selected node have up-to-date information and 2) the body pane is loaded from the correct data in the corresponding tnode. Early versions of Leo attempted to satisfy these conditions when the user switched outline nodes. Such attempts never worked well; there were too many special cases. Later versions of Leo, including leo.py, use a much more direct approach. The event handlers make sure that the vnode and tnode corresponding to the presently selected node are always kept up-to-date. In particular, every keystroke in the body pane causes the presently selected tnode to be updated immediately. There is no longer any need for the `c.synchVnode` method, though that method still exists for compatibility with old scripts.

The leoTree class contains all the event handlers for the body and outline panes. The actual work is done in the `idle_head_key` and `idle_body_key` methods. These routines are surprisingly complex; they must handle all the tasks mentioned above, as well as others. The `idle_head_key` and `idle_body_key` methods should not be called outside the leoTree class. However, it often happens that code that handles user commands must simulate an event. That is, the code needs to indicate that headline or body text has changed so that the screen may be redrawn properly. The leoTree class defines the following simplified event handlers: `onBodyChanged`, `onBodyWillChange`, `onBodyKey`, `onHeadChanged` and `onHeadlineKey`. Commanders and subcommanders call these event handlers to indicate that a command has changed, or will change, the headline or body text. Calling event handlers rather than `c.beginUpdate` and `c.endUpdate` ensures that the outline pane is redrawn only when needed.

## Clones

Since version 4.2, Leo represents clones by sharing tnodes. Cloned vnodes share the same tnode. This shared tnode represents the entire shared subtree of both clones. Thus, the `_firstChild` link must reside in tnodes, not vnodes.

Because vnodes may be visited many times during a complete traversal of a tree, a vnode does not represent unique location on the screen. As a result, the position class had to be invented. `c.rootVnode` and `c.currentVnode` now return positions instead of vnodes. The position class was designed to allow compatibility with old scripts. Old code can use positions just like they used to use vnodes.

Earlier versions of Leo duplicated all the descendants of vnode v when cloning v. This created many complications that no longer exist. In particular, in the new scheme a vnode v is cloned if and only if `len(v.t.vnodeList) > 1`.

## Find and change commands

The find and change commands are tricky; there are many details that must be handled properly. The following principles govern the LeoFind class:

1. Find and Change commands initialize themselves using only the state of the present Leo window. In particular, the Find class must not save internal state information from one invocation to the next. This means that when the user changes the nodes, or selects new text in headline or body text, those changes will affect the next invocation of any Find or Change command. Failure to follow this principle caused all kinds of problems in the Borland and Macintosh codes. There is one exception to this rule: we must remember where interactive wrapped searches start. This principle simplifies the code because most ivars do not persist. However, each command must ensure that the Leo window is left in a state suitable for restarting the incremental (interactive) Find and Change commands. Details of initialization are discussed below.
2. The Find and Change commands must not change the state of the outline or body pane during execution. That would cause severe flashing and slow down the commands a great deal. In particular, the `c.selectPosition` and `c.editPosition` methods must not be called while looking for matches.
3. When incremental Find or Change commands succeed they must leave the Leo window in the proper state to execute another incremental command. We restore the Leo window as it was on entry whenever an incremental search fails and after any Find All and Change All command. Initialization involves setting the `self.c`, `self.v`, `self.in_headline`, `self.wrapping` and `self.s_text` ivars.

Setting `self.in_headline` is tricky; we must be sure to retain the state of the outline pane until initialization is complete. Initializing the Find All and Change All commands is much easier because such initialization does not depend on the state of the Leo window. Using Tk.Text widgets for both headlines and body text results in a huge simplification of the code.

Indeed, the searching code does not know whether it is searching headline or body text. The search code knows only that `self.s_text` is a Tk.Text widget that contains the text to be searched or changed and the insert and sel Tk attributes of self.search_text indicate the range of text to be searched. Searching headline and body text simultaneously is complicated. The selectNextVnode() method handles the many details involved by setting `self.s_text` and its insert and sel attributes.

## Tangle and untangle commands

This section describes Leo's explicit `Tangle` and `Untangle` commands. Such commands operate only on `@root` and `@unit` trees. The previous chapter discusses the implicit Tangle on Write/Untangle on Read processes used to read and write `@file` trees.

The `Tangle` command translates the selected `@root` tree into one or more well-formatted C source files. The outline should contain directives, sections references and section definitions, as described in Chapter 4. The Untangle command is essentially the reverse of the `Tangle` command. The Tangle command creates a derived file from an `@root` tree; the Untangle command incorporates changes made to derived files back into the `@root` tree.

The `Tangle` command operates in two passes. The first pass discovers the complete definitions of all sections and places these definitions in a symbol table. The first pass also makes a list of root sections. Definitions can appear in any order, so we must scan the entire input file to know whether any particular definition has been completed.

This second pass creates one file for each `@root` node. `Tangle` rescans each section in the list of roots, copying the root text to the output and replacing each section reference by the section's definition. This is a recursive process because any definition may contain other references. We can not allow a section to be defined in terms of itself, either directly or indirectly. We check for such illegally recursive definitions in pass 2 using the section stack class. `Tangle` indicates where sections begin and end using comment lines called sentinel lines. The this part of the appendix discusses the format of the sentinels output by the `Tangle` command.

The key design principle of the `Tangle` command is this:

    Tangle must output newlines in a context-free manner.

That is, `Tangle` must never output conditional newlines, either directly or indirectly. Without this rule `Untangle` could not determine whether to skip or copy newlines.

The `Tangle` command increases the indentation level of a section expansion the minimum necessary to align the section expansion with the surrounding code. In essence, this scheme aligns all section expansions with the line of code in which the reference to the section occurs. In some cases, several nested sections expansions will have the same indentation level. This can occur, for example, when a section reference in an outline occurs at the left margin of the outline.

This scheme is probably better than more obvious schemes that indent more "consistently." Such schemes would produce too much indentation for deeply nested outlines. The present scheme is clear enough and avoids indentation wherever possible, yet indents sections adequately. End sentinel lines make this scheme work by making clear where the expansion of one section ends and the expansion of a containing section resumes.

`Tangle` increases indentation if the section reference does not start a line. Untangle is aware of this hack and adjusts accordingly. This extra indentation handles several common code idioms, which otherwise would create under-indented code. In short, `Tangle` produces highly readable output, given the necessity of preserving newlines for `Untangle`.

`Untangle` is inherently complex. It must do a perfect job of updating the outline, especially whitespace, from expansions of section definitions created by the Tangle command. Such expansions need not be identical because they may have been generated at different levels of indentation. The `Untangle` command can not assume that all expansions of a section will be identical in the derived file; within the derived file, the programmer may have made incompatible changes to two different expansions of the same section. `Untangle` must check to see that all expansions of a section are "equivalent". As an added complication, derived files do not contain all the information found in `@root` trees. `@root` trees may contain headlines that generate no code at all. Also, an outline may define a section in several ways: with an `@c` or `@code` directive or with a section definition line. To be useful, Untangle must handle all these complications flawlessly. The this part of the appendix discusses the various conventions used in the sentinels output by the `Tangle` command. These conventions allow the `Untangle` command to recreate whitespace correctly.

`Untangle` operates in two passes. The first pass finds definitions in the derived file and enters them into the Untangle Symbol Table, or UST. Definitions often include references to other sections, so definitions often include nested definitions of referenced sections. The first pass uses a definition stack to keep track of nested definitions. The top of the stack represents the definition following the latest reference, except for the very first entry pushed on the stack, which represents the code in the outline that contains the `@root` directive. The stack never becomes empty because of the entry for the `@root` section. All definitions of a section should match--otherwise there is an inconsistent definition. This pass uses a forgiving compare routine that ignores differences that do not affect the meaning of a program.

The second pass of `Untangle` enters definitions from the outline into the Tangle Symbol Table, or TST. The second pass simultaneously updates all sections in the outline whose definition in the TST does not match the definition in the UST. The central coding insight of the Untangle command is this: the second pass of Untangle is almost identical to the first pass of Tangle. That is, `Tangle` and `Untangle` share key parts of code, namely the `skip_body` method and its allies. Just when `skip_body` enters a definition into the symbol table, all the information is present that `Untangle` needs to update that definition.

## Unicode

Leo uses unicode objects in vnodes and tnodes to denote headline and body text. Note that unicode strings have no encoding; only plain strings have encodings. This means that once an (encoded) plain string has been converted to a unicode string it doesn't matter how the unicode string was created. This is the key that makes Leo's new code robust: internally Leo never has to worry about encodings. Encoding matter only when encoded strings are converted to and from Unicode. This happens when Leo reads or writes files.

Python expressions that mix unicode strings u and plain strings s, like one of these:

    u + s
    u == s
    u[5] == s[2:]

are promoted to unicode objects using the "system encoding". This encoding should never be changed, but we can't assume that we know what it is, so for safety we should assume the most restrictive encoding, namely "ascii". With this assumption, Leo's code can't throw an exception during these promotions provided that:

- All strings are converted to unicode when Leo reads files or gets text from Tk.Text widgets.
- All string literals in Leo's code have only ascii characters.

## Unlimited undo

Unlimited undo is straightforward; it merely requires that all commands that affect the outline or body text must be undoable. In other words, everything that affects the outline or body text must be remembered. We may think of all the actions that may be Undone or Redone as a string of beads (undo nodes).

Undoing an operation moves backwards to the next bead; redoing an operation moves forwards to the next bead. A bead pointer points to the present bead. The bead pointer points in front of the first bead when Undo is disabled. The bead pointer points at the last bead when Redo is disabled. An undo node is a Python dictionary containing all information needed to undo or redo the operation. The Undo command uses the present bead to undo the action, then moves the bead pointer backwards.

The Redo command uses the bead after the present bead to redo the action, then moves the bead pointer forwards. All undoable operations call setUndoParams() to create a new bead. The list of beads does not branch; all undoable operations (except the Undo and Redo commands themselves) delete any beads following the newly created bead. I did not invent this model of unlimited undo. I first came across it in the documentation for Apple's Yellow Box classes.

## Key bindings

leoKeys.py handles key bindings. There are two kinds of bindings, gui bindings and pane bindings.

Gui bindings are the actual binding as seen by Tkinter (or whatever gui is in effect). Leo binds every key that has a binding to k.masterKeyHander. For Tkinter, a separate binding must be made, rather than a single <Key> binding, because, alas, Tkinter key events provide insufficient enough information to tell what key actually caused the key event(!) This is a significant hole in Tkinter's event mechanism.

At present Leo makes gui bindings in several places, all equivalent. Bindings are made to callbacks, all of which have this form:

    def callback(event=None,k=k,stroke=stroke):
       return k.masterKeyHandler(event,stroke)

As a result, changing gui bindings actually has no effect whatever. It would be clearer to have a single place to make these bindings...

In any case, the purpose of these callbacks is to capture the value of 'stroke' so that it can be passed to k.masterKeyHandler. This relieves k.masterKeyHandler of the impossible task of computing the stroke from the event. Important: No function argument is ever passed to k.masterKeyHandler from these callbacks, because k.masterKeyHandler binds keys to command handlers as described next.

Pane bindings are bindings represented by various Python dictionaries in the keyHandlerClass (see below). k.masterKeyHandler and its helpers use these dictionaries to call the proper command or mode handler. This logic is hairy, but it is completely separate from the gui binding logic.

Here are the dictionaries that k.masterKeyHandler uses:

- c.commandsDict: Keys are minibuffer command names; values are functions f.
- k.inverseCommandsDict: Keys are f.__name__l values are emacs command names.
- k.bindingsDict: Keys are shortcuts; values are lists of g.bunch(func,name,warningGiven).
- k.masterBindingsDict: Keys are pane names: 'all','text',etc. or mode names. Values are dicts: keys are strokes; values are g.Bunch(commandName,func,pane,stroke).
- k.modeWidgetsDict: Keys are mode names; values are lists of widgets to which bindings have been made.
- k.settingsNameDict: Keys are lowercase settings; values are 'real' Tk key specifiers. Important: this table has no inverse.
- inverseBindingDict: This is not an ivar; it is computed by k.computeInverseBindingDict(). Keys are emacs command names; values are lists of shortcuts.