forked from joodland/bm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm.el
1542 lines (1278 loc) · 52.4 KB
/
bm.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
;;; bm.el --- Visible bookmarks in buffer.
;; Copyright (C) 2000-2015 Jo Odland
;; Author: Jo Odland <jo.odland(at)gmail.com>
;; Keywords: bookmark, highlight, faces, persistent
;; URL: https://github.com/joodland/bm
;; Portions Copyright (C) 2002 by Ben Key
;; Updated by Ben Key <bkey1(at)tampabay.rr.com> on 2002-12-05
;; to add support for XEmacs
;; 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 2, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;;; Description:
;;
;; This package was created because I missed the bookmarks from M$
;; Visual Studio. I find that they provide an easy way to navigate
;; in a buffer.
;;
;; bm.el provides visible, buffer local, bookmarks and the ability
;; to jump forward and backward to the next bookmark.
;;
;; Features:
;; - Toggle bookmarks with `bm-toggle' and navigate forward and
;; backward in buffer with `bm-next' and `bm-previous'.
;;
;; - Different wrapping modes, see `bm-wrap-search' and
;; `bm-wrap-immediately'. Use `bm-toggle-wrapping' to turn
;; wrapping on/off. Wrapping is only available when
;; `bm-cycle-all-buffers' is nil.
;;
;; - Navigate between bookmarks only in current buffer or cycle
;; through all buffers. Use `bm-cycle-all-buffers' to enable
;; looking for bookmarks across all open buffers. When cycling
;; through bookmarks in all open buffers, the search will always
;; wrap around.
;;
;; - Setting bookmarks based on a regexp, see `bm-bookmark-regexp'
;; and `bm-bookmark-regexp-region'.
;;
;; - Setting bookmark based on line number, see `bm-bookmark-line'.
;;
;; - Goto line position or start of line, see `bm-goto-position'.
;;
;; - Persistent bookmarks (see below). Use
;; `bm-toggle-buffer-persistence' to enable/disable persistent
;; bookmarks (buffer local).
;;
;; - List bookmarks with annotations and context in a separate
;; buffer, see `bm-show' (current buffer) and `bm-show-all' (all
;; open buffers). See `bm-show-mode-map' for key bindings.
;;
;; - Remove all bookmarks in current buffer with
;; `bm-remove-all-current-buffer' and all bookmarks in all open
;; buffers with `bm-remove-all-all-buffers'.
;;
;; - Annotate bookmarks, see `bm-bookmark-annotate' and
;; `bm-bookmark-show-annotation'. The annotation is displayed in
;; the message area when navigating to a bookmark. Set the
;; variable `bm-annotate-on-create' to t to be prompted for an
;; annotation when bookmark is created.
;;
;; - Different bookmark styles, fringe-only, line-only or both, see
;; `bm-highlight-style'. It is possible to have fringe-markers on
;; left or right side.
;;
;;; Known limitations:
;;
;; This package is developed and tested on GNU Emacs 23.x. It should
;; work on all GNU Emacs 21.x, GNU Emacs 22.x and also on XEmacs
;; 21.x with some limitations.
;;
;; There are some incompatibilities with lazy-lock when using
;; fill-paragraph. All bookmark below the paragraph being filled
;; will be lost. This issue can be resolved using the
;; `jit-lock-mode' introduced in GNU Emacs 21.1
;;
;; Bookmarks will be extended when inserting text (before, inside or
;; after) bookmark in XEmacs. This is due to the missing support for
;; overlay hooks i XEmacs.
;;; Installation:
;;
;; To use bm.el, put it in your load-path and add
;; the following to your .emacs
;;
;; (require 'bm)
;;
;; or
;;
;; (autoload 'bm-toggle "bm" "Toggle bookmark in current buffer." t)
;; (autoload 'bm-next "bm" "Goto bookmark." t)
;; (autoload 'bm-previous "bm" "Goto previous bookmark." t)
;;
;;; Configuration:
;;
;; To make it easier to use, assign the commands to some keys.
;;
;; M$ Visual Studio key setup.
;; (global-set-key (kbd "<C-f2>") 'bm-toggle)
;; (global-set-key (kbd "<f2>") 'bm-next)
;; (global-set-key (kbd "<S-f2>") 'bm-previous)
;;
;; Click on fringe to toggle bookmarks, and use mouse wheel to move
;; between them.
;; (global-set-key (kbd "<left-fringe> <mouse-5>") 'bm-next-mouse)
;; (global-set-key (kbd "<left-fringe> <mouse-4>") 'bm-previous-mouse)
;; (global-set-key (kbd "<left-fringe> <mouse-1>") 'bm-toggle-mouse)
;;
;; If you would like the markers on the right fringe instead of the
;; left, add the following to line:
;;
;; (setq bm-marker 'bm-marker-right)
;;
;;; Persistence:
;;
;; Bookmark persistence is achieved by storing bookmark data in a
;; repository when a buffer is killed. The repository is saved to
;; disk on exit. See `bm-repository-file'. The maximum size of the
;; repository is controlled by `bm-repository-size'.
;;
;; The buffer local variable `bm-buffer-persistence' decides if
;; bookmarks in a buffer is persistent or not. Non-file buffers
;; can't have persistent bookmarks, except for *info* and
;; indirect buffers.
;;
;; Bookmarks are non-persistent as default. To have bookmarks
;; persistent as default add the following line to .emacs.
;;
;; ;; make bookmarks persistent as default
;; (setq-default bm-buffer-persistence t)
;; Use the function `bm-toggle-buffer-persistence' to toggle
;; bookmark persistence.
;;
;; To have automagic bookmark persistence we need to add some
;; functions to the following hooks. Insert the following code
;; into your .emacs file:
;;
;; If you are using desktop or other packages that restore buffers
;; on start up, bookmarks will not be restored. When using
;; `after-init-hook' to restore the repository, it will be restored
;; *after* .emacs is finished. To load the repository when bm is
;; loaded set the variable `bm-restore-repository-on-load' to t,
;; *before* loading bm (and don't use the `after-init-hook').
;;
;; ;; Make sure the repository is loaded as early as possible
;; (setq bm-restore-repository-on-load t)
;; (require 'bm)
;;
;; ;; Loading the repository from file when on start up.
;; (add-hook' after-init-hook 'bm-repository-load)
;;
;; ;; Restoring bookmarks when on file find.
;; (add-hook 'find-file-hooks 'bm-buffer-restore)
;;
;; ;; Saving bookmark data on killing a buffer
;; (add-hook 'kill-buffer-hook 'bm-buffer-save)
;;
;; ;; Saving the repository to file when on exit.
;; ;; kill-buffer-hook is not called when Emacs is killed, so we
;; ;; must save all bookmarks first.
;; (add-hook 'kill-emacs-hook '(lambda nil
;; (bm-buffer-save-all)
;; (bm-repository-save)))
;;
;; ;; Update bookmark repository when saving the file.
;; (add-hook 'after-save-hook 'bm-buffer-save)
;;
;; ;; Restore bookmarks when buffer is reverted.
;; (add-hook 'after-revert-hook 'bm-buffer-restore)
;;
;;
;; The `after-save-hook' and `after-revert-hook' is not necessary to
;; use to achieve persistence, but it makes the bookmark data in
;; repository more in sync with the file state.
;;
;; The `after-revert-hook' might cause trouble when using packages
;; that automatically reverts the buffer (like vc after a check-in).
;; This can easily be avoided if the package provides a hook that is
;; called before the buffer is reverted (like `vc-before-checkin-hook').
;; Then new bookmarks can be saved before the buffer is reverted.
;;
;; ;; make sure bookmarks is saved before check-in (and revert-buffer)
;; (add-hook 'vc-before-checkin-hook 'bm-buffer-save)
;;; Acknowledgements:
;;
;; - The use of overlays for bookmarks was inspired by highline.el by
;; Vinicius Jose Latorre <vinicius(at)cpqd.com.br>.
;; - Thanks to Ben Key for XEmacs support.
;; - Thanks to Peter Heslin for notifying me on the incompability
;; with lazy-lock.
;; - Thanks to Christoph Conrad for adding support for goto line
;; position in bookmarks and simpler wrapping.
;; - Thanks to Jan Rehders for adding support for different bookmark
;; - styles.
;; - Thanks to Dan McKinley <mcfunley(at)gmail.com> for inspiration
;; to add support for listing bookmarks in all buffers,
;; `bm-show-all'. (http://www.emacswiki.org/cgi-bin/wiki/bm-ext.el)
;; - Thanks to Jonathan Kotta <jpkotta(at)gmail.com> for mouse
;; support and fringe markers on left or right side.
;; - Thanks to Juanma Barranquero <lekktu(at)gmail.com> for making
;; `bm-show' an electric window, cleaning up the code, finding and
;; fixing bugs and correcting spelling errors.
;; - Thanks to jixiuf <jixiuf(at)gmail.com> for adding LIFO support
;; to bookmark navigation. See `bm-in-lifo-order' for more
;; information.
;;; Change log:
;; See version history at Github, https://github.com/joodland/bm
;;; Code:
;;
(eval-and-compile
(require 'cl-lib)
;; avoid compile warning on unbound variable
(require 'info)
;; xemacs needs overlay emulation package
(unless (fboundp 'overlay-lists)
(require 'overlay)))
(defconst bm-bookmark-repository-version 2
"The repository version.")
(defgroup bm nil
"Visible, buffer local bookmarks."
:link '(emacs-library-link :tag "Source Lisp File" "bm.el")
:group 'faces
:group 'editing
:prefix "bm-")
(defcustom bm-highlight-style 'bm-highlight-only-line
"*Specify how bookmars are highlighted."
:type '(choice (const bm-highlight-only-line)
(const bm-highlight-only-fringe)
(const bm-highlight-line-and-fringe))
:group 'bm)
(defcustom bm-face 'bm-face
"*Specify face used to highlight the current line."
:type 'face
:group 'bm)
(defcustom bm-persistent-face 'bm-persistent-face
"*Specify face used to highlight the current line for persistent bookmarks."
:type 'face
:group 'bm)
(defcustom bm-priority 0
"*Specify bm overlay priority.
Higher integer means higher priority, so bm overlay will have precedence
over overlays with lower priority. *Don't* use a negative number."
:type 'integer
:group 'bm)
(defface bm-face
'((((class grayscale)
(background light)) (:background "DimGray"))
(((class grayscale)
(background dark)) (:background "LightGray"))
(((class color)
(background light)) (:foreground "White" :background "DarkOrange1"))
(((class color)
(background dark)) (:foreground "Black" :background "DarkOrange1")))
"Face used to highlight current line."
:group 'bm)
(defface bm-persistent-face
'((((class grayscale)
(background light)) (:background "DimGray"))
(((class grayscale)
(background dark)) (:background "LightGray"))
(((class color)
(background light)) (:foreground "White" :background "DarkBlue"))
(((class color)
(background dark)) (:foreground "White" :background "DarkBlue")))
"Face used to highlight current line if bookmark is persistent."
:group 'bm)
(defcustom bm-fringe-face 'bm-fringe-face
"*Specify face used to highlight the fringe."
:type 'face
:group 'bm)
(defcustom bm-fringe-persistent-face 'bm-fringe-persistent-face
"*Specify face used to highlight the fringe for persistent bookmarks."
:type 'face
:group 'bm)
(defface bm-fringe-face
'((((class grayscale)
(background light)) (:background "DimGray"))
(((class grayscale)
(background dark)) (:background "LightGray"))
(((class color)
(background light)) (:foreground "White" :background "DarkOrange1"))
(((class color)
(background dark)) (:foreground "Black" :background "DarkOrange1")))
"Face used to highlight bookmarks in the fringe."
:group 'bm)
(defface bm-fringe-persistent-face
'((((class grayscale)
(background light)) (:background "DimGray"))
(((class grayscale)
(background dark)) (:background "LightGray"))
(((class color)
(background light)) (:foreground "White" :background "DarkBlue"))
(((class color)
(background dark)) (:foreground "White" :background "DarkBlue")))
"Face used to highlight bookmarks in the fringe if bookmark is persistent."
:group 'bm)
(defcustom bm-annotate-on-create nil
"*Specify if bookmarks must be annotated when created.
nil, don't ask for an annotation when creating a bookmark.
t, always ask for annotation when creating a bookmark."
:type 'boolean
:group 'bm)
(defcustom bm-wrap-search t
"*Specify if bookmark search should wrap.
nil, don't wrap when there are no more bookmarks.
t, wrap."
:type 'boolean
:group 'bm)
(defcustom bm-wrap-immediately t
"*Specify if a wrap should be announced or not.
Only has effect when `bm-wrap-search' is t.
nil, announce before wrapping.
t, don't announce."
:type 'boolean
:group 'bm)
(defcustom bm-cycle-all-buffers nil
"*Specify if bookmark search is done across buffers.
This will ignore the `bm-wrap-search' setting.
nil, only search in current buffer.
t, search in all open buffers."
:type 'boolean
:group 'bm)
(defcustom bm-in-lifo-order nil
"`bm-show' and `bm-show-all' list bookmarks in LIFO order,
`bm-next' and `bm-previous' goto bookmark in LIFO order,
(that is, most recently set ones come first, oldest ones come last)"
:type 'boolean
:group 'bm)
(defcustom temporary-bookmark-p nil
"when visit a bookmark using `bm-next' or `bm-previsour' the bookmark
will be auto removed if this option is not nil."
:type 'boolean
:group 'bm)
(defcustom bm-recenter nil
"*Specify if the buffer should be recentered after jumping to a bookmark."
:type 'boolean
:group 'bm)
(defcustom bm-goto-position t
"*Specify the position, on line, to go to when jumping to a bookmark.
nil, goto start of line.
t, goto position on the line where the bookmark was set."
:type 'boolean
:group 'bm)
(defcustom bm-electric-show t
"*If t, `bm-show' acts like an electric buffer."
:type 'boolean
:group 'bm)
(defcustom bm-repository-file (expand-file-name "~/.bm-repository")
"*Filename to store persistent bookmarks across sessions.
nil, the repository will not be persistent."
:type 'string
:group 'bm)
(defcustom bm-repository-size 100
"*Size of persistent repository. If nil then there if no limit."
:type 'integer
:group 'bm)
(defcustom bm-buffer-persistence nil
"*Specify if bookmarks in a buffer should be persistent.
Buffer local variable.
nil, don't save bookmarks.
t, save bookmarks."
:type 'boolean
:group 'bm)
(make-variable-buffer-local 'bm-buffer-persistence)
(defcustom bm-show-annotations t
"*Specify if annotations are shown by `bm-show' and
`bm-show-all'."
:type 'boolean
:group 'bm)
(defvar bm-restore-repository-on-load nil
"Specify if repository should be restored when loading bm.
nil, don't restore repository on load.
t, restore repository when this file is loaded. This must be set
before bm is loaded.")
(defvar bm-repository nil
"Alist with all persistent bookmark data.")
(defvar bm-regexp-history nil
"Bookmark regexp history.")
(defvar bm-annotation-history nil
"Bookmark annotation history.")
(defvar bm-bookmark-context-size 16
"The size of context stored, before and after, for each bookmark.")
(defvar bm-wrapped nil
"State variable to support wrapping.")
(make-variable-buffer-local 'bm-wrapped)
(defconst bm-show-buffer-name "*bm-bookmarks*"
"The name of the buffer used to show bookmarks by `bm-show'.")
(defvar bm-marker 'bm-marker-left
"Fringe marker side. Left of right.")
(defvar bm-current nil)
;; avoid errors on emacs running in a terminal
(when (fboundp 'define-fringe-bitmap)
(define-fringe-bitmap 'bm-marker-left [#x00 #x00 #xFC #xFE #x0F #xFE #xFC #x00])
(define-fringe-bitmap 'bm-marker-right [#x00 #x00 #x3F #x7F #xF0 #x7F #x3F #x00]))
(defun bm-customize nil
"Customize bm group."
(interactive)
(customize-group 'bm))
(defun bm-bookmark-annotate (&optional bookmark annotation)
"Annotate bookmark at point or the BOOKMARK specified as parameter.
If ANNOTATION is provided use this, and not prompt for input."
(interactive)
(if (null bookmark)
(setq bookmark (bm-bookmark-at (point))))
(if (bm-bookmarkp bookmark)
(progn
(if (null annotation)
(setq annotation (read-from-minibuffer "Annotation: " nil nil nil 'bm-annotation-history)))
(overlay-put bookmark 'annotation annotation))
(if (called-interactively-p 'interactive) (message "No bookmark at point"))))
(defun bm-bookmark-show-annotation (&optional bookmark)
"Show annotation for bookmark.
Either the bookmark at point or the BOOKMARK specified as parameter."
(interactive)
(if (null bookmark)
(setq bookmark (bm-bookmark-at (point))))
(if (bm-bookmarkp bookmark)
(message (or (overlay-get bookmark 'annotation)
"No annotation for current bookmark."))
(message "No bookmark at current line.")))
(defun bm-highlight-line ()
"Test if line is highlighted."
(or (equal bm-highlight-style 'bm-highlight-only-line)
(equal bm-highlight-style 'bm-highlight-line-and-fringe)))
(defun bm-highlight-fringe ()
"Test if fringe is highlighted."
(or (equal bm-highlight-style 'bm-highlight-only-fringe)
(equal bm-highlight-style 'bm-highlight-line-and-fringe)))
(defun bm-get-highlight-face nil
"Get the correct face according to the value of `bm-buffer-presistence'."
(if bm-buffer-persistence bm-persistent-face bm-face))
(defun bm-get-highlight-face-fringde nil
"Get the correct fringde face according to the value of `bm-buffer-presistence'."
(if bm-buffer-persistence bm-fringe-persistent-face bm-fringe-face))
(defun bm-get-fringe-marker nil
"Get the fringde marker string."
(let ((marker-string "*fringe-dummy*"))
(put-text-property 0 (length marker-string) 'display
(list (if (eq bm-marker 'bm-marker-left)
'left-fringe
'right-fringe)
bm-marker (bm-get-highlight-face-fringde))
marker-string)
marker-string))
(defun bm-bookmark-add (&optional annotation time temporary-bookmark)
"Add bookmark at current line.
If ANNOTATION is provided use this, and do not prompt for input.
Only used if `bm-annotate-on-create' is true.
TIME is useful when `bm-in-lifo-order' is not nil.
if TEMPORARY-BOOKMARK not nil,the bookmark will be removed
when `bm-next' or `bm-previous' navigate to this bookmark."
(let((bookmark (bm-bookmark-at (point))))
(if bookmark
(progn (setq bm-current bookmark)
(overlay-put bookmark 'position (point-marker))
(overlay-put bookmark 'time (or time (float-time))))
(let ((bookmark (make-overlay (bm-start-position) (bm-end-position)))
(hlface (if bm-buffer-persistence bm-persistent-face bm-face))
(hlface-fringe (if bm-buffer-persistence bm-fringe-persistent-face bm-fringe-face)))
;; set market
(overlay-put bookmark 'time (or time (float-time)))
(overlay-put bookmark 'temporary-bookmark
(if temporary-bookmark t temporary-bookmark-p))
(overlay-put bookmark 'position (point-marker))
;; select bookmark face
(when (bm-highlight-line)
(overlay-put bookmark 'face hlface))
(overlay-put bookmark 'evaporate t)
(overlay-put bookmark 'category 'bm)
(when (bm-highlight-fringe)
(overlay-put bookmark 'before-string (bm-get-fringe-marker)))
(if (or bm-annotate-on-create annotation)
(bm-bookmark-annotate bookmark annotation))
(unless (featurep 'xemacs)
;; gnu emacs specific features
(overlay-put bookmark 'priority bm-priority)
(overlay-put bookmark 'modification-hooks '(bm-freeze))
(overlay-put bookmark 'insert-in-front-hooks '(bm-freeze-in-front))
(overlay-put bookmark 'insert-behind-hooks '(bm-freeze)))
(setq bm-current bookmark)
bookmark))))
(defun bm-bookmark-remove (&optional bookmark)
"Remove bookmark at point or the BOOKMARK specified as parameter."
(if (null bookmark)
(setq bookmark (bm-bookmark-at (point))))
(if (bm-bookmarkp bookmark)
(delete-overlay bookmark)))
;;;###autoload
(defun bm-toggle nil
"Toggle bookmark at point."
(interactive)
(let ((bookmark (bm-bookmark-at (point))))
(if bookmark
(bm-bookmark-remove bookmark)
(bm-bookmark-add))))
;;;###autoload
(defun bm-toggle-mouse (ev)
"Toggle a bookmark with a mouse click.
EV is the mouse event."
(interactive "e")
(save-excursion
(mouse-set-point ev)
(bm-toggle)))
(defun bm-count nil
"Count the number of bookmarks in buffer."
(let ((bookmarks (bm-lists t 'bm-bookmark-is-visible)))
(+ (length (car bookmarks)) (length (cdr bookmarks)))))
(defun bm-start-position nil
"Return the bookmark start position."
(point-at-bol))
(defun bm-end-position nil
"Return the bookmark end position."
(min (point-max) (+ 1 (point-at-eol))))
(defun bm-freeze-in-front (overlay after begin end &optional len)
"Prevent overlay from being extended to multiple lines.
When inserting in front of overlay move overlay forward.
OVERLAY the overlay being modified.
AFTER nil when called before, t when called after modification.
BEGIN the beginning of the text being modified.
END the end of the text being modified.
When called after, the length of the modification is passed as LEN.
See Overlay Properties in the Emacs manual for more information:
http://www.gnu.org/s/emacs/manual/html_node/elisp/Overlay-Properties.html"
(if after
(move-overlay overlay (bm-start-position) (bm-end-position))))
(defun bm-freeze (overlay after begin end &optional len)
"Prevent OVERLAY from being extended to multiple lines.
When inserting inside or behind the overlay, keep the original start postion.
OVERLAY the overlay being modified.
AFTER nil when called before, t when called after modification.
BEGIN the beginning of the text being modified.
END the end of the text being modified.
When called after, the length of the modification is passed as LEN.
See Overlay Properties in the Emacs manual for more information:
http://www.gnu.org/s/emacs/manual/html_node/elisp/Overlay-Properties.html"
(if after
(let ((bm-start (overlay-start overlay)))
(if bm-start
(move-overlay overlay
bm-start
(save-excursion
(goto-char bm-start)
(bm-end-position)))))))
(defun bm-equal (first second)
"Compare two bookmarks. Return t if FIRST is equal to SECOND."
(if (and (bm-bookmarkp first) (bm-bookmarkp second))
(= (overlay-start first) (overlay-start second))
nil))
(defun bm-bookmarkp (bookmark)
"Return the BOOKMARK if overlay is a bookmark."
(if (and (overlayp bookmark)
(overlay-buffer bookmark)
(string= (overlay-get bookmark 'category) "bm"))
bookmark
nil))
(defun bm-bookmark-is-visible (bookmark)
"Return the BOOKMARK if the BOOKMARK is in the visible part of the buffer."
(if (and (bm-bookmarkp bookmark)
(>= (overlay-start bookmark) (point-min))
(<= (overlay-end bookmark) (point-max)))
bookmark
nil))
(defun bm-bookmark-at (point)
"Get bookmark at POINT."
(let ((overlays (overlays-at point))
(bookmark nil))
(while (and (not bookmark) overlays)
(if (bm-bookmarkp (car overlays))
(setq bookmark (car overlays))
(setq overlays (cdr overlays))))
bookmark))
(defun bm-lists (&optional direction predicate)
"Return a pair of lists giving all the bookmarks of the current buffer.
The car has all the bookmarks before the overlay center;
the cdr has all the bookmarks after the overlay center.
A bookmark implementation of `overlay-lists'.
If optional argument DIRECTION is provided, only return bookmarks
in the specified direction.
If optional argument PREDICATE is provided, it is used as a
selection criteria for filtering the lists."
(if (null predicate)
(setq predicate 'bm-bookmarkp))
(overlay-recenter (point))
(cond ((equal 'forward direction)
(cons nil (remq nil (mapcar predicate (cdr (overlay-lists))))))
((equal 'backward direction)
(cons (remq nil (mapcar predicate (car (overlay-lists)))) nil))
(t
(cons (remq nil (mapcar predicate (car (overlay-lists))))
(remq nil (mapcar predicate (cdr (overlay-lists))))))))
(defun bm-overlay-in-buffer()
"overlays in current buffer"
(let ((bookmarks (bm-lists)))
(append
;; xemacs has the list sorted after buffer position, while
;; gnu emacs list is sorted relative to current position.
(if (featurep 'xemacs)
(car bookmarks)
(reverse (car bookmarks)))
(cdr bookmarks))))
(defun bm-overlay-all()
"overlays in all buffer"
(cl-mapcan (lambda (x) (if (listp x) x nil))
(remq
nil (mapcar #'(lambda (buffer)
(with-current-buffer buffer
(bm-overlay-in-buffer))
)(buffer-list)))))
(defun bm-overlays-lifo-order(&optional all reverse)
(sort (if all
(bm-overlay-all)
(bm-overlay-in-buffer))
#'(lambda(o1 o2)
(if reverse
(< (overlay-get o1 'time) (overlay-get o2 'time))
(> (overlay-get o1 'time) (overlay-get o2 'time))))))
(defun bm-find-lifo-next(&optional reverse)
(let ((sorted-bm-list (bm-overlays-lifo-order bm-cycle-all-buffers reverse))
ret)
(setq ret (loop with next for i in sorted-bm-list
until (bm-equal bm-current i) do (setq next i)
finally return next))
(if ret ret
(car (last (bm-overlays-lifo-order bm-cycle-all-buffers reverse))))))
;;;###autoload
(defun bm-lifo-previous()
"Goto previous bookmark in LIFO order . (that is, most
recently set ones come first, oldest ones come last)"
(interactive)
(let ((sorted-bm-list (bm-overlays-lifo-order bm-cycle-all-buffers t))
next)
(cond ((null sorted-bm-list)
(message "no next bookmark"))
((or (null bm-current) (not (member bm-current sorted-bm-list)))
(switch-to-buffer (overlay-buffer (car sorted-bm-list)))
(bm-goto (car sorted-bm-list))
(setq bm-current (car (last sorted-bm-list))))
(t
(switch-to-buffer (overlay-buffer bm-current))
(bm-goto bm-current)
(setq bm-current (bm-find-lifo-next t))))))
;;;###autoload
(defun bm-lifo-next()
"Goto next bookmark in LIFO order .(that is, most
recently set ones come first, oldest ones come last)"
(interactive)
(let ((sorted-bm-list (bm-overlays-lifo-order bm-cycle-all-buffers))
next)
(cond ((null sorted-bm-list)
(message "no next bookmark"))
((or (null bm-current) (not (member bm-current sorted-bm-list)))
(switch-to-buffer (overlay-buffer (car sorted-bm-list)))
(bm-goto (car sorted-bm-list))
(setq bm-current (car (last sorted-bm-list))))
(t
(switch-to-buffer (overlay-buffer bm-current))
(bm-goto bm-current)
(setq bm-current (bm-find-lifo-next))))))
;;;###autoload
(defun bm-next nil
(interactive)
(if bm-in-lifo-order
(bm-lifo-next)
(bm-common-next)))
;;;###autoload
(defun bm-common-next nil
"Goto next bookmark."
(interactive)
(let ((bm-list-forward (cdr (bm-lists 'forward 'bm-bookmark-is-visible))))
;; remove bookmark at point
(if (bm-equal (bm-bookmark-at (point)) (car bm-list-forward))
(setq bm-list-forward (cdr bm-list-forward)))
(if bm-list-forward
(bm-goto (car bm-list-forward))
(cond (bm-cycle-all-buffers (bm-first-in-next-buffer))
(bm-wrap-search (bm-wrap-forward))
(t (message "No next bookmark."))))))
(defun bm-wrap-forward nil
"Goto next bookmark, wrapping."
(if (= (bm-count) 0)
(message "No next bookmark.")
(if (or bm-wrapped bm-wrap-immediately)
(progn
(message "Wrapped.")
(bm-first))
(setq bm-wrapped t) ; wrap on next goto
(message "No next bookmark."))))
;;;###autoload
(defun bm-next-mouse (ev)
"Go to the next bookmark with the scroll wheel.
EV is the mouse event."
(interactive "e")
(let ((old-selected-window (selected-window)))
(select-window (posn-window (event-start ev)))
(bm-next)
(select-window old-selected-window)))
;;;###autoload
(defun bm-previous nil
(interactive)
(if bm-in-lifo-order
(bm-lifo-previous)
(bm-common-previous)))
;;;###autoload
(defun bm-common-previous nil
"Goto previous bookmark."
(interactive)
(let ((bm-list-backward (car (bm-lists 'backward 'bm-bookmark-is-visible))))
;; remove bookmark at point
(if (bm-equal (bm-bookmark-at (point)) (car bm-list-backward))
(setq bm-list-backward (cdr bm-list-backward)))
(if bm-list-backward
(bm-goto (car bm-list-backward))
(cond (bm-cycle-all-buffers (bm-last-in-previous-buffer))
(bm-wrap-search (bm-wrap-backward))
(t (message "No previous bookmark."))))))
(defun bm-wrap-backward nil
"Goto previous bookmark, wrapping."
(if (= (bm-count) 0)
(message "No previous bookmark.")
(if (or bm-wrapped bm-wrap-immediately)
(progn
(message "Wrapped.")
(bm-last))
(setq bm-wrapped t) ; wrap on next goto
(message "No previous bookmark."))))
;;;###autoload
(defun bm-previous-mouse (ev)
"Go to the previous bookmark with the scroll wheel.
EV is the mouse event."
(interactive "e")
(let ((old-selected-window (selected-window)))
(select-window (posn-window (event-start ev)))
(bm-previous)
(select-window old-selected-window)))
(defun bm-first-in-next-buffer nil
"Goto first bookmark in next buffer."
(interactive)
(let ((current (current-buffer))
(buffers
(save-excursion
(remq nil (mapcar #'(lambda (buffer)
(set-buffer buffer)
(if (> (bm-count) 0)
buffer
nil))
;; drop current buffer from list
(cdr (buffer-list)))))))
(if buffers
(progn
(switch-to-buffer (car buffers))
(bury-buffer current)
(message "Switched to '%s'" (car buffers))
(bm-first))
;; no bookmarks found in other open buffers,
;; wrap in current buffer?
(if bm-wrap-search
(bm-wrap-forward)
(message "No bookmarks found in other open buffers.")))))
(defun bm-last-in-previous-buffer nil
"Goto last bookmark in previous buffer."
(interactive)
(let ((buffers
(save-excursion
(remq nil (mapcar #'(lambda (buffer)
(set-buffer buffer)
(if (> (bm-count) 0)
buffer
nil))
;; drop current buffer from list
(reverse (cdr (buffer-list))))))))
(if buffers
(progn
(switch-to-buffer (car buffers))
(message "Switched to '%s'" (car buffers))
(bm-last))
;; no bookmarks found in other open buffers,
;; wrap in current buffer?
(if bm-wrap-search
(bm-wrap-backward)
(message "No bookmarks found in other open buffers.")))))
(defun bm-first nil
"Goto first bookmark in buffer."
(let ((bookmark (bm-bookmark-at (point-min))))
(if bookmark
;; display correct behaviour even when standing on a bookmark
(bm-goto bookmark)
(goto-char (point-min))
(bm-next))))
(defun bm-last nil
"Goto last bookmark in buffer."
(let ((bookmark (bm-bookmark-at (point-max))))
(if bookmark
;; display correct behaviour even when standing on a bookmark
(bm-goto bookmark)
(goto-char (point-max))
(bm-previous))))
(defun bm-remove-all-all-buffers nil
"Delete all visible bookmarks in all open buffers."
(interactive)
(save-excursion
(mapcar #'(lambda (buffer)
(set-buffer buffer)
(bm-remove-all-current-buffer))
(buffer-list))))
(defun bm-remove-all-current-buffer nil
"Delete all visible bookmarks in current buffer."
(interactive)
(let ((bookmarks (bm-lists t 'bm-bookmark-is-visible)))
(mapc 'bm-bookmark-remove (append (car bookmarks) (cdr bookmarks)))))
(defun bm-toggle-wrapping nil
"Toggle wrapping on/off, when searching for next/previous bookmark."
(interactive)