Merge branch 'notebook-buffer-file-name'

fixes #61 (vc.el with EIN)
This commit is contained in:
Takafumi Arakaki 2012-10-09 15:47:23 +02:00
commit b17551f57c
3 changed files with 64 additions and 10 deletions

View file

@ -135,6 +135,11 @@ This key will be installed in the `ein:notebook-mode-map'."
:type 'boolean
:group 'ein)
(defcustom ein:notebook-set-buffer-file-name nil
"[EXPERIMENTAL] Set `buffer-file-name' of notebook buffer."
:type 'boolean
:group 'ein)
(defvar ein:notebook-after-rename-hook nil
"Hooks to run after notebook is renamed successfully.
Current buffer for these functions is set to the notebook buffer.")
@ -506,8 +511,26 @@ This is equivalent to do ``C-c`` in the console program."
(lambda () ein:%worksheet%)
#'ein:worksheet-name)
(ein:notebook-setup-kill-buffer-hook)
(ein:notebook-set-buffer-file-name-maybe notebook)
(setq ein:%notebook% notebook)))
(defun ein:notebook-set-buffer-file-name-maybe (notebook)
"Set `buffer-file-name' of the current buffer to ipynb file
of NOTEBOOK."
(when ein:notebook-set-buffer-file-name
(ein:notebook-fetch-data
notebook
(lambda (data notebook buffer)
(with-current-buffer buffer
(destructuring-bind (&key project &allow-other-keys)
data
(setq buffer-file-name
(expand-file-name
(format "%s.ipynb"
(ein:$notebook-notebook-name notebook))
project)))))
(list notebook (current-buffer)))))
(defun ein:notebook-from-json (notebook data)
(destructuring-bind (&key metadata nbformat nbformat_minor
&allow-other-keys)
@ -1249,6 +1272,34 @@ Note that print page is not supported in IPython 0.12.1."
(message "Opening %s in browser" url)
(browse-url url)))
(defun ein:notebook-fetch-data (notebook callback &optional cbargs)
"Fetch data in body tag of NOTEBOOK html page.
CALLBACK is called with a plist with data in the body tag as
the first argument and CBARGS as the rest of arguments."
(let ((url-or-port (ein:$notebook-url-or-port notebook))
(notebook-id (ein:$notebook-notebook-id notebook)))
(ein:query-singleton-ajax
(list 'notebook-fetch-data url-or-port notebook-id)
(ein:url url-or-port notebook-id)
:parser
(lambda ()
(list
:project
(ein:html-get-data-in-body-tag "data-project")
:base-project-url
(ein:html-get-data-in-body-tag "data-base-project-url")
:base-kernel-url
(ein:html-get-data-in-body-tag "data-base-kernel-url")
:read-only
(ein:html-get-data-in-body-tag "data-read-only")
:notebook-id
(ein:html-get-data-in-body-tag "data-notebook-id")))
:success
(cons (function*
(lambda (packed &key data &allow-other-keys)
(apply (car packed) data (cadr packed))))
(list callback cbargs)))))
;;; Buffer and kill hooks

View file

@ -192,15 +192,6 @@ This function is called via `ein:notebook-after-rename-hook'."
(add-hook 'ein:notebook-after-rename-hook 'ein:notebooklist-refresh-related)
(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 "<body")
(search-forward-regexp (format "%s=\\([^[:space:]\n]+\\)" key))
(match-string 1))))
(defun ein:notebooklist-open-notebook (nblist notebook-id &optional name
callback cbargs)
(ein:notebook-open (ein:$notebooklist-url-or-port nblist) notebook-id
@ -220,7 +211,7 @@ This function is called via `ein:notebook-after-rename-hook'."
(list 'notebooklist-new-notebook url-or-port)
(ein:notebooklist-new-url url-or-port)
:parser (lambda ()
(ein:notebooklist-get-data-in-body-tag "data-notebook-id"))
(ein:html-get-data-in-body-tag "data-notebook-id"))
:error (cons #'ein:notebooklist-new-notebook-error
(list url-or-port callback cbargs))
:success (cons #'ein:notebooklist-new-notebook-callback

View file

@ -143,6 +143,18 @@ before previous opening parenthesis."
See: http://api.jquery.com/jQuery.ajax/"
(concat url (format-time-string "?_=%s")))
;;; HTML utils
(defun ein:html-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 "<body")
(search-forward-regexp (format "%s=\\([^[:space:]\n]+\\)" key))
(match-string 1))))
;;; JSON utils