From 6ccde2026b7d095d31587c435ddc6952991cebe8 Mon Sep 17 00:00:00 2001 From: deb0ch Date: Fri, 23 Dec 2016 22:55:12 +0100 Subject: [PATCH] replace variable winum-keymap-prefix with function winum-set-keymap-prefix Fixes #1. Setting the key bindings prefix using a variable just couldn't work given how Emacs handles key bindings for minor modes and that the available keymap API doesn't handle keymaps. --- README.org | 47 +++++++++++++++++++++---------------------- winum.el | 58 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/README.org b/README.org index 4de5184..43ce395 100644 --- a/README.org +++ b/README.org @@ -95,16 +95,23 @@ Emacs configuration: * Configuration ** Keybindings -By default, all key bindings are prefixed with a value stored in -=winum-keymap-prefix=, by default ~C-x w~. See [[#customize-options][Customize options]]. +The default prefix for key bindings is ~C-x w~ for compatibility with native +Emacs bindings. -The default prefix is ~C-x w~ for compatibility with native Emacs bindings. -However, it gives long key bindings and can be incomfortable to use. +If you don't like ~C-x w~, you can set a prefix of your choosing using the +function =winum-set-keymap-prefix=: -For instance, you could replace the native prefix with ~C-c~ or ~M-m~. +#+BEGIN_SRC elisp + (winum-set-keymap-prefix (kbd "C-c")) +#+END_SRC -If you prefer no to use a prefix to have even shorter bindings, you can also -directly override =winum-keymap=: +This function overrides the value of =winum-keymap=, so you should call it +before customization of =winum-keymap= and/or after customization of +=winum-base-map=. Its argument must be a key sequence, like the ones returned by +=kbd=. + +If you prefer no to use a prefix and have even shorter bindings, you can also +override =winum-keymap= in the minor mode bindings table: #+BEGIN_SRC emacs-lisp (require 'winum) @@ -127,13 +134,16 @@ directly override =winum-keymap=: (winum-mode) #+END_SRC -This is a little bit hacky but is less code than using =define-key=, and will -also shadow the old bindings. +You can also use the more conventional =define-key= on =winum-keymap=: -*NB:* Both ~`~ and ~²~ are mapped to =winum-select-window-by-number= to handle - both =qwerty= and =azerty= keyboard layouts. - If you are using a different kind of layout, the recommended place to map - it is the key beside ~1~. +#+BEGIN_SRC emacs-lisp + (define-key winum-keymap (kbd "C-x y o l o") 'winum-select-window-by-number) +#+END_SRC + +*NB:* Both ~`~ and ~²~ are mapped to =winum-select-window-by-number= by default + to handle both =qwerty= and =azerty= keyboard layouts. If you are using a + different kind of layout, the recommended place to map it is the key + beside ~1~. ** Customize options Several options are available through Emacs' Customize interface under @@ -205,12 +215,6 @@ Several options are available through Emacs' Customize interface under Default: '(" *which-key*") -- =winum-keymap-prefix= - - Prefix key sequence for keybindings. - - Default: =(kbd "C-x w")= - - face: =winum-face= Face used for the number in the mode-line. @@ -220,8 +224,6 @@ Here is an example that you could put in your =.emacs=, which includes all available winum options. #+BEGIN_SRC emacs-lisp - (add-to-list 'load-path "/path/to/install/folder/winum.el/") - (require 'winum) (setcdr (assoc 'winum-mode minor-mode-map-alist) @@ -256,8 +258,7 @@ available winum options. winum-assign-func 'my-winum-assign-func winum-auto-setup-mode-line t winum-mode-line-position 1 - winum-ignored-buffers '(" *which-key*") - winum-keymap-prefix nil) + winum-ignored-buffers '(" *which-key*")) (winum-mode) #+END_SRC diff --git a/winum.el b/winum.el index 28b4acc..4929251 100644 --- a/winum.el +++ b/winum.el @@ -111,33 +111,30 @@ numbers in the mode-line.") :group 'winum :type '(repeat string)) -(defcustom winum-keymap-prefix (kbd "C-x w") - "Prefix key sequence for keybindings." - :group 'winum - :type 'string) - (defface winum-face '() "Face used for the number in the mode-line." :group 'winum) -(defvar winum-keymap - (when winum-keymap-prefix - (let ((map (make-sparse-keymap))) - (let ((prefix-map (make-sparse-keymap))) - (define-key prefix-map (kbd "`") 'winum-select-window-by-number) - (define-key prefix-map (kbd "²") 'winum-select-window-by-number) - (define-key prefix-map (kbd "0") 'winum-select-window-0-or-10) - (define-key prefix-map (kbd "1") 'winum-select-window-1) - (define-key prefix-map (kbd "2") 'winum-select-window-2) - (define-key prefix-map (kbd "3") 'winum-select-window-3) - (define-key prefix-map (kbd "4") 'winum-select-window-4) - (define-key prefix-map (kbd "5") 'winum-select-window-5) - (define-key prefix-map (kbd "6") 'winum-select-window-6) - (define-key prefix-map (kbd "7") 'winum-select-window-7) - (define-key prefix-map (kbd "8") 'winum-select-window-8) - (define-key prefix-map (kbd "9") 'winum-select-window-9) - (define-key map winum-keymap-prefix prefix-map)) - map)) +(defvar winum-base-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "`") 'winum-select-window-by-number) + (define-key map (kbd "²") 'winum-select-window-by-number) + (define-key map (kbd "0") 'winum-select-window-0-or-10) + (define-key map (kbd "1") 'winum-select-window-1) + (define-key map (kbd "2") 'winum-select-window-2) + (define-key map (kbd "3") 'winum-select-window-3) + (define-key map (kbd "4") 'winum-select-window-4) + (define-key map (kbd "5") 'winum-select-window-5) + (define-key map (kbd "6") 'winum-select-window-6) + (define-key map (kbd "7") 'winum-select-window-7) + (define-key map (kbd "8") 'winum-select-window-8) + (define-key map (kbd "9") 'winum-select-window-9) + map) + "Keymap to be used under the prefix provided by `winum-keymap-prefix'.") + +(defvar winum-keymap (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-x w") winum-base-map) + map) "Keymap used for `winum-mode'.") ;; Internal variables ---------------------------------------------------------- @@ -281,7 +278,7 @@ There are several ways to provide the number: (interactive "P") (let* ((n (cond ((integerp arg) arg) - ((eq arg '-) 0) ; negative-argument + ((eq arg '-) 0) ; the negative argument (arg (winum-get-number)) ((called-interactively-p 'any) (let ((user-input-str (read-from-minibuffer "Window: "))) @@ -301,6 +298,19 @@ There are several ways to provide the number: ;; Public API ------------------------------------------------------------------ +;;;###autoload +(defun winum-set-keymap-prefix (prefix) + "Set key bindings prefix for `winum-keymap' based on `winum-base-map'. +This function overrides the value of `winum-keymap', so you +should call it before customization of `winum-keymap' and/or +after customization of `winum-base-map'. +PREFIX must be a key sequence, like the ones returned by `kbd'." + (setq winum-keymap (when prefix (let ((map (make-sparse-keymap))) + (define-key map prefix winum-base-map) + map))) + (setcdr (assoc 'winum-mode minor-mode-map-alist) + winum-keymap)) + ;;;###autoload (defun winum-get-window-by-number (n) "Return window numbered N if exists, nil otherwise."