boon/boon-keys.el

140 lines
5.3 KiB
EmacsLisp
Raw Normal View History

;;; boon-keys.el --- An Ergonomic Command Mode -*- lexical-binding: t -*-
2014-10-19 13:59:12 +02:00
;;; Commentary:
2014-10-19 23:01:11 +02:00
;; This module defines various keymaps and portions of keymaps, common
;; to all keyboard layouts.
2014-10-19 13:59:12 +02:00
;;; Code:
(require 'boon-core)
(require 'boon-main)
2016-02-02 21:55:22 +01:00
(defvar boon-goto-map (make-sparse-keymap))
2016-08-29 20:10:07 +02:00
(define-key boon-select-map "@" 'boon-select-occurences)
(define-key boon-select-map "#" 'boon-select-all)
2015-11-30 23:22:50 +01:00
(define-key boon-select-map " " 'boon-select-line)
2016-08-29 20:10:07 +02:00
(define-key boon-moves-map "'" 'boon-switch-mark)
2015-11-30 23:22:50 +01:00
2016-08-29 20:10:07 +02:00
(define-key boon-command-map "'" 'boon-toggle-mark)
2014-10-19 22:29:29 +02:00
(define-key boon-command-map [(return)] 'undefined)
2014-10-29 20:31:03 +01:00
(define-key boon-command-map (kbd "<RET>") 'undefined)
2014-10-19 22:29:29 +02:00
(define-key boon-command-map [(backspace)] 'undefined)
(define-key boon-command-map "`" 'boon-toggle-case)
2014-10-19 23:01:11 +02:00
2015-12-11 20:38:53 +01:00
(define-key boon-command-map "!" 'shell-command)
(define-key boon-command-map "|" 'shell-command-on-region)
2014-10-19 22:29:29 +02:00
(define-key boon-command-map "_" 'redo)
(define-key boon-command-map "-" 'undo)
2016-08-14 22:21:07 +02:00
(dolist (number '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
2016-08-27 20:30:47 +02:00
(define-key boon-command-map number 'digit-argument))
2016-08-14 22:21:07 +02:00
2014-10-19 22:29:29 +02:00
(define-key boon-command-map " " 'boon-drop-mark)
(define-key boon-command-map [(escape)] 'boon-quit)
2014-10-19 13:59:12 +02:00
2016-08-30 20:43:19 +02:00
;; Special mode rebinds
(define-key boon-special-map "`" 'boon-quote-character)
(define-key boon-special-map "'" 'boon-quote-character)
(define-key boon-special-map "x" boon-x-map)
2016-08-30 20:43:19 +02:00
;; insert mode rebinds
(define-key boon-insert-map [(escape)] 'boon-set-command-state)
2014-10-19 13:59:12 +02:00
2014-10-19 22:29:29 +02:00
;; Off mode rebinds
(define-key boon-off-map [(escape)] 'boon-set-command-state)
;; Insert mode rebinds
(define-key boon-insert-map [remap newline] 'boon-newline-dwim)
2014-10-19 13:59:12 +02:00
(define-key boon-insert-map [(escape)] 'boon-set-command-state)
2014-10-19 22:29:29 +02:00
;; Global rebinds
2014-10-19 13:59:12 +02:00
(define-key global-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key isearch-mode-map [escape] 'isearch-abort)
(defun boon-completer-space ()
(interactive)
(if (= (minibuffer-prompt-end) (point))
;(string= "" (ivy--input))
(progn (next-history-element 1)
;; alt: ivy-next-history-element
(move-end-of-line 1))
(self-insert-command 1)))
2014-10-19 13:59:12 +02:00
;; Helm keys
(defun boon-helm-browse (action)
"Run the ACTION and set 'boon-helm-command-map' as transient keymap."
2016-08-28 19:41:41 +02:00
(lambda ()
(interactive)
(call-interactively action)
(setq cursor-type 'box)
(set-transient-map boon-helm-command-map t (lambda () (setq cursor-type 'bar)))))
(defun define-helm-key (key action)
2016-08-30 20:43:19 +02:00
"Bind C-<KEY> in helm-map, and <KEY> in boon-helm-command-map (both to ACTION)."
2016-08-28 19:41:41 +02:00
(define-key helm-map (vconcat (mapcar 'boon-god-control-swap key)) (boon-helm-browse action))
(define-key boon-helm-command-map key action))
2016-08-28 19:41:41 +02:00
(defvar boon-helm-command-map (make-sparse-keymap))
(suppress-keymap boon-helm-command-map 't)
2014-10-19 14:17:46 +02:00
2016-08-29 22:50:16 +02:00
(with-eval-after-load 'helm
(define-key helm-map [(tab)] 'helm-select-action)
(define-key helm-map (kbd "C-z") 'undefined)
(define-key helm-map [(escape)] 'helm-keyboard-quit)
(define-key helm-map (kbd "SPC") 'boon-completer-space)
(define-key helm-map (kbd "M-SPC") 'helm-toggle-visible-mark)
(define-key helm-map (kbd "C-<down>") 'helm-narrow-window)
(define-key helm-map (kbd "C-<up>") 'helm-enlarge-window)
(define-key helm-map (kbd "C-<return>") 'helm-execute-persistent-action)
(define-key helm-map [(shift backspace)] 'helm-unmark-all)
(define-helm-key (kbd "f") 'helm-follow-mode))
2014-10-19 22:50:50 +02:00
2016-08-28 19:41:41 +02:00
(define-key boon-goto-map "r" 'helm-register)
2015-11-25 22:20:12 +01:00
(define-key boon-goto-map "a" 'helm-apropos)
2014-10-28 13:05:55 +01:00
(define-key boon-goto-map "b" 'helm-buffers-list)
(define-key boon-goto-map "g" 'helm-resume)
2014-10-19 22:50:50 +02:00
(define-key boon-goto-map "i" 'helm-git-grep)
2015-11-25 22:20:12 +01:00
(define-key boon-goto-map "k" 'helm-show-kill-ring)
2014-10-28 13:05:55 +01:00
(define-key boon-goto-map "l" 'goto-line) ;; no helm version of this
2015-11-25 22:20:12 +01:00
(define-key boon-goto-map "m" 'helm-all-mark-ring)
2015-10-14 22:55:53 +02:00
(define-key boon-goto-map "o" 'helm-multi-occur)
2014-10-19 22:50:50 +02:00
(define-key boon-goto-map "t" 'helm-etags-select)
(define-key boon-goto-map "y" 'helm-flycheck)
2016-08-31 21:58:55 +02:00
(defun boon-god-control-swap (event)
"Swap the control 'bit' in EVENT, if that is a good choice."
(interactive (list (read-key)))
(cond
((memq event '(9 13)) event)
((<= event 27) (+ 96 event))
((not (eq 0 (logand (lsh 1 26) event))) (logxor (lsh 1 26) event))
(t (list 'control event))))
(defun boon-c-god ()
"Input a key sequence, prepend C- to each key, and run the command bound to that sequence."
(interactive)
(let ((keys '((control c)))
(binding (key-binding (kbd "C-c")))
(key-vector (kbd "C-c"))
(prompt "C-c-"))
(while (and binding (not (symbolp binding)))
(let ((key (read-key (format "%s" prompt))))
(if (eq key ?h) (describe-bindings key-vector)
(push (boon-god-control-swap key) keys)
(setq key-vector (vconcat (reverse keys)))
(setq prompt (key-description key-vector))
(setq binding (key-binding key-vector)))))
(setq this-command-keys key-vector)
(cond
((not binding) (error "No command bound to %s" prompt))
((commandp binding) (call-interactively binding))
(t (error "Key not bound to a command: %s" binding)))))
2014-10-19 14:17:46 +02:00
(provide 'boon-keys)
;;; boon-keys.el ends here