03cfdd41f08bc76343ea90be72be7d82b3eb36b3
[ir-tcl-moved-to-github.git] / client2 / client.tcl
1 wm title . "IrTcl Client"
2
3 # Procedure irmenu
4 proc irmenu {w} {
5     menu $w -tearoff off
6 }
7
8 proc debug-window {} {
9     set w .debug-window
10     toplevel $w
11
12     wm title $w "Debug Window" 
13     
14     frame $w.top -relief raised -border 1
15     frame $w.bot -relief raised -border 1
16     pack  $w.top -side top -fill both -expand yes
17     pack  $w.bot -fill both
18     scrollbar $w.top.s -command [list $w.top.t yview]
19     text $w.top.t -width 60 -height 10 -wrap word -relief flat -borderwidth 0 \
20         -font fixed -yscroll [list $w.top.s set]
21     pack $w.top.s -side right -fill y
22     pack $w.top.t -expand yes -fill both -expand y
23 }
24 debug-window
25
26     
27 # Procedure configure-enable-e {w n}
28 #  w   is a menu
29 #  n   menu entry number (0 is first entry)
30 # Enables menu entry
31 proc configure-enable-e {w n} {
32     $w entryconfigure $n -state normal
33 }
34
35 # Procedure configure-disable-e {w n}
36 #  w   is a menu
37 #  n   menu entry number (0 is first entry)
38 # Disables menu entry
39 proc configure-disable-e {w n} {
40     $w entryconfigure $n -state disabled
41 }
42 set noFocus [list -takefocus 0]
43
44
45 # Define dummy clock function if it is not there.
46 if {[catch {clock seconds}]} {
47     proc clock {args} {
48         return {}
49     }
50 }
51
52 # Define libdir to the IrTcl configuration directory.
53 # In the line below LIBDIR will be modified during 'make install'.
54 set libdir LIBDIR
55
56 # If the bitmaps sub directory is present with a bitmap we assume 
57 # the client is run from the source directory in which case we
58 # set libdir the current directory.
59 if {[file readable [file join bitmaps book2]]} {
60     set libdir .
61 }
62
63 # Make a final check to see if libdir was set ok.
64 if {! [file readable [file join $libdir bitmaps book2]]} {
65     puts "Cannot locate system files in ${libdir}. You must either run this"
66     puts "program from the source directory root of ir-tcl or you must assure"
67     puts "that it is installed - normally in /usr/local/lib/irtcl"
68     exit 1
69 }
70
71 # Initialize a lot of globals.
72 set hotTargets {}
73 set hotInfo {}
74 set busy 0
75
76 set profile(Default,description) {}
77 set profile(Default,host) {}
78 set profile(Default,port) 210
79 set profile(Default,authentication) {}
80 set profile(Default,maximumRecordSize) 50000
81 set profile(Default,preferredMessageSize) 30000
82 set profile(Default,comstack) tcpip
83 set profile(Default,namedResultSets) 1
84 set profile(Default,queryRPN) 1
85 set profile(Default,queryCCL) 0
86 set profile(Default,protocol) Z39
87 set profile(Default,windowNumber) 1
88 set profile(Default,largeSetLowerBound) 2
89 set profile(Default,smallSetUpperBound) 0
90 set profile(Default,mediumSetPresentNumber) 0
91 set profile(Default,presentChunk) 4
92 set profile(Default,timeDefine) {}
93 set profile(Default,timeLastInit) {}
94 set profile(Default,timeLastExplain) {}
95 set profile(Default,targetInfoName) {}
96 set profile(Default,recentNews) {}
97 set profile(Default,maxResultSets) {}
98 set profile(Default,maxResultSize) {}
99 set profile(Default,maxTerms) {}
100 set profile(Default,multipleDatabases) 0
101 set profile(Default,welcomeMessage) {}
102
103 set hostid Default
104 set currentDb Default
105 set settingsChanged 0
106 set setNo 0
107 set setNoLast 0
108 set cancelFlag 0
109 set scanEnable 0
110 set displayFormat 1
111 set popupMarcdf 0
112 set textWrap word
113 set recordSyntax None
114 set elementSetNames None
115 set delayRequest {}
116 set debugMode 0
117 set queryAutoOld 0
118
119 set queryTypes {Simple}
120 set queryButtons { { {I 0} {I 1} {I 2} } }
121 set queryInfo { { {Title {1=4 4=1}} {Author {1=1}} \
122         {Subject {1=21}} {Any {1=1016}} } }
123 set queryAuto 1
124 wm minsize . 0 0
125
126 set setOffset 0
127 set setMax 0
128
129 set syntaxList {None sep USMARC UNIMARC UKMARC DANMARC FINMARC NORMARC PICAMARC sep SUTRS sep GRS1}
130
131
132 set font(bb,normal) {Helvetica 24}
133 set font(bb,bold) {Helvetica 24 bold}
134 set font(b,normal) {Helvetica 24}
135 set font(b,bold) {Helvetica 18 bold}
136 set font(n,normal) {Helvetica 12}
137 set font(n,bold) {Helvetica 12 bold}
138 set font(s,bold) {Helvetica 10 bold}
139 set font(ss,bold) {Helvetica 8 bold}
140
141 # Procedure tkerror {err}
142 #   err   error message
143 # Override the Tk error handler function.
144 if {1} {
145     proc tkerror err {
146         global font
147         set w .tkerrorw
148         
149         if {[winfo exists $w]} {
150             destroy $w
151         }
152         toplevel $w
153         wm title $w "Error"
154         
155         place-force $w .
156         top-down-window $w
157         
158         label $w.top.b -bitmap error
159         message $w.top.t -aspect 300 -text "Error: $err" -font $font(b,bold)
160         pack $w.top.b $w.top.t -side left -padx 10 -pady 10
161         
162         bottom-buttons $w [list {Close} [list destroy $w]] 1
163     }
164 }
165
166 # Read tag set file (if present)
167 if {[file readable [file join $libdir tagsets.tcl]]} {
168     source [file join $libdir tagsets.tcl]
169 }
170
171 # Read the global target configuration file.
172 if {[file readable [file join $libdir irtdb.tcl]]} {
173 #    source "${libdir}/irtdb.tcl"
174     source [file join $libdir irtdb.tcl]
175 }
176 # Read the local target configuration file.
177 if {[file readable "irtdb.tcl"]} {
178     source "irtdb.tcl"
179 }
180
181 # Read the user configuration file.
182 if {[file readable [file join $libdir .clientrc.tcl]]} {
183 #    source "${libdir}/.clientrc.tcl"
184     source [file join $libdir .clientrc.tcl]
185 }
186
187 source "bib-1.tcl"
188
189 set queryAutoOld $queryAuto
190
191 # Convert old format to new format...
192 foreach target [array names profile] {
193     set timedef [clock seconds]
194     if {[string first , $target] == -1} {
195     if {![info exists profile($target,port)]} {
196         foreach n [array names profile Default,*] {
197         set profile($target,[string range $n 8 end]) $profile($n)
198         }
199         set profile($target,description) [lindex $profile($target) 0]
200         set profile($target,host) [lindex $profile($target) 1]
201         set profile($target,port) [lindex $profile($target) 2]
202         set profile($target,authentication) [lindex $profile($target) 3]
203         set profile($target,maximumRecordSize) \
204         [lindex $profile($target) 4]
205         set profile($target,preferredMessageSize) \
206         [lindex $profile($target) 5]
207         set profile($target,comstack) [lindex $profile($target) 6]
208         set profile($target,databases) [lindex $profile($target) 7]
209         set profile($target,timeDefine) $timedef
210         set profile($target,windowNumber) 1
211     }
212     unset profile($target)
213     }
214 }
215
216 # Assign unique ID's to each target's window number
217 set wno 1
218 foreach n [array names profile *,windowNumber] {
219     set profile($n) $wno
220     incr wno
221 }
222 set profile(Default,windowNumber) $wno
223
224 # These globals describe the current query type. They are set to the
225 # first query type.
226 set queryButtonsFind [lindex $queryButtons 0]
227 set queryInfoFind [lindex $queryInfo 0]
228
229 # Procedure read-formats
230 # Read all Tcl source files in the subdirectory 'formats'.
231 # The name of each source will correspond to a display format.
232 proc read-formats {} {
233     global displayFormats
234     global libdir
235
236     set oldDir [pwd]
237     cd [file join $libdir formats]
238     set formats [glob {*.[tT][cC][lL]}]
239     foreach f $formats {
240     if {[file readable $f]} {
241             source $f
242             set l [string length $f]
243             set f [string tolower [string range $f 0 [expr $l - 5]]]
244             lappend displayFormats $f
245         }
246     }
247     cd $oldDir
248 }
249
250 # Procedure set-wrap {m}
251 #  m    boolean wrap mode
252 # Handler to enable/disable text wrap in the main record window
253 proc set-wrap {m} {
254     global textWrap
255
256     set textWrap $m
257     .data.record configure -wrap $m
258 }
259
260 # Procedure dputs {m}
261 #  m    string to be printed
262 # puts utility for debugging.
263 proc dputs {m} {
264     global debugMode
265     if {$debugMode} {
266         puts $m
267     }
268 }
269
270 # Procedure apduDump {}
271 # Logs BER dump of last APDU in window if debugMode is true.
272 proc apduDump {} {
273     global debugMode
274
275     set w .apdu
276
277     if {$debugMode == 0} return
278     set x [z39 apduInfo]
279
280     set offset [lindex $x 1]
281     set length [lindex $x 0]
282
283     if {![winfo exists $w]} {
284         catch {destroy $w}
285         toplevelG $w
286
287         wm title $w "APDU information"       
288         wm minsize $w 0 0
289         
290         top-down-window $w
291         
292         text $w.top.t -font fixed -width 60 -height 12 -wrap word \
293             -relief flat -borderwidth 0 \
294             -yscrollcommand [list $w.top.s set] -background grey85
295         scrollbar $w.top.s -command [list $w.top.t yview]
296         pack $w.top.s -side right -fill y
297         pack $w.top.t -expand yes -fill both
298
299         bottom-buttons $w [list {Close} [list destroy $w]] 0
300     }
301     $w.top.t insert end "Length: ${length}\n"
302     if {$offset != -1} {
303         $w.top.t insert end "Offset: ${offset}\n"
304     }
305     $w.top.t insert end [lindex $x 2]
306     $w.top.t insert end "---------------------------------\n"
307
308 }
309
310 # Procedure set-display-format {f}
311 #  f    display format
312 # Reformats main record window to use display format given by f
313 proc set-display-format {f} {
314     global displayFormat setNo busy
315
316     set displayFormat $f
317     if {$setNo == 0} {
318         return
319     }
320     if {!$busy} {
321         .bot.a.status configure -text "Reformatting"
322     }
323     update idletasks
324     add-title-lines -1 10000 1
325 }
326
327 # Procedure initBindings
328 # Disables various default bindings for Text and Listbox widgets.
329 proc initBindings {} {
330     global TextBinding
331
332     foreach e [bind Text] {
333         set TextBinding($e) [bind Text $e]
334         bind Text $e {}
335     }
336     set w Listbox
337     bind $w <B1-Motion> {}
338     bind $w <Shift-B1-Motion> {}
339
340     set w Entry
341 }
342
343 # Procedure TextEditable 
344 # Apply "standard" events to a text widget. It should be editable now.
345 proc TextEditable {w} {
346     global TextBinding
347
348     foreach e [array names TextBinding] {
349         bind $w $e $TextBinding($e)
350     }
351 }
352
353 # Procedure destroyGW {w}
354 #   w     top level widget
355 # Saves geometry of widget w in windowGeometry array. This
356 # Procedure is used to save current geometry of a window before
357 # it is destroyed.
358 # See also topLevelG.
359 proc destroyGW {w} {
360     global windowGeometry
361     catch {set windowGeometry($w) [wm geometry $w]}
362 }    
363
364 # Procedure topLevelG
365 #   w     top level widget
366 # Makes a new top level widget named w; sets geometry of window if it 
367 # exists in windowGeometry array. The destroyGW procedure is set 
368 # to be called when the Destroy event occurs.
369 proc toplevelG {w} {
370     global windowGeometry
371
372     toplevel $w
373     if {[info exists windowGeometry($w)]} {
374         set g $windowGeometry($w)
375         if {$g != ""} {
376             wm geometry $w $g
377         }
378     }
379     bind $w <Destroy> [list destroyGW $w]
380 }
381
382 # Procedure top-down-window {w}
383 #  w    window (possibly top level)
384 # Makes two frames inside w called top and bot.
385 proc top-down-window {w} {
386     frame $w.top -relief raised -border 1
387     frame $w.bot -relief raised -border 1
388     pack  $w.top -side top -fill both -expand yes
389     pack  $w.bot -fill both
390 }
391
392 # Procedure top-down-ok-cancel {w ok-action g}
393 #  w          top level widget with $w.bot-frame
394 #  ok-action  ok script
395 #  g          grab flag
396 # Makes two buttons in the bot frame called Ok and Cancel. The
397 # ok-action is executed if Ok is pressed. If Cancel is activated
398 # The window is destroyed. If g is true a grab is performed on the
399 # window and the procedure waits until the window is destroyed.
400 proc top-down-ok-cancel {w ok-action g} {
401     frame $w.bot.left -relief sunken -border 1
402     pack $w.bot.left -side left -expand yes -ipadx 2 -ipady 2 -padx 1 -pady 1
403     button $w.bot.left.ok -width 4 -text {Ok} -command ${ok-action}
404     pack $w.bot.left.ok -expand yes -ipadx 1 -ipady 1 -padx 2 -pady 2
405     button $w.bot.cancel -width 5 -text {Cancel} -command [list destroy $w]
406     pack $w.bot.cancel -side left -expand yes    
407
408     if {$g} {
409         grab $w
410         tkwait window $w
411     }
412 }
413
414 # Procedure bottom-buttons {w buttonList g}
415 #  w          top level widget with $w.bot-frame
416 #  buttonList button specifications
417 #  g          grab flag
418 # Makes a list of buttons in the $w.bot frame. The buttonList is a list 
419 # of button specifications. Each button specification consists of two
420 # items; the first item is button label name; the second item is a script
421 # of be executed when that button is executed. A grab is performed if g 
422 # is true and it waits for the window to be destroyed.
423 proc bottom-buttons {w buttonList g} {
424     set i 0
425     set l [llength $buttonList]
426
427     frame $w.bot.$i -relief sunken -border 1
428     pack $w.bot.$i -side left -expand yes -padx 2 -pady 2
429     button $w.bot.$i.ok -text [lindex $buttonList $i] \
430         -command [lindex $buttonList [expr $i + 1]]
431     pack $w.bot.$i.ok -expand yes -padx 2 -pady 2 -side left
432
433     incr i 2
434     while {$i < $l} {
435         button $w.bot.$i -text [lindex $buttonList $i] \
436                 -command [lindex $buttonList [expr $i + 1]]
437         pack $w.bot.$i -expand yes -padx 2 -pady 2 -side left
438         incr i 2
439     }
440     if {$g} {
441         # Grab ...
442         grab $w
443         tkwait window $w
444     }
445 }
446
447 # Procedure cancel-operation
448 # This handler is invoked when the user wishes to cancel an operation.
449 # If the system is currently busy a "Cancel" will be displayed in the
450 # status area and the cancelFlag is set to true indicating that future
451 # responses from the target should be ignored. The system is no longer
452 # busy when this procedure exists.
453 proc cancel-operation {} {
454     global cancelFlag busy delayRequest
455
456     if {$busy} {
457         set cancelFlag 1
458         set delayRequest {}
459         show-status Cancel 0 1
460     }
461 }
462
463 # Procedure show-target {target base}
464 #  target     name of target
465 #  base       name of database
466 # Displays target name and database name in the target status area.
467 proc show-target {target base} {
468     if {![string length $target]} {
469         .bot.a.target configure -text {}
470         return
471     }
472     if {![string length $base]} {
473         .bot.a.target configure -text "$target"
474     } else {
475          .bot.a.target configure -text "$target - $base"
476     }
477 }
478
479 # Procedure show-logo {v1}
480 #  v1    integer level
481 # This procedure maintains the book logo in the bottom of the screen.
482 # It is invoked only once during initialization of windows, etc., and
483 # by itself. The global 'busy' variable determines whether the logo is
484 # moving or not.
485 proc show-logo {v1} {
486     global busy libdir
487
488     if {$busy != 0} {
489         incr v1
490         if {$v1==10} {
491             set v1 1
492         }
493         .bot.logo configure -bitmap @[file join $libdir bitmaps book${v1}] 
494         after 140 [list show-logo $v1]
495         return
496     }
497     while {1} {
498         .bot.logo configure -bitmap @[file join $libdir bitmaps book1]
499         tkwait variable busy
500         if {$busy} {
501             show-logo 1
502             return
503         }
504     }
505 }
506
507 # Procedure show-status {status b sb}
508 #  status     status message string
509 #  b          busy indicator
510 #  sb         search busy indicator
511 # Display status information according to 'status' and sets the global
512 # busy flag 'busy' to b if b is non-empty. If sb is non-empty it indicates
513 # whether service buttons should be enabled or disabled.
514 proc show-status {status b sb} {
515     global busy scanEnable setOffset setMax setNo
516
517     .bot.a.status configure -text "$status"
518     if {$b == 1} {
519         if {$busy == 0} {set busy 1}
520     } else {
521         set busy 0
522     }
523     if {$sb == {}} {
524         return
525     }
526     if {$sb} {
527         .top.service configure -state normal
528         .mid.search configure -state normal
529         if {$scanEnable} {
530             .mid.scan configure -state normal
531         } else {
532             configure-disable-e .top.service.m 3
533         }
534         if {$setNo == 0} {
535             configure-disable-e .top.service.m 1
536         } elseif {[z39.$setNo nextResultSetPosition] > 0 && 
537             [z39.$setNo nextResultSetPosition] <= [z39.$setNo resultCount]} {
538             configure-enable-e .top.service.m 1
539             .mid.present configure -state normal
540         } else {
541             configure-disable-e .top.service.m 1
542             .mid.present configure -state disabled
543         }
544         if {[winfo exists .scan-window]} {
545             .scan-window.bot.2 configure -state normal
546             .scan-window.bot.4 configure -state normal
547         }
548     } else {
549         .top.service configure -state disabled
550         .mid.search configure -state disabled
551         .mid.scan configure -state disabled
552         .mid.present configure -state disabled
553
554         if {[winfo exists .scan-window]} {
555             .scan-window.bot.2 configure -state disabled
556             .scan-window.bot.4 configure -state disabled
557         }
558     }
559 }
560
561 # Procedure show-message {msg}
562 #  msg    message string
563 # Sets message the bottom of the screen to msg.
564 proc show-message {msg} {
565     .bot.a.message configure -text "$msg"
566 }
567
568 # Procedure insertWithTags {w text args}
569 #  w      text widget
570 #  text   string to be inserted
571 #  args   list of tags
572 # Inserts text at the insertion point in widget w. The text is tagged 
573 # with the tags in args.
574 proc insertWithTags {w text args} {
575     set start [$w index insert]
576     $w insert insert $text
577     foreach tag [$w tag names $start] {
578         $w tag remove $tag $start insert
579     }
580     foreach i $args {
581         $w tag add $i $start insert
582     }
583 }
584
585 # Procedure popup-license and displays LICENSE information.
586 proc popup-license {} {
587     global libdir
588     set w .popup-license
589     toplevel $w
590
591     wm title $w "License" 
592     wm minsize $w 0 0
593
594     top-down-window $w
595
596     text $w.top.t -width 80 -height 10 -wrap word -relief flat -borderwidth 0 \
597         -font fixed -yscrollcommand [list $w.top.s set]
598     scrollbar $w.top.s -command [list $w.top.t yview]
599     pack $w.top.s -side right -fill y
600     pack $w.top.t -expand yes -fill both
601
602     if {[file readable [file join $libdir LICENSE]]} {
603         set f [open [file join $libdir LICENSE] r]
604         while {[gets $f buf] != -1} {
605             $w.top.t insert end $buf
606             $w.top.t insert end "\n"
607         } 
608         close $f
609     }
610     bottom-buttons $w [list {Close} [list destroy $w]] 1
611 }
612
613 # Procedure about-target
614 # Displays various information about the current target, such
615 # as implementation-name, implementation-id, etc.
616 proc about-target {} {
617     set w .about-target-w
618     global hostid font
619
620     toplevel $w
621
622     wm title $w "About target"
623     place-force $w .
624     top-down-window $w
625
626     frame $w.top.a -relief ridge -border 2
627     frame $w.top.p -relief ridge -border 2
628     pack $w.top.a $w.top.p -side top -fill x
629
630     label $w.top.a.about -text "About"
631     label $w.top.a.irtcl -text $hostid -font $font(bb,bold)
632     pack $w.top.a.about $w.top.a.irtcl -side top
633
634     set i [z39 targetImplementationName]
635     label $w.top.p.in -text "Implementation name: $i"
636     set i [z39 targetImplementationId]
637     label $w.top.p.ii -text "Implementation id: $i"
638     set i [z39 targetImplementationVersion]
639     label $w.top.p.iv -text "Implementation version: $i"
640     set i [z39 options]
641     label $w.top.p.op -text "Protocol options: $i"
642     pack $w.top.p.in $w.top.p.ii $w.top.p.iv $w.top.p.op -side top -anchor nw
643
644     bottom-buttons $w [list {Close} [list destroy $w]] 1
645 }
646
647 # Procedure about-origin-logo {n}
648 #   n    integer level
649 # Displays book logo in the .about-origin-w widget
650 proc about-origin-logo {n} {
651     global libdir
652     set w .about-origin-w
653     if {![winfo exists $w]} {
654         return
655     }
656     incr n
657     if {$n==10} {
658         set n 1
659     }
660     $w.top.a.logo configure -bitmap @[file join $libdir bitmaps book$n]
661     after 140 [list about-origin-logo $n]
662 }
663
664 # Procedure about-origin
665 # Display various information about origin (this client).
666 proc about-origin {} {
667     set w .about-origin-w
668     global libdir font tk_version
669     
670     if {[winfo exists $w]} {
671         destroy $w
672     }
673     toplevel $w
674
675     wm title $w "About IrTcl"
676     place-force $w .
677     top-down-window $w
678
679     frame $w.top.a -relief ridge -border 2
680     frame $w.top.p -relief ridge -border 2
681     pack $w.top.a $w.top.p -side top -fill x
682     
683     label $w.top.a.irtcl -text "IrTcl" -font $font(bb,bold)
684     label $w.top.a.logo -bitmap @[file join $libdir bitmaps book1] 
685     pack $w.top.a.irtcl $w.top.a.logo -side left -expand yes
686
687     set i unknown
688     catch {set i [z39 implementationName]}
689     label $w.top.p.in -text "Implementation name: $i"
690     catch {set i [z39 implementationId]}
691     label $w.top.p.ii -text "Implementation id: $i"
692     catch {set i [z39 implementationVersion]}
693     label $w.top.p.iv -text "Implementation version: $i"
694     set i $tk_version
695     label $w.top.p.tk -text "Tk version: $i"
696     pack $w.top.p.in $w.top.p.ii $w.top.p.iv $w.top.p.tk -side top -anchor nw
697
698     about-origin-logo 1
699     bottom-buttons $w [list {Close} [list destroy $w] \
700                             {License} [list popup-license]] 0
701 }
702
703 # Procedure popup-marc {sno no b df}
704 #  sno     result set number
705 #  no      record position number
706 #  b       popup window number
707 #  df      display format
708 # Displays record in set $sno at position $no in window .full-marc$b.
709 # The global variable $popupMarcdf holds the current format method.
710 proc popup-marc {sno no b df} {
711     global font displayFormats popupMarcdf
712
713     if {[z39.$sno type $no] != "DB"} {
714         return
715     }
716     if {$b == -1} {
717         set b 0
718         while {[winfo exists .full-marc$b]} {
719             incr b
720         }
721     }
722     set df $popupMarcdf
723     set w .full-marc$b
724     if {![winfo exists $w]} {
725         toplevelG $w
726
727         wm minsize $w 0 0
728         
729         frame $w.top -relief raised -border 1
730         frame $w.bot -relief raised -border 1
731         pack  $w.top -side top -fill both -expand yes
732         pack  $w.bot -fill both
733
734         text $w.top.record -width 60 -height 5 -wrap word -relief flat \
735             -borderwidth 0 -font fixed \
736             -yscrollcommand [list $w.top.s set] -background grey85
737         scrollbar $w.top.s -command [list $w.top.record yview] 
738         $w.top.record tag configure marc-tag -foreground blue
739         $w.top.record tag configure marc-id -foreground red
740         $w.top.record tag configure marc-data -foreground black
741         $w.top.record tag configure marc-head -font $font(n,bold) \
742              -background black -foreground white
743         $w.top.record tag configure marc-pref -font $font(n,normal) -foreground blue
744         $w.top.record tag configure marc-text -font $font(n,normal) -foreground black
745         $w.top.record tag configure marc-it -font $font(n,normal) -foreground black
746
747         pack $w.top.s -side right -fill y
748         pack $w.top.record -expand yes -fill both
749         
750         bottom-buttons $w [list \
751             {Close} [list destroy $w] \
752             {Prev} {} \
753             {Next} {} \
754             {Duplicate} {}] 0
755         menubutton $w.bot.formats -text "Format" -menu $w.bot.formats.m -relief raised
756         irmenu $w.bot.formats.m
757         pack $w.bot.formats -expand yes -ipadx 2 -ipady 2 -padx 3 -pady 3 -side left
758     } else {
759         $w.bot.formats.m delete 0 last
760     }
761     set i 0
762     foreach f $displayFormats {
763         $w.bot.formats.m add radiobutton -label $f -variable popupMarcdf -value $i \
764             -command [list popup-marc $sno $no $b 0]
765         incr i
766     }
767     $w.top.record delete 0.0 end
768     set recordType [z39.$sno recordType $no]
769     wm title $w "$recordType record #$no"
770
771     $w.bot.2 configure -command [list popup-marc $sno [expr $no-1] $b $df]
772     $w.bot.4 configure -command [list popup-marc $sno [expr $no+1] $b $df]
773     if {$no == 1} {
774         $w.bot.2 configure -state disabled
775     } else {
776         $w.bot.2 configure -state normal
777     }
778     if {[z39.$sno type [expr $no+1]] != "DB"} {
779         $w.bot.4 configure -state disabled
780     } else {
781         $w.bot.4 configure -state normal
782     }
783     $w.bot.6 configure -command [list popup-marc $sno $no -1 0]
784     set ffunc [lindex $displayFormats $df]
785     set ffunc "display-$ffunc"
786
787     $ffunc $sno $no $w.top.record 0
788 }
789
790 # Procedure update-target-hotlist {target base}
791 #  target     current target name
792 #  base       current database name
793 # Updates the global $hotTargets so that $target and $base are
794 # moved to the front, i.e. they become the number 1 target/base.
795 # The target menu is updated by a call to set-target-hotlist.
796 proc update-target-hotlist {target base} {
797     global hotTargets
798
799     set olen [llength $hotTargets]
800     set i 0
801     foreach e $hotTargets {
802         if {$target == [lindex $e 0] && $base == [lindex $e 1]} {
803             set hotTargets [lreplace $hotTargets $i $i]
804             break
805         }
806         incr i    
807     }
808     set hotTargets [linsert $hotTargets 0 [list $target $base]]
809     set-target-hotlist $olen
810
811
812 # Procedure delete-target-hotlist {target}
813 #  target    target to be deleted
814 # Updates the global $hotTargets so that $target is removed.
815 # The target menu is updated by a call to set-target-hotlist.
816 proc delete-target-hotlist {target} {
817     global hotTargets
818
819     set olen [llength $hotTargets]
820     set i 0
821     foreach e $hotTargets {
822         if {$target == [lindex $e 0]} {
823         set hotTargets [lreplace $hotTargets $i $i]
824         }
825         incr i
826     }
827     set-target-hotlist $olen
828 }
829
830 # Procedure set-target-hotlist {olen}
831 #  olen     number of hot target entries to be deleted from menu
832 # Updates the target menu with the targets with the first 8 entries
833 # in the $hotTargets global.
834 proc set-target-hotlist {olen} {
835     global hotTargets
836    
837     if {$olen > 0} {
838        .top.target.m delete 6 [expr 6+$olen]
839     }
840     set i 1
841     foreach e $hotTargets {
842         set target [lindex $e 0]
843         set base [lindex $e 1]
844         if {![string length $base]} {
845             .top.target.m add command -label "$i $target" -command \
846                 [list reopen-target $target {}]
847         } else {
848             .top.target.m add command -label "$i $target - $base" -command \
849                 [list reopen-target $target $base]
850         }
851         incr i
852         if {$i > 8} {
853              break
854         }
855     }
856 }
857
858 # Procedure reopen-target {target base}
859 #  target    target to be opened
860 #  base      base to be used
861 # Closes connection with current target and opens a new connection
862 # with $target and database $base.
863 proc reopen-target {target base} {
864     close-target
865     open-target $target $base
866     update-target-hotlist $target $base
867 }
868
869 # Procedure define-target-action
870 # Prepares the setup of a new target. The name of the target
871 # is read from the dialog .target-define dialog (procedure
872 # define-target-dialog) and the target definition window is displayed by
873 # a call to protocol-setup.
874 proc define-target-action {} {
875     global profile
876     
877     set target [.target-define.top.target.entry get]
878     if {![string length $target]} {
879         return
880     }
881     foreach n [array names profile *,host] {
882         if {![string compare $n ${target},host]} {
883             destroy .target-define
884             protocol-setup $n
885             return
886         }
887     }
888     foreach n [array names profile Default,*] {
889     set profile($target,[string range $n 8 end]) $profile($n)
890
891     }
892     incr profile(Default,windowNumber)
893     
894     protocol-setup $target
895     destroy .target-define
896 }
897
898 # Procedure fail-response {target}
899 #  target   current target
900 # Error handler (IrTcl failback) that takes care of serious protocol
901 # errors, connection lost, etc.
902 proc fail-response {target} {
903     global debugMode
904
905     set c [lindex [z39 failInfo] 0]
906     set m [lindex [z39 failInfo] 1]
907     if {$c == 4 || $c == 5} {
908         set debugMode 1        
909         apduDump
910     }
911     close-target
912 #    tkerror "$m ($c)"
913     bgerror "$m ($c)"
914 }
915
916 # Procedure connect-response {target base}
917 #  target   current target
918 #  base     current database
919 # IrTcl connect response handler.
920 proc connect-response {target base} {
921     dputs "connect-response"
922     init-request $target $base
923 }
924
925 # Procedure open-target {target base}
926 #  target   target to be opened
927 #  base     database to be used
928 # Opens a new connection with $target/$base.
929 proc open-target {target base} {
930     global profile hostid presentChunk currentDb
931
932     z39 disconnect
933     z39 comstack $profile($target,comstack)
934     z39 protocol $profile($target,protocol)
935     eval z39 idAuthentication $profile($target,authentication)
936     z39 maximumRecordSize $profile($target,maximumRecordSize)
937     z39 preferredMessageSize $profile($target,preferredMessageSize)
938     dputs "maximumRecordSize=[z39 maximumRecordSize]"
939     dputs "preferredMessageSize=[z39 preferredMessageSize]"
940     show-status Connecting 1 0
941     set x $profile($target,largeSetLowerBound)
942     if {![string length $x]} {
943         set x 2
944     }
945     z39 largeSetLowerBound $x
946     
947     set x $profile($target,smallSetUpperBound)
948     if {![string length $x]} {
949         set x 0
950     }
951     z39 smallSetUpperBound $x
952     
953     set x $profile($target,mediumSetPresentNumber)
954     if {![string length $x]} {
955         set x 0
956     }
957     z39 mediumSetPresentNumber $x
958
959     set presentChunk $profile($target,presentChunk)
960     if {![string length $presentChunk]} {
961         set presentChunk 4
962     }
963
964     z39 failback [list fail-response $target]
965     z39 callback [list connect-response $target $base]
966     show-target $target $base
967     update idletasks
968     set err [catch {
969         z39 connect $profile($target,host):$profile($target,port)
970     } errorMessage]
971     if {$err} {
972         set hostid Default
973 #        tkerror $errorMessage
974         bgerror $errorMessage
975         show-status "Not connected" 0 {}
976         show-target {} {}
977         return
978     }
979     set hostid $target
980     set currentDb $base
981     configure-disable-e .top.target.m 0
982     configure-enable-e .top.target.m 1
983     configure-enable-e .top.target.m 2
984 }
985
986 # Procedure close-target
987 # Shuts down the connection with current target.
988 proc close-target {} {
989     global hostid cancelFlag setNo setNoLast currentDb
990
991     set cancelFlag 0
992     set setNo 0
993     set setNoLast 0
994     .bot.a.set configure -text ""
995     set hostid Default
996     set currentDb Default
997     z39 disconnect
998     show-target {} {}
999     show-status {Not connected} 0 0
1000     .top.options.m.query.slist entryconfigure 2 -state disabled
1001     init-title-lines
1002     show-message {}
1003     configure-disable-e .top.target.m 1
1004     configure-disable-e .top.target.m 2
1005     .top.rset.m delete 1 last
1006     .top.rset.m add separator
1007     configure-enable-e .top.target.m 0
1008 }
1009
1010 # Procedure load-set-action
1011 # Loads records from a file. The filename is read from the entry
1012 # .load-set.filename.entry (see function load-set)
1013 proc load-set-action {} {
1014     global setNoLast
1015
1016     incr setNoLast
1017     ir-set z39.$setNoLast z39
1018
1019     set fname [.load-set.top.filename.entry get]
1020     destroy .load-set
1021     if {$fname != ""} {
1022         show-status Loading 1 {}
1023         update
1024         z39.$setNoLast loadFile $fname
1025
1026         set no [z39.$setNoLast numberOfRecordsReturned]
1027         add-title-lines $setNoLast $no 1
1028     }
1029     set l [format "%-4d %7d" $setNoLast $no]
1030     .top.rset.m add command -label $l \
1031             -command [list add-title-lines $setNoLast 10000 1]
1032     show-status Ready 0 {}
1033 }
1034
1035 # Procedure load-set
1036 # Dialog that asks for a filename with records to be loaded
1037 # into a result set.
1038 proc load-set {} {
1039     set w .load-set
1040     toplevel $w
1041     set oldFocus [focus]
1042     place-force $w .
1043     top-down-window $w
1044
1045     frame $w.top.filename
1046     pack $w.top.filename -side top -anchor e -pady 2
1047     
1048     entry-fields $w.top {filename} \
1049             {{Filename:}} \
1050             {load-set-action} {destroy .load-set}
1051     
1052     top-down-ok-cancel $w {load-set-action} 1
1053     focus $oldFocus
1054 }
1055
1056 # Procedure init-request
1057 # Sends an initialize request to the target. This procedure is called
1058 # when a connect has been established.
1059 proc init-request {target base} {
1060     global cancelFlag
1061
1062     if {$cancelFlag} {
1063         close-target
1064         return
1065     }
1066     z39 callback [list init-response $target $base]
1067     show-status Initializing 1 {}
1068     set err [catch {z39 init} errorMessage]
1069     if {$err} {
1070 #        tkerror $errorMessage
1071         bgerror $errorMessage
1072         show-status Ready 0 {}
1073     }
1074 }
1075
1076 # Procedure init-response
1077 # Handles and incoming init-response. The service buttons
1078 # are enabled. The global $scanEnable indicates whether the target
1079 # supports scan.
1080 proc init-response {target base} {
1081     global cancelFlag profile scanEnable settingsChanged
1082
1083     dputs {init-response}
1084     apduDump
1085     if {$cancelFlag} {
1086         close-target
1087         return
1088     }
1089     if {![z39 initResult]} {
1090         set u [z39 userInformationField]
1091         close-target
1092 #        tkerror "Connection rejected by target: $u"
1093         bgerror "Connection rejected by target: $u"
1094     } else {
1095         z39 failback [list explain-crash $target $base]
1096         explain-check $target [list ready-response $base] $base
1097     }
1098 }
1099
1100 # Procedure explain-crash
1101 # Handles target that dies during explain.
1102 proc explain-crash {target base} {
1103     global profile settingsChanged
1104     
1105     set profile($target,timeLastInit) [clock seconds]
1106     set settingsChanged 1
1107
1108     show-message {}
1109     open-target $target $base
1110 }
1111
1112 # Procedure explain-check 
1113 # Stub function to check explain. May be overwritten later.
1114 #proc explain-check {target response} 
1115 #    eval $response [list $target]
1116
1117
1118 # Procedure ready-response
1119 # Called after a target has been initialized and, possibly, explained
1120 proc ready-response {base target} {
1121     global profile settingsChanged scanEnable queryAuto
1122     
1123     z39 failback [list fail-response $target]
1124     if {[string length $base]} {
1125     set profile($target,timeLastInit) [clock seconds]
1126     set settingsChanged 1
1127
1128     z39 databaseNames $base
1129     cascade-dblist $target $base
1130     show-target $target $base
1131     }
1132     if {[lsearch [z39 options] scan] >= 0} {
1133         set scanEnable 1
1134     } else {
1135         set scanEnable 0
1136     }
1137     .data.record delete 1.0 end
1138     set desc [string trim $profile($target,description)]
1139     if {[string length $desc]} {
1140         .data.record insert end "$desc\n\n"
1141     } else {
1142         .data.record insert end "$target\n\n"
1143     }
1144     set data [string trim $profile($target,welcomeMessage)]
1145     if {[string length $data]} {
1146         .data.record insert end "Welcome Message:\n$data\n\n"
1147     }
1148     set data [string trim $profile($target,recentNews)]
1149     if {[string length $data]} {
1150         .data.record insert end "News:\n$data\n"
1151     }
1152     ready-response-actions $target $base
1153     show-message {}
1154     show-status Ready 0 1
1155 }
1156
1157 #proc ready-response-actions {target base}
1158 #This procedure take care of all the actions that should start if connect is succesfull.
1159 proc ready-response-actions {target base} {
1160     global profile queryAuto attributeTypeSelected
1161     configureOptionsSyntax $target $base
1162     if {[info exists profile($target,AttributeDetails,$base,Bib1Use)] && $queryAuto == 1} {
1163         changeQueryButtons $target $base 
1164         change-queryInfo $target $base
1165         query-select 2
1166         .top.options.m.query.slist entryconfigure 2 -state normal
1167     } else {
1168         query-select 0
1169         .top.options.m.query.slist entryconfigure 2 -state disabled
1170     }
1171     if {[info exists attributeTypeSelected]} {
1172         global attribute[set attributeTypeSelected]
1173         set attribute$attributeTypeSelected 1
1174     } else {
1175         global attributeBib1
1176         set attributeBib1 1
1177     }
1178 }
1179
1180 # Procedure search-request
1181 #  bflag     flag to indicate if this procedure calls itself
1182 # Performs a search. If $busy is 1, the search-request is performed
1183 # at a later time (when another response arrives). This procedure
1184 # sets many search-related Z39-settings. The global $setNo is set
1185 # to the result set number (z39.$setNo).
1186 proc search-request {bflag} {
1187     global setNo setNoLast profile hostid busy cancelFlag delayRequest recordSyntax elementSetNames
1188
1189     set target $hostid
1190     
1191     if {![string length [z39 connect]]} {
1192         return
1193     }
1194     dputs "search-request"
1195     show-message {}
1196     if {!$bflag && $busy} {
1197         dputs "busy: search-request ignored"
1198         return
1199     }
1200     if {$cancelFlag} {
1201         dputs "cancelFlag"
1202         show-status Searching 1 0
1203         set delayRequest {search-request 1}
1204         return
1205     }
1206     set delayRequest {} 
1207
1208     set query [index-query]
1209     if {![string length $query]} {
1210         return
1211     }
1212     incr setNoLast
1213     set setNo $setNoLast
1214     ir-set z39.$setNo z39
1215     
1216     if {$profile($target,namedResultSets)} {
1217         z39.$setNo setName $setNo
1218         dputs "setName=${setNo}"
1219     } else {
1220         z39.$setNo setName default
1221         dputs "setName=default"
1222     }
1223     if {$profile($target,queryRPN)} {
1224         z39.$setNo queryType rpn
1225     } elseif {$profile($target,queryCCL)} {
1226         z39.$setNo queryType ccl
1227     }
1228     dputs Setting
1229     dputs $recordSyntax
1230     if {![string compare $recordSyntax None]} {
1231         z39.$setNo preferredRecordSyntax {}
1232     } else {
1233         z39.$setNo preferredRecordSyntax $recordSyntax
1234     }
1235     if {![string compare $elementSetNames None]} {
1236         z39.$setNo elementSetNames {}
1237         z39.$setNo smallSetElementSetNames {}
1238         z39.$setNo mediumSetElementSetNames {}
1239     } else {
1240         z39.$setNo elementSetNames $elementSetNames
1241         z39.$setNo smallSetElementSetNames $elementSetNames
1242         z39.$setNo mediumSetElementSetNames $elementSetNames
1243     }
1244     z39 callback {search-response}
1245     z39.$setNo search $query
1246     show-status Searching 1 0
1247 }
1248
1249 # Procedure scan-copy {y entry}
1250 #  y       y-position of mouse pointer
1251 #  entry   a search entry in the top
1252 # Copies the term in the list nearest $y to the query entry specified
1253 # by $entry
1254 proc scan-copy {y entry} {
1255     set w .scan-window
1256     set no [$w.top.list nearest $y]
1257     dputs "no=$no"
1258     .lines.$entry.e delete 0 end
1259     .lines.$entry.e insert 0 [string range [$w.top.list get $no] 8 end]
1260 }
1261
1262 # Procedure scan-request
1263 # Performs a scan on term "0" with the current attributes in entry
1264 # specified by the global $curIndexEntry.
1265 proc scan-request {} {
1266     set w .scan-window
1267
1268     global profile hostid scanView scanTerm curIndexEntry queryButtonsFind \
1269         queryInfoFind cancelFlag delayRequest
1270
1271     dputs "scan-request"
1272     if {$cancelFlag} {
1273         dputs "cancelFlag"
1274         show-status Scanning 1 0
1275         set delayRequest scan-request
1276         return
1277     }
1278     set delayRequest {} 
1279     set target $hostid
1280     set scanView 0
1281     set scanTerm {}
1282     set b [lindex $queryButtonsFind $curIndexEntry]
1283     set attr {}
1284     foreach a [lrange [lindex $queryInfoFind [lindex $b 1]] 1 end] {
1285         set attr "@attr $a $attr"
1286     }
1287     set title [lindex [lindex $queryInfoFind [lindex $b 1]] 0]
1288     ir-scan z39.scan z39
1289
1290     if {![winfo exists $w]} {
1291         toplevelG $w
1292         
1293         wm minsize $w 0 0
1294
1295         top-down-window $w
1296
1297         entry $w.top.entry -relief sunken 
1298         pack $w.top.entry -fill x -padx 4 -pady 2
1299         bind $w.top.entry <KeyRelease> [list scan-term-h $attr]
1300         listbox $w.top.list -yscrollcommand [list $w.top.scroll set] -font fixed 
1301         scrollbar $w.top.scroll -orient vertical -border 1
1302         pack $w.top.list -side left -fill both -expand yes
1303         pack $w.top.scroll -side right -fill y
1304         $w.top.scroll config -command [list $w.top.list yview]
1305         
1306         bottom-buttons $w [list {Close} [list destroy $w] \
1307                 {Up} [list scan-up $attr] \
1308                 {Down} [list scan-down $attr]] 0
1309         bind $w.top.list <Up> [list scan-up $attr]
1310         bind $w.top.list <Down> [list scan-down $attr]
1311         focus $w.top.entry
1312     }
1313     bind $w.top.list <Double-Button-1> [list scan-copy %y $curIndexEntry]
1314     wm title $w "Scan $title"
1315         
1316     z39 callback [list scan-response $attr 0 35]
1317     z39.scan numberOfTermsRequested 5
1318     z39.scan preferredPositionInResponse 1
1319     z39.scan scan "${attr} 0"
1320     
1321     show-status Scanning 1 0
1322 }
1323
1324 # Procedure scan-term-h {attr} 
1325 # attr    attribute specification
1326 # This procedure is called whenever a key is released in the entry in the
1327 # scan window (.scan-window). A scan is then initiated with the new contents
1328 # of the entry as the starting term.
1329 proc scan-term-h {attr} {
1330     global busy scanTerm
1331
1332     if {$busy} {
1333         return
1334     }
1335     set w .scan-window
1336     set nScanTerm [$w.top.entry get]
1337     if {$nScanTerm == $scanTerm} {
1338         return
1339     }
1340     set scanTerm $nScanTerm
1341     z39 callback [list scan-response $attr 0 35]
1342     z39.scan numberOfTermsRequested 5
1343     z39.scan preferredPositionInResponse 1
1344     dputs "${attr} \{${scanTerm}\}"
1345     if {![string length $scanTerm]} {
1346         z39.scan scan "${attr} 0"
1347     } else {
1348         z39.scan scan "${attr} \{${scanTerm}\}"
1349     }
1350     show-status Scanning 1 0
1351 }
1352
1353 # Procedure scan-response {attr start toget}
1354 #  attr   attribute specification
1355 #  start  position of first term in the response
1356 #  toget  number of total terms to get
1357 # This procedure handles all scan-responses. $start specifies the list
1358 # entry number of the first incoming term. The $toget indicates the total
1359 # number of terms to be retrieved from the target. The $toget may be
1360 # negative in which case, scan is performed 'backwards' (- $toget is
1361 # the total number of terms in this case). This procedure usually calls
1362 # itself several times in order to get small scan-term-list chunks.
1363 proc scan-response {attr start toget} {
1364     global cancelFlag delayRequest scanTerm scanView
1365
1366     set w .scan-window
1367     dputs "In scan-response"
1368     apduDump
1369     set m [z39.scan numberOfEntriesReturned]
1370     dputs $m
1371     dputs attr=$attr
1372     dputs start=$start
1373     dputs toget=$toget
1374
1375     if {![winfo exists .scan-window]} {
1376         if {$cancelFlag} {
1377             set cancelFlag 0
1378             dputs "Handling cancel"
1379             if {$delayRequest != ""} {
1380                 eval $delayRequest
1381             }
1382             return
1383         }
1384         show-status Ready 0 1
1385         return
1386     }
1387     set nScanTerm [$w.top.entry get]
1388     if {$nScanTerm != $scanTerm} {
1389         z39 callback [list scan-response $attr 0 35]
1390         z39.scan numberOfTermsRequested 5
1391         z39.scan preferredPositionInResponse 1
1392         set scanTerm $nScanTerm
1393         dputs "${attr} \{${scanTerm}\}"
1394         if {![string length $scanTerm]} {
1395             z39.scan scan "${attr} 0"
1396         } else {
1397             z39.scan scan "${attr} \{${scanTerm}\}"
1398         }
1399         show-status Scanning 1 0
1400         return
1401     }
1402     set status [z39.scan scanStatus]
1403     if {$status == 6} {
1404 #        tkerror "Scan fail"
1405         bgerror "Scan fail"
1406         show-status Ready 0 1
1407         set cancelFlag 0
1408         return
1409     }
1410     if {$toget < 0} {
1411         for {set i 0} {$i < $m} {incr i} {
1412             set term [lindex [z39.scan scanLine $i] 1]
1413             set nostr [format " %-6d" [lindex [z39.scan scanLine $i] 2]]
1414             $w.top.list insert $i "$nostr $term"
1415         }
1416         incr scanView $m
1417         $w.top.list yview $scanView
1418     } else {
1419         $w.top.list delete $start end
1420         for {set i 0} {$i < $m} {incr i} {
1421             set term [lindex [z39.scan scanLine $i] 1]
1422             set nostr [format " %-6d" [lindex [z39.scan scanLine $i] 2]]
1423             $w.top.list insert end "$nostr $term"
1424         }
1425     }
1426     if {$cancelFlag} {
1427         dputs "Handling cancel"
1428         set cancelFlag 0
1429         if {$delayRequest != ""} {
1430             eval $delayRequest
1431         }
1432         return
1433     }
1434     set delayRequest {}
1435     if {$toget > 0 && $m > 1 && $m < $toget} {
1436         set ntoget [expr $toget - $m + 1]
1437         dputs ntoget=$ntoget
1438         z39 callback [list scan-response $attr [expr $start + $m - 1] $ntoget]
1439         set q $term
1440         dputs "down continue: $q"
1441         if {$ntoget > 10} {
1442             z39.scan numberOfTermsRequested 10
1443         } else {
1444             z39.scan numberOfTermsRequested $ntoget
1445         }
1446         z39.scan preferredPositionInResponse 1
1447         dputs "${attr} \{$q\}"
1448         z39.scan scan "${attr} \{$q\}"
1449         return
1450     }
1451     if {$toget < 0 && $m > 1 && $m < [expr - $toget]} {
1452         set ntoget [expr - $toget - $m]
1453         dputs ntoget=$ntoget
1454         z39 callback [list scan-response $attr 0 -$ntoget]
1455         set q [string range [$w.top.list get 0] 8 end]
1456         dputs "up continue: $q"
1457         if {$ntoget > 10} {
1458             z39.scan numberOfTermsRequested 10
1459             z39.scan preferredPositionInResponse 11
1460         } else {
1461             z39.scan numberOfTermsRequested $ntoget
1462             z39.scan preferredPositionInResponse [incr ntoget]
1463         }
1464         dputs "${attr} \{$q\}"
1465         z39.scan scan "${attr} \{$q\}"
1466         return
1467     }
1468     show-status Ready 0 1
1469 }
1470
1471 # Procedure scan-down {attr}
1472 #  attr   attribute specification
1473 # This procedure is called when the user hits the Down button the scan
1474 # window. A new scan is initiated with a positive $toget passed to the
1475 # scan-response handler.
1476 proc scan-down {attr} {
1477     global scanView cancelFlag delayRequest
1478
1479     dputs {scan-down}
1480     if {$cancelFlag} {
1481         dputs "cancelFlag"
1482         show-status {Scanning down} 1 0
1483         set delayRequest [list scan-down $attr]
1484         return
1485     }
1486     set delayRequest {} 
1487
1488     set w .scan-window
1489     set scanView [expr $scanView + 5]
1490     set s [$w.top.list size]
1491     if {$scanView > $s} {
1492         z39 callback [list scan-response $attr [expr $s - 1] 25]
1493         set q [string range [$w.top.list get [expr $s - 1]] 8 end]
1494         dputs "down: $q"
1495         z39.scan numberOfTermsRequested 10
1496         z39.scan preferredPositionInResponse 1
1497         show-status Scanning 1 0
1498         dputs "${attr} \{$q\}"
1499         z39.scan scan "${attr} \{$q\}"
1500         return
1501     }
1502     $w.top.list yview $scanView
1503 }
1504
1505 # Procedure scan-up {attr}
1506 #  attr   attribute specification
1507 # This procedure is called when the user hits the Up button the scan
1508 # window. A new scan is initiated with a negative $toget passed to the
1509 # scan-response handler.
1510 proc scan-up {attr} {
1511     global scanView cancelFlag delayRequest
1512
1513     dputs {scan-up}
1514     if {$cancelFlag} {
1515         dputs "cancelFlag"
1516         show-status Scanning 1 0
1517         set delayRequest [list scan-up $attr]
1518         return
1519     }
1520     set delayRequest {} 
1521
1522     set w .scan-window
1523     set scanView [expr $scanView - 5]
1524     if {$scanView < 0} {
1525         z39 callback [list scan-response $attr 0 -25]
1526         set q [string range [$w.top.list get 0] 8 end]
1527         dputs "up: $q"
1528         z39.scan numberOfTermsRequested 10
1529         z39.scan preferredPositionInResponse 11
1530         show-status Scanning 1 0
1531         z39.scan scan "${attr} \{$q\}"
1532         return
1533     }
1534     $w.top.list yview $scanView
1535 }
1536
1537 # Procedure search-response
1538 # This procedure handles search-responses. If the search is successful
1539 # this procedure will try to retrieve a total of 20 records from the target;
1540 # however not more than $presentChunk records at a time. This procedure
1541 # affects the following globals:
1542 #   $setOffset        current record position offset
1543 #   $setMax           total number of records to be retrieved
1544 proc search-response {} {
1545     global setNo setOffset setMax cancelFlag busy delayRequest presentChunk
1546
1547     apduDump
1548     dputs "In search-response"
1549     if {$cancelFlag} {
1550         dputs "Handling cancel"
1551         set cancelFlag 0
1552         if {$delayRequest != ""} {
1553             eval $delayRequest
1554         }
1555         return
1556     }
1557     set setOffset 0
1558     set delayRequest {}
1559     init-title-lines
1560     set setMax [z39.$setNo resultCount]
1561     show-status Ready 0 1
1562     set status [z39.$setNo responseStatus]
1563     if {![string compare [lindex $status 0] NSD]} {
1564         z39.$setNo nextResultSetPosition 0
1565         set code [lindex $status 1]
1566         set msg [lindex $status 2]
1567         set addinfo [lindex $status 3]
1568 #        tkerror "NSD$code: $msg: $addinfo"
1569         bgerror "NSD$code: $msg: $addinfo"
1570         return
1571     }
1572     show-message "${setMax} hits"
1573     if {$setMax == 0} {
1574         return
1575     }
1576     set setOffset 1
1577     show-status Ready 0 1
1578     set l [format "%-4d %7d" $setNo $setMax]
1579     .top.rset.m add command -label $l -command [list recall-set $setNo]
1580     if {$setMax > 20} {
1581         set setMax 20
1582     }
1583     set no [z39.$setNo numberOfRecordsReturned]
1584     dputs "Returned $no records, setOffset $setOffset"
1585     add-title-lines $setNo $no $setOffset
1586     set setOffset [expr $setOffset + $no]
1587
1588     set toGet [expr $setMax - $setOffset + 1]
1589     if {$toGet > 0} {
1590         if {$setOffset == 1} {
1591             set toGet 1
1592         } elseif {$toGet > $presentChunk} {
1593             set toGet $presentChunk
1594         }
1595         z39 callback {present-response}
1596         z39.$setNo present $setOffset $toGet
1597         show-status Retrieving 1 0
1598     }
1599 }
1600
1601 # Procedure present-more {number}
1602 #  number      number of records to be retrieved
1603 # This procedure starts a present-request. The $number variable indicates
1604 # the total number of records to be retrieved. The global $presentChunk
1605 # specifies the number of records to be retrieved at a time. If $number
1606 # is the empty string all remaining records in the result set are 
1607 # retrieved.
1608 proc present-more {number} {
1609     global setNo setOffset setMax busy cancelFlag delayRequest presentChunk
1610
1611     dputs "present-more"
1612     if {$cancelFlag} {
1613         show-status Retrieving 1 0
1614         set delayRequest "present-more $number"
1615         return
1616     }
1617     set delayRequest {}
1618
1619     if {$setNo == 0} {
1620         dputs "setNo=$setNo"
1621     return
1622     }
1623     set setOffset [z39.$setNo nextResultSetPosition]
1624     dputs "setOffest=${setOffset}"
1625     dputs "setNo=${setNo}"
1626     set max [z39.$setNo resultCount]
1627     if {$max < $setOffset} {
1628         dputs "max=$max"
1629         dputs "setOffset=$setOffset"
1630         show-status Ready 0 1
1631         return
1632     }
1633     if {![string length $number]} {
1634         set setMax $max
1635     } else {
1636         incr setMax $number
1637         if {$setMax > $max} {
1638             set setMax $max
1639         }
1640     }
1641     z39 callback {present-response}
1642     
1643     set toGet [expr $setMax - $setOffset + 1]
1644     if {$toGet <= 0} {
1645         return
1646     }
1647     if {$toGet > $presentChunk} {
1648         set toGet $presentChunk
1649     } 
1650     z39.$setNo present $setOffset $toGet
1651     show-status Retrieving 1 0
1652 }
1653
1654 # Procedure init-title-lines 
1655 # Utility that cleans the main record window.
1656 proc init-title-lines {} {
1657     .data.record delete 1.0 end
1658 }
1659
1660 # Procedure recall-set {setno}
1661 #  setno    Set number to recall
1662 proc recall-set {setno} {
1663     add-title-lines $setno 10000 1
1664 }
1665
1666 # Procedure add-title-lines {setno no offset}
1667 #  setno    Set number
1668 #  no       Number of records
1669 #  offset   Starting offset
1670 # This procedure displays the records $offset .. $offset+$no-1 in result
1671 # set $setno in the main record window by using the display format in the
1672 # global $displayFormat
1673 proc add-title-lines {setno no offset} {
1674     global displayFormats displayFormat setNo busy
1675
1676     dputs "add-title-lines offset=${offset} no=${no}"
1677     if {$setno != -1} {
1678         set setNo $setno
1679     } else {
1680         set setno $setNo
1681     }
1682     if {$offset == 1} {
1683         .bot.a.set configure -text $setno
1684         .data.record delete 1.0 end
1685     }
1686     set ffunc [lindex $displayFormats $displayFormat]
1687     dputs "ffunc=$ffunc"
1688     set ffunc "display-$ffunc"
1689     for {set i 0} {$i < $no} {incr i} {
1690         set o [expr $i + $offset]
1691         set type [z39.$setno type $o]
1692         if {![string length $type]} {
1693             dputs "no more at $o"
1694             break
1695         }
1696         .data.record tag bind r$o <Any-Enter> {}
1697         .data.record tag bind r$o <Any-Leave> {}
1698         set insert0 [.data.record index insert]
1699         $ffunc $setno $o .data.record 1
1700         .data.record tag add r$o $insert0 insert
1701         .data.record tag bind r$o <1> [list popup-marc $setno $o 0 0]
1702         update idletasks
1703     }
1704     if {!$busy} {
1705         show-status Ready 0 1
1706     }
1707 }
1708
1709 # Procedure present-response
1710 # Present-response handler. The incoming records are displayed and a new
1711 # present request is performed until all records ($setMax) is returned
1712 # from the target.
1713 proc present-response {} {
1714     global setNo setOffset setMax cancelFlag delayRequest presentChunk
1715
1716     dputs "In present-response"
1717     apduDump
1718     set no [z39.$setNo numberOfRecordsReturned]
1719     dputs "Returned $no records, setOffset $setOffset"
1720     add-title-lines $setNo $no $setOffset
1721     set setOffset [expr $setOffset + $no]
1722     if {$cancelFlag} {
1723         dputs "Handling cancel"
1724         set cancelFlag 0
1725         if {$delayRequest != ""} {
1726             eval $delayRequest
1727         }
1728         return
1729     }
1730     set status [z39.$setNo responseStatus]
1731     if {![string compare [lindex $status 0] NSD]} {
1732         show-status Ready 0 1
1733         set code [lindex $status 1]
1734         set msg [lindex $status 2]
1735         set addinfo [lindex $status 3]
1736 #        tkerror "NSD$code: $msg: $addinfo"
1737         bgerror "NSD$code: $msg: $addinfo"
1738         return
1739     }
1740     if {$no > 0 && $setOffset <= $setMax} {
1741         dputs "present-request from ${setOffset}"
1742         set toGet [expr $setMax - $setOffset + 1]
1743         if {$toGet > $presentChunk} {
1744             set toGet $presentChunk
1745         }
1746         z39.$setNo present $setOffset $toGet
1747     } else {
1748         show-status Ready 0 1
1749     }
1750 }
1751
1752 # Procedure left-cursor {w}
1753 #  w    entry widget
1754 # Tries to move the cursor left in entry window $w
1755 proc left-cursor {w} {
1756     set i [$w index insert]
1757     if {$i > 0} {
1758         incr i -1
1759         $w icursor $i
1760     }
1761     dputs left
1762 }
1763
1764 # Procedure right-cursor {w}
1765 #  w    entry widget
1766 # Tries to move the cursor right in entry window $w
1767 proc right-cursor {w} {
1768     set i [$w index insert]
1769     incr i
1770     dputs right
1771     $w icursor $i
1772 }
1773
1774 # Procedure bind-fields {list returnAction escapeAction}
1775 #  list          list of entry widgets
1776 #  returnAction  return script
1777 #  escapeAction  escape script
1778 # Each widget in list are assigned bindings for <Tab>, <Left>, <Right>,
1779 # <Return> and <Escape>.
1780 proc bind-fields {list returnAction escapeAction} {
1781     set max [expr [llength $list]-1]
1782     for {set i 0} {$i < $max} {incr i} {
1783         bind [lindex $list $i] <Return> $returnAction
1784         bind [lindex $list $i] <Escape> $escapeAction
1785         bind [lindex $list $i] <Tab> [list focus [lindex $list [expr $i+1]]]
1786         bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
1787         bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
1788     }
1789     bind [lindex $list $i] <Return> $returnAction
1790     bind [lindex $list $i] <Escape> $escapeAction
1791     bind [lindex $list $i] <Tab>  [list focus [lindex $list 0]]
1792     bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
1793     bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
1794     focus [lindex $list 0]
1795 }
1796
1797 # Procedure entry-fields {parent list tlist returnAction escapeAction}
1798 #  list          list of frame widgets
1799 #  tlist         list of text to be used as lead of each entry
1800 #  returnAction  return script
1801 #  escapeAction  escape script
1802 # Makes label and entry widgets in each widget in $list.
1803 proc entry-fields {parent list tlist returnAction escapeAction} {
1804     set alist {}
1805     set i 0
1806     foreach field $list {
1807         set label ${parent}.${field}.label
1808         set entry ${parent}.${field}.entry
1809         label $label -text [lindex $tlist $i] -anchor e
1810         entry $entry -width 32 -relief sunken
1811         pack $label -side left
1812         pack $entry -side right
1813         lappend alist $entry
1814         incr i
1815     }
1816     bind-fields $alist $returnAction $escapeAction
1817 }
1818
1819 # Procedure define-target-dialog
1820 # Dialog that asks for new target to be defined.
1821 proc define-target-dialog {} {
1822     set w .target-define
1823
1824     toplevel $w
1825     place-force $w .
1826     top-down-window $w
1827     frame $w.top.target
1828     pack $w.top.target -side top -anchor e -pady 2 
1829     entry-fields $w.top {target} {{Target:}} \
1830             {define-target-action} {destroy .target-define}
1831     top-down-ok-cancel $w {define-target-action} 1
1832 }
1833
1834 # Procedure place-force {window parent}
1835 #  window      new top level widget
1836 #  parent      parent widget used as base
1837 # Sets geometry of $window relative to $parent window.
1838 proc place-force {window parent} {
1839     set g [wm geometry $parent]
1840     set p1 [string first + $g]
1841     set p2 [string last + $g]
1842     set x [expr 40+[string range $g [expr {$p1 + 1}] [expr {$p2 -1}]]]
1843     set y [expr 60+[string range $g [expr {$p2 + 1}] end]]
1844     wm geometry $window +${x}+${y}
1845 }
1846
1847 # Procedure add-database-action {target w}
1848 #  target      target to be defined
1849 #  w           top level widget for the target definition
1850 # Adds the contents of .database-select.top.database.entry to list of
1851 # databases.
1852 proc add-database-action {target w} {
1853     global profile
1854
1855     $w.top.databases.list insert end [.database-select.top.database.entry get]
1856     destroy .database-select
1857 }
1858
1859 # Procedure add-database {target wp}
1860 #  target      target to be defined
1861 #  wp          top level widget for the target definition
1862 # Makes a dialog in which the user enters new database
1863 proc add-database {target wp} {
1864     global profile
1865
1866     set w .database-select
1867     toplevel $w
1868     set oldFocus [focus]
1869     place-force $w $wp
1870     top-down-window $w
1871     frame $w.top.database
1872     pack $w.top.database -side top -anchor e -pady 2
1873        entry-fields $w.top {database} {{Database to add:}} \
1874             [list add-database-action $target $wp] {destroy .database-select}
1875
1876     top-down-ok-cancel $w [list add-database-action $target $wp] 1
1877     focus $oldFocus
1878 }
1879
1880
1881 # Procedure delete-database {target w}
1882 #  target     target to be defined
1883 #  w          top level widget for the target definition
1884 # Asks the user if he/she really wishes to delete a database and removes
1885 # the database from the database-list if requested.
1886 proc delete-database {target w} {
1887     global profile
1888
1889     set l {}
1890     foreach i [$w.top.databases.list curselection] {
1891         set b [$w.top.databases.list get $i]
1892         set l "$l $b"
1893     }
1894     set a [alert "Are you sure you want to remove the database(s)${l}?"]
1895     if {$a} {
1896         foreach i [lsort -decreasing \
1897                 [$w.top.databases.list curselection]] {
1898             $w.top.databases.list delete $i
1899         }
1900     }
1901 }
1902
1903 # Procedure advanced-setup {target b}
1904 #  target     target to be defined
1905 #  b          window number of target top level
1906 # Makes a dialog in which the user may modify/view advanced settings
1907 # of a target definition (profile).
1908 proc advanced-setup {target b} {
1909     global profile profileS
1910
1911     set w .advanced-setup-$b
1912     toplevelG $w
1913     wm title $w "Advanced setup $target"
1914     top-down-window $w
1915      if {![string length $target]} {
1916         set target Default
1917     }
1918     dputs target
1919     
1920     frame $w.top.largeSetLowerBound
1921     frame $w.top.smallSetUpperBound
1922     frame $w.top.mediumSetPresentNumber
1923     frame $w.top.presentChunk
1924     frame $w.top.maximumRecordSize
1925     frame $w.top.preferredMessageSize
1926
1927     pack $w.top.largeSetLowerBound $w.top.smallSetUpperBound \
1928             $w.top.mediumSetPresentNumber $w.top.presentChunk \
1929             $w.top.maximumRecordSize $w.top.preferredMessageSize \
1930             -side top -anchor e -pady 2
1931     
1932     entry-fields $w.top {largeSetLowerBound smallSetUpperBound \
1933             mediumSetPresentNumber presentChunk maximumRecordSize \
1934             preferredMessageSize} \
1935             {{Large Set Lower Bound:} {Small Set Upper Bound:} \
1936             {Medium Set Present Number:} {Present Chunk:} \
1937             {Maximum Record Size:} {Preferred Message Size:}} \
1938             [list advanced-setup-action $target $b] [list destroy $w]
1939
1940     $w.top.largeSetLowerBound.entry configure -textvariable \
1941     profileS($target,largeSetLowerBound)
1942     $w.top.smallSetUpperBound.entry configure -textvariable \
1943     profileS($target,smallSetUpperBound)
1944     $w.top.mediumSetPresentNumber.entry configure -textvariable \
1945     profileS($target,mediumSetPresentNumber)
1946     $w.top.presentChunk.entry configure -textvariable \
1947     profileS($target,presentChunk)
1948     $w.top.maximumRecordSize.entry configure -textvariable \
1949     profileS($target,maximumRecordSize)
1950     $w.top.preferredMessageSize.entry configure -textvariable \
1951     profileS($target,preferredMessageSize)
1952     
1953     bottom-buttons $w [list {Ok} [list advanced-setup-action $target $b] \
1954             {Cancel} [list destroy $w]] 0   
1955 }
1956
1957 # Procedure advanced-setup-action {target b}
1958 #  target     target to be defined
1959 #  b          window number of target top level
1960 # This procedure is called when the user hits Ok in the advanced target
1961 # setup dialog. The temporary result is stored in the $profileS - array.
1962 proc advanced-setup-action {target b} {
1963     set w .advanced-setup-$b
1964     global profileS
1965     
1966     set profileS($target,LSLB) [$w.top.largeSetLowerBound.entry get]
1967     set profileS($target,SSUB) [$w.top.smallSetUpperBound.entry get]
1968     set profileS($target,MSPN) [$w.top.mediumSetPresentNumber.entry get]
1969     set profileS($target,presentChunk) [$w.top.presentChunk.entry get]
1970     set profileS($target,MRS) [$w.top.maximumRecordSize.entry get]
1971     set profileS($target,PMS) [$w.top.preferredMessageSize.entry get]
1972
1973     dputs "advanced-setup-action"
1974     destroy $w
1975 }
1976
1977 # Procedure database-select-action
1978 # Called when the user commits a database select change. See procedure
1979 # database-select.
1980 proc database-select-action {} {
1981     set w .database-select.top
1982     set b {}
1983     foreach indx [$w.databases.list curselection] {
1984         lappend b [$w.databases.list get $indx]
1985     }
1986     if {$b != ""} {
1987         z39 databaseNames $b
1988     }
1989     destroy .database-select
1990 }
1991
1992 # Procedure database-select
1993 # Makes a dialog in which the user may select a database
1994 proc database-select {} {
1995     set w .database-select
1996     global profile hostid
1997
1998     toplevel $w
1999     set oldFocus [focus]
2000     place-force $w .
2001     top-down-window $w
2002
2003     frame $w.top.databases -relief ridge -border 2
2004     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
2005
2006     label $w.top.databases.label -text "List"
2007     listbox $w.top.databases.list -width 20 -height 6 \
2008             -yscrollcommand "$w.top.databases.scroll set"
2009     scrollbar $w.top.databases.scroll -orient vertical -border 1
2010     pack $w.top.databases.label -side top -fill x -padx 2 -pady 2
2011     pack $w.top.databases.list -side left -fill both -expand yes -padx 2 -pady 2
2012     pack $w.top.databases.scroll -side right -fill y -padx 2 -pady 2
2013     $w.top.databases.scroll config -command "$w.top.databases.list yview"
2014
2015     foreach b $profile($hostid,databases) {
2016         $w.top.databases.list insert end $b
2017     }
2018     top-down-ok-cancel $w {database-select-action} 1
2019     focus $oldFocus
2020 }
2021
2022 # Procedure cascase-dblist-select
2023 proc cascade-dblist-select {target db} {
2024     show-target $target $db
2025     z39 databaseNames $db
2026     ready-response-actions $target $db
2027 }
2028
2029 # Procedure cascade-dblist 
2030 # Makes the Service/database list with proper databases for the target
2031 proc cascade-dblist {target base} {
2032     global profile
2033
2034     set w .top.service.m.dblist
2035     $w delete 0 200
2036     if {[info exists profile($target,databases)]} {
2037         foreach db $profile($target,databases) {
2038             $w add command -label $db \
2039             -command [list cascade-dblist-select $target $db]
2040         }
2041     }
2042 }
2043
2044 # Procedure cascade-target-list
2045 # Makes all target/databases available in the Target|Connect
2046 # menu as well as all targets in the Target|Setup menu.
2047 # This procedure is called whenever target definitions occur.
2048 proc cascade-target-list {} {
2049     global profile
2050     
2051     foreach sub [winfo children .top.target.m.clist] {
2052         destroy $sub
2053     }
2054     .top.target.m.clist delete 0 last
2055     foreach nn [lsort [array names profile *,host]] {
2056         if {[string length $profile($nn)]} {
2057             set ll [expr {[string length $nn] - 6}]
2058             set n [string range $nn 0 $ll]
2059             
2060             set nl $profile($n,windowNumber)
2061             if {[info exists profile($n,databases)]} {
2062                 set ndb [llength $profile($n,databases)]
2063             } else {
2064                 set ndb 0
2065             }
2066             if {$ndb > 1} {
2067                 .top.target.m.clist add cascade -label $n \
2068                     -menu .top.target.m.clist.$nl
2069                 irmenu .top.target.m.clist.$nl
2070                 foreach b $profile($n,databases) {
2071                     .top.target.m.clist.$nl add command -label $b \
2072                     -command [list reopen-target $n $b]
2073                 }
2074             } elseif {$ndb == 1} {
2075                 .top.target.m.clist add command -label $n -command \
2076                     [list reopen-target $n [lindex $profile($n,databases) 0]]
2077             } else {
2078                 .top.target.m.clist add command -label $n -command \
2079                     [list reopen-target $n {}]
2080             }
2081         }
2082     }
2083     .top.target.m.slist delete 0 last
2084     foreach nn [lsort [array names profile *,host]] {
2085         set ll [expr {[string length $nn] - 6}]
2086         set n [string range $nn 0 $ll]
2087         .top.target.m.slist add command -label $n -command [list protocol-setup $n]
2088     }
2089 }
2090
2091 # Procedure query-select {i}
2092 #  i       Query type number (integer)
2093 # This procedure is called when the user selects a Query type. The current
2094 # query type information given by the globals $queryButtonsFind and
2095 # $queryInfoFind are affected by this operation.
2096 proc query-select {i} {
2097     global queryButtonsFind queryInfoFind queryButtons queryInfo queryAuto queryAutoOld hostid currentDb profile
2098     
2099     if {$queryAutoOld == 1 && $queryAuto == 0} {
2100         set queryAutoOld $queryAuto
2101         return
2102     }
2103     if {$queryAutoOld == 0 && $queryAuto == 1 && [info exists profile($hostid,AttributeDetails,$currentDb,Bib1Use)] == 0} {
2104         set queryAutoOld $queryAuto
2105         return
2106     }
2107     set queryInfoFind [lindex $queryInfo $i]
2108     set queryButtonsFind [lindex $queryButtons $i]
2109     index-lines .lines 1 $queryButtonsFind $queryInfoFind activate-index
2110 }
2111
2112 # Procedure query-new-action 
2113 # Commits a new query type definition by extending the globals
2114 # $queryTypes, $queryButtons and $queryInfo.
2115 proc query-new-action {} {
2116     global queryTypes queryButtons queryInfo settingsChanged
2117
2118     set settingsChanged 1
2119     lappend queryTypes [.query-new.top.index.entry get]
2120     lappend queryButtons {}
2121     lappend queryInfo {}
2122
2123     destroy .query-new
2124     cascade-query-list
2125 }
2126
2127 # Procedure query-new
2128 # Makes a dialog in which the user is requested to enter the name of a
2129 # new query type.
2130 proc query-new {} {
2131     set w .query-new
2132
2133     toplevel $w
2134     set oldFocus [focus]
2135     place-force $w .
2136     top-down-window $w
2137     frame $w.top.index
2138     pack $w.top.index -side top -anchor e -pady 2 
2139     entry-fields $w.top index {{Query Name:}} \
2140             query-new-action {destroy .query-new}
2141     top-down-ok-cancel $w query-new-action 1
2142     focus $oldFocus
2143 }
2144
2145 # Procedure query-delete-action {queryNo}
2146 #  queryNo     query type number (integer)
2147 # Procedure that deletes the query type specified by $queryNo.
2148 proc query-delete-action {queryNo} {
2149     global queryTypes queryButtons queryInfo settingsChanged
2150
2151     set settingsChanged 1
2152
2153     set queryTypes [lreplace $queryTypes $queryNo $queryNo]
2154     set queryButtons [lreplace $queryButtons $queryNo $queryNo]
2155     set queryInfo [lreplace $queryInfo $queryNo $queryNo]
2156     destroy .query-delete
2157     cascade-query-list
2158 }
2159
2160 # Procedure query-delete {queryNo}
2161 #  queryNo     query type number (integer)
2162 # Asks if the user really want to delete a given query type; calls
2163 # query-delete-action if 'yes'.
2164 proc query-delete {queryNo} {
2165     global queryTypes
2166
2167     set w .query-delete
2168
2169     toplevel $w
2170     place-force $w .
2171     top-down-window $w
2172     set n [lindex $queryTypes $queryNo]
2173
2174     label $w.top.warning -bitmap warning
2175     message $w.top.quest -text "Are you sure you want to delete the \
2176         query type $n ?"  -aspect 300
2177     pack $w.top.warning $w.top.quest -side left -expand yes -padx 10 -pady 5
2178     bottom-buttons $w [list {Ok} [list query-delete-action $queryNo] \
2179         {Cancel} [list destroy $w]] 1
2180 }
2181
2182 # Procedure cascade-query-list
2183 # Updates the entries below Options|Query to list all query types.
2184 proc cascade-query-list {} {
2185     global queryTypes hostid queryAuto attributeTypes
2186     set w .top.options.m.query
2187     set i 0
2188     $w.clist delete 0 last
2189     foreach n $queryTypes {
2190         if {$n == "Auto"} {
2191             $w.clist add check -label $n -variable queryAuto -command [list query-select $i]
2192         } else {
2193             $w.clist add command -label $n -command [list query-select $i]      
2194         }
2195         incr i
2196     }
2197     set i 0
2198     $w.slist delete 0 last
2199     foreach n $queryTypes {
2200         if {$n == "Auto"} {
2201                if {$hostid == "Default"} {
2202                 $w.slist add command -label $n -state disabled -command [list query-setup $i]
2203             } else {
2204                 $w.slist add command -label $n -command [list query-setup $i]
2205             }
2206         } else {
2207             $w.slist add command -label $n -command [list query-setup $i]
2208         }
2209         incr i
2210     }
2211     set i 0
2212     $w.tlist delete 0 last
2213     foreach n $attributeTypes {
2214         global attribute$n
2215         $w.tlist add check -label $n -variable attribute$n -command [list attribute-select $i]
2216         incr i
2217     }
2218     set i 0
2219     $w.dlist delete 0 last
2220     foreach n $queryTypes {
2221         $w.dlist add command -label $n -command [list query-delete $i]
2222         incr i
2223     }
2224 }
2225
2226 # Procedure save-geometry
2227 # This procedure saves the per-user related settings in ~/.clientrc.tcl.
2228 # The geometry information stored in the global array $windowGeometry is
2229 # saved. Also a few other user settings, such as current display format, are
2230 # saved.
2231 proc save-geometry {} {
2232     global windowGeometry hotTargets textWrap displayFormat popupMarcdf \
2233         recordSyntax elementSetNames hostid
2234
2235     set windowGeometry(.) [wm geometry .]
2236
2237     if {[catch {set f [open ~/.clientrc.tcl w]}]} {
2238         return
2239     } 
2240     if {$hostid != "Default"} {
2241         puts $f "set hostid [list $hostid]"
2242         set b [z39 databaseNames]
2243         puts $f "set hostbase [list $b]"
2244     }
2245     puts $f "set hotTargets [list $hotTargets]"
2246     puts $f "set textWrap $textWrap"
2247     puts $f "set displayFormat $displayFormat"
2248     puts $f "set popupMarcdf $popupMarcdf"
2249     puts $f "set recordSyntax $recordSyntax"
2250     puts $f "set elementSetNames $elementSetNames"
2251     foreach n [array names windowGeometry] {
2252         puts -nonewline $f "set [list windowGeometry($n)] "
2253         puts $f [list $windowGeometry($n)]
2254     }
2255     close $f
2256 }
2257
2258 # Procedure save-settings
2259 # This procedure saves the per-host related settings irtdb.tcl which
2260 # is normally kept in the directory /usr/local/lib/irtcl.
2261 # All query types and target defintion profiles are saved.
2262 proc save-settings {} {
2263     global profile libdir settingsChanged queryTypes queryButtons queryInfo queryAuto attributeTypes attributeTypeSelected
2264
2265     if {[file writable [file join $libdir irtdb.tcl]]} {
2266         set f [open [file join $libdir irtdb.tcl] w]
2267     } else {
2268         set f [open "irtdb.tcl" w]
2269     }
2270     puts $f "# Setup file"
2271     foreach n [lsort [array names profile]] {
2272         puts $f "set [list profile($n)] [list $profile($n)]"
2273     }
2274     puts $f "set attributeTypes [list $attributeTypes]"
2275     puts $f "set attributeTypeSelected [list $attributeTypeSelected]"
2276     puts $f "set queryTypes [list $queryTypes]"
2277     puts $f "set queryButtons [list $queryButtons]"
2278     puts $f "set queryInfo [list $queryInfo]"
2279     puts $f "set queryAuto [list $queryAuto]"
2280     close $f
2281     set settingsChanged 0
2282 }
2283
2284 # Procedure alert {ask}
2285 #  ask    prompt string
2286 # Makes a grabbed dialog in which the user is requested to answer
2287 # "Ok" or "Cancel". This procedure returns 1 if the user hits "Ok"; 0
2288 # otherwise.
2289 proc alert {ask} {
2290     set w .alert
2291
2292     global alertAnswer font
2293
2294     toplevel $w
2295     set oldFocus [focus]
2296     place-force $w .
2297     top-down-window $w
2298
2299     label $w.top.warning -bitmap warning
2300     message $w.top.message -text $ask -aspect 300 -font $font(b,normal)
2301     pack $w.top.warning $w.top.message -side left -pady 5 -padx 10 -expand yes
2302   
2303     set alertAnswer 0
2304     top-down-ok-cancel $w {alert-action} 1
2305     focus $oldFocus
2306     return $alertAnswer
2307 }
2308
2309 # Procedure alert-action
2310 # Called when the user hits "Ok" in the .alert-window.
2311 proc alert-action {} {
2312     global alertAnswer
2313     set alertAnswer 1
2314     destroy .alert
2315 }
2316
2317 # Procedure exit-action
2318 # This procedure is called if the user exists the application
2319 proc exit-action {} {
2320     global settingsChanged
2321
2322     if {$settingsChanged} {
2323         save-settings
2324     }
2325     save-geometry
2326     exit 0
2327 }
2328
2329 # Procedure listbuttonaction {w name h user i}
2330 #  w       menubutton widget
2331 #  name    name information
2332 #  h       handler to be invoked
2333 #  user    user information to be passed to handler $h
2334 #  i       index passed as second argument to handler $h
2335 # Utility function to emulate a listbutton. Called when the user
2336 # Modifies the listbutton. See procedure listbuttonx.
2337 proc listbuttonaction {w name h user i} {
2338     $w configure -text [lindex $name 0]
2339     $h [lindex $name 1] $user $i
2340 }
2341
2342 # Procedure listbuttonx {button no names handle user}
2343 #  button  menubutton widget
2344 #  no      initial value index (integer)
2345 #  names   list of name entries. The first entry in each name
2346 #          entry is the actual name
2347 #  handle  user function to be called when the listbutton changes
2348 #          its value
2349 #  user    user argument to the $handle function
2350 # Makes an extended listbutton.
2351 proc listbuttonx {button no names handle user} {
2352     set width 10
2353     foreach name $names {
2354         set buttonName [lindex $name 0]
2355         if {[string length $buttonName] > $width} {
2356             set width [string length $buttonName]
2357         }
2358     } 
2359     if {[winfo exists $button]} {
2360         $button configure -width $width -text [lindex [lindex $names $no] 0]
2361         ${button}.m delete 0 last
2362     } else {
2363         menubutton $button -text [lindex [lindex $names $no] 0] \
2364             -width $width -menu ${button}.m -relief raised -border 1
2365         irmenu ${button}.m
2366         ${button}.m configure -tearoff off
2367     }
2368     set i 0
2369     foreach name $names {
2370         ${button}.m add command -label [lindex $name 0] \
2371             -command [list listbuttonaction ${button} $name $handle $user $i]
2372         incr i
2373     }
2374 }
2375
2376 # Procedure listbutton {button no names}
2377 #  button  menubutton widget
2378 #  no      initial value index (integer)
2379 #  names   list of possible values.
2380 # Makes a listbutton. The functionality is emulated by the use menubutton-
2381 # and menu widgets.
2382 proc listbutton {button no names} {
2383     menubutton $button -text [lindex $names $no] -width 10 -menu ${button}.m \
2384             -relief raised -border 1
2385     irmenu ${button}.m
2386     ${button}.m configure -tearoff off
2387     foreach name $names {
2388         ${button}.m add command -label $name \
2389                 -command [list ${button} configure -text $name]
2390     }
2391 }
2392
2393 # Procedure listbuttonv-action {button var names i}
2394 #  button   menubutton widget
2395 #  var      global variable to be affected
2396 #  names    list of possible names and values
2397 # This procedure is called when the user alters a menu created by the
2398 # listbuttonv procedure. The global variable $var is updated.
2399 proc listbuttonv-action {button var names i} {
2400     global $var
2401
2402     set $var [lindex $names [expr {$i+1}]]
2403     $button configure -text [lindex $names $i]
2404 }
2405
2406 # Procedure listbuttonv {button var names}
2407 #  button   menubutton widget
2408 #  var      global variable to be affected
2409 #  names    List of name/value pairs, i.e. {n1 v1 n2 v2 ...}.
2410 # This procedure emulates a listbutton by means of menu/menubutton widgets.
2411 # The global variable $var is automatically updated and set to one of the
2412 # values v1, v2, ...
2413 proc listbuttonv {button var names} {
2414     global $var
2415
2416     set n "-"
2417     set val [set $var]
2418     set l [llength $names]
2419     for {set i 1} {$i < $l} {incr i 2} {
2420         if {$val == [lindex $names $i]} {
2421             incr i -1
2422             set n [lindex $names $i]
2423             break
2424         }
2425     }
2426     if {[winfo exists $button]} {
2427         $button configure -text $n
2428         return
2429     }
2430     menubutton $button -text $n -menu ${button}.m -relief raised -border 1
2431     irmenu ${button}.m
2432     ${button}.m configure -tearoff off
2433     for {set i 0} {$i < $l} {incr i 2} {
2434         ${button}.m add command -label [lindex $names $i] \
2435                 -command [list listbuttonv-action $button $var $names $i]
2436     }
2437 }
2438
2439 # Procedure query-add-index-action {queryNo}
2440 #  queryNo       query type number (integer)
2441 # Handler that makes a new query index.
2442 proc query-add-index-action {queryNo} {
2443     set w .query-setup
2444
2445     global queryInfoTmp queryButtonsTmp
2446
2447     set newI [.query-add-index.top.index.entry get]
2448     lappend queryInfoTmp [list $newI {}]
2449     $w.top.index.list insert end $newI
2450     destroy .query-add-index
2451     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
2452 }
2453
2454 # Procedure query-add-line
2455 #  queryNo      query type number (integer)
2456 # Handler that adds new query line.
2457 proc query-add-line {queryNo} {
2458     set w .query-setup
2459
2460     global queryInfoTmp queryButtonsTmp
2461
2462     lappend queryButtonsTmp {I 0}
2463
2464     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
2465 }
2466
2467 # Procedure query-del-line
2468 #  queryNo      query type number (integer)
2469 # Handler that removes query line.
2470 proc query-del-line {queryNo} {
2471     set w .query-setup
2472
2473     global queryInfoTmp queryButtonsTmp
2474
2475     set l [llength $queryButtonsTmp]
2476     if {$l <= 0} {
2477         return
2478     }
2479     incr l -1
2480     set queryButtonsTmp [lreplace $queryButtonsTmp $l $l]
2481     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
2482 }
2483
2484 # Procedure query-add-index
2485 #  queryNo      query type number (integer)
2486 # Handler that adds new query index.
2487 proc query-add-index {queryNo} {
2488     set w .query-add-index
2489
2490     toplevel $w
2491     set oldFocus [focus]
2492     place-force $w .query-setup
2493     top-down-window $w
2494     frame $w.top.index
2495     pack $w.top.index -side top -anchor e -pady 2 
2496     entry-fields $w.top {index} {{Index Name:}} \
2497             [list query-add-index-action $queryNo] [list destroy $w]
2498     top-down-ok-cancel $w [list query-add-index-action $queryNo] 1
2499     focus $oldFocus
2500 }
2501
2502 # Procedure query-setup-action
2503 #  queryNo      query type number (integer)
2504 # Handler that updates the query information database stored in the
2505 # globals $queryInfo and $queryButtons. This procedure is executed when
2506 # the user commits the query setup changes by pressing button "Ok".
2507 proc query-setup-action {queryNo} {
2508     global queryButtons queryInfo queryButtonsTmp queryInfoTmp queryButtonsFind \
2509         queryInfoFind settingsChanged hostid currentDb profile
2510     set settingsChanged 1
2511     set queryInfo [lreplace $queryInfo $queryNo $queryNo $queryInfoTmp]
2512     set queryButtons [lreplace $queryButtons $queryNo $queryNo $queryButtonsTmp]
2513     if {[info exists profile($hostid,AttributeDetails,$currentDb,Bib1Use)]} {
2514         set profile($hostid,queryButtons,$currentDb) $queryButtonsTmp
2515     }
2516     set queryInfoFind $queryInfoTmp
2517     set queryButtonsFind $queryButtonsTmp
2518     destroy .query-setup
2519     index-lines .lines 1 $queryButtonsFind $queryInfoFind activate-index
2520 }
2521
2522 #This procedure handles selection of what attribute set the user wants to use for searching.
2523 #queryNo    index in the attributeTypes list (also the menu item number in Query|Type).
2524 proc attribute-select {queryNo} {
2525     global attributeTypes attributeTypeSelected
2526     set attributeTypeSelected [lindex $attributeTypes $queryNo]
2527     foreach type $attributeTypes {
2528         global attribute[set type]
2529         set attribute$type 0
2530     }
2531     set attribute[lindex $attributeTypes $queryNo] 1
2532 }
2533
2534 #proc changeQueryButtons {target base}
2535 #target        target name
2536 #base        database name
2537 #Substitutes the third element (the Auto element) in queryButtons with 
2538 #profile(target,queryButtons,base). The third element in queryInfo is also substituted with
2539 #profile(target,AttributeDetails,base,Bib1Use)
2540 proc changeQueryButtons {target base} {
2541     source bib-1.tcl
2542     global profile queryButtons queryInfo
2543     if {[info exists profile($target,queryButtons,$base)]} {
2544         set queryButtons [lreplace $queryButtons 2 2 $profile($target,queryButtons,$base)]
2545         foreach tag $profile($target,AttributeDetails,$base,Bib1Use) {
2546             if {$tag < 1037} {
2547                 lappend tempList [list $bib1($tag) 1=$tag]
2548             }
2549         }
2550         set queryInfo [lreplace $queryInfo 2 2 $tempList]
2551     }
2552 }
2553
2554 # Procedure activate-e-index {value no i}
2555 #   value   menu name
2556 #   no      query index number
2557 #   i       menu index (integer)
2558 # Procedure called when listbutton is activated in the query type edit
2559 # window. The global $queryButtonsTmp is updated in this operation.
2560 proc activate-e-index {value no i} {
2561     global queryButtonsTmp queryIndexTmp
2562     set queryButtonsTmp [lreplace $queryButtonsTmp $no $no [list I $i]]
2563     dputs $queryButtonsTmp
2564     set queryIndexTmp $i
2565 }
2566
2567 # Procedure activate-index {value no i}
2568 #   value   menu name
2569 #   no      query index number
2570 #   i       menu index (integer)
2571 # Procedure called when listbutton is activated in the main query 
2572 # window. The global $queryButtonsFind is updated in this operation.
2573 proc activate-index {value no i} {
2574     global queryButtonsFind
2575
2576     set queryButtonsFind [lreplace $queryButtonsFind $no $no [list I $i]]
2577     dputs "queryButtonsFind $queryButtonsFind"
2578 }
2579
2580 # Procedure update-attr
2581 # This procedure creates listbuttons for all bib-1 attributes except
2582 # the use-attribute in the .index-setup window.
2583 # The globals $relationTmpValue, $positionTmpValue, $structureTmpValue,
2584 # $truncationTmpValue and $completenessTmpValue are maintainted by the
2585 # listbuttons.
2586 proc update-attr {} {
2587     set w .index-setup
2588     listbuttonv $w.top.relation.b relationTmpValue\
2589             {{None} 0 {Less than} 1 {Greater than or equal} 2 {Equal} 3 \
2590             {Greater than or equal} 4 {Greater than} 5 {Not equal} 6 \
2591             {Phonetic} 100 {Stem} 101 {Relevance} 102 {AlwaysMatches} 103}
2592     listbuttonv $w.top.position.b positionTmpValue {{None} 0 \
2593             {First in field} 1 {First in subfield} 2 {Any position in field} 3}
2594     listbuttonv $w.top.structure.b structureTmpValue {{None} 0 {Phrase} 1 \
2595             {Word} 2 {Key} 3 {Year} 4 {Date (norm)} 5 {Word list}  6 \
2596             {Date (un-norm)} 100 {Name (norm)} 101 {Date (un-norm)} 102 \
2597             {Structure} 103 {urx} 104 {free-form} 105 {doc-text} 106 \
2598             {local-number} 107 {string} 108 {numeric string} 109}
2599     listbuttonv $w.top.truncation.b truncationTmpValue {{Auto} 0 {Right} 1 \
2600             {Left} 2 {Left and right} 3 {No truncation} 100 \
2601             {Process #} 101 {Re-1} 102 {Re-2} 103}
2602     listbuttonv $w.top.completeness.b completenessTmpValue {{None} 0 \
2603             {Incomplete subfield} 1 {Complete subfield} 2 {Complete field} 3}
2604 }
2605
2606 # Procedure use-attr {init}
2607 #  init      init flag
2608 # This procedure creates a listbox with several Bib-1 use attributes.
2609 # If $init is 1 the listbox is created with the attributes. If $init
2610 # is 0 the current selection of the listbox is read and the global
2611 # $useTmpValue is set to the current use-value.
2612 proc use-attr {init} {
2613     global useTmpValue
2614     source bib-1.tcl
2615     set attr {
2616         {None}                           0
2617         {Personal name}                  1 
2618         {Corporate name}                 2 
2619         {Conference name}                3 
2620         {Title}                          4 
2621         {Title-series}                   5 
2622         {Title-uniform}                  6 
2623         {ISBN}                           7 
2624         {ISSN}                           8 
2625         {LC card number}                 9 
2626         {BNB card number}                10
2627         {BGF(sic) number}                11 
2628         {Local number}                   12 
2629         {Dewey classification}           13 
2630         {UDC classification}             14 
2631         {Bliss classification}           15 
2632         {LC call number}                 16 
2633         {NLM call number}                17 
2634         {NAL call number}                18 
2635         {MOS call number}                19 
2636         {Local classification}           20 
2637         {Subject heading}                21 
2638         {Subject-RAMEAU}                 22 
2639         {BDI-index-subject}              23 
2640         {INSPEC-subject}                 24 
2641         {MESH-subject}                   25 
2642         {PA-subject}                     26 
2643         {LC-subject-heading}             27 
2644         {RVM-subject-heading}            28 
2645         {Local subject index}            29 
2646         {Date}                           30 
2647         {Date of publication}            31 
2648         {Date of acquisition}            32 
2649         {Title-key}                      33 
2650         {Title-collective}               34 
2651         {Title-parallel}                 35 
2652         {Title-cover}                    36 
2653         {Title-added-title-page}         37 
2654         {Title-caption}                  38 
2655         {Title-running}                  39 
2656         {Title-spine}                    40 
2657         {Title-other-variant}            41 
2658         {Title-former}                   42 
2659         {Title-abbreviated}              43 
2660         {Title-expanded}                 44 
2661         {Subject-PRECIS}                 45 
2662         {Subject-RSWK}                   46 
2663         {Subject-subdivision}            47 
2664         {Number-natl-bibliography}       48 
2665         {Number-legal-deposit}           49 
2666         {Number-govt-publication}        50 
2667         {Number-publisher-for-music}     51 
2668         {Number-DB}                      52 
2669         {Number-local-call}              53 
2670         {Code-language}                  54 
2671         {Code-geographic-area}           55 
2672         {Code-institution}               56 
2673         {Name and title}                 57 
2674         {Name-geographic}                58 
2675         {Place-publication}              59 
2676         {CODEN}                          60 
2677         {Microform-generation}           61 
2678         {Abstract}                       62 
2679         {Note}                           63 
2680         {Author-title}                 1000 
2681         {Record type}                  1001 
2682         {Name}                         1002 
2683         {Author}                       1003 
2684         {Author-name-personal}         1004 
2685         {Author-name-corporate}        1005 
2686         {Author-name-conference}       1006 
2687         {Identifier-standard}          1007 
2688         {Subject-LC-children's}        1008 
2689         {Subject-name-personal}        1009 
2690         {Body of text}                 1010 
2691         {Date/time added to database}  1011 
2692         {Date/time last modified}      1012 
2693         {Authority/format identifier}  1013 
2694         {Concept-text}                 1014 
2695         {Concept-reference}            1015 
2696         {Any}                          1016 
2697         {Server choice}                1017 
2698         {Publisher}                    1018 
2699         {Record source}                1019 
2700         {Editor}                       1020 
2701         {Bib-level}                    1021 
2702         {Geographic class}             1022 
2703         {Indexed by}                   1023 
2704         {Map scale}                    1024 
2705         {Music key}                    1025 
2706         {Related periodical}           1026 
2707         {Report number}                1027 
2708         {Stock number}                 1028 
2709         {Thematic number}              1030 
2710         {Material type}                1031 
2711         {Doc ID}                       1032 
2712         {Host item}                    1033 
2713         {Content type}                 1034 
2714         {Anywhere}                     1035 
2715     }
2716     set w .index-setup
2717
2718     if {$init} {
2719         set s 0
2720         set lno 0
2721         for {set i 0} {$i < 1037} {incr i} {
2722             $w.top.use.list insert end $bib1($i)
2723             incr i
2724             if {$useTmpValue == $bib1($i)} {
2725                 set s $lno
2726             }
2727             if {$i == 63} {
2728                 set i 1000
2729             }
2730             incr lno
2731         }
2732         $w.top.use.list selection clear 0 end
2733         $w.top.use.list selection set $s $s
2734         incr s -3
2735         if {$s < 0} {
2736             set s 0
2737         }
2738         $w.top.use.list yview $s
2739     } else {
2740         set lno [lindex [$w.top.use.list curselection] 0]
2741         set i [expr {$lno+$lno+1}]
2742         set useTmpValue $bib1($i)
2743         dputs "useTmpValue=$useTmpValue"
2744     }
2745 }
2746
2747 # Procedure index-setup-action {oldAttr queryNo indexNo}
2748 #  oldAttr     original attributes (?)
2749 #  queryNo     query number
2750 #  indexNo     index number
2751 # Commits setup of a query index. The mapping from the index to 
2752 # the Bib-1 attributes are handled by this function.
2753 proc index-setup-action {oldAttr queryNo indexNo} {
2754     set attr [lindex $oldAttr 0]
2755
2756     global useTmpValue relationTmpValue structureTmpValue truncationTmpValue \
2757         completenessTmpValue positionTmpValue queryInfoTmp
2758
2759     use-attr 0
2760
2761     dputs "index-setup-action"
2762     dputs "queryNo $queryNo"
2763     dputs "indexNo $indexNo"
2764     if {$useTmpValue > 0} {
2765         lappend attr "1=$useTmpValue"
2766     }
2767     if {$relationTmpValue > 0} {
2768         lappend attr "2=$relationTmpValue"
2769     }
2770     if {$positionTmpValue > 0} {
2771         lappend attr "3=$positionTmpValue"
2772     }
2773     if {$structureTmpValue > 0} {
2774         lappend attr "4=$structureTmpValue"
2775     }
2776     if {$truncationTmpValue > 0} {
2777         lappend attr "5=$truncationTmpValue"
2778     }
2779     if {$completenessTmpValue > 0} {
2780         lappend attr "6=$completenessTmpValue"
2781     }
2782     dputs "new attr $attr"
2783     set queryInfoTmp [lreplace $queryInfoTmp $indexNo $indexNo $attr]
2784     destroy .index-setup
2785 }
2786
2787 # Procedure index-setup {attr queryNo indexNo}
2788 #  attr        original attributes
2789 #  queryNo     query number
2790 #  indexNo     index number
2791 # Makes a window with settings of a given query index which the user
2792 # may inspect/modify.
2793 proc index-setup {attr queryNo indexNo} {
2794     set w .index-setup
2795
2796     global relationTmpValue structureTmpValue truncationTmpValue \
2797         completenessTmpValue positionTmpValue useTmpValue
2798     set relationTmpValue 0
2799     set truncationTmpValue 0
2800     set structureTmpValue 0
2801     set positionTmpValue 0
2802     set completenessTmpValue 0
2803     set useTmpValue 0
2804
2805     catch {destroy $w}
2806     toplevelG $w
2807
2808     set n [lindex $attr 0]
2809     wm title $w "Index setup $n"
2810
2811     top-down-window $w
2812
2813     set len [llength $attr]
2814     for {set i 1} {$i < $len} {incr i} {
2815         set q [lindex $attr $i]
2816         set l [string first = $q]
2817         if {$l > 0} {
2818             set t [string range $q 0 [expr {$l - 1}]]
2819             set v [string range $q [expr {$l + 1}] end]
2820             switch $t {
2821                 1
2822                 { set useTmpValue $v }
2823                 2
2824                 { set relationTmpValue $v }
2825                 3
2826                 { set positionTmpValue $v }
2827                 4
2828                 { set structureTmpValue $v }
2829                 5
2830                 { set truncationTmpValue $v }
2831                 6
2832                 { set completenessTmpValue $v }
2833             }
2834         }
2835     }
2836
2837     frame $w.top.use -relief ridge -border 2
2838     frame $w.top.relation -relief ridge -border 2
2839     frame $w.top.position -relief ridge -border 2
2840     frame $w.top.structure -relief ridge -border 2
2841     frame $w.top.truncation -relief ridge -border 2
2842     frame $w.top.completeness -relief ridge -border 2
2843
2844     update-attr
2845
2846     # Use Attributes
2847
2848     pack $w.top.use -side left -pady 6 -padx 6 -fill y
2849
2850     label $w.top.use.label -text "Use"
2851     listbox $w.top.use.list -width 26 -yscrollcommand "$w.top.use.scroll set"
2852     scrollbar $w.top.use.scroll -orient vertical -border 1
2853     pack $w.top.use.label -side top -fill x -padx 2 -pady 2
2854     pack $w.top.use.list -side left -fill both -expand yes -padx 2 -pady 2
2855     pack $w.top.use.scroll -side right -fill y -padx 2 -pady 2
2856     $w.top.use.scroll config -command "$w.top.use.list yview"
2857
2858     use-attr 1
2859
2860     # Relation Attributes
2861
2862     pack $w.top.relation -pady 6 -padx 6 -side top
2863     label $w.top.relation.label -text "Relation" -width 18
2864     
2865     pack $w.top.relation.label $w.top.relation.b -fill x 
2866
2867     # Position Attributes
2868
2869     pack $w.top.position -pady 6 -padx 6 -side top
2870     label $w.top.position.label -text "Position" -width 18
2871
2872     pack $w.top.position.label $w.top.position.b -fill x
2873
2874     # Structure Attributes
2875
2876     pack $w.top.structure -pady 6 -padx 6 -side top
2877     label $w.top.structure.label -text "Structure" -width 18
2878
2879     pack $w.top.structure.label $w.top.structure.b -fill x
2880
2881     # Truncation Attributes
2882
2883     pack $w.top.truncation -pady 6 -padx 6 -side top
2884     label $w.top.truncation.label -text "Truncation" -width 18
2885
2886     pack $w.top.truncation.label $w.top.truncation.b -fill x
2887
2888     # Completeness Attributes
2889
2890     pack $w.top.completeness -pady 6 -padx 6 -side top
2891     label $w.top.completeness.label -text "Completeness" -width 18
2892
2893     pack $w.top.completeness.label $w.top.completeness.b -fill x
2894
2895     # Ok-cancel
2896     bottom-buttons $w [list \
2897         {Ok} [list index-setup-action $attr $queryNo $indexNo] \
2898         {Cancel} [list destroy $w]] 0
2899
2900 }
2901
2902 # Procedure query-edit-index {queryNo}
2903 #  queryNo     query number
2904 # Determines if a selection of an index is active. If one is selected
2905 # the index-setup dialog is started.
2906 proc query-edit-index {queryNo} {
2907     global queryInfoTmp
2908     set w .query-setup
2909
2910     set i [lindex [$w.top.index.list curselection] 0]
2911     if {![string length $i]} {
2912         return
2913     }
2914     set attr [lindex $queryInfoTmp $i]
2915     dputs "Editing no $i $attr"
2916     index-setup $attr $queryNo $i
2917 }
2918
2919 # Procedure query-delete-index {queryNo}
2920 #  queryNo     query number
2921 # Determines if a selection of an index is active. If one is selected
2922 # the index is deleted.
2923 proc query-delete-index {queryNo} {
2924     global queryInfoTmp queryButtonsTmp
2925     set w .query-setup
2926
2927     set i [lindex [$w.top.index.list curselection] 0]
2928     if {![string length $i]} {
2929         return
2930     }
2931     set queryInfoTmp [lreplace $queryInfoTmp $i $i]
2932     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
2933     $w.top.index.list delete $i
2934 }
2935     
2936 # Procedure query-setup {queryNo}
2937 #  queryNo     query number
2938 # Makes a dialog in which a query type an be customized.
2939 proc query-setup {queryNo} {
2940     set w .query-setup
2941
2942     global queryTypes queryButtons queryInfo queryButtonsTmp queryInfoTmp queryIndexTmp
2943     
2944     set queryIndexTmp 0
2945     set queryName [lindex $queryTypes $queryNo]
2946     set queryInfoTmp [lindex $queryInfo $queryNo]
2947     set queryButtonsTmp [lindex $queryButtons $queryNo]
2948
2949     toplevelG $w
2950
2951     wm minsize $w 0 0
2952     wm title $w "Query setup $queryName"
2953
2954     top-down-window $w
2955
2956     frame $w.top.lines -relief ridge -border 2
2957     pack $w.top.lines -side left -pady 6 -padx 6 -fill y
2958
2959     # Index Lines
2960
2961     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
2962
2963     button $w.top.lines.add -text "Add" -command [list query-add-line $queryNo]
2964     button $w.top.lines.del -text "Remove" -command [list query-del-line $queryNo]
2965     pack $w.top.lines.del -fill x -side bottom
2966     pack $w.top.lines.add -fill x -pady 10 -side bottom
2967
2968     # Indexes
2969
2970     frame $w.top.index -relief ridge -border 2
2971     pack $w.top.index -pady 6 -padx 6 -side right -fill y
2972
2973     listbox $w.top.index.list -yscrollcommand [list $w.top.index.scroll set]
2974     scrollbar $w.top.index.scroll -orient vertical -border 1 \
2975         -command [list $w.top.index.list yview]
2976     bind $w.top.index.list <Double-1> [list query-edit-index $queryNo]
2977
2978     pack $w.top.index.list -side left -fill both -expand yes -padx 2 -pady 2
2979     pack $w.top.index.scroll -side right -fill y -padx 2 -pady 2
2980
2981     $w.top.index.list selection clear 0 end
2982     $w.top.index.list selection set 0 0
2983     foreach x $queryInfoTmp {
2984     $w.top.index.list insert end [lindex $x 0]
2985     }
2986
2987     # Bottom
2988     bottom-buttons $w [list \
2989             Ok [list query-setup-action $queryNo] \
2990             Add [list query-add-index $queryNo] \
2991             Edit [list query-edit-index $queryNo] \
2992             Delete [list query-delete-index $queryNo] \
2993             Cancel [list destroy $w]] 0
2994 }
2995
2996 # Procedure index-clear
2997 # Handler that clears the search entry fields.
2998 proc index-clear {} {
2999     global queryButtonsFind
3000
3001     set i 0
3002     foreach b $queryButtonsFind {
3003         .lines.$i.e delete 0 end
3004         incr i
3005     }
3006 }
3007
3008 # Procedure index-query
3009 # The purpose of this function is to read the user's query and convert
3010 # it to the prefix query that IrTcl/YAZ uses to represent an RPN query.
3011 # Each entry in a search fields takes the form
3012 #    [relOp][?]term[?]
3013 #  Here, relOp is an optional relational operator and one of:
3014 #      >  < >= <=  <>
3015 #    which sets the Bib-1 relation to greater-than, less-than, etc.
3016 #  The ? (question-mark) is also optional. A (?) on left-side indicates
3017 #    left truncation; (?) on right-side indicates right-truncation; (?)
3018 #    on both sides indicates both-left-and-right truncation.
3019 proc index-query {} {
3020     global queryButtonsFind queryInfoFind attributeTypeSelected
3021
3022     set i 0
3023     set qs {}
3024
3025     foreach b $queryButtonsFind {
3026         set term [string trim [.lines.$i.e get]]
3027         if {$term != ""} {
3028             set attr [lrange [lindex $queryInfoFind [lindex $b 1]] 1 end]
3029
3030             set relation ""
3031             set len [string length $term]
3032             incr len -1
3033
3034             if {$len > 1} {
3035                 if {[string index $term 0] == ">"} {
3036                     if {[string index $term 1] == "=" } {
3037                         set term [string trim [string range $term 2 $len]]
3038                         set relation 4
3039                     } else {
3040                         set term [string trim [string range $term 1 $len]]
3041                         set relation 5
3042                     }
3043                 } elseif {[string index $term 0] == "<"} {
3044                     if {[string index $term 1] == "=" } {
3045                         set term [string trim [string range $term 2 $len]]
3046                         set relation 2
3047                     } elseif {[string index $term 1] == ">"} {
3048                         set term [string trim [string range $term 2 $len]]
3049                         set relation 6
3050                     } else {
3051                         set term [string trim [string range $term 1 $len]]
3052                         set relation 1
3053                     }
3054                 }
3055             } 
3056             set len [string length $term]
3057             incr len -1
3058             set left 0
3059             set right 0
3060             if {[string index $term $len] == "?"} {
3061                 set right 1
3062                 set term [string range $term 0 [expr {$len - 1}]]
3063             }
3064             if {[string index $term 0] == "?"} {
3065                 set left 1
3066                 set term [string range $term 1 end]
3067             }
3068             set term "\{${term}\}"
3069             if {$right && $left} {
3070                 set term "@attrset $attributeTypeSelected @attr 5=3 ${term}"
3071             } elseif {$right} {
3072                 set term "@attrset $attributeTypeSelected @attr 5=1 ${term}"
3073             } elseif {$left} {
3074                 set term "@attrset $attributeTypeSelected @attr 5=2 ${term}"
3075             }
3076             if {$relation != ""} {
3077                 set term "@attrset $attributeTypeSelected @attr 2=${relation} ${term}"
3078             }
3079             foreach a $attr {
3080                 set term "@attrset $attributeTypeSelected @attr $a ${term}"
3081             }
3082             if {$qs != ""} {
3083                 set qs "@and ${qs} ${term}"
3084             } else {
3085                 set qs $term
3086             }
3087         }
3088         incr i
3089     }
3090     dputs "qs=  $qs"
3091     return $qs
3092 }
3093
3094 # Procedure index-focus-in {w i}
3095 #  w    index frame
3096 #  i    index number
3097 # This procedure handles <FocusIn> events. A red border is drawed
3098 # around the active search entry field.
3099 proc index-focus-in {w i} {
3100     global curIndexEntry
3101     $w.$i configure -background red
3102     set curIndexEntry $i
3103 }
3104
3105 # Procedure index-lines {w readOp buttonInfo queryInfo handle}
3106 #  w          search fields entry frame
3107 #  realOp     if true, search-request bindings are bound to the entries.
3108 #  buttonInfo query type button information
3109 #  queryInfo  query type field information
3110 #  handle     handler called a when a 'listbutton' changes its value
3111 # Makes one or more search areas - with listbuttons on the left
3112 # and entries on the right. 
3113 proc index-lines {w realOp buttonInfo queryInfo handle} {
3114     set i 0
3115     foreach b $buttonInfo {
3116         if {! [winfo exists $w.$i]} {
3117             frame $w.$i -border 0
3118         }
3119         listbuttonx $w.$i.l [lindex $b 1] $queryInfo $handle $i
3120
3121         if {$realOp} {
3122             if {! [winfo exists $w.$i.e]} {
3123                 entry $w.$i.e -width 32 -relief sunken -border 1
3124                 bind $w.$i.e <FocusIn> [list index-focus-in $w $i]
3125                 bind $w.$i.e <FocusOut> [list $w.$i configure -background white]
3126                 pack $w.$i.l -side left
3127                 pack $w.$i.e -side left -fill x -expand yes
3128                 pack $w.$i -side top -fill x -padx 2 -pady 2
3129                 bind $w.$i.e <Left> [list left-cursor $w.$i.e]
3130                 bind $w.$i.e <Right> [list right-cursor $w.$i.e]
3131                 bind $w.$i.e <Return> {search-request 0}
3132             }
3133         } else {
3134             pack $w.$i.l -side left
3135             pack $w.$i -side top -fill x -padx 2 -pady 2
3136         }
3137         incr i
3138     }
3139     set j $i
3140     while {[winfo exists $w.$j]} {
3141         destroy $w.$j
3142         incr j
3143     }
3144     if {! $realOp} {
3145         return
3146     }
3147     set j 0
3148     incr i -1
3149     while {$j < $i} {
3150         set k [expr {$j + 1}]
3151         bind $w.$j.e <Tab> "focus $w.$k.e"
3152         set j $k
3153     }
3154     if {$i >= 0} {
3155         bind $w.$i.e <Tab> "focus $w.0.e"
3156         focus $w.0.e
3157     }
3158 }
3159
3160 #Procedure configureOptionsSyntax {target base}
3161 #target        target name
3162 #base         database name
3163 #Changes the Options|Syntax menu acording to the information obtained via explain.
3164 proc configureOptionsSyntax {target base} {
3165     global profile syntaxList recordSyntax 
3166     set activate 0
3167     set i -1
3168     set w .top.options.m.syntax
3169     if {[info exists profile($target,RecordSyntaxes,$base)]} {
3170         foreach syntax $syntaxList {
3171             incr i
3172             if {$syntax == "sep"} {continue}
3173             if {[lsearch $profile($target,RecordSyntaxes,$base) $syntax] != -1} {
3174                 configure-enable-e $w $i
3175                 if {$activate == 0} {
3176                     $w invoke $i
3177                     set recordSyntax $syntax
3178                     set activate 1
3179                 }
3180             } else {
3181                 configure-disable-e $w $i
3182             }
3183         }
3184     } else {
3185         foreach syntax $syntaxList {
3186             incr i
3187             if {$syntax == "sep"} {continue}
3188             configure-enable-e $w $i
3189         }
3190         $w invoke 0
3191     }
3192 }
3193
3194 # Init: The geometry information for the main window is set - either
3195 # to a default value or to the value in windowGeometry(.)
3196 if {[catch {set g $windowGeometry(.)}]} {
3197     wm geometry . 420x340
3198 } else {
3199     wm geometry . $g
3200 }
3201
3202 # Init: Presentation formats are read.
3203 read-formats
3204
3205 # Init: The main window is defined.
3206 frame .top  -border 1 -relief raised
3207 frame .lines  -border 1 -relief raised
3208 frame .mid  -border 1 -relief raised
3209 frame .data -border 1 -relief raised
3210 frame .bot  -border 1 -relief raised
3211 pack .top .lines .mid -side top -fill x
3212 pack .data -side top -fill both -expand yes
3213 pack .bot -fill x
3214
3215 # Init: Definition of File menu.
3216 menubutton .top.file -text File -menu .top.file.m
3217 irmenu .top.file.m
3218 .top.file.m add command -label {Save settings} -command {save-settings}
3219 .top.file.m add separator
3220 .top.file.m add command -label Exit -command {exit-action}
3221
3222 # Init: Definition of Target menu.
3223 menubutton .top.target -text Target -menu .top.target.m
3224 irmenu .top.target.m
3225 .top.target.m add cascade -label Connect -menu .top.target.m.clist
3226 .top.target.m add command -label Disconnect -command {close-target}
3227 .top.target.m add command -label About -command {about-target}
3228 .top.target.m add cascade -label Setup -menu .top.target.m.slist
3229 .top.target.m add command -label {Setup new} -command {define-target-dialog}
3230 .top.target.m add separator
3231 set-target-hotlist 0
3232
3233 configure-disable-e .top.target.m 1
3234 configure-disable-e .top.target.m 2
3235
3236 irmenu .top.target.m.clist
3237 irmenu .top.target.m.slist
3238 cascade-target-list
3239
3240 # Init: Definition of Service menu.
3241 menubutton .top.service -text Service -menu .top.service.m
3242 irmenu .top.service.m
3243 .top.service.m add cascade -label Database -menu .top.service.m.dblist
3244 .top.service.m add cascade -label Present -menu .top.service.m.present
3245 irmenu .top.service.m.present
3246 .top.service.m.present add command -label {10 More} -command [list present-more 10]
3247 .top.service.m.present add command -label All -command [list present-more {}]
3248 .top.service.m add command -label Search -command {search-request 0}
3249 .top.service.m add command -label Scan -command {scan-request}
3250 .top.service.m add command -label Explain -command \
3251     {explain-refresh $hostid {ready-response {}} }
3252
3253 .top.service configure -state disabled
3254
3255 irmenu .top.service.m.dblist
3256
3257 # Init: Definition of Set menu.
3258 menubutton .top.rset -text Set -menu .top.rset.m
3259 irmenu .top.rset.m
3260 .top.rset.m add command -label Load -command {load-set}
3261 .top.rset.m add separator
3262
3263 # Init: Definition of the Options menu.
3264 menubutton .top.options -text Options -menu .top.options.m
3265 irmenu .top.options.m
3266 .top.options.m add cascade -label Query -menu .top.options.m.query
3267 .top.options.m add cascade -label Format -menu .top.options.m.formats
3268 .top.options.m add cascade -label Wrap -menu .top.options.m.wrap
3269 .top.options.m add cascade -label Syntax -menu .top.options.m.syntax
3270 .top.options.m add cascade -label Elements -menu .top.options.m.elements
3271 .top.options.m add radiobutton -label Debug -variable debugMode -value 1
3272
3273 # Init: Definition of the Options|Query menu.
3274 irmenu .top.options.m.query
3275 .top.options.m.query add cascade -label Select -menu .top.options.m.query.clist
3276 .top.options.m.query add cascade -label Type -menu .top.options.m.query.tlist
3277 .top.options.m.query add cascade -label Edit -menu .top.options.m.query.slist
3278 .top.options.m.query add command -label New -command {query-new}
3279 .top.options.m.query add cascade -label Delete -menu .top.options.m.query.dlist
3280
3281 irmenu .top.options.m.query.slist
3282 irmenu .top.options.m.query.tlist
3283 irmenu .top.options.m.query.clist
3284 irmenu .top.options.m.query.dlist
3285 cascade-query-list
3286
3287 # Init: Definition of the Options|Formats menu.
3288 irmenu .top.options.m.formats
3289 set i 0
3290 foreach f $displayFormats {
3291     .top.options.m.formats add radiobutton -label $f -value $i \
3292             -command [list set-display-format $i] -variable displayFormat
3293     incr i
3294 }
3295
3296 # Init: Definition of the Options|Wrap menu.
3297 irmenu .top.options.m.wrap
3298 .top.options.m.wrap add radiobutton -label Character \
3299         -value char -variable textWrap -command {set-wrap char}
3300 .top.options.m.wrap add radiobutton -label Word \
3301         -value word -variable textWrap -command {set-wrap word}
3302 .top.options.m.wrap add radiobutton -label None \
3303         -value none -variable textWrap -command {set-wrap none}
3304
3305 # Init: Definition of the Options|Syntax menu.
3306 proc initOptionsSyntax {} {
3307     global syntaxList recordSyntax
3308     set w .top.options.m.syntax
3309     irmenu $w
3310     foreach syntax $syntaxList {
3311         if {$syntax == "sep"} {
3312             $w add separator
3313         } else {
3314             $w add radiobutton -label $syntax -value $syntax -variable recordSyntax
3315         }
3316     }
3317 }
3318 initOptionsSyntax
3319
3320 # Init: Definition of the Options|Elements menu.
3321 irmenu .top.options.m.elements
3322 .top.options.m.elements add radiobutton -label Unspecified \
3323         -value None -variable elementSetNames
3324 .top.options.m.elements add radiobutton -label Full -value F -variable elementSetNames
3325 .top.options.m.elements add radiobutton -label Brief -value B -variable elementSetNames
3326
3327 # Init: Definition of Help menu.
3328 menubutton .top.help -text "Help" -menu .top.help.m
3329 irmenu .top.help.m
3330
3331 #.top.help.m add command -label "Help on help" -command {tkerror "Help on help not available. Sorry"}
3332 .top.help.m add command -label "Help on help" -command {bgerror "Help on help not available. Sorry"}
3333 .top.help.m add command -label "About" -command {about-origin}
3334
3335 # Init: Pack menu bar items.
3336 pack .top.file .top.target .top.service .top.rset .top.options -side left
3337 pack .top.help -side right
3338
3339 # Init: Define query area.
3340 index-lines .lines 1 $queryButtonsFind [lindex $queryInfo 0] activate-index
3341 image create photo scan -file [file join $libdir bitmaps a-z.gif]
3342 image create photo clear -file [file join $libdir bitmaps trash.gif]
3343 image create photo present -file [file join $libdir bitmaps page.gif]
3344 image create photo search -file [file join $libdir bitmaps search.gif]
3345 button .mid.search -image search -command {search-request 0} -state disabled -relief flat
3346 button .mid.scan -image scan -command scan-request -state disabled -relief flat
3347 button .mid.present -image present -command [list present-more 10] -state disabled -relief flat
3348 button .mid.clear -image clear -command index-clear -relief flat
3349 pack .mid.search .mid.scan .mid.present .mid.clear -side left -fill y -pady 1
3350
3351 # Init: Define record area in main window.
3352 text .data.record -font fixed -height 2 -width 20 -wrap none -borderwidth 0 \
3353     -relief flat -yscrollcommand [list .data.scroll set] -wrap $textWrap
3354 scrollbar .data.scroll -command [list .data.record yview]
3355 .data.record configure -takefocus 0
3356 .data.scroll configure -takefocus 0
3357
3358 pack .data.scroll -side right -fill y
3359 pack .data.record -expand yes -fill both
3360 initBindings
3361
3362 # Init: Define standards tags. These are used in the display
3363 # format procedures.
3364
3365 .data.record tag configure marc-tag -foreground blue
3366 .data.record tag configure marc-id -foreground red
3367 .data.record tag configure marc-data -foreground black
3368 .data.record tag configure marc-head -font $font(n,normal) \
3369         -foreground brown -relief raised -borderwidth 1
3370 .data.record tag configure marc-small-head -foreground brown
3371 .data.record tag configure marc-pref -font $font(n,normal) -foreground blue
3372 .data.record tag configure marc-text -font $font(n,normal) -foreground black
3373 .data.record tag configure marc-it -font $font(n,normal) -foreground black
3374
3375 # Init: Define logo.
3376 button .bot.logo -bitmap @[file join $libdir bitmaps book1] -command cancel-operation
3377 .bot.logo configure -takefocus 0
3378
3379 # Init: Define status information fields at the bottom.
3380 frame .bot.a
3381 pack .bot.a -side left -fill x
3382 pack .bot.logo -side right -padx 2 -pady 2 -ipadx 1
3383
3384 message .bot.a.target -text {} -aspect 2000 -border 1
3385
3386 label .bot.a.status -text "Not connected" -width 15 -relief sunken -anchor w -border 1
3387 label .bot.a.set -text "" -width 5 -relief sunken -anchor w -border 1
3388 label .bot.a.message -text "" -width 15 -relief sunken -anchor w -border 1
3389
3390 pack .bot.a.target -side top -anchor nw -padx 2 -pady 2
3391 pack .bot.a.status .bot.a.set .bot.a.message -side left -padx 2 -pady 2 -ipadx 1 -ipady 1
3392
3393 # Init: Determine if the IrTcl extension is already there. If
3394 #  not, then dynamically load the IrTcl extension.
3395 if {[catch {ir z39}]} {
3396     set e [info sharedlibextension]
3397     puts -nonewline "Loading irtcl$e ..."
3398     load [file join $libdir irtcl$e] irtcl
3399     ir z39
3400     puts "ok"
3401 }
3402
3403 if {[file exists [file join $libdir explain.tcl]]} {
3404     source [file join $libdir explain.tcl]
3405 }
3406
3407 #if {[file exists ${libdir}/setup.tcl]} 
3408     source [file join $libdir setup.tcl]
3409
3410
3411 # Init: Uncomment this line if you wan't to enable logging.
3412 ir-log-init all irtcl irtcl.log
3413
3414 # Init: If hostid is a valid target, a new connection will be established
3415 # immediately.
3416 if {[string compare $hostid Default]} {
3417     catch {open-target $hostid $hostbase}
3418 }
3419
3420 # Init: Enable the logo.
3421 show-logo 1
3422