Rethink lazy-loading of elisp-mode

This commit is contained in:
Henrik Lissner 2018-06-27 22:52:46 +02:00
parent 07d37f97fb
commit 3742a671a6
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,22 +1,32 @@
;;; lang/emacs-lisp/config.el -*- lexical-binding: t; -*- ;;; lang/emacs-lisp/config.el -*- lexical-binding: t; -*-
;; `elisp-mode' is always loaded at startup, so to lazy load its config we need
;; to be creative. So we configure it the first tiem `emacs-lisp-mode' is run.
(advice-add #'emacs-lisp-mode :before #'+emacs-lisp|init)
;; And we remove `elisp-mode' so later invokations of (after! elisp-mode ...)
;; work as expected.
(delq 'elisp-mode features)
(defun +emacs-lisp|init (&rest _)
;; Some plugins (like yasnippet) will run `emacs-lisp-mode' early, prematurely
;; triggering this function in a non-ideal environment (`emacs-lisp-mode-hook'
;; is let-bound to nil). This breaks a lot of Doom setters, because they try
;; to add hooks to `emacs-lisp-mode-hook'!
;;
;; This means, in some sessions, elisp-mode is never configured properly, so
;; we have to make extra sure `emacs-lisp-mode' was executed interactively.
(when (and emacs-lisp-mode-hook (not delay-mode-hooks))
(provide 'elisp-mode)
(advice-remove #'emacs-lisp-mode #'+emacs-lisp|init)))
;;
;; Config
;;
(add-to-list 'auto-mode-alist '("\\.Cask\\'" . emacs-lisp-mode)) (add-to-list 'auto-mode-alist '("\\.Cask\\'" . emacs-lisp-mode))
(add-hook! 'emacs-lisp-mode-hook (after! elisp-mode
#'(;; 3rd-party functionality
auto-compile-on-save-mode doom|enable-delete-trailing-whitespace
;; fontification
rainbow-delimiters-mode highlight-quoted-mode highlight-numbers-mode +emacs-lisp|extra-fontification
;; initialization
+emacs-lisp|init-imenu +emacs-lisp|disable-flycheck-maybe))
(defun +emacs-lisp|init (&rest _)
;; Some plugins will run `emacs-lisp-mode' with `emacs-lisp-mode-hook' set to
;; nil (cough yasnippet cough) or its mode hooks delayed. This prematurely
;; fires this function. Most of these setters affect emacs-lisp-mode-hook, so
;; they'd be swallowed up if it's letbound to nil, causing a half-initialized
;; elisp-mode in some sessions.
(when (and emacs-lisp-mode-hook (not delay-mode-hooks))
(set-repl-handler! 'emacs-lisp-mode #'+emacs-lisp/repl) (set-repl-handler! 'emacs-lisp-mode #'+emacs-lisp/repl)
(set-eval-handler! 'emacs-lisp-mode #'+emacs-lisp-eval) (set-eval-handler! 'emacs-lisp-mode #'+emacs-lisp-eval)
(set-lookup-handlers! 'emacs-lisp-mode :documentation 'info-lookup-symbol) (set-lookup-handlers! 'emacs-lisp-mode :documentation 'info-lookup-symbol)
@ -30,11 +40,16 @@
("advice-add" "advice-remove") ("advice-add" "advice-remove")
("add-hook" "remove-hook") ("add-hook" "remove-hook")
("add-hook!" "remove-hook!"))) ("add-hook!" "remove-hook!")))
;;
(advice-remove #'emacs-lisp-mode #'+emacs-lisp|init)))
(advice-add #'emacs-lisp-mode :before #'+emacs-lisp|init)
(defun +emacs-lisp|extra-fontification () (add-hook! 'emacs-lisp-mode-hook
#'(;; 3rd-party functionality
auto-compile-on-save-mode doom|enable-delete-trailing-whitespace
;; fontification
rainbow-delimiters-mode highlight-quoted-mode highlight-numbers-mode +emacs-lisp|extra-fontification
;; initialization
+emacs-lisp|init-imenu +emacs-lisp|disable-flycheck-maybe))
(defun +emacs-lisp|extra-fontification ()
"Display lambda as a smybol and fontify doom module functions." "Display lambda as a smybol and fontify doom module functions."
(font-lock-add-keywords (font-lock-add-keywords
nil `(;; Highlight custom Doom cookies nil `(;; Highlight custom Doom cookies
@ -42,7 +57,7 @@
;; Highlight doom/module functions ;; Highlight doom/module functions
("\\(^\\|\\s-\\|,\\)(\\(\\(doom\\|\\+\\)[^) ]+\\|[^) ]+!\\)[) \n]" (2 font-lock-keyword-face))))) ("\\(^\\|\\s-\\|,\\)(\\(\\(doom\\|\\+\\)[^) ]+\\|[^) ]+!\\)[) \n]" (2 font-lock-keyword-face)))))
(defun +emacs-lisp|init-imenu () (defun +emacs-lisp|init-imenu ()
"Improve imenu support with better expression regexps and Doom-specific forms." "Improve imenu support with better expression regexps and Doom-specific forms."
(setq imenu-generic-expression (setq imenu-generic-expression
'(("Evil Commands" "^\\s-*(evil-define-\\(?:command\\|operator\\|motion\\) +\\(\\_<[^ ()\n]+\\_>\\)" 1) '(("Evil Commands" "^\\s-*(evil-define-\\(?:command\\|operator\\|motion\\) +\\(\\_<[^ ()\n]+\\_>\\)" 1)
@ -61,13 +76,13 @@
("Variables" "^\\s-*(defvar\\(?:-local\\)?\\s-+\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)+\\)[[:space:]\n]+[^)]" 1) ("Variables" "^\\s-*(defvar\\(?:-local\\)?\\s-+\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)+\\)[[:space:]\n]+[^)]" 1)
("Types" "^\\s-*(\\(cl-def\\(?:struct\\|type\\)\\|def\\(?:class\\|face\\|group\\|ine-\\(?:condition\\|error\\|widget\\)\\|package\\|struct\\|t\\(?:\\(?:hem\\|yp\\)e\\)\\)\\)\\s-+'?\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)+\\)" 2)))) ("Types" "^\\s-*(\\(cl-def\\(?:struct\\|type\\)\\|def\\(?:class\\|face\\|group\\|ine-\\(?:condition\\|error\\|widget\\)\\|package\\|struct\\|t\\(?:\\(?:hem\\|yp\\)e\\)\\)\\)\\s-+'?\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)+\\)" 2))))
(defun +emacs-lisp|disable-flycheck-maybe () (defun +emacs-lisp|disable-flycheck-maybe ()
"Disable flycheck-mode if in emacs.d." "Disable flycheck-mode if in emacs.d."
(when (or (not buffer-file-name) (when (or (not buffer-file-name)
(cl-loop for dir in (list doom-emacs-dir doom-private-dir) (cl-loop for dir in (list doom-emacs-dir doom-private-dir)
if (file-in-directory-p buffer-file-name dir) if (file-in-directory-p buffer-file-name dir)
return t)) return t))
(flycheck-mode -1))) (flycheck-mode -1))))
;; ;;