File: Enscript.tcl

package info (click to toggle)
tkchooser 2.0652-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 432 kB
  • ctags: 272
  • sloc: tcl: 3,344; makefile: 76; sh: 13
file content (496 lines) | stat: -rw-r--r-- 14,887 bytes parent folder | download | duplicates (4)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
## This is a plugin to provide some kind of printing
## pre-filtering for printing utilities using GNU Enscript.
## It builds an enscript postscript formatter config file
## and relies on the printfilter plugin to choose
## the printfilter that calls enscript.
##
## This plugin uses a comment structure inside enscript config
## files to parse everything for the GUI
##
## Ethan Gold <etgold@cs.vassar.edu> 3/27/98
##

if {[debug]} { puts "loading Enscript plugin..." }

## register the plugin with the main chooser
register_plugin misc enscript Enscript

#### required functions to return plugin information
#### use these instead of globals
proc enscript.geticon {} { return "enscript.ppm" }
proc enscript.getpubname {} { return "Enscript" }
proc enscript.getprotocol {} { return "misc" }
proc enscript.getfilterfile {} { return ".enscriptplug.filter" }
proc enscript.getconfigfile {} { return ".enscriptplug.config" }

####### required functions that actually do stuff (maybe) #########

## start function
proc enscript.start {} {
    ## read in config file and set options as global
    if {[debug]} {puts "enscript: starting enscript plugin"}
    set pluglabel [plug_list_label]
    $pluglabel configure -text "Categories:"
    update
    enscript.widgets "start"
    enscript.readcfg
    enscript.showsections
}

## stop function
proc enscript.stop {} {
    ## global data area will automatically be cleared
    if {[debug]} {puts "enscript: stopping enscript plugin"}
    enscript.widgets "stop"
}

## doubleclick callback
proc enscript.doubleclick {} {
    ## get the value of the double-clicked item
    ## and pass it to the configurator
    enscript.configsection [get_curr_item]
}

## callback for new zone selection
proc enscript.newzone {} {
    ## do nothing
}

####### end required functions ########

proc enscript.widgets {command} {
    set plugframe [plug_frame]

    if {[string compare $command "start"] == 0} {
	button $plugframe.save -text "save" -command {enscript.doSave ""}
	button $plugframe.saveas -text "save as..." -command {enscript.doSaveAs}

	pack $plugframe.save
	pack $plugframe.saveas
	update
    } else {
	destroy $plugframe.save
	destroy $plugframe.saveas
	update
    }
}

