An open API service indexing awesome lists of open source software.

https://github.com/riscy/elfmt

Simple code formatter for Emacs Lisp
https://github.com/riscy/elfmt

code-formatter emacs lisp

Last synced: 24 days ago
JSON representation

Simple code formatter for Emacs Lisp

Awesome Lists containing this project

README

        

#+TITLE: elfmt
#+OPTIONS: toc:3 author:t creator:nil num:nil
#+AUTHOR: Chris Rayner
#+EMAIL: [email protected]

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'
#+begin_src elisp
(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)))))))
#+end_src

*** 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

- [[https://gitlab.com/ideasman42/emacs-elisp-autofmt][elisp-autofmt]]
- [[https://github.com/Yuki-Inoue/elisp-format][elisp-format]]
- [[https://github.com/Malabarba/aggressive-indent-mode][aggressive-indent-mode]]
- [[https://github.com/abo-abo/lispy][lispy]]'s ~lispy-alt-multiline~
- [[https://github.com/tuhdo/semantic-refactor][semantic-refactor]]
- =M-x pp-buffer= and [[https://www.emacswiki.org/emacs/pp+.el][extensions]]
- ...and [[https://emacs.stackexchange.com/questions/283/command-that-formats-prettifies-elisp-code][a discussion on StackOverflow]]