Add basic Makefile
[jackhill/mal.git] / racket / readline.rkt
1 #lang racket
2
3 (provide readline)
4
5 (require (prefix-in readline: readline/readline))
6
7 (require "types.rkt")
8
9 (define history-loaded #f)
10 (define HISTORY-FILE (format "~a/.mal-history" (find-system-path 'home-dir)))
11
12 (define (load-history path)
13 (with-handlers
14 ([exn:fail? (lambda (e) #t)])
15 (map
16 (lambda (line) (readline:add-history line))
17 (string-split
18 (port->string (open-input-file path))
19 #px"\n"))))
20
21 (define (readline prompt)
22 (when (not history-loaded)
23 (set! history-loaded #t)
24 (load-history HISTORY-FILE))
25 (let ([line (readline:readline prompt)])
26 (if (eq? eof line)
27 nil
28 (begin
29 (readline:add-history line)
30 (with-handlers
31 ([exn:fail? (lambda (e) #t)])
32 (with-output-to-file
33 HISTORY-FILE
34 (lambda () (printf "~a~n" line))
35 #:exists 'append))
36 line))))