blob: af26546a7744834b7257a73ff811996368fb9128 (
plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
## (auto-install)
##
## This is file `dtxload.tcl',
## generated with the docstrip utility.
##
## The original source files were:
##
## dtxload.dtx (with options: `tcl')
##
## This file may be distributed and/or modified under the conditions
## of the LaTeX Project Public License, either version 1.2 of this
## license or (at your option) any later version. The latest version
## of this license is in
## http://www.latex-project.org/lppl.txt
## and version 1.2 or later is part of all distributions of LaTeX
## version 1999/12/01 or later.
##
## This file may only be distributed together with a copy of the source
## file from which it was generated. You may distribute that source file
## without this generated file.
##
alpha::feature dtxload 1.1 {TeX} {
# When set, the Load Tcl Code command will be disabled in
# TeX files whose suffix is not dtx.
newPref flag onlyLoadDtxFiles 1 TeX
# When set, the Load Tcl Code command will only extract code
# from code lines in tcl and tcl* environments.
newPref flag onlyLoadTclEnvs 1 TeX
# This is the key binding for dtxload's Load Tcl Code command.
newPref binding loadTclCode <O/L TeX "" dtxload::load TeX
} "" "" uninstall {this-file} \
maintainer {"Lars Hellstr\x9am" Lars.Hellstrom@math.umu.se} \
help {The dtxload package implements a command for loading
Tcl code that is embedded in a .dtx file without docstripping
the file first. For more help, LaTeX the file dtxload.dtx.}
namespace eval dtxload {}
proc dtxload::eval_guard {e} {
global dtxload::known_expressions
if {![info exists dtxload::known_expressions($e)]} then {
set dtxload::known_expressions($e)\
[expr {"yes" ==\
"[askyesno "Should <$e> modules be included?"]"}]
}
set dtxload::known_expressions($e)
}
proc dtxload::push_module {e p} {
global dtxload::module_stack
lappend dtxload::module_stack [list $e $p]
}
proc dtxload::pop_module {e p} {
global dtxload::module_stack dtxload::module_included\
dtxload::next_module_idx
set len [llength ${dtxload::module_stack}]
if {$len==0} then {return}
set L [lindex ${dtxload::module_stack} [expr {$len-1}]]
if {[lindex $L 0] != $e} then {
switch [buttonAlert "Module nesting error: <*[lindex $L 0]>\
module ended by </$e>. For which guards should the positions\
be pushed?" None Start End Both]\
{
Start {dtxload::push_bookmarks [lindex $L 1]}
End {dtxload::push_bookmarks $p}
Both {dtxload::push_bookmarks [lindex $L 1] $p}
}
}
set dtxload::module_stack\
[lreplace ${dtxload::module_stack} end end]
if {$len>=${dtxload::next_module_idx}} then {
incr dtxload::next_module_idx -1
}
if {[llength $L]>2} then {
set dtxload::module_included [lindex $L 2]
}
}
proc dtxload::push_bookmarks {args} {
global markStack markName
foreach pos $args {
set name mark$markName
incr markName
createTMark $name $pos
set fileName [win::Current]
set markStack [linsert $markStack 0 [list $fileName $name]]
}
}
proc dtxload::update_included {} {
global dtxload::module_included dtxload::module_stack\
dtxload::next_module_idx
while {${dtxload::module_included} &&\
${dtxload::next_module_idx}<[llength ${dtxload::module_stack}]}\
{
set L [lindex ${dtxload::module_stack} ${dtxload::next_module_idx}]
lappend L ${dtxload::module_included}
set dtxload::module_stack [lreplace ${dtxload::module_stack}\
${dtxload::next_module_idx} ${dtxload::next_module_idx} $L]
incr dtxload::next_module_idx
set dtxload::module_included [dtxload::eval_guard [lindex $L 0]]
}
}
proc dtxload::extract_line {startpos endpos} {
global dtxload::in_tcl_env onlyLoadTclEnvs dtxload::module_included
for {set pos $startpos} {[pos::compare {$pos<$endpos}]}\
{set pos $pos2} {
set pos2 [nextLineStart $pos]
if {$pos2>$endpos} then {set pos2 $endpos}
set line [getText $pos $pos2]
switch -regexp -- $line {
{^%<<} {
if {!$onlyLoadTclEnvs || ${dtxload::in_tcl_env}} then {
dtxload::update_included
if {${dtxload::module_included}} then {
alertnote "docstrip verbatim mode guard line\
encountered. Verbatim mode extraction is\
currently not supported, so I stop here."
goto $pos
return [list $endpos]
}
}
set endtag %[string trimright [string trimright\
[string range $line 3 end] \n\r] \ ]
while {[string trimright [string trimright $line \n\r] \ ]\
!= $endtag} {
set pos $pos2
if {[pos::compare {$pos>=$endpos}]}\
then {return [list $pos]}
set pos2 [nextLineStart $pos]
if {$pos2>$endpos} then {set pos2 $endpos}
set line [getText $pos $pos2]
}
}
{^%<[*/]} {
if {![regexp {^%<(\*|/)([^>]+)>} $line foo modifier\
expression]}\
then {
if {"[askyesno "Malformed guard line \"$line\"\
encountered. Push position?"]"=="yes"}\
then {dtxload::push_bookmarks $pos}
} elseif {$modifier=="*"} then {
dtxload::push_module $expression $pos
} else {
dtxload::pop_module $expression $pos
}
}
{^%<} {
if {![regexp {^%<(-|\+|)([^>]+)>(.*)$} $line\
foo modifier expression code]}\
then {
if {"[askyesno "Malformed guard line \"$line\"\
encountered. Push position?"]"=="yes"}\
then {dtxload::push_bookmarks $pos}
} elseif {!$onlyLoadTclEnvs || ${dtxload::in_tcl_env}} then {
dtxload::update_included
if {${dtxload::module_included}} then {
if {[dtxload::eval_guard $expression] !=\
($modifier=="-")}\
then {return [list $pos2 $code $pos]}
}
}
}
{^%} {
while {[regexp -indices {\\(begin|end) *{tcl\*?}} $line\
match type]} {
set dtxload::in_tcl_env\
[expr {"[eval string range \$line $type]"=="begin"}]
set line [string range $line\
[expr {[lindex $match 1]+1}] end]
}
}
default {
if {!$onlyLoadTclEnvs || ${dtxload::in_tcl_env}} then {
dtxload::update_included
if {${dtxload::module_included}}\
then {return [list $pos2 $line $pos]}
}
}
}
}
list $endpos
}
proc dtxload::evaluate {t} {
global errorInfo errorCode evaluateRemotely tcl_platform
if {!$evaluateRemotely} then {
if {[set code [catch {uplevel \#0 $t} msg]] == 1} then {
set L [split $errorInfo \n]
set errorInfo [join [lrange $L 0 [expr {[llength $L] - 4}]] \n]
}
list $code $msg $errorInfo $errorCode
} elseif {$tcl_platform(platform) == "macintosh"} then {
global tclshSig
app::ensureRunning $tclshSig
list [catch {
aebuild::result -t 30000 '${tclshSig}' misc dosc ----\
[aebuild::TEXT $t]
} res]\
$res
} else {
global tclshInterp
if {![info exists tclshInterp]} {
if {[catch {tcltk::findTclshInterp}]} {
return [list 1 "No shell selected"]
}
}
if {$tcl_platform(platform) == "windows"} {
if {[dde services Tk $tclshInterp] == ""} {
alertnote "The remote shell has died, please select a new one."
unset tclshInterp
return [dtxload::evaluate $t]
}
list [catch {dde execute Tk $tclshInterp $t} res] $res
} else {
list [catch {send $tclshInterp $t} res] $res
}
}
}
proc dtxload::message_log {script} {
set res [dtxload::evaluate $script]
if {[lindex $res 0]==1} then {
message "Tcl eval error: [lindex $res 1]"
expr 1
} else {
if {[string length [lindex $res 1]] == 0} then {
message "Tcl eval OK."
} else {
message "Tcl eval OK: [lindex $res 1]"
}
expr 0
}
}
if {[info exists dtxload::log_window_name]} then {
unset dtxload::log_window_name
}
proc dtxload::window_log {script} {
upvar #0 dtxload::log_window_name win
global win::Modes Shel::startPrompt Shel::endPrompt
if {[set win::Modes($win)] == "Shel"} then {
global Shel::histnum
regsub -all \n [string trimright $script] " ; " text
history add $text
set Shel::histnum [history nextid]
}
set pos [maxPos -w $win]
select -w $win $pos $pos
set dtxprompt "${Shel::startPrompt}dtxload${Shel::endPrompt} "
if {[set win::Modes($win)] != "Shel"} then {
insertText -w $win \r$dtxprompt
} else {
set stdprompt [Alpha::Prompt]
set pos2 [pos::math $pos - [string length $stdprompt]]
if {[getText -w $win $pos2 $pos] != $stdprompt} then {
insertText -w $win \r$dtxprompt
} else {
replaceText -w $win $pos2 $pos $dtxprompt
refresh
}
}
regsub -all \n [string trimright $script] \r$dtxprompt text
insertText -w $win "$text\r"
set res [dtxload::evaluate $script]
if {[lindex $res 0]==1} then {
insertText -w $win "Error: [lindex $res 1]\r"
if {[llength $res]>2} then {
global errorInfo
set errorInfo [lindex $res 2]
}
message "Tcl eval error"
} else {
if {[string length [lindex $res 1]]>0}\
then {insertText -w $win "[lindex $res 1]\r"}
message "Tcl eval OK"
}
if {[set win::Modes($win)] == "Shel"}\
then {insertText -w $win [Alpha::Prompt]}
expr {[lindex $res 0]==1}
}
proc dtxload::load {} {
global onlyLoadDtxFiles
if {$onlyLoadDtxFiles && ![string match *.dtx [win::CurrentTail]]}\
then {
message "Not a .dtx file."
return
}
set startpos [getPos]
set endpos [selEnd]
if {[pos::compare {$startpos == $endpos}]} then {
set startpos [minPos]
set endpos [maxPos]
}
global dtxload::log_window_name
if {
[info exists dtxload::log_window_name] &&
[lsearch -exact [winNames] ${dtxload::log_window_name}]!=-1
} then {set log dtxload::window_log}\
else {set log dtxload::message_log}
global dtxload::in_tcl_env dtxload::module_stack\
dtxload::next_module_idx dtxload::module_included
set dtxload::in_tcl_env\
[expr {[getText $startpos [pos::math {$startpos+1}]] != "%"}]
set dtxload::module_stack {}
set dtxload::next_module_idx 0
set dtxload::module_included 1
set script ""
set escaped 0
set spos none
while {[pos::compare $startpos < $endpos]} {
set extract [dtxload::extract_line $startpos $endpos]
if {[llength $extract]>1} then {
if {[string length $script]==0} then {
set spos [lindex $extract 2]
}
set line [lindex $extract 1]
if {$escaped} then {set line [string trimleft $line]}
set escaped [regsub -all\
"((^|\[^\\\\\])(\\\\\\\\)*)\\\\(\r|\n|\r\n)$" $line {\1 } line]
if {!$escaped} then {regsub "\r\n?\$" $line \n line}
append script $line
if {!$escaped && [info complete $script]} then {
if {[$log $script]} then {
select $spos $endpos
markHilite
return
} else {
set script ""
}
}
} elseif {$spos=="none"} then {
message "No extractable code found."
}
set startpos [lindex $extract 0]
}
if {[string length $script]>0 && [$log $script]} then {
select $spos $endpos
markHilite
} else {
global dtxload::known_expressions
if {[info exists dtxload::known_expressions]}\
then {unset dtxload::known_expressions}
}
}
proc dtxload::logdest {type {win "dtxload log"}} {
global dtxload::log_window_name
switch -exact -- $type {
status {
if {[info exists dtxload::log_window_name]}\
then {unset dtxload::log_window_name}
}
here {set dtxload::log_window_name [win::CurrentTail]}
window {
if {[lsearch -exact [winNames] $win] == -1} then {
new -n $win -shell 1 -text "This is a dtxload log window.\r"
}
set dtxload::log_window_name $win
}
shell {
Shel::start "Alpha" $win\
"This is a dtxload log window (and Alpha Tcl shell).\r"
set dtxload::log_window_name $win
}
}
}
##
##
## End of file `dtxload.tcl'.
|