Eskil

Diff
Login

Differences From Artifact [cf374614a0]:

To Artifact [15ccacfb0f]:


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    foreach dir $dirs {
        set dir [file normalize $dir]
        set files {}
        lappend files [file join $dir $plugin]
        lappend files [file join $dir $plugin.tcl]
        foreach file $files {
            if {![file exists   $file]} continue
            if {![file isfile   $file]} continue
            if {![file readable $file]} continue
            set ch [open $file r]
            set data [read $ch 20]
            close $ch
            if {[string match "##Eskil Plugin*" $data]} {
                set fSrc $file
                break
            }







|
|
|







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    foreach dir $dirs {
        set dir [file normalize $dir]
        set files {}
        lappend files [file join $dir $plugin]
        lappend files [file join $dir $plugin.tcl]
        foreach file $files {
            if { ! [file exists   $file]} continue
            if { ! [file isfile   $file]} continue
            if { ! [file readable $file]} continue
            set ch [open $file r]
            set data [read $ch 20]
            close $ch
            if {[string match "##Eskil Plugin*" $data]} {
                set fSrc $file
                break
            }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    # Setup info
    $pi eval [list set ::WhoAmI [file rootname [file tail $src]]]
    $pi eval [list set ::WhoAmIFull [file normalize $src]]
    $pi eval [list set ::Info $info]
    interp share {} stdout $pi

    # Expose needed commands
    if {!$allow} {
        interp expose $pi fconfigure ;# needed??
        interp hide $pi close
    }

    set pinfo {file 0 dir 0}
    dict set pinfo "allow" $allow
    if {[$pi eval info proc PreProcess] ne ""} {







|







98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    # Setup info
    $pi eval [list set ::WhoAmI [file rootname [file tail $src]]]
    $pi eval [list set ::WhoAmIFull [file normalize $src]]
    $pi eval [list set ::Info $info]
    interp share {} stdout $pi

    # Expose needed commands
    if { ! $allow} {
        interp expose $pi fconfigure ;# needed??
        interp hide $pi close
    }

    set pinfo {file 0 dir 0}
    dict set pinfo "allow" $allow
    if {[$pi eval info proc PreProcess] ne ""} {
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        return
    }
    set ch [open $src]
    set lines [split [read $ch] \n]
    foreach line $lines {
        set lineT [string trim $line]
        if {$short} {
            if {![string match "#*" $lineT]} {
                break
            }
        }
        puts $line
    }
    close $ch
}

proc listPlugins {} {
    set dirs [PluginSearchPath]
    set result {}

    foreach dir $dirs {
        set dir [file normalize $dir]
        set files [glob -nocomplain [file join $dir *.tcl]]
        foreach file $files {
            set file [file normalize $file]
            if {[info exists done($file)]} continue
            if {![file exists $file]} continue
            if {![file isfile $file]} continue
            if {![file readable $file]} continue

            set done($file) 1
            set ch [open $file r]
            set data [read $ch 200]
            if {[regexp {^\#\#Eskil Plugin :(.*?)(\n|$)} $data -> descr]} {
                set root [file rootname [file tail $file]]
                dict set result $root "descr" $descr







|


















|
|
|







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        return
    }
    set ch [open $src]
    set lines [split [read $ch] \n]
    foreach line $lines {
        set lineT [string trim $line]
        if {$short} {
            if { ! [string match "#*" $lineT]} {
                break
            }
        }
        puts $line
    }
    close $ch
}

proc listPlugins {} {
    set dirs [PluginSearchPath]
    set result {}

    foreach dir $dirs {
        set dir [file normalize $dir]
        set files [glob -nocomplain [file join $dir *.tcl]]
        foreach file $files {
            set file [file normalize $file]
            if {[info exists done($file)]} continue
            if { ! [file exists $file]} continue
            if { ! [file isfile $file]} continue
            if { ! [file readable $file]} continue

            set done($file) 1
            set ch [open $file r]
            set data [read $ch 200]
            if {[regexp {^\#\#Eskil Plugin :(.*?)(\n|$)} $data -> descr]} {
                set root [file rootname [file tail $file]]
                dict set result $root "descr" $descr
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    }

    disallowEdit $top
    set in1 $::eskil($top,leftFile)
    set in2 $::eskil($top,rightFile)

    foreach item [lsort -dictionary [array names ::eskil $top,pluginname,*]] {
        set n [lindex [split $item ","] end]

        set allow [dict get $::eskil($top,pluginpinfo,$n) allow]
        # Pass ::argv to plugin
        set pArgv $::eskil(argv)
        if {[info exists ::eskil($top,pluginargv,$n)]} {
            lappend pArgv {*}$::eskil($top,pluginargv,$n)
        }
        $::eskil($top,plugin,$n) eval [list set ::argv $pArgv]
        # Pass ::Pref to plugin
        $::eskil($top,plugin,$n) eval [list array set ::Pref [array get ::Pref]]
        # Pass File info to plugin
        $::eskil($top,plugin,$n) eval [list set ::File(left)  $::eskil($top,leftFile)]
        $::eskil($top,plugin,$n) eval [list set ::File(right) $::eskil($top,rightFile)]

        set out1 [tmpFile]
        set out2 [tmpFile]

        set chi [open $in1 r]
        set cho [open $out1 w]
        set chi2 [open $in2 r]
        set cho2 [open $out2 w]
        interp share {} $chi $::eskil($top,plugin,$n)
        interp share {} $cho $::eskil($top,plugin,$n)
        interp share {} $chi2 $::eskil($top,plugin,$n)
        interp share {} $cho2 $::eskil($top,plugin,$n)

        set cmd1 [list PreProcess left $chi $cho]
        set cmd2 [list PreProcess right $chi2 $cho2]
        if {[info commands yield] ne ""} {
            # When in 8.6, this is done in coroutines allowing each call
            # to yield and to alternate between them until done
            set c1 __plugin_cr1$top
            set c2 __plugin_cr2$top
            set cmd1 [linsert $cmd1 0 coroutine $c1]
            set cmd2 [linsert $cmd2 0 coroutine $c2]
            set usenew1 [$::eskil($top,plugin,$n) eval $cmd1]
            set usenew2 [$::eskil($top,plugin,$n) eval $cmd2]
            interp alias {} pnw $::eskil($top,plugin,$n) namespace which
            while {[pnw $c1] ne {} || [pnw $c2] ne {}} {
                if {[pnw $c1] ne {}} {
                    set usenew1 [$::eskil($top,plugin,$n) eval $c1]
                }
                if {[pnw $c2] ne {}} {
                    set usenew2 [$::eskil($top,plugin,$n) eval $c2]
                }
            }
        } else {
            set usenew1 [$::eskil($top,plugin,$n) eval $cmd1]
            set usenew2 [$::eskil($top,plugin,$n) eval $cmd2]
        }

        if {$allow} {
            $::eskil($top,plugin,$n) eval close $chi
            $::eskil($top,plugin,$n) eval close $cho
            $::eskil($top,plugin,$n) eval close $chi2
            $::eskil($top,plugin,$n) eval close $cho2
        } else {
            $::eskil($top,plugin,$n) invokehidden close $chi
            $::eskil($top,plugin,$n) invokehidden close $cho
            $::eskil($top,plugin,$n) invokehidden close $chi2
            $::eskil($top,plugin,$n) invokehidden close $cho2
        }
        close $chi
        close $cho
        close $chi2
        close $cho2

        if {$usenew1} {
            # The file after processing should be used both
            # for comparison and for displaying.
            if {![info exists ::eskil($top,leftFileBak)]} {
                set ::eskil($top,leftFileBak) $::eskil($top,leftFile)
            }
            unset -nocomplain ::eskil($top,leftFileDiff)
            set ::eskil($top,leftFile) $out1
        } else {
            set ::eskil($top,leftFileDiff) $out1
        }
        if {$usenew2} {
            if {![info exists ::eskil($top,rightFileBak)]} {
                set ::eskil($top,rightFileBak) $::eskil($top,rightFile)
            }
            unset -nocomplain ::eskil($top,rightFileDiff)
            set ::eskil($top,rightFile) $out2
        } else {
            set ::eskil($top,rightFileDiff) $out2
        }







|

|


|
|

|

|

|
|








|
|
|
|










|
|
|


|


|



|
|



|
|
|
|

|
|
|
|









|








|







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    }

    disallowEdit $top
    set in1 $::eskil($top,leftFile)
    set in2 $::eskil($top,rightFile)

    foreach item [lsort -dictionary [array names ::eskil $top,pluginname,*]] {
        set pI [lindex [split $item ","] end]

        set allow [dict get $::eskil($top,pluginpinfo,$pI) allow]
        # Pass ::argv to plugin
        set pArgv $::eskil(argv)
        if {[info exists ::eskil($top,pluginargv,$pI)]} {
            lappend pArgv {*}$::eskil($top,pluginargv,$pI)
        }
        $::eskil($top,plugin,$pI) eval [list set ::argv $pArgv]
        # Pass ::Pref to plugin
        $::eskil($top,plugin,$pI) eval [list array set ::Pref [array get ::Pref]]
        # Pass File info to plugin
        $::eskil($top,plugin,$pI) eval [list set ::File(left)  $::eskil($top,leftFile)]
        $::eskil($top,plugin,$pI) eval [list set ::File(right) $::eskil($top,rightFile)]

        set out1 [tmpFile]
        set out2 [tmpFile]

        set chi [open $in1 r]
        set cho [open $out1 w]
        set chi2 [open $in2 r]
        set cho2 [open $out2 w]
        interp share {} $chi $::eskil($top,plugin,$pI)
        interp share {} $cho $::eskil($top,plugin,$pI)
        interp share {} $chi2 $::eskil($top,plugin,$pI)
        interp share {} $cho2 $::eskil($top,plugin,$pI)

        set cmd1 [list PreProcess left $chi $cho]
        set cmd2 [list PreProcess right $chi2 $cho2]
        if {[info commands yield] ne ""} {
            # When in 8.6, this is done in coroutines allowing each call
            # to yield and to alternate between them until done
            set c1 __plugin_cr1$top
            set c2 __plugin_cr2$top
            set cmd1 [linsert $cmd1 0 coroutine $c1]
            set cmd2 [linsert $cmd2 0 coroutine $c2]
            set usenew1 [$::eskil($top,plugin,$pI) eval $cmd1]
            set usenew2 [$::eskil($top,plugin,$pI) eval $cmd2]
            interp alias {} pnw $::eskil($top,plugin,$pI) namespace which
            while {[pnw $c1] ne {} || [pnw $c2] ne {}} {
                if {[pnw $c1] ne {}} {
                    set usenew1 [$::eskil($top,plugin,$pI) eval $c1]
                }
                if {[pnw $c2] ne {}} {
                    set usenew2 [$::eskil($top,plugin,$pI) eval $c2]
                }
            }
        } else {
            set usenew1 [$::eskil($top,plugin,$pI) eval $cmd1]
            set usenew2 [$::eskil($top,plugin,$pI) eval $cmd2]
        }

        if {$allow} {
            $::eskil($top,plugin,$pI) eval close $chi
            $::eskil($top,plugin,$pI) eval close $cho
            $::eskil($top,plugin,$pI) eval close $chi2
            $::eskil($top,plugin,$pI) eval close $cho2
        } else {
            $::eskil($top,plugin,$pI) invokehidden close $chi
            $::eskil($top,plugin,$pI) invokehidden close $cho
            $::eskil($top,plugin,$pI) invokehidden close $chi2
            $::eskil($top,plugin,$pI) invokehidden close $cho2
        }
        close $chi
        close $cho
        close $chi2
        close $cho2

        if {$usenew1} {
            # The file after processing should be used both
            # for comparison and for displaying.
            if { ! [info exists ::eskil($top,leftFileBak)]} {
                set ::eskil($top,leftFileBak) $::eskil($top,leftFile)
            }
            unset -nocomplain ::eskil($top,leftFileDiff)
            set ::eskil($top,leftFile) $out1
        } else {
            set ::eskil($top,leftFileDiff) $out1
        }
        if {$usenew2} {
            if { ! [info exists ::eskil($top,rightFileBak)]} {
                set ::eskil($top,rightFileBak) $::eskil($top,rightFile)
            }
            unset -nocomplain ::eskil($top,rightFileDiff)
            set ::eskil($top,rightFile) $out2
        } else {
            set ::eskil($top,rightFileDiff) $out2
        }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    incr pos -1
    $wt insert $pos $win
}

# Add a tab to plugin prefernces
proc EditPrefPluginsAddTab {top dirdiff {pos {}}} {
    set wt $top.prefplugin.tab
    set n [$wt index end]
    if {$pos eq "" || $pos >= ($n - 1)} {
        # Since the "+" tab is last, the index is n for any new one
        set pos [expr {$n - 1}]
    }
    ttk::frame $wt.f,$n
    $wt insert $pos $wt.f,$n -text "Plugin"

    set wt $wt.f,$n

    set plugins [listPlugins]
    if {[llength $plugins] == 0} {
        grid [ttk::label $wt.l -text "No plugins found."] - -padx 3 -pady 3
    }
    if {![info exists ::eskil($top,pluginname,$n)]} {
        set ::eskil($top,pluginname,$n) ""
    }
    if {![info exists ::eskil($top,plugininfo,$n)]} {
        set ::eskil($top,plugininfo,$n) ""
    }
    if {![info exists ::eskil($top,pluginallow,$n)]} {
        set ::eskil($top,pluginallow,$n) 0
    }
    set ::eskil($top,edit,pluginname,$n) $::eskil($top,pluginname,$n)
    set ::eskil($top,edit,plugininfo,$n) $::eskil($top,plugininfo,$n)
    set ::eskil($top,edit,pluginallow,$n) $::eskil($top,pluginallow,$n)

    ttk::labelframe $wt.lfs -text "Select"
    grid columnconfigure $wt.lfs 1 -weight 1

    set t 0
    foreach {plugin info} $plugins {
        set descr [dict get $info descr]
        if {$dirdiff && ![dict get $info dir]} continue
        ttk::radiobutton $wt.rb$t -variable ::eskil($top,edit,pluginname,$n) \
                -value $plugin -text $plugin
        ttk::label $wt.l$t -text $descr -anchor w
        grid $wt.rb$t $wt.l$t -in $wt.lfs -sticky we -padx 3 -pady 3
        incr t
    }
    ttk::radiobutton $wt.rb$t -variable ::eskil($top,edit,pluginname,$n) \
            -value "" -text "No Plugin"
    ttk::button $wt.bs -text "Show" \
            -command "ShowPlugin $wt \$::eskil($top,edit,pluginname,$n)"
    addBalloon $wt.bs "Show plugin source code."
    grid $wt.rb$t $wt.bs -in $wt.lfs -sticky we -padx 3 -pady 3
    grid $wt.bs -sticky e

    ttk::labelframe $wt.lfgc -text "Generic Configuration"
    grid columnconfigure $wt.lfgc 1 -weight 1

    ttk::label $wt.li -text "Info" -anchor w
    addBalloon $wt.li "Info passed to plugin. Plugin specific."
    ttk::entry $wt.ei -textvariable ::eskil($top,edit,plugininfo,$n)
    grid $wt.li $wt.ei -in $wt.lfgc -sticky we -padx 3 -pady 3

    ttk::checkbutton $wt.cb -text "Privilege" \
            -variable ::eskil($top,edit,pluginallow,$n)
    addBalloon $wt.cb "Run plugin with raised privileges"
    grid $wt.cb -  -in $wt.lfgc -sticky w -padx 3 -pady 3

    ttk::labelframe $wt.lfsc -text "Specific Configuration"
    set ::widgets($top,prefPluginsSpec,$n) $wt.lfsc
    trace add variable ::eskil($top,edit,pluginname,$n) write \
            [list UpdateSpecificPluginConf $top $n]
    UpdateSpecificPluginConf $top $n

    grid $wt.lfs  -sticky we -padx 3 -pady 3
    grid $wt.lfgc -sticky we -padx 3 -pady 3
    grid $wt.lfsc -sticky we -padx 3 -pady 3
    grid columnconfigure $wt 0 -weight 1
}

# When a new plugin is selected, update the list of specific options.
# "args" is needed to swallow the extra variable trace args.
proc UpdateSpecificPluginConf {top n args} {
    set w $::widgets($top,prefPluginsSpec,$n)
    # If the dialog is closed w might not exist
    if {![winfo exists $w]} return
    eval destroy [winfo children $w]

    set arg $::eskil($top,edit,pluginname,$n)
    set pOpts {}
    if {$arg ne ""} {
        set res [LocatePlugin $arg]
        set pOpts [dict get $res opts]
    }
    # Look for defaults on the command line
    set pArgv $::eskil(argv)
    if {[info exists ::eskil($top,pluginargv,$n)]} {
        lappend pArgv {*}$::eskil($top,pluginargv,$n)
    }
    # Look for declarations of command line options
    set t 0
    set ::eskil($top,edit,opts,$n) $pOpts
    foreach {name flag doc} $pOpts {
        ttk::label $w.l$t -text $name
        addBalloon $w.l$t -fmt $doc
        grid $w.l$t -sticky "w" -padx 3 -pady 3
        if {$flag} {
            # Initialise if given.
            if {[lsearch -exact $pArgv $name] >= 0} {
                set ::eskil($top,edit,$name,$n) 1
                # Move responsibility from global argv
                set ix [lsearch -exact $::eskil(argv) $name]
                if {$ix >= 0} {
                    set ::eskil(argv) [lreplace $::eskil(argv) $ix $ix]
                    lappend ::eskil($top,pluginargv,$n) $name
                }
            }
            ttk::checkbutton $w.s$t -text "On" \
                    -variable ::eskil($top,edit,$name,$n)
            grid $w.s$t -row $t -column 1 -sticky "w" -padx 3 -pady 3
        } else {
            # Initialise if given.
            set ix [lsearch -exact $pArgv $name]
            if {$ix >= 0} {
                set ::eskil($top,edit,$name,$n) [lindex $pArgv $ix+1]
                # Move responsibility from global argv
                set ix [lsearch -exact $::eskil(argv) $name]
                if {$ix >= 0} {
                    lappend ::eskil($top,pluginargv,$n) $name \
                            [lindex $::eskil(argv) $ix+1]
                    set ::eskil(argv) [lreplace $::eskil(argv) $ix $ix+1]
                }
            }
            ttk::entry $w.s$t \
                    -textvariable ::eskil($top,edit,$name,$n)
            grid $w.s$t -row $t -column 1 -sticky we -padx 3 -pady 3
        }
        incr t
    }
    grid columnconfigure $w 1 -weight 1
    if {$t == 0} {
        ttk::label $w.l -text "No specific configuration"
        grid $w.l -sticky "w" -padx 3 -pady 3
        return
    }
}

# Ok or Apply pressend in Plugin Preference
proc EditPrefPluginsOk {top wt apply} {
    # Compress plugin info in tab order
    set allN {}
    foreach win [$wt.tab tabs] {
        set n [lindex [split $win ","] end]
        if {![string is integer -strict $n]} continue
        # Find all used.
        if {$::eskil($top,edit,pluginname,$n) ne ""} {
            lappend allN $n
        }
    }
    if {[llength $allN] == 0} {
        lappend allN 1
    }

    # Keep the dialog if we are only applying
    if {!$apply} {
        destroy $wt
    }

    # Transfer them to consecutive numbers
    set t 1
    foreach n $allN {
        set ::eskil($top,pluginname,$t)  $::eskil($top,edit,pluginname,$n)
        set ::eskil($top,plugininfo,$t)  $::eskil($top,edit,plugininfo,$n)
        set ::eskil($top,pluginallow,$t) $::eskil($top,edit,pluginallow,$n)
        incr t
    }
    # Remove any old
    foreach item [array names ::eskil $top,pluginname,*] {
        set n [lindex [split $item ","] end]
        if {$n >= $t} {
            unset ::eskil($top,pluginname,$n)
            set ::eskil($top,plugininfo,$n) ""
            set ::eskil($top,pluginallow,$n) 0
        }
    }

    # Handle all plugins
    foreach item [array names ::eskil $top,pluginname,*] {
        set n [lindex [split $item ","] end]
        if {$::eskil($top,pluginname,$n) ne ""} {
            set pinterp [createPluginInterp $::eskil($top,pluginname,$n) \
                                 $::eskil($top,plugininfo,$n) \
                                 $::eskil($top,pluginallow,$n) pinfo]
        } else {
            set pinterp ""
            set pinfo ""
        }
        set ::eskil($top,plugin,$n) $pinterp
        set ::eskil($top,pluginpinfo,$n) $pinfo
        set ::eskil($top,pluginargv,$n) {}
        foreach {name flag doc} $::eskil($top,edit,opts,$n) {
            if {$flag} {
                if {[info exists ::eskil($top,edit,$name,$n)] && \
                            $::eskil($top,edit,$name,$n)} {
                    lappend ::eskil($top,pluginargv,$n) $name
                }
            } else {
                if {[info exists ::eskil($top,edit,$name,$n)] && \
                            $::eskil($top,edit,$name,$n) ne ""} {
                    lappend ::eskil($top,pluginargv,$n) $name \
                            $::eskil($top,edit,$name,$n)
                }
            }
        }
    }
}

# Put Tcl code in a text widget, with some syntax highlighting
proc TextViewTcl {t data} {
    $t tag configure comment -foreground "#b22222"
    foreach line [split $data \n] {
        if {[regexp {^\s*#} $line]} {
            $t insert end $line\n comment
        } elseif {[regexp {^(.*;\s*)(#.*)$} $line -> pre post]} {
            $t insert end $pre
            $t insert end $post\n comment
        } else {
            $t insert end $line\n
        }
    }
}

# Show plugin source
proc ShowPlugin {parent plugin} {
    set res [LocatePlugin $plugin]







|
|

|

|
|

|





|
|

|
|

|
|

|
|
|








|





|


|









|



|




|
|
|
|









|
|

|


|







|
|



|







|




|



|





|



|





|

















|
|

|
|







|





|
|
|
|




|
|
|
|
|





|
|
|
|
|




|
|
|
|

|
|
|


|
|
|
|







|
|


|

|
|

|







404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    incr pos -1
    $wt insert $pos $win
}

# Add a tab to plugin prefernces
proc EditPrefPluginsAddTab {top dirdiff {pos {}}} {
    set wt $top.prefplugin.tab
    set pI [$wt index end]
    if {$pos eq "" || $pos >= ($pI - 1)} {
        # Since the "+" tab is last, the index is n for any new one
        set pos [expr {$pI - 1}]
    }
    ttk::frame $wt.f,$pI
    $wt insert $pos $wt.f,$pI -text "Plugin"

    set wt $wt.f,$pI

    set plugins [listPlugins]
    if {[llength $plugins] == 0} {
        grid [ttk::label $wt.l -text "No plugins found."] - -padx 3 -pady 3
    }
    if { ! [info exists ::eskil($top,pluginname,$pI)]} {
        set ::eskil($top,pluginname,$pI) ""
    }
    if { ! [info exists ::eskil($top,plugininfo,$pI)]} {
        set ::eskil($top,plugininfo,$pI) ""
    }
    if { ! [info exists ::eskil($top,pluginallow,$pI)]} {
        set ::eskil($top,pluginallow,$pI) 0
    }
    set ::eskil($top,edit,pluginname,$pI) $::eskil($top,pluginname,$pI)
    set ::eskil($top,edit,plugininfo,$pI) $::eskil($top,plugininfo,$pI)
    set ::eskil($top,edit,pluginallow,$pI) $::eskil($top,pluginallow,$pI)

    ttk::labelframe $wt.lfs -text "Select"
    grid columnconfigure $wt.lfs 1 -weight 1

    set t 0
    foreach {plugin info} $plugins {
        set descr [dict get $info descr]
        if {$dirdiff && ![dict get $info dir]} continue
        ttk::radiobutton $wt.rb$t -variable ::eskil($top,edit,pluginname,$pI) \
                -value $plugin -text $plugin
        ttk::label $wt.l$t -text $descr -anchor w
        grid $wt.rb$t $wt.l$t -in $wt.lfs -sticky we -padx 3 -pady 3
        incr t
    }
    ttk::radiobutton $wt.rb$t -variable ::eskil($top,edit,pluginname,$pI) \
            -value "" -text "No Plugin"
    ttk::button $wt.bs -text "Show" \
            -command "ShowPlugin $wt \$::eskil($top,edit,pluginname,$pI)"
    addBalloon $wt.bs "Show plugin source code."
    grid $wt.rb$t $wt.bs -in $wt.lfs -sticky we -padx 3 -pady 3
    grid $wt.bs -sticky e

    ttk::labelframe $wt.lfgc -text "Generic Configuration"
    grid columnconfigure $wt.lfgc 1 -weight 1

    ttk::label $wt.li -text "Info" -anchor w
    addBalloon $wt.li "Info passed to plugin. Plugin specific."
    ttk::entry $wt.ei -textvariable ::eskil($top,edit,plugininfo,$pI)
    grid $wt.li $wt.ei -in $wt.lfgc -sticky we -padx 3 -pady 3

    ttk::checkbutton $wt.cb -text "Privilege" \
            -variable ::eskil($top,edit,pluginallow,$pI)
    addBalloon $wt.cb "Run plugin with raised privileges"
    grid $wt.cb -  -in $wt.lfgc -sticky w -padx 3 -pady 3

    ttk::labelframe $wt.lfsc -text "Specific Configuration"
    set ::widgets($top,prefPluginsSpec,$pI) $wt.lfsc
    trace add variable ::eskil($top,edit,pluginname,$pI) write \
            [list UpdateSpecificPluginConf $top $pI]
    UpdateSpecificPluginConf $top $pI

    grid $wt.lfs  -sticky we -padx 3 -pady 3
    grid $wt.lfgc -sticky we -padx 3 -pady 3
    grid $wt.lfsc -sticky we -padx 3 -pady 3
    grid columnconfigure $wt 0 -weight 1
}

# When a new plugin is selected, update the list of specific options.
# "args" is needed to swallow the extra variable trace args.
proc UpdateSpecificPluginConf {top pI args} {
    set w $::widgets($top,prefPluginsSpec,$pI)
    # If the dialog is closed w might not exist
    if { ! [winfo exists $w]} return
    eval destroy [winfo children $w]

    set arg $::eskil($top,edit,pluginname,$pI)
    set pOpts {}
    if {$arg ne ""} {
        set res [LocatePlugin $arg]
        set pOpts [dict get $res opts]
    }
    # Look for defaults on the command line
    set pArgv $::eskil(argv)
    if {[info exists ::eskil($top,pluginargv,$pI)]} {
        lappend pArgv {*}$::eskil($top,pluginargv,$pI)
    }
    # Look for declarations of command line options
    set t 0
    set ::eskil($top,edit,opts,$pI) $pOpts
    foreach {name flag doc} $pOpts {
        ttk::label $w.l$t -text $name
        addBalloon $w.l$t -fmt $doc
        grid $w.l$t -sticky "w" -padx 3 -pady 3
        if {$flag} {
            # Initialise if given.
            if {[lsearch -exact $pArgv $name] >= 0} {
                set ::eskil($top,edit,$name,$pI) 1
                # Move responsibility from global argv
                set ix [lsearch -exact $::eskil(argv) $name]
                if {$ix >= 0} {
                    set ::eskil(argv) [lreplace $::eskil(argv) $ix $ix]
                    lappend ::eskil($top,pluginargv,$pI) $name
                }
            }
            ttk::checkbutton $w.s$t -text "On" \
                    -variable ::eskil($top,edit,$name,$pI)
            grid $w.s$t -row $t -column 1 -sticky "w" -padx 3 -pady 3
        } else {
            # Initialise if given.
            set ix [lsearch -exact $pArgv $name]
            if {$ix >= 0} {
                set ::eskil($top,edit,$name,$pI) [lindex $pArgv $ix+1]
                # Move responsibility from global argv
                set ix [lsearch -exact $::eskil(argv) $name]
                if {$ix >= 0} {
                    lappend ::eskil($top,pluginargv,$pI) $name \
                            [lindex $::eskil(argv) $ix+1]
                    set ::eskil(argv) [lreplace $::eskil(argv) $ix $ix+1]
                }
            }
            ttk::entry $w.s$t \
                    -textvariable ::eskil($top,edit,$name,$pI)
            grid $w.s$t -row $t -column 1 -sticky we -padx 3 -pady 3
        }
        incr t
    }
    grid columnconfigure $w 1 -weight 1
    if {$t == 0} {
        ttk::label $w.l -text "No specific configuration"
        grid $w.l -sticky "w" -padx 3 -pady 3
        return
    }
}

# Ok or Apply pressend in Plugin Preference
proc EditPrefPluginsOk {top wt apply} {
    # Compress plugin info in tab order
    set allN {}
    foreach win [$wt.tab tabs] {
        set pI [lindex [split $win ","] end]
        if { ! [string is integer -strict $pI]} continue
        # Find all used.
        if {$::eskil($top,edit,pluginname,$pI) ne ""} {
            lappend allN $pI
        }
    }
    if {[llength $allN] == 0} {
        lappend allN 1
    }

    # Keep the dialog if we are only applying
    if { ! $apply} {
        destroy $wt
    }

    # Transfer them to consecutive numbers
    set t 1
    foreach pI $allN {
        set ::eskil($top,pluginname,$t)  $::eskil($top,edit,pluginname,$pI)
        set ::eskil($top,plugininfo,$t)  $::eskil($top,edit,plugininfo,$pI)
        set ::eskil($top,pluginallow,$t) $::eskil($top,edit,pluginallow,$pI)
        incr t
    }
    # Remove any old
    foreach item [array names ::eskil $top,pluginname,*] {
        set pI [lindex [split $item ","] end]
        if {$pI >= $t} {
            unset ::eskil($top,pluginname,$pI)
            set ::eskil($top,plugininfo,$pI) ""
            set ::eskil($top,pluginallow,$pI) 0
        }
    }

    # Handle all plugins
    foreach item [array names ::eskil $top,pluginname,*] {
        set pI [lindex [split $item ","] end]
        if {$::eskil($top,pluginname,$pI) ne ""} {
            set pinterp [createPluginInterp $::eskil($top,pluginname,$pI) \
                                 $::eskil($top,plugininfo,$pI) \
                                 $::eskil($top,pluginallow,$pI) pinfo]
        } else {
            set pinterp ""
            set pinfo ""
        }
        set ::eskil($top,plugin,$pI) $pinterp
        set ::eskil($top,pluginpinfo,$pI) $pinfo
        set ::eskil($top,pluginargv,$pI) {}
        foreach {name flag doc} $::eskil($top,edit,opts,$pI) {
            if {$flag} {
                if {[info exists ::eskil($top,edit,$name,$pI)] && \
                            $::eskil($top,edit,$name,$pI)} {
                    lappend ::eskil($top,pluginargv,$pI) $name
                }
            } else {
                if {[info exists ::eskil($top,edit,$name,$pI)] && \
                            $::eskil($top,edit,$name,$pI) ne ""} {
                    lappend ::eskil($top,pluginargv,$pI) $name \
                            $::eskil($top,edit,$name,$pI)
                }
            }
        }
    }
}

# Put Tcl code in a text widget, with some syntax highlighting
proc TextViewTcl {tW data} {
    $tW tag configure comment -foreground "#b22222"
    foreach line [split $data \n] {
        if {[regexp {^\s*#} $line]} {
            $tW insert end $line\n comment
        } elseif {[regexp {^(.*;\s*)(#.*)$} $line -> pre post]} {
            $tW insert end $pre
            $tW insert end $post\n comment
        } else {
            $tW insert end $line\n
        }
    }
}

# Show plugin source
proc ShowPlugin {parent plugin} {
    set res [LocatePlugin $plugin]