Would like to hide comments

Syntax highlighting, code folding, brace matching, code indenting, and function list

Would like to hide comments

Postby aleksazr » Wed Jul 04, 2012 3:56 pm

probably by changing comment color to background color... how do I do that automatically?
I want to press a shortcut key and have the comments turn to white,
and press another key to turn the color back to what it was.

If that is not possible, I could invoke an external program that would edit the UEW file.
I could just edit the file for C lang (which I use most), but it would be better if I could detect which UEW is currently in use... possible?
Then, how do I force UE to refresh the screen with the new colors?

As a last option, I could have two sets of UEW files, one with visible comments, and one with non visible comments,
probably in two separate folders. How would I quickly change between them? (of course, this option I don't like much.)
aleksazr
Basic User
Basic User
 
Posts: 16
Joined: Sun Dec 18, 2011 7:16 pm

Re: Would like to hide comments

Postby rhapdog » Wed Jul 04, 2012 6:37 pm

These options you list simply will not work well at all.

Even if you find a way to change the UEW file, it is loaded into memory by UE and the changes will not be recognized.

Let me give you a possibility:
The only way to have UE recognize the changes is to have the particular UEW file in question loaded into an edit window, and make sure that UE is configured to automatically detect file changes, and reload a file when changed. Then, you can write an external compiled program that can be launched via user tool, that would search and replace the necessary colors in the UEW file. Focus will change to that tool, then, once focus returns to UE, it will detect the change to the UEW file that has been loaded. This will update the colors in question.

This is not exactly an ideal solution, but is the only solution I can think of that will actually work the way you wish.

If you don't have the UEW file you wish to change loaded, and if UE isn't set to detect and reload changed files, then it simply won't work.

Of course, I have not tested this, but I have used an external program to make "live" changes to a UEW file and seen the changes take place on the file types controlled by that particular UEW file, so I am fairly sure the theory of doing it this way would work.
User avatar
rhapdog
Master
Master
 
Posts: 253
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA

Re: Would like to hide comments

Postby aleksazr » Wed Jul 04, 2012 7:18 pm

I know nothing about UE scripts, but, could I (or someone else :)) write a script that would
open some specific UEW file, do a search & replace (/Colors = ...), and than save and close the file?
That should update the UE syntax engine, right?

I did a quick glimpse at scripts - it seems that it can be done.
aleksazr
Basic User
Basic User
 
Posts: 16
Joined: Sun Dec 18, 2011 7:16 pm

Re: Would like to hide comments

Postby aleksazr » Wed Jul 04, 2012 7:50 pm

Hey, I did it :)

UltraEdit.open("path and file");
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.replace("/Colors = blah, blah, blah,", "/Colors = blah2, blah2...,");
UltraEdit.closeFile("path and file",1);
aleksazr
Basic User
Basic User
 
Posts: 16
Joined: Sun Dec 18, 2011 7:16 pm

Re: Would like to hide comments

Postby Mofi » Thu Jul 05, 2012 2:48 am

It would be perhaps better to use for this task a macro stored in a macro file being automatically loaded on startup. Scripts usually destroy contents of active output window on execution which you perhaps don't want here. Running the same commands from within a macro would avoid a modification of contents of active output window.

Another idea: Instead of modifying the used wordfile, you could use 2 wordfiles with identical contents, but slightly different language name and of course a different color setting for comments. The second wordfile which hides the comments has no File Extensions = at end of first line.

Now you can switch for the active file via View - View as (Highlighting File Type) from wordfile displaying the comments to the wordfile hiding the comments and vice versa. For the first 20 languages in this list a hotkey or chord can be assigned in the key mapping configuration dialog to quickly switch by key the wordfile respectively view of the comments.

If you are using smart language templates you have to duplicate also the language template file to have access to the smart templates independent of which wordfile is currently used for syntax highlighting.

The advantage of using 2 wordfiles is that the wordfile must not be opened, modified, saved and closed and therefore the recent file list is not modified with the name of the wordfile.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Would like to hide comments

Postby min2max » Mon Dec 10, 2012 8:58 pm

I'm also interested.

1. Are the comments foldable? How?
2. Is there a way to switch quickly between with and without comments or, in other words, hide and show comments?
min2max
Basic User
Basic User
 
Posts: 18
Joined: Mon Dec 10, 2012 5:41 am

Re: Would like to hide comments

Postby Mofi » Tue Dec 11, 2012 4:15 am

Block comments as defined in the syntax highlighting wordfile applied to active file can be folded. UltraEdit offers for all block comments spanning over at least 2 lines the symbol to fold it.

This is not supported for multiple consecutive lines containing line comments. The reason is quite simple. Multiple consecutive lines with line comments could contain also parts not belonging to the line comments. So it would be necessary to evaluate if there is nothing else between beginning of a line and line comment starting string than spaces and tabs. It is possible with a macro or script to fold multiple consecutive lines containing only line comments.

For example the following macro folds all blocks of multiple consecutive lines containing only line comments in C/C++/C#/Javascript files.

