3 exec ${GUILE-guile} -L $(dirname 0) -e '(diff)' -s "$0" "$@"
6 ;;; Mes --- Maxwell Equations of Software
7 ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
9 ;;; mes-snarf.scm: This file is part of Mes.
11 ;;; Mes is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; Mes is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-9)
27 #:use-module (ice-9 rdelim)
32 (define %scheme "mes"))
34 (define %scheme "guile")
35 (define-macro (mes-use-module . rest) #t))
37 (use-modules (ice-9 syncase))
38 (define %scheme "guile")
39 (define-macro (mes-use-module . rest) #t)))
41 (mes-use-module (mes guile))
43 (format (current-error-port) "diff[~a]...\n" %scheme)
46 (string-append "+" a))
48 (string-append "-" a))
50 (string-append " " a))
52 (define-record-type <hunk> (make-hunk context after removed added)
54 (context hunk.context)
56 (removed hunk.removed)
59 (define (hunk->lines o)
60 (append `(,(format #f "@@ -~a,~a +~a,~a" (length (hunk.removed o)) (+ 3 (car (hunk.context o))) (length (hunk.added o)) (+ 3 (cadr (hunk.context o))))
61 ,@(map keep (filter identity (cddr (hunk.context o)))))
62 (map minus (hunk.removed o))
63 (map plus (hunk.added o))
64 (map keep (hunk.after o))))
66 (define (safe-list-head lst n)
67 (list-head lst (min n (length lst))))
71 (let ((a-lines (string-split (with-input-from-file a read-string) #\newline))
72 (b-lines (string-split (with-input-from-file b read-string) #\newline)))
73 (let loop ((context '(1 1 #f #f #f)) (a-lines a-lines) (b-lines b-lines))
74 ;;(format (current-error-port) "loop context=~s\n" context)
75 (cond ((and (null? a-lines) (null? b-lines)) '())
77 (list (make-hunk context (safe-list-head a-lines 3) '() b-lines)))
79 (list (make-hunk context (safe-list-head a-lines 3) a-lines '())))
80 ((equal? (car a-lines) (car b-lines))
81 (loop `(,(1+ (car context))
85 (cdr a-lines) (cdr b-lines)))
87 (cond ((and (pair? (cdr b-lines)) (equal? (car a-lines) (cadr b-lines)))
88 (cons (make-hunk context (safe-list-head a-lines 3) '() (list (car b-lines)))
89 (loop `(,(+ 1 (car context))
93 (cdr a-lines) (cddr b-lines))))
94 ((and (pair? (cdr a-lines)) (equal? (cadr a-lines) (car b-lines)))
95 (cons (make-hunk context (safe-list-head a-lines 3) (list (car a-lines)) '())
96 (loop `(,(+ 2 (car context))
101 (cddr a-lines) (cdr b-lines))))
102 (else (cons (make-hunk context (safe-list-head a-lines 3) (list (car a-lines)) (list (car b-lines)))
103 (loop `(,(1+ (car context))
107 (cdr a-lines) (cdr b-lines))))))))))
110 (let* ((files (cdr args))
111 (files (if (equal? (car files) "-u") (cdr files) files))
112 (hunks (apply diff (list-head files 2))))
114 (display (string-join (append-map hunk->lines hunks) "\n"))