Spacemacs toggle framework

Use helm-spacemacs to get a list of all available toggles and
activate them.
This commit is contained in:
syl20bnr 2015-01-29 00:08:48 -05:00
parent ca5861c03d
commit 8292d39046
5 changed files with 141 additions and 23 deletions

View File

@ -113,6 +113,7 @@
(defun spacemacs/initialize ()
"Create the special buffer for `spacemacs-mode' and perform startup
initialization."
(require 'core-toggles)
(switch-to-buffer (get-buffer-create "*spacemacs*"))
(spacemacs-mode))

63
core/core-toggles.el Normal file
View File

@ -0,0 +1,63 @@
;;; core-toggles.el --- Spacemacs Core File
;;
;; 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:
`:function FUNCTION'
A symbol of a function to handle the toggle or one of the following
symbols: `global' or `globalized'.
`global' define a default function to handle the toggle of a minor mode
defined with the property `:global t'
`globalized' define a default function to handle the toogle of a minor
mode defined with `define-globalized-minor-mode'.
`:documentation STRING'
A docstring to describe what the toggle does.
`:key STRING'
A key sequence to use the toggle."
(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))
(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)
spacemacs-toggles)
`(progn
(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)))))
(provide 'core-toggles)

View File

@ -60,7 +60,8 @@
(helm-spacemacs-mode)
(helm :buffer "*helm: spacemacs*"
:sources `(,(helm-spacemacs//layer-source)
,(helm-spacemacs//package-source))))
,(helm-spacemacs//package-source)
,(helm-spacemacs//toggle-source))))
(defun helm-spacemacs//layer-source ()
"Construct the helm source for the layer section."
@ -84,6 +85,19 @@
helm-spacemacs-all-packages)
(sort result 'string<)))
(defun helm-spacemacs//toggle-source ()
"Construct the helm source for the toggles."
`((name . "Toggles")
(candidates . ,(helm-spacemacs//toggle-candidates))
(action . (("Toggle" . helm-spacemacs//toggle)))))
(defun helm-spacemacs//toggle-candidates ()
"Return the sorted candidates for toggle source."
(let (result)
(dolist (toggle spacemacs-toggles)
(push (symbol-name (car toggle)) result))
(sort result 'string<)))
(defun helm-spacemacs//layer-action-open-file (file candidate)
"Open FILE of the passed CANDIDATE."
(let ((path (file-name-as-directory
@ -119,6 +133,12 @@
(re-search-forward (format "init-%s" package))
(beginning-of-line))))
(defun helm-spacemacs//toggle (candidate)
"Toggle candidate."
(let ((toggle (assq (intern candidate) spacemacs-toggles)))
(when toggle
(funcall (plist-get (cdr toggle) :func)))))
(provide 'helm-spacemacs)
;;; helm-spacemacs.el ends here

View File

@ -130,19 +130,54 @@
"Sc" 'cofi/helm-flyspell-correct
"Sn" 'flyspell-goto-next-error)
;; toggle ---------------------------------------------------------------------
(evil-leader/set-key
"t8" 'toggle-fill-column-indicator
"tF" 'spacemacs/toggle-frame-fullscreen
"tf" 'fringe-mode
"th" 'global-hl-line-mode
"tl" 'toggle-truncate-lines
"tL" 'visual-line-mode
"tM" 'toggle-frame-maximized
"tn" 'global-linum-mode
"tt" 'toggle-transparency
"tT" 'spacemacs/toggle-tool-bar
"tU" 'spacemacs/toggle-menu-bar
"t SPC" 'whitespace-mode)
(spacemacs|add-toggle fill-column-indicator
:toggle-function toggle-fill-column-indicator
:documentation "Display the fill column indicator."
:key "t8")
(spacemacs|add-toggle fringe
:toggle-function fringe-mode
:documentation "Display the fringe in GUI mode."
:key "tf")
(spacemacs|add-toggle fullscreen-frame
:toggle-function 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")
(spacemacs|add-toggle truncate-lines
:toggle-function toggle-truncate-lines
:documentation "Truncate the long lines (no wrap)."
:key "tl")
(spacemacs|add-toggle visual-line-navigation
:toggle-function visual-line-mode
:documentation "Move point according to visual lines."
:key "tL")
(spacemacs|add-toggle maximize-frame
:toggle-function toggle-frame-maximized
:documentation "Maximize the current frame."
:key "tM")
(spacemacs|add-toggle line-numbers
:toggle-function global-linum-mode
:documentation "Show the line numbers."
:key "tn")
(spacemacs|add-toggle transparent-frame
:toggle-function toggle-transparency
:documentation "Make the current frame non-opaque."
:key "tt")
(spacemacs|add-toggle tool-bar
:toggle-function spacemacs/toggle-tool-bar
:documentation "Display the tool bar in GUI mode."
:key "tT")
(spacemacs|add-toggle menu-bar
:toggle-function spacemacs/toggle-menu-bar
:documentation "Display the menu bar."
:key "tU")
(spacemacs|add-toggle whitespaces
:toggle-function whitespace-mode
:documentation "Display the whitespaces."
:key "t SPC")
;; quit -----------------------------------------------------------------------
(evil-leader/set-key
"q s" 'spacemacs/save-buffers-kill-emacs

View File

@ -1045,15 +1045,14 @@ which require an initialization must be listed explicitly in the list.")
(use-package golden-ratio
:defer t
:init
(progn
(defun spacemacs/toggle-golden-ratio ()
"Toggle golden-ratio mode on and off."
(interactive)
(if (symbol-value golden-ratio-mode)
(progn (golden-ratio-mode -1)(balance-windows))
(golden-ratio-mode)
(golden-ratio)))
(evil-leader/set-key "tg" 'spacemacs/toggle-golden-ratio))
(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")
:config
(progn
(setq golden-ratio-extra-commands