Tapatalk

Code folding (Codefaltung) in AutoLISP

Code folding (Codefaltung) in AutoLISP

1581
Power UserPower User
1581

    Feb 18, 2015#1

    UltraEdit 21.00.0.1030

    Hi,

    I tried code folding in Autolisp, but it seems not to work.

    Here is the section from UEW (downloaded from the download page)

    Code: Select all

    /L9"AutoLISP" Line Comment = ; Block Comment On = ;| Block Comment Off = |; Escape Char = \ Nocase String Chars = " File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
    /TGBegin "Commands"
    /TGFindStr = "(defun +c:^([~)]+^)("
    /TGEnd
    /TGBegin "Functions"
    /TGFindStr = "(defun +^([~)]+^)("
    /TGBegin "Arguments"
    /TGFindStr = "[ ^t^p^n^r]++^([~ ]+^)"
    /TGFindBStart = "(defun +^([~)]+^)("
    /TGFindBEnd = "^{/^}^{)^}"
    /TGEnd
    /TGEnd
    /Delimiters = '() "
    /Indent Strings = "("
    /Unindent Strings = ")"
    /Open Brace Strings =  "("
    /Close Brace Strings = ")"
    /Open Fold Strings = "(progn" "(if"              -->> that's modified, original was "("
    /Close Fold Strings = ")"
    
    Here is Autolisp pseudo code, and all blocks should be folded:

    Code: Select all

    (defun c:test ()
        (if something
            (progn
                (step1)
                (step2)
            )
            (progn
                (step3)
                (step4)
            )
        )
    )
    
    Is it possible to get the "+" and "-" sign on the left site for each "defun / progn / if"?

    Thanks

    Peter
    UE 26.20.0.74 German / Win 10 x 64 Pro

    115
    Power UserPower User
    115

      Feb 18, 2015#2

      Go back to the open fold string of "(" and the close fold string of ")". You will find that it works the best for your example.

      1581
      Power UserPower User
      1581

        Feb 18, 2015#3

        But that would bring me a folding in every line, like "(step1)" too. Autolisp consists only of brackets ...

        Peter
        UE 26.20.0.74 German / Win 10 x 64 Pro

        6,685587
        Grand MasterGrand Master
        6,685587

          Feb 18, 2015#4

          I don't know anything about syntax of AutoLISP. Therefore I don't know if the following definition is useful on *.lsp files. It works for the small example.

          /L9"AutoLISP" Nocase Line Comment Num = 2;  Block Comment On = ;| Block Comment Off = |; Escape Char = \ String Chars = " File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
          /TGBegin "Commands"
          /TGFindStr = "(defun +c:^([~)]+^)("
          /TGEnd
          /TGBegin "Functions"
          /TGFindStr = "(defun +^([~)]+^)("
          /TGBegin "Arguments"
          /TGFindStr = "[ ^t^p]++^([~ ]+^)"
          /TGFindBStart = "(defun +^([~)]+^)("
          /TGFindBEnd = "[/)]"
          /TGEnd
          /TGEnd
          /Delimiters = '( )tab"
          /Indent Strings = "("
          /Unindent Strings = ")"
          /Open Brace Strings = "("
          /Close Brace Strings = ")"
          /Open Fold Strings = "("
          /Close Fold Strings = ")"

          /Ignore Fold Strings = "(step"

          tab must be replaced by a horizontal tab character in the list of delimiters above.

          /Ignore Fold Strings definition is used for not counting all opening and closing parentheses.

          [/)] should do the same as ^{/^}^{)^}.

          ^p matches in an UltraEdit regular expression function string any type of line termination - DOS, UNIX and MAC. This is an discrepancy to meaning of ^p in UltraEdit regular expressions executed with Find/Replace/Find in Files/Replace in Files command. ^r and ^n in a function string in a wordfile are interpreted as character n and r instead of line-feed and carriage return as usual.

          As line comment starting character is the same also starts a block comment, it is necessary to define a semicolon and a space as beginning of a line comment otherwise UltraEdit would interpret also ;| as beginning of a line comment. Note: This means there must be a space after ; in the AutoLISP files for being interpreted as line comment. There are 2 spaces between ; and Block Comment On in the first definition line above.

          Another code folding solution for well indented AutoLISP files would be to use code folding by indentation level.

          /L9"AutoLISP" Nocase Line Comment Num = 2;  Block Comment On = ;| Block Comment Off = |; Escape Char = \ String Chars = " EnableCFByIndent File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
          /TGBegin "Commands"
          /TGFindStr = "(defun +c:^([~)]+^)("
          /TGEnd
          /TGBegin "Functions"
          /TGFindStr = "(defun +^([~)]+^)("
          /TGBegin "Arguments"
          /TGFindStr = "[ ^t^p]++^([~ ]+^)"
          /TGFindBStart = "(defun +^([~)]+^)("
          /TGFindBEnd = "[/)]"
          /TGEnd
          /TGEnd
          /Delimiters = '( )tab"
          /Indent Strings = "("
          /Unindent Strings = ")"
          /Open Brace Strings = "("
          /Close Brace Strings = ")"

          With using this code folding solution I would uncheck setting Show last line of fold in syntax highlighted files at Advanced - Configuration - Code Folding.
          Best regards from an UC/UE/UES for Windows user from Austria

          1581
          Power UserPower User
          1581

            Feb 18, 2015#5

            Hi mofi

            in the example above the code "(step1)" is a placeholder for many different codes. So I tried with these lines:

            Code: Select all

            /Open Fold Strings = "(defun" "(progn" "(if"
            /Close Fold Strings = ")"
            /Ignore Fold Strings = "("
            
            But there seem to be a basic problem of understanding of functionality:

            When I open a HTML, the green line on the left is filled with "+" / "-"; "Ctrl+-" folds and expands the relating code. Fine.

            But trying in Lisp I had only for one time the entire list in the green line; "Ctrl+-" in the second "progn" folds the first one, and copying the simple example disables the entire function.
            Should it work the same way as in HTML?

            Peter

            EDIT:
            - The code-folding is refreshed only with the next ENTER / CRLF in the code.
            - This is different to highlighting which is refreshed immediately.
            - Right?
            UE 26.20.0.74 German / Win 10 x 64 Pro

            115
            Power UserPower User
            115

              Re: Code-Folding (Codefaltung) in AutoLISP

              Feb 18, 2015#6

              Unlike brace strings, fold strings are not paired exactly. Any close fold string matches the preceding open fold string. Since all of your Lisp blocks end with a ")" string, that would seem ideal, but unfortunately, the ")" string is used in other places in Lisp.

              The problem isn't with the open fold strings, it is with the close fold string ")". If you take your test block and remove the ) everywhere except as the end of blocks you will see that your code folding now works the way you expect. But of course now your code won't work correctly as those ) you removed are part of the syntax.

              If you have a open fold string and a close fold string on the same line, that line is not marked for folding. Same thing if you have an open fold string on one line and a close fold string on the next line. There is nothing gained by folding in that case since UE has to show two lines after folding so UE doesn't mark the lines to fold. If you take your example code and put blank lines after each (progn and (if lines so that there are now 3 lines between open fold strings and close fold strings you will see the + and - folding characters, but they won't match up the way you expect them to be.

              When you define an open fold string like "(if" the problem is that a line like (* n (factorial (- n 1))) now contains no open fold strings but does have 3 close fold strings. So now it will match with the previous open fold string and the actual block ending ) will match with some open fold string further back in the code instead of where you expected. When the "(" is an open fold string, then each ( will match and neutralize those ) in regular lines of Lisp.

              1581
              Power UserPower User
              1581

                Feb 18, 2015#7

                Hi MickRC3
                I don't understand your answer in detail, but the conclusion is: In (Auto)Lisp is does not work in my sense.
                Right?

                I saw that selecting the matching braces with "Ctrl+B" and then "Ctrl+-" does a code folding too, but less comfortable. maybe the only way? :cry:

                Peter
                UE 26.20.0.74 German / Win 10 x 64 Pro

                6,685587
                Grand MasterGrand Master
                6,685587

                  Feb 19, 2015#8

                  Mick has explained code folding feature very good.

                  The code folding searches first in a line for one of the strings in ignore fold strings list. If one of those strings is found in the line, the line is ignored. This is the reason why your definition with ignoring all lines containing opening parenthesis does not work as this results also in ignoring the lines containing the open fold strings.

                  Next it searches in the line for open and close fold strings and count them all. As it can be seen also on the AutoLISP snippet, there is no match functionality like on brace matching as very often several open fold strings have same close fold string, not only in AutoLISP. Therefore UltraEdit counts also the closing parenthesis everywhere except on the lines with an ignore fold string as Mick has explained very good.

                  So with many varying commands between parenthesis a code folding just for defun, if and progn is not really possible even with using ignore fold strings.

                  What about my second suggestion using code folding based on indent?

                  I think that would be better for AutoLISP as long as the code blocks are well indented. Code folding based on indent those not need any open/close/ignore fold strings definition. It is really based only on indents. See for example my last post on Code folding for text outlines?

                  About update after a saved change of syntax highlighting wordfile in same instance of UltraEdit:

                  Saving a wordfile results in reloading and interpreting it immediately. On switch from wordfile to the syntax highlighted file the display must be redrawn and therefore the syntax highlighting for the displayed text must be also determined once again resulting in an immediate update of the highlighting of the words after saving the wordfile.

                  But the code folding and function list features require a parsing of entire file, not just the displayed text. Therefore a file change must be made or alternatively File - Revert to Saved is executed even if file is not modified at all since last load. This triggers parsing for functions and code folding.
                  Best regards from an UC/UE/UES for Windows user from Austria

                  1581
                  Power UserPower User
                  1581

                    Feb 19, 2015#9

                    Mofi wrote:...What about my second suggestion using code folding based on indent?....
                    Thanks a lot - I missed this info for the first time, but I think it is a fine solution.

                    Peter

                      Feb 24, 2015#10

                      Sorry, one more question:

                      Now I have this header in my UEW:

                      Code: Select all

                      /L9"AutoLISP" Nocase Line Comment Num = 2;  Block Comment On = ;| Block Comment Off = |; Escape Char = \ String Chars = " EnableCFByIndent File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
                      /Colors = 0,8421376,8421376,8421504,255,
                      /Colors Back = 16777215,16777215,16777215,16777215,16777215,
                      /Colors Auto Back = 1,1,1,1,1,
                      /Font Style = 0,0,0,0,0,
                      /Delimiters = ~!@%^&()+=|\{}[]:;"'<> ,	.?
                      /Function String = "^{(defun ^}^{^[[~^]]+^]^}"
                      /Indent Strings = "("
                      /Unindent Strings = ")"
                      The result in "commented lines" is different - see attachment. Where does it come from?

                      Peter
                      LISP_Highlight.zip (3.6 KiB)   187
                      UE 26.20.0.74 German / Win 10 x 64 Pro

                      6,685587
                      Grand MasterGrand Master
                      6,685587

                        Feb 24, 2015#11

                        The line comment is defined now different in comparison to your initial post. With the line comment definition in initial post no block comment was ever highlighted by UltraEdit as block comment.

                        Of course all lines containing a semicolon as beginning of a line comment but no space after the semicolon are now not interpreted anymore as line comment by UltraEdit.

                        You can run a Perl regular expression Replace All searching for ;(?![ |]) with a semicolon and a space character as replace string to insert after a semicolon a space character if next character is whether a | nor a space.
                        Best regards from an UC/UE/UES for Windows user from Austria

                        1581
                        Power UserPower User
                        1581

                          Feb 24, 2015#12

                          Mofi wrote:...The line comment is defined now different in comparison to your initial post. With the line comment definition in initial post no block comment was ever highlighted by UltraEdit as block comment....
                          Yes, it is the definition taken from your post.
                          Mofi wrote:...Of course all lines containing a semicolon as beginning of a line comment but no space after the semicolon are now not interpreted anymore as line comment by UltraEdit....
                          Question b) Does it come from the "Block comment"?
                          But before:
                          Question a) Is there a "Reference" with examples of all the highlighting syntax? I found the online help (very short), your pinned topics and a small FAQ, but (IMHO) there are not all features explained.
                          Mofi wrote:...You can run a Perl regular expression Replace All ....
                          I need both types: ";" and ";;;;" are both comments for the computer, but different types for me.

                          Peter
                          UE 26.20.0.74 German / Win 10 x 64 Pro

                          6,685587
                          Grand MasterGrand Master
                          6,685587

                            Feb 24, 2015#13

                            Peter wrote:Is there a "Reference" with examples of all the highlighting syntax? I found the online help (very short), your pinned topics and a small FAQ, but (IMHO) there are not all features explained.
                            Help of UltraEdit contains a quite large page about Syntax Highlighting. This page is opened for example on opening Advanced - Configuration - Editor Display - Syntax Highlighting and clicking on button Help or pressing key F1.

                            Correct me in case I'm wrong, but I think sticky Template for syntax highlighting language wordfile explains everything with the exception of /TGBegin, /TGFindStr, /TGEnd, /TGFindBStart and /TGFindBEnd. I don't want to explain them in the template topic because grouped function strings can be configured via GUI by right clicking on Function List view and left clicking on Configuration in context menu, or by opening Advanced - Configuration - Function List, selecting language and clicking on button Modify Groups.

                            There are hundreds of wordfiles available for download. So there are lots of examples. Well, many wordfiles are not designed well as most of them were not created by an expert in creating a syntax highlighting wordfile. However, this forum exists for helping users to best define a syntax highlighting language.
                            Peter wrote:Does it come from the "Block comment"?
                            No. Multiple semicolons are not interpreted anymore as line comments because just a semicolon with a space is now interpreted as line comment. That's the reason for the different look.

                            UltraEdit supports:
                            1. 0, 1 or 2 line comments with 0, 1 or 2 block comments.
                            2. 3 line comments and 0 or 1 block comment.
                            3. 4 line comments and 0 block comments.
                            I use for example the third option for batch file syntax highlighting.

                            Use for first line either

                            Code: Select all

                            /L9"AutoLISP" Nocase Line Comment Num = 2;  Line Comment Alt = ;; Block Comment On = ;| Block Comment Off = |; Escape Char = \ String Chars = " EnableCFByIndent File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
                            to get a line comment starting with two semicolons highlighted with same color as block comments and line comments starting with a semicolon and a space.

                            Or use for first line

                            Code: Select all

                            /L9"AutoLISP" Nocase Line Comment Num = 2;  Block Comment On = ;| Block Comment Off = |; Block Comment On Alt = ;; Escape Char = \ String Chars = " EnableCFByIndent File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
                            to a get line comment starting with two semicolons highlighted with the color defined for alternate block comments.

                            You can see here how to define a third or fourth line comment instead of a block comment. Just define a block comment or alternate block comment with On string only and UltraEdit interprets this string as beginning of a line comment.

                            Of course you can also use

                            Code: Select all

                            /L9"AutoLISP" Nocase Line Comment Num = 2;  Line Comment Alt = ;; Block Comment On Alt = ;| Block Comment Off Alt = |; Escape Char = \ String Chars = " EnableCFByIndent File Extensions = LSP MNL SCR PRP PRV XBS SHP MNU ISS
                            in case of wanting line comments always highlighted with same color and block comments highlighted with a different color.

                            BTW: The color and font style settings are not read anymore from *.uew file with UE v21.00 as you are using. You can remove all of them for a little faster reading in the wordfile by UltraEdit. The syntax highlighting colors for the languages are stored now in the UltraEdit theme file. Open View - Themes - Manage Themes and configure the language colors on tab Syntax. The option All Languages is usually not very handy. Configuring the languages individually makes often more sense.
                            Best regards from an UC/UE/UES for Windows user from Austria

                            1581
                            Power UserPower User
                            1581

                              Feb 24, 2015#14

                              Hi Mofi

                              both your knowledge and your helpfulness are impressive - not only now, just for some years. I hope that IDM appreciate the big value you give to the forum.

                              Peter
                              UE 26.20.0.74 German / Win 10 x 64 Pro