File: sampleclient.tcl

package info (click to toggle)
lyx 2.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 96,552 kB
  • sloc: cpp: 388,556; python: 19,985; ansic: 9,725; sh: 5,696; makefile: 3,907; pascal: 1,388; objc: 985; perl: 319; yacc: 289; tcl: 163; xml: 23; sed: 16
file content (266 lines) | stat: -rw-r--r-- 7,805 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# this one avoids absolute path names: \
exec wish -f $0 "$@"
# =========================================================================
#   File: sampleclient.tcl, chb, Sun 05.11.1995 (19:24)
#   sampleclient.tcl,v 1.1.1.1 1996/08/19 14:39:38 larsbj Exp
#   This file contains examples for communicating to LyX via the
#   LyXserver interface. It contains a panel of shortcuts to
#   demonstrate how users can define their own macros. It also shows
#   how new commands can be built into LyX using the 'notify' mechanism.
# =========================================================================


# --- 1. check cmdargs (pipename) -----------------------------------------

if { [llength $argv] > 0 } {
    set inpipe  [lrange $argv 0 0].in
    set outpipe [lrange $argv 0 0].out
} else {
    set inpipe  $env(HOME)/.lyxpipe.in
    set outpipe $env(HOME)/.lyxpipe.out
}


# --- 2. check if pipe is there; if not inform about lyxrc ----------------

proc inform {} {
    global inpipe outpipe
    puts "$inpipe or $outpipe not found"
    puts "Either LyX is not running or it is not configured"
    puts "to start the server. To start the server, insert"
    puts "     \serverpipe \"<pipename>\""
    puts "in your .lyxrc"
    exit
}

if { ![file exists $inpipe] }           inform
if { "[file type $inpipe]" != "fifo" }  inform
if { ![file exists $outpipe] }          inform
if { "[file type $outpipe]" != "fifo" } inform


# --- 3. check if addinput is defined, if not; fake it --------------------

if { [info command addinput] == "" } {
    puts "Warning: this wish does not have the addinput command"
    puts "         reading back information from LyX is disabled"

    proc addinput {opt file cmd} {}
    proc removeinput {file} {}
}


# --- 4. define addinput callbacks ----------------------------------------

set outbuf ""

proc outputhandler {fd} {
    global lyxnotify
    gets $fd outbuf
    set type [lrange [split $outbuf :] 0 0]
    if { "$type" == "NOTIFY" } {
	set val [lrange [split $outbuf :] 1 1]
	# puts "$val"
	if { [catch { set lyxnotify($val) }] == 0 } {
	    eval $lyxnotify($val)
	}
    }
}

set outfd [open $outpipe r]

proc out_on {} {
    global outfd
    addinput -read $outfd "outputhandler %F"
}

proc out_off {} {
    global outfd
    removeinput $outfd
}

out_on


# --- 5. make windows -----------------------------------------------------

toplevel .t
button .t.b1 -text "->"   -command {lyxsend_form {\\rightarrow }}
button .t.b2 -text "<-"   -command {lyxsend_form {\\leftarrow }}
button .t.b3 -text "-->"  -command {lyxsend_form {\\longrightarrow }}
button .t.b4 -text "<--"  -command {lyxsend_form {\\longleftarro w}}
button .t.b5 -text "<->"  -command {lyxsend_form {\\leftrightarrow }}
button .t.b6 -text "<-->" -command {lyxsend_form {\\longleftrightarrow }}
button .t.b7 -text "..."  -command {lyxsend_tex  {\\ldots }}
label  .t.t  -text "TeX Arrows:"

pack .t.t  -fill x -in .t
pack .t.b1 -fill x -in .t
pack .t.b2 -fill x -in .t
pack .t.b3 -fill x -in .t
pack .t.b4 -fill x -in .t
pack .t.b5 -fill x -in .t
pack .t.b6 -fill x -in .t
pack .t.b7 -fill x -in .t

label  .func -text "New Functions"
button .xp   -text "Transpose Chars" -command "lyx_transpose"
button .dw   -text "Delete word"     -command "lyx_delword"
button .sp   -text "Save position"   -command "lyx_savepos"
button .rp   -text "Restore pos"     -command "lyx_restorepos"
button .gi   -text "Get Buffer info" -command "lyx_getinfo"
button .q    -text "Quit"            -command "exit"

pack .func -fill x
pack .xp   -fill x
pack .dw   -fill x
pack .sp   -fill x
pack .rp   -fill x
pack .gi   -fill x
pack .q    -fill x


# --- 6. functions --------------------------------------------------------

