Eskil

Diff
Login

Differences From Artifact [6b42e6ee61]:

To Artifact [959296f1cc]:


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
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
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

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







+
+
+
+



-
+





-




-
-
+
-
-
-






-
+




-
+
-
-
+
-



-
+



+

+


-
+

-
+








-
-
-
-
-
-
-
-
-
-

+
-
+


-
-
-
+
+
+





-
+

-
+



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





+







#  along with this program; see the file COPYING.  If not, write to
#  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#  Boston, MA 02111-1307, USA.
#
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

# Helpers, replace with sugar macros or mathops if available in 8.5
proc + {a b} { expr {$a + $b} }
proc - {a b} { expr {$a - $b} }

# Format a line number for printing
# It will always be 5 chars wide.
proc FormatLineno {lineno gray} {
proc FormatLineno {lineno} {
    if {[string is integer -strict $lineno]} {
        set res [format "%3d: " $lineno]
    } else {
        # Non-numerical linenumbers might turn up in some cases
        set res [format "%-5s" $lineno]
        set gray 0.9
    }
    if {[string length $res] > 5} {
        set res [string range $res end-5 end-1]
    }
    if {$gray eq "1.0"} {
        return $res
    return $res
    } else {
        return "\0bggray\{$gray\}$res\0bggray\{1.0\}"
    }
}

# Process the line numbers from the line number widget into a list
# of "linestarters"
proc ProcessLineno {w} {
    set tdump [$w dump -tag -text 1.0 end]
    set gray 1.0
    set tag ""
    set line ""
    set lines {}
    foreach {key value index} $tdump {
        if {$key eq "tagon"} {
            if {$value eq "change"} {
            if {$value eq "change" || [string match "new*" $value]} {
                set gray $::Pref(grayLevel1)
            } elseif {[string match "new*" $value]} {
                set tag $value
                set gray $::Pref(grayLevel2)
            }
        } elseif {$key eq "tagoff"} {
            if {$value eq "change" || [string match "new*" $value]} {
                set gray 1.0
                set tag ""
            }
        } elseif {$key eq "text"} {
            append line $value
            # Collect until end of line
            if {[string index $value end] eq "\n"} {
                # Clean everything but the line number
                set line [string trim [string trim $line] :]
                if {$line eq ""} {
                    lappend lines ""
                    lappend lines {}
                } else {
                    lappend lines [FormatLineno $line $gray]
                    lappend lines [list [FormatLineno $line] $tag]
                }
                set line ""
            }
        }
    }
    return $lines
}

# Handle wrapping of a too long line for printing
# The indentation of the wrapped line is 5 chars, same as a line number.
proc LineWrap {gray} {
    if {$gray eq "1.0"} {
        return "\n     "
    } else {
        return "\0bggray\{1.0\}\n     \0bggray\{$gray\}"
    }
}

# Prepare a text block for printing
# Index denotes where in the text widget this text starts. It is used to get
# Index denotes where this text starts. It is used to get tab expansion right.
# tab expansion right.
proc FixTextBlock {text index} {
    # Remove any form feed
    if {[regsub -all "\f" $text {} apa]} {
        set text $apa
    }
    set text [string map {\f {}} $text]

    # Extract column number from index
    regexp {\d+\.(\d+)} $index -> index

    # Expand tabs to 8 chars
    while 1 {
        set i [string first \t $text]
        if {$i eq -1} break
        if {$i == -1} break
        set n [expr {(- $i - $index - 1) % 8 + 1}]
        set text [string replace $text $i $i [format %${n}s ""]]
        set text [string replace $text $i $i [format %*s $n ""]]
    }
    return $text
}

# Format a line of text/tag pairs to enscript code
proc FormatLine {line} {
    set result ""
    foreach {text tag} $line {
        if {$tag eq ""} {
            append result $text
        } else {
            if {$tag eq "change"} {
                set gray $::Pref(grayLevel1)
            } elseif {[string match "new*" $tag]} {
                set gray $::Pref(grayLevel2)
            } else {
                # Should not happen
                set gray 1.0
                puts stderr "Bad tag in FormatLine: '$tag'"
            }
            append result "\0bggray\{$gray\}$text\0bggray\{1.0\}"
        }
    }
    return $result
}

# Main print function
proc PrintDiffs {top {quiet 0}} {
    busyCursor $top
    update idletasks

    set tmpFile [file nativename ~/eskil.enscript]
    if {$::diff($top,printFile) != ""} {
        set tmpFile2 [file nativename $::diff($top,printFile)]
    } else {
        set tmpFile2 [file nativename ~/eskil.ps]
    }

139
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170


171

172
173
174
175
176
177

178
179
180
181

182
183
184

185
186
187
188
189
190

191
192

193
194
195
196
197
198
199
200

201
202


203
204
205
206
207
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
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

185

186
187
188
189

190
191
192
193

194



195


196
197
198

199


200
201
202
203
204
205
206
207
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







-
+








-















+
+
-
+
-




-
+



-
+
-
-
-
+
-
-



-
+
-
-
+








+
-
-
+
+






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

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


-
+



-
+








+









-
+



-
+












+
+
+
-
+
+
+
+
+







            lineNo [list $lineNo1 $lineNo2] {
        ##nagelfar variable lineName varName
        ##nagelfar variable wrapName varName
        set lines {}
        set wraps {}
        set line [lindex $lineNo 0]
        set newline 0
        set gray 1.0
        set tag   {}
        set chars 0
        set wrapc 0
        foreach {key value index} $tdump {
            if {$key != "tagoff" && $newline == 1} {
                lappend lines $line
                lappend wraps $wrapc
                set newline 0
                set line [lindex $lineNo [llength $lines]]
                append line "\0bggray\{$gray\}"
                set chars 0
                set wrapc 0
            }
            switch $key {
                text {
                    set value [FixTextBlock $value $index]
                    if {[string index $value end] eq "\n"} {
                        set newline 1
                        set value [string trimright $value "\n"]
                    }
                    set len [string length $value]
                    while {$chars + $len > $wraplength} {
                        set wrap [expr {$wraplength - $chars}]
                        set val1 [string range $value 0 [expr {$wrap - 1}]]
                        set value [string range $value $wrap end]
                        # The newline has its own element to simplify finding
                        # it later.
                        append line $val1
                        lappend line $val1 $tag "\n" {} "     " {}
                        append line [LineWrap $gray]
                        set chars 5
                        incr wrapc
                        set len [string length $value]
                    }
                    append line $value
                    lappend line $value $tag
                    incr chars $len
                }
                tagon {
                    if {$value eq "change"} {
                    if {$value eq "change" || [string match "new*" $value]} {
                        set gray $::Pref(grayLevel1)
                        append line "\0bggray\{$gray\}"
                    } elseif {$value != "last"} {
                        set tag $value
                        set gray $::Pref(grayLevel2)
                        append line "\0bggray\{$gray\}"
                    }
                }
                tagoff {
                    if {$value != "last"} {
                    if {$value eq "change" || [string match "new*" $value]} {
                        append line "\0bggray\{1.0\}"
                        set gray 1.0
                        set tag {}
                    }
                }
            }
        }
        set $lineName $lines
        set $wrapName $wraps
    }

    # Go through both lists and put each wrapped line as one element.
    # Go through both lists and pad with empty lines as needed to accomodate
    # for wrapped lines in the other side.
    # Pad with empty lines as needed to accomodate for wrapped lines
    # in the other side.

    set wraplines1 {}
    set wraplines2 {}

    foreach l1 $lines1 l2 $lines2 w1 $wrap1 w2 $wrap2 {
        if {$w1 > 0} {
            set apa [split $l1 "\n"]
            set wraplines1 [concat $wraplines1 $apa]
        } else {
            lappend wraplines1 $l1
        }
            while {[set i [lsearch $l1 "\n"]] >= 0} {
                lappend wraplines1 [lrange $l1 0 [- $i 1]]
                set l1 [lrange $l1 [+ $i 2] end]
            }
        }
        lappend wraplines1 $l1

        if {$w2 > 0} {
            set apa [split $l2 "\n"]
            set wraplines2 [concat $wraplines2 $apa]
        } else {
            lappend wraplines2 $l2
        }
            while {[set i [lsearch $l2 "\n"]] >= 0} {
                lappend wraplines2 [lrange $l2 0 [- $i 1]]
                set l2 [lrange $l2 [+ $i 2] end]
            }
        }
        lappend wraplines2 $l2

        if {$w1 > $w2} {
            for {set t $w2} {$t < $w1} {incr t} {
                lappend wraplines2 ""
                lappend wraplines2 {}
            }
        } elseif {$w2 > $w1} {
            for {set t $w1} {$t < $w2} {incr t} {
                lappend wraplines1 ""
                lappend wraplines1 {}
            }
        }
    }

    # Write all lines to a file, taking one page at a time from each
    # side.

    set ch [open $tmpFile "w"]
    fconfigure $ch -encoding binary

    set len1 [llength $wraplines1]
    set len2 [llength $wraplines2]

    set i1 0
    set i2 0

    while {$i1 < $len1 && $i2 < $len2} {
        for {set i 0} {$i < $linesPerPage && $i1 < $len1} {incr i ; incr i1} {
            puts $ch [lindex $wraplines1 $i1]
            puts $ch [FormatLine [lindex $wraplines1 $i1]]
        }
        if {$i < $linesPerPage} {puts -nonewline $ch "\f"}
        for {set i 0} {$i < $linesPerPage && $i2 < $len2} {incr i ; incr i2} {
            puts $ch [lindex $wraplines2 $i2]
            puts $ch [FormatLine [lindex $wraplines2 $i2]]
        }
        if {$i < $linesPerPage} {puts -nonewline $ch "\f"}
    }

    close $ch

    # Run enscript to generate postscript

    if {$::tcl_platform(platform) eq "windows" &&\
            ![info exists ::env(ENSCRIPT_LIBRARY)]} {
        set ::env(ENSCRIPT_LIBRARY) [pwd]
    }
    if {[auto_execok enscript.bin] ne ""} {
        set enscriptCmd [list enscript.bin]
    } else {
    set enscriptCmd [list enscript -2jcre -L $linesPerPage -M A4]
        set enscriptCmd [list enscript]
    }

    lappend enscriptCmd -2jcre -L $linesPerPage -M A4

    if {$::Pref(wideLines)} {
        lappend enscriptCmd  -f Courier6
    }
    if {![regexp {^(.*)( \(.*?\))$} $::diff($top,leftLabel) -> lfile lrest]} {
        set lfile $::diff($top,leftLabel)
        set lrest ""
    }
277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312







-
+







    if {$::diff(prettyPrint) != ""} {
        lappend enscriptCmd -E$::diff(prettyPrint)
    }
    lappend enscriptCmd -p $tmpFile2 $tmpFile

    if {[catch {eval exec $enscriptCmd} result]} {
        if {[string index $result 0] != "\["} {
            tk_messageBox -message "Enscript error: $result"
            tk_messageBox -message "Enscript error: $result\ncmd: $enscriptCmd"
            return
        }
    }

    # Finished

    normalCursor $top