2015-02-08 17:19:34 +00:00
|
|
|
;;; core-toggle.el --- Spacemacs Core File
|
2015-01-29 05:08:48 +00:00
|
|
|
;;
|
2017-01-06 03:51:13 +00:00
|
|
|
;; Copyright (c) 2012-2017 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
|
|
|
|
|
2015-01-29 05:08:48 +00:00
|
|
|
Avaiblabe PROPS:
|
|
|
|
|
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.
|
|
|
|
|
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))))
|
2016-05-31 13:33:44 +00:00
|
|
|
(wrapper-func-status (intern (format "%s-p" 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))
|
2016-05-31 13:11:00 +00:00
|
|
|
(on-body (if mode `((,mode)) (spacemacs/mplist-get props :on)))
|
|
|
|
(off-body (if mode `((,mode -1)) (spacemacs/mplist-get props :off)))
|
2016-03-22 16:32:21 +00:00
|
|
|
(prefix-arg-var (plist-get props :prefix))
|
|
|
|
(on-message (plist-get props :on-message))
|
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))
|
|
|
|
(condition-eval `(or (null ',condition)
|
|
|
|
(and (or (and (symbolp ',condition) (boundp ',condition))
|
|
|
|
(listp ',condition))
|
|
|
|
,condition))))
|
2015-01-29 05:08:48 +00:00
|
|
|
`(progn
|
2016-05-31 13:33:44 +00:00
|
|
|
(push (append '(,name) '(:function ,wrapper-func
|
|
|
|
:predicate ,wrapper-func-status) ',props)
|
2015-02-01 06:31:52 +00:00
|
|
|
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) ())
|
2015-01-29 05:08:48 +00:00
|
|
|
,(format "Toggle %s on and off." (symbol-name name))
|
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
|
2016-05-31 13:33:44 +00:00
|
|
|
(if (,wrapper-func-status)
|
2015-08-01 03:55:24 +00:00
|
|
|
(progn ,@off-body
|
2016-06-24 08:04:03 +00:00
|
|
|
(when (called-interactively-p 'any)
|
|
|
|
(message ,(format "%s disabled." name))))
|
2015-08-01 03:55:24 +00:00
|
|
|
,@on-body
|
2016-06-24 08:04:03 +00:00
|
|
|
(when (called-interactively-p 'any)
|
|
|
|
(message ,(or on-message (format "%s enabled." name)))))
|
2015-01-30 05:18:48 +00:00
|
|
|
(message "This toggle is not supported.")))
|
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))
|
2015-08-24 13:27:06 +00:00
|
|
|
;; Only define on- or off-functions when status is available
|
|
|
|
,@(when status
|
|
|
|
;; on-function
|
|
|
|
`((defun ,wrapper-func-on ()
|
|
|
|
,(format "Toggle %s on." (symbol-name name))
|
|
|
|
(interactive)
|
2016-05-31 13:33:44 +00:00
|
|
|
(unless (,wrapper-func-status) (,wrapper-func)))
|
2015-08-24 13:27:06 +00:00
|
|
|
;; off-function
|
|
|
|
(defun ,wrapper-func-off ()
|
|
|
|
,(format "Toggle %s off." (symbol-name name))
|
|
|
|
(interactive)
|
2016-05-31 13:33:44 +00:00
|
|
|
(when (,wrapper-func-status) (,wrapper-func)))))
|
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)
|