1 #---------------------------------------------------------------
2 # Text regexp manipulation and text file import
4 # The core routine text_regexp takes either the selected
5 # items or all items, searches them for text labels, and
6 # makes the text replacement according to the regular
7 # expression "searchstr" and the replacement text "replacestr".
9 #---------------------------------------------------------------
11 # Declare the package by setting variable XCOps(module,text)
12 set XCOps
(module
,text) 1
14 proc text_regexp
{searchstr replacestr
{mode all
}} {
15 switch -glob -- $mode {
16 select
* {set partlist
[select get
]; set resel
1}
17 default {deselect selected
; set partlist
[object parts
]; set resel
0}
22 if {[element
$j type
] == "Label"} {
23 set labelparts
[lindex [label $j list] 0]
26 foreach k
$labelparts {
27 if {[lindex $k 0] == "Text"} {
28 set tstring
[lindex $k 1]
29 set newt
[regsub [list $searchstr] $tstring $replacestr]
30 set newk
[lreplace $k 1 1 $newt]
31 lappend newparts
$newk
32 if {"$newt" != "$tstring"} {set modified
1}
38 label $j replace
$newparts
42 if {$resel == 1} {catch {select
$partlist}}
48 #-----------------------------------------------------------------
49 # Procedures to help with label generation (label autoincrement)
50 #-----------------------------------------------------------------
52 # autoincrement the first number found in the text of the indicated
53 # label(s). Use argument "amount" to decrement, or increment by 10,
56 # example: xcircuit::textincrement selected
58 proc xcircuit
::textincrement {mode
{amount
1} {position first
}} {
59 switch -glob -- $mode {
60 select
* {set handle
[select get
]; set resel
1}
61 default {deselect selected
; set handle
[object parts
]; set resel
0}
67 if {[element
$h type
] == "Label"} {
68 set tlist
[join [label $h list]]
69 set tlen
[llength $tlist]
70 for {set i
0} {$i < $tlen} {incr i
} {
71 set t
[lindex $tlist $i]
74 set ltext
[lindex $t 1]
76 if {"$position" == "last"} {
77 set result
[regexp -indices -all {([+-]?
)[0]*[[:digit
:]]+} \
79 set idx
[lindex $bounds 0]
81 regexp -start $idx {([+-]?
)([0]*)([[:digit
:]]+)} $ltext \
85 set result
[regexp {([+-]?
)([0]*)([[:digit
:]]+)} $ltext \
89 if {$result > 0 && $num != ""} {
93 set num
[expr abs
($num)]
96 if {$num == 0 && "$pre" == "-"} {set pre
""}
97 if {"$position" == "last"} {
98 regsub -start $idx {[+-]?
[0]*[[:digit
:]]+} $ltext $pre$zer$num ltext
100 regsub {[+-]?
[0]*[[:digit
:]]+} $ltext $pre$zer$num ltext
102 set t
[lreplace $t 1 1 $ltext]
103 set tlist
[lreplace $tlist $i $i $t]
104 label $h replace
$tlist
111 if {$resel == 1} {catch {select
$handle}}
117 proc xcircuit
::autoincr {{value
1} {position first
}} {
120 if {$e != "text" && $e != "etext" && $e != "epoly"} {
127 xcircuit
::textincrement selected
$value $position
140 error "no auto-incr in text mode"
144 #-----------------------------------------------------------------
145 # Create a popup window for text modification
146 #-----------------------------------------------------------------
148 proc xcircuit
::maketextmod {} {
149 toplevel .textmod
-bg beige
152 frame .textmod.title
-bg beige
153 label .textmod.title.field
-text "Label text modification for:" -bg beige
154 menubutton .textmod.title.type
-text "selected text" -bg beige
\
155 -menu .textmod.title.type.seltype
156 button .textmod.title.dbut
-text "Dismiss" -bg beige
-command \
157 {wm withdraw .textmod
}
159 menu .textmod.title.type.seltype
-tearoff 0
160 .textmod.title.type.seltype add command
-label "selected text" -command \
161 {.textmod.title.type configure
-text "selected text"}
162 .textmod.title.type.seltype add command
-label "all text in page" -command \
163 {.textmod.title.type configure
-text "all text in page"}
166 pack .textmod.title
-side top
-fill x
167 pack .textmod.title.field
-side left
-padx 10
168 pack .textmod.title.type
-side left
-padx 10
169 pack .textmod.title.dbut
-side right
-ipadx 10
171 labelframe .textmod.replace
-text "Search and Replace" -bg beige
172 pack .textmod.replace
-side top
-fill x
-expand true
-pady 10
174 label .textmod.replace.title1
-text "Search for:" -bg beige
175 entry .textmod.replace.original
-bg white
176 label .textmod.replace.title2
-text "Replace with:" -bg beige
177 entry .textmod.replace.new
-bg white
178 button .textmod.replace.apply
-text "Apply" -bg beige
-command \
179 {text_regexp
[.textmod.replace.original get
] \
180 [.textmod.replace.new get
] [.textmod.title.type cget
-text]}
182 pack .textmod.replace.title1
-side left
183 pack .textmod.replace.original
-side left
184 pack .textmod.replace.title2
-side left
185 pack .textmod.replace.new
-side left
186 pack .textmod.replace.apply
-side right
-ipadx 10 -padx 10
188 # Numeric Increment/Decrement
190 labelframe .textmod.numeric
-text "Embedded Numbers" -bg beige
191 pack .textmod.numeric
-side top
-fill x
-expand true
193 button .textmod.numeric.
incr -text "Increment" -bg beige
-command \
194 {xcircuit
::textincrement [.textmod.title.type cget
-text] \
195 [.textmod.numeric.amount get
] [.textmod.numeric.pos cget
-text]}
196 button .textmod.numeric.decr
-text "Decrement" -bg beige
-command \
197 {xcircuit
::textincrement [.textmod.title.type cget
-text] \
198 [expr -[.textmod.numeric.amount get
]] [.textmod.numeric.pos cget
-text]}
199 label .textmod.numeric.title1
-text "Amount: " -bg beige
200 entry .textmod.numeric.amount
-bg white
202 menubutton .textmod.numeric.pos
-text "first" -bg beige
\
203 -menu .textmod.numeric.pos.posmenu
204 menu .textmod.numeric.pos.posmenu
-tearoff 0
205 .textmod.numeric.pos.posmenu add command
-label "first" -command \
206 {.textmod.numeric.pos configure
-text "first"}
207 .textmod.numeric.pos.posmenu add command
-label "last" -command \
208 {.textmod.numeric.pos configure
-text "last"}
210 .textmod.numeric.amount insert
0 "1"
212 pack .textmod.numeric.
incr -side left
-ipadx 10 -padx 10
213 pack .textmod.numeric.decr
-side left
-ipadx 10 -padx 10
214 pack .textmod.numeric.title1
-side left
215 pack .textmod.numeric.amount
-side left
216 pack .textmod.numeric.pos
-side left
219 proc xcircuit
::textmod {} {
221 if {[catch {wm state .textmod
}]} {
222 xcircuit
::maketextmod
224 set wstate
[xcircuit
::getinitstate .textmod
]
228 if {"$wstate" != "normal"} {
229 wm deiconify .textmod
230 xcircuit
::centerwin .textmod
235 #--------------------------------------------------------------
236 # This procedure reads an entire file into an xcircuit string.
237 #--------------------------------------------------------------
239 proc xcircuit
::importtext {filename} {
240 set ffile
[open $filename]
242 if {[gets $ffile line
] >= 0} {
243 set line
[string map
{\t " "} $line]
244 lappend tlist
[list Text
$line]
245 while {[gets $ffile line
] >= 0} {
246 set line
[string map
{\t " "} $line]
248 lappend tlist
[list Text
$line]
251 label make normal
$tlist "0 0"
256 #-----------------------------------------------------------------
258 proc xcircuit
::promptimporttext {} {
259 .filelist.bbar.okay configure
-command \
260 {xcircuit
::importtext [.filelist.textent.txt get
]; \
261 wm withdraw .filelist
}
262 .filelist.listwin.win configure
-data ""
263 .filelist.textent.title.field configure
-text "Select text file:"
264 .filelist.textent.txt delete
0 end
265 xcircuit
::popupfilelist
268 #-----------------------------------------------------------------