codeblock line numbers
original commit: 0c4d90b1db8ce9a01215ac413505314349c852b3
This commit is contained in:
parent
442283d9be
commit
c9595e0a7a
|
@ -25,7 +25,10 @@
|
||||||
#:defaults ([keep-lang-line?-expr #'#t])
|
#:defaults ([keep-lang-line?-expr #'#t])
|
||||||
#:name "#:keep-lang-line? keyword")
|
#:name "#:keep-lang-line? keyword")
|
||||||
(~optional (~seq #:context context-expr:expr)
|
(~optional (~seq #:context context-expr:expr)
|
||||||
#:name "#:context keyword"))
|
#:name "#:context keyword")
|
||||||
|
(~optional (~seq #:line-numbers line-numbers:expr)
|
||||||
|
#:defaults ([line-numbers #'#f])
|
||||||
|
#:name "#:line-numbers keyword"))
|
||||||
...)
|
...)
|
||||||
str ...)
|
str ...)
|
||||||
#`(typeset-code str ...
|
#`(typeset-code str ...
|
||||||
|
@ -38,7 +41,8 @@
|
||||||
(let ([v #'(str ...)])
|
(let ([v #'(str ...)])
|
||||||
(and (pair? (syntax-e v))
|
(and (pair? (syntax-e v))
|
||||||
#`#'#,(car (syntax-e v))))
|
#`#'#,(car (syntax-e v))))
|
||||||
#'#f)))]))
|
#'#f))
|
||||||
|
#:line-numbers line-numbers)]))
|
||||||
|
|
||||||
(define (code-inset p)
|
(define (code-inset p)
|
||||||
(make-nested-flow (make-style 'code-inset '()) (list p)))
|
(make-nested-flow (make-style 'code-inset '()) (list p)))
|
||||||
|
@ -50,6 +54,7 @@
|
||||||
#:expand [expand #f]
|
#:expand [expand #f]
|
||||||
#:indent [indent 2]
|
#:indent [indent 2]
|
||||||
#:keep-lang-line? [keep-lang-line? #t]
|
#:keep-lang-line? [keep-lang-line? #t]
|
||||||
|
#:line-numbers [line-numbers #f]
|
||||||
. strs)
|
. strs)
|
||||||
(let* ([str (apply string-append strs)]
|
(let* ([str (apply string-append strs)]
|
||||||
[bstr (string->bytes/utf-8 (regexp-replace* #rx"(?m:^$)" str "\xA0"))]
|
[bstr (string->bytes/utf-8 (regexp-replace* #rx"(?m:^$)" str "\xA0"))]
|
||||||
|
@ -157,6 +162,7 @@
|
||||||
((if keep-lang-line? values cdr) ; FIXME: #lang can span lines
|
((if keep-lang-line? values cdr) ; FIXME: #lang can span lines
|
||||||
(list->lines
|
(list->lines
|
||||||
indent
|
indent
|
||||||
|
#:line-numbers line-numbers
|
||||||
(let loop ([pos 0]
|
(let loop ([pos 0]
|
||||||
[tokens tokens])
|
[tokens tokens])
|
||||||
(cond
|
(cond
|
||||||
|
@ -203,19 +209,37 @@
|
||||||
|
|
||||||
(define omitable (make-style #f '(omitable)))
|
(define omitable (make-style #f '(omitable)))
|
||||||
|
|
||||||
(define (list->lines indent-amt l)
|
(define (list->lines indent-amt l #:line-numbers [line-numbers #f])
|
||||||
(define (make-line accum-line) (list (paragraph omitable
|
|
||||||
(cons indent-elem
|
|
||||||
(reverse accum-line)))))
|
|
||||||
(define indent-elem (if (zero? indent-amt)
|
(define indent-elem (if (zero? indent-amt)
|
||||||
""
|
""
|
||||||
(hspace indent-amt)))
|
(hspace indent-amt)))
|
||||||
(let loop ([l l] [accum-line null])
|
;(list of any) delim -> (list of (list of any))
|
||||||
(cond
|
(define (break-list lst delim)
|
||||||
[(null? l) (if (null? accum-line)
|
(let loop ([l lst] [n null] [c null])
|
||||||
null
|
(cond
|
||||||
(list (make-line accum-line)))]
|
[(null? l) (reverse (if (null? c) n (cons (reverse c) n)))]
|
||||||
[(eq? 'newline (car l))
|
[(eq? delim (car l)) (loop (cdr l) (cons (reverse c) n) null)]
|
||||||
(cons (make-line accum-line)
|
[else (loop (cdr l) n (cons (car l) c) )])))
|
||||||
(loop (cdr l) null))]
|
|
||||||
[else (loop (cdr l) (cons (car l) accum-line))])))
|
(define lines (break-list l 'newline))
|
||||||
|
(define line-cnt (length lines))
|
||||||
|
(define line-cntl (string-length (format "~a" (+ line-cnt (or line-numbers 0)))))
|
||||||
|
|
||||||
|
(define (prepend-line-number n r)
|
||||||
|
(define ln (format "~a" n))
|
||||||
|
(define lnl (string-length ln))
|
||||||
|
(define diff (- line-cntl lnl))
|
||||||
|
(define l1 (list (tt ln) (hspace 1)))
|
||||||
|
(cons (make-element 'smaller (make-element 'smaller
|
||||||
|
(if (not (zero? diff)) (cons (hspace diff) l1) l1))) r))
|
||||||
|
|
||||||
|
(define (make-line accum-line line-number)
|
||||||
|
(define rest (cons indent-elem accum-line))
|
||||||
|
(list (paragraph omitable (if line-numbers
|
||||||
|
(prepend-line-number line-number rest)
|
||||||
|
rest))))
|
||||||
|
|
||||||
|
(for/list ([l (break-list l 'newline)]
|
||||||
|
[i (in-naturals (or line-numbers 1))])
|
||||||
|
(make-line l i)))
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,13 @@ includes a @racket[latex-defaults] @tech{style property}.
|
||||||
([option (code:line #:keep-lang-line? keep-expr)
|
([option (code:line #:keep-lang-line? keep-expr)
|
||||||
(code:line #:indent indent-expr)
|
(code:line #:indent indent-expr)
|
||||||
(code:line #:expand expand-expr)
|
(code:line #:expand expand-expr)
|
||||||
(code:line #:context context-expr)])
|
(code:line #:context context-expr)
|
||||||
|
(code:line #:w/line-numbers line-number-expr)])
|
||||||
#:contracts ([keep-expr any/c]
|
#:contracts ([keep-expr any/c]
|
||||||
[indent-expr exact-nonnegative-integer?]
|
[indent-expr exact-nonnegative-integer?]
|
||||||
[expand-expr (or/c #f (syntax-object? . -> . syntax-object?))]
|
[expand-expr (or/c #f (syntax-object? . -> . syntax-object?))]
|
||||||
[context-expr syntax-object?])]{
|
[context-expr syntax-object?]
|
||||||
|
[line-number-expr (or/c #f exact-nonnegative-integer?)])]{
|
||||||
|
|
||||||
Parses the code formed by the strings produced by the
|
Parses the code formed by the strings produced by the
|
||||||
@racket[str-expr]s as a Racket module (roughly) and produces a
|
@racket[str-expr]s as a Racket module (roughly) and produces a
|
||||||
|
@ -64,6 +66,8 @@ identifiers in the typeset code are colored and linked based on
|
||||||
for-label bindings in the lexical environment of the syntax object
|
for-label bindings in the lexical environment of the syntax object
|
||||||
provided by @racket[context-expr]. The default @racket[context-expr]
|
provided by @racket[context-expr]. The default @racket[context-expr]
|
||||||
has the same lexical context as the first @racket[str-expr].
|
has the same lexical context as the first @racket[str-expr].
|
||||||
|
When @racket[line-number-expr] is true, line number is enabled starting
|
||||||
|
from @racket[line-number-expr].
|
||||||
|
|
||||||
When @racket[expand-expr] produces a procedure, it is used to
|
When @racket[expand-expr] produces a procedure, it is used to
|
||||||
macro-expand the parsed program, and syntax coloring is based on the
|
macro-expand the parsed program, and syntax coloring is based on the
|
||||||
|
|
Loading…
Reference in New Issue
Block a user