InsertMode
ColumnModeOff
HexOff
PerlReOn
Top
Loop 0
Find MatchCase RegExp "^(?:[ \t]*//.*\r\n){2,}"
IfFound
HideShowSelection
Key DOWN ARROW
Else
ExitLoop
EndIf
EndLoop
Top

It is also possible to use the advanced find option Hide Lines to hide all lines containing for example //. But this would hide also lines containing not only a line comment. Therefore it would be better to use Unix/Perl regular expression search string ^[ \t]*// or UltraEdit regexp search string %[ ^t]++// with Hide Lines to hide only lines containing only a line comment.

aleksazr used a different method as he hides all comments by setting the color for the comments to the background color.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Would like to hide comments

Postby spohnsoftware » Tue Dec 11, 2012 1:14 pm

I have found that this code loops on occasion. I modified it slightly to handle COBOL, i.e. an "*" in column 7. The code is:

InsertMode
ColumnModeOff
HexOff
Top
Loop 0
Find MatchCase RegExp "%??????^*"
IfFound
HideShowSelection
Key DOWN ARROW
Else
ExitLoop
EndIf
EndLoop

I removed the PERL line and replaced the regexp with my own. The problem is that when two lines are commented next to each other in the code, the script "sometimes" loops. Thank goodness for the cancel button. It seems to me like there has to be an easier way.

If I go into the Find dialog box, enter in the regexp above, select Advanced and press "Hide Lines" it works like a champ each and every time(and much faster!). I tried to "record" this process, but the macro it gave me was not even close. What function is the "Hide Lines" button performing behind the scenes and how do you recreate this and attach it to a chord?

So I followed the instructions per Mofi, yet the macro still loops once it locates two lines in succession in the code.
:cry: :cry:
spohnsoftware
Newbie
 
Posts: 3
Joined: Wed Dec 05, 2012 12:24 pm

Re: Would like to hide comments

Postby min2max » Tue Dec 11, 2012 8:09 pm

Thanks Mofi.

I'm new to UE macro. It must be a silly question, but I don't find a way to register the above code as a macro, say, FoldComments.
min2max
Basic User
Basic User
 
Posts: 18
Joined: Mon Dec 10, 2012 5:41 am

Re: Would like to hide comments

Postby Mofi » Wed Dec 12, 2012 4:50 am

The steps to save a posted macro into a macro file:

  1. Use Macro - Load to load a macro file to which the new macro should be added. If this macro should be the first macro in a macro file, ignore this step or in case of a macro file currently loaded, use Macro - Delete All to unload all already loaded macros.
  2. Click on menu item Macro - Edit Macro.
  3. Click on button New Macro.
  4. Enter the macro name FoldComments.
  5. For this macro uncheck the macro property Show Cancel dialog for this macro and let Continue if search string not found checked.
  6. If you later want to quickly run the macro by key, enter a hotkey or a chord (multi-key assignment).
  7. Click on button OK.
  8. Select the macro code in the browser and copy it to the clipboard with Ctrl+C.
  9. Select the 3 lines of the new macro with mouse or using Ctrl+A and press Ctrl+Y to replace the 3 lines with the macro code in Windows clipboard.
  10. Click on button Close and confirm the update question with a click on button Yes.
  11. Use Macro - Save All to save this macro together with all other currently loaded macros into a macro file.
The macro is now ready for usage. After every startup of UltraEdit the macro file must be loaded again for being able to execute the macro. This process can be automated by opening Macro - Set Auto Load and browsing to the just saved macro file.

The macro can be executed by

  • hotkey or chord if assigned to the macro,
  • using Macro - Play Any/Multiple Times, selecting the macro and clicking on button OK,
  • opening via View - Views/Lists - Macro List the macro list and double click on the macro to execute. The macro list can be docked on any side of the UltraEdit window with auto-hide option for quick opening by moving mouse pointer over the tab of the list.
It is not possible to assign an icon to a macro and add it to a menu or a toolbar.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Would like to hide comments

Postby min2max » Wed Dec 12, 2012 8:29 am

Thanks Mofi very much for your detailed instruction.

It now runs well as after I press the shortcut key the consecutive // comment lines now becomes
--- Lines Are Hidden ---

One other problem is when I press the shorcut again, the display does not expand to the original unfolded state.
min2max
Basic User
Basic User
 
Posts: 18
Joined: Mon Dec 10, 2012 5:41 am

Re: Would like to hide comments

Postby Mofi » Wed Dec 12, 2012 1:41 pm

Well, the macro is designed only for folding (collapsing) consecutive lines containing only line comments.

It is not possible with a macro or script to find out if a selection contains already folded lines. Therefore the same macro cannot be used to expand all collapsed blocks with consecutive line comments. It is necessary to use a second macro stored in same macro file with a different hotkey to expand all line comment blocks. This second macro is very similar to the first one for folding the comments.

InsertMode
ColumnModeOff
HexOff
PerlReOn
Top
Loop 0
Find MatchCase RegExp "^(?:[ \t]*//.*\r\n){2,}"
IfFound
HideShowExpandAll
Else
ExitLoop
EndIf
EndLoop
Top

