Eskil

Diff
Login

Differences From Artifact [dc7ebe7725]:

To Artifact [cf6cf540b5]:


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        }
    }
    return $res
}

# This is called when Ok or Apply is pressed.
# Update preference from dialog contents.
proc EditPrefRegsubOk {top w item {keep 0}} {
    set exa $::eskil($top,prefregexa)

    set result {}
    for {set t 1} {[info exists ::eskil($top,prefregexp$t)]} {incr t} {
        set RE $::eskil($top,prefregexp$t)
        set Sub $::eskil($top,prefregsub$t)
        set l $::eskil($top,prefregleft$t)
        set r $::eskil($top,prefregright$t)
        if {$RE eq ""} continue
        switch $::eskil($top,prefregtype$t) {
            Subst {
                lappend result $RE $Sub Subst
            }
            Prefix {
                lappend result $RE "" Prefix
            }
            default {
                set side ""
                if {$l && !$r} { set side left }
                if {!$l && $r} { set side right }
                if {!$l && !$r} { continue }

                if {[catch {regsub -all -- $RE $exa $Sub _} err]} {
                    return
                }
                lappend result $RE $Sub $side
            }
        }
    }

    set ::TmpPref(preprocess,re,$item) $result
    set ::TmpPref(preprocess,active,$item) 1

    if {$keep} {
        # Apply was pressed, also apply main dialog
        # TODO: Get widgets right. Right now it does not matter
        EditPrefPrepOk . $top 1
        return
    }
    destroy $w

    array unset ::eskil $top,prefregexp*
    array unset ::eskil $top,prefregsub*
    array unset ::eskil $top,prefregleft*
    array unset ::eskil $top,prefregright*
    array unset ::eskil $top,prefregtype*
}







|



















|
|


















|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        }
    }
    return $res
}

