Minor docstring a comment improvements.
[emacs.git] / lisp / bookmark.el
blob7a3e4d9a12a35c1026ac7d630a97b5aa21246cf1
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, 2008 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 3, 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 ;; Added for lucid emacs compatibility, db
211 (or (fboundp 'defalias) (fset 'defalias 'fset))
213 ;; suggested for lucid compatibility by david hughes:
214 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
217 ;;; Keymap stuff:
219 ;; Set up these bindings dumping time *only*;
220 ;; if the user alters them, don't override the user when loading bookmark.el.
222 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
223 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
224 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
226 ;;;###autoload
227 (defvar bookmark-map
228 (let ((map (make-sparse-keymap)))
229 ;; Read the help on all of these functions for details...
230 (define-key map "x" 'bookmark-set)
231 (define-key map "m" 'bookmark-set) ;"m"ark
232 (define-key map "j" 'bookmark-jump)
233 (define-key map "g" 'bookmark-jump) ;"g"o
234 (define-key map "o" 'bookmark-jump-other-window)
235 (define-key map "i" 'bookmark-insert)
236 (define-key map "e" 'edit-bookmarks)
237 (define-key map "f" 'bookmark-insert-location) ;"f"ind
238 (define-key map "r" 'bookmark-rename)
239 (define-key map "d" 'bookmark-delete)
240 (define-key map "l" 'bookmark-load)
241 (define-key map "w" 'bookmark-write)
242 (define-key map "s" 'bookmark-save)
243 map)
244 "Keymap containing bindings to bookmark functions.
245 It is not bound to any key by default: to bind it
246 so that you have a bookmark prefix, just use `global-set-key' and bind a
247 key of your choice to `bookmark-map'. All interactive bookmark
248 functions have a binding in this keymap.")
250 ;;;###autoload (fset 'bookmark-map bookmark-map)
253 ;;; Core variables and data structures:
254 (defvar bookmark-alist ()
255 "Association list of bookmarks and their records.
256 You probably don't want to change the value of this alist yourself;
257 instead, let the various bookmark functions do it for you.
259 The format of the alist is
261 \(BOOKMARK1 BOOKMARK2 ...\)
263 where each BOOKMARK is typically of the form
265 \(NAME
266 (filename . FILE)
267 (front-context-string . FRONT-STR)
268 (rear-context-string . REAR-STR)
269 (position . POS)
270 (annotation . ANNOTATION))
272 So the cdr of each bookmark is an alist too.")
275 (defvar bookmarks-already-loaded nil)
278 ;; more stuff added by db.
280 (defvar bookmark-current-bookmark nil
281 "Name of bookmark most recently used in the current file.
282 It is buffer local, used to make moving a bookmark forward
283 through a file easier.")
285 (make-variable-buffer-local 'bookmark-current-bookmark)
288 (defvar bookmark-alist-modification-count 0
289 "Number of modifications to bookmark list since it was last saved.")
292 (defvar bookmark-search-size 16
293 "Length of the context strings recorded on either side of a bookmark.")
296 (defvar bookmark-current-point 0)
297 (defvar bookmark-yank-point 0)
298 (defvar bookmark-current-buffer nil)
300 (defvar Info-suffix-list)
302 ;; Helper functions.
304 ;; Only functions on this page and the next one (file formats) need to
305 ;; know anything about the format of bookmark-alist entries.
306 ;; Everyone else should go through them.
309 (defun bookmark-name-from-full-record (full-record)
310 "Return name of FULL-RECORD \(an alist element instead of a string\)."
311 (car full-record))
314 (defun bookmark-all-names ()
315 "Return a list of all current bookmark names."
316 (bookmark-maybe-load-default-file)
317 (mapcar
318 (lambda (full-record)
319 (bookmark-name-from-full-record full-record))
320 bookmark-alist))
323 (defun bookmark-get-bookmark (bookmark)
324 "Return the full entry for BOOKMARK in `bookmark-alist'.
325 If BOOKMARK is not a string, return nil."
326 (when (stringp bookmark)
327 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
330 (defun bookmark-get-bookmark-record (bookmark)
331 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
332 That is, all information but the name."
333 (car (cdr (bookmark-get-bookmark bookmark))))
336 (defun bookmark-set-name (bookmark newname)
337 "Set BOOKMARK's name to NEWNAME."
338 (setcar
339 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
340 newname))
342 (defun bookmark-prop-get (bookmark prop)
343 "Return the property PROP of BOOKMARK, or nil if none."
344 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
346 (defun bookmark-prop-set (bookmark prop val)
347 "Set the property PROP of BOOKMARK to VAL."
348 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
349 (if cell
350 (setcdr cell val)
351 (nconc (bookmark-get-bookmark-record bookmark)
352 (list (cons prop val))))))
354 (defun bookmark-get-annotation (bookmark)
355 "Return the annotation of BOOKMARK, or nil if none."
356 (bookmark-prop-get bookmark 'annotation))
358 (defun bookmark-set-annotation (bookmark ann)
359 "Set the annotation of BOOKMARK to ANN."
360 (bookmark-prop-set bookmark 'annotation ann))
363 (defun bookmark-get-filename (bookmark)
364 "Return the full filename of BOOKMARK."
365 (bookmark-prop-get bookmark 'filename))
368 (defun bookmark-set-filename (bookmark filename)
369 "Set the full filename of BOOKMARK to FILENAME."
370 (bookmark-prop-set bookmark 'filename filename)
371 (setq bookmark-alist-modification-count
372 (1+ bookmark-alist-modification-count))
373 (if (bookmark-time-to-save-p)
374 (bookmark-save)))
377 (defun bookmark-get-position (bookmark)
378 "Return the position \(i.e.: point\) of BOOKMARK."
379 (bookmark-prop-get bookmark 'position))
382 (defun bookmark-set-position (bookmark position)
383 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
384 (bookmark-prop-set bookmark 'position position))
387 (defun bookmark-get-front-context-string (bookmark)
388 "Return the front-context-string of BOOKMARK."
389 (bookmark-prop-get bookmark 'front-context-string))
392 (defun bookmark-set-front-context-string (bookmark string)
393 "Set the front-context-string of BOOKMARK to STRING."
394 (bookmark-prop-set bookmark 'front-context-string string))
397 (defun bookmark-get-rear-context-string (bookmark)
398 "Return the rear-context-string of BOOKMARK."
399 (bookmark-prop-get bookmark 'rear-context-string))
402 (defun bookmark-set-rear-context-string (bookmark string)
403 "Set the rear-context-string of BOOKMARK to STRING."
404 (bookmark-prop-set bookmark 'rear-context-string string))
407 (defun bookmark-get-handler (bookmark)
408 (bookmark-prop-get bookmark 'handler))
410 (defvar bookmark-history nil
411 "The history list for bookmark functions.")
414 (defun bookmark-completing-read (prompt &optional default)
415 "Prompting with PROMPT, read a bookmark name in completion.
416 PROMPT will get a \": \" stuck on the end no matter what, so you
417 probably don't want to include one yourself.
418 Optional second arg DEFAULT is a string to return if the user enters
419 the empty string."
420 (bookmark-maybe-load-default-file) ; paranoia
421 (if (listp last-nonmenu-event)
422 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
423 (let* ((completion-ignore-case bookmark-completion-ignore-case)
424 (default default)
425 (prompt (if default
426 (concat prompt (format " (%s): " default))
427 (concat prompt ": ")))
428 (str
429 (completing-read prompt
430 bookmark-alist
434 'bookmark-history)))
435 (if (string-equal "" str) default str))))
438 (defmacro bookmark-maybe-historicize-string (string)
439 "Put STRING into the bookmark prompt history, if caller non-interactive.
440 We need this because sometimes bookmark functions are invoked from
441 menus, so `completing-read' never gets a chance to set `bookmark-history'."
442 `(or
443 (interactive-p)
444 (setq bookmark-history (cons ,string bookmark-history))))
446 (defvar bookmark-make-record-function 'bookmark-make-record-for-text-file
447 "A function that should be called to create a bookmark record.
448 Modes may set this variable buffer-locally to enable bookmarking of
449 locations that should be treated specially, such as Info nodes,
450 news posts, images, pdf documents, etc.
452 The function will be called with no arguments.
454 The returned record should be a cons cell of the form (NAME . ALIST)
455 where ALIST is as described in `bookmark-alist' and may typically contain
456 a special cons (handler . SOME-FUNCTION) which sets the handler function
457 that should be used to open this bookmark instead of
458 `bookmark-default-handler'. The handler should return an alist like the
459 one that function returns, and (of course) should likewise
460 not select the buffer.
461 It should signal a user error if it is unable to construct a record for the current
462 location.
464 NAME is a suggested name for the constructed bookmark. It can be nil
465 in which case a default heuristic will be used.")
467 (defun bookmark-make-record ()
468 "Return a new bookmark record (NAME . ALIST) for the current location."
469 (let ((record (funcall bookmark-make-record-function)))
470 ;; Set up default name.
471 (if (stringp (car record))
472 ;; The function already provided a default name.
473 record
474 (if (car record) (push nil record))
475 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
476 record)))
478 (defun bookmark-store (name alist no-overwrite)
479 "Store the bookmark NAME with data ALIST.
480 If NO-OVERWRITE is non-nil and another bookmark of the same name already
481 exists in `bookmark-alist', record the new bookmark without throwing away the
482 old one."
483 (bookmark-maybe-load-default-file)
484 (let ((stripped-name (copy-sequence name)))
485 (or (featurep 'xemacs)
486 ;; XEmacs's `set-text-properties' doesn't work on
487 ;; free-standing strings, apparently.
488 (set-text-properties 0 (length stripped-name) nil stripped-name))
489 (if (and (bookmark-get-bookmark stripped-name) (not no-overwrite))
490 ;; already existing bookmark under that name and
491 ;; no prefix arg means just overwrite old bookmark
492 (setcdr (bookmark-get-bookmark stripped-name) (list alist))
494 ;; otherwise just cons it onto the front (either the bookmark
495 ;; doesn't exist already, or there is no prefix arg. In either
496 ;; case, we want the new bookmark consed onto the alist...)
498 (push (list stripped-name alist) bookmark-alist))
500 ;; Added by db
501 (setq bookmark-current-bookmark stripped-name)
502 (setq bookmark-alist-modification-count
503 (1+ bookmark-alist-modification-count))
504 (if (bookmark-time-to-save-p)
505 (bookmark-save))
507 (setq bookmark-current-bookmark stripped-name)
508 (bookmark-bmenu-surreptitiously-rebuild-list)))
510 (defun bookmark-make-record-for-text-file ()
511 "Return the record describing the location of a new bookmark.
512 Must be at the correct position in the buffer in which the bookmark is
513 being set (this might change someday)."
514 `((filename . ,(bookmark-buffer-file-name))
515 (front-context-string
516 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
517 (buffer-substring-no-properties
518 (point)
519 (+ (point) bookmark-search-size))
520 nil))
521 (rear-context-string
522 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
523 (buffer-substring-no-properties
524 (point)
525 (- (point) bookmark-search-size))
526 nil))
527 (position . ,(point))))
530 ;;; File format stuff
532 ;; The OLD format of the bookmark-alist was:
534 ;; ((BOOKMARK-NAME . (FILENAME
535 ;; STRING-IN-FRONT
536 ;; STRING-BEHIND
537 ;; POINT))
538 ;; ...)
540 ;; The NEW format of the bookmark-alist is:
542 ;; ((BOOKMARK-NAME (filename . FILENAME)
543 ;; (front-context-string . STRING-IN-FRONT)
544 ;; (rear-context-string . STRING-BEHIND)
545 ;; (position . POINT)
546 ;; (annotation . ANNOTATION)
547 ;; (whatever . VALUE)
548 ;; ...
549 ;; ))
550 ;; ...)
553 ;; I switched to using an internal as well as external alist because I
554 ;; felt that would be a more flexible framework in which to add
555 ;; features. It means that the order in which values appear doesn't
556 ;; matter, and it means that arbitrary values can be added without
557 ;; risk of interfering with existing ones.
559 ;; BOOKMARK-NAME is the string the user gives the bookmark and
560 ;; accesses it by from then on.
562 ;; FILENAME is the location of the file in which the bookmark is set.
564 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
565 ;; context in front of the point at which the bookmark is set.
567 ;; STRING-BEHIND is the same thing, but after the point.
569 ;; The context strings exist so that modifications to a file don't
570 ;; necessarily cause a bookmark's position to be invalidated.
571 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
572 ;; case the file has changed since the bookmark was set. It will
573 ;; attempt to place the user before the changes, if there were any.
574 ;; ANNOTATION is the annotation for the bookmark; it may not exist
575 ;; (for backward compatibility), be nil (no annotation), or be a
576 ;; string.
579 (defconst bookmark-file-format-version 1
580 "The current version of the format used by bookmark files.
581 You should never need to change this.")
584 (defconst bookmark-end-of-version-stamp-marker
585 "-*- End Of Bookmark File Format Version Stamp -*-\n"
586 "This string marks the end of the version stamp in a bookmark file.")
589 (defun bookmark-alist-from-buffer ()
590 "Return a `bookmark-alist' (in any format) from the current buffer.
591 The buffer must of course contain bookmark format information.
592 Does not care from where in the buffer it is called, and does not
593 affect point."
594 (save-excursion
595 (goto-char (point-min))
596 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
597 (read (current-buffer))
598 ;; Else we're dealing with format version 0
599 (if (search-forward "(" nil t)
600 (progn
601 (forward-char -1)
602 (read (current-buffer)))
603 ;; Else no hope of getting information here.
604 (error "Not bookmark format")))))
607 (defun bookmark-upgrade-version-0-alist (old-list)
608 "Upgrade a version 0 alist OLD-LIST to the current version."
609 (mapcar
610 (lambda (bookmark)
611 (let* ((name (car bookmark))
612 (record (car (cdr bookmark)))
613 (filename (nth 0 record))
614 (front-str (nth 1 record))
615 (rear-str (nth 2 record))
616 (position (nth 3 record))
617 (ann (nth 4 record)))
618 (list
619 name
620 `((filename . ,filename)
621 (front-context-string . ,(or front-str ""))
622 (rear-context-string . ,(or rear-str ""))
623 (position . ,position)
624 (annotation . ,ann)))))
625 old-list))
628 (defun bookmark-upgrade-file-format-from-0 ()
629 "Upgrade a bookmark file of format 0 (the original format) to format 1.
630 This expects to be called from `point-min' in a bookmark file."
631 (message "Upgrading bookmark format from 0 to %d..."
632 bookmark-file-format-version)
633 (let* ((old-list (bookmark-alist-from-buffer))
634 (new-list (bookmark-upgrade-version-0-alist old-list)))
635 (delete-region (point-min) (point-max))
636 (bookmark-insert-file-format-version-stamp)
637 (pp new-list (current-buffer))
638 (save-buffer))
639 (goto-char (point-min))
640 (message "Upgrading bookmark format from 0 to %d...done"
641 bookmark-file-format-version)
645 (defun bookmark-grok-file-format-version ()
646 "Return an integer which is the file-format version of this bookmark file.
647 This expects to be called from `point-min' in a bookmark file."
648 (if (looking-at "^;;;;")
649 (save-excursion
650 (save-match-data
651 (re-search-forward "[0-9]")
652 (forward-char -1)
653 (read (current-buffer))))
654 ;; Else this is format version 0, the original one, which didn't
655 ;; even have version stamps.
659 (defun bookmark-maybe-upgrade-file-format ()
660 "Check the file-format version of this bookmark file.
661 If the version is not up-to-date, upgrade it automatically.
662 This expects to be called from `point-min' in a bookmark file."
663 (let ((version (bookmark-grok-file-format-version)))
664 (cond
665 ((= version bookmark-file-format-version)
666 ) ; home free -- version is current
667 ((= version 0)
668 (bookmark-upgrade-file-format-from-0))
670 (error "Bookmark file format version strangeness")))))
673 (defun bookmark-insert-file-format-version-stamp ()
674 "Insert text indicating current version of bookmark file format."
675 (insert
676 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
677 bookmark-file-format-version))
678 (insert ";;; This format is meant to be slightly human-readable;\n"
679 ";;; nevertheless, you probably don't want to edit it.\n"
680 ";;; "
681 bookmark-end-of-version-stamp-marker))
684 ;;; end file-format stuff
687 ;;; Generic helpers.
689 (defun bookmark-maybe-message (fmt &rest args)
690 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
691 (if (>= baud-rate 9600)
692 (apply 'message fmt args)))
695 ;;; Core code:
697 (defvar bookmark-minibuffer-read-name-map
698 (let ((map (make-sparse-keymap)))
699 (set-keymap-parent map minibuffer-local-map)
700 (define-key map "\C-w" 'bookmark-yank-word)
701 ;; This C-u binding might not be very useful any more now that we
702 ;; provide access to the default via the standard M-n binding.
703 ;; Maybe we should just remove it? --Stef-08
704 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
705 map))
707 ;;;###autoload
708 (defun bookmark-set (&optional name parg)
709 "Set a bookmark named NAME inside a file.
710 If name is nil, then the user will be prompted.
711 With prefix arg, will not overwrite a bookmark that has the same name
712 as NAME if such a bookmark already exists, but instead will \"push\"
713 the new bookmark onto the bookmark alist. Thus the most recently set
714 bookmark with name NAME would be the one in effect at any given time,
715 but the others are still there, should you decide to delete the most
716 recent one.
718 To yank words from the text of the buffer and use them as part of the
719 bookmark name, type C-w while setting a bookmark. Successive C-w's
720 yank successive words.
722 Typing C-u inserts the name of the last bookmark used in the buffer
723 \(as an aid in using a single bookmark name to track your progress
724 through a large file\). If no bookmark was used, then C-u inserts the
725 name of the file being visited.
727 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
728 and it removes only the first instance of a bookmark with that name from
729 the list of bookmarks.\)"
730 (interactive (list nil current-prefix-arg))
731 (let* ((record (bookmark-make-record))
732 (default (car record)))
734 (bookmark-maybe-load-default-file)
736 (setq bookmark-current-point (point))
737 (setq bookmark-yank-point (point))
738 (setq bookmark-current-buffer (current-buffer))
740 (let ((str
741 (or name
742 (read-from-minibuffer
743 (format "Set bookmark (%s): " default)
745 bookmark-minibuffer-read-name-map
746 nil nil default))))
747 (and (string-equal str "") (setq str default))
748 (bookmark-store str (cdr record) parg)
750 ;; Ask for an annotation buffer for this bookmark
751 (if bookmark-use-annotations
752 (bookmark-edit-annotation str)
753 (goto-char bookmark-current-point)))))
755 (defun bookmark-kill-line (&optional newline-too)
756 "Kill from point to end of line.
757 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
758 Does not affect the kill ring."
759 (let ((eol (save-excursion (end-of-line) (point))))
760 (delete-region (point) eol)
761 (if (and newline-too (looking-at "\n"))
762 (delete-char 1))))
765 ;; Defvars to avoid compilation warnings:
766 (defvar bookmark-annotation-name nil
767 "Variable holding the name of the bookmark.
768 This is used in `bookmark-edit-annotation' to record the bookmark
769 whose annotation is being edited.")
772 (defun bookmark-default-annotation-text (bookmark)
773 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
774 "# All lines which start with a '#' will be deleted.\n"
775 "# Type C-c C-c when done.\n#\n"
776 "# Author: " (user-full-name) " <" (user-login-name) "@"
777 (system-name) ">\n"
778 "# Date: " (current-time-string) "\n"))
781 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
782 "Function to return default text to use for a bookmark annotation.
783 It takes one argument, the name of the bookmark, as a string.")
784 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
785 'bookmark-edit-annotation-text-func "23.1")
787 (defvar bookmark-edit-annotation-mode-map
788 (let ((map (make-sparse-keymap)))
789 (set-keymap-parent map text-mode-map)
790 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
791 map)
792 "Keymap for editing an annotation of a bookmark.")
795 (defun bookmark-edit-annotation-mode (bookmark)
796 "Mode for editing the annotation of bookmark BOOKMARK.
797 When you have finished composing, type \\[bookmark-send-annotation].
799 \\{bookmark-edit-annotation-mode-map}"
800 (interactive)
801 (kill-all-local-variables)
802 (make-local-variable 'bookmark-annotation-name)
803 (setq bookmark-annotation-name bookmark)
804 (use-local-map bookmark-edit-annotation-mode-map)
805 (setq major-mode 'bookmark-edit-annotation-mode
806 mode-name "Edit Bookmark Annotation")
807 (insert (funcall bookmark-edit-annotation-text-func bookmark))
808 (let ((annotation (bookmark-get-annotation bookmark)))
809 (if (and annotation (not (string-equal annotation "")))
810 (insert annotation)))
811 (run-mode-hooks 'text-mode-hook))
814 (defun bookmark-send-edited-annotation ()
815 "Use buffer contents as annotation for a bookmark.
816 Lines beginning with `#' are ignored."
817 (interactive)
818 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
819 (error "Not in bookmark-edit-annotation-mode"))
820 (goto-char (point-min))
821 (while (< (point) (point-max))
822 (if (looking-at "^#")
823 (bookmark-kill-line t)
824 (forward-line 1)))
825 ;; Take no chances with text properties.
826 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
827 (bookmark bookmark-annotation-name))
828 (bookmark-set-annotation bookmark annotation)
829 (bookmark-bmenu-surreptitiously-rebuild-list)
830 (goto-char bookmark-current-point))
831 (kill-buffer (current-buffer)))
834 (defun bookmark-edit-annotation (bookmark)
835 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
836 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
837 (bookmark-edit-annotation-mode bookmark))
840 (defun bookmark-insert-current-bookmark ()
841 "Insert this buffer's value of `bookmark-current-bookmark'.
842 Default to file name if it's nil."
843 (interactive)
844 (let ((str
845 (with-current-buffer bookmark-current-buffer
846 (or bookmark-current-bookmark
847 (bookmark-buffer-name)))))
848 (insert str)))
851 (defun bookmark-buffer-name ()
852 "Return the name of the current buffer's file, non-directory."
853 (cond
854 ;; Or are we a file?
855 (buffer-file-name (file-name-nondirectory buffer-file-name))
856 ;; Or are we a directory?
857 ((and (boundp 'dired-directory) dired-directory)
858 (let* ((dirname (if (stringp dired-directory)
859 dired-directory
860 (car dired-directory)))
861 (idx (1- (length dirname))))
862 ;; Strip the trailing slash.
863 (if (= ?/ (aref dirname idx))
864 (file-name-nondirectory (substring dirname 0 idx))
865 ;; Else return the current-buffer
866 (buffer-name (current-buffer)))))
867 ;; If all else fails, use the buffer's name.
869 (buffer-name (current-buffer)))))
872 (defun bookmark-yank-word ()
873 (interactive)
874 ;; get the next word from the buffer and append it to the name of
875 ;; the bookmark currently being set.
876 (let ((string (save-excursion
877 (set-buffer bookmark-current-buffer)
878 (goto-char bookmark-yank-point)
879 (buffer-substring-no-properties
880 (point)
881 (progn
882 (forward-word 1)
883 (setq bookmark-yank-point (point)))))))
884 (insert string)))
886 (defun bookmark-buffer-file-name ()
887 "Return the current buffer's file in a way useful for bookmarks."
888 (cond
889 (buffer-file-name
890 ;; Abbreviate the path, both so it's shorter and so it's more
891 ;; portable. E.g., the user's home dir might be a different
892 ;; path on different machines, but "~/" will still reach it.
893 (abbreviate-file-name buffer-file-name))
894 ((and (boundp 'dired-directory) dired-directory)
895 (if (stringp dired-directory)
896 dired-directory
897 (car dired-directory)))
898 (t (error "Buffer not visiting a file or directory"))))
901 (defun bookmark-maybe-load-default-file ()
902 (and (not bookmarks-already-loaded)
903 (null bookmark-alist)
904 (prog2
905 (and
906 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
907 ;; to be renamed.
908 (file-exists-p (expand-file-name bookmark-old-default-file))
909 (not (file-exists-p (expand-file-name bookmark-default-file)))
910 (rename-file (expand-file-name bookmark-old-default-file)
911 (expand-file-name bookmark-default-file)))
912 ;; return t so the `and' will continue...
915 (file-readable-p (expand-file-name bookmark-default-file))
916 (bookmark-load bookmark-default-file t t)
917 (setq bookmarks-already-loaded t)))
920 (defun bookmark-maybe-sort-alist ()
921 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
922 ;;is non-nil, then return a sorted copy of the alist.
923 (if bookmark-sort-flag
924 (sort (copy-alist bookmark-alist)
925 (function
926 (lambda (x y) (string-lessp (car x) (car y)))))
927 bookmark-alist))
930 (defvar bookmark-after-jump-hook nil
931 "Hook run after `bookmark-jump' jumps to a bookmark.
932 Useful for example to unhide text in `outline-mode'.")
934 ;;;###autoload
935 (defun bookmark-jump (bookmark)
936 "Jump to bookmark BOOKMARK (a point in some file).
937 You may have a problem using this function if the value of variable
938 `bookmark-alist' is nil. If that happens, you need to load in some
939 bookmarks. See help on function `bookmark-load' for more about
940 this.
942 If the file pointed to by BOOKMARK no longer exists, you will be asked
943 if you wish to give the bookmark a new location, and `bookmark-jump'
944 will then jump to the new location, as well as recording it in place
945 of the old one in the permanent bookmark record."
946 (interactive
947 (list (bookmark-completing-read "Jump to bookmark"
948 bookmark-current-bookmark)))
949 (unless bookmark
950 (error "No bookmark specified"))
951 (bookmark-maybe-historicize-string bookmark)
952 (let ((alist (bookmark-jump-noselect bookmark)))
953 (and alist
954 (switch-to-buffer (cadr (assq 'buffer alist)))
955 (goto-char (cadr (assq 'position alist)))
956 (progn (run-hooks 'bookmark-after-jump-hook) t)
957 (if bookmark-automatically-show-annotations
958 ;; if there is an annotation for this bookmark,
959 ;; show it in a buffer.
960 (bookmark-show-annotation bookmark)))))
963 ;;;###autoload
964 (defun bookmark-jump-other-window (bookmark)
965 "Jump to BOOKMARK (a point in some file) in another window.
966 See `bookmark-jump'."
967 (interactive
968 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
969 bookmark-current-bookmark)))
970 (if (> emacs-major-version 21)
971 (list bkm) bkm)))
972 (when bookmark
973 (bookmark-maybe-historicize-string bookmark)
974 (let ((alist (bookmark-jump-noselect bookmark)))
975 (and alist
976 (switch-to-buffer-other-window (cadr (assq 'buffer alist)))
977 (goto-char (cadr (assq 'position alist)))
978 (if bookmark-automatically-show-annotations
979 ;; if there is an annotation for this bookmark,
980 ;; show it in a buffer.
981 (bookmark-show-annotation bookmark))))))
984 (defun bookmark-file-or-variation-thereof (file)
985 "Return FILE (a string) if it exists, or return a reasonable
986 variation of FILE if that exists. Reasonable variations are checked
987 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
988 nor a reasonable variation thereof, then still return FILE if it can
989 be retrieved from a VC backend, else return nil."
990 (if (file-exists-p file)
991 file
993 (progn (require 'info) ; ensure Info-suffix-list is bound
994 (catch 'found
995 (mapc (lambda (elt)
996 (let ((suffixed-file (concat file (car elt))))
997 (if (file-exists-p suffixed-file)
998 (throw 'found suffixed-file))))
999 Info-suffix-list)
1000 nil))
1001 ;; Last possibility: try VC
1002 (if (vc-backend file) file))))
1004 (defun bookmark-jump-noselect (bookmark)
1005 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none."
1006 (let ((found (funcall (or (bookmark-get-handler bookmark)
1007 'bookmark-default-handler)
1008 bookmark)))
1009 (unless found
1010 ;; Else unable to find the marked file, so ask if user wants to
1011 ;; relocate the bookmark, else remind them to consider deletion.
1012 (let ((file (bookmark-get-filename bookmark)))
1013 (when file ;Don't know how to relocate if there's no `file'.
1014 (setq file (expand-file-name file))
1015 (ding)
1016 (if (y-or-n-p (concat (file-name-nondirectory file)
1017 " nonexistent. Relocate \""
1018 bookmark
1019 "\"? "))
1020 (progn
1021 (bookmark-relocate bookmark)
1022 ;; Try again.
1023 (setq found (funcall (or (bookmark-get-handler bookmark)
1024 'bookmark-default-handler)
1025 bookmark)))
1026 (message
1027 "Bookmark not relocated; consider removing it \(%s\)." bookmark)))))
1028 (when found
1029 ;; Added by db.
1030 (setq bookmark-current-bookmark bookmark)
1031 found)))
1033 (defun bookmark-default-handler (str)
1034 ;; Helper for bookmark-jump. STR is a bookmark name, of the sort
1035 ;; accepted by `bookmark-get-bookmark'.
1037 ;; Return an alist '((buffer BUFFER) (position POSITION) ...)
1038 ;; indicating the bookmarked point within the specied buffer. Any
1039 ;; elements not documented here should be ignored.
1040 (bookmark-maybe-load-default-file)
1041 (let* ((file (expand-file-name (bookmark-get-filename str)))
1042 (forward-str (bookmark-get-front-context-string str))
1043 (behind-str (bookmark-get-rear-context-string str))
1044 (place (bookmark-get-position str)))
1045 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1046 ;; but now that Info bookmarks are handled elsewhere it seems that we
1047 ;; should be able to get rid of it. --Stef
1048 (if (setq file (bookmark-file-or-variation-thereof file))
1049 (with-current-buffer (find-file-noselect file)
1050 (goto-char place)
1052 ;; Go searching forward first. Then, if forward-str exists and
1053 ;; was found in the file, we can search backward for behind-str.
1054 ;; Rationale is that if text was inserted between the two in the
1055 ;; file, it's better to be put before it so you can read it,
1056 ;; rather than after and remain perhaps unaware of the changes.
1057 (if forward-str
1058 (if (search-forward forward-str (point-max) t)
1059 (goto-char (match-beginning 0))))
1060 (if behind-str
1061 (if (search-backward behind-str (point-min) t)
1062 (goto-char (match-end 0))))
1063 `((buffer ,(current-buffer)) (position ,(point)))))))
1066 ;;;###autoload
1067 (defun bookmark-relocate (bookmark)
1068 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1069 This makes an already existing bookmark point to that file, instead of
1070 the one it used to point at. Useful when a file has been renamed
1071 after a bookmark was set in it."
1072 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1073 (bookmark-maybe-historicize-string bookmark)
1074 (bookmark-maybe-load-default-file)
1075 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1076 (newloc (expand-file-name
1077 (read-file-name
1078 (format "Relocate %s to: " bookmark)
1079 (file-name-directory bmrk-filename)))))
1080 (bookmark-set-filename bookmark newloc)
1081 (bookmark-bmenu-surreptitiously-rebuild-list)))
1084 ;;;###autoload
1085 (defun bookmark-insert-location (bookmark &optional no-history)
1086 "Insert the name of the file associated with BOOKMARK.
1087 Optional second arg NO-HISTORY means don't record this in the
1088 minibuffer history list `bookmark-history'."
1089 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1090 (or no-history (bookmark-maybe-historicize-string bookmark))
1091 (let ((start (point)))
1092 (prog1
1093 (insert (bookmark-location bookmark)) ; *Return this line*
1094 (if (and (display-color-p) (display-mouse-p))
1095 (add-text-properties
1096 start
1097 (save-excursion (re-search-backward
1098 "[^ \t]")
1099 (1+ (point)))
1100 '(mouse-face highlight
1101 follow-link t
1102 help-echo "mouse-2: go to this bookmark in other window"))))))
1104 ;;;###autoload
1105 (defalias 'bookmark-locate 'bookmark-insert-location)
1107 (defun bookmark-location (bookmark)
1108 "Return the name of the file associated with BOOKMARK."
1109 (bookmark-maybe-load-default-file)
1110 (bookmark-get-filename bookmark))
1113 ;;;###autoload
1114 (defun bookmark-rename (old &optional new)
1115 "Change the name of OLD bookmark to NEW name.
1116 If called from keyboard, prompt for OLD and NEW. If called from
1117 menubar, select OLD from a menu and prompt for NEW.
1119 If called from Lisp, prompt for NEW if only OLD was passed as an
1120 argument. If called with two strings, then no prompting is done. You
1121 must pass at least OLD when calling from Lisp.
1123 While you are entering the new name, consecutive C-w's insert
1124 consecutive words from the text of the buffer into the new bookmark
1125 name."
1126 (interactive (list (bookmark-completing-read "Old bookmark name")))
1127 (bookmark-maybe-historicize-string old)
1128 (bookmark-maybe-load-default-file)
1130 (setq bookmark-current-point (point))
1131 (setq bookmark-yank-point (point))
1132 (setq bookmark-current-buffer (current-buffer))
1133 (let ((newname
1134 (or new ; use second arg, if non-nil
1135 (read-from-minibuffer
1136 "New name: "
1138 (let ((now-map (copy-keymap minibuffer-local-map)))
1139 (define-key now-map "\C-w" 'bookmark-yank-word)
1140 now-map)
1142 'bookmark-history))))
1143 (bookmark-set-name old newname)
1144 (setq bookmark-current-bookmark newname)
1145 (bookmark-bmenu-surreptitiously-rebuild-list)
1146 (setq bookmark-alist-modification-count
1147 (1+ bookmark-alist-modification-count))
1148 (if (bookmark-time-to-save-p)
1149 (bookmark-save))))
1152 ;;;###autoload
1153 (defun bookmark-insert (bookmark)
1154 "Insert the text of the file pointed to by bookmark BOOKMARK.
1155 You may have a problem using this function if the value of variable
1156 `bookmark-alist' is nil. If that happens, you need to load in some
1157 bookmarks. See help on function `bookmark-load' for more about
1158 this."
1159 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1160 (bookmark-maybe-historicize-string bookmark)
1161 (bookmark-maybe-load-default-file)
1162 (let ((orig-point (point))
1163 (str-to-insert
1164 (save-excursion
1165 (set-buffer (cadr (assq 'buffer (bookmark-jump-noselect bookmark))))
1166 (buffer-string))))
1167 (insert str-to-insert)
1168 (push-mark)
1169 (goto-char orig-point)))
1172 ;;;###autoload
1173 (defun bookmark-delete (bookmark &optional batch)
1174 "Delete BOOKMARK from the bookmark list.
1175 Removes only the first instance of a bookmark with that name. If
1176 there are one or more other bookmarks with the same name, they will
1177 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1178 one most recently used in this file, if any\).
1179 Optional second arg BATCH means don't update the bookmark list buffer,
1180 probably because we were called from there."
1181 (interactive
1182 (list (bookmark-completing-read "Delete bookmark"
1183 bookmark-current-bookmark)))
1184 (bookmark-maybe-historicize-string bookmark)
1185 (bookmark-maybe-load-default-file)
1186 (let ((will-go (bookmark-get-bookmark bookmark)))
1187 (setq bookmark-alist (delq will-go bookmark-alist))
1188 ;; Added by db, nil bookmark-current-bookmark if the last
1189 ;; occurrence has been deleted
1190 (or (bookmark-get-bookmark bookmark-current-bookmark)
1191 (setq bookmark-current-bookmark nil)))
1192 ;; Don't rebuild the list
1193 (if batch
1195 (bookmark-bmenu-surreptitiously-rebuild-list)
1196 (setq bookmark-alist-modification-count
1197 (1+ bookmark-alist-modification-count))
1198 (if (bookmark-time-to-save-p)
1199 (bookmark-save))))
1202 (defun bookmark-time-to-save-p (&optional last-time)
1203 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1204 ;; finds out whether it's time to save bookmarks to a file, by
1205 ;; examining the value of variable bookmark-save-flag, and maybe
1206 ;; bookmark-alist-modification-count. Returns t if they should be
1207 ;; saved, nil otherwise. if last-time is non-nil, then this is
1208 ;; being called when emacs is killed.
1209 (cond (last-time
1210 (and (> bookmark-alist-modification-count 0)
1211 bookmark-save-flag))
1212 ((numberp bookmark-save-flag)
1213 (>= bookmark-alist-modification-count bookmark-save-flag))
1215 nil)))
1218 ;;;###autoload
1219 (defun bookmark-write ()
1220 "Write bookmarks to a file (reading the file name with the minibuffer).
1221 Don't use this in Lisp programs; use `bookmark-save' instead."
1222 (interactive)
1223 (bookmark-maybe-load-default-file)
1224 (bookmark-save t))
1227 ;;;###autoload
1228 (defun bookmark-save (&optional parg file)
1229 "Save currently defined bookmarks.
1230 Saves by default in the file defined by the variable
1231 `bookmark-default-file'. With a prefix arg, save it in file FILE
1232 \(second argument\).
1234 If you are calling this from Lisp, the two arguments are PARG and
1235 FILE, and if you just want it to write to the default file, then
1236 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1237 instead. If you pass in one argument, and it is non-nil, then the
1238 user will be interactively queried for a file to save in.
1240 When you want to load in the bookmarks from a file, use
1241 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1242 for a file, defaulting to the file defined by variable
1243 `bookmark-default-file'."
1244 (interactive "P")
1245 (bookmark-maybe-load-default-file)
1246 (cond
1247 ((and (null parg) (null file))
1248 ;;whether interactive or not, write to default file
1249 (bookmark-write-file bookmark-default-file))
1250 ((and (null parg) file)
1251 ;;whether interactive or not, write to given file
1252 (bookmark-write-file file))
1253 ((and parg (not file))
1254 ;;have been called interactively w/ prefix arg
1255 (let ((file (read-file-name "File to save bookmarks in: ")))
1256 (bookmark-write-file file)))
1257 (t ; someone called us with prefix-arg *and* a file, so just write to file
1258 (bookmark-write-file file)))
1259 ;; signal that we have synced the bookmark file by setting this to
1260 ;; 0. If there was an error at any point before, it will not get
1261 ;; set, which is what we want.
1262 (setq bookmark-alist-modification-count 0))
1266 (defun bookmark-write-file (file)
1267 (save-excursion
1268 (save-window-excursion
1269 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1270 (set-buffer (get-buffer-create " *Bookmarks*"))
1271 (goto-char (point-min))
1272 (delete-region (point-min) (point-max))
1273 (let ((print-length nil)
1274 (print-level nil))
1275 (bookmark-insert-file-format-version-stamp)
1276 (pp bookmark-alist (current-buffer))
1277 (let ((version-control
1278 (cond
1279 ((null bookmark-version-control) nil)
1280 ((eq 'never bookmark-version-control) 'never)
1281 ((eq 'nospecial bookmark-version-control) version-control)
1283 t))))
1284 (condition-case nil
1285 (write-region (point-min) (point-max) file)
1286 (file-error (message "Can't write %s" file)))
1287 (kill-buffer (current-buffer))
1288 (bookmark-maybe-message
1289 "Saving bookmarks to file %s...done" file))))))
1292 (defun bookmark-import-new-list (new-list)
1293 ;; Walk over the new list, adding each individual bookmark
1294 ;; carefully. "Carefully" means checking against the existing
1295 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1296 ;; as necessary.
1297 (let ((lst new-list)
1298 (names (bookmark-all-names)))
1299 (while lst
1300 (let* ((full-record (car lst)))
1301 (bookmark-maybe-rename full-record names)
1302 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1303 (setq names (cons (bookmark-name-from-full-record full-record) names))
1304 (setq lst (cdr lst))))))
1307 (defun bookmark-maybe-rename (full-record names)
1308 ;; just a helper for bookmark-import-new-list; it is only for
1309 ;; readability that this is not inlined.
1311 ;; Once this has found a free name, it sets full-record to that
1312 ;; name.
1313 (let ((found-name (bookmark-name-from-full-record full-record)))
1314 (if (member found-name names)
1315 ;; We've got a conflict, so generate a new name
1316 (let ((count 2)
1317 (new-name found-name))
1318 (while (member new-name names)
1319 (setq new-name (concat found-name (format "<%d>" count)))
1320 (setq count (1+ count)))
1321 (bookmark-set-name full-record new-name)))))
1324 ;;;###autoload
1325 (defun bookmark-load (file &optional overwrite no-msg)
1326 "Load bookmarks from FILE (which must be in bookmark format).
1327 Appends loaded bookmarks to the front of the list of bookmarks. If
1328 optional second argument OVERWRITE is non-nil, existing bookmarks are
1329 destroyed. Optional third arg NO-MSG means don't display any messages
1330 while loading.
1332 If you load a file that doesn't contain a proper bookmark alist, you
1333 will corrupt Emacs's bookmark list. Generally, you should only load
1334 in files that were created with the bookmark functions in the first
1335 place. Your own personal bookmark file, `~/.emacs.bmk', is
1336 maintained automatically by Emacs; you shouldn't need to load it
1337 explicitly.
1339 If you load a file containing bookmarks with the same names as
1340 bookmarks already present in your Emacs, the new bookmarks will get
1341 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1342 method buffers use to resolve name collisions."
1343 (interactive
1344 (list (read-file-name
1345 (format "Load bookmarks from: (%s) "
1346 bookmark-default-file)
1347 ;;Default might not be used often,
1348 ;;but there's no better default, and
1349 ;;I guess it's better than none at all.
1350 "~/" bookmark-default-file 'confirm)))
1351 (setq file (expand-file-name file))
1352 (if (file-readable-p file)
1353 (save-excursion
1354 (save-window-excursion
1355 (if (null no-msg)
1356 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1357 (set-buffer (let ((enable-local-variables nil))
1358 (find-file-noselect file)))
1359 (goto-char (point-min))
1360 (bookmark-maybe-upgrade-file-format)
1361 (let ((blist (bookmark-alist-from-buffer)))
1362 (if (listp blist)
1363 (progn
1364 (if overwrite
1365 (progn
1366 (setq bookmark-alist blist)
1367 (setq bookmark-alist-modification-count 0))
1368 ;; else
1369 (bookmark-import-new-list blist)
1370 (setq bookmark-alist-modification-count
1371 (1+ bookmark-alist-modification-count)))
1372 (if (string-equal
1373 (expand-file-name bookmark-default-file)
1374 file)
1375 (setq bookmarks-already-loaded t))
1376 (bookmark-bmenu-surreptitiously-rebuild-list))
1377 (error "Invalid bookmark list in %s" file)))
1378 (kill-buffer (current-buffer)))
1379 (if (null no-msg)
1380 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1381 (error "Cannot read bookmark file %s" file)))
1385 ;;; Code supporting the dired-like bookmark menu. Prefix is
1386 ;;; "bookmark-bmenu" for "buffer-menu":
1389 (defvar bookmark-bmenu-bookmark-column nil)
1392 (defvar bookmark-bmenu-hidden-bookmarks ())
1395 (defvar bookmark-bmenu-mode-map nil)
1398 (if bookmark-bmenu-mode-map
1400 (setq bookmark-bmenu-mode-map (make-keymap))
1401 (suppress-keymap bookmark-bmenu-mode-map t)
1402 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1403 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1404 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1405 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1406 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1407 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1408 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1409 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1410 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1411 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1412 (define-key bookmark-bmenu-mode-map "\C-o"
1413 'bookmark-bmenu-switch-other-window)
1414 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1415 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1416 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1417 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1418 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1419 (define-key bookmark-bmenu-mode-map " " 'next-line)
1420 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1421 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1422 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1423 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1424 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1425 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1426 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1427 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1428 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1429 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1430 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1431 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1432 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1433 (define-key bookmark-bmenu-mode-map [mouse-2]
1434 'bookmark-bmenu-other-window-with-mouse))
1438 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1439 ;; data.
1440 (put 'bookmark-bmenu-mode 'mode-class 'special)
1443 ;; todo: need to display whether or not bookmark exists as a buffer in
1444 ;; flag column.
1446 ;; Format:
1447 ;; FLAGS BOOKMARK [ LOCATION ]
1450 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1451 "Rebuild the Bookmark List if it exists.
1452 Don't affect the buffer ring order."
1453 (if (get-buffer "*Bookmark List*")
1454 (save-excursion
1455 (save-window-excursion
1456 (bookmark-bmenu-list)))))
1459 ;;;###autoload
1460 (defun bookmark-bmenu-list ()
1461 "Display a list of existing bookmarks.
1462 The list is displayed in a buffer named `*Bookmark List*'.
1463 The leftmost column displays a D if the bookmark is flagged for
1464 deletion, or > if it is flagged for displaying."
1465 (interactive)
1466 (bookmark-maybe-load-default-file)
1467 (if (interactive-p)
1468 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1469 (set-buffer (get-buffer-create "*Bookmark List*")))
1470 (let ((inhibit-read-only t))
1471 (erase-buffer)
1472 (insert "% Bookmark\n- --------\n")
1473 (add-text-properties (point-min) (point)
1474 '(font-lock-face bookmark-menu-heading))
1475 (mapc
1476 (lambda (full-record)
1477 ;; if a bookmark has an annotation, prepend a "*"
1478 ;; in the list of bookmarks.
1479 (let ((annotation (bookmark-get-annotation
1480 (bookmark-name-from-full-record full-record))))
1481 (if (and annotation (not (string-equal annotation "")))
1482 (insert " *")
1483 (insert " "))
1484 (let ((start (point)))
1485 (insert (bookmark-name-from-full-record full-record))
1486 (if (and (display-color-p) (display-mouse-p))
1487 (add-text-properties
1488 start
1489 (save-excursion (re-search-backward
1490 "[^ \t]")
1491 (1+ (point)))
1492 '(mouse-face highlight
1493 follow-link t
1494 help-echo "mouse-2: go to this bookmark in other window")))
1495 (insert "\n")
1497 (bookmark-maybe-sort-alist)))
1498 (goto-char (point-min))
1499 (forward-line 2)
1500 (bookmark-bmenu-mode)
1501 (if bookmark-bmenu-toggle-filenames
1502 (bookmark-bmenu-toggle-filenames t)))
1504 ;;;###autoload
1505 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1506 ;;;###autoload
1507 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1511 (defun bookmark-bmenu-mode ()
1512 "Major mode for editing a list of bookmarks.
1513 Each line describes one of the bookmarks in Emacs.
1514 Letters do not insert themselves; instead, they are commands.
1515 Bookmark names preceded by a \"*\" have annotations.
1516 \\<bookmark-bmenu-mode-map>
1517 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1518 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1519 Also show bookmarks marked using m in other windows.
1520 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1521 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1522 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1523 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1524 together with bookmark selected before this one in another window.
1525 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1526 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1527 so the bookmark menu bookmark remains visible in its window.
1528 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1529 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1530 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1531 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1532 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1533 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1534 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1535 With a prefix arg, prompts for a file to save in.
1536 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1537 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1538 With prefix argument, also move up one line.
1539 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1540 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1541 in another buffer.
1542 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1543 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1544 (kill-all-local-variables)
1545 (use-local-map bookmark-bmenu-mode-map)
1546 (setq truncate-lines t)
1547 (setq buffer-read-only t)
1548 (setq major-mode 'bookmark-bmenu-mode)
1549 (setq mode-name "Bookmark Menu")
1550 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1553 (defun bookmark-bmenu-toggle-filenames (&optional show)
1554 "Toggle whether filenames are shown in the bookmark list.
1555 Optional argument SHOW means show them unconditionally."
1556 (interactive)
1557 (cond
1558 (show
1559 (setq bookmark-bmenu-toggle-filenames nil)
1560 (bookmark-bmenu-show-filenames)
1561 (setq bookmark-bmenu-toggle-filenames t))
1562 (bookmark-bmenu-toggle-filenames
1563 (bookmark-bmenu-hide-filenames)
1564 (setq bookmark-bmenu-toggle-filenames nil))
1566 (bookmark-bmenu-show-filenames)
1567 (setq bookmark-bmenu-toggle-filenames t))))
1570 (defun bookmark-bmenu-show-filenames (&optional force)
1571 (if (and (not force) bookmark-bmenu-toggle-filenames)
1572 nil ;already shown, so do nothing
1573 (save-excursion
1574 (save-window-excursion
1575 (goto-char (point-min))
1576 (forward-line 2)
1577 (setq bookmark-bmenu-hidden-bookmarks ())
1578 (let ((inhibit-read-only t))
1579 (while (< (point) (point-max))
1580 (let ((bmrk (bookmark-bmenu-bookmark)))
1581 (setq bookmark-bmenu-hidden-bookmarks
1582 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1583 (let ((start (save-excursion (end-of-line) (point))))
1584 (move-to-column bookmark-bmenu-file-column t)
1585 ;; Strip off `mouse-face' from the white spaces region.
1586 (if (and (display-color-p) (display-mouse-p))
1587 (remove-text-properties start (point)
1588 '(mouse-face nil help-echo nil))))
1589 (delete-region (point) (progn (end-of-line) (point)))
1590 (insert " ")
1591 ;; Pass the NO-HISTORY arg:
1592 (bookmark-insert-location bmrk t)
1593 (forward-line 1))))))))
1596 (defun bookmark-bmenu-hide-filenames (&optional force)
1597 (if (and (not force) bookmark-bmenu-toggle-filenames)
1598 ;; nothing to hide if above is nil
1599 (save-excursion
1600 (save-window-excursion
1601 (goto-char (point-min))
1602 (forward-line 2)
1603 (setq bookmark-bmenu-hidden-bookmarks
1604 (nreverse bookmark-bmenu-hidden-bookmarks))
1605 (save-excursion
1606 (goto-char (point-min))
1607 (search-forward "Bookmark")
1608 (backward-word 1)
1609 (setq bookmark-bmenu-bookmark-column (current-column)))
1610 (save-excursion
1611 (let ((inhibit-read-only t))
1612 (while bookmark-bmenu-hidden-bookmarks
1613 (move-to-column bookmark-bmenu-bookmark-column t)
1614 (bookmark-kill-line)
1615 (let ((start (point)))
1616 (insert (car bookmark-bmenu-hidden-bookmarks))
1617 (if (and (display-color-p) (display-mouse-p))
1618 (add-text-properties
1619 start
1620 (save-excursion (re-search-backward
1621 "[^ \t]")
1622 (1+ (point)))
1623 '(mouse-face highlight
1624 follow-link t
1625 help-echo
1626 "mouse-2: go to this bookmark in other window"))))
1627 (setq bookmark-bmenu-hidden-bookmarks
1628 (cdr bookmark-bmenu-hidden-bookmarks))
1629 (forward-line 1))))))))
1632 (defun bookmark-bmenu-check-position ()
1633 ;; Returns non-nil if on a line with a bookmark.
1634 ;; (The actual value returned is bookmark-alist).
1635 ;; Else reposition and try again, else return nil.
1636 (cond ((< (count-lines (point-min) (point)) 2)
1637 (goto-char (point-min))
1638 (forward-line 2)
1639 bookmark-alist)
1640 ((and (bolp) (eobp))
1641 (beginning-of-line 0)
1642 bookmark-alist)
1644 bookmark-alist)))
1647 (defun bookmark-bmenu-bookmark ()
1648 ;; return a string which is bookmark of this line.
1649 (if (bookmark-bmenu-check-position)
1650 (save-excursion
1651 (save-window-excursion
1652 (goto-char (point-min))
1653 (search-forward "Bookmark")
1654 (backward-word 1)
1655 (setq bookmark-bmenu-bookmark-column (current-column)))))
1656 (if bookmark-bmenu-toggle-filenames
1657 (bookmark-bmenu-hide-filenames))
1658 (save-excursion
1659 (save-window-excursion
1660 (beginning-of-line)
1661 (forward-char bookmark-bmenu-bookmark-column)
1662 (prog1
1663 (buffer-substring-no-properties (point)
1664 (progn
1665 (end-of-line)
1666 (point)))
1667 ;; well, this is certainly crystal-clear:
1668 (if bookmark-bmenu-toggle-filenames
1669 (bookmark-bmenu-toggle-filenames t))))))
1672 (defun bookmark-show-annotation (bookmark)
1673 "Display the annotation for bookmark named BOOKMARK in a buffer,
1674 if an annotation exists."
1675 (let ((annotation (bookmark-get-annotation bookmark)))
1676 (if (and annotation (not (string-equal annotation "")))
1677 (save-excursion
1678 (let ((old-buf (current-buffer)))
1679 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1680 (delete-region (point-min) (point-max))
1681 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1682 (insert annotation)
1683 (goto-char (point-min))
1684 (pop-to-buffer old-buf))))))
1687 (defun bookmark-show-all-annotations ()
1688 "Display the annotations for all bookmarks in a buffer."
1689 (let ((old-buf (current-buffer)))
1690 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1691 (delete-region (point-min) (point-max))
1692 (mapc
1693 (lambda (full-record)
1694 (let* ((name (bookmark-name-from-full-record full-record))
1695 (ann (bookmark-get-annotation name)))
1696 (insert (concat name ":\n"))
1697 (if (and ann (not (string-equal ann "")))
1698 ;; insert the annotation, indented by 4 spaces.
1699 (progn
1700 (save-excursion (insert ann) (unless (bolp)
1701 (insert "\n")))
1702 (while (< (point) (point-max))
1703 (beginning-of-line) ; paranoia
1704 (insert " ")
1705 (forward-line)
1706 (end-of-line))))))
1707 bookmark-alist)
1708 (goto-char (point-min))
1709 (pop-to-buffer old-buf)))
1712 (defun bookmark-bmenu-mark ()
1713 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1714 (interactive)
1715 (beginning-of-line)
1716 (if (bookmark-bmenu-check-position)
1717 (let ((inhibit-read-only t))
1718 (delete-char 1)
1719 (insert ?>)
1720 (forward-line 1)
1721 (bookmark-bmenu-check-position))))
1724 (defun bookmark-bmenu-select ()
1725 "Select this line's bookmark; also display bookmarks marked with `>'.
1726 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1727 (interactive)
1728 (if (bookmark-bmenu-check-position)
1729 (let ((bmrk (bookmark-bmenu-bookmark))
1730 (menu (current-buffer))
1731 (others ())
1732 tem)
1733 (goto-char (point-min))
1734 (while (re-search-forward "^>" nil t)
1735 (setq tem (bookmark-bmenu-bookmark))
1736 (let ((inhibit-read-only t))
1737 (delete-char -1)
1738 (insert ?\s))
1739 (or (string-equal tem bmrk)
1740 (member tem others)
1741 (setq others (cons tem others))))
1742 (setq others (nreverse others)
1743 tem (/ (1- (frame-height)) (1+ (length others))))
1744 (delete-other-windows)
1745 (bookmark-jump bmrk)
1746 (bury-buffer menu)
1747 (if others
1748 (while others
1749 (split-window nil tem)
1750 (other-window 1)
1751 (bookmark-jump (car others))
1752 (setq others (cdr others)))
1753 (other-window 1)))))
1756 (defun bookmark-bmenu-save (parg)
1757 "Save the current list into a bookmark file.
1758 With a prefix arg, prompts for a file to save them in."
1759 (interactive "P")
1760 (save-excursion
1761 (save-window-excursion
1762 (bookmark-save parg))))
1765 (defun bookmark-bmenu-load ()
1766 "Load the bookmark file and rebuild the bookmark menu-buffer."
1767 (interactive)
1768 (if (bookmark-bmenu-check-position)
1769 (save-excursion
1770 (save-window-excursion
1771 ;; This will call `bookmark-bmenu-list'
1772 (call-interactively 'bookmark-load)))))
1775 (defun bookmark-bmenu-1-window ()
1776 "Select this line's bookmark, alone, in full frame."
1777 (interactive)
1778 (if (bookmark-bmenu-check-position)
1779 (progn
1780 (bookmark-jump (bookmark-bmenu-bookmark))
1781 (bury-buffer (other-buffer))
1782 (delete-other-windows))))
1785 (defun bookmark-bmenu-2-window ()
1786 "Select this line's bookmark, with previous buffer in second window."
1787 (interactive)
1788 (if (bookmark-bmenu-check-position)
1789 (let ((bmrk (bookmark-bmenu-bookmark))
1790 (menu (current-buffer))
1791 (pop-up-windows t))
1792 (delete-other-windows)
1793 (switch-to-buffer (other-buffer))
1794 (let* ((alist (bookmark-jump-noselect bmrk))
1795 (buff (cadr (assq 'buffer alist)))
1796 (pos (cadr (assq 'position alist))))
1797 (pop-to-buffer buff)
1798 (goto-char pos))
1799 (bury-buffer menu))))
1802 (defun bookmark-bmenu-this-window ()
1803 "Select this line's bookmark in this window."
1804 (interactive)
1805 (if (bookmark-bmenu-check-position)
1806 (bookmark-jump (bookmark-bmenu-bookmark))))
1809 (defun bookmark-bmenu-other-window ()
1810 "Select this line's bookmark in other window, leaving bookmark menu visible."
1811 (interactive)
1812 (let ((bookmark (bookmark-bmenu-bookmark)))
1813 (if (bookmark-bmenu-check-position)
1814 (let* ((alist (bookmark-jump-noselect bookmark))
1815 (buff (cadr (assq 'buffer alist)))
1816 (pos (cadr (assq 'position alist))))
1817 (switch-to-buffer-other-window buff)
1818 (goto-char pos)
1819 (set-window-point (get-buffer-window buff) pos)
1820 (bookmark-show-annotation bookmark)))))
1823 (defun bookmark-bmenu-switch-other-window ()
1824 "Make the other window select this line's bookmark.
1825 The current window remains selected."
1826 (interactive)
1827 (let ((bookmark (bookmark-bmenu-bookmark))
1828 (pop-up-windows t)
1829 same-window-buffer-names
1830 same-window-regexps)
1831 (if (bookmark-bmenu-check-position)
1832 (let* ((alist (bookmark-jump-noselect bookmark))
1833 (buff (cadr (assq 'buffer alist)))
1834 (pos (cadr (assq 'position alist))))
1835 (display-buffer buff)
1836 (let ((o-buffer (current-buffer)))
1837 ;; save-excursion won't do
1838 (set-buffer buff)
1839 (goto-char pos)
1840 (set-window-point (get-buffer-window buff) pos)
1841 (set-buffer o-buffer))
1842 (bookmark-show-annotation bookmark)))))
1844 (defun bookmark-bmenu-other-window-with-mouse (event)
1845 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1846 (interactive "e")
1847 (save-excursion
1848 (set-buffer (window-buffer (posn-window (event-end event))))
1849 (save-excursion
1850 (goto-char (posn-point (event-end event)))
1851 (bookmark-bmenu-other-window))))
1854 (defun bookmark-bmenu-show-annotation ()
1855 "Show the annotation for the current bookmark in another window."
1856 (interactive)
1857 (let ((bookmark (bookmark-bmenu-bookmark)))
1858 (if (bookmark-bmenu-check-position)
1859 (bookmark-show-annotation bookmark))))
1862 (defun bookmark-bmenu-show-all-annotations ()
1863 "Show the annotation for all bookmarks in another window."
1864 (interactive)
1865 (bookmark-show-all-annotations))
1868 (defun bookmark-bmenu-edit-annotation ()
1869 "Edit the annotation for the current bookmark in another window."
1870 (interactive)
1871 (let ((bookmark (bookmark-bmenu-bookmark)))
1872 (if (bookmark-bmenu-check-position)
1873 (bookmark-edit-annotation bookmark))))
1876 (defun bookmark-bmenu-unmark (&optional backup)
1877 "Cancel all requested operations on bookmark on this line and move down.
1878 Optional BACKUP means move up."
1879 (interactive "P")
1880 (beginning-of-line)
1881 (if (bookmark-bmenu-check-position)
1882 (progn
1883 (let ((inhibit-read-only t))
1884 (delete-char 1)
1885 ;; any flags to reset according to circumstances? How about a
1886 ;; flag indicating whether this bookmark is being visited?
1887 ;; well, we don't have this now, so maybe later.
1888 (insert " "))
1889 (forward-line (if backup -1 1))
1890 (bookmark-bmenu-check-position))))
1893 (defun bookmark-bmenu-backup-unmark ()
1894 "Move up and cancel all requested operations on bookmark on line above."
1895 (interactive)
1896 (forward-line -1)
1897 (if (bookmark-bmenu-check-position)
1898 (progn
1899 (bookmark-bmenu-unmark)
1900 (forward-line -1)
1901 (bookmark-bmenu-check-position))))
1904 (defun bookmark-bmenu-delete ()
1905 "Mark bookmark on this line to be deleted.
1906 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1907 (interactive)
1908 (beginning-of-line)
1909 (if (bookmark-bmenu-check-position)
1910 (let ((inhibit-read-only t))
1911 (delete-char 1)
1912 (insert ?D)
1913 (forward-line 1)
1914 (bookmark-bmenu-check-position))))
1917 (defun bookmark-bmenu-delete-backwards ()
1918 "Mark bookmark on this line to be deleted, then move up one line.
1919 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1920 (interactive)
1921 (bookmark-bmenu-delete)
1922 (forward-line -2)
1923 (if (bookmark-bmenu-check-position)
1924 (forward-line 1))
1925 (bookmark-bmenu-check-position))
1928 (defun bookmark-bmenu-execute-deletions ()
1929 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1930 (interactive)
1931 (message "Deleting bookmarks...")
1932 (let ((hide-em bookmark-bmenu-toggle-filenames)
1933 (o-point (point))
1934 (o-str (save-excursion
1935 (beginning-of-line)
1936 (if (looking-at "^D")
1938 (buffer-substring
1939 (point)
1940 (progn (end-of-line) (point))))))
1941 (o-col (current-column)))
1942 (if hide-em (bookmark-bmenu-hide-filenames))
1943 (setq bookmark-bmenu-toggle-filenames nil)
1944 (goto-char (point-min))
1945 (forward-line 1)
1946 (while (re-search-forward "^D" (point-max) t)
1947 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1948 (bookmark-bmenu-list)
1949 (setq bookmark-bmenu-toggle-filenames hide-em)
1950 (if bookmark-bmenu-toggle-filenames
1951 (bookmark-bmenu-toggle-filenames t))
1952 (if o-str
1953 (progn
1954 (goto-char (point-min))
1955 (search-forward o-str)
1956 (beginning-of-line)
1957 (forward-char o-col))
1958 (goto-char o-point))
1959 (beginning-of-line)
1960 (setq bookmark-alist-modification-count
1961 (1+ bookmark-alist-modification-count))
1962 (if (bookmark-time-to-save-p)
1963 (bookmark-save))
1964 (message "Deleting bookmarks...done")
1968 (defun bookmark-bmenu-rename ()
1969 "Rename bookmark on current line. Prompts for a new name."
1970 (interactive)
1971 (if (bookmark-bmenu-check-position)
1972 (let ((bmrk (bookmark-bmenu-bookmark))
1973 (thispoint (point)))
1974 (bookmark-rename bmrk)
1975 (bookmark-bmenu-list)
1976 (goto-char thispoint))))
1979 (defun bookmark-bmenu-locate ()
1980 "Display location of this bookmark. Displays in the minibuffer."
1981 (interactive)
1982 (if (bookmark-bmenu-check-position)
1983 (let ((bmrk (bookmark-bmenu-bookmark)))
1984 (message "%s" (bookmark-location bmrk)))))
1986 (defun bookmark-bmenu-relocate ()
1987 "Change the file path of the bookmark on the current line,
1988 prompting with completion for the new path."
1989 (interactive)
1990 (if (bookmark-bmenu-check-position)
1991 (let ((bmrk (bookmark-bmenu-bookmark))
1992 (thispoint (point)))
1993 (bookmark-relocate bmrk)
1994 (goto-char thispoint))))
1997 ;;; Menu bar stuff. Prefix is "bookmark-menu".
1999 (defun bookmark-menu-popup-paned-menu (event name entries)
2000 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2001 That is, ENTRIES is a list of strings which appear as the choices
2002 in the menu.
2003 The number of panes depends on the number of entries.
2004 The visible entries are truncated to `bookmark-menu-length', but the
2005 strings returned are not."
2006 (let ((f-height (/ (frame-height) 2))
2007 (pane-list nil)
2008 (iter 0))
2009 (while entries
2010 (let (lst
2011 (count 0))
2012 (while (and (< count f-height) entries)
2013 (let ((str (car entries)))
2014 (push (cons
2015 (if (> (length str) bookmark-menu-length)
2016 (substring str 0 bookmark-menu-length)
2017 str)
2018 str)
2019 lst)
2020 (setq entries (cdr entries))
2021 (setq count (1+ count))))
2022 (setq iter (1+ iter))
2023 (push (cons
2024 (format "-*- %s (%d) -*-" name iter)
2025 (nreverse lst))
2026 pane-list)))
2028 ;; Popup the menu and return the string.
2029 (x-popup-menu event (cons (concat "-*- " name " -*-")
2030 (nreverse pane-list)))))
2033 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2034 ;; following works, and for explaining what to do to make it work.
2036 ;; We MUST autoload EACH form used to set up this variable's value, so
2037 ;; that the whole job is done in loaddefs.el.
2039 ;; Emacs menubar stuff.
2041 ;;;###autoload
2042 (defvar menu-bar-bookmark-map
2043 (let ((map (make-sparse-keymap "Bookmark functions")))
2044 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2045 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2046 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2047 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2048 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2049 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2050 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2051 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2052 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2053 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2054 map))
2056 ;;;###autoload
2057 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2059 ;; make bookmarks appear toward the right side of the menu.
2060 (if (boundp 'menu-bar-final-items)
2061 (if menu-bar-final-items
2062 (setq menu-bar-final-items
2063 (cons 'bookmark menu-bar-final-items)))
2064 (setq menu-bar-final-items '(bookmark)))
2066 ;;;; end bookmark menu stuff ;;;;
2069 ;;; Load Hook
2070 (defvar bookmark-load-hook nil
2071 "Hook run at the end of loading bookmark.")
2073 ;;; Exit Hook, called from kill-emacs-hook
2074 (defvar bookmark-exit-hook nil
2075 "Hook run when Emacs exits.")
2077 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2079 (defun bookmark-exit-hook-internal ()
2080 "Save bookmark state, if necessary, at Emacs exit time.
2081 This also runs `bookmark-exit-hook'."
2082 (run-hooks 'bookmark-exit-hook)
2083 (and bookmark-alist
2084 (bookmark-time-to-save-p t)
2085 (bookmark-save)))
2087 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2090 (run-hooks 'bookmark-load-hook)
2092 (provide 'bookmark)
2094 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2095 ;;; bookmark.el ends here