If nothing other is folded than blocks of line comments it is also possible to use View - Hide/Show Lines - Expand All without having anything selected.

Please note that the behavior of find within a folded block is controlled with setting Automatically unfold hidden areas on Find and Goto at Advanced - Configuration - Editor Display - Code Folding.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Would like to hide comments

Postby Mofi » Wed Dec 12, 2012 2:14 pm

spohnsoftware, you replaced the Perl regular expression by an UltraEdit regular expression, but without selecting at least once the UltraEdit regular expression engine with command UltraEditReOn. That's not good as the behavior of the regular expression find is not determined in this case.

The advanced find options Hide Lines and Show Lines cannot be recorded into a macro. There is whether a macro nor a script command to hide all lines containing a string or show only lines containing a string and hide all other.

I used in the macro for folding consecutive lines containing only a line comment starting with the string // the command View - Hide/Show Lines - Hide/Show Selection. This command hides only a selection if at least 2 lines are selected at the time of execution. At least 3 lines should be selected if the configuration setting Show last line of fold in syntax highlighted files at Advanced - Configuration - Editor Display - Code Folding is checked otherwise the fold hides no lines.

The selection of 2 or more consecutive line comment lines is not possible with the UltraEdit or Unix regular expression engine. The more powerful Perl regexp engine is needed for this task.

The macro for folding 2 or more consecutive line comment lines in COBOL files is:

InsertMode
ColumnModeOff
HexOff
PerlReOn
Top
Loop 0
Find MatchCase RegExp "^(?:.{6}\*.*\r\n){2,}"
IfFound
HideShowSelection
Key DOWN ARROW
Else
ExitLoop
EndIf
EndLoop
Top

Explanation for the regular expression:

^ ... start search at beginning of a line.

(...) ... group the expression inside the parentheses. Why is answered below.

?: ... make the group a non marking group which means do not tag the string found by the expression for back reference either in the search string (only supported by Perl regexp engine) or replace string (all regexp engines).

.{6} ... find any character except new line characters carriage return or line-feed exactly 6 times. It would be possible to use also 6 points.

\* ... find next at column 7 an asterisk. As this character has a special meaning in a Perl regular expression search string, it must be escaped with a backslash for being interpreted as asterisk character.

.* ... find zero or more characters except new line characters.

\r\n ... find a carriage return and line-feed pair.

So the expression inside the parentheses finds any line with an asterisk at column 7. But required is that 2 or more consecutive lines with an asterisk at column 7 should be found. That's the reason why the expression is grouped with the parenthesis. Finding (and selecting) a block with at least 2 lines is achieved with the multiplier expression {2,} which means at least 2 times applied to the group left this multiplier expression.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Would like to hide comments

Postby min2max » Wed Dec 12, 2012 9:31 pm

Mofi wrote:<snip>
Please note that the behavior of find within a folded block is controlled with setting Automatically unfold hidden areas on Find and Goto at Advanced - Configuration - Editor Display - Code Folding.

Thanks Mofi.

I now have 2 further requirements:

1. I hope the position of caret before collapsing could be retained after the collapsing;
2. I hope the affected part, or-if that is not easy-all, of the collapsed lines expand automatically when edit takes place in the collapsed comment lines.
min2max
Basic User
Basic User
 
Posts: 18
Joined: Mon Dec 10, 2012 5:41 am

Re: Would like to hide comments

Postby Mofi » Thu Dec 13, 2012 7:17 am

1. The macro environment does not support variables. To keep current caret position there are 3 possibilities:

  1. The macro code is converted to script code as scripts support variables and therefore can remember current line and column position and set caret back after making the collapses. Scripts added to the script list can be also executed by hotkey or chord. The disadvantage in comparison to a macro is that a Cancel dialog is always displayed and usually the content of active output window is overwritten by script status information.

  2. If you usually don't use bookmarks, the macro command ToggleBookmark can be inserted above first command Top to bookmark current line and column with configuration setting Bookmark column with line at Advanced - Configuration - Editor - Bookmarks checked. The second command Top at end of the macro must be replaced by the commands

    GotoBookMark -1
    ToggleBookmark


    to move caret back to initial position on macro start.

  3. If you often use bookmarks, it is possible to insert a special string to mark current position of the caret by inserting for example above first command Top the write command "CaReT". Below second command Top the commands

    Find MatchCase "CaReT"
    Replace ""


    need to be appended to the macro to restore the caret position and remove the marker string. The disadvantage of this solution is a modification of the file with producing 2 undo steps.
Whichever solution is used, restoring the caret position after script/macro execution works only if the caret before execution is not on a line which is collapsed after script/macro execution.


2. I do not really understand your second requirement. If you want to edit something in the collapsed lines, you have to expand this part first. This can be done easily by clicking with mouse button on the [+] symbol left the collapsed lines or set caret on first line of collapsed block (with the [+] symbol left) and execute the command View - Hide/Show Lines - Hide/Show Selection. Do not select anything to expand a collapsed block. The hotkey for this command can be also configured at Advanced - Configuration - Key Mapping.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Next

Return to Syntax Highlighting