rmt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/sh
  2. # the next line restarts using wish \
  3. exec wish "$0" ${1+"$@"}
  4. # rmt --
  5. # This script implements a simple remote-control mechanism for
  6. # Tk applications. It allows you to select an application and
  7. # then type commands to that application.
  8. package require Tk
  9. wm title . "Tk Remote Controller"
  10. wm iconname . "Tk Remote"
  11. wm minsize . 1 1
  12. # The global variable below keeps track of the remote application
  13. # that we're sending to. If it's an empty string then we execute
  14. # the commands locally.
  15. set app "local"
  16. # The global variable below keeps track of whether we're in the
  17. # middle of executing a command entered via the text.
  18. set executing 0
  19. # The global variable below keeps track of the last command executed,
  20. # so it can be re-executed in response to !! commands.
  21. set lastCommand ""
  22. # Create menu bar. Arrange to recreate all the information in the
  23. # applications sub-menu whenever it is cascaded to.
  24. . configure -menu [menu .menu]
  25. menu .menu.file
  26. menu .menu.file.apps -postcommand fillAppsMenu
  27. .menu add cascade -label "File" -underline 0 -menu .menu.file
  28. .menu.file add cascade -label "Select Application" -underline 0 \
  29. -menu .menu.file.apps
  30. .menu.file add command -label "Quit" -command "destroy ." -underline 0
  31. # Create text window and scrollbar.
  32. text .t -yscrollcommand ".s set" -setgrid true
  33. scrollbar .s -command ".t yview"
  34. grid .t .s -sticky nsew
  35. grid rowconfigure . 0 -weight 1
  36. grid columnconfigure . 0 -weight 1
  37. # Create a binding to forward commands to the target application,
  38. # plus modify many of the built-in bindings so that only information
  39. # in the current command can be deleted (can still set the cursor
  40. # earlier in the text and select and insert; just can't delete).
  41. bindtags .t {.t Text . all}
  42. bind .t <Return> {
  43. .t mark set insert {end - 1c}
  44. .t insert insert \n
  45. invoke
  46. break
  47. }
  48. bind .t <Delete> {
  49. catch {.t tag remove sel sel.first promptEnd}
  50. if {[.t tag nextrange sel 1.0 end] eq ""} {
  51. if {[.t compare insert < promptEnd]} {
  52. break
  53. }
  54. }
  55. }
  56. bind .t <BackSpace> {
  57. catch {.t tag remove sel sel.first promptEnd}
  58. if {[.t tag nextrange sel 1.0 end] eq ""} {
  59. if {[.t compare insert <= promptEnd]} {
  60. break
  61. }
  62. }
  63. }
  64. bind .t <Control-d> {
  65. if {[.t compare insert < promptEnd]} {
  66. break
  67. }
  68. }
  69. bind .t <Control-k> {
  70. if {[.t compare insert < promptEnd]} {
  71. .t mark set insert promptEnd
  72. }
  73. }
  74. bind .t <Control-t> {
  75. if {[.t compare insert < promptEnd]} {
  76. break
  77. }
  78. }
  79. bind .t <Meta-d> {
  80. if {[.t compare insert < promptEnd]} {
  81. break
  82. }
  83. }
  84. bind .t <Meta-BackSpace> {
  85. if {[.t compare insert <= promptEnd]} {
  86. break
  87. }
  88. }
  89. bind .t <Control-h> {
  90. if {[.t compare insert <= promptEnd]} {
  91. break
  92. }
  93. }
  94. ### This next bit *isn't* nice - DKF ###
  95. auto_load tk::TextInsert
  96. proc tk::TextInsert {w s} {
  97. if {$s eq ""} {
  98. return
  99. }
  100. catch {
  101. if {
  102. [$w compare sel.first <= insert] && [$w compare sel.last >= insert]
  103. } then {
  104. $w tag remove sel sel.first promptEnd
  105. $w delete sel.first sel.last
  106. }
  107. }
  108. $w insert insert $s
  109. $w see insert
  110. }
  111. .t configure -font {Courier 12}
  112. .t tag configure bold -font {Courier 12 bold}
  113. # The procedure below is used to print out a prompt at the
  114. # insertion point (which should be at the beginning of a line
  115. # right now).
  116. proc prompt {} {
  117. global app
  118. .t insert insert "$app: "
  119. .t mark set promptEnd {insert}
  120. .t mark gravity promptEnd left
  121. .t tag add bold {promptEnd linestart} promptEnd
  122. }
  123. # The procedure below executes a command (it takes everything on the
  124. # current line after the prompt and either sends it to the remote
  125. # application or executes it locally, depending on "app".
  126. proc invoke {} {
  127. global app executing lastCommand
  128. set cmd [.t get promptEnd insert]
  129. incr executing 1
  130. if {[info complete $cmd]} {
  131. if {$cmd eq "!!\n"} {
  132. set cmd $lastCommand
  133. } else {
  134. set lastCommand $cmd
  135. }
  136. if {$app eq "local"} {
  137. set result [catch [list uplevel #0 $cmd] msg]
  138. } else {
  139. set result [catch [list send $app $cmd] msg]
  140. }
  141. if {$result != 0} {
  142. .t insert insert "Error: $msg\n"
  143. } elseif {$msg ne ""} {
  144. .t insert insert $msg\n
  145. }
  146. prompt
  147. .t mark set promptEnd insert
  148. }
  149. incr executing -1
  150. .t yview -pickplace insert
  151. }
  152. # The following procedure is invoked to change the application that
  153. # we're talking to. It also updates the prompt for the current
  154. # command, unless we're in the middle of executing a command from
  155. # the text item (in which case a new prompt is about to be output
  156. # so there's no need to change the old one).
  157. proc newApp appName {
  158. global app executing
  159. set app $appName
  160. if {!$executing} {
  161. .t mark gravity promptEnd right
  162. .t delete "promptEnd linestart" promptEnd
  163. .t insert promptEnd "$appName: "
  164. .t tag add bold "promptEnd linestart" promptEnd
  165. .t mark gravity promptEnd left
  166. }
  167. return
  168. }
  169. # The procedure below will fill in the applications sub-menu with a list
  170. # of all the applications that currently exist.
  171. proc fillAppsMenu {} {
  172. set m .menu.file.apps
  173. catch {$m delete 0 last}
  174. foreach i [lsort [winfo interps]] {
  175. $m add command -label $i -command [list newApp $i]
  176. }
  177. $m add command -label local -command {newApp local}
  178. }
  179. set app [winfo name .]
  180. prompt
  181. focus .t
  182. # Local Variables:
  183. # mode: tcl
  184. # End: