2018-08-22 02:15:44 +02:00
|
|
|
;;; editor/format/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
2018-08-31 12:44:24 +02:00
|
|
|
(defvar +format-on-save-enabled-modes
|
|
|
|
'(not emacs-lisp-mode ; elisp's mechanisms are good enough
|
|
|
|
sql-mode) ; sqlformat is currently broken
|
2018-09-08 23:36:24 -04:00
|
|
|
"A list of major modes in which to reformat the buffer upon saving.
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
If this list begins with `not', then it negates the list.
|
|
|
|
If it is `t', it is enabled in all modes.
|
|
|
|
If nil, it is disabled in all modes, the same as if the +onsave flag wasn't
|
2018-09-08 23:36:24 -04:00
|
|
|
used at all.
|
|
|
|
|
|
|
|
Irrelevant if you do not have the +onsave flag enabled for this module.")
|
|
|
|
|
2018-09-13 11:15:01 -04:00
|
|
|
(defvar-local +format-with nil
|
|
|
|
"Set this to explicitly use a certain formatter for the current buffer.")
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2018-09-07 19:36:16 -04:00
|
|
|
;; Bootstrap
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
(defun +format|enable-on-save-maybe ()
|
2018-08-28 12:02:56 +02:00
|
|
|
"Enable formatting on save in certain major modes.
|
|
|
|
|
|
|
|
This is controlled by `+format-on-save-enabled-modes'."
|
2018-08-22 02:15:44 +02:00
|
|
|
(unless (or (eq major-mode 'fundamental-mode)
|
|
|
|
(cond ((booleanp +format-on-save-enabled-modes)
|
|
|
|
(null +format-on-save-enabled-modes))
|
|
|
|
((eq (car +format-on-save-enabled-modes) 'not)
|
|
|
|
(memq major-mode (cdr +format-on-save-enabled-modes)))
|
|
|
|
((not (memq major-mode +format-on-save-enabled-modes))))
|
2018-09-08 23:41:47 -04:00
|
|
|
(not (require 'format-all nil t)))
|
2018-08-28 12:02:56 +02:00
|
|
|
(add-hook 'before-save-hook #'+format|buffer nil t)))
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
(when (featurep! +onsave)
|
|
|
|
(add-hook 'after-change-major-mode-hook #'+format|enable-on-save-maybe))
|
2018-09-08 23:36:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Hacks
|
|
|
|
|
|
|
|
;; Allow a specific formatter to be used by setting `+format-with', either
|
|
|
|
;; buffer-locally or let-bound.
|
|
|
|
(advice-add #'format-all-probe :around #'+format*probe)
|
2018-09-08 23:39:52 -04:00
|
|
|
|
|
|
|
;; Doom uses a modded `format-all-buffer', which
|
|
|
|
;; 1. Doesn't move the cursorafter reformatting,
|
|
|
|
;; 2. Can reformat regions, rather than the entire buffer (while preserving
|
|
|
|
;; leading indentation),
|
|
|
|
;; 3. Applies changes via RCS patch, line by line, as not to protect buffer
|
|
|
|
;; markers and avoid any jarring cursor+window scrolling.
|
|
|
|
(advice-add #'format-all-buffer :override #'+format/buffer)
|