rmt 5.4 KB

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