forked from lispunion/code-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme-format.scm
68 lines (66 loc) · 1.95 KB
/
scheme-format.scm
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(include "etc.scm")
(include "format.scm")
(import scheme)
(import chicken.base)
(import chicken.io)
(import chicken.module)
(import chicken.port)
(import chicken.process-context)
(import chicken.string)
(import chicken.sort)
(import callable-data-structures)
(import srfi-1)
(import srfi-13)
(import srfi-69)
(import simple-loops)
(import matchable)
(import list-utils)
; Options
(define end-options #f)
(define inplace #f)
(define files
(filt s (cdr (argv))
(or (not (string-prefix? "-" s))
end-options
(begin
(cond
((or (string-prefix? "--h" s)
(string-prefix? "-h" s))
(print "Usage: scheme-format [options] files")
(print)
(print "-h Show help")
(print "-i Inplace edit")
(print "-v Show version")
(exit 0))
((or (string-prefix? "--i" s)
(string-prefix? "-i" s))
(set! inplace #t))
((or (string-prefix? "--v" s)
(string-prefix? "-V" s)
(string-prefix? "-v" s))
(print "scheme-format version 1")
(exit 0))
((string= "--" s)
(set! end-options #t))
(else
(print s ": unknown option")
(exit 1)))
#f))))
; Format from stdin
(if (length=0? files)
(begin
(let ((xs (with-input-from-port (current-input-port) read/comments)))
(set! xs (tidy xs))
(define s (with-output-to-string (curry write/comments xs)))
(display s))))
; Format from given file
(for file files
(define xs (with-input-from-file file read/comments))
(set! xs (tidy xs))
(define s (with-output-to-string (curry write/comments xs)))
(if inplace
(with-output-to-file file
(lambda ()
(display s))
#:binary)
(display s)))