;;; ein-notebooklist.el --- Notebook list buffer ;; Copyright (C) 2012- Takafumi Arakaki ;; Author: Takafumi Arakaki ;; This file is NOT part of GNU Emacs. ;; ein-notebooklist.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-notebooklist.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-notebooklist.el. If not, see . ;;; Commentary: ;; ;;; Code: (eval-when-compile (require 'cl)) (require 'widget) (require 'ein-utils) (require 'ein-notebook) (require 'ein-subpackages) (defstruct ein:$notebooklist "Hold notebooklist variables. `ein:$notebooklist-url-or-port' URL or port of IPython server. `ein:$notebooklist-data' JSON data sent from the server." url-or-port data) (ein:deflocal ein:notebooklist nil "Buffer local variable to store an instance of `ein:$notebooklist'.") (defvar ein:notebooklist-buffer-name-template "*ein:notebooklist %s*") (defun ein:notebooklist-url (url-or-port) (ein:url url-or-port "notebooks")) (defun ein:notebooklist-new-url (url-or-port) (ein:url url-or-port "new")) (defun ein:notebooklist-get-buffer (url-or-port) (get-buffer-create (format ein:notebooklist-buffer-name-template url-or-port))) ;;;###autoload (defun ein:notebooklist-open (&optional url-or-port no-popup) "Open notebook list buffer." (interactive (list (completing-read "URL or port number (hit TAB to complete): " (mapcar (lambda (x) (format "%s" x)) ein:url-or-port)))) (unless url-or-port (setq url-or-port (or (car ein:url-or-port) 8888))) (ein:subpackages-load) (when (and (stringp url-or-port) (string-match "^[0-9]+$" url-or-port)) (setq url-or-port (string-to-number url-or-port))) (let ((success (if no-popup #'ein:notebooklist-url-retrieve-callback (lambda (&rest args) (pop-to-buffer (apply #'ein:notebooklist-url-retrieve-callback args)))))) (ein:query-ajax (ein:notebooklist-url url-or-port) :cache nil :success (cons success url-or-port) :timeout 5000)) (ein:notebooklist-get-buffer url-or-port)) (defun* ein:notebooklist-url-retrieve-callback (url-or-port &key status &allow-other-keys) "Called via `ein:notebooklist-open'." (ein:aif (plist-get status :error) (error "Failed to connect to server '%s'. Got: %S" (ein:url url-or-port) it)) (let ((data (ein:json-read))) (with-current-buffer (ein:notebooklist-get-buffer url-or-port) (setq ein:notebooklist (make-ein:$notebooklist :url-or-port url-or-port :data data)) (ein:notebooklist-render) (goto-char (point-min)) (current-buffer)))) (defun ein:notebooklist-reload () "Reload current Notebook list." (interactive) (ein:notebooklist-open (ein:$notebooklist-url-or-port ein:notebooklist) t)) (defun ein:notebooklist-get-data-in-body-tag (key) "Very ad-hoc parser to get data in body tag." (ignore-errors (save-excursion (goto-char (point-min)) (search-forward "