doom/repos/x-lib/yank-indent.el

48 lines
1.6 KiB
EmacsLisp
Raw Permalink Normal View History

2022-04-12 22:40:25 +02:00
;;; yank-indent.el -*- lexical-binding: t -*-
;; Automatically indent yanked text in programming modes.
;;
;;; Variables:
(defvar +x-yank-indent--additional-modes
'(LaTeX-mode TeX-mode)
"Additional modes in which to indent yanked(-popped) regions.
Only modes that don't derive from `prog-mode' should be listed.")
(defvar +x-yank-indent--blacklisted-modes
'(python-mode slim-mode haml-mode yaml-mode)
"Modes for which auto-indenting is suppressed.")
(defvar +x-yank-indent--threshold 10000
"Threshold (# chars) for auto yank indentation.")
;;
;;; Code:
;; -----------------------------------------------------------------------------
;; To configure, add `+x-yank-indent-a' as `:after' advice to all the yank-like
;; functions that you would like to auto-indent. For example:
;;
;; (advice-add 'yank :after #'+x-yank-indent-a)
;; (advice-add 'yank-pop :after #'+x-yank-indent-a)
;; (advice-add 'consult-yank-pop :after #'+x-yank-indent-a)
;; -----------------------------------------------------------------------------
;;;###autoload
(defun +x-yank-indent-a (&optional _)
"Indent yanked text.
Except when used with prefix arg or in one of the excluded modes."
(when (and (not current-prefix-arg)
(not (member major-mode +x-yank-indent--blacklisted-modes))
(or (derived-mode-p 'prog-mode)
(member major-mode +x-yank-indent--additional-modes)))
(let ((transient-mark-mode nil)
(beg (region-beginning))
(end (region-end)))
(unless (> (- end beg) +x-yank-indent--threshold)
(indent-region beg end nil)))))
(provide 'yank-indent)
;;; yank-indent.el ends here