raart/kitty.rkt

85 lines
2.5 KiB
Racket
Raw Normal View History

2018-12-09 02:52:24 +00:00
#lang racket/base
(require racket/match
2019-01-21 05:35:19 +00:00
racket/set
racket/runtime-path
json
2018-12-09 02:52:24 +00:00
(prefix-in pict: pict)
file/convertible
racket/class
2019-09-19 13:21:32 +00:00
racket/gui/dynamic)
2018-12-09 02:52:24 +00:00
(define (convert->png-bytes v)
(and (convertible? v)
(convert v 'png-bytes+bounds #f)))
;; Replace "racket -t file"
;; with
;; "racket -I raart/kitty-init -i -t file -e '(exit 0)'"
2019-01-21 05:35:19 +00:00
(define (term-is-kitty?)
2018-12-09 02:52:24 +00:00
(define t (environment-variables-ref (current-environment-variables) #"TERM"))
2019-01-21 05:35:19 +00:00
(equal? t #"xterm-kitty"))
(define (install-kitty-print!)
(when (term-is-kitty?)
2019-09-19 13:21:32 +00:00
(define (snip? v)
(and (gui-available?)
(is-a? v (gui-dynamic-require 'snip%))))
2018-12-09 02:52:24 +00:00
;; XXX This could do better and use
#;(pretty-print-size-hook)
;; and
#;(pretty-print-print-hook)
;; to pretty these things inside other structures
;; but, then I believe I could not rely on icat, but I'd have to implement it myself
(define old-print (current-print))
(define (new-print v)
(match (or (convert->png-bytes v) v)
[(list bs w h d v)
(define-values
(sp stdout stdin stderr)
(subprocess (current-output-port) #f (current-error-port)
(find-executable-path "kitty")
"+kitten" "icat"))
(write-bytes bs stdin)
(close-output-port stdin)
(subprocess-wait sp)]
[(? snip?)
(define wb (box #f))
(define hb (box #f))
(send v get-extent (pict:dc-for-text-size) 0 0 wb hb)
(define w (unbox wb))
(define h (unbox hb))
(new-print
(pict:dc (λ (dc x y)
(send v draw dc x y 0 0 w h 0 0 'no-caret))
w h))]
[v (old-print v)]))
(current-print new-print)))
2019-01-21 05:35:19 +00:00
(define-runtime-path kk.j "kitty-key.json")
(define kk-ht
(for/hash ([(s e) (in-hash (with-input-from-file kk.j read-json))])
(define o (symbol->string s))
(values (string->bytes/utf-8 e)
(cond
[(= 1 (string-length o))
(string-ref o 0)]
[else o]))))
(define (kitty-key-lookup k)
(hash-ref kk-ht k (λ () k)))
(define (kitty-mods-lookup mb)
(define mn
(- (bytes-ref mb 0) (char->integer #\A)))
(for/fold ([s (seteq)])
([m (in-list '(shift meta control super))]
[i (in-list '(1 2 4 8))])
(if (zero? (bitwise-and mn i)) s
(set-add s m))))
(provide install-kitty-print!
term-is-kitty?
kitty-key-lookup
kitty-mods-lookup)