2012-05-24 19:33:07 +02:00
|
|
|
|
;;; ein-notification.el --- Notification widget for Notebook
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2012- Takafumi Arakaki
|
|
|
|
|
|
2012-07-01 20:18:05 +02:00
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
2012-05-24 19:33:07 +02:00
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; ein-notification.el is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; ein-notification.el is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with ein-notification.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
(require 'eieio)
|
|
|
|
|
|
2012-08-28 15:26:32 +02:00
|
|
|
|
(require 'ein-core)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(require 'ein-events)
|
2012-05-24 19:33:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Class and variable
|
|
|
|
|
|
2012-08-18 22:50:06 +02:00
|
|
|
|
(ein:deflocal ein:%notification% nil
|
2012-05-24 19:33:07 +02:00
|
|
|
|
"Buffer local variable to hold an instance of `ein:notification'.")
|
2012-08-18 22:50:06 +02:00
|
|
|
|
(define-obsolete-variable-alias 'ein:@notification 'ein:%notification% "0.1.2")
|
2012-05-24 19:33:07 +02:00
|
|
|
|
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(defvar ein:header-line-format '(:eval (ein:header-line)))
|
|
|
|
|
;; Note: can't put this below of `ein:notification-setup'...
|
|
|
|
|
|
2012-05-24 19:33:07 +02:00
|
|
|
|
(defclass ein:notification-status ()
|
2012-05-25 01:32:05 +02:00
|
|
|
|
((status :initarg :status :initform nil)
|
|
|
|
|
(message :initarg :message :initform nil)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(s2m :initarg :s2m))
|
2012-05-24 19:33:07 +02:00
|
|
|
|
"Hold status and it's string representation (message).")
|
|
|
|
|
|
2012-09-02 05:50:34 +02:00
|
|
|
|
(defclass ein:notification-tab ()
|
|
|
|
|
((get-list :initarg :get-list :type function)
|
|
|
|
|
(get-current :initarg :get-current :type function)
|
2012-10-09 22:54:05 +02:00
|
|
|
|
(get-name :initarg :get-name :type function)
|
|
|
|
|
(get-buffer :initarg :get-buffer :type function))
|
|
|
|
|
;; These "methods" are for not depending on what the TABs for.
|
|
|
|
|
;; Probably I'd want change this to be a separated Emacs lisp
|
|
|
|
|
;; library at some point.
|
|
|
|
|
"See `ein:notification-setup' for explanation.")
|
2012-09-02 05:50:34 +02:00
|
|
|
|
|
2012-05-24 19:33:07 +02:00
|
|
|
|
(defclass ein:notification ()
|
|
|
|
|
((buffer :initarg :buffer :type buffer :document "Notebook buffer")
|
2012-09-02 05:50:34 +02:00
|
|
|
|
(tab :initarg :tab :type ein:notification-tab)
|
2012-10-09 20:24:17 +02:00
|
|
|
|
(execution-count
|
|
|
|
|
:initform "y" :initarg :execution-count
|
|
|
|
|
:documentation "Last `execution_count' sent by `execute_reply'.")
|
2012-05-24 19:33:07 +02:00
|
|
|
|
(notebook
|
|
|
|
|
:initarg :notebook
|
2012-05-25 01:11:33 +02:00
|
|
|
|
:initform
|
|
|
|
|
(ein:notification-status
|
|
|
|
|
"NotebookStatus"
|
|
|
|
|
:s2m
|
2012-06-06 21:03:54 +02:00
|
|
|
|
'((notebook_saving.Notebook . "Saving Notebook...")
|
|
|
|
|
(notebook_saved.Notebook . "Notebook is saved")
|
|
|
|
|
(notebook_save_failed.Notebook . "Failed to save Notebook!")))
|
2012-05-24 19:33:07 +02:00
|
|
|
|
:type ein:notification-status)
|
|
|
|
|
(kernel
|
|
|
|
|
:initarg :kernel
|
2012-05-25 01:11:33 +02:00
|
|
|
|
:initform
|
|
|
|
|
(ein:notification-status
|
|
|
|
|
"KernelStatus"
|
|
|
|
|
:s2m
|
2012-06-06 21:03:54 +02:00
|
|
|
|
'((status_idle.Kernel . nil)
|
|
|
|
|
(status_busy.Kernel . "Kernel is busy...")
|
|
|
|
|
(status_dead.Kernel . "Kernel is dead. Need restart.")))
|
2012-05-24 19:33:07 +02:00
|
|
|
|
:type ein:notification-status))
|
|
|
|
|
"Notification widget for Notebook.")
|
|
|
|
|
|
2012-05-25 01:32:05 +02:00
|
|
|
|
(defmethod ein:notification-status-set ((ns ein:notification-status) status)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(let* ((message (cdr (assoc status (oref ns :s2m)))))
|
|
|
|
|
(oset ns :status status)
|
|
|
|
|
(oset ns :message message)))
|
|
|
|
|
|
2012-05-25 01:32:05 +02:00
|
|
|
|
(defmethod ein:notification-bind-events ((notification ein:notification)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
events)
|
|
|
|
|
"Bind a callback to events of the event handler EVENTS which
|
|
|
|
|
just set the status \(= event-type):
|
|
|
|
|
\(ein:notification-status-set NS EVENT-TYPE)
|
|
|
|
|
where NS is `:kernel' or `:notebook' slot of NOTIFICATION."
|
|
|
|
|
(loop for ns in (list (oref notification :kernel)
|
|
|
|
|
(oref notification :notebook))
|
2012-05-25 01:49:45 +02:00
|
|
|
|
for statuses = (mapcar #'car (oref ns :s2m))
|
2012-05-25 01:11:33 +02:00
|
|
|
|
do (loop for st in statuses
|
2012-05-25 01:48:29 +02:00
|
|
|
|
do (ein:events-on events
|
|
|
|
|
st ; = event-type
|
2012-05-25 01:11:33 +02:00
|
|
|
|
#'ein:notification--callback
|
2012-05-25 03:03:57 +02:00
|
|
|
|
(cons ns st))))
|
2012-09-02 06:01:17 +02:00
|
|
|
|
(ein:events-on events
|
|
|
|
|
'notebook_saved.Notebook
|
|
|
|
|
#'ein:notification--fadeout-callback
|
|
|
|
|
(list (oref notification :notebook)
|
|
|
|
|
"Notebook is saved"
|
|
|
|
|
'notebook_saved.Notebook
|
|
|
|
|
nil))
|
2012-10-09 20:24:17 +02:00
|
|
|
|
(ein:events-on events
|
|
|
|
|
'execution_count.Kernel
|
|
|
|
|
#'ein:notification--set-execution-count
|
|
|
|
|
notification)
|
2012-05-25 03:03:57 +02:00
|
|
|
|
(ein:events-on events
|
2012-06-06 21:03:54 +02:00
|
|
|
|
'status_restarting.Kernel
|
2012-05-25 03:03:57 +02:00
|
|
|
|
#'ein:notification--fadeout-callback
|
|
|
|
|
(list (oref notification :kernel)
|
|
|
|
|
"Restarting kernel..."
|
2012-06-06 21:03:54 +02:00
|
|
|
|
'status_restarting.Kernel
|
|
|
|
|
'status_idle.Kernel)))
|
2012-05-25 01:11:33 +02:00
|
|
|
|
|
|
|
|
|
(defun ein:notification--callback (packed data)
|
|
|
|
|
(let ((ns (car packed))
|
|
|
|
|
(status (cdr packed)))
|
|
|
|
|
(ein:notification-status-set ns status)))
|
|
|
|
|
|
2012-10-09 20:24:17 +02:00
|
|
|
|
(defun ein:notification--set-execution-count (notification count)
|
|
|
|
|
(oset notification :execution-count count))
|
|
|
|
|
|
2012-05-25 03:03:57 +02:00
|
|
|
|
(defun ein:notification--fadeout-callback (packed data)
|
2012-09-02 06:16:55 +02:00
|
|
|
|
;; FIXME: I can simplify this.
|
|
|
|
|
;; Do not pass around message, for exmaple.
|
2012-05-25 03:03:57 +02:00
|
|
|
|
(let ((ns (nth 0 packed))
|
|
|
|
|
(message (nth 1 packed))
|
|
|
|
|
(status (nth 2 packed))
|
|
|
|
|
(next (nth 3 packed)))
|
|
|
|
|
(oset ns :status status)
|
|
|
|
|
(oset ns :message message)
|
|
|
|
|
(apply #'run-at-time
|
|
|
|
|
1 nil
|
|
|
|
|
(lambda (ns message status next)
|
|
|
|
|
(when (equal (oref ns :status) status)
|
|
|
|
|
(ein:notification-status-set ns next)
|
2012-08-19 12:45:22 +02:00
|
|
|
|
(ein:with-live-buffer (oref ns :buffer)
|
|
|
|
|
(force-mode-line-update))))
|
2012-05-25 03:03:57 +02:00
|
|
|
|
packed)))
|
|
|
|
|
|
2012-10-09 22:54:05 +02:00
|
|
|
|
(defun ein:notification-setup (buffer events get-list get-current get-name
|
|
|
|
|
get-buffer)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
"Setup a new notification widget in the BUFFER.
|
|
|
|
|
This function saves the new notification widget instance in the
|
2012-09-02 05:50:34 +02:00
|
|
|
|
local variable of the BUFFER.
|
|
|
|
|
|
2012-10-09 22:56:40 +02:00
|
|
|
|
Rest of the arguments are for TABs in `header-line'.
|
|
|
|
|
|
|
|
|
|
GET-LIST : function
|
|
|
|
|
Return a list of worksheets.
|
|
|
|
|
|
|
|
|
|
GET-CURRENT : function
|
|
|
|
|
Return the current worksheet.
|
|
|
|
|
|
|
|
|
|
GET-NAME : function
|
|
|
|
|
Return a name of the worksheet given as its argument.
|
2012-10-09 22:54:05 +02:00
|
|
|
|
|
|
|
|
|
GET-BUFFER : function
|
|
|
|
|
Get a buffer of given worksheet. Render it if needed.
|
|
|
|
|
"
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(with-current-buffer buffer
|
2012-08-18 22:50:06 +02:00
|
|
|
|
(setq ein:%notification%
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(ein:notification "NotificationWidget" :buffer buffer))
|
|
|
|
|
(setq header-line-format ein:header-line-format)
|
2012-08-28 21:48:06 +02:00
|
|
|
|
(ein:notification-bind-events ein:%notification% events)
|
2012-09-02 05:50:34 +02:00
|
|
|
|
(oset ein:%notification% :tab
|
|
|
|
|
(make-instance 'ein:notification-tab
|
|
|
|
|
:get-list get-list
|
|
|
|
|
:get-current get-current
|
2012-10-09 22:54:05 +02:00
|
|
|
|
:get-name get-name
|
|
|
|
|
:get-buffer get-buffer))
|
2012-08-18 22:50:06 +02:00
|
|
|
|
ein:%notification%))
|
2012-05-25 01:11:33 +02:00
|
|
|
|
|
2012-09-02 05:50:34 +02:00
|
|
|
|
|
|
|
|
|
;;; Tabs
|
|
|
|
|
|
|
|
|
|
(defface ein:notification-tab-selected
|
2012-10-09 18:41:35 +02:00
|
|
|
|
'((t :inherit (header-line match) :underline t))
|
2012-09-02 05:50:34 +02:00
|
|
|
|
"Face for headline selected tab."
|
|
|
|
|
:group 'ein)
|
|
|
|
|
|
|
|
|
|
(defface ein:notification-tab-normal
|
2012-10-09 18:41:35 +02:00
|
|
|
|
'((t :inherit (header-line) :underline t :height 0.8))
|
2012-09-02 05:50:34 +02:00
|
|
|
|
"Face for headline selected tab."
|
|
|
|
|
:group 'ein)
|
|
|
|
|
|
|
|
|
|
(defmethod ein:notification-tab-create-line ((tab ein:notification-tab))
|
|
|
|
|
(let ((list (funcall (oref tab :get-list)))
|
|
|
|
|
(current (funcall (oref tab :get-current)))
|
|
|
|
|
(get-name (oref tab :get-name)))
|
|
|
|
|
(ein:join-str
|
|
|
|
|
" "
|
|
|
|
|
(loop for i from 1
|
|
|
|
|
for elem in list
|
|
|
|
|
if (eq elem current)
|
|
|
|
|
collect (propertize
|
|
|
|
|
(or (ein:and-let* ((name (funcall get-name elem)))
|
2012-10-09 18:41:35 +02:00
|
|
|
|
(format "/%d: %s\\" i name))
|
|
|
|
|
(format "/%d\\" i))
|
2012-09-02 05:50:34 +02:00
|
|
|
|
'face 'ein:notification-tab-selected)
|
|
|
|
|
else
|
|
|
|
|
collect (propertize
|
2012-10-09 18:41:35 +02:00
|
|
|
|
(format "/%d\\" i)
|
2012-09-02 05:50:34 +02:00
|
|
|
|
'face 'ein:notification-tab-normal)))))
|
|
|
|
|
|
2012-05-25 01:11:33 +02:00
|
|
|
|
|
|
|
|
|
;;; Header line
|
|
|
|
|
|
|
|
|
|
(defun ein:header-line ()
|
|
|
|
|
(format
|
2012-10-09 20:24:17 +02:00
|
|
|
|
"IP[%s]: %s"
|
|
|
|
|
(oref ein:%notification% :execution-count)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(ein:join-str
|
|
|
|
|
" | "
|
|
|
|
|
(ein:filter
|
|
|
|
|
'identity
|
2012-08-18 22:50:06 +02:00
|
|
|
|
(list (oref (oref ein:%notification% :notebook) :message)
|
2012-09-02 05:50:34 +02:00
|
|
|
|
(oref (oref ein:%notification% :kernel) :message)
|
|
|
|
|
(ein:notification-tab-create-line
|
|
|
|
|
(oref ein:%notification% :tab)))))))
|
2012-05-25 01:11:33 +02:00
|
|
|
|
|
|
|
|
|
(defun ein:header-line-setup-maybe ()
|
|
|
|
|
"Setup `header-line-format' for mumamo.
|
|
|
|
|
As `header-line-format' is buffer local variable, it must be set
|
|
|
|
|
for each chunk when in
|
|
|
|
|
See also `ein:ac-setup-maybe'."
|
2012-08-19 04:34:47 +02:00
|
|
|
|
(and (ein:eval-if-bound 'ein:%notebook%)
|
2012-05-25 01:11:33 +02:00
|
|
|
|
(ein:eval-if-bound 'mumamo-multi-major-mode)
|
|
|
|
|
(setq header-line-format ein:header-line-format)))
|
|
|
|
|
(add-hook 'after-change-major-mode-hook 'ein:header-line-setup-maybe)
|
|
|
|
|
|
2012-05-24 19:33:07 +02:00
|
|
|
|
(provide 'ein-notification)
|
|
|
|
|
|
|
|
|
|
;;; ein-notification.el ends here
|