Skip to content

Commit

Permalink
[task] bqn-mode-map.el: improvements
Browse files Browse the repository at this point in the history
As taken from #22
  • Loading branch information
AndersonTorres committed Mar 11, 2023
1 parent 4b5474e commit 2ea7da0
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions bqn-mode-map.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,27 @@

;;; Code:

(eval-and-compile
(defun bqn-mode-map--make-key-command-sym (n)
(intern (concat "insert-sym-bqn-" n))))
(defun bqn-mode-map--command-name (name)
"Return a symbol for the command NAME."
(intern (concat "bqn-insert-sym-" name)))

(cl-macrolet ((make-insert-functions ()
`(progn
,@(mapcar (lambda (command)
`(defun ,(bqn-mode-map--make-key-command-sym (car command)) ()
(interactive)
(insert ,(cadr command))))
bqn-symbols--list))))
(make-insert-functions))

(defun bqn-mode-map--kbd (definition)
"Function wrapper for `kbd' with arg DEFINITION."
(if (functionp #'kbd)
(kbd definition)
(eval `(kbd ,definition))))
(pcase-dolist (`(,name ,symbol ,_) bqn-symbols--list)
(defalias (bqn-mode-map--command-name name)
(lambda ()
(interactive)
(insert symbol))))

(defun bqn-mode-map--make-base (prefix)
"Create a new keymap using the key PREFIX."
(let ((map (make-sparse-keymap)))
(dolist (command bqn-symbols--list)
(let ((key (single-key-description (caddr command))))
(define-key map (bqn-mode-map--kbd (concat prefix key)) (bqn-mode-map--make-key-command-sym (car command)))))
(pcase-dolist (`(,name ,_ ,key) bqn-symbols--list)
(let ((cmd (bqn-mode-map--command-name name))
(key (single-key-description key)))
(define-key map (kbd (concat prefix key)) cmd)))
(define-key map [menu-bar bqn] (cons "BQN" (make-sparse-keymap "BQN")))
map))

;; value gets updated by initialization of bqn-mode-map-prefix
;; value gets updated by initialization of `bqn-mode-map-prefix'
(defvar bqn-mode-map--keymap nil
"The keymap for ‘bqn-mode’.")

Expand All @@ -52,15 +44,15 @@
(setq bqn-mode-map--keymap (bqn-mode-map--make-base new)))

(defcustom bqn-mode-map-prefix "s-"
"This stores the keymap prefix for bqn-mode-map--keymap.
"This stores the keymap prefix for `bqn-mode-map--keymap'.
It is used both to store the new value using
set-create’ and to update bqn-mode-map--keymap using
`set-create’ and to update `bqn-mode-map--keymap' using
`bqn-mode-map--make-base'. Kill and restart your BQN buffers
to reflect the change."
:type 'string
:group 'bqn
:set 'bqn-mode-map--set-prefix
:initialize 'custom-initialize-set)
:set #'bqn-mode-map--set-prefix
:initialize #'custom-initialize-set)

(provide 'bqn-mode-map)

Expand Down

0 comments on commit 2ea7da0

Please sign in to comment.