This Engineering notebook post discusses how to support code/text snippets in Leo.
The Aha: gnx's are an underused resource!
I have been thinking about literate programming (LP), documentation, and related Leo issues. Some links:
-
#189: Better Support for UNL Navigation.
-
#946: Create a way to isolate documentation in a subnode.
-
#1035: References to nodes via gnx's or tags.
These topics are all related to the wish to make Leo a better mimic of the original goals of LP, which morphs into the desire to allow references to snippets of code, especially in Leo's rst3 command.
Yesterday I realized that gnx's, the unique, unchanging part of each node, provides a way to identify snippets, much like org mode's code block:
@snippet <description>
<Lines of text or code>
@end-snippet
The snippet would be like a section definition. A snippet reference would be a text-based way of referring to a particular snippet.
Snippet references will contain the gnx of the node containing the snippet and the snippet description. Snippet descriptions need to be unique only within each Leo node.
Leo will search all open outlines for snippets, much like Leo searches for @button nodes when we right-click the @button node and choose "Find Script."
Using snippets in @rst trees
Inside @rst trees, Leo's rst3 command could support a new kind of @rst node:
@rst-insert-snippet (description)
These nodes would insert the snippet into the rST output. The body text of @insert-snippet nodes would contain the gnx.
Using snippets in @file trees
Similarly, Leo might be able to incorporate snippets into external files using a new Leo directive:
@insert-snippet (description)
Outlines must define all snippets referenced in @file trees. This constraint will likely ensure that the @file read logic will never fail to find snippets.
Summary
Using snippets, users, documents, and scripts can safely use text from all open outlines. Snippets are, in effect, permalinks, uniquely defined by the combination of gnx and description.
Snippets can become invalid only if the user permanently deletes the node containing the snippets. Leo will provide tools for finding snippets, just as Leo can find the script corresponding to @button nodes.
Within @rst trees, snippets will provide all LP's features in a more Leonine way.
Snippets might also work in @file trees. If so, snippets would provide most (all?) of the benefits of cross-file clones.
Edward