Skip to content

Instantly share code, notes, and snippets.

@MatthewDarling
Created October 17, 2017 18:17
Show Gist options
  • Save MatthewDarling/9d27b1364882a901ece8f858788fb7b5 to your computer and use it in GitHub Desktop.
Save MatthewDarling/9d27b1364882a901ece8f858788fb7b5 to your computer and use it in GitHub Desktop.
Indent Clojure code with Emacs
;;; File: check-clojure-mode-version
;;; Matthew Darling
;;; 15 June 2016
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("melpa-stable" . "http://stable.melpa.org/packages/")))
(require 'package)
(package-initialize)
(package-refresh-contents)
(defun apu--package-up-to-date-p (package)
"Borrowed from auto-package-update: https://github.com/rranelli/auto-package-update.el/blob/cdef79f9fc6f8347fdd05664978fb9a948ea0410/auto-package-update.el#L226"
(when (and (package-installed-p package)
(cadr (assq package package-archive-contents)))
(let* ((newest-desc (cadr (assq package package-archive-contents)))
(installed-desc (cadr (or (assq package package-alist)
(assq package package--builtins))))
(newest-version (package-desc-version newest-desc))
(installed-version (package-desc-version installed-desc)))
(version-list-<= newest-version installed-version))))
(defun clojure-mode-up-to-date-p ()
"Check if clojure-mode is the latest version."
(apu--package-up-to-date-p 'clojure-mode))
(defun die-if-outdated-clojure-mode ()
(when (not (clojure-mode-up-to-date-p))
(message "clojure-mode is out of date! Please boot up Emacs separately and update it with M-x package-install clojure-mode")
(kill-emacs -1)))
#!/bin/sh
# File: clj-indent
# Opens the first argument in emacs and executes the
# emacs-format-function based on clojure-mode
## originally from: http://www.cslab.pepperdine.edu/warford/BatchIndentationEmacs.html
## note: compared to the version on that web page, this version only
## handles one file because, when used with find, everything else is
## unnecessary
echo "Indenting $1 with emacs in batch mode"
/Applications/Emacs.app/Contents/MacOS/Emacs -batch $1 \
# load this specific file
-l ~/bin/emacs-format-file \
# run this specific function
-f "emacs-format-function"
;;; File: emacs-format-file
;;; Stan Warford
;;; 17 May 2006
;;; from http://www.cslab.pepperdine.edu/warford/BatchIndentationEmacs.html
(require 'package)
(package-initialize)
(defun emacs-format-function ()
"Format the whole buffer."
(when (or (string-suffix-p ".clj" (buffer-name))
(string-suffix-p ".edn" (buffer-name)))
(clojure-mode))
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max))
(save-buffer))
#!/bin/sh
# File: indent-in-directory
echo "Checking for newer versions of packages"
/Applications/Emacs.app/Contents/MacOS/Emacs -batch $1 \
# load this specific file
-l ~/bin/check-clojure-mode-version.el \
# run this specific function
-f "die-if-outdated-clojure-mode" \
# redirect stderr to nowhere
2> /dev/null &&
echo "Indenting all .clj and .edn files in" $(pwd) &&
# find files in the current directory ending with .clj or .edn
find . -type f \( -name "*.clj" -o -name "*.edn" \) \
# execute this shell script, where {} represents a particular file that is found
-exec ~/bin/clj-indent.sh {} \; \
# redirect stderr to nowhere
2> /dev/null
@MatthewDarling
Copy link
Author

Checking the file extensions shouldn't even be needed, but it seemed like the auto-mode-alist wasn't being populated. Or it wasn't being respected. Anyway, that's why the manual call to (clojure-mode) is there, and the file extension checking elsewhere.

Otherwise, this could be totally generic and indent any kind of file as needed! Which would be neat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment