@@ -252,10 +252,16 @@ of context; the default of three should be good enough for most situations.")
252
252
(defclass diff ()
253
253
((original-pathname :initarg :original-pathname :accessor original-pathname)
254
254
(modified-pathname :initarg :modified-pathname :accessor modified-pathname)
255
+ (window-class :initarg :window-class :reader diff-window-class)
255
256
(windows :initform nil :accessor diff-windows)))
256
257
257
- (defclass unified-diff (diff) ())
258
- (defclass context-diff (diff) ())
258
+ (defclass unified-diff (diff) ()
259
+ (:default-initargs
260
+ :window-class ' unified-diff-window))
261
+
262
+ (defclass context-diff (diff) ()
263
+ (:default-initargs
264
+ :window-class ' context-diff-window))
259
265
260
266
(defclass diff-generator ()
261
267
((interned-lines :initarg :interned-lines :reader interner)
@@ -314,13 +320,8 @@ of context; the default of three should be good enough for most situations.")
314
320
(defun create-window (generator)
315
321
(create-window-for-diff (diff generator)))
316
322
317
- (defgeneric create-window-for-diff (diff))
318
-
319
- (defmethod create-window-for-diff ((diff unified-diff))
320
- (make-instance ' unified-diff-window))
321
-
322
- (defmethod create-window-for-diff ((context context-diff))
323
- (make-instance ' context-diff-window))
323
+ (defun create-window-for-diff (diff)
324
+ (make-instance (diff-window-class diff)))
324
325
325
326
(defun original-window-length (window)
326
327
(reduce #' + (window-chunks window)
@@ -463,6 +464,12 @@ of context; the default of three should be good enough for most situations.")
463
464
:original-pathname original-pathname
464
465
:modified-pathname modified-pathname)))
465
466
467
+ (defgeneric render-diff (diff stream )
468
+ (:documentation " Print DIFF object to STREAM" ))
469
+
470
+ (defgeneric render-diff-window (window stream )
471
+ (:documentation " Print WINDOW to STREAM" ))
472
+
466
473
(defun generate-diff (diff-kind original-pathname modified-pathname)
467
474
" Compute a diff between ORIGINAL-PATHNAME and MODIFIED-PATHNAME.
468
475
DIFF-KIND indicates the type of DIFF generated and should be the symbol
@@ -477,12 +484,21 @@ DIFF:UNIFIED-DIFF or DIFF:CONTEXT-DIFF."
477
484
original-pathname original
478
485
modified-pathname modified)))
479
486
(walk-diff-regions context diff-regions)))))
480
-
487
+
488
+ (defun format-diff (diff-kind original-pathname modified-pathname &optional (stream *standard-output* ))
489
+ (render-diff (generate-diff diff-kind
490
+ original-pathname
491
+ modified-pathname)
492
+ stream ))
493
+
494
+ (defun format-diff-string (diff-kind original-pathname modified-pathname)
495
+ (with-output-to-string (out)
496
+ (format-diff diff-kind original-pathname modified-pathname out)))
497
+
481
498
; ;; printing diffs on streams
482
499
483
- (defgeneric print-diff-window-header (window stream ))
484
500
485
- (defmethod print -diff-window-header ((window unified-diff-window) stream )
501
+ (defmethod render -diff-window :before ((window unified-diff-window) stream )
486
502
(let ((original-length (original-window-length window))
487
503
(modified-length (modified-window-length window)))
488
504
(format stream " @@ -~A " (1+ (original-start-line window)))
@@ -494,10 +510,10 @@ DIFF:UNIFIED-DIFF or DIFF:CONTEXT-DIFF."
494
510
(write-string " @@" stream )
495
511
(terpri stream )))
496
512
497
- (defmethod print -diff-window-header ((window context-diff-window) stream )
513
+ (defmethod render -diff-window :before ((window context-diff-window) stream )
498
514
(format stream " ***************~% " ))
499
515
500
- (defmethod print-object ((object unified-diff-window) stream )
516
+ (defmethod render-diff-window ((object unified-diff-window) stream )
501
517
(dolist (chunk (window-chunks object))
502
518
(let ((prefix (ecase (chunk-kind chunk)
503
519
(:common #\Space )
@@ -514,7 +530,7 @@ DIFF:UNIFIED-DIFF or DIFF:CONTEXT-DIFF."
514
530
(defun window-contains-inserts-p (window)
515
531
(some #' modified-chunk-p (window-chunks window)))
516
532
517
- (defmethod print-object ((window context-diff-window) stream )
533
+ (defmethod render-diff-window ((window context-diff-window) stream )
518
534
(let ((original-length (1- (original-window-length window)))
519
535
(original-start-line (1+ (original-start-line window)))
520
536
(modified-length (1- (modified-window-length window)))
@@ -551,23 +567,17 @@ DIFF:UNIFIED-DIFF or DIFF:CONTEXT-DIFF."
551
567
(write-string line stream )
552
568
(terpri stream ))))))))
553
569
554
- (defgeneric print-diff-header (diff stream ))
555
-
556
- (defmethod print-diff-header ((diff unified-diff) stream )
570
+ (defmethod render-diff :before ((diff unified-diff) stream )
557
571
(format stream " --- ~A~% +++ ~A~% "
558
572
(namestring (original-pathname diff))
559
573
(namestring (modified-pathname diff))))
560
574
561
- (defmethod print -diff-header ((diff context-diff) stream )
575
+ (defmethod render -diff :before ((diff context-diff) stream )
562
576
(format stream " *** ~A~% --- ~A~% "
563
577
(namestring (original-pathname diff))
564
578
(namestring (modified-pathname diff))))
565
579
566
- (defmethod print-object :before ((object diff-window) stream )
567
- (print-diff-window-header object stream ))
568
-
569
- (defmethod print-object ((object diff) stream )
570
- (print-diff-header object stream )
580
+ (defmethod render-diff ((object diff) stream )
571
581
(dolist (window (diff-windows object))
572
- (print-object window stream )))
582
+ (render-diff-window window stream )))
573
583
0 commit comments