scratch one scratch all

This commit is contained in:
dickmao 2020-01-11 20:52:58 -05:00
parent fbbace4b34
commit f8e6bebbab
2 changed files with 59 additions and 54 deletions

View file

@ -162,12 +162,14 @@ Current buffer for these functions is set to the notebook buffer.")
args))) args)))
(defun ein:notebook-close-worksheet (notebook ws) (defun ein:notebook-close-worksheet (notebook ws)
"Close worksheet WS in NOTEBOOK." "Close worksheet WS in NOTEBOOK.
(symbol-macrolet ((worksheets (ein:$notebook-worksheets notebook))
(scratchsheets (ein:$notebook-scratchsheets notebook))) This is problematic as ein:$notebook-worksheets doesn't delq ws.
And I don't know if I can on account of the dont-save-cells nonsense."
(symbol-macrolet ((scratchsheets (ein:$notebook-scratchsheets notebook)))
(cond (cond
((ein:worksheet-p ws) (ein:worksheet-save-cells ws t)) ((ein:worksheet-p ws) (ein:worksheet-save-cells ws t))
(t (setq scratchsheets (delq ws scratchsheets)))))) (t (setf scratchsheets (delq ws scratchsheets))))))
;;; Notebook utility functions ;;; Notebook utility functions
@ -200,13 +202,13 @@ combo must match exactly these url/port you used format
(ein:$notebook-scratchsheets it))))) (ein:$notebook-scratchsheets it)))))
(defun ein:notebook-buffer-list (notebook) (defun ein:notebook-buffer-list (notebook)
"Return the buffers associated with NOTEBOOK's kernel. "Return the direct and indirect buffers."
The buffer local variable `default-directory' of these buffers (mapcan (lambda (ws)
will be updated with kernel's cwd." (when-let ((ws-buf (ein:worksheet-buffer ws)))
(delete nil (with-current-buffer ws-buf
(mapcar #'ein:worksheet-buffer (mapcar #'buffer-name (eieio-oref pm/polymode '-buffers)))))
(append (ein:$notebook-worksheets notebook) (append (ein:$notebook-worksheets notebook)
(ein:$notebook-scratchsheets notebook))))) (ein:$notebook-scratchsheets notebook))))
(defun ein:notebook--get-nb-or-error () (defun ein:notebook--get-nb-or-error ()
(or ein:%notebook% (error "Not in notebook buffer."))) (or ein:%notebook% (error "Not in notebook buffer.")))
@ -766,26 +768,28 @@ NAME is any non-empty string that does not contain '/' or '\\'.
(defun ein:notebook-kill-buffers (notebook) (defun ein:notebook-kill-buffers (notebook)
"Callback for `ein:notebook-close'" "Callback for `ein:notebook-close'"
(let ((buffers (ein:notebook-buffer-list notebook)))
(mapc (lambda (b) (mapc (lambda (b)
(with-current-buffer b (with-current-buffer b
(aif ein:%worksheet% (aif ein:%worksheet%
(ein:notebook-close-worksheet ein:%notebook% it)) (ein:notebook-close-worksheet ein:%notebook% it))
(aif ein:%notebook% (aif ein:%notebook%
(ein:notebook-tidy-opened-notebooks it)))) (ein:notebook-tidy-opened-notebooks it))))
(ein:notebook-buffer-list notebook)) buffers)
(ein:notebook-avoid-recursion (ein:notebook-avoid-recursion (mapc #'kill-buffer buffers))))
(mapc #'kill-buffer (ein:notebook-buffer-list notebook))))
(defun ein:notebook-kill-buffer-query () (defun ein:notebook-kill-buffer-query ()
(if-let ((notebook (ein:get-notebook)) (if-let ((notebook (ein:get-notebook))
(ws ein:%worksheet%)) (ws ein:%worksheet%))
(prog1 nil
(cond ((ein:scratchsheet-p ws) (cond ((ein:scratchsheet-p ws)
(ein:notebook-close-worksheet notebook ws) (ein:notebook-close-worksheet notebook ws)
t) (with-current-buffer (ein:worksheet-buffer ws)
(ein:notebook-avoid-recursion
(mapc #'kill-buffer (eieio-oref pm/polymode '-buffers)))))
(t (t
(cl-assert (ein:worksheet-p ws)) (cl-assert (ein:worksheet-p ws))
(ein:notebook-close notebook) (ein:notebook-close notebook))))
nil))
t)) t))
(defun ein:notebook-ask-save (notebook &optional callback0) (defun ein:notebook-ask-save (notebook &optional callback0)

View file

@ -10,7 +10,6 @@
(require 'ein-testing-notebook) (require 'ein-testing-notebook)
(require 'ein-testing-cell) (require 'ein-testing-cell)
;; Test utils ;; Test utils
;;; This is the content portion of a response fromt he content API. ;;; This is the content portion of a response fromt he content API.
@ -170,25 +169,22 @@ is not found."
When NUM-OPEN = NUM-CLOSE, notebook should be closed." When NUM-OPEN = NUM-CLOSE, notebook should be closed."
(should (> num-open 0)) (should (> num-open 0))
(let ((notebook (buffer-local-value 'ein:%notebook% (with-current-buffer (ein:testing-notebook-make-empty)
(ein:testing-notebook-make-empty)))) (symbol-macrolet ((ss-list (ein:$notebook-scratchsheets ein:%notebook%)))
(symbol-macrolet ((ss-list (ein:$notebook-scratchsheets notebook)))
;; Add scratchsheets. They can be just empty instance for this test. ;; Add scratchsheets. They can be just empty instance for this test.
(dotimes (_ num-open) (dotimes (_ num-open)
(setq ss-list (ein:notebook-scratchsheet-render-new ein:%notebook%))
(append ss-list (list (make-instance 'ein:scratchsheet))))) (let ((ss (car ss-list)))
;; Close worksheet (kill-buffer (ein:worksheet-buffer ss)))
(let ((ws (car (ein:$notebook-worksheets notebook)))) (should (= (length ss-list) (1- num-open)))
(ein:notebook-close-worksheet notebook ws) (dotimes (_ (1- num-close))
(kill-buffer (ein:worksheet-buffer ws))) (kill-buffer (ein:worksheet-buffer (car ss-list))))
;; Make sure adding scratchsheet work. (should (= (length ss-list) (- num-open num-close)))
(should (= (length ss-list) num-open)) (let ((my-buffer (buffer-name)))
(mapc (lambda (ws) (should (ein:scratchsheet-p ws))) ss-list) (kill-buffer (current-buffer))
;; Close scratchsheets (should-not (seq-some (lambda (b)
(dotimes (_ num-close) (cl-search my-buffer (buffer-name b)))
(ein:notebook-close-worksheet notebook (car ss-list))) (buffer-list)))))))
;; Actual tests:
(should (= (length ss-list) (- num-open num-close))))))
(ert-deftest ein:notebook-close-scratchsheet/open-one-close-one () (ert-deftest ein:notebook-close-scratchsheet/open-one-close-one ()
(ein:testing-notebook-close-scratchsheet-open-and-close 1 1)) (ein:testing-notebook-close-scratchsheet-open-and-close 1 1))
@ -825,14 +821,19 @@ defined."
(defun ein:testin-notebook-close (num-ws num-ss) (defun ein:testin-notebook-close (num-ws num-ss)
(should (= num-ws 1)) ; currently EIN only supports 1 WS (should (= num-ws 1)) ; currently EIN only supports 1 WS
(should (>= num-ss 0)) (should (>= num-ss 0))
(let ((notebook (buffer-local-value 'ein:%notebook% (let* ((buf (ein:testing-notebook-make-empty))
(ein:testing-notebook-make-empty)))) (buffers (with-current-buffer buf
(ein:notebook-buffer-list ein:%notebook%))))
(with-current-buffer buf
(dotimes (_ num-ss) (dotimes (_ num-ss)
(ein:notebook-scratchsheet-render-new notebook)) (ein:notebook-scratchsheet-render-new ein:%notebook%))
(should (= (length (ein:notebook-buffer-list notebook)) (+ num-ws num-ss))) (should (= (+ num-ws num-ss)
(ein:notebook-close notebook) (length (seq-filter
(mapc (lambda (b) (should-not (buffer-live-p b))) (lambda (b) (not (buffer-base-buffer (get-buffer b))))
(ein:notebook-buffer-list notebook)))) (ein:notebook-buffer-list ein:%notebook%))))))
(kill-buffer buf)
(mapc (lambda (b) (should-not (get-buffer b))) buffers)))
(ert-deftest ein:notebook-close/one-ws-no-ss () (ert-deftest ein:notebook-close/one-ws-no-ss ()
(ein:testin-notebook-close 1 0)) (ein:testin-notebook-close 1 0))
@ -1228,7 +1229,7 @@ value of `ein:worksheet-enable-undo'."
(should (ein:worksheet-modified-p ein:%worksheet%)) (should (ein:worksheet-modified-p ein:%worksheet%))
(cl-letf (((symbol-function 'y-or-n-p) (cl-letf (((symbol-function 'y-or-n-p)
(lambda (&rest _args) (setq asked t) nil))) (lambda (&rest _args) (setq asked t) nil)))
(should (kill-buffer buf2))) (should-not (kill-buffer buf2)))
(should-not asked) (should-not asked)
(should-not (buffer-live-p buf2)))) (should-not (buffer-live-p buf2))))
(should-not (ein:worksheet-modified-p ein:%worksheet%)) (should-not (ein:worksheet-modified-p ein:%worksheet%))