Tweak previous doc fix.
[emacs.git] / lisp / bookmark.el
blobdebfac03140022fdd148501e31e1fb27bfd845b7
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, 2009 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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package is for setting "bookmarks" in files. A bookmark
29 ;; associates a string with a location in a certain file. Thus, you
30 ;; can navigate your way to that location by providing the string.
31 ;; See the "User Variables" section for customizations.
33 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
34 ;; then implementing the bookmark-current-bookmark idea. He even
35 ;; sent *patches*, bless his soul...
37 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
38 ;; fixing and improving bookmark-time-to-save-p.
40 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
41 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
42 ;; and the menu-bar).
44 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
45 ;; suggestions and the code to implement them (like
46 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
47 ;; stuff).
49 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
50 ;; for his eminently sensible suggestion to separate bookmark-jump
51 ;; into bookmark-jump and bookmark-jump-noselect, which made many
52 ;; other things cleaner as well.
54 ;; Thanks to Roland McGrath for encouragement and help with defining
55 ;; autoloads on the menu-bar.
57 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
58 ;; values in bookmark-jump and bookmark-set. Everybody please keep
59 ;; all the keystrokes they save thereby and send them to him at the
60 ;; end of each year :-) (No, seriously, thanks Jonathan!)
62 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
63 ;; thinking up the annotations feature and implementing it so well.
65 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
66 ;; <olstad@msc.edu>.
68 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
69 ;; reported and fixed.
71 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
73 ;; Enough with the credits already, get on to the good stuff:
75 ;; FAVORITE CHINESE RESTAURANT:
76 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
77 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
79 ;;; Code:
81 (require 'pp)
82 (eval-when-compile (require 'cl))
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom bookmark-save-flag t
108 "Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "Non-nil means show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'bookmark)
200 (defface bookmark-menu-heading
201 '((t (:inherit font-lock-type-face)))
202 "Face used to highlight the heading in bookmark menu buffers."
203 :group 'bookmark
204 :version "22.1")
207 ;;; No user-serviceable parts beyond this point.
209 ;; Added for lucid emacs compatibility, db
210 (or (fboundp 'defalias) (fset 'defalias 'fset))
212 ;; suggested for lucid compatibility by david hughes:
213 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
216 ;;; Keymap stuff:
218 ;; Set up these bindings dumping time *only*;
219 ;; if the user alters them, don't override the user when loading bookmark.el.
221 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
222 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
223 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
225 ;;;###autoload
226 (defvar bookmark-map
227 (let ((map (make-sparse-keymap)))
228 ;; Read the help on all of these functions for details...
229 (define-key map "x" 'bookmark-set)
230 (define-key map "m" 'bookmark-set) ;"m"ark
231 (define-key map "j" 'bookmark-jump)
232 (define-key map "g" 'bookmark-jump) ;"g"o
233 (define-key map "o" 'bookmark-jump-other-window)
234 (define-key map "i" 'bookmark-insert)
235 (define-key map "e" 'edit-bookmarks)
236 (define-key map "f" 'bookmark-insert-location) ;"f"ind
237 (define-key map "r" 'bookmark-rename)
238 (define-key map "d" 'bookmark-delete)
239 (define-key map "l" 'bookmark-load)
240 (define-key map "w" 'bookmark-write)
241 (define-key map "s" 'bookmark-save)
242 map)
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
249 ;;;###autoload (fset 'bookmark-map bookmark-map)
252 ;;; Core variables and data structures:
253 (defvar bookmark-alist ()
254 "Association list of bookmarks and their records.
255 You probably don't want to change the value of this alist yourself;
256 instead, let the various bookmark functions do it for you.
258 The format of the alist is
260 \(BOOKMARK1 BOOKMARK2 ...\)
262 where each BOOKMARK is of the form
264 (NAME PARAM-ALIST) or (NAME . PARAM-ALIST)
266 where the first form is the old deprecated one and the second is
267 the new favored one. PARAM-ALIST is typically of the form:
269 ((filename . FILE)
270 (front-context-string . FRONT-STR)
271 (rear-context-string . REAR-STR)
272 (position . POS)
273 (annotation . ANNOTATION)))")
276 (defvar bookmarks-already-loaded nil)
279 ;; more stuff added by db.
281 (defvar bookmark-current-bookmark nil
282 "Name of bookmark most recently used in the current file.
283 It is buffer local, used to make moving a bookmark forward
284 through a file easier.")
286 (make-variable-buffer-local 'bookmark-current-bookmark)
289 (defvar bookmark-alist-modification-count 0
290 "Number of modifications to bookmark list since it was last saved.")
293 (defvar bookmark-search-size 16
294 "Length of the context strings recorded on either side of a bookmark.")
297 (defvar bookmark-current-point 0)
298 (defvar bookmark-yank-point 0)
299 (defvar bookmark-current-buffer nil)
301 (defvar Info-suffix-list)
303 ;; Helper functions.
305 ;; Only functions on this page and the next one (file formats) need to
306 ;; know anything about the format of bookmark-alist entries.
307 ;; Everyone else should go through them.
310 (defun bookmark-name-from-full-record (full-record)
311 "Return name of FULL-RECORD \(an alist element instead of a string\)."
312 (car full-record))
315 (defun bookmark-all-names ()
316 "Return a list of all current bookmark names."
317 (bookmark-maybe-load-default-file)
318 (mapcar 'bookmark-name-from-full-record bookmark-alist))
321 (defun bookmark-get-bookmark (bookmark &optional noerror)
322 "Return the bookmark record corresponding to BOOKMARK.
323 If BOOKMARK is already a bookmark record, just return it,
324 Otherwise look for the corresponding bookmark in `bookmark-alist'."
325 (cond
326 ((consp bookmark) bookmark)
327 ((stringp bookmark)
328 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
329 (unless noerror (error "Invalid bookmark %s" bookmark))))))
332 (defun bookmark-get-bookmark-record (bookmark)
333 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
334 That is, all information but the name."
335 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
336 ;; The bookmark objects can either look like (NAME ALIST) or
337 ;; (NAME . ALIST), so we have to distinguish the two here.
338 (if (and (null (cdr alist)) (consp (caar alist)))
339 (car alist) alist)))
342 (defun bookmark-set-name (bookmark newname)
343 "Set BOOKMARK's name to NEWNAME."
344 (setcar
345 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
346 newname))
348 (defun bookmark-prop-get (bookmark prop)
349 "Return the property PROP of BOOKMARK, or nil if none."
350 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
352 (defun bookmark-prop-set (bookmark prop val)
353 "Set the property PROP of BOOKMARK to VAL."
354 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
355 (if cell
356 (setcdr cell val)
357 (nconc (bookmark-get-bookmark-record bookmark)
358 (list (cons prop val))))))
360 (defun bookmark-get-annotation (bookmark)
361 "Return the annotation of BOOKMARK, or nil if none."
362 (bookmark-prop-get bookmark 'annotation))
364 (defun bookmark-set-annotation (bookmark ann)
365 "Set the annotation of BOOKMARK to ANN."
366 (bookmark-prop-set bookmark 'annotation ann))
369 (defun bookmark-get-filename (bookmark)
370 "Return the full filename of BOOKMARK."
371 (bookmark-prop-get bookmark 'filename))
374 (defun bookmark-set-filename (bookmark filename)
375 "Set the full filename of BOOKMARK to FILENAME."
376 (bookmark-prop-set bookmark 'filename filename))
379 (defun bookmark-get-position (bookmark)
380 "Return the position \(i.e.: point\) of BOOKMARK."
381 (bookmark-prop-get bookmark 'position))
384 (defun bookmark-set-position (bookmark position)
385 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
386 (bookmark-prop-set bookmark 'position position))
389 (defun bookmark-get-front-context-string (bookmark)
390 "Return the front-context-string of BOOKMARK."
391 (bookmark-prop-get bookmark 'front-context-string))
394 (defun bookmark-set-front-context-string (bookmark string)
395 "Set the front-context-string of BOOKMARK to STRING."
396 (bookmark-prop-set bookmark 'front-context-string string))
399 (defun bookmark-get-rear-context-string (bookmark)
400 "Return the rear-context-string of BOOKMARK."
401 (bookmark-prop-get bookmark 'rear-context-string))
404 (defun bookmark-set-rear-context-string (bookmark string)
405 "Set the rear-context-string of BOOKMARK to STRING."
406 (bookmark-prop-set bookmark 'rear-context-string string))
409 (defun bookmark-get-handler (bookmark)
410 (bookmark-prop-get bookmark 'handler))
412 (defvar bookmark-history nil
413 "The history list for bookmark functions.")
416 (defun bookmark-completing-read (prompt &optional default)
417 "Prompting with PROMPT, read a bookmark name in completion.
418 PROMPT will get a \": \" stuck on the end no matter what, so you
419 probably don't want to include one yourself.
420 Optional second arg DEFAULT is a string to return if the user enters
421 the empty string."
422 (bookmark-maybe-load-default-file) ; paranoia
423 (if (listp last-nonmenu-event)
424 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
425 (let* ((completion-ignore-case bookmark-completion-ignore-case)
426 (default default)
427 (prompt (if default
428 (concat prompt (format " (%s): " default))
429 (concat prompt ": ")))
430 (str
431 (completing-read prompt
432 bookmark-alist
436 'bookmark-history)))
437 (if (string-equal "" str) default str))))
440 (defmacro bookmark-maybe-historicize-string (string)
441 "Put STRING into the bookmark prompt history, if caller non-interactive.
442 We need this because sometimes bookmark functions are invoked from
443 menus, so `completing-read' never gets a chance to set `bookmark-history'."
444 `(or
445 (interactive-p)
446 (setq bookmark-history (cons ,string bookmark-history))))
448 (defvar bookmark-make-record-function 'bookmark-make-record-default
449 "A function that should be called to create a bookmark record.
450 Modes may set this variable buffer-locally to enable bookmarking of
451 locations that should be treated specially, such as Info nodes,
452 news posts, images, pdf documents, etc.
454 The function will be called with no arguments.
455 It should signal a user error if it is unable to construct a record for
456 the current location.
458 The returned record should be a cons cell of the form (NAME . ALIST)
459 where ALIST is as described in `bookmark-alist' and may typically contain
460 a special cons (handler . SOME-FUNCTION) which sets the handler function
461 that should be used to open this bookmark instead of
462 `bookmark-default-handler'. The handler should follow the same calling
463 convention as the one used by `bookmark-default-handler'.
465 NAME is a suggested name for the constructed bookmark. It can be nil
466 in which case a default heuristic will be used. The function can also
467 equivalently just return ALIST without NAME.")
469 (defun bookmark-make-record ()
470 "Return a new bookmark record (NAME . ALIST) for the current location."
471 (let ((record (funcall bookmark-make-record-function)))
472 ;; Set up default name.
473 (if (stringp (car record))
474 ;; The function already provided a default name.
475 record
476 (if (car record) (push nil record))
477 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
478 record)))
480 (defun bookmark-store (name alist no-overwrite)
481 "Store the bookmark NAME with data ALIST.
482 If NO-OVERWRITE is non-nil and another bookmark of the same name already
483 exists in `bookmark-alist', record the new bookmark without throwing away the
484 old one."
485 (bookmark-maybe-load-default-file)
486 (let ((stripped-name (copy-sequence name)))
487 (or (featurep 'xemacs)
488 ;; XEmacs's `set-text-properties' doesn't work on
489 ;; free-standing strings, apparently.
490 (set-text-properties 0 (length stripped-name) nil stripped-name))
491 (if (and (not no-overwrite)
492 (bookmark-get-bookmark stripped-name 'noerror))
493 ;; already existing bookmark under that name and
494 ;; no prefix arg means just overwrite old bookmark
495 ;; Use the new (NAME . ALIST) format.
496 (setcdr (bookmark-get-bookmark stripped-name) alist)
498 ;; otherwise just cons it onto the front (either the bookmark
499 ;; doesn't exist already, or there is no prefix arg. In either
500 ;; case, we want the new bookmark consed onto the alist...)
502 (push (cons stripped-name alist) bookmark-alist))
504 ;; Added by db
505 (setq bookmark-current-bookmark stripped-name)
506 (setq bookmark-alist-modification-count
507 (1+ bookmark-alist-modification-count))
508 (if (bookmark-time-to-save-p)
509 (bookmark-save))
511 (setq bookmark-current-bookmark stripped-name)
512 (bookmark-bmenu-surreptitiously-rebuild-list)))
514 (defun bookmark-make-record-default (&optional point-only)
515 "Return the record describing the location of a new bookmark.
516 Must be at the correct position in the buffer in which the bookmark is
517 being set.
518 If POINT-ONLY is non-nil, then only return the subset of the
519 record that pertains to the location within the buffer."
520 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
521 (front-context-string
522 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
523 (buffer-substring-no-properties
524 (point)
525 (+ (point) bookmark-search-size))
526 nil))
527 (rear-context-string
528 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
529 (buffer-substring-no-properties
530 (point)
531 (- (point) bookmark-search-size))
532 nil))
533 (position . ,(point))))
536 ;;; File format stuff
538 ;; The OLD format of the bookmark-alist was:
540 ;; ((BOOKMARK-NAME . (FILENAME
541 ;; STRING-IN-FRONT
542 ;; STRING-BEHIND
543 ;; POINT))
544 ;; ...)
546 ;; The NEW format of the bookmark-alist is:
548 ;; ((BOOKMARK-NAME (filename . FILENAME)
549 ;; (front-context-string . STRING-IN-FRONT)
550 ;; (rear-context-string . STRING-BEHIND)
551 ;; (position . POINT)
552 ;; (annotation . ANNOTATION)
553 ;; (whatever . VALUE)
554 ;; ...
555 ;; ))
556 ;; ...)
559 ;; I switched to using an internal as well as external alist because I
560 ;; felt that would be a more flexible framework in which to add
561 ;; features. It means that the order in which values appear doesn't
562 ;; matter, and it means that arbitrary values can be added without
563 ;; risk of interfering with existing ones.
565 ;; BOOKMARK-NAME is the string the user gives the bookmark and
566 ;; accesses it by from then on.
568 ;; FILENAME is the location of the file in which the bookmark is set.
570 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
571 ;; context in front of the point at which the bookmark is set.
573 ;; STRING-BEHIND is the same thing, but after the point.
575 ;; The context strings exist so that modifications to a file don't
576 ;; necessarily cause a bookmark's position to be invalidated.
577 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
578 ;; case the file has changed since the bookmark was set. It will
579 ;; attempt to place the user before the changes, if there were any.
580 ;; ANNOTATION is the annotation for the bookmark; it may not exist
581 ;; (for backward compatibility), be nil (no annotation), or be a
582 ;; string.
585 (defconst bookmark-file-format-version 1
586 "The current version of the format used by bookmark files.
587 You should never need to change this.")
590 (defconst bookmark-end-of-version-stamp-marker
591 "-*- End Of Bookmark File Format Version Stamp -*-\n"
592 "This string marks the end of the version stamp in a bookmark file.")
595 (defun bookmark-alist-from-buffer ()
596 "Return a `bookmark-alist' (in any format) from the current buffer.
597 The buffer must of course contain bookmark format information.
598 Does not care from where in the buffer it is called, and does not
599 affect point."
600 (save-excursion
601 (goto-char (point-min))
602 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
603 (read (current-buffer))
604 ;; Else we're dealing with format version 0
605 (if (search-forward "(" nil t)
606 (progn
607 (forward-char -1)
608 (read (current-buffer)))
609 ;; Else no hope of getting information here.
610 (error "Not bookmark format")))))
613 (defun bookmark-upgrade-version-0-alist (old-list)
614 "Upgrade a version 0 alist OLD-LIST to the current version."
615 (mapcar
616 (lambda (bookmark)
617 (let* ((name (car bookmark))
618 (record (car (cdr bookmark)))
619 (filename (nth 0 record))
620 (front-str (nth 1 record))
621 (rear-str (nth 2 record))
622 (position (nth 3 record))
623 (ann (nth 4 record)))
624 (list
625 name
626 `((filename . ,filename)
627 (front-context-string . ,(or front-str ""))
628 (rear-context-string . ,(or rear-str ""))
629 (position . ,position)
630 (annotation . ,ann)))))
631 old-list))
634 (defun bookmark-upgrade-file-format-from-0 ()
635 "Upgrade a bookmark file of format 0 (the original format) to format 1.
636 This expects to be called from `point-min' in a bookmark file."
637 (message "Upgrading bookmark format from 0 to %d..."
638 bookmark-file-format-version)
639 (let* ((old-list (bookmark-alist-from-buffer))
640 (new-list (bookmark-upgrade-version-0-alist old-list)))
641 (delete-region (point-min) (point-max))
642 (bookmark-insert-file-format-version-stamp)
643 (pp new-list (current-buffer))
644 (save-buffer))
645 (goto-char (point-min))
646 (message "Upgrading bookmark format from 0 to %d...done"
647 bookmark-file-format-version)
651 (defun bookmark-grok-file-format-version ()
652 "Return an integer which is the file-format version of this bookmark file.
653 This expects to be called from `point-min' in a bookmark file."
654 (if (looking-at "^;;;;")
655 (save-excursion
656 (save-match-data
657 (re-search-forward "[0-9]")
658 (forward-char -1)
659 (read (current-buffer))))
660 ;; Else this is format version 0, the original one, which didn't
661 ;; even have version stamps.
665 (defun bookmark-maybe-upgrade-file-format ()
666 "Check the file-format version of this bookmark file.
667 If the version is not up-to-date, upgrade it automatically.
668 This expects to be called from `point-min' in a bookmark file."
669 (let ((version (bookmark-grok-file-format-version)))
670 (cond
671 ((= version bookmark-file-format-version)
672 ) ; home free -- version is current
673 ((= version 0)
674 (bookmark-upgrade-file-format-from-0))
676 (error "Bookmark file format version strangeness")))))
679 (defun bookmark-insert-file-format-version-stamp ()
680 "Insert text indicating current version of bookmark file format."
681 (insert
682 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
683 bookmark-file-format-version))
684 (insert ";;; This format is meant to be slightly human-readable;\n"
685 ";;; nevertheless, you probably don't want to edit it.\n"
686 ";;; "
687 bookmark-end-of-version-stamp-marker))
690 ;;; end file-format stuff
693 ;;; Generic helpers.
695 (defun bookmark-maybe-message (fmt &rest args)
696 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
697 (if (>= baud-rate 9600)
698 (apply 'message fmt args)))
701 ;;; Core code:
703 (defvar bookmark-minibuffer-read-name-map
704 (let ((map (make-sparse-keymap)))
705 (set-keymap-parent map minibuffer-local-map)
706 (define-key map "\C-w" 'bookmark-yank-word)
707 ;; This C-u binding might not be very useful any more now that we
708 ;; provide access to the default via the standard M-n binding.
709 ;; Maybe we should just remove it? --Stef-08
710 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
711 map))
713 ;;;###autoload
714 (defun bookmark-set (&optional name parg)
715 "Set a bookmark named NAME inside a file.
716 If name is nil, then the user will be prompted.
717 With prefix arg, will not overwrite a bookmark that has the same name
718 as NAME if such a bookmark already exists, but instead will \"push\"
719 the new bookmark onto the bookmark alist. Thus the most recently set
720 bookmark with name NAME would be the one in effect at any given time,
721 but the others are still there, should you decide to delete the most
722 recent one.
724 To yank words from the text of the buffer and use them as part of the
725 bookmark name, type C-w while setting a bookmark. Successive C-w's
726 yank successive words.
728 Typing C-u inserts the name of the last bookmark used in the buffer
729 \(as an aid in using a single bookmark name to track your progress
730 through a large file\). If no bookmark was used, then C-u inserts the
731 name of the file being visited.
733 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
734 and it removes only the first instance of a bookmark with that name from
735 the list of bookmarks.\)"
736 (interactive (list nil current-prefix-arg))
737 (let* ((record (bookmark-make-record))
738 (default (car record)))
740 (bookmark-maybe-load-default-file)
742 (setq bookmark-current-point (point))
743 (setq bookmark-yank-point (point))
744 (setq bookmark-current-buffer (current-buffer))
746 (let ((str
747 (or name
748 (read-from-minibuffer
749 (format "Set bookmark (%s): " default)
751 bookmark-minibuffer-read-name-map
752 nil nil default))))
753 (and (string-equal str "") (setq str default))
754 (bookmark-store str (cdr record) parg)
756 ;; Ask for an annotation buffer for this bookmark
757 (if bookmark-use-annotations
758 (bookmark-edit-annotation str)
759 (goto-char bookmark-current-point)))))
761 (defun bookmark-kill-line (&optional newline-too)
762 "Kill from point to end of line.
763 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
764 Does not affect the kill ring."
765 (let ((eol (save-excursion (end-of-line) (point))))
766 (delete-region (point) eol)
767 (if (and newline-too (looking-at "\n"))
768 (delete-char 1))))
771 ;; Defvars to avoid compilation warnings:
772 (defvar bookmark-annotation-name nil
773 "Variable holding the name of the bookmark.
774 This is used in `bookmark-edit-annotation' to record the bookmark
775 whose annotation is being edited.")
778 (defun bookmark-default-annotation-text (bookmark)
779 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
780 "# All lines which start with a '#' will be deleted.\n"
781 "# Type C-c C-c when done.\n#\n"
782 "# Author: " (user-full-name) " <" (user-login-name) "@"
783 (system-name) ">\n"
784 "# Date: " (current-time-string) "\n"))
787 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
788 "Function to return default text to use for a bookmark annotation.
789 It takes one argument, the name of the bookmark, as a string.")
790 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
791 'bookmark-edit-annotation-text-func "23.1")
793 (defvar bookmark-edit-annotation-mode-map
794 (let ((map (make-sparse-keymap)))
795 (set-keymap-parent map text-mode-map)
796 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
797 map)
798 "Keymap for editing an annotation of a bookmark.")
801 (defun bookmark-edit-annotation-mode (bookmark)
802 "Mode for editing the annotation of bookmark BOOKMARK.
803 When you have finished composing, type \\[bookmark-send-annotation].
805 \\{bookmark-edit-annotation-mode-map}"
806 (interactive)
807 (kill-all-local-variables)
808 (make-local-variable 'bookmark-annotation-name)
809 (setq bookmark-annotation-name bookmark)
810 (use-local-map bookmark-edit-annotation-mode-map)
811 (setq major-mode 'bookmark-edit-annotation-mode
812 mode-name "Edit Bookmark Annotation")
813 (insert (funcall bookmark-edit-annotation-text-func bookmark))
814 (let ((annotation (bookmark-get-annotation bookmark)))
815 (if (and annotation (not (string-equal annotation "")))
816 (insert annotation)))
817 (run-mode-hooks 'text-mode-hook))
820 (defun bookmark-send-edited-annotation ()
821 "Use buffer contents as annotation for a bookmark.
822 Lines beginning with `#' are ignored."
823 (interactive)
824 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
825 (error "Not in bookmark-edit-annotation-mode"))
826 (goto-char (point-min))
827 (while (< (point) (point-max))
828 (if (looking-at "^#")
829 (bookmark-kill-line t)
830 (forward-line 1)))
831 ;; Take no chances with text properties.
832 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
833 (bookmark bookmark-annotation-name))
834 (bookmark-set-annotation bookmark annotation)
835 (bookmark-bmenu-surreptitiously-rebuild-list)
836 (goto-char bookmark-current-point))
837 (kill-buffer (current-buffer)))
840 (defun bookmark-edit-annotation (bookmark)
841 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
842 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
843 (bookmark-edit-annotation-mode bookmark))
846 (defun bookmark-insert-current-bookmark ()
847 "Insert this buffer's value of `bookmark-current-bookmark'.
848 Default to file name if it's nil."
849 (interactive)
850 (let ((str
851 (with-current-buffer bookmark-current-buffer
852 (or bookmark-current-bookmark
853 (bookmark-buffer-name)))))
854 (insert str)))
857 (defun bookmark-buffer-name ()
858 "Return the name of the current buffer's file, non-directory."
859 (cond
860 ;; Or are we a file?
861 (buffer-file-name (file-name-nondirectory buffer-file-name))
862 ;; Or are we a directory?
863 ((and (boundp 'dired-directory) dired-directory)
864 (let* ((dirname (if (stringp dired-directory)
865 dired-directory
866 (car dired-directory)))
867 (idx (1- (length dirname))))
868 ;; Strip the trailing slash.
869 (if (= ?/ (aref dirname idx))
870 (file-name-nondirectory (substring dirname 0 idx))
871 ;; Else return the current-buffer
872 (buffer-name (current-buffer)))))
873 ;; If all else fails, use the buffer's name.
875 (buffer-name (current-buffer)))))
878 (defun bookmark-yank-word ()
879 (interactive)
880 ;; get the next word from the buffer and append it to the name of
881 ;; the bookmark currently being set.
882 (let ((string (with-current-buffer bookmark-current-buffer
883 (goto-char bookmark-yank-point)
884 (buffer-substring-no-properties
885 (point)
886 (progn
887 (forward-word 1)
888 (setq bookmark-yank-point (point)))))))
889 (insert string)))
891 (defun bookmark-buffer-file-name ()
892 "Return the current buffer's file in a way useful for bookmarks."
893 ;; Abbreviate the path, both so it's shorter and so it's more
894 ;; portable. E.g., the user's home dir might be a different
895 ;; path on different machines, but "~/" will still reach it.
896 (abbreviate-file-name
897 (cond
898 (buffer-file-name buffer-file-name)
899 ((and (boundp 'dired-directory) dired-directory)
900 (if (stringp dired-directory)
901 dired-directory
902 (car dired-directory)))
903 (t (error "Buffer not visiting a file or directory")))))
906 (defun bookmark-maybe-load-default-file ()
907 (and (not bookmarks-already-loaded)
908 (null bookmark-alist)
909 (prog2
910 (and
911 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
912 ;; to be renamed.
913 (file-exists-p (expand-file-name bookmark-old-default-file))
914 (not (file-exists-p (expand-file-name bookmark-default-file)))
915 (rename-file (expand-file-name bookmark-old-default-file)
916 (expand-file-name bookmark-default-file)))
917 ;; return t so the `and' will continue...
920 (file-readable-p (expand-file-name bookmark-default-file))
921 (bookmark-load bookmark-default-file t t)
922 (setq bookmarks-already-loaded t)))
925 (defun bookmark-maybe-sort-alist ()
926 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
927 ;;is non-nil, then return a sorted copy of the alist.
928 (if bookmark-sort-flag
929 (sort (copy-alist bookmark-alist)
930 (function
931 (lambda (x y) (string-lessp (car x) (car y)))))
932 bookmark-alist))
935 (defvar bookmark-after-jump-hook nil
936 "Hook run after `bookmark-jump' jumps to a bookmark.
937 Useful for example to unhide text in `outline-mode'.")
939 (defun bookmark--jump-via (bookmark display-function)
940 (bookmark-handle-bookmark bookmark)
941 (save-current-buffer
942 (funcall display-function (current-buffer)))
943 (let ((win (get-buffer-window (current-buffer) 0)))
944 (if win (set-window-point win (point))))
945 ;; FIXME: we used to only run bookmark-after-jump-hook in
946 ;; `bookmark-jump' itself, but in none of the other commands.
947 (run-hooks 'bookmark-after-jump-hook)
948 (if bookmark-automatically-show-annotations
949 ;; if there is an annotation for this bookmark,
950 ;; show it in a buffer.
951 (bookmark-show-annotation bookmark)))
954 ;;;###autoload
955 (defun bookmark-jump (bookmark)
956 "Jump to bookmark BOOKMARK (a point in some file).
957 You may have a problem using this function if the value of variable
958 `bookmark-alist' is nil. If that happens, you need to load in some
959 bookmarks. See help on function `bookmark-load' for more about
960 this.
962 If the file pointed to by BOOKMARK no longer exists, you will be asked
963 if you wish to give the bookmark a new location, and `bookmark-jump'
964 will then jump to the new location, as well as recording it in place
965 of the old one in the permanent bookmark record."
966 (interactive
967 (list (bookmark-completing-read "Jump to bookmark"
968 bookmark-current-bookmark)))
969 (unless bookmark
970 (error "No bookmark specified"))
971 (bookmark-maybe-historicize-string bookmark)
972 (bookmark--jump-via bookmark 'switch-to-buffer))
975 ;;;###autoload
976 (defun bookmark-jump-other-window (bookmark)
977 "Jump to BOOKMARK (a point in some file) in another window.
978 See `bookmark-jump'."
979 (interactive
980 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
981 bookmark-current-bookmark)))
982 (if (> emacs-major-version 21)
983 (list bkm) bkm)))
984 (when bookmark
985 (bookmark-maybe-historicize-string bookmark)
986 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
989 (defun bookmark-file-or-variation-thereof (file)
990 "Return FILE (a string) if it exists, or return a reasonable
991 variation of FILE if that exists. Reasonable variations are checked
992 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
993 nor a reasonable variation thereof, then still return FILE if it can
994 be retrieved from a VC backend, else return nil."
995 (if (file-exists-p file)
996 file
998 (progn (require 'info) ; ensure Info-suffix-list is bound
999 (catch 'found
1000 (mapc (lambda (elt)
1001 (let ((suffixed-file (concat file (car elt))))
1002 (if (file-exists-p suffixed-file)
1003 (throw 'found suffixed-file))))
1004 Info-suffix-list)
1005 nil))
1006 ;; Last possibility: try VC
1007 (if (vc-backend file) file))))
1009 (defun bookmark-jump-noselect (bookmark)
1010 "Return the location pointed to by the bookmark BOOKMARK.
1011 The return value has the form (BUFFER . POINT).
1013 Note: this function is deprecated and is present for Emacs 22
1014 compatibility only."
1015 (save-excursion
1016 (bookmark-handle-bookmark bookmark)
1017 (cons (current-buffer) (point))))
1019 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1021 (defun bookmark-handle-bookmark (bookmark)
1022 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1023 Changes current buffer and point and returns nil, or signals a `file-error'.
1024 BOOKMARK can be a bookmark record used internally by some other
1025 elisp package, or the name of a bookmark to be found in `bookmark-alist'."
1026 (condition-case err
1027 (funcall (or (bookmark-get-handler bookmark)
1028 'bookmark-default-handler)
1029 (bookmark-get-bookmark bookmark))
1030 (file-error
1031 ;; We were unable to find the marked file, so ask if user wants to
1032 ;; relocate the bookmark, else remind them to consider deletion.
1033 (when (stringp bookmark)
1034 ;; `bookmark' can either be a bookmark name (found in
1035 ;; `bookmark-alist') or a bookmark object. If it's an object, we
1036 ;; assume it's a bookmark used internally by some other package.
1037 (let ((file (bookmark-get-filename bookmark)))
1038 (when file ;Don't know how to relocate if there's no `file'.
1039 (setq file (expand-file-name file))
1040 (ding)
1041 (if (y-or-n-p (concat (file-name-nondirectory file)
1042 " nonexistent. Relocate \""
1043 bookmark
1044 "\"? "))
1045 (progn
1046 (bookmark-relocate bookmark)
1047 ;; Try again.
1048 (funcall (or (bookmark-get-handler bookmark)
1049 'bookmark-default-handler)
1050 (bookmark-get-bookmark bookmark)))
1051 (message
1052 "Bookmark not relocated; consider removing it \(%s\)." bookmark)
1053 (signal (car err) (cdr err))))))))
1054 ;; Added by db.
1055 (when (stringp bookmark)
1056 (setq bookmark-current-bookmark bookmark))
1057 nil)
1059 (defun bookmark-default-handler (bmk)
1060 "Default handler to jump to a particular bookmark location.
1061 BMK is a bookmark record.
1062 Changes current buffer and point and returns nil, or signals a `file-error'."
1063 (let* ((file (bookmark-get-filename bmk))
1064 (buf (bookmark-prop-get bmk 'buffer))
1065 (forward-str (bookmark-get-front-context-string bmk))
1066 (behind-str (bookmark-get-rear-context-string bmk))
1067 (place (bookmark-get-position bmk)))
1068 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1069 ;; but now that Info bookmarks are handled elsewhere it seems that we
1070 ;; should be able to get rid of it. --Stef
1071 (if (not (if buf (buffer-live-p buf)
1072 (setq file (bookmark-file-or-variation-thereof file))))
1073 (signal 'file-error
1074 `("Jumping to bookmark" "No such file or directory"
1075 (bookmark-get-filename bmk)))
1076 (set-buffer (or buf (find-file-noselect file)))
1077 (if place (goto-char place))
1079 ;; Go searching forward first. Then, if forward-str exists and
1080 ;; was found in the file, we can search backward for behind-str.
1081 ;; Rationale is that if text was inserted between the two in the
1082 ;; file, it's better to be put before it so you can read it,
1083 ;; rather than after and remain perhaps unaware of the changes.
1084 (if forward-str
1085 (if (search-forward forward-str (point-max) t)
1086 (goto-char (match-beginning 0))))
1087 (if behind-str
1088 (if (search-backward behind-str (point-min) t)
1089 (goto-char (match-end 0)))))
1090 nil))
1092 ;;;###autoload
1093 (defun bookmark-relocate (bookmark)
1094 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1095 This makes an already existing bookmark point to that file, instead of
1096 the one it used to point at. Useful when a file has been renamed
1097 after a bookmark was set in it."
1098 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1099 (bookmark-maybe-historicize-string bookmark)
1100 (bookmark-maybe-load-default-file)
1101 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1102 (newloc (expand-file-name
1103 (read-file-name
1104 (format "Relocate %s to: " bookmark)
1105 (file-name-directory bmrk-filename)))))
1106 (bookmark-set-filename bookmark newloc)
1107 (setq bookmark-alist-modification-count
1108 (1+ bookmark-alist-modification-count))
1109 (if (bookmark-time-to-save-p)
1110 (bookmark-save))
1111 (bookmark-bmenu-surreptitiously-rebuild-list)))
1114 ;;;###autoload
1115 (defun bookmark-insert-location (bookmark &optional no-history)
1116 "Insert the name of the file associated with BOOKMARK.
1117 Optional second arg NO-HISTORY means don't record this in the
1118 minibuffer history list `bookmark-history'."
1119 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1120 (or no-history (bookmark-maybe-historicize-string bookmark))
1121 (let ((start (point)))
1122 (prog1
1123 (insert (bookmark-location bookmark)) ; *Return this line*
1124 (if (and (display-color-p) (display-mouse-p))
1125 (add-text-properties
1126 start
1127 (save-excursion (re-search-backward
1128 "[^ \t]")
1129 (1+ (point)))
1130 '(mouse-face highlight
1131 follow-link t
1132 help-echo "mouse-2: go to this bookmark in other window"))))))
1134 ;;;###autoload
1135 (defalias 'bookmark-locate 'bookmark-insert-location)
1137 (defun bookmark-location (bookmark)
1138 "Return the name of the file associated with BOOKMARK."
1139 (bookmark-maybe-load-default-file)
1140 (bookmark-get-filename bookmark))
1143 ;;;###autoload
1144 (defun bookmark-rename (old &optional new)
1145 "Change the name of OLD bookmark to NEW name.
1146 If called from keyboard, prompt for OLD and NEW. If called from
1147 menubar, select OLD from a menu and prompt for NEW.
1149 If called from Lisp, prompt for NEW if only OLD was passed as an
1150 argument. If called with two strings, then no prompting is done. You
1151 must pass at least OLD when calling from Lisp.
1153 While you are entering the new name, consecutive C-w's insert
1154 consecutive words from the text of the buffer into the new bookmark
1155 name."
1156 (interactive (list (bookmark-completing-read "Old bookmark name")))
1157 (bookmark-maybe-historicize-string old)
1158 (bookmark-maybe-load-default-file)
1160 (setq bookmark-current-point (point))
1161 (setq bookmark-yank-point (point))
1162 (setq bookmark-current-buffer (current-buffer))
1163 (let ((newname
1164 (or new ; use second arg, if non-nil
1165 (read-from-minibuffer
1166 "New name: "
1168 (let ((now-map (copy-keymap minibuffer-local-map)))
1169 (define-key now-map "\C-w" 'bookmark-yank-word)
1170 now-map)
1172 'bookmark-history))))
1173 (bookmark-set-name old newname)
1174 (setq bookmark-current-bookmark newname)
1175 (bookmark-bmenu-surreptitiously-rebuild-list)
1176 (setq bookmark-alist-modification-count
1177 (1+ bookmark-alist-modification-count))
1178 (if (bookmark-time-to-save-p)
1179 (bookmark-save))))
1182 ;;;###autoload
1183 (defun bookmark-insert (bookmark)
1184 "Insert the text of the file pointed to by bookmark BOOKMARK.
1185 You may have a problem using this function if the value of variable
1186 `bookmark-alist' is nil. If that happens, you need to load in some
1187 bookmarks. See help on function `bookmark-load' for more about
1188 this."
1189 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1190 (bookmark-maybe-historicize-string bookmark)
1191 (bookmark-maybe-load-default-file)
1192 (let ((orig-point (point))
1193 (str-to-insert
1194 (save-current-buffer
1195 (bookmark-handle-bookmark bookmark)
1196 (buffer-string))))
1197 (insert str-to-insert)
1198 (push-mark)
1199 (goto-char orig-point)))
1202 ;;;###autoload
1203 (defun bookmark-delete (bookmark &optional batch)
1204 "Delete BOOKMARK from the bookmark list.
1205 Removes only the first instance of a bookmark with that name. If
1206 there are one or more other bookmarks with the same name, they will
1207 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1208 one most recently used in this file, if any\).
1209 Optional second arg BATCH means don't update the bookmark list buffer,
1210 probably because we were called from there."
1211 (interactive
1212 (list (bookmark-completing-read "Delete bookmark"
1213 bookmark-current-bookmark)))
1214 (bookmark-maybe-historicize-string bookmark)
1215 (bookmark-maybe-load-default-file)
1216 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1217 (setq bookmark-alist (delq will-go bookmark-alist))
1218 ;; Added by db, nil bookmark-current-bookmark if the last
1219 ;; occurrence has been deleted
1220 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1221 (setq bookmark-current-bookmark nil)))
1222 ;; Don't rebuild the list
1223 (if batch
1225 (bookmark-bmenu-surreptitiously-rebuild-list)
1226 (setq bookmark-alist-modification-count
1227 (1+ bookmark-alist-modification-count))
1228 (if (bookmark-time-to-save-p)
1229 (bookmark-save))))
1232 (defun bookmark-time-to-save-p (&optional last-time)
1233 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1234 ;; finds out whether it's time to save bookmarks to a file, by
1235 ;; examining the value of variable bookmark-save-flag, and maybe
1236 ;; bookmark-alist-modification-count. Returns t if they should be
1237 ;; saved, nil otherwise. if last-time is non-nil, then this is
1238 ;; being called when emacs is killed.
1239 (cond (last-time
1240 (and (> bookmark-alist-modification-count 0)
1241 bookmark-save-flag))
1242 ((numberp bookmark-save-flag)
1243 (>= bookmark-alist-modification-count bookmark-save-flag))
1245 nil)))
1248 ;;;###autoload
1249 (defun bookmark-write ()
1250 "Write bookmarks to a file (reading the file name with the minibuffer).
1251 Don't use this in Lisp programs; use `bookmark-save' instead."
1252 (interactive)
1253 (bookmark-maybe-load-default-file)
1254 (bookmark-save t))
1257 ;;;###autoload
1258 (defun bookmark-save (&optional parg file)
1259 "Save currently defined bookmarks.
1260 Saves by default in the file defined by the variable
1261 `bookmark-default-file'. With a prefix arg, save it in file FILE
1262 \(second argument\).
1264 If you are calling this from Lisp, the two arguments are PARG and
1265 FILE, and if you just want it to write to the default file, then
1266 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1267 instead. If you pass in one argument, and it is non-nil, then the
1268 user will be interactively queried for a file to save in.
1270 When you want to load in the bookmarks from a file, use
1271 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1272 for a file, defaulting to the file defined by variable
1273 `bookmark-default-file'."
1274 (interactive "P")
1275 (bookmark-maybe-load-default-file)
1276 (cond
1277 ((and (null parg) (null file))
1278 ;;whether interactive or not, write to default file
1279 (bookmark-write-file bookmark-default-file))
1280 ((and (null parg) file)
1281 ;;whether interactive or not, write to given file
1282 (bookmark-write-file file))
1283 ((and parg (not file))
1284 ;;have been called interactively w/ prefix arg
1285 (let ((file (read-file-name "File to save bookmarks in: ")))
1286 (bookmark-write-file file)))
1287 (t ; someone called us with prefix-arg *and* a file, so just write to file
1288 (bookmark-write-file file)))
1289 ;; signal that we have synced the bookmark file by setting this to
1290 ;; 0. If there was an error at any point before, it will not get
1291 ;; set, which is what we want.
1292 (setq bookmark-alist-modification-count 0))
1296 (defun bookmark-write-file (file)
1297 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1298 (with-current-buffer (get-buffer-create " *Bookmarks*")
1299 (goto-char (point-min))
1300 (delete-region (point-min) (point-max))
1301 (let ((print-length nil)
1302 (print-level nil))
1303 (bookmark-insert-file-format-version-stamp)
1304 (pp bookmark-alist (current-buffer))
1305 (let ((version-control
1306 (cond
1307 ((null bookmark-version-control) nil)
1308 ((eq 'never bookmark-version-control) 'never)
1309 ((eq 'nospecial bookmark-version-control) version-control)
1310 (t t))))
1311 (condition-case nil
1312 (write-region (point-min) (point-max) file)
1313 (file-error (message "Can't write %s" file)))
1314 (kill-buffer (current-buffer))
1315 (bookmark-maybe-message
1316 "Saving bookmarks to file %s...done" file)))))
1319 (defun bookmark-import-new-list (new-list)
1320 ;; Walk over the new list, adding each individual bookmark
1321 ;; carefully. "Carefully" means checking against the existing
1322 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1323 ;; as necessary.
1324 (let ((lst new-list)
1325 (names (bookmark-all-names)))
1326 (while lst
1327 (let* ((full-record (car lst)))
1328 (bookmark-maybe-rename full-record names)
1329 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1330 (setq names (cons (bookmark-name-from-full-record full-record) names))
1331 (setq lst (cdr lst))))))
1334 (defun bookmark-maybe-rename (full-record names)
1335 ;; just a helper for bookmark-import-new-list; it is only for
1336 ;; readability that this is not inlined.
1338 ;; Once this has found a free name, it sets full-record to that
1339 ;; name.
1340 (let ((found-name (bookmark-name-from-full-record full-record)))
1341 (if (member found-name names)
1342 ;; We've got a conflict, so generate a new name
1343 (let ((count 2)
1344 (new-name found-name))
1345 (while (member new-name names)
1346 (setq new-name (concat found-name (format "<%d>" count)))
1347 (setq count (1+ count)))
1348 (bookmark-set-name full-record new-name)))))
1351 ;;;###autoload
1352 (defun bookmark-load (file &optional overwrite no-msg)
1353 "Load bookmarks from FILE (which must be in bookmark format).
1354 Appends loaded bookmarks to the front of the list of bookmarks. If
1355 optional second argument OVERWRITE is non-nil, existing bookmarks are
1356 destroyed. Optional third arg NO-MSG means don't display any messages
1357 while loading.
1359 If you load a file that doesn't contain a proper bookmark alist, you
1360 will corrupt Emacs's bookmark list. Generally, you should only load
1361 in files that were created with the bookmark functions in the first
1362 place. Your own personal bookmark file, `~/.emacs.bmk', is
1363 maintained automatically by Emacs; you shouldn't need to load it
1364 explicitly.
1366 If you load a file containing bookmarks with the same names as
1367 bookmarks already present in your Emacs, the new bookmarks will get
1368 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1369 method buffers use to resolve name collisions."
1370 (interactive
1371 (list (read-file-name
1372 (format "Load bookmarks from: (%s) "
1373 bookmark-default-file)
1374 ;;Default might not be used often,
1375 ;;but there's no better default, and
1376 ;;I guess it's better than none at all.
1377 "~/" bookmark-default-file 'confirm)))
1378 (setq file (expand-file-name file))
1379 (if (not (file-readable-p file))
1380 (error "Cannot read bookmark file %s" file)
1381 (if (null no-msg)
1382 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1383 (with-current-buffer (let ((enable-local-variables nil))
1384 (find-file-noselect file))
1385 (goto-char (point-min))
1386 (bookmark-maybe-upgrade-file-format)
1387 (let ((blist (bookmark-alist-from-buffer)))
1388 (if (listp blist)
1389 (progn
1390 (if overwrite
1391 (progn
1392 (setq bookmark-alist blist)
1393 (setq bookmark-alist-modification-count 0))
1394 ;; else
1395 (bookmark-import-new-list blist)
1396 (setq bookmark-alist-modification-count
1397 (1+ bookmark-alist-modification-count)))
1398 (if (string-equal
1399 (expand-file-name bookmark-default-file)
1400 file)
1401 (setq bookmarks-already-loaded t))
1402 (bookmark-bmenu-surreptitiously-rebuild-list))
1403 (error "Invalid bookmark list in %s" file)))
1404 (kill-buffer (current-buffer)))
1405 (if (null no-msg)
1406 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1410 ;;; Code supporting the dired-like bookmark menu.
1411 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1414 (defvar bookmark-bmenu-bookmark-column nil)
1417 (defvar bookmark-bmenu-hidden-bookmarks ())
1420 (defvar bookmark-bmenu-mode-map nil)
1423 (if bookmark-bmenu-mode-map
1425 (setq bookmark-bmenu-mode-map (make-keymap))
1426 (suppress-keymap bookmark-bmenu-mode-map t)
1427 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1428 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1429 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1430 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1431 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1432 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1433 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1434 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1435 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1436 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1437 (define-key bookmark-bmenu-mode-map "\C-o"
1438 'bookmark-bmenu-switch-other-window)
1439 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1440 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1441 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1442 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1443 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1444 (define-key bookmark-bmenu-mode-map " " 'next-line)
1445 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1446 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1447 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1448 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1449 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1450 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1451 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1452 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1453 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1454 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1455 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1456 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1457 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1458 (define-key bookmark-bmenu-mode-map [mouse-2]
1459 'bookmark-bmenu-other-window-with-mouse))
1463 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1464 ;; data.
1465 (put 'bookmark-bmenu-mode 'mode-class 'special)
1468 ;; todo: need to display whether or not bookmark exists as a buffer in
1469 ;; flag column.
1471 ;; Format:
1472 ;; FLAGS BOOKMARK [ LOCATION ]
1475 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1476 "Rebuild the Bookmark List if it exists.
1477 Don't affect the buffer ring order."
1478 (if (get-buffer "*Bookmark List*")
1479 (save-excursion
1480 (save-window-excursion
1481 (bookmark-bmenu-list)))))
1484 ;;;###autoload
1485 (defun bookmark-bmenu-list ()
1486 "Display a list of existing bookmarks.
1487 The list is displayed in a buffer named `*Bookmark List*'.
1488 The leftmost column displays a D if the bookmark is flagged for
1489 deletion, or > if it is flagged for displaying."
1490 (interactive)
1491 (bookmark-maybe-load-default-file)
1492 (if (interactive-p)
1493 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1494 (set-buffer (get-buffer-create "*Bookmark List*")))
1495 (let ((inhibit-read-only t))
1496 (erase-buffer)
1497 (insert "% Bookmark\n- --------\n")
1498 (add-text-properties (point-min) (point)
1499 '(font-lock-face bookmark-menu-heading))
1500 (mapc
1501 (lambda (full-record)
1502 ;; if a bookmark has an annotation, prepend a "*"
1503 ;; in the list of bookmarks.
1504 (let ((annotation (bookmark-get-annotation
1505 (bookmark-name-from-full-record full-record))))
1506 (if (and annotation (not (string-equal annotation "")))
1507 (insert " *")
1508 (insert " "))
1509 (let ((start (point)))
1510 (insert (bookmark-name-from-full-record full-record))
1511 (if (and (display-color-p) (display-mouse-p))
1512 (add-text-properties
1513 start
1514 (save-excursion (re-search-backward
1515 "[^ \t]")
1516 (1+ (point)))
1517 '(mouse-face highlight
1518 follow-link t
1519 help-echo "mouse-2: go to this bookmark in other window")))
1520 (insert "\n")
1522 (bookmark-maybe-sort-alist)))
1523 (goto-char (point-min))
1524 (forward-line 2)
1525 (bookmark-bmenu-mode)
1526 (if bookmark-bmenu-toggle-filenames
1527 (bookmark-bmenu-toggle-filenames t)))
1529 ;;;###autoload
1530 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1531 ;;;###autoload
1532 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1536 (defun bookmark-bmenu-mode ()
1537 "Major mode for editing a list of bookmarks.
1538 Each line describes one of the bookmarks in Emacs.
1539 Letters do not insert themselves; instead, they are commands.
1540 Bookmark names preceded by a \"*\" have annotations.
1541 \\<bookmark-bmenu-mode-map>
1542 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1543 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1544 Also show bookmarks marked using m in other windows.
1545 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1546 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1547 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1548 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1549 together with bookmark selected before this one in another window.
1550 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1551 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1552 so the bookmark menu bookmark remains visible in its window.
1553 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1554 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1555 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1556 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1557 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1558 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1559 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1560 With a prefix arg, prompts for a file to save in.
1561 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1562 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1563 With prefix argument, also move up one line.
1564 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1565 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1566 in another buffer.
1567 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1568 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1569 (kill-all-local-variables)
1570 (use-local-map bookmark-bmenu-mode-map)
1571 (setq truncate-lines t)
1572 (setq buffer-read-only t)
1573 (setq major-mode 'bookmark-bmenu-mode)
1574 (setq mode-name "Bookmark Menu")
1575 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1578 (defun bookmark-bmenu-toggle-filenames (&optional show)
1579 "Toggle whether filenames are shown in the bookmark list.
1580 Optional argument SHOW means show them unconditionally."
1581 (interactive)
1582 (cond
1583 (show
1584 (setq bookmark-bmenu-toggle-filenames nil)
1585 (bookmark-bmenu-show-filenames)
1586 (setq bookmark-bmenu-toggle-filenames t))
1587 (bookmark-bmenu-toggle-filenames
1588 (bookmark-bmenu-hide-filenames)
1589 (setq bookmark-bmenu-toggle-filenames nil))
1591 (bookmark-bmenu-show-filenames)
1592 (setq bookmark-bmenu-toggle-filenames t))))
1595 (defun bookmark-bmenu-show-filenames (&optional force)
1596 (if (and (not force) bookmark-bmenu-toggle-filenames)
1597 nil ;already shown, so do nothing
1598 (save-excursion
1599 (save-window-excursion
1600 (goto-char (point-min))
1601 (forward-line 2)
1602 (setq bookmark-bmenu-hidden-bookmarks ())
1603 (let ((inhibit-read-only t))
1604 (while (< (point) (point-max))
1605 (let ((bmrk (bookmark-bmenu-bookmark)))
1606 (setq bookmark-bmenu-hidden-bookmarks
1607 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1608 (let ((start (save-excursion (end-of-line) (point))))
1609 (move-to-column bookmark-bmenu-file-column t)
1610 ;; Strip off `mouse-face' from the white spaces region.
1611 (if (and (display-color-p) (display-mouse-p))
1612 (remove-text-properties start (point)
1613 '(mouse-face nil help-echo nil))))
1614 (delete-region (point) (progn (end-of-line) (point)))
1615 (insert " ")
1616 ;; Pass the NO-HISTORY arg:
1617 (bookmark-insert-location bmrk t)
1618 (forward-line 1))))))))
1621 (defun bookmark-bmenu-hide-filenames (&optional force)
1622 (if (and (not force) bookmark-bmenu-toggle-filenames)
1623 ;; nothing to hide if above is nil
1624 (save-excursion
1625 (save-window-excursion
1626 (goto-char (point-min))
1627 (forward-line 2)
1628 (setq bookmark-bmenu-hidden-bookmarks
1629 (nreverse bookmark-bmenu-hidden-bookmarks))
1630 (save-excursion
1631 (goto-char (point-min))
1632 (search-forward "Bookmark")
1633 (backward-word 1)
1634 (setq bookmark-bmenu-bookmark-column (current-column)))
1635 (save-excursion
1636 (let ((inhibit-read-only t))
1637 (while bookmark-bmenu-hidden-bookmarks
1638 (move-to-column bookmark-bmenu-bookmark-column t)
1639 (bookmark-kill-line)
1640 (let ((start (point)))
1641 (insert (car bookmark-bmenu-hidden-bookmarks))
1642 (if (and (display-color-p) (display-mouse-p))
1643 (add-text-properties
1644 start
1645 (save-excursion (re-search-backward
1646 "[^ \t]")
1647 (1+ (point)))
1648 '(mouse-face highlight
1649 follow-link t
1650 help-echo
1651 "mouse-2: go to this bookmark in other window"))))
1652 (setq bookmark-bmenu-hidden-bookmarks
1653 (cdr bookmark-bmenu-hidden-bookmarks))
1654 (forward-line 1))))))))
1657 (defun bookmark-bmenu-check-position ()
1658 ;; Returns non-nil if on a line with a bookmark.
1659 ;; (The actual value returned is bookmark-alist).
1660 ;; Else reposition and try again, else return nil.
1661 (cond ((< (count-lines (point-min) (point)) 2)
1662 (goto-char (point-min))
1663 (forward-line 2)
1664 bookmark-alist)
1665 ((and (bolp) (eobp))
1666 (beginning-of-line 0)
1667 bookmark-alist)
1669 bookmark-alist)))
1672 (defun bookmark-bmenu-bookmark ()
1673 ;; return a string which is bookmark of this line.
1674 (if (bookmark-bmenu-check-position)
1675 (save-excursion
1676 (save-window-excursion
1677 (goto-char (point-min))
1678 (search-forward "Bookmark")
1679 (backward-word 1)
1680 (setq bookmark-bmenu-bookmark-column (current-column)))))
1681 (if bookmark-bmenu-toggle-filenames
1682 (bookmark-bmenu-hide-filenames))
1683 (save-excursion
1684 (save-window-excursion
1685 (beginning-of-line)
1686 (forward-char bookmark-bmenu-bookmark-column)
1687 (prog1
1688 (buffer-substring-no-properties (point)
1689 (progn
1690 (end-of-line)
1691 (point)))
1692 ;; well, this is certainly crystal-clear:
1693 (if bookmark-bmenu-toggle-filenames
1694 (bookmark-bmenu-toggle-filenames t))))))
1697 (defun bookmark-show-annotation (bookmark)
1698 "Display the annotation for bookmark named BOOKMARK in a buffer,
1699 if an annotation exists."
1700 (let ((annotation (bookmark-get-annotation bookmark)))
1701 (if (and annotation (not (string-equal annotation "")))
1702 (save-excursion
1703 (let ((old-buf (current-buffer)))
1704 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1705 (delete-region (point-min) (point-max))
1706 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1707 (insert annotation)
1708 (goto-char (point-min))
1709 (pop-to-buffer old-buf))))))
1712 (defun bookmark-show-all-annotations ()
1713 "Display the annotations for all bookmarks in a buffer."
1714 (let ((old-buf (current-buffer)))
1715 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1716 (delete-region (point-min) (point-max))
1717 (mapc
1718 (lambda (full-record)
1719 (let* ((name (bookmark-name-from-full-record full-record))
1720 (ann (bookmark-get-annotation name)))
1721 (insert (concat name ":\n"))
1722 (if (and ann (not (string-equal ann "")))
1723 ;; insert the annotation, indented by 4 spaces.
1724 (progn
1725 (save-excursion (insert ann) (unless (bolp)
1726 (insert "\n")))
1727 (while (< (point) (point-max))
1728 (beginning-of-line) ; paranoia
1729 (insert " ")
1730 (forward-line)
1731 (end-of-line))))))
1732 bookmark-alist)
1733 (goto-char (point-min))
1734 (pop-to-buffer old-buf)))
1737 (defun bookmark-bmenu-mark ()
1738 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1739 (interactive)
1740 (beginning-of-line)
1741 (if (bookmark-bmenu-check-position)
1742 (let ((inhibit-read-only t))
1743 (delete-char 1)
1744 (insert ?>)
1745 (forward-line 1)
1746 (bookmark-bmenu-check-position))))
1749 (defun bookmark-bmenu-select ()
1750 "Select this line's bookmark; also display bookmarks marked with `>'.
1751 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1752 (interactive)
1753 (if (bookmark-bmenu-check-position)
1754 (let ((bmrk (bookmark-bmenu-bookmark))
1755 (menu (current-buffer))
1756 (others ())
1757 tem)
1758 (goto-char (point-min))
1759 (while (re-search-forward "^>" nil t)
1760 (setq tem (bookmark-bmenu-bookmark))
1761 (let ((inhibit-read-only t))
1762 (delete-char -1)
1763 (insert ?\s))
1764 (or (string-equal tem bmrk)
1765 (member tem others)
1766 (setq others (cons tem others))))
1767 (setq others (nreverse others)
1768 tem (/ (1- (frame-height)) (1+ (length others))))
1769 (delete-other-windows)
1770 (bookmark-jump bmrk)
1771 (bury-buffer menu)
1772 (if others
1773 (while others
1774 (split-window nil tem)
1775 (other-window 1)
1776 (bookmark-jump (car others))
1777 (setq others (cdr others)))
1778 (other-window 1)))))
1781 (defun bookmark-bmenu-save (parg)
1782 "Save the current list into a bookmark file.
1783 With a prefix arg, prompts for a file to save them in."
1784 (interactive "P")
1785 (save-excursion
1786 (save-window-excursion
1787 (bookmark-save parg))))
1790 (defun bookmark-bmenu-load ()
1791 "Load the bookmark file and rebuild the bookmark menu-buffer."
1792 (interactive)
1793 (if (bookmark-bmenu-check-position)
1794 (save-excursion
1795 (save-window-excursion
1796 ;; This will call `bookmark-bmenu-list'
1797 (call-interactively 'bookmark-load)))))
1800 (defun bookmark-bmenu-1-window ()
1801 "Select this line's bookmark, alone, in full frame."
1802 (interactive)
1803 (if (bookmark-bmenu-check-position)
1804 (progn
1805 (bookmark-jump (bookmark-bmenu-bookmark))
1806 (bury-buffer (other-buffer))
1807 (delete-other-windows))))
1810 (defun bookmark-bmenu-2-window ()
1811 "Select this line's bookmark, with previous buffer in second window."
1812 (interactive)
1813 (if (bookmark-bmenu-check-position)
1814 (let ((bmrk (bookmark-bmenu-bookmark))
1815 (menu (current-buffer))
1816 (pop-up-windows t))
1817 (delete-other-windows)
1818 (switch-to-buffer (other-buffer))
1819 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1820 (bookmark--jump-via bmrk 'pop-to-buffer))
1821 (bury-buffer menu))))
1824 (defun bookmark-bmenu-this-window ()
1825 "Select this line's bookmark in this window."
1826 (interactive)
1827 (if (bookmark-bmenu-check-position)
1828 (bookmark-jump (bookmark-bmenu-bookmark))))
1831 (defun bookmark-bmenu-other-window ()
1832 "Select this line's bookmark in other window, leaving bookmark menu visible."
1833 (interactive)
1834 (let ((bookmark (bookmark-bmenu-bookmark)))
1835 (if (bookmark-bmenu-check-position)
1836 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1837 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1840 (defun bookmark-bmenu-switch-other-window ()
1841 "Make the other window select this line's bookmark.
1842 The current window remains selected."
1843 (interactive)
1844 (let ((bookmark (bookmark-bmenu-bookmark))
1845 (pop-up-windows t)
1846 same-window-buffer-names
1847 same-window-regexps)
1848 (if (bookmark-bmenu-check-position)
1849 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1850 (bookmark--jump-via bookmark 'display-buffer)))))
1852 (defun bookmark-bmenu-other-window-with-mouse (event)
1853 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1854 (interactive "e")
1855 (with-current-buffer (window-buffer (posn-window (event-end event)))
1856 (save-excursion
1857 (goto-char (posn-point (event-end event)))
1858 (bookmark-bmenu-other-window))))
1861 (defun bookmark-bmenu-show-annotation ()
1862 "Show the annotation for the current bookmark in another window."
1863 (interactive)
1864 (let ((bookmark (bookmark-bmenu-bookmark)))
1865 (if (bookmark-bmenu-check-position)
1866 (bookmark-show-annotation bookmark))))
1869 (defun bookmark-bmenu-show-all-annotations ()
1870 "Show the annotation for all bookmarks in another window."
1871 (interactive)
1872 (bookmark-show-all-annotations))
1875 (defun bookmark-bmenu-edit-annotation ()
1876 "Edit the annotation for the current bookmark in another window."
1877 (interactive)
1878 (let ((bookmark (bookmark-bmenu-bookmark)))
1879 (if (bookmark-bmenu-check-position)
1880 (bookmark-edit-annotation bookmark))))
1883 (defun bookmark-bmenu-unmark (&optional backup)
1884 "Cancel all requested operations on bookmark on this line and move down.
1885 Optional BACKUP means move up."
1886 (interactive "P")
1887 (beginning-of-line)
1888 (if (bookmark-bmenu-check-position)
1889 (progn
1890 (let ((inhibit-read-only t))
1891 (delete-char 1)
1892 ;; any flags to reset according to circumstances? How about a
1893 ;; flag indicating whether this bookmark is being visited?
1894 ;; well, we don't have this now, so maybe later.
1895 (insert " "))
1896 (forward-line (if backup -1 1))
1897 (bookmark-bmenu-check-position))))
1900 (defun bookmark-bmenu-backup-unmark ()
1901 "Move up and cancel all requested operations on bookmark on line above."
1902 (interactive)
1903 (forward-line -1)
1904 (if (bookmark-bmenu-check-position)
1905 (progn
1906 (bookmark-bmenu-unmark)
1907 (forward-line -1)
1908 (bookmark-bmenu-check-position))))
1911 (defun bookmark-bmenu-delete ()
1912 "Mark bookmark on this line to be deleted.
1913 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1914 (interactive)
1915 (beginning-of-line)
1916 (if (bookmark-bmenu-check-position)
1917 (let ((inhibit-read-only t))
1918 (delete-char 1)
1919 (insert ?D)
1920 (forward-line 1)
1921 (bookmark-bmenu-check-position))))
1924 (defun bookmark-bmenu-delete-backwards ()
1925 "Mark bookmark on this line to be deleted, then move up one line.
1926 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1927 (interactive)
1928 (bookmark-bmenu-delete)
1929 (forward-line -2)
1930 (if (bookmark-bmenu-check-position)
1931 (forward-line 1))
1932 (bookmark-bmenu-check-position))
1935 (defun bookmark-bmenu-execute-deletions ()
1936 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1937 (interactive)
1938 (message "Deleting bookmarks...")
1939 (let ((hide-em bookmark-bmenu-toggle-filenames)
1940 (o-point (point))
1941 (o-str (save-excursion
1942 (beginning-of-line)
1943 (if (looking-at "^D")
1945 (buffer-substring
1946 (point)
1947 (progn (end-of-line) (point))))))
1948 (o-col (current-column)))
1949 (if hide-em (bookmark-bmenu-hide-filenames))
1950 (setq bookmark-bmenu-toggle-filenames nil)
1951 (goto-char (point-min))
1952 (forward-line 1)
1953 (while (re-search-forward "^D" (point-max) t)
1954 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1955 (bookmark-bmenu-list)
1956 (setq bookmark-bmenu-toggle-filenames hide-em)
1957 (if bookmark-bmenu-toggle-filenames
1958 (bookmark-bmenu-toggle-filenames t))
1959 (if o-str
1960 (progn
1961 (goto-char (point-min))
1962 (search-forward o-str)
1963 (beginning-of-line)
1964 (forward-char o-col))
1965 (goto-char o-point))
1966 (beginning-of-line)
1967 (setq bookmark-alist-modification-count
1968 (1+ bookmark-alist-modification-count))
1969 (if (bookmark-time-to-save-p)
1970 (bookmark-save))
1971 (message "Deleting bookmarks...done")
1975 (defun bookmark-bmenu-rename ()
1976 "Rename bookmark on current line. Prompts for a new name."
1977 (interactive)
1978 (if (bookmark-bmenu-check-position)
1979 (let ((bmrk (bookmark-bmenu-bookmark))
1980 (thispoint (point)))
1981 (bookmark-rename bmrk)
1982 (bookmark-bmenu-list)
1983 (goto-char thispoint))))
1986 (defun bookmark-bmenu-locate ()
1987 "Display location of this bookmark. Displays in the minibuffer."
1988 (interactive)
1989 (if (bookmark-bmenu-check-position)
1990 (let ((bmrk (bookmark-bmenu-bookmark)))
1991 (message "%s" (bookmark-location bmrk)))))
1993 (defun bookmark-bmenu-relocate ()
1994 "Change the file path of the bookmark on the current line,
1995 prompting with completion for the new path."
1996 (interactive)
1997 (if (bookmark-bmenu-check-position)
1998 (let ((bmrk (bookmark-bmenu-bookmark))
1999 (thispoint (point)))
2000 (bookmark-relocate bmrk)
2001 (goto-char thispoint))))
2004 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2006 (defun bookmark-menu-popup-paned-menu (event name entries)
2007 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2008 That is, ENTRIES is a list of strings which appear as the choices
2009 in the menu.
2010 The number of panes depends on the number of entries.
2011 The visible entries are truncated to `bookmark-menu-length', but the
2012 strings returned are not."
2013 (let ((f-height (/ (frame-height) 2))
2014 (pane-list nil)
2015 (iter 0))
2016 (while entries
2017 (let (lst
2018 (count 0))
2019 (while (and (< count f-height) entries)
2020 (let ((str (car entries)))
2021 (push (cons
2022 (if (> (length str) bookmark-menu-length)
2023 (substring str 0 bookmark-menu-length)
2024 str)
2025 str)
2026 lst)
2027 (setq entries (cdr entries))
2028 (setq count (1+ count))))
2029 (setq iter (1+ iter))
2030 (push (cons
2031 (format "-*- %s (%d) -*-" name iter)
2032 (nreverse lst))
2033 pane-list)))
2035 ;; Popup the menu and return the string.
2036 (x-popup-menu event (cons (concat "-*- " name " -*-")
2037 (nreverse pane-list)))))
2040 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2041 ;; following works, and for explaining what to do to make it work.
2043 ;; We MUST autoload EACH form used to set up this variable's value, so
2044 ;; that the whole job is done in loaddefs.el.
2046 ;; Emacs menubar stuff.
2048 ;;;###autoload
2049 (defvar menu-bar-bookmark-map
2050 (let ((map (make-sparse-keymap "Bookmark functions")))
2051 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2052 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2053 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2054 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2055 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2056 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2057 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2058 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2059 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2060 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2061 map))
2063 ;;;###autoload
2064 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2066 ;; make bookmarks appear toward the right side of the menu.
2067 (if (boundp 'menu-bar-final-items)
2068 (if menu-bar-final-items
2069 (setq menu-bar-final-items
2070 (cons 'bookmark menu-bar-final-items)))
2071 (setq menu-bar-final-items '(bookmark)))
2073 ;;;; end bookmark menu stuff ;;;;
2076 ;; Load Hook
2077 (defvar bookmark-load-hook nil
2078 "Hook run at the end of loading bookmark.")
2080 ;; Exit Hook, called from kill-emacs-hook
2081 (defvar bookmark-exit-hook nil
2082 "Hook run when Emacs exits.")
2084 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2086 (defun bookmark-exit-hook-internal ()
2087 "Save bookmark state, if necessary, at Emacs exit time.
2088 This also runs `bookmark-exit-hook'."
2089 (run-hooks 'bookmark-exit-hook)
2090 (and bookmark-alist
2091 (bookmark-time-to-save-p t)
2092 (bookmark-save)))
2094 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2096 (defun bookmark-unload-function ()
2097 "Unload the Bookmark library."
2098 (when bookmark-save-flag (bookmark-save))
2099 ;; continue standard unloading
2100 nil)
2103 (run-hooks 'bookmark-load-hook)
2105 (provide 'bookmark)
2107 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2108 ;;; bookmark.el ends here