mirror of
https://github.com/vale981/emacs-ipython-notebook
synced 2025-03-06 01:21:38 -05:00
Edit notebook cells in popout buffer ala org-mode
Using C-c', users can create a temporary buffer running an appropriate mode to edit the contents of a notebook cell. This functionality is intentionally very similar to what org-mode provides for editing blocks of code or other.
This commit is contained in:
parent
37f8e4d041
commit
c0144e2cbf
4 changed files with 150 additions and 3 deletions
147
lisp/ein-cell-edit.el
Normal file
147
lisp/ein-cell-edit.el
Normal file
|
@ -0,0 +1,147 @@
|
|||
;;; 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:
|
||||
|
||||
(defvar ein:src--cell nil)
|
||||
(defvar ein:src--ws nil)
|
||||
(defvar ein:src--allow-write-back t)
|
||||
|
||||
(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)
|
||||
(define-key map "\C-x\C-s" 'ein:edit-cell-save)
|
||||
map))
|
||||
|
||||
(define-minor-mode ein:edit-cell-mode
|
||||
"Minor mode for language major mode buffers generated by EIN.
|
||||
\\<org-mode-map>
|
||||
This minor mode is turned on when editing a source code snippet with \\[org-edit-special]
|
||||
|
||||
\\{ein:edit-cell-mode-map}
|
||||
|
||||
See also `org-src-mode-hook'."
|
||||
nil " EinCell" nil
|
||||
(set (make-local-variable 'header-line-format)
|
||||
(substitute-command-keys
|
||||
"Edit, then exit with \\[ein:edit-cell-exit] or abort with \
|
||||
\\[ein:edit-cell-abort]"))
|
||||
;; 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 ()
|
||||
(when (org-bound-and-true-p org-src--from-org-mode)
|
||||
(org-add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
|
||||
(if (org-bound-and-true-p org-src--allow-write-back)
|
||||
(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))))
|
||||
|
||||
(defun ein:edit-cell-save ()
|
||||
(interactive)
|
||||
;;(unless (ein:cell-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
|
||||
(set-buffer-modified-p nil)
|
||||
(let ((edited-code (buffer-string)
|
||||
;;(org-src--contents-for-write-back)
|
||||
)
|
||||
;; (beg org-src--beg-marker)
|
||||
;; (end org-src--end-marker)
|
||||
;; (overlay org-src--overlay)
|
||||
)
|
||||
(setf (slot-value ein:src--cell 'input) edited-code)
|
||||
(ein:worksheet-render ein:src--ws)))
|
||||
|
||||
(defun ein:edit-cell-exit ()
|
||||
(interactive)
|
||||
;;(unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
|
||||
(let ((edit-buffer (current-buffer))
|
||||
(ws ein:src--ws))
|
||||
(switch-to-buffer (ein:worksheet-buffer ws))
|
||||
(when ein:src--allow-write-back
|
||||
(ein:edit-cell-save))
|
||||
(kill-buffer edit-buffer)
|
||||
;; FIXME: put point at an appropriate location as org does?
|
||||
))
|
||||
|
||||
(defun ein:edit-cell-abort ()
|
||||
(interactive)
|
||||
(let (ein:src--allow-write-back) (ein:edit-cell-exit)))
|
||||
|
||||
(defun ein:construct-cell-edit-buffer-name (bufname cell-type)
|
||||
(concat "*EIN Src " bufname "[ " cell-type " ]*" ))
|
||||
|
||||
(defun ein:edit-cell-contents ()
|
||||
(interactive)
|
||||
(let* ((cell (or (ein:get-cell-at-point)
|
||||
(error "Must be called from inside an EIN worksheet cell.")))
|
||||
(ws ein:%worksheet%)
|
||||
(contents (slot-value cell 'input))
|
||||
(type (slot-value cell 'cell-type))
|
||||
(name (ein:construct-cell-edit-buffer-name (buffer-name) type))
|
||||
(buffer (generate-new-buffer-name name)))
|
||||
(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)
|
||||
(setq buffer-file-name nil)
|
||||
(condition-case e
|
||||
(ein:case-equal type
|
||||
(("markdown") (markdown-mode))
|
||||
(("code") (python-mode)))
|
||||
(error (message "Language mode `%s' fails with: %S"
|
||||
type (nth 1 e))))
|
||||
(set (make-local-variable 'ein:src--cell) cell)
|
||||
(set (make-local-variable 'ein:src--ws) ws)
|
||||
(set (make-local-variable 'ein:src--allow-write-back) t)
|
||||
(ein:edit-cell-mode)))
|
||||
|
||||
(provide 'ein-cell-edit)
|
|
@ -44,6 +44,7 @@
|
|||
(require 'ein-kernel)
|
||||
(require 'ein-kernelinfo)
|
||||
(require 'ein-cell)
|
||||
(require 'ein-cell-edit)
|
||||
(require 'ein-cell-output)
|
||||
(require 'ein-worksheet)
|
||||
(require 'ein-scratchsheet)
|
||||
|
@ -1385,6 +1386,7 @@ This hook is run regardless the actual major mode used."
|
|||
(defvar ein:notebook-mode-map (make-sparse-keymap))
|
||||
|
||||
(let ((map ein:notebook-mode-map))
|
||||
(define-key map "\C-c'" 'ein:edit-cell-contents)
|
||||
(define-key map "\C-cS" 'ein:worksheet-toggle-slideshow-view)
|
||||
(define-key map "\C-c\C-c" 'ein:worksheet-execute-cell)
|
||||
(define-key map (kbd "M-RET") 'ein:worksheet-execute-cell-and-goto-next)
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
|
||||
;;; Code:
|
||||
|
||||
|
||||
(eval-when-compile (require 'cl))
|
||||
(require 'eieio)
|
||||
(require 'ewoc)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
;;; ob-ein.el --- org-babel functions for template evaluation
|
||||
|
||||
;; Copyright (C) John M. miller (
|
||||
;; Copyright (C) John M. Miller
|
||||
|
||||
;; Author: John M. Miller <millejoh at mac.com>
|
||||
;;
|
||||
|
@ -29,7 +29,6 @@
|
|||
;;; Code:
|
||||
(require 'ob)
|
||||
(require 'ob-python)
|
||||
;; possibly require modes required for your language
|
||||
(require 'cl)
|
||||
(require 'ein-notebook)
|
||||
(require 'ein-shared-output)
|
||||
|
|
Loading…
Add table
Reference in a new issue