# This is called when Ok or Apply is pressed.
# Update preference from dialog contents.
proc EditPrefRegsubOk {top W item {keep 0}} {
    set exa $::eskil($top,prefregexa)

    set result {}
    for {set t 1} {[info exists ::eskil($top,prefregexp$t)]} {incr t} {
        set RE $::eskil($top,prefregexp$t)
        set Sub $::eskil($top,prefregsub$t)
        set l $::eskil($top,prefregleft$t)
        set r $::eskil($top,prefregright$t)
        if {$RE eq ""} continue
        switch $::eskil($top,prefregtype$t) {
            Subst {
                lappend result $RE $Sub Subst
            }
            Prefix {
                lappend result $RE "" Prefix
            }
            default {
                set side ""
                if {$l && !$r} { set side left }
                if { ! $l && $r} { set side right }
                if { ! $l && !$r} { continue }

                if {[catch {regsub -all -- $RE $exa $Sub _} err]} {
                    return
                }
                lappend result $RE $Sub $side
            }
        }
    }

    set ::TmpPref(preprocess,re,$item) $result
    set ::TmpPref(preprocess,active,$item) 1

    if {$keep} {
        # Apply was pressed, also apply main dialog
        # TODO: Get widgets right. Right now it does not matter
        EditPrefPrepOk . $top 1
        return
    }
    destroy $W

    array unset ::eskil $top,prefregexp*
    array unset ::eskil $top,prefregsub*
    array unset ::eskil $top,prefregleft*
    array unset ::eskil $top,prefregright*
    array unset ::eskil $top,prefregtype*
}
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Add a new entry in the preprocess dialog
proc AddPrefRegsub {top parent {type {}}} {
    # Figure out next number to use
    for {set t 1} {[winfo exists $parent.fr$t]} {incr t} {
        #Empty
    }
    # Default values
    if {![info exists ::eskil($top,prefregexp$t)]} {
        set ::eskil($top,prefregtype$t) Generic
        set ::eskil($top,prefregexp$t) ""
        set ::eskil($top,prefregexp$t) ""
        set ::eskil($top,prefregsub$t) ""
        set ::eskil($top,prefregleft$t) 1
        set ::eskil($top,prefregright$t) 1
    }
    # Override type if given
    if {$type ne ""} {
        set ::eskil($top,prefregtype$t) $type
    }

    set w [ttk::frame $parent.fr$t -borderwidth 2 -relief groove -padding 3]
    pack $w -side "top" -fill x -padx 3 -pady 3

    switch $::eskil($top,prefregtype$t) {
        Subst {
            ttk::label $w.l1 -text "Left:" -anchor "w"
            ttk::entryX $w.e1 -textvariable ::eskil($top,prefregexp$t) -width 20
            ttk::label $w.l2 -text "Right:" -anchor "w"
            ttk::entryX $w.e2 -textvariable ::eskil($top,prefregsub$t)
            grid $w.l1 $w.e1 $w.l2 $w.e2 -sticky we -padx 3 -pady 3
            grid columnconfigure $w {0 2} -uniform a
            grid columnconfigure $w {1 3} -weight 1 -uniform b

            addBalloon $w.l1 -fmt {
                Each pattern is applied to its side and substituted
                for a common unique string.
            }
        }
        Prefix {
            ttk::label $w.l1 -text "Prefix:" -anchor "w"
            ttk::entryX $w.e1 -textvariable ::eskil($top,prefregexp$t) -width 20
            grid $w.l1 $w.e1 -sticky we -padx 3 -pady 3
            grid columnconfigure $w 1 -weight 1
            addBalloon $w.l1 -fmt {
                Only one word that start with prefix is valid for line
                comparison.
            }
        }
        default {
            ttk::label $w.l1 -text "Regexp:" -anchor "w"
            ttk::entryX $w.e1 -textvariable ::eskil($top,prefregexp$t) -width 60
            ttk::label $w.l2 -text "Subst:" -anchor "w"
            ttk::entryX $w.e2 -textvariable ::eskil($top,prefregsub$t)
            ttk::checkbutton $w.cb1 -text "Left"  -variable ::eskil($top,prefregleft$t)
            ttk::checkbutton $w.cb2 -text "Right" -variable ::eskil($top,prefregright$t)
            addBalloon $w.cb1 "Apply to left file"
            addBalloon $w.cb2 "Apply to right file"

            grid $w.l1 $w.e1 $w.cb1 -sticky we -padx 3 -pady 3
            grid $w.l2 $w.e2 $w.cb2 -sticky we -padx 3 -pady 3
            grid columnconfigure $w 1 -weight 1
        }
    }

    trace add variable ::eskil($top,prefregexp$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregsub$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregleft$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregright$t) write \
            [list EditPrefRegsubUpdate $top]
}

