Eskil

Diff
Login

Differences From Artifact [78f6f77662]:

To Artifact [adffe4b5fb]:


26
27
28
29
30
31
32
33

34

35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

52
53
54
55

56



















57

58
59
60
61
62


63
64
65
66
67
68
69
26
27
28
29
30
31
32

33
34
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

52
53
54
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81

82
83
84
85
86
87
88
89
90







-
+

+
-
+















-
+



-
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+




-
+
+







    set dirs [list . ./plugins]
    lappend dirs [file join $::eskil(thisDir) .. ..]
    lappend dirs [file join $::eskil(thisDir) .. .. plugins]
    lappend dirs [file join $::eskil(thisDir) .. plugins]
    return $dirs
}

# Locate plugin source
# Locate plugin source and extract some info
proc LocatePlugin {plugin} {
    set res [dict create name "" src "" opts ""]
    set src ""
    set fSrc ""
    set dirs [PluginSearchPath]

    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 src $file
                set fSrc $file
                break
            }
        }
        if {$src ne ""} break
        if {$fSrc ne ""} break
    }
    if {$fSrc ne ""} {
        dict set res name $plugin
        dict set res src  $fSrc
        # Look for declarations of command line options
        set ch [open $fSrc r]
        while {[gets $ch line] >= 0} {
            # Only look until empty line
            if {[string trim $line] eq ""} break
            if {[regexp {^\#\# Option\s+(\S+)(.*)} $line -> name rest]} {
                # structure is name flag doc
                dict lappend res opts $name 0 [string trim $rest " :"]
            }
            if {[regexp {^\#\# Flag\s+(\S+)(.*)} $line -> name rest]} {
                dict lappend res opts $name 1 [string trim $rest " :"]
            }
        }
        close $ch
    }

    return $src
    return $res
}

proc createPluginInterp {plugin info allow pinfoName} {
    upvar 1 $pinfoName pinfo
    set src [LocatePlugin $plugin]
    set res [LocatePlugin $plugin]
    set src [dict get $res src]

    if {$src eq ""} {
        return ""
    }

    # Create interpreter and load source
    if {$allow} {
95
96
97
98
99
100
101
102


103
104
105
106
107
108
109
116
117
118
119
120
121
122

123
124
125
126
127
128
129
130
131







-
+
+







        dict set pinfo dir 1
    }

    return $pi
}

proc printPlugin {plugin {short 0}} {
    set src [LocatePlugin $plugin]
    set res [LocatePlugin $plugin]
    set src [dict get $res src]
    if {$src eq ""} {
        printPlugins
        return
    }
    set ch [open $src]
    set lines [split [read $ch] \n]
    foreach line $lines {
179
180
181
182
183
184
185




186

187
188
189
190
191
192
193
201
202
203
204
205
206
207
208
209
210
211

212
213
214
215
216
217
218
219







+
+
+
+
-
+







    }
}

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

    set out1 [tmpFile]
310
311
312
313
314
315
316



317


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336




337
338
339

340
341
342
343
344



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359






































360
361




362
363
364
365
366
367
368
369
370
371
372
373
374














375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394


395
396
397
398
399
400
401
336
337
338
339
340
341
342
343
344
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362




363
364
365
366



367
368
369



370
371
372

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424


425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

475
476
477
478
479
480
481
482
483







+
+
+
-
+
+















-
-
-
-
+
+
+
+
-
-
-
+


-
-
-
+
+
+
-














+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+













+
+
+
+
+
+
+
+
+
+
+
+
+
+



















-
+
+







                -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) \
            -value "" -text "No Plugin"
    ttk::button $wt.bs -text "Show" \
            -command "ShowPlugin $wt \$::eskil($top,edit,pluginname)"
    addBalloon $wt.bs "Show plugin source code."
    grid $wt.rb$t -in $wt.lfs -sticky we -padx 3 -pady 3
    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)
    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)
    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"
    grid columnconfigure $wt.lfgc 1 -weight 1

    ttk::label $wt.lfsc.l -text "Not implemented yet" -anchor w
    addBalloon $wt.lfsc.l "The plan is to make selected plugin's specific\
    set ::widgets($top,prefPluginsSpec) $wt.lfsc
    trace add variable ::eskil($top,edit,pluginname) write \
            [list UpdateSpecificPluginConf $top]
    UpdateSpecificPluginConf $top
                           options available here"
    grid $wt.lfsc.l -sticky w -padx 3 -pady 3

    
    ttk::frame $wt.fb -padding 3
    ttk::button $wt.fb.b1 -text "Ok"   \
            -command [list EditPrefPluginsOk $top $wt]
    ttk::button $wt.fb.b2 -text "Show" \
            -command "ShowPlugin $wt \$::eskil($top,edit,pluginname)"
            -command [list EditPrefPluginsOk $top $wt 0]
    ttk::button $wt.fb.b2 -text "Apply" \
            -command [list EditPrefPluginsOk $top $wt 1]
    addBalloon $wt.fb.b2 "Show plugin source code."
    ttk::button $wt.fb.b3 -text "Cancel" -command [list destroy $wt]
    set ::widgets($top,prefPluginsOk) $wt.fb.b1

    grid $wt.fb.b1 x $wt.fb.b2 x $wt.fb.b3 -sticky we
    grid columnconfigure $wt.fb {0 2 4} -uniform a
    grid columnconfigure $wt.fb {1 3} -weight 1

    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 $wt.fb   -sticky we -padx 3 -pady 3
    grid columnconfigure $wt 0 -weight 1
}

