diff --git a/Makefile b/Makefile index 243ca43..6e6a2a0 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ testing.so: testing.o testing.o: testing.import.scm testing.import.scm: $(TESTING-SOURCES) -LISTING-SOURCES=listing.scm testing.import.scm +LISTING-SOURCES=listing.scm testing.import.scm ansi.import.scm listing.so: listing.o listing.o: listing.import.scm diff --git a/listing.scm b/listing.scm index fa7936a..8229910 100644 --- a/listing.scm +++ b/listing.scm @@ -36,7 +36,9 @@ (chicken base) (chicken string) (chicken format) - testing) + (chicken keyword) + testing + ansi) ;; Returns the number of digits required to represent a given number ;; in decimal format. @@ -108,46 +110,58 @@ ;; Prints and highlights a selection of source listing lines and ;; their optional context. - (define (print-source-listing lines highlights context hl-pre hl-post ctx-pre ctx-post ellipsis) - (let ((digits (number-digits (length lines)))) - (let loop ((lines lines) - (number 1) - (printed-something #f) - (was-printing #f)) - (when (not (null? lines)) - (let* ((content? (in-highlights? number highlights)) - (context? (and (not content?) - (line-near-targets? number highlights context))) - (print? (or content? context?))) - (cond (print? - (when (and printed-something - (not was-printing)) - (print ellipsis)) - (if content? - (display hl-pre) - (when context? - (display ctx-pre))) - (display (sprintf "~A~A~A" - (format-line-number number digits) - (car lines) - (let ((comment (highlight-comment number highlights))) - (if comment - (sprintf " # <<< ~A" comment) - "")))) - (if content? - (display hl-post) - (when context? - (display ctx-post))) - (newline) - (loop (cdr lines) - (+ number 1) - #t - #t)) - (else - (loop (cdr lines) - (+ number 1) - printed-something - #f)))))))) + (define (print-source-listing lines highlights . args) + (let ((highlight-rules (get-keyword #:highlight-rules args + (lambda () + '((error (ansi #:bold #:red) (ansi #:default)) + (warning (ansi #:yellow) (ansi #:default)) + (info (ansi #:cyan) (ansi #:default)) + )))) + (ellipsis (get-keyword #:ellipsis args (lambda () "..."))) + (ctx-pre (get-keyword #:context-pre args (lambda () ""))) + (ctx-post (get-keyword #:context-post args (lambda () ""))) + (hl-pre (get-keyword #:highlight-pre args (lambda () a:error))) + (hl-post (get-keyword #:highlight-post args (lambda () a:default))) + (context (get-keyword #:context args (lambda () 3)))) + (let ((digits (number-digits (length lines)))) + (let loop ((lines lines) + (number 1) + (printed-something #f) + (was-printing #f)) + (when (not (null? lines)) + (let* ((content? (in-highlights? number highlights)) + (context? (and (not content?) + (line-near-targets? number highlights context))) + (print? (or content? context?))) + (cond (print? + (when (and printed-something + (not was-printing)) + (print ellipsis)) + (if content? + (display hl-pre) + (when context? + (display ctx-pre))) + (display (sprintf "~A~A~A" + (format-line-number number digits) + (car lines) + (let ((comment (highlight-comment number highlights))) + (if comment + (sprintf " # <<< ~A" comment) + "")))) + (if content? + (display hl-post) + (when context? + (display ctx-post))) + (newline) + (loop (cdr lines) + (+ number 1) + #t + #t)) + (else + (loop (cdr lines) + (+ number 1) + printed-something + #f))))))))) ;; Performs self-tests of the listing module. (define (listing-tests!) diff --git a/member-print.scm b/member-print.scm index 9cca785..b2f3c14 100644 --- a/member-print.scm +++ b/member-print.scm @@ -139,10 +139,7 @@ (print-source-listing lines hls - -1 - a:error a:default - "" "" ; Not used - "..." ; Not used + #:context -1 ))) )