]> git.lyx.org Git - lyx.git/blob - development/lyxserver/sampleclient.tcl
Customization: correct some color names.
[lyx.git] / development / lyxserver / sampleclient.tcl
1 # this one avoids absolute path names: \
2 exec wish -f $0 "$@"
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 # =========================================================================
11
12
13 # --- 1. check cmdargs (pipename) -----------------------------------------
14
15 if { [llength $argv] > 0 } {
16     set inpipe  [lrange $argv 0 0].in
17     set outpipe [lrange $argv 0 0].out
18 } else {
19     set inpipe  $env(HOME)/.lyxpipe.in
20     set outpipe $env(HOME)/.lyxpipe.out
21 }
22
23
24 # --- 2. check if pipe is there; if not inform about lyxrc ----------------
25
26 proc inform {} {
27     global inpipe outpipe
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>\""
32     puts "in your .lyxrc"
33     exit
34 }
35
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
40
41
42 # --- 3. check if addinput is defined, if not; fake it --------------------
43
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"
47
48     proc addinput {opt file cmd} {}
49     proc removeinput {file} {}
50 }
51
52
53 # --- 4. define addinput callbacks ----------------------------------------
54
55 set outbuf ""
56
57 proc outputhandler {fd} {
58     global lyxnotify
59     gets $fd outbuf
60     set type [lrange [split $outbuf :] 0 0]
61     if { "$type" == "NOTIFY" } {
62         set val [lrange [split $outbuf :] 1 1]
63         # puts "$val"
64         if { [catch { set lyxnotify($val) }] == 0 } {
65             eval $lyxnotify($val)
66         }
67     }
68 }
69
70 set outfd [open $outpipe r]
71
72 proc out_on {} {
73     global outfd
74     addinput -read $outfd "outputhandler %F"
75 }
76
77 proc out_off {} {
78     global outfd
79     removeinput $outfd
80 }
81
82 out_on
83
84
85 # --- 5. make windows -----------------------------------------------------
86
87 toplevel .t
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:"
96
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
105
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"
113
114 pack .func -fill x
115 pack .xp   -fill x
116 pack .dw   -fill x
117 pack .sp   -fill x
118 pack .rp   -fill x
119 pack .gi   -fill x
120 pack .q    -fill x
121
122
123 # --- 6. functions --------------------------------------------------------
124
125 # ---
126 #  Format for communication:
127 #
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'
132 #     above)
133 # ---
134
135 proc lyxsend_form {string} {
136     global inpipe outpipe outfd
137 #    out_off
138 #    exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
139 #    gets $outfd buf
140 #    set chr [lrange [split $buf :] 3 3]
141 #    out_on
142 #
143 #    if { "$chr" != "F" } {
144 #       exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
145 #    }
146 #    exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
147 #    if { "$chr" != "F" } {
148 #       exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
149 #    }
150
151     exec echo "LYXCMD:sampleclient:insert-inset-formula-latex-del:$string" >$inpipe
152
153 }
154
155 proc lyxsend_tex {string} {
156     global inpipe outpipe outfd
157 #    out_off
158 #    exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
159 #    gets $outfd buf
160 #    set chr [lrange [split $buf :] 3 3]
161 #    out_on
162 #
163 #   if { "$chr" != "L" } {
164 #       exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
165 #    }
166 #    exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
167 #    if { "$chr" != "L" } {
168 #       exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
169 #    }
170     exec echo "LYXCMD:sampleclient:insert-inset-latex-del:$string" >$inpipe
171
172 }
173
174 proc lyx_transpose {} {
175     global inpipe outpipe outfd
176     out_off
177     exec echo "LYXCMD:sampleclient:char-after:" >$inpipe
178     gets $outfd buf
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]
183     }
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
188     } else {
189         exec echo "LYXCMD:sampleclient:insert-self:$chr" >$inpipe
190     }
191     out_on
192 }
193
194 set lyxnotify(C-t) lyx_transpose
195
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
203 }
204
205 set lyxnotify(M-d) lyx_delword
206
207 set posx ""
208 set posy ""
209
210 proc lyx_savepos {} {
211     global posx posy inpipe outpipe outfd
212
213     out_off
214     exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
215     gets $outfd buf
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]
221     out_on
222 }
223
224 set "lyxnotify({C-x slash})" lyx_savepos
225
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
231 }
232
233 set "lyxnotify({C-x j})" lyx_restorepos
234
235 proc lyx_getinfo {} {
236     global inpipe outpipe outfd
237     out_off
238     exec echo "LYXCMD:sampleclient:get-name:" >$inpipe
239     gets $outfd buf
240     set fname [lrange [split $buf :] 3 3]
241     exec echo "LYXCMD:sampleclient:get-layout:" >$inpipe
242     gets $outfd buf
243     set layout [lrange [split $buf :] 3 3]
244     exec echo "LYXCMD:sampleclient:get-font:" >$inpipe
245     gets $outfd buf
246     set fnt [lrange [split $buf :] 3 3]
247     exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
248     gets $outfd buf
249     set ltx [lrange [split $buf :] 3 3]
250     exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
251     gets $outfd buf
252     set arg [lrange [split $buf :] 3 3]
253     set arg [string range $arg 1 [expr [string length $arg]-2]]
254
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"
261
262     out_on
263 }
264
265
266 # === End of file: sampleclient.tcl =======================================