Add spurious * in docstrings.
[emacs.git] / lisp / bookmark.el
blobada3f0345801725c983a8048fd4dc60f40f30bbf
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)
252 ;;; The annotation maps.
253 (defvar bookmark-read-annotation-mode-map
254 (let ((map (make-sparse-keymap)))
255 (set-keymap-parent map text-mode-map)
256 (define-key map "\C-c\C-c" 'bookmark-send-annotation)
257 map)
258 "Keymap for composing an annotation for a bookmark.")
261 ;;; Core variables and data structures:
262 (defvar bookmark-alist ()
263 "Association list of bookmarks and their records.
264 You probably don't want to change the value of this alist yourself;
265 instead, let the various bookmark functions do it for you.
267 The format of the alist is
269 \(BOOKMARK1 BOOKMARK2 ...\)
271 where each BOOKMARK is typically of the form
273 \(NAME
274 (\(filename . FILE\)
275 \(front-context-string . FRONT-STR\)
276 \(rear-context-string . REAR-STR\)
277 \(position . POS\)
278 \(annotation . ANNOTATION\)\))
280 So the cdr of each bookmark is an alist too.")
283 (defvar bookmarks-already-loaded nil)
286 ;; more stuff added by db.
288 (defvar bookmark-current-bookmark nil
289 "Name of bookmark most recently used in the current file.
290 It is buffer local, used to make moving a bookmark forward
291 through a file easier.")
293 (make-variable-buffer-local 'bookmark-current-bookmark)
296 (defvar bookmark-alist-modification-count 0
297 "Number of modifications to bookmark list since it was last saved.")
300 (defvar bookmark-search-size 16
301 "Length of the context strings recorded on either side of a bookmark.")
304 (defvar bookmark-current-point 0)
305 (defvar bookmark-yank-point 0)
306 (defvar bookmark-current-buffer nil)
308 (defvar Info-current-node)
309 (defvar Info-suffix-list)
311 ;; Helper functions.
313 ;; Only functions on this page and the next one (file formats) need to
314 ;; know anything about the format of bookmark-alist entries.
315 ;; Everyone else should go through them.
318 (defun bookmark-name-from-full-record (full-record)
319 "Return name of FULL-RECORD \(an alist element instead of a string\)."
320 (car full-record))
323 (defun bookmark-all-names ()
324 "Return a list of all current bookmark names."
325 (bookmark-maybe-load-default-file)
326 (mapcar
327 (lambda (full-record)
328 (bookmark-name-from-full-record full-record))
329 bookmark-alist))
332 (defun bookmark-get-bookmark (bookmark)
333 "Return the full entry for BOOKMARK in `bookmark-alist'.
334 If BOOKMARK is not a string, return nil."
335 (when (stringp bookmark)
336 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
339 (defun bookmark-get-bookmark-record (bookmark)
340 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
341 That is, all information but the name."
342 (car (cdr (bookmark-get-bookmark bookmark))))
345 (defun bookmark-set-name (bookmark newname)
346 "Set BOOKMARK's name to NEWNAME."
347 (setcar
348 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
349 newname))
351 (defun bookmark-prop-get (bookmark prop)
352 "Return the property PROP of BOOKMARK, or nil if none."
353 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
355 (defun bookmark-prop-set (bookmark prop val)
356 "Set the property PROP of BOOKMARK to VAL."
357 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
358 (if cell
359 (setcdr cell val)
360 (nconc (bookmark-get-bookmark-record bookmark)
361 (list (cons prop val))))))
363 (defun bookmark-get-annotation (bookmark)
364 "Return the annotation of BOOKMARK, or nil if none."
365 (bookmark-prop-get bookmark 'annotation))
367 (defun bookmark-set-annotation (bookmark ann)
368 "Set the annotation of BOOKMARK to ANN."
369 (bookmark-prop-set bookmark 'annotation ann))
372 (defun bookmark-get-filename (bookmark)
373 "Return the full filename of BOOKMARK."
374 (bookmark-prop-get bookmark 'filename))
377 (defun bookmark-set-filename (bookmark filename)
378 "Set the full filename of BOOKMARK to FILENAME."
379 (bookmark-prop-set bookmark 'filename filename)
380 (setq bookmark-alist-modification-count
381 (1+ bookmark-alist-modification-count))
382 (if (bookmark-time-to-save-p)
383 (bookmark-save)))
386 (defun bookmark-get-position (bookmark)
387 "Return the position \(i.e.: point\) of BOOKMARK."
388 (bookmark-prop-get bookmark 'position))
391 (defun bookmark-set-position (bookmark position)
392 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
393 (bookmark-prop-set bookmark 'position position))
396 (defun bookmark-get-front-context-string (bookmark)
397 "Return the front-context-string of BOOKMARK."
398 (bookmark-prop-get bookmark 'front-context-string))
401 (defun bookmark-set-front-context-string (bookmark string)
402 "Set the front-context-string of BOOKMARK to STRING."
403 (bookmark-prop-set bookmark 'front-context-string string))
406 (defun bookmark-get-rear-context-string (bookmark)
407 "Return the rear-context-string of BOOKMARK."
408 (bookmark-prop-get bookmark 'rear-context-string))
411 (defun bookmark-set-rear-context-string (bookmark string)
412 "Set the rear-context-string of BOOKMARK to STRING."
413 (bookmark-prop-set bookmark 'rear-context-string string))
416 (defun bookmark-get-handler (bookmark)
417 (bookmark-prop-get bookmark 'handler))
419 (defvar bookmark-history nil
420 "The history list for bookmark functions.")
423 (defun bookmark-completing-read (prompt &optional default)
424 "Prompting with PROMPT, read a bookmark name in completion.
425 PROMPT will get a \": \" stuck on the end no matter what, so you
426 probably don't want to include one yourself.
427 Optional second arg DEFAULT is a string to return if the user enters
428 the empty string."
429 (bookmark-maybe-load-default-file) ; paranoia
430 (if (listp last-nonmenu-event)
431 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
432 (let* ((completion-ignore-case bookmark-completion-ignore-case)
433 (default default)
434 (prompt (if default
435 (concat prompt (format " (%s): " default))
436 (concat prompt ": ")))
437 (str
438 (completing-read prompt
439 bookmark-alist
443 'bookmark-history)))
444 (if (string-equal "" str) default str))))
447 (defmacro bookmark-maybe-historicize-string (string)
448 "Put STRING into the bookmark prompt history, if caller non-interactive.
449 We need this because sometimes bookmark functions are invoked from
450 menus, so `completing-read' never gets a chance to set `bookmark-history'."
451 `(or
452 (interactive-p)
453 (setq bookmark-history (cons ,string bookmark-history))))
455 (defvar bookmark-make-name-function nil
456 "A function that should be called to return the name of the bookmark.
457 When called with an argument, the function should return a file
458 name -- or whatever is required to jump to the location. Modes
459 may set this variable buffer-locally to enable a default name to
460 be proposed when calling `bookmark-set'.")
462 (defvar bookmark-make-record-function 'bookmark-make-record-for-text-file
463 "A function that should be called to create a bookmark record.
464 Modes may set this variable buffer-locally to enable bookmarking of
465 locations that should be treated specially, such as Info nodes,
466 news posts, images, pdf documents, etc.
468 The function will be called with no arguments.
469 The returned record may contain a special cons (handler . SOME-FUNCTION)
470 which sets the handler function that should be used to open this
471 bookmark instead of `bookmark-default-handler'. The handler should
472 return an alist like the one that function returns, and (of course)
473 should likewise not select the buffer.")
475 (defun bookmark-make (name &optional annotation overwrite)
476 "Make a bookmark named NAME.
477 Optional second arg ANNOTATION gives it an annotation.
478 Optional third arg OVERWRITE means replace any existing bookmarks with
479 this name."
480 (bookmark-maybe-load-default-file)
481 (let ((stripped-name (copy-sequence name)))
482 (or (featurep 'xemacs)
483 ;; XEmacs's `set-text-properties' doesn't work on
484 ;; free-standing strings, apparently.
485 (set-text-properties 0 (length stripped-name) nil stripped-name))
486 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
487 ;; already existing bookmark under that name and
488 ;; no prefix arg means just overwrite old bookmark
489 (setcdr (bookmark-get-bookmark stripped-name)
490 (list (funcall bookmark-make-record-function)))
492 ;; otherwise just cons it onto the front (either the bookmark
493 ;; doesn't exist already, or there is no prefix arg. In either
494 ;; case, we want the new bookmark consed onto the alist...)
496 (push (list stripped-name
497 (funcall bookmark-make-record-function))
498 bookmark-alist))
500 (when annotation
501 ;; Take no chances with text properties.
502 (set-text-properties 0 (length annotation) nil annotation)
503 (bookmark-prop-set stripped-name 'annotation annotation))
505 ;; Added by db
506 (setq bookmark-current-bookmark stripped-name)
507 (setq bookmark-alist-modification-count
508 (1+ bookmark-alist-modification-count))
509 (if (bookmark-time-to-save-p)
510 (bookmark-save))))
513 (defun bookmark-make-record-for-text-file ()
514 "Return the record describing the location of a new bookmark.
515 Must be at the correct position in the buffer in which the bookmark is
516 being set (this might change someday)."
517 `((filename . ,(bookmark-buffer-file-name))
518 (front-context-string
519 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
520 (buffer-substring-no-properties
521 (point)
522 (+ (point) bookmark-search-size))
523 nil))
524 (rear-context-string
525 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
526 (buffer-substring-no-properties
527 (point)
528 (- (point) bookmark-search-size))
529 nil))
530 (position . ,(point))))
533 ;;; File format stuff
535 ;; The OLD format of the bookmark-alist was:
537 ;; ((bookmark-name (filename
538 ;; string-in-front
539 ;; string-behind
540 ;; point))
541 ;; ...)
543 ;; The NEW format of the bookmark-alist is:
545 ;; ((bookmark-name ((filename . FILENAME)
546 ;; (front-context-string . string-in-front)
547 ;; (rear-context-string . string-behind)
548 ;; (position . POINT)
549 ;; (annotation . annotation)
550 ;; (whatever . VALUE)
551 ;; ...
552 ;; ))
553 ;; ...)
556 ;; I switched to using an internal as well as external alist because I
557 ;; felt that would be a more flexible framework in which to add
558 ;; features. It means that the order in which values appear doesn't
559 ;; matter, and it means that arbitrary values can be added without
560 ;; risk of interfering with existing ones.
562 ;; BOOKMARK-NAME is the string the user gives the bookmark and
563 ;; accesses it by from then on.
565 ;; FILENAME is the location of the file in which the bookmark is set.
567 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
568 ;; context in front of the point at which the bookmark is set.
570 ;; STRING-BEHIND is the same thing, but after the point.
572 ;; The context strings exist so that modifications to a file don't
573 ;; necessarily cause a bookmark's position to be invalidated.
574 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
575 ;; case the file has changed since the bookmark was set. It will
576 ;; attempt to place the user before the changes, if there were any.
577 ;; ANNOTATION is the annotation for the bookmark; it may not exist
578 ;; (for backward compatibility), be nil (no annotation), or be a
579 ;; string.
582 (defconst bookmark-file-format-version 1
583 "The current version of the format used by bookmark files.
584 You should never need to change this.")
587 (defconst bookmark-end-of-version-stamp-marker
588 "-*- End Of Bookmark File Format Version Stamp -*-\n"
589 "This string marks the end of the version stamp in a bookmark file.")
592 (defun bookmark-alist-from-buffer ()
593 "Return a `bookmark-alist' (in any format) from the current buffer.
594 The buffer must of course contain bookmark format information.
595 Does not care from where in the buffer it is called, and does not
596 affect point."
597 (save-excursion
598 (goto-char (point-min))
599 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
600 (read (current-buffer))
601 ;; Else we're dealing with format version 0
602 (if (search-forward "(" nil t)
603 (progn
604 (forward-char -1)
605 (read (current-buffer)))
606 ;; Else no hope of getting information here.
607 (error "Not bookmark format")))))
610 (defun bookmark-upgrade-version-0-alist (old-list)
611 "Upgrade a version 0 alist OLD-LIST to the current version."
612 (mapcar
613 (lambda (bookmark)
614 (let* ((name (car bookmark))
615 (record (car (cdr bookmark)))
616 (filename (nth 0 record))
617 (front-str (nth 1 record))
618 (rear-str (nth 2 record))
619 (position (nth 3 record))
620 (ann (nth 4 record)))
621 (list
622 name
623 `((filename . ,filename)
624 (front-context-string . ,(or front-str ""))
625 (rear-context-string . ,(or rear-str ""))
626 (position . ,position)
627 (annotation . ,ann)))))
628 old-list))
631 (defun bookmark-upgrade-file-format-from-0 ()
632 "Upgrade a bookmark file of format 0 (the original format) to format 1.
633 This expects to be called from `point-min' in a bookmark file."
634 (message "Upgrading bookmark format from 0 to %d..."
635 bookmark-file-format-version)
636 (let* ((old-list (bookmark-alist-from-buffer))
637 (new-list (bookmark-upgrade-version-0-alist old-list)))
638 (delete-region (point-min) (point-max))
639 (bookmark-insert-file-format-version-stamp)
640 (pp new-list (current-buffer))
641 (save-buffer))
642 (goto-char (point-min))
643 (message "Upgrading bookmark format from 0 to %d...done"
644 bookmark-file-format-version)
648 (defun bookmark-grok-file-format-version ()
649 "Return an integer which is the file-format version of this bookmark file.
650 This expects to be called from `point-min' in a bookmark file."
651 (if (looking-at "^;;;;")
652 (save-excursion
653 (save-match-data
654 (re-search-forward "[0-9]")
655 (forward-char -1)
656 (read (current-buffer))))
657 ;; Else this is format version 0, the original one, which didn't
658 ;; even have version stamps.
662 (defun bookmark-maybe-upgrade-file-format ()
663 "Check the file-format version of this bookmark file.
664 If the version is not up-to-date, upgrade it automatically.
665 This expects to be called from `point-min' in a bookmark file."
666 (let ((version (bookmark-grok-file-format-version)))
667 (cond
668 ((= version bookmark-file-format-version)
669 ) ; home free -- version is current
670 ((= version 0)
671 (bookmark-upgrade-file-format-from-0))
673 (error "Bookmark file format version strangeness")))))
676 (defun bookmark-insert-file-format-version-stamp ()
677 "Insert text indicating current version of bookmark file format."
678 (insert
679 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
680 bookmark-file-format-version))
681 (insert ";;; This format is meant to be slightly human-readable;\n"
682 ";;; nevertheless, you probably don't want to edit it.\n"
683 ";;; "
684 bookmark-end-of-version-stamp-marker))
687 ;;; end file-format stuff
690 ;;; Generic helpers.
692 (defun bookmark-maybe-message (fmt &rest args)
693 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
694 (if (>= baud-rate 9600)
695 (apply 'message fmt args)))
698 ;;; Core code:
700 (defvar bookmark-minibuffer-read-name-map
701 (let ((map (make-sparse-keymap)))
702 (set-keymap-parent map minibuffer-local-map)
703 (define-key map "\C-w" 'bookmark-yank-word)
704 ;; This C-u binding might not be very useful any more now that we
705 ;; provide access to the default via the standard M-n binding.
706 ;; Maybe we should just remove it? --Stef-08
707 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
708 map))
710 ;;;###autoload
711 (defun bookmark-set (&optional name parg)
712 "Set a bookmark named NAME inside a file.
713 If name is nil, then the user will be prompted.
714 With prefix arg, will not overwrite a bookmark that has the same name
715 as NAME if such a bookmark already exists, but instead will \"push\"
716 the new bookmark onto the bookmark alist. Thus the most recently set
717 bookmark with name NAME would be the one in effect at any given time,
718 but the others are still there, should you decide to delete the most
719 recent one.
721 To yank words from the text of the buffer and use them as part of the
722 bookmark name, type C-w while setting a bookmark. Successive C-w's
723 yank successive words.
725 Typing C-u inserts the name of the last bookmark used in the buffer
726 \(as an aid in using a single bookmark name to track your progress
727 through a large file\). If no bookmark was used, then C-u inserts the
728 name of the file being visited.
730 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
731 and it removes only the first instance of a bookmark with that name from
732 the list of bookmarks.\)"
733 (interactive (list nil current-prefix-arg))
735 (bookmark-buffer-file-name)
736 (error "Buffer not visiting a file or directory"))
738 (bookmark-maybe-load-default-file)
740 (setq bookmark-current-point (point))
741 (setq bookmark-yank-point (point))
742 (setq bookmark-current-buffer (current-buffer))
744 (let* ((default (or bookmark-current-bookmark
745 (bookmark-buffer-name)))
746 (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 (annotation nil))
754 (and (string-equal str "") (setq str default))
755 ;; Ask for an annotation buffer for this bookmark
756 (if bookmark-use-annotations
757 (bookmark-read-annotation parg str)
758 (bookmark-make str annotation parg)
759 (setq bookmark-current-bookmark str)
760 (bookmark-bmenu-surreptitiously-rebuild-list)
761 (goto-char bookmark-current-point))))
764 (defun bookmark-kill-line (&optional newline-too)
765 "Kill from point to end of line.
766 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
767 Does not affect the kill ring."
768 (let ((eol (save-excursion (end-of-line) (point))))
769 (delete-region (point) eol)
770 (if (and newline-too (looking-at "\n"))
771 (delete-char 1))))
774 ;; Defvars to avoid compilation warnings:
775 (defvar bookmark-annotation-paragraph nil)
776 (defvar bookmark-annotation-name nil)
777 (defvar bookmark-annotation-buffer nil)
778 (defvar bookmark-annotation-file nil)
779 (defvar bookmark-annotation-point nil)
782 (defun bookmark-send-annotation ()
783 "Use buffer contents as the annotation for a bookmark.
784 Exclude lines that begin with `#'.
785 Store the annotation text in the bookmark list with
786 the bookmark (and file, and point) specified in buffer local variables."
787 (interactive)
788 (if (not (eq major-mode 'bookmark-read-annotation-mode))
789 (error "Not in bookmark-read-annotation-mode"))
790 (goto-char (point-min))
791 (while (< (point) (point-max))
792 (if (looking-at "^#")
793 (bookmark-kill-line t)
794 (forward-line 1)))
795 (let ((annotation (buffer-string))
796 (parg bookmark-annotation-paragraph)
797 (bookmark bookmark-annotation-name)
798 (pt bookmark-annotation-point)
799 (buf bookmark-annotation-buffer))
800 ;; for bookmark-make-record-function to work, we need to be
801 ;; in the relevant buffer, at the relevant point.
802 ;; Actually, the bookmark-make-record-function spec should
803 ;; probably be changed to avoid this need. Should I handle the
804 ;; error if a buffer is killed between "C-x r m" and a "C-c C-c"
805 ;; in the annotation buffer?
806 (save-excursion
807 (pop-to-buffer buf)
808 (goto-char pt)
809 (bookmark-make bookmark annotation parg)
810 (setq bookmark-current-bookmark bookmark))
811 (bookmark-bmenu-surreptitiously-rebuild-list)
812 (goto-char bookmark-current-point))
813 (kill-buffer (current-buffer)))
816 (defun bookmark-default-annotation-text (bookmark)
817 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
818 "# All lines which start with a '#' will be deleted.\n"
819 "# Type C-c C-c when done.\n#\n"
820 "# Author: " (user-full-name) " <" (user-login-name) "@"
821 (system-name) ">\n"
822 "# Date: " (current-time-string) "\n"))
825 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
826 "Function to return default text to use for a bookmark annotation.
827 It takes one argument, the name of the bookmark, as a string.")
829 (defun bookmark-read-annotation-mode (buf point parg bookmark)
830 "Mode for composing annotations for a bookmark.
831 Wants BUF, POINT, PARG, and BOOKMARK.
832 When you have finished composing, type \\[bookmark-send-annotation] to send
833 the annotation.
835 \\{bookmark-read-annotation-mode-map}"
836 (interactive)
837 (kill-all-local-variables)
838 (make-local-variable 'bookmark-annotation-paragraph)
839 (make-local-variable 'bookmark-annotation-name)
840 (make-local-variable 'bookmark-annotation-buffer)
841 (make-local-variable 'bookmark-annotation-file)
842 (make-local-variable 'bookmark-annotation-point)
843 (setq bookmark-annotation-paragraph parg)
844 (setq bookmark-annotation-name bookmark)
845 (setq bookmark-annotation-buffer buf)
846 (setq bookmark-annotation-file (buffer-file-name buf))
847 (setq bookmark-annotation-point point)
848 (use-local-map bookmark-read-annotation-mode-map)
849 (setq major-mode 'bookmark-read-annotation-mode)
850 (insert (funcall bookmark-read-annotation-text-func bookmark))
851 (run-mode-hooks 'text-mode-hook))
854 (defun bookmark-read-annotation (parg bookmark)
855 "Pop up a buffer for entering a bookmark annotation.
856 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
857 (let ((buf (current-buffer))
858 (point (point)))
859 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
860 (bookmark-read-annotation-mode buf point parg bookmark)))
863 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
864 "Keymap for editing an annotation of a bookmark.")
867 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
868 'bookmark-send-edited-annotation)
871 (defun bookmark-edit-annotation-mode (bookmark)
872 "Mode for editing the annotation of bookmark BOOKMARK.
873 When you have finished composing, type \\[bookmark-send-annotation].
875 \\{bookmark-edit-annotation-mode-map}"
876 (interactive)
877 (kill-all-local-variables)
878 (make-local-variable 'bookmark-annotation-name)
879 (setq bookmark-annotation-name bookmark)
880 (use-local-map bookmark-edit-annotation-mode-map)
881 (setq major-mode 'bookmark-edit-annotation-mode
882 mode-name "Edit Bookmark Annotation")
883 (insert (funcall bookmark-read-annotation-text-func bookmark))
884 (let ((annotation (bookmark-get-annotation bookmark)))
885 (if (and annotation (not (string-equal annotation "")))
886 (insert annotation)))
887 (run-mode-hooks 'text-mode-hook))
890 (defun bookmark-send-edited-annotation ()
891 "Use buffer contents as annotation for a bookmark.
892 Lines beginning with `#' are ignored."
893 (interactive)
894 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
895 (error "Not in bookmark-edit-annotation-mode"))
896 (goto-char (point-min))
897 (while (< (point) (point-max))
898 (if (looking-at "^#")
899 (bookmark-kill-line t)
900 (forward-line 1)))
901 ;; Take no chances with text properties.
902 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
903 (bookmark bookmark-annotation-name))
904 (bookmark-set-annotation bookmark annotation)
905 (bookmark-bmenu-surreptitiously-rebuild-list)
906 (goto-char bookmark-current-point))
907 (kill-buffer (current-buffer)))
910 (defun bookmark-edit-annotation (bookmark)
911 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
912 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
913 (bookmark-edit-annotation-mode bookmark))
916 (defun bookmark-insert-current-bookmark ()
917 "Insert this buffer's value of `bookmark-current-bookmark'.
918 Default to file name if it's nil."
919 (interactive)
920 (let ((str
921 (with-current-buffer bookmark-current-buffer
922 (or bookmark-current-bookmark
923 (bookmark-buffer-name)))))
924 (insert str)))
927 (defun bookmark-buffer-name ()
928 "Return the name of the current buffer's file, non-directory.
929 In Info, return the current node."
930 (cond
931 ;; Is the mode defining the bookmark buffer name?
932 (bookmark-make-name-function
933 (funcall bookmark-make-name-function))
934 ;; Or are we a file?
935 (buffer-file-name (file-name-nondirectory buffer-file-name))
936 ;; Or are we a directory?
937 ((and (boundp 'dired-directory) dired-directory)
938 (let* ((dirname (if (stringp dired-directory)
939 dired-directory
940 (car dired-directory)))
941 (idx (1- (length dirname))))
942 ;; Strip the trailing slash.
943 (if (= ?/ (aref dirname idx))
944 (file-name-nondirectory (substring dirname 0 idx))
945 ;; Else return the current-buffer
946 (buffer-name (current-buffer)))))
947 ;; If all else fails, use the buffer's name.
949 (buffer-name (current-buffer)))))
952 (defun bookmark-yank-word ()
953 (interactive)
954 ;; get the next word from the buffer and append it to the name of
955 ;; the bookmark currently being set.
956 (let ((string (save-excursion
957 (set-buffer bookmark-current-buffer)
958 (goto-char bookmark-yank-point)
959 (buffer-substring-no-properties
960 (point)
961 (progn
962 (forward-word 1)
963 (setq bookmark-yank-point (point)))))))
964 (insert string)))
967 (defvar Info-current-file)
969 (defun bookmark-buffer-file-name ()
970 "Return the current buffer's file in a way useful for bookmarks.
971 For example, if this is a Info buffer, return the Info file's name."
972 (cond
973 ;; Return the location the handler should jump to
974 ;; E.g. the Info file name for the Info handler
975 (bookmark-make-name-function
976 (funcall bookmark-make-name-function t))
977 (buffer-file-name
978 ;; Abbreviate the path, both so it's shorter and so it's more
979 ;; portable. E.g., the user's home dir might be a different
980 ;; path on different machines, but "~/" will still reach it.
981 (abbreviate-file-name buffer-file-name))
982 ((and (boundp 'dired-directory) dired-directory)
983 (if (stringp dired-directory)
984 dired-directory
985 (car dired-directory)))
986 (t (error "Buffer not visiting a file or directory"))))
989 (defun bookmark-maybe-load-default-file ()
990 (and (not bookmarks-already-loaded)
991 (null bookmark-alist)
992 (prog2
993 (and
994 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
995 ;; to be renamed.
996 (file-exists-p (expand-file-name bookmark-old-default-file))
997 (not (file-exists-p (expand-file-name bookmark-default-file)))
998 (rename-file (expand-file-name bookmark-old-default-file)
999 (expand-file-name bookmark-default-file)))
1000 ;; return t so the `and' will continue...
1003 (file-readable-p (expand-file-name bookmark-default-file))
1004 (bookmark-load bookmark-default-file t t)
1005 (setq bookmarks-already-loaded t)))
1008 (defun bookmark-maybe-sort-alist ()
1009 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1010 ;;is non-nil, then return a sorted copy of the alist.
1011 (if bookmark-sort-flag
1012 (sort (copy-alist bookmark-alist)
1013 (function
1014 (lambda (x y) (string-lessp (car x) (car y)))))
1015 bookmark-alist))
1018 (defvar bookmark-after-jump-hook nil
1019 "Hook run after `bookmark-jump' jumps to a bookmark.
1020 Useful for example to unhide text in `outline-mode'.")
1022 ;;;###autoload
1023 (defun bookmark-jump (bookmark)
1024 "Jump to bookmark BOOKMARK (a point in some file).
1025 You may have a problem using this function if the value of variable
1026 `bookmark-alist' is nil. If that happens, you need to load in some
1027 bookmarks. See help on function `bookmark-load' for more about
1028 this.
1030 If the file pointed to by BOOKMARK no longer exists, you will be asked
1031 if you wish to give the bookmark a new location, and `bookmark-jump'
1032 will then jump to the new location, as well as recording it in place
1033 of the old one in the permanent bookmark record."
1034 (interactive
1035 (list (bookmark-completing-read "Jump to bookmark"
1036 bookmark-current-bookmark)))
1037 (unless bookmark
1038 (error "No bookmark specified"))
1039 (bookmark-maybe-historicize-string bookmark)
1040 (let ((alist (bookmark-jump-noselect bookmark)))
1041 (and alist
1042 (switch-to-buffer (cadr (assq 'buffer alist)))
1043 (goto-char (cadr (assq 'position alist)))
1044 (progn (run-hooks 'bookmark-after-jump-hook) t)
1045 (if bookmark-automatically-show-annotations
1046 ;; if there is an annotation for this bookmark,
1047 ;; show it in a buffer.
1048 (bookmark-show-annotation bookmark)))))
1051 ;;;###autoload
1052 (defun bookmark-jump-other-window (bookmark)
1053 "Jump to BOOKMARK (a point in some file) in another window.
1054 See `bookmark-jump'."
1055 (interactive
1056 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
1057 bookmark-current-bookmark)))
1058 (if (> emacs-major-version 21)
1059 (list bkm) bkm)))
1060 (when bookmark
1061 (bookmark-maybe-historicize-string bookmark)
1062 (let ((alist (bookmark-jump-noselect bookmark)))
1063 (and alist
1064 (switch-to-buffer-other-window (cadr (assq 'buffer alist)))
1065 (goto-char (cadr (assq 'position alist)))
1066 (if bookmark-automatically-show-annotations
1067 ;; if there is an annotation for this bookmark,
1068 ;; show it in a buffer.
1069 (bookmark-show-annotation bookmark))))))
1072 (defun bookmark-file-or-variation-thereof (file)
1073 "Return FILE (a string) if it exists, or return a reasonable
1074 variation of FILE if that exists. Reasonable variations are checked
1075 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1076 nor a reasonable variation thereof, then still return FILE if it can
1077 be retrieved from a VC backend, else return nil."
1078 (if (file-exists-p file)
1079 file
1081 (progn (require 'info) ; ensure Info-suffix-list is bound
1082 (catch 'found
1083 (mapc (lambda (elt)
1084 (let ((suffixed-file (concat file (car elt))))
1085 (if (file-exists-p suffixed-file)
1086 (throw 'found suffixed-file))))
1087 Info-suffix-list)
1088 nil))
1089 ;; Last possibility: try VC
1090 (if (vc-backend file) file))))
1092 (defun bookmark-jump-noselect (bookmark)
1093 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none."
1094 (let ((found (funcall (or (bookmark-get-handler bookmark)
1095 'bookmark-default-handler)
1096 bookmark)))
1097 (unless found
1098 ;; Else unable to find the marked file, so ask if user wants to
1099 ;; relocate the bookmark, else remind them to consider deletion.
1100 (let ((file (bookmark-get-filename bookmark)))
1101 (when file ;Don't know how to relocate if there's no `file'.
1102 (setq file (expand-file-name file))
1103 (ding)
1104 (if (y-or-n-p (concat (file-name-nondirectory file)
1105 " nonexistent. Relocate \""
1106 bookmark
1107 "\"? "))
1108 (progn
1109 (bookmark-relocate bookmark)
1110 ;; Try again.
1111 (setq found (funcall (or (bookmark-get-handler bookmark)
1112 'bookmark-default-handler)
1113 bookmark)))
1114 (message
1115 "Bookmark not relocated; consider removing it \(%s\)." bookmark)))))
1116 (when found
1117 ;; Added by db.
1118 (setq bookmark-current-bookmark bookmark)
1119 found)))
1121 (defun bookmark-default-handler (str)
1122 ;; Helper for bookmark-jump. STR is a bookmark name, of the sort
1123 ;; accepted by `bookmark-get-bookmark'.
1125 ;; Return an alist '((buffer BUFFER) (position POSITION) ...)
1126 ;; indicating the bookmarked point within the specied buffer. Any
1127 ;; elements not documented here should be ignored.
1128 (bookmark-maybe-load-default-file)
1129 (let* ((file (expand-file-name (bookmark-get-filename str)))
1130 (forward-str (bookmark-get-front-context-string str))
1131 (behind-str (bookmark-get-rear-context-string str))
1132 (place (bookmark-get-position str)))
1133 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1134 ;; but now that Info bookmarks are handled elsewhere it seems that we
1135 ;; should be able to get rid of it. --Stef
1136 (if (setq file (bookmark-file-or-variation-thereof file))
1137 (with-current-buffer (find-file-noselect file)
1138 (goto-char place)
1140 ;; Go searching forward first. Then, if forward-str exists and
1141 ;; was found in the file, we can search backward for behind-str.
1142 ;; Rationale is that if text was inserted between the two in the
1143 ;; file, it's better to be put before it so you can read it,
1144 ;; rather than after and remain perhaps unaware of the changes.
1145 (if forward-str
1146 (if (search-forward forward-str (point-max) t)
1147 (goto-char (match-beginning 0))))
1148 (if behind-str
1149 (if (search-backward behind-str (point-min) t)
1150 (goto-char (match-end 0))))
1151 `((buffer ,(current-buffer)) (position ,(point)))))))
1154 ;;;###autoload
1155 (defun bookmark-relocate (bookmark)
1156 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1157 This makes an already existing bookmark point to that file, instead of
1158 the one it used to point at. Useful when a file has been renamed
1159 after a bookmark was set in it."
1160 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1161 (bookmark-maybe-historicize-string bookmark)
1162 (bookmark-maybe-load-default-file)
1163 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1164 (newloc (expand-file-name
1165 (read-file-name
1166 (format "Relocate %s to: " bookmark)
1167 (file-name-directory bmrk-filename)))))
1168 (bookmark-set-filename bookmark newloc)
1169 (bookmark-bmenu-surreptitiously-rebuild-list)))
1172 ;;;###autoload
1173 (defun bookmark-insert-location (bookmark &optional no-history)
1174 "Insert the name of the file associated with BOOKMARK.
1175 Optional second arg NO-HISTORY means don't record this in the
1176 minibuffer history list `bookmark-history'."
1177 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1178 (or no-history (bookmark-maybe-historicize-string bookmark))
1179 (let ((start (point)))
1180 (prog1
1181 (insert (bookmark-location bookmark)) ; *Return this line*
1182 (if (and (display-color-p) (display-mouse-p))
1183 (add-text-properties
1184 start
1185 (save-excursion (re-search-backward
1186 "[^ \t]")
1187 (1+ (point)))
1188 '(mouse-face highlight
1189 follow-link t
1190 help-echo "mouse-2: go to this bookmark in other window"))))))
1192 ;;;###autoload
1193 (defalias 'bookmark-locate 'bookmark-insert-location)
1195 (defun bookmark-location (bookmark)
1196 "Return the name of the file associated with BOOKMARK."
1197 (bookmark-maybe-load-default-file)
1198 (bookmark-get-filename bookmark))
1201 ;;;###autoload
1202 (defun bookmark-rename (old &optional new)
1203 "Change the name of OLD bookmark to NEW name.
1204 If called from keyboard, prompt for OLD and NEW. If called from
1205 menubar, select OLD from a menu and prompt for NEW.
1207 If called from Lisp, prompt for NEW if only OLD was passed as an
1208 argument. If called with two strings, then no prompting is done. You
1209 must pass at least OLD when calling from Lisp.
1211 While you are entering the new name, consecutive C-w's insert
1212 consecutive words from the text of the buffer into the new bookmark
1213 name."
1214 (interactive (list (bookmark-completing-read "Old bookmark name")))
1215 (bookmark-maybe-historicize-string old)
1216 (bookmark-maybe-load-default-file)
1218 (setq bookmark-current-point (point))
1219 (setq bookmark-yank-point (point))
1220 (setq bookmark-current-buffer (current-buffer))
1221 (let ((newname
1222 (or new ; use second arg, if non-nil
1223 (read-from-minibuffer
1224 "New name: "
1226 (let ((now-map (copy-keymap minibuffer-local-map)))
1227 (define-key now-map "\C-w" 'bookmark-yank-word)
1228 now-map)
1230 'bookmark-history))))
1231 (bookmark-set-name old newname)
1232 (setq bookmark-current-bookmark newname)
1233 (bookmark-bmenu-surreptitiously-rebuild-list)
1234 (setq bookmark-alist-modification-count
1235 (1+ bookmark-alist-modification-count))
1236 (if (bookmark-time-to-save-p)
1237 (bookmark-save))))
1240 ;;;###autoload
1241 (defun bookmark-insert (bookmark)
1242 "Insert the text of the file pointed to by bookmark BOOKMARK.
1243 You may have a problem using this function if the value of variable
1244 `bookmark-alist' is nil. If that happens, you need to load in some
1245 bookmarks. See help on function `bookmark-load' for more about
1246 this."
1247 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1248 (bookmark-maybe-historicize-string bookmark)
1249 (bookmark-maybe-load-default-file)
1250 (let ((orig-point (point))
1251 (str-to-insert
1252 (save-excursion
1253 (set-buffer (cadr (assq 'buffer (bookmark-jump-noselect bookmark))))
1254 (buffer-string))))
1255 (insert str-to-insert)
1256 (push-mark)
1257 (goto-char orig-point)))
1260 ;;;###autoload
1261 (defun bookmark-delete (bookmark &optional batch)
1262 "Delete BOOKMARK from the bookmark list.
1263 Removes only the first instance of a bookmark with that name. If
1264 there are one or more other bookmarks with the same name, they will
1265 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1266 one most recently used in this file, if any\).
1267 Optional second arg BATCH means don't update the bookmark list buffer,
1268 probably because we were called from there."
1269 (interactive
1270 (list (bookmark-completing-read "Delete bookmark"
1271 bookmark-current-bookmark)))
1272 (bookmark-maybe-historicize-string bookmark)
1273 (bookmark-maybe-load-default-file)
1274 (let ((will-go (bookmark-get-bookmark bookmark)))
1275 (setq bookmark-alist (delq will-go bookmark-alist))
1276 ;; Added by db, nil bookmark-current-bookmark if the last
1277 ;; occurrence has been deleted
1278 (or (bookmark-get-bookmark bookmark-current-bookmark)
1279 (setq bookmark-current-bookmark nil)))
1280 ;; Don't rebuild the list
1281 (if batch
1283 (bookmark-bmenu-surreptitiously-rebuild-list)
1284 (setq bookmark-alist-modification-count
1285 (1+ bookmark-alist-modification-count))
1286 (if (bookmark-time-to-save-p)
1287 (bookmark-save))))
1290 (defun bookmark-time-to-save-p (&optional last-time)
1291 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1292 ;; finds out whether it's time to save bookmarks to a file, by
1293 ;; examining the value of variable bookmark-save-flag, and maybe
1294 ;; bookmark-alist-modification-count. Returns t if they should be
1295 ;; saved, nil otherwise. if last-time is non-nil, then this is
1296 ;; being called when emacs is killed.
1297 (cond (last-time
1298 (and (> bookmark-alist-modification-count 0)
1299 bookmark-save-flag))
1300 ((numberp bookmark-save-flag)
1301 (>= bookmark-alist-modification-count bookmark-save-flag))
1303 nil)))
1306 ;;;###autoload
1307 (defun bookmark-write ()
1308 "Write bookmarks to a file (reading the file name with the minibuffer).
1309 Don't use this in Lisp programs; use `bookmark-save' instead."
1310 (interactive)
1311 (bookmark-maybe-load-default-file)
1312 (bookmark-save t))
1315 ;;;###autoload
1316 (defun bookmark-save (&optional parg file)
1317 "Save currently defined bookmarks.
1318 Saves by default in the file defined by the variable
1319 `bookmark-default-file'. With a prefix arg, save it in file FILE
1320 \(second argument\).
1322 If you are calling this from Lisp, the two arguments are PARG and
1323 FILE, and if you just want it to write to the default file, then
1324 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1325 instead. If you pass in one argument, and it is non-nil, then the
1326 user will be interactively queried for a file to save in.
1328 When you want to load in the bookmarks from a file, use
1329 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1330 for a file, defaulting to the file defined by variable
1331 `bookmark-default-file'."
1332 (interactive "P")
1333 (bookmark-maybe-load-default-file)
1334 (cond
1335 ((and (null parg) (null file))
1336 ;;whether interactive or not, write to default file
1337 (bookmark-write-file bookmark-default-file))
1338 ((and (null parg) file)
1339 ;;whether interactive or not, write to given file
1340 (bookmark-write-file file))
1341 ((and parg (not file))
1342 ;;have been called interactively w/ prefix arg
1343 (let ((file (read-file-name "File to save bookmarks in: ")))
1344 (bookmark-write-file file)))
1345 (t ; someone called us with prefix-arg *and* a file, so just write to file
1346 (bookmark-write-file file)))
1347 ;; signal that we have synced the bookmark file by setting this to
1348 ;; 0. If there was an error at any point before, it will not get
1349 ;; set, which is what we want.
1350 (setq bookmark-alist-modification-count 0))
1354 (defun bookmark-write-file (file)
1355 (save-excursion
1356 (save-window-excursion
1357 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1358 (set-buffer (get-buffer-create " *Bookmarks*"))
1359 (goto-char (point-min))
1360 (delete-region (point-min) (point-max))
1361 (let ((print-length nil)
1362 (print-level nil))
1363 (bookmark-insert-file-format-version-stamp)
1364 (pp bookmark-alist (current-buffer))
1365 (let ((version-control
1366 (cond
1367 ((null bookmark-version-control) nil)
1368 ((eq 'never bookmark-version-control) 'never)
1369 ((eq 'nospecial bookmark-version-control) version-control)
1371 t))))
1372 (condition-case nil
1373 (write-region (point-min) (point-max) file)
1374 (file-error (message "Can't write %s" file)))
1375 (kill-buffer (current-buffer))
1376 (bookmark-maybe-message
1377 "Saving bookmarks to file %s...done" file))))))
1380 (defun bookmark-import-new-list (new-list)
1381 ;; Walk over the new list, adding each individual bookmark
1382 ;; carefully. "Carefully" means checking against the existing
1383 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1384 ;; as necessary.
1385 (let ((lst new-list)
1386 (names (bookmark-all-names)))
1387 (while lst
1388 (let* ((full-record (car lst)))
1389 (bookmark-maybe-rename full-record names)
1390 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1391 (setq names (cons (bookmark-name-from-full-record full-record) names))
1392 (setq lst (cdr lst))))))
1395 (defun bookmark-maybe-rename (full-record names)
1396 ;; just a helper for bookmark-import-new-list; it is only for
1397 ;; readability that this is not inlined.
1399 ;; Once this has found a free name, it sets full-record to that
1400 ;; name.
1401 (let ((found-name (bookmark-name-from-full-record full-record)))
1402 (if (member found-name names)
1403 ;; We've got a conflict, so generate a new name
1404 (let ((count 2)
1405 (new-name found-name))
1406 (while (member new-name names)
1407 (setq new-name (concat found-name (format "<%d>" count)))
1408 (setq count (1+ count)))
1409 (bookmark-set-name full-record new-name)))))
1412 ;;;###autoload
1413 (defun bookmark-load (file &optional overwrite no-msg)
1414 "Load bookmarks from FILE (which must be in bookmark format).
1415 Appends loaded bookmarks to the front of the list of bookmarks. If
1416 optional second argument OVERWRITE is non-nil, existing bookmarks are
1417 destroyed. Optional third arg NO-MSG means don't display any messages
1418 while loading.
1420 If you load a file that doesn't contain a proper bookmark alist, you
1421 will corrupt Emacs's bookmark list. Generally, you should only load
1422 in files that were created with the bookmark functions in the first
1423 place. Your own personal bookmark file, `~/.emacs.bmk', is
1424 maintained automatically by Emacs; you shouldn't need to load it
1425 explicitly.
1427 If you load a file containing bookmarks with the same names as
1428 bookmarks already present in your Emacs, the new bookmarks will get
1429 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1430 method buffers use to resolve name collisions."
1431 (interactive
1432 (list (read-file-name
1433 (format "Load bookmarks from: (%s) "
1434 bookmark-default-file)
1435 ;;Default might not be used often,
1436 ;;but there's no better default, and
1437 ;;I guess it's better than none at all.
1438 "~/" bookmark-default-file 'confirm)))
1439 (setq file (expand-file-name file))
1440 (if (file-readable-p file)
1441 (save-excursion
1442 (save-window-excursion
1443 (if (null no-msg)
1444 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1445 (set-buffer (let ((enable-local-variables nil))
1446 (find-file-noselect file)))
1447 (goto-char (point-min))
1448 (bookmark-maybe-upgrade-file-format)
1449 (let ((blist (bookmark-alist-from-buffer)))
1450 (if (listp blist)
1451 (progn
1452 (if overwrite
1453 (progn
1454 (setq bookmark-alist blist)
1455 (setq bookmark-alist-modification-count 0))
1456 ;; else
1457 (bookmark-import-new-list blist)
1458 (setq bookmark-alist-modification-count
1459 (1+ bookmark-alist-modification-count)))
1460 (if (string-equal
1461 (expand-file-name bookmark-default-file)
1462 file)
1463 (setq bookmarks-already-loaded t))
1464 (bookmark-bmenu-surreptitiously-rebuild-list))
1465 (error "Invalid bookmark list in %s" file)))
1466 (kill-buffer (current-buffer)))
1467 (if (null no-msg)
1468 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1469 (error "Cannot read bookmark file %s" file)))
1473 ;;; Code supporting the dired-like bookmark menu. Prefix is
1474 ;;; "bookmark-bmenu" for "buffer-menu":
1477 (defvar bookmark-bmenu-bookmark-column nil)
1480 (defvar bookmark-bmenu-hidden-bookmarks ())
1483 (defvar bookmark-bmenu-mode-map nil)
1486 (if bookmark-bmenu-mode-map
1488 (setq bookmark-bmenu-mode-map (make-keymap))
1489 (suppress-keymap bookmark-bmenu-mode-map t)
1490 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1491 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1492 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1493 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1494 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1495 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1496 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1497 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1498 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1499 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1500 (define-key bookmark-bmenu-mode-map "\C-o"
1501 'bookmark-bmenu-switch-other-window)
1502 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1503 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1504 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1505 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1506 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1507 (define-key bookmark-bmenu-mode-map " " 'next-line)
1508 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1509 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1510 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1511 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1512 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1513 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1514 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1515 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1516 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1517 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1518 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1519 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1520 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1521 (define-key bookmark-bmenu-mode-map [mouse-2]
1522 'bookmark-bmenu-other-window-with-mouse))
1526 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1527 ;; data.
1528 (put 'bookmark-bmenu-mode 'mode-class 'special)
1531 ;; todo: need to display whether or not bookmark exists as a buffer in
1532 ;; flag column.
1534 ;; Format:
1535 ;; FLAGS BOOKMARK [ LOCATION ]
1538 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1539 "Rebuild the Bookmark List if it exists.
1540 Don't affect the buffer ring order."
1541 (if (get-buffer "*Bookmark List*")
1542 (save-excursion
1543 (save-window-excursion
1544 (bookmark-bmenu-list)))))
1547 ;;;###autoload
1548 (defun bookmark-bmenu-list ()
1549 "Display a list of existing bookmarks.
1550 The list is displayed in a buffer named `*Bookmark List*'.
1551 The leftmost column displays a D if the bookmark is flagged for
1552 deletion, or > if it is flagged for displaying."
1553 (interactive)
1554 (bookmark-maybe-load-default-file)
1555 (if (interactive-p)
1556 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1557 (set-buffer (get-buffer-create "*Bookmark List*")))
1558 (let ((inhibit-read-only t))
1559 (erase-buffer)
1560 (insert "% Bookmark\n- --------\n")
1561 (add-text-properties (point-min) (point)
1562 '(font-lock-face bookmark-menu-heading))
1563 (mapc
1564 (lambda (full-record)
1565 ;; if a bookmark has an annotation, prepend a "*"
1566 ;; in the list of bookmarks.
1567 (let ((annotation (bookmark-get-annotation
1568 (bookmark-name-from-full-record full-record))))
1569 (if (and annotation (not (string-equal annotation "")))
1570 (insert " *")
1571 (insert " "))
1572 (let ((start (point)))
1573 (insert (bookmark-name-from-full-record full-record))
1574 (if (and (display-color-p) (display-mouse-p))
1575 (add-text-properties
1576 start
1577 (save-excursion (re-search-backward
1578 "[^ \t]")
1579 (1+ (point)))
1580 '(mouse-face highlight
1581 follow-link t
1582 help-echo "mouse-2: go to this bookmark in other window")))
1583 (insert "\n")
1585 (bookmark-maybe-sort-alist)))
1586 (goto-char (point-min))
1587 (forward-line 2)
1588 (bookmark-bmenu-mode)
1589 (if bookmark-bmenu-toggle-filenames
1590 (bookmark-bmenu-toggle-filenames t)))
1592 ;;;###autoload
1593 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1594 ;;;###autoload
1595 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1599 (defun bookmark-bmenu-mode ()
1600 "Major mode for editing a list of bookmarks.
1601 Each line describes one of the bookmarks in Emacs.
1602 Letters do not insert themselves; instead, they are commands.
1603 Bookmark names preceded by a \"*\" have annotations.
1604 \\<bookmark-bmenu-mode-map>
1605 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1606 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1607 Also show bookmarks marked using m in other windows.
1608 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1609 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1610 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1611 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1612 together with bookmark selected before this one in another window.
1613 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1614 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1615 so the bookmark menu bookmark remains visible in its window.
1616 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1617 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1618 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1619 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1620 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1621 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1622 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1623 With a prefix arg, prompts for a file to save in.
1624 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1625 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1626 With prefix argument, also move up one line.
1627 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1628 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1629 in another buffer.
1630 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1631 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1632 (kill-all-local-variables)
1633 (use-local-map bookmark-bmenu-mode-map)
1634 (setq truncate-lines t)
1635 (setq buffer-read-only t)
1636 (setq major-mode 'bookmark-bmenu-mode)
1637 (setq mode-name "Bookmark Menu")
1638 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1641 (defun bookmark-bmenu-toggle-filenames (&optional show)
1642 "Toggle whether filenames are shown in the bookmark list.
1643 Optional argument SHOW means show them unconditionally."
1644 (interactive)
1645 (cond
1646 (show
1647 (setq bookmark-bmenu-toggle-filenames nil)
1648 (bookmark-bmenu-show-filenames)
1649 (setq bookmark-bmenu-toggle-filenames t))
1650 (bookmark-bmenu-toggle-filenames
1651 (bookmark-bmenu-hide-filenames)
1652 (setq bookmark-bmenu-toggle-filenames nil))
1654 (bookmark-bmenu-show-filenames)
1655 (setq bookmark-bmenu-toggle-filenames t))))
1658 (defun bookmark-bmenu-show-filenames (&optional force)
1659 (if (and (not force) bookmark-bmenu-toggle-filenames)
1660 nil ;already shown, so do nothing
1661 (save-excursion
1662 (save-window-excursion
1663 (goto-char (point-min))
1664 (forward-line 2)
1665 (setq bookmark-bmenu-hidden-bookmarks ())
1666 (let ((inhibit-read-only t))
1667 (while (< (point) (point-max))
1668 (let ((bmrk (bookmark-bmenu-bookmark)))
1669 (setq bookmark-bmenu-hidden-bookmarks
1670 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1671 (let ((start (save-excursion (end-of-line) (point))))
1672 (move-to-column bookmark-bmenu-file-column t)
1673 ;; Strip off `mouse-face' from the white spaces region.
1674 (if (and (display-color-p) (display-mouse-p))
1675 (remove-text-properties start (point)
1676 '(mouse-face nil help-echo nil))))
1677 (delete-region (point) (progn (end-of-line) (point)))
1678 (insert " ")
1679 ;; Pass the NO-HISTORY arg:
1680 (bookmark-insert-location bmrk t)
1681 (forward-line 1))))))))
1684 (defun bookmark-bmenu-hide-filenames (&optional force)
1685 (if (and (not force) bookmark-bmenu-toggle-filenames)
1686 ;; nothing to hide if above is nil
1687 (save-excursion
1688 (save-window-excursion
1689 (goto-char (point-min))
1690 (forward-line 2)
1691 (setq bookmark-bmenu-hidden-bookmarks
1692 (nreverse bookmark-bmenu-hidden-bookmarks))
1693 (save-excursion
1694 (goto-char (point-min))
1695 (search-forward "Bookmark")
1696 (backward-word 1)
1697 (setq bookmark-bmenu-bookmark-column (current-column)))
1698 (save-excursion
1699 (let ((inhibit-read-only t))
1700 (while bookmark-bmenu-hidden-bookmarks
1701 (move-to-column bookmark-bmenu-bookmark-column t)
1702 (bookmark-kill-line)
1703 (let ((start (point)))
1704 (insert (car bookmark-bmenu-hidden-bookmarks))
1705 (if (and (display-color-p) (display-mouse-p))
1706 (add-text-properties
1707 start
1708 (save-excursion (re-search-backward
1709 "[^ \t]")
1710 (1+ (point)))
1711 '(mouse-face highlight
1712 follow-link t
1713 help-echo
1714 "mouse-2: go to this bookmark in other window"))))
1715 (setq bookmark-bmenu-hidden-bookmarks
1716 (cdr bookmark-bmenu-hidden-bookmarks))
1717 (forward-line 1))))))))
1720 (defun bookmark-bmenu-check-position ()
1721 ;; Returns non-nil if on a line with a bookmark.
1722 ;; (The actual value returned is bookmark-alist).
1723 ;; Else reposition and try again, else return nil.
1724 (cond ((< (count-lines (point-min) (point)) 2)
1725 (goto-char (point-min))
1726 (forward-line 2)
1727 bookmark-alist)
1728 ((and (bolp) (eobp))
1729 (beginning-of-line 0)
1730 bookmark-alist)
1732 bookmark-alist)))
1735 (defun bookmark-bmenu-bookmark ()
1736 ;; return a string which is bookmark of this line.
1737 (if (bookmark-bmenu-check-position)
1738 (save-excursion
1739 (save-window-excursion
1740 (goto-char (point-min))
1741 (search-forward "Bookmark")
1742 (backward-word 1)
1743 (setq bookmark-bmenu-bookmark-column (current-column)))))
1744 (if bookmark-bmenu-toggle-filenames
1745 (bookmark-bmenu-hide-filenames))
1746 (save-excursion
1747 (save-window-excursion
1748 (beginning-of-line)
1749 (forward-char bookmark-bmenu-bookmark-column)
1750 (prog1
1751 (buffer-substring-no-properties (point)
1752 (progn
1753 (end-of-line)
1754 (point)))
1755 ;; well, this is certainly crystal-clear:
1756 (if bookmark-bmenu-toggle-filenames
1757 (bookmark-bmenu-toggle-filenames t))))))
1760 (defun bookmark-show-annotation (bookmark)
1761 "Display the annotation for bookmark named BOOKMARK in a buffer,
1762 if an annotation exists."
1763 (let ((annotation (bookmark-get-annotation bookmark)))
1764 (if (and annotation (not (string-equal annotation "")))
1765 (save-excursion
1766 (let ((old-buf (current-buffer)))
1767 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1768 (delete-region (point-min) (point-max))
1769 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1770 (insert annotation)
1771 (goto-char (point-min))
1772 (pop-to-buffer old-buf))))))
1775 (defun bookmark-show-all-annotations ()
1776 "Display the annotations for all bookmarks in a buffer."
1777 (let ((old-buf (current-buffer)))
1778 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1779 (delete-region (point-min) (point-max))
1780 (mapc
1781 (lambda (full-record)
1782 (let* ((name (bookmark-name-from-full-record full-record))
1783 (ann (bookmark-get-annotation name)))
1784 (insert (concat name ":\n"))
1785 (if (and ann (not (string-equal ann "")))
1786 ;; insert the annotation, indented by 4 spaces.
1787 (progn
1788 (save-excursion (insert ann) (unless (bolp)
1789 (insert "\n")))
1790 (while (< (point) (point-max))
1791 (beginning-of-line) ; paranoia
1792 (insert " ")
1793 (forward-line)
1794 (end-of-line))))))
1795 bookmark-alist)
1796 (goto-char (point-min))
1797 (pop-to-buffer old-buf)))
1800 (defun bookmark-bmenu-mark ()
1801 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1802 (interactive)
1803 (beginning-of-line)
1804 (if (bookmark-bmenu-check-position)
1805 (let ((inhibit-read-only t))
1806 (delete-char 1)
1807 (insert ?>)
1808 (forward-line 1)
1809 (bookmark-bmenu-check-position))))
1812 (defun bookmark-bmenu-select ()
1813 "Select this line's bookmark; also display bookmarks marked with `>'.
1814 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1815 (interactive)
1816 (if (bookmark-bmenu-check-position)
1817 (let ((bmrk (bookmark-bmenu-bookmark))
1818 (menu (current-buffer))
1819 (others ())
1820 tem)
1821 (goto-char (point-min))
1822 (while (re-search-forward "^>" nil t)
1823 (setq tem (bookmark-bmenu-bookmark))
1824 (let ((inhibit-read-only t))
1825 (delete-char -1)
1826 (insert ?\s))
1827 (or (string-equal tem bmrk)
1828 (member tem others)
1829 (setq others (cons tem others))))
1830 (setq others (nreverse others)
1831 tem (/ (1- (frame-height)) (1+ (length others))))
1832 (delete-other-windows)
1833 (bookmark-jump bmrk)
1834 (bury-buffer menu)
1835 (if others
1836 (while others
1837 (split-window nil tem)
1838 (other-window 1)
1839 (bookmark-jump (car others))
1840 (setq others (cdr others)))
1841 (other-window 1)))))
1844 (defun bookmark-bmenu-save (parg)
1845 "Save the current list into a bookmark file.
1846 With a prefix arg, prompts for a file to save them in."
1847 (interactive "P")
1848 (save-excursion
1849 (save-window-excursion
1850 (bookmark-save parg))))
1853 (defun bookmark-bmenu-load ()
1854 "Load the bookmark file and rebuild the bookmark menu-buffer."
1855 (interactive)
1856 (if (bookmark-bmenu-check-position)
1857 (save-excursion
1858 (save-window-excursion
1859 ;; This will call `bookmark-bmenu-list'
1860 (call-interactively 'bookmark-load)))))
1863 (defun bookmark-bmenu-1-window ()
1864 "Select this line's bookmark, alone, in full frame."
1865 (interactive)
1866 (if (bookmark-bmenu-check-position)
1867 (progn
1868 (bookmark-jump (bookmark-bmenu-bookmark))
1869 (bury-buffer (other-buffer))
1870 (delete-other-windows))))
1873 (defun bookmark-bmenu-2-window ()
1874 "Select this line's bookmark, with previous buffer in second window."
1875 (interactive)
1876 (if (bookmark-bmenu-check-position)
1877 (let ((bmrk (bookmark-bmenu-bookmark))
1878 (menu (current-buffer))
1879 (pop-up-windows t))
1880 (delete-other-windows)
1881 (switch-to-buffer (other-buffer))
1882 (let* ((alist (bookmark-jump-noselect bmrk))
1883 (buff (cadr (assq 'buffer alist)))
1884 (pos (cadr (assq 'position alist))))
1885 (pop-to-buffer buff)
1886 (goto-char pos))
1887 (bury-buffer menu))))
1890 (defun bookmark-bmenu-this-window ()
1891 "Select this line's bookmark in this window."
1892 (interactive)
1893 (if (bookmark-bmenu-check-position)
1894 (bookmark-jump (bookmark-bmenu-bookmark))))
1897 (defun bookmark-bmenu-other-window ()
1898 "Select this line's bookmark in other window, leaving bookmark menu visible."
1899 (interactive)
1900 (let ((bookmark (bookmark-bmenu-bookmark)))
1901 (if (bookmark-bmenu-check-position)
1902 (let* ((alist (bookmark-jump-noselect bookmark))
1903 (buff (cadr (assq 'buffer alist)))
1904 (pos (cadr (assq 'position alist))))
1905 (switch-to-buffer-other-window buff)
1906 (goto-char pos)
1907 (set-window-point (get-buffer-window buff) pos)
1908 (bookmark-show-annotation bookmark)))))
1911 (defun bookmark-bmenu-switch-other-window ()
1912 "Make the other window select this line's bookmark.
1913 The current window remains selected."
1914 (interactive)
1915 (let ((bookmark (bookmark-bmenu-bookmark))
1916 (pop-up-windows t)
1917 same-window-buffer-names
1918 same-window-regexps)
1919 (if (bookmark-bmenu-check-position)
1920 (let* ((alist (bookmark-jump-noselect bookmark))
1921 (buff (cadr (assq 'buffer alist)))
1922 (pos (cadr (assq 'position alist))))
1923 (display-buffer buff)
1924 (let ((o-buffer (current-buffer)))
1925 ;; save-excursion won't do
1926 (set-buffer buff)
1927 (goto-char pos)
1928 (set-window-point (get-buffer-window buff) pos)
1929 (set-buffer o-buffer))
1930 (bookmark-show-annotation bookmark)))))
1932 (defun bookmark-bmenu-other-window-with-mouse (event)
1933 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1934 (interactive "e")
1935 (save-excursion
1936 (set-buffer (window-buffer (posn-window (event-end event))))
1937 (save-excursion
1938 (goto-char (posn-point (event-end event)))
1939 (bookmark-bmenu-other-window))))
1942 (defun bookmark-bmenu-show-annotation ()
1943 "Show the annotation for the current bookmark in another window."
1944 (interactive)
1945 (let ((bookmark (bookmark-bmenu-bookmark)))
1946 (if (bookmark-bmenu-check-position)
1947 (bookmark-show-annotation bookmark))))
1950 (defun bookmark-bmenu-show-all-annotations ()
1951 "Show the annotation for all bookmarks in another window."
1952 (interactive)
1953 (bookmark-show-all-annotations))
1956 (defun bookmark-bmenu-edit-annotation ()
1957 "Edit the annotation for the current bookmark in another window."
1958 (interactive)
1959 (let ((bookmark (bookmark-bmenu-bookmark)))
1960 (if (bookmark-bmenu-check-position)
1961 (bookmark-edit-annotation bookmark))))
1964 (defun bookmark-bmenu-unmark (&optional backup)
1965 "Cancel all requested operations on bookmark on this line and move down.
1966 Optional BACKUP means move up."
1967 (interactive "P")
1968 (beginning-of-line)
1969 (if (bookmark-bmenu-check-position)
1970 (progn
1971 (let ((inhibit-read-only t))
1972 (delete-char 1)
1973 ;; any flags to reset according to circumstances? How about a
1974 ;; flag indicating whether this bookmark is being visited?
1975 ;; well, we don't have this now, so maybe later.
1976 (insert " "))
1977 (forward-line (if backup -1 1))
1978 (bookmark-bmenu-check-position))))
1981 (defun bookmark-bmenu-backup-unmark ()
1982 "Move up and cancel all requested operations on bookmark on line above."
1983 (interactive)
1984 (forward-line -1)
1985 (if (bookmark-bmenu-check-position)
1986 (progn
1987 (bookmark-bmenu-unmark)
1988 (forward-line -1)
1989 (bookmark-bmenu-check-position))))
1992 (defun bookmark-bmenu-delete ()
1993 "Mark bookmark on this line to be deleted.
1994 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1995 (interactive)
1996 (beginning-of-line)
1997 (if (bookmark-bmenu-check-position)
1998 (let ((inhibit-read-only t))
1999 (delete-char 1)
2000 (insert ?D)
2001 (forward-line 1)
2002 (bookmark-bmenu-check-position))))
2005 (defun bookmark-bmenu-delete-backwards ()
2006 "Mark bookmark on this line to be deleted, then move up one line.
2007 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2008 (interactive)
2009 (bookmark-bmenu-delete)
2010 (forward-line -2)
2011 (if (bookmark-bmenu-check-position)
2012 (forward-line 1))
2013 (bookmark-bmenu-check-position))
2016 (defun bookmark-bmenu-execute-deletions ()
2017 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2018 (interactive)
2019 (message "Deleting bookmarks...")
2020 (let ((hide-em bookmark-bmenu-toggle-filenames)
2021 (o-point (point))
2022 (o-str (save-excursion
2023 (beginning-of-line)
2024 (if (looking-at "^D")
2026 (buffer-substring
2027 (point)
2028 (progn (end-of-line) (point))))))
2029 (o-col (current-column)))
2030 (if hide-em (bookmark-bmenu-hide-filenames))
2031 (setq bookmark-bmenu-toggle-filenames nil)
2032 (goto-char (point-min))
2033 (forward-line 1)
2034 (while (re-search-forward "^D" (point-max) t)
2035 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2036 (bookmark-bmenu-list)
2037 (setq bookmark-bmenu-toggle-filenames hide-em)
2038 (if bookmark-bmenu-toggle-filenames
2039 (bookmark-bmenu-toggle-filenames t))
2040 (if o-str
2041 (progn
2042 (goto-char (point-min))
2043 (search-forward o-str)
2044 (beginning-of-line)
2045 (forward-char o-col))
2046 (goto-char o-point))
2047 (beginning-of-line)
2048 (setq bookmark-alist-modification-count
2049 (1+ bookmark-alist-modification-count))
2050 (if (bookmark-time-to-save-p)
2051 (bookmark-save))
2052 (message "Deleting bookmarks...done")
2056 (defun bookmark-bmenu-rename ()
2057 "Rename bookmark on current line. Prompts for a new name."
2058 (interactive)
2059 (if (bookmark-bmenu-check-position)
2060 (let ((bmrk (bookmark-bmenu-bookmark))
2061 (thispoint (point)))
2062 (bookmark-rename bmrk)
2063 (bookmark-bmenu-list)
2064 (goto-char thispoint))))
2067 (defun bookmark-bmenu-locate ()
2068 "Display location of this bookmark. Displays in the minibuffer."
2069 (interactive)
2070 (if (bookmark-bmenu-check-position)
2071 (let ((bmrk (bookmark-bmenu-bookmark)))
2072 (message "%s" (bookmark-location bmrk)))))
2074 (defun bookmark-bmenu-relocate ()
2075 "Change the file path of the bookmark on the current line,
2076 prompting with completion for the new path."
2077 (interactive)
2078 (if (bookmark-bmenu-check-position)
2079 (let ((bmrk (bookmark-bmenu-bookmark))
2080 (thispoint (point)))
2081 (bookmark-relocate bmrk)
2082 (goto-char thispoint))))
2085 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2087 (defun bookmark-menu-popup-paned-menu (event name entries)
2088 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2089 That is, ENTRIES is a list of strings which appear as the choices
2090 in the menu.
2091 The number of panes depends on the number of entries.
2092 The visible entries are truncated to `bookmark-menu-length', but the
2093 strings returned are not."
2094 (let ((f-height (/ (frame-height) 2))
2095 (pane-list nil)
2096 (iter 0))
2097 (while entries
2098 (let (lst
2099 (count 0))
2100 (while (and (< count f-height) entries)
2101 (let ((str (car entries)))
2102 (push (cons
2103 (if (> (length str) bookmark-menu-length)
2104 (substring str 0 bookmark-menu-length)
2105 str)
2106 str)
2107 lst)
2108 (setq entries (cdr entries))
2109 (setq count (1+ count))))
2110 (setq iter (1+ iter))
2111 (push (cons
2112 (format "-*- %s (%d) -*-" name iter)
2113 (nreverse lst))
2114 pane-list)))
2116 ;; Popup the menu and return the string.
2117 (x-popup-menu event (cons (concat "-*- " name " -*-")
2118 (nreverse pane-list)))))
2121 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2122 ;; following works, and for explaining what to do to make it work.
2124 ;; We MUST autoload EACH form used to set up this variable's value, so
2125 ;; that the whole job is done in loaddefs.el.
2127 ;; Emacs menubar stuff.
2129 ;;;###autoload
2130 (defvar menu-bar-bookmark-map
2131 (let ((map (make-sparse-keymap "Bookmark functions")))
2132 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2133 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2134 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2135 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2136 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2137 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2138 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2139 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2140 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2141 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2142 map))
2144 ;;;###autoload
2145 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2147 ;; make bookmarks appear toward the right side of the menu.
2148 (if (boundp 'menu-bar-final-items)
2149 (if menu-bar-final-items
2150 (setq menu-bar-final-items
2151 (cons 'bookmark menu-bar-final-items)))
2152 (setq menu-bar-final-items '(bookmark)))
2154 ;;;; end bookmark menu stuff ;;;;
2157 ;;; Load Hook
2158 (defvar bookmark-load-hook nil
2159 "Hook run at the end of loading bookmark.")
2161 ;;; Exit Hook, called from kill-emacs-hook
2162 (defvar bookmark-exit-hook nil
2163 "Hook run when Emacs exits.")
2165 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2167 (defun bookmark-exit-hook-internal ()
2168 "Save bookmark state, if necessary, at Emacs exit time.
2169 This also runs `bookmark-exit-hook'."
2170 (run-hooks 'bookmark-exit-hook)
2171 (and bookmark-alist
2172 (bookmark-time-to-save-p t)
2173 (bookmark-save)))
2175 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2178 (run-hooks 'bookmark-load-hook)
2180 (provide 'bookmark)
2182 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2183 ;;; bookmark.el ends here