(isearch-search-string): Simplify and convert docstring.
[emacs.git] / lisp / bookmark.el
blob658097c8bf09663a3f88e3185d14a1ba82f3a880
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 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)
83 ;;; Misc comments:
85 ;; If variable bookmark-use-annotations is non-nil, an annotation is
86 ;; queried for when setting a bookmark.
88 ;; The bookmark list is sorted lexically by default, but you can turn
89 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
90 ;; the list will be presented in the order it is recorded
91 ;; (chronologically), which is actually fairly useful as well.
93 ;;; User Variables
95 (defgroup bookmark nil
96 "Setting, annotation and jumping to bookmarks."
97 :group 'matching)
100 (defcustom bookmark-use-annotations nil
101 "If non-nil, saving a bookmark queries for an annotation in a buffer."
102 :type 'boolean
103 :group 'bookmark)
106 (defcustom bookmark-save-flag t
107 "Controls when Emacs saves bookmarks to a file.
108 --> nil means never save bookmarks, except when `bookmark-save' is
109 explicitly called \(\\[bookmark-save]\).
110 --> t means save bookmarks when Emacs is killed.
111 --> Otherwise, it should be a number that is the frequency with which
112 the bookmark list is saved \(i.e.: the number of times which
113 Emacs' bookmark list may be modified before it is automatically
114 saved.\). If it is a number, Emacs will also automatically save
115 bookmarks when it is killed.
117 Therefore, the way to get it to save every time you make or delete a
118 bookmark is to set this variable to 1 \(or 0, which produces the same
119 behavior.\)
121 To specify the file in which to save them, modify the variable
122 `bookmark-default-file', which is `~/.emacs.bmk' by default."
123 :type '(choice (const nil) integer (other t))
124 :group 'bookmark)
127 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
128 "*The `.emacs.bmk' file used to be called this name.")
131 ;; defvarred to avoid a compilation warning:
132 (defvar bookmark-file nil
133 "Old name for `bookmark-default-file'.")
135 (defcustom bookmark-default-file
136 (if bookmark-file
137 ;; In case user set `bookmark-file' in her .emacs:
138 bookmark-file
139 (convert-standard-filename "~/.emacs.bmk"))
140 "File in which to save bookmarks by default."
141 :type 'file
142 :group 'bookmark)
145 (defcustom bookmark-version-control 'nospecial
146 "Whether or not to make numbered backups of the bookmark file.
147 It can have four values: t, nil, `never', and `nospecial'.
148 The first three have the same meaning that they do for the
149 variable `version-control', and the final value `nospecial' means just
150 use the value of `version-control'."
151 :type '(choice (const nil) (const never) (const nospecial)
152 (other t))
153 :group 'bookmark)
156 (defcustom bookmark-completion-ignore-case t
157 "Non-nil means bookmark functions ignore case in completion."
158 :type 'boolean
159 :group 'bookmark)
162 (defcustom bookmark-sort-flag t
163 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
164 Otherwise they will be displayed in LIFO order (that is, most
165 recently set ones come first, oldest ones come last)."
166 :type 'boolean
167 :group 'bookmark)
170 (defcustom bookmark-automatically-show-annotations t
171 "Non-nil means show annotations when jumping to a bookmark."
172 :type 'boolean
173 :group 'bookmark)
176 (defcustom bookmark-bmenu-file-column 30
177 "Column at which to display filenames in a buffer listing bookmarks.
178 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
179 :type 'integer
180 :group 'bookmark)
183 (defcustom bookmark-bmenu-toggle-filenames t
184 "Non-nil means show filenames when listing bookmarks.
185 This may result in truncated bookmark names. To disable this, put the
186 following in your `.emacs' file:
188 \(setq bookmark-bmenu-toggle-filenames nil\)"
189 :type 'boolean
190 :group 'bookmark)
193 (defcustom bookmark-menu-length 70
194 "Maximum length of a bookmark name displayed on a popup menu."
195 :type 'integer
196 :group 'bookmark)
199 (defface bookmark-menu-heading
200 '((t (:inherit font-lock-type-face)))
201 "Face used to highlight the heading in bookmark menu buffers."
202 :group 'bookmark
203 :version "22.1")
206 ;;; No user-serviceable parts beyond this point.
208 ;; Added for lucid emacs compatibility, db
209 (or (fboundp 'defalias) (fset 'defalias 'fset))
211 ;; suggested for lucid compatibility by david hughes:
212 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
215 ;;; Keymap stuff:
217 ;; Set up these bindings dumping time *only*;
218 ;; if the user alters them, don't override the user when loading bookmark.el.
220 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
221 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
222 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
224 ;;;###autoload
225 (defvar bookmark-map
226 (let ((map (make-sparse-keymap)))
227 ;; Read the help on all of these functions for details...
228 (define-key map "x" 'bookmark-set)
229 (define-key map "m" 'bookmark-set) ;"m"ark
230 (define-key map "j" 'bookmark-jump)
231 (define-key map "g" 'bookmark-jump) ;"g"o
232 (define-key map "o" 'bookmark-jump-other-window)
233 (define-key map "i" 'bookmark-insert)
234 (define-key map "e" 'edit-bookmarks)
235 (define-key map "f" 'bookmark-insert-location) ;"f"ind
236 (define-key map "r" 'bookmark-rename)
237 (define-key map "d" 'bookmark-delete)
238 (define-key map "l" 'bookmark-load)
239 (define-key map "w" 'bookmark-write)
240 (define-key map "s" 'bookmark-save)
241 map)
242 "Keymap containing bindings to bookmark functions.
243 It is not bound to any key by default: to bind it
244 so that you have a bookmark prefix, just use `global-set-key' and bind a
245 key of your choice to `bookmark-map'. All interactive bookmark
246 functions have a binding in this keymap.")
248 ;;;###autoload (fset 'bookmark-map bookmark-map)
251 ;;; Core variables and data structures:
252 (defvar bookmark-alist ()
253 "Association list of bookmarks and their records.
254 You probably don't want to change the value of this alist yourself;
255 instead, let the various bookmark functions do it for you.
257 The format of the alist is
259 \(BOOKMARK1 BOOKMARK2 ...\)
261 where each BOOKMARK is of the form
263 (NAME PARAM-ALIST) or (NAME . PARAM-ALIST)
265 where the first form is the old deprecated one and the second is
266 the new favored one. PARAM-ALIST is typically of the form:
268 ((filename . FILE)
269 (front-context-string . FRONT-STR)
270 (rear-context-string . REAR-STR)
271 (position . POS)
272 (annotation . ANNOTATION)))")
275 (defvar bookmarks-already-loaded nil)
278 ;; more stuff added by db.
280 (defvar bookmark-current-bookmark nil
281 "Name of bookmark most recently used in the current file.
282 It is buffer local, used to make moving a bookmark forward
283 through a file easier.")
285 (make-variable-buffer-local 'bookmark-current-bookmark)
288 (defvar bookmark-alist-modification-count 0
289 "Number of modifications to bookmark list since it was last saved.")
292 (defvar bookmark-search-size 16
293 "Length of the context strings recorded on either side of a bookmark.")
296 (defvar bookmark-current-point 0)
297 (defvar bookmark-yank-point 0)
298 (defvar bookmark-current-buffer nil)
300 (defvar Info-suffix-list)
302 ;; Helper functions.
304 ;; Only functions on this page and the next one (file formats) need to
305 ;; know anything about the format of bookmark-alist entries.
306 ;; Everyone else should go through them.
309 (defun bookmark-name-from-full-record (full-record)
310 "Return name of FULL-RECORD \(an alist element instead of a string\)."
311 (car full-record))
314 (defun bookmark-all-names ()
315 "Return a list of all current bookmark names."
316 (bookmark-maybe-load-default-file)
317 (mapcar 'bookmark-name-from-full-record bookmark-alist))
320 (defun bookmark-get-bookmark (bookmark)
321 "Return the bookmark record corresponding to BOOKMARK.
322 If BOOKMARK is already a bookmark record, just return it,
323 Otherwise look for the corresponding bookmark in `bookmark-alist'."
324 (cond
325 ((consp bookmark) bookmark)
326 ((stringp bookmark)
327 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case))))
330 (defun bookmark-get-bookmark-record (bookmark)
331 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
332 That is, all information but the name."
333 (let ((alist (cdr (or (bookmark-get-bookmark bookmark)
334 (error "Invalid bookmark %s" bookmark)))))
335 ;; The bookmark objects can either look like (NAME ALIST) or
336 ;; (NAME . ALIST), so we have to distinguish the two here.
337 (if (and (null (cdr alist)) (consp (caar alist)))
338 (car alist) alist)))
341 (defun bookmark-set-name (bookmark newname)
342 "Set BOOKMARK's name to NEWNAME."
343 (setcar
344 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
345 newname))
347 (defun bookmark-prop-get (bookmark prop)
348 "Return the property PROP of BOOKMARK, or nil if none."
349 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
351 (defun bookmark-prop-set (bookmark prop val)
352 "Set the property PROP of BOOKMARK to VAL."
353 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
354 (if cell
355 (setcdr cell val)
356 (nconc (bookmark-get-bookmark-record bookmark)
357 (list (cons prop val))))))
359 (defun bookmark-get-annotation (bookmark)
360 "Return the annotation of BOOKMARK, or nil if none."
361 (bookmark-prop-get bookmark 'annotation))
363 (defun bookmark-set-annotation (bookmark ann)
364 "Set the annotation of BOOKMARK to ANN."
365 (bookmark-prop-set bookmark 'annotation ann))
368 (defun bookmark-get-filename (bookmark)
369 "Return the full filename of BOOKMARK."
370 (bookmark-prop-get bookmark 'filename))
373 (defun bookmark-set-filename (bookmark filename)
374 "Set the full filename of BOOKMARK to FILENAME."
375 (bookmark-prop-set bookmark 'filename filename))
378 (defun bookmark-get-position (bookmark)
379 "Return the position \(i.e.: point\) of BOOKMARK."
380 (bookmark-prop-get bookmark 'position))
383 (defun bookmark-set-position (bookmark position)
384 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
385 (bookmark-prop-set bookmark 'position position))
388 (defun bookmark-get-front-context-string (bookmark)
389 "Return the front-context-string of BOOKMARK."
390 (bookmark-prop-get bookmark 'front-context-string))
393 (defun bookmark-set-front-context-string (bookmark string)
394 "Set the front-context-string of BOOKMARK to STRING."
395 (bookmark-prop-set bookmark 'front-context-string string))
398 (defun bookmark-get-rear-context-string (bookmark)
399 "Return the rear-context-string of BOOKMARK."
400 (bookmark-prop-get bookmark 'rear-context-string))
403 (defun bookmark-set-rear-context-string (bookmark string)
404 "Set the rear-context-string of BOOKMARK to STRING."
405 (bookmark-prop-set bookmark 'rear-context-string string))
408 (defun bookmark-get-handler (bookmark)
409 (bookmark-prop-get bookmark 'handler))
411 (defvar bookmark-history nil
412 "The history list for bookmark functions.")
415 (defun bookmark-completing-read (prompt &optional default)
416 "Prompting with PROMPT, read a bookmark name in completion.
417 PROMPT will get a \": \" stuck on the end no matter what, so you
418 probably don't want to include one yourself.
419 Optional second arg DEFAULT is a string to return if the user enters
420 the empty string."
421 (bookmark-maybe-load-default-file) ; paranoia
422 (if (listp last-nonmenu-event)
423 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
424 (let* ((completion-ignore-case bookmark-completion-ignore-case)
425 (default default)
426 (prompt (if default
427 (concat prompt (format " (%s): " default))
428 (concat prompt ": ")))
429 (str
430 (completing-read prompt
431 bookmark-alist
435 'bookmark-history)))
436 (if (string-equal "" str) default str))))
439 (defmacro bookmark-maybe-historicize-string (string)
440 "Put STRING into the bookmark prompt history, if caller non-interactive.
441 We need this because sometimes bookmark functions are invoked from
442 menus, so `completing-read' never gets a chance to set `bookmark-history'."
443 `(or
444 (interactive-p)
445 (setq bookmark-history (cons ,string bookmark-history))))
447 (defvar bookmark-make-record-function 'bookmark-make-record-default
448 "A function that should be called to create a bookmark record.
449 Modes may set this variable buffer-locally to enable bookmarking of
450 locations that should be treated specially, such as Info nodes,
451 news posts, images, pdf documents, etc.
453 The function will be called with no arguments.
454 It should signal a user error if it is unable to construct a record for
455 the current location.
457 The returned record should be a cons cell of the form (NAME . ALIST)
458 where ALIST is as described in `bookmark-alist' and may typically contain
459 a special cons (handler . SOME-FUNCTION) which sets the handler function
460 that should be used to open this bookmark instead of
461 `bookmark-default-handler'. The handler should follow the same calling
462 convention as the one used by `bookmark-default-handler'.
464 NAME is a suggested name for the constructed bookmark. It can be nil
465 in which case a default heuristic will be used. The function can also
466 equivalently just return ALIST without NAME.")
468 (defun bookmark-make-record ()
469 "Return a new bookmark record (NAME . ALIST) for the current location."
470 (let ((record (funcall bookmark-make-record-function)))
471 ;; Set up default name.
472 (if (stringp (car record))
473 ;; The function already provided a default name.
474 record
475 (if (car record) (push nil record))
476 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
477 record)))
479 (defun bookmark-store (name alist no-overwrite)
480 "Store the bookmark NAME with data ALIST.
481 If NO-OVERWRITE is non-nil and another bookmark of the same name already
482 exists in `bookmark-alist', record the new bookmark without throwing away the
483 old one."
484 (bookmark-maybe-load-default-file)
485 (let ((stripped-name (copy-sequence name)))
486 (or (featurep 'xemacs)
487 ;; XEmacs's `set-text-properties' doesn't work on
488 ;; free-standing strings, apparently.
489 (set-text-properties 0 (length stripped-name) nil stripped-name))
490 (if (and (bookmark-get-bookmark stripped-name) (not no-overwrite))
491 ;; already existing bookmark under that name and
492 ;; no prefix arg means just overwrite old bookmark
493 ;; Use the new (NAME . ALIST) format.
494 (setcdr (bookmark-get-bookmark stripped-name) alist)
496 ;; otherwise just cons it onto the front (either the bookmark
497 ;; doesn't exist already, or there is no prefix arg. In either
498 ;; case, we want the new bookmark consed onto the alist...)
500 (push (cons stripped-name alist) bookmark-alist))
502 ;; Added by db
503 (setq bookmark-current-bookmark stripped-name)
504 (setq bookmark-alist-modification-count
505 (1+ bookmark-alist-modification-count))
506 (if (bookmark-time-to-save-p)
507 (bookmark-save))
509 (setq bookmark-current-bookmark stripped-name)
510 (bookmark-bmenu-surreptitiously-rebuild-list)))
512 (defun bookmark-make-record-default (&optional point-only)
513 "Return the record describing the location of a new bookmark.
514 Must be at the correct position in the buffer in which the bookmark is
515 being set.
516 If POINT-ONLY is non-nil, then only return the subset of the
517 record that pertains to the location within the buffer."
518 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
519 (front-context-string
520 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
521 (buffer-substring-no-properties
522 (point)
523 (+ (point) bookmark-search-size))
524 nil))
525 (rear-context-string
526 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
527 (buffer-substring-no-properties
528 (point)
529 (- (point) bookmark-search-size))
530 nil))
531 (position . ,(point))))
534 ;;; File format stuff
536 ;; The OLD format of the bookmark-alist was:
538 ;; ((BOOKMARK-NAME . (FILENAME
539 ;; STRING-IN-FRONT
540 ;; STRING-BEHIND
541 ;; POINT))
542 ;; ...)
544 ;; The NEW format of the bookmark-alist is:
546 ;; ((BOOKMARK-NAME (filename . FILENAME)
547 ;; (front-context-string . STRING-IN-FRONT)
548 ;; (rear-context-string . STRING-BEHIND)
549 ;; (position . POINT)
550 ;; (annotation . ANNOTATION)
551 ;; (whatever . VALUE)
552 ;; ...
553 ;; ))
554 ;; ...)
557 ;; I switched to using an internal as well as external alist because I
558 ;; felt that would be a more flexible framework in which to add
559 ;; features. It means that the order in which values appear doesn't
560 ;; matter, and it means that arbitrary values can be added without
561 ;; risk of interfering with existing ones.
563 ;; BOOKMARK-NAME is the string the user gives the bookmark and
564 ;; accesses it by from then on.
566 ;; FILENAME is the location of the file in which the bookmark is set.
568 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
569 ;; context in front of the point at which the bookmark is set.
571 ;; STRING-BEHIND is the same thing, but after the point.
573 ;; The context strings exist so that modifications to a file don't
574 ;; necessarily cause a bookmark's position to be invalidated.
575 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
576 ;; case the file has changed since the bookmark was set. It will
577 ;; attempt to place the user before the changes, if there were any.
578 ;; ANNOTATION is the annotation for the bookmark; it may not exist
579 ;; (for backward compatibility), be nil (no annotation), or be a
580 ;; string.
583 (defconst bookmark-file-format-version 1
584 "The current version of the format used by bookmark files.
585 You should never need to change this.")
588 (defconst bookmark-end-of-version-stamp-marker
589 "-*- End Of Bookmark File Format Version Stamp -*-\n"
590 "This string marks the end of the version stamp in a bookmark file.")
593 (defun bookmark-alist-from-buffer ()
594 "Return a `bookmark-alist' (in any format) from the current buffer.
595 The buffer must of course contain bookmark format information.
596 Does not care from where in the buffer it is called, and does not
597 affect point."
598 (save-excursion
599 (goto-char (point-min))
600 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
601 (read (current-buffer))
602 ;; Else we're dealing with format version 0
603 (if (search-forward "(" nil t)
604 (progn
605 (forward-char -1)
606 (read (current-buffer)))
607 ;; Else no hope of getting information here.
608 (error "Not bookmark format")))))
611 (defun bookmark-upgrade-version-0-alist (old-list)
612 "Upgrade a version 0 alist OLD-LIST to the current version."
613 (mapcar
614 (lambda (bookmark)
615 (let* ((name (car bookmark))
616 (record (car (cdr bookmark)))
617 (filename (nth 0 record))
618 (front-str (nth 1 record))
619 (rear-str (nth 2 record))
620 (position (nth 3 record))
621 (ann (nth 4 record)))
622 (list
623 name
624 `((filename . ,filename)
625 (front-context-string . ,(or front-str ""))
626 (rear-context-string . ,(or rear-str ""))
627 (position . ,position)
628 (annotation . ,ann)))))
629 old-list))
632 (defun bookmark-upgrade-file-format-from-0 ()
633 "Upgrade a bookmark file of format 0 (the original format) to format 1.
634 This expects to be called from `point-min' in a bookmark file."
635 (message "Upgrading bookmark format from 0 to %d..."
636 bookmark-file-format-version)
637 (let* ((old-list (bookmark-alist-from-buffer))
638 (new-list (bookmark-upgrade-version-0-alist old-list)))
639 (delete-region (point-min) (point-max))
640 (bookmark-insert-file-format-version-stamp)
641 (pp new-list (current-buffer))
642 (save-buffer))
643 (goto-char (point-min))
644 (message "Upgrading bookmark format from 0 to %d...done"
645 bookmark-file-format-version)
649 (defun bookmark-grok-file-format-version ()
650 "Return an integer which is the file-format version of this bookmark file.
651 This expects to be called from `point-min' in a bookmark file."
652 (if (looking-at "^;;;;")
653 (save-excursion
654 (save-match-data
655 (re-search-forward "[0-9]")
656 (forward-char -1)
657 (read (current-buffer))))
658 ;; Else this is format version 0, the original one, which didn't
659 ;; even have version stamps.
663 (defun bookmark-maybe-upgrade-file-format ()
664 "Check the file-format version of this bookmark file.
665 If the version is not up-to-date, upgrade it automatically.
666 This expects to be called from `point-min' in a bookmark file."
667 (let ((version (bookmark-grok-file-format-version)))
668 (cond
669 ((= version bookmark-file-format-version)
670 ) ; home free -- version is current
671 ((= version 0)
672 (bookmark-upgrade-file-format-from-0))
674 (error "Bookmark file format version strangeness")))))
677 (defun bookmark-insert-file-format-version-stamp ()
678 "Insert text indicating current version of bookmark file format."
679 (insert
680 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
681 bookmark-file-format-version))
682 (insert ";;; This format is meant to be slightly human-readable;\n"
683 ";;; nevertheless, you probably don't want to edit it.\n"
684 ";;; "
685 bookmark-end-of-version-stamp-marker))
688 ;;; end file-format stuff
691 ;;; Generic helpers.
693 (defun bookmark-maybe-message (fmt &rest args)
694 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
695 (if (>= baud-rate 9600)
696 (apply 'message fmt args)))
699 ;;; Core code:
701 (defvar bookmark-minibuffer-read-name-map
702 (let ((map (make-sparse-keymap)))
703 (set-keymap-parent map minibuffer-local-map)
704 (define-key map "\C-w" 'bookmark-yank-word)
705 ;; This C-u binding might not be very useful any more now that we
706 ;; provide access to the default via the standard M-n binding.
707 ;; Maybe we should just remove it? --Stef-08
708 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
709 map))
711 ;;;###autoload
712 (defun bookmark-set (&optional name parg)
713 "Set a bookmark named NAME inside a file.
714 If name is nil, then the user will be prompted.
715 With prefix arg, will not overwrite a bookmark that has the same name
716 as NAME if such a bookmark already exists, but instead will \"push\"
717 the new bookmark onto the bookmark alist. Thus the most recently set
718 bookmark with name NAME would be the one in effect at any given time,
719 but the others are still there, should you decide to delete the most
720 recent one.
722 To yank words from the text of the buffer and use them as part of the
723 bookmark name, type C-w while setting a bookmark. Successive C-w's
724 yank successive words.
726 Typing C-u inserts the name of the last bookmark used in the buffer
727 \(as an aid in using a single bookmark name to track your progress
728 through a large file\). If no bookmark was used, then C-u inserts the
729 name of the file being visited.
731 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
732 and it removes only the first instance of a bookmark with that name from
733 the list of bookmarks.\)"
734 (interactive (list nil current-prefix-arg))
735 (let* ((record (bookmark-make-record))
736 (default (car record)))
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 ((str
745 (or name
746 (read-from-minibuffer
747 (format "Set bookmark (%s): " default)
749 bookmark-minibuffer-read-name-map
750 nil nil default))))
751 (and (string-equal str "") (setq str default))
752 (bookmark-store str (cdr record) parg)
754 ;; Ask for an annotation buffer for this bookmark
755 (if bookmark-use-annotations
756 (bookmark-edit-annotation str)
757 (goto-char bookmark-current-point)))))
759 (defun bookmark-kill-line (&optional newline-too)
760 "Kill from point to end of line.
761 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
762 Does not affect the kill ring."
763 (let ((eol (save-excursion (end-of-line) (point))))
764 (delete-region (point) eol)
765 (if (and newline-too (looking-at "\n"))
766 (delete-char 1))))
769 ;; Defvars to avoid compilation warnings:
770 (defvar bookmark-annotation-name nil
771 "Variable holding the name of the bookmark.
772 This is used in `bookmark-edit-annotation' to record the bookmark
773 whose annotation is being edited.")
776 (defun bookmark-default-annotation-text (bookmark)
777 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
778 "# All lines which start with a '#' will be deleted.\n"
779 "# Type C-c C-c when done.\n#\n"
780 "# Author: " (user-full-name) " <" (user-login-name) "@"
781 (system-name) ">\n"
782 "# Date: " (current-time-string) "\n"))
785 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
786 "Function to return default text to use for a bookmark annotation.
787 It takes one argument, the name of the bookmark, as a string.")
788 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
789 'bookmark-edit-annotation-text-func "23.1")
791 (defvar bookmark-edit-annotation-mode-map
792 (let ((map (make-sparse-keymap)))
793 (set-keymap-parent map text-mode-map)
794 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
795 map)
796 "Keymap for editing an annotation of a bookmark.")
799 (defun bookmark-edit-annotation-mode (bookmark)
800 "Mode for editing the annotation of bookmark BOOKMARK.
801 When you have finished composing, type \\[bookmark-send-annotation].
803 \\{bookmark-edit-annotation-mode-map}"
804 (interactive)
805 (kill-all-local-variables)
806 (make-local-variable 'bookmark-annotation-name)
807 (setq bookmark-annotation-name bookmark)
808 (use-local-map bookmark-edit-annotation-mode-map)
809 (setq major-mode 'bookmark-edit-annotation-mode
810 mode-name "Edit Bookmark Annotation")
811 (insert (funcall bookmark-edit-annotation-text-func bookmark))
812 (let ((annotation (bookmark-get-annotation bookmark)))
813 (if (and annotation (not (string-equal annotation "")))
814 (insert annotation)))
815 (run-mode-hooks 'text-mode-hook))
818 (defun bookmark-send-edited-annotation ()
819 "Use buffer contents as annotation for a bookmark.
820 Lines beginning with `#' are ignored."
821 (interactive)
822 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
823 (error "Not in bookmark-edit-annotation-mode"))
824 (goto-char (point-min))
825 (while (< (point) (point-max))
826 (if (looking-at "^#")
827 (bookmark-kill-line t)
828 (forward-line 1)))
829 ;; Take no chances with text properties.
830 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
831 (bookmark bookmark-annotation-name))
832 (bookmark-set-annotation bookmark annotation)
833 (bookmark-bmenu-surreptitiously-rebuild-list)
834 (goto-char bookmark-current-point))
835 (kill-buffer (current-buffer)))
838 (defun bookmark-edit-annotation (bookmark)
839 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
840 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
841 (bookmark-edit-annotation-mode bookmark))
844 (defun bookmark-insert-current-bookmark ()
845 "Insert this buffer's value of `bookmark-current-bookmark'.
846 Default to file name if it's nil."
847 (interactive)
848 (let ((str
849 (with-current-buffer bookmark-current-buffer
850 (or bookmark-current-bookmark
851 (bookmark-buffer-name)))))
852 (insert str)))
855 (defun bookmark-buffer-name ()
856 "Return the name of the current buffer's file, non-directory."
857 (cond
858 ;; Or are we a file?
859 (buffer-file-name (file-name-nondirectory buffer-file-name))
860 ;; Or are we a directory?
861 ((and (boundp 'dired-directory) dired-directory)
862 (let* ((dirname (if (stringp dired-directory)
863 dired-directory
864 (car dired-directory)))
865 (idx (1- (length dirname))))
866 ;; Strip the trailing slash.
867 (if (= ?/ (aref dirname idx))
868 (file-name-nondirectory (substring dirname 0 idx))
869 ;; Else return the current-buffer
870 (buffer-name (current-buffer)))))
871 ;; If all else fails, use the buffer's name.
873 (buffer-name (current-buffer)))))
876 (defun bookmark-yank-word ()
877 (interactive)
878 ;; get the next word from the buffer and append it to the name of
879 ;; the bookmark currently being set.
880 (let ((string (with-current-buffer bookmark-current-buffer
881 (goto-char bookmark-yank-point)
882 (buffer-substring-no-properties
883 (point)
884 (progn
885 (forward-word 1)
886 (setq bookmark-yank-point (point)))))))
887 (insert string)))
889 (defun bookmark-buffer-file-name ()
890 "Return the current buffer's file in a way useful for bookmarks."
891 (cond
892 (buffer-file-name
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 buffer-file-name))
897 ((and (boundp 'dired-directory) dired-directory)
898 (if (stringp dired-directory)
899 dired-directory
900 (car dired-directory)))
901 (t (error "Buffer not visiting a file or directory"))))
904 (defun bookmark-maybe-load-default-file ()
905 (and (not bookmarks-already-loaded)
906 (null bookmark-alist)
907 (prog2
908 (and
909 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
910 ;; to be renamed.
911 (file-exists-p (expand-file-name bookmark-old-default-file))
912 (not (file-exists-p (expand-file-name bookmark-default-file)))
913 (rename-file (expand-file-name bookmark-old-default-file)
914 (expand-file-name bookmark-default-file)))
915 ;; return t so the `and' will continue...
918 (file-readable-p (expand-file-name bookmark-default-file))
919 (bookmark-load bookmark-default-file t t)
920 (setq bookmarks-already-loaded t)))
923 (defun bookmark-maybe-sort-alist ()
924 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
925 ;;is non-nil, then return a sorted copy of the alist.
926 (if bookmark-sort-flag
927 (sort (copy-alist bookmark-alist)
928 (function
929 (lambda (x y) (string-lessp (car x) (car y)))))
930 bookmark-alist))
933 (defvar bookmark-after-jump-hook nil
934 "Hook run after `bookmark-jump' jumps to a bookmark.
935 Useful for example to unhide text in `outline-mode'.")
937 (defun bookmark--jump-via (bookmark display-function)
938 (bookmark-handle-bookmark bookmark)
939 (save-current-buffer
940 (funcall display-function (current-buffer)))
941 (let ((win (get-buffer-window (current-buffer) 0)))
942 (if win (set-window-point win (point))))
943 ;; FIXME: we used to only run bookmark-after-jump-hook in
944 ;; `bookmark-jump' itself, but in none of the other commands.
945 (run-hooks 'bookmark-after-jump-hook)
946 (if bookmark-automatically-show-annotations
947 ;; if there is an annotation for this bookmark,
948 ;; show it in a buffer.
949 (bookmark-show-annotation bookmark)))
952 ;;;###autoload
953 (defun bookmark-jump (bookmark)
954 "Jump to bookmark BOOKMARK (a point in some file).
955 You may have a problem using this function if the value of variable
956 `bookmark-alist' is nil. If that happens, you need to load in some
957 bookmarks. See help on function `bookmark-load' for more about
958 this.
960 If the file pointed to by BOOKMARK no longer exists, you will be asked
961 if you wish to give the bookmark a new location, and `bookmark-jump'
962 will then jump to the new location, as well as recording it in place
963 of the old one in the permanent bookmark record."
964 (interactive
965 (list (bookmark-completing-read "Jump to bookmark"
966 bookmark-current-bookmark)))
967 (unless bookmark
968 (error "No bookmark specified"))
969 (bookmark-maybe-historicize-string bookmark)
970 (bookmark--jump-via bookmark 'switch-to-buffer))
973 ;;;###autoload
974 (defun bookmark-jump-other-window (bookmark)
975 "Jump to BOOKMARK (a point in some file) in another window.
976 See `bookmark-jump'."
977 (interactive
978 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
979 bookmark-current-bookmark)))
980 (if (> emacs-major-version 21)
981 (list bkm) bkm)))
982 (when bookmark
983 (bookmark-maybe-historicize-string bookmark)
984 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
987 (defun bookmark-file-or-variation-thereof (file)
988 "Return FILE (a string) if it exists, or return a reasonable
989 variation of FILE if that exists. Reasonable variations are checked
990 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
991 nor a reasonable variation thereof, then still return FILE if it can
992 be retrieved from a VC backend, else return nil."
993 (if (file-exists-p file)
994 file
996 (progn (require 'info) ; ensure Info-suffix-list is bound
997 (catch 'found
998 (mapc (lambda (elt)
999 (let ((suffixed-file (concat file (car elt))))
1000 (if (file-exists-p suffixed-file)
1001 (throw 'found suffixed-file))))
1002 Info-suffix-list)
1003 nil))
1004 ;; Last possibility: try VC
1005 (if (vc-backend file) file))))
1007 (defun bookmark-jump-noselect (bookmark)
1008 "Return the location pointed to by the bookmark BOOKMARK.
1009 The return value has the form (BUFFER . POINT).
1011 Note: this function is deprecated and is present for Emacs 22
1012 compatibility only. Use `bookmark-handle-bookmark' instead."
1013 (save-excursion
1014 (bookmark-handle-bookmark bookmark)
1015 (cons (current-buffer) (point))))
1017 (defun bookmark-handle-bookmark (bookmark)
1018 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1019 Changes current buffer and point and returns nil, or signals a `file-error'.
1020 BOOKMARK can be a bookmark record used internally by some other
1021 elisp package, or the name of a bookmark to be found in `bookmark-alist'."
1022 (condition-case err
1023 (funcall (or (bookmark-get-handler bookmark)
1024 'bookmark-default-handler)
1025 (bookmark-get-bookmark bookmark))
1026 (file-error
1027 ;; We were unable to find the marked file, so ask if user wants to
1028 ;; relocate the bookmark, else remind them to consider deletion.
1029 (when (stringp bookmark)
1030 ;; `bookmark' can either be a bookmark name (found in
1031 ;; `bookmark-alist') or a bookmark object. If it's an object, we
1032 ;; assume it's a bookmark used internally by some other package.
1033 (let ((file (bookmark-get-filename bookmark)))
1034 (when file ;Don't know how to relocate if there's no `file'.
1035 (setq file (expand-file-name file))
1036 (ding)
1037 (if (y-or-n-p (concat (file-name-nondirectory file)
1038 " nonexistent. Relocate \""
1039 bookmark
1040 "\"? "))
1041 (progn
1042 (bookmark-relocate bookmark)
1043 ;; Try again.
1044 (funcall (or (bookmark-get-handler bookmark)
1045 'bookmark-default-handler)
1046 (bookmark-get-bookmark bookmark)))
1047 (message
1048 "Bookmark not relocated; consider removing it \(%s\)." bookmark)
1049 (signal (car err) (cdr err))))))))
1050 ;; Added by db.
1051 (when (stringp bookmark)
1052 (setq bookmark-current-bookmark bookmark))
1053 nil)
1055 (defun bookmark-default-handler (bmk)
1056 "Default handler to jump to a particular bookmark location.
1057 BMK is a bookmark record.
1058 Changes current buffer and point and returns nil, or signals a `file-error'."
1059 (let* ((file (bookmark-get-filename bmk))
1060 (buf (bookmark-prop-get bmk 'buffer))
1061 (forward-str (bookmark-get-front-context-string bmk))
1062 (behind-str (bookmark-get-rear-context-string bmk))
1063 (place (bookmark-get-position bmk)))
1064 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1065 ;; but now that Info bookmarks are handled elsewhere it seems that we
1066 ;; should be able to get rid of it. --Stef
1067 (if (not (if buf (buffer-live-p buf)
1068 (setq file (bookmark-file-or-variation-thereof file))))
1069 (signal 'file-error
1070 `("Jumping to bookmark" "No such file or directory"
1071 (bookmark-get-filename bmk)))
1072 (set-buffer (or buf (find-file-noselect file)))
1073 (if place (goto-char place))
1075 ;; Go searching forward first. Then, if forward-str exists and
1076 ;; was found in the file, we can search backward for behind-str.
1077 ;; Rationale is that if text was inserted between the two in the
1078 ;; file, it's better to be put before it so you can read it,
1079 ;; rather than after and remain perhaps unaware of the changes.
1080 (if forward-str
1081 (if (search-forward forward-str (point-max) t)
1082 (goto-char (match-beginning 0))))
1083 (if behind-str
1084 (if (search-backward behind-str (point-min) t)
1085 (goto-char (match-end 0)))))
1086 nil))
1088 ;;;###autoload
1089 (defun bookmark-relocate (bookmark)
1090 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1091 This makes an already existing bookmark point to that file, instead of
1092 the one it used to point at. Useful when a file has been renamed
1093 after a bookmark was set in it."
1094 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1095 (bookmark-maybe-historicize-string bookmark)
1096 (bookmark-maybe-load-default-file)
1097 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1098 (newloc (expand-file-name
1099 (read-file-name
1100 (format "Relocate %s to: " bookmark)
1101 (file-name-directory bmrk-filename)))))
1102 (bookmark-set-filename bookmark newloc)
1103 (setq bookmark-alist-modification-count
1104 (1+ bookmark-alist-modification-count))
1105 (if (bookmark-time-to-save-p)
1106 (bookmark-save))
1107 (bookmark-bmenu-surreptitiously-rebuild-list)))
1110 ;;;###autoload
1111 (defun bookmark-insert-location (bookmark &optional no-history)
1112 "Insert the name of the file associated with BOOKMARK.
1113 Optional second arg NO-HISTORY means don't record this in the
1114 minibuffer history list `bookmark-history'."
1115 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1116 (or no-history (bookmark-maybe-historicize-string bookmark))
1117 (let ((start (point)))
1118 (prog1
1119 (insert (bookmark-location bookmark)) ; *Return this line*
1120 (if (and (display-color-p) (display-mouse-p))
1121 (add-text-properties
1122 start
1123 (save-excursion (re-search-backward
1124 "[^ \t]")
1125 (1+ (point)))
1126 '(mouse-face highlight
1127 follow-link t
1128 help-echo "mouse-2: go to this bookmark in other window"))))))
1130 ;;;###autoload
1131 (defalias 'bookmark-locate 'bookmark-insert-location)
1133 (defun bookmark-location (bookmark)
1134 "Return the name of the file associated with BOOKMARK."
1135 (bookmark-maybe-load-default-file)
1136 (bookmark-get-filename bookmark))
1139 ;;;###autoload
1140 (defun bookmark-rename (old &optional new)
1141 "Change the name of OLD bookmark to NEW name.
1142 If called from keyboard, prompt for OLD and NEW. If called from
1143 menubar, select OLD from a menu and prompt for NEW.
1145 If called from Lisp, prompt for NEW if only OLD was passed as an
1146 argument. If called with two strings, then no prompting is done. You
1147 must pass at least OLD when calling from Lisp.
1149 While you are entering the new name, consecutive C-w's insert
1150 consecutive words from the text of the buffer into the new bookmark
1151 name."
1152 (interactive (list (bookmark-completing-read "Old bookmark name")))
1153 (bookmark-maybe-historicize-string old)
1154 (bookmark-maybe-load-default-file)
1156 (setq bookmark-current-point (point))
1157 (setq bookmark-yank-point (point))
1158 (setq bookmark-current-buffer (current-buffer))
1159 (let ((newname
1160 (or new ; use second arg, if non-nil
1161 (read-from-minibuffer
1162 "New name: "
1164 (let ((now-map (copy-keymap minibuffer-local-map)))
1165 (define-key now-map "\C-w" 'bookmark-yank-word)
1166 now-map)
1168 'bookmark-history))))
1169 (bookmark-set-name old newname)
1170 (setq bookmark-current-bookmark newname)
1171 (bookmark-bmenu-surreptitiously-rebuild-list)
1172 (setq bookmark-alist-modification-count
1173 (1+ bookmark-alist-modification-count))
1174 (if (bookmark-time-to-save-p)
1175 (bookmark-save))))
1178 ;;;###autoload
1179 (defun bookmark-insert (bookmark)
1180 "Insert the text of the file pointed to by bookmark BOOKMARK.
1181 You may have a problem using this function if the value of variable
1182 `bookmark-alist' is nil. If that happens, you need to load in some
1183 bookmarks. See help on function `bookmark-load' for more about
1184 this."
1185 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1186 (bookmark-maybe-historicize-string bookmark)
1187 (bookmark-maybe-load-default-file)
1188 (let ((orig-point (point))
1189 (str-to-insert
1190 (save-current-buffer
1191 (bookmark-handle-bookmark bookmark)
1192 (buffer-string))))
1193 (insert str-to-insert)
1194 (push-mark)
1195 (goto-char orig-point)))
1198 ;;;###autoload
1199 (defun bookmark-delete (bookmark &optional batch)
1200 "Delete BOOKMARK from the bookmark list.
1201 Removes only the first instance of a bookmark with that name. If
1202 there are one or more other bookmarks with the same name, they will
1203 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1204 one most recently used in this file, if any\).
1205 Optional second arg BATCH means don't update the bookmark list buffer,
1206 probably because we were called from there."
1207 (interactive
1208 (list (bookmark-completing-read "Delete bookmark"
1209 bookmark-current-bookmark)))
1210 (bookmark-maybe-historicize-string bookmark)
1211 (bookmark-maybe-load-default-file)
1212 (let ((will-go (bookmark-get-bookmark bookmark)))
1213 (setq bookmark-alist (delq will-go bookmark-alist))
1214 ;; Added by db, nil bookmark-current-bookmark if the last
1215 ;; occurrence has been deleted
1216 (or (bookmark-get-bookmark bookmark-current-bookmark)
1217 (setq bookmark-current-bookmark nil)))
1218 ;; Don't rebuild the list
1219 (if batch
1221 (bookmark-bmenu-surreptitiously-rebuild-list)
1222 (setq bookmark-alist-modification-count
1223 (1+ bookmark-alist-modification-count))
1224 (if (bookmark-time-to-save-p)
1225 (bookmark-save))))
1228 (defun bookmark-time-to-save-p (&optional last-time)
1229 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1230 ;; finds out whether it's time to save bookmarks to a file, by
1231 ;; examining the value of variable bookmark-save-flag, and maybe
1232 ;; bookmark-alist-modification-count. Returns t if they should be
1233 ;; saved, nil otherwise. if last-time is non-nil, then this is
1234 ;; being called when emacs is killed.
1235 (cond (last-time
1236 (and (> bookmark-alist-modification-count 0)
1237 bookmark-save-flag))
1238 ((numberp bookmark-save-flag)
1239 (>= bookmark-alist-modification-count bookmark-save-flag))
1241 nil)))
1244 ;;;###autoload
1245 (defun bookmark-write ()
1246 "Write bookmarks to a file (reading the file name with the minibuffer).
1247 Don't use this in Lisp programs; use `bookmark-save' instead."
1248 (interactive)
1249 (bookmark-maybe-load-default-file)
1250 (bookmark-save t))
1253 ;;;###autoload
1254 (defun bookmark-save (&optional parg file)
1255 "Save currently defined bookmarks.
1256 Saves by default in the file defined by the variable
1257 `bookmark-default-file'. With a prefix arg, save it in file FILE
1258 \(second argument\).
1260 If you are calling this from Lisp, the two arguments are PARG and
1261 FILE, and if you just want it to write to the default file, then
1262 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1263 instead. If you pass in one argument, and it is non-nil, then the
1264 user will be interactively queried for a file to save in.
1266 When you want to load in the bookmarks from a file, use
1267 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1268 for a file, defaulting to the file defined by variable
1269 `bookmark-default-file'."
1270 (interactive "P")
1271 (bookmark-maybe-load-default-file)
1272 (cond
1273 ((and (null parg) (null file))
1274 ;;whether interactive or not, write to default file
1275 (bookmark-write-file bookmark-default-file))
1276 ((and (null parg) file)
1277 ;;whether interactive or not, write to given file
1278 (bookmark-write-file file))
1279 ((and parg (not file))
1280 ;;have been called interactively w/ prefix arg
1281 (let ((file (read-file-name "File to save bookmarks in: ")))
1282 (bookmark-write-file file)))
1283 (t ; someone called us with prefix-arg *and* a file, so just write to file
1284 (bookmark-write-file file)))
1285 ;; signal that we have synced the bookmark file by setting this to
1286 ;; 0. If there was an error at any point before, it will not get
1287 ;; set, which is what we want.
1288 (setq bookmark-alist-modification-count 0))
1292 (defun bookmark-write-file (file)
1293 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1294 (with-current-buffer (get-buffer-create " *Bookmarks*")
1295 (goto-char (point-min))
1296 (delete-region (point-min) (point-max))
1297 (let ((print-length nil)
1298 (print-level nil))
1299 (bookmark-insert-file-format-version-stamp)
1300 (pp bookmark-alist (current-buffer))
1301 (let ((version-control
1302 (cond
1303 ((null bookmark-version-control) nil)
1304 ((eq 'never bookmark-version-control) 'never)
1305 ((eq 'nospecial bookmark-version-control) version-control)
1306 (t t))))
1307 (condition-case nil
1308 (write-region (point-min) (point-max) file)
1309 (file-error (message "Can't write %s" file)))
1310 (kill-buffer (current-buffer))
1311 (bookmark-maybe-message
1312 "Saving bookmarks to file %s...done" file)))))
1315 (defun bookmark-import-new-list (new-list)
1316 ;; Walk over the new list, adding each individual bookmark
1317 ;; carefully. "Carefully" means checking against the existing
1318 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1319 ;; as necessary.
1320 (let ((lst new-list)
1321 (names (bookmark-all-names)))
1322 (while lst
1323 (let* ((full-record (car lst)))
1324 (bookmark-maybe-rename full-record names)
1325 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1326 (setq names (cons (bookmark-name-from-full-record full-record) names))
1327 (setq lst (cdr lst))))))
1330 (defun bookmark-maybe-rename (full-record names)
1331 ;; just a helper for bookmark-import-new-list; it is only for
1332 ;; readability that this is not inlined.
1334 ;; Once this has found a free name, it sets full-record to that
1335 ;; name.
1336 (let ((found-name (bookmark-name-from-full-record full-record)))
1337 (if (member found-name names)
1338 ;; We've got a conflict, so generate a new name
1339 (let ((count 2)
1340 (new-name found-name))
1341 (while (member new-name names)
1342 (setq new-name (concat found-name (format "<%d>" count)))
1343 (setq count (1+ count)))
1344 (bookmark-set-name full-record new-name)))))
1347 ;;;###autoload
1348 (defun bookmark-load (file &optional overwrite no-msg)
1349 "Load bookmarks from FILE (which must be in bookmark format).
1350 Appends loaded bookmarks to the front of the list of bookmarks. If
1351 optional second argument OVERWRITE is non-nil, existing bookmarks are
1352 destroyed. Optional third arg NO-MSG means don't display any messages
1353 while loading.
1355 If you load a file that doesn't contain a proper bookmark alist, you
1356 will corrupt Emacs's bookmark list. Generally, you should only load
1357 in files that were created with the bookmark functions in the first
1358 place. Your own personal bookmark file, `~/.emacs.bmk', is
1359 maintained automatically by Emacs; you shouldn't need to load it
1360 explicitly.
1362 If you load a file containing bookmarks with the same names as
1363 bookmarks already present in your Emacs, the new bookmarks will get
1364 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1365 method buffers use to resolve name collisions."
1366 (interactive
1367 (list (read-file-name
1368 (format "Load bookmarks from: (%s) "
1369 bookmark-default-file)
1370 ;;Default might not be used often,
1371 ;;but there's no better default, and
1372 ;;I guess it's better than none at all.
1373 "~/" bookmark-default-file 'confirm)))
1374 (setq file (expand-file-name file))
1375 (if (not (file-readable-p file))
1376 (error "Cannot read bookmark file %s" file)
1377 (if (null no-msg)
1378 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1379 (with-current-buffer (let ((enable-local-variables nil))
1380 (find-file-noselect file))
1381 (goto-char (point-min))
1382 (bookmark-maybe-upgrade-file-format)
1383 (let ((blist (bookmark-alist-from-buffer)))
1384 (if (listp blist)
1385 (progn
1386 (if overwrite
1387 (progn
1388 (setq bookmark-alist blist)
1389 (setq bookmark-alist-modification-count 0))
1390 ;; else
1391 (bookmark-import-new-list blist)
1392 (setq bookmark-alist-modification-count
1393 (1+ bookmark-alist-modification-count)))
1394 (if (string-equal
1395 (expand-file-name bookmark-default-file)
1396 file)
1397 (setq bookmarks-already-loaded t))
1398 (bookmark-bmenu-surreptitiously-rebuild-list))
1399 (error "Invalid bookmark list in %s" file)))
1400 (kill-buffer (current-buffer)))
1401 (if (null no-msg)
1402 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1406 ;;; Code supporting the dired-like bookmark menu.
1407 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1410 (defvar bookmark-bmenu-bookmark-column nil)
1413 (defvar bookmark-bmenu-hidden-bookmarks ())
1416 (defvar bookmark-bmenu-mode-map nil)
1419 (if bookmark-bmenu-mode-map
1421 (setq bookmark-bmenu-mode-map (make-keymap))
1422 (suppress-keymap bookmark-bmenu-mode-map t)
1423 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1424 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1425 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1426 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1427 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1428 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1429 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1430 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1431 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1432 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1433 (define-key bookmark-bmenu-mode-map "\C-o"
1434 'bookmark-bmenu-switch-other-window)
1435 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1436 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1437 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1438 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1439 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1440 (define-key bookmark-bmenu-mode-map " " 'next-line)
1441 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1442 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1443 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1444 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1445 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1446 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1447 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1448 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1449 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1450 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1451 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1452 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1453 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1454 (define-key bookmark-bmenu-mode-map [mouse-2]
1455 'bookmark-bmenu-other-window-with-mouse))
1459 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1460 ;; data.
1461 (put 'bookmark-bmenu-mode 'mode-class 'special)
1464 ;; todo: need to display whether or not bookmark exists as a buffer in
1465 ;; flag column.
1467 ;; Format:
1468 ;; FLAGS BOOKMARK [ LOCATION ]
1471 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1472 "Rebuild the Bookmark List if it exists.
1473 Don't affect the buffer ring order."
1474 (if (get-buffer "*Bookmark List*")
1475 (save-excursion
1476 (save-window-excursion
1477 (bookmark-bmenu-list)))))
1480 ;;;###autoload
1481 (defun bookmark-bmenu-list ()
1482 "Display a list of existing bookmarks.
1483 The list is displayed in a buffer named `*Bookmark List*'.
1484 The leftmost column displays a D if the bookmark is flagged for
1485 deletion, or > if it is flagged for displaying."
1486 (interactive)
1487 (bookmark-maybe-load-default-file)
1488 (if (interactive-p)
1489 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1490 (set-buffer (get-buffer-create "*Bookmark List*")))
1491 (let ((inhibit-read-only t))
1492 (erase-buffer)
1493 (insert "% Bookmark\n- --------\n")
1494 (add-text-properties (point-min) (point)
1495 '(font-lock-face bookmark-menu-heading))
1496 (mapc
1497 (lambda (full-record)
1498 ;; if a bookmark has an annotation, prepend a "*"
1499 ;; in the list of bookmarks.
1500 (let ((annotation (bookmark-get-annotation
1501 (bookmark-name-from-full-record full-record))))
1502 (if (and annotation (not (string-equal annotation "")))
1503 (insert " *")
1504 (insert " "))
1505 (let ((start (point)))
1506 (insert (bookmark-name-from-full-record full-record))
1507 (if (and (display-color-p) (display-mouse-p))
1508 (add-text-properties
1509 start
1510 (save-excursion (re-search-backward
1511 "[^ \t]")
1512 (1+ (point)))
1513 '(mouse-face highlight
1514 follow-link t
1515 help-echo "mouse-2: go to this bookmark in other window")))
1516 (insert "\n")
1518 (bookmark-maybe-sort-alist)))
1519 (goto-char (point-min))
1520 (forward-line 2)
1521 (bookmark-bmenu-mode)
1522 (if bookmark-bmenu-toggle-filenames
1523 (bookmark-bmenu-toggle-filenames t)))
1525 ;;;###autoload
1526 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1527 ;;;###autoload
1528 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1532 (defun bookmark-bmenu-mode ()
1533 "Major mode for editing a list of bookmarks.
1534 Each line describes one of the bookmarks in Emacs.
1535 Letters do not insert themselves; instead, they are commands.
1536 Bookmark names preceded by a \"*\" have annotations.
1537 \\<bookmark-bmenu-mode-map>
1538 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1539 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1540 Also show bookmarks marked using m in other windows.
1541 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1542 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1543 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1544 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1545 together with bookmark selected before this one in another window.
1546 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1547 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1548 so the bookmark menu bookmark remains visible in its window.
1549 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1550 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1551 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1552 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1553 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1554 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1555 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1556 With a prefix arg, prompts for a file to save in.
1557 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1558 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1559 With prefix argument, also move up one line.
1560 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1561 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1562 in another buffer.
1563 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1564 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1565 (kill-all-local-variables)
1566 (use-local-map bookmark-bmenu-mode-map)
1567 (setq truncate-lines t)
1568 (setq buffer-read-only t)
1569 (setq major-mode 'bookmark-bmenu-mode)
1570 (setq mode-name "Bookmark Menu")
1571 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1574 (defun bookmark-bmenu-toggle-filenames (&optional show)
1575 "Toggle whether filenames are shown in the bookmark list.
1576 Optional argument SHOW means show them unconditionally."
1577 (interactive)
1578 (cond
1579 (show
1580 (setq bookmark-bmenu-toggle-filenames nil)
1581 (bookmark-bmenu-show-filenames)
1582 (setq bookmark-bmenu-toggle-filenames t))
1583 (bookmark-bmenu-toggle-filenames
1584 (bookmark-bmenu-hide-filenames)
1585 (setq bookmark-bmenu-toggle-filenames nil))
1587 (bookmark-bmenu-show-filenames)
1588 (setq bookmark-bmenu-toggle-filenames t))))
1591 (defun bookmark-bmenu-show-filenames (&optional force)
1592 (if (and (not force) bookmark-bmenu-toggle-filenames)
1593 nil ;already shown, so do nothing
1594 (save-excursion
1595 (save-window-excursion
1596 (goto-char (point-min))
1597 (forward-line 2)
1598 (setq bookmark-bmenu-hidden-bookmarks ())
1599 (let ((inhibit-read-only t))
1600 (while (< (point) (point-max))
1601 (let ((bmrk (bookmark-bmenu-bookmark)))
1602 (setq bookmark-bmenu-hidden-bookmarks
1603 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1604 (let ((start (save-excursion (end-of-line) (point))))
1605 (move-to-column bookmark-bmenu-file-column t)
1606 ;; Strip off `mouse-face' from the white spaces region.
1607 (if (and (display-color-p) (display-mouse-p))
1608 (remove-text-properties start (point)
1609 '(mouse-face nil help-echo nil))))
1610 (delete-region (point) (progn (end-of-line) (point)))
1611 (insert " ")
1612 ;; Pass the NO-HISTORY arg:
1613 (bookmark-insert-location bmrk t)
1614 (forward-line 1))))))))
1617 (defun bookmark-bmenu-hide-filenames (&optional force)
1618 (if (and (not force) bookmark-bmenu-toggle-filenames)
1619 ;; nothing to hide if above is nil
1620 (save-excursion
1621 (save-window-excursion
1622 (goto-char (point-min))
1623 (forward-line 2)
1624 (setq bookmark-bmenu-hidden-bookmarks
1625 (nreverse bookmark-bmenu-hidden-bookmarks))
1626 (save-excursion
1627 (goto-char (point-min))
1628 (search-forward "Bookmark")
1629 (backward-word 1)
1630 (setq bookmark-bmenu-bookmark-column (current-column)))
1631 (save-excursion
1632 (let ((inhibit-read-only t))
1633 (while bookmark-bmenu-hidden-bookmarks
1634 (move-to-column bookmark-bmenu-bookmark-column t)
1635 (bookmark-kill-line)
1636 (let ((start (point)))
1637 (insert (car bookmark-bmenu-hidden-bookmarks))
1638 (if (and (display-color-p) (display-mouse-p))
1639 (add-text-properties
1640 start
1641 (save-excursion (re-search-backward
1642 "[^ \t]")
1643 (1+ (point)))
1644 '(mouse-face highlight
1645 follow-link t
1646 help-echo
1647 "mouse-2: go to this bookmark in other window"))))
1648 (setq bookmark-bmenu-hidden-bookmarks
1649 (cdr bookmark-bmenu-hidden-bookmarks))
1650 (forward-line 1))))))))
1653 (defun bookmark-bmenu-check-position ()
1654 ;; Returns non-nil if on a line with a bookmark.
1655 ;; (The actual value returned is bookmark-alist).
1656 ;; Else reposition and try again, else return nil.
1657 (cond ((< (count-lines (point-min) (point)) 2)
1658 (goto-char (point-min))
1659 (forward-line 2)
1660 bookmark-alist)
1661 ((and (bolp) (eobp))
1662 (beginning-of-line 0)
1663 bookmark-alist)
1665 bookmark-alist)))
1668 (defun bookmark-bmenu-bookmark ()
1669 ;; return a string which is bookmark of this line.
1670 (if (bookmark-bmenu-check-position)
1671 (save-excursion
1672 (save-window-excursion
1673 (goto-char (point-min))
1674 (search-forward "Bookmark")
1675 (backward-word 1)
1676 (setq bookmark-bmenu-bookmark-column (current-column)))))
1677 (if bookmark-bmenu-toggle-filenames
1678 (bookmark-bmenu-hide-filenames))
1679 (save-excursion
1680 (save-window-excursion
1681 (beginning-of-line)
1682 (forward-char bookmark-bmenu-bookmark-column)
1683 (prog1
1684 (buffer-substring-no-properties (point)
1685 (progn
1686 (end-of-line)
1687 (point)))
1688 ;; well, this is certainly crystal-clear:
1689 (if bookmark-bmenu-toggle-filenames
1690 (bookmark-bmenu-toggle-filenames t))))))
1693 (defun bookmark-show-annotation (bookmark)
1694 "Display the annotation for bookmark named BOOKMARK in a buffer,
1695 if an annotation exists."
1696 (let ((annotation (bookmark-get-annotation bookmark)))
1697 (if (and annotation (not (string-equal annotation "")))
1698 (save-excursion
1699 (let ((old-buf (current-buffer)))
1700 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1701 (delete-region (point-min) (point-max))
1702 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1703 (insert annotation)
1704 (goto-char (point-min))
1705 (pop-to-buffer old-buf))))))
1708 (defun bookmark-show-all-annotations ()
1709 "Display the annotations for all bookmarks in a buffer."
1710 (let ((old-buf (current-buffer)))
1711 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1712 (delete-region (point-min) (point-max))
1713 (mapc
1714 (lambda (full-record)
1715 (let* ((name (bookmark-name-from-full-record full-record))
1716 (ann (bookmark-get-annotation name)))
1717 (insert (concat name ":\n"))
1718 (if (and ann (not (string-equal ann "")))
1719 ;; insert the annotation, indented by 4 spaces.
1720 (progn
1721 (save-excursion (insert ann) (unless (bolp)
1722 (insert "\n")))
1723 (while (< (point) (point-max))
1724 (beginning-of-line) ; paranoia
1725 (insert " ")
1726 (forward-line)
1727 (end-of-line))))))
1728 bookmark-alist)
1729 (goto-char (point-min))
1730 (pop-to-buffer old-buf)))
1733 (defun bookmark-bmenu-mark ()
1734 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1735 (interactive)
1736 (beginning-of-line)
1737 (if (bookmark-bmenu-check-position)
1738 (let ((inhibit-read-only t))
1739 (delete-char 1)
1740 (insert ?>)
1741 (forward-line 1)
1742 (bookmark-bmenu-check-position))))
1745 (defun bookmark-bmenu-select ()
1746 "Select this line's bookmark; also display bookmarks marked with `>'.
1747 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1748 (interactive)
1749 (if (bookmark-bmenu-check-position)
1750 (let ((bmrk (bookmark-bmenu-bookmark))
1751 (menu (current-buffer))
1752 (others ())
1753 tem)
1754 (goto-char (point-min))
1755 (while (re-search-forward "^>" nil t)
1756 (setq tem (bookmark-bmenu-bookmark))
1757 (let ((inhibit-read-only t))
1758 (delete-char -1)
1759 (insert ?\s))
1760 (or (string-equal tem bmrk)
1761 (member tem others)
1762 (setq others (cons tem others))))
1763 (setq others (nreverse others)
1764 tem (/ (1- (frame-height)) (1+ (length others))))
1765 (delete-other-windows)
1766 (bookmark-jump bmrk)
1767 (bury-buffer menu)
1768 (if others
1769 (while others
1770 (split-window nil tem)
1771 (other-window 1)
1772 (bookmark-jump (car others))
1773 (setq others (cdr others)))
1774 (other-window 1)))))
1777 (defun bookmark-bmenu-save (parg)
1778 "Save the current list into a bookmark file.
1779 With a prefix arg, prompts for a file to save them in."
1780 (interactive "P")
1781 (save-excursion
1782 (save-window-excursion
1783 (bookmark-save parg))))
1786 (defun bookmark-bmenu-load ()
1787 "Load the bookmark file and rebuild the bookmark menu-buffer."
1788 (interactive)
1789 (if (bookmark-bmenu-check-position)
1790 (save-excursion
1791 (save-window-excursion
1792 ;; This will call `bookmark-bmenu-list'
1793 (call-interactively 'bookmark-load)))))
1796 (defun bookmark-bmenu-1-window ()
1797 "Select this line's bookmark, alone, in full frame."
1798 (interactive)
1799 (if (bookmark-bmenu-check-position)
1800 (progn
1801 (bookmark-jump (bookmark-bmenu-bookmark))
1802 (bury-buffer (other-buffer))
1803 (delete-other-windows))))
1806 (defun bookmark-bmenu-2-window ()
1807 "Select this line's bookmark, with previous buffer in second window."
1808 (interactive)
1809 (if (bookmark-bmenu-check-position)
1810 (let ((bmrk (bookmark-bmenu-bookmark))
1811 (menu (current-buffer))
1812 (pop-up-windows t))
1813 (delete-other-windows)
1814 (switch-to-buffer (other-buffer))
1815 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1816 (bookmark--jump-via bmrk 'pop-to-buffer))
1817 (bury-buffer menu))))
1820 (defun bookmark-bmenu-this-window ()
1821 "Select this line's bookmark in this window."
1822 (interactive)
1823 (if (bookmark-bmenu-check-position)
1824 (bookmark-jump (bookmark-bmenu-bookmark))))
1827 (defun bookmark-bmenu-other-window ()
1828 "Select this line's bookmark in other window, leaving bookmark menu visible."
1829 (interactive)
1830 (let ((bookmark (bookmark-bmenu-bookmark)))
1831 (if (bookmark-bmenu-check-position)
1832 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1833 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1836 (defun bookmark-bmenu-switch-other-window ()
1837 "Make the other window select this line's bookmark.
1838 The current window remains selected."
1839 (interactive)
1840 (let ((bookmark (bookmark-bmenu-bookmark))
1841 (pop-up-windows t)
1842 same-window-buffer-names
1843 same-window-regexps)
1844 (if (bookmark-bmenu-check-position)
1845 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1846 (bookmark--jump-via bookmark 'display-buffer)))))
1848 (defun bookmark-bmenu-other-window-with-mouse (event)
1849 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1850 (interactive "e")
1851 (with-current-buffer (window-buffer (posn-window (event-end event)))
1852 (save-excursion
1853 (goto-char (posn-point (event-end event)))
1854 (bookmark-bmenu-other-window))))
1857 (defun bookmark-bmenu-show-annotation ()
1858 "Show the annotation for the current bookmark in another window."
1859 (interactive)
1860 (let ((bookmark (bookmark-bmenu-bookmark)))
1861 (if (bookmark-bmenu-check-position)
1862 (bookmark-show-annotation bookmark))))
1865 (defun bookmark-bmenu-show-all-annotations ()
1866 "Show the annotation for all bookmarks in another window."
1867 (interactive)
1868 (bookmark-show-all-annotations))
1871 (defun bookmark-bmenu-edit-annotation ()
1872 "Edit the annotation for the current bookmark in another window."
1873 (interactive)
1874 (let ((bookmark (bookmark-bmenu-bookmark)))
1875 (if (bookmark-bmenu-check-position)
1876 (bookmark-edit-annotation bookmark))))
1879 (defun bookmark-bmenu-unmark (&optional backup)
1880 "Cancel all requested operations on bookmark on this line and move down.
1881 Optional BACKUP means move up."
1882 (interactive "P")
1883 (beginning-of-line)
1884 (if (bookmark-bmenu-check-position)
1885 (progn
1886 (let ((inhibit-read-only t))
1887 (delete-char 1)
1888 ;; any flags to reset according to circumstances? How about a
1889 ;; flag indicating whether this bookmark is being visited?
1890 ;; well, we don't have this now, so maybe later.
1891 (insert " "))
1892 (forward-line (if backup -1 1))
1893 (bookmark-bmenu-check-position))))
1896 (defun bookmark-bmenu-backup-unmark ()
1897 "Move up and cancel all requested operations on bookmark on line above."
1898 (interactive)
1899 (forward-line -1)
1900 (if (bookmark-bmenu-check-position)
1901 (progn
1902 (bookmark-bmenu-unmark)
1903 (forward-line -1)
1904 (bookmark-bmenu-check-position))))
1907 (defun bookmark-bmenu-delete ()
1908 "Mark bookmark on this line to be deleted.
1909 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1910 (interactive)
1911 (beginning-of-line)
1912 (if (bookmark-bmenu-check-position)
1913 (let ((inhibit-read-only t))
1914 (delete-char 1)
1915 (insert ?D)
1916 (forward-line 1)
1917 (bookmark-bmenu-check-position))))
1920 (defun bookmark-bmenu-delete-backwards ()
1921 "Mark bookmark on this line to be deleted, then move up one line.
1922 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1923 (interactive)
1924 (bookmark-bmenu-delete)
1925 (forward-line -2)
1926 (if (bookmark-bmenu-check-position)
1927 (forward-line 1))
1928 (bookmark-bmenu-check-position))
1931 (defun bookmark-bmenu-execute-deletions ()
1932 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1933 (interactive)
1934 (message "Deleting bookmarks...")
1935 (let ((hide-em bookmark-bmenu-toggle-filenames)
1936 (o-point (point))
1937 (o-str (save-excursion
1938 (beginning-of-line)
1939 (if (looking-at "^D")
1941 (buffer-substring
1942 (point)
1943 (progn (end-of-line) (point))))))
1944 (o-col (current-column)))
1945 (if hide-em (bookmark-bmenu-hide-filenames))
1946 (setq bookmark-bmenu-toggle-filenames nil)
1947 (goto-char (point-min))
1948 (forward-line 1)
1949 (while (re-search-forward "^D" (point-max) t)
1950 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1951 (bookmark-bmenu-list)
1952 (setq bookmark-bmenu-toggle-filenames hide-em)
1953 (if bookmark-bmenu-toggle-filenames
1954 (bookmark-bmenu-toggle-filenames t))
1955 (if o-str
1956 (progn
1957 (goto-char (point-min))
1958 (search-forward o-str)
1959 (beginning-of-line)
1960 (forward-char o-col))
1961 (goto-char o-point))
1962 (beginning-of-line)
1963 (setq bookmark-alist-modification-count
1964 (1+ bookmark-alist-modification-count))
1965 (if (bookmark-time-to-save-p)
1966 (bookmark-save))
1967 (message "Deleting bookmarks...done")
1971 (defun bookmark-bmenu-rename ()
1972 "Rename bookmark on current line. Prompts for a new name."
1973 (interactive)
1974 (if (bookmark-bmenu-check-position)
1975 (let ((bmrk (bookmark-bmenu-bookmark))
1976 (thispoint (point)))
1977 (bookmark-rename bmrk)
1978 (bookmark-bmenu-list)
1979 (goto-char thispoint))))
1982 (defun bookmark-bmenu-locate ()
1983 "Display location of this bookmark. Displays in the minibuffer."
1984 (interactive)
1985 (if (bookmark-bmenu-check-position)
1986 (let ((bmrk (bookmark-bmenu-bookmark)))
1987 (message "%s" (bookmark-location bmrk)))))
1989 (defun bookmark-bmenu-relocate ()
1990 "Change the file path of the bookmark on the current line,
1991 prompting with completion for the new path."
1992 (interactive)
1993 (if (bookmark-bmenu-check-position)
1994 (let ((bmrk (bookmark-bmenu-bookmark))
1995 (thispoint (point)))
1996 (bookmark-relocate bmrk)
1997 (goto-char thispoint))))
2000 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2002 (defun bookmark-menu-popup-paned-menu (event name entries)
2003 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2004 That is, ENTRIES is a list of strings which appear as the choices
2005 in the menu.
2006 The number of panes depends on the number of entries.
2007 The visible entries are truncated to `bookmark-menu-length', but the
2008 strings returned are not."
2009 (let ((f-height (/ (frame-height) 2))
2010 (pane-list nil)
2011 (iter 0))
2012 (while entries
2013 (let (lst
2014 (count 0))
2015 (while (and (< count f-height) entries)
2016 (let ((str (car entries)))
2017 (push (cons
2018 (if (> (length str) bookmark-menu-length)
2019 (substring str 0 bookmark-menu-length)
2020 str)
2021 str)
2022 lst)
2023 (setq entries (cdr entries))
2024 (setq count (1+ count))))
2025 (setq iter (1+ iter))
2026 (push (cons
2027 (format "-*- %s (%d) -*-" name iter)
2028 (nreverse lst))
2029 pane-list)))
2031 ;; Popup the menu and return the string.
2032 (x-popup-menu event (cons (concat "-*- " name " -*-")
2033 (nreverse pane-list)))))
2036 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2037 ;; following works, and for explaining what to do to make it work.
2039 ;; We MUST autoload EACH form used to set up this variable's value, so
2040 ;; that the whole job is done in loaddefs.el.
2042 ;; Emacs menubar stuff.
2044 ;;;###autoload
2045 (defvar menu-bar-bookmark-map
2046 (let ((map (make-sparse-keymap "Bookmark functions")))
2047 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2048 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2049 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2050 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2051 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2052 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2053 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2054 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2055 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2056 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2057 map))
2059 ;;;###autoload
2060 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2062 ;; make bookmarks appear toward the right side of the menu.
2063 (if (boundp 'menu-bar-final-items)
2064 (if menu-bar-final-items
2065 (setq menu-bar-final-items
2066 (cons 'bookmark menu-bar-final-items)))
2067 (setq menu-bar-final-items '(bookmark)))
2069 ;;;; end bookmark menu stuff ;;;;
2072 ;; Load Hook
2073 (defvar bookmark-load-hook nil
2074 "Hook run at the end of loading bookmark.")
2076 ;; Exit Hook, called from kill-emacs-hook
2077 (defvar bookmark-exit-hook nil
2078 "Hook run when Emacs exits.")
2080 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2082 (defun bookmark-exit-hook-internal ()
2083 "Save bookmark state, if necessary, at Emacs exit time.
2084 This also runs `bookmark-exit-hook'."
2085 (run-hooks 'bookmark-exit-hook)
2086 (and bookmark-alist
2087 (bookmark-time-to-save-p t)
2088 (bookmark-save)))
2090 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2092 (defun bookmark-unload-function ()
2093 "Unload the Bookmark library."
2094 (when bookmark-save-flag (bookmark-save))
2095 ;; continue standard unloading
2096 nil)
2099 (run-hooks 'bookmark-load-hook)
2101 (provide 'bookmark)
2103 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2104 ;;; bookmark.el ends here