helm/ivy: Use hook to update hjkl navigation

Introduce hjkl-completion-navigation-functions to hold the functions to
disable and enable hjkl navigation for ivy and/or helm. The hook is run
with args to indicate whether to enable or disable.
This commit is contained in:
justbur 2016-02-06 13:54:21 -05:00 committed by syl20bnr
parent cf90c8ee07
commit f6b82ee90d
4 changed files with 40 additions and 28 deletions

View File

@ -386,7 +386,7 @@ Removes the automatic guessing of the initial value based on thing at point. "
(add-hook 'helm-mode-hook 'simpler-helm-bookmark-keybindings)
;; helm navigation on hjkl
(defun spacemacs//hjkl-completion-navigation (&optional arg)
(defun spacemacs//helm-hjkl-navigation (&optional arg)
"Set navigation in helm on `jklh'.
ARG non nil means Vim like movements."
(cond
@ -398,18 +398,21 @@ ARG non nil means Vim like movements."
(define-key helm-map (kbd "C-h") 'helm-next-source)
(define-key helm-map (kbd "C-S-h") 'describe-key)
(define-key helm-map (kbd "C-l") (kbd "RET"))
(dolist (keymap (list helm-find-files-map helm-read-file-map))
(define-key keymap (kbd "C-l") 'helm-execute-persistent-action)
(define-key keymap (kbd "C-h") 'helm-find-files-up-one-level)
(define-key keymap (kbd "C-S-h") 'describe-key)))
(with-eval-after-load 'helm-files
(dolist (keymap (list helm-find-files-map helm-read-file-map))
(define-key keymap (kbd "C-l") 'helm-execute-persistent-action)
(define-key keymap (kbd "C-h") 'helm-find-files-up-one-level)
(define-key keymap (kbd "C-S-h") 'describe-key))))
(t
(define-key helm-map (kbd "C-j") 'helm-execute-persistent-action)
(define-key helm-map (kbd "C-k") 'helm-delete-minibuffer-contents)
(define-key helm-map (kbd "C-h") nil)
(define-key helm-map
(kbd "C-l") 'helm-recenter-top-bottom-other-window))))
(spacemacs//hjkl-completion-navigation
(member dotspacemacs-editing-style '(vim hybrid)))
(add-hook 'spacemacs--hjkl-completion-navigation-functions
'spacemacs//helm-hjkl-navigation)
(run-hook-with-args 'spacemacs--hjkl-completion-navigation-functions
(member dotspacemacs-editing-style '(vim hybrid)))
(defun spacemacs/helm-edit ()
"Switch in edit mode depending on the current helm buffer."

View File

@ -335,26 +335,28 @@ Helm hack."
(require (car repl))
(call-interactively (cdr repl))))))
(defun spacemacs//hjkl-completion-navigation (&optional arg)
"Set navigation on `jklh'. ARG non nil means Vim like movements."
(cond
(arg
;; better navigation on homerow
(define-key ivy-minibuffer-map (kbd "C-j") 'ivy-next-line)
(define-key ivy-minibuffer-map (kbd "C-k") 'ivy-previous-line)
(define-key ivy-minibuffer-map (kbd "C-h") (kbd "DEL"))
;; Move C-h to C-S-h
(define-key ivy-minibuffer-map (kbd "C-S-h") help-map)
(define-key ivy-minibuffer-map (kbd "C-l") 'ivy-alt-done)
(define-key ivy-minibuffer-map (kbd "<escape>")
'minibuffer-keyboard-quit))
(t
(defun spacemacs//ivy-hjkl-navigation (&optional arg)
"Set navigation on \"hjkl\" for ivy. ARG non nil means
vim like movements."
(if arg
(progn
;; better navigation on homerow
(define-key ivy-minibuffer-map (kbd "C-j") 'ivy-next-line)
(define-key ivy-minibuffer-map (kbd "C-k") 'ivy-previous-line)
(define-key ivy-minibuffer-map (kbd "C-h") (kbd "DEL"))
;; Move C-h to C-S-h
(define-key ivy-minibuffer-map (kbd "C-S-h") help-map)
(define-key ivy-minibuffer-map (kbd "C-l") 'ivy-alt-done)
(define-key ivy-minibuffer-map (kbd "<escape>")
'minibuffer-keyboard-quit))
(define-key ivy-minibuffer-map (kbd "C-j") 'ivy-alt-done)
(define-key ivy-minibuffer-map (kbd "C-k") 'ivy-kill-line)
(define-key ivy-minibuffer-map (kbd "C-h") nil)
(define-key ivy-minibuffer-map (kbd "C-l") nil))))
(spacemacs//hjkl-completion-navigation
(member dotspacemacs-editing-style '(vim hybrid)))
(define-key ivy-minibuffer-map (kbd "C-l") nil)))
(add-hook 'spacemacs--hjkl-completion-navigation-functions
'spacemacs//ivy-hjkl-navigation)
(run-hook-with-args 'spacemacs--hjkl-completion-navigation-functions
(member dotspacemacs-editing-style '(vim hybrid)))
(defun spacemacs/counsel-up-directory-no-error ()
"`counsel-up-directory' ignoring errors."

View File

@ -367,6 +367,13 @@
(evil-define-key 'insert comint-mode-map [up] 'comint-previous-input)
(evil-define-key 'insert comint-mode-map [down] 'comint-next-input))
;; ivy/helm keys --------------------------------------------------------------
(defvar spacemacs--hjkl-completion-navigation-functions nil
"Hook to adjust hjkl keys for completion (helm/ivy) navigation.
Each function in the hook is run with a single argument, which
when true should disable the hjkl keys.")
;; ---------------------------------------------------------------------------
;; Transient-states
;; ---------------------------------------------------------------------------

View File

@ -51,10 +51,10 @@ before it was disabled.")
(when (boundp (car mode)) (funcall (car mode) -1)))
(setq cursor-type 'box)
(set-cursor-color "SkyBlue2")
(when (fboundp 'spacemacs//hjkl-completion-navigation)
(spacemacs//hjkl-completion-navigation nil)))
(when (fboundp 'spacemacs//hjkl-completion-navigation)
(spacemacs//hjkl-completion-navigation t))
(run-hook-with-args 'spacemacs--hjkl-completion-navigation-functions
nil))
(run-hook-with-args 'spacemacs--hjkl-completion-navigation-functions
t)
(dolist (mode holy-mode-modes-to-disable-alist)
(when (boundp (car mode))
(funcall (car mode) (cdr mode))))))