# Editor for one item in ::Pref(preprocessn)
proc EditPrefRegsub {top item} {
    set w $top.prefregsub

    ToplevelForce $w "Preferences: Preprocess group"

    # Buttons
    ttk::frame $w.fb1 -padding 3
    ttk::button $w.fb1.b1 -text "Add" -command [list AddPrefRegsub $top $w Generic]
    addBalloon $w.fb1.b1 "Add generic pattern"
    ttk::button $w.fb1.b2 -text "Add Subst" -command [list AddPrefRegsub $top $w Subst]
    addBalloon $w.fb1.b2 "Add using substitution shortcut"
    ttk::button $w.fb1.b3 -text "Add Prefix" -command [list AddPrefRegsub $top $w Prefix]
    addBalloon $w.fb1.b3 "Add using prefix shortcut"
    grid $w.fb1.b1 $w.fb1.b2 $w.fb1.b3 -sticky we -ipadx 5 -padx 3 -pady 3
    grid columnconfigure $w.fb1 all -uniform a
    grid anchor $w.fb1 "w"

    # Result example part
    if {![info exists ::eskil($top,prefregexa)]} {
        set ::eskil($top,prefregexa) \
                "An example TextString FOR_REGSUB /* Comment */"
        set ::eskil($top,prefregexa2) \
                "An example TextString FOR_REGSUB /* Comment */"
    }
    ttk::labelframe $w.res -text "Preprocessing result" -padding 3
    ttk::label $w.res.l3 -text "Example 1:" -anchor "w"
    ttk::entryX $w.res.e3 -textvariable ::eskil($top,prefregexa) -width 60
    ttk::label $w.res.l4l -text "Result 1 L:" -anchor "w"
    ttk::label $w.res.l4r -text "Result 1 R:" -anchor "w"
    ttk::label $w.res.e4l -textvariable ::eskil($top,prefregresultl) \
            -anchor "w" -width 10
    ttk::label $w.res.e4r -textvariable ::eskil($top,prefregresultr) \
            -anchor "w" -width 10
    ttk::label $w.res.l5 -text "Example 2:" -anchor "w"
    ttk::entryX $w.res.e5 -textvariable ::eskil($top,prefregexa2)
    ttk::label $w.res.l6l -text "Result 2 L:" -anchor "w"
    ttk::label $w.res.l6r -text "Result 2 R:" -anchor "w"
    ttk::label $w.res.e6l -textvariable ::eskil($top,prefregresultl2) \
            -anchor "w" -width 10
    ttk::label $w.res.e6r -textvariable ::eskil($top,prefregresultr2) \
            -anchor "w" -width 10

    grid $w.res.l3  $w.res.e3  -sticky we -padx 3 -pady 3
    grid $w.res.l4l $w.res.e4l -sticky we -padx 3 -pady 3
    grid $w.res.l4r $w.res.e4r -sticky we -padx 3 -pady 3
    grid $w.res.l5  $w.res.e5  -sticky we -padx 3 -pady 3
    grid $w.res.l6l $w.res.e6l -sticky we -padx 3 -pady 3
    grid $w.res.l6r $w.res.e6r -sticky we -padx 3 -pady 3
    grid columnconfigure $w.res 1 -weight 1

    # Buttons
    ttk::frame $w.fb -padding 3
    ttk::button $w.fb.b1 -text "Ok"     -command [list EditPrefRegsubOk $top $w $item]
    ttk::button $w.fb.b2 -text "Apply"  -command [list EditPrefRegsubOk $top $w $item 1]
    ttk::button $w.fb.b3 -text "Cancel" -command [list destroy $w]
    set ::widgets($top,prefRegsubOk) $w.fb.b1
    set ::widgets($top,prefRegsubApply) $w.fb.b2

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

    # Top layout
    pack $w.fb1 -side "top" -fill x -padx 3 -pady 3
    pack $w.fb $w.res -side bottom -fill x -padx 3 -pady 3

    # Fill in existing or an empty line
    set preprocess $::TmpPref(preprocess,re,$item)

    if {[llength $preprocess] == 0} {
        AddPrefRegsub $top $w Generic
    } else {
        set t 1
        foreach {RE Sub side} $preprocess {
            set ::eskil($top,prefregexp$t) $RE
            set ::eskil($top,prefregsub$t) $Sub
            set ::eskil($top,prefregleft$t) 0
            set ::eskil($top,prefregright$t) 0
            set ::eskil($top,prefregtype$t) Generic
            if {$side in {Subst Prefix}} {
                set ::eskil($top,prefregtype$t) $side
            } else {
                if {$side eq "" || $side eq "left"} {
                    set ::eskil($top,prefregleft$t) 1
                }
                if {$side eq "" || $side eq "right"} {
                    set ::eskil($top,prefregright$t) 1
                }
            }
            AddPrefRegsub $top $w
            incr t
        }
    }

    trace add variable ::eskil($top,prefregexa) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregexa2) write \
            [list EditPrefRegsubUpdate $top]
    EditPrefRegsubUpdate $top
}


