Link headlines to one another and indent all nodes

This commit is contained in:
Renaud Casenave-Péré 2022-12-08 23:18:15 +01:00
parent d595cbe273
commit 98d7d9e918
5 changed files with 65 additions and 10 deletions

View file

@ -14,12 +14,14 @@
(define-roles #.|Qt.UserRole|
+nodetype-role+
+rawtext-role+
+headline-depth-role+
+depth-role+
+title-role+)
(defvar *current-pathname* nil)
(defvar *org-document* nil)
(defvar *current-headline* nil)
(defvar *current-node* nil)
(defvar *current-index* 0)
(defvar *nodes-count* 0)
@ -62,17 +64,37 @@
(defgeneric step-previous-node (node))
(defmethod step-previous-node ((node org-line))
(previous-of node))
(defmethod step-previous-node :before ((node org-headline))
(setf *current-headline* (previous-headline-of node)))
(defgeneric step-next-node (node))
(defmethod step-next-node ((node org-line))
(next-of node))
(defmethod step-next-node :before ((node org-headline))
(setf *current-headline* node))
(defgeneric node-depth (node))
(defmethod node-depth ((node org-line))
(if *current-headline*
(1+ (depth-of *current-headline*))
0))
(defmethod node-depth ((node org-headline))
(depth-of node))
(defun step-to-node (from step)
(assert from)
(let* ((index (+ *current-index* step))
(node (if (< step 0)
(dotimes (i (- step) from)
(setf from (previous-of from)))
(setf from (step-previous-node from)))
(dotimes (i step from)
(setf from (next-of from))))))
(setf from (step-next-node from))))))
(setf *current-node* node
*current-index* index)
(values *current-node* *current-index*)))
*current-node*))
(defun goto-node (index)
(step-to-node *current-node* (- index *current-index*)))
@ -95,7 +117,7 @@
(#.+rawtext-role+
(qvariant-from-value (raw-text-of item) "QString"))
(#.+depth-role+
(qvariant-from-value (depth-of item) "int"))
(qvariant-from-value (node-depth item) "int"))
(#.+title-role+
(qvariant-from-value (title-of item) "QString"))))))))
(qoverride model "roleNames()"
@ -115,6 +137,7 @@
(setf *current-pathname* pathname)
(|beginResetModel| *org-model*)
(setf *org-document* org-document
*current-headline* nil
*current-node* (next-of org-document)
*current-index* 0
*nodes-count* (loop for c = (next-of org-document) then (next-of c)

View file

@ -10,10 +10,13 @@
#:line-ending-of
#:depth-of
#:title-of
#:previous-headline-of
#:next-headline-of
#:make-org-document
#:make-org-line
#:make-org-headline
#:append-node
#:process-last-node
#:collect-nodes))
(in-package :sextant/org/nodes)
@ -54,7 +57,17 @@
:type string
:accessor title-of
:initform ""
:documentation "The title of this headline.")))
:documentation "The title of this headline.")
(previous-headline :initarg :previous-headline
:type org-headline
:accessor previous-headline-of
:initform nil
:documentation "The headline preceding this one.")
(next-headline :initarg :next-headline
:type org-headline
:accessor next-headline-of
:initform nil
:documentation "The headline following this one.")))
(defun make-org-document (first-node)
(make-instance 'org-document :next first-node))
@ -68,10 +81,28 @@
(defun append-node (node next)
(setf (next-of node) next
(previous-of next) node)
node)
(defgeneric append-node (node next))
(defgeneric process-last-node (node))
(let (current-headline)
(defmethod append-node ((node org-line) next)
(setf (next-of node) next
(previous-of next) node)
node)
(defmethod append-node :before ((node org-headline) next)
(when current-headline
(setf (next-headline-of current-headline) node
(previous-headline-of node) current-headline))
(setf current-headline node))
(defmethod process-last-node ((node org-line))
(setf current-headline nil))
(defmethod process-last-node :before ((node org-headline))
(when current-headline
(setf (next-headline-of current-headline) node
(previous-headline-of node) current-headline))))
(defun collect-nodes (&rest nodes)
(loop for n on nodes

View file

@ -9,6 +9,7 @@ ListItem {
anchors {
left: parent.left
right: parent.right
leftMargin: Theme.paddingMedium * depth
}
contentHeight: loader.item ? loader.item.height : 0

View file

@ -12,7 +12,6 @@ OrgText {
anchors {
top: parent.top
left: parent.left
leftMargin: Theme.paddingMedium * depth
}
font {

View file

@ -37,6 +37,7 @@ namespace sextant
cl_object&& finish() &&
{
ast_funcall("PROCESS-LAST-NODE", _tail);
return LEXY_MOV(_head);
}
};