spacemacs/core/core-use-package-ext.el
Maximilian Wolff 6e6bbe039e
[core] Make spacemacs|use-package-add-hook emit code for warning generation
In my first commit I did not add the warning message properly.
Basically the macro should emit code to create a warning if
the generated code is run and the predicate is not fulfilled.

However in my last commit it did run the code during macro
expansion which would do the testing there which is not
what I wanted.
2020-06-07 21:32:42 +02:00

57 lines
2.2 KiB
EmacsLisp

;;; core-use-package-ext.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defconst spacemacs--use-package-add-hook-keywords '(:pre-init
:post-init
:pre-config
:post-config))
(defmacro spacemacs|use-package-add-hook (name &rest plist)
"Add post hooks to `:init' or `:config' arguments of an existing
configuration.
In order to use this macro the variable `use-package-inject-hooks'
must be non-nil. If it is not a warning will be issued.
This is useful in the dotfile to override the default configuration
of a package.
Usage:
(spacemacs|use-package-add-hook package-name
[:keyword [option]]...)
:pre-init Code to run before the default `:init' configuration.
:post-init Code to run after the default `:init' configuration.
:pre-config Code to run before the default `:config' configuration.
:post-config Code to run after the default `:config' configuration.
In practice the most useful hook is the `:post-config' where you can
override lazy-loaded settings."
(declare (indent 1))
(let ((name-symbol (if (stringp name) (intern name) name))
(expanded-forms '()))
(dolist (keyword spacemacs--use-package-add-hook-keywords)
(let ((body (spacemacs/mplist-get-values plist keyword)))
(when body
(let ((hook (intern (format "use-package--%S--%s-hook"
name-symbol
(substring (format "%s" keyword) 1)))))
(push `(add-hook ',hook (lambda nil ,@body t)) expanded-forms)))))
(push `(when (not use-package-inject-hooks)
(message (concat "!!!!!!WARNING!!!!!! Called use-package-add-hook without"
" "
"use-package-inject-hooks non-nil, this will most probably not work.")))
expanded-forms)
`(progn ,@expanded-forms)))
(provide 'core-use-package-ext)