Sublime Forum

Fold Away Lines Not Matching A Pattern

#11

Forgive me as I am not a coder, but how can you adapt the code so that it folds away lines that DO match the pattern? And/or to allow regex search?

Also, sylvanaarā€™s suggestion is a good one (as otherwise you have to do something like press ctrl-a, then ctrl-shift-] to unfold), but two problems:

  1. After running the command on a particular file, when command rerun it folds for the same text as before and the selection box no longer appears.
  2. I bound a ā€œall_toggleā€ command but it does nothing.
0 Likes

#12

Iā€™ve tinkered with this to improve its behavior (for what Iā€™m using it for). Iā€™m not a programmer, so itā€™s rather rickety.

You can find my version here:
raw.github.com/alehandrof/MDSH/ ā€¦ %20Fold.py

Bind a key as follows

{ "keys": "ctrl+'"], "command": "all_toggle", "args": { "foldstr": ""  } },

This command will toggle between:

  • unfold all lines, and
  • fold all lines EXCEPT those matching ā€œfoldstrā€

ā€œFoldstrā€ can be passed as an argument, by selected text, or input.

What I use this for is to parse a todo list based on #tags and @contexts:
find a place in the text, hit Ctrl+D to select the word, and Ctrl+ā€™ to fold all lines except those matching the word.

Hope this makes sense / is useful.

Alex

0 Likes

#13

Cheers Alex, I did manage to figure out that commenting out:

# if self.view.settings().has("all"):
#   self.done(self.view.settings().get("all"))
#   return

stopped the repeating of the filter. And having a single command to switch between folding state seems a sensible behaviour. I had a similar usage scenario to yours, though I also wanted to allow filtering out of lines (e.g. those with a @done tag).

0 Likes

#14

The argument is also quite handy, because it means you can set up commands in the palette to fold for standard tags like @home, @calls, etc. (I also list agenda items by @person, as in @bob.)

Iā€™ve been thinking of adding an exclusion filter for the same reason (the files Iā€™m using are loosely based on todo.txt so I would be looking to exclude a regex like ^x \d{4}-\d{2}-\d{2}). It shouldnā€™t be too tricky, but I donā€™t entirely understand the code of the all function. Iā€™ll take a stab at it when I can. Let me know if you come up with anything.

Alex

0 Likes

#15

I did find this:
superuser.com/questions/452189/h ā€¦ ime-text-2
And tried to adapt it, but my understanding of the coding is limited. But having a separate regular text search and regex search would be good.

This regex filter in the link would be useful for destructive removal of items (e.g. @done lines)

Use of regex with negative look arounds can also be used to filter out instances, so this:
^((?!DONE).)*$
Will match any string, or line without a line break, not containing the (sub) string ā€˜DONEā€™. You could then combine that with regex term to exclude dones and include something else, which means the regex would start getting complicated, but running from the palette would help.

0 Likes

#16

Thatā€™s a nice find. Although I suspect the folding, rather than the regex, is the tricky bit.

Actually, I donā€™t really like the folding on this plugin, I would prefer the fold market at the end. You mention SmartMarkdown elsewhere, which has a similar problem: the folding marker is underneath the # heading, which makes it more likely I will leave something behind when moving chunks around. I would prefer if it folded like so:

# heading ...]
## other heading ...]
&c.

Incidentally, did you notice that all your posts are in threads I had started or posted in? ā€“ with this exception:

(Iā€™m surprised that didnā€™t take off, @facelessuser and @castles_made_of_sand can usually pull amazing things seemingly out of a hat.)

Anyway, if you tell me what youā€™re trying to cook up, I can tell you whether I have failed to find it/make it :smiley:

Alex

0 Likes

#17

I guess, without really being aware of its existence, that I have inadvertently being trying to emulate foldingtext which I recently discovered, which is a freeform outliner/task manager from the maker of taskpaper (of which plaintasks is a copy of).

If you watch the folding text video they use folds next to the item rather than below, as you were suggesting.
foldingtext.com/about/

To avoid overly complicated and lengthy nested structures, markdown headings (with folding) allow structure, but still provide nesting if you want with tabbed bullet points below them (which can also be folded with native sublime), and it all looks nice in markdown. One minor issue with markdone syntax is that the state of item (e.g. + for completed) isnā€™t shown in markdown view, which is why appending @done to items is useful. There is the itodo plugin which works well with markdone to make converting todo items ( starting with -) to done items, e.g.:

  • done task @done (2012-11-16 16:59)

One thing that I havenā€™t seen in sublime is a ā€œzoomā€ mode, which is what workflowy does well, and is found in foldingtext and writemonkey - so you press a hotkey and you only see the current scope (e.g. the level of the heading within markdown), and ā€œzoom outā€ to see the whole document.

Anyway, putting all these things together makes for something pretty similar to foldingtext and potentially quite a nice and powerful tool. If I was more technically minded I would think about creating a package.

0 Likes

#18

It sounds like weā€™re treading pretty similar territory.

After about a year of using half-baked workflows I had cobbled together from orgmode, todotxt, iTodo, and various other packages, I decided to switch to Plain Tasks a few weeks ago. Almost immediately I started re-writing it to make it more Markdown-like. After a few days I gave up and went back to my half-baked system, which currently consists of the following:

  • A syntax almost identical to todo.txt, along with some useful commands to add and complete actions. This is where I find use for the All Fold command, because the syntax is just one item per line. Iā€™ve taken the plunge and am trying to implement a full GTD system, and, although I dislike this format, I found I have the least resistance to using it.

  • A syntax which is a sort of fast-and-loose Markdown. For example, you can cram things next to each other without empty lines (which is not valid in strict Markdown):

# Heading 1
## Heading 2
- List item
## Heading 2

