global-keymap.lisp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;;; global-keymap.lisp
  2. (in-package #:global-keymap)
  3. (defvar *keybindings*)
  4. (defvar *keybinding-functions*)
  5. (defvar *keybinding-counter*)
  6. (defun init ()
  7. (setf *keybindings* (make-hash-table :test 'equalp)
  8. *keybinding-functions* (make-hash-table)
  9. *keybinding-counter* 0))
  10. (init)
  11. (defun sync ()
  12. (let ((m (stumpwm:make-sparse-keymap)))
  13. (loop for key being the hash-keys in *keybindings* using (hash-value code)
  14. do (stumpwm:define-key m key (format nil "call-thunk ~D" code)))
  15. (setf stumpwm:*top-map* m)
  16. (stumpwm::sync-keys)))
  17. (defun install-key (key)
  18. (ensure-normalized-key key)
  19. (stumpwm:define-key stumpwm:*top-map* key (format nil "call-thunk ~D" (gethash key *keybindings*))))
  20. (defun uninstall-key (key)
  21. (ensure-normalized-key key)
  22. (stumpwm:undefine-key stumpwm:*top-map* key))
  23. (defun add-binding (key thunk &key (sync t))
  24. (multiple-value-bind (code binding-exists?) (gethash key *keybindings* (incf *keybinding-counter*))
  25. (unless binding-exists?
  26. (setf (gethash key *keybindings*) code))
  27. (setf (gethash code *keybinding-functions*) thunk))
  28. (when sync
  29. (install-key key)))
  30. (defun remove-binding (key &key (sync t))
  31. (ensure-normalized-key key)
  32. (when sync
  33. (uninstall-key key))
  34. (remhash (gethash key *keybinding-functions*) *keybinding-functions*)
  35. (remhash key *keybinding-functions*))
  36. (defun binding (key)
  37. (ensure-normalized-key key)
  38. (multiple-value-bind (code binding-exists?) (gethash key *keybindings*)
  39. (if binding-exists?
  40. (values (gethash code *keybinding-functions*))
  41. nil)))
  42. (defun clear (&key (sync t))
  43. (init)
  44. (when sync
  45. (setf stumpwm:*top-map* (stumpwm:make-sparse-keymap))
  46. (stumpwm::sync-keys)))
  47. (defun keys ()
  48. (loop for key being the hash-keys in *keybindings*
  49. collect key))
  50. (stumpwm:defcommand call-thunk (code) ((:number code))
  51. (handler-case (funcall (gethash code *keybinding-functions*))
  52. (simple-error (c) (stumpwm:message "~A" c))
  53. (error () nil)))
  54. (unexport 'call-thunk)