## procedure to read in the configuration
## information in the chosen config file
proc enscript.readcfg {} {
    ## Check for global config in tkchooser directory
    ## and for personal config in ~/.enscriptrc. If
    ## the personal one doesn't contain the properly
    ## formatted options, make a note of that or have
    ## and option to use the global instead.

    global plug_globals env

    set plugframe [plug_frame]
    $plugframe.status configure -text "reading enscript config..."
    update

    set sections ""
    set currsection ""

    set configpath [enscript.choosecfg]
    #vwait configpath

    if {[debug]} {puts "enscript: using $configpath"}

    set configrc [open $configpath r]
    gets $configrc line
    if {![regexp {.*tkchooser valid.*} $line]} {
	set currsection "Everything"
	set sections "$sections $currsection"
    }
    
    ## read thru the whole config file
    while {[gets $configrc line] != -1} {
	if {[regexp {^\# tkchooser help.*} $line] } {
	    set directive [lindex $line 3]
	    set mesg [lindex $line 4]
	    set plug_globals($currsection.$directive.help) $mesg
	} elseif {[regexp {^\# tkchooser options.*} $line] } {
	    set directive [lindex $line 3]
	    set options [lrange $line 4 end]
	    set plug_globals($currsection.$directive.options) $options
	    if {[debug]} {puts "enscript: found options \"$options\" for \"$directive\""}
	} elseif {[regexp {^\# tkchooser comment.*} $line] } {
	    set directive [lindex $line 3]
	    set directive [string trim $directive :]
	    set comment [lrange $line 4 end]
	    set plug_globals($currsection.$directive.comment) $comment
	    if {[debug]} {puts "enscript: found comment"}
	} elseif {[regexp {^\# tkchooser section .*} $line] } {
	    set currsection [lindex $line 3]
	    set sections "$sections $currsection"
	    if {[debug]} {puts "enscript: entering section $currsection"}
	} elseif {[regexp {^\#.+} $line] } {
	    ## ignore all other comments - do nothing
	} elseif {[regexp {^$} $line] } {
	    ## ignore blank lines
	} else {
	    ## else we have an enscript directive
	    set directive [string trim [lindex $line 0] :]
	    set value [lrange $line 1 end]
	    if {[string compare $directive "Media"] == 0} {
		set value \"[lrange $line 1 end]\"
		
	    }
	    if {[debug]} {
		puts "enscript: found directive \"$directive\" with value \"$value\""
	    }
	    if {[info exists plug_globals($currsection.$directive)]} {
		lappend plug_globals($currsection.$directive) $value
	    } else {
		set plug_globals($currsection.$directive) $value
	    }
	}	
    }
    set plug_globals(sections) $sections

    $plugframe.status configure -text "ready."
    update
}

## pick the config file to used based
## on contents and location and return
## its path.
proc enscript.choosecfg {} {
    ## The first line must be "# tkchooser valid"

    global libdir plug_globals env
    
    set configpath "$libdir/etc/enscript.cfg"
    ## first look for a personal .enscriptrc and see if
    ## it's valid
    
    if {[file readable $env(HOME)/.enscriptrc]} {
	set homerc [open $env(HOME)/.enscriptrc r]
	gets $homerc line
	close $homerc
	if {[regexp {.*tkchooser valid.*} $line]} {
	    set configpath $env(HOME)/.enscriptrc
	} else {
	    ## Personal .enscriptrc is in the wrong format.
	    ## Use it anyway?
	    if {[enscript.formatquery]} {
		set configpath $env(HOME)/.enscriptrc
	    }
	}	
    }
    return $configpath
}

## procedure to query user if bad
## .enscriptrc is found
proc enscript.formatquery {} {
    set w .enscriptquery

    set homeflag -1

    toplevel $w -class dialog
    message $w.message -width 150 \
	    -text "Your ~/.enscriptrc is not in the enscript \
	    plugin format. Do you want to use it anyway?"
    button $w.yes -text "Yes, use it" -command {set homeflag 1}
    button $w.no -text "No, use default" -command {set homeflag 0}
    pack $w.message
    pack $w.no -side left
    pack $w.yes -side right
    
    vwait homeflag
    destroy $w
    if {[debug]} {puts "enscript: homeflag = $homeflag"}
    return $homeflag

}

## procedure that builds and runs the configuration
## windows for each section.
proc enscript.configsection {section} {
    global plug_globals

    if {[debug]} { puts "enscript: configuring $section" }
    set w ".a$section"
    catch {destroy $w}
    
    set directivelist ""

    foreach item [array names plug_globals "$section.*"] {
	## make sure we only grab the directive entry
	if {![regexp "$section\..*\.options" $item] \
		&& ![regexp "$section\..*\.help" $item]} {
	    if {[debug]} {puts "enscript: found $item"}
	    regsub "$section\." $item "" item
	    set directivelist "$directivelist $item"
	}
    }

    set directivelist [lsort -dictionary $directivelist]
    if {[debug]} {puts "enscript: directivelist is: $directivelist"}
    
    ## create the main window and then
    ## build all the items into it, adding
    ## option lists where available
    toplevel $w -class Dialog
    set f $w.listwin
    frame $w.listwin -relief groove
    canvas $f.canvas -width 325 -height 300 -relief ridge \
	    -yscrollcommand "$f.fscroll set"
    scrollbar $f.fscroll -takefocus 0\
	    -command "$f.canvas yview"
    
    frame $f.canvas.f
    $f.canvas create window 0 0 -anchor nw -window $f.canvas.f

    button $w.close -text "Ok" -command "destroy $w" -default active
    bind $w.close <Return> "$w.close invoke"

    pack $f.canvas -side left -anchor n -pady 4 -padx 4 -fill y -expand on
    pack $f.fscroll -side right -anchor n -fill y -expand on
    pack $f -padx 2 -pady 6 -fill y -expand on
    pack $w.close -side bottom -fill x -expand on
    

    ## each entry will consist of a frame which
    ## contains a label, entry or option list, and help
    
    foreach directive $directivelist {
	set ff $f.canvas.f.frame$directive
	frame $ff -relief groove -width 300
	
	## build help
	button $ff.help$directive -text "help" -takefocus 0\
		-command "enscript.showhelp $section $directive"
	pack $ff.help$directive -side left

	## build label
	label $ff.label$directive -text "$directive"
	pack $ff.label$directive -side left

	## build entry or options
	
	## special cases:
	if {[string compare $directive "DefaultMedia"] == 0} {
	    if {![info exists plug_globals($section.$directive.options)]} {
		foreach item $plug_globals($section.Media) {
		    set medianame [lindex $item 0]
		    if {[debug]} {puts "enscript: adding $medianame to list"}
		    if {[lsearch plug_globals($section.$directive.options) $medianame] != -1} {
			lappend plug_globals($section.$directive.options) $medianame
		    }
		}
	    }
	}
	if {[string compare $directive "Printer"] == 0} {
	    ## call readprintcap to get list of printers
	    foreach item [enscript.getprinters] {
		lappend plug_globals($section.$directive.options) $item
	    }
	}
	
	## normal
	if {[info exists plug_globals($section.$directive.options)]} {
	    menubutton $ff.menu$directive -relief sunken \
		    -text $plug_globals($section.$directive)
	    menu $ff.menu$directive.menu
	    foreach item $plug_globals($section.$directive.options) {
		$ff.menu$directive.menu add command -label $item \
			-command "set \
			plug_globals($section.$directive) $item;\
			$ff.menu$directive configure -text $item"
	    }
	    $ff.menu$directive configure -menu $ff.menu$directive.menu
	    pack $ff.menu$directive -side left
	} else {
	    entry $ff.entry$directive \
		    -textvariable plug_globals($section.$directive)
	    pack $ff.entry$directive -side left -fill x -expand on
	}
	
	pack $ff -anchor w -fill x
    }

    set optionheight [winfo reqheight $ff.help$directive]
    set listheight [expr [llength $directivelist] * $optionheight]
    $f.canvas configure -scrollregion "-1 -1 400 $listheight"
    wm title $w $section
}

## procedure to parse printernames from /etc/printcap
proc enscript.getprinters {} {
    if {[debug]} {puts "enscript: in getprinters"}
    set printerlist ""
    if {[file readable "/etc/printcap"]} {
	set fd [open "/etc/printcap" r]
	while {[gets $fd line] != -1} {
	    if {[regexp {^\#.*} $line]} {
		## do nothing - ignore comments
	    } elseif {[regexp {^.+} $line]} {
		set printer [lindex $line 0]
		if {![regexp {^:.*} $printer]} {
		    set printer [lindex [split $printer |] 0]
		    if {[debug]} {puts "enscript: found printer $printer"}
		    set printerlist "$printerlist $printer"
		}
	    }
	}
    }
    return [lsort -dictionary $printerlist]
}

## procedure to show help window
## for directives
proc enscript.showhelp {section directive} {
    global plug_globals

    if {[info exists plug_globals($section.$directive.help)]} {
	set mesg $plug_globals($section.$directive.help)
    } else {
	set mesg "Sorry, no help available for this item."
    }
    
    set w .help$section$directive
    catch {destroy $w}
    toplevel $w -class Dialog
    message $w.mesg -width 200 -text $mesg -relief groove
    button $w.close -text "Close" -command "destroy $w"
    pack $w.mesg -pady 2 -padx 2
    pack $w.close -fill x

    wm title $w "Help for $directive"

    bind $w <Return> "$w.close invoke"
    bind $w <Control-c> "$w.close invoke"
    bind $w <Control-w> "$w.close invoke"
    bind $w <Meta-w> "$w.close invoke"
}


## query for a file to save the configuration in
proc enscript.doSaveAs {} {
    set w .saveas
    catch {destroy $w}
    
    toplevel $w -class Dialog
    label $w.label -text "Save config as: "
    entry $w.entry -textvariable filename
    button $w.cancel -text "Cancel" -command "destroy $w"
    button $w.save -text "Save" \
	    -command "enscript.doSave \$filename; destroy $w" \
	    -default active

    bind $w <Return> "$w.save invoke"
    
    pack $w.label
    pack $w.entry
    pack $w.cancel -side left -anchor s -padx 4 -pady 4
    pack $w.save -side right -anchor s
}

## Save the enscript configuration to
## ~/.enscriptrc
proc enscript.doSave {homefile} {
    global plug_globals env
    if {[string compare $homefile ""] == 0} {
	set homefile "$env(HOME)/.enscriptrc"
    }

    set homefile [tilde $homefile]

    if {[debug]} {puts "enscript: doSave called with $homefile"}


    if {[file readable $homefile]} {
	file delete $homefile.bak
	file copy $homefile $homefile.bak
	file delete $homefile
    }

    set fd [open $homefile w]

    set validflag "# tkchooser valid"
    set sectionflag "# tkchooser section"
    set helpflag "# tkchooser help"
    set optionflag "# tkchooser options"
    set commentflag "# tkchooser comment"
    set colon ":"

    ## write out valid enscript plugin flag line
    puts $fd "$validflag"
    puts $fd "## This file is automatically generated! Edit with care!!"
    puts $fd ""

    foreach section $plug_globals(sections) {
	puts $fd ""
	puts $fd "#############################################"
	puts $fd "$sectionflag $section"
	puts $fd "#############################################"
	puts $fd ""

	## extract list of directives
	set directivelist ""
	foreach item [array names plug_globals "$section.*"] {
	    ## make sure we only grab the directive entry
	    if {![regexp "$section\..*\.options" $item] \
		    && ![regexp "$section\..*\.help" $item]} {
		if {[debug]} {puts "enscript: wrote $item"}
		regsub "$section\." $item "" item
		set directivelist "$directivelist $item"
	    }
	}
	
	foreach directive $directivelist {
	    puts $fd ""
	    if {[string compare $directive "Media"] == 0} {
		foreach type $plug_globals($section.$directive) {
		    if {[llength $type] > 1} {
			if {[debug]} {puts "enscript: Media: $type"}
			puts $fd "$directive$colon $type"
		    }
		}
		continue
	    }
	    
	    if {[info exists plug_globals($section.$directive.help)]} {
		puts $fd "$helpflag $directive \
			\"$plug_globals($section.$directive.help)\""
	    }
	    if {[info exists plug_globals($section.$directive.options)]} {
		puts $fd "$optionflag $directive \
			$plug_globals($section.$directive.options)"
	    }
	    if {[info exists plug_globals($section.$directive.comment)]} {
		puts $fd "$commentflag $directive$colon \
			$plug_globals($section.$directive.comment)"
	    }
	    puts $fd "$directive$colon $plug_globals($section.$directive)"
	}
    }   
    close $fd
}

## show the section headings in
## the listbox window and bind them
proc enscript.showsections {} {
    global plug_globals

    enscript.clearsections

    set pluglist [plug_list]
    set sections [lsort -dictionary $plug_globals(sections)]
    foreach section $sections {
	$pluglist insert end $section
    }
}

## delete the stuff in the listbox
proc enscript.clearsections {} {
    set pluglist [plug_list]
    $pluglist delete 0 end
}

if {[debug]} { puts "finished loading Enscript plugin." }