File: fresh-line.rkt

package info (click to toggle)
racket-mode 20201227git0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,040 kB
  • sloc: lisp: 9,808; makefile: 55
file content (37 lines) | stat: -rw-r--r-- 1,246 bytes parent folder | download
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 (make-parameter #f))
(define last-error-port  (make-parameter #f))

(define (maybe-new-output-ports)
  (define (maybe last cur)
    (unless (eq? (last) (cur))
      (when (and (last)
                 (not (port-closed? (last))))
        (flush-output (last))) ;just in case
      (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))