close
  • chevron_right

    Common Lisp: downcase Lisp files

    Michał "phoe" Herda · Wednesday, 15 April - 20:15 edit

#Lisp #CommonLisp

If you ever encounter a Lisp file that is written with upcased symbols, you might want to be able to easily convert it to downcase. That's my implementation of that.

(defun case-lisp-file (pathname case-function)
  (with-input-from-file (input pathname)
    (with-output-to-string (output)
      (loop for char = (read-char input nil nil)
            while char
            do (case char
                 (#\#
                  (princ char output)
                  (let ((next-char (read-char input)))
                    (case next-char
                      (#\\
                       (princ next-char output)
                       (princ (read-char input) output))
                      (t (unread-char next-char input)))))
                 (#\;
                  (unread-char char input)
                  (princ (read-line input) output)
                  (terpri output))
                 ((#\" #\|)
                  (unread-char char input)
                  (prin1 (read input) output))
                 (t (write-char (funcall case-function char) output)))))))

(defun upcase-lisp-file (pathname)
  "Upcases a Common Lisp source file."
  (case-lisp-file pathname #'char-upcase))

(defun downcase-lisp-file (pathname)
  "Downcases a Common Lisp source file."
  (case-lisp-file pathname #'char-downcase))

Thanks to Rainer Joswig and to travv0 for noticing and implementing fixes for that.