Switch listings to use util-kwargs.

This commit is contained in:
Dominik Pantůček 2023-05-15 11:27:24 +02:00
parent 7bd4c7d222
commit 2ede889714
2 changed files with 61 additions and 59 deletions

View file

@ -96,7 +96,8 @@ TESTING-SOURCES=testing.scm
testing.o: testing.import.scm testing.o: testing.import.scm
testing.import.scm: $(TESTING-SOURCES) testing.import.scm: $(TESTING-SOURCES)
LISTING-SOURCES=listing.scm testing.import.scm ansi.import.scm LISTING-SOURCES=listing.scm testing.import.scm ansi.import.scm \
util-kwargs.import.scm
listing.o: listing.import.scm listing.o: listing.import.scm
listing.import.scm: $(LISTING-SOURCES) listing.import.scm: $(LISTING-SOURCES)

View file

@ -37,9 +37,9 @@
(chicken base) (chicken base)
(chicken string) (chicken string)
(chicken format) (chicken format)
(chicken keyword)
testing testing
ansi) ansi
util-kwargs)
;; Returns the number of digits required to represent a given number ;; Returns the number of digits required to represent a given number
;; in decimal format. ;; in decimal format.
@ -125,62 +125,63 @@
;; Prints and highlights a selection of source listing lines and ;; Prints and highlights a selection of source listing lines and
;; their optional context. ;; their optional context.
(define (print-source-listing lines highlights . args) (define-kwproc (print-source-listing
(let ((highlight-rules (get-keyword #:highlight-rules args lines
(lambda () highlights
`((error ,(ansi #:bold #:red) ,(ansi #:default)) (#:highlight-rules highlight-rules
(warning ,(ansi #:yellow) ,(ansi #:default)) `((error ,(ansi #:bold #:red) ,(ansi #:default))
(info ,(ansi #:cyan) ,(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 () ""))) (#:ellipsis ellipsis "...")
(ctx-post (get-keyword #:context-post args (lambda () ""))) (#:context-pre ctx-pre "")
(hl-pre (get-keyword #:highlight-pre args (lambda () a:error))) (#:context-post ctx-post "")
(hl-post (get-keyword #:highlight-post args (lambda () a:default))) (#:highlight-pre hl-pre a:error)
(context (get-keyword #:context args (lambda () 3)))) (#:highlight-post hl-post a:default)
(let ((digits (number-digits (length lines)))) (#:context context 3))
(let loop ((lines lines) (let ((digits (number-digits (length lines))))
(number 1) (let loop ((lines lines)
(printed-something #f) (number 1)
(was-printing #f)) (printed-something #f)
(when (not (null? lines)) (was-printing #f))
(let* ((highlight (match-highlight number highlights)) (when (not (null? lines))
(hl-type (if highlight (cadddr highlight) #f)) (let* ((highlight (match-highlight number highlights))
(hl-def (assq hl-type highlight-rules)) (hl-type (if highlight (cadddr highlight) #f))
(hl-pre-real (if hl-def (cadr hl-def) hl-pre)) (hl-def (assq hl-type highlight-rules))
(hl-post-real (if hl-def (caddr hl-def) hl-post)) (hl-pre-real (if hl-def (cadr hl-def) hl-pre))
(context? (and (not highlight) (hl-post-real (if hl-def (caddr hl-def) hl-post))
(line-near-targets? number highlights context))) (context? (and (not highlight)
(print? (or highlight context?))) (line-near-targets? number highlights context)))
(cond (print? (print? (or highlight context?)))
(when (and printed-something (cond (print?
(not was-printing)) (when (and printed-something
(print ellipsis)) (not was-printing))
(if highlight (print ellipsis))
(display hl-pre-real) (if highlight
(when context? (display hl-pre-real)
(display ctx-pre))) (when context?
(display (sprintf "~A~A~A" (display ctx-pre)))
(format-line-number number digits) (display (sprintf "~A~A~A"
(car lines) (format-line-number number digits)
(let ((comment (highlight-comment number highlights))) (car lines)
(if comment (let ((comment (highlight-comment number highlights)))
(sprintf " # <<< ~A" comment) (if comment
"")))) (sprintf " # <<< ~A" comment)
(if highlight ""))))
(display hl-post-real) (if highlight
(when context? (display hl-post-real)
(display ctx-post))) (when context?
(newline) (display ctx-post)))
(loop (cdr lines) (newline)
(+ number 1) (loop (cdr lines)
#t (+ number 1)
#t)) #t
(else #t))
(loop (cdr lines) (else
(+ number 1) (loop (cdr lines)
printed-something (+ number 1)
#f))))))))) printed-something
#f))))))))
;; Performs self-tests of the listing module. ;; Performs self-tests of the listing module.
(define (listing-tests!) (define (listing-tests!)