Topic: Electric scissors: Collapsing+Expanding text (1 of 13), Read 44 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Tuesday, November 15, 2005 05:23 AM


Today I wrote a little new tool: Electric scissors for text files.

It mimics a feature some other programs have:
You can collapse some lines of text to make the remaining part smaller and so easier to read.
At will you can of course expand those lines again.

In fact those lines are not hidden, but cut out and stored elsewhere... (Those cut-out lines are stored in a file PATHNAME.clx)

I'm using the keys Ctrl-Numpad- and Ctrl-Numpad+ for this macro.

Please note: This is a beta version!
It's not yet fully stress-tested!
So use at your own risk!



Example:

Imagine some code like this:


// Check version #, first time in.
//
if (#69==0) {
#100 = 610
if (Version_Num<#100) {
Reg_Push(0,1)
Reg_Set(1,"Field delimiting macro requires VEDIT version ")
#1 = #100/100
#2 = Remainder
ITOA(#1,1,LEFT+NOCR+APPEND)
Reg_Set(1,".",APPEND)
ITOA(#2,1,LEFT+NOCR+APPEND)
Reg_Set(1," or later.",APPEND)
Alert()
Dialog_Input_1(0,`"Error","|@(1)"`)
Reg_Pop(0,1)
if (Is_Quiet) {
XALL(1)
} else {
Break_Out(EXTRA)
}
}
}



Now highlighting the inner part of the big if() and collapsing it (via Ctrl-Numpad-)
gives:


//
// Check version #, first time in.
//
if (#69==0) {
#100 = 610
if (Version_Num<#100) {
// %<--00007-- -->%
}
}



(Imagine those %< as a pair of scissors)
A bit more readable, isn't it?

Now some commenting within the label improves it a bit more:


//
// Check version #, first time in.
//
if (#69==0) {
#100 = 610
if (Version_Num<#100) {
// %<--00007-- do something complicated -->%
}
}



Positioning the cursor in that line of scissors and pressing Ctrl-Numpad+
expand the lines again and all is as it was originally.


The macro can be downloaded here: http://www.ziemski.net/vedit/macros/collexp.vdm

Comments, suggestions etc. are welcome.


Christian


Edit: At 2005-11-15 I enhanced it a bit: Indention and (some) comment characters are automatically inserted before a label.
Additionally the macro shows a message box when initially called: It says that it made some key assignments.

 


Topic: Electric scissors: Collapsing+Expanding text (2 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Pauli Lindgren
Date: Monday, November 21, 2005 11:10 AM

On 11/15/2005 5:23:49 AM, Christian Ziemski wrote:
>
> You can collapse some lines of text to make the remaining
> part smaller and so easier to read.

This is definitely a significant feature Vedit is missing.
I think it is usually called Code Folding.


> Now highlighting the inner part of the big if() and
> collapsing it (via Ctrl-Numpad-)

You need to manually highlight the block?
Maybe the macro could automatically select the block by using either braces {} or indent level.

In my C keyboard configuration, I have the following keyboard macro for selecting C block:
BOL BB(CP) S("|123") MP EOL BE(CP) GP(BB)


>(Imagine those %< as a pair of scissors)

I have usually used -- 8<------


>Comments, suggestions etc. are welcome.

I haven't tried it yet, but I'll let you know.

--
Pauli

 


Topic: Re: Electric scissors: Collapsing+Expanding text (3 of 13), Read 32 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Monday, November 21, 2005 11:56 AM

On Mon, 21 Nov 2005 11:10:00 -0500, Pauli Lindgren wrote:

>On 11/15/2005 5:23:49 AM, Christian Ziemski wrote:
>>
>> You can collapse some lines of text to make the remaining
>> part smaller and so easier to read.
>
>This is definitely a significant feature Vedit is missing.
>I think it is usually called Code Folding.

Yes, but for now my macro isn't aware of "code" but only "text".

>> Now highlighting the inner part of the big if() and
>> collapsing it (via Ctrl-Numpad-)
>
>You need to manually highlight the block?
>Maybe the macro could automatically select the block by using
>either braces {} or indent level.

I'll implement s.th. like that in the next version.

Perhaps like so: If a block is highlighted: Use it.
Else use innermost block by braces {}.


>>(Imagine those %< as a pair of scissors)
>
>I have usually used -- 8<------

Nice one.
But for this function I think I'll stick with %< since it seems much
more unlikely to be found anywhere in normal text or code. Hmmm ...


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (5 of 13), Read 34 times
Conf: VEDIT Macro Library
From: Pauli Lindgren
Date: Monday, November 21, 2005 04:36 PM

I now tested the macro.
First, id didn't work, it always gave error message "Aborted due to user request".
I then added line
#104=1
at the beginning of the macro, and now it works.

I then implemented the automatic code block detect. I replaced the these lines:

:collapse:
if (BB==-1) {
return
}

with these:

:collapse:
if (BB==-1) {
S("|{|123,|125}", REVERSE+NOERR)
if (Match_Item == 1) {
#104 = CP
Line(1)
BB(CP)
GP(#104)
Match_Paren
Line(-1) EOL
BE(CP)
} else {
return
}
}

Now, if a block has been highlighted, the macro works as before. If the block is not highlighted, you can move the cursor anywhere inside the block you want to collapse and then press the key, and the block is collapsed.

--
Pauli

 


Topic: Re: Electric scissors: Collapsing+Expanding text (6 of 13), Read 34 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Monday, November 21, 2005 05:33 PM

On Mon, 21 Nov 2005 16:36:00 -0500, Pauli Lindgren wrote:

>I now tested the macro.
>First, id didn't work, it always gave error message
>"Aborted due to user request".
>I then added line
>#104=1
>at the beginning of the macro, and now it works.

Ah, yes, thanks.
Since I always get the dialog which sets #104 I didn't see that
before.


>I then implemented the automatic code block detect.
> [...]

Funny, while you did that, I did almost the same at the same time ;-)

And added some more enhancements.


The new version can be downloaded here
http://www.ziemski.net/vedit/macros/collexp.vdm


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (7 of 13), Read 32 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Tuesday, November 22, 2005 03:16 AM


Now I
- implemented the "expand all" function with Ctrl-Numpad*
- fixed some things
- filled in a to-do-list as comments at the beginning

http://www.ziemski.net/vedit/macros/collexp.vdm


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (9 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Pauli Lindgren
Date: Tuesday, November 22, 2005 03:14 PM

On 11/22/2005 3:16:18 AM, Christian Ziemski wrote:
>
>Now I
> - implemented the "expand all" function with Ctrl-Numpad*
> - fixed some things
> - filled in a to-do-list as comments at the beginning

I found some problems with the latest version.

- If you call the macro and the keys have already been defined, the buffer witch key listing is left open.

- Collapse removes the parenthesis, too. This is not nice, especially if you have something like

if (x) {
do_something;
} else {
something_else;
}


if you collapse the "do_something" in the above, then both if and else lines are deleted. You can not collapse both if and else branch.
I think it is better to only delete the lines inside braces, as in my earlier message.

- If you collapse two sections, then expand them back, then close the file without saving, then the first section is saved as collapsed and there is no way to restore it. (Why is the save command there anyway?)

--
Pauli

 


Topic: Re: Electric scissors: Collapsing+Expanding text (10 of 13), Read 32 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Tuesday, November 22, 2005 05:07 PM

On Tue, 22 Nov 2005 15:14:00 -0500, Pauli Lindgren wrote:

>I found some problems with the latest version.

Pauli:

Thanks for testing!

>- If you call the macro and the keys have already been defined,
>the buffer witch key listing is left open.

I'll have a look later this evening.

>- Collapse removes the parenthesis, too. This is not nice,
>especially if you have something like
>

> if (x) {
> do_something;
> } else {
> something_else;
> }
>

>
>if you collapse the "do_something" in the above, then both if
>and else lines are deleted.
>You can not collapse both if and else branch.
>I think it is better to only delete the lines inside braces,

That's already on my to-do list.


>- If you collapse two sections, then expand them back, then
>close the file without saving, then the first section is saved
>as collapsed and there is no
>way to restore it. (Why is the save command there anyway?)

Haven't seen that yet. I know that there are some "security" problems.
I'll try to fix those ones next.

(The macro seems not really to be in beta state.
It's still "heavy under construction" ;-)


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (11 of 13), Read 34 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Tuesday, November 22, 2005 05:08 PM

On Tue, 22 Nov 2005 15:14:00 -0500, Pauli Lindgren wrote:

>- If you call the macro and the keys have already been defined,
>the buffer witch key listing is left open.

Fixed.


>- Collapse removes the parenthesis, too. This is not nice,
>especially if you have something like
>
>  if (x) {
> do_something;
> } else {
> something_else;
> }

>
>if you collapse the "do_something" in the above, then both if and
>else lines are deleted. You can not collapse both if and else branch.
>I think it is better to only delete the lines inside braces, as in
>my earlier message.

Now only the lines inside braces are being folded as you wrote.

Anyway: In the following two cases there is no collapsing:

if (x) { do_something; }
else { something_else;
}



>- If you collapse two sections, then expand them back, then close
>the file without saving, then the first section is saved as
>collapsed and there is no way to restore it.

Fixed. The now removed File_Save() has not been a good idea.

As you can see in the macro-included to-do-list there is much to do
for data reliability. To catch all those cases...


I think the level the macro has reached now is a good starting point
for further work.

http://www.ziemski.net/vedit/macros/collexp.vdm


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (13 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Wednesday, November 23, 2005 03:29 AM


- Fixed a problem when using mixed Windows/UNIX type files.
Config(F_F_TYPE) has to be explicitely set!

- Added checks to avoid unnecessary nested collapses

- Enhanced "Expand all" to expand really all (incl. possible nested collapses)


http://www.ziemski.net/vedit/macros/collexp.vdm


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (4 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Ted Green
Date: Monday, November 21, 2005 12:17 PM

At 11:11 AM 11/21/2005, you wrote:

>This is definitely a significant feature Vedit is missing.
>I think it is usually called Code Folding.

I am following this discussion with great interest.
Yes, a key that fold the current subroutine would be useful.

I still want to re-design VEDIT's virtual file handling into a linked list of memory segments (some on disk, some in memory).
I now see how to implement folding into this new virtual file structure.

Ted.

 


Topic: Re: Electric scissors: Collapsing+Expanding text (8 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Christian Ziemski
Date: Tuesday, November 22, 2005 12:51 PM

On Mon, 21 Nov 2005 00:17:00 -0500, Ted Green wrote:

>I still want to re-design VEDIT's virtual file handling into
>a linked list of memory segments (some on disk, some in memory).
>I now see how to implement folding into this new virtual file
>structure.

Ted:

I suppose that will not happen for 6.15, even not for any 6.1x ?


Christian

 


Topic: Re: Electric scissors: Collapsing+Expanding text (12 of 13), Read 33 times
Conf: VEDIT Macro Library
From: Ted Green
Date: Tuesday, November 22, 2005 05:11 PM

At 12:53 PM 11/22/2005, you wrote:

>I suppose that will not happen for 6.15, even not for any 6.1x ?

Correct. I think I have the 6.15 code done. Testing it now.

Ted.