On 18/02/2012, at 02.42, Megan Nix Nissen wrote:
Is there a way to implement folding in plain text files in TM2?
Yes, add folding patterns to the text.plain scope.
In the bundle editor use ⌘N to create a new Settings item, set scope selector to text.plain and the content should set foldingStart/StopMarker. You can look e.g. in C → Settings → Foldings for how the C sets this up, although you can simplify these patterns if you only want to fold on braces.
Also Markdown bundle sets foldings for headings, which is using the new indent-based folding patterns explained in the release notes (the start/stop marker is explained in the manual, but has been deprecated to have in grammars, and should instead be done as scoped settings).
On Thu, Feb 23, 2012 at 00:12, Megan Nix Nissen megan@techcycled.com wrote:
In any case, it seems that the best way to accomplish my goal is to convert all of my text files to markdown and my brace-delimited blocks into indented blocks. The only method I can think of is to write a regexp and tediously pick through my files searching and replacing, then saving each one as markdown. Unless anyone else has a better suggestion..?
You could start with something like this awk script to do the indentation:
#!/usr/bin/awk -F $0 ~ "^}" { indent="" } { print indent, $0 } $0 ~ "{$" { indent=" " }
It indents by four spaces, leaves the braces in the text and can't handle nested blocks, but it's a start :-)
Cheers, Martin
On 23/02/2012, at 06.12, Megan Nix Nissen wrote:
Ah, I was going to try the first method, but I could not find a button or menu option anywhere to create a new item, and did not know that ⌘N would do it. Am I missing the GUI element for this, or does it not exist?
Does not exist — bundle editor is provisional at best ;)
Indent-based folding would be ideal, and headers even better. I read the patch notes multiple times and did a great deal of searching, but never found anything that explained it in enough detail for me to do anything with it. Knowing where to look (Markdown -> Settings -> Folding) definitely helps, and hopefully more comprehensive, up to date TM2 & bundle documentation is coming so that I can RTFM rather than bugging the mailing list. :)
Here is the missing documentation (from the pre-public alpha release notes):
The folding system has been extended with two new patterns to allow indent-based foldings.
For C++ I have set these to:
{ foldingIndentedBlockStart = '^\s*(public|protected|private):\s*(//.*)?$|^\s*/[*](?!.*[*]/)'; foldingIndentedBlockIgnore = '^\s*#'; }
And for Markdown I use:
{ foldingIndentedBlockStart = '^#+\s+'; foldingIndentedBlockIgnore = '^(?!#+\s+)'; }
So the first pattern decides what starts a foldable block. Incase of C++ it is either a visibility keyword (`public`/`private`) or `/*`.
The latter is to make a block like the following foldable:
/* * t_foldings.cc * Avian * * Created by Allan Odgaard on 2011-09-30. * Copyright 2011 MacroMates. All rights reserved. */
The second pattern are lines to ignore. For C++ this would be preprocessor directives, e.g.:
class foldings_t { public: foldings_t (arguments); #if 0 virtual ~foldings_t (); #endif
private: /* data */ };
Here we can still fold from `public` and down to `private` despite the non-indented preprocessor directives.
The Markdown patterns are a bit more sneaky, we let a heading be a start marker and then we effectively ignore everything which is not a heading, causing those lines to be included in the foldable block, despite not being indented.
In any case, it seems that the best way to accomplish my goal is to convert all of my text files to markdown and my brace-delimited blocks into indented blocks […]
The default patterns for Markdown only fold headings, that is, lines starting with one or more pound signs (#) and the folding is down to the next section.
If this works for you, then converting to Markdown is probably a good idea as it’s becoming a de facto standard and you can benefit from many Markdown workflow tools.
But if you already have established your own convention for foldable blocks and use in many text files, I’d adjust the indent patterns to match these.