]> code.delx.au - gnu-emacs/blob - lisp/bookmark.el
* bookmark.el: Provide a generic exit hook, as suggested by Ovidiu
[gnu-emacs] / lisp / bookmark.el
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later.
2
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation
4
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This package is for setting "bookmarks" in files. A bookmark
30 ;; associates a string with a location in a certain file. Thus, you
31 ;; can navigate your way to that location by providing the string.
32 ;; See the "User Variables" section for customizations.
33
34 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
35 ;; then implementing the bookmark-current-bookmark idea. He even
36 ;; sent *patches*, bless his soul...
37
38 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
39 ;; fixing and improving bookmark-time-to-save-p.
40
41 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
42 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
43 ;; and the menu-bar).
44
45 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
46 ;; suggestions and the code to implement them (like
47 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
48 ;; stuff).
49
50 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
51 ;; for his eminently sensible suggestion to separate bookmark-jump
52 ;; into bookmark-jump and bookmark-jump-noselect, which made many
53 ;; other things cleaner as well.
54
55 ;; Thanks to Roland McGrath for encouragement and help with defining
56 ;; autoloads on the menu-bar.
57
58 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
59 ;; values in bookmark-jump and bookmark-set. Everybody please keep
60 ;; all the keystrokes they save thereby and send them to him at the
61 ;; end of each year :-) (No, seriously, thanks Jonathan!)
62
63 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
64 ;; thinking up the annotations feature and implementing it so well.
65
66 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
67 ;; <olstad@msc.edu>.
68
69 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
70 ;; reported and fixed.
71
72 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
73
74 ;; Enough with the credits already, get on to the good stuff:
75
76 ;; FAVORITE CHINESE RESTAURANT:
77 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
78 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
79 \f
80 ;;;; Code:
81
82 (require 'pp)
83
84 ;;; Misc comments:
85 ;;
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
88 ;;
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
93
94 ;;; User Variables
95
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
99
100
101 (defcustom bookmark-use-annotations nil
102 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
105
106
107 (defcustom bookmark-save-flag t
108 "*Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
117
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
121
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
126
127
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "*The `.emacs.bmk' file used to be called this name.")
130
131
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
135
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "*File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
144
145
146 (defcustom bookmark-version-control 'nospecial
147 "*Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
155
156
157 (defcustom bookmark-completion-ignore-case t
158 "*Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
161
162
163 (defcustom bookmark-sort-flag t
164 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
169
170
171 (defcustom bookmark-automatically-show-annotations t
172 "*Nil means don't show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
175
176
177 (defcustom bookmark-bmenu-file-column 30
178 "*Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
182
183
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "*Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
188
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
192
193
194 (defcustom bookmark-menu-length 70
195 "*Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'boolean)
198
199
200 ;;; No user-serviceable parts beyond this point.
201
202 ;; Is it XEmacs?
203 (defconst bookmark-xemacsp
204 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
205
206
207 ;; Added for lucid emacs compatibility, db
208 (or (fboundp 'defalias) (fset 'defalias 'fset))
209
210 ;; suggested for lucid compatibility by david hughes:
211 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
212
213 ;; This variable is probably obsolete now...
214 (or (boundp 'baud-rate)
215 ;; some random value higher than 9600
216 (setq baud-rate 19200))
217
218
219 \f
220 ;;; Keymap stuff:
221
222 ;; Set up these bindings dumping time *only*;
223 ;; if the user alters them, don't override the user when loading bookmark.el.
224
225 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
226 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
227 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
228
229 ;;;###autoload
230 (defvar bookmark-map nil
231 "Keymap containing bindings to bookmark functions.
232 It is not bound to any key by default: to bind it
233 so that you have a bookmark prefix, just use `global-set-key' and bind a
234 key of your choice to `bookmark-map'. All interactive bookmark
235 functions have a binding in this keymap.")
236
237 ;;;###autoload
238 (define-prefix-command 'bookmark-map)
239
240 ;; Read the help on all of these functions for details...
241 ;;;###autoload
242 (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload
244 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
245 ;;;###autoload
246 (define-key bookmark-map "j" 'bookmark-jump)
247 ;;;###autoload
248 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
249 ;;;###autoload
250 (define-key bookmark-map "i" 'bookmark-insert)
251 ;;;###autoload
252 (define-key bookmark-map "e" 'edit-bookmarks)
253 ;;;###autoload
254 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
255 ;;;###autoload
256 (define-key bookmark-map "r" 'bookmark-rename)
257 ;;;###autoload
258 (define-key bookmark-map "d" 'bookmark-delete)
259 ;;;###autoload
260 (define-key bookmark-map "l" 'bookmark-load)
261 ;;;###autoload
262 (define-key bookmark-map "w" 'bookmark-write)
263 ;;;###autoload
264 (define-key bookmark-map "s" 'bookmark-save)
265
266
267 ;;; The annotation maps.
268 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
269 "Keymap for composing an annotation for a bookmark.")
270
271 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
272 'bookmark-send-annotation)
273
274
275 \f
276 ;;; Core variables and data structures:
277 (defvar bookmark-alist ()
278 "Association list of bookmarks and their records.
279 You probably don't want to change the value of this alist yourself;
280 instead, let the various bookmark functions do it for you.
281
282 The format of the alist is
283
284 \(BOOKMARK1 BOOKMARK2 ...\)
285
286 where each BOOKMARK is of the form
287
288 \(NAME
289 \(filename . FILE\)
290 \(front-context-string . FRONT-STR\)
291 \(rear-context-string . REAR-STR\)
292 \(position . POS\)
293 \(info-node . POS\)
294 \(annotation . ANNOTATION\)\)
295
296 So the cdr of each bookmark is an alist too.
297 `info-node' is optional, by the way.")
298
299
300 (defvar bookmarks-already-loaded nil)
301
302
303 ;; more stuff added by db.
304
305 (defvar bookmark-current-bookmark nil
306 "Name of bookmark most recently used in the current file.
307 It is buffer local, used to make moving a bookmark forward
308 through a file easier.")
309
310 (make-variable-buffer-local 'bookmark-current-bookmark)
311
312
313 (defvar bookmark-alist-modification-count 0
314 "Number of modifications to bookmark list since it was last saved.")
315
316
317 (defvar bookmark-search-size 16
318 "Length of the context strings recorded on either side of a bookmark.")
319
320
321 (defvar bookmark-current-point 0)
322 (defvar bookmark-yank-point 0)
323 (defvar bookmark-current-buffer nil)
324
325
326 \f
327 ;; Helper functions.
328
329 ;; Only functions on this page and the next one (file formats) need to
330 ;; know anything about the format of bookmark-alist entries.
331 ;; Everyone else should go through them.
332
333 (defun bookmark-name-from-full-record (full-record)
334 "Return name of FULL-RECORD \(an alist element instead of a string\)."
335 (car full-record))
336
337
338 (defun bookmark-all-names ()
339 "Return a list of all current bookmark names."
340 (bookmark-maybe-load-default-file)
341 (mapcar
342 (lambda (full-record)
343 (bookmark-name-from-full-record full-record))
344 bookmark-alist))
345
346
347 (defun bookmark-get-bookmark (bookmark)
348 "Return the full entry for BOOKMARK in bookmark-alist."
349 (assoc bookmark bookmark-alist))
350
351
352 (defun bookmark-get-bookmark-record (bookmark)
353 "Return the guts of the entry for BOOKMARK in bookmark-alist.
354 That is, all information but the name."
355 (car (cdr (bookmark-get-bookmark bookmark))))
356
357
358 (defun bookmark-set-name (bookmark newname)
359 "Set BOOKMARK's name to NEWNAME."
360 (setcar
361 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
362 newname))
363
364
365 (defun bookmark-get-annotation (bookmark)
366 "Return the annotation of BOOKMARK, or nil if none."
367 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
368
369
370 (defun bookmark-set-annotation (bookmark ann)
371 "Set the annotation of BOOKMARK to ANN."
372 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
373 (if cell
374 (setcdr cell ann)
375 (nconc (bookmark-get-bookmark-record bookmark)
376 (list (cons 'annotation ann))))))
377
378
379 (defun bookmark-get-filename (bookmark)
380 "Return the full filename of BOOKMARK."
381 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
382
383
384 (defun bookmark-set-filename (bookmark filename)
385 "Set the full filename of BOOKMARK to FILENAME."
386 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
387 (if cell
388 (setcdr cell filename)
389 (nconc (bookmark-get-bookmark-record bookmark)
390 (list (cons 'filename filename))))))
391
392
393 (defun bookmark-get-position (bookmark)
394 "Return the position \(i.e.: point\) of BOOKMARK."
395 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
396
397
398 (defun bookmark-set-position (bookmark position)
399 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
400 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
401 (if cell
402 (setcdr cell position)
403 (nconc (bookmark-get-bookmark-record bookmark)
404 (list (cons 'position position))))))
405
406
407 (defun bookmark-get-front-context-string (bookmark)
408 "Return the front-context-string of BOOKMARK."
409 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
410
411
412 (defun bookmark-set-front-context-string (bookmark string)
413 "Set the front-context-string of BOOKMARK to STRING."
414 (let ((cell (assq 'front-context-string
415 (bookmark-get-bookmark-record bookmark))))
416 (if cell
417 (setcdr cell string)
418 (nconc (bookmark-get-bookmark-record bookmark)
419 (list (cons 'front-context-string string))))))
420
421
422 (defun bookmark-get-rear-context-string (bookmark)
423 "Return the rear-context-string of BOOKMARK."
424 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
425
426
427 (defun bookmark-set-rear-context-string (bookmark string)
428 "Set the rear-context-string of BOOKMARK to STRING."
429 (let ((cell (assq 'rear-context-string
430 (bookmark-get-bookmark-record bookmark))))
431 (if cell
432 (setcdr cell string)
433 (nconc (bookmark-get-bookmark-record bookmark)
434 (list (cons 'rear-context-string string))))))
435
436
437 (defun bookmark-get-info-node (bookmark)
438 "Get the info node associated with BOOKMARK."
439 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
440
441
442 (defun bookmark-set-info-node (bookmark node)
443 "Set the Info node of BOOKMARK to NODE."
444 (let ((cell (assq 'info-node
445 (bookmark-get-bookmark-record bookmark))))
446 (if cell
447 (setcdr cell node)
448 (nconc (bookmark-get-bookmark-record bookmark)
449 (list (cons 'info-node node)))))
450
451 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
452 (sit-for 4)
453 )
454
455
456 (defvar bookmark-history nil
457 "The history list for bookmark functions.")
458
459
460 (defun bookmark-completing-read (prompt &optional default)
461 "Prompting with PROMPT, read a bookmark name in completion.
462 PROMPT will get a \": \" stuck on the end no matter what, so you
463 probably don't want to include one yourself.
464 Optional second arg DEFAULT is a string to return if the user enters
465 the empty string."
466 (bookmark-maybe-load-default-file) ; paranoia
467 (let* ((completion-ignore-case bookmark-completion-ignore-case)
468 (default default)
469 (prompt (if default
470 (concat prompt (format " (%s): " default))
471 (concat prompt ": ")))
472 (str
473 (completing-read prompt
474 bookmark-alist
475 nil
476 0
477 nil
478 'bookmark-history)))
479 (if (string-equal "" str)
480 (list default)
481 (list str))))
482
483
484 (defmacro bookmark-maybe-historicize-string (string)
485 "Put STRING into the bookmark prompt history, if caller non-interactive.
486 We need this because sometimes bookmark functions are invoked from
487 menus, so `completing-read' never gets a chance to set `bookmark-history'."
488 (` (or
489 (interactive-p)
490 (setq bookmark-history (cons (, string) bookmark-history)))))
491
492
493 (defun bookmark-make (name &optional annotation overwrite info-node)
494 "Make a bookmark named NAME.
495 Optional second arg ANNOTATION gives it an annotation.
496 Optional third arg OVERWRITE means replace any existing bookmarks with
497 this name.
498 Optional fourth arg INFO-NODE means this bookmark is at info node
499 INFO-NODE, so record this fact in the bookmark's entry."
500 (bookmark-maybe-load-default-file)
501 (let ((stripped-name (copy-sequence name)))
502 (or bookmark-xemacsp
503 ;; XEmacs's `set-text-properties' doesn't work on
504 ;; free-standing strings, apparently.
505 (set-text-properties 0 (length stripped-name) nil stripped-name))
506 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
507 ;; already existing bookmark under that name and
508 ;; no prefix arg means just overwrite old bookmark
509 (setcdr (bookmark-get-bookmark stripped-name)
510 (list (bookmark-make-cell annotation info-node)))
511
512 ;; otherwise just cons it onto the front (either the bookmark
513 ;; doesn't exist already, or there is no prefix arg. In either
514 ;; case, we want the new bookmark consed onto the alist...)
515
516 (setq bookmark-alist
517 (cons
518 (list stripped-name
519 (bookmark-make-cell annotation info-node))
520 bookmark-alist)))
521
522 ;; Added by db
523 (setq bookmark-current-bookmark stripped-name)
524 (setq bookmark-alist-modification-count
525 (1+ bookmark-alist-modification-count))
526 (if (bookmark-time-to-save-p)
527 (bookmark-save))))
528
529
530 (defun bookmark-make-cell (annotation &optional info-node)
531 "Return the record part of a new bookmark, given ANNOTATION.
532 Must be at the correct position in the buffer in which the bookmark is
533 being set. This might change someday.
534 Optional second arg INFO-NODE means this bookmark is at info node
535 INFO-NODE, so record this fact in the bookmark's entry."
536 (let ((the-record
537 (` ((filename . (, (bookmark-buffer-file-name)))
538 (front-context-string
539 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
540 (buffer-substring-no-properties
541 (point)
542 (+ (point) bookmark-search-size))
543 nil)))
544 (rear-context-string
545 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
546 (buffer-substring-no-properties
547 (point)
548 (- (point) bookmark-search-size))
549 nil)))
550 (position . (, (point)))
551 ))))
552
553 ;; Now fill in the optional parts:
554
555 ;; Take no chances with text properties
556 (set-text-properties 0 (length annotation) nil annotation)
557 (set-text-properties 0 (length info-node) nil info-node)
558
559 (if annotation
560 (nconc the-record (list (cons 'annotation annotation))))
561 (if info-node
562 (nconc the-record (list (cons 'info-node info-node))))
563
564 ;; Finally, return the completed record.
565 the-record))
566
567
568 \f
569 ;;; File format stuff
570
571 ;; The OLD format of the bookmark-alist was:
572 ;;
573 ;; ((bookmark-name (filename
574 ;; string-in-front
575 ;; string-behind
576 ;; point))
577 ;; ...)
578 ;;
579 ;; The NEW format of the bookmark-alist is:
580 ;;
581 ;; ((bookmark-name ((filename . FILENAME)
582 ;; (front-context-string . string-in-front)
583 ;; (rear-context-string . string-behind)
584 ;; (position . POINT)
585 ;; (annotation . annotation)
586 ;; (whatever . VALUE)
587 ;; ...
588 ;; ))
589 ;; ...)
590 ;;
591 ;;
592 ;; I switched to using an internal as well as external alist because I
593 ;; felt that would be a more flexible framework in which to add
594 ;; features. It means that the order in which values appear doesn't
595 ;; matter, and it means that arbitrary values can be added without
596 ;; risk of interfering with existing ones.
597 ;;
598 ;; BOOKMARK-NAME is the string the user gives the bookmark and
599 ;; accesses it by from then on.
600 ;;
601 ;; FILENAME is the location of the file in which the bookmark is set.
602 ;;
603 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
604 ;; context in front of the point at which the bookmark is set.
605 ;;
606 ;; STRING-BEHIND is the same thing, but after the point.
607 ;;
608 ;; The context strings exist so that modifications to a file don't
609 ;; necessarily cause a bookmark's position to be invalidated.
610 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
611 ;; case the file has changed since the bookmark was set. It will
612 ;; attempt to place the user before the changes, if there were any.
613 ;; ANNOTATION is the annotation for the bookmark; it may not exist
614 ;; (for backward compatibility), be nil (no annotation), or be a
615 ;; string.
616
617
618 (defconst bookmark-file-format-version 1
619 "The current version of the format used by bookmark files.
620 You should never need to change this.")
621
622
623 (defconst bookmark-end-of-version-stamp-marker
624 "-*- End Of Bookmark File Format Version Stamp -*-\n"
625 "This string marks the end of the version stamp in a bookmark file.")
626
627
628 (defun bookmark-alist-from-buffer ()
629 "Return a bookmark-alist (in any format) from the current buffer.
630 The buffer must of course contain bookmark format information.
631 Does not care from where in the buffer it is called, and does not
632 affect point."
633 (save-excursion
634 (goto-char (point-min))
635 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
636 (read (current-buffer))
637 ;; Else we're dealing with format version 0
638 (if (search-forward "(" nil t)
639 (progn
640 (forward-char -1)
641 (read (current-buffer)))
642 ;; Else no hope of getting information here.
643 (error "Not bookmark format")))))
644
645
646 (defun bookmark-upgrade-version-0-alist (old-list)
647 "Upgrade a version 0 alist OLD-LIST to the current version."
648 (mapcar
649 (lambda (bookmark)
650 (let* ((name (car bookmark))
651 (record (car (cdr bookmark)))
652 (filename (nth 0 record))
653 (front-str (nth 1 record))
654 (rear-str (nth 2 record))
655 (position (nth 3 record))
656 (ann (nth 4 record)))
657 (list
658 name
659 (` ((filename . (, filename))
660 (front-context-string . (, (or front-str "")))
661 (rear-context-string . (, (or rear-str "")))
662 (position . (, position))
663 (annotation . (, ann)))))))
664 old-list))
665
666
667 (defun bookmark-upgrade-file-format-from-0 ()
668 "Upgrade a bookmark file of format 0 (the original format) to format 1.
669 This expects to be called from point-min in a bookmark file."
670 (message "Upgrading bookmark format from 0 to %d..."
671 bookmark-file-format-version)
672 (let* ((old-list (bookmark-alist-from-buffer))
673 (new-list (bookmark-upgrade-version-0-alist old-list)))
674 (delete-region (point-min) (point-max))
675 (bookmark-insert-file-format-version-stamp)
676 (pp new-list (current-buffer))
677 (save-buffer))
678 (goto-char (point-min))
679 (message "Upgrading bookmark format from 0 to %d...done"
680 bookmark-file-format-version)
681 )
682
683
684 (defun bookmark-grok-file-format-version ()
685 "Return an integer which is the file-format version of this bookmark file.
686 This expects to be called from point-min in a bookmark file."
687 (if (looking-at "^;;;;")
688 (save-excursion
689 (save-match-data
690 (re-search-forward "[0-9]")
691 (forward-char -1)
692 (read (current-buffer))))
693 ;; Else this is format version 0, the original one, which didn't
694 ;; even have version stamps.
695 0))
696
697
698 (defun bookmark-maybe-upgrade-file-format ()
699 "Check the file-format version of this bookmark file.
700 If the version is not up-to-date, upgrade it automatically.
701 This expects to be called from point-min in a bookmark file."
702 (let ((version (bookmark-grok-file-format-version)))
703 (cond
704 ((= version bookmark-file-format-version)
705 ) ; home free -- version is current
706 ((= version 0)
707 (bookmark-upgrade-file-format-from-0))
708 (t
709 (error "Bookmark file format version strangeness")))))
710
711
712 (defun bookmark-insert-file-format-version-stamp ()
713 "Insert text indicating current version of bookmark file format."
714 (insert
715 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
716 bookmark-file-format-version))
717 (insert ";;; This format is meant to be slightly human-readable;\n"
718 ";;; nevertheless, you probably don't want to edit it.\n"
719 ";;; "
720 bookmark-end-of-version-stamp-marker))
721
722
723 ;;; end file-format stuff
724
725 \f
726 ;;; Core code:
727
728 ;;;###autoload
729 (defun bookmark-set (&optional name parg)
730 "Set a bookmark named NAME inside a file.
731 If name is nil, then the user will be prompted.
732 With prefix arg, will not overwrite a bookmark that has the same name
733 as NAME if such a bookmark already exists, but instead will \"push\"
734 the new bookmark onto the bookmark alist. Thus the most recently set
735 bookmark with name NAME would be the one in effect at any given time,
736 but the others are still there, should you decide to delete the most
737 recent one.
738
739 To yank words from the text of the buffer and use them as part of the
740 bookmark name, type C-w while setting a bookmark. Successive C-w's
741 yank successive words.
742
743 Typing C-u inserts the name of the last bookmark used in the buffer
744 \(as an aid in using a single bookmark name to track your progress
745 through a large file\). If no bookmark was used, then C-u inserts the
746 name of the file being visited.
747
748 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
749 and it removes only the first instance of a bookmark with that name from
750 the list of bookmarks.\)"
751 (interactive (list nil current-prefix-arg))
752 (or
753 (bookmark-buffer-file-name)
754 (error "Buffer not visiting a file or directory"))
755
756 (bookmark-maybe-load-default-file)
757
758 (setq bookmark-current-point (point))
759 (setq bookmark-yank-point (point))
760 (setq bookmark-current-buffer (current-buffer))
761
762 (let* ((default (or bookmark-current-bookmark
763 (bookmark-buffer-name)))
764 (str
765 (or name
766 (read-from-minibuffer
767 (format "Set bookmark (%s): " default)
768 nil
769 (let ((now-map (copy-keymap minibuffer-local-map)))
770 (define-key now-map "\C-w" 'bookmark-yank-word)
771 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
772 now-map))))
773 (annotation nil))
774 (and (string-equal str "") (setq str default))
775 ;; Ask for an annotation buffer for this bookmark
776 (if bookmark-use-annotations
777 (bookmark-read-annotation parg str)
778 (bookmark-make str annotation parg (bookmark-info-current-node))
779 (setq bookmark-current-bookmark str)
780 (bookmark-bmenu-surreptitiously-rebuild-list)
781 (goto-char bookmark-current-point))))
782
783
784 (defun bookmark-info-current-node ()
785 "If in Info-mode, return current node name (a string), else nil."
786 (if (eq major-mode 'Info-mode)
787 Info-current-node))
788
789
790 (defun bookmark-kill-line (&optional newline-too)
791 "Kill from point to end of line.
792 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
793 Does not affect the kill-ring."
794 (let ((eol (save-excursion (end-of-line) (point))))
795 (delete-region (point) eol)
796 (if (and newline-too (looking-at "\n"))
797 (delete-char 1))))
798
799
800 ;; Defvars to avoid compilation warnings:
801 (defvar bookmark-annotation-paragraph nil)
802 (defvar bookmark-annotation-name nil)
803 (defvar bookmark-annotation-buffer nil)
804 (defvar bookmark-annotation-file nil)
805 (defvar bookmark-annotation-point nil)
806
807
808 (defun bookmark-send-annotation ()
809 "Use buffer contents as the annotation for a bookmark.
810 Exclude lines that begin with `#'.
811 Store the annotation text in the bookmark list with
812 the bookmark (and file, and point) specified in buffer local variables."
813 (interactive)
814 (if (not (eq major-mode 'bookmark-read-annotation-mode))
815 (error "Not in bookmark-read-annotation-mode"))
816 (goto-char (point-min))
817 (while (< (point) (point-max))
818 (if (looking-at "^#")
819 (bookmark-kill-line t)
820 (forward-line 1)))
821 (let ((annotation (buffer-string))
822 (parg bookmark-annotation-paragraph)
823 (bookmark bookmark-annotation-name)
824 (pt bookmark-annotation-point)
825 (buf bookmark-annotation-buffer))
826 ;; for bookmark-make-cell to work, we need to be
827 ;; in the relevant buffer, at the relevant point.
828 ;; Actually, bookmark-make-cell should probably be re-written,
829 ;; to avoid this need. Should I handle the error if a buffer is
830 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
831 (save-excursion
832 (pop-to-buffer buf)
833 (goto-char pt)
834 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
835 (setq bookmark-current-bookmark bookmark))
836 (bookmark-bmenu-surreptitiously-rebuild-list)
837 (goto-char bookmark-current-point))
838 (kill-buffer (current-buffer)))
839
840
841 (defun bookmark-default-annotation-text (bookmark)
842 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
843 "# All lines which start with a '#' will be deleted.\n"
844 "# Type C-c C-c when done.\n#\n"
845 "# Author: " (user-full-name) " <" (user-login-name) "@"
846 (system-name) ">\n"
847 "# Date: " (current-time-string) "\n"))
848
849
850 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
851 "Function to return default text to use for a bookmark annotation.
852 It takes the name of the bookmark, as a string, as an arg.")
853
854 (defun bookmark-read-annotation-mode (buf point parg bookmark)
855 "Mode for composing annotations for a bookmark.
856 Wants BUF POINT PARG and BOOKMARK.
857 When you have finished composing, type \\[bookmark-send-annotation] to send
858 the annotation.
859
860 \\{bookmark-read-annotation-mode-map}
861 "
862 (interactive)
863 (kill-all-local-variables)
864 (make-local-variable 'bookmark-annotation-paragraph)
865 (make-local-variable 'bookmark-annotation-name)
866 (make-local-variable 'bookmark-annotation-buffer)
867 (make-local-variable 'bookmark-annotation-file)
868 (make-local-variable 'bookmark-annotation-point)
869 (setq bookmark-annotation-paragraph parg)
870 (setq bookmark-annotation-name bookmark)
871 (setq bookmark-annotation-buffer buf)
872 (setq bookmark-annotation-file (buffer-file-name buf))
873 (setq bookmark-annotation-point point)
874 (use-local-map bookmark-read-annotation-mode-map)
875 (setq major-mode 'bookmark-read-annotation-mode)
876 (insert (funcall bookmark-read-annotation-text-func bookmark))
877 (run-hooks 'text-mode-hook))
878
879
880 (defun bookmark-read-annotation (parg bookmark)
881 "Pop up a buffer for entering a bookmark annotation.
882 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
883 (let ((buf (current-buffer))
884 (point (point)))
885 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
886 (bookmark-read-annotation-mode buf point parg bookmark)))
887
888
889 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
890 "Keymap for editing an annotation of a bookmark.")
891
892
893 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
894 'bookmark-send-edited-annotation)
895
896
897 (defun bookmark-edit-annotation-mode (bookmark)
898 "Mode for editing the annotation of bookmark BOOKMARK.
899 When you have finished composing, type \\[bookmark-send-annotation].
900
901 \\{bookmark-edit-annotation-mode-map}
902 "
903 (interactive)
904 (kill-all-local-variables)
905 (make-local-variable 'bookmark-annotation-name)
906 (setq bookmark-annotation-name bookmark)
907 (use-local-map bookmark-edit-annotation-mode-map)
908 (setq major-mode 'bookmark-edit-annotation-mode)
909 (insert (funcall bookmark-read-annotation-text-func bookmark))
910 (let ((annotation (bookmark-get-annotation bookmark)))
911 (if (and annotation (not (string-equal annotation "")))
912 (insert annotation)))
913 (run-hooks 'text-mode-hook))
914
915
916 (defun bookmark-send-edited-annotation ()
917 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
918 (interactive)
919 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
920 (error "Not in bookmark-edit-annotation-mode"))
921 (goto-char (point-min))
922 (while (< (point) (point-max))
923 (if (looking-at "^#")
924 (bookmark-kill-line t)
925 (forward-line 1)))
926 (let ((annotation (buffer-string))
927 (bookmark bookmark-annotation-name))
928 (bookmark-set-annotation bookmark annotation)
929 (bookmark-bmenu-surreptitiously-rebuild-list)
930 (goto-char bookmark-current-point))
931 (kill-buffer (current-buffer)))
932
933
934 (defun bookmark-edit-annotation (bookmark)
935 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
936 (let ((buf (current-buffer))
937 (point (point)))
938 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
939 (bookmark-edit-annotation-mode bookmark)))
940
941
942 (defun bookmark-insert-current-bookmark ()
943 "Insert this buffer's value of bookmark-current-bookmark.
944 Default to file name if it's nil."
945 (interactive)
946 (let ((str
947 (save-excursion
948 (set-buffer bookmark-current-buffer)
949 bookmark-current-bookmark)))
950 (if str (insert str) (bookmark-insert-buffer-name))))
951
952
953 (defun bookmark-insert-buffer-name ()
954 "Insert the current file name into the bookmark name being set.
955 The directory part of the file name is not used."
956 (interactive)
957 (let ((str
958 (save-excursion
959 (set-buffer bookmark-current-buffer)
960 (bookmark-buffer-name))))
961 (insert str)))
962
963
964 (defun bookmark-buffer-name ()
965 "Return the name of the current buffer's file, non-directory.
966 In Info, return the current node."
967 (cond
968 ;; Are we in Info?
969 ((string-equal mode-name "Info") Info-current-node)
970 ;; Or are we a file?
971 (buffer-file-name (file-name-nondirectory buffer-file-name))
972 ;; Or are we a directory?
973 ((and (boundp 'dired-directory) dired-directory)
974 (let* ((dirname (if (stringp dired-directory)
975 dired-directory
976 (car dired-directory)))
977 (idx (1- (length dirname))))
978 ;; Strip the trailing slash.
979 (if (= ?/ (aref dirname idx))
980 (file-name-nondirectory (substring dirname 0 idx))
981 ;; Else return the current-buffer
982 (buffer-name (current-buffer)))))
983 ;; If all else fails, use the buffer's name.
984 (t
985 (buffer-name (current-buffer)))))
986
987
988 (defun bookmark-yank-word ()
989 (interactive)
990 ;; get the next word from the buffer and append it to the name of
991 ;; the bookmark currently being set.
992 (let ((string (save-excursion
993 (set-buffer bookmark-current-buffer)
994 (goto-char bookmark-yank-point)
995 (buffer-substring-no-properties
996 (point)
997 (progn
998 (forward-word 1)
999 (setq bookmark-yank-point (point)))))))
1000 (insert string)))
1001
1002
1003 (defun bookmark-buffer-file-name ()
1004 "Return the current buffer's file in a way useful for bookmarks.
1005 For example, if this is a Info buffer, return the Info file's name."
1006 (if (eq major-mode 'Info-mode)
1007 Info-current-file
1008 (or
1009 buffer-file-name
1010 (if (and (boundp 'dired-directory) dired-directory)
1011 (if (stringp dired-directory)
1012 dired-directory
1013 (car dired-directory))))))
1014
1015
1016 (defun bookmark-maybe-load-default-file ()
1017 (and (not bookmarks-already-loaded)
1018 (null bookmark-alist)
1019 (prog2
1020 (and
1021 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1022 ;; to be renamed.
1023 (file-exists-p (expand-file-name bookmark-old-default-file))
1024 (not (file-exists-p (expand-file-name bookmark-default-file)))
1025 (rename-file (expand-file-name bookmark-old-default-file)
1026 (expand-file-name bookmark-default-file)))
1027 ;; return t so the `and' will continue...
1028 t)
1029
1030 (file-readable-p (expand-file-name bookmark-default-file))
1031 (bookmark-load bookmark-default-file t t)
1032 (setq bookmarks-already-loaded t)))
1033
1034
1035 (defun bookmark-maybe-sort-alist ()
1036 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1037 ;;is non-nil, then return a sorted copy of the alist.
1038 (if bookmark-sort-flag
1039 (setq bookmark-alist
1040 (sort (copy-alist bookmark-alist)
1041 (function
1042 (lambda (x y) (string-lessp (car x) (car y))))))))
1043
1044
1045 ;;;###autoload
1046 (defun bookmark-jump (bookmark)
1047 "Jump to bookmark BOOKMARK (a point in some file).
1048 You may have a problem using this function if the value of variable
1049 `bookmark-alist' is nil. If that happens, you need to load in some
1050 bookmarks. See help on function `bookmark-load' for more about
1051 this.
1052
1053 If the file pointed to by BOOKMARK no longer exists, you will be asked
1054 if you wish to give the bookmark a new location, and bookmark-jump
1055 will then jump to the new location, as well as recording it in place
1056 of the old one in the permanent bookmark record."
1057 (interactive
1058 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1059 (bookmark-maybe-historicize-string bookmark)
1060 (let ((cell (bookmark-jump-noselect bookmark)))
1061 (and cell
1062 (switch-to-buffer (car cell))
1063 (goto-char (cdr cell))
1064 (if bookmark-automatically-show-annotations
1065 ;; if there is an annotation for this bookmark,
1066 ;; show it in a buffer.
1067 (bookmark-show-annotation bookmark)))))
1068
1069
1070 (defun bookmark-file-or-variation-thereof (file)
1071 "Return FILE (a string) if it exists in any reasonable variation, else nil.
1072 Reasonable variations are FILE.gz, FILE.Z, FILE.info, FILE.info.gz, etc."
1073 (cond
1074 ((file-exists-p file) file)
1075 ((file-exists-p (concat file ".Z")) (concat file ".Z"))
1076 ((file-exists-p (concat file ".gz")) (concat file ".gz"))
1077 ((file-exists-p (concat file ".z")) (concat file ".z"))
1078 ((file-exists-p (concat file ".info")) (concat file ".info"))
1079 ((file-exists-p (concat file ".info.gz")) (concat file ".info.gz"))
1080 ((file-exists-p (concat file ".info.Z")) (concat file ".info.Z"))
1081 ((file-exists-p (concat file ".info.z")) (concat file ".info.z"))
1082 ((vc-backend file) file) ; maybe VC has it?
1083 (t nil)))
1084
1085
1086 (defun bookmark-jump-noselect (str)
1087 ;; a leetle helper for bookmark-jump :-)
1088 ;; returns (BUFFER . POINT)
1089 (bookmark-maybe-load-default-file)
1090 (let* ((file (expand-file-name (bookmark-get-filename str)))
1091 (forward-str (bookmark-get-front-context-string str))
1092 (behind-str (bookmark-get-rear-context-string str))
1093 (place (bookmark-get-position str))
1094 (info-node (bookmark-get-info-node str))
1095 (orig-file file)
1096 )
1097 (if (setq file (bookmark-file-or-variation-thereof file))
1098 (save-excursion
1099 (save-window-excursion
1100 (if info-node
1101 ;; Info nodes must be visited with care.
1102 (progn
1103 (require 'info)
1104 (Info-find-node file info-node))
1105 ;; Else no Info. Can do an ordinary find-file:
1106 (set-buffer (find-file-noselect file))
1107 (goto-char place))
1108
1109 ;; Go searching forward first. Then, if forward-str exists and
1110 ;; was found in the file, we can search backward for behind-str.
1111 ;; Rationale is that if text was inserted between the two in the
1112 ;; file, it's better to be put before it so you can read it,
1113 ;; rather than after and remain perhaps unaware of the changes.
1114 (if forward-str
1115 (if (search-forward forward-str (point-max) t)
1116 (goto-char (match-beginning 0))))
1117 (if behind-str
1118 (if (search-backward behind-str (point-min) t)
1119 (goto-char (match-end 0))))
1120 ;; added by db
1121 (setq bookmark-current-bookmark str)
1122 (cons (current-buffer) (point))))
1123
1124 ;; Else unable to find the marked file, so ask if user wants to
1125 ;; relocate the bookmark, else remind them to consider deletion.
1126 (ding)
1127 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1128 " nonexistent. Relocate \""
1129 str
1130 "\"? "))
1131 (progn
1132 (bookmark-relocate str)
1133 ;; gasp! It's a recursive function call in Emacs Lisp!
1134 (bookmark-jump-noselect str))
1135 (message
1136 "Bookmark not relocated; consider removing it \(%s\)." str)
1137 nil))))
1138
1139
1140 ;;;###autoload
1141 (defun bookmark-relocate (bookmark)
1142 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1143 This makes an already existing bookmark point to that file, instead of
1144 the one it used to point at. Useful when a file has been renamed
1145 after a bookmark was set in it."
1146 (interactive (bookmark-completing-read "Bookmark to relocate"))
1147 (bookmark-maybe-historicize-string bookmark)
1148 (bookmark-maybe-load-default-file)
1149 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1150 (newloc (expand-file-name
1151 (read-file-name
1152 (format "Relocate %s to: " bookmark)
1153 (file-name-directory bmrk-filename)))))
1154 (bookmark-set-filename bookmark newloc)
1155 (bookmark-bmenu-surreptitiously-rebuild-list)))
1156
1157
1158 ;;;###autoload
1159 (defun bookmark-insert-location (bookmark &optional no-history)
1160 "Insert the name of the file associated with BOOKMARK.
1161 Optional second arg NO-HISTORY means don't record this in the
1162 minibuffer history list `bookmark-history'."
1163 (interactive (bookmark-completing-read "Insert bookmark location"))
1164 (or no-history (bookmark-maybe-historicize-string bookmark))
1165 (let ((start (point)))
1166 (prog1
1167 (insert (bookmark-location bookmark)) ; *Return this line*
1168 (if (and (display-color-p) (display-mouse-p))
1169 (put-text-property start
1170 (save-excursion (re-search-backward
1171 "[^ \t]")
1172 (1+ (point)))
1173 'mouse-face 'highlight)))))
1174
1175 ;;;###autoload
1176 (defalias 'bookmark-locate 'bookmark-insert-location)
1177
1178 (defun bookmark-location (bookmark)
1179 "Return the name of the file associated with BOOKMARK."
1180 (bookmark-maybe-load-default-file)
1181 (bookmark-get-filename bookmark))
1182
1183
1184 ;;;###autoload
1185 (defun bookmark-rename (old &optional new)
1186 "Change the name of OLD bookmark to NEW name.
1187 If called from keyboard, prompt for OLD and NEW. If called from
1188 menubar, select OLD from a menu and prompt for NEW.
1189
1190 If called from Lisp, prompt for NEW if only OLD was passed as an
1191 argument. If called with two strings, then no prompting is done. You
1192 must pass at least OLD when calling from Lisp.
1193
1194 While you are entering the new name, consecutive C-w's insert
1195 consecutive words from the text of the buffer into the new bookmark
1196 name."
1197 (interactive (bookmark-completing-read "Old bookmark name"))
1198 (bookmark-maybe-historicize-string old)
1199 (bookmark-maybe-load-default-file)
1200
1201 (setq bookmark-current-point (point))
1202 (setq bookmark-yank-point (point))
1203 (setq bookmark-current-buffer (current-buffer))
1204 (let ((newname
1205 (or new ; use second arg, if non-nil
1206 (read-from-minibuffer
1207 "New name: "
1208 nil
1209 (let ((now-map (copy-keymap minibuffer-local-map)))
1210 (define-key now-map "\C-w" 'bookmark-yank-word)
1211 now-map)
1212 nil
1213 'bookmark-history))))
1214 (bookmark-set-name old newname)
1215 (setq bookmark-current-bookmark newname)
1216 (bookmark-bmenu-surreptitiously-rebuild-list)
1217 (setq bookmark-alist-modification-count
1218 (1+ bookmark-alist-modification-count))
1219 (if (bookmark-time-to-save-p)
1220 (bookmark-save))))
1221
1222
1223 ;;;###autoload
1224 (defun bookmark-insert (bookmark)
1225 "Insert the text of the file pointed to by bookmark BOOKMARK.
1226 You may have a problem using this function if the value of variable
1227 `bookmark-alist' is nil. If that happens, you need to load in some
1228 bookmarks. See help on function `bookmark-load' for more about
1229 this."
1230 (interactive (bookmark-completing-read "Insert bookmark contents"))
1231 (bookmark-maybe-historicize-string bookmark)
1232 (bookmark-maybe-load-default-file)
1233 (let ((orig-point (point))
1234 (str-to-insert
1235 (save-excursion
1236 (set-buffer (car (bookmark-jump-noselect bookmark)))
1237 (buffer-string))))
1238 (insert str-to-insert)
1239 (push-mark)
1240 (goto-char orig-point)))
1241
1242
1243 ;;;###autoload
1244 (defun bookmark-delete (bookmark &optional batch)
1245 "Delete BOOKMARK from the bookmark list.
1246 Removes only the first instance of a bookmark with that name. If
1247 there are one or more other bookmarks with the same name, they will
1248 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1249 one most recently used in this file, if any\).
1250 Optional second arg BATCH means don't update the bookmark list buffer,
1251 probably because we were called from there."
1252 (interactive
1253 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1254 (bookmark-maybe-historicize-string bookmark)
1255 (bookmark-maybe-load-default-file)
1256 (let ((will-go (bookmark-get-bookmark bookmark)))
1257 (setq bookmark-alist (delq will-go bookmark-alist))
1258 ;; Added by db, nil bookmark-current-bookmark if the last
1259 ;; occurrence has been deleted
1260 (or (bookmark-get-bookmark bookmark-current-bookmark)
1261 (setq bookmark-current-bookmark nil)))
1262 ;; Don't rebuild the list
1263 (if batch
1264 nil
1265 (bookmark-bmenu-surreptitiously-rebuild-list)
1266 (setq bookmark-alist-modification-count
1267 (1+ bookmark-alist-modification-count))
1268 (if (bookmark-time-to-save-p)
1269 (bookmark-save))))
1270
1271
1272 (defun bookmark-time-to-save-p (&optional last-time)
1273 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1274 ;; finds out whether it's time to save bookmarks to a file, by
1275 ;; examining the value of variable bookmark-save-flag, and maybe
1276 ;; bookmark-alist-modification-count. Returns t if they should be
1277 ;; saved, nil otherwise. if last-time is non-nil, then this is
1278 ;; being called when emacs is killed.
1279 (cond (last-time
1280 (and (> bookmark-alist-modification-count 0)
1281 bookmark-save-flag))
1282 ((numberp bookmark-save-flag)
1283 (>= bookmark-alist-modification-count bookmark-save-flag))
1284 (t
1285 nil)))
1286
1287
1288 ;;;###autoload
1289 (defun bookmark-write ()
1290 "Write bookmarks to a file (reading the file name with the minibuffer).
1291 Don't use this in Lisp programs; use `bookmark-save' instead."
1292 (interactive)
1293 (bookmark-maybe-load-default-file)
1294 (bookmark-save t))
1295
1296
1297 ;;;###autoload
1298 (defun bookmark-save (&optional parg file)
1299 "Save currently defined bookmarks.
1300 Saves by default in the file defined by the variable
1301 `bookmark-default-file'. With a prefix arg, save it in file FILE
1302 \(second argument\).
1303
1304 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1305 and FILE, and if you just want it to write to the default file, then
1306 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1307 instead. If you pass in one argument, and it is non-nil, then the
1308 user will be interactively queried for a file to save in.
1309
1310 When you want to load in the bookmarks from a file, use
1311 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1312 for a file, defaulting to the file defined by variable
1313 `bookmark-default-file'."
1314 (interactive "P")
1315 (bookmark-maybe-load-default-file)
1316 (cond
1317 ((and (null parg) (null file))
1318 ;;whether interactive or not, write to default file
1319 (bookmark-write-file bookmark-default-file))
1320 ((and (null parg) file)
1321 ;;whether interactive or not, write to given file
1322 (bookmark-write-file file))
1323 ((and parg (not file))
1324 ;;have been called interactively w/ prefix arg
1325 (let ((file (read-file-name "File to save bookmarks in: ")))
1326 (bookmark-write-file file)))
1327 (t ; someone called us with prefix-arg *and* a file, so just write to file
1328 (bookmark-write-file file)))
1329 ;; signal that we have synced the bookmark file by setting this to
1330 ;; 0. If there was an error at any point before, it will not get
1331 ;; set, which is what we want.
1332 (setq bookmark-alist-modification-count 0))
1333
1334
1335 \f
1336 (defun bookmark-write-file (file)
1337 (save-excursion
1338 (save-window-excursion
1339 (if (>= baud-rate 9600)
1340 (message "Saving bookmarks to file %s..." file))
1341 (set-buffer (let ((enable-local-variables nil))
1342 (find-file-noselect file)))
1343 (goto-char (point-min))
1344 (delete-region (point-min) (point-max))
1345 (bookmark-insert-file-format-version-stamp)
1346 (pp bookmark-alist (current-buffer))
1347 (let ((version-control
1348 (cond
1349 ((null bookmark-version-control) nil)
1350 ((eq 'never bookmark-version-control) 'never)
1351 ((eq 'nospecial bookmark-version-control) version-control)
1352 (t
1353 t))))
1354 (write-file file)
1355 (kill-buffer (current-buffer))
1356 (if (>= baud-rate 9600)
1357 (message "Saving bookmarks to file %s...done" file))
1358 ))))
1359
1360
1361 (defun bookmark-import-new-list (new-list)
1362 ;; Walk over the new list, adding each individual bookmark
1363 ;; carefully. "Carefully" means checking against the existing
1364 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1365 ;; as necessary.
1366 (let ((lst new-list)
1367 (names (bookmark-all-names)))
1368 (while lst
1369 (let* ((full-record (car lst)))
1370 (bookmark-maybe-rename full-record names)
1371 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1372 (setq names (cons (bookmark-name-from-full-record full-record) names))
1373 (setq lst (cdr lst))))))
1374
1375
1376 (defun bookmark-maybe-rename (full-record names)
1377 ;; just a helper for bookmark-import-new-list; it is only for
1378 ;; readability that this is not inlined.
1379 ;;
1380 ;; Once this has found a free name, it sets full-record to that
1381 ;; name.
1382 (let ((found-name (bookmark-name-from-full-record full-record)))
1383 (if (member found-name names)
1384 ;; We've got a conflict, so generate a new name
1385 (let ((count 2)
1386 (new-name found-name))
1387 (while (member new-name names)
1388 (setq new-name (concat found-name (format "<%d>" count)))
1389 (setq count (1+ count)))
1390 (bookmark-set-name full-record new-name)))))
1391
1392
1393 ;;;###autoload
1394 (defun bookmark-load (file &optional overwrite no-msg)
1395 "Load bookmarks from FILE (which must be in bookmark format).
1396 Appends loaded bookmarks to the front of the list of bookmarks. If
1397 optional second argument OVERWRITE is non-nil, existing bookmarks are
1398 destroyed. Optional third arg NO-MSG means don't display any messages
1399 while loading.
1400
1401 If you load a file that doesn't contain a proper bookmark alist, you
1402 will corrupt Emacs's bookmark list. Generally, you should only load
1403 in files that were created with the bookmark functions in the first
1404 place. Your own personal bookmark file, `~/.emacs.bmk', is
1405 maintained automatically by Emacs; you shouldn't need to load it
1406 explicitly.
1407
1408 If you load a file containing bookmarks with the same names as
1409 bookmarks already present in your Emacs, the new bookmarks will get
1410 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1411 method buffers use to resolve name collisions."
1412 (interactive
1413 (list (read-file-name
1414 (format "Load bookmarks from: (%s) "
1415 bookmark-default-file)
1416 ;;Default might not be used often,
1417 ;;but there's no better default, and
1418 ;;I guess it's better than none at all.
1419 "~/" bookmark-default-file 'confirm)))
1420 (setq file (expand-file-name file))
1421 (if (file-readable-p file)
1422 (save-excursion
1423 (save-window-excursion
1424 (if (and (null no-msg) (>= baud-rate 9600))
1425 (message "Loading bookmarks from %s..." file))
1426 (set-buffer (let ((enable-local-variables nil))
1427 (find-file-noselect file)))
1428 (goto-char (point-min))
1429 (bookmark-maybe-upgrade-file-format)
1430 (let ((blist (bookmark-alist-from-buffer)))
1431 (if (listp blist)
1432 (progn
1433 (if overwrite
1434 (progn
1435 (setq bookmark-alist blist)
1436 (setq bookmark-alist-modification-count 0))
1437 ;; else
1438 (bookmark-import-new-list blist)
1439 (setq bookmark-alist-modification-count
1440 (1+ bookmark-alist-modification-count)))
1441 (if (string-equal
1442 (expand-file-name bookmark-default-file)
1443 file)
1444 (setq bookmarks-already-loaded t))
1445 (bookmark-bmenu-surreptitiously-rebuild-list))
1446 (error "Invalid bookmark list in %s" file)))
1447 (kill-buffer (current-buffer)))
1448 (if (and (null no-msg) (>= baud-rate 9600))
1449 (message "Loading bookmarks from %s...done" file)))
1450 (error "Cannot read bookmark file %s" file)))
1451
1452
1453 \f
1454 ;;; Code supporting the dired-like bookmark menu. Prefix is
1455 ;;; "bookmark-bmenu" for "buffer-menu":
1456
1457
1458 (defvar bookmark-bmenu-bookmark-column nil)
1459
1460
1461 (defvar bookmark-bmenu-hidden-bookmarks ())
1462
1463
1464 (defvar bookmark-bmenu-mode-map nil)
1465
1466
1467 (if bookmark-bmenu-mode-map
1468 nil
1469 (setq bookmark-bmenu-mode-map (make-keymap))
1470 (suppress-keymap bookmark-bmenu-mode-map t)
1471 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1472 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1473 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1474 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1475 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1476 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1477 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1478 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1479 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1480 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1481 (define-key bookmark-bmenu-mode-map "\C-o"
1482 'bookmark-bmenu-switch-other-window)
1483 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1484 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1485 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1486 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1487 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1488 (define-key bookmark-bmenu-mode-map " " 'next-line)
1489 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1490 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1491 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1492 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1493 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1494 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1495 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1496 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1497 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1498 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1499 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1500 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1501 (define-key bookmark-bmenu-mode-map [mouse-2]
1502 'bookmark-bmenu-other-window-with-mouse))
1503
1504
1505
1506 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1507 ;; data.
1508 (put 'bookmark-bmenu-mode 'mode-class 'special)
1509
1510
1511 ;; todo: need to display whether or not bookmark exists as a buffer in
1512 ;; flag column.
1513
1514 ;; Format:
1515 ;; FLAGS BOOKMARK [ LOCATION ]
1516
1517
1518 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1519 "Rebuild the Bookmark List if it exists.
1520 Don't affect the buffer ring order."
1521 (if (get-buffer "*Bookmark List*")
1522 (save-excursion
1523 (save-window-excursion
1524 (bookmark-bmenu-list)))))
1525
1526
1527 ;;;###autoload
1528 (defun bookmark-bmenu-list ()
1529 "Display a list of existing bookmarks.
1530 The list is displayed in a buffer named `*Bookmark List*'.
1531 The leftmost column displays a D if the bookmark is flagged for
1532 deletion, or > if it is flagged for displaying."
1533 (interactive)
1534 (bookmark-maybe-load-default-file)
1535 (if (interactive-p)
1536 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1537 (set-buffer (get-buffer-create "*Bookmark List*")))
1538 (let ((buffer-read-only nil))
1539 (delete-region (point-max) (point-min))
1540 (goto-char (point-min)) ;sure are playing it safe...
1541 (insert "% Bookmark\n- --------\n")
1542 (bookmark-maybe-sort-alist)
1543 (mapcar
1544 (lambda (full-record)
1545 ;; if a bookmark has an annotation, prepend a "*"
1546 ;; in the list of bookmarks.
1547 (let ((annotation (bookmark-get-annotation
1548 (bookmark-name-from-full-record full-record))))
1549 (if (and annotation (not (string-equal annotation "")))
1550 (insert " *")
1551 (insert " "))
1552 (let ((start (point)))
1553 (insert (bookmark-name-from-full-record full-record))
1554 (if (and (display-color-p) (display-mouse-p))
1555 (put-text-property start
1556 (save-excursion (re-search-backward
1557 "[^ \t]")
1558 (1+ (point)))
1559 'mouse-face 'highlight))
1560 (insert "\n")
1561 )))
1562 bookmark-alist))
1563 (goto-char (point-min))
1564 (forward-line 2)
1565 (bookmark-bmenu-mode)
1566 (if bookmark-bmenu-toggle-filenames
1567 (bookmark-bmenu-toggle-filenames t)))
1568
1569 ;;;###autoload
1570 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1571 ;;;###autoload
1572 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1573
1574
1575
1576 (defun bookmark-bmenu-mode ()
1577 "Major mode for editing a list of bookmarks.
1578 Each line describes one of the bookmarks in Emacs.
1579 Letters do not insert themselves; instead, they are commands.
1580 Bookmark names preceded by a \"*\" have annotations.
1581 \\<bookmark-bmenu-mode-map>
1582 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1583 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1584 Also show bookmarks marked using m in other windows.
1585 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1586 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1587 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1588 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1589 together with bookmark selected before this one in another window.
1590 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1591 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1592 so the bookmark menu bookmark remains visible in its window.
1593 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1594 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1595 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1596 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1597 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1598 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1599 With a prefix arg, prompts for a file to save in.
1600 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1601 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1602 With prefix argument, also move up one line.
1603 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1604 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1605 in another buffer.
1606 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1607 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1608 (kill-all-local-variables)
1609 (use-local-map bookmark-bmenu-mode-map)
1610 (setq truncate-lines t)
1611 (setq buffer-read-only t)
1612 (setq major-mode 'bookmark-bmenu-mode)
1613 (setq mode-name "Bookmark Menu")
1614 (run-hooks 'bookmark-bmenu-mode-hook))
1615
1616
1617 (defun bookmark-bmenu-toggle-filenames (&optional show)
1618 "Toggle whether filenames are shown in the bookmark list.
1619 Optional argument SHOW means show them unconditionally."
1620 (interactive)
1621 (cond
1622 (show
1623 (setq bookmark-bmenu-toggle-filenames nil)
1624 (bookmark-bmenu-show-filenames)
1625 (setq bookmark-bmenu-toggle-filenames t))
1626 (bookmark-bmenu-toggle-filenames
1627 (bookmark-bmenu-hide-filenames)
1628 (setq bookmark-bmenu-toggle-filenames nil))
1629 (t
1630 (bookmark-bmenu-show-filenames)
1631 (setq bookmark-bmenu-toggle-filenames t))))
1632
1633
1634 (defun bookmark-bmenu-show-filenames (&optional force)
1635 (if (and (not force) bookmark-bmenu-toggle-filenames)
1636 nil ;already shown, so do nothing
1637 (save-excursion
1638 (save-window-excursion
1639 (goto-char (point-min))
1640 (forward-line 2)
1641 (setq bookmark-bmenu-hidden-bookmarks ())
1642 (let ((buffer-read-only nil))
1643 (while (< (point) (point-max))
1644 (let ((bmrk (bookmark-bmenu-bookmark)))
1645 (setq bookmark-bmenu-hidden-bookmarks
1646 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1647 (let ((start (save-excursion (end-of-line) (point))))
1648 (move-to-column bookmark-bmenu-file-column t)
1649 ;; Strip off `mouse-face' from the white spaces region.
1650 (if (and (display-color-p) (display-mouse-p))
1651 (remove-text-properties start (point)
1652 '(mouse-face))))
1653 (delete-region (point) (progn (end-of-line) (point)))
1654 (insert " ")
1655 ;; Pass the NO-HISTORY arg:
1656 (bookmark-insert-location bmrk t)
1657 (forward-line 1))))))))
1658
1659
1660 (defun bookmark-bmenu-hide-filenames (&optional force)
1661 (if (and (not force) bookmark-bmenu-toggle-filenames)
1662 ;; nothing to hide if above is nil
1663 (save-excursion
1664 (save-window-excursion
1665 (goto-char (point-min))
1666 (forward-line 2)
1667 (setq bookmark-bmenu-hidden-bookmarks
1668 (nreverse bookmark-bmenu-hidden-bookmarks))
1669 (save-excursion
1670 (goto-char (point-min))
1671 (search-forward "Bookmark")
1672 (backward-word 1)
1673 (setq bookmark-bmenu-bookmark-column (current-column)))
1674 (save-excursion
1675 (let ((buffer-read-only nil))
1676 (while bookmark-bmenu-hidden-bookmarks
1677 (move-to-column bookmark-bmenu-bookmark-column t)
1678 (bookmark-kill-line)
1679 (let ((start (point)))
1680 (insert (car bookmark-bmenu-hidden-bookmarks))
1681 (if (and (display-color-p) (display-mouse-p))
1682 (put-text-property start
1683 (save-excursion (re-search-backward
1684 "[^ \t]")
1685 (1+ (point)))
1686 'mouse-face 'highlight)))
1687 (setq bookmark-bmenu-hidden-bookmarks
1688 (cdr bookmark-bmenu-hidden-bookmarks))
1689 (forward-line 1))))))))
1690
1691
1692 (defun bookmark-bmenu-check-position ()
1693 ;; Returns non-nil if on a line with a bookmark.
1694 ;; (The actual value returned is bookmark-alist).
1695 ;; Else reposition and try again, else return nil.
1696 (cond ((< (count-lines (point-min) (point)) 2)
1697 (goto-char (point-min))
1698 (forward-line 2)
1699 bookmark-alist)
1700 ((and (bolp) (eobp))
1701 (beginning-of-line 0)
1702 bookmark-alist)
1703 (t
1704 bookmark-alist)))
1705
1706
1707 (defun bookmark-bmenu-bookmark ()
1708 ;; return a string which is bookmark of this line.
1709 (if (bookmark-bmenu-check-position)
1710 (save-excursion
1711 (save-window-excursion
1712 (goto-char (point-min))
1713 (search-forward "Bookmark")
1714 (backward-word 1)
1715 (setq bookmark-bmenu-bookmark-column (current-column)))))
1716 (if bookmark-bmenu-toggle-filenames
1717 (bookmark-bmenu-hide-filenames))
1718 (save-excursion
1719 (save-window-excursion
1720 (beginning-of-line)
1721 (forward-char bookmark-bmenu-bookmark-column)
1722 (prog1
1723 (buffer-substring-no-properties (point)
1724 (progn
1725 (end-of-line)
1726 (point)))
1727 ;; well, this is certainly crystal-clear:
1728 (if bookmark-bmenu-toggle-filenames
1729 (bookmark-bmenu-toggle-filenames t))))))
1730
1731
1732 (defun bookmark-show-annotation (bookmark)
1733 "Display the annotation for bookmark named BOOKMARK in a buffer,
1734 if an annotation exists."
1735 (let ((annotation (bookmark-get-annotation bookmark)))
1736 (if (and annotation (not (string-equal annotation "")))
1737 (save-excursion
1738 (let ((old-buf (current-buffer)))
1739 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1740 (delete-region (point-min) (point-max))
1741 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1742 (insert annotation)
1743 (goto-char (point-min))
1744 (pop-to-buffer old-buf))))))
1745
1746
1747 (defun bookmark-show-all-annotations ()
1748 "Display the annotations for all bookmarks in a buffer."
1749 (let ((old-buf (current-buffer)))
1750 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1751 (delete-region (point-min) (point-max))
1752 (mapcar
1753 (lambda (full-record)
1754 (let* ((name (bookmark-name-from-full-record full-record))
1755 (ann (bookmark-get-annotation name)))
1756 (insert (concat name ":\n"))
1757 (if (and ann (not (string-equal ann "")))
1758 ;; insert the annotation, indented by 4 spaces.
1759 (progn
1760 (save-excursion (insert ann))
1761 (while (< (point) (point-max))
1762 (beginning-of-line) ; paranoia
1763 (insert " ")
1764 (forward-line)
1765 (end-of-line))))))
1766 bookmark-alist)
1767 (goto-char (point-min))
1768 (pop-to-buffer old-buf)))
1769
1770
1771 (defun bookmark-bmenu-mark ()
1772 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1773 (interactive)
1774 (beginning-of-line)
1775 (if (bookmark-bmenu-check-position)
1776 (let ((buffer-read-only nil))
1777 (delete-char 1)
1778 (insert ?>)
1779 (forward-line 1)
1780 (bookmark-bmenu-check-position))))
1781
1782
1783 (defun bookmark-bmenu-select ()
1784 "Select this line's bookmark; also display bookmarks marked with `>'.
1785 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1786 (interactive)
1787 (if (bookmark-bmenu-check-position)
1788 (let ((bmrk (bookmark-bmenu-bookmark))
1789 (menu (current-buffer))
1790 (others ())
1791 tem)
1792 (goto-char (point-min))
1793 (while (re-search-forward "^>" nil t)
1794 (setq tem (bookmark-bmenu-bookmark))
1795 (let ((buffer-read-only nil))
1796 (delete-char -1)
1797 (insert ?\ ))
1798 (or (string-equal tem bmrk)
1799 (member tem others)
1800 (setq others (cons tem others))))
1801 (setq others (nreverse others)
1802 tem (/ (1- (frame-height)) (1+ (length others))))
1803 (delete-other-windows)
1804 (bookmark-jump bmrk)
1805 (bury-buffer menu)
1806 (if others
1807 (while others
1808 (split-window nil tem)
1809 (other-window 1)
1810 (bookmark-jump (car others))
1811 (setq others (cdr others)))
1812 (other-window 1)))))
1813
1814
1815 (defun bookmark-bmenu-save (parg)
1816 "Save the current list into a bookmark file.
1817 With a prefix arg, prompts for a file to save them in."
1818 (interactive "P")
1819 (save-excursion
1820 (save-window-excursion
1821 (bookmark-save parg))))
1822
1823
1824 (defun bookmark-bmenu-load ()
1825 "Load the bookmark file and rebuild the bookmark menu-buffer."
1826 (interactive)
1827 (if (bookmark-bmenu-check-position)
1828 (save-excursion
1829 (save-window-excursion
1830 ;; This will call `bookmark-bmenu-list'
1831 (call-interactively 'bookmark-load)))))
1832
1833
1834 (defun bookmark-bmenu-1-window ()
1835 "Select this line's bookmark, alone, in full frame."
1836 (interactive)
1837 (if (bookmark-bmenu-check-position)
1838 (progn
1839 (bookmark-jump (bookmark-bmenu-bookmark))
1840 (bury-buffer (other-buffer))
1841 (delete-other-windows))))
1842
1843
1844 (defun bookmark-bmenu-2-window ()
1845 "Select this line's bookmark, with previous buffer in second window."
1846 (interactive)
1847 (if (bookmark-bmenu-check-position)
1848 (let ((bmrk (bookmark-bmenu-bookmark))
1849 (menu (current-buffer))
1850 (pop-up-windows t))
1851 (delete-other-windows)
1852 (switch-to-buffer (other-buffer))
1853 (let* ((pair (bookmark-jump-noselect bmrk))
1854 (buff (car pair))
1855 (pos (cdr pair)))
1856 (pop-to-buffer buff)
1857 (goto-char pos))
1858 (bury-buffer menu))))
1859
1860
1861 (defun bookmark-bmenu-this-window ()
1862 "Select this line's bookmark in this window."
1863 (interactive)
1864 (if (bookmark-bmenu-check-position)
1865 (bookmark-jump (bookmark-bmenu-bookmark))))
1866
1867
1868 (defun bookmark-bmenu-other-window ()
1869 "Select this line's bookmark in other window, leaving bookmark menu visible."
1870 (interactive)
1871 (let ((bookmark (bookmark-bmenu-bookmark)))
1872 (if (bookmark-bmenu-check-position)
1873 (let* ((pair (bookmark-jump-noselect bookmark))
1874 (buff (car pair))
1875 (pos (cdr pair)))
1876 (switch-to-buffer-other-window buff)
1877 (goto-char pos)
1878 (set-window-point (get-buffer-window buff) pos)
1879 (bookmark-show-annotation bookmark)))))
1880
1881
1882 (defun bookmark-bmenu-switch-other-window ()
1883 "Make the other window select this line's bookmark.
1884 The current window remains selected."
1885 (interactive)
1886 (let ((bookmark (bookmark-bmenu-bookmark))
1887 (pop-up-windows t)
1888 same-window-buffer-names
1889 same-window-regexps)
1890 (if (bookmark-bmenu-check-position)
1891 (let* ((pair (bookmark-jump-noselect bookmark))
1892 (buff (car pair))
1893 (pos (cdr pair)))
1894 (display-buffer buff)
1895 (let ((o-buffer (current-buffer)))
1896 ;; save-excursion won't do
1897 (set-buffer buff)
1898 (goto-char pos)
1899 (set-window-point (get-buffer-window buff) pos)
1900 (set-buffer o-buffer))
1901 (bookmark-show-annotation bookmark)))))
1902
1903 (defun bookmark-bmenu-other-window-with-mouse (event)
1904 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1905 (interactive "e")
1906 (save-excursion
1907 (set-buffer (window-buffer (posn-window (event-end event))))
1908 (save-excursion
1909 (goto-char (posn-point (event-end event)))
1910 (bookmark-bmenu-other-window))))
1911
1912
1913 (defun bookmark-bmenu-show-annotation ()
1914 "Show the annotation for the current bookmark in another window."
1915 (interactive)
1916 (let ((bookmark (bookmark-bmenu-bookmark)))
1917 (if (bookmark-bmenu-check-position)
1918 (bookmark-show-annotation bookmark))))
1919
1920
1921 (defun bookmark-bmenu-show-all-annotations ()
1922 "Show the annotation for all bookmarks in another window."
1923 (interactive)
1924 (bookmark-show-all-annotations))
1925
1926
1927 (defun bookmark-bmenu-edit-annotation ()
1928 "Edit the annotation for the current bookmark in another window."
1929 (interactive)
1930 (let ((bookmark (bookmark-bmenu-bookmark)))
1931 (if (bookmark-bmenu-check-position)
1932 (bookmark-edit-annotation bookmark))))
1933
1934
1935 (defun bookmark-bmenu-unmark (&optional backup)
1936 "Cancel all requested operations on bookmark on this line and move down.
1937 Optional BACKUP means move up."
1938 (interactive "P")
1939 (beginning-of-line)
1940 (if (bookmark-bmenu-check-position)
1941 (progn
1942 (let ((buffer-read-only nil))
1943 (delete-char 1)
1944 ;; any flags to reset according to circumstances? How about a
1945 ;; flag indicating whether this bookmark is being visited?
1946 ;; well, we don't have this now, so maybe later.
1947 (insert " "))
1948 (forward-line (if backup -1 1))
1949 (bookmark-bmenu-check-position))))
1950
1951
1952 (defun bookmark-bmenu-backup-unmark ()
1953 "Move up and cancel all requested operations on bookmark on line above."
1954 (interactive)
1955 (forward-line -1)
1956 (if (bookmark-bmenu-check-position)
1957 (progn
1958 (bookmark-bmenu-unmark)
1959 (forward-line -1)
1960 (bookmark-bmenu-check-position))))
1961
1962
1963 (defun bookmark-bmenu-delete ()
1964 "Mark bookmark on this line to be deleted.
1965 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1966 (interactive)
1967 (beginning-of-line)
1968 (if (bookmark-bmenu-check-position)
1969 (let ((buffer-read-only nil))
1970 (delete-char 1)
1971 (insert ?D)
1972 (forward-line 1)
1973 (bookmark-bmenu-check-position))))
1974
1975
1976 (defun bookmark-bmenu-delete-backwards ()
1977 "Mark bookmark on this line to be deleted, then move up one line.
1978 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1979 (interactive)
1980 (bookmark-bmenu-delete)
1981 (forward-line -2)
1982 (if (bookmark-bmenu-check-position)
1983 (forward-line 1))
1984 (bookmark-bmenu-check-position))
1985
1986
1987 (defun bookmark-bmenu-execute-deletions ()
1988 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1989 (interactive)
1990 (message "Deleting bookmarks...")
1991 (let ((hide-em bookmark-bmenu-toggle-filenames)
1992 (o-point (point))
1993 (o-str (save-excursion
1994 (beginning-of-line)
1995 (if (looking-at "^D")
1996 nil
1997 (buffer-substring
1998 (point)
1999 (progn (end-of-line) (point))))))
2000 (o-col (current-column)))
2001 (if hide-em (bookmark-bmenu-hide-filenames))
2002 (setq bookmark-bmenu-toggle-filenames nil)
2003 (goto-char (point-min))
2004 (forward-line 1)
2005 (while (re-search-forward "^D" (point-max) t)
2006 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2007 (bookmark-bmenu-list)
2008 (setq bookmark-bmenu-toggle-filenames hide-em)
2009 (if bookmark-bmenu-toggle-filenames
2010 (bookmark-bmenu-toggle-filenames t))
2011 (if o-str
2012 (progn
2013 (goto-char (point-min))
2014 (search-forward o-str)
2015 (beginning-of-line)
2016 (forward-char o-col))
2017 (goto-char o-point))
2018 (beginning-of-line)
2019 (setq bookmark-alist-modification-count
2020 (1+ bookmark-alist-modification-count))
2021 (if (bookmark-time-to-save-p)
2022 (bookmark-save))
2023 (message "Deleting bookmarks...done")
2024 ))
2025
2026
2027 (defun bookmark-bmenu-rename ()
2028 "Rename bookmark on current line. Prompts for a new name."
2029 (interactive)
2030 (if (bookmark-bmenu-check-position)
2031 (let ((bmrk (bookmark-bmenu-bookmark))
2032 (thispoint (point)))
2033 (bookmark-rename bmrk)
2034 (bookmark-bmenu-list)
2035 (goto-char thispoint))))
2036
2037
2038 (defun bookmark-bmenu-locate ()
2039 "Display location of this bookmark. Displays in the minibuffer."
2040 (interactive)
2041 (if (bookmark-bmenu-check-position)
2042 (let ((bmrk (bookmark-bmenu-bookmark)))
2043 (message (bookmark-location bmrk)))))
2044
2045
2046 \f
2047 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2048
2049 (defun bookmark-menu-build-paned-menu (name entries)
2050 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2051 That is, ENTRIES is a list of strings which appear as the choices
2052 in the menu. The number of panes depends on the number of entries.
2053 The visible entries are truncated to `bookmark-menu-length', but the
2054 strings returned are not."
2055 (let* ((f-height (/ (frame-height) 2))
2056 (pane-list
2057 (let (temp-pane-list
2058 (iter 0))
2059 (while entries
2060 (let (lst
2061 (count 0))
2062 (while (and (< count f-height) entries)
2063 (let ((str (car entries)))
2064 (setq lst (cons
2065 (cons
2066 (if (> (length str) bookmark-menu-length)
2067 (substring str 0 bookmark-menu-length)
2068 str)
2069 str)
2070 lst))
2071 (setq entries (cdr entries))
2072 (setq count (1+ count))))
2073 (setq iter (1+ iter))
2074 (setq
2075 temp-pane-list
2076 (cons
2077 (cons
2078 (format "-*- %s (%d) -*-" name iter)
2079 (nreverse lst))
2080 temp-pane-list))))
2081 (nreverse temp-pane-list))))
2082
2083 ;; Return the menu:
2084 (cons (concat "-*- " name " -*-") pane-list)))
2085
2086
2087 (defun bookmark-menu-popup-paned-menu (event name entries)
2088 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2089 That is, ENTRIES is a list of strings which appear as the choices
2090 in the menu.
2091 The number of panes depends on the number of entries."
2092 (interactive "e")
2093 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2094
2095
2096 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2097 "Pop up menu of bookmarks, return chosen bookmark.
2098 Pop up at EVENT, menu's name is NAME.
2099 The number of panes depends on the number of bookmarks."
2100 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2101
2102
2103 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2104 ;; help function for making menus that need to apply a bookmark
2105 ;; function to a string.
2106 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2107 event menu-label)))
2108 (if choice (apply func-sym (list choice)))))
2109
2110
2111 ;;;###autoload
2112 (defun bookmark-menu-insert (event)
2113 "Insert the text of the file pointed to by bookmark BOOKMARK.
2114 You may have a problem using this function if the value of variable
2115 `bookmark-alist' is nil. If that happens, you need to load in some
2116 bookmarks. See help on function `bookmark-load' for more about
2117 this.
2118
2119 Warning: this function only takes an EVENT as argument. Use the
2120 corresponding bookmark function from Lisp \(the one without the
2121 \"-menu-\" in its name\)."
2122 (interactive "e")
2123 (bookmark-popup-menu-and-apply-function
2124 'bookmark-insert "Insert Bookmark Contents" event))
2125
2126
2127 ;;;###autoload
2128 (defun bookmark-menu-jump (event)
2129 "Jump to bookmark BOOKMARK (a point in some file).
2130 You may have a problem using this function if the value of variable
2131 `bookmark-alist' is nil. If that happens, you need to load in some
2132 bookmarks. See help on function `bookmark-load' for more about
2133 this.
2134
2135 Warning: this function only takes an EVENT as argument. Use the
2136 corresponding bookmark function from Lisp \(the one without the
2137 \"-menu-\" in its name\)."
2138 (interactive "e")
2139 (bookmark-popup-menu-and-apply-function
2140 'bookmark-jump "Jump to Bookmark" event))
2141
2142
2143 ;;;###autoload
2144 (defun bookmark-menu-locate (event)
2145 "Insert the name of the file associated with BOOKMARK.
2146 \(This is not the same as the contents of that file\).
2147
2148 Warning: this function only takes an EVENT as argument. Use the
2149 corresponding bookmark function from Lisp \(the one without the
2150 \"-menu-\" in its name\)."
2151 (interactive "e")
2152 (bookmark-popup-menu-and-apply-function
2153 'bookmark-insert-location "Insert Bookmark Location" event))
2154
2155
2156 ;;;###autoload
2157 (defun bookmark-menu-rename (event)
2158 "Change the name of OLD-BOOKMARK to NEWNAME.
2159 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2160 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2161 prompts for NEWNAME.
2162 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2163 passed as an argument. If called with two strings, then no prompting
2164 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2165
2166 While you are entering the new name, consecutive C-w's insert
2167 consecutive words from the text of the buffer into the new bookmark
2168 name.
2169
2170 Warning: this function only takes an EVENT as argument. Use the
2171 corresponding bookmark function from Lisp \(the one without the
2172 \"-menu-\" in its name\)."
2173 (interactive "e")
2174 (bookmark-popup-menu-and-apply-function
2175 'bookmark-rename "Rename Bookmark" event))
2176
2177
2178 ;;;###autoload
2179 (defun bookmark-menu-delete (event)
2180 "Delete the bookmark named NAME from the bookmark list.
2181 Removes only the first instance of a bookmark with that name. If
2182 there are one or more other bookmarks with the same name, they will
2183 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2184 one most recently used in this file, if any\).
2185
2186 Warning: this function only takes an EVENT as argument. Use the
2187 corresponding bookmark function from Lisp \(the one without the
2188 \"-menu-\" in its name\)."
2189 (interactive "e")
2190 (bookmark-popup-menu-and-apply-function
2191 'bookmark-delete "Delete Bookmark" event))
2192
2193
2194 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2195 ;; following works, and for explaining what to do to make it work.
2196
2197 ;; We MUST autoload EACH form used to set up this variable's value, so
2198 ;; that the whole job is done in loaddefs.el.
2199
2200 ;; Emacs menubar stuff.
2201
2202 ;;;###autoload
2203 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2204
2205 ;;;###autoload
2206 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2207
2208 ;; make bookmarks appear toward the right side of the menu.
2209 (if (boundp 'menu-bar-final-items)
2210 (if menu-bar-final-items
2211 (setq menu-bar-final-items
2212 (cons 'bookmark menu-bar-final-items)))
2213 (setq menu-bar-final-items '(bookmark)))
2214
2215 ;;;###autoload
2216 (define-key menu-bar-bookmark-map [load]
2217 '("Load a Bookmark File..." . bookmark-load))
2218
2219 ;;;###autoload
2220 (define-key menu-bar-bookmark-map [write]
2221 '("Save Bookmarks As..." . bookmark-write))
2222
2223 ;;;###autoload
2224 (define-key menu-bar-bookmark-map [save]
2225 '("Save Bookmarks" . bookmark-save))
2226
2227 ;;;###autoload
2228 (define-key menu-bar-bookmark-map [edit]
2229 '("Edit Bookmark List" . bookmark-bmenu-list))
2230
2231 ;;;###autoload
2232 (define-key menu-bar-bookmark-map [delete]
2233 '("Delete Bookmark" . bookmark-menu-delete))
2234
2235 ;;;###autoload
2236 (define-key menu-bar-bookmark-map [rename]
2237 '("Rename Bookmark" . bookmark-menu-rename))
2238
2239 ;;;###autoload
2240 (define-key menu-bar-bookmark-map [locate]
2241 '("Insert Location" . bookmark-menu-locate))
2242
2243 ;;;###autoload
2244 (define-key menu-bar-bookmark-map [insert]
2245 '("Insert Contents" . bookmark-menu-insert))
2246
2247 ;;;###autoload
2248 (define-key menu-bar-bookmark-map [set]
2249 '("Set Bookmark" . bookmark-set))
2250
2251 ;;;###autoload
2252 (define-key menu-bar-bookmark-map [jump]
2253 '("Jump to Bookmark" . bookmark-menu-jump))
2254
2255 ;;;; end bookmark menu stuff ;;;;
2256
2257 \f
2258 ;;; Load Hook
2259 (defvar bookmark-load-hook nil
2260 "Hook to run at the end of loading bookmark.")
2261
2262 ;;; Exit Hook, called from kill-emacs-hook
2263 (defvar bookmark-exit-hook nil
2264 "Hook to run when emacs exits")
2265
2266 (defun bookmark-exit-hook-internal ()
2267 "Save bookmark state, if necessary, at Emacs exit time.
2268 This also runs `bookmark-exit-hooks'."
2269 (and
2270 (progn (run-hooks 'bookmark-exit-hooks) t)
2271 bookmark-alist
2272 (bookmark-time-to-save-p t)
2273 (bookmark-save)))
2274
2275 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2276
2277
2278 (run-hooks 'bookmark-load-hook)
2279
2280 (provide 'bookmark)
2281
2282 ;;; bookmark.el ends here