Skip to content

Instantly share code, notes, and snippets.

@shegeley
Forked from ojarjur/scmfmt.scm
Created June 14, 2023 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shegeley/cb44e156c10b8f235d8abd0dd768cff4 to your computer and use it in GitHub Desktop.
Save shegeley/cb44e156c10b8f235d8abd0dd768cff4 to your computer and use it in GitHub Desktop.
Code formatter for Scheme using Guile's pretty-print method. This reads from stdin and writes to stdout.
(use-modules (ice-9 pretty-print))
;; Helper methods for maintaining comments and whitespace.
(define (copy-line-comment)
(let ((char (read-char)))
(if (not (eof-object? char))
(if (eq? char #\newline)
(newline)
(begin (write-char char) (copy-line-comment))))))
(define (maintain-empty-lines)
(let ((char1 (read-char)) (char2 (peek-char)))
(if (and (eq? char1 #\newline) (eq? char2 #\newline))
(write-char (read-char)))))
;; The main method. This reads from and writes to stdin/stdout.
(define (scmfmt)
(let ((char (peek-char)))
(if (not (eof-object? char))
(begin
(cond ((eq? char #\;) (copy-line-comment))
((eq? char #\newline) (maintain-empty-lines))
((char-whitespace? char) (read-char))
(#t (pretty-print (read))))
(scmfmt)))))
(scmfmt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment