1 # this one avoids absolute path names: \
3 # =========================================================================
4 # File: sampleclient.tcl, chb, Sun 05.11.1995 (19:24)
5 # sampleclient.tcl,v 1.1.1.1 1996/08/19 14:39:38 larsbj Exp
6 # This file contains examples for communicating to LyX via the
7 # LyXserver interface. It contains a panel of shortcuts to
8 # demonstrate how users can define their own macros. It also shows
9 # how new commands can be built into LyX using the 'notify' mechanism.
10 # =========================================================================
13 # --- 1. check cmdargs (pipename) -----------------------------------------
15 if { [llength $argv] > 0 } {
16 set inpipe [lrange $argv 0 0].in
17 set outpipe [lrange $argv 0 0].out
19 set inpipe $env(HOME)/.lyxpipe.in
20 set outpipe $env(HOME)/.lyxpipe.out
24 # --- 2. check if pipe is there; if not inform about lyxrc ----------------
28 puts "$inpipe or $outpipe not found"
29 puts "Either LyX is not running or it is not configured"
30 puts "to start the server. To start the server, insert"
31 puts " \serverpipe \"<pipename>\""
36 if { ![file exists $inpipe] } inform
37 if { "[file type $inpipe]" != "fifo" } inform
38 if { ![file exists $outpipe] } inform
39 if { "[file type $outpipe]" != "fifo" } inform
42 # --- 3. check if addinput is defined, if not; fake it --------------------
44 if { [info command addinput] == "" } {
45 puts "Warning: this wish does not have the addinput command"
46 puts " reading back information from LyX is disabled"
48 proc addinput {opt file cmd} {}
49 proc removeinput {file} {}
53 # --- 4. define addinput callbacks ----------------------------------------
57 proc outputhandler {fd} {
60 set type [lrange [split $outbuf :] 0 0]
61 if { "$type" == "NOTIFY" } {
62 set val [lrange [split $outbuf :] 1 1]
64 if { [catch { set lyxnotify($val) }] == 0 } {
70 set outfd [open $outpipe r]
74 addinput -read $outfd "outputhandler %F"
85 # --- 5. make windows -----------------------------------------------------
88 button .t.b1 -text "->" -command {lyxsend_form {\\rightarrow }}
89 button .t.b2 -text "<-" -command {lyxsend_form {\\leftarrow }}
90 button .t.b3 -text "-->" -command {lyxsend_form {\\longrightarrow }}
91 button .t.b4 -text "<--" -command {lyxsend_form {\\longleftarro w}}
92 button .t.b5 -text "<->" -command {lyxsend_form {\\leftrightarrow }}
93 button .t.b6 -text "<-->" -command {lyxsend_form {\\longleftrightarrow }}
94 button .t.b7 -text "..." -command {lyxsend_tex {\\ldots }}
95 label .t.t -text "TeX Arrows:"
97 pack .t.t -fill x -in .t
98 pack .t.b1 -fill x -in .t
99 pack .t.b2 -fill x -in .t
100 pack .t.b3 -fill x -in .t
101 pack .t.b4 -fill x -in .t
102 pack .t.b5 -fill x -in .t
103 pack .t.b6 -fill x -in .t
104 pack .t.b7 -fill x -in .t
106 label .func -text "New Functions"
107 button .xp -text "Transpose Chars" -command "lyx_transpose"
108 button .dw -text "Delete word" -command "lyx_delword"
109 button .sp -text "Save position" -command "lyx_savepos"
110 button .rp -text "Restore pos" -command "lyx_restorepos"
111 button .gi -text "Get Buffer info" -command "lyx_getinfo"
112 button .q -text "Quit" -command "exit"
123 # --- 6. functions --------------------------------------------------------
126 # Format for communication:
128 # client->LyX: "LYXCMD:<client>:<command>:<argument>" >$inpipe
129 # LyX->client (answer): "INFO:<client>:<command>:<data>" <$outpipe
130 # LyX->client (notify): "NOTIFY:<key-sequence>" <$outpipe
131 # (notifies are asynchroneous and are handled by 'outputhandler'
135 proc lyxsend_form {string} {
136 global inpipe outpipe outfd
138 # exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
140 # set chr [lrange [split $buf :] 3 3]
143 # if { "$chr" != "F" } {
144 # exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
146 # exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
147 # if { "$chr" != "F" } {
148 # exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
151 exec echo "LYXCMD:sampleclient:insert-inset-formula-latex-del:$string" >$inpipe
155 proc lyxsend_tex {string} {
156 global inpipe outpipe outfd
158 # exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
160 # set chr [lrange [split $buf :] 3 3]
163 # if { "$chr" != "L" } {
164 # exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
166 # exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
167 # if { "$chr" != "L" } {
168 # exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
170 exec echo "LYXCMD:sampleclient:insert-inset-latex-del:$string" >$inpipe
174 proc lyx_transpose {} {
175 global inpipe outpipe outfd
177 exec echo "LYXCMD:sampleclient:char-after:" >$inpipe
179 set chr [lrange [split $buf :] 3 3]
180 if { "$chr" == "EOF" } {out_on; return}
181 if { [string length $chr] > 1 } {
182 set chr [string range $chr 1 1]
184 exec echo "LYXCMD:sampleclient:delete-forward:" >$inpipe
185 exec echo "LYXCMD:sampleclient:left:" >$inpipe
186 if { "$chr" == "{ }" } {
187 exec echo "LYXCMD:sampleclient:insert-self: " >$inpipe
189 exec echo "LYXCMD:sampleclient:insert-self:$chr" >$inpipe
194 set lyxnotify(C-t) lyx_transpose
196 proc lyx_delword {} {
197 global inpipe outpipe outfd
198 exec echo "LYXCMD:sampleclient:word-right-select:" >$inpipe
199 exec echo "LYXCMD:sampleclient:left-select:" >$inpipe
200 # !!! should check for end of line here:
201 # if ( char-after == char-in-word ) right;
202 exec echo "LYXCMD:sampleclient:delete-forward:" >$inpipe
205 set lyxnotify(M-d) lyx_delword
210 proc lyx_savepos {} {
211 global posx posy inpipe outpipe outfd
214 exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
216 set arg [lrange [split $buf :] 3 3]
217 # TCL is SO pathetic!
218 set arg [string range $arg 1 [expr [string length $arg]-2]]
219 set posx [lrange $arg 0 0]
220 set posy [lrange $arg 1 1]
224 set "lyxnotify({C-x slash})" lyx_savepos
226 proc lyx_restorepos {} {
227 global posx posy inpipe
228 if { "$posx" == "" } return
229 exec echo "LYXCMD:sampleclient:set-xy:$posx $posy" >$inpipe
230 exec echo "LYXCMD:sampleclient:recenter:" >$inpipe
233 set "lyxnotify({C-x j})" lyx_restorepos
235 proc lyx_getinfo {} {
236 global inpipe outpipe outfd
238 exec echo "LYXCMD:sampleclient:get-name:" >$inpipe
240 set fname [lrange [split $buf :] 3 3]
241 exec echo "LYXCMD:sampleclient:get-layout:" >$inpipe
243 set layout [lrange [split $buf :] 3 3]
244 exec echo "LYXCMD:sampleclient:get-font:" >$inpipe
246 set fnt [lrange [split $buf :] 3 3]
247 exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
249 set ltx [lrange [split $buf :] 3 3]
250 exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
252 set arg [lrange [split $buf :] 3 3]
253 set arg [string range $arg 1 [expr [string length $arg]-2]]
255 puts "---------------"
256 puts "Buffer: $fname"
257 puts "Current layout: $layout"
258 puts "Font flag: $fnt"
259 puts "Latex flag: $ltx"
260 puts "Position: $arg"
266 # === End of file: sampleclient.tcl =======================================