boon/boon-core.el

205 lines
6.7 KiB
EmacsLisp
Raw Normal View History

2014-10-19 13:59:12 +02:00
;;; boon --- An Ergonomic Command Mode -*- lexical-binding: t -*-
;;; Commentary:
2014-10-23 13:05:17 +02:00
;; This module sets up the emulation keymaps for each boon state.
;; Functions to switch to each state is also provided.
2014-10-19 13:59:12 +02:00
;;; Code:
(require 'face-remap)
2014-10-20 14:06:34 +02:00
(require 'cl-macs)
2014-10-19 13:59:12 +02:00
;; Maps
(defvar boon-c-map (make-sparse-keymap))
2014-10-19 14:17:46 +02:00
(defvar boon-x-map (make-sparse-keymap))
(set-keymap-parent boon-x-map ctl-x-map)
2014-10-19 13:59:12 +02:00
(defvar boon-helm-command-map (make-sparse-keymap))
2014-10-19 22:29:29 +02:00
(suppress-keymap boon-helm-command-map 't)
2014-10-19 13:59:12 +02:00
(defvar boon-moves-map (make-sparse-keymap) "Keymap for moves.")
(defvar boon-command-map (make-sparse-keymap) "Keymap used in Boon command mode.")
(set-keymap-parent boon-command-map boon-moves-map)
2014-10-19 22:29:29 +02:00
(suppress-keymap boon-command-map 't) ; so that typing is disabled altogether in command mode
2014-10-19 13:59:12 +02:00
(defvar boon-select-map (make-sparse-keymap) "Keymap for non-moves text regions.")
(set-keymap-parent boon-select-map boon-moves-map)
(defvar boon-off-map (make-sparse-keymap))
(defvar boon-insert-map (make-sparse-keymap))
(defvar boon-mode-map-alist (list (cons 'boon-command-state boon-command-map)
(cons 'boon-off-state boon-off-map)
(cons 'boon-insert-state boon-insert-map)
(cons 'boon-helm-command-state boon-helm-command-map)))
(push 'boon-mode-map-alist emulation-mode-map-alists)
;; States
(defvar-local boon-command-state nil)
(defvar-local boon-insert-state nil)
(defvar-local boon-off-state nil)
(defvar-local boon-helm-command-state nil
"non-nil if the helm command mode is active. Makes sense only
in a helm minibuffer.")
2014-10-19 23:01:11 +02:00
(defvar-local boon-modeline-face-cookie nil)
2014-10-19 13:59:12 +02:00
(defun boon-set-state (state)
2014-10-19 23:01:11 +02:00
"Set the boon state (as STATE) for this buffer."
2014-10-19 13:59:12 +02:00
(setq boon-command-state nil)
(setq boon-insert-state nil)
(setq boon-off-state nil)
(set state t)
(unless boon-command-state
(deactivate-mark)
(save-excursion
(when (not (bolp))
(let ((orig (point)))
(skip-chars-forward " " (line-end-position))
(when (eolp) (delete-and-extract-region orig (point))))))
(setq boon-modeline-face-cookie
(face-remap-add-relative
'mode-line '((:foreground "darkred") mode-line)))
2014-10-19 13:59:12 +02:00
(setq cursor-type 'bar))
(cond (boon-command-state
;; (do-auto-save)
(when (< 2 (abs (-
(line-number-at-pos (point))
(line-number-at-pos (mark)))))
(push-mark)) ; remember where the last edition was by pushing a mark
(setq cursor-type 'box)
(when (bound-and-true-p boon-modeline-face-cookie)
(face-remap-remove-relative boon-modeline-face-cookie)))
2014-10-19 13:59:12 +02:00
(boon-off-state)
(boon-insert-state)
(t (message "Unknown state!")))
(force-mode-line-update))
(defun boon-set-insert-like-state ()
2014-10-20 14:06:34 +02:00
"Switch to off or insert state, depending on mode."
2014-10-19 13:59:12 +02:00
(interactive)
2014-10-20 14:06:34 +02:00
(if (boon-special-mode-p) (boon-set-off-state) (boon-set-state 'boon-insert-state)))
2014-10-19 13:59:12 +02:00
2014-10-20 22:44:11 +02:00
(defun boon-set-insert-state ()
"Switch to insert state."
(interactive)
(boon-set-state 'boon-insert-state))
2014-10-19 13:59:12 +02:00
(defun boon-set-command-state ()
2014-10-20 13:39:34 +02:00
"Switch to command state and push a mark to remember the last edition point."
2014-10-19 13:59:12 +02:00
(interactive) (boon-set-state 'boon-command-state))
(defun boon-set-off-state ()
2014-10-20 14:06:34 +02:00
"Switch to off state."
2014-10-19 13:59:12 +02:00
(interactive) (boon-set-state 'boon-off-state))
(defun boon-helm-set-insert-state ()
2014-10-20 14:06:34 +02:00
"Switch to insert state in an helm minibuffer."
2014-10-19 13:59:12 +02:00
(interactive)
(setq boon-helm-command-state nil)
(setq cursor-type 'bar))
(defun boon-helm-set-command-state ()
2014-10-20 14:06:34 +02:00
"Switch to command state in an helm minibuffer."
2014-10-19 13:59:12 +02:00
(interactive)
(setq boon-helm-command-state t)
(setq cursor-type 'box))
2014-10-20 14:06:34 +02:00
(defvar boon-special-mode-list
'(
2014-10-23 13:05:17 +02:00
Buffer-menu-mode
2014-10-20 14:06:34 +02:00
Custom-mode
completion-list-mode
debugger-mode
dired-mode
2014-10-20 14:06:34 +02:00
ediff-mode
git-rebase-mode
magit-branch-manager-mode
magit-key-mode
2014-10-20 14:06:34 +02:00
magit-log-mode
magit-status-mode
package-menu-mode
)
2014-10-23 13:05:17 +02:00
"List of modes which start in boon-off-state, and go back to off state instead of inserting."
2014-10-20 14:06:34 +02:00
)
(defun boon-special-mode-p ()
"Is the major mode in boon-special-mode-list?"
(memq major-mode boon-special-mode-list))
2014-10-19 13:59:12 +02:00
;;; Initialisation and activation
(define-minor-mode boon-local-mode
"Minor mode for setting up command mode in a single buffer."
:init-value nil
:lighter (:eval (boon-modeline-string))
:keymap nil
(cond
(boon-local-mode
;; restore the proper value of `major-mode' in Fundamental buffers
(when (eq major-mode 'turn-on-boon-mode)
(setq major-mode 'fundamental-mode))
;; The initial state is usually setup by `boon-initialize' when
;; the major-mode in a buffer changes. This preliminary
;; initialization is only for the case when `boon-local-mode' is
;; called directly for the first time in a buffer.
(cond
2014-10-20 14:06:34 +02:00
((boon-special-mode-p)
2014-10-19 13:59:12 +02:00
(boon-set-off-state))
((memq major-mode '(magit-commit-mode
git-commit-mode
))
2014-10-20 14:06:34 +02:00
(boon-set-insert-like-state))
2014-10-19 13:59:12 +02:00
(t (boon-set-command-state))))
(t
(boon-set-off-state)
(message "Boon disabled")
)))
(add-hook 'minibuffer-setup-hook 'boon-minibuf-hook)
(defun boon-minibuf-hook ()
2014-10-20 10:28:56 +02:00
"Detect if the minibuffer is a helm minibuffer, and activate boon helm command mode if so."
2014-10-20 14:06:34 +02:00
(cl-flet ((eq-if-bound (sym val) (and (boundp sym) (eq (eval sym) val))))
(cond
((eq-if-bound 'helm-map (current-local-map))
(boon-helm-set-command-state))
((eq-if-bound 'helm-git-grep-map (current-local-map))
(boon-helm-set-command-state))
(t (setq cursor-type 'bar)))))
2014-10-19 13:59:12 +02:00
2014-10-20 14:06:34 +02:00
;; The function `boon-initialize' should only be used to initialize
;; `boon-local-mode' from the globalized minor-mode `boon-mode'. It is
;; called whenever boon is enabled in a buffer for the first time or
;; when boon is active and the major-mode of the buffer changes.
2014-10-19 13:59:12 +02:00
(defun boon-initialize ()
2014-10-20 10:28:56 +02:00
"Enable Boon in the current buffer, if appropriate. To enable Boon globally, do (boon-mode 1)."
(unless (minibufferp)
2014-10-19 13:59:12 +02:00
(boon-local-mode 1)))
2014-10-20 14:06:34 +02:00
;;;###autoload (autoload 'boon-mode "boon" "Toggle boon in all buffers" t)
2014-10-19 13:59:12 +02:00
(define-globalized-minor-mode boon-mode
boon-local-mode boon-initialize)
2014-10-20 14:06:34 +02:00
;;;###autoload
(defun turn-on-boon-mode ()
2014-10-19 13:59:12 +02:00
"Turn on Boon in the current buffer."
(interactive)
2014-10-20 14:06:34 +02:00
(boon-local-mode 1))
2014-10-19 13:59:12 +02:00
2014-10-20 14:06:34 +02:00
;;;###autoload
(defun turn-off-boon-mode ()
2014-10-19 13:59:12 +02:00
"Turn off Boon in the current buffer."
(interactive)
2014-10-20 14:06:34 +02:00
(boon-local-mode -1))
2014-10-19 13:59:12 +02:00
(defun boon-modeline-string ()
"Return a string describing the current state."
(concat " Boon:" (cond
(boon-command-state "CMD")
(boon-insert-state "INS")
(boon-off-state "OFF")
(t "???"))))
(provide 'boon-core)
;;; boon-core ends here