Skip to content

Commit

Permalink
core: remove core-evilify-keymap.el
Browse files Browse the repository at this point in the history
Move its contents to core-evilified-state.el
  • Loading branch information
syl20bnr committed Sep 26, 2015
1 parent d237d56 commit 773076a
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 222 deletions.
137 changes: 137 additions & 0 deletions core/core-evilified-state.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

;;; Code:

(require 'core-dotspacemacs)
(require 'evil)
(require 'evil-leader)

(defvar spacemacs-core-evilified-state--modes nil
"List of all evilified modes.")
Expand Down Expand Up @@ -108,6 +110,141 @@
(define-key evil-evilified-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-evilified-state-map (kbd "C-z") 'evil-emacs-state)

;; old macro
(defmacro evilify (mode map &rest body)
"Set `evilified state' as default for MODE.
BODY is a list of additional key bindings to apply for the given MAP in
`evilified state'."
(let ((defkey (when body `(evil-define-key 'evilified ,map ,@body))))
`(progn (unless ,(null mode)
(unless (memq ',mode spacemacs-core-evilified-state--modes)
(push ',mode spacemacs-core-evilified-state--modes))
(unless (or (bound-and-true-p holy-mode)
(memq ',mode evil-evilified-state-modes))
(delq ',mode evil-emacs-state-modes)
(push ',mode evil-evilified-state-modes)))
(unless ,(null defkey) (,@defkey)))))

;; new macro
(defmacro spacemacs|evilify-map (map &rest props)
"Evilify MAP.
Avaiblabe PROPS:
`:mode SYMBOL'
A mode SYMBOL associated with MAP. Used to add SYMBOL to the list of modes
defaulting to `evilified-state'.
`:evilified-map SYMBOL'
A map SYMBOL of an alternate evilified map, if nil then
`evil-evilified-state-map' is used.
`:eval-after-load SYMBOL'
If specified the evilification of MAP is deferred to the loading of the feature
bound to SYMBOL. May be required for some lazy-loaded maps.
`:bindings EXPRESSIONS'
One or several EXPRESSIONS with the form `KEY FUNCTION':
KEY1 FUNCTION1
KEY2 FUNCTION2
...
Each pair KEYn FUNCTIONn is defined in MAP after the evilification of it."
(declare (indent 1))
(let* ((mode (plist-get props :mode))
(evilified-map (plist-get props :evilified-map))
(eval-after-load (plist-get props :eval-after-load))
(bindings (spacemacs/mplist-get props :bindings))
(defkey (when bindings `(evil-define-key 'evilified ,map ,@bindings)))
(body
`(progn
(let ((sorted-map (spacemacs//evilify-sort-keymap
(or ,evilified-map evil-evilified-state-map)))
processed)
(mapc (lambda (map-entry)
(unless (member (car map-entry) processed)
(setq processed (spacemacs//evilify-event
,map ',map
(or ,evilified-map
evil-evilified-state-map)
(car map-entry) (cdr map-entry)))))
sorted-map))
(unless ,(null defkey)
(,@defkey))
(unless ,(null mode)
(spacemacs/evilify-configure-default-state ',mode)))))
(if (null eval-after-load)
`(,@body)
`(eval-after-load ',eval-after-load '(,@body)))))

(defun spacemacs/evilify-configure-default-state (mode)
"Configure default state for the passed mode."
(add-to-list 'spacemacs-core-evilified-state--modes mode)
(unless (bound-and-true-p holy-mode)
(delq mode evil-emacs-state-modes)
(add-to-list 'evil-evilified-state-modes mode)))

(defun spacemacs//evilify-event (map map-symbol evil-map event evil-value
&optional processed pending-funcs)
"Evilify EVENT in MAP and return a list of PROCESSED events."
(if (and event (or evil-value pending-funcs))
(let* ((kbd-event (kbd (single-key-description event)))
(map-value (lookup-key map kbd-event))
(evil-value (or evil-value
(lookup-key evil-map kbd-event)
(car (pop pending-funcs)))))
(when evil-value
(evil-define-key 'evilified map kbd-event evil-value))
(when map-value
(add-to-list 'pending-funcs (cons map-value event) 'append))
(push event processed)
(setq processed (spacemacs//evilify-event
map map-symbol evil-map
(spacemacs//evilify-find-new-event event) nil
processed pending-funcs)))
(when pending-funcs
(spacemacs-buffer/warning
(concat (format (concat "Auto-evilication could not remap these "
"functions in map `%s':\n")
map-symbol)
(mapconcat (lambda (x)
(format " - `%s' originally mapped on `%s'"
(car x) (single-key-description (cdr x))))
pending-funcs "\n")))))
processed)

(defun spacemacs//evilify-find-new-event (event)
"Return a new event for the evilified EVENT."
(when event
(cond
;; space
((equal event 32) nil)
((equal event ?/) nil)
((equal event ?:) nil)
;; C-g (cannot remap C-g)
((equal event ?\a) nil)
((and (<= ?a event) (<= event ?z)) (- event 32))
;; don't shadow C-g, G is mapped directly to C-S-g
((equal event ?G) (+ (expt 2 25) ?\a))
((and (<= ?A event) (<= event ?Z)) (- event 64))
((and (<= 1 event) (<= event 26)) (+ (expt 2 25) event)))))

(defun spacemacs//evilify-sort-keymap (map)
"Sort MAP following the order: `s' > `S' > `C-s' > `C-S-s'"
(let (list)
(map-keymap (lambda (a b) (push (cons a b) list)) map)
(sort list
(lambda (a b)
(setq a (car a) b (car b))
(if (integerp a)
(if (integerp b)
(if (and (< a 256) (< b 256))
(> a b)
(< a b))
t)
(if (integerp b) nil
(string< a b)))))))

(provide 'core-evilified-state)

;;; core-evilified-state.el ends here
148 changes: 0 additions & 148 deletions core/core-evilify-keymap.el

This file was deleted.

44 changes: 44 additions & 0 deletions core/core-funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3

(defun spacemacs/load-or-install-package (pkg &optional log file-to-load)
"Load PKG package. PKG will be installed if it is not already installed.
Whenever the initial require fails the absolute path to the package
directory is returned.
If LOG is non-nil a message is displayed in spacemacs-mode buffer.
FILE-TO-LOAD is an explicit file to load after the installation."
(let ((warning-minimum-level :error))
(condition-case nil
(require pkg)
(error
;; not installed, we try to initialize package.el only if required to
;; precious seconds during boot time
(require 'cl)
(let ((pkg-elpa-dir (spacemacs//get-package-directory pkg)))
(if pkg-elpa-dir
(add-to-list 'load-path pkg-elpa-dir)
;; install the package
(when log
(spacemacs-buffer/append
(format "(Bootstrap) Installing %s...\n" pkg))
(spacemacs//redisplay))
(package-refresh-contents)
(package-install pkg)
(setq pkg-elpa-dir (spacemacs//get-package-directory pkg)))
(require pkg nil 'noerror)
(when file-to-load
(load-file (concat pkg-elpa-dir file-to-load)))
pkg-elpa-dir)))))

(defun spacemacs//get-package-directory (pkg)
"Return the directory of PKG. Return nil if not found."
(let ((elpa-dir (concat user-emacs-directory "elpa/")))
(when (file-exists-p elpa-dir)
(let ((dir (reduce (lambda (x y) (if x x y))
(mapcar (lambda (x)
(when (string-match
(concat "/"
(symbol-name pkg)
"-[0-9]+") x) x))
(directory-files elpa-dir 'full))
:initial-value nil)))
(when dir (file-name-as-directory dir))))))

(defun spacemacs/mplist-get (plist prop)
"Get the values associated to PROP in PLIST, a modified plist.
Expand Down
Loading

0 comments on commit 773076a

Please sign in to comment.