2018-05-08 19:59:08 +02:00
|
|
|
;;; tools/editorconfig/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
2018-06-14 00:11:52 +02:00
|
|
|
;; editorconfig cannot procure the correct settings for extension-less files.
|
|
|
|
;; Executable scripts with a shebang line, for example. So why not use Emacs'
|
|
|
|
;; major mode to drop editorconfig a hint? This is accomplished by temporarily
|
|
|
|
;; appending an extension to `buffer-file-name' when we talk to editorconfig.
|
|
|
|
(defvar +editorconfig-mode-alist
|
2018-09-08 18:22:41 -04:00
|
|
|
'((sh-mode . "sh")
|
|
|
|
(python-mode . "py")
|
|
|
|
(ruby-mode . "rb")
|
|
|
|
(enh-ruby-mode . "rb")
|
|
|
|
(perl-mode . "pl")
|
|
|
|
(php-mode . "php"))
|
2018-06-14 00:11:52 +02:00
|
|
|
"An alist mapping major modes to extensions. Used by
|
|
|
|
`doom*editorconfig-smart-detection' to give editorconfig filetype hints.")
|
|
|
|
|
|
|
|
|
2018-05-08 19:59:08 +02:00
|
|
|
;; Handles whitespace (tabs/spaces) settings externally. This way projects can
|
|
|
|
;; specify their own formatting rules.
|
|
|
|
(def-package! editorconfig
|
2018-08-31 23:43:31 +02:00
|
|
|
:defer 3
|
2018-08-31 14:10:25 +02:00
|
|
|
:after-call (doom-enter-buffer-hook after-find-file)
|
2018-05-08 19:59:08 +02:00
|
|
|
:config
|
|
|
|
;; Register missing indent variables
|
2018-06-23 16:48:58 +02:00
|
|
|
(unless (assq 'mips-mode editorconfig-indentation-alist)
|
|
|
|
(setq editorconfig-indentation-alist
|
|
|
|
(append '((mips-mode mips-tab-width)
|
|
|
|
(haxor-mode haxor-tab-width)
|
2018-09-04 04:26:06 +02:00
|
|
|
(nasm-mode nasm-basic-offset)
|
|
|
|
(enh-ruby-mode enh-ruby-indent-level))
|
2018-06-23 16:48:58 +02:00
|
|
|
editorconfig-indentation-alist)))
|
2018-05-08 19:59:08 +02:00
|
|
|
|
2018-09-07 22:08:11 -04:00
|
|
|
(defun doom*editorconfig-smart-detection (orig-fn)
|
2018-05-08 19:59:08 +02:00
|
|
|
"Retrieve the properties for the current file. If it doesn't have an
|
|
|
|
extension, try to guess one."
|
|
|
|
(let ((buffer-file-name
|
|
|
|
(if (and (not (bound-and-true-p org-src-mode))
|
|
|
|
(file-name-extension buffer-file-name))
|
|
|
|
buffer-file-name
|
|
|
|
(format "%s%s" buffer-file-name
|
2018-06-14 00:11:52 +02:00
|
|
|
(if-let* ((ext (cdr (assq major-mode +editorconfig-mode-alist))))
|
2018-05-08 19:59:08 +02:00
|
|
|
(concat "." ext)
|
|
|
|
"")))))
|
2018-09-07 22:08:11 -04:00
|
|
|
(funcall orig-fn)))
|
2018-05-08 19:59:08 +02:00
|
|
|
(advice-add #'editorconfig-call-editorconfig-exec :around #'doom*editorconfig-smart-detection)
|
|
|
|
|
2018-08-31 13:56:50 +02:00
|
|
|
(defun +editorconfig|disable-ws-butler-maybe (props)
|
|
|
|
"Disable `ws-butler-mode' if trim_trailing_whitespace is true."
|
2018-09-07 22:08:11 -04:00
|
|
|
(when (and (equal (gethash 'trim_trailing_whitespace props) "true")
|
|
|
|
(bound-and-true-p ws-butler-mode))
|
2018-08-31 13:56:50 +02:00
|
|
|
(ws-butler-mode -1)))
|
|
|
|
(add-hook 'editorconfig-custom-hooks #'+editorconfig|disable-ws-butler-maybe)
|
|
|
|
|
2018-05-18 01:28:40 +02:00
|
|
|
(defun +editorconfig|disable-indent-detection (props)
|
2018-08-31 13:56:50 +02:00
|
|
|
"Inhibit `dtrt-indent' if an explicit indent_style and indent_size is
|
|
|
|
specified by editorconfig."
|
2018-05-18 01:28:40 +02:00
|
|
|
(when (or (gethash 'indent_style props)
|
|
|
|
(gethash 'indent_size props))
|
|
|
|
(setq doom-inhibit-indent-detection t)))
|
|
|
|
(add-hook 'editorconfig-custom-hooks #'+editorconfig|disable-indent-detection)
|
|
|
|
|
2018-05-08 19:59:08 +02:00
|
|
|
;; Editorconfig makes indentation too rigid in Lisp modes, so tell
|
2018-09-27 23:04:16 -04:00
|
|
|
;; editorconfig to ignore indentation there. The dynamic indentation support
|
|
|
|
;; built into Emacs is superior.
|
2018-05-08 19:59:08 +02:00
|
|
|
(dolist (mode '(emacs-lisp-mode lisp-mode))
|
2018-06-23 16:48:58 +02:00
|
|
|
(delq (assq mode editorconfig-indentation-alist)
|
|
|
|
editorconfig-indentation-alist))
|
2018-05-08 19:59:08 +02:00
|
|
|
|
2018-05-25 00:46:11 +02:00
|
|
|
;;
|
|
|
|
(editorconfig-mode +1))
|