# ---
#  Format for communication:
#
#    client->LyX:          "LYXCMD:<client>:<command>:<argument>" >$inpipe
#    LyX->client (answer): "INFO:<client>:<command>:<data>"       <$outpipe
#    LyX->client (notify): "NOTIFY:<key-sequence>"                <$outpipe
#    (notifies are asynchroneous and are handled by 'outputhandler'
#     above)
# ---

proc lyxsend_form {string} {
    global inpipe outpipe outfd
#    out_off
#    exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
#    gets $outfd buf
#    set chr [lrange [split $buf :] 3 3]
#    out_on
#
#    if { "$chr" != "F" } {
#	exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
#    }
#    exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
#    if { "$chr" != "F" } {
#	exec echo "LYXCMD:sampleclient:set-formula:" >$inpipe
#    }

    exec echo "LYXCMD:sampleclient:insert-inset-formula-latex-del:$string" >$inpipe

}

proc lyxsend_tex {string} {
    global inpipe outpipe outfd
#    out_off
#    exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
#    gets $outfd buf
#    set chr [lrange [split $buf :] 3 3]
#    out_on
#
#   if { "$chr" != "L" } {
#	exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
#    }
#    exec echo "LYXCMD:sampleclient:insert-self:$string" >$inpipe
#    if { "$chr" != "L" } {
#	exec echo "LYXCMD:sampleclient:set-tex:" >$inpipe
#    }
    exec echo "LYXCMD:sampleclient:insert-inset-latex-del:$string" >$inpipe

}

proc lyx_transpose {} {
    global inpipe outpipe outfd
    out_off
    exec echo "LYXCMD:sampleclient:char-after:" >$inpipe
    gets $outfd buf
    set chr [lrange [split $buf :] 3 3]
    if { "$chr" == "EOF" } {out_on; return}
    if { [string length $chr] > 1 } {
	set chr [string range $chr 1 1]
    }
    exec echo "LYXCMD:sampleclient:delete-forward:" >$inpipe
    exec echo "LYXCMD:sampleclient:left:" >$inpipe
    if { "$chr" == "{ }" } {
	exec echo "LYXCMD:sampleclient:insert-self: " >$inpipe
    } else {
	exec echo "LYXCMD:sampleclient:insert-self:$chr" >$inpipe
    }
    out_on
}

set lyxnotify(C-t) lyx_transpose

proc lyx_delword {} {
    global inpipe outpipe outfd
    exec echo "LYXCMD:sampleclient:word-right-select:" >$inpipe
    exec echo "LYXCMD:sampleclient:left-select:" >$inpipe
    # !!! should check for end of line here:
    #     if ( char-after == char-in-word ) right;
    exec echo "LYXCMD:sampleclient:delete-forward:" >$inpipe
}

set lyxnotify(M-d) lyx_delword

set posx ""
set posy ""

proc lyx_savepos {} {
    global posx posy inpipe outpipe outfd

    out_off
    exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
    gets $outfd buf
    set arg [lrange [split $buf :] 3 3]
    # TCL is SO pathetic!
    set arg [string range $arg 1 [expr [string length $arg]-2]]
    set posx [lrange $arg 0 0]
    set posy [lrange $arg 1 1]
    out_on
}

set "lyxnotify({C-x slash})" lyx_savepos

proc lyx_restorepos {} {
    global posx posy inpipe
    if { "$posx" == "" } return
    exec echo "LYXCMD:sampleclient:set-xy:$posx $posy" >$inpipe
    exec echo "LYXCMD:sampleclient:recenter:" >$inpipe
}

set "lyxnotify({C-x j})" lyx_restorepos

proc lyx_getinfo {} {
    global inpipe outpipe outfd
    out_off
    exec echo "LYXCMD:sampleclient:get-name:" >$inpipe
    gets $outfd buf
    set fname [lrange [split $buf :] 3 3]
    exec echo "LYXCMD:sampleclient:get-layout:" >$inpipe
    gets $outfd buf
    set layout [lrange [split $buf :] 3 3]
    exec echo "LYXCMD:sampleclient:get-font:" >$inpipe
    gets $outfd buf
    set fnt [lrange [split $buf :] 3 3]
    exec echo "LYXCMD:sampleclient:get-latex:" >$inpipe
    gets $outfd buf
    set ltx [lrange [split $buf :] 3 3]
    exec echo "LYXCMD:sampleclient:get-xy:" >$inpipe
    gets $outfd buf
    set arg [lrange [split $buf :] 3 3]
    set arg [string range $arg 1 [expr [string length $arg]-2]]

    puts "---------------"
    puts "Buffer:         $fname"
    puts "Current layout: $layout"
    puts "Font flag:      $fnt"
    puts "Latex flag:     $ltx"
    puts "Position:       $arg"

    out_on
}


# === End of file: sampleclient.tcl =======================================