Refactor commands

This commit is contained in:
Greg Sexton 2014-06-01 17:49:48 +01:00
parent 1b0e818fbe
commit c4bbdfee6e

View File

@ -264,6 +264,8 @@ contains point, or null."
;;; monadic parser combinator ;;; monadic parser combinator
;;; TODO: document parsers
(defun origami-run-parser (parser content) (defun origami-run-parser (parser content)
(funcall parser content)) (funcall parser content))
@ -454,90 +456,71 @@ parser to allow for recursive nesting of a parser."
"Facade. Build the tree if it hasn't already been built "Facade. Build the tree if it hasn't already been built
otherwise fetch cached tree." otherwise fetch cached tree."
;; TODO: caching -- don't parse again if there have been no edits since last time ;; TODO: caching -- don't parse again if there have been no edits since last time
(origami-build-tree buffer (origami-get-parser buffer))) (when origami-mode
(origami-build-tree buffer (origami-get-parser buffer))))
;;; commands ;;; commands
;;; TODO: should ensure that minor mode is enabled ;;; TODO: document all commands
;;; TODO: extract common pattern
;;; TODO: document (defun origami-apply-new-tree (buffer old-tree new-tree)
(when new-tree
(origami-fold-diff old-tree (origami-store-cached-tree buffer new-tree)
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn)))
(defun origami-open-node (buffer point) (defun origami-open-node (buffer point)
(interactive (list (current-buffer) (point))) (interactive (list (current-buffer) (point)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(-when-let (path (origami-fold-find-path-containing tree point)) (-when-let (path (origami-fold-find-path-containing tree point))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-assoc path (lambda (node)
(origami-fold-assoc (origami-fold-open-set node t)))))))
path (lambda (node)
(origami-fold-open-set node t))))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn))))
(defun origami-show-node (buffer point) (defun origami-show-node (buffer point)
"Like `origami-open-node' but opens parent nodes recursively so "Like `origami-open-node' but opens parent nodes recursively so
as to ensure seeing where POINT is." as to ensure seeing where POINT is."
(interactive (list (current-buffer) (point))) (interactive (list (current-buffer) (point)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(-when-let (path (origami-fold-find-path-containing tree point)) (-when-let (path (origami-fold-find-path-containing tree point))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-path-map
(origami-fold-path-map (lambda (node)
(lambda (node) (origami-fold-open-set node t))
(origami-fold-open-set node t)) path)))))
path))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn))))
(defun origami-close-node (buffer point) (defun origami-close-node (buffer point)
(interactive (list (current-buffer) (point))) (interactive (list (current-buffer) (point)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(-when-let (path (origami-fold-find-path-containing tree point)) (-when-let (path (origami-fold-find-path-containing tree point))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-assoc
(origami-fold-assoc path (lambda (node)
path (lambda (node) (origami-fold-open-set node nil)))))))
(origami-fold-open-set node nil))))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn))))
(defun origami-toggle-node (buffer point) (defun origami-toggle-node (buffer point)
(interactive (list (current-buffer) (point))) (interactive (list (current-buffer) (point)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(-when-let (path (origami-fold-find-path-containing tree point)) (-when-let (path (origami-fold-find-path-containing tree point))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-assoc
(origami-fold-assoc path (lambda (node)
path (lambda (node) (origami-fold-open-set
(origami-fold-open-set node (not (origami-fold-open-p
node (not (origami-fold-open-p (-last-item path))))))))))
(-last-item path)))))))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn))))
(defun origami-open-all-nodes (buffer) (defun origami-open-all-nodes (buffer)
(interactive (list (current-buffer))) (interactive (list (current-buffer)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-map
(origami-fold-map (lambda (node)
(lambda (node) (origami-fold-open-set node t))
(origami-fold-open-set node t)) tree))))
tree))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn)))
(defun origami-close-all-nodes (buffer) (defun origami-close-all-nodes (buffer)
(interactive (list (current-buffer))) (interactive (list (current-buffer)))
(let ((tree (origami-get-fold-tree buffer))) (-when-let (tree (origami-get-fold-tree buffer))
(origami-fold-diff tree (origami-store-cached-tree buffer (origami-apply-new-tree buffer tree (origami-fold-map
(origami-fold-map (lambda (node)
(lambda (node) (origami-fold-open-set node nil))
(origami-fold-open-set node nil)) tree))))
tree))
'origami-hide-overlay-from-fold-tree-fn
'origami-show-overlay-from-fold-tree-fn
'origami-change-overlay-from-fold-node-fn)))
(defun origami-show-only-node (buffer point) (defun origami-show-only-node (buffer point)
(interactive (list (current-buffer) (point))) (interactive (list (current-buffer) (point)))