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
|
2019-10-22 19:21:19 -04:00
|
|
|
sql-mode ; sqlformat is currently broken
|
|
|
|
tex-mode ; latexindent is broken
|
|
|
|
latex-mode)
|
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-18 13:14:39 -04:00
|
|
|
(defvar +format-preserve-indentation t
|
|
|
|
"If non-nil, the leading indentation is preserved when formatting the whole
|
|
|
|
buffer. This is particularly useful for partials.
|
|
|
|
|
2019-06-14 11:08:59 +02:00
|
|
|
Indentation is always preserved when formatting regions.")
|
2018-09-18 13:14:39 -04:00
|
|
|
|
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
|
|
|
|
2020-05-21 01:20:00 -04:00
|
|
|
(defvar +format-with-lsp t
|
|
|
|
"If non-nil, format with LSP formatter if it's available.
|
|
|
|
|
|
|
|
This can be set buffer-locally with `setq-hook!' to disable LSP formatting in
|
|
|
|
select buffers.")
|
|
|
|
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
;;
|
2019-06-14 11:08:59 +02:00
|
|
|
;;; Bootstrap
|
2018-08-22 02:15:44 +02:00
|
|
|
|
2019-07-23 12:30:47 +02:00
|
|
|
(defun +format-enable-on-save-maybe-h ()
|
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)))
|
2020-10-20 18:33:31 +05:30
|
|
|
(+format-enable-on-save-h)))
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
(when (featurep! +onsave)
|
2019-07-23 12:30:47 +02:00
|
|
|
(add-hook 'after-change-major-mode-hook #'+format-enable-on-save-maybe-h))
|
2018-09-08 23:36:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2019-06-14 11:08:59 +02:00
|
|
|
;;; Hacks
|
2018-09-08 23:36:24 -04:00
|
|
|
|
|
|
|
;; Allow a specific formatter to be used by setting `+format-with', either
|
|
|
|
;; buffer-locally or let-bound.
|
2019-07-23 12:30:47 +02:00
|
|
|
(advice-add #'format-all--probe :around #'+format-probe-a)
|
2018-09-08 23:39:52 -04:00
|
|
|
|
|
|
|
;; Doom uses a modded `format-all-buffer', which
|
2019-06-14 11:08:59 +02:00
|
|
|
;; 1. Enables partial reformatting (while preserving leading indentation),
|
|
|
|
;; 2. Applies changes via RCS patch, line by line, to protect buffer markers
|
|
|
|
;; and avoid any jarring cursor+window scrolling.
|
2020-03-10 01:48:20 -04:00
|
|
|
(advice-add #'format-all-buffer--with :around #'+format-buffer-a)
|
2020-11-01 08:49:14 -08:00
|
|
|
|
|
|
|
;; format-all-mode "helpfully" raises an error when it doesn't know how to
|
|
|
|
;; format a buffer.
|
|
|
|
(add-to-list 'debug-ignored-errors "^Don't know how to format ")
|