Reindent does not work for .tcl files like wanted by me

Reindent does not work for .tcl files like wanted by me

2
NewbieNewbie
2

    Dec 01, 2021#1

    I've tried a few things that unfortunately didn't turn out as expected.

    I use the Reindent function every day. But I keep running into the same problems that I have to intervene manually.
    And unfortunately I don't find my mistake.
    Maybe someone can help.

    Code: Select all

    proc test1 {} {
    uplevel #0 {
    set ::test 0
    }
    
    }
    
    proc test2 {} {
    
    if {$::test == 1} {
    if {$::test1 == 0} {return}}
    
    }
    
    proc test3 {} {
    global test2
    
    if {$test2 == 0} {
    set ::help_me 0
    }
    }
    
    proc test4 {} {
    global test1234
    switch -- $test1234 {
    1    {set returnvalue "" ; # any_comment }
    2    {set returnvalue "" ; # any_comment }
    10   {set returnvalue "" ; # any_comment }
    110  {set returnvalue "" ; # any_comment }
    }                         
    
    }
    
    set descr($id) {{Sets a new value for the subprogram output file's basename. The Output Basename control setting determines when this value is used.}}
    set ui_parent($id) "@CUI_OutSubFileBase"

    Code: Select all

    This should be the result:
    
    proc test1 {} {
        uplevel #0 {
            set ::test 0
        }
    }
    
    proc test2 {} {
    
        if {$::test == 1} {
            if {$::test1 == 0} {return}
        }
    }
    
    
    proc test3 {} {
        global test2
    
        if {$test2 == 0} {
            set ::help_me 0
        }
    }
    
    proc test4 {} {
        global test1234
        switch -- $test1234 {
            1    {set returnvalue "" ; # any_comment }
            2    {set returnvalue "" ; # any_comment }
            10   {set returnvalue "" ; # any_comment }
            110  {set returnvalue "" ; # any_comment }
        }                         
    
    }

    Code: Select all

    However but when i have this constellation in the Second line i get an Tab before "set ui_parent($id)" but it not should be there.
    
    set descr($id) {{Sets a new value for the subprogram output file's basename. The Output Basename control setting determines when this value is used.}}
        set ui_parent($id) "@CUI_OutSubFileBase"
    I would be grateful for any help.
    tcl_test.tcl (695 Bytes)   0
    A TCL test file
    tcl-tk.uew.zip (18.1 KiB)   0
    My settings for Reindent

    6,686585
    Grand MasterGrand Master
    6,686585

      Dec 01, 2021#2

      There is written on the help page Syntax Highlighting in chapter about Indentation:
      Any number of words may be specified in quotes (each word/string must be in a separate set of quotes "").  If an indent string occurs anywhere on a line it will be used for indenting (except when it occurs in quoted/commented text).
      There is configured in the wordfile that everything from # to end of line should be interpreted as line comment. That is done by UltraEdit which means } at end of several lines is not interpreted as unindent string as being inside a comment (lines 2, 28-31).

      Line 36 contains a ' being interpreted as beginning of a string with no ' up to end of the line being therefore interpreted as end of the string by UltraEdit. For that reason the two } at end of line 36 are ignored also by UltraEdit for unindent.

      The comment and string definition must be changed to get } interpreted as unindent string for your example.

      The string definition could be modified to just String Chars = " to interpret just " as beginning/end of a string. I don't know anything about the language Tcl/Tk and so don't know if that is a good idea or not.

      Line Comment = # must be removed as otherwise there will be never interpreted a closing brace at end of a line as unindent string containing before #

      There could be perhaps inserted above /C1"Tcl Commands" STYLE_FUNCTION the line

      Code: Select all

      /Marker Characters = "#}"
      and appended at bottom of the wordfile

      Code: Select all

      /C13"Comments/Braces"
      #}
      {
      }
      
      I don't know if that is a good idea for language Tcl/Tk. Otherwise remove Line Comment = # and append at bottom of the wordfile:

      Code: Select all

      /C13"Braces/Hash"
      #
      {
      }
      
      PS: The macro TestForDuplicate reports:

      Code: Select all

      Sorry! Found following duplicate words:
      
      ** $ -> in /C10
      ** $ -> in /C11
      ** $ -> in /C9
      
      auto_execok -> in /C1
      auto_execok -> in /C3
      
      coroutine -> in /C1
      coroutine -> in /C5
      
      bell -> in /C1
      bell -> in /C4
      
      frame -> in /C1
      frame -> in /C4
      frame -> in /C5
      
      panedwindow -> in /C1
      panedwindow -> in /C4
      
      tk_dialog -> in /C1
      tk_dialog -> in /C4
      
      tkwait -> in /C1
      tkwait -> in /C4
      
      ttk::scrollbar -> in /C1
      ttk::scrollbar -> in /C4
      
      bind -> in /C1
      bind -> in /C4
      
      grab -> in /C1
      grab -> in /C4
      
      photo -> in /C1
      photo -> in /C4
      
      tk_focusFollowsMouse -> in /C1
      tk_focusFollowsMouse -> in /C4
      
      toplevel -> in /C1
      toplevel -> in /C4
      
      ttk::separator -> in /C1
      ttk::separator -> in /C4
      
      bindtags -> in /C1
      bindtags -> in /C4
      
      grid -> in /C1
      grid -> in /C4
      
      place -> in /C1
      place -> in /C4
      
      tk_focusNext -> in /C1
      tk_focusNext -> in /C4
      
      ttk::button -> in /C1
      ttk::button -> in /C4
      
      ttk::sizegrip -> in /C1
      ttk::sizegrip -> in /C4
      
      bitmap -> in /C1
      bitmap -> in /C4
      
      image -> in /C1
      image -> in /C4
      
      radiobutton -> in /C1
      radiobutton -> in /C4
      
      tk_focusPrev -> in /C1
      tk_focusPrev -> in /C4
      
      ttk::checkbutton -> in /C1
      ttk::checkbutton -> in /C4
      
      button -> in /C1
      button -> in /C4
      
      raise -> in /C1
      raise -> in /C4
      
      tk_getOpenFile -> in /C1
      tk_getOpenFile -> in /C4
      
      ttk::combobox -> in /C1
      ttk::combobox -> in /C4
      
      ttk::style -> in /C1
      ttk::style -> in /C4
      
      canvas -> in /C1
      canvas -> in /C4
      
      label -> in /C1
      label -> in /C4
      
      scale -> in /C1
      scale -> in /C4
      
      tk_getSaveFile -> in /C1
      tk_getSaveFile -> in /C4
      
      ttk::entry -> in /C1
      ttk::entry -> in /C4
      
      ttk::treeview -> in /C1
      ttk::treeview -> in /C4
      
      checkbutton -> in /C1
      checkbutton -> in /C4
      
      labelframe -> in /C1
      labelframe -> in /C4
      
      scrollbar -> in /C1
      scrollbar -> in /C4
      
      tk_menuSetFocus -> in /C1
      tk_menuSetFocus -> in /C4
      
      ttk::frame -> in /C1
      ttk::frame -> in /C4
      
      ttk::widget -> in /C1
      ttk::widget -> in /C4
      
      clipboard -> in /C1
      clipboard -> in /C4
      
      listbox -> in /C1
      listbox -> in /C4
      
      selection -> in /C1
      selection -> in /C4
      
      tk_messageBox -> in /C1
      tk_messageBox -> in /C4
      
      loadTk -> in /C1
      loadTk -> in /C4
      
      send -> in /C1
      send -> in /C4
      
      tk_optionMenu -> in /C1
      tk_optionMenu -> in /C4
      
      ttk::label -> in /C1
      ttk::label -> in /C4
      
      console -> in /C1
      console -> in /C4
      
      lower -> in /C1
      lower -> in /C4
      
      spinbox -> in /C1
      spinbox -> in /C4
      
      tk_popup -> in /C1
      tk_popup -> in /C4
      
      ttk::labelframe -> in /C1
      ttk::labelframe -> in /C4
      
      winfo -> in /C1
      winfo -> in /C4
      
      menu -> in /C1
      menu -> in /C4
      
      text -> in /C1
      text -> in /C4
      
      tk_setPalette -> in /C1
      tk_setPalette -> in /C4
      
      ttk::menubutton -> in /C1
      ttk::menubutton -> in /C4
      
      wm -> in /C1
      wm -> in /C4
      
      destroy -> in /C1
      destroy -> in /C4
      
      menubutton -> in /C1
      menubutton -> in /C4
      
      tk -> in /C1
      tk -> in /C4
      
      tk_textCopy -> in /C1
      tk_textCopy -> in /C4
      
      ttk::notebook -> in /C1
      ttk::notebook -> in /C4
      
      entry -> in /C1
      entry -> in /C4
      
      message -> in /C1
      message -> in /C4
      
      tk_textCut -> in /C1
      tk_textCut -> in /C4
      
      ttk::panedwindow -> in /C1
      ttk::panedwindow -> in /C4
      
      event -> in /C1
      event -> in /C4
      
      option -> in /C1
      option -> in /C4
      
      tk_bisque -> in /C1
      tk_bisque -> in /C4
      
      tk_textPaste -> in /C1
      tk_textPaste -> in /C4
      
      ttk::progressbar -> in /C1
      ttk::progressbar -> in /C4
      
      ttk::intro -> in /C1
      ttk::intro -> in /C4
      
      focus -> in /C1
      focus -> in /C4
      
      tk_chooseColor -> in /C1
      tk_chooseColor -> in /C4
      
      tkerror -> in /C1
      tkerror -> in /C4
      
      ttk::radiobutton -> in /C1
      ttk::radiobutton -> in /C4
      
      font -> in /C1
      font -> in /C4
      
      pack -> in /C1
      pack -> in /C4
      
      tk_chooseDirectory -> in /C1
      tk_chooseDirectory -> in /C4
      
      ttk::scale -> in /C1
      ttk::scale -> in /C4
      
      ttk::spinbox -> in /C1
      ttk::spinbox -> in /C4
      
      SIM_feedback_message -> in /C12
      SIM_feedback_message -> in /C12
      
      The duplicate words in color group with greater color number should be removed as not used by UltraEdit.

      The macro TestForInvalid reports:

      Code: Select all

      Sorry! Found following invalid words:
      
      platform::shell <- contains the delimiter:  :
      pkg::create <- contains the delimiter:  :
      ttk::scrollbar <- contains the delimiter:  :
      ttk::separator <- contains the delimiter:  :
      ttk::button <- contains the delimiter:  :
      ttk::sizegrip <- contains the delimiter:  :
      ttk::checkbutton <- contains the delimiter:  :
      ttk::combobox <- contains the delimiter:  :
      ttk::style <- contains the delimiter:  :
      ttk::entry <- contains the delimiter:  :
      ttk::treeview <- contains the delimiter:  :
      ttk::frame <- contains the delimiter:  :
      ttk::widget <- contains the delimiter:  :
      ttk::label <- contains the delimiter:  :
      ttk::labelframe <- contains the delimiter:  :
      ttk::menubutton <- contains the delimiter:  :
      ttk::notebook <- contains the delimiter:  :
      tk::mac <- contains the delimiter:  :
      ttk::panedwindow <- contains the delimiter:  :
      ttk::progressbar <- contains the delimiter:  :
      ttk::intro <- contains the delimiter:  :
      ttk::radiobutton <- contains the delimiter:  :
      ttk::scale <- contains the delimiter:  :
      ttk::spinbox <- contains the delimiter:  :
      oo::patchlevel <- contains the delimiter:  :
      oo::version <- contains the delimiter:  :
      tk::Priv <- contains the delimiter:  :
      ttk::button <- contains the delimiter:  :
      ttk::checkbutton <- contains the delimiter:  :
      ttk::combobox <- contains the delimiter:  :
      ttk::entry <- contains the delimiter:  :
      ttk::frame <- contains the delimiter:  :
      ttk::intro <- contains the delimiter:  :
      ttk::label <- contains the delimiter:  :
      ttk::labelframe <- contains the delimiter:  :
      ttk::menubutton <- contains the delimiter:  :
      ttk::notebook <- contains the delimiter:  :
      ttk::panedwindow <- contains the delimiter:  :
      ttk::progressbar <- contains the delimiter:  :
      ttk::radiobutton <- contains the delimiter:  :
      ttk::scale <- contains the delimiter:  :
      ttk::scrollbar <- contains the delimiter:  :
      ttk::separator <- contains the delimiter:  :
      ttk::sizegrip <- contains the delimiter:  :
      ttk::spinbox <- contains the delimiter:  :
      ttk::style <- contains the delimiter:  :
      ttk::treeview <- contains the delimiter:  :
      ttk::widget <- contains the delimiter:  :
      && <- contains the delimiter:  &
      <= <- contains the delimiter:  =
      >= <- contains the delimiter:  =
      || <- contains the delimiter:  |
      mom_attr_OPER_<name> <- contains the delimiters: < >
      mom_attr_PART_<name> <- contains the delimiters: < >
      mom_attr_PROGRAMVIEW_<name> <- contains the delimiters: < >
      mom_attr_TOOL_<name> <- contains the delimiters: < >
      
      A word ...series of characters delimited by a word delimiter as defined with /Delimiters = ... cannot contain a word delimiting character, with the exception at beginning of the word to syntax highlight. The reported combination of operators can be simply deleted in the wordfile. The others need an individual change, i.e. removing the colon from the list of word delimiters if that has no bad effect somewhere or the words itself are split up in the color groups according to the delimiters.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Dec 02, 2021#3

        Hi Mofi,

        Thanks for the fast feedback and your solution. 
        I removed the invalid/duplicated words from the file, and also removed the # from Line Comment
        Now it works as you described. 

        That is a relief so that I can work a little more automated. 

        I still have two questions. 

        How do I get that now a line break between two brackets "}}"? (See the situation in tcl_test.tcl line 14.)
        Is it possible to highlight everything that is starting with # like it was before?

        I know you are not familiar with the TCL/TK coding. 
        But its very simply code:

        All code which is starting with an # it's just a comment.

        There is one exception. It is included in my tcl_test.tcl file. As soon as I write the command uplevel a # is allowed in the TCL code and is not used as a comment but as a code. So example would be here:

        Code: Select all

        uplevel #0 { 
            ..... 
        }
        
        The syntax is always the same.
        Tcl code: uplevel followed by an # and then comes an integer without any space

        How can I distinguish this from a comment?
        tcl-tk_uew_update.zip (17.51 KiB)   0
        Updated_tcl-tk_uew

        6,686585
        Grand MasterGrand Master
        6,686585

          Dec 03, 2021#4

          1. Reformatting lines in addition to re-indenting the lines

          The command Reindent selection is just for reindenting the lines within a selection. It does not reformat the lines by inserting line breaks, removing or adding spaces around brackets, etc. UltraEdit has built-in the support for the third-party tool Artistic Style Formatter which is installed with UltraEdit. That tool really reformats a source code file according to multiple available options. But that tool supports only a specific set of programming languages. TCL/TK is not included as supported language.

          However, reformatting of lines before reindenting them can be done with an UltraEdit macro or an UltraEdit script using regular expression replaces. Here is a macro example:

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          Top
          TrimTrailingSpaces
          PerlReOn
          Find MatchCase RegExp "^(?:.(?!{{))+\K}}$"
          Replace All "}\r\n}"
          Top
          Find MatchCase RegExp "}\r\n\K(?:\r\n)+([\t ]*?)(?=})"
          Replace All "\1"
          SelectAll
          ReIndentSelection
          Top
          
          The macro first deletes all trailing normal spaces and horizontal tabs in entire file. Next it runs a case-sensitive (faster) Perl regular expression replace all searching for a line ending with }} not having {{ left in the line and inserts a carriage return and a line-feed (DOS/Windows line ending) between the two closing braces. It next runs one more case-sensitive Perl regular expression replace all to find one or more empty lines between a line ending with } and another line having also a closing brace after 0 or more horizontal tabs or normal spaces. Finally the entire file is selected, the command reindent selection is executed and the caret is moved to top of the script. The attached ZIP file contains tcl_test.tcl reformatted and reindented by this macro.

          2. Highlight everything from # with a color, except ...

          The syntax highlighting of UltraEdit is strictly word based. It is not possible to take the context of a word or a word delimiter like # into account. So it is impossible to define a rule like:
          • highlight everything from # to end of the line,
          • except there is left the string update and one or more spaces/tabs and there is next to # a digit,
          • and do not interpret one or more } as part of the string to highlight.
          There are text editors which support regular expression search strings in the syntax highlighting definition file making it possible to use complex Perl compatible regular expressions to take the context around strings (not words) to highlight with a specific color into account. But UltraEdit v28.20 does not yet support Perl regular expressions for syntax highlighting, just for finding strings to show in the function list.

          Syntax highlighting of UltraEdit supports only four types to highlight more than one word:
          1. Line comments ... cannot be used here as there are lines with } which does not belong to the comment.
          2. Block comments ... cannot be used here too for the same reason as for line comments.
          3. Strings ... cannot be used here too for the same reason as for line comments.
          4. Marker characters which is a pair of word characters, more precisely word delimiters, to highlight everything from first to second character of the marker characters pair in same line. If a line has only the first marker character, but not the second marker character, nothing is highlighted in that line.
          The attached ZIP file contains such a marker characters definition. The string from first to second marker character is highlighted with the color of the color group on where this marker character pair is added like a word to highlight. So there is a string containing the two word delimiters #} in the added color group 13 with the name Comments/Braces interpreted by UltraEdit not as word to highlight, but as information that the string from first to second marker character should be highlighted with the color and font style defined for this color group.

          Something better is unfortunately not possible for TCL/TK syntax highlighting.

          3. Contents of the attached ZIP file

          The attached ZIP file contains a once more updated syntax highlighting wordfile for TCL/TK
          • with keyphrase Line Comment = removed;
          • with the delimiters sorted (not really necessary, but I favor that);
          • with resorted words as required for a valid UE/UES syntax highlighting wordfile;
          • with expression operator / defined correct by using at beginning of the line // as required as otherwise UE/UES interpret this character as beginning of one of its keyphrases;
          • with ** $ removed from color group 10 and 11 because of strings starting with $ are highlighted already with the settings of color group 9;
          • with color group 13 with name Comments/Braces added of which color and font style settings are used for strings between the marker characters #} and the word delimiters # { and }. The color and font style for this added color group must be configured by you in the Manage Themes dialog window on tab Syntax for language Tcl/Tk.
          The ZIP file contains also the reformatted and reindented example file tcl_test.tcl.
          tcl-tk_files_v3.zip (18.4 KiB)   1
          This ZIP file contains version 3 of wordfile tcl-tk.uew and reformatted and reinndent TCL example file.
          Best regards from an UC/UE/UES for Windows user from Austria