Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

  1. ;;; ein-events.el --- Event module
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-events.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-events.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-events.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'eieio)
  19. (require 'ein-core)
  20. (require 'ein-classes)
  21. (require 'ein-log)
  22. (defun ein:events-new ()
  23. "Return a new event handler instance."
  24. (make-instance 'ein:events))
  25. (defun ein:events-trigger (events event-type &optional data)
  26. "Trigger EVENT-TYPE and let event handler EVENTS handle that event."
  27. (ein:log 'debug "Event: %S" event-type)
  28. (ein:aif (gethash event-type (slot-value events 'callbacks))
  29. (mapc (lambda (cb-arg) (ein:funcall-packed cb-arg data)) it)
  30. (ein:log 'info "Unknown event: %S" event-type)))
  31. (cl-defmethod ein:events-on ((events ein:events) event-type
  32. callback &optional arg)
  33. "Set event trigger hook.
  34. When EVENT-TYPE is triggered on the event handler EVENTS,
  35. CALLBACK is called. CALLBACK must take two arguments:
  36. ARG as the first argument and DATA, which is passed via
  37. `ein:events-trigger', as the second."
  38. (cl-assert (symbolp event-type) t "%s not symbol" event-type)
  39. (let* ((table (slot-value events 'callbacks))
  40. (cbs (gethash event-type table)))
  41. (push (cons callback arg) cbs)
  42. (puthash event-type cbs table)))
  43. (provide 'ein-events)
  44. ;;; ein-events.el ends here