proc UpdateSpecificPluginConf {top args} {
    set w $::widgets($top,prefPluginsSpec)
    # 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)
    set pOpts {}
    if {$arg ne ""} {
        set res [LocatePlugin $arg]
        set pOpts [dict get $res opts]
    }
    # Look for declarations of command line options
    set n 0
    set ::eskil($top,edit,opts) $pOpts
    foreach {name flag doc} $pOpts {
        ttk::label $w.l$n -text $name
        addBalloon $w.l$n $doc
        grid $w.l$n -sticky "w" -padx 3 -pady 3
        if {$flag} {
            ttk::checkbutton $w.s$n -text "On" \
                    -variable ::eskil($top,edit,$name)
            grid $w.s$n -row $n -column 1 -sticky "w" -padx 3 -pady 3
        } else {
            ttk::entry $w.s$n \
                    -textvariable ::eskil($top,edit,$name)
            grid $w.s$n -row $n -column 1 -sticky we -padx 3 -pady 3
        }
        incr n
    }
    grid columnconfigure $w 1 -weight 1
    if {$n == 0} {
        ttk::label $w.l -text "No specific configuration"
        grid $w.l -sticky "w" -padx 3 -pady 3
        return
    }
}

proc EditPrefPluginsOk {top wt} {
    destroy $wt
proc EditPrefPluginsOk {top wt apply} {
    if {!$apply} {
        destroy $wt
    }
    set ::eskil($top,pluginname) $::eskil($top,edit,pluginname)
    set ::eskil($top,plugininfo) $::eskil($top,edit,plugininfo)
    set ::eskil($top,pluginallow) $::eskil($top,edit,pluginallow)
    if {$::eskil($top,pluginname) ne ""} {
        set pinterp [createPluginInterp $::eskil($top,pluginname) \
                             $::eskil($top,plugininfo) \
                             $::eskil($top,pluginallow) pinfo]
    } else {
        set pinterp ""
        set pinfo ""
    }
    set ::eskil($top,plugin) $pinterp
    set ::eskil($top,pluginpinfo) $pinfo
    set ::eskil($top,pluginargv) {}
    foreach {name flag doc} $::eskil($top,edit,opts) {
        if {$flag} {
            if {[info exists ::eskil($top,edit,$name)] && \
                        $::eskil($top,edit,$name)} {
                lappend ::eskil($top,pluginargv) $name
            }
        } else {
            if {[info exists ::eskil($top,edit,$name)] && \
                        $::eskil($top,edit,$name) ne ""} {
                lappend ::eskil($top,pluginargv) $name $::eskil($top,edit,$name)
            }
        }
    }
}

# 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 src [LocatePlugin $plugin]
    set res [LocatePlugin $plugin]
    set src [dict get $res src]
    if {$src eq ""} return
    set ch [open $src]
    set data [read $ch]
    close $ch
    set wt $parent.plugin
    if {[winfo exists $wt]} {
        wm deiconify $wt