summaryrefslogtreecommitdiff
path: root/coorgi-client.org
blob: 17383de994aefc9a4f2b11bdd56677ef9ac8be94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#+PROPERTY: header-args:emacs-lisp :lexical t

* Coorgi Client - A collaborative org document sync client.

Coorgi stands for "cooperative org interchange", and intends to enable
multiple users to sync org documents between one another. Possible uses
include issue trackers, shared agendas, and near real-time collaboration.

** Start hacking on this literate program

Assuming you're using Emacs, this project is written as literate code.
This document /is/ the program, written for humans first. Some things
to ensure your environment is ready to go:

- enable emacs lisp and org as org-babel languages

  #+begin_src emacs-lisp
    (org-babel-do-load-languages
     'org-babel-load-languages
     '((emacs-lisp . t)
       (org . t)))
  #+end_src

- install the ~annotate~ package (optional)

  #+begin_src emacs-lisp
    (package-install 'annotate)
  #+end_src

  We're experimenting with using ~annotate-mode~ to comment on the document.
  The ~.dir-locals~ file here will set the annotate database file to the
  ~annotations~ file in this repository.

** Overview

If you evaluate the following block, it will effectively "eval-buffer"
on the whole shebang.

#+begin_src emacs-lisp :tangle yes :noweb no-export :results silent
  ;;; coorgi-client.el --- A collaborative org document sync client. -*- lexical-binding: t; -*-

  <<copyright>>

  <<license>>

  <<dependencies>>

  <<defvars>>

  <<coorgi--find-headline>>

  <<coorgi--find-property-drawer>>

  <<coorgify-at-point>>

  ;;; coorgi-client.el ends here
#+end_src

** Parsing, modifying, and re-inserting org data

~org-element-parse-buffer~ will turn any org document into a parsed
data tree. ~org-element-interpret-data~ will turn any (valid) parsed
org-data structure back into a string.

therefore, we /should/ be able to act upon the parsed data with Coorgi
functions to update the document, rather than mucking with live buffer
edits and updates.

*** TODO example proof of concept

For example:

#+begin_src emacs-lisp :var org-doc=trivial-example() :results verbatim
  (let ((subject (with-temp-buffer (insert org-doc) (org-element-parse-buffer))))
    (org-element-interpret-data subject))
#+end_src

** Syncing Coorgi documents with collaborators
*** TODO ~coorgi-initialize~ creates the document on the server
*** TODO Coorgi syncs on the ~after-save-hook~
*** TODO Coorgi syncs on buffer change if its been idle X time

* Dependencies

#+begin_src emacs-lisp :results silent :noweb-ref dependencies
  (require 'cl-lib)
#+end_src

* defvars
:PROPERTIES:
:header-args:emacs-lisp+: :noweb-ref defvars :noweb-sep "\n\n" :results silent
:END:

** coorgi-properties

This variable stores a list of upcased symbols that appear in the headline
element's parsed property drawer. 

#+begin_src emacs-lisp
  (defvar coorgi-properties '(:COORGI-KEY
			      :COORGI-LAST-MODIFIED
			      :COORGI-LAST-MODIFIED-BY
			      :COORGI-VERSION)
    "List of properties coorgi cares about.")
#+end_src

* Functions
** coorgify-at-point

Converts the current org outline section into a coorgi-node data structure.

#+name: coorgify-at-point
#+begin_src emacs-lisp :results none
  (defun coorgify-at-point ()
    (interactive)
    (save-excursion
      (let* ((current-element (org-element-at-point))
	     (entry (if (not (equal (car current-element) 'heading))
			(progn (org-up-heading-safe)
			       (org-element-at-point))
		      current-element))
	     (parsed (print (org-element-parse-buffer)))
	     (props (org-element--get-node-properties))
	     (non-coorgi-props (cl-loop for (key value) on props by #'cddr
					unless (member key coorgi-properties)
					append (list key value)))
	     (content-bounds (cons (progn (org-down-element) (org-forward-element) (point)) (plist-get (cadr entry) :contents-end))))
	`( type entry
	   headline ,(plist-get (cadr entry) :title)
	   version ,(string-to-number (plist-get props :COORGI-VERSION))
	   last-modified-by ,(plist-get props :COORGI-LAST-MODIFIED-BY)
	   last-modified ,(plist-get props :COORGI-LAST-MODIFIED)
	   key ,(plist-get props :COORGI-KEY)
	   properties ,non-coorgi-props
	   contents ,(buffer-substring-no-properties (car content-bounds) (cdr content-bounds))))))
#+end_src

*** Testing
**** Trivial Org Example

Given this org document:

#+name: trivial-example
#+begin_src org
,* MOO
:PROPERTIES:
:coorgi-version: 1
:coorgi-last-modified-by: colin/123
:coorgi-last-modified: 123
:coorgi-key: 101
:foo: bar
:END:

hello

#+end_src

~coorgify-at-point~ should return something like this:

#+name: trivial-expected-value
#+begin_src emacs-lisp
  '(type entry
   headline "MOO"
   version 1
   last-modified-by "colin/123"
   last-modified "123"
   key "101"
   properties (:FOO "bar")
   content "
  hello

  "
   children ())
#+end_src

No matter where the cursor is in the buffer (there's only one coorgi node)

***** TODO When the cursor is in the body of the heading

#+name: test-trivial-example-cursor-in-body
#+begin_src emacs-lisp :var org-doc=trivial-example() expected=trivial-expected-value :results code
  (let ((subject (with-temp-buffer (insert org-doc) (coorgify-at-point))))
    (if (not (equal subject expected))
      `((actual ,subject) (expected ,expected))
      "PASS"))
#+end_src

#+RESULTS: test-trivial-example-cursor-in-body
#+begin_example
,* MOO
:PROPERTIES:
:coorgi-version: 1
:coorgi-last-modified-by: colin/123
:coorgi-last-modified: 123
:coorgi-key: 101
:foo:      bar
:END:

hello
#+end_example

***** TODO When the cursor at the top of the document

#+name: test-trivial-example-cursor-at-top
#+begin_src emacs-lisp :var org-doc=trivial-example() expected=trivial-expected-value :results code
  (let ((subject (with-temp-buffer (insert org-doc) (beginning-of-buffer) (coorgify-at-point))))
    (if (not (equal subject expected))
	`((actual ,subject) (expected ,expected))
      "PASS"))
#+end_src

#+RESULTS: test-trivial-example-cursor-at-top
#+begin_src emacs-lisp
((actual
  (type entry headline "MOO" version 1 last-modified-by "colin/123" last-modified "123" key "101" properties
	(:FOO "bar")))
 (expected
  (type entry headline "MOO" version 1 last-modified-by "colin/123" last-modified "123" key "101" properties
	(:FOO "bar")
	content "\n\nhello\n\n" children nil)))
#+end_src

**** Basic Org Example

#+name: basic-example
#+begin_src org
,* MOO
:PROPERTIES:
:coorgi-version: 211
:coorgi-last-modified-by: “colin/1231234123”
:coorgi-last-modified: 123124331
:coorgi-key: “1010101010101”
:foo: “bar”
:END:

hello

,** TODO [#B] Cow :foobar:
:PROPERTIES:
:coorgi-version: 32
:coorgi-last-modified-by: “colin/1231234123”
:coorgi-last-modified: 123124331
:coorgi-key: “123sdfas3wasd”
:foo: “bar”
:END:

Cows say moo. CURSORPOSITION

,*** Hunky

a cow

,*** Dory

another cow
#+end_src

** coorgi--find-headline

We need to used the parsed version of the org buffer to look up the elements
we're turning into coorgi nodes and get the relevant data to pack it up recursively.

#+name: coorgi--find-headline
#+begin_src emacs-lisp :results none
  (defun coorgi--find-headline (headline org-data)
    (car (org-element-map org-data 'headline
	   (lambda (hl)
	     (when (string-equal (org-element-property :raw-value hl)
				 (org-element-property :raw-value headline))
	       hl)))))
#+end_src

#+begin_src emacs-lisp :var org-doc=trivial-example() :results code
  (let ((org-data (with-temp-buffer (insert org-doc) (org-element-parse-buffer))))
    (coorgi--find-headline '(headline (:raw-value "MOO")) org-data))
#+end_src

#+RESULTS:
#+begin_src emacs-lisp
(headline
 (:raw-value "MOO" :begin 1 :end 142 :pre-blank 0 :contents-begin 7 :contents-end 142 :level 1 :priority nil :tags nil :todo-keyword nil :todo-type nil :post-blank 0 :footnote-section-p nil :archivedp nil :commentedp nil :post-affiliated 1 :FOO "bar" :COORGI-KEY "101" :COORGI-LAST-MODIFIED "123" :COORGI-LAST-MODIFIED-BY "colin/123" :COORGI-VERSION "1" :title
	     (#("MOO" 0 3
		(:parent #0)))
	     :parent
	     (org-data nil #0))
 (section
  (:begin 7 :end 142 :contents-begin 7 :contents-end 142 :post-blank 0 :post-affiliated 7 :parent #0)
  (property-drawer
   (:begin 7 :end 136 :contents-begin 20 :contents-end 129 :post-blank 1 :post-affiliated 7 :parent #1)
   (node-property
    (:key "coorgi-version" :value "1" :begin 20 :end 39 :post-blank 0 :post-affiliated 20 :parent #2))
   (node-property
    (:key "coorgi-last-modified-by" :value "colin/123" :begin 39 :end 75 :post-blank 0 :post-affiliated 39 :parent #2))
   (node-property
    (:key "coorgi-last-modified" :value "123" :begin 75 :end 102 :post-blank 0 :post-affiliated 75 :parent #2))
   (node-property
    (:key "coorgi-key" :value "101" :begin 102 :end 119 :post-blank 0 :post-affiliated 102 :parent #2))
   (node-property
    (:key "foo" :value "bar" :begin 119 :end 129 :post-blank 0 :post-affiliated 119 :parent #2)))
  (paragraph
   (:begin 136 :end 142 :contents-begin 136 :contents-end 142 :post-blank 0 :post-affiliated 136 :parent #1)
   #("hello\n" 0 6
     (:parent #2)))))
#+end_src

** coorgi--find-property-drawer

It is useful to know whether or not a headline has a property drawer.

If not, we know that Coorgi doesn't know about it yet.

If so, we need to parse the existing properties into the coorgified
entry data structure.

In addition, we get more information about the org document's structure
as it gets parsed into its various elements.

#+name: coorgi--find-property-drawer
#+begin_src emacs-lisp :results none
  (defun coorgi--find-property-drawer (headline)
    (caddr (caddr headline)))
#+end_src

*** tests

#+begin_src emacs-lisp :var org-doc=trivial-example()
  (let* ((org-data (with-temp-buffer (insert org-doc) (org-element-parse-buffer)))
	 (headline (coorgi--find-headline '(headline (:raw-value "MOO")) org-data)))
    (when (equal (car (coorgi--find-property-drawer headline)) 'property-drawer) "PASS"))
#+end_src

#+RESULTS:
: PASS

* Copyright

#+begin_src emacs-lisp :noweb-ref copyright
  ;; Copyright (C) 2022 Grant Shangreaux

  ;; Author: Grant Shangreaux <shoshin@cicadas.surf>
  ;; Maintainer: Grant Shangreaux <shoshin@cicadas.surf>
  ;; Created: 11 Nov 2022
  ;; Keywords: org sync collaborative
  ;; URL: https://cicadas.surf/cgit/coorgi-client.git
#+end_src

* License
** GNU General Public License version 3

#+begin_src emacs-lisp :noweb-ref license
  ;; This file is not part of GNU Emacs.

  ;; This file is free software: you can redistribute it and/or modify
  ;; it under the terms of the GNU General Public License as published by
  ;; the Free Software Foundation, either version 3 of the License, or
  ;; (at your option) any later version.
  ;;
  ;; GNU Emacs is distributed in the hope that it will be useful,
  ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  ;; GNU General Public License for more details.
  ;;
  ;; You should have received a copy of the GNU General Public License
  ;; along with this file.  If not, see <https://www.gnu.org/licenses/>.
#+end_src