This syntax is in flux always and mostly broken at the moment.

Iā€™ve gone ahead and installed the Clickable URLs plugin and chucked my own orgmode-derived abominations.

I have some mostly working code for opening local files with Wiki style links [other-file.md]. (From the Wiki plugin).

I use SmartMarkdown but it doesnā€™t ā€œfeelā€ right, although itā€™s probably an improvement over what Sublime provides by default. Navigating a file with lots of folded coded is very annoying and very easy to erase entire chunks. I wish there was a plugin that made folded text indestructible.

I think that ā€œzoomā€ is an interesting idea. It should be possible to write a plugin that does this by folding the text above and below the current location. I think a sane way of doing this is to re-write the Markdown (or whatever) syntax so that thereā€™s a scope defined between headings (including the heading itelf), otherwise you have to resort to regexs at the plugin level. (Does this make sense?)

Iā€™m very fond of the orgmode checkboxes:

-  ] Incomplete item
- [X] Completed item

For about the first half of 2012, all my organization was basically Markdown + orgmode-style checkboxes.

(Then this happened: viewtopic.php?f=3&t=7388&hilit=+usb#p34327 )

I just watched the Folding Text video again, and I noticed that the ā€œ.todoā€ lists use checkboxes. Iā€™ve been trying to figure out how to use checkboxes or checkbox-like things with the minimum of conversion difficulties (via Pandoc and the like) and have encountered the difficulty you also point out. I think Iā€™ll go back to combining the Markdown syntax with the checkboxes (and maybe some bits of the GTD syntax/commands) and worry about how to convert to other formats later.

Although I like the Workflow/PaperTasks/FoldingText simplicity, one of the things Iā€™ve found really useful is to date everything, so I can refer to it later. I only need the date, not the time, but itā€™s pretty intrusive:

- incomplete item @added(2012-02-06)
+ complete item @added(2012-05-24) @done(2012-06-02)

In my todo.txt syntax, I color it using the comment scope so that I can tune it out, but I can only do so because the dates are always at the beginning of the line.

Maybe folding these meta-tags, coupled with the orgmode checkboxes is the way to go:

-  ] incomplete item ...]
- [X] complete item ...]

Iā€™m not quite sure how this would work, though.

(Sorry for the long, chaotic post.)

Alex

0 Likes

#19

I opened an issue on SmartMarkdown regarding folding the content on the same line as the heading:
github.com/demon386/SmartMarkdown/issues/12

(Although the developer of SmartMarkdown has stated heā€™s quite busy.)

0 Likes

#20

Does it support multiple pattern or single matching ?

0 Likes

#21

FWIW you can do ad-hoc folding without any plugins by selecting all the text of interest (e.g., via Find All), and then using the Edit/Code Folding menu. In S1 there was an invert_selection command, which would enable selecting all the non-matching bits of text, Iā€™ll have to add it back in for S3.

0 Likes

#22

Not sure what you mean. It matches all lines based on one pattern (string, actually; I failed to rewrite this as a regex).

What I would like to do is to automate precisely this. I think the ideal command would be folding (as well as inverted folding) based on a regex. This is very useful for logfiles, todo lists and other files where lines are standalone. (Some kind of hook between Goto Symbol and this command would also be usefulā€¦)

Alex

0 Likes

#23

http://ndudfield.com/zencoding/old/Untitled\Untitled_media\Untitled.gif

I wrote a command back in the day for someone on the forum along them lines. The command would use folding to preview lines matching a pattern to filter from the document.

Iā€™ll see if I can dig up the code.

0 Likes

#24

That looks interesting ā€“ and more sophisticated (i.e., more moving parts) than what I described, but Iā€™d have to use it to be sure ā€“ please share your code if you can dig it up!

Incidentally, I see you have a TODO.txt file open. Do you use anything to parse it, or do you use a long static file that you just scroll through?

Alex

0 Likes

#25

TODO.txt is just one big text file Iā€™ve been using since 2009 or so

New entries go at the top of the file

Donā€™t really use any special plugins

I always was annoyed that I hadnā€™t gotten around to making it show the whole current function/class/node/etc of a match, rather than a simplistic 2 lines above/below

More so, when using a similar technique for debugging / writing tmLanguage files

I might just try writing it again because I canā€™t find the code for that plugin

0 Likes

#26

Ah, I see. I thought it parsed the indentation or the scope, which would have been quite a bit more ambitious.

Youā€™re such a tease, Iā€™m still waiting for those keybindingsā€¦ :unamused:

0 Likes

#27

Soz, busy as shit atm :smile:

My old manā€™s coming to visit in me (overseas for him) and got an event need to have a new website up for.

Trying to smash it out so I can spend some time with him

FFFFFF. deadlines :confused:

0 Likes

#28

Pft! Excuses, excuses :wink:

0 Likes

#29

FFFFF. Now you gone and called me on my BS.

Ok, you got me, itā€™s trick screencast photography. Itā€™s not a real plugin.

0 Likes

#30

If anyone is playing along, I have a slightly improved version of this plugin here:
raw.github.com/alehandrof/MarkD ā€¦ Folding.py

Itā€™s part of a bigger mess called MarkDo that I will clean up and describe soonish*.

What it does

Trigger the plugin with a binding such as this:

{ "keys": "ctrl+'"], "command": "all_toggle", "args": { "foldstr": ""  } },

If there is nothing folded, it will fold all lines NOT matching (in order of preference):

  1. the selection
  2. the ā€œfoldstrā€ argument
  3. user input

When there are folded sections, triggering the plugin will unfold everything. (Itā€™s a toggle, you see.)

Sorry for the very sloppy instructions :smile: Let me know if you need any help using this.

  • One hopes.

Alex

0 Likes