# This is called when Ok or Apply is pressed.
proc EditPrefPrepOk {top w {keep 0}} {
    # Update preference from dialog contents.
    set new {}
    for {set r 1} {$r <= $::TmpPref(preprocess,n)} {incr r} {
        set name $::TmpPref(preprocess,name,$r)
        set act $::TmpPref(preprocess,active,$r)
        set save $::TmpPref(preprocess,save,$r)
        set re $::TmpPref(preprocess,re,$r)
        lappend new $name
        lappend new [dict create active $act "save" $save preprocess $re]
    }
    set ::Pref(preprocessn) $new
    
    if {$keep} return
    destroy $w
}

# Create a toplevel, even if it exists
proc ToplevelForce {w title} {
    destroy $w
    ttk::toplevel $w -padx 3 -pady 3
    wm title $w $title
}

# Move an item one step up
proc EditPrefPreUp {r} {
    puts EditPrefPreUp$r
    # Sanity check
    if {$r <= 1 || $r > $::TmpPref(preprocess,n)} {
        return
    }
    set p [expr {$r - 1}]
    foreach item {name active save re} {
        set tmp $::TmpPref(preprocess,$item,$r)
        set ::TmpPref(preprocess,$item,$r) $::TmpPref(preprocess,$item,$p)
        set ::TmpPref(preprocess,$item,$p) $tmp
    }
}

proc EditPrefPreprocessAddItem {w autoEdit} {
    set r $::TmpPref(preprocess,n)
    incr r
    if {![info exists ::TmpPref(preprocess,name,$r)]} {
        set ::TmpPref(preprocess,name,$r) ""
        set ::TmpPref(preprocess,active,$r) 0
        set ::TmpPref(preprocess,save,$r) 0
        set ::TmpPref(preprocess,re,$r) ""
    }
    ttk::entry $w.fp.ne$r -textvariable ::TmpPref(preprocess,name,$r)
    addBalloon $w.fp.ne$r "Name of preprocess group (optional)"
    ttk::checkbutton $w.fp.cba$r -text "Active" \
            -variable ::TmpPref(preprocess,active,$r)
    addBalloon $w.fp.cba$r "Activate group for this session"
    ttk::checkbutton $w.fp.cbs$r -text "Save" \
            -variable ::TmpPref(preprocess,save,$r)
    addBalloon $w.fp.cbs$r "Save group when preferences are saved"
    ttk::button $w.fp.be$r -text "Edit" \
            -command [list EditPrefRegsub $w $r]
    addBalloon $w.fp.be$r "Edit the associated list of regexps"
    if {$autoEdit} {
        after idle [list after 50 [list $w.fp.be$r invoke]]
    }
    ttk::button $w.fp.bu$r -image $::img(up) \
            -command [list EditPrefPreUp $r]
    addBalloon $w.fp.bu$r "Move group up in list"
    grid $w.fp.ne$r $w.fp.cba$r $w.fp.cbs$r $w.fp.be$r $w.fp.bu$r -sticky we \
            -padx 3 -pady 3
    # Make buttons symmetric
    grid  $w.fp.be$r $w.fp.bu$r -sticky news

    set ::TmpPref(preprocess,n) $r
}

