The selection algorithm

Next we must consider the selection algorithm. When we click with the mouse, how do we recover the variable name? And given a variable name, how do we traverse the source tree and memorize all occurrences in a selection? The selection algorithm will apply equally to the source and environment views.

First, let's suppose we have a variable name. To calculate occurrences of this name in an Exp tree, we traverse the Exp tree via the function
{tree}:select-all-true described in the path manual:


(de :calculate-variable-path (tree varname)
    (when varname
       ({tree}:select-all-true 
          tree
          (lambda (subtree)
              (when (eq 'variable ({tree}:operator-name subtree))
                    (eqstring varname ({tree}:atom_value subtree))
                    )))))

Every time the lambda function returns true, the function adds a subtree to the selection path. We simply test whether the subtree's associated operator is a variable. When the variable name matches the argument variable name, the function returns true.

The result of this function is a path. The path must be stored in the highlight selection. When you change the value of a selection, you must explicitly announce the change by calling {variable}:selection-modified. One effect of this function is to redisplay the select in every ctedit that displays the selection. Thus, let us encapsulate the previous function so that we have the Centaur variable structure available:


(de :select-variables (var varname)
    (let ((root ({variable}:root var)))
      ({selection}:change 
        ({variable}:find-selection var '{current}:highlight-variable)
        (:calculate-variable-path root varname))
      ({variable}:selection-modified var '{current}:highlight-variable)
      ))


Tutorial