* bookmark.el: Don't define bookmark keys under the "C-xr" map;
[emacs.git] / lisp / bookmark.el
blob156e8affaa5f403cfbe14ab03fb394aee41a5b86
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders, annotations
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This package is for setting "bookmarks" in files. A bookmark
31 ;; associates a string with a location in a certain file. Thus, you
32 ;; can navigate your way to that location by providing the string.
33 ;; See the "User Variables" section for customizations.
35 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
36 ;; then implementing the bookmark-current-bookmark idea. He even
37 ;; sent *patches*, bless his soul...
39 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
40 ;; fixing and improving bookmark-time-to-save-p.
42 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
43 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
44 ;; and the menu-bar).
46 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
47 ;; suggestions and the code to implement them (like
48 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
49 ;; stuff).
51 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
52 ;; for his eminently sensible suggestion to separate bookmark-jump
53 ;; into bookmark-jump and bookmark-jump-noselect, which made many
54 ;; other things cleaner as well.
56 ;; Thanks to Roland McGrath for encouragement and help with defining
57 ;; autoloads on the menu-bar.
59 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
60 ;; values in bookmark-jump and bookmark-set. Everybody please keep
61 ;; all the keystrokes they save thereby and send them to him at the
62 ;; end of each year :-) (No, seriously, thanks Jonathan!)
64 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
65 ;; thinking up the annotations feature and implementing it so well.
67 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
68 ;; <olstad@msc.edu>.
70 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
71 ;; reported and fixed.
73 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
75 ;; Enough with the credits already, get on to the good stuff:
77 ;; FAVORITE CHINESE RESTAURANT:
78 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
79 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
81 ;;; Code:
83 (require 'pp)
85 ;;; Misc comments:
87 ;; If variable bookmark-use-annotations is non-nil, an annotation is
88 ;; queried for when setting a bookmark.
90 ;; The bookmark list is sorted lexically by default, but you can turn
91 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
92 ;; the list will be presented in the order it is recorded
93 ;; (chronologically), which is actually fairly useful as well.
95 ;;; User Variables
97 (defgroup bookmark nil
98 "Setting, annotation and jumping to bookmarks."
99 :group 'matching)
102 (defcustom bookmark-use-annotations nil
103 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
104 :type 'boolean
105 :group 'bookmark)
108 (defcustom bookmark-save-flag t
109 "*Controls when Emacs saves bookmarks to a file.
110 --> nil means never save bookmarks, except when `bookmark-save' is
111 explicitly called \(\\[bookmark-save]\).
112 --> t means save bookmarks when Emacs is killed.
113 --> Otherwise, it should be a number that is the frequency with which
114 the bookmark list is saved \(i.e.: the number of times which
115 Emacs' bookmark list may be modified before it is automatically
116 saved.\). If it is a number, Emacs will also automatically save
117 bookmarks when it is killed.
119 Therefore, the way to get it to save every time you make or delete a
120 bookmark is to set this variable to 1 \(or 0, which produces the same
121 behavior.\)
123 To specify the file in which to save them, modify the variable
124 `bookmark-default-file', which is `~/.emacs.bmk' by default."
125 :type '(choice (const nil) integer (other t))
126 :group 'bookmark)
129 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
130 "*The `.emacs.bmk' file used to be called this name.")
133 ;; defvarred to avoid a compilation warning:
134 (defvar bookmark-file nil
135 "Old name for `bookmark-default-file'.")
137 (defcustom bookmark-default-file
138 (if bookmark-file
139 ;; In case user set `bookmark-file' in her .emacs:
140 bookmark-file
141 (convert-standard-filename "~/.emacs.bmk"))
142 "*File in which to save bookmarks by default."
143 :type 'file
144 :group 'bookmark)
147 (defcustom bookmark-version-control 'nospecial
148 "*Whether or not to make numbered backups of the bookmark file.
149 It can have four values: t, nil, `never', and `nospecial'.
150 The first three have the same meaning that they do for the
151 variable `version-control', and the final value `nospecial' means just
152 use the value of `version-control'."
153 :type '(choice (const nil) (const never) (const nospecial)
154 (other t))
155 :group 'bookmark)
158 (defcustom bookmark-completion-ignore-case t
159 "*Non-nil means bookmark functions ignore case in completion."
160 :type 'boolean
161 :group 'bookmark)
164 (defcustom bookmark-sort-flag t
165 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
166 Otherwise they will be displayed in LIFO order (that is, most
167 recently set ones come first, oldest ones come last)."
168 :type 'boolean
169 :group 'bookmark)
172 (defcustom bookmark-automatically-show-annotations t
173 "*Non-nil means show annotations when jumping to a bookmark."
174 :type 'boolean
175 :group 'bookmark)
178 (defcustom bookmark-bmenu-file-column 30
179 "*Column at which to display filenames in a buffer listing bookmarks.
180 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
181 :type 'integer
182 :group 'bookmark)
185 (defcustom bookmark-bmenu-toggle-filenames t
186 "*Non-nil means show filenames when listing bookmarks.
187 This may result in truncated bookmark names. To disable this, put the
188 following in your `.emacs' file:
190 \(setq bookmark-bmenu-toggle-filenames nil\)"
191 :type 'boolean
192 :group 'bookmark)
195 (defcustom bookmark-menu-length 70
196 "*Maximum length of a bookmark name displayed on a popup menu."
197 :type 'integer
198 :group 'bookmark)
201 (defface bookmark-menu-heading
202 '((t (:inherit font-lock-type-face)))
203 "Face used to highlight the heading in bookmark menu buffers."
204 :group 'bookmark
205 :version "22.1")
208 ;;; No user-serviceable parts beyond this point.
210 ;; Is it XEmacs?
211 (defconst bookmark-xemacsp
212 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
215 ;; Added for lucid emacs compatibility, db
216 (or (fboundp 'defalias) (fset 'defalias 'fset))
218 ;; suggested for lucid compatibility by david hughes:
219 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
222 ;;; Keymap stuff:
224 ;; Set up these bindings dumping time *only*;
225 ;; if the user alters them, don't override the user when loading bookmark.el.
227 ;;;###autoload
228 (defvar bookmark-map nil
229 "Keymap containing bindings to bookmark functions.
230 It is not bound to any key by default: to bind it
231 so that you have a bookmark prefix, just use `global-set-key' and bind a
232 key of your choice to `bookmark-map'. All interactive bookmark
233 functions have a binding in this keymap.")
235 ;;;###autoload (define-prefix-command 'bookmark-map)
237 ;;;###autoload (define-key ctl-x-map "p" bookmark-map)
239 ;; Read the help on all of these functions for details...
240 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
241 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
242 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
243 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
244 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
245 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
246 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
247 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
248 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
249 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
250 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
251 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
254 ;;; The annotation maps.
255 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
256 "Keymap for composing an annotation for a bookmark.")
258 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
259 'bookmark-send-annotation)
263 ;;; Core variables and data structures:
264 (defvar bookmark-alist ()
265 "Association list of bookmarks and their records.
266 You probably don't want to change the value of this alist yourself;
267 instead, let the various bookmark functions do it for you.
269 The format of the alist is
271 \(BOOKMARK1 BOOKMARK2 ...\)
273 where each BOOKMARK is of the form
275 \(NAME
276 \(filename . FILE\)
277 \(front-context-string . FRONT-STR\)
278 \(rear-context-string . REAR-STR\)
279 \(position . POS\)
280 \(info-node . POS\)
281 \(annotation . ANNOTATION\)\)
283 So the cdr of each bookmark is an alist too.
284 `info-node' is optional, by the way.")
287 (defvar bookmarks-already-loaded nil)
290 ;; more stuff added by db.
292 (defvar bookmark-current-bookmark nil
293 "Name of bookmark most recently used in the current file.
294 It is buffer local, used to make moving a bookmark forward
295 through a file easier.")
297 (make-variable-buffer-local 'bookmark-current-bookmark)
300 (defvar bookmark-alist-modification-count 0
301 "Number of modifications to bookmark list since it was last saved.")
304 (defvar bookmark-search-size 16
305 "Length of the context strings recorded on either side of a bookmark.")
308 (defvar bookmark-current-point 0)
309 (defvar bookmark-yank-point 0)
310 (defvar bookmark-current-buffer nil)
312 (defvar Info-current-node)
313 (defvar Info-suffix-list)
315 ;; Helper functions.
317 ;; Only functions on this page and the next one (file formats) need to
318 ;; know anything about the format of bookmark-alist entries.
319 ;; Everyone else should go through them.
321 (defun bookmark-name-from-full-record (full-record)
322 "Return name of FULL-RECORD \(an alist element instead of a string\)."
323 (car full-record))
326 (defun bookmark-all-names ()
327 "Return a list of all current bookmark names."
328 (bookmark-maybe-load-default-file)
329 (mapcar
330 (lambda (full-record)
331 (bookmark-name-from-full-record full-record))
332 bookmark-alist))
335 (defun bookmark-get-bookmark (bookmark)
336 "Return the full entry for BOOKMARK in `bookmark-alist'.
337 If BOOKMARK is not a string, return nil."
338 (when (stringp bookmark)
339 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
342 (defun bookmark-get-bookmark-record (bookmark)
343 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
344 That is, all information but the name."
345 (car (cdr (bookmark-get-bookmark bookmark))))
348 (defun bookmark-set-name (bookmark newname)
349 "Set BOOKMARK's name to NEWNAME."
350 (setcar
351 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
352 newname))
355 (defun bookmark-get-annotation (bookmark)
356 "Return the annotation of BOOKMARK, or nil if none."
357 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
360 (defun bookmark-set-annotation (bookmark ann)
361 "Set the annotation of BOOKMARK to ANN."
362 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
363 (if cell
364 (setcdr cell ann)
365 (nconc (bookmark-get-bookmark-record bookmark)
366 (list (cons 'annotation ann))))))
369 (defun bookmark-get-filename (bookmark)
370 "Return the full filename of BOOKMARK."
371 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
374 (defun bookmark-set-filename (bookmark filename)
375 "Set the full filename of BOOKMARK to FILENAME."
376 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
377 (if cell
378 (setcdr cell filename)
379 (nconc (bookmark-get-bookmark-record bookmark)
380 (list (cons 'filename filename))))
381 (setq bookmark-alist-modification-count
382 (1+ bookmark-alist-modification-count))
383 (if (bookmark-time-to-save-p)
384 (bookmark-save))))
387 (defun bookmark-get-position (bookmark)
388 "Return the position \(i.e.: point\) of BOOKMARK."
389 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
392 (defun bookmark-set-position (bookmark position)
393 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
394 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
395 (if cell
396 (setcdr cell position)
397 (nconc (bookmark-get-bookmark-record bookmark)
398 (list (cons 'position position))))))
401 (defun bookmark-get-front-context-string (bookmark)
402 "Return the front-context-string of BOOKMARK."
403 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
406 (defun bookmark-set-front-context-string (bookmark string)
407 "Set the front-context-string of BOOKMARK to STRING."
408 (let ((cell (assq 'front-context-string
409 (bookmark-get-bookmark-record bookmark))))
410 (if cell
411 (setcdr cell string)
412 (nconc (bookmark-get-bookmark-record bookmark)
413 (list (cons 'front-context-string string))))))
416 (defun bookmark-get-rear-context-string (bookmark)
417 "Return the rear-context-string of BOOKMARK."
418 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
421 (defun bookmark-set-rear-context-string (bookmark string)
422 "Set the rear-context-string of BOOKMARK to STRING."
423 (let ((cell (assq 'rear-context-string
424 (bookmark-get-bookmark-record bookmark))))
425 (if cell
426 (setcdr cell string)
427 (nconc (bookmark-get-bookmark-record bookmark)
428 (list (cons 'rear-context-string string))))))
431 (defun bookmark-get-info-node (bookmark)
432 "Get the info node associated with BOOKMARK."
433 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
436 (defun bookmark-set-info-node (bookmark node)
437 "Set the Info node of BOOKMARK to NODE."
438 (let ((cell (assq 'info-node
439 (bookmark-get-bookmark-record bookmark))))
440 (if cell
441 (setcdr cell node)
442 (nconc (bookmark-get-bookmark-record bookmark)
443 (list (cons 'info-node node)))))
445 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
446 (sit-for 4))
449 (defvar bookmark-history nil
450 "The history list for bookmark functions.")
453 (defun bookmark-completing-read (prompt &optional default)
454 "Prompting with PROMPT, read a bookmark name in completion.
455 PROMPT will get a \": \" stuck on the end no matter what, so you
456 probably don't want to include one yourself.
457 Optional second arg DEFAULT is a string to return if the user enters
458 the empty string."
459 (bookmark-maybe-load-default-file) ; paranoia
460 (if (listp last-nonmenu-event)
461 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
462 (let* ((completion-ignore-case bookmark-completion-ignore-case)
463 (default default)
464 (prompt (if default
465 (concat prompt (format " (%s): " default))
466 (concat prompt ": ")))
467 (str
468 (completing-read prompt
469 bookmark-alist
473 'bookmark-history)))
474 (if (string-equal "" str) default str))))
477 (defmacro bookmark-maybe-historicize-string (string)
478 "Put STRING into the bookmark prompt history, if caller non-interactive.
479 We need this because sometimes bookmark functions are invoked from
480 menus, so `completing-read' never gets a chance to set `bookmark-history'."
481 `(or
482 (interactive-p)
483 (setq bookmark-history (cons ,string bookmark-history))))
486 (defun bookmark-make (name &optional annotation overwrite info-node)
487 "Make a bookmark named NAME.
488 Optional second arg ANNOTATION gives it an annotation.
489 Optional third arg OVERWRITE means replace any existing bookmarks with
490 this name.
491 Optional fourth arg INFO-NODE means this bookmark is at info node
492 INFO-NODE, so record this fact in the bookmark's entry."
493 (bookmark-maybe-load-default-file)
494 (let ((stripped-name (copy-sequence name)))
495 (or bookmark-xemacsp
496 ;; XEmacs's `set-text-properties' doesn't work on
497 ;; free-standing strings, apparently.
498 (set-text-properties 0 (length stripped-name) nil stripped-name))
499 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
500 ;; already existing bookmark under that name and
501 ;; no prefix arg means just overwrite old bookmark
502 (setcdr (bookmark-get-bookmark stripped-name)
503 (list (bookmark-make-cell annotation info-node)))
505 ;; otherwise just cons it onto the front (either the bookmark
506 ;; doesn't exist already, or there is no prefix arg. In either
507 ;; case, we want the new bookmark consed onto the alist...)
509 (setq bookmark-alist
510 (cons
511 (list stripped-name
512 (bookmark-make-cell annotation info-node))
513 bookmark-alist)))
515 ;; Added by db
516 (setq bookmark-current-bookmark stripped-name)
517 (setq bookmark-alist-modification-count
518 (1+ bookmark-alist-modification-count))
519 (if (bookmark-time-to-save-p)
520 (bookmark-save))))
523 (defun bookmark-make-cell (annotation &optional info-node)
524 "Return the record part of a new bookmark, given ANNOTATION.
525 Must be at the correct position in the buffer in which the bookmark is
526 being set. This might change someday.
527 Optional second arg INFO-NODE means this bookmark is at info node
528 INFO-NODE, so record this fact in the bookmark's entry."
529 (let ((the-record
530 `((filename . ,(bookmark-buffer-file-name))
531 (front-context-string
532 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
533 (buffer-substring-no-properties
534 (point)
535 (+ (point) bookmark-search-size))
536 nil))
537 (rear-context-string
538 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
539 (buffer-substring-no-properties
540 (point)
541 (- (point) bookmark-search-size))
542 nil))
543 (position . ,(point)))))
545 ;; Now fill in the optional parts:
547 ;; Take no chances with text properties
548 (set-text-properties 0 (length annotation) nil annotation)
549 (set-text-properties 0 (length info-node) nil info-node)
551 (if annotation
552 (nconc the-record (list (cons 'annotation annotation))))
553 (if info-node
554 (nconc the-record (list (cons 'info-node info-node))))
556 ;; Finally, return the completed record.
557 the-record))
561 ;;; File format stuff
563 ;; The OLD format of the bookmark-alist was:
565 ;; ((bookmark-name (filename
566 ;; string-in-front
567 ;; string-behind
568 ;; point))
569 ;; ...)
571 ;; The NEW format of the bookmark-alist is:
573 ;; ((bookmark-name ((filename . FILENAME)
574 ;; (front-context-string . string-in-front)
575 ;; (rear-context-string . string-behind)
576 ;; (position . POINT)
577 ;; (annotation . annotation)
578 ;; (whatever . VALUE)
579 ;; ...
580 ;; ))
581 ;; ...)
584 ;; I switched to using an internal as well as external alist because I
585 ;; felt that would be a more flexible framework in which to add
586 ;; features. It means that the order in which values appear doesn't
587 ;; matter, and it means that arbitrary values can be added without
588 ;; risk of interfering with existing ones.
590 ;; BOOKMARK-NAME is the string the user gives the bookmark and
591 ;; accesses it by from then on.
593 ;; FILENAME is the location of the file in which the bookmark is set.
595 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
596 ;; context in front of the point at which the bookmark is set.
598 ;; STRING-BEHIND is the same thing, but after the point.
600 ;; The context strings exist so that modifications to a file don't
601 ;; necessarily cause a bookmark's position to be invalidated.
602 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
603 ;; case the file has changed since the bookmark was set. It will
604 ;; attempt to place the user before the changes, if there were any.
605 ;; ANNOTATION is the annotation for the bookmark; it may not exist
606 ;; (for backward compatibility), be nil (no annotation), or be a
607 ;; string.
610 (defconst bookmark-file-format-version 1
611 "The current version of the format used by bookmark files.
612 You should never need to change this.")
615 (defconst bookmark-end-of-version-stamp-marker
616 "-*- End Of Bookmark File Format Version Stamp -*-\n"
617 "This string marks the end of the version stamp in a bookmark file.")
620 (defun bookmark-alist-from-buffer ()
621 "Return a `bookmark-alist' (in any format) from the current buffer.
622 The buffer must of course contain bookmark format information.
623 Does not care from where in the buffer it is called, and does not
624 affect point."
625 (save-excursion
626 (goto-char (point-min))
627 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
628 (read (current-buffer))
629 ;; Else we're dealing with format version 0
630 (if (search-forward "(" nil t)
631 (progn
632 (forward-char -1)
633 (read (current-buffer)))
634 ;; Else no hope of getting information here.
635 (error "Not bookmark format")))))
638 (defun bookmark-upgrade-version-0-alist (old-list)
639 "Upgrade a version 0 alist OLD-LIST to the current version."
640 (mapcar
641 (lambda (bookmark)
642 (let* ((name (car bookmark))
643 (record (car (cdr bookmark)))
644 (filename (nth 0 record))
645 (front-str (nth 1 record))
646 (rear-str (nth 2 record))
647 (position (nth 3 record))
648 (ann (nth 4 record)))
649 (list
650 name
651 `((filename . ,filename)
652 (front-context-string . ,(or front-str ""))
653 (rear-context-string . ,(or rear-str ""))
654 (position . ,position)
655 (annotation . ,ann)))))
656 old-list))
659 (defun bookmark-upgrade-file-format-from-0 ()
660 "Upgrade a bookmark file of format 0 (the original format) to format 1.
661 This expects to be called from `point-min' in a bookmark file."
662 (message "Upgrading bookmark format from 0 to %d..."
663 bookmark-file-format-version)
664 (let* ((old-list (bookmark-alist-from-buffer))
665 (new-list (bookmark-upgrade-version-0-alist old-list)))
666 (delete-region (point-min) (point-max))
667 (bookmark-insert-file-format-version-stamp)
668 (pp new-list (current-buffer))
669 (save-buffer))
670 (goto-char (point-min))
671 (message "Upgrading bookmark format from 0 to %d...done"
672 bookmark-file-format-version)
676 (defun bookmark-grok-file-format-version ()
677 "Return an integer which is the file-format version of this bookmark file.
678 This expects to be called from `point-min' in a bookmark file."
679 (if (looking-at "^;;;;")
680 (save-excursion
681 (save-match-data
682 (re-search-forward "[0-9]")
683 (forward-char -1)
684 (read (current-buffer))))
685 ;; Else this is format version 0, the original one, which didn't
686 ;; even have version stamps.
690 (defun bookmark-maybe-upgrade-file-format ()
691 "Check the file-format version of this bookmark file.
692 If the version is not up-to-date, upgrade it automatically.
693 This expects to be called from `point-min' in a bookmark file."
694 (let ((version (bookmark-grok-file-format-version)))
695 (cond
696 ((= version bookmark-file-format-version)
697 ) ; home free -- version is current
698 ((= version 0)
699 (bookmark-upgrade-file-format-from-0))
701 (error "Bookmark file format version strangeness")))))
704 (defun bookmark-insert-file-format-version-stamp ()
705 "Insert text indicating current version of bookmark file format."
706 (insert
707 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
708 bookmark-file-format-version))
709 (insert ";;; This format is meant to be slightly human-readable;\n"
710 ";;; nevertheless, you probably don't want to edit it.\n"
711 ";;; "
712 bookmark-end-of-version-stamp-marker))
715 ;;; end file-format stuff
718 ;;; Generic helpers.
720 (defun bookmark-maybe-message (fmt &rest args)
721 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
722 (if (>= baud-rate 9600)
723 (apply 'message fmt args)))
726 ;;; Core code:
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.
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.
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.
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))
753 (bookmark-buffer-file-name)
754 (error "Buffer not visiting a file or directory"))
756 (bookmark-maybe-load-default-file)
758 (setq bookmark-current-point (point))
759 (setq bookmark-yank-point (point))
760 (setq bookmark-current-buffer (current-buffer))
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)
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))))
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))
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))))
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)
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)))
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"))
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 one argument, the name of the bookmark, as a string.")
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.
860 \\{bookmark-read-annotation-mode-map}
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-mode-hooks 'text-mode-hook))
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)))
889 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
890 "Keymap for editing an annotation of a bookmark.")
893 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
894 'bookmark-send-edited-annotation)
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].
901 \\{bookmark-edit-annotation-mode-map}
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 mode-name "Edit Bookmark Annotation")
910 (insert (funcall bookmark-read-annotation-text-func bookmark))
911 (let ((annotation (bookmark-get-annotation bookmark)))
912 (if (and annotation (not (string-equal annotation "")))
913 (insert annotation)))
914 (run-mode-hooks 'text-mode-hook))
917 (defun bookmark-send-edited-annotation ()
918 "Use buffer contents as annotation for a bookmark.
919 Lines beginning with `#' are ignored."
920 (interactive)
921 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
922 (error "Not in bookmark-edit-annotation-mode"))
923 (goto-char (point-min))
924 (while (< (point) (point-max))
925 (if (looking-at "^#")
926 (bookmark-kill-line t)
927 (forward-line 1)))
928 (let ((annotation (buffer-string))
929 (bookmark bookmark-annotation-name))
930 (bookmark-set-annotation bookmark annotation)
931 (bookmark-bmenu-surreptitiously-rebuild-list)
932 (goto-char bookmark-current-point))
933 (kill-buffer (current-buffer)))
936 (defun bookmark-edit-annotation (bookmark)
937 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
938 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
939 (bookmark-edit-annotation-mode bookmark))
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))))
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)))
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.
985 (buffer-name (current-buffer)))))
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)))
1003 (defvar Info-current-file)
1005 (defun bookmark-buffer-file-name ()
1006 "Return the current buffer's file in a way useful for bookmarks.
1007 For example, if this is a Info buffer, return the Info file's name."
1008 (cond
1009 ((eq major-mode 'Info-mode)
1010 Info-current-file)
1011 (buffer-file-name
1012 ;; Abbreviate the path, both so it's shorter and so it's more
1013 ;; portable. E.g., the user's home dir might be a different
1014 ;; path on different machines, but "~/" will still reach it.
1015 (abbreviate-file-name buffer-file-name))
1016 ((and (boundp 'dired-directory) dired-directory)
1017 (if (stringp dired-directory)
1018 dired-directory
1019 (car dired-directory)))))
1022 (defun bookmark-maybe-load-default-file ()
1023 (and (not bookmarks-already-loaded)
1024 (null bookmark-alist)
1025 (prog2
1026 (and
1027 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1028 ;; to be renamed.
1029 (file-exists-p (expand-file-name bookmark-old-default-file))
1030 (not (file-exists-p (expand-file-name bookmark-default-file)))
1031 (rename-file (expand-file-name bookmark-old-default-file)
1032 (expand-file-name bookmark-default-file)))
1033 ;; return t so the `and' will continue...
1036 (file-readable-p (expand-file-name bookmark-default-file))
1037 (bookmark-load bookmark-default-file t t)
1038 (setq bookmarks-already-loaded t)))
1041 (defun bookmark-maybe-sort-alist ()
1042 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1043 ;;is non-nil, then return a sorted copy of the alist.
1044 (if bookmark-sort-flag
1045 (setq bookmark-alist
1046 (sort (copy-alist bookmark-alist)
1047 (function
1048 (lambda (x y) (string-lessp (car x) (car y))))))))
1051 (defvar bookmark-after-jump-hook nil
1052 "Hook run after `bookmark-jump' jumps to a bookmark.
1053 Useful for example to unhide text in `outline-mode'.")
1055 ;;;###autoload
1056 (defun bookmark-jump (bookmark)
1057 "Jump to bookmark BOOKMARK (a point in some file).
1058 You may have a problem using this function if the value of variable
1059 `bookmark-alist' is nil. If that happens, you need to load in some
1060 bookmarks. See help on function `bookmark-load' for more about
1061 this.
1063 If the file pointed to by BOOKMARK no longer exists, you will be asked
1064 if you wish to give the bookmark a new location, and `bookmark-jump'
1065 will then jump to the new location, as well as recording it in place
1066 of the old one in the permanent bookmark record."
1067 (interactive
1068 (list (bookmark-completing-read "Jump to bookmark"
1069 bookmark-current-bookmark)))
1070 (unless bookmark
1071 (error "No bookmark specified"))
1072 (bookmark-maybe-historicize-string bookmark)
1073 (let ((cell (bookmark-jump-noselect bookmark)))
1074 (and cell
1075 (switch-to-buffer (car cell))
1076 (goto-char (cdr cell))
1077 (progn (run-hooks 'bookmark-after-jump-hook) t)
1078 (if bookmark-automatically-show-annotations
1079 ;; if there is an annotation for this bookmark,
1080 ;; show it in a buffer.
1081 (bookmark-show-annotation bookmark)))))
1084 (defun bookmark-file-or-variation-thereof (file)
1085 "Return FILE (a string) if it exists, or return a reasonable
1086 variation of FILE if that exists. Reasonable variations are checked
1087 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1088 nor a reasonable variation thereof, then still return FILE if it can
1089 be retrieved from a VC backend, else return nil."
1090 (if (file-exists-p file)
1091 file
1093 (progn (require 'info) ; ensure Info-suffix-list is bound
1094 (catch 'found
1095 (mapc (lambda (elt)
1096 (let ((suffixed-file (concat file (car elt))))
1097 (if (file-exists-p suffixed-file)
1098 (throw 'found suffixed-file))))
1099 Info-suffix-list)
1100 nil))
1101 ;; Last possibility: try VC
1102 (if (vc-backend file) file))))
1105 (defun bookmark-jump-noselect (str)
1106 ;; a leetle helper for bookmark-jump :-)
1107 ;; returns (BUFFER . POINT)
1108 (bookmark-maybe-load-default-file)
1109 (let* ((file (expand-file-name (bookmark-get-filename str)))
1110 (forward-str (bookmark-get-front-context-string str))
1111 (behind-str (bookmark-get-rear-context-string str))
1112 (place (bookmark-get-position str))
1113 (info-node (bookmark-get-info-node str))
1114 (orig-file file)
1116 (if (setq file (bookmark-file-or-variation-thereof file))
1117 (save-excursion
1118 (save-window-excursion
1119 (if info-node
1120 ;; Info nodes must be visited with care.
1121 (progn
1122 (require 'info)
1123 (with-no-warnings
1124 (Info-find-node file info-node)))
1125 ;; Else no Info. Can do an ordinary find-file:
1126 (set-buffer (find-file-noselect file))
1127 (goto-char place))
1129 ;; Go searching forward first. Then, if forward-str exists and
1130 ;; was found in the file, we can search backward for behind-str.
1131 ;; Rationale is that if text was inserted between the two in the
1132 ;; file, it's better to be put before it so you can read it,
1133 ;; rather than after and remain perhaps unaware of the changes.
1134 (if forward-str
1135 (if (search-forward forward-str (point-max) t)
1136 (goto-char (match-beginning 0))))
1137 (if behind-str
1138 (if (search-backward behind-str (point-min) t)
1139 (goto-char (match-end 0))))
1140 ;; added by db
1141 (setq bookmark-current-bookmark str)
1142 (cons (current-buffer) (point))))
1144 ;; Else unable to find the marked file, so ask if user wants to
1145 ;; relocate the bookmark, else remind them to consider deletion.
1146 (ding)
1147 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1148 " nonexistent. Relocate \""
1150 "\"? "))
1151 (progn
1152 (bookmark-relocate str)
1153 ;; gasp! It's a recursive function call in Emacs Lisp!
1154 (bookmark-jump-noselect str))
1155 (message
1156 "Bookmark not relocated; consider removing it \(%s\)." str)
1157 nil))))
1160 ;;;###autoload
1161 (defun bookmark-relocate (bookmark)
1162 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1163 This makes an already existing bookmark point to that file, instead of
1164 the one it used to point at. Useful when a file has been renamed
1165 after a bookmark was set in it."
1166 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1167 (bookmark-maybe-historicize-string bookmark)
1168 (bookmark-maybe-load-default-file)
1169 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1170 (newloc (expand-file-name
1171 (read-file-name
1172 (format "Relocate %s to: " bookmark)
1173 (file-name-directory bmrk-filename)))))
1174 (bookmark-set-filename bookmark newloc)
1175 (bookmark-bmenu-surreptitiously-rebuild-list)))
1178 ;;;###autoload
1179 (defun bookmark-insert-location (bookmark &optional no-history)
1180 "Insert the name of the file associated with BOOKMARK.
1181 Optional second arg NO-HISTORY means don't record this in the
1182 minibuffer history list `bookmark-history'."
1183 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1184 (or no-history (bookmark-maybe-historicize-string bookmark))
1185 (let ((start (point)))
1186 (prog1
1187 (insert (bookmark-location bookmark)) ; *Return this line*
1188 (if (and (display-color-p) (display-mouse-p))
1189 (add-text-properties
1190 start
1191 (save-excursion (re-search-backward
1192 "[^ \t]")
1193 (1+ (point)))
1194 '(mouse-face highlight
1195 follow-link t
1196 help-echo "mouse-2: go to this bookmark in other window"))))))
1198 ;;;###autoload
1199 (defalias 'bookmark-locate 'bookmark-insert-location)
1201 (defun bookmark-location (bookmark)
1202 "Return the name of the file associated with BOOKMARK."
1203 (bookmark-maybe-load-default-file)
1204 (bookmark-get-filename bookmark))
1207 ;;;###autoload
1208 (defun bookmark-rename (old &optional new)
1209 "Change the name of OLD bookmark to NEW name.
1210 If called from keyboard, prompt for OLD and NEW. If called from
1211 menubar, select OLD from a menu and prompt for NEW.
1213 If called from Lisp, prompt for NEW if only OLD was passed as an
1214 argument. If called with two strings, then no prompting is done. You
1215 must pass at least OLD when calling from Lisp.
1217 While you are entering the new name, consecutive C-w's insert
1218 consecutive words from the text of the buffer into the new bookmark
1219 name."
1220 (interactive (list (bookmark-completing-read "Old bookmark name")))
1221 (bookmark-maybe-historicize-string old)
1222 (bookmark-maybe-load-default-file)
1224 (setq bookmark-current-point (point))
1225 (setq bookmark-yank-point (point))
1226 (setq bookmark-current-buffer (current-buffer))
1227 (let ((newname
1228 (or new ; use second arg, if non-nil
1229 (read-from-minibuffer
1230 "New name: "
1232 (let ((now-map (copy-keymap minibuffer-local-map)))
1233 (define-key now-map "\C-w" 'bookmark-yank-word)
1234 now-map)
1236 'bookmark-history))))
1237 (bookmark-set-name old newname)
1238 (setq bookmark-current-bookmark newname)
1239 (bookmark-bmenu-surreptitiously-rebuild-list)
1240 (setq bookmark-alist-modification-count
1241 (1+ bookmark-alist-modification-count))
1242 (if (bookmark-time-to-save-p)
1243 (bookmark-save))))
1246 ;;;###autoload
1247 (defun bookmark-insert (bookmark)
1248 "Insert the text of the file pointed to by bookmark BOOKMARK.
1249 You may have a problem using this function if the value of variable
1250 `bookmark-alist' is nil. If that happens, you need to load in some
1251 bookmarks. See help on function `bookmark-load' for more about
1252 this."
1253 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1254 (bookmark-maybe-historicize-string bookmark)
1255 (bookmark-maybe-load-default-file)
1256 (let ((orig-point (point))
1257 (str-to-insert
1258 (save-excursion
1259 (set-buffer (car (bookmark-jump-noselect bookmark)))
1260 (buffer-string))))
1261 (insert str-to-insert)
1262 (push-mark)
1263 (goto-char orig-point)))
1266 ;;;###autoload
1267 (defun bookmark-delete (bookmark &optional batch)
1268 "Delete BOOKMARK from the bookmark list.
1269 Removes only the first instance of a bookmark with that name. If
1270 there are one or more other bookmarks with the same name, they will
1271 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1272 one most recently used in this file, if any\).
1273 Optional second arg BATCH means don't update the bookmark list buffer,
1274 probably because we were called from there."
1275 (interactive
1276 (list (bookmark-completing-read "Delete bookmark"
1277 bookmark-current-bookmark)))
1278 (bookmark-maybe-historicize-string bookmark)
1279 (bookmark-maybe-load-default-file)
1280 (let ((will-go (bookmark-get-bookmark bookmark)))
1281 (setq bookmark-alist (delq will-go bookmark-alist))
1282 ;; Added by db, nil bookmark-current-bookmark if the last
1283 ;; occurrence has been deleted
1284 (or (bookmark-get-bookmark bookmark-current-bookmark)
1285 (setq bookmark-current-bookmark nil)))
1286 ;; Don't rebuild the list
1287 (if batch
1289 (bookmark-bmenu-surreptitiously-rebuild-list)
1290 (setq bookmark-alist-modification-count
1291 (1+ bookmark-alist-modification-count))
1292 (if (bookmark-time-to-save-p)
1293 (bookmark-save))))
1296 (defun bookmark-time-to-save-p (&optional last-time)
1297 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1298 ;; finds out whether it's time to save bookmarks to a file, by
1299 ;; examining the value of variable bookmark-save-flag, and maybe
1300 ;; bookmark-alist-modification-count. Returns t if they should be
1301 ;; saved, nil otherwise. if last-time is non-nil, then this is
1302 ;; being called when emacs is killed.
1303 (cond (last-time
1304 (and (> bookmark-alist-modification-count 0)
1305 bookmark-save-flag))
1306 ((numberp bookmark-save-flag)
1307 (>= bookmark-alist-modification-count bookmark-save-flag))
1309 nil)))
1312 ;;;###autoload
1313 (defun bookmark-write ()
1314 "Write bookmarks to a file (reading the file name with the minibuffer).
1315 Don't use this in Lisp programs; use `bookmark-save' instead."
1316 (interactive)
1317 (bookmark-maybe-load-default-file)
1318 (bookmark-save t))
1321 ;;;###autoload
1322 (defun bookmark-save (&optional parg file)
1323 "Save currently defined bookmarks.
1324 Saves by default in the file defined by the variable
1325 `bookmark-default-file'. With a prefix arg, save it in file FILE
1326 \(second argument\).
1328 If you are calling this from Lisp, the two arguments are PARG and
1329 FILE, and if you just want it to write to the default file, then
1330 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1331 instead. If you pass in one argument, and it is non-nil, then the
1332 user will be interactively queried for a file to save in.
1334 When you want to load in the bookmarks from a file, use
1335 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1336 for a file, defaulting to the file defined by variable
1337 `bookmark-default-file'."
1338 (interactive "P")
1339 (bookmark-maybe-load-default-file)
1340 (cond
1341 ((and (null parg) (null file))
1342 ;;whether interactive or not, write to default file
1343 (bookmark-write-file bookmark-default-file))
1344 ((and (null parg) file)
1345 ;;whether interactive or not, write to given file
1346 (bookmark-write-file file))
1347 ((and parg (not file))
1348 ;;have been called interactively w/ prefix arg
1349 (let ((file (read-file-name "File to save bookmarks in: ")))
1350 (bookmark-write-file file)))
1351 (t ; someone called us with prefix-arg *and* a file, so just write to file
1352 (bookmark-write-file file)))
1353 ;; signal that we have synced the bookmark file by setting this to
1354 ;; 0. If there was an error at any point before, it will not get
1355 ;; set, which is what we want.
1356 (setq bookmark-alist-modification-count 0))
1360 (defun bookmark-write-file (file)
1361 (save-excursion
1362 (save-window-excursion
1363 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1364 (set-buffer (get-buffer-create " *Bookmarks*"))
1365 (goto-char (point-min))
1366 (delete-region (point-min) (point-max))
1367 (let ((print-length nil)
1368 (print-level nil))
1369 (bookmark-insert-file-format-version-stamp)
1370 (pp bookmark-alist (current-buffer))
1371 (let ((version-control
1372 (cond
1373 ((null bookmark-version-control) nil)
1374 ((eq 'never bookmark-version-control) 'never)
1375 ((eq 'nospecial bookmark-version-control) version-control)
1377 t))))
1378 (condition-case nil
1379 (write-region (point-min) (point-max) file)
1380 (file-error (message "Can't write %s" file)))
1381 (kill-buffer (current-buffer))
1382 (bookmark-maybe-message
1383 "Saving bookmarks to file %s...done" file))))))
1386 (defun bookmark-import-new-list (new-list)
1387 ;; Walk over the new list, adding each individual bookmark
1388 ;; carefully. "Carefully" means checking against the existing
1389 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1390 ;; as necessary.
1391 (let ((lst new-list)
1392 (names (bookmark-all-names)))
1393 (while lst
1394 (let* ((full-record (car lst)))
1395 (bookmark-maybe-rename full-record names)
1396 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1397 (setq names (cons (bookmark-name-from-full-record full-record) names))
1398 (setq lst (cdr lst))))))
1401 (defun bookmark-maybe-rename (full-record names)
1402 ;; just a helper for bookmark-import-new-list; it is only for
1403 ;; readability that this is not inlined.
1405 ;; Once this has found a free name, it sets full-record to that
1406 ;; name.
1407 (let ((found-name (bookmark-name-from-full-record full-record)))
1408 (if (member found-name names)
1409 ;; We've got a conflict, so generate a new name
1410 (let ((count 2)
1411 (new-name found-name))
1412 (while (member new-name names)
1413 (setq new-name (concat found-name (format "<%d>" count)))
1414 (setq count (1+ count)))
1415 (bookmark-set-name full-record new-name)))))
1418 ;;;###autoload
1419 (defun bookmark-load (file &optional overwrite no-msg)
1420 "Load bookmarks from FILE (which must be in bookmark format).
1421 Appends loaded bookmarks to the front of the list of bookmarks. If
1422 optional second argument OVERWRITE is non-nil, existing bookmarks are
1423 destroyed. Optional third arg NO-MSG means don't display any messages
1424 while loading.
1426 If you load a file that doesn't contain a proper bookmark alist, you
1427 will corrupt Emacs's bookmark list. Generally, you should only load
1428 in files that were created with the bookmark functions in the first
1429 place. Your own personal bookmark file, `~/.emacs.bmk', is
1430 maintained automatically by Emacs; you shouldn't need to load it
1431 explicitly.
1433 If you load a file containing bookmarks with the same names as
1434 bookmarks already present in your Emacs, the new bookmarks will get
1435 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1436 method buffers use to resolve name collisions."
1437 (interactive
1438 (list (read-file-name
1439 (format "Load bookmarks from: (%s) "
1440 bookmark-default-file)
1441 ;;Default might not be used often,
1442 ;;but there's no better default, and
1443 ;;I guess it's better than none at all.
1444 "~/" bookmark-default-file 'confirm)))
1445 (setq file (expand-file-name file))
1446 (if (file-readable-p file)
1447 (save-excursion
1448 (save-window-excursion
1449 (if (null no-msg)
1450 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1451 (set-buffer (let ((enable-local-variables nil))
1452 (find-file-noselect file)))
1453 (goto-char (point-min))
1454 (bookmark-maybe-upgrade-file-format)
1455 (let ((blist (bookmark-alist-from-buffer)))
1456 (if (listp blist)
1457 (progn
1458 (if overwrite
1459 (progn
1460 (setq bookmark-alist blist)
1461 (setq bookmark-alist-modification-count 0))
1462 ;; else
1463 (bookmark-import-new-list blist)
1464 (setq bookmark-alist-modification-count
1465 (1+ bookmark-alist-modification-count)))
1466 (if (string-equal
1467 (expand-file-name bookmark-default-file)
1468 file)
1469 (setq bookmarks-already-loaded t))
1470 (bookmark-bmenu-surreptitiously-rebuild-list))
1471 (error "Invalid bookmark list in %s" file)))
1472 (kill-buffer (current-buffer)))
1473 (if (null no-msg)
1474 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1475 (error "Cannot read bookmark file %s" file)))
1479 ;;; Code supporting the dired-like bookmark menu. Prefix is
1480 ;;; "bookmark-bmenu" for "buffer-menu":
1483 (defvar bookmark-bmenu-bookmark-column nil)
1486 (defvar bookmark-bmenu-hidden-bookmarks ())
1489 (defvar bookmark-bmenu-mode-map nil)
1492 (if bookmark-bmenu-mode-map
1494 (setq bookmark-bmenu-mode-map (make-keymap))
1495 (suppress-keymap bookmark-bmenu-mode-map t)
1496 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1497 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1498 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1499 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1500 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1501 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1502 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1503 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1504 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1505 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1506 (define-key bookmark-bmenu-mode-map "\C-o"
1507 'bookmark-bmenu-switch-other-window)
1508 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1509 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1510 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1511 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1512 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1513 (define-key bookmark-bmenu-mode-map " " 'next-line)
1514 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1515 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1516 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1517 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1518 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1519 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1520 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1521 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1522 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1523 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1524 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1525 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1526 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1527 (define-key bookmark-bmenu-mode-map [mouse-2]
1528 'bookmark-bmenu-other-window-with-mouse))
1532 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1533 ;; data.
1534 (put 'bookmark-bmenu-mode 'mode-class 'special)
1537 ;; todo: need to display whether or not bookmark exists as a buffer in
1538 ;; flag column.
1540 ;; Format:
1541 ;; FLAGS BOOKMARK [ LOCATION ]
1544 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1545 "Rebuild the Bookmark List if it exists.
1546 Don't affect the buffer ring order."
1547 (if (get-buffer "*Bookmark List*")
1548 (save-excursion
1549 (save-window-excursion
1550 (bookmark-bmenu-list)))))
1553 ;;;###autoload
1554 (defun bookmark-bmenu-list ()
1555 "Display a list of existing bookmarks.
1556 The list is displayed in a buffer named `*Bookmark List*'.
1557 The leftmost column displays a D if the bookmark is flagged for
1558 deletion, or > if it is flagged for displaying."
1559 (interactive)
1560 (bookmark-maybe-load-default-file)
1561 (if (interactive-p)
1562 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1563 (set-buffer (get-buffer-create "*Bookmark List*")))
1564 (let ((inhibit-read-only t))
1565 (erase-buffer)
1566 (insert "% Bookmark\n- --------\n")
1567 (add-text-properties (point-min) (point)
1568 '(font-lock-face bookmark-menu-heading))
1569 (bookmark-maybe-sort-alist)
1570 (mapcar
1571 (lambda (full-record)
1572 ;; if a bookmark has an annotation, prepend a "*"
1573 ;; in the list of bookmarks.
1574 (let ((annotation (bookmark-get-annotation
1575 (bookmark-name-from-full-record full-record))))
1576 (if (and annotation (not (string-equal annotation "")))
1577 (insert " *")
1578 (insert " "))
1579 (let ((start (point)))
1580 (insert (bookmark-name-from-full-record full-record))
1581 (if (and (display-color-p) (display-mouse-p))
1582 (add-text-properties
1583 start
1584 (save-excursion (re-search-backward
1585 "[^ \t]")
1586 (1+ (point)))
1587 '(mouse-face highlight
1588 follow-link t
1589 help-echo "mouse-2: go to this bookmark in other window")))
1590 (insert "\n")
1592 bookmark-alist))
1593 (goto-char (point-min))
1594 (forward-line 2)
1595 (bookmark-bmenu-mode)
1596 (if bookmark-bmenu-toggle-filenames
1597 (bookmark-bmenu-toggle-filenames t)))
1599 ;;;###autoload
1600 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1601 ;;;###autoload
1602 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1606 (defun bookmark-bmenu-mode ()
1607 "Major mode for editing a list of bookmarks.
1608 Each line describes one of the bookmarks in Emacs.
1609 Letters do not insert themselves; instead, they are commands.
1610 Bookmark names preceded by a \"*\" have annotations.
1611 \\<bookmark-bmenu-mode-map>
1612 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1613 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1614 Also show bookmarks marked using m in other windows.
1615 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1616 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1617 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1618 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1619 together with bookmark selected before this one in another window.
1620 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1621 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1622 so the bookmark menu bookmark remains visible in its window.
1623 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1624 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1625 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1626 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1627 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1628 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1629 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1630 With a prefix arg, prompts for a file to save in.
1631 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1632 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1633 With prefix argument, also move up one line.
1634 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1635 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1636 in another buffer.
1637 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1638 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1639 (kill-all-local-variables)
1640 (use-local-map bookmark-bmenu-mode-map)
1641 (setq truncate-lines t)
1642 (setq buffer-read-only t)
1643 (setq major-mode 'bookmark-bmenu-mode)
1644 (setq mode-name "Bookmark Menu")
1645 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1648 (defun bookmark-bmenu-toggle-filenames (&optional show)
1649 "Toggle whether filenames are shown in the bookmark list.
1650 Optional argument SHOW means show them unconditionally."
1651 (interactive)
1652 (cond
1653 (show
1654 (setq bookmark-bmenu-toggle-filenames nil)
1655 (bookmark-bmenu-show-filenames)
1656 (setq bookmark-bmenu-toggle-filenames t))
1657 (bookmark-bmenu-toggle-filenames
1658 (bookmark-bmenu-hide-filenames)
1659 (setq bookmark-bmenu-toggle-filenames nil))
1661 (bookmark-bmenu-show-filenames)
1662 (setq bookmark-bmenu-toggle-filenames t))))
1665 (defun bookmark-bmenu-show-filenames (&optional force)
1666 (if (and (not force) bookmark-bmenu-toggle-filenames)
1667 nil ;already shown, so do nothing
1668 (save-excursion
1669 (save-window-excursion
1670 (goto-char (point-min))
1671 (forward-line 2)
1672 (setq bookmark-bmenu-hidden-bookmarks ())
1673 (let ((inhibit-read-only t))
1674 (while (< (point) (point-max))
1675 (let ((bmrk (bookmark-bmenu-bookmark)))
1676 (setq bookmark-bmenu-hidden-bookmarks
1677 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1678 (let ((start (save-excursion (end-of-line) (point))))
1679 (move-to-column bookmark-bmenu-file-column t)
1680 ;; Strip off `mouse-face' from the white spaces region.
1681 (if (and (display-color-p) (display-mouse-p))
1682 (remove-text-properties start (point)
1683 '(mouse-face nil help-echo nil))))
1684 (delete-region (point) (progn (end-of-line) (point)))
1685 (insert " ")
1686 ;; Pass the NO-HISTORY arg:
1687 (bookmark-insert-location bmrk t)
1688 (forward-line 1))))))))
1691 (defun bookmark-bmenu-hide-filenames (&optional force)
1692 (if (and (not force) bookmark-bmenu-toggle-filenames)
1693 ;; nothing to hide if above is nil
1694 (save-excursion
1695 (save-window-excursion
1696 (goto-char (point-min))
1697 (forward-line 2)
1698 (setq bookmark-bmenu-hidden-bookmarks
1699 (nreverse bookmark-bmenu-hidden-bookmarks))
1700 (save-excursion
1701 (goto-char (point-min))
1702 (search-forward "Bookmark")
1703 (backward-word 1)
1704 (setq bookmark-bmenu-bookmark-column (current-column)))
1705 (save-excursion
1706 (let ((inhibit-read-only t))
1707 (while bookmark-bmenu-hidden-bookmarks
1708 (move-to-column bookmark-bmenu-bookmark-column t)
1709 (bookmark-kill-line)
1710 (let ((start (point)))
1711 (insert (car bookmark-bmenu-hidden-bookmarks))
1712 (if (and (display-color-p) (display-mouse-p))
1713 (add-text-properties
1714 start
1715 (save-excursion (re-search-backward
1716 "[^ \t]")
1717 (1+ (point)))
1718 '(mouse-face highlight
1719 follow-link t
1720 help-echo
1721 "mouse-2: go to this bookmark in other window"))))
1722 (setq bookmark-bmenu-hidden-bookmarks
1723 (cdr bookmark-bmenu-hidden-bookmarks))
1724 (forward-line 1))))))))
1727 (defun bookmark-bmenu-check-position ()
1728 ;; Returns non-nil if on a line with a bookmark.
1729 ;; (The actual value returned is bookmark-alist).
1730 ;; Else reposition and try again, else return nil.
1731 (cond ((< (count-lines (point-min) (point)) 2)
1732 (goto-char (point-min))
1733 (forward-line 2)
1734 bookmark-alist)
1735 ((and (bolp) (eobp))
1736 (beginning-of-line 0)
1737 bookmark-alist)
1739 bookmark-alist)))
1742 (defun bookmark-bmenu-bookmark ()
1743 ;; return a string which is bookmark of this line.
1744 (if (bookmark-bmenu-check-position)
1745 (save-excursion
1746 (save-window-excursion
1747 (goto-char (point-min))
1748 (search-forward "Bookmark")
1749 (backward-word 1)
1750 (setq bookmark-bmenu-bookmark-column (current-column)))))
1751 (if bookmark-bmenu-toggle-filenames
1752 (bookmark-bmenu-hide-filenames))
1753 (save-excursion
1754 (save-window-excursion
1755 (beginning-of-line)
1756 (forward-char bookmark-bmenu-bookmark-column)
1757 (prog1
1758 (buffer-substring-no-properties (point)
1759 (progn
1760 (end-of-line)
1761 (point)))
1762 ;; well, this is certainly crystal-clear:
1763 (if bookmark-bmenu-toggle-filenames
1764 (bookmark-bmenu-toggle-filenames t))))))
1767 (defun bookmark-show-annotation (bookmark)
1768 "Display the annotation for bookmark named BOOKMARK in a buffer,
1769 if an annotation exists."
1770 (let ((annotation (bookmark-get-annotation bookmark)))
1771 (if (and annotation (not (string-equal annotation "")))
1772 (save-excursion
1773 (let ((old-buf (current-buffer)))
1774 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1775 (delete-region (point-min) (point-max))
1776 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1777 (insert annotation)
1778 (goto-char (point-min))
1779 (pop-to-buffer old-buf))))))
1782 (defun bookmark-show-all-annotations ()
1783 "Display the annotations for all bookmarks in a buffer."
1784 (let ((old-buf (current-buffer)))
1785 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1786 (delete-region (point-min) (point-max))
1787 (mapcar
1788 (lambda (full-record)
1789 (let* ((name (bookmark-name-from-full-record full-record))
1790 (ann (bookmark-get-annotation name)))
1791 (insert (concat name ":\n"))
1792 (if (and ann (not (string-equal ann "")))
1793 ;; insert the annotation, indented by 4 spaces.
1794 (progn
1795 (save-excursion (insert ann))
1796 (while (< (point) (point-max))
1797 (beginning-of-line) ; paranoia
1798 (insert " ")
1799 (forward-line)
1800 (end-of-line))))))
1801 bookmark-alist)
1802 (goto-char (point-min))
1803 (pop-to-buffer old-buf)))
1806 (defun bookmark-bmenu-mark ()
1807 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1808 (interactive)
1809 (beginning-of-line)
1810 (if (bookmark-bmenu-check-position)
1811 (let ((inhibit-read-only t))
1812 (delete-char 1)
1813 (insert ?>)
1814 (forward-line 1)
1815 (bookmark-bmenu-check-position))))
1818 (defun bookmark-bmenu-select ()
1819 "Select this line's bookmark; also display bookmarks marked with `>'.
1820 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1821 (interactive)
1822 (if (bookmark-bmenu-check-position)
1823 (let ((bmrk (bookmark-bmenu-bookmark))
1824 (menu (current-buffer))
1825 (others ())
1826 tem)
1827 (goto-char (point-min))
1828 (while (re-search-forward "^>" nil t)
1829 (setq tem (bookmark-bmenu-bookmark))
1830 (let ((inhibit-read-only t))
1831 (delete-char -1)
1832 (insert ?\s))
1833 (or (string-equal tem bmrk)
1834 (member tem others)
1835 (setq others (cons tem others))))
1836 (setq others (nreverse others)
1837 tem (/ (1- (frame-height)) (1+ (length others))))
1838 (delete-other-windows)
1839 (bookmark-jump bmrk)
1840 (bury-buffer menu)
1841 (if others
1842 (while others
1843 (split-window nil tem)
1844 (other-window 1)
1845 (bookmark-jump (car others))
1846 (setq others (cdr others)))
1847 (other-window 1)))))
1850 (defun bookmark-bmenu-save (parg)
1851 "Save the current list into a bookmark file.
1852 With a prefix arg, prompts for a file to save them in."
1853 (interactive "P")
1854 (save-excursion
1855 (save-window-excursion
1856 (bookmark-save parg))))
1859 (defun bookmark-bmenu-load ()
1860 "Load the bookmark file and rebuild the bookmark menu-buffer."
1861 (interactive)
1862 (if (bookmark-bmenu-check-position)
1863 (save-excursion
1864 (save-window-excursion
1865 ;; This will call `bookmark-bmenu-list'
1866 (call-interactively 'bookmark-load)))))
1869 (defun bookmark-bmenu-1-window ()
1870 "Select this line's bookmark, alone, in full frame."
1871 (interactive)
1872 (if (bookmark-bmenu-check-position)
1873 (progn
1874 (bookmark-jump (bookmark-bmenu-bookmark))
1875 (bury-buffer (other-buffer))
1876 (delete-other-windows))))
1879 (defun bookmark-bmenu-2-window ()
1880 "Select this line's bookmark, with previous buffer in second window."
1881 (interactive)
1882 (if (bookmark-bmenu-check-position)
1883 (let ((bmrk (bookmark-bmenu-bookmark))
1884 (menu (current-buffer))
1885 (pop-up-windows t))
1886 (delete-other-windows)
1887 (switch-to-buffer (other-buffer))
1888 (let* ((pair (bookmark-jump-noselect bmrk))
1889 (buff (car pair))
1890 (pos (cdr pair)))
1891 (pop-to-buffer buff)
1892 (goto-char pos))
1893 (bury-buffer menu))))
1896 (defun bookmark-bmenu-this-window ()
1897 "Select this line's bookmark in this window."
1898 (interactive)
1899 (if (bookmark-bmenu-check-position)
1900 (bookmark-jump (bookmark-bmenu-bookmark))))
1903 (defun bookmark-bmenu-other-window ()
1904 "Select this line's bookmark in other window, leaving bookmark menu visible."
1905 (interactive)
1906 (let ((bookmark (bookmark-bmenu-bookmark)))
1907 (if (bookmark-bmenu-check-position)
1908 (let* ((pair (bookmark-jump-noselect bookmark))
1909 (buff (car pair))
1910 (pos (cdr pair)))
1911 (switch-to-buffer-other-window buff)
1912 (goto-char pos)
1913 (set-window-point (get-buffer-window buff) pos)
1914 (bookmark-show-annotation bookmark)))))
1917 (defun bookmark-bmenu-switch-other-window ()
1918 "Make the other window select this line's bookmark.
1919 The current window remains selected."
1920 (interactive)
1921 (let ((bookmark (bookmark-bmenu-bookmark))
1922 (pop-up-windows t)
1923 same-window-buffer-names
1924 same-window-regexps)
1925 (if (bookmark-bmenu-check-position)
1926 (let* ((pair (bookmark-jump-noselect bookmark))
1927 (buff (car pair))
1928 (pos (cdr pair)))
1929 (display-buffer buff)
1930 (let ((o-buffer (current-buffer)))
1931 ;; save-excursion won't do
1932 (set-buffer buff)
1933 (goto-char pos)
1934 (set-window-point (get-buffer-window buff) pos)
1935 (set-buffer o-buffer))
1936 (bookmark-show-annotation bookmark)))))
1938 (defun bookmark-bmenu-other-window-with-mouse (event)
1939 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1940 (interactive "e")
1941 (save-excursion
1942 (set-buffer (window-buffer (posn-window (event-end event))))
1943 (save-excursion
1944 (goto-char (posn-point (event-end event)))
1945 (bookmark-bmenu-other-window))))
1948 (defun bookmark-bmenu-show-annotation ()
1949 "Show the annotation for the current bookmark in another window."
1950 (interactive)
1951 (let ((bookmark (bookmark-bmenu-bookmark)))
1952 (if (bookmark-bmenu-check-position)
1953 (bookmark-show-annotation bookmark))))
1956 (defun bookmark-bmenu-show-all-annotations ()
1957 "Show the annotation for all bookmarks in another window."
1958 (interactive)
1959 (bookmark-show-all-annotations))
1962 (defun bookmark-bmenu-edit-annotation ()
1963 "Edit the annotation for the current bookmark in another window."
1964 (interactive)
1965 (let ((bookmark (bookmark-bmenu-bookmark)))
1966 (if (bookmark-bmenu-check-position)
1967 (bookmark-edit-annotation bookmark))))
1970 (defun bookmark-bmenu-unmark (&optional backup)
1971 "Cancel all requested operations on bookmark on this line and move down.
1972 Optional BACKUP means move up."
1973 (interactive "P")
1974 (beginning-of-line)
1975 (if (bookmark-bmenu-check-position)
1976 (progn
1977 (let ((inhibit-read-only t))
1978 (delete-char 1)
1979 ;; any flags to reset according to circumstances? How about a
1980 ;; flag indicating whether this bookmark is being visited?
1981 ;; well, we don't have this now, so maybe later.
1982 (insert " "))
1983 (forward-line (if backup -1 1))
1984 (bookmark-bmenu-check-position))))
1987 (defun bookmark-bmenu-backup-unmark ()
1988 "Move up and cancel all requested operations on bookmark on line above."
1989 (interactive)
1990 (forward-line -1)
1991 (if (bookmark-bmenu-check-position)
1992 (progn
1993 (bookmark-bmenu-unmark)
1994 (forward-line -1)
1995 (bookmark-bmenu-check-position))))
1998 (defun bookmark-bmenu-delete ()
1999 "Mark bookmark on this line to be deleted.
2000 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2001 (interactive)
2002 (beginning-of-line)
2003 (if (bookmark-bmenu-check-position)
2004 (let ((inhibit-read-only t))
2005 (delete-char 1)
2006 (insert ?D)
2007 (forward-line 1)
2008 (bookmark-bmenu-check-position))))
2011 (defun bookmark-bmenu-delete-backwards ()
2012 "Mark bookmark on this line to be deleted, then move up one line.
2013 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2014 (interactive)
2015 (bookmark-bmenu-delete)
2016 (forward-line -2)
2017 (if (bookmark-bmenu-check-position)
2018 (forward-line 1))
2019 (bookmark-bmenu-check-position))
2022 (defun bookmark-bmenu-execute-deletions ()
2023 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2024 (interactive)
2025 (message "Deleting bookmarks...")
2026 (let ((hide-em bookmark-bmenu-toggle-filenames)
2027 (o-point (point))
2028 (o-str (save-excursion
2029 (beginning-of-line)
2030 (if (looking-at "^D")
2032 (buffer-substring
2033 (point)
2034 (progn (end-of-line) (point))))))
2035 (o-col (current-column)))
2036 (if hide-em (bookmark-bmenu-hide-filenames))
2037 (setq bookmark-bmenu-toggle-filenames nil)
2038 (goto-char (point-min))
2039 (forward-line 1)
2040 (while (re-search-forward "^D" (point-max) t)
2041 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2042 (bookmark-bmenu-list)
2043 (setq bookmark-bmenu-toggle-filenames hide-em)
2044 (if bookmark-bmenu-toggle-filenames
2045 (bookmark-bmenu-toggle-filenames t))
2046 (if o-str
2047 (progn
2048 (goto-char (point-min))
2049 (search-forward o-str)
2050 (beginning-of-line)
2051 (forward-char o-col))
2052 (goto-char o-point))
2053 (beginning-of-line)
2054 (setq bookmark-alist-modification-count
2055 (1+ bookmark-alist-modification-count))
2056 (if (bookmark-time-to-save-p)
2057 (bookmark-save))
2058 (message "Deleting bookmarks...done")
2062 (defun bookmark-bmenu-rename ()
2063 "Rename bookmark on current line. Prompts for a new name."
2064 (interactive)
2065 (if (bookmark-bmenu-check-position)
2066 (let ((bmrk (bookmark-bmenu-bookmark))
2067 (thispoint (point)))
2068 (bookmark-rename bmrk)
2069 (bookmark-bmenu-list)
2070 (goto-char thispoint))))
2073 (defun bookmark-bmenu-locate ()
2074 "Display location of this bookmark. Displays in the minibuffer."
2075 (interactive)
2076 (if (bookmark-bmenu-check-position)
2077 (let ((bmrk (bookmark-bmenu-bookmark)))
2078 (message "%s" (bookmark-location bmrk)))))
2080 (defun bookmark-bmenu-relocate ()
2081 "Change the file path of the bookmark on the current line,
2082 prompting with completion for the new path."
2083 (interactive)
2084 (if (bookmark-bmenu-check-position)
2085 (let ((bmrk (bookmark-bmenu-bookmark))
2086 (thispoint (point)))
2087 (bookmark-relocate bmrk)
2088 (goto-char thispoint))))
2091 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2093 (defun bookmark-menu-popup-paned-menu (event name entries)
2094 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2095 That is, ENTRIES is a list of strings which appear as the choices
2096 in the menu.
2097 The number of panes depends on the number of entries.
2098 The visible entries are truncated to `bookmark-menu-length', but the
2099 strings returned are not."
2100 (let ((f-height (/ (frame-height) 2))
2101 (pane-list nil)
2102 (iter 0))
2103 (while entries
2104 (let (lst
2105 (count 0))
2106 (while (and (< count f-height) entries)
2107 (let ((str (car entries)))
2108 (push (cons
2109 (if (> (length str) bookmark-menu-length)
2110 (substring str 0 bookmark-menu-length)
2111 str)
2112 str)
2113 lst)
2114 (setq entries (cdr entries))
2115 (setq count (1+ count))))
2116 (setq iter (1+ iter))
2117 (push (cons
2118 (format "-*- %s (%d) -*-" name iter)
2119 (nreverse lst))
2120 pane-list)))
2122 ;; Popup the menu and return the string.
2123 (x-popup-menu event (cons (concat "-*- " name " -*-")
2124 (nreverse pane-list)))))
2127 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2128 ;; following works, and for explaining what to do to make it work.
2130 ;; We MUST autoload EACH form used to set up this variable's value, so
2131 ;; that the whole job is done in loaddefs.el.
2133 ;; Emacs menubar stuff.
2135 ;;;###autoload
2136 (defvar menu-bar-bookmark-map
2137 (let ((map (make-sparse-keymap "Bookmark functions")))
2138 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2139 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2140 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2141 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2142 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2143 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2144 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2145 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2146 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2147 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2148 map))
2150 ;;;###autoload
2151 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2153 ;; make bookmarks appear toward the right side of the menu.
2154 (if (boundp 'menu-bar-final-items)
2155 (if menu-bar-final-items
2156 (setq menu-bar-final-items
2157 (cons 'bookmark menu-bar-final-items)))
2158 (setq menu-bar-final-items '(bookmark)))
2160 ;;;; end bookmark menu stuff ;;;;
2163 ;;; Load Hook
2164 (defvar bookmark-load-hook nil
2165 "Hook run at the end of loading bookmark.")
2167 ;;; Exit Hook, called from kill-emacs-hook
2168 (defvar bookmark-exit-hook nil
2169 "Hook run when Emacs exits.")
2171 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2173 (defun bookmark-exit-hook-internal ()
2174 "Save bookmark state, if necessary, at Emacs exit time.
2175 This also runs `bookmark-exit-hook'."
2176 (run-hooks 'bookmark-exit-hook)
2177 (and bookmark-alist
2178 (bookmark-time-to-save-p t)
2179 (bookmark-save)))
2181 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2184 (run-hooks 'bookmark-load-hook)
2186 (provide 'bookmark)
2188 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2189 ;;; bookmark.el ends here