proc EditPrefPreprocess {top} {
    set w $top.prefpreprocess

    # Make a working copy more suitable for GUI connection
    set r 0
    foreach {name data} $::Pref(preprocessn) {
        incr r
        set ::TmpPref(preprocess,name,$r) $name
        set ::TmpPref(preprocess,active,$r) [dict get $data active]
        set ::TmpPref(preprocess,save,$r) [dict get $data save]
        set ::TmpPref(preprocess,re,$r) [dict get $data preprocess]
    }
    # Create one if there is none, to simplify GUI usage
    set autoEdit 0
    if {$r == 0} {
        set autoEdit 1
        incr r
    }
    set ::TmpPref(preprocess,n) 0
    set nItems $r

    ToplevelForce $w "Preferences: Preprocess"

    # Frame for List of preprocessing
    ttk::frame $w.fp -padding 3
    grid columnconfigure $w.fp 0 -weight 1
    
    for {set r 1} {$r <= $nItems} {incr r} {
        EditPrefPreprocessAddItem $w $autoEdit
    }

    # Frame for action buttons
    ttk::frame $w.fa -padding 3
    ttk::button $w.fa.b1 -text "Add" \
            -command [list EditPrefPreprocessAddItem $w 1]
    addBalloon $w.fa.b1 "Add a preprocess group"

    grid $w.fa.b1 -sticky we
    grid columnconfigure $w.fa {0 2 4} -uniform a
    grid columnconfigure $w.fa {1 3} -weight 1

    
    # Frame for dialog Buttons
    ttk::frame $w.fb -padding 3
    ttk::button $w.fb.b1 -text "Ok"     -command [list EditPrefPrepOk $top $w]
    ttk::button $w.fb.b2 -text "Apply"  -command [list EditPrefPrepOk $top $w 1]
    ttk::button $w.fb.b3 -text "Cancel" -command [list destroy $w]

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

    # Top layout
    pack $w.fb -side bottom -fill x
    pack $w.fa -side bottom -fill x
    pack $w.fp -side "top" -fill both -expand 1
}







|












|
|



|
|
|
|
|
|
|

|





|
|
|
|
|





|
|
|
|
|
|
|
|

|
|
|















|

|


|
|
|
|
|
|
|
|
|
|


|





|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|


|
|
|
|
|
|

|
|
|


|
|





|


















|













|













|



|
|
|
|



|
|

|


|

|
|
|



|


|





|
|
|

|
|

|
|
|
|

|

|

|
|


|





|



















|


|
|


|



|
|
|
|

|
|
|



|
|
|
|

|
|
|


|
|
|

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Add a new entry in the preprocess dialog
proc AddPrefRegsub {top parent {type {}}} {
    # Figure out next number to use
    for {set t 1} {[winfo exists $parent.fr$t]} {incr t} {
        #Empty
    }
    # Default values
    if { ! [info exists ::eskil($top,prefregexp$t)]} {
        set ::eskil($top,prefregtype$t) Generic
        set ::eskil($top,prefregexp$t) ""
        set ::eskil($top,prefregexp$t) ""
        set ::eskil($top,prefregsub$t) ""
        set ::eskil($top,prefregleft$t) 1
        set ::eskil($top,prefregright$t) 1
    }
    # Override type if given
    if {$type ne ""} {
        set ::eskil($top,prefregtype$t) $type
    }

    set W [ttk::frame $parent.fr$t -borderwidth 2 -relief groove -padding 3]
    pack $W -side "top" -fill x -padx 3 -pady 3

    switch $::eskil($top,prefregtype$t) {
        Subst {
            ttk::label $W.l1 -text "Left:" -anchor w
            ttk::entryX $W.e1 -textvariable ::eskil($top,prefregexp$t) -width 20
            ttk::label $W.l2 -text "Right:" -anchor w
            ttk::entryX $W.e2 -textvariable ::eskil($top,prefregsub$t)
            grid $W.l1 $W.e1 $W.l2 $W.e2 -sticky we -padx 3 -pady 3
            grid columnconfigure $W {0 2} -uniform a
            grid columnconfigure $W {1 3} -weight 1 -uniform b

            addBalloon $W.l1 -fmt {
                Each pattern is applied to its side and substituted
                for a common unique string.
            }
        }
        Prefix {
            ttk::label $W.l1 -text "Prefix:" -anchor w
            ttk::entryX $W.e1 -textvariable ::eskil($top,prefregexp$t) -width 20
            grid $W.l1 $W.e1 -sticky we -padx 3 -pady 3
            grid columnconfigure $W 1 -weight 1
            addBalloon $W.l1 -fmt {
                Only one word that start with prefix is valid for line
                comparison.
            }
        }
        default {
            ttk::label $W.l1 -text "Regexp:" -anchor w
            ttk::entryX $W.e1 -textvariable ::eskil($top,prefregexp$t) -width 60
            ttk::label $W.l2 -text "Subst:" -anchor w
            ttk::entryX $W.e2 -textvariable ::eskil($top,prefregsub$t)
            ttk::checkbutton $W.cb1 -text "Left"  -variable ::eskil($top,prefregleft$t)
            ttk::checkbutton $W.cb2 -text "Right" -variable ::eskil($top,prefregright$t)
            addBalloon $W.cb1 "Apply to left file"
            addBalloon $W.cb2 "Apply to right file"

            grid $W.l1 $W.e1 $W.cb1 -sticky we -padx 3 -pady 3
            grid $W.l2 $W.e2 $W.cb2 -sticky we -padx 3 -pady 3
            grid columnconfigure $W 1 -weight 1
        }
    }

    trace add variable ::eskil($top,prefregexp$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregsub$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregleft$t) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregright$t) write \
            [list EditPrefRegsubUpdate $top]
}

