summaryrefslogtreecommitdiff
path: root/support/dinbrief-gui/dinbrief.vfs/hyphen.tcl
blob: 1e1deb1720833f39797d848038b53d86ba331e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
namespace eval global {
    variable hyphenFile [file join $defaultDir hyphen.txt]
    array set hyphen {}
}

proc global::loadHyphenation {} {
    variable hyphenFile
    variable hyphen
    if {[file exists $hyphenFile]} {
	array unset hyphen
	array set hyphen [[lambda {file} {
	    set result {}
	    foreach name [cat $file] {
		lappend result [string map {- {}} $name] $name
	    }
	    set result
	}] $hyphenFile]
    }
}

namespace eval global {
    namespace export loadHyphenation
}

namespace import -force global::loadHyphenation

after idle loadHyphenation

proc global::hyphenWordPattern {word pattern} {
    set fragList [split $pattern -]
    set endIndex -1
    set startIndex 0
    set result {}
    foreach frag $fragList {
	set l [string length $frag]
	incr endIndex $l
	lappend result [string range $word $startIndex $endIndex]
	incr startIndex $l
    }
    join $result \u00ad
}

proc global::hyphenated {l} {
    set words [regexp -inline -all {[[:alnum:]]+} $l]
    set puncts [regexp -inline -all {[^[:alnum:]]+} $l]
    if {[regexp {[^[:alnum:]]} [string index $l 0]]} {
	set words [concat [list ""] $words]
    } 
    set result ""
    variable hyphen
    foreach word $words punct $puncts {
	set word_ [string tolower $word]
	if {[info exists hyphen($word_)]} {
	    append result [hyphenWordPattern $word $hyphen($word_)]
	} else {
	    append result $word
	}
	append result $punct
    }
    set result
}

proc global::editHyphenations {} {
    variable hyphenFile
    if {[winfo exists .hyphenation]} {
	raise .hyphenation
	focus -force .hyphenation
	return
    }
    toplevel .hyphenation
    wm title .hyphenation Silbentrennung
    wm transient .hyphenation .
    pack [scrolledtext .hyphenation.editor -width 30] -expand yes -fill both
    variable hyphenFile
    if {[file exists $hyphenFile]} {
	.hyphenation.editor insert end\
	    [join [lsort [string tolower [cat $hyphenFile]]] \n]
    }
    wm protocol .hyphenation WM_DELETE_WINDOW [subst -nocommand {
	saveString [.hyphenation.editor get 1.0 end-1chars] $hyphenFile
	destroy .hyphenation
	loadHyphenation
    }]
}

namespace eval global {
    namespace export hyphenated editHyphenations 
}

namespace import -force\
    global::hyphenated\
    global::editHyphenations