From fabff694764a8748c5291acba515400c99a7e2c6 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Fri, 11 May 2012 19:00:40 +0200 Subject: [PATCH] Add ein:cell-convert(-inplace) and ein:cell-copy --- ein-cell.el | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ein-cell.el b/ein-cell.el index 6d2e454..b1ba38e 100644 --- a/ein-cell.el +++ b/ein-cell.el @@ -114,6 +114,49 @@ (oset cell :input it)) cell) +(defmethod ein:cell-convert ((cell ein:basecell) type) + (let ((new (ein:cell-class-from-type type))) + ;; copy attributes + (loop for k in '(:read-only :ewoc) + do (oset new k (oref cell k))) + ;; copy input + (oset new :input (ein:cell-get-text cell)) + ;; copy output when the new cell has it + (when (memq :output (oref new :element-names)) + (oset new :outputs (mapcar 'identity (oref cell :outputs)))) + new)) + +(defmethod ein:cell-copy ((cell ein:basecell)) + (ein:cell-convert cell (oref cell :cell-type))) + +(defmethod ein:cell-convert-inplace ((cell ein:basecell) type) + "Convert CELL to TYPE and redraw corresponding ewoc nodes." + (let ((new (ein:cell-convert cell type))) + ;; copy element attribute + (loop for k in (oref new :element-names) + with old-element = (oref cell :element) + with new-element = (oref new :element) + do (plist-put new-element k (plist-get old-element k))) + ;; setting ewoc nodes + (loop for en in (ein:cell-all-element new) + for node = (ewoc-data en) + do (setf (ein:$node-data node) new)) + (let ((inhibit-read-only t)) + ;; delete ewoc nodes that is not copied + (apply + #'ewoc-delete (oref new :ewoc) + (apply + #'append + (loop for name in (oref new :element-names) + unless (memq name (oref cell :element-names)) + collect (let ((ens (ein:cell-element-get name))) + (if (listp ens) ens (list ens)))))) + ;; draw ewoc node + (loop with ewoc = (oref new :ewoc) + for en in (ein:cell-all-element new) + do (ewoc-invalidate ewoc en))) + new)) + ;;; Getter/setter