2012-05-24 12:54:41 +02:00
|
|
|
|
;;; ein-shared-output.el --- Output buffer for ein-connect.el
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2012- Takafumi Arakaki
|
|
|
|
|
|
2012-07-01 20:18:05 +02:00
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
2012-05-24 12:54:41 +02:00
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; ein-shared-output.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-shared-output.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-shared-output.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; When executing code from outside of notebook, some place for output
|
|
|
|
|
;; is needed. This module buffer containing one special cell for that
|
|
|
|
|
;; purpose.
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
(require 'eieio)
|
|
|
|
|
|
|
|
|
|
(require 'ein-cell)
|
|
|
|
|
|
2012-05-24 14:43:28 +02:00
|
|
|
|
|
|
|
|
|
;;; Classes and variables
|
|
|
|
|
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(defclass ein:shared-output-cell (ein:codecell)
|
|
|
|
|
((cell-type :initarg :cell-type :initform "shared-output")
|
|
|
|
|
;; (element-names :initform (:prompt :output :footer))
|
2012-06-08 15:30:04 +02:00
|
|
|
|
(popup :initarg :popup :initform nil :type boolean)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
)
|
|
|
|
|
"A singleton cell to show output from non-notebook buffers.")
|
|
|
|
|
|
|
|
|
|
(defclass ein:$shared-output ()
|
|
|
|
|
((cell :initarg :cell :type ein:shared-output-cell)
|
2012-07-17 18:29:00 +02:00
|
|
|
|
(events :initarg :events :type ein:events)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(ewoc :initarg :ewoc :type ewoc)))
|
|
|
|
|
|
|
|
|
|
(defvar ein:@shared-output nil
|
|
|
|
|
"Hold an instance of `ein:$shared-output'.")
|
|
|
|
|
|
|
|
|
|
(defconst ein:shared-output-buffer-name "*ein:shared-output*")
|
|
|
|
|
|
2012-05-24 14:43:28 +02:00
|
|
|
|
|
|
|
|
|
;;; Cell related
|
|
|
|
|
|
2012-06-08 15:30:04 +02:00
|
|
|
|
(defmethod ein:cell-execute ((cell ein:shared-output-cell) kernel code
|
|
|
|
|
&optional popup)
|
|
|
|
|
(oset cell :popup popup)
|
2012-06-08 16:15:44 +02:00
|
|
|
|
(oset cell :kernel kernel)
|
2012-06-02 21:53:59 +02:00
|
|
|
|
(ein:cell-execute-internal cell kernel code :silent nil))
|
2012-05-24 12:54:41 +02:00
|
|
|
|
|
2012-05-24 14:40:19 +02:00
|
|
|
|
(defmethod ein:cell--handle-output ((cell ein:shared-output-cell)
|
2012-08-08 21:30:14 +02:00
|
|
|
|
msg-type content -metadata-not-used-)
|
2012-05-24 14:40:19 +02:00
|
|
|
|
(ein:log 'info "Got output '%s' in the shared buffer." msg-type)
|
2012-06-08 15:30:04 +02:00
|
|
|
|
(when (oref cell :popup)
|
2012-06-08 19:13:30 +02:00
|
|
|
|
(pop-to-buffer (ein:shared-output-create-buffer)))
|
2012-05-24 14:40:19 +02:00
|
|
|
|
(call-next-method))
|
|
|
|
|
|
2012-05-24 14:43:28 +02:00
|
|
|
|
|
|
|
|
|
;;; Main
|
|
|
|
|
|
2012-06-08 19:13:30 +02:00
|
|
|
|
(defun ein:shared-output-create-buffer ()
|
|
|
|
|
"Get or create the shared output buffer."
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(get-buffer-create ein:shared-output-buffer-name))
|
|
|
|
|
|
2012-06-08 19:10:06 +02:00
|
|
|
|
(defun ein:shared-output-buffer ()
|
2012-06-08 19:13:30 +02:00
|
|
|
|
"Get the buffer associated with `ein:@shared-output'."
|
2012-06-08 19:10:06 +02:00
|
|
|
|
(ewoc-buffer (oref ein:@shared-output :ewoc)))
|
|
|
|
|
|
|
|
|
|
(defun ein:shared-output-healthy-p ()
|
|
|
|
|
(and (ein:$shared-output-p ein:@shared-output)
|
|
|
|
|
(buffer-live-p (ein:shared-output-buffer))))
|
|
|
|
|
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(defun ein:shared-output-get-or-create ()
|
2012-06-08 19:10:06 +02:00
|
|
|
|
(if (ein:shared-output-healthy-p)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
ein:@shared-output
|
2012-06-08 19:13:30 +02:00
|
|
|
|
(with-current-buffer (ein:shared-output-create-buffer)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
;; FIXME: This is a duplication of `ein:notebook-from-json'.
|
|
|
|
|
;; Must be merged.
|
|
|
|
|
(let* ((inhibit-read-only t)
|
|
|
|
|
;; Enable nonsep for ewoc object (the last argument is non-nil).
|
|
|
|
|
;; This is for putting read-only text properties to the newlines.
|
|
|
|
|
;; FIXME: Do not depend on `ein:notebook-pp'!
|
|
|
|
|
(ewoc (ewoc-create 'ein:notebook-pp
|
|
|
|
|
(ein:propertize-read-only "\n")
|
|
|
|
|
nil t))
|
2012-06-05 00:52:00 +02:00
|
|
|
|
(events (ein:events-new (current-buffer)))
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(cell (ein:shared-output-cell "SharedOutputCell"
|
2012-06-05 00:52:00 +02:00
|
|
|
|
:ewoc ewoc
|
|
|
|
|
:events events)))
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(erase-buffer)
|
2012-06-05 00:52:00 +02:00
|
|
|
|
(ein:shared-output-bind-events events)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(setq ein:@shared-output
|
2012-07-17 18:29:00 +02:00
|
|
|
|
(ein:$shared-output "SharedOutput" :ewoc ewoc :cell cell
|
|
|
|
|
:events events))
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(ein:cell-enter-last cell))
|
2012-06-08 16:23:06 +02:00
|
|
|
|
(setq buffer-read-only t)
|
2012-06-08 16:15:44 +02:00
|
|
|
|
(ein:shared-output-mode)
|
2012-05-24 12:54:41 +02:00
|
|
|
|
ein:@shared-output)))
|
|
|
|
|
|
2012-06-05 00:52:00 +02:00
|
|
|
|
(defun ein:shared-output-bind-events (events)
|
2012-06-17 01:12:13 +02:00
|
|
|
|
"Add dummy event handlers."
|
|
|
|
|
(ein:events-on events 'set_dirty.Notebook (lambda (&rest ignore)))
|
|
|
|
|
(ein:events-on events 'maybe_reset_undo.Notebook (lambda (&rest ignore))))
|
2012-06-05 00:52:00 +02:00
|
|
|
|
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(defun ein:shared-output-get-cell ()
|
|
|
|
|
"Get the singleton shared output cell.
|
|
|
|
|
Create a cell if the buffer has none."
|
|
|
|
|
(oref (ein:shared-output-get-or-create) :cell))
|
|
|
|
|
|
2012-06-08 16:15:44 +02:00
|
|
|
|
(defun ein:shared-output-get-kernel ()
|
|
|
|
|
(let ((cell (ein:shared-output-get-cell)))
|
|
|
|
|
(when (slot-boundp cell :kernel)
|
|
|
|
|
(oref cell :kernel))))
|
|
|
|
|
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(defun ein:shared-output-pop-to-buffer ()
|
2012-06-11 06:15:34 +02:00
|
|
|
|
"Open shared output buffer."
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(interactive)
|
|
|
|
|
(ein:shared-output-get-or-create)
|
2012-06-08 19:13:30 +02:00
|
|
|
|
(pop-to-buffer (ein:shared-output-create-buffer)))
|
2012-05-24 12:54:41 +02:00
|
|
|
|
|
2012-07-17 18:29:00 +02:00
|
|
|
|
(defmethod ein:shared-output-show-code-cell ((cell ein:codecell))
|
|
|
|
|
"Show code CELL in shared-output buffer.
|
|
|
|
|
Note that this function assumed to be called in the buffer
|
|
|
|
|
where CELL locates."
|
|
|
|
|
(let ((new (ein:cell-convert cell "shared-output")))
|
|
|
|
|
;; Make sure `ein:@shared-output' is initialized:
|
|
|
|
|
(ein:shared-output-get-or-create)
|
|
|
|
|
(with-current-buffer (ein:shared-output-create-buffer)
|
2012-07-17 18:48:02 +02:00
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
|
(ein:cell-max-num-outputs nil))
|
2012-07-17 18:29:00 +02:00
|
|
|
|
(oset new :ewoc (oref ein:@shared-output :ewoc))
|
|
|
|
|
(oset new :events (oref ein:@shared-output :events))
|
|
|
|
|
(erase-buffer) ; because there are only one cell anyway
|
|
|
|
|
(oset ein:@shared-output :cell new)
|
|
|
|
|
(ein:cell-enter-last new)
|
|
|
|
|
(pop-to-buffer (current-buffer))))))
|
|
|
|
|
|
2012-08-13 18:55:32 +02:00
|
|
|
|
|
|
|
|
|
;;; Generic getter
|
|
|
|
|
|
|
|
|
|
(defun ein:get-url-or-port--shared-output ()
|
|
|
|
|
(ein:aand (ein:get-kernel--shared-output) (ein:$kernel-url-or-port it)))
|
|
|
|
|
|
|
|
|
|
;; (defun ein:get-notebook--shared-output ())
|
|
|
|
|
|
|
|
|
|
(defun ein:get-kernel--shared-output ()
|
2012-08-14 19:32:50 +02:00
|
|
|
|
(let ((cell (ein:get-cell-at-point--shared-output)))
|
|
|
|
|
(when (slot-boundp cell :kernel)
|
|
|
|
|
(oref cell :kernel))))
|
|
|
|
|
|
|
|
|
|
(defun ein:get-cell-at-point--shared-output ()
|
2012-08-13 18:55:32 +02:00
|
|
|
|
(when (ein:$shared-output-p ein:@shared-output)
|
2012-08-14 19:32:50 +02:00
|
|
|
|
(oref ein:@shared-output :cell)))
|
2012-08-13 18:55:32 +02:00
|
|
|
|
|
2012-06-08 16:15:44 +02:00
|
|
|
|
|
|
|
|
|
;;; ein:shared-output-mode
|
|
|
|
|
|
|
|
|
|
(define-derived-mode ein:shared-output-mode fundamental-mode "ein:so"
|
|
|
|
|
"Shared output mode."
|
|
|
|
|
(font-lock-mode))
|
|
|
|
|
|
|
|
|
|
(let ((map ein:shared-output-mode-map))
|
2012-06-17 02:36:00 +02:00
|
|
|
|
(define-key map "\M-." 'ein:pytools-jump-to-source-command)
|
2012-06-19 00:41:33 +02:00
|
|
|
|
(define-key map (kbd "C-c C-.") 'ein:pytools-jump-to-source-command)
|
|
|
|
|
(define-key map "q" 'bury-buffer))
|
2012-06-08 16:15:44 +02:00
|
|
|
|
|
2012-06-08 19:02:11 +02:00
|
|
|
|
(add-hook 'ein:shared-output-mode-hook 'ein:truncate-lines-on)
|
|
|
|
|
|
2012-06-08 16:15:44 +02:00
|
|
|
|
|
2012-05-24 12:54:41 +02:00
|
|
|
|
(provide 'ein-shared-output)
|
|
|
|
|
|
|
|
|
|
;;; ein-shared-output.el ends here
|