Leo + Python: the ultimate scripting tool #6

Edward K. Ream edreamleo at charter.net
Sat Nov 8 11:22:26 EST 2003


Reasons why Leo and Python work so well together:

6. Leo is the ultimate platform for finding and changing text.

A few days ago I have added a script-based option to Leo's Find/Change
panel.  For more than 30 years I have been disgusted with tools like regular
expressions.  Calling re powerful is an utter joke; it's way too complex and
way too wimpy.  Script-based find-change is infinitely more powerful.
Instead of containing find text or change _text_, Leo's Find/Change panel
may contains find or change _scripts.  This becomes possible because scripts
have complete access to both the structure of the outline and all of Leo's
code.

It all works so smoothly:   The find script is responsible for traversing
the tree and for highlighting the found text or otherwise indicating to the
change script what the found text was.  Leo dedicates a Python dict called
app.searchDict for communication between the search script and the change
script.  The change script is responsible for:

-  changing the text, typically by using v.setBodyStringOrPane(newText), and

-  implementing undo, typically by calling
c.frame.onBodyChanged(v,"Change",oldText=oldText).

Some details:

- When executing the search and change scripts when the user selects the
Find All or Change All commands, Leo executes the find or change scripts
repeatedly only if app.searchDict.get("continue") evaluates to True.
Otherwise these scripts are only executed once.  Otherwise, scripts can use
app.searchDict as they please.

- For simplicity, most find and change scripts will ignore settings in the
Find Panel like "whole word", "pattern match", and "reverse": the scripts
know what to do!  However, these settings are available to the scripts via
ivars such as c.whole_word_flag, etc. if desired.

- Either the find and change scripts may use Python's re module.  For
example, the find script could set app.searchDict["m"] to the match object
returned by re's match method.  The change script would then compute the
result, change the text and set the undo info as usual.

In effect, Leo's Find/Change panel becomes a new platform for running
scripts interactively.  Leo now has all the find/change capability of
pattern matching languages like Snobol and Icon, using the capabilities of
the plain Python language.  Moreover, Leo can deliver these capabilities
interactively or not depending which buttons you push in Leo's Find/Change
panel.

One last point.  As I mentioned much earlier, there is no reason to confine
the find and change scripts to operate on data only within a Leo outline.
These scripts could as easily traverse your file system as the Leo outline.
But notice: scripts could pull data from the file system into the outline so
that you can see the effects of changes as the scripts operate.  Again, this
can all happen interactively if you like.

No, one more last point.  It's convenient to defined an initScriptFind
utility very similar to the makeColorSuite utility, used like this:

# Initialize Leo's find panel using the named children of this node.
from leoGlobals import *
initScriptFind("Find script","Change script")
# Start searching at the top.
top().selectVnode(c.rootVnode())

Put the search script in a child node called "Find script" (no quotes), put
the change script in a child node called "Change script", execute this
script and the following happens:

- The body text of "Find script" node gets put in the find text of Leo's
Find/Change dialog.

- The body text of "Change script" node gets put in the find text of Leo's
Find/Change dialog.

- The Script Find radio buttons gets selected, as does the Script Change
checkbox.

- The root of the outline becomes the selected node.

Presto!  Leo is ready for a script search.

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list