2015-02-08 17:19:34 +00:00
|
|
|
;;; core-toggle.el --- Spacemacs Core File
|
2015-01-29 05:08:48 +00:00
|
|
|
;;
|
|
|
|
;; Copyright (c) 2012-2014 Sylvain Benner
|
|
|
|
;; Copyright (c) 2014-2015 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
|
|
|
|
(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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
`:evil-leader STRING'
|
|
|
|
A key sequence string to be set with `evil-leader/set-key'.
|
|
|
|
|
|
|
|
`:evil-leader-for-mode CONS CELL'
|
|
|
|
A cons cell (MODE . KEY) where MODE is a major-mode symbol and KEY is a
|
|
|
|
key sequence string to be set with `evil-leader/set-key-for-mode'.
|
|
|
|
|
|
|
|
`:global-key STRING'
|
|
|
|
A key sequence string to be set with `global-set-key'.
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-01-30 05:26:49 +00:00
|
|
|
`:define-key CONS CELL'
|
2015-01-30 05:18:48 +00:00
|
|
|
A cons cell (MAP . KEY) where MAP is a mode map and KEY is a
|
|
|
|
key sequence string to be set with `define-key'. "
|
2015-01-29 05:08:48 +00:00
|
|
|
(let* ((wrapper-func (intern (format "spacemacs/toggle-%s"
|
|
|
|
(symbol-name name))))
|
2015-01-30 05:18:48 +00:00
|
|
|
(status (plist-get props :status))
|
|
|
|
(condition (plist-get props :if))
|
2015-01-29 05:08:48 +00:00
|
|
|
(doc (plist-get props :documentation))
|
|
|
|
(on-body (spacemacs/mplist-get props :on))
|
|
|
|
(off-body (spacemacs/mplist-get props :off))
|
2015-01-30 05:18:48 +00:00
|
|
|
(evil-leader (plist-get props :evil-leader))
|
|
|
|
(evil-leader-for-mode (plist-get props :evil-leader-for-mode))
|
|
|
|
(global-key (plist-get props :global-key))
|
2015-01-30 05:26:49 +00:00
|
|
|
(def-key (plist-get props :define-key)))
|
2015-01-29 05:08:48 +00:00
|
|
|
`(progn
|
2015-02-01 06:31:52 +00:00
|
|
|
(push (append '(,name) '(:function ,wrapper-func) ',props)
|
|
|
|
spacemacs-toggles)
|
2015-01-30 05:18:48 +00:00
|
|
|
;; toggle function
|
2015-01-29 05:08:48 +00:00
|
|
|
(defun ,wrapper-func ()
|
|
|
|
,(format "Toggle %s on and off." (symbol-name name))
|
|
|
|
(interactive)
|
2015-01-30 05:18:48 +00:00
|
|
|
;; we evaluate condition and status only if they are a list or
|
|
|
|
;; a bound symbol
|
|
|
|
(if (or (null ',condition)
|
|
|
|
(and (or (and (symbolp ',condition) (boundp ',condition))
|
|
|
|
(listp ',condition))
|
|
|
|
,condition))
|
|
|
|
(if (and (or (and (symbolp ',status) (boundp ',status))
|
|
|
|
(listp ',status))
|
|
|
|
,status) (progn ,@off-body) ,@on-body)
|
|
|
|
(message "This toggle is not supported.")))
|
|
|
|
;; key bindings
|
|
|
|
(when ,evil-leader
|
|
|
|
(evil-leader/set-key ,evil-leader ',wrapper-func))
|
|
|
|
(when ,evil-leader-for-mode
|
|
|
|
(evil-leader/set-key-for-mode
|
|
|
|
'(car ,evil-leader-for-mode)
|
|
|
|
(cdr ,evil-leader-for-mode) ',wrapper-func))
|
|
|
|
(when ,global-key
|
|
|
|
(global-set-key (kbd ,global-key) ',wrapper-func))
|
2015-01-30 05:26:49 +00:00
|
|
|
(when ,def-key
|
|
|
|
(define-key (car ,def-key) (kbd ,(cdr def-key)) ',wrapper-func)))))
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-02-08 17:19:34 +00:00
|
|
|
(provide 'core-toggle)
|