2014-03-18 06:23:46 +11:00
|
|
|
;;; origami.el --- Flexible text folding -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Author: Greg Sexton <gregsexton@gmail.com>
|
|
|
|
;; Version: 1.0
|
|
|
|
;; Keywords: folding
|
2015-04-04 14:04:13 +11:00
|
|
|
;; URL: https://github.com/gregsexton/origami.el
|
2014-03-20 08:45:52 +11:00
|
|
|
;; Package-Requires: ((s "1.9.0") (dash "2.5.0") (emacs "24"))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
|
|
|
;; The MIT License (MIT)
|
|
|
|
|
|
|
|
;; Copyright (c) 2014 Greg Sexton
|
|
|
|
|
|
|
|
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
;; of this software and associated documentation files (the "Software"), to deal
|
|
|
|
;; in the Software without restriction, including without limitation the rights
|
|
|
|
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
;; copies of the Software, and to permit persons to whom the Software is
|
|
|
|
;; furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
;; The above copyright notice and this permission notice shall be included in
|
|
|
|
;; all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
;; THE SOFTWARE.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2014-03-20 08:45:52 +11:00
|
|
|
(require 'dash)
|
|
|
|
(require 's)
|
2014-06-02 02:37:02 +10:00
|
|
|
(require 'cl)
|
2014-08-09 23:02:37 +10:00
|
|
|
(require 'origami-parsers)
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-05-06 03:14:36 +10:00
|
|
|
;;; overlay manipulation
|
|
|
|
|
2014-08-11 01:14:14 +10:00
|
|
|
(defun origami-create-overlay (beg end offset buffer)
|
2014-05-06 03:14:36 +10:00
|
|
|
(when (> (- end beg) 0)
|
2014-11-22 21:26:26 +11:00
|
|
|
(let ((ov (make-overlay (+ beg offset) end buffer)))
|
|
|
|
(overlay-put ov 'isearch-open-invisible 'origami-isearch-show)
|
|
|
|
(overlay-put ov 'isearch-open-invisible-temporary
|
|
|
|
(lambda (ov hide-p) (if hide-p (origami-hide-overlay ov)
|
|
|
|
(origami-show-overlay ov))))
|
|
|
|
ov)))
|
|
|
|
|
|
|
|
(defun origami-hide-overlay (ov)
|
|
|
|
;; TODO: make all of this customizable
|
|
|
|
(overlay-put ov 'invisible 'origami)
|
|
|
|
(overlay-put ov 'display "...")
|
|
|
|
(overlay-put ov 'face 'font-lock-comment-delimiter-face))
|
|
|
|
|
|
|
|
(defun origami-show-overlay (ov)
|
|
|
|
(overlay-put ov 'invisible nil)
|
|
|
|
(overlay-put ov 'display nil)
|
|
|
|
(overlay-put ov 'face nil))
|
2014-05-06 06:34:50 +10:00
|
|
|
|
|
|
|
(defun origami-hide-node-overlay (node)
|
|
|
|
(-when-let (ov (origami-fold-data node))
|
2014-11-22 21:26:26 +11:00
|
|
|
(origami-hide-overlay ov)))
|
2014-05-06 06:34:50 +10:00
|
|
|
|
|
|
|
(defun origami-show-node-overlay (node)
|
|
|
|
(-when-let (ov (origami-fold-data node))
|
2014-11-22 21:26:26 +11:00
|
|
|
(origami-show-overlay ov)))
|
|
|
|
|
|
|
|
(defun origami-isearch-show (ov)
|
|
|
|
(origami-show-node (current-buffer) (point)))
|
2014-05-06 06:34:50 +10:00
|
|
|
|
|
|
|
(defun origami-hide-overlay-from-fold-tree-fn (node)
|
|
|
|
(origami-fold-postorder-each node 'origami-hide-node-overlay))
|
|
|
|
|
|
|
|
(defun origami-show-overlay-from-fold-tree-fn (node)
|
|
|
|
(origami-fold-postorder-each node 'origami-show-node-overlay))
|
|
|
|
|
|
|
|
(defun origami-change-overlay-from-fold-node-fn (old new)
|
2014-08-09 23:02:37 +10:00
|
|
|
(if (origami-fold-open? new)
|
2014-05-06 06:34:50 +10:00
|
|
|
(origami-show-node-overlay old)
|
|
|
|
(origami-hide-node-overlay new)))
|
2014-05-06 03:14:36 +10:00
|
|
|
|
|
|
|
(defun origami-remove-all-overlays (buffer)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(remove-overlays (point-min) (point-max) 'invisible 'origami)))
|
|
|
|
|
2014-03-18 06:23:46 +11:00
|
|
|
;;; fold structure
|
|
|
|
|
2014-08-11 01:14:14 +10:00
|
|
|
(defun origami-fold-node (beg end offset open &optional children data)
|
2014-04-19 20:43:47 +10:00
|
|
|
(let ((sorted-children (-sort (lambda (a b)
|
|
|
|
(or (< (origami-fold-beg a) (origami-fold-beg b))
|
|
|
|
(and (= (origami-fold-beg a) (origami-fold-beg b))
|
|
|
|
(< (origami-fold-end a) (origami-fold-end b)))))
|
2014-04-19 23:51:07 +10:00
|
|
|
(remove nil children))))
|
2014-04-19 20:43:47 +10:00
|
|
|
;; ensure invariant: no children overlap
|
|
|
|
(when (-some? (lambda (pair)
|
|
|
|
(let ((a (car pair))
|
|
|
|
(b (cadr pair)))
|
|
|
|
(when b ;for the odd numbered case - there may be a single item
|
|
|
|
;; the < function doesn't support varargs
|
|
|
|
(or (>= (origami-fold-beg a) (origami-fold-end a))
|
|
|
|
(>= (origami-fold-end a) (origami-fold-beg b))
|
|
|
|
(>= (origami-fold-beg b) (origami-fold-end b))))))
|
|
|
|
(-partition-all-in-steps 2 1 sorted-children))
|
|
|
|
(error "Tried to construct a node where the children overlap or are not distinct regions: %s"
|
|
|
|
sorted-children))
|
|
|
|
;; ensure invariant: parent encompases children
|
|
|
|
(let ((beg-children (origami-fold-beg (car sorted-children)))
|
|
|
|
(end-children (origami-fold-end (-last-item sorted-children))))
|
|
|
|
(if (and beg-children (or (> beg beg-children) (< end end-children)))
|
|
|
|
(error "Node does not overlap children in range. beg=%s end=%s beg-children=%s end-children=%s"
|
2014-05-06 06:34:50 +10:00
|
|
|
beg end beg-children end-children)
|
2014-08-11 01:14:14 +10:00
|
|
|
(if (> (+ beg offset) end)
|
|
|
|
(error "Offset is not within the range of the node: beg=%s end=%s offset=%s" beg end offset)
|
|
|
|
(vector beg end offset open sorted-children data))))))
|
2014-04-19 20:43:47 +10:00
|
|
|
|
2014-04-22 05:21:05 +10:00
|
|
|
(defun origami-fold-root-node (&optional children)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Create a root container node."
|
|
|
|
(origami-fold-node 1 most-positive-fixnum 0 t children 'root))
|
2014-04-22 05:21:05 +10:00
|
|
|
|
|
|
|
(defun origami-fold-is-root-node? (node) (eq (origami-fold-data node) 'root))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-05-06 06:34:50 +10:00
|
|
|
(defun origami-fold-beg (node)
|
|
|
|
(when node
|
|
|
|
(if (origami-fold-is-root-node? node)
|
|
|
|
(aref node 0)
|
2014-08-11 01:14:14 +10:00
|
|
|
(- (overlay-start (origami-fold-data node)) (origami-fold-offset node)))))
|
2014-03-18 07:25:10 +11:00
|
|
|
|
2014-05-06 06:34:50 +10:00
|
|
|
(defun origami-fold-end (node)
|
|
|
|
(when node
|
|
|
|
(if (origami-fold-is-root-node? node)
|
|
|
|
(aref node 1)
|
2014-11-16 08:15:28 +11:00
|
|
|
(overlay-end (origami-fold-data node)))))
|
2014-03-18 07:25:10 +11:00
|
|
|
|
2014-08-11 01:14:14 +10:00
|
|
|
(defun origami-fold-offset (node) (when node (aref node 2)))
|
|
|
|
|
|
|
|
(defun origami-fold-open? (node) (when node (aref node 3)))
|
2014-03-18 07:25:10 +11:00
|
|
|
|
2014-04-24 05:21:56 +10:00
|
|
|
(defun origami-fold-open-set (node value)
|
|
|
|
(when node
|
|
|
|
(if (origami-fold-is-root-node? node)
|
|
|
|
node
|
2014-08-11 00:29:44 +10:00
|
|
|
(origami-fold-node (origami-fold-beg node)
|
|
|
|
(origami-fold-end node)
|
2014-08-11 01:14:14 +10:00
|
|
|
(origami-fold-offset node)
|
2014-08-11 00:29:44 +10:00
|
|
|
value
|
|
|
|
(origami-fold-children node)
|
|
|
|
(origami-fold-data node)))))
|
2014-04-22 05:21:05 +10:00
|
|
|
|
2014-08-11 01:14:14 +10:00
|
|
|
(defun origami-fold-children (node) (when node (aref node 4)))
|
2014-04-19 21:36:49 +10:00
|
|
|
|
2014-04-22 05:21:05 +10:00
|
|
|
(defun origami-fold-children-set (node children)
|
2014-04-19 20:43:47 +10:00
|
|
|
(when node
|
2014-08-11 00:29:44 +10:00
|
|
|
(origami-fold-node (origami-fold-beg node)
|
|
|
|
(origami-fold-end node)
|
2014-08-11 01:14:14 +10:00
|
|
|
(origami-fold-offset node)
|
2014-08-11 00:29:44 +10:00
|
|
|
(origami-fold-open? node)
|
|
|
|
children
|
|
|
|
(origami-fold-data node))))
|
2014-05-06 06:34:50 +10:00
|
|
|
|
2014-08-11 01:14:14 +10:00
|
|
|
(defun origami-fold-data (node) (when node (aref node 5)))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-08-17 03:23:04 +10:00
|
|
|
;;; fold structure utils
|
|
|
|
|
2014-03-18 07:23:57 +11:00
|
|
|
(defun origami-fold-range-equal (a b)
|
2014-03-18 06:23:46 +11:00
|
|
|
(and (equal (origami-fold-beg a) (origami-fold-beg b))
|
2014-03-18 07:23:57 +11:00
|
|
|
(equal (origami-fold-end a) (origami-fold-end b))))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-03-18 07:23:57 +11:00
|
|
|
(defun origami-fold-state-equal (a b)
|
2014-08-09 23:02:37 +10:00
|
|
|
(equal (origami-fold-open? a) (origami-fold-open? b)))
|
2014-03-18 07:23:57 +11:00
|
|
|
|
2015-04-09 06:31:14 +10:00
|
|
|
(defun origami-fold-add-child (node new)
|
|
|
|
(origami-fold-children-set node
|
|
|
|
(cons new (origami-fold-children node))))
|
|
|
|
|
2014-04-19 20:43:47 +10:00
|
|
|
(defun origami-fold-replace-child (node old new)
|
2014-04-22 05:21:05 +10:00
|
|
|
(origami-fold-children-set node
|
|
|
|
(cons new (remove old (origami-fold-children node)))))
|
2014-04-19 20:43:47 +10:00
|
|
|
|
2014-04-24 05:21:56 +10:00
|
|
|
(defun origami-fold-assoc (path f)
|
2014-04-22 06:32:04 +10:00
|
|
|
"Rewrite the tree, replacing the node referenced by PATH with
|
2014-04-24 05:21:56 +10:00
|
|
|
F applied to the leaf."
|
2014-04-19 20:43:47 +10:00
|
|
|
(cdr
|
|
|
|
(-reduce-r-from (lambda (node acc)
|
|
|
|
(destructuring-bind (old-node . new-node) acc
|
|
|
|
(cons node (origami-fold-replace-child node old-node new-node))))
|
2014-04-24 05:21:56 +10:00
|
|
|
(let ((leaf (-last-item path))) (cons leaf (funcall f leaf)))
|
2014-04-19 20:43:47 +10:00
|
|
|
(butlast path))))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
2014-03-18 07:23:57 +11:00
|
|
|
(defun origami-fold-diff (old new on-add on-remove on-change)
|
|
|
|
(cl-labels ((diff-children (old-children new-children)
|
|
|
|
(let ((old (car old-children))
|
|
|
|
(new (car new-children)))
|
|
|
|
(cond ((null old) (-each new-children on-add))
|
|
|
|
((null new) (-each old-children on-remove))
|
|
|
|
((and (null old) (null new)) nil)
|
|
|
|
((origami-fold-range-equal old new)
|
|
|
|
(origami-fold-diff old new on-add on-remove on-change)
|
|
|
|
(diff-children (cdr old-children) (cdr new-children)))
|
|
|
|
((<= (origami-fold-beg old) (origami-fold-beg new))
|
|
|
|
(funcall on-remove old)
|
|
|
|
(diff-children (cdr old-children) new-children))
|
|
|
|
(t (funcall on-add new)
|
|
|
|
(diff-children old-children (cdr new-children)))))))
|
|
|
|
(unless (origami-fold-range-equal old new)
|
|
|
|
(error "Precondition invalid: old must have the same range as new."))
|
|
|
|
(unless (origami-fold-state-equal old new)
|
|
|
|
(funcall on-change old new))
|
|
|
|
(diff-children (origami-fold-children old)
|
|
|
|
(origami-fold-children new))))
|
|
|
|
|
2014-03-18 06:23:46 +11:00
|
|
|
(defun origami-fold-postorder-each (node f)
|
|
|
|
(-each (origami-fold-children node) f)
|
|
|
|
(funcall f node))
|
|
|
|
|
2014-04-22 05:21:05 +10:00
|
|
|
(defun origami-fold-map (f tree)
|
|
|
|
"Map F over the tree. Replacing each node with the result of (f
|
|
|
|
node). The children cannot be manipulated using f as the map will
|
|
|
|
replace them. This cannot change the structure of the tree, just
|
|
|
|
the state of each node."
|
|
|
|
(origami-fold-children-set
|
|
|
|
(funcall f tree)
|
|
|
|
(-map (lambda (node) (origami-fold-map f node))
|
|
|
|
(origami-fold-children tree))))
|
|
|
|
|
2014-04-22 06:32:04 +10:00
|
|
|
(defun origami-fold-path-map (f path)
|
|
|
|
"Map F over the nodes in path. As with `origami-fold-map',
|
|
|
|
children cannot be manipulated."
|
|
|
|
(cond ((null path) nil)
|
|
|
|
((cdr path) (funcall f (origami-fold-replace-child (car path)
|
|
|
|
(cadr path)
|
|
|
|
(origami-fold-path-map f (cdr path)))))
|
|
|
|
(t (funcall f (car path)))))
|
|
|
|
|
2014-04-19 23:51:07 +10:00
|
|
|
(defun origami-fold-find-deepest (tree pred)
|
|
|
|
(when tree
|
|
|
|
(when (funcall pred tree)
|
|
|
|
(-if-let (child (-first pred (origami-fold-children tree)))
|
|
|
|
(cons tree (origami-fold-find-deepest child pred))
|
|
|
|
(list tree)))))
|
|
|
|
|
2015-04-09 06:31:14 +10:00
|
|
|
(defun origami-fold-find-path-containing-range (tree beg end)
|
|
|
|
(origami-fold-find-deepest tree
|
|
|
|
(lambda (node)
|
|
|
|
(and (>= beg (origami-fold-beg node))
|
|
|
|
(<= end (origami-fold-end node))))))
|
|
|
|
|
2014-04-20 00:04:42 +10:00
|
|
|
(defun origami-fold-find-path-with-range (tree beg end)
|
2014-04-19 23:51:07 +10:00
|
|
|
"Return the path to the most specific (deepest) node that has
|
|
|
|
exactly the range BEG-END, or null."
|
2015-04-09 06:31:14 +10:00
|
|
|
(-when-let (path (origami-fold-find-path-containing-range tree beg end))
|
2014-04-19 23:51:07 +10:00
|
|
|
(let ((last (-last-item path)))
|
|
|
|
(when (and (= beg (origami-fold-beg last))
|
|
|
|
(= end (origami-fold-end last)))
|
|
|
|
path))))
|
|
|
|
|
2014-04-20 00:04:42 +10:00
|
|
|
(defun origami-fold-find-path-containing (tree point)
|
2014-04-19 21:36:49 +10:00
|
|
|
"Return the path to the most specific (deepest) node that
|
|
|
|
contains point, or null."
|
2014-04-19 23:51:07 +10:00
|
|
|
(origami-fold-find-deepest tree
|
|
|
|
(lambda (node)
|
|
|
|
(and (<= (origami-fold-beg node) point)
|
|
|
|
(>= (origami-fold-end node) point)))))
|
2014-04-19 21:36:49 +10:00
|
|
|
|
2014-06-02 06:39:08 +10:00
|
|
|
(defun origami-fold-preorder-reduce (tree f initial-state)
|
|
|
|
"Reduce the tree by doing a preorder traversal. F is applied
|
|
|
|
with the current state and the current node at each iteration."
|
|
|
|
(-reduce-from (lambda (state node) (origami-fold-preorder-reduce node f state))
|
|
|
|
(funcall f initial-state tree)
|
|
|
|
(origami-fold-children tree)))
|
|
|
|
|
|
|
|
(defun origami-fold-postorder-reduce (tree f initial-state)
|
|
|
|
"Reduce the tree by doing a postorder traversal. F is applied
|
|
|
|
with the current state and the current node at each iteration."
|
|
|
|
(funcall f (-reduce-from (lambda (state node) (origami-fold-postorder-reduce node f state))
|
|
|
|
initial-state
|
|
|
|
(origami-fold-children tree))
|
|
|
|
tree))
|
|
|
|
|
2014-08-15 07:06:48 +10:00
|
|
|
(defun origami-fold-node-recursively-closed? (node)
|
|
|
|
(origami-fold-postorder-reduce node (lambda (acc node)
|
|
|
|
(and acc (not (origami-fold-open? node)))) t))
|
|
|
|
|
|
|
|
(defun origami-fold-node-recursively-open? (node)
|
|
|
|
(origami-fold-postorder-reduce node (lambda (acc node)
|
|
|
|
(and acc (origami-fold-open? node))) t))
|
|
|
|
|
2015-04-09 06:31:14 +10:00
|
|
|
(defun origami-fold-shallow-merge (tree1 tree2)
|
|
|
|
"Shallow merge the children of TREE2 in to TREE1."
|
|
|
|
(-reduce-from (lambda (tree node)
|
|
|
|
(origami-fold-assoc (origami-fold-find-path-containing-range tree
|
|
|
|
(origami-fold-beg node)
|
|
|
|
(origami-fold-end node))
|
|
|
|
(lambda (leaf)
|
|
|
|
(origami-fold-add-child leaf node))))
|
|
|
|
tree1 (origami-fold-children tree2)))
|
|
|
|
|
2014-08-17 04:01:25 +10:00
|
|
|
;;; linear history structure
|
|
|
|
|
|
|
|
(defun origami-h-new (present)
|
|
|
|
"Create a new history structure."
|
|
|
|
(vector nil present nil))
|
|
|
|
|
|
|
|
(defun origami-h-push (h new)
|
|
|
|
"Create a new history structure with new as the present value."
|
2014-08-17 04:07:20 +10:00
|
|
|
(when new
|
|
|
|
(let ((past (aref h 0))
|
|
|
|
(present (aref h 1)))
|
|
|
|
(vector (cons present (-take 19 past)) new nil))))
|
2014-08-17 04:01:25 +10:00
|
|
|
|
|
|
|
(defun origami-h-undo (h)
|
|
|
|
(let ((past (aref h 0))
|
|
|
|
(present (aref h 1))
|
|
|
|
(future (aref h 2)))
|
2014-08-17 04:07:20 +10:00
|
|
|
(if (null past) h
|
|
|
|
(vector (cdr past) (car past) (cons present future)))))
|
2014-08-17 04:01:25 +10:00
|
|
|
|
|
|
|
(defun origami-h-redo (h)
|
|
|
|
(let ((past (aref h 0))
|
|
|
|
(present (aref h 1))
|
|
|
|
(future (aref h 2)))
|
2014-08-17 04:07:20 +10:00
|
|
|
(if (null future) h
|
|
|
|
(vector (cons present past) (car future) (cdr future)))))
|
2014-08-17 04:01:25 +10:00
|
|
|
|
|
|
|
(defun origami-h-present (h)
|
|
|
|
(when h (aref h 1)))
|
|
|
|
|
2014-04-14 06:08:02 +10:00
|
|
|
;;; interactive utils
|
|
|
|
|
2014-08-17 01:31:37 +10:00
|
|
|
(defun origami-setup-local-vars (buffer)
|
|
|
|
(with-current-buffer buffer
|
2014-08-17 04:01:25 +10:00
|
|
|
(set (make-local-variable 'origami-history)
|
|
|
|
(origami-h-new (origami-fold-root-node)))
|
2014-08-17 01:31:37 +10:00
|
|
|
(set (make-local-variable 'origami-tree-tick) 0)))
|
2014-06-03 05:52:39 +10:00
|
|
|
|
2014-04-14 06:08:02 +10:00
|
|
|
(defun origami-get-cached-tree (buffer)
|
2014-08-17 04:01:25 +10:00
|
|
|
(or (local-variable-p 'origami-history buffer)
|
2014-08-17 03:23:04 +10:00
|
|
|
(error "Necessary local variables were not available"))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-h-present (buffer-local-value 'origami-history buffer)))
|
2014-08-17 03:23:04 +10:00
|
|
|
|
2014-08-17 04:01:25 +10:00
|
|
|
(defun origami-store-cached-tree (buffer tree)
|
|
|
|
(or (and (local-variable-p 'origami-history buffer)
|
|
|
|
(local-variable-p 'origami-tree-tick buffer))
|
2014-08-17 03:23:04 +10:00
|
|
|
(error "Necessary local variables were not available"))
|
|
|
|
(with-current-buffer buffer
|
2014-08-17 04:01:25 +10:00
|
|
|
(setq origami-tree-tick (buffer-modified-tick))
|
|
|
|
(setq origami-history (origami-h-push origami-history tree)))
|
|
|
|
tree)
|
2014-08-17 03:23:04 +10:00
|
|
|
|
2014-08-17 04:01:25 +10:00
|
|
|
(defun origami-update-history (buffer f)
|
|
|
|
(or (local-variable-p 'origami-history buffer)
|
2014-06-03 05:52:39 +10:00
|
|
|
(error "Necessary local variables were not available"))
|
|
|
|
(with-current-buffer buffer
|
2014-08-17 04:01:25 +10:00
|
|
|
(setq origami-history (funcall f origami-history))))
|
2014-06-03 05:52:39 +10:00
|
|
|
|
|
|
|
(defun origami-rebuild-tree? (buffer)
|
|
|
|
"Determines if the tree needs to be rebuilt for BUFFER since it
|
|
|
|
was last built."
|
|
|
|
(not (= (buffer-local-value 'origami-tree-tick buffer)
|
|
|
|
(buffer-modified-tick buffer))))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
|
|
|
(defun origami-build-tree (buffer parser)
|
|
|
|
(when parser
|
|
|
|
(with-current-buffer buffer
|
2015-04-09 06:31:14 +10:00
|
|
|
(let ((contents (buffer-string)))
|
2014-04-19 21:36:49 +10:00
|
|
|
(-> parser
|
2015-04-09 06:31:14 +10:00
|
|
|
(funcall contents)
|
|
|
|
origami-fold-root-node)))))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
2014-05-06 06:34:50 +10:00
|
|
|
(defun origami-get-parser (buffer)
|
2014-08-11 00:29:44 +10:00
|
|
|
(let* ((cached-tree (origami-get-cached-tree buffer))
|
2014-08-11 01:14:14 +10:00
|
|
|
(create (lambda (beg end offset children)
|
2014-08-11 00:29:44 +10:00
|
|
|
(let ((previous-fold (-last-item (origami-fold-find-path-with-range cached-tree beg end))))
|
2014-08-11 01:14:14 +10:00
|
|
|
(origami-fold-node beg end offset
|
2014-08-11 00:29:44 +10:00
|
|
|
(if previous-fold (origami-fold-open? previous-fold) t)
|
|
|
|
children
|
|
|
|
(or (-> (origami-fold-find-path-with-range
|
|
|
|
(origami-get-cached-tree buffer) beg end)
|
2015-04-26 23:25:41 +10:00
|
|
|
-last-item
|
|
|
|
origami-fold-data)
|
2014-08-11 01:14:14 +10:00
|
|
|
(origami-create-overlay beg end offset buffer)))))))
|
2015-08-14 00:48:10 +10:00
|
|
|
(-when-let (parser-gen (or (cdr (assoc (if (local-variable-p 'origami-fold-style)
|
|
|
|
(buffer-local-value 'origami-fold-style buffer)
|
|
|
|
(buffer-local-value 'major-mode buffer))
|
2015-04-26 23:25:41 +10:00
|
|
|
origami-parser-alist))
|
|
|
|
'origami-indent-parser))
|
2014-08-09 23:02:37 +10:00
|
|
|
(funcall parser-gen create))))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
|
|
|
(defun origami-get-fold-tree (buffer)
|
|
|
|
"Facade. Build the tree if it hasn't already been built
|
|
|
|
otherwise fetch cached tree."
|
2014-06-02 02:49:48 +10:00
|
|
|
(when origami-mode
|
2014-06-03 05:52:39 +10:00
|
|
|
(if (origami-rebuild-tree? buffer)
|
|
|
|
(origami-build-tree buffer (origami-get-parser buffer))
|
|
|
|
(origami-get-cached-tree buffer))))
|
2014-03-20 08:45:52 +11:00
|
|
|
|
2014-06-02 02:49:48 +10:00
|
|
|
(defun origami-apply-new-tree (buffer old-tree new-tree)
|
|
|
|
(when new-tree
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-fold-diff old-tree new-tree
|
2014-06-02 02:49:48 +10:00
|
|
|
'origami-hide-overlay-from-fold-tree-fn
|
|
|
|
'origami-show-overlay-from-fold-tree-fn
|
|
|
|
'origami-change-overlay-from-fold-node-fn)))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
2014-08-15 07:06:48 +10:00
|
|
|
(defun origami-search-forward-for-path (buffer point)
|
|
|
|
(let (end)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(save-excursion
|
|
|
|
(goto-char point)
|
|
|
|
(setq end (line-end-position))))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
|
|
|
(let ((forward-node (-first (lambda (node)
|
|
|
|
(and (>= (origami-fold-beg node) point)
|
|
|
|
(<= (origami-fold-beg node) end)))
|
|
|
|
(origami-fold-children (-last-item path)))))
|
|
|
|
(if forward-node (append path (list forward-node)) path))))))
|
|
|
|
|
2014-08-07 07:00:01 +10:00
|
|
|
;;; commands
|
|
|
|
|
2014-04-14 06:08:02 +10:00
|
|
|
(defun origami-open-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Open the fold node at POINT in BUFFER. The fold node opened
|
|
|
|
will be the deepest nested at POINT."
|
2014-04-14 06:08:02 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-04-20 00:04:42 +10:00
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc path (lambda (node)
|
|
|
|
(origami-fold-open-set node t))))))))
|
2014-04-24 05:21:56 +10:00
|
|
|
|
2014-06-03 05:19:43 +10:00
|
|
|
(defun origami-open-node-recursively (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Open the fold node and all of its children at POINT in BUFFER.
|
|
|
|
The fold node opened will be the deepest nested at POINT."
|
2014-06-03 05:19:43 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
|
|
|
(origami-apply-new-tree
|
2014-08-17 04:01:25 +10:00
|
|
|
buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc path
|
|
|
|
(lambda (node)
|
|
|
|
(origami-fold-map (lambda (node)
|
|
|
|
(origami-fold-open-set node t))
|
|
|
|
node))))))))
|
2014-06-03 05:19:43 +10:00
|
|
|
|
2014-04-24 05:21:56 +10:00
|
|
|
(defun origami-show-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Like `origami-open-node' but also opens parent fold nodes
|
|
|
|
recursively so as to ensure the position where POINT is is
|
|
|
|
visible."
|
2014-04-24 05:21:56 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-04-24 05:21:56 +10:00
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-path-map
|
|
|
|
(lambda (node)
|
|
|
|
(origami-fold-open-set node t))
|
|
|
|
path))))))
|
2014-04-19 23:51:07 +10:00
|
|
|
|
|
|
|
(defun origami-close-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Close the fold node at POINT in BUFFER. The fold node closed
|
|
|
|
will be the deepest nested at POINT."
|
2014-04-14 06:08:02 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-04-20 00:04:42 +10:00
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc
|
|
|
|
path (lambda (node)
|
|
|
|
(origami-fold-open-set node nil))))))))
|
2014-04-14 06:08:02 +10:00
|
|
|
|
2014-06-03 05:19:43 +10:00
|
|
|
(defun origami-close-node-recursively (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Close the fold node and all of its children at POINT in BUFFER.
|
|
|
|
The fold node closed will be the deepest nested at POINT."
|
2014-06-03 05:19:43 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
|
|
|
(origami-apply-new-tree
|
2014-08-17 04:01:25 +10:00
|
|
|
buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc path
|
|
|
|
(lambda (node)
|
|
|
|
(origami-fold-map (lambda (node)
|
|
|
|
(origami-fold-open-set node nil))
|
|
|
|
node))))))))
|
2014-06-03 05:19:43 +10:00
|
|
|
|
2014-04-20 00:08:46 +10:00
|
|
|
(defun origami-toggle-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Toggle the fold node at POINT in BUFFER open or closed. The
|
|
|
|
fold node opened or closed will be the deepest nested at POINT."
|
2014-04-20 00:08:46 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-04-20 00:08:46 +10:00
|
|
|
(-when-let (path (origami-fold-find-path-containing tree point))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc
|
|
|
|
path (lambda (node)
|
|
|
|
(origami-fold-open-set
|
|
|
|
node (not (origami-fold-open?
|
|
|
|
(-last-item path)))))))))))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-08-05 07:01:00 +10:00
|
|
|
(defun origami-forward-toggle-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Like `origami-toggle-node' but search forward in BUFFER for a
|
|
|
|
fold node. If a fold node is found after POINT and before the
|
|
|
|
next line break, this will be toggled. Otherwise, behave exactly
|
|
|
|
as `origami-toggle-node'."
|
2014-08-05 07:01:00 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
2014-08-15 07:06:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-when-let (path (origami-search-forward-for-path buffer point))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-assoc
|
|
|
|
path (lambda (node)
|
|
|
|
(origami-fold-open-set
|
|
|
|
node (not (origami-fold-open?
|
|
|
|
(-last-item path)))))))))))
|
2014-08-15 07:06:48 +10:00
|
|
|
|
|
|
|
(defun origami-recursively-toggle-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Cycle a fold node between recursively closed, open and
|
|
|
|
recursively open depending on its current state. The fold node
|
|
|
|
acted upon is searched for forward in BUFFER from POINT. If a
|
|
|
|
fold node is found after POINT and before the next line break,
|
|
|
|
this will be toggled otherwise the fold node nested deepest at
|
|
|
|
POINT will be acted upon.
|
|
|
|
|
|
|
|
This command will only work if bound to a key. For those familiar
|
|
|
|
with org-mode heading opening and collapsing, this will feel
|
|
|
|
familiar. It's easiest to grasp this just by giving it a go."
|
2014-08-15 07:06:48 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(-when-let (path (origami-search-forward-for-path buffer point))
|
|
|
|
(let ((node (-last-item path)))
|
|
|
|
(if (eq last-command 'origami-recursively-toggle-node)
|
|
|
|
(cond ((origami-fold-node-recursively-open? node)
|
|
|
|
(origami-close-node-recursively buffer (origami-fold-beg node)))
|
|
|
|
((origami-fold-node-recursively-closed? node)
|
|
|
|
(origami-toggle-node buffer (origami-fold-beg node)))
|
|
|
|
(t (origami-open-node-recursively buffer (origami-fold-beg node))))
|
|
|
|
(origami-forward-toggle-node buffer point)))))
|
2014-08-05 07:01:00 +10:00
|
|
|
|
2014-04-22 05:21:05 +10:00
|
|
|
(defun origami-open-all-nodes (buffer)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Recursively open every fold node in BUFFER."
|
2014-04-22 05:21:05 +10:00
|
|
|
(interactive (list (current-buffer)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-map
|
|
|
|
(lambda (node)
|
|
|
|
(origami-fold-open-set node t))
|
|
|
|
tree)))))
|
2014-04-22 05:21:05 +10:00
|
|
|
|
|
|
|
(defun origami-close-all-nodes (buffer)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Recursively close every fold node in BUFFER."
|
2014-04-22 05:21:05 +10:00
|
|
|
(interactive (list (current-buffer)))
|
2014-06-02 02:49:48 +10:00
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
2014-08-17 04:01:25 +10:00
|
|
|
(origami-apply-new-tree buffer tree (origami-store-cached-tree
|
|
|
|
buffer
|
|
|
|
(origami-fold-map
|
|
|
|
(lambda (node)
|
|
|
|
(origami-fold-open-set node nil))
|
|
|
|
tree)))))
|
2014-04-22 05:21:05 +10:00
|
|
|
|
2014-11-17 05:09:19 +11:00
|
|
|
(defun origami-toggle-all-nodes (buffer)
|
|
|
|
"Toggle all fold nodes in the buffer recursively open or
|
|
|
|
recursively closed."
|
|
|
|
(interactive (list (current-buffer)))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
;; use the first child as root is always open
|
|
|
|
(if (-> tree origami-fold-children car origami-fold-open?)
|
|
|
|
(origami-close-all-nodes buffer)
|
|
|
|
(origami-open-all-nodes buffer))))
|
|
|
|
|
2014-04-22 06:32:04 +10:00
|
|
|
(defun origami-show-only-node (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Close all fold nodes in BUFFER except for those necessary to
|
|
|
|
make POINT visible. Very useful for quickly collapsing everything
|
|
|
|
in the buffer other than what you are looking at."
|
2014-04-22 06:32:04 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(origami-close-all-nodes buffer)
|
2014-04-24 05:21:56 +10:00
|
|
|
(origami-show-node buffer point))
|
2014-04-22 06:32:04 +10:00
|
|
|
|
2014-06-02 06:39:08 +10:00
|
|
|
(defun origami-previous-fold (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Move point to the beginning of the fold before POINT. If POINT
|
|
|
|
is in a fold, move to the beginning of the fold that POINT is
|
2014-06-02 06:39:08 +10:00
|
|
|
in."
|
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-> tree
|
|
|
|
(origami-fold-preorder-reduce (lambda (state n)
|
|
|
|
(cons (origami-fold-beg n) state)) nil)
|
|
|
|
(->> (-reduce (lambda (state pos)
|
|
|
|
(if (< state point) state pos))))
|
|
|
|
goto-char)))
|
|
|
|
|
|
|
|
(defun origami-next-fold (buffer point)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Move point to the end of the fold after POINT. If POINT is in
|
|
|
|
a fold, move to the end of the fold that POINT is in."
|
2014-06-02 06:39:08 +10:00
|
|
|
(interactive (list (current-buffer) (point)))
|
|
|
|
(-when-let (tree (origami-get-fold-tree buffer))
|
|
|
|
(-> tree
|
|
|
|
(origami-fold-postorder-reduce (lambda (state n)
|
|
|
|
(cons (origami-fold-end n) state)) nil)
|
2014-08-12 06:58:46 +10:00
|
|
|
(->> (-last (lambda (pos) (> pos point))))
|
2014-06-02 06:39:08 +10:00
|
|
|
goto-char)))
|
|
|
|
|
2014-08-17 03:23:04 +10:00
|
|
|
(defun origami-undo (buffer)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Undo the last folding operation applied to BUFFER. Undo
|
|
|
|
history is linear. If you undo some fold operations and then
|
|
|
|
perform a new fold operation you will lose the history of
|
|
|
|
operations undone."
|
2014-08-17 03:23:04 +10:00
|
|
|
(interactive (list (current-buffer)))
|
2014-08-17 04:01:25 +10:00
|
|
|
(let ((current-tree (origami-get-cached-tree buffer)))
|
|
|
|
(origami-update-history buffer (lambda (h) (origami-h-undo h)))
|
|
|
|
(let ((old-tree (origami-get-cached-tree buffer)))
|
|
|
|
(origami-apply-new-tree buffer current-tree old-tree))))
|
|
|
|
|
|
|
|
(defun origami-redo (buffer)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Redo the last folding operation applied to BUFFER. You can
|
|
|
|
only redo undone operations while a new folding operation hasn't
|
|
|
|
been performed to BUFFER."
|
2014-08-17 04:01:25 +10:00
|
|
|
(interactive (list (current-buffer)))
|
|
|
|
(let ((current-tree (origami-get-cached-tree buffer)))
|
|
|
|
(origami-update-history buffer (lambda (h) (origami-h-redo h)))
|
|
|
|
(let ((new-tree (origami-get-cached-tree buffer)))
|
|
|
|
(origami-apply-new-tree buffer current-tree new-tree))))
|
2014-08-17 03:23:04 +10:00
|
|
|
|
2014-03-18 06:23:46 +11:00
|
|
|
(defun origami-reset (buffer)
|
2014-11-16 04:39:49 +11:00
|
|
|
"Remove all folds from BUFFER and reset all origami state
|
|
|
|
associated with this buffer. Useful during development or if you
|
|
|
|
uncover any bugs."
|
2014-04-22 05:05:50 +10:00
|
|
|
(interactive (list (current-buffer)))
|
2014-08-17 01:31:37 +10:00
|
|
|
(origami-setup-local-vars buffer)
|
2014-04-22 05:05:50 +10:00
|
|
|
(origami-remove-all-overlays buffer))
|
2014-03-18 06:23:46 +11:00
|
|
|
|
2014-04-14 06:08:02 +10:00
|
|
|
;;; minor mode
|
|
|
|
|
2014-06-01 03:02:04 +10:00
|
|
|
(defvar origami-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
map)
|
|
|
|
"Keymap for `origami-mode'.")
|
|
|
|
|
2014-06-01 03:29:27 +10:00
|
|
|
(defcustom origami-mode-hook nil
|
|
|
|
"Hook called when origami minor mode is activated or deactivated."
|
|
|
|
:type 'hook
|
|
|
|
:group 'origami)
|
|
|
|
|
2015-03-30 01:14:03 +11:00
|
|
|
(defun origami-find-occurrence-show-node ()
|
|
|
|
(call-interactively 'origami-show-node))
|
|
|
|
|
2014-11-17 01:26:35 +11:00
|
|
|
;;;###autoload
|
2014-06-01 03:02:04 +10:00
|
|
|
(define-minor-mode origami-mode
|
|
|
|
"Minor mode to selectively hide/show text in the current buffer.
|
|
|
|
With a prefix argument ARG, enable the mode if ARG is positive,
|
|
|
|
and disable it otherwise. If called from Lisp, enable the mode
|
|
|
|
if ARG is omitted or nil.
|
|
|
|
|
|
|
|
Lastly, the normal hook `origami-mode-hook' is run using
|
|
|
|
`run-hooks'.
|
|
|
|
|
|
|
|
Key bindings:
|
|
|
|
\\{origami-mode-map}"
|
|
|
|
:group 'origami
|
|
|
|
:lighter nil
|
|
|
|
:keymap origami-mode-map
|
|
|
|
:init-value nil
|
2015-03-30 01:14:03 +11:00
|
|
|
(if origami-mode
|
|
|
|
(progn
|
|
|
|
(add-hook 'occur-mode-find-occurrence-hook
|
|
|
|
'origami-find-occurrence-show-node nil t)
|
|
|
|
(setq next-error-move-function (lambda (ignored pos)
|
|
|
|
(goto-char pos)
|
|
|
|
(call-interactively 'origami-show-node))))
|
|
|
|
(remove-hook 'occur-mode-find-occurrence-hook
|
|
|
|
'origami-find-occurrence-show-node t)
|
|
|
|
(setq next-error-move-function nil))
|
2014-08-17 01:31:37 +10:00
|
|
|
(origami-reset (current-buffer)))
|
2014-06-01 03:02:04 +10:00
|
|
|
|
2015-04-26 00:20:15 +10:00
|
|
|
;;;###autoload
|
|
|
|
(define-global-minor-mode global-origami-mode origami-mode
|
|
|
|
(lambda () (origami-mode 1)))
|
|
|
|
|
2014-06-01 03:02:04 +10:00
|
|
|
(provide 'origami)
|
|
|
|
|
2014-03-18 06:23:46 +11:00
|
|
|
;;; origami.el ends here
|