diff options
author | bd <bdunahu@colostate.edu> | 2024-03-29 19:11:40 -0600 |
---|---|---|
committer | bd <bdunahu@colostate.edu> | 2024-03-29 19:11:40 -0600 |
commit | bb92d2715f9ea0868ea5edd3ac870bbec9d4f343 (patch) | |
tree | f268e5d0da421f6183cc492d0d834fb7a9c96e09 | |
parent | 69aaa9077b9e8aa575ae5681f82e019e8c34ee18 (diff) |
Create autoformatter library, replacing cleanup function
-rw-r--r-- | .config/emacs/libraries/autoformat.el | 44 | ||||
-rw-r--r-- | .config/emacs/modules/bd--essentials.el | 16 |
2 files changed, 47 insertions, 13 deletions
diff --git a/.config/emacs/libraries/autoformat.el b/.config/emacs/libraries/autoformat.el new file mode 100644 index 0000000..f9914e2 --- /dev/null +++ b/.config/emacs/libraries/autoformat.el @@ -0,0 +1,44 @@ +;; -*- lexical-binding: t; -*- + +(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 t) + +(defun autoformat-toggle-space-rule (&optional ARG) + (interactive (list 'toggle)) + (setq autoformat-do-untabify + (if (eq ARG 'toggle) + (not autoformat-do-untabify) + ARG))) + +(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))) + (indent-region (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 (not autoformat-mode) + (add-hook 'prog-mode-hook #'autoformat--prog-mode-hook) + (remove-hook 'prog-mode-hook #'autoformat--prog-mode-hook))) + + +(provide 'autoformat) diff --git a/.config/emacs/modules/bd--essentials.el b/.config/emacs/modules/bd--essentials.el index e15f29f..8b07ed6 100644 --- a/.config/emacs/modules/bd--essentials.el +++ b/.config/emacs/modules/bd--essentials.el @@ -23,20 +23,10 @@ (global-hl-line-mode 1) (global-visual-line-mode t) -(defun bd/cleanup-exempt-modes () - "Modes which should NOT be autoformatted -on save." - (unless (or (derived-mode-p 'text-mode) (equal major-mode 'makefile-gmake-mode)) - (bd/cleanup-buffer))) - -(defun bd/cleanup-buffer () - (interactive) - (delete-trailing-whitespace) - (untabify (point-min) (point-max)) - (indent-region (point-min) (point-max))) - +;; my custom cleanup mode +(require 'autoformat) +(autoformat-mode 1) (setopt next-line-add-newlines t) -(add-hook 'before-save-hook 'bd/cleanup-exempt-modes) ;;;; buffers |