# Editor for one item in ::Pref(preprocessn)
proc EditPrefRegsub {top item} {
    set W $top.prefregsub

    ToplevelForce $W "Preferences: Preprocess group"

    # Buttons
    ttk::frame $W.fb1 -padding 3
    ttk::button $W.fb1.b1 -text "Add" -command [list AddPrefRegsub $top $W Generic]
    addBalloon $W.fb1.b1 "Add generic pattern"
    ttk::button $W.fb1.b2 -text "Add Subst" -command [list AddPrefRegsub $top $W Subst]
    addBalloon $W.fb1.b2 "Add using substitution shortcut"
    ttk::button $W.fb1.b3 -text "Add Prefix" -command [list AddPrefRegsub $top $W Prefix]
    addBalloon $W.fb1.b3 "Add using prefix shortcut"
    grid $W.fb1.b1 $W.fb1.b2 $W.fb1.b3 -sticky we -ipadx 5 -padx 3 -pady 3
    grid columnconfigure $W.fb1 all -uniform a
    grid anchor $W.fb1 w

    # Result example part
    if { ! [info exists ::eskil($top,prefregexa)]} {
        set ::eskil($top,prefregexa) \
                "An example TextString FOR_REGSUB /* Comment */"
        set ::eskil($top,prefregexa2) \
                "An example TextString FOR_REGSUB /* Comment */"
    }
    ttk::labelframe $W.res -text "Preprocessing result" -padding 3
    ttk::label $W.res.l3 -text "Example 1:" -anchor w
    ttk::entryX $W.res.e3 -textvariable ::eskil($top,prefregexa) -width 60
    ttk::label $W.res.l4l -text "Result 1 L:" -anchor w
    ttk::label $W.res.l4r -text "Result 1 R:" -anchor w
    ttk::label $W.res.e4l -textvariable ::eskil($top,prefregresultl) \
            -anchor w -width 10
    ttk::label $W.res.e4r -textvariable ::eskil($top,prefregresultr) \
            -anchor w -width 10
    ttk::label $W.res.l5 -text "Example 2:" -anchor w
    ttk::entryX $W.res.e5 -textvariable ::eskil($top,prefregexa2)
    ttk::label $W.res.l6l -text "Result 2 L:" -anchor w
    ttk::label $W.res.l6r -text "Result 2 R:" -anchor w
    ttk::label $W.res.e6l -textvariable ::eskil($top,prefregresultl2) \
            -anchor w -width 10
    ttk::label $W.res.e6r -textvariable ::eskil($top,prefregresultr2) \
            -anchor w -width 10

    grid $W.res.l3  $W.res.e3  -sticky we -padx 3 -pady 3
    grid $W.res.l4l $W.res.e4l -sticky we -padx 3 -pady 3
    grid $W.res.l4r $W.res.e4r -sticky we -padx 3 -pady 3
    grid $W.res.l5  $W.res.e5  -sticky we -padx 3 -pady 3
    grid $W.res.l6l $W.res.e6l -sticky we -padx 3 -pady 3
    grid $W.res.l6r $W.res.e6r -sticky we -padx 3 -pady 3
    grid columnconfigure $W.res 1 -weight 1

    # Buttons
    ttk::frame $W.fb -padding 3
    ttk::button $W.fb.b1 -text "Ok"     -command [list EditPrefRegsubOk $top $W $item]
    ttk::button $W.fb.b2 -text "Apply"  -command [list EditPrefRegsubOk $top $W $item 1]
    ttk::button $W.fb.b3 -text "Cancel" -command [list destroy $W]
    set ::widgets($top,prefRegsubOk) $W.fb.b1
    set ::widgets($top,prefRegsubApply) $W.fb.b2

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

    # Top layout
    pack $W.fb1 -side "top" -fill x -padx 3 -pady 3
    pack $W.fb $W.res -side bottom -fill x -padx 3 -pady 3

    # Fill in existing or an empty line
    set preprocess $::TmpPref(preprocess,re,$item)

    if {[llength $preprocess] == 0} {
        AddPrefRegsub $top $W Generic
    } else {
        set t 1
        foreach {RE Sub side} $preprocess {
            set ::eskil($top,prefregexp$t) $RE
            set ::eskil($top,prefregsub$t) $Sub
            set ::eskil($top,prefregleft$t) 0
            set ::eskil($top,prefregright$t) 0
            set ::eskil($top,prefregtype$t) Generic
            if {$side in {Subst Prefix}} {
                set ::eskil($top,prefregtype$t) $side
            } else {
                if {$side eq "" || $side eq "left"} {
                    set ::eskil($top,prefregleft$t) 1
                }
                if {$side eq "" || $side eq "right"} {
                    set ::eskil($top,prefregright$t) 1
                }
            }
            AddPrefRegsub $top $W
            incr t
        }
    }

    trace add variable ::eskil($top,prefregexa) write \
            [list EditPrefRegsubUpdate $top]
    trace add variable ::eskil($top,prefregexa2) write \
            [list EditPrefRegsubUpdate $top]
    EditPrefRegsubUpdate $top
}


