diff options
author | Ludovic Court`es | 2007-03-28 09:21:28 +0000 |
---|---|---|
committer | Ludovic Court`es | 2007-03-28 09:21:28 +0000 |
commit | 8142294b8c0874515385eff2102a4f3c83c1417a (patch) | |
tree | 20845b7463746098b9149c49b9fac18054a5ea87 /src | |
parent | 8518f2cf57d353c541b9a393e85a7ee34c321bd5 (diff) | |
parent | df6fd7f4a9d047224887258b6d9d3c57e272410b (diff) | |
download | skribilo-8142294b8c0874515385eff2102a4f3c83c1417a.tar.gz skribilo-8142294b8c0874515385eff2102a4f3c83c1417a.tar.lz skribilo-8142294b8c0874515385eff2102a4f3c83c1417a.zip |
`diff' package: Preserve space around diff'd strings.
* src/guile/skribilo/package/diff.scm: Use `srfi-14'.
(annotated-string-diff)[space-preserving-substring]: New.
Use it.
git-archimport-id: skribilo@sv.gnu.org--2006/skribilo--devo--1.2--patch-71
Diffstat (limited to 'src')
-rw-r--r-- | src/guile/skribilo/package/diff.scm | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/guile/skribilo/package/diff.scm b/src/guile/skribilo/package/diff.scm index 3aeed08..12eedb3 100644 --- a/src/guile/skribilo/package/diff.scm +++ b/src/guile/skribilo/package/diff.scm @@ -21,6 +21,7 @@ (define-module (skribilo package diff) :use-module (differ) :use-module (srfi srfi-1) + :use-module (srfi srfi-14) :use-module (srfi srfi-39) :use-module (ice-9 optargs) @@ -197,19 +198,45 @@ ;; Return a list (actually an AST) denoting the differences between STR1 ;; and STR2. The returned text is actually that of STR2 augmented with ;; `insertion', `deletion', `replacement', and `unchanged' markup. + + (define (space-preserving-substring str start end) + ;; Return the substring of STR, possibly inserting unbreakable spaces at + ;; the beginning/end if STR starts/ends in whitespaces. + (let ((len (- end start))) + (if (> len 0) + (let* ((lead (string-ref str start)) + (lead* (if (char-set-contains? char-set:whitespace + lead) + (~) + (string lead)))) + (if (> len 1) + (let* ((trail (string-ref str (- end 1))) + (trail* (if (char-set-contains? char-set:whitespace + trail) + (~) + (string trail)))) + (list lead* (substring str (+ start 1) (- end 1)) + trail*)) + (list lead* (substring str (+ start 1) end)))) + ""))) + (reverse! (fold (lambda (edit result) (let ((start (cadr edit)) (end (+ 1 (caddr edit)))) (cons (case (car edit) ((insertion) - (insertion (substring str2 start end))) + (insertion (space-preserving-substring str2 start + end))) ((deletion) - (deletion (substring str1 start end))) + (deletion (space-preserving-substring str1 start + end))) ((replacement) - (replacement (substring str2 start end))) + (replacement (space-preserving-substring str2 start + end))) ((unchanged) - (unchanged (substring str2 start end)))) + (unchanged (space-preserving-substring str2 start + end)))) result))) '() (string-diff-sequences str1 str2)))) |