forked from greghendershott/racket-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresh-line.rkt
37 lines (31 loc) · 1.18 KB
/
fresh-line.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#lang racket/base
(provide fresh-line
zero-column!)
;; Borrowed from xrepl
(define last-output-port #f)
(define last-error-port #f)
(define (maybe-new-output-ports)
(define-syntax-rule (maybe last cur)
(unless (eq? last cur)
(when (and last
(not (port-closed? last)))
(flush-output last)) ;just in case
(set! last cur)
(flush-output last)
(port-count-lines! last)))
(maybe last-output-port (current-output-port))
(maybe last-error-port (current-error-port)))
(define (fresh-line [stderr? #f])
(maybe-new-output-ports)
(define port (if stderr? last-error-port last-output-port))
(flush-output port)
(define-values [line col pos] (port-next-location port))
(unless (eq? col 0) (newline)))
(define (zero-column!)
;; there's a problem whenever there's some printout followed by a
;; read: the cursor will be at column zero, but the port counting
;; will think that it's still right after the printout; call this
;; function in such cases to adjust the column to 0.
(maybe-new-output-ports)
(define-values [line col pos] (port-next-location last-output-port))
(set-port-next-location! last-output-port line 0 pos))