forked from weirdNox/org-noter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
org-noter-core.el
2604 lines (2233 loc) · 117 KB
/
org-noter-core.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-core.el --- Core functions of Org-noter -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2019 Gonçalo Santos
;; Author: Gonçalo Santos (aka. weirdNox@GitHub)
;; 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/>.
;;; Code:
(require 'org)
(require 'org-element)
(require 'cl-lib)
(declare-function org-noter "org-noter")
(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 org-attach-dir "org-attach")
(declare-function org-attach-file-list "org-attach")
;; --------------------------------------------------------------------------------
;;; User variables
(defgroup org-noter nil
"A synchronized, external annotator."
:group 'convenience
:version "25.3.1")
(defgroup org-noter-layout nil
"Org-noter layout and visibility variables."
:group 'org-noter
:version "28.2")
(defgroup org-noter-navigation nil
"Org-noter navigation and display variables."
:group 'org-noter
:version "28.2")
(defgroup org-noter-insertion nil
"Org-noter note-insertion variables."
:group 'org-noter
:version "28.2")
(defcustom org-noter-supported-modes '(doc-view-mode pdf-view-mode nov-mode djvu-read-mode)
"Major modes that are supported by org-noter."
:group 'org-noter
:type '(repeat symbol))
(defvar org-noter--doc-extensions nil
"List of extensions handled by org-noter when documents are moved.
Used by `org-noter--update-doc-rename-in-notes'. This variable gets filled in by supported modes, so it is not a `defcustom' variable.")
(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-insertion
:type 'string)
(defcustom org-noter-notes-window-behavior '(start scroll)
"Specifies situations for which the notes window is created.
When the list contains:
- `start', the notes window will be created when starting an
`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
"The default document/notes window layout.
Options are: \"Horizontal\", \"Vertical\", or \"Other 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-layout
: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-layout
:type '(cons (number :tag "Horizontal fraction") (number :tag "Vertical fraction")))
(defcustom org-noter-auto-save-last-location nil
"Option to save document location in notes file.
When non-nil, save the last visited location automatically; when
starting a new session, go to that location. When nil, sessions
start at the beginning of the document."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-prefer-root-as-file-level nil
"Option to preferentially use the file-level property drawer.
When non-nil, org-noter will always try to return the file-level
property drawer even when there are headings.
With the default value nil, org-noter will always use the first
heading as root when there is at least one heading."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-hide-other t
"Hide notes that are not linked to the current document page.
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-layout
:type 'boolean)
(defcustom org-noter-always-create-frame t
"Create a new frame for each document session.
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-disable-narrowing nil
"Disable narrowing in notes/org buffer."
:group 'org-noter-layout
:type 'boolean)
(defcustom org-noter-use-indirect-buffer t
"Use indirect buffer for notes.
When non-nil, org-noter will create an indirect buffer of the
calling org file as a note buffer of the session. When nil, it
will use the real buffer."
:group 'org-noter
:type 'boolean)
(defcustom org-noter-swap-window nil
"Swap the left/right or top/bottom layout of the doc and notes.
By default `org-noter' will make a session by setting the buffer
of the selected window to the document buffer then split with the
window of the notes buffer on the right.
If this variable is non-nil, the buffers of the two windows will
be the other way around."
:group 'org-noter-layout
:type 'boolean)
(defcustom org-noter-suggest-from-attachments t
"Suggest document files from attachments (in an Org file).
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-insertion
:type 'boolean)
(defcustom org-noter-insert-selected-text-inside-note t
"Option to append selected text to existing note.
When non-nil (default), it will automatically append the selected
text into an existing note.
When nil, selected text will not be appended to existing
note (not recommended)."
:group 'org-noter-insertion
: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-navigation
:type 'number)
(defcustom org-noter-default-notes-file-names '("Notes.org")
"List of possible names for the default notes file.
The list is 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
"Delay (in seconds) afte a sync before showing the tooltip arrow.
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-navigation
:type 'number)
(defcustom org-noter-arrow-horizontal-offset -20
"Horizontal offset of the tooltip arrow relative to a precise location.
Units are display pixels; positive values move the arrow to the
right, while negative values move it to the left. The intent is
to move the arrow so that it does not cover text of intereest,
but roundoff errors cause the arrow position still to be
dependent upon magnification at the 1-em level"
:group 'org-noter-navigation
:type 'number
:version "28.2")
(defcustom org-noter-arrow-foreground-color "orange red"
"Default color of the tooltip arrow."
:group 'org-noter-navigation
:type 'string
:version "28.2")
(defcustom org-noter-arrow-background-color "white"
"Default background color of the tooltip arrow."
:group 'org-noter-navigation
:type 'string
:version "28.2")
(defcustom org-noter-vscroll-buffer 5
"Minimum number of document display lines to leave above precise note.
Navigation will scroll precise notes to the top of the buffer. A
value of 0 places the precise note at the top of the window when
possible. A positive number leaves some context above the
precise note location."
:group 'org-noter-navigation
:type 'number
:version "28.2")
(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
"Do not prompt for a note title.
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 one of
defaults: the selected text (if it does not exceed
`org-noter-max-short-selected-text-length') or
`org-noter-default-heading-title'."
:group 'org-noter-insertion
:type 'boolean)
(defcustom org-noter-kill-frame-at-session-end t
"Close the frame when exiting a session.
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-insertion
:type 'hook)
(defcustom org-noter-create-session-from-document-hook '(org-noter--create-session-from-document-file-default)
"Hook that is invoked when `org-noter' is invoked from a document."
:group 'org-noter
:type 'hook)
(defcustom org-noter-highlight-selected-text nil
"Highlight selected text when creating notes.
If non-nil, highlight selected-text when creating notes. This
variable is temporarily toggled by prefixing the insertion
command with any non-nil prefix such as \\[universal-argument]."
:group 'org-noter-insertion
:type 'boolean
:version "28.2")
(defcustom org-noter-max-short-selected-text-length 80
"Maximum length of a short text selection.
Short text selections are the primary default note title. When
they are quoted in the note, they are quoted as
``short-selected-text'' rather than inside a QUOTE-block."
:group 'org-noter-insertion
:type 'integer
:version "28.2")
(defcustom org-noter-find-additional-notes-functions nil
"List of functions that map a document to an Org-noter filepath.
The functions in this list must accept 1 argument, a file name.
The argument will be given by `org-noter'.
The return value must be a path to an org file. No matter if
it's an absolute or relative path, the file name will be expanded
to each directory set in `org-noter-notes-search-path' to test if
it exists.
If it exists, it will be listed as a candidate that `org-noter'
will have the user select to use as the note file of the
document."
:group 'org-noter
:type 'hook
:version "28.2")
(defcustom org-noter-headline-title-decoration ""
"Decoration (emphasis) for the headline title string.
If you use the Org STARTUP option 'entitiespretty', filenames
with underscores will end up looking ugly. This string is
prepended and appended to the document title in the top-level
headline, making it look nicer.
Reasonable choices are: /, *, =, ~, _
With '/', 'The_Title' would become '/The_Title/'."
:group 'org-noter
:type 'string
:version "28.2")
(defface org-noter-no-notes-exist-face
'((t
:foreground "chocolate"
:weight bold))
"Face for modeline note count, when 0."
:group 'org-noter-navigation)
(defface org-noter-notes-exist-face
'((t
:foreground "SpringGreen"
:weight bold))
"Face for modeline note count, when not 0."
:group 'org-noter-navigation)
;; --------------------------------------------------------------------------------
;;; Integration with other packages
(defgroup org-noter-module-hooks nil
"Hooks for integrating org-noter with other packages (pdfview, nov, djvu)."
:group 'org-noter
:version "28.2")
(defcustom org-noter--get-location-property-hook nil
"The list of functions that will return the note location of an org element.
These functions must accept one argument, an org element.
These functions is used by `org-noter--parse-location-property' and
`org-noter--check-location-property' when they can't find the note location
of the org element given to them, that org element will be passed to
the functions in this list."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--get-containing-element-hook '(org-noter--get-containing-heading
org-noter--get-containing-property-drawer)
"List of functions that return the Org element of a note.
These functions will be called by
`org-noter--get-containing-element' to get the Org element of the
note at point."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-parse-document-property-hook nil
"The list of functions that parse NOTER_DOCUMENT for a filename.
Or whatever the property `org-noter-property-doc-file' is set to.
This is used by `org-noter--get-or-read-document-property' and
`org-noter--doc-file-property'.
This is added for integration with other packages.
For example, the module `org-noter-citar' adds the function
`org-noter-citar-find-document-from-refs' to this list which when
the property \"NOTER_DOCUMENT\" (the default value of
`org-noter-property-doc-file') of an org file passed to it is a
citation key, it will return the path to the note file associated
with the citation key and that path will be used for other
operations instead of the real value of the property."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-get-buffer-file-name-hook nil
"Functions that when passed a major mode, return the current buffer file name.
This is used by the `org-noter' command to determine the file name when
user calls `org-noter' on a document buffer.
For example, `nov-mode', a renderer for EPUB documents uses a unique variable
called `nov-file-name' to store the file name of its document while the other
major modes use the variable `buffer-file-name'."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-set-up-document-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-get-selected-text-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--check-location-property-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--parse-location-property-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--pretty-print-location-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--pretty-print-location-for-title-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--convert-to-location-cons-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--doc-goto-location-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--pretty-print-highlight-location-hook nil
"Hook that serializes a highlight location so that it can be stored in org."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--get-highlight-location-hook nil
"Hook that runs to get the location of a highlight."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--add-highlight-hook nil
"Hook called to highlight selected text when creating notes.
When a note is created this will be given `MAJOR-MODE' and
`PRECISE-INFO'. For example, this hook can be used in pdf-mode
to add a permanent highlight to the document."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--note-after-tipping-point-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--relative-position-to-view-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--get-precise-info-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--get-current-view-hook nil
"TODO."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--doc-approx-location-hook nil
"This returns an approximate location if no precise info is passed: (PAGE 0)
or if precise info is passed, it's (PAGE V . H)."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-create-skeleton-functions nil
"List of functions that convert document outline into noter headlines.
The functions will be given a major mode of the document and must
return a non-nil value when the outline is created.
Used by `org-noter-create-skeleton'."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter-open-document-functions nil
"Functions that gives a buffer when passed with a document property.
Used by `org-noter--create-session' when creating a new session."
:group 'org-noter-module-hooks
:type 'hook)
(defcustom org-noter--show-arrow-hook nil
"List of functions that show precise note location in document.
For example, see `org-noter-pdf--show-arrow'."
:group 'org-noter-module-hooks
:type 'hook)
;; --------------------------------------------------------------------------------
;;; 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 org-noter--arrow-location nil
"A vector that shows where the arrow should appear, when idling.
Format: [TIMER WINDOW TOP LEFT]")
(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--note-search-element-type '(headline)
"List of elements that should be searched for notes.")
(defconst org-noter--id-text-property 'org-noter-session-id
"Text property used to mark the headings with open sessions.")
(defvar org-noter--url-regexp
(concat
"\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
"nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
"\\(//[-a-z0-9_.]+:[0-9]*\\)?"
(let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
(punct "!?:;.,"))
(concat
"\\(?:"
;; Match paired parentheses, e.g. in Wikipedia URLs:
;; http://thread.gmane.org/[email protected]
"[" chars punct "]+" "(" "[" chars punct "]+" ")"
"\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
"\\|"
"[" chars punct "]+" "[" chars "]"
"\\)"))
"\\)")
"Regular expression that matches URLs.")
(defvar org-noter--no-sessions-remove-advice-hooks nil
"List of functions to remove advice when all sessions are closed.")
;; --------------------------------------------------------------------------------
;;; Utility functions
(defun org-noter--no-heading-p ()
"Return nil if the current buffer has atleast one heading.
Otherwise return the maximum value for point."
(save-excursion
(and (org-before-first-heading-p) (org-next-visible-heading 1))))
(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-parse-link (s)
(pcase (with-temp-buffer
(let ((org-inhibit-startup nil))
(insert s)
(org-mode)
(goto-char (point-min))
(org-element-link-parser)))
(`nil nil)
(link link)))
(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))
(link-p (or (string-match-p org-link-bracket-re document-property-value)
(string-match-p org-noter--url-regexp document-property-value)))
(display-name (if raw-value-not-empty
(org-element-property :raw-value ast)
(if link-p
document-property-value
(file-name-nondirectory document-property-value))))
(frame-name (format "Emacs Org-noter - %s" display-name))
(document (or (run-hook-with-args-until-success 'org-noter-open-document-functions document-property-value)
(if link-p
(progn (org-link-open-from-string document-property-value)
(current-buffer))
(find-file-noselect document-property-value))))
(document-major-mode (if (or link-p (eq document (current-buffer)))
document-property-value
(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 document)
(notes-buffer
(progn (when (and org-window-config-before-follow-link link-p)
(set-window-configuration org-window-config-before-follow-link))
(if org-noter-use-indirect-buffer
(make-indirect-buffer
(or (buffer-base-buffer)
(current-buffer))
(generate-new-buffer-name (concat "Notes of " display-name)) t)
(current-buffer))))
(single (eq (or (buffer-base-buffer document-buffer)
document-buffer)
(or (buffer-base-buffer notes-buffer)
notes-buffer)))
(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 (or (org-element-property :level ast) 0)
: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
(or (run-hook-with-args-until-success 'org-noter-set-up-document-hook document-major-mode)
(run-hook-with-args-until-success 'org-noter-set-up-document-hook document-property-value)
(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-element t)))))
;; NOTE(nox): This timer is for preventing reflowing too soon.
(unless single
(run-with-idle-timer
0.05 nil
(lambda ()
;; NOTE(ahmed-shariff): setup-window run here to avoid crash when notes buffer not setup in time
(org-noter--setup-windows session)
(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) (or (stringp wanted-prop)
(eq 'link (org-element-type 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
(while t
(let ((document-property (org-entry-get nil org-noter-property-doc-file t)))
(when (string= (or (run-hook-with-args-until-success 'org-noter-parse-document-property-hook document-property)
document-property)
wanted-prop)
(setq root-pos (copy-marker (if (and org-noter-prefer-root-as-file-level
(save-excursion
(goto-char (point-min))
(eq 'property-drawer (org-element-type (org-element-at-point)))))
(point-min)
(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 (if (or org-noter-prefer-root-as-file-level (org-noter--no-heading-p))
(setq root-pos (copy-marker (point-min)))
(org-next-visible-heading 1)
(setq root-pos (copy-marker (point)))))
(with-current-buffer (marker-buffer root-pos)
(org-with-point-at (marker-position root-pos)
(org-back-to-heading-or-point-min t)
(if (org-at-heading-p)
(org-narrow-to-subtree)
(org-hide-drawer-toggle 'force))
(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 (or (org-element-map section 'property-drawer 'identity nil t)
(org-element-map contents '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 (or (org-element-property :level ast) 0))
(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)))
(if (= level 0)
(when properties-end
(add-text-properties contents-begin properties-end
`(read-only t rear-nonsticky t ,org-noter--id-text-property ,id))
(set-buffer-modified-p modified))
(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))
(when properties-end
(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)))
(when end
(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.
When LOCATION is provded, it is written into the property drawer
of the heading under `org-noter-property-note-location' (default:
NOTER_PAGE)."
(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 (and ast (not (org-noter--no-heading-p)))
(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)
"Conjure the notes-window from the void."
(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--relocate-notes-window (notes-buffer)
"Clear the notes-window and (re)locate it.