Version 0 of End of line "\" elimination

Updated 2022-04-04 07:39:17 by JHJL2

I like to place the widget arguments on separate lines but I dislike having to use line continuations "\" because it makes the code look dense. So I have been experimenting with the following code layout hack:

proc naivelyRemoveComments {body} {
  regsub -all -line {((^[ \t;]*[#].*$)|([ \t][;][ \t]*[#].*$))} $body ""
}

proc ~~ {body} {
  regsub -all -- "\n" [naivelyRemoveComments $body] " " body
  return [uplevel 1 $body]
}

The ~~ command simply concatenates lines using a space character and then executes the resulting script. I used "~~" because it helps highlight that something doggy is going on and it also looks good if your editor font supports ligatures :)

The naivelyRemoveComments command should deal with simple comment placements but will fail if the ";#" is within a string literal

# Demo
package require Tk
proc mybutton {w txt cmd args} {
  ~~ {
    # my cool button
    button $w
      -text     $txt
      -command  $cmd
      {*}$args  ;# additional button options can override the defaults
  }
}

set tb [~~ {
  mybutton .tb "Click me" [list puts "clicked!"]
    -underline  1 
    -font       {arial 30}
    -fg         blue
}]

pack .tb

For larger blocks of code containing multiple commands you must use ";" to separate each command; position the ";" to taste

~~ {

  grid [addTitle $ctn]
    -padx 4
    -pady 4
    -sticky ew ;

  grid [addCandidatesPanel $ctn]
    -padx 8
    -pady 8
    -sticky news ;

}

# vs

~~ {

  grid [addTitle $ctn]
   -padx 4
   -pady 4
   -sticky ew
  ;
  grid [addCandidatesPanel $ctn]
    -padx 8
    -pady 8
    -sticky news
   ;

 }