2015-02-08 17:19:34 +00:00
|
|
|
;;; core-toggle.el --- Spacemacs Core File
|
2015-01-29 05:08:48 +00:00
|
|
|
;;
|
2020-09-16 21:34:40 +00:00
|
|
|
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
|
2015-01-29 05:08:48 +00:00
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;;; License: GPLv3
|
|
|
|
(require 'core-funcs)
|
|
|
|
|
|
|
|
(defvar spacemacs-toggles '()
|
|
|
|
"List of all declared toggles. The structure of an element is a
|
|
|
|
property list (name :func FUNCTION :doc STRING :key STRING).")
|
|
|
|
|
|
|
|
(defmacro spacemacs|add-toggle (name &rest props)
|
|
|
|
"Add a toggle with NAME symbol.
|
|
|
|
|
2015-08-01 03:55:24 +00:00
|
|
|
This macro creates the following functions:
|
|
|
|
- spacemacs/toggle-NAME switches on or off depending on the current state
|
|
|
|
- spacemacs/toggle-NAME-on only switches on if currently disabled
|
|
|
|
- spacemacs/toggle-NAME-off only switches off if currently enabled
|
2018-01-04 05:03:22 +00:00
|
|
|
- spacemacs/toggle-NAME-status returns non-nil if the toggle is on
|
|
|
|
|
|
|
|
Additional sets of functions are created when the toggle is major mode
|
|
|
|
specific (i.e. it uses the keyword `:evil-leader-for-mode'):
|
|
|
|
- spacemacs/toggle-NAME-register-on-hook-MODE to add a hook to call the toggle on
|
|
|
|
function
|
|
|
|
- spacemacs/toggle-NAME-on-unregister-hook-MODE to remove the hook
|
|
|
|
- spacemacs/toggle-NAME-on-register-hooks to add hooks for all supported major modes
|
|
|
|
- spacemacs/toggle-NAME-on-unregister-hooks to remove all the hooks
|
2015-08-01 03:55:24 +00:00
|
|
|
|
2018-01-04 08:48:46 +00:00
|
|
|
Available PROPS:
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-01-30 05:18:48 +00:00
|
|
|
`:status EXPRESSION'
|
|
|
|
The EXPRESSION to evaluate to get the current status of the toggle.
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-01-30 05:18:48 +00:00
|
|
|
`:if EXPRESSION'
|
|
|
|
If this EXPRESSION evaluate to nil then no attempt to update the toggle
|
|
|
|
status will be performed.
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-01-30 05:18:48 +00:00
|
|
|
`:on BODY'
|
|
|
|
Evaluate BODY when the toggle is switched on.
|
|
|
|
|
|
|
|
`:off BODY'
|
|
|
|
Evaluate BODY when the toggle is switched off.
|
2015-01-29 05:08:48 +00:00
|
|
|
|
|
|
|
`:documentation STRING'
|
2015-01-30 05:18:48 +00:00
|
|
|
STRING describes what the toggle does.
|
|
|
|
|
2016-03-22 16:32:21 +00:00
|
|
|
`:prefix SYMBOL'
|
2016-05-06 11:44:49 +00:00
|
|
|
SYMBOL is bound to the raw value of prefix-arg (same as calling
|
|
|
|
(interactive \"P\")) in the wrapper function.
|
2016-03-22 16:32:21 +00:00
|
|
|
|
|
|
|
`:on-message EXPRESSION'
|
|
|
|
EXPRESSION is evaluated and displayed when the \"on\" toggle is activated.
|
|
|
|
|
2018-05-10 17:09:23 +00:00
|
|
|
`:off-message EXPRESSION'
|
|
|
|
EXPRESSION is evaluated and displayed when the \"off\" toggle is activated.
|
|
|
|
|
2016-05-31 13:11:00 +00:00
|
|
|
`:mode SYMBOL'
|
|
|
|
If given, must be a minor mode. This overrides `:on', `:off' and `:status'.
|
|
|
|
|
2015-02-24 04:28:23 +00:00
|
|
|
All properties supported by `spacemacs//create-key-binding-form' can be
|
|
|
|
used."
|
2015-07-25 13:14:53 +00:00
|
|
|
(declare (indent 1))
|
2015-01-29 05:08:48 +00:00
|
|
|
(let* ((wrapper-func (intern (format "spacemacs/toggle-%s"
|
|
|
|
(symbol-name name))))
|
2018-01-04 05:03:22 +00:00
|
|
|
(wrapper-func-status (intern (format "%s-status" wrapper-func)))
|
2015-07-31 20:41:41 +00:00
|
|
|
(wrapper-func-on (intern (format "%s-on" wrapper-func)))
|
|
|
|
(wrapper-func-off (intern (format "%s-off" wrapper-func)))
|
2016-05-31 13:11:00 +00:00
|
|
|
(mode (plist-get props :mode))
|
|
|
|
(status (or mode (plist-get props :status)))
|
2015-01-30 05:18:48 +00:00
|
|
|
(condition (plist-get props :if))
|
2015-01-29 05:08:48 +00:00
|
|
|
(doc (plist-get props :documentation))
|
2018-06-07 10:13:06 +00:00
|
|
|
(on-body (if mode `((,mode)) (spacemacs/mplist-get-values props :on)))
|
|
|
|
(off-body (if mode `((,mode -1)) (spacemacs/mplist-get-values props :off)))
|
2016-03-22 16:32:21 +00:00
|
|
|
(prefix-arg-var (plist-get props :prefix))
|
|
|
|
(on-message (plist-get props :on-message))
|
2018-05-10 17:09:23 +00:00
|
|
|
(off-message (plist-get props :off-message))
|
2018-06-07 10:13:06 +00:00
|
|
|
(evil-leader-for-mode (spacemacs/mplist-get-values props :evil-leader-for-mode))
|
2018-01-07 02:59:16 +00:00
|
|
|
(supported-modes-string (mapconcat (lambda (x) (symbol-name (car x)))
|
2018-01-04 05:03:22 +00:00
|
|
|
evil-leader-for-mode ", "))
|
2015-07-31 20:41:41 +00:00
|
|
|
(bindkeys (spacemacs//create-key-binding-form props wrapper-func))
|
|
|
|
;; we evaluate condition and status only if they are a list or
|
|
|
|
;; a bound symbol
|
|
|
|
(status-eval `(and (or (and (symbolp ',status) (boundp ',status))
|
|
|
|
(listp ',status))
|
2016-10-27 10:58:00 +00:00
|
|
|
,status))
|
2018-01-04 05:03:22 +00:00
|
|
|
(condition-eval (if condition
|
|
|
|
`(and (or (and (symbolp ',condition)
|
|
|
|
(boundp ',condition))
|
2016-10-27 10:58:00 +00:00
|
|
|
(listp ',condition))
|
2018-01-04 05:03:22 +00:00
|
|
|
,condition)
|
|
|
|
t)))
|
2015-01-29 05:08:48 +00:00
|
|
|
`(progn
|
2018-06-01 06:59:57 +00:00
|
|
|
(let ((properties (append '(:function ,wrapper-func :predicate ,wrapper-func-status)
|
|
|
|
',props))
|
|
|
|
(cell (assq ',name spacemacs-toggles)))
|
|
|
|
(if cell
|
|
|
|
(setcdr cell properties)
|
|
|
|
(push (cons ',name properties) spacemacs-toggles)))
|
2015-01-30 05:18:48 +00:00
|
|
|
;; toggle function
|
2016-03-22 16:32:21 +00:00
|
|
|
(defun ,wrapper-func ,(if prefix-arg-var (list prefix-arg-var) ())
|
2019-08-04 16:55:29 +00:00
|
|
|
,(format "Toggle %s on and off.%s"
|
|
|
|
(symbol-name name)
|
|
|
|
(if doc (concat "\n\n" doc) ""))
|
2016-03-22 16:32:21 +00:00
|
|
|
,(if prefix-arg-var '(interactive "P") '(interactive))
|
2016-10-27 10:58:00 +00:00
|
|
|
(if ,condition-eval
|
2018-01-04 05:03:22 +00:00
|
|
|
;; check if current buffer major mode supports the toggle
|
|
|
|
(if (and ',evil-leader-for-mode
|
|
|
|
(not (assq major-mode ',evil-leader-for-mode)))
|
|
|
|
(message (concat
|
|
|
|
"Toggle: %S\n"
|
|
|
|
"This toggle is not supported with major mode: %S\n"
|
|
|
|
"Supported major modes are: %s")
|
|
|
|
',name
|
|
|
|
major-mode
|
|
|
|
,supported-modes-string
|
|
|
|
)
|
|
|
|
(if (,wrapper-func-status)
|
|
|
|
(progn ,@off-body
|
|
|
|
(when (called-interactively-p 'any)
|
2018-05-10 17:09:23 +00:00
|
|
|
(message ,(or off-message (format "%s disabled." name)))))
|
2018-01-04 05:03:22 +00:00
|
|
|
,@on-body
|
|
|
|
(when (called-interactively-p 'any)
|
|
|
|
(message ,(or on-message (format "%s enabled." name))))))
|
|
|
|
(message (concat
|
|
|
|
"Toggle: %S\n"
|
|
|
|
"This toggle is not supported.")
|
|
|
|
',name)))
|
2016-05-31 13:33:44 +00:00
|
|
|
;; predicate function
|
|
|
|
(defun ,wrapper-func-status ()
|
|
|
|
,(format "Check if %s is on." (symbol-name name))
|
2016-10-27 10:58:00 +00:00
|
|
|
(and ,condition-eval ,status-eval))
|
2018-01-04 05:03:22 +00:00
|
|
|
;; Only define on or off functions when status is available
|
2015-08-24 13:27:06 +00:00
|
|
|
,@(when status
|
2018-01-04 05:03:22 +00:00
|
|
|
`(
|
|
|
|
;; on function
|
|
|
|
(defun ,wrapper-func-on ()
|
2015-08-24 13:27:06 +00:00
|
|
|
,(format "Toggle %s on." (symbol-name name))
|
|
|
|
(interactive)
|
2016-05-31 13:33:44 +00:00
|
|
|
(unless (,wrapper-func-status) (,wrapper-func)))
|
2018-01-04 05:03:22 +00:00
|
|
|
;; off function
|
2015-08-24 13:27:06 +00:00
|
|
|
(defun ,wrapper-func-off ()
|
|
|
|
,(format "Toggle %s off." (symbol-name name))
|
|
|
|
(interactive)
|
2018-01-04 05:03:22 +00:00
|
|
|
(when (,wrapper-func-status) (,wrapper-func)))
|
|
|
|
;; on and off functions for each mode specific toggles
|
|
|
|
,@(when evil-leader-for-mode
|
|
|
|
(let ((wrapper-func-register-hooks
|
|
|
|
(intern (format "%s-register-hooks" wrapper-func-on)))
|
|
|
|
(wrapper-func-unregister-hooks
|
|
|
|
(intern (format "%s-unregister-hooks" wrapper-func-on)))
|
|
|
|
wrapper-mode-funcs)
|
|
|
|
;; register all hooks to turn on toggle
|
|
|
|
(push `(defun ,wrapper-func-register-hooks ()
|
|
|
|
,(format (concat
|
|
|
|
"Register hooks to toggle %s on for all "
|
|
|
|
"supported buffers.\n"
|
|
|
|
"Supported buffer major modes are: %s")
|
|
|
|
(symbol-name name)
|
|
|
|
supported-modes-string)
|
|
|
|
(interactive)
|
|
|
|
(dolist (m ',(mapcar 'car evil-leader-for-mode))
|
|
|
|
(let ((mode-hook (intern (format "%s-hook" m))))
|
|
|
|
(add-hook mode-hook ',wrapper-func-on))))
|
|
|
|
wrapper-mode-funcs)
|
|
|
|
;; unregister all hooks to turn on toggle
|
|
|
|
(push `(defun ,wrapper-func-unregister-hooks ()
|
|
|
|
,(format (concat
|
|
|
|
"Unregister hooks to toggle %s on for all"
|
|
|
|
" supported buffers.\n"
|
|
|
|
"Supported buffer major modes are: %s")
|
|
|
|
(symbol-name name)
|
|
|
|
supported-modes-string)
|
|
|
|
(interactive)
|
|
|
|
(dolist (m ',(mapcar 'car evil-leader-for-mode))
|
|
|
|
(let ((mode-hook (intern (format "%s-hook" m))))
|
|
|
|
(remove-hook mode-hook ',wrapper-func-on))))
|
|
|
|
wrapper-mode-funcs)
|
|
|
|
(dolist (m (mapcar 'car evil-leader-for-mode))
|
|
|
|
(let* ((mode-hook (intern (format "%s-hook" m)))
|
|
|
|
(wrapper-func-register-hook
|
|
|
|
(intern (format "%s-register-hook-%s"
|
|
|
|
wrapper-func-on m)))
|
|
|
|
(wrapper-func-unregister-hook
|
|
|
|
(intern (format "%s-unregister-hook-%s"
|
|
|
|
wrapper-func-on m))))
|
|
|
|
;; register hook to turn on toggle
|
|
|
|
(push `(defun ,wrapper-func-register-hook ()
|
|
|
|
,(format (concat
|
|
|
|
"Register hook to toggle %s on for "
|
|
|
|
"all `%s' buffers.")
|
|
|
|
(symbol-name name) m)
|
|
|
|
(interactive)
|
|
|
|
(add-hook ',mode-hook ',wrapper-func-on))
|
|
|
|
wrapper-mode-funcs)
|
|
|
|
;; unregister hook to turn on toggle
|
|
|
|
(push `(defun ,wrapper-func-unregister-hook ()
|
|
|
|
,(format (concat
|
|
|
|
"Unregister hook to toggle %s off for"
|
|
|
|
" all `%s' buffers.")
|
|
|
|
(symbol-name name) m)
|
|
|
|
(interactive)
|
|
|
|
(remove-hook ',mode-hook ',wrapper-func-on))
|
|
|
|
wrapper-mode-funcs)))
|
|
|
|
wrapper-mode-funcs))))
|
2015-02-24 04:28:23 +00:00
|
|
|
,@bindkeys)))
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-02-08 17:19:34 +00:00
|
|
|
(provide 'core-toggle)
|