cp-mode: use native projectile keymaps

This commit is contained in:
Eric Danan 2018-08-15 14:22:22 +02:00
parent a091875789
commit b1484297c3
2 changed files with 54 additions and 30 deletions

View file

@ -49,6 +49,12 @@ Install the package from [MELPA](https://melpa.org), [MELPA Stable](https://stab
## Getting started
To turn on counsel-projectile mode, either call the command `counsel-projectile-mode` or use the Customize interface to toggle on the variable `counsel-projectile-mode`. This will turn on projectile mode, thus enabling all projectile key bindings, and add the counsel-projectile key bindings on top of them.
Note that starting with projectile version 1.1, the projectile (and counsel-projectile) key bindings are only available after you select a keymap prefix for them. For instance, to select <kbd>C-c p</kbd> as prefix (the default prior to version 1.1), you need to execute the following form:
```emacs-lisp
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
```
The counsel-projectile key bindings either remap existing projectile commands to their counsel-projectile replacements (e.g. <kbd>C-c p f</kbd> now calls `counsel-projectile-find-file` instead of `projectile-find-file`) or bind keys to counsel-projectile commands that have no projectile counterparts (e.g. <kbd>C-c p SPC</kbd> calls the command `counsel-projectile`).
Calling the command `counsel-projectile-mode` once again or toggling off the variable `counsel-projectile-mode` disables the counsel-projectile key bindings and turns off projectile mode.
@ -184,6 +190,14 @@ This command opens the current project's agenda. It simply calls `org-agenda` af
# Configuration
## Enabling counsel-projectile mode when emacs starts
To automatically enable counsel-projectile mode when emacs starts, you can either use the Customize interface to toggle on the variable `counsel-projectile-mode` and save your customization, or add `(counsel-projectile-mode)` to your init file.
Note that starting with projectile version 1.1, the projectile (and counsel-projectile) key bindings are only available after you select a keymap prefix for them. For instance, to select <kbd>C-c p</kbd> as prefix (the default prior to version 1.1), you need to add the following form to your init file:
```emacs-lisp
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
```
## Customizing counsel-projectile key bindings
The key bindings that are added when counsel-projectile-mode is turned on are determined by the variable `counsel-projectile-key-bindings`. You can set this variable, either directly or through the customize interface, to customize these key bindings. It holds an alist of `(KEY . DEF)` pairs, where KEY is either a key sequence to bind in `projectile-command-map` or a projectile command to remap in `projectile-mode-map`, and DEF is the counsel-projectile command to which KEY is remapped or bound.
## Customizing action lists
The lists of available actions (including the default action) for most of the commands above are stored in custom variables. If you set one of these variables, either directly or through the through the Customize interface, the new value will be picked up the next time you invoke the corresponding commmand.

View file

@ -1361,28 +1361,27 @@ If not inside a project, call `counsel-projectile-switch-project'."
;;;; counsel-projectile-mode
(defvar counsel-projectile-command-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map projectile-command-map)
(define-key map (kbd "s r") 'counsel-projectile-rg)
(define-key map (kbd "O c") 'counsel-projectile-org-capture)
(define-key map (kbd "O a") 'counsel-projectile-org-agenda)
(define-key map (kbd "SPC") 'counsel-projectile)
map)
"Keymap for Counesl-Projectile commands after `projectile-keymap-prefix'.")
(fset 'counsel-projectile-command-map counsel-projectile-command-map)
(defcustom counsel-projectile-key-bindings
'((projectile-find-file . counsel-projectile-find-file)
(projectile-find-dir . counsel-projectile-find-dir)
(projectile-switch-to-buffer . counsel-projectile-switch-to-buffer)
(projectile-grep . counsel-projectile-grep)
(projectile-ag . counsel-projectile-ag)
(projectile-switch-project . counsel-projectile-switch-project)
(" " . counsel-projectile)
("sr" . counsel-projectile-rg)
("Oc" . counsel-projectile-org-capture)
("Oa" . counsel-projectile-org-agenda))
"Alist of counsel-projectile key bindings.
(defvar counsel-projectile-mode-map
(let ((map (make-sparse-keymap)))
(define-key map projectile-keymap-prefix 'counsel-projectile-command-map)
(define-key map [remap projectile-find-file] 'counsel-projectile-find-file)
(define-key map [remap projectile-find-dir] 'counsel-projectile-find-dir)
(define-key map [remap projectile-switch-to-buffer] 'counsel-projectile-switch-to-buffer)
(define-key map [remap projectile-grep] 'counsel-projectile-grep)
(define-key map [remap projectile-ag] 'counsel-projectile-ag)
(define-key map [remap projectile-switch-project] 'counsel-projectile-switch-project)
map)
"Keymap for Counsel-Projectile mode.")
Each element is of the form \(KEY . DEF\) where KEY is either a
key sequence to bind in `projectile-command-map' or a projectile
command to remap in `projectile-mode-map', and DEF is the
counsel-projectile command to which KEY is remapped or bound."
:type '(alist :key-type (choice (function :tag "Projectile command")
key-sequence)
:value-type (function :tag "Counsel-projectile command"))
:group 'counsel-projectile)
;;;###autoload
(define-minor-mode counsel-projectile-mode
@ -1392,19 +1391,30 @@ With a prefix argument ARG, enable the mode if ARG is positive,
and disable it otherwise. If called from Lisp, enable the mode
if ARG is omitted or nil, and toggle it if ARG is `toggle'.
Counsel-Projectile mode triggers Projectile mode, remaps
Projectile commands that have counsel replacements, and adds key
bindings for Counsel-Projectile commands that have no Projectile
counterpart.
Counsel-Projectile mode turns on Projectile mode, thus enabling
all projectile key bindings, and adds the counsel-projectile key
bindings on top of them.
\\{counsel-projectile-mode-map}"
The counsel-projectile key bindings either remap existing
projectile commands to their counsel-projectile replacements or
bind keys to counsel-projectile commands that have no projectile
counterparts."
:group 'counsel-projectile
:require 'counsel-projectile
:keymap counsel-projectile-mode-map
:global t
(if counsel-projectile-mode
(cond
(counsel-projectile-mode
(projectile-mode)
(projectile-mode -1)))
(dolist (binding counsel-projectile-key-bindings)
(if (functionp (car binding))
(define-key projectile-mode-map `[remap ,(car binding)] (cdr binding))
(define-key projectile-command-map (car binding) (cdr binding)))))
(t
(dolist (binding counsel-projectile-key-bindings)
(if (functionp (car binding))
(define-key projectile-mode-map `[remap ,(car binding)] nil)
(define-key projectile-command-map (car binding) nil)))
(projectile-mode -1))))
;;;; provide