doomemacs/modules/lang/emacs-lisp/config.el

260 lines
10 KiB
EmacsLisp
Raw Normal View History

;;; lang/emacs-lisp/config.el -*- lexical-binding: t; -*-
2015-06-15 09:06:10 +02:00
(defvar +emacs-lisp-enable-extra-fontification t
"If non-nil, highlight special forms, and defined functions and variables.")
(defvar +emacs-lisp-outline-regexp "[ \t]*;;;;* [^ \t\n]"
"Regexp to use for `outline-regexp' in `emacs-lisp-mode'.
This marks a foldable marker for `outline-minor-mode' in elisp buffers.")
(defvar +emacs-lisp-disable-flycheck-in-dirs
(list doom-emacs-dir doom-private-dir)
"List of directories to disable `emacs-lisp-checkdoc' in.
This checker tends to produce a lot of false positives in your .emacs.d and
private config, so it is mostly useless there. However, special hacks are
employed so that flycheck still does *some* helpful linting.")
;; `elisp-mode' is loaded at startup. In order to lazy load its config we need
;; to pretend it isn't loaded
(defer-feature! elisp-mode emacs-lisp-mode)
2018-06-27 22:52:46 +02:00
;;
;;; Config
2018-06-27 22:52:46 +02:00
(use-package! elisp-mode
2019-02-24 13:58:56 -05:00
:mode ("\\.Cask\\'" . emacs-lisp-mode)
:init
;; Instead of pestering the user about accepting unsafe file local variables,
;; I'd rather it quietly ignore them...
(setq-default enable-local-variables :safe)
;; ...but still log them, so we can discover them if we're looking for it.
(defadvice! +emacs-lisp-log-unsafe-local-variables-a (variables dir-name)
:before #'hack-local-variables-filter
(when (eq enable-local-variables :safe)
(pcase-dolist (`(,var . ,val) variables)
(cond ((memq var ignored-local-variables))
((memq var '(mode unibyte coding)))
((eq var 'eval)
(and enable-local-eval
(not (or (hack-one-local-variable-eval-safep val)
(safe-local-variable-p var val)))
(message "Ignoring unsafe form in file local variable: %S" val)))
((not (safe-local-variable-p var val))
(message "Ignoring unsafe file local variable: %S" var))
((get var 'risky-local-variable)
(message "Ignoring risky file local variable: %S" var))))))
2019-02-24 13:58:56 -05:00
:config
2019-12-20 00:47:04 -05:00
(set-repl-handler! '(emacs-lisp-mode lisp-interaction-mode) #'+emacs-lisp/open-repl)
(set-eval-handler! '(emacs-lisp-mode lisp-interaction-mode) #'+emacs-lisp-eval)
(set-lookup-handlers! '(emacs-lisp-mode lisp-interaction-mode helpful-mode)
:definition #'+emacs-lisp-lookup-definition
:documentation #'+emacs-lisp-lookup-documentation)
2019-12-20 00:47:04 -05:00
(set-docsets! '(emacs-lisp-mode lisp-interaction-mode) "Emacs Lisp")
(set-ligatures! 'emacs-lisp-mode :lambda "lambda")
2018-06-27 22:52:46 +02:00
(set-rotate-patterns! 'emacs-lisp-mode
:symbols '(("t" "nil")
("let" "let*")
("when" "unless")
("advice-add" "advice-remove")
("defadvice!" "undefadvice!")
2018-06-27 22:52:46 +02:00
("add-hook" "remove-hook")
("add-hook!" "remove-hook!")
("it" "xit")
("describe" "xdescribe")))
2018-06-27 22:52:46 +02:00
(setq-hook! 'emacs-lisp-mode-hook
;; Emacs' built-in elisp files use a hybrid tab->space indentation scheme
;; with a tab width of 8. Any smaller and the indentation will be
;; unreadable. Since Emacs' lisp indenter doesn't respect this variable it's
;; safe to ignore this setting otherwise.
tab-width 8
;; shorter name in modeline
mode-name "Elisp"
;; Don't treat autoloads or sexp openers as outline headers, we have
;; hideshow for that.
2020-04-30 15:54:36 -04:00
outline-regexp +emacs-lisp-outline-regexp
;; Fixed indenter that intends plists sensibly.
lisp-indent-function #'+emacs-lisp-indent-function)
2020-05-25 02:29:30 -04:00
;; variable-width indentation is superior in elisp. Otherwise, `dtrt-indent'
;; and `editorconfig' would force fixed indentation on elisp.
(add-to-list 'doom-detect-indentation-excluded-modes 'emacs-lisp-mode)
2018-06-27 22:52:46 +02:00
(add-hook! 'emacs-lisp-mode-hook
2020-05-25 02:29:30 -04:00
;; Allow folding of outlines in comments
#'outline-minor-mode
2020-05-25 02:29:30 -04:00
;; Make parenthesis depth easier to distinguish at a glance
#'rainbow-delimiters-mode
2020-05-25 02:29:30 -04:00
;; Make quoted symbols easier to distinguish from free variables
#'highlight-quoted-mode
2020-05-25 02:29:30 -04:00
;; Extend imenu support to Doom constructs
#'+emacs-lisp-extend-imenu-h
;; Ensure straight sees modifications to installed packages
#'+emacs-lisp-init-straight-maybe-h)
2019-04-26 17:42:44 -04:00
;; Flycheck's two emacs-lisp checkers produce a *lot* of false positives in
;; emacs configs, so we disable `emacs-lisp-checkdoc' and reduce the
;; `emacs-lisp' checker's verbosity.
(add-hook 'flycheck-mode-hook #'+emacs-lisp-reduce-flycheck-errors-in-emacs-config-h)
2020-05-25 02:29:30 -04:00
;; Enhance elisp syntax highlighting, by highlighting Doom-specific
;; constructs, defined symbols, and truncating :pin's in `package!' calls.
(font-lock-add-keywords
'emacs-lisp-mode
(append `(;; custom Doom cookies
2019-03-09 02:42:03 -05:00
("^;;;###\\(autodef\\|if\\|package\\)[ \n]" (1 font-lock-warning-face t)))
2020-05-25 02:29:30 -04:00
;; Shorten the :pin of `package!' statements to 10 characters
`(("(package!\\_>" (0 (+emacs-lisp-truncate-pin))))
;; highlight defined, special variables & functions
(when +emacs-lisp-enable-extra-fontification
2020-05-25 02:29:30 -04:00
`((+emacs-lisp-highlight-vars-and-faces . +emacs-lisp--face)))))
;; Recenter window after following definition
:boom: revise advice naming convention (1/2) This is first of three big naming convention updates that have been a long time coming. With 2.1 on the horizon, all the breaking updates will batched together in preparation for the long haul. In this commit, we do away with the asterix to communicate that a function is an advice function, and we replace it with the '-a' suffix. e.g. doom*shut-up -> doom-shut-up-a doom*recenter -> doom-recenter-a +evil*static-reindent -> +evil--static-reindent-a The rationale behind this change is: 1. Elisp's own formatting/indenting tools would occasionally struggle with | and * (particularly pp and cl-prettyprint). They have no problem with / and :, fortunately. 2. External syntax highlighters (like pygmentize, discord markdown or github markdown) struggle with it, sometimes refusing to highlight code beyond these symbols. 3. * and | are less expressive than - and -- in communicating the intended visibility, versatility and stability of a function. 4. It complicated the regexps we must use to search for them. 5. They were arbitrary and over-complicated to begin with, decided on haphazardly way back when Doom was simply "my private config". Anyhow, like how predicate functions have the -p suffix, we'll adopt the -a suffix for advice functions, -h for hook functions and -fn for variable functions. Other noteable changes: - Replaces advice-{add,remove}! macro with new def-advice! macro. The old pair weren't as useful. The new def-advice! saves on a lot of space. - Removed "stage" assertions to make sure you were using the right macros in the right place. Turned out to not be necessary, we'll employ better checks later.
2019-07-18 15:42:52 +02:00
(advice-add #'elisp-def :after #'doom-recenter-a)
Introduce general.el & rewrite map! + Now uses an overriding keymap for leader keys, so that it is always available, even outside of normal/visual states. In insert/emacs states, or in sessions where evil is absent, an alternative prefix is used for leader/localleader keys. See these variables: + doom-leader-prefix + doom-leader-alt-prefix + doom-localleader-prefix + doom-localleader-alt-prefix + Keybinds now support alternative prefixes through the new :alt-prefix property. This is useful for non-evil users and non-normal evil states. By default, this is M-SPC (leader) and M-SPC m (localleader). + Removed +evil-commands flag from config/default (moved to feature/evil/+commands.el). + config/default/+bindings.el has been split into config/default/+{evil,emacs}-bindings.el, which one is loaded depends on whether evil is present or not. The latter is blank, but will soon be populated with a keybinding scheme for non-evil users (perhaps inspired by #641). + The define-key! macro has been replaced; it is now an alias for general-def. + Added unmap! as an alias for general-unbind. + The following modifier key conventions are now enforced for consistency, across all OSes: alt/option = meta windows/command = super It used to be alt/option = alt windows/command = meta Many of the default keybinds have been updated to reflect this switch, but it is likely to affect personal meta/super keybinds! The map! macro has also been rewritten to use general-define-key. Here is what has been changed: + map! no longer works with characters, e.g. (map! ?x #'do-something) is no longer supported. Keys must be kbd-able strings like "C-c x" or vectors like [?C-c ?x]. + The :map and :map* properties are now the same thing. If specified keymaps aren't defined when binding keys, it is automatically deferred. + The way you bind local keybinds has changed: ;; Don't do this (map! :l "a" #'func-a :l "b" #'func-b) ;; Do this (map! :map 'local "a" #'func-a "b" #'func-b) + map! now supports the following new blocks: + (:if COND THEN-FORM ELSE-FORM...) + (:alt-prefix PREFIX KEYS...) -- this prefix will be used for non-normal evil states. Equivalent to :non-normal-prefix in general. + The way you declare a which-key label for a prefix key has changed: ;; before (map! :desc "label" :prefix "a" ...) ;; now (map! :prefix ("a" . "label") ...) + It used to be that map! supported binding a key to a key sequence, like so: (map! "a" [?x]) ; pressing a is like pressing x This functionality was removed *temporarily* while I figure out the implementation. Addresses: #448, #814, #860 Mentioned in: #940
2018-12-22 03:30:04 -05:00
2020-05-06 21:01:04 -04:00
(defadvice! +emacs-lisp-append-value-to-eldoc-a (orig-fn sym)
"Display variable value next to documentation in eldoc."
:around #'elisp-get-var-docstring
(when-let (ret (funcall orig-fn sym))
(if (boundp sym)
(concat ret " "
(let* ((truncated " [...]")
(print-escape-newlines t)
(str (symbol-value sym))
(str (prin1-to-string str))
(limit (- (frame-width) (length ret) (length truncated) 1)))
(format (format "%%0.%ds%%s" (max limit 0))
(propertize str 'face 'warning)
(if (< (length str) limit) "" truncated))))
ret)))
2020-05-06 21:01:04 -04:00
Introduce general.el & rewrite map! + Now uses an overriding keymap for leader keys, so that it is always available, even outside of normal/visual states. In insert/emacs states, or in sessions where evil is absent, an alternative prefix is used for leader/localleader keys. See these variables: + doom-leader-prefix + doom-leader-alt-prefix + doom-localleader-prefix + doom-localleader-alt-prefix + Keybinds now support alternative prefixes through the new :alt-prefix property. This is useful for non-evil users and non-normal evil states. By default, this is M-SPC (leader) and M-SPC m (localleader). + Removed +evil-commands flag from config/default (moved to feature/evil/+commands.el). + config/default/+bindings.el has been split into config/default/+{evil,emacs}-bindings.el, which one is loaded depends on whether evil is present or not. The latter is blank, but will soon be populated with a keybinding scheme for non-evil users (perhaps inspired by #641). + The define-key! macro has been replaced; it is now an alias for general-def. + Added unmap! as an alias for general-unbind. + The following modifier key conventions are now enforced for consistency, across all OSes: alt/option = meta windows/command = super It used to be alt/option = alt windows/command = meta Many of the default keybinds have been updated to reflect this switch, but it is likely to affect personal meta/super keybinds! The map! macro has also been rewritten to use general-define-key. Here is what has been changed: + map! no longer works with characters, e.g. (map! ?x #'do-something) is no longer supported. Keys must be kbd-able strings like "C-c x" or vectors like [?C-c ?x]. + The :map and :map* properties are now the same thing. If specified keymaps aren't defined when binding keys, it is automatically deferred. + The way you bind local keybinds has changed: ;; Don't do this (map! :l "a" #'func-a :l "b" #'func-b) ;; Do this (map! :map 'local "a" #'func-a "b" #'func-b) + map! now supports the following new blocks: + (:if COND THEN-FORM ELSE-FORM...) + (:alt-prefix PREFIX KEYS...) -- this prefix will be used for non-normal evil states. Equivalent to :non-normal-prefix in general. + The way you declare a which-key label for a prefix key has changed: ;; before (map! :desc "label" :prefix "a" ...) ;; now (map! :prefix ("a" . "label") ...) + It used to be that map! supported binding a key to a key sequence, like so: (map! "a" [?x]) ; pressing a is like pressing x This functionality was removed *temporarily* while I figure out the implementation. Addresses: #448, #814, #860 Mentioned in: #940
2018-12-22 03:30:04 -05:00
(map! :localleader
:map emacs-lisp-mode-map
:desc "Expand macro" "m" #'macrostep-expand
(:prefix ("d" . "debug")
"f" #'+emacs-lisp/edebug-instrument-defun-on
"F" #'+emacs-lisp/edebug-instrument-defun-off)
(:prefix ("e" . "eval")
"b" #'eval-buffer
"d" #'eval-defun
"e" #'eval-last-sexp
"r" #'eval-region
"l" #'load-library)
(:prefix ("g" . "goto")
"f" #'find-function
"v" #'find-variable
"l" #'find-library)))
2017-02-08 02:23:06 -05:00
2020-05-28 11:53:15 +10:00
(use-package! ielm
:defer t
:config
(set-lookup-handlers! 'inferior-emacs-lisp-mode
:definition #'+emacs-lisp-lookup-definition
:documentation #'+emacs-lisp-lookup-documentation)
;; Adapted from http://www.modernemacs.com/post/comint-highlighting/ to add
;; syntax highlighting to ielm REPLs.
(setq ielm-font-lock-keywords
(append '(("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)"
(1 font-lock-comment-face)
(2 font-lock-constant-face)))
(when (require 'highlight-numbers nil t)
(highlight-numbers--get-regexp-for-mode 'emacs-lisp-mode))
(cl-loop for (matcher . match-highlights)
in (append lisp-el-font-lock-keywords-2
lisp-cl-font-lock-keywords-2)
collect
`((lambda (limit)
(when ,(if (symbolp matcher)
`(,matcher limit)
`(re-search-forward ,matcher limit t))
;; Only highlight matches after the prompt
(> (match-beginning 0) (car comint-last-prompt))
;; Make sure we're not in a comment or string
(let ((state (syntax-ppss)))
(not (or (nth 3 state)
(nth 4 state))))))
,@match-highlights)))))
2019-10-17 01:53:14 -04:00
;;
;;; Packages
;;;###package overseer
(autoload 'overseer-test "overseer" nil t)
2020-05-25 02:29:30 -04:00
;; Properly lazy load overseer by not loading it so early:
(remove-hook 'emacs-lisp-mode-hook #'overseer-enable-mode)
(use-package! flycheck-cask
:when (featurep! :checkers syntax)
:defer t
2017-07-17 11:33:47 +02:00
:init
(add-hook! 'emacs-lisp-mode-hook
2017-07-17 11:33:47 +02:00
(add-hook 'flycheck-mode-hook #'flycheck-cask-setup nil t)))
2021-07-25 16:37:28 -04:00
(use-package! flycheck-package
:when (featurep! :checkers syntax)
:after flycheck
:config (flycheck-package-setup))
(use-package! elisp-demos
:defer t
:init
(advice-add 'describe-function-1 :after #'elisp-demos-advice-describe-function-1)
(advice-add 'helpful-update :after #'elisp-demos-advice-helpful-update)
:config
(defadvice! +emacs-lisp--add-doom-elisp-demos-a (orig-fn symbol)
"Add Doom's own demos to help buffers."
:around #'elisp-demos--search
(or (funcall orig-fn symbol)
2021-01-17 15:53:12 -05:00
(when-let (demos-file (doom-module-locate-path :lang 'emacs-lisp "demos.org"))
2019-09-07 19:56:52 -04:00
(with-temp-buffer
(insert-file-contents demos-file)
(goto-char (point-min))
(when (re-search-forward
2021-01-17 15:53:12 -05:00
(format "^\\*\\* %s$" (regexp-quote (symbol-name symbol)))
2019-09-07 19:56:52 -04:00
nil t)
(let (beg end)
(forward-line 1)
(setq beg (point))
(if (re-search-forward "^\\*" nil t)
(setq end (line-beginning-position))
(setq end (point-max)))
(string-trim (buffer-substring-no-properties beg end)))))))))
(use-package! buttercup
:defer t
:minor ("/test[/-].+\\.el$" . buttercup-minor-mode)
:preface
;; buttercup.el doesn't define a keymap for `buttercup-minor-mode', as we have
;; to fool its internal `define-minor-mode' call into thinking one exists, so
;; it will associate it with the mode.
(defvar buttercup-minor-mode-map (make-sparse-keymap))
:config
(set-popup-rule! "^\\*Buttercup\\*$" :size 0.45 :select nil :ttl 0)
(set-yas-minor-mode! 'buttercup-minor-mode)
(when (featurep 'evil)
(add-hook 'buttercup-minor-mode-hook #'evil-normalize-keymaps))
2019-12-22 23:02:54 -05:00
(map! :localleader
:map buttercup-minor-mode-map
:prefix "t"
"t" #'+emacs-lisp/buttercup-run-file
"a" #'+emacs-lisp/buttercup-run-project
"s" #'buttercup-run-at-point))
;;
;;; Project modes
(def-project-mode! +emacs-lisp-ert-mode
:modes '(emacs-lisp-mode)
:match "/test[/-].+\\.el$"
:add-hooks '(overseer-enable-mode))