# This is called when Ok or Apply is pressed.
proc EditPrefPrepOk {top W {keep 0}} {
    # Update preference from dialog contents.
    set new {}
    for {set r 1} {$r <= $::TmpPref(preprocess,n)} {incr r} {
        set name $::TmpPref(preprocess,name,$r)
        set act $::TmpPref(preprocess,active,$r)
        set save $::TmpPref(preprocess,save,$r)
        set re $::TmpPref(preprocess,re,$r)
        lappend new $name
        lappend new [dict create active $act "save" $save preprocess $re]
    }
    set ::Pref(preprocessn) $new
    
    if {$keep} return
    destroy $W
}

# Create a toplevel, even if it exists
proc ToplevelForce {W title} {
    destroy $W
    ttk::toplevel $W -padx 3 -pady 3
    wm title $W $title
}

# Move an item one step up
proc EditPrefPreUp {rI} {
    #puts EditPrefPreUp$rI
    # Sanity check
    if {$rI <= 1 || $rI > $::TmpPref(preprocess,n)} {
        return
    }
    set pI [expr {$rI - 1}]
    foreach item {name active save re} {
        set tmp $::TmpPref(preprocess,$item,$rI)
        set ::TmpPref(preprocess,$item,$rI) $::TmpPref(preprocess,$item,$pI)
        set ::TmpPref(preprocess,$item,$pI) $tmp
    }
}

proc EditPrefPreprocessAddItem {W autoEdit} {
    set r $::TmpPref(preprocess,n)
    incr r
    if { ! [info exists ::TmpPref(preprocess,name,$r)]} {
        set ::TmpPref(preprocess,name,$r) ""
        set ::TmpPref(preprocess,active,$r) 0
        set ::TmpPref(preprocess,save,$r) 0
        set ::TmpPref(preprocess,re,$r) ""
    }
    ttk::entry $W.fp.ne$r -textvariable ::TmpPref(preprocess,name,$r)
    addBalloon $W.fp.ne$r "Name of preprocess group (optional)"
    ttk::checkbutton $W.fp.cba$r -text "Active" \
            -variable ::TmpPref(preprocess,active,$r)
    addBalloon $W.fp.cba$r "Activate group for this session"
    ttk::checkbutton $W.fp.cbs$r -text "Save" \
            -variable ::TmpPref(preprocess,save,$r)
    addBalloon $W.fp.cbs$r "Save group when preferences are saved"
    ttk::button $W.fp.be$r -text "Edit" \
            -command [list EditPrefRegsub $W $r]
    addBalloon $W.fp.be$r "Edit the associated list of regexps"
    if {$autoEdit} {
        after idle [list after 50 [list $W.fp.be$r invoke]]
    }
    ttk::button $W.fp.bu$r -image $::img(up) \
            -command [list EditPrefPreUp $r]
    addBalloon $W.fp.bu$r "Move group up in list"
    grid $W.fp.ne$r $W.fp.cba$r $W.fp.cbs$r $W.fp.be$r $W.fp.bu$r -sticky we \
            -padx 3 -pady 3
    # Make buttons symmetric
    grid  $W.fp.be$r $W.fp.bu$r -sticky news

    set ::TmpPref(preprocess,n) $r
}

proc EditPrefPreprocess {top} {
    set W $top.prefpreprocess

    # Make a working copy more suitable for GUI connection
    set r 0
    foreach {name data} $::Pref(preprocessn) {
        incr r
        set ::TmpPref(preprocess,name,$r) $name
        set ::TmpPref(preprocess,active,$r) [dict get $data active]
        set ::TmpPref(preprocess,save,$r) [dict get $data save]
        set ::TmpPref(preprocess,re,$r) [dict get $data preprocess]
    }
    # Create one if there is none, to simplify GUI usage
    set autoEdit 0
    if {$r == 0} {
        set autoEdit 1
        incr r
    }
    set ::TmpPref(preprocess,n) 0
    set nItems $r

    ToplevelForce $W "Preferences: Preprocess"

    # Frame for List of preprocessing
    ttk::frame $W.fp -padding 3
    grid columnconfigure $W.fp 0 -weight 1
    
    for {set r 1} {$r <= $nItems} {incr r} {
        EditPrefPreprocessAddItem $W $autoEdit
    }

    # Frame for action buttons
    ttk::frame $W.fa -padding 3
    ttk::button $W.fa.b1 -text "Add" \
            -command [list EditPrefPreprocessAddItem $W 1]
    addBalloon $W.fa.b1 "Add a preprocess group"

    grid $W.fa.b1 -sticky we
    grid columnconfigure $W.fa {0 2 4} -uniform a
    grid columnconfigure $W.fa {1 3} -weight 1

    
    # Frame for dialog Buttons
    ttk::frame $W.fb -padding 3
    ttk::button $W.fb.b1 -text "Ok"     -command [list EditPrefPrepOk $top $W]
    ttk::button $W.fb.b2 -text "Apply"  -command [list EditPrefPrepOk $top $W 1]
    ttk::button $W.fb.b3 -text "Cancel" -command [list destroy $W]

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

    # Top layout
    pack $W.fb -side bottom -fill x
    pack $W.fa -side bottom -fill x
    pack $W.fp -side "top" -fill both -expand 1
}