jed-users mailing list

[2003 Date Index] [2003 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: Folding


On Wed, May 14, 2003 at 07:21:20AM +0200, Johann Gerell wrote:
> Hi,
> 
> I seem to remember that John a couple of years ago answered a question on an alternative way of performing folds. The questionee wanted to fold a C source file (or similar) based on the indents in the file. For example, he wanted (the underscores are there for simulating spaces)
> 
> __if(expression) {
> ____DoLotsOfThings();
> ____.
> ____.
> ____.
> ____Done();
> __}
> 
> to be folded as
> 
> __if(expression) ...
> 
> or something like that. 

Folding is (simply speaking) hiding some lines based on meta-information +
accounting the number of folds...

If you do not care too much about the accounting, help is easy using the
set_line_hidden(0/1) intrinsic function. I use this together with regexp
search for a view-filter (see attachment). It is iserted into the Menu by

define gm_load_popup_hook (menubar)
{
   menu_insert_item (5, "Global.&Buffers", "&Hide Matching Lines", "set_matching_hidden");
   menu_insert_item (6, "Global.&Buffers", "Show &Only Matching Lines", "set_matching_hidden(0)");
   menu_insert_item (7, "Global.&Buffers", "Show &All Lines", "set_buffer_hidden(0)");
   % ... + many more ...
}
append_to_hook ("load_popup_hooks", &gm_load_popup_hook);

in my .jedrc.

You can either just call it with the right regexp, or use it as model for
writing your own filter rules (or ask me to do it, well, the idea is not
bad, so I really might want to do it some day :-).

I imagine a function set_indented_hidden(indent=red_mini) that can either
be called with an argument giving the number of spaces or ask this in the
minibuffer. (Obviously, set_indented_hidden(0) will unhide the buffer.)

Günter

-- 
Milde at ife.et.tu-dresden.de
% --- Filtered view: show/hide lines that match a pattern

autoload("get_comment_info", "comments");
autoload("push_defaults", "sl_utils");    % see jedmodes.sf.net

% Hide/unhide the whole buffer
public define set_buffer_hidden() % (hide=1)
{
   variable hide = 1;
   if (_NARGS)
     hide = ();
   push_spot();
   !if (is_visible_mark())
     mark_buffer();
   set_region_hidden(hide);
   pop_spot();
}

% Filter: hide all lines that match the regexp pat   
public define set_matching_hidden() %(hide=1, [pat])
{
   variable hide, pat;
   (hide, pat) = push_defaults(1, NULL, _NARGS);
   
   variable prompt = ["Show only", "Hide all"];
   prompt = prompt[hide] + " lines containing regexp:";
   if (pat == NULL)
     pat = read_mini(prompt, "", ".*");
   
   push_spot_bob();
   % speadup for "matchall"
   if (pat == ".*")
     {
	set_buffer_hidden(hide);
	pop_spot();
	return;
     }

   if (hide)
     while (andelse{not(eobp())}{re_fsearch(pat)})
       {
	  set_line_hidden(hide);
	  eol;
       }
   else  % hide non-matching
     {
	do
	  set_line_hidden(not(string_match(line_as_string, pat, 1)));
	while (down_1);
     }
     
   pop_spot();
}

public define toggle_hidden_lines()
{
   push_spot_bob();
   do
     set_line_hidden(not(is_line_hidden()));
   while (down_1);
   pop_spot();
}

public define set_comments_hidden() % (hide=1)
{
   variable hide;
   (hide) = push_defaults(1, _NARGS);
   variable cinfo = get_comment_info();
   variable white = "[ \\t]*";
   variable pat = "^" + white + cinfo.cbeg + ".*" + cinfo.cend + white + "$";
   set_matching_hidden(hide, pat);
}

[2003 date index] [2003 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]