Add :magic and :magic-fallback keywords (issue)

These keywords work equivalently to `:mode` or `:interpreter`, but for
`magic-mode-alist` and `magic-fallback-mode-alist`.

The handler function implementation is now passed a list to add to,
and shared by all four of them.
GitHub-reference: https://github.com/jwiegley/use-package/issues/469
This commit is contained in:
Joe Wreschnig 2017-06-15 20:44:11 +02:00
parent ec088b747a
commit ac4a3a4aa8

View file

@ -152,6 +152,8 @@ the user specified."
:bind-keymap* :bind-keymap*
:interpreter :interpreter
:mode :mode
:magic
:magic-fallback
:commands :commands
:defines :defines
:functions :functions
@ -1092,21 +1094,21 @@ deferred until the prefix key sequence is pressed."
;; ;;
(defun use-package-normalize-mode (name keyword args) (defun use-package-normalize-mode (name keyword args)
"Normalize arguments for keywords which add regexp/mode pairs to an alist."
(use-package-as-one (symbol-name keyword) args (use-package-as-one (symbol-name keyword) args
(apply-partially #'use-package-normalize-pairs (apply-partially #'use-package-normalize-pairs
#'use-package-regex-p #'use-package-regex-p
(lambda (m) (and (not (null m)) (symbolp m))) (lambda (m) (and (not (null m)) (symbolp m)))
name))) name)))
(defalias 'use-package-normalize/:interpreter 'use-package-normalize-mode) (defun use-package-handle-mode (name alist arg rest state)
"Handle keywords which add regexp/mode pairs to an alist."
(defun use-package-handler/:interpreter (name keyword arg rest state)
(let* (commands (let* (commands
(form (mapcar #'(lambda (interpreter) (form (mapcar #'(lambda (thing)
(push (cdr interpreter) commands) (push (cdr thing) commands)
(setcar interpreter (setcar thing
(use-package-normalize-regex (car interpreter))) (use-package-normalize-regex (car thing)))
`(add-to-list 'interpreter-mode-alist ',interpreter)) arg))) `(add-to-list ',alist ',thing)) arg)))
(use-package-concat (use-package-concat
(use-package-process-keywords name (use-package-process-keywords name
(use-package-sort-keywords (use-package-sort-keywords
@ -1114,6 +1116,11 @@ deferred until the prefix key sequence is pressed."
(use-package-plist-append state :commands commands)) (use-package-plist-append state :commands commands))
`((ignore ,@form))))) `((ignore ,@form)))))
(defalias 'use-package-normalize/:interpreter 'use-package-normalize-mode)
(defun use-package-handler/:interpreter (name keyword arg rest state)
(use-package-handle-mode name 'interpreter-mode-alist arg rest state))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;;; :mode ;;; :mode
@ -1122,18 +1129,27 @@ deferred until the prefix key sequence is pressed."
(defalias 'use-package-normalize/:mode 'use-package-normalize-mode) (defalias 'use-package-normalize/:mode 'use-package-normalize-mode)
(defun use-package-handler/:mode (name keyword arg rest state) (defun use-package-handler/:mode (name keyword arg rest state)
(let* (commands (use-package-handle-mode name 'auto-mode-alist arg rest state))
(form (mapcar #'(lambda (mode)
(push (cdr mode) commands) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setcar mode ;;
(use-package-normalize-regex (car mode))) ;;; :magic
`(add-to-list 'auto-mode-alist ',mode)) arg))) ;;
(use-package-concat
(use-package-process-keywords name (defalias 'use-package-normalize/:magic 'use-package-normalize-mode)
(use-package-sort-keywords
(use-package-plist-maybe-put rest :defer t)) (defun use-package-handler/:magic (name keyword arg rest state)
(use-package-plist-append state :commands commands)) (use-package-handle-mode name 'magic-mode-alist arg rest state))
`((ignore ,@form)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; :magic-fallback
;;
(defalias 'use-package-normalize/:magic-fallback 'use-package-normalize-mode)
(defun use-package-handler/:magic-fallback (name keyword arg rest state)
(use-package-handle-mode name 'magic-fallback-mode-alist arg rest state))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
@ -1455,47 +1471,51 @@ this file. Usage:
(use-package package-name (use-package package-name
[:keyword [option]]...) [:keyword [option]]...)
:init Code to run before PACKAGE-NAME has been loaded. :init Code to run before PACKAGE-NAME has been loaded.
:config Code to run after PACKAGE-NAME has been loaded. Note that if :config Code to run after PACKAGE-NAME has been loaded. Note that
loading is deferred for any reason, this code does not execute if loading is deferred for any reason, this code does not
until the lazy load has occurred. execute until the lazy load has occurred.
:preface Code to be run before everything except `:disabled'; this can :preface Code to be run before everything except `:disabled'; this
be used to define functions for use in `:if', or that should be can be used to define functions for use in `:if', or that
seen by the byte-compiler. should be seen by the byte-compiler.
:mode Form to be added to `auto-mode-alist'. :mode Form to be added to `auto-mode-alist'.
:interpreter Form to be added to `interpreter-mode-alist'. :magic Form to be added to `magic-mode-alist'.
:magic-fallback Form to be added to `magic-fallback-mode-alist'.
:mode Form to be added to `auto-mode-alist'.
:interpreter Form to be added to `interpreter-mode-alist'.
:commands Define autoloads for commands that will be defined by the :commands Define autoloads for commands that will be defined by the
package. This is useful if the package is being lazily loaded, package. This is useful if the package is being lazily
and you wish to conditionally call functions in your `:init' loaded, and you wish to conditionally call functions in your
block that are defined in the package. `:init' block that are defined in the package.
:bind Bind keys, and define autoloads for the bound commands. :bind Bind keys, and define autoloads for the bound commands.
:bind* Bind keys, and define autoloads for the bound commands, :bind* Bind keys, and define autoloads for the bound commands,
*overriding all minor mode bindings*. *overriding all minor mode bindings*.
:bind-keymap Bind a key prefix to an auto-loaded keymap defined in the :bind-keymap Bind a key prefix to an auto-loaded keymap defined in the
package. This is like `:bind', but for keymaps. package. This is like `:bind', but for keymaps.
:bind-keymap* Like `:bind-keymap', but overrides all minor mode bindings :bind-keymap* Like `:bind-keymap', but overrides all minor mode bindings
:defer Defer loading of a package -- this is implied when using :defer Defer loading of a package -- this is implied when using
`:commands', `:bind', `:bind*', `:mode' or `:interpreter'. `:commands', `:bind', `:bind*', `:mode', `:magic',
This can be an integer, to force loading after N seconds of `:magic-fallback', or `:interpreter'. This can be an integer,
idle time, if the package has not already been loaded. to force loading after N seconds of idle time, if the package
has not already been loaded.
:after Defer loading of a package until after any of the named :after Defer loading of a package until after any of the named
features are loaded. features are loaded.
:demand Prevent deferred loading in all cases. :demand Prevent deferred loading in all cases.
:if EXPR Initialize and load only if EXPR evaluates to a non-nil value. :if EXPR Initialize and load only if EXPR evaluates to a non-nil value.
:disabled The package is ignored completely if this keyword is present. :disabled The package is ignored completely if this keyword is present.
:defines Declare certain variables to silence the byte-compiler. :defines Declare certain variables to silence the byte-compiler.
:functions Declare certain functions to silence the byte-compiler. :functions Declare certain functions to silence the byte-compiler.
:load-path Add to the `load-path' before attempting to load the package. :load-path Add to the `load-path' before attempting to load the package.
:diminish Support for diminish.el (if installed). :diminish Support for diminish.el (if installed).
:ensure Loads the package using package.el if necessary. :ensure Loads the package using package.el if necessary.
:pin Pin the package to an archive." :pin Pin the package to an archive."
(declare (indent 1)) (declare (indent 1))
(unless (member :disabled args) (unless (member :disabled args)
(let ((name-symbol (if (stringp name) (intern name) name)) (let ((name-symbol (if (stringp name) (intern name) name))