/elfmt

Simple code formatter for Emacs Lisp

Primary LanguageEmacs LispGNU General Public License v3.0GPL-3.0

elfmt

This is (yet another) code formatter for Emacs Lisp.

Features

  • Won’t format lines that end in ; nofmt
  • Focuses on the placement of lists and (mostly) ignores atoms
  • Tries to break at fill-column, but lines may exceed this number due to inline comments, long literals, trailing sequences of closed parens, or postprocessing (see elfmt-autojoin-1 for example)
  • Prefers “modern” Elisp (old-style backquotes will cause it to halt)

‘Screenshot’

(defun elfmt--mend-line-p ()
  "Whether to join the current line with the next.
This uses heuristics that disregard the contributions of trailing
comments, closing parentheses, and backslash abbreviations like
\\n, so lines may end up longer than `fill-column'."
  ;; precond: point is on an sexp
  (let ((sexp-str (thing-at-point 'sexp)))
    (and
     sexp-str
     (string-match "\n" sexp-str)  ; sexp crosses to the next line
     (setq sexp-str (format "%S" (car (read-from-string sexp-str))))
     (< (length sexp-str) (- fill-column (current-column))) ; mostly fits
     (< (cl-count ?\( sexp-str) 5) ; visual complexity (i.e. nested parens)
     (save-excursion
       (and
        (not (elfmt--trailing-syntax))     ; not inside a string/comment
        (eq (forward-line 1) 0)            ; and the next line, if any:
        (not (looking-at "[[:space:]]*;")) ; ...is not a comment
        (not (elfmt--nofmt-line-p)))))))

Usage

  • Type M-x elfmt to format the current buffer
  • Type M-x elfmt-sexp to format the current sexp
  • Type M-x elfmt-mode to automatically format the buffer on save
  • Type M-x elfmt-global-mode to enable elfmt-mode everywhere

Alternatives