-
Notifications
You must be signed in to change notification settings - Fork 101
/
org-noter.el
2294 lines (1963 loc) · 105 KB
/
org-noter.el
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-noter.el --- A synchronized, Org-mode, document annotator -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2018 Gonçalo Santos
;; Author: Gonçalo Santos (aka. weirdNox@GitHub)
;; Homepage: https://github.com/weirdNox/org-noter
;; Keywords: lisp pdf interleave annotate external sync notes documents org-mode
;; Package-Requires: ((emacs "24.4") (cl-lib "0.6") (org "9.0"))
;; Version: 1.4.1
;; This file is not part of GNU Emacs.
;; This program 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.
;; This program 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 program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The idea is to let you create notes that are kept in sync when you scroll through the
;; document, but that are external to it - the notes themselves live in an Org-mode file. As
;; such, this leverages the power of Org-mode (the notes may have outlines, latex fragments,
;; babel, etc...) while acting like notes that are made /in/ the document.
;; Also, I must thank Sebastian for the original idea and inspiration!
;; Link to the original Interleave package:
;; https://github.com/rudolfochrist/interleave
;;; Code:
(require 'org)
(require 'org-element)
(require 'cl-lib)
(declare-function doc-view-goto-page "doc-view")
(declare-function image-display-size "image-mode")
(declare-function image-get-display-property "image-mode")
(declare-function image-mode-window-get "image-mode")
(declare-function image-scroll-up "image-mode")
(declare-function nov-render-document "ext:nov")
(declare-function org-attach-dir "org-attach")
(declare-function org-attach-file-list "org-attach")
(declare-function pdf-info-getannots "ext:pdf-info")
(declare-function pdf-info-gettext "ext:pdf-info")
(declare-function pdf-info-outline "ext:pdf-info")
(declare-function pdf-info-pagelinks "ext:pdf-info")
(declare-function pdf-util-tooltip-arrow "ext:pdf-util")
(declare-function pdf-view-active-region "ext:pdf-view")
(declare-function pdf-view-active-region-p "ext:pdf-view")
(declare-function pdf-view-active-region-text "ext:pdf-view")
(declare-function pdf-view-goto-page "ext:pdf-view")
(declare-function pdf-view-mode "ext:pdf-view")
(defvar nov-documents-index)
(defvar nov-file-name)
;; --------------------------------------------------------------------------------
;; NOTE(nox): User variables
(defgroup org-noter nil
"A synchronized, external annotator"
:group 'convenience
:version "25.3.1")
(defcustom org-noter-property-doc-file "NOTER_DOCUMENT"
"Name of the property that specifies the document."
:group 'org-noter
:type 'string)
(defcustom org-noter-property-note-location "NOTER_PAGE"
"Name of the property that specifies the location of the current note.
The default value is still NOTER_PAGE for backwards compatibility."
:group 'org-noter
:type 'string)
(defcustom org-noter-default-heading-title "Notes for page $p$"
"The default title for headings created with `org-noter-insert-note'.
$p$ is replaced with the number of the page or chapter you are in
at the moment."
:group 'org-noter
:type 'string)
(defcustom org-noter-notes-window-behavior '(start scroll)
"This setting specifies in what situations the notes window should be created.
When the list contains:
- `start', the window will be created when starting a `org-noter' session.
- `scroll', it will be created when you go to a location with an associated note.
- `only-prev', it will be created when you go to a location without notes, but that
has previous notes that are shown."
:group 'org-noter
:type '(set (const :tag "Session start" start)
(const :tag "Scroll to location with notes" scroll)
(const :tag "Scroll to location with previous notes only" only-prev)))
(defcustom org-noter-notes-window-location 'horizontal-split
"Whether the notes should appear in the main frame (horizontal or vertical split) or in a separate frame.
Note that this will only have effect on session startup if `start'
is member of `org-noter-notes-window-behavior' (which see)."
:group 'org-noter
:type '(choice (const :tag "Horizontal" horizontal-split)
(const :tag "Vertical" vertical-split)
(const :tag "Other frame" other-frame)))
(define-obsolete-variable-alias 'org-noter-doc-split-percentage 'org-noter-doc-split-fraction "1.2.0")
(defcustom org-noter-doc-split-fraction '(0.5 . 0.5)
"Fraction of the frame that the document window will occupy when split.
This is a cons of the type (HORIZONTAL-FRACTION . VERTICAL-FRACTION)."
:group 'org-noter
:type '(cons (number :tag "Horizontal fraction") (number :tag "Vertical fraction")))
(defcustom org-noter-auto-save-last-location nil
"When non-nil, save the last visited location automatically; when starting a new session, go to that location."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-hide-other t
"When non-nil, hide all headings not related to the command used.
For example, when scrolling to pages with notes, collapse all the
notes that are not annotating the current page."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-always-create-frame t
"When non-nil, org-noter will always create a new frame for the session.
When nil, it will use the selected frame if it does not belong to any other session."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-suggest-from-attachments t
"When non-nil, org-noter will suggest files from the attachments
when creating a session, if the document is missing."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-separate-notes-from-heading nil
"When non-nil, add an empty line between each note's heading and content."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-insert-selected-text-inside-note t
"When non-nil, it will automatically append the selected text into an existing note."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-closest-tipping-point 0.3
"Defines when to show the closest previous note.
Let x be (this value)*100. The following schematic represents the
view (eg. a page of a PDF):
+----+
| | -> If there are notes in here, the closest previous note is not shown
+----+--> Tipping point, at x% of the view
| | -> When _all_ notes are in here, below the tipping point, the closest
| | previous note will be shown.
+----+
When this value is negative, disable this feature.
This setting may be overridden in a document with the function
`org-noter-set-closest-tipping-point', which see."
:group 'org-noter
:type 'number)
(defcustom org-noter-default-notes-file-names '("Notes.org")
"List of possible names for the default notes file, in increasing order of priority."
:group 'org-noter
:type '(repeat string))
(defcustom org-noter-notes-search-path '("~/Documents")
"List of paths to check (non recursively) when searching for a notes file."
:group 'org-noter
:type '(repeat string))
(defcustom org-noter-arrow-delay 0.2
"Number of seconds from when the command was invoked until the tooltip arrow appears.
When set to a negative number, the arrow tooltip is disabled.
This is needed in order to keep Emacs from hanging when doing many syncs."
:group 'org-noter
:type 'number)
(defcustom org-noter-doc-property-in-notes nil
"If non-nil, every new note will have the document property too.
This makes moving notes out of the root heading easier."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-insert-note-no-questions nil
"When non-nil, `org-noter-insert-note' won't ask for a title and will always insert a new note.
The title used will be the default one."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-kill-frame-at-session-end t
"If non-nil, `org-noter-kill-session' will delete the frame if others exist on the current display.'"
:group 'org-noter
:type 'boolean)
(defcustom org-noter-insert-heading-hook nil
"Hook being run after inserting a new heading."
:group 'org-noter
:type 'hook)
(defface org-noter-no-notes-exist-face
'((t
:foreground "chocolate"
:weight bold))
"Face for modeline note count, when 0."
:group 'org-noter)
(defface org-noter-notes-exist-face
'((t
:foreground "SpringGreen"
:weight bold))
"Face for modeline note count, when not 0."
:group 'org-noter)
;; --------------------------------------------------------------------------------
;; NOTE(nox): Integration with other packages
(defcustom org-noter--check-location-property-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--parse-location-property-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--pretty-print-location-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--convert-to-location-cons-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--doc-goto-location-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--note-after-tipping-point-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--relative-position-to-view-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--get-precise-info-hook nil
"TODO"
:group 'org-noter
:type 'hook)
(defcustom org-noter--doc-approx-location-hook nil
"TODO"
:group 'org-noter
:type 'hook)
;; --------------------------------------------------------------------------------
;; NOTE(nox): Private variables or constants
(cl-defstruct org-noter--session
id frame doc-buffer notes-buffer ast modified-tick doc-mode display-name notes-file-path property-text
level num-notes-in-view window-behavior window-location doc-split-fraction auto-save-last-location
hide-other closest-tipping-point)
(defvar org-noter--sessions nil
"List of `org-noter' sessions.")
(defvar-local org-noter--session nil
"Session associated with the current buffer.")
(defvar org-noter--inhibit-location-change-handler nil
"Prevent location change from updating point in notes.")
(defvar org-noter--start-location-override nil
"Used to open the session from the document in the right page.")
(defvar-local org-noter--nov-timer nil
"Timer for synchronizing notes after scrolling.")
(defvar org-noter--arrow-location nil
"A vector [TIMER WINDOW TOP] that shows where the arrow should appear, when idling.")
(defvar org-noter--completing-read-keymap (make-sparse-keymap)
"A `completing-read' keymap that let's the user insert spaces.")
(set-keymap-parent org-noter--completing-read-keymap minibuffer-local-completion-map)
(define-key org-noter--completing-read-keymap (kbd "SPC") 'self-insert-command)
(defconst org-noter--property-behavior "NOTER_NOTES_BEHAVIOR"
"Property for overriding global `org-noter-notes-window-behavior'.")
(defconst org-noter--property-location "NOTER_NOTES_LOCATION"
"Property for overriding global `org-noter-notes-window-location'.")
(defconst org-noter--property-doc-split-fraction "NOTER_DOCUMENT_SPLIT_FRACTION"
"Property for overriding global `org-noter-doc-split-fraction'.")
(defconst org-noter--property-auto-save-last-location "NOTER_AUTO_SAVE_LAST_LOCATION"
"Property for overriding global `org-noter-auto-save-last-location'.")
(defconst org-noter--property-hide-other "NOTER_HIDE_OTHER"
"Property for overriding global `org-noter-hide-other'.")
(defconst org-noter--property-closest-tipping-point "NOTER_CLOSEST_TIPPING_POINT"
"Property for overriding global `org-noter-closest-tipping-point'.")
(defconst org-noter--note-search-no-recurse (delete 'headline (append org-element-all-elements nil))
"List of elements that shouldn't be recursed into when searching for notes.")
(defconst org-noter--id-text-property 'org-noter-session-id
"Text property used to mark the headings with open sessions.")
;; --------------------------------------------------------------------------------
;; NOTE(nox): Utility functions
(defun org-noter--get-new-id ()
(catch 'break
(while t
(let ((id (random most-positive-fixnum)))
(unless (cl-loop for session in org-noter--sessions
when (= (org-noter--session-id session) id) return t)
(throw 'break id))))))
(defmacro org-noter--property-or-default (name)
(let ((function-name (intern (concat "org-noter--" (symbol-name name) "-property")))
(variable (intern (concat "org-noter-" (symbol-name name)))))
`(let ((prop-value (,function-name ast)))
(cond ((eq prop-value 'disable) nil)
(prop-value)
(t ,variable)))))
(defun org-noter--create-session (ast document-property-value notes-file-path)
(let* ((raw-value-not-empty (> (length (org-element-property :raw-value ast)) 0))
(display-name (if raw-value-not-empty
(org-element-property :raw-value ast)
(file-name-nondirectory document-property-value)))
(frame-name (format "Emacs Org-noter - %s" display-name))
(document (find-file-noselect document-property-value))
(document-path (expand-file-name document-property-value))
(document-major-mode (buffer-local-value 'major-mode document))
(document-buffer-name
(generate-new-buffer-name (concat (unless raw-value-not-empty "Org-noter: ") display-name)))
(document-buffer
(if (eq document-major-mode 'nov-mode)
document
(make-indirect-buffer document document-buffer-name t)))
(notes-buffer
(make-indirect-buffer
(or (buffer-base-buffer) (current-buffer))
(generate-new-buffer-name (concat "Notes of " display-name)) t))
(session
(make-org-noter--session
:id (org-noter--get-new-id)
:display-name display-name
:frame
(if (or org-noter-always-create-frame
(catch 'has-session
(dolist (test-session org-noter--sessions)
(when (eq (org-noter--session-frame test-session) (selected-frame))
(throw 'has-session t)))))
(make-frame `((name . ,frame-name) (fullscreen . maximized)))
(set-frame-parameter nil 'name frame-name)
(selected-frame))
:doc-mode document-major-mode
:property-text document-property-value
:notes-file-path notes-file-path
:doc-buffer document-buffer
:notes-buffer notes-buffer
:level (org-element-property :level ast)
:window-behavior (org-noter--property-or-default notes-window-behavior)
:window-location (org-noter--property-or-default notes-window-location)
:doc-split-fraction (org-noter--property-or-default doc-split-fraction)
:auto-save-last-location (org-noter--property-or-default auto-save-last-location)
:hide-other (org-noter--property-or-default hide-other)
:closest-tipping-point (org-noter--property-or-default closest-tipping-point)
:modified-tick -1))
(target-location org-noter--start-location-override)
(starting-point (point)))
(add-hook 'delete-frame-functions 'org-noter--handle-delete-frame)
(push session org-noter--sessions)
(with-current-buffer document-buffer
(cond
;; NOTE(nox): PDF Tools
((eq document-major-mode 'pdf-view-mode)
(setq buffer-file-name document-path)
(pdf-view-mode)
(add-hook 'pdf-view-after-change-page-hook 'org-noter--doc-location-change-handler nil t))
;; NOTE(nox): DocView
((eq document-major-mode 'doc-view-mode)
(setq buffer-file-name document-path)
(doc-view-mode)
(advice-add 'doc-view-goto-page :after 'org-noter--location-change-advice))
;; NOTE(nox): Nov.el
((eq document-major-mode 'nov-mode)
(rename-buffer document-buffer-name)
(advice-add 'nov-render-document :after 'org-noter--nov-scroll-handler)
(add-hook 'window-scroll-functions 'org-noter--nov-scroll-handler nil t))
(t (error "This document handler is not supported :/")))
(org-noter-doc-mode 1)
(setq org-noter--session session)
(add-hook 'kill-buffer-hook 'org-noter--handle-kill-buffer nil t))
(with-current-buffer notes-buffer
(org-noter-notes-mode 1)
;; NOTE(nox): This is needed because a session created in an indirect buffer would use the point of
;; the base buffer (as this buffer is indirect to the base!)
(goto-char starting-point)
(setq buffer-file-name notes-file-path
org-noter--session session
fringe-indicator-alist '((truncation . nil)))
(add-hook 'kill-buffer-hook 'org-noter--handle-kill-buffer nil t)
(add-hook 'window-scroll-functions 'org-noter--set-notes-scroll nil t)
(org-noter--set-text-properties (org-noter--parse-root (vector notes-buffer document-property-value))
(org-noter--session-id session))
(unless target-location
(setq target-location (org-noter--parse-location-property (org-noter--get-containing-heading t)))))
(org-noter--setup-windows session)
;; NOTE(nox): This timer is for preventing reflowing too soon.
(run-with-idle-timer
0.05 nil
(lambda ()
(with-current-buffer document-buffer
(let ((org-noter--inhibit-location-change-handler t))
(when target-location (org-noter--doc-goto-location target-location)))
(org-noter--doc-location-change-handler))))))
(defun org-noter--valid-session (session)
(when session
(if (and (frame-live-p (org-noter--session-frame session))
(buffer-live-p (org-noter--session-doc-buffer session))
(buffer-live-p (org-noter--session-notes-buffer session)))
t
(org-noter-kill-session session)
nil)))
(defmacro org-noter--with-valid-session (&rest body)
(declare (debug (body)))
`(let ((session org-noter--session))
(when (org-noter--valid-session session)
(progn ,@body))))
(defun org-noter--handle-kill-buffer ()
(org-noter--with-valid-session
(let ((buffer (current-buffer))
(notes-buffer (org-noter--session-notes-buffer session))
(doc-buffer (org-noter--session-doc-buffer session)))
;; NOTE(nox): This needs to be checked in order to prevent session killing because of
;; temporary buffers with the same local variables
(when (or (eq buffer notes-buffer)
(eq buffer doc-buffer))
(org-noter-kill-session session)))))
(defun org-noter--handle-delete-frame (frame)
(dolist (session org-noter--sessions)
(when (eq (org-noter--session-frame session) frame)
(org-noter-kill-session session))))
(defun org-noter--parse-root (&optional info)
"Parse and return the root AST.
When used, the INFO argument may be an org-noter session or a vector [NotesBuffer PropertyText].
If nil, the session used will be `org-noter--session'."
(let* ((arg-is-session (org-noter--session-p info))
(session (or (and arg-is-session info) org-noter--session))
root-pos ast)
(cond
((and (not arg-is-session) (vectorp info))
;; NOTE(nox): Use arguments to find heading, by trying to find the outermost parent heading with
;; the specified property
(let ((notes-buffer (aref info 0))
(wanted-prop (aref info 1)))
(unless (and (buffer-live-p notes-buffer) (stringp wanted-prop)
(eq (buffer-local-value 'major-mode notes-buffer) 'org-mode))
(error "Error parsing root with invalid arguments"))
(with-current-buffer notes-buffer
(org-with-wide-buffer
(catch 'break
(org-back-to-heading t)
(while t
(when (string= (org-entry-get nil org-noter-property-doc-file) wanted-prop)
(setq root-pos (copy-marker (point))))
(unless (org-up-heading-safe) (throw 'break t))))))))
((org-noter--valid-session session)
;; NOTE(nox): Use session to find heading
(or (and (= (buffer-chars-modified-tick (org-noter--session-notes-buffer session))
(org-noter--session-modified-tick session))
(setq ast (org-noter--session-ast session))) ; NOTE(nox): Cached version!
;; NOTE(nox): Find session id text property
(with-current-buffer (org-noter--session-notes-buffer session)
(org-with-wide-buffer
(let ((pos (text-property-any (point-min) (point-max) org-noter--id-text-property
(org-noter--session-id session))))
(when pos (setq root-pos (copy-marker pos)))))))))
(unless ast
(unless root-pos (error "Root heading not found"))
(with-current-buffer (marker-buffer root-pos)
(org-with-wide-buffer
(goto-char (marker-position root-pos))
(org-narrow-to-subtree)
(setq ast (car (org-element-contents (org-element-parse-buffer 'greater-element))))
(when (and (not (vectorp info)) (org-noter--valid-session session))
(setf (org-noter--session-ast session) ast
(org-noter--session-modified-tick session) (buffer-chars-modified-tick))))))
ast))
(defun org-noter--get-properties-end (ast &optional force-trim)
(when ast
(let* ((contents (org-element-contents ast))
(section (org-element-map contents 'section 'identity nil t 'headline))
(properties (org-element-map section 'property-drawer 'identity nil t))
properties-end)
(if (not properties)
(org-element-property :contents-begin ast)
(setq properties-end (org-element-property :end properties))
(when (or force-trim
(= (org-element-property :end section) properties-end))
(while (not (eq (char-before properties-end) ?:))
(setq properties-end (1- properties-end))))
properties-end))))
(defun org-noter--set-text-properties (ast id)
(org-with-wide-buffer
(when ast
(let* ((level (org-element-property :level ast))
(begin (org-element-property :begin ast))
(title-begin (+ 1 level begin))
(contents-begin (org-element-property :contents-begin ast))
(properties-end (org-noter--get-properties-end ast t))
(inhibit-read-only t)
(modified (buffer-modified-p)))
(add-text-properties (max 1 (1- begin)) begin '(read-only t))
(add-text-properties begin (1- title-begin) `(read-only t front-sticky t ,org-noter--id-text-property ,id))
(add-text-properties (1- title-begin) title-begin '(read-only t rear-nonsticky t))
(add-text-properties (1- contents-begin) (1- properties-end) '(read-only t))
(add-text-properties (1- properties-end) properties-end
'(read-only t rear-nonsticky t))
(set-buffer-modified-p modified)))))
(defun org-noter--unset-text-properties (ast)
(when ast
(org-with-wide-buffer
(let* ((begin (org-element-property :begin ast))
(end (org-noter--get-properties-end ast t))
(inhibit-read-only t)
(modified (buffer-modified-p)))
(remove-list-of-text-properties (max 1 (1- begin)) end
`(read-only front-sticky rear-nonsticky ,org-noter--id-text-property))
(set-buffer-modified-p modified)))))
(defun org-noter--set-notes-scroll (window &rest ignored)
(when window
(with-selected-window window
(org-noter--with-valid-session
(let* ((level (org-noter--session-level session))
(goal (* (1- level) 2))
(current-scroll (window-hscroll)))
(when (and (bound-and-true-p org-indent-mode) (< current-scroll goal))
(scroll-right current-scroll)
(scroll-left goal t)))))))
(defun org-noter--insert-heading (level title &optional newlines-number location)
"Insert a new heading at LEVEL with TITLE.
The point will be at the start of the contents, after any
properties, by a margin of NEWLINES-NUMBER."
(setq newlines-number (or newlines-number 1))
(org-insert-heading nil t)
(let* ((initial-level (org-element-property :level (org-element-at-point)))
(changer (if (> level initial-level) 'org-do-demote 'org-do-promote))
(number-of-times (abs (- level initial-level))))
(dotimes (_ number-of-times) (funcall changer))
(insert (org-trim (replace-regexp-in-string "\n" " " title)))
(org-end-of-subtree)
(unless (bolp) (insert "\n"))
(org-N-empty-lines-before-current (1- newlines-number))
(when location
(org-entry-put nil org-noter-property-note-location (org-noter--pretty-print-location location))
(when org-noter-doc-property-in-notes
(org-noter--with-valid-session
(org-entry-put nil org-noter-property-doc-file (org-noter--session-property-text session))
(org-entry-put nil org-noter--property-auto-save-last-location "nil"))))
(run-hooks 'org-noter-insert-heading-hook)))
(defun org-noter--narrow-to-root (ast)
(when ast
(save-excursion
(goto-char (org-element-property :contents-begin ast))
(org-show-entry)
(org-narrow-to-subtree)
(org-cycle-hide-drawers 'all))))
(defun org-noter--get-doc-window ()
(org-noter--with-valid-session
(or (get-buffer-window (org-noter--session-doc-buffer session)
(org-noter--session-frame session))
(org-noter--setup-windows org-noter--session)
(get-buffer-window (org-noter--session-doc-buffer session)
(org-noter--session-frame session)))))
(defun org-noter--get-notes-window (&optional type)
(org-noter--with-valid-session
(let ((notes-buffer (org-noter--session-notes-buffer session))
(window-location (org-noter--session-window-location session))
(window-behavior (org-noter--session-window-behavior session))
notes-window)
(or (get-buffer-window notes-buffer t)
(when (or (eq type 'force) (memq type window-behavior))
(if (eq window-location 'other-frame)
(let ((restore-frame (selected-frame)))
(switch-to-buffer-other-frame notes-buffer)
(setq notes-window (get-buffer-window notes-buffer t))
(x-focus-frame restore-frame)
(raise-frame (window-frame notes-window)))
(with-selected-window (org-noter--get-doc-window)
(let ((horizontal (eq window-location 'horizontal-split)))
(setq
notes-window
(if (window-combined-p nil horizontal)
;; NOTE(nox): Reuse already existent window
(let ((sibling-window (or (window-next-sibling) (window-prev-sibling))))
(or (window-top-child sibling-window) (window-left-child sibling-window)
sibling-window))
(if horizontal
(split-window-right (ceiling (* (car (org-noter--session-doc-split-fraction session))
(window-total-width))))
(split-window-below (ceiling (* (cdr (org-noter--session-doc-split-fraction session))
(window-total-height)))))))))
(set-window-buffer notes-window notes-buffer))
notes-window)))))
(defun org-noter--setup-windows (session)
"Setup windows when starting session, respecting user configuration."
(when (org-noter--valid-session session)
(with-selected-frame (org-noter--session-frame session)
(delete-other-windows)
(let* ((doc-buffer (org-noter--session-doc-buffer session))
(doc-window (selected-window))
(notes-buffer (org-noter--session-notes-buffer session))
notes-window)
(set-window-buffer doc-window doc-buffer)
(set-window-dedicated-p doc-window t)
(with-current-buffer notes-buffer
(org-noter--narrow-to-root (org-noter--parse-root session))
(setq notes-window (org-noter--get-notes-window 'start))
(org-noter--set-notes-scroll notes-window))))))
(defmacro org-noter--with-selected-notes-window (error-str &rest body)
(declare (debug ([&optional stringp] body)))
(let ((with-error (stringp error-str)))
`(org-noter--with-valid-session
(let ((notes-window (org-noter--get-notes-window)))
(if notes-window
(with-selected-window notes-window
,(if with-error
`(progn ,@body)
(if body
`(progn ,error-str ,@body)
`(progn ,error-str))))
,(when with-error `(user-error "%s" ,error-str)))))))
(defun org-noter--notes-window-behavior-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-behavior)) ast))
value)
(when (and (stringp property) (> (length property) 0))
(setq value (car (read-from-string property)))
(when (listp value) value))))
(defun org-noter--notes-window-location-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-location)) ast))
value)
(when (and (stringp property) (> (length property) 0))
(setq value (intern property))
(when (memq value '(horizontal-split vertical-split other-frame)) value))))
(defun org-noter--doc-split-fraction-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-doc-split-fraction)) ast))
value)
(when (and (stringp property) (> (length property) 0))
(setq value (car (read-from-string property)))
(when (consp value) value))))
(defun org-noter--auto-save-last-location-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-auto-save-last-location)) ast)))
(when (and (stringp property) (> (length property) 0))
(if (intern property) t 'disable))))
(defun org-noter--hide-other-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-hide-other)) ast)))
(when (and (stringp property) (> (length property) 0))
(if (intern property) t 'disable))))
(defun org-noter--closest-tipping-point-property (ast)
(let ((property (org-element-property (intern (concat ":" org-noter--property-closest-tipping-point)) ast)))
(when (and (stringp property) (> (length property) 0))
(ignore-errors (string-to-number property)))))
(defun org-noter--doc-approx-location-cons (&optional precise-info)
(cond
((memq major-mode '(doc-view-mode pdf-view-mode))
(cons (image-mode-window-get 'page) (if (numberp precise-info) precise-info 0)))
((eq major-mode 'nov-mode)
(cons nov-documents-index (if (integerp precise-info)
precise-info
(max 1 (/ (+ (window-start) (window-end nil t)) 2)))))
(t (error "Unknown document type %s" major-mode))))
(defun org-noter--doc-approx-location (&optional precise-info force-new-ref)
(let ((window (if (org-noter--valid-session org-noter--session)
(org-noter--get-doc-window)
(selected-window))))
(cl-assert window)
(with-selected-window window
(or (run-hook-with-args-until-success 'org-noter--doc-approx-location-hook major-mode
precise-info force-new-ref)
(org-noter--doc-approx-location-cons precise-info)))))
(defun org-noter--location-change-advice (&rest _)
(org-noter--with-valid-session (org-noter--doc-location-change-handler)))
(defun org-noter--nov-scroll-handler (&rest _)
(when org-noter--nov-timer (cancel-timer org-noter--nov-timer))
(unless org-noter--inhibit-location-change-handler
(setq org-noter--nov-timer (run-with-timer 0.25 nil 'org-noter--doc-location-change-handler))))
(defsubst org-noter--doc-file-property (headline)
(org-element-property (intern (concat ":" org-noter-property-doc-file)) headline))
(defun org-noter--check-location-property (arg)
(let ((property (if (stringp arg) arg
(org-element-property (intern (concat ":" org-noter-property-note-location)) arg))))
(when (and (stringp property) (> (length property) 0))
(or (run-hook-with-args-until-success 'org-noter--check-location-property-hook property)
(let ((value (car (read-from-string property))))
(or (and (consp value) (integerp (car value)) (numberp (cdr value)))
(integerp value)))))))
(defun org-noter--parse-location-property (arg)
(let ((property (if (stringp arg) arg
(org-element-property (intern (concat ":" org-noter-property-note-location)) arg))))
(when (and (stringp property) (> (length property) 0))
(or (run-hook-with-args-until-success 'org-noter--parse-location-property-hook property)
(let ((value (car (read-from-string property))))
(cond ((and (consp value) (integerp (car value)) (numberp (cdr value))) value)
((integerp value) (cons value 0))))))))
(defun org-noter--pretty-print-location (location)
(org-noter--with-valid-session
(or (run-hook-with-args-until-success 'org-noter--pretty-print-location-hook location)
(format "%s" (cond
((memq (org-noter--session-doc-mode session) '(doc-view-mode pdf-view-mode))
(if (or (not (cdr location)) (<= (cdr location) 0))
(car location)
location))
((eq (org-noter--session-doc-mode session) 'nov-mode)
(if (or (not (cdr location)) (<= (cdr location) 1))
(car location)
location)))))))
(defun org-noter--get-containing-heading (&optional include-root)
"Get smallest containing heading that encloses the point and has location property.
If the point isn't inside any heading with location property, return the outer heading.
When INCLUDE-ROOT is non-nil, the root heading is also eligible to be returned."
(org-noter--with-valid-session
(org-with-wide-buffer
(unless (org-before-first-heading-p)
(org-back-to-heading t)
(let (previous)
(catch 'break
(while t
(let ((prop (org-noter--check-location-property (org-entry-get nil org-noter-property-note-location)))
(at-root (equal (org-noter--session-id session)
(get-text-property (point) org-noter--id-text-property)))
(heading (org-element-at-point)))
(when (and prop (or include-root (not at-root)))
(throw 'break heading))
(when (or at-root (not (org-up-heading-safe)))
(throw 'break (if include-root heading previous)))
(setq previous heading)))))))))
(defun org-noter--doc-get-page-slice ()
"Return (slice-top . slice-height)."
(let* ((slice (or (image-mode-window-get 'slice) '(0 0 1 1)))
(slice-top (float (nth 1 slice)))
(slice-height (float (nth 3 slice))))
(when (or (> slice-top 1)
(> slice-height 1))
(let ((height (cdr (image-size (image-mode-window-get 'image) t))))
(setq slice-top (/ slice-top height)
slice-height (/ slice-height height))))
(cons slice-top slice-height)))
(defun org-noter--conv-page-scroll-percentage (scroll)
(let* ((slice (org-noter--doc-get-page-slice))
(display-height (cdr (image-display-size (image-get-display-property))))
(display-percentage (/ scroll display-height))
(percentage (+ (car slice) (* (cdr slice) display-percentage))))
(max 0 (min 1 percentage))))
(defun org-noter--conv-page-percentage-scroll (percentage)
(let* ((slice (org-noter--doc-get-page-slice))
(display-height (cdr (image-display-size (image-get-display-property))))
(display-percentage (min 1 (max 0 (/ (- percentage (car slice)) (cdr slice)))))
(scroll (max 0 (floor (* display-percentage display-height)))))
scroll))
(defun org-noter--get-precise-info ()
(org-noter--with-valid-session
(let ((window (org-noter--get-doc-window))
(mode (org-noter--session-doc-mode session))
event)
(with-selected-window window
(cond
((run-hook-with-args-until-success 'org-noter--get-precise-info-hook mode))
((eq mode 'pdf-view-mode)
(if (pdf-view-active-region-p)
(cadar (pdf-view-active-region))
(while (not (and (eq 'mouse-1 (car event))
(eq window (posn-window (event-start event)))))
(setq event (read-event "Click where you want the start of the note to be!")))
(org-noter--conv-page-scroll-percentage (+ (window-vscroll)
(cdr (posn-col-row (event-start event)))))))
((eq mode 'doc-view-mode)
(while (not (and (eq 'mouse-1 (car event))
(eq window (posn-window (event-start event)))))
(setq event (read-event "Click where you want the start of the note to be!")))
(org-noter--conv-page-scroll-percentage (+ (window-vscroll)
(cdr (posn-col-row (event-start event))))))
((eq mode 'nov-mode)
(if (region-active-p)
(min (mark) (point))
(while (not (and (eq 'mouse-1 (car event))
(eq window (posn-window (event-start event)))))
(setq event (read-event "Click where you want the start of the note to be!")))
(posn-point (event-start event)))))))))
(defun org-noter--show-arrow ()
(when (and org-noter--arrow-location
(window-live-p (aref org-noter--arrow-location 1)))
(with-selected-window (aref org-noter--arrow-location 1)
(pdf-util-tooltip-arrow (aref org-noter--arrow-location 2))))
(setq org-noter--arrow-location nil))
(defun org-noter--doc-goto-location (location)
"Go to location specified by LOCATION."
(org-noter--with-valid-session
(let ((window (org-noter--get-doc-window))
(mode (org-noter--session-doc-mode session)))
(with-selected-window window
(cond
((run-hook-with-args-until-success 'org-noter--doc-goto-location-hook mode location))
((memq mode '(doc-view-mode pdf-view-mode))
(if (eq mode 'doc-view-mode)
(doc-view-goto-page (car location))
(pdf-view-goto-page (car location))
;; NOTE(nox): This timer is needed because the tooltip may introduce a delay,
;; so syncing multiple pages was slow
(when (>= org-noter-arrow-delay 0)
(when org-noter--arrow-location (cancel-timer (aref org-noter--arrow-location 0)))
(setq org-noter--arrow-location
(vector (run-with-idle-timer org-noter-arrow-delay nil 'org-noter--show-arrow)
window
(cdr location)))))
(image-scroll-up (- (org-noter--conv-page-percentage-scroll (cdr location))
(window-vscroll))))
((eq mode 'nov-mode)
(setq nov-documents-index (car location))
(nov-render-document)
(goto-char (cdr location))
(recenter)))
;; NOTE(nox): This needs to be here, because it would be issued anyway after
;; everything and would run org-noter--nov-scroll-handler.
(redisplay)))))
(defun org-noter--compare-location-cons (comp l1 l2)
"Compare L1 and L2, which are location cons.
See `org-noter--compare-locations'"
(cl-assert (and (consp l1) (consp l2)))
(cond ((eq comp '=)
(and (= (car l1) (car l2))
(= (cdr l1) (cdr l2))))
((eq comp '<)
(or (< (car l1) (car l2))
(and (= (car l1) (car l2))
(< (cdr l1) (cdr l2)))))
((eq comp '<=)
(or (< (car l1) (car l2))
(and (= (car l1) (car l2))
(<= (cdr l1) (cdr l2)))))
((eq comp '>)
(or (> (car l1) (car l2))
(and (= (car l1) (car l2))
(> (cdr l1) (cdr l2)))))
((eq comp '>=)
(or (> (car l1) (car l2))
(and (= (car l1) (car l2))
(>= (cdr l1) (cdr l2)))))
((eq comp '>f)
(or (> (car l1) (car l2))
(and (= (car l1) (car l2))
(< (cdr l1) (cdr l2)))))
(t (error "Comparison operator %s not known" comp))))
(defun org-noter--compare-locations (comp l1 l2)
"Compare L1 and L2.
When COMP is '<, '<=, '>, or '>=, it works as expected.
When COMP is '>f, it will return t when L1 is a page greater than
L2 or, when in the same page, if L1 is the _f_irst of the two."
(cond ((not l1) nil)
((not l2) t)
(t
(setq l1 (or (run-hook-with-args-until-success 'org-noter--convert-to-location-cons-hook l1) l1)
l2 (or (run-hook-with-args-until-success 'org-noter--convert-to-location-cons-hook l2) l2))
(org-noter--compare-location-cons comp l1 l2))))
(defun org-noter--show-note-entry (session note)
"This will show the note entry and its children.
Every direct subheading _until_ the first heading that doesn't
belong to the same view (ie. until a heading with location or
document property) will be opened."
(save-excursion
(goto-char (org-element-property :contents-begin note))
(org-show-set-visibility t)
(org-element-map (org-element-contents note) 'headline
(lambda (headline)
(let ((doc-file (org-noter--doc-file-property headline)))
(if (or (and doc-file (not (string= doc-file (org-noter--session-property-text session))))
(org-noter--check-location-property headline))
t
(goto-char (org-element-property :begin headline))
(org-show-entry)
(org-show-children)
nil)))
nil t org-element-all-elements)))
(defun org-noter--focus-notes-region (view-info)
(org-noter--with-selected-notes-window
(if (org-noter--session-hide-other session)
(save-excursion
(goto-char (org-element-property :begin (org-noter--parse-root)))
(outline-hide-subtree))
(org-cycle-hide-drawers 'all))
(let* ((notes-cons (org-noter--view-info-notes view-info))
(regions (or (org-noter--view-info-regions view-info)
(org-noter--view-info-prev-regions view-info)))
(point-before (point))
target-region
point-inside-target-region)
(cond
(notes-cons
(dolist (note-cons notes-cons) (org-noter--show-note-entry session (car note-cons)))
(setq target-region (or (catch 'result (dolist (region regions)