Posted to tcl by sebres at Fri Sep 11 19:43:15 GMT 2020view raw

  1. proc cm args {
  2. array set in {-out auto}
  3. array set in $args
  4. set scr ""
  5. puts -nonewline "%cm% "; flush stdout
  6. while {[gets stdin line] >= 0 && ($line ni {/cm /exit} || $scr ne "")} {
  7. append scr $line "\n"
  8. if {![info complete $scr]} {
  9. puts -nonewline " ... "; flush stdout
  10. continue
  11. }
  12. if {[catch {uplevel 1 $scr} msg]} {
  13. puts stderr "\7 \[\u212E\] $msg"
  14. } elseif {$msg ne ""} {
  15. if {$in(-out) ni {0 off no false}} {
  16. if {$in(-out) eq "auto"} {
  17. set msg [string map {"\r\n" "\u255c" "\n" "\u2518" "\r" "\u2534" "\t" "\u25A1" "\b" "\u2190" "\7" "\u266B"} $msg]
  18. if {[string length $msg] > 80} {set msg [string range $msg 0 77]...}
  19. }
  20. puts " \u2550\u2550\u25ba $msg"
  21. }
  22. }
  23. set scr ""
  24. puts -nonewline "%cm% "; flush stdout
  25. }
  26. }
  27.  
  28. return
  29.  
  30. ## ------------------------------------------
  31. ## Usage example:
  32. ## ------------------------------------------
  33.  
  34. %
  35. % cm
  36. %cm% set a 5
  37. PPº 5
  38. %cm% set b
  39. [.] can't read "b": no such variable
  40. %cm% string repeat a\r\nb\nc 65536
  41. PPº a\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\bca\b...
  42. %cm% /cm
  43. %
  44. % cm -out off
  45. %cm% set a 5
  46. %cm% puts $a
  47. 5
  48. %cm% /cm
  49. %