blob: 5be5804e694d2e7a8eb6f3ed33c968d2e114c523 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
;;; -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(defcustom autoformat-do-untabify t
"If the value is t, then `autoformat-mode' will replace all tabs
with spaces upon saving a file. Else, the function greedily replaces
spaces with tabs."
:type 'boolean
:group 'quality
:safe 'booleanp)
(defun autoformat-toggle-space-rule (&optional ARG)
"Toggles the formatting behavior of variable
`autoformat-do-untabify'."
(interactive (list 'toggle))
(setq autoformat-do-untabify
(if (eq ARG 'toggle)
(not autoformat-do-untabify)
ARG))
(message "Autoformatter will %suse spaces!"
(if autoformat-do-untabify
"" "no longer ")))
(defun autoformat--before-save-handler ()
"Automatically reformats spacing of the current buffer."
(when autoformat-mode
(delete-trailing-whitespace)
(if autoformat-do-untabify
(untabify (point-min) (point-max))
(tabify (point-min) (point-max)))))
(defun autoformat--prog-mode-hook ()
(add-hook 'before-save-hook #'autoformat--before-save-handler nil t))
(define-minor-mode autoformat-mode
"Toggles global autoformat-mode."
:init-value nil
:global t
:group 'quality
:lighter " Autoformatter"
:keymap (list
(cons (kbd "C-z f") #'autoformat-toggle-space-rule))
(if autoformat-mode
(add-hook 'prog-mode-hook #'autoformat--prog-mode-hook)
(remove-hook 'prog-mode-hook #'autoformat--prog-mode-hook)))
(provide 'autoformat)
;;; autoformat ends here
|