Browse Source

Finalize the defkey macro

v3
Sameer Rahmani 2 years ago
parent
commit
a7cfe76bf3
  1. 2
      core/cubes/elisp.el
  2. 6
      core/cubes/org.el
  3. 14
      core/fg42/system/core.el
  4. 90
      core/fg42/system/keys.el

2
core/cubes/elisp.el

@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
;;:modes '((emacs-lisp-mode . f42/elisp-cube-after-load))
;;:hooks (list (lambda (system) '(emacs-lisp-mode-hook . fssd)))
;;:auto-modes (list (lambda (system) '("\\.el\\'" . fg42/elisp-cube-mmm)))
;; :init #'iii
;;:init #'iii
:dependencies '((:name rainbow-delimiters :version :latest)))

6
core/cubes/org.el

@ -27,11 +27,9 @@ @@ -27,11 +27,9 @@
;;; Code:
(require 'fg42/cube)
(defcube fg42/org-cube ()
:keys '((org-mode-map
:good (("s-s" . (lambda () (message "s-s"))))
:evil))
;;:hooks '((org-mode-hook . iii))
;:init #'iii
:dependencies '((:name org :version :latest)))

14
core/fg42/system/core.el

@ -24,8 +24,7 @@ @@ -24,8 +24,7 @@
;; Each system has to have a `start' function to start the setup process.
;;
;;; Code:
(require 'cl-lib)
(require 'seq)
(require 'fg42/utils)
@ -62,7 +61,16 @@ @@ -62,7 +61,16 @@
"Define a system with the given NAME, PROPS and BODY."
(declare (indent 1))
`(defun ,name ()
(fg42/cube-compose ,@body)))
(if ,props
(lambda (___system___)
(funcall
(fg42/cube-compose ,@body)
(seq-reduce
(lambda (system pair)
(fg42/system-cons (car pair) (cadr pair)))
(seq-partition ,props 2)
___system___)))
(fg42/cube-compose ,@body))))
(provide 'fg42/system/core)

90
core/fg42/system/keys.el

@ -28,6 +28,16 @@ @@ -28,6 +28,16 @@
(require 'fg42/system/core)
(defun fg42/system-is-good? (system)
"Return non-nil if the keyset on the given SYSTEM is set to :good."
(eq (or (fg42/system-get system :i-am) :good) :good))
(defun fg42/system-is-evil? (system)
"Return non-nil if the keyset on the given SYSTEM is set to :good."
(eq (or (fg42/system-get system :i-am) :good) :evil))
(defun fg42/system-merge-keys (system cube-name keys)
"Retun an updated SYSTEM with the given keys KEYS for CUBE-NAME.
It converts the `:keys' of the cube to the following format and adds
@ -50,45 +60,6 @@ them to the system. (cube-nam mode (key f)s...)." @@ -50,45 +60,6 @@ them to the system. (cube-nam mode (key f)s...)."
system)))
;; (defun fg42/system-merge-keys (system cube-name keys)
;; "Retun an updated SYSTEM with the given keys KEYS for CUBE-NAME.
;; It converts the `:keys' of the cube to the following format and adds
;; them to the system. (cube-nam mode (key f)s...)."
;; (let ((keyset :good))
;; (if keys
;; ;; TODO: Validate the deps here
;; (fg42/system-cons-to system
;; :keys
;; (car
;; (seq-reduce
;; (lambda (bindings keys)
;; (let ((mode (car keys))
;; (pair-list (plist-get (cdr keys) keyset)))
;; (cons
;; (list cube-name mode pair-list)
;; bindings)))
;; keys
;; (assoc))))
;; system)))
;; (defun fg42/system-setup-keys (system)
;; "Setup the key binding through out the SYSTEM."
;; (message "#$$$$$########### %s" (fg42/system-get system :keys))
;; (let ((keyset :good))
;; (mapcar
;; (lambda (key-desc)
;; (let ((cube-name (car key-desc))
;; (map (cadr key-desc))
;; (binding (caddr key-desc))
;; (f (cadddr key-desc)))
;; (message "Set keybinding %s" key-desc)
;; (if (eq keyset :good)
;; (define-in-keymap map binding f)
;; (define-evil-in-keymap map binding f))))
;; (fg42/system-get system :keys)))
;; system)
(defun fg42/system-setup-keys (system)
"Setup the key binding through out the SYSTEM."
@ -120,5 +91,46 @@ them to the system. (cube-nam mode (key f)s...)." @@ -120,5 +91,46 @@ them to the system. (cube-nam mode (key f)s...)."
nil)
(defun fg42/defkey--good (map key fn)
"Set the given KEY on key map MAP to FN."
(message "SSSSSSSSSSSS %s %s" (type-of map) map)
(funcall #'define-key map (kbd key) fn))
(defun fg42/defkey--evil (map state-keys fn)
"Set the given STATE-KEYS on key map MAP to FN."
(let ((normal-key (plist-get state-keys :normal))
(visual-key (plist-get state-keys :visual))
(insert-key (plist-get state-keys :insert))
(emacs-key (plist-get state-keys :emacs)))
(cond
((not (null normal-key)) (evil-define-key 'normal map (kbd normal-key) fn))
((not (null visual-key)) (evil-define-key 'visual map (kbd visual-key) fn))
((not (null insert-key)) (evil-define-key 'insert map (kbd insert-key) fn))
((not (null emacs-key)) (evil-define-key 'emacs map (kbd emacs-key) fn)))))
(defmacro defkey (map fn &rest keys)
"Defines a key binding for FG42 for different types.
Defines a keybinding in the given MAP for the given KEYS that maps
to the given FN with the given DOCSTRING.
Example usage :
\\(defkey `'global-map`' 'goto-line
:evil \\(:normal \"SPC s u\"\\)
:good \"<f2>\"\\)"
(declare (indent defun))
(let ((good-key (plist-get keys :good))
(evil-state-key (plist-get keys :evil)))
(when (and (fg42/system-is-evil? fg42/system) (null evil-state-key))
(error "You should pass :evil keys when you are evil user"))
(when (and (fg42/system-is-good? fg42/system) (null good-key))
(error "You should pass :good keys when you are a good user"))
(cond
((fg42/system-is-good? fg42/system)
`(fg42/defkey--good ,map ,good-key ,fn))
((fg42/system-is-evil? fg42/system)
`(fg42/defkey-evil ,map (quote ,evil-state-key) ,fn)))))
(provide 'fg42/system/keys)
;;; keys.el ends here

Loading…
Cancel
Save