Debian Codegraph @

lib/text/diff.scm

raw
1 ;;;; diff.scm -- LCS for Scheme
2
3 ;;; Created: <2002-06-21 15:36:46 foof>
4 ;;; Time-stamp: <2003-02-15 00:09:55 foof>
5 ;;; Author: Alex Shinn <[email protected]>
6
7 (define-module text.diff
8 (use util.lcs)
9 (use util.match)
10 (export diff diff-report diff-report/context diff-report/unified)
11 )
12 (select-module text.diff)
13
14 ;; aux. fun to convert arg to a list
15 (define (source->list src reader)
16 (port->list reader
17 (cond [(port? src) src]
18 [(string? src) (open-input-string src)]
19 [else (error "don't know how to diff from:" src)])))
20
21 ;; lcs on text. Returns edit-list (as defined in lcs-edit-list).
22 (define (diff a b :key (reader read-line) (equal equal?))
23 (lcs-edit-list (source->list a reader)
24 (source->list b reader)
25 equal))
26
27 (define (write-line-diff line type)
28 (case type
29 [(+) (format #t "+ ~A\n" line)]
30 [(-) (format #t "- ~A\n" line)]
31 [else (format #t " ~A\n" line)]))
32
33 (define (diff-report a b :key (writer write-line-diff)
34 (reader read-line) (equal equal?))
35 (lcs-fold (^[line _] [writer line '-])
36 (^[line _] (writer line '+))
37 (^[line _] (writer line #f))
38 #f
39 (source->list a reader)
40 (source->list b reader)
41 equal))
42
43 (define (diff-report/context a b :key (writer write-line-diff)
44 (reader read-line)
45 (equal equal?)
46 (context-size (undefined)))
47 (define (show-lines half-hunk header-lead header-tail)
48 (match-let1 (start end . lines) half-hunk
49 ;; Traditionally, line number is 1-origin, and both ends are inclusive,
50 ;; so we need adjustment. As a special case, if (= start end),
51 ;; we only show a single index (it only occurs when that side is empty).
52 (if (= start end)
53 (format #t "~a 0 ~a\n" header-lead header-tail)
54 (format #t "~a ~d,~d ~a\n" header-lead (+ 1 start) end header-tail))
55 ;; If the lines are all unchanged, we don't emit it, following
56 ;; the context-diff format.
57 (unless (every (^e (eq? (car e) '=)) lines)
58 (dolist [line lines]
59 (match line
60 [('= x) (format #t " ~a\n" x)]
61 [('- x) (format #t "- ~a\n" x)]
62 [('+ x) (format #t "+ ~a\n" x)]
63 [('! x) (format #t "! ~a\n" x)])))))
64
65 (dolist [hunk (lcs-edit-list/context (source->list a reader)
66 (source->list b reader)
67 equal
68 :context-size context-size)]
69 (print "***************")
70 (show-lines (~ hunk 0) "***" "****")
71 (show-lines (~ hunk 1) "---" "----")))
72
73 (define (diff-report/unified a b :key (writer write-line-diff)
74 (reader read-line)
75 (equal equal?)
76 (context-size (undefined)))
77 (define (show-line start len)
78 (if (= len 1)
79 (format #t "~d" (+ start 1))
80 (format #t "~d,~d" (if (= len 0) 0 (+ start 1)) len)))
81
82 (define (show-hunk hunk)
83 (match-let1 #(as alen bs blen lines) hunk
84 (display "@@ -")
85 (show-line as alen)
86 (display " +")
87 (show-line bs blen)
88 (display " @@\n")
89 (dolist [line lines]
90 (match line
91 [('= x) (format #t " ~a\n" x)]
92 [('- x) (format #t "-~a\n" x)]
93 [('+ x) (format #t "+~a\n" x)]))))
94 (for-each show-hunk
95 (lcs-edit-list/unified (source->list a reader)
96 (source->list b reader)
97 equal
98 :context-size context-size)))