summaryrefslogtreecommitdiff
path: root/tw/home/files/ppscm
blob: 26edb39f3d713ca5a3069723457f259a3427cb35 (about) (plain)
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
#!/usr/bin/env -S guile --no-auto-compile
!#
(use-modules (ice-9 match)
             (ice-9 popen)
             (ice-9 pretty-print))

(define (main input-port)
  (define highlighter
    (open-output-pipe "source-highlight -s scheme -f esc | $PAGER"))
  (define (exn-handler exn)
    (close-pipe highlighter)   ; make sure $PAGER doesn't mess up the terminal
    (raise-exception exn))     ; re-raise exception
  (define (pretty-printer)
    (match (read input-port)
      ((? eof-object? _)
       (close-pipe highlighter))
      (thing
       (pretty-print thing highlighter)
       (newline highlighter)   ; add empty line between top-level forms
       (pretty-printer))))
  (with-exception-handler exn-handler pretty-printer))

(define (help-message program-name)
  (string-append "\
usage: " (basename program-name) " [-h] FILENAME

This utility reads a Guile scheme file and pretty-prints it, throwing
away any original formatting and comments.

arguments:
  -h, --help   show this message and exit
  FILENAME     the scheme file name to pretty-print; required
"))

(match (program-arguments)
  ((program-name
    . (? (lambda (args)
           (or (member "-h" args)
               (member "--help" args)))
         _))
   (display (help-message program-name)))

  ((_ input-file)
   (call-with-input-file input-file main))

  ((program-name . _)
   (display "error: invalid number of arguments\n\n"
            (current-error-port))
   (display (help-message program-name)
            (current-error-port))
   (exit 1)))