2016-10-25 18:00:51 -05:00
|
|
|
;;; ein-cell-edit.el --- Notebook cell editing
|
|
|
|
|
|
|
|
;; Copyright (C) 2016 John M. Miller
|
|
|
|
|
|
|
|
;; Author: John Miller <millejoh at mac.com>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-cell-edit.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-cell-edit.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-worksheet.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This code inspired by borrowing from org-src.el.
|
|
|
|
|
|
|
|
;;; Code:
|
2016-10-26 13:25:52 -05:00
|
|
|
(require 'ein-cell)
|
2018-10-15 11:21:05 -04:00
|
|
|
|
2019-02-20 12:21:11 -03:00
|
|
|
(autoload 'julia-mode "julia-mode")
|
2018-10-15 11:21:05 -04:00
|
|
|
(autoload 'markdown-mode "markdown-mode")
|
|
|
|
(autoload 'R-mode "ess-r-mode")
|
|
|
|
(autoload 'org-src--remove-overlay "org-src")
|
|
|
|
(autoload 'org-src-switch-to-buffer "org-src")
|
2016-10-25 18:00:51 -05:00
|
|
|
|
|
|
|
(defvar ein:src--cell nil)
|
|
|
|
(defvar ein:src--ws nil)
|
|
|
|
(defvar ein:src--allow-write-back t)
|
2016-11-10 07:13:16 -06:00
|
|
|
(defvar ein:src--overlay nil)
|
2019-06-01 09:35:50 -05:00
|
|
|
(defvar ein:src--saved-window-config nil)
|
2016-10-25 18:00:51 -05:00
|
|
|
|
2019-10-07 16:27:38 -04:00
|
|
|
(declare-function ein:notebook--get-nb-or-error "ein-notebook" ())
|
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
(defvar ein:edit-cell-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "\C-c'" 'ein:edit-cell-exit)
|
|
|
|
(define-key map "\C-c\C-k" 'ein:edit-cell-abort)
|
2016-10-30 18:37:14 -05:00
|
|
|
(define-key map "\C-c\C-c" 'ein:edit-cell-save-and-execute)
|
2016-10-25 18:00:51 -05:00
|
|
|
(define-key map "\C-x\C-s" 'ein:edit-cell-save)
|
2016-11-05 17:49:52 -05:00
|
|
|
(define-key map "\C-c\C-x" 'ein:edit-cell-view-traceback)
|
2016-10-25 18:00:51 -05:00
|
|
|
map))
|
|
|
|
|
|
|
|
(define-minor-mode ein:edit-cell-mode
|
|
|
|
"Minor mode for language major mode buffers generated by EIN.
|
2016-10-26 14:08:19 -05:00
|
|
|
This minor mode is turned on when editing a source code snippet with \\[ein:edit-cell-contents]
|
2016-10-25 18:00:51 -05:00
|
|
|
|
2016-10-26 14:08:19 -05:00
|
|
|
\\{ein:edit-cell-mode-map}
|
2016-10-25 18:00:51 -05:00
|
|
|
|
2016-10-26 14:08:19 -05:00
|
|
|
."
|
2016-10-25 18:00:51 -05:00
|
|
|
nil " EinCell" nil
|
|
|
|
(set (make-local-variable 'header-line-format)
|
|
|
|
(substitute-command-keys
|
2016-10-26 14:08:19 -05:00
|
|
|
"Edit, execute with \\[ein:edit-cell-execute] then exit with \\[ein:edit-cell-exit] \
|
|
|
|
or abort with \\[ein:edit-cell-abort]"))
|
2016-10-25 18:00:51 -05:00
|
|
|
;; Possibly activate various auto-save features (for the edit buffer
|
|
|
|
;; or the source buffer).
|
|
|
|
;; (when org-edit-src-turn-on-auto-save
|
|
|
|
;; (setq buffer-auto-save-file-name
|
|
|
|
;; (concat (make-temp-name "org-src-")
|
|
|
|
;; (format-time-string "-%Y-%d-%m")
|
|
|
|
;; ".txt")))
|
|
|
|
;; (unless (or org-src--auto-save-timer (zerop org-edit-src-auto-save-idle-delay))
|
|
|
|
;; (setq org-src--auto-save-timer
|
|
|
|
;; (run-with-idle-timer
|
|
|
|
;; org-edit-src-auto-save-idle-delay t
|
|
|
|
;; (lambda ()
|
|
|
|
;; (save-excursion
|
|
|
|
;; (let (edit-flag)
|
|
|
|
;; (dolist (b (buffer-list))
|
|
|
|
;; (with-current-buffer b
|
|
|
|
;; (when (org-src-edit-buffer-p)
|
|
|
|
;; (unless edit-flag (setq edit-flag t))
|
|
|
|
;; (when (buffer-modified-p) (org-edit-src-save)))))
|
|
|
|
;; (unless edit-flag
|
|
|
|
;; (cancel-timer org-src--auto-save-timer)
|
|
|
|
;; (setq org-src--auto-save-timer nil))))))))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defun ein:cell-configure-edit-buffer ()
|
2018-10-15 11:21:05 -04:00
|
|
|
(when (and (bound-and-true-p org-src--from-org-mode) (boundp 'org-src--beg-marker))
|
2016-12-22 12:19:52 -06:00
|
|
|
(add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
|
|
|
|
(if (bound-and-true-p org-src--allow-write-back)
|
2016-10-25 18:00:51 -05:00
|
|
|
(progn
|
|
|
|
(setq buffer-offer-save t)
|
|
|
|
(setq buffer-file-name
|
|
|
|
(concat (buffer-file-name (marker-buffer org-src--beg-marker))
|
|
|
|
"[" (buffer-name) "]"))
|
|
|
|
(setq write-contents-functions '(ein:edit-cell-save)))
|
|
|
|
(setq buffer-read-only t))))
|
|
|
|
|
2016-11-05 17:49:52 -05:00
|
|
|
(defun ein:edit-cell-view-traceback ()
|
|
|
|
"Jump to traceback, if there is one, for current edit."
|
|
|
|
(interactive)
|
2016-11-10 07:13:16 -06:00
|
|
|
(let ((buf (current-buffer))
|
|
|
|
(cell ein:src--cell))
|
|
|
|
(with-current-buffer (ein:worksheet--get-buffer ein:src--ws)
|
|
|
|
(ein:cell-goto cell)
|
|
|
|
(ein:tb-show))))
|
2016-11-05 17:49:52 -05:00
|
|
|
|
2016-10-30 18:37:14 -05:00
|
|
|
(defun ein:edit-cell-save-and-execute ()
|
|
|
|
"Save, then execute the countents of the EIN source edit buffer
|
|
|
|
and place results (if any) in output of original notebook cell."
|
2016-10-26 13:25:52 -05:00
|
|
|
(interactive)
|
|
|
|
(ein:edit-cell-save)
|
2017-06-02 21:05:02 -05:00
|
|
|
(when (and (slot-exists-p ein:src--cell 'kernel)
|
|
|
|
(slot-boundp ein:src--cell 'kernel))
|
|
|
|
(ein:cell-execute-internal ein:src--cell
|
|
|
|
(slot-value ein:src--cell 'kernel)
|
|
|
|
(buffer-string)
|
|
|
|
:silent nil)))
|
2016-10-26 13:25:52 -05:00
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
(defun ein:edit-cell-save ()
|
2016-10-30 18:37:14 -05:00
|
|
|
"Save contents of EIN source edit buffer back to original notebook
|
|
|
|
cell."
|
2016-10-25 18:00:51 -05:00
|
|
|
(interactive)
|
|
|
|
(set-buffer-modified-p nil)
|
2016-11-10 18:20:19 -06:00
|
|
|
(let* ((edited-code (buffer-string))
|
2017-01-28 11:08:26 -06:00
|
|
|
(cell ein:src--cell)
|
2016-11-10 18:20:19 -06:00
|
|
|
(overlay ein:src--overlay)
|
|
|
|
(read-only (overlay-get overlay 'modification-hooks)))
|
|
|
|
(overlay-put overlay 'modification-hooks nil)
|
|
|
|
(overlay-put overlay 'insert-in-front-hooks nil)
|
|
|
|
(overlay-put overlay 'insert-behind-hooks nil)
|
2017-01-28 11:08:26 -06:00
|
|
|
(with-current-buffer (ein:worksheet--get-buffer ein:src--ws)
|
|
|
|
(ein:cell-set-text cell edited-code))
|
|
|
|
;;(setf (slot-value ein:src--cell 'input) edited-code)
|
2016-11-10 18:20:19 -06:00
|
|
|
(overlay-put overlay 'modification-hooks read-only)
|
|
|
|
(overlay-put overlay 'insert-in-front-hooks read-only)
|
|
|
|
(overlay-put overlay 'insert-behind-hooks read-only)))
|
2016-11-10 07:13:16 -06:00
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
|
|
|
|
(defun ein:edit-cell-exit ()
|
2016-10-30 18:37:14 -05:00
|
|
|
"Close the EIN source edit buffer, saving contents back to the
|
|
|
|
original notebook cell, unless being called via
|
|
|
|
`ein:edit-cell-abort'."
|
2016-10-25 18:00:51 -05:00
|
|
|
(interactive)
|
|
|
|
(let ((edit-buffer (current-buffer))
|
2016-11-10 07:13:16 -06:00
|
|
|
(ws ein:src--ws)
|
|
|
|
(cell ein:src--cell))
|
2016-11-10 18:20:19 -06:00
|
|
|
(ein:remove-overlay)
|
2016-10-25 18:00:51 -05:00
|
|
|
(when ein:src--allow-write-back
|
|
|
|
(ein:edit-cell-save))
|
|
|
|
(kill-buffer edit-buffer)
|
2016-11-10 07:13:16 -06:00
|
|
|
(switch-to-buffer-other-window (ein:worksheet--get-buffer ws))
|
2019-06-01 09:35:50 -05:00
|
|
|
(ein:cell-goto cell)
|
|
|
|
(when ein:src--saved-window-config
|
|
|
|
(set-window-configuration ein:src--saved-window-config)
|
|
|
|
(setq ein:src--saved-window-config nil))))
|
2016-10-25 18:00:51 -05:00
|
|
|
|
|
|
|
(defun ein:edit-cell-abort ()
|
2016-10-30 18:37:14 -05:00
|
|
|
"Abort editing the current cell, contents will revert to
|
|
|
|
previous value."
|
2016-10-25 18:00:51 -05:00
|
|
|
(interactive)
|
|
|
|
(let (ein:src--allow-write-back) (ein:edit-cell-exit)))
|
|
|
|
|
2017-04-02 08:59:02 -05:00
|
|
|
(defun ein:construct-cell-edit-buffer-name (bufname cid cell-type)
|
|
|
|
(concat "*EIN Src " bufname "[ " cid "/" cell-type " ]*" ))
|
2016-10-25 18:00:51 -05:00
|
|
|
|
2016-10-28 14:13:32 -05:00
|
|
|
(defun ein:get-mode-for-kernel (kernelspec)
|
2016-12-14 15:55:18 -06:00
|
|
|
(if (null kernelspec)
|
|
|
|
'python ;; FIXME
|
2018-09-26 10:07:50 -04:00
|
|
|
(ein:case-equal (ein:$kernelspec-language kernelspec)
|
2019-02-20 12:23:27 -03:00
|
|
|
(("julia" "python" "R") (intern (ein:$kernelspec-language kernelspec)))
|
2018-09-26 10:07:50 -04:00
|
|
|
(t 'python))))
|
2016-10-28 14:13:32 -05:00
|
|
|
|
2016-11-10 07:13:16 -06:00
|
|
|
(defun ein:edit-src-continue (e)
|
|
|
|
(interactive "e")
|
|
|
|
(mouse-set-point e)
|
|
|
|
(let ((buf (get-char-property (point) 'edit-buffer)))
|
|
|
|
(if buf (org-src-switch-to-buffer buf 'continue)
|
|
|
|
(user-error "No sub-editing buffer for area at point"))))
|
|
|
|
|
|
|
|
(defun ein:make-source-overlay (beg end edit-buffer)
|
|
|
|
"Create overlay between BEG and END positions and return it.
|
|
|
|
EDIT-BUFFER is the buffer currently editing area between BEG and
|
|
|
|
END."
|
|
|
|
(let ((overlay (make-overlay beg end)))
|
|
|
|
(overlay-put overlay 'face 'secondary-selection)
|
|
|
|
(overlay-put overlay 'edit-buffer edit-buffer)
|
|
|
|
(overlay-put overlay 'help-echo
|
|
|
|
"Click with mouse-1 to switch to buffer editing this segment")
|
|
|
|
(overlay-put overlay 'face 'secondary-selection)
|
|
|
|
(overlay-put overlay 'keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map [mouse-1] 'ein:edit-src-continue)
|
|
|
|
map))
|
|
|
|
(let ((read-only
|
2016-11-10 18:20:19 -06:00
|
|
|
(list
|
|
|
|
(lambda (&rest _)
|
|
|
|
(user-error
|
|
|
|
"Cannot modify an area being edited in a dedicated buffer")))))
|
2016-11-10 07:13:16 -06:00
|
|
|
(overlay-put overlay 'modification-hooks read-only)
|
|
|
|
(overlay-put overlay 'insert-in-front-hooks read-only)
|
|
|
|
(overlay-put overlay 'insert-behind-hooks read-only))
|
|
|
|
overlay))
|
|
|
|
|
|
|
|
(defun ein:remove-overlay ()
|
|
|
|
"Remove overlay from current source buffer."
|
|
|
|
(when (overlayp ein:src--overlay) (delete-overlay ein:src--overlay)))
|
|
|
|
|
2017-06-02 21:05:02 -05:00
|
|
|
(defcustom ein:raw-cell-default-edit-mode 'LaTeX-mode
|
|
|
|
"The major mode to use when editing a cell of type 'Raw' in the
|
|
|
|
dedicated edit buffer. By default we use LaTeX-mode."
|
|
|
|
:type 'symbol
|
|
|
|
:group 'ein)
|
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
(defun ein:edit-cell-contents ()
|
2016-10-30 18:37:14 -05:00
|
|
|
"Edit the contents of the current cell in a buffer using an
|
|
|
|
appropriate language major mode. Functionality is very similar to
|
|
|
|
`org-edit-special'."
|
2016-10-25 18:00:51 -05:00
|
|
|
(interactive)
|
2019-06-01 09:35:50 -05:00
|
|
|
(setq ein:src--saved-window-config (current-window-configuration))
|
2016-10-26 13:25:52 -05:00
|
|
|
(let* ((cell (or (ein:worksheet-get-current-cell)
|
2016-10-25 18:00:51 -05:00
|
|
|
(error "Must be called from inside an EIN worksheet cell.")))
|
2016-10-28 14:13:32 -05:00
|
|
|
(nb (ein:notebook--get-nb-or-error))
|
2016-10-26 13:25:52 -05:00
|
|
|
(ws (ein:worksheet--get-ws-or-error))
|
2016-10-25 18:00:51 -05:00
|
|
|
(type (slot-value cell 'cell-type))
|
2017-04-02 08:59:02 -05:00
|
|
|
(name (ein:construct-cell-edit-buffer-name (buffer-name) (ein:cell-id cell) type)))
|
|
|
|
(ein:aif (get-buffer name)
|
|
|
|
(switch-to-buffer-other-window it)
|
|
|
|
(ein:create-edit-cell-buffer name cell nb ws))))
|
|
|
|
|
2017-06-02 21:05:02 -05:00
|
|
|
(defun ein:edit-cell-detect-type (contents notebook &optional raw-cell-p)
|
|
|
|
(if (string-match "^%%\\(.*\\)" contents)
|
|
|
|
(ein:case-equal (match-string 1 contents)
|
|
|
|
(("html" "HTML") (html-mode))
|
|
|
|
(("latex" "LATEX") (LaTeX-mode))
|
|
|
|
(("ruby") (ruby-mode))
|
|
|
|
(("sh" "bash") (sh-mode))
|
|
|
|
(("javascript" "js") (javascript-mode))
|
|
|
|
(t (funcall ein:raw-cell-default-edit-mode)))
|
|
|
|
(if raw-cell-p
|
|
|
|
(funcall ein:raw-cell-default-edit-mode)
|
2019-10-07 16:27:38 -04:00
|
|
|
(cl-case (ein:get-mode-for-kernel (ein:$notebook-kernelspec notebook))
|
Appveyor iterating (WIP) (#573)
* ob-ein: Bring back old functionality.
Bring back some old features to babel edit buffers while trying to respect
recent addition of polymode support.
* Override polymode if the user really wants.
Polymode is really for notebook buffers in any case, but this will override
whatever completion backmode a user has configured for python-mode.
* Install cask using python2.
For now python2 is the easiest option for testing on Windows since cask does
not properly support python3 when in Windows.
* Let's throw in the ert-runner, see what happens.
* Can I use my fork of cask?
Work around smartrep weirdness, try to live without command line wildcard
expansion.
* Get the url for the fork right.
* Experiment with python37, use test_script.
* Unstick appveyor, I hope.
* Fix parsing error.
* test_script is not executing. Why?
* Add ert testing.
But why are the test_script commands not executing?
* tasks: Automate building and testing using invoke.
Invoke leverages Python, which I hope will allow us to abstract out differences
in platforms when it comes to building and testing ein.
* Use invoke on appveyor.
* appveyor: Use the environment python.
So we can test versions other than python 2.7.
* Parsing error.
Is
https://packaging.python.org/guides/supporting-windows-using-appveyor/#setting-up
lying to me?
* Quote commands, just in case.
* Get python onto the path.
* Appveyor is catching up to travis.
* Parsing error.
* Update pip, try to get quoted syntax right.
* Still not liking my pip call.
Last try, next step we go to a requirements.txt file.
* Go to using a requirements file for pip.
* ecukes needs bash to work.
* Cleaning up and fiddling.
Seems like the emacs-jupyter guy has his act together - maybe we can take some
inspiration for our appveyor config.
* Syntax error in environment.
* More syntax errros.
* Maybe we need quoting.
* I give up.
* Formatting and cleanup.
* Add customization, yet another syntax error.
New customizable variable `ob-ein-babel-edit-polymode-ignore' to override
keybinding for \C-c\C-c in an org source code edit buffer.
* John learned some Powershell today.
* Fix the executable path.
Sometimes there is more than one curl installed on the system, make sure we can
account for that in testing.
* Handle updating the path inside invoke.
* Report which curl we are using before starting functional tests.
* Enable RDP so we can see error logs.
* Keep the build alive even when it finishes.
* Fix #568.
Apparently we need to specify the user agent when on windows, otherwise tornado
will start throwing 403 responses. Currently using Mozilla/4.0 as the agent, but
might be a good idea to make this value customizable.
* Clean up emacs config.
* Why is appveyor dropping the xsrf token?
* xsrf cookie found, what does the header look like?
* Try different user-agent header, reenable rdp.
* JSON encoding issues on Python side, it appears. Let's try an older Python.
Login works, contents query to get notebooklist works (i.e. GET on
/contents/api), but creating a notebook (i.e. POST on /contents/api) fails with
invalid JSON. ein and emacs-request appear to be generating the proper json, but
jupyter notebook does not see the same thing that is being written. Could be
bytes vs. text issue with modern v3.x python, so let us see how this all works
with Python 2.7.
* Python27 does not have pathlib out of the box.
* Make amends with Python27
* Back to python37.
Tornado/notebook still isn't reading the POST'ed json correctly.
* Do we need to specify content type?
* Must be selective in specifying application/json content.
* Re-enable rdp.
* Let's try a different curl.
* Ensure most recent curl is on path
* Try a different path.
* Try to warn user if suspicious curl detected.
* Remove debugging statements.
* EVM depends on trusty for 26.x
See issue #125 (https://github.com/rejeep/evm/issues/125). Let's hope I got the
travis.yml syntax right.
* Minimal support for ecukes from invoke.
* Cleaner server shutdowns, better ecukes support from invoke.
Use the /api/shutdown REST API call now to shutdown running server. Also support
more command line options for ecukes from invoke.
* Almost full support of ecukes using invoke.
But! Also disabling integration testing for the time being until I understand
why ecukes fails even though everything else is working.
* Just do integration and functional testing on appveyor.
Better than nothing while I work out what is breaking the integration tests.
2019-07-28 18:20:13 -06:00
|
|
|
(julia (julia-mode))
|
2018-02-25 09:27:29 +09:00
|
|
|
(python (python-mode))
|
|
|
|
(R (R-mode))))))
|
2017-06-02 21:05:02 -05:00
|
|
|
|
2017-04-02 08:59:02 -05:00
|
|
|
(defun ein:create-edit-cell-buffer (name cell notebook worksheet)
|
|
|
|
(let* ((contents (ein:cell-get-text cell))
|
|
|
|
(type (slot-value cell 'cell-type))
|
2016-11-10 07:13:16 -06:00
|
|
|
(buffer (generate-new-buffer-name name))
|
|
|
|
(overlay (ein:make-source-overlay (ein:cell-input-pos-min cell)
|
|
|
|
(ein:cell-input-pos-max cell)
|
|
|
|
buffer)))
|
2016-10-25 18:00:51 -05:00
|
|
|
(switch-to-buffer-other-window buffer)
|
|
|
|
(insert contents)
|
|
|
|
(remove-text-properties (point-min) (point-max)
|
|
|
|
'(display nil invisible nil intangible nil))
|
|
|
|
(set-buffer-modified-p nil)
|
2016-10-28 14:13:32 -05:00
|
|
|
(setq buffer-file-name buffer) ;; Breaks anaconda-mode without this special fix.
|
2017-04-02 08:59:02 -05:00
|
|
|
|
2016-10-25 18:00:51 -05:00
|
|
|
(condition-case e
|
|
|
|
(ein:case-equal type
|
|
|
|
(("markdown") (markdown-mode))
|
2017-06-02 21:05:02 -05:00
|
|
|
(("raw") (ein:edit-cell-detect-type contents notebook t))
|
|
|
|
(("code") (ein:edit-cell-detect-type contents notebook)))
|
2016-10-25 18:00:51 -05:00
|
|
|
(error (message "Language mode `%s' fails with: %S"
|
|
|
|
type (nth 1 e))))
|
2016-11-10 07:13:16 -06:00
|
|
|
(set (make-local-variable 'ein:src--overlay) overlay)
|
2016-10-25 18:00:51 -05:00
|
|
|
(set (make-local-variable 'ein:src--cell) cell)
|
2017-04-02 08:59:02 -05:00
|
|
|
(set (make-local-variable 'ein:src--ws) worksheet)
|
2016-10-25 18:00:51 -05:00
|
|
|
(set (make-local-variable 'ein:src--allow-write-back) t)
|
|
|
|
(ein:edit-cell-mode)))
|
|
|
|
|
|
|
|
(provide 'ein-cell-edit)
|