summaryrefslogtreecommitdiff
path: root/coorgi-client.el
blob: 42ae1b5ddf909acded65822a913b0800113e951b (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
;;; coorgi-client.el --- A collaborative org document sync client. -*- lexical-binding: t; -*-

;; 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

;; 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/>.

(require 'cl-lib)

(defvar coorgi-properties '(:COORGI-KEY
			    :COORGI-LAST-MODIFIED
			    :COORGI-LAST-MODIFIED-BY
			    :COORGI-VERSION)
  "List of properties the Coorgi server cares about.")

(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)))))

(defun coorgi--find-property-drawer (headline)
  (caddr (caddr headline)))

(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))))))

;;; coorgi-client.el ends here