You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/quadwriter/lang-helper.rkt

152 lines
6.2 KiB
Racket

6 years ago
#lang debug racket/base
(require (for-syntax racket/base)
racket/match
5 years ago
racket/path
racket/dict
6 years ago
pollen/tag
6 years ago
racket/system
racket/class
racket/string
6 years ago
syntax/strip-context
6 years ago
scribble/reader
quadwriter/core
5 years ago
txexpr
quad/log
5 years ago
"log.rkt")
6 years ago
(provide (all-defined-out))
6 years ago
(define q (default-tag-function 'q))
6 years ago
(define ((make-read-syntax expander-mod pt-proc) path-string p)
;; peel off any lines of format #:keyword val (bounded by newline)
;; and turn them into qexpr attrs
(define kw-attrs
(let loop ([kw-val-pairs null])
(cond
[(regexp-try-match #px"^\\s+" p) (loop kw-val-pairs)]
[(equal? (peek-bytes 2 0 p) #"#:") ; discovered a keyword
(parameterize ([current-input-port (open-input-string (read-line p))])
(match (list (read) (read))
[(list (? keyword? kw) val) #:when (not (eof-object? val))
(loop (cons (cons kw val) kw-val-pairs))]
[_ (loop kw-val-pairs)]))]
6 years ago
;; reverse in case of multiple values with same keyword, latest takes precedence (by becoming first)
[else (reverse (for/list ([(kw val) (in-dict kw-val-pairs)])
5 years ago
(list (string->symbol (string-trim (keyword->string kw) "#:"))
(format "~a" val))))])))
6 years ago
(strip-context
(with-syntax ([PATH-STRING path-string]
[((ATTR-NAME ATTR-VAL) ...) kw-attrs]
6 years ago
[PT (pt-proc path-string p)]
[EXPANDER-MOD expander-mod])
#'(module _ EXPANDER-MOD
PATH-STRING
((ATTR-NAME ATTR-VAL) ...)
6 years ago
. PT))))
6 years ago
(define-syntax-rule (make-module-begin DOC-PROC)
6 years ago
(begin
6 years ago
(provide (rename-out [new-module-begin #%module-begin]))
(define-syntax (new-module-begin stx)
6 years ago
(syntax-case stx ()
[(_ PATH-STRING ATTRS . EXPRS)
6 years ago
(with-syntax ([DOC (datum->syntax #'PATH-STRING 'doc)]
[VIEW-OUTPUT (datum->syntax #'PATH-STRING 'view-output)])
6 years ago
#'(#%module-begin
(provide DOC VIEW-OUTPUT)
(define attrs (let ([h (make-hasheq)])
(for ([kvlist (in-list 'ATTRS)])
5 years ago
(apply hash-set! h kvlist))
h))
5 years ago
(define pdf-path (hash-ref! attrs 'output-path (λ () (path-string->pdf-path-string 'PATH-STRING))))
(define DOC `(q ,(for/list ([(k v) (in-hash attrs)])
5 years ago
(list k v))
,(DOC-PROC (list . EXPRS))))
5 years ago
(define (VIEW-OUTPUT)
6 years ago
(when (file-exists? pdf-path)
5 years ago
(define open-string
(case (system-type 'os)
[(macosx) "open '~a'"]
[(windows) "start '~a'"]
[(unix) "xdg-open '~a' &> /dev/null"]
[else (error "Unknown platform. Don't know how to view PDF.")]))
(void (system (format open-string pdf-path)))))
5 years ago
(define (make-pdf [the-pdf-path #false])
5 years ago
(with-logging-to-port
5 years ago
(current-output-port)
(λ () (with-logging-to-port
(current-output-port)
(λ () (render-pdf DOC the-pdf-path))
5 years ago
#:logger quadwriter-logger
'debug))
#:logger quad-logger
'debug))
(module+ pdf
(define pdf (make-pdf pdf-path))
5 years ago
(provide pdf))
(module+ main
(make-pdf pdf-path))))]))))
6 years ago
5 years ago
(define (path-string->pdf-path-string path-string)
(path->string
5 years ago
(match (format "~a" path-string)
;; weird test but sometimes DrRacket calls the unsaved file
;; 'unsaved-editor and sometimes "unsaved editor"
[(regexp #rx"unsaved.editor")
(build-path (find-system-path 'desk-dir) "untitled.pdf")]
5 years ago
[_ (define path-without-ext (path-replace-extension path-string #""))
;; do the right thing with pollen-style double extension like "foo.pdf.pm"
(if (path-has-extension? path-without-ext #".pdf")
path-without-ext
(path-add-extension path-without-ext #".pdf"))])))
6 years ago
(define quad-at-reader (make-at-reader
#:syntax? #t
#:inside? #t
#:command-char #\◊))
(define (xexpr->parse-tree x)
;; an ordinary txexpr can't serve as a parse tree because of the attrs list fails when passed to #%app.
;; so stick an `attr-list` identifier on it which can hook into the expander.
;; sort of SXML-ish.
(let loop ([x x])
(match x
[(txexpr tag attrs elems) (list* tag (cons 'attr-list attrs) (map loop elems))]
[(? list? xs) (map loop xs)]
6 years ago
[_ x])))
6 years ago
5 years ago
(define (get-info-texty in mod line col pos)
6 years ago
;; DrRacket caches source file information per session,
;; so we can do the same to avoid multiple searches for the command char.
(define command-char-cache (make-hash))
(define my-command-char #\◊)
(λ (key default)
(case key
[(color-lexer)
(match (dynamic-require 'syntax-color/scribble-lexer 'make-scribble-inside-lexer (λ () #false))
[(? procedure? make-lexer) (make-lexer #:command-char my-command-char)]
[_ default])]
[(drracket:toolbar-buttons)
(match (dynamic-require 'pollen/private/drracket-buttons 'make-drracket-buttons (λ () #false))
[(? procedure? make-buttons) (make-buttons my-command-char)])]
[(drracket:indentation)
(λ (text pos)
(define line-idx (send text position-line pos))
(define line-start-pos (send text line-start-position line-idx))
(define line-end-pos (send text line-end-position line-idx))
(define first-vis-pos
(or
(for/first ([pos (in-range line-start-pos line-end-pos)]
#:unless (char-blank? (send text get-character pos)))
5 years ago
pos)
6 years ago
line-start-pos))
(- first-vis-pos line-start-pos))]
[else default])))
5 years ago
(define (get-info-rackety in mod line col pos) (λ (key default) default))