forked from bastibe/annotate.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.el
3716 lines (3358 loc) · 174 KB
/
annotate.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
;;; annotate.el --- annotate files without changing them -*- lexical-binding: t; -*-
;; Copyright © 2015 Bastian Bechtold and contributors:
;; Naoya Yamashita (2018)
;; Università degli Studi di Palermo (2019)
;; Author: Bastian Bechtold
;; Maintainer: Bastian Bechtold <[email protected]>, cage <[email protected]>
;; URL: https://github.com/bastibe/annotate.el
;; Created: 2015-06-10
;; Version: 2.2.3
;; This file is NOT part of GNU Emacs.
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; This package provides the minor mode annotate-mode, which can add
;; annotations to arbitrary files without changing the files
;; themselves. Annotations are saved in the annotate-file
;; (~/.annotations by default).
;;
;; To add annotations to a file, select a region and hit C-c C-a. The
;; region will be underlined, and the annotation will be displayed in
;; the right margin. Annotations are saved whenever the file is saved.
;;
;; Use C-c ] to jump to the next annotation and C-c [ to jump to
;; the previous annotation. Use M-x annotate-export-annotations to
;; save annotations as a no-difference diff file.
;; Important note for developers: annotation can not overlaps and newline character
;; can not be annotated.
;;; Code:
(require 'info)
(require 'cl-lib)
;;;###autoload
(defgroup annotate nil
"Annotate files without changing them."
:version "2.2.3"
:group 'text)
(defvar annotate-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-a") #'annotate-annotate)
(define-key map (kbd "C-c C-d") #'annotate-delete-annotation)
(define-key map (kbd "C-c C-p") #'annotate-change-annotation-text-position)
(define-key map (kbd "C-c C-c") #'annotate-change-annotation-colors)
(define-key map (kbd "C-c C-s") #'annotate-show-annotation-summary)
(define-key map (kbd "C-c ]") #'annotate-goto-next-annotation)
(define-key map (kbd "C-c [") #'annotate-goto-previous-annotation)
map))
;;;###autoload
(define-minor-mode annotate-mode
"Toggle Annotate mode.
See https://github.com/bastibe/annotate.el/ for documentation."
:lighter " Ann"
:group 'annotate
(annotate-initialize-maybe))
(defcustom annotate-file (locate-user-emacs-file "annotations" ".annotations")
"File where annotations are stored."
:type 'file)
(defcustom annotate-file-buffer-local nil
"If non nil (default `nil'), for each annotated file `filename', a database
`filename.notes', containing the annotations, is generated in the
same directory that contains `filename'."
:type 'string)
(defcustom annotate-buffer-local-database-extension "notes"
"The extension appended to the annotated filename to get the
name of the local database annotation"
:type 'string)
(defcustom annotate-highlight-faces '((:underline "#EEF192")
(:underline "#92EEF1")
(:underline "#F192EE"))
"List of faces for annotated text."
:type 'list)
(defcustom annotate-annotation-text-faces '((:background "#EEF192" :foreground "black")
(:background "#92EEF1" :foreground "black")
(:background "#F192EE" :foreground "black"))
"List of faces for annotation's text."
:type 'list)
(defface annotate-prefix
'((t (:inherit default)))
"Face for character used to pad annotation.
This is the fill space between text lines and annotation text.")
(defcustom annotate-annotation-column 85
"Where annotations appear."
:type 'number)
(defcustom annotate-diff-export-options ""
"Options passed to `diff' in `annotate-export-annotations'.
This is used when diffing between a buffer with and without
integrated annotations.
Note that there is an implicit `-u' at the end of default options
that Emacs passes to the diff program."
:type 'string)
(defcustom annotate-use-messages t
"Whether status messages may appear in the minibuffer."
:type 'boolean)
(defcustom annotate-popup-warning-indirect-buffer t
"Whether an information popup message is shown when killing an
annotated indirect buffer."
:type 'boolean)
(defcustom annotate-integrate-marker " ANNOTATION: "
"Marker that is written before every integrated annotation."
:type 'string)
(defcustom annotate-integrate-highlight ?~
"Character used to underline an annotated text."
:type 'character)
(defcustom annotate-fallback-comment "#"
"When variable `COMMENT-START' is nil use this string instead."
:type 'string)
(defcustom annotate-blacklist-major-mode '()
"Major modes in which to prevent auto-activation of command `annotate-mode'.
This is consulted when visiting a file.
It can be useful when some mode does not work well with
annotate (like source blocks in `org-mode') as this ensure that it
will be never loaded, see `annotate-initialize-maybe'."
:type '(repeat symbol))
(defcustom annotate-summary-ask-query t
"If non nil a prompt asking for a query to filter the database
before showing it in a summary window is used. If nil the
database is not filtered at all."
:type 'boolean)
(defcustom annotate-database-confirm-deletion t
"If non nil a prompt asking confirmation before deleting a
database file that is going to be empty after saving an annotated
file will be shown."
:type 'boolean)
(defcustom annotate-annotation-confirm-deletion nil
"If non nil a prompt asking confirmation before deleting an
annotation file will be shown."
:type 'boolean)
(defcustom annotate-database-confirm-import t
"If non nil a prompt asking confirmation before importing a
database file will be shown."
:type 'boolean)
(defcustom annotate-annotation-max-size-not-place-new-line 15
"The maximum \"string-width\" allowed for an annotation to be
placed on the right margin of the window instead of its own line
after the annotated text."
:type 'number)
(defconst annotate-allowed-positioning-policy
'(:by-length :margin :new-line)
"The allowed values for annotation positioning")
(defcustom annotate-annotation-position-policy :by-length
"Policy for annotation's position:
- :new-line
always in a new-line
- :margin
always on right margin
- :by-length
decide by text's length
if the length is more than the value of
`ANNOTATE-ANNOTATION-MAX-SIZE-NOT-PLACE-NEW-LINE' place the
annotation on a new line, place on the right margin
otherwise."
:type 'symbol)
(defcustom annotate-use-echo-area nil
"Whether annotation text should appear in the echo area only when mouse
id positioned over the annotated text instead of positioning them in
the the buffer (the default)."
:type 'boolean)
(defcustom annotate-print-annotation-under-cursor nil
"Whether annotation text should appear in the minibuffer when
the cursor is positioned over an annotated text (default: nil).
Important note: for this changes to take effect also
annotate-use-echo-area must be non nil"
:type 'boolean)
(defcustom annotate-print-annotation-under-cursor-prefix "ANNOTATION: "
"Prefix that is printed before annotation in the minibuffer when
annotate-print-annotation-under-cursor is non nil"
:type 'string)
(defcustom annotate-print-annotation-under-cursor-delay 0.5
"The delay (in seconds) after an annotation id printed in the
minibuffer, when the pursor is placed over an annotated text.
This variable works only if `annotate-print-annotation-under-cursor' is non nil"
:type 'float)
(defcustom annotate-warn-if-hash-mismatch t
"Whether a warning message should be printed if a mismatch
occurs, for an annotated file, between the hash stored in the
database annotations and the hash calculated from the actual
file.
This usually happens if an annotated file (a file with an entry in the
database) is saved with annotated-mode *not* active or the file
has been modified outside Emacs."
:type 'boolean)
(defcustom annotate-endline-annotate-whole-line t
"Whether trying to annotate the end of line character will
annotate the whole line before (or after if the line is composed
by the newline character only) instead."
:type 'boolean)
(defcustom annotate-search-region-lines-delta 2
"When the annotated file is out of sync with its annotation
database the software looks for annotated text in the region with
delta equals to the value of this variable. Units are in number
of lines. The center of the region is the position of the
annotation as defined in the database."
:type 'number)
(defconst annotate-prop-chain-position
'position)
(defconst annotate-prop-chain-pos-marker-first
0)
(defconst annotate-prop-chain-pos-marker-last
-1)
(defconst annotate-warn-file-changed-control-string
(concat "The file '%s' has changed on disk "
"from the last time the annotations were saved.\n"
"Chances are that they will not be displayed correctly.")
"The message to warn the user that file has been modified and
annotations positions could be outdated.")
(defconst annotate-warn-file-searching-annotation-failed-control-string
(concat "The file '%s' has changed on disk "
"from the last time the annotations were saved and "
"Unfortunately was not possible to show annotation %S "
"because i failed looking for test %S.")
"The message to warn the user that file has been modified and
an annotations could not be restored.")
(defconst annotate-warn-buffer-has-no-valid-file
"Annotations can not be saved: unable to find a file for buffer %S"
"The message to warn the user that a buffer it is not visiting
a valid file to be annotated.")
(defconst annotate-popup-warn-killing-an-indirect-buffer
(concat "You killed an indirect buffer that contains annotation.\n"
"Annotate mode can not save annotation in an indirect buffer.\n"
"The buffer's content has been saved in a regular buffer "
"(together with its annotations) named:\n\n%S\n\n"
"If you want you can save that buffer in a file and "
"the annotations will be saved as well.")
"The message to warn the user that an annotated indirect buffer
has been killed.")
(defconst annotate-error-summary-win-filename-invalid
"Error: File not found or in an unsupported format"
"The message to warn the user that file can not be show in
summary window because does not exist or is in an unsupported
format.")
(defconst annotate-info-valid-file-extensions
'(".info" ".info.gz" ".gz")
"The valid extension for files that contains info document.")
(defconst annotate-summary-list-prefix " "
"The string used as prefix for each text annotation item in summary window.")
(defconst annotate-summary-list-prefix-file "* File: "
"The string used as prefix for each annotated file item in summary window.")
(defconst annotate-summary-list-prefix-snippet "** Annotated text: "
"The string used as prefix for each annotation snippet item in summary window.")
(defconst annotate-ellipse-text-marker "..."
"The string used when a string is truncated with an ellipse.")
(defconst annotate-info-root-name "dir"
"The pseudo-filename of info root.")
(defconst annotate-summary-buffer-name "*annotations*"
"The name of the buffer for summary window.")
(defconst annotate-dump-from-indirect-bugger-suffix "-was-annotated-indirect-buffer"
"Append this suffix to a buffer generated from an annotated indirect buffer.")
(defconst annotate-annotation-prompt "Annotation: "
"The prompt when asking user for annotation modification.")
(defconst annotate-summary-delete-button-label "[delete]"
"The label for the button, in summary window, to delete an annotation.")
(defconst annotate-summary-replace-button-label "[replace]"
"The label for the button, in summary window, to replace an annotation.")
(defconst annotate-confirm-deleting-annotation-prompt "Delete this annotation? "
"Prompt to be shown when asking for annotation deletion confirm.")
(defconst annotate-confirm-appending-newline-prompt "No newline character at the end of the buffer %S, annotation will not be displayed properly: append one? "
"Prompt to be shown when asking for appending a newline at the end of the buffer.")
(defconst annotate-message-annotation-loaded "Annotations loaded."
"The message shown when annotations has been loaded.")
(defconst annotate-message-annotations-not-found "No annotations found."
"The message shown when no annotations has been loaded from the database.")
;;;; buffer locals variables
(defvar-local annotate-echo-annotation-timer nil
"The buffer local variable bound to a timer that is in charge to print
the annotation under cursor on the echo area.")
(defvar-local annotate-colors-index-counter 0
"An always increasing value to address annotation colors
in the customizable colors lists:
- annotate-highlight-faces
- annotate-annotation-text-faces.")
;;;; custom errors
(define-error 'annotate-error "Annotation error")
(define-error 'annotate-empty-annotation-text-error
"Empty annotation text"
'annotate-error)
(define-error 'annotate-no-new-line-at-end-file-error
"No newline found at the end of the buffer"
'annotate-error)
(define-error 'annotate-db-file-not-found
"Annotations database file not found"
'annotate-error)
(define-error 'annotate-annotate-region-overlaps
"Error: the region overlaps with at least an already existing annotation"
'annotate-error)
(define-error 'annotate-query-parsing-error
"Parsing failed:"
'annotate-error)
(cl-defmacro annotate-with-disable-read-only (&body body)
"Run `BODY' with `READ-ONLY-MODE' temporary disabled."
(let ((read-mode-p (gensym)))
`(let ((,read-mode-p (if buffer-read-only
1
-1)))
(when (= ,read-mode-p 1)
(read-only-mode -1))
,@body
(when (= ,read-mode-p 1)
(read-only-mode 1)))))
(defun annotate-annotations-exist-p ()
"Does this buffer contains at least one or more annotations?"
(cl-find-if 'annotationp
(overlays-in 0 (buffer-size))))
(defun annotate-initialize-maybe ()
"Initialize annotate mode only if buffer's major mode is not in the blacklist.
See `annotate-blacklist-major-mode'."
(cl-flet ((shutdown ()
(setq annotate-mode t)
(annotate-shutdown)
(setq annotate-mode nil)))
(let ((annotate-allowed-p (with-current-buffer (current-buffer)
(not (apply #'derived-mode-p annotate-blacklist-major-mode)))))
(cond
((not annotate-allowed-p)
(shutdown))
(annotate-mode
(when (not (annotate-annotations-exist-p))
(annotate-initialize)))
(t
(shutdown))))))
(cl-defun annotate-buffer-checksum (&optional (object (current-buffer)))
"Calculate an hash for the argument `OBJECT'."
(secure-hash 'md5 object))
(defun annotate-end-of-line-pos ()
"Get the position of the end of line and rewind the point's
position (so that it is unchanged after this function is called)."
(line-end-position))
(defun annotate-beginning-of-line-pos ()
"Get the position of the beginning of line and rewind the point's
position (so that it is unchanged after this function is called)."
(save-excursion
(beginning-of-line)
(point)))
(defun annotate-annotated-text-empty-p (annotation)
"Does this `ANNOTATION' contains annotated text?"
(= (overlay-start annotation)
(overlay-end annotation)))
(defun annotate-annotation-set-face (annotation face)
"Set property face to `FACE' for `ANNOTATION'."
(overlay-put annotation 'face face))
(defun annotate-annotation-face (annotation)
"Get property face from `ANNOTATION'."
(overlay-get annotation 'face))
(defun annotate-annotation-set-annotation-face (annotation face)
"Set property annotation-face to `FACE' for `ANNOTATION'."
(overlay-put annotation 'annotation-face face))
(defun annotate-annotation-property-annotation-face (annotation)
"Get property annotation-face from `ANNOTATION'."
(overlay-get annotation 'annotation-face))
(defun annotate-annotation-set-annotation-text (annotation annotation-text)
"Set the annotation's content for `ANNOTATION` to `ANNOTATION-TEXT`."
(overlay-put annotation 'annotation annotation-text))
(defun annotate-annotation-get-annotation-text (annotation)
"Get the annotation's content for `ANNOTATION`."
(overlay-get annotation 'annotation))
(defun annotate-annotation-set-position (annotation position)
"Set the annotation's position policy for `ANNOTATION` to the value bound to `POSITION`."
(overlay-put annotation 'annotate-position position))
(defun annotate-annotation-get-position (annotation)
"Get the annotation's position policy for `ANNOTATION`."
(overlay-get annotation 'annotate-position))
(defun annotate-overlay-maybe-set-position (overlay position)
"Set the annotation's position policy for `ANNOTATION` to the value bound to `POSITION`,
but only if the value of the property 'position is not null."
(when position
(annotate-annotation-set-position overlay position)))
(defun annotate-chain-last-ring (chain)
"Get the last ring of `CHAIN'."
(car (last chain)))
(defun annotate--remap-chain-pos (annotations)
"Remap `ANNOTATIONS' as an annotation \"chain\".
An annotation is a collection of one or more overlays that
contains the property `ANNOTATE-PROP-CHAIN-POSITION'.
The value of `ANNOTATE-PROP-CHAIN-POSITION' in each chain is an
integer starting from:
`ANNOTATE-PROP-CHAIN-POS-MARKER-FIRST' and *always* ending with
`ANNOTATE-PROP-CHAIN-POS-MARKER-LAST'
This means that a value of said property for a chain that
contains only an element is equal to
`ANNOTATE-PROP-CHAIN-POS-MARKER-LAST'.
This function ensure this constrains for the chain `ANNOTATION'
belong."
(cond
((< (length annotations)
1)
annotations)
((= (length annotations)
1)
(annotate-annotation-set-chain-last (cl-first annotations)))
(t
(let ((all-but-last (butlast annotations))
(last-element (car (last annotations))))
(cl-loop for annotation in all-but-last
for i from annotate-prop-chain-pos-marker-first
do
(annotate-annotation-chain-position annotation i))
(when last-element
(annotate-annotation-set-chain-last last-element))))))
(defun annotate-before-change-fn (a _b)
"This function is added to \"before-change-functions\" hook and
it is called any time the buffer content is changed (so, for
example, text is added or deleted). In particular, it will
rearrange the overlays bounds when an annotated text is
modified (for example a newline is inserted)."
(with-silent-modifications
(save-excursion
(let* ((bol (annotate-beginning-of-line-pos))
(eol (annotate-end-of-line-pos))
(ov (cl-remove-if-not #'annotationp
(overlays-in bol eol))))
(dolist (overlay ov)
(annotate--remove-annotation-property (overlay-start overlay)
(overlay-end overlay))
;; check if we are breaking the overlay
(when (<= (overlay-start overlay)
a
(overlay-end overlay))
(let ((start-overlay (overlay-start overlay)))
;; delete overlay if there is no more annotated text
(when (<= a start-overlay)
(let ((chain (cl-remove overlay (annotate-find-chain overlay))))
(delete-overlay overlay)
(annotate--remap-chain-pos chain))))))))))
(defun annotate-info-select-fn ()
"The function to be called when an info buffer is updated."
(annotate-clear-annotations)
(annotate-load-annotations)
(font-lock-flush))
(defun on-window-size-change (_frame)
"The function to call when window-size-change-functions is called,
note that the argument `FRAME' is ignored"
(font-lock-flush))
(defun annotate--filepath->local-database-name (filepath)
"Generates the file path of the local database form `FILEPATH'."
(concat (file-name-nondirectory filepath)
"."
annotate-buffer-local-database-extension))
(defun annotate--maybe-database-set-buffer-local ()
"Sets, if user asked to do so, the annotation database to a
local version (i.e. a different database for each annotated file"
(when annotate-file-buffer-local
(make-local-variable 'annotate-file)
(when-let* ((buffer-file-path (buffer-file-name))
(parent-directory (file-name-directory buffer-file-path))
(db-name (annotate--filepath->local-database-name buffer-file-path)))
(setq-local annotate-file db-name))))
(defun annotate-timer-print-annotation-function ()
"Print annotation under point in the minibuffer.
Used by the timer set in `annotate--maybe-make-timer-print-annotation'.
See also the customizable variables: `annotate-echo-annotation-timer' and
`annotate-print-annotation-under-cursor'."
(with-current-buffer (current-buffer)
(when annotate-mode
(when-let ((annotation (annotate-annotation-at (point))))
(message "%s%s"
annotate-print-annotation-under-cursor-prefix
(annotate-annotation-get-annotation-text annotation))))))
(defun annotate-print-annotation-under-cursor-p ()
"Non nil if the user configured the package to print
annotation's text in the minibuffer."
(and annotate-use-echo-area
annotate-print-annotation-under-cursor))
(defun annotate--maybe-make-timer-print-annotation ()
"Set the timer to print the annotation's text in the minibuffer.
Used when the mode is activated."
(when (annotate-print-annotation-under-cursor-p)
(setf annotate-echo-annotation-timer
(run-with-idle-timer annotate-print-annotation-under-cursor-delay
t
#'annotate-timer-print-annotation-function))))
(defun annotate--maybe-cancel-timer-print-annotation ()
"Cancel the timer to print the annotations text in the minibuffer.
Used when the mode is deactivated."
(when (and (annotate-print-annotation-under-cursor-p)
annotate-echo-annotation-timer
(timerp annotate-echo-annotation-timer))
(cancel-timer annotate-echo-annotation-timer)))
(defun annotate-initialize ()
"Load annotations and set up save and display hooks."
(annotate--maybe-database-set-buffer-local)
(annotate--maybe-make-timer-print-annotation)
(annotate-load-annotations)
(add-hook 'kill-buffer-hook #'annotate-save-annotations t t)
(add-hook 'kill-emacs-hook #'annotate-save-all-annotated-buffers t nil)
;; This hook is needed to reorganize the layout of the annotation
;; text when a window vertically resized
(add-hook 'window-size-change-functions #'on-window-size-change t t)
(add-hook 'before-change-functions #'annotate-before-change-fn t t)
(add-hook 'Info-selection-hook #'annotate-info-select-fn t t)
(if annotate-use-echo-area
(font-lock-add-keywords
nil
'((annotate--font-lock-matcher (2 (annotate--annotation-builder)))))
(font-lock-add-keywords
nil
'((annotate--font-lock-matcher (2 (annotate--annotation-builder))
(1 (annotate--change-guard)))))))
(defun annotate-shutdown ()
"Clear annotations and remove save and display hooks."
(annotate-clear-annotations)
(annotate--maybe-cancel-timer-print-annotation)
(remove-hook 'kill-buffer-hook #'annotate-save-annotations t)
(remove-hook 'kill-emacs-hook #'annotate-save-all-annotated-buffers nil)
(remove-hook 'window-size-change-functions #'on-window-size-change t)
(remove-hook 'before-change-functions #'annotate-before-change-fn t)
(remove-hook 'Info-selection-hook #'annotate-info-select-fn t)
(if annotate-use-echo-area
(font-lock-remove-keywords
nil
'((annotate--font-lock-matcher (2 (annotate--annotation-builder)))))
(font-lock-remove-keywords
nil
'((annotate--font-lock-matcher (2 (annotate--annotation-builder))
(1 (annotate--change-guard)))))))
(defun annotate-overlay-filled-p (overlay)
"Does this `OVERLAY' contains an \"annotation\" property?"
(and overlay
(overlayp overlay)
(annotate-annotation-get-annotation-text overlay)))
(defun annotationp (overlay)
"Is `OVERLAY' an annotation?"
(annotate-overlay-filled-p overlay))
(cl-defmacro annotate-ensure-annotation ((overlay) &body body)
"Runs `BODY' only if `OVERLAY' is an annotation (i.e. passes annotationp)."
`(and (annotationp ,overlay)
(progn ,@body)))
(defun annotate--position-on-annotated-text-p (pos)
"Does `POS' (as buffer position) corresponds to a character
that belong to some annotated text?"
(let ((annotation (annotate-annotation-at pos)))
(if annotation
t
;; there is a chance that a point do not belong to the text
;; rendered as annotated but belong to a chain anyway example:
;;
;; legend:
;; a = annotated text
;; * = non annotated text
;; # = annotation
;;
;; Create a multiline annotation using region.
;;
;; aaaa
;; aaaa
;; aaaa
;;
;;
;; aaaa
;; aaaa
;; aaaa ####
;;
;; place the cursor here:
;;
;; aaaa
;; aaaa
;; ^ cursor
;; aaaa ####
;;
;; type some text
;;
;; aaaa
;; *****
;; aaaa ####
;;
;; the text (the asterisks) is not rendered as annotated but as
;; annotations can not have gaps so we enforce this limitation
;; and consider it still parts of a chain formed by the
;; surrounding annotated text.
(let* ((previous-annotation (annotate-previous-annotation-ends pos))
(next-annotation (annotate-next-annotation-starts pos))
(previous-chain (annotate-chain-first previous-annotation))
(next-chain (annotate-chain-first next-annotation)))
(if (and previous-chain
next-chain
(eq previous-chain
next-chain))
t
nil)))))
(defun annotate-delete-chains-in-region (from to)
"Deletes all the chains enclosed in the range specified by
positions `FROM' and `TO'."
(let* ((enclosed-chains (annotate-annotations-chain-in-range from to)))
(dolist (chain enclosed-chains)
(annotate--delete-annotation-chain (cl-first chain)))))
(defun annotate-count-newline-in-region (from to)
"Counts the number of newlines character (?\n) in range
specified by `FROM' and `TO'."
(cl-count-if (lambda (a) (char-equal a ?\n))
(buffer-substring-no-properties from to)))
(defun annotate-annotate (&optional color-index)
"Create, modify, or delete annotation.
if `COLOR-INDEX' is not null must be an index that adresses an element both in
- `annotate-highlight-faces'
and
- `annotate-annotation-text-faces'"
(interactive "P")
(when color-index
(setf color-index (min (max (1- color-index) 0)
(1- (length annotate-highlight-faces)))))
(cl-labels ((create-new-annotation ()
;; create a new annotation in the region returned by `annotate-bound'
(cl-destructuring-bind (start end)
(annotate-bounds)
(let ((annotation-text (read-from-minibuffer annotate-annotation-prompt)))
(condition-case nil
(annotate-create-annotation start end annotation-text nil color-index)
(annotate-no-new-line-at-end-file-error
(user-error "Missing newline at the end of the buffer"))
(annotate-empty-annotation-text-error
(user-error "Annotation text is empty"))))))
(cut-right (region-beg region-stop &optional delete-enclosed)
;; This function will trim on the right one or more
;; existing chains of overlays that compose an
;; annotation (i.e. the overlays applied on the
;; annotated text). After this function is called the
;; text staring from `region-beg' and ending on
;; `region-stop' will be cleared of all annotations if
;; `delete-enclosed' is non null.
(let* ((last-of-chain-to-cut (annotate-chain-last-at region-beg))
(first-of-chain-to-cut (annotate-chain-first-at region-beg))
(chain-start (overlay-start first-of-chain-to-cut))
(chain-end (overlay-end last-of-chain-to-cut))
(newlines-count (annotate-count-newline-in-region region-beg
chain-end))
(cut-count (- chain-end
region-beg
newlines-count)))
(cl-loop repeat cut-count do
(when (annotate-annotation-at chain-start)
(annotate--cut-right-annotation first-of-chain-to-cut t)))
(when delete-enclosed
(annotate-delete-chains-in-region chain-end region-stop))))
(cut-left (region-stop delete-enclosed)
;; This function will trim on the left one or more
;; existing chains of overlays that compose an
;; annotation (i.e. the overlays applied on the
;; annotated text). After this function is called the
;; text starting from the last char of the last chain
;; element of the annotation and ending on
;; `region-stop' will be cleared of all annotations if
;; `delete-enclosed' is non null.
(let* ((last-of-chain-to-cut (annotate-chain-last-at region-stop))
(first-of-chain-to-cut (annotate-chain-first-at region-stop))
(chain-start (overlay-start first-of-chain-to-cut))
(chain-end (overlay-end last-of-chain-to-cut))
(newlines-count (annotate-count-newline-in-region chain-start
region-stop))
(cut-count (- region-stop
chain-start
newlines-count)))
(cl-loop repeat cut-count do
(when (annotate-annotation-at (1- chain-end))
(annotate--cut-left-annotation last-of-chain-to-cut)))
(when delete-enclosed
(annotate-delete-chains-in-region chain-end region-stop))))
(annotate-overwrite-range (start end)
;; annotate text starting from `start' and ending on
;; `end', overwriting any other annotation existing in
;; that range
(goto-char end)
(push-mark (point) t t)
(goto-char start)
(annotate-annotate))
(annotate-line (eol)
;; annotate a line that terminate at `eol'
;;
;; if the line contains no text before the newline
;; annotate the next line with text, if any.
;;
;; if the line contains a single annotation that spans
;; the whole line update the existing annotation
;;
;; if the line contains no annotation, or more than
;; one annotation, annotate the whole line that
;; terminate at `eol'
(let* ((bol (annotate-beginning-of-line-pos))
(annotations-on-the-line (annotate-annotations-overlay-in-range bol
eol)))
(if (= (length annotations-on-the-line)
1)
(let* ((annotation (cl-first annotations-on-the-line))
(start-overlay (overlay-start annotation))
(end-overlay (overlay-end annotation))
(annotation-spans-whole-line-p (and (= start-overlay bol)
(= end-overlay eol))))
(if annotation-spans-whole-line-p
(progn
(goto-char end-overlay)
(push-mark start-overlay t t)
(annotate-change-annotation (overlay-start annotation))
(pop-mark))
(annotate-overwrite-range bol eol)))
(annotate-overwrite-range bol eol)))))
(let ((annotation (annotate-annotation-at (point))))
(cond
((use-region-p)
(let* ((region-beg (region-beginning))
(region-stop (region-end))
(enclosed-chains (annotate-annotations-chain-in-range region-beg region-stop)))
(cond
((and (annotate--position-on-annotated-text-p region-beg)
(annotate--position-on-annotated-text-p region-stop))
;; aaaaaaaaaaaaaaaaaa
;; ^-----------^
(let ((starting-chain-at-start (annotate-chain-first-at region-beg))
(starting-chain-at-end (annotate-chain-first-at region-stop)))
(if (eq starting-chain-at-start
starting-chain-at-end)
(signal 'annotate-annotate-region-overlaps nil)
(cut-left region-stop nil)
(cut-right region-beg region-stop t)
(create-new-annotation))))
((annotate--position-on-annotated-text-p region-beg)
;; aaaabbcc**********
;; ^------------^
(cut-right region-beg region-stop t)
(create-new-annotation))
((annotate--position-on-annotated-text-p region-stop)
;; **********cccaaaa
;; ^------------^
(cut-left region-stop t)
(create-new-annotation))
(enclosed-chains
;; ****aaaaaaaaaaaaaaa****
;; ^------------------^
(annotate-delete-chains-in-region region-beg region-stop)
(create-new-annotation))
(t
(create-new-annotation)))))
(annotation
(annotate-change-annotation (point))
(font-lock-flush))
(t
(if (annotate--position-on-annotated-text-p (point))
(signal 'annotate-annotate-region-overlaps nil)
(let ((char-maybe-newline (char-after)))
(when char-maybe-newline
(cond
((not (char-equal char-maybe-newline ?\n))
(create-new-annotation))
((null annotate-endline-annotate-whole-line)
(user-error "The end of line can not be annotated"))
(t ;; annotate the whole line before or after
(save-excursion
(let* ((bol (annotate-beginning-of-line-pos))
(eol (point)))
(if (/= eol bol) ; text before the newline, annotate it
(annotate-line eol)
(progn ; no text before the new
; line, annotate next line
; with proper text
(forward-line 1)
(goto-char (annotate-end-of-line-pos))
(annotate-annotate)))))))))))))))
(defun annotate-toggle-annotation-text ()
"Hide annotation's text at current cursor's point, if such annotation exists."
(interactive)
(when-let* ((chain (annotate-chain-at (point)))
(last-ring (annotate-chain-last-ring chain)))
(if (annotate-tail-overlay-hide-text-p last-ring)
(annotate-chain-show-text chain)
(annotate-chain-hide-text chain))
(font-lock-flush)))
(defun annotate-toggle-all-annotations-text ()
"Hide annototation's text in the whole buffer."
(interactive)
(let ((chains (annotate-annotations-chain-in-range 0 (buffer-size))))
(dolist (chain chains)
(if (annotate-tail-overlay-hide-text-p (annotate-chain-last-ring chain))
(annotate-chain-show-text chain)
(annotate-chain-hide-text chain))))
(font-lock-flush))
(cl-defun annotate-goto-next-annotation (&key (startingp t))
"Move point to the next annotation."
(interactive)
(let ((annotation (annotate-annotation-at (point))))
(if startingp
(if annotation
(let* ((chain-last (annotate-chain-last annotation))
(annotation-last-end (overlay-end chain-last))
(look-ahead (annotate-next-annotation-starts annotation-last-end)))
(if look-ahead
(progn
(goto-char annotation-last-end)
(annotate-goto-next-annotation :startingp nil))
(when annotate-use-messages
(message "This is the last annotation."))))
(let ((next-annotation (annotate-next-annotation-starts (point))))
(when next-annotation
(goto-char (overlay-start next-annotation)))))
(if annotation
(let ((chain-first (annotate-chain-first annotation)))
(goto-char (overlay-start chain-first)))
(annotate-goto-next-annotation :startingp t)))))
(cl-defun annotate-goto-previous-annotation (&key (startingp t))
"Move point to the previous annotation."
(interactive)
(let ((annotation (annotate-annotation-at (point))))
(if startingp
(if annotation
(let* ((chain-first (annotate-chain-first annotation))
(annotation-first-start (overlay-start chain-first))
(look-behind (annotate-previous-annotation-ends annotation-first-start)))
(if look-behind
(progn
(goto-char (1- annotation-first-start))
(annotate-goto-previous-annotation :startingp nil))
(when annotate-use-messages
(message "This is the first annotation."))))
(let ((previous-annotation (annotate-previous-annotation-ends (point))))
(when previous-annotation
(goto-char (1- (overlay-end previous-annotation))))))
(if annotation
(let ((chain-last (annotate-chain-last annotation)))
(goto-char (overlay-end chain-last)))
(annotate-goto-previous-annotation :startingp t)))))
(defun annotate-change-annotation-text-position ()
"Change the policy positioning for the annotation under point."
(interactive)
(when-let ((annotation (annotate-annotation-at (point))))
(let ((current-position (annotate-annotation-get-position annotation)))
(if (null current-position)
(annotate-annotation-set-position annotation
(cl-first annotate-allowed-positioning-policy))
(when-let ((current-position-index (cl-position current-position
annotate-allowed-positioning-policy))
(next-position-index (mod (1+ current-position-index)
(length annotate-allowed-positioning-policy))))
(annotate-annotation-set-position annotation
(elt annotate-allowed-positioning-policy
next-position-index)))))
(message "New position policy for this annotation is %s"
(annotate-annotation-get-position annotation))
(font-lock-flush)))
(defun annotate-change-annotation-colors ()
"Change the colors for the annotation under point."
(interactive)
(cl-flet ((new-color-index (annotation)
(let ((current-annotation-face (annotate-annotation-property-annotation-face annotation)))
(if current-annotation-face
(let* ((current-color-index (cl-position-if (lambda (a)
(cl-equalp current-annotation-face
a))
annotate-annotation-text-faces))
(new-color-index (mod (1+ current-color-index)
(length annotate-annotation-text-faces))))
new-color-index)
0))))
(when-let ((annotation (annotate-annotation-at (point))))
(let ((new-color-index (new-color-index annotation)))
(annotate-annotation-set-annotation-face annotation
(elt annotate-annotation-text-faces
new-color-index))
(annotate-annotation-set-face annotation
(elt annotate-highlight-faces
new-color-index))
(font-lock-flush)))))
(defun annotate-actual-comment-start ()