Scite Text Folding

lua-users home
wiki

Outline Mode for Text Documents

This is a simple extension which allows structured text documents to be viewed in outline, by making folding available. It is an example of a basic line-driven lexical styler. It currently requires SciteExtMan; to install put Files:wiki_insecure/editors/SciTE/fold.lua in your scite_lua directory.

Text documents of course come in many forms; this extension picks up on some common patterns for fold information. The default is to use the Wiki convention, where a line begining with a number of '=' characters means a heading:

  =Main Heading
  ==Subheading 1
  ==Subheading 2
  ===Sub 2 1
  ===Sub 2 2
You can of course change the character used. For Emacs-style outline files, put this in your properties file:
 text.outline.char=*
You can specify which files are to be treated as text, and what font to use with the following properties. For example, I like editing text in the fixed system font (years of using Notepad do that to a person!):
text.ext=*.txt;*.doc
text.font.name=Fixedsys
Another common style is to use numbered sections, like 1.2.1. (A bureaucratic curse, perhaps, but some people like it). Here is how to set the folder to recognize this style:
text.outline.number=1

Showing outlines

The standard View|Toggle All Folds command will now work on your document, and you can of course manually fold and unfold in the usual way. It's common for Wiki-style documents to start at '==', and not have a top-level caption. In that case, you can specify what the starting level is:
 text.outline.start=1
This will allow you to view the outline for the markup of this page properly.

Tools|Show Outline shows all fold lines.

Limitations, Implementation and Further Possibilities

Line-based lexers don't always work properly, since they're expecting the return key to be pressed. I've provided a Tools|Rescan command to sort the document out if it gets confused; note that this happens automatically on a buffer switch.

These functions allow you to set the folding level of the Scintilla control, and then to extract it. The basic level must start at SC_FOLDLEVELBASE, and any fold lines must in addition have SC_FOLDLEVELHEADERFLAG. Please note that the fold information generated by real lexers is more complex, and one has to fake bit operations.

local function set_level(i,lev,fold)
  local foldlevel = lev + SC_FOLDLEVELBASE
  if fold then 
     foldlevel = foldlevel + SC_FOLDLEVELHEADERFLAG
     editor.FoldExpanded[i] = true
  end
  editor.FoldLevel[i] = foldlevel
end

local function get_level(i)
  local fold = false
  local level_flags = editor.FoldLevel[i] - SC_FOLDLEVELBASE
  if level_flags - SC_FOLDLEVELHEADERFLAG >= 0 then
    fold = true
    level_flags = level_flags - SC_FOLDLEVELHEADERFLAG 
  end
  return level_flags, fold  
end
I've made this a SciteExtMan script, to handle the tedious stuff of setting up command handlers and (most importantly) the OnEditorLine event. That can get confused with the OnOutputLine event, so if you're using the Lua console extension, expect to get the occaisional odd error message in the output pane.

I've toyed with the idea of doing automatic level number generation, but it's not trivial to do properly; one has to renumber items when new items are inserted, etc. (MS Word gets confused about it). This is a start at defining a 'major' mode for text documents (to use Emacs terminology), and obviously there would be 'minor' modes as well, like Wiki markup. It would be cool, for instance, if it automatically detected expressions like editor.FoldLevel[i] and put it in monospaced font markup.

SteveDonovan


RecentChanges · preferences
edit · history
Last edited August 31, 2006 7:48 pm GMT (diff)