Improve `spacemacs|add-toggle`

Add properties `:if` `:status`
Drop properties `:toggle-function` `:toggle-variable`
Add properties for several types of key bindings

The macro should now support a wide variety of toggles
This commit is contained in:
syl20bnr 2015-01-30 00:18:48 -05:00
parent 55c0a88a9b
commit f92e653612
5 changed files with 110 additions and 74 deletions

View File

@ -20,44 +20,73 @@ property list (name :func FUNCTION :doc STRING :key STRING).")
Avaiblabe PROPS:
`:function FUNCTION'
A symbol of a function to handle the toggle or one of the following
symbols: `global' or `globalized'.
`:status EXPRESSION'
The EXPRESSION to evaluate to get the current status of the toggle.
`global' define a default function to handle the toggle of a minor mode
defined with the property `:global t'
`:if EXPRESSION'
If this EXPRESSION evaluate to nil then no attempt to update the toggle
status will be performed.
`globalized' define a default function to handle the toogle of a minor
mode defined with `define-globalized-minor-mode'.
`:on BODY'
Evaluate BODY when the toggle is switched on.
`:off BODY'
Evaluate BODY when the toggle is switched off.
`:documentation STRING'
A docstring to describe what the toggle does.
STRING describes what the toggle does.
`:key STRING'
A key sequence to use the toggle."
`: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'.
`:local-key CONS CELL'
A cons cell (MAP . KEY) where MAP is a mode map and KEY is a
key sequence string to be set with `define-key'. "
(let* ((wrapper-func (intern (format "spacemacs/toggle-%s"
(symbol-name name))))
(toggle-func (plist-get props :toggle-function))
(toggle-var (if (plist-get props :toggle-variable)
(plist-get props :toggle-variable)
toggle-func))
(status (plist-get props :status))
(condition (plist-get props :if))
(doc (plist-get props :documentation))
(on-body (spacemacs/mplist-get props :on))
(off-body (spacemacs/mplist-get props :off))
(key (plist-get props :key)))
(push `(,name :func ,wrapper-func :doc ,doc :key ,key)
(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))
(local-key (plist-get props :local-key)))
(push (append (list name) (list :function wrapper-func) props)
spacemacs-toggles)
`(progn
;; toggle function
(defun ,wrapper-func ()
,(format "Toggle %s on and off." (symbol-name name))
(interactive)
(if (and (boundp ',toggle-var) ,toggle-var)
(progn
(,toggle-func -1)
,@on-body)
(,toggle-func)
,@off-body))
(when ,key
(evil-leader/set-key ,key ',wrapper-func)))))
;; 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))
(when ,local-key
(define-key (car ,local-key) (kbd ,(cdr local-key)) ',wrapper-func)))))
(provide 'core-toggles)

View File

@ -137,7 +137,7 @@
"Toggle candidate."
(let ((toggle (assq (intern candidate) spacemacs-toggles)))
(when toggle
(funcall (plist-get (cdr toggle) :func)))))
(funcall (plist-get (cdr toggle) :function)))))
(provide 'helm-spacemacs)

View File

@ -828,21 +828,6 @@ If ASCII si not provided then UNICODE is used instead."
"Return the line at point as a string."
(buffer-substring (line-beginning-position) (line-end-position)))
(defun spacemacs/toggle-tool-bar ()
"Toggle the tool bar.
It has no effect in a terminal."
(interactive)
(when window-system
(tool-bar-mode (if tool-bar-mode -1 1))))
(defun spacemacs/toggle-menu-bar ()
"Toggle the menu bar.
It has no effect in a terminal if the Emacs version is < `24.4'."
(interactive)
(when (or window-system
(version<= "24.3.1" emacs-version))
(menu-bar-mode (if menu-bar-mode -1 1))))
(defun spacemacs/open-in-external-app ()
"Open current file in external application."
(interactive)

View File

@ -131,53 +131,75 @@
"Sn" 'flyspell-goto-next-error)
;; toggle ---------------------------------------------------------------------
(spacemacs|add-toggle fill-column-indicator
:toggle-function toggle-fill-column-indicator
:status nil
:on (toggle-fill-column-indicator)
:documentation "Display the fill column indicator."
:key "t8")
:evil-leader "t8")
(spacemacs|add-toggle fringe
:toggle-function fringe-mode
:status (not (equal fringe-mode 0))
:on (call-interactively 'fringe-mode)
:off (fringe-mode 0)
:documentation "Display the fringe in GUI mode."
:key "tf")
:evil-leader "tf")
(spacemacs|add-toggle fullscreen-frame
:toggle-function spacemacs/toggle-frame-fullscreen
:status nil
:on (spacemacs/toggle-frame-fullscreen)
:documentation "Display the current frame in full screen."
:key "tF")
(spacemacs|add-toggle highlight-current-line
:toggle-function global-hl-line-mode
:documentation "Highlight the current line."
:key "th")
:evil-leader "tF")
(spacemacs|add-toggle highlight-current-line-globally
:status global-hl-line-mode
:on (global-hl-line-mode)
:off (global-hl-line-mode -1)
:documentation "Globally Highlight the current line."
:evil-leader "th")
(spacemacs|add-toggle truncate-lines
:toggle-function toggle-truncate-lines
:status nil
:on (toggle-truncate-lines)
:documentation "Truncate the long lines (no wrap)."
:key "tl")
:evil-leader "tl")
(spacemacs|add-toggle visual-line-navigation
:toggle-function visual-line-mode
:status visual-line-mode
:on (visual-line-mode)
:off (visual-line-mode -1)
:documentation "Move point according to visual lines."
:key "tL")
:evil-leader "tL")
(spacemacs|add-toggle maximize-frame
:toggle-function toggle-frame-maximized
:if (version< "24.3.50" emacs-version)
:status nil
:on (toggle-frame-maximized)
:documentation "Maximize the current frame."
:key "tM")
:evil-leader "tM")
(spacemacs|add-toggle line-numbers
:toggle-function global-linum-mode
:status linum-mode
:on (global-linum-mode)
:off (global-linum-mode -1)
:documentation "Show the line numbers."
:key "tn")
:evil-leader "tn")
(spacemacs|add-toggle transparent-frame
:toggle-function toggle-transparency
:status nil
:on (toggle-transparency)
:documentation "Make the current frame non-opaque."
:key "tt")
:evil-leader "tt")
(spacemacs|add-toggle tool-bar
:toggle-function spacemacs/toggle-tool-bar
:if window-system
:status tool-bar-mode
:on (tool-bar-mode)
:off (tool-bar-mode -1)
:documentation "Display the tool bar in GUI mode."
:key "tT")
:evil-leader "tT")
(spacemacs|add-toggle menu-bar
:toggle-function spacemacs/toggle-menu-bar
:if (or window-system (version<= "24.3.1" emacs-version))
:status menu-bar-mode
:on (menu-bar-mode)
:off (menu-bar-mode -1)
:documentation "Display the menu bar."
:key "tU")
:evil-leader "tU")
(spacemacs|add-toggle whitespaces
:toggle-function whitespace-mode
:status whitespace-mode
:on (whitespace-mode)
:off (whitespace-mode -1)
:documentation "Display the whitespaces."
:key "t SPC")
:evil-leader "t SPC")
;; quit -----------------------------------------------------------------------
(evil-leader/set-key
"q s" 'spacemacs/save-buffers-kill-emacs

View File

@ -1045,14 +1045,14 @@ which require an initialization must be listed explicitly in the list.")
(use-package golden-ratio
:defer t
:init
(spacemacs|add-toggle
golden-ratio
:toggle-function golden-ratio-mode
:on (balance-windows)
:off (golden-ratio)
:documentation
"Dynamically resize the focused window using the golden ratio."
:key "tg")
(eval `(spacemacs|add-toggle
golden-ratio
:status golden-ratio-mode
:on (golden-ratio-mode) (golden-ratio)
:off (golden-ratio-mode -1) (balance-windows)
:documentation ,(concat "Dynamically resize the focused window "
"using the golden ratio.")
:evil-leader "tg"))
:config
(progn
(setq golden-ratio-extra-commands