* bookmark.el
[emacs.git] / lisp / bookmark.el
blob6c9559de5ae3d9e12d60f1dba7b731939f530bd4
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders, annotations
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package is for setting "bookmarks" in files. A bookmark
29 ;; associates a string with a location in a certain file. Thus, you
30 ;; can navigate your way to that location by providing the string.
31 ;; See the "User Variables" section for customizations.
33 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
34 ;; then implementing the bookmark-current-bookmark idea. He even
35 ;; sent *patches*, bless his soul...
37 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
38 ;; fixing and improving bookmark-time-to-save-p.
40 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
41 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
42 ;; and the menu-bar).
44 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
45 ;; suggestions and the code to implement them (like
46 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
47 ;; stuff).
49 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
50 ;; for his eminently sensible suggestion to separate bookmark-jump
51 ;; into bookmark-jump and bookmark-jump-noselect, which made many
52 ;; other things cleaner as well.
54 ;; Thanks to Roland McGrath for encouragement and help with defining
55 ;; autoloads on the menu-bar.
57 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
58 ;; values in bookmark-jump and bookmark-set. Everybody please keep
59 ;; all the keystrokes they save thereby and send them to him at the
60 ;; end of each year :-) (No, seriously, thanks Jonathan!)
62 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
63 ;; thinking up the annotations feature and implementing it so well.
65 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
66 ;; <olstad@msc.edu>.
68 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
69 ;; reported and fixed.
71 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
73 ;; Enough with the credits already, get on to the good stuff:
75 ;; FAVORITE CHINESE RESTAURANT:
76 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
77 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
79 ;;; Code:
81 (require 'pp)
82 (eval-when-compile (require 'cl))
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom bookmark-save-flag t
108 "Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "Non-nil means show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'bookmark)
200 (defface bookmark-menu-heading
201 '((t (:inherit font-lock-type-face)))
202 "Face used to highlight the heading in bookmark menu buffers."
203 :group 'bookmark
204 :version "22.1")
207 ;;; No user-serviceable parts beyond this point.
209 ;; Added for lucid emacs compatibility, db
210 (or (fboundp 'defalias) (fset 'defalias 'fset))
212 ;; suggested for lucid compatibility by david hughes:
213 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
216 ;;; Keymap stuff:
218 ;; Set up these bindings dumping time *only*;
219 ;; if the user alters them, don't override the user when loading bookmark.el.
221 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
222 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
223 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
225 ;;;###autoload
226 (defvar bookmark-map
227 (let ((map (make-sparse-keymap)))
228 ;; Read the help on all of these functions for details...
229 (define-key map "x" 'bookmark-set)
230 (define-key map "m" 'bookmark-set) ;"m"ark
231 (define-key map "j" 'bookmark-jump)
232 (define-key map "g" 'bookmark-jump) ;"g"o
233 (define-key map "o" 'bookmark-jump-other-window)
234 (define-key map "i" 'bookmark-insert)
235 (define-key map "e" 'edit-bookmarks)
236 (define-key map "f" 'bookmark-insert-location) ;"f"ind
237 (define-key map "r" 'bookmark-rename)
238 (define-key map "d" 'bookmark-delete)
239 (define-key map "l" 'bookmark-load)
240 (define-key map "w" 'bookmark-write)
241 (define-key map "s" 'bookmark-save)
242 map)
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
249 ;;;###autoload (fset 'bookmark-map bookmark-map)
252 ;;; Core variables and data structures:
253 (defvar bookmark-alist ()
254 "Association list of bookmarks and their records.
255 You probably don't want to change the value of this alist yourself;
256 instead, let the various bookmark functions do it for you.
258 The format of the alist is
260 \(BOOKMARK1 BOOKMARK2 ...\)
262 where each BOOKMARK is of the form
264 (NAME PARAM-ALIST) or (NAME . PARAM-ALIST)
266 where the first form is the old deprecated one and the second is
267 the new favored one. PARAM-ALIST is typically of the form:
269 ((filename . FILE)
270 (front-context-string . FRONT-STR)
271 (rear-context-string . REAR-STR)
272 (position . POS)
273 (handler . HANDLER-FUNC)
274 (annotation . ANNOTATION))
276 If the element `(handler . HANDLER-FUNC)' is present, HANDLER-FUNC
277 will be used to open this bookmark instead of `bookmark-default-handler',
278 whose calling discipline HANDLER-FUNC should of course match.")
281 (defvar bookmarks-already-loaded nil
282 "Non-nil iff bookmarks have been loaded from `bookmark-default-file'.")
285 ;; more stuff added by db.
287 (defvar bookmark-current-bookmark nil
288 "Name of bookmark most recently used in the current file.
289 It is buffer local, used to make moving a bookmark forward
290 through a file easier.")
292 (make-variable-buffer-local 'bookmark-current-bookmark)
295 (defvar bookmark-alist-modification-count 0
296 "Number of modifications to bookmark list since it was last saved.")
299 (defvar bookmark-search-size 16
300 "Length of the context strings recorded on either side of a bookmark.")
303 (defvar bookmark-current-buffer nil
304 "The buffer in which a bookmark is currently being set or renamed.
305 Functions that insert strings into the minibuffer use this to know
306 the source buffer for that information; see `bookmark-yank-word' and
307 `bookmark-insert-current-bookmark' for example.")
310 (defvar bookmark-yank-point 0
311 "The next point from which to pull source text for `bookmark-yank-word'.
312 This point is in `bookmark-curent-buffer'.")
316 ;; Helper functions.
318 ;; Only functions on this page and the next one (file formats) need to
319 ;; know anything about the format of bookmark-alist entries.
320 ;; Everyone else should go through them.
323 (defun bookmark-name-from-full-record (full-record)
324 "Return name of FULL-RECORD \(an alist element instead of a string\)."
325 (car full-record))
328 (defun bookmark-all-names ()
329 "Return a list of all current bookmark names."
330 (bookmark-maybe-load-default-file)
331 (mapcar 'bookmark-name-from-full-record bookmark-alist))
334 (defun bookmark-get-bookmark (bookmark &optional noerror)
335 "Return the bookmark record corresponding to BOOKMARK.
336 If BOOKMARK is a string, look for the corresponding bookmark record in
337 `bookmark-alist'; return it if found, otherwise error. Else if
338 BOOKMARK is already a bookmark record, just return it."
339 (cond
340 ((consp bookmark) bookmark)
341 ((stringp bookmark)
342 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
343 (unless noerror (error "Invalid bookmark %s" bookmark))))))
346 (defun bookmark-get-bookmark-record (bookmark)
347 "Return the record portion of the entry for BOOKMARK in
348 `bookmark-alist' (that is, all information but the name).
349 BOOKMARK may be a bookmark name (a string) or a bookmark record."
350 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
351 ;; The bookmark objects can either look like (NAME ALIST) or
352 ;; (NAME . ALIST), so we have to distinguish the two here.
353 (if (and (null (cdr alist)) (consp (caar alist)))
354 (car alist) alist)))
357 (defun bookmark-set-name (bookmark newname)
358 "Set BOOKMARK's name to NEWNAME.
359 BOOKMARK may be a bookmark name (a string) or a bookmark record."
360 (setcar
361 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
362 newname))
364 (defun bookmark-prop-get (bookmark prop)
365 "Return the property PROP of BOOKMARK, or nil if none.
366 BOOKMARK may be a bookmark name (a string) or a bookmark record."
367 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
369 (defun bookmark-prop-set (bookmark prop val)
370 "Set the property PROP of BOOKMARK to VAL.
371 BOOKMARK may be a bookmark name (a string) or a bookmark record."
372 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
373 (if cell
374 (setcdr cell val)
375 (nconc (bookmark-get-bookmark-record bookmark)
376 (list (cons prop val))))))
378 (defun bookmark-get-annotation (bookmark)
379 "Return the annotation of BOOKMARK, or nil if none.
380 BOOKMARK may be a bookmark name (a string) or a bookmark record."
381 (bookmark-prop-get bookmark 'annotation))
383 (defun bookmark-set-annotation (bookmark ann)
384 "Set the annotation of BOOKMARK to ANN.
385 BOOKMARK may be a bookmark name (a string) or a bookmark record."
386 (bookmark-prop-set bookmark 'annotation ann))
389 (defun bookmark-get-filename (bookmark)
390 "Return the full filename of BOOKMARK, or nil if none.
391 BOOKMARK may be a bookmark name (a string) or a bookmark record."
392 (bookmark-prop-get bookmark 'filename))
395 (defun bookmark-set-filename (bookmark filename)
396 "Set the full filename of BOOKMARK to FILENAME.
397 BOOKMARK may be a bookmark name (a string) or a bookmark record."
398 (bookmark-prop-set bookmark 'filename filename))
401 (defun bookmark-get-position (bookmark)
402 "Return the position \(i.e.: point\) of BOOKMARK, or nil if none.
403 BOOKMARK may be a bookmark name (a string) or a bookmark record."
404 (bookmark-prop-get bookmark 'position))
407 (defun bookmark-set-position (bookmark position)
408 "Set the position \(i.e.: point\) of BOOKMARK to POSITION.
409 BOOKMARK may be a bookmark name (a string) or a bookmark record."
410 (bookmark-prop-set bookmark 'position position))
413 (defun bookmark-get-front-context-string (bookmark)
414 "Return the front-context-string of BOOKMARK, or nil if none.
415 BOOKMARK may be a bookmark name (a string) or a bookmark record."
416 (bookmark-prop-get bookmark 'front-context-string))
419 (defun bookmark-set-front-context-string (bookmark string)
420 "Set the front-context-string of BOOKMARK to STRING.
421 BOOKMARK may be a bookmark name (a string) or a bookmark record."
422 (bookmark-prop-set bookmark 'front-context-string string))
425 (defun bookmark-get-rear-context-string (bookmark)
426 "Return the rear-context-string of BOOKMARK, or nil if none.
427 BOOKMARK may be a bookmark name (a string) or a bookmark record."
428 (bookmark-prop-get bookmark 'rear-context-string))
431 (defun bookmark-set-rear-context-string (bookmark string)
432 "Set the rear-context-string of BOOKMARK to STRING.
433 BOOKMARK may be a bookmark name (a string) or a bookmark record."
434 (bookmark-prop-set bookmark 'rear-context-string string))
437 (defun bookmark-get-handler (bookmark)
438 "Return the handler function for BOOKMARK, or nil if none.
439 BOOKMARK may be a bookmark name (a string) or a bookmark record."
440 (bookmark-prop-get bookmark 'handler))
442 (defvar bookmark-history nil
443 "The history list for bookmark functions.")
446 (defun bookmark-completing-read (prompt &optional default)
447 "Prompting with PROMPT, read a bookmark name in completion.
448 PROMPT will get a \": \" stuck on the end no matter what, so you
449 probably don't want to include one yourself.
450 Optional second arg DEFAULT is a string to return if the user enters
451 the empty string."
452 (bookmark-maybe-load-default-file) ; paranoia
453 (if (listp last-nonmenu-event)
454 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
455 (let* ((completion-ignore-case bookmark-completion-ignore-case)
456 (default default)
457 (prompt (if default
458 (concat prompt (format " (%s): " default))
459 (concat prompt ": ")))
460 (str
461 (completing-read prompt
462 bookmark-alist
466 'bookmark-history)))
467 (if (string-equal "" str) default str))))
470 (defmacro bookmark-maybe-historicize-string (string)
471 "Put STRING into the bookmark prompt history, if caller non-interactive.
472 We need this because sometimes bookmark functions are invoked from
473 menus, so `completing-read' never gets a chance to set `bookmark-history'."
474 `(or
475 (called-interactively-p 'interactive)
476 (setq bookmark-history (cons ,string bookmark-history))))
478 (defvar bookmark-make-record-function 'bookmark-make-record-default
479 "A function that should be called to create a bookmark record.
480 Modes may set this variable buffer-locally to enable bookmarking of
481 locations that should be treated specially, such as Info nodes,
482 news posts, images, pdf documents, etc.
484 The function will be called with no arguments.
485 It should signal a user error if it is unable to construct a record for
486 the current location.
488 The returned record should be a cons cell of the form (NAME . ALIST)
489 where ALIST is as described in `bookmark-alist' and may typically contain
490 a special cons (handler . HANDLER-FUNC) which specifies the handler function
491 that should be used instead of `bookmark-default-handler' to open this
492 bookmark. See the documentation for `bookmark-alist' for more.
494 NAME is a suggested name for the constructed bookmark. It can be nil
495 in which case a default heuristic will be used. The function can also
496 equivalently just return ALIST without NAME.")
498 (defun bookmark-make-record ()
499 "Return a new bookmark record (NAME . ALIST) for the current location."
500 (let ((record (funcall bookmark-make-record-function)))
501 ;; Set up default name.
502 (if (stringp (car record))
503 ;; The function already provided a default name.
504 record
505 (if (car record) (push nil record))
506 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
507 record)))
509 (defun bookmark-store (name alist no-overwrite)
510 "Store the bookmark NAME with data ALIST.
511 If NO-OVERWRITE is non-nil and another bookmark of the same name already
512 exists in `bookmark-alist', record the new bookmark without throwing away the
513 old one."
514 (bookmark-maybe-load-default-file)
515 (let ((stripped-name (copy-sequence name)))
516 (or (featurep 'xemacs)
517 ;; XEmacs's `set-text-properties' doesn't work on
518 ;; free-standing strings, apparently.
519 (set-text-properties 0 (length stripped-name) nil stripped-name))
520 (if (and (not no-overwrite)
521 (bookmark-get-bookmark stripped-name 'noerror))
522 ;; already existing bookmark under that name and
523 ;; no prefix arg means just overwrite old bookmark
524 ;; Use the new (NAME . ALIST) format.
525 (setcdr (bookmark-get-bookmark stripped-name) alist)
527 ;; otherwise just cons it onto the front (either the bookmark
528 ;; doesn't exist already, or there is no prefix arg. In either
529 ;; case, we want the new bookmark consed onto the alist...)
531 (push (cons stripped-name alist) bookmark-alist))
533 ;; Added by db
534 (setq bookmark-current-bookmark stripped-name)
535 (setq bookmark-alist-modification-count
536 (1+ bookmark-alist-modification-count))
537 (if (bookmark-time-to-save-p)
538 (bookmark-save))
540 (setq bookmark-current-bookmark stripped-name)
541 (bookmark-bmenu-surreptitiously-rebuild-list)))
543 (defun bookmark-make-record-default (&optional point-only)
544 "Return the record describing the location of a new bookmark.
545 Must be at the correct position in the buffer in which the bookmark is
546 being set.
547 If POINT-ONLY is non-nil, then only return the subset of the
548 record that pertains to the location within the buffer."
549 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
550 (front-context-string
551 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
552 (buffer-substring-no-properties
553 (point)
554 (+ (point) bookmark-search-size))
555 nil))
556 (rear-context-string
557 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
558 (buffer-substring-no-properties
559 (point)
560 (- (point) bookmark-search-size))
561 nil))
562 (position . ,(point))))
565 ;;; File format stuff
567 ;; The OLD format of the bookmark-alist was:
569 ;; ((BOOKMARK-NAME . (FILENAME
570 ;; STRING-IN-FRONT
571 ;; STRING-BEHIND
572 ;; POINT))
573 ;; ...)
575 ;; The NEW format of the bookmark-alist is:
577 ;; ((BOOKMARK-NAME (filename . FILENAME)
578 ;; (front-context-string . STRING-IN-FRONT)
579 ;; (rear-context-string . STRING-BEHIND)
580 ;; (position . POINT)
581 ;; (annotation . ANNOTATION)
582 ;; (whatever . VALUE)
583 ;; ...
584 ;; ))
585 ;; ...)
588 ;; I switched to using an internal as well as external alist because I
589 ;; felt that would be a more flexible framework in which to add
590 ;; features. It means that the order in which values appear doesn't
591 ;; matter, and it means that arbitrary values can be added without
592 ;; risk of interfering with existing ones.
594 ;; BOOKMARK-NAME is the string the user gives the bookmark and
595 ;; accesses it by from then on.
597 ;; FILENAME is the location of the file in which the bookmark is set.
599 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
600 ;; context in front of the point at which the bookmark is set.
602 ;; STRING-BEHIND is the same thing, but after the point.
604 ;; The context strings exist so that modifications to a file don't
605 ;; necessarily cause a bookmark's position to be invalidated.
606 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
607 ;; case the file has changed since the bookmark was set. It will
608 ;; attempt to place the user before the changes, if there were any.
609 ;; ANNOTATION is the annotation for the bookmark; it may not exist
610 ;; (for backward compatibility), be nil (no annotation), or be a
611 ;; string.
614 (defconst bookmark-file-format-version 1
615 "The current version of the format used by bookmark files.
616 You should never need to change this.")
619 (defconst bookmark-end-of-version-stamp-marker
620 "-*- End Of Bookmark File Format Version Stamp -*-\n"
621 "This string marks the end of the version stamp in a bookmark file.")
624 (defun bookmark-alist-from-buffer ()
625 "Return a `bookmark-alist' (in any format) from the current buffer.
626 The buffer must of course contain bookmark format information.
627 Does not care from where in the buffer it is called, and does not
628 affect point."
629 (save-excursion
630 (goto-char (point-min))
631 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
632 (read (current-buffer))
633 ;; Else we're dealing with format version 0
634 (if (search-forward "(" nil t)
635 (progn
636 (forward-char -1)
637 (read (current-buffer)))
638 ;; Else no hope of getting information here.
639 (error "Not bookmark format")))))
642 (defun bookmark-upgrade-version-0-alist (old-list)
643 "Upgrade a version 0 alist OLD-LIST to the current version."
644 (mapcar
645 (lambda (bookmark)
646 (let* ((name (car bookmark))
647 (record (car (cdr bookmark)))
648 (filename (nth 0 record))
649 (front-str (nth 1 record))
650 (rear-str (nth 2 record))
651 (position (nth 3 record))
652 (ann (nth 4 record)))
653 (list
654 name
655 `((filename . ,filename)
656 (front-context-string . ,(or front-str ""))
657 (rear-context-string . ,(or rear-str ""))
658 (position . ,position)
659 (annotation . ,ann)))))
660 old-list))
663 (defun bookmark-upgrade-file-format-from-0 ()
664 "Upgrade a bookmark file of format 0 (the original format) to format 1.
665 This expects to be called from `point-min' in a bookmark file."
666 (message "Upgrading bookmark format from 0 to %d..."
667 bookmark-file-format-version)
668 (let* ((old-list (bookmark-alist-from-buffer))
669 (new-list (bookmark-upgrade-version-0-alist old-list)))
670 (delete-region (point-min) (point-max))
671 (bookmark-insert-file-format-version-stamp)
672 (pp new-list (current-buffer))
673 (save-buffer))
674 (goto-char (point-min))
675 (message "Upgrading bookmark format from 0 to %d...done"
676 bookmark-file-format-version)
680 (defun bookmark-grok-file-format-version ()
681 "Return an integer which is the file-format version of this bookmark file.
682 This expects to be called from `point-min' in a bookmark file."
683 (if (looking-at "^;;;;")
684 (save-excursion
685 (save-match-data
686 (re-search-forward "[0-9]")
687 (forward-char -1)
688 (read (current-buffer))))
689 ;; Else this is format version 0, the original one, which didn't
690 ;; even have version stamps.
694 (defun bookmark-maybe-upgrade-file-format ()
695 "Check the file-format version of this bookmark file.
696 If the version is not up-to-date, upgrade it automatically.
697 This expects to be called from `point-min' in a bookmark file."
698 (let ((version (bookmark-grok-file-format-version)))
699 (cond
700 ((= version bookmark-file-format-version)
701 ) ; home free -- version is current
702 ((= version 0)
703 (bookmark-upgrade-file-format-from-0))
705 (error "Bookmark file format version strangeness")))))
708 (defun bookmark-insert-file-format-version-stamp ()
709 "Insert text indicating current version of bookmark file format."
710 (insert
711 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
712 bookmark-file-format-version))
713 (insert ";;; This format is meant to be slightly human-readable;\n"
714 ";;; nevertheless, you probably don't want to edit it.\n"
715 ";;; "
716 bookmark-end-of-version-stamp-marker))
719 ;;; end file-format stuff
722 ;;; Generic helpers.
724 (defun bookmark-maybe-message (fmt &rest args)
725 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
726 (if (>= baud-rate 9600)
727 (apply 'message fmt args)))
730 ;;; Core code:
732 (defvar bookmark-minibuffer-read-name-map
733 (let ((map (make-sparse-keymap)))
734 (set-keymap-parent map minibuffer-local-map)
735 (define-key map "\C-w" 'bookmark-yank-word)
736 ;; This C-u binding might not be very useful any more now that we
737 ;; provide access to the default via the standard M-n binding.
738 ;; Maybe we should just remove it? --Stef-08
739 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
740 map))
742 ;;;###autoload
743 (defun bookmark-set (&optional name no-overwrite)
744 "Set a bookmark named NAME at the current location.
745 If name is nil, then prompt the user.
747 With prefix arg (NO-OVERWRITE), do not overwrite a bookmark that
748 has the same name as NAME if such a bookmark already exists, but
749 instead push the new bookmark onto the bookmark alist. Thus the
750 most recently set bookmark with name NAME would be the one in
751 effect at any given time, but the others are still there, should
752 the user decide to delete the most recent one.
754 To yank words from the text of the buffer and use them as part of the
755 bookmark name, type C-w while setting a bookmark. Successive C-w's
756 yank successive words.
758 Typing C-u will insert (at the bookmark name prompt) the name of the
759 last bookmark used in the document where the new bookmark is being set;
760 this helps one use a single bookmark name to track progress through
761 a large document. If there is no prior bookmark for this document,
762 then C-u inserts an appropriate name based on the buffer or file.
764 Use \\[bookmark-delete] to remove bookmarks \(give it a name and it
765 removes only the first instance of a bookmark with that name from
766 the list of bookmarks.\)"
767 (interactive (list nil current-prefix-arg))
768 (let* ((record (bookmark-make-record))
769 (default (car record)))
771 (bookmark-maybe-load-default-file)
773 (setq bookmark-yank-point (point))
774 (setq bookmark-current-buffer (current-buffer))
776 (let ((str
777 (or name
778 (read-from-minibuffer
779 (format "Set bookmark (%s): " default)
781 bookmark-minibuffer-read-name-map
782 nil nil default))))
783 (and (string-equal str "") (setq str default))
784 (bookmark-store str (cdr record) no-overwrite)
786 ;; Ask for an annotation buffer for this bookmark
787 (when bookmark-use-annotations
788 (bookmark-edit-annotation str)))))
790 (defun bookmark-kill-line (&optional newline-too)
791 "Kill from point to end of line.
792 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
793 Does not affect the kill ring."
794 (let ((eol (save-excursion (end-of-line) (point))))
795 (delete-region (point) eol)
796 (if (and newline-too (looking-at "\n"))
797 (delete-char 1))))
800 ;; Defvars to avoid compilation warnings:
801 (defvar bookmark-annotation-name nil
802 "Variable holding the name of the bookmark.
803 This is used in `bookmark-edit-annotation' to record the bookmark
804 whose annotation is being edited.")
807 (defun bookmark-default-annotation-text (bookmark)
808 "Return default annotation text for BOOKMARK (a string, not a record).
809 The default annotation text is simply some text explaining how to use
810 annotations."
811 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
812 "# All lines which start with a '#' will be deleted.\n"
813 "# Type C-c C-c when done.\n#\n"
814 "# Author: " (user-full-name) " <" (user-login-name) "@"
815 (system-name) ">\n"
816 "# Date: " (current-time-string) "\n"))
819 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
820 "Function to return default text to use for a bookmark annotation.
821 It takes one argument, the name of the bookmark, as a string.")
822 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
823 'bookmark-edit-annotation-text-func "23.1")
825 (defvar bookmark-edit-annotation-mode-map
826 (let ((map (make-sparse-keymap)))
827 (set-keymap-parent map text-mode-map)
828 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
829 map)
830 "Keymap for editing an annotation of a bookmark.")
833 (defun bookmark-edit-annotation-mode (bookmark)
834 "Mode for editing the annotation of bookmark BOOKMARK.
835 When you have finished composing, type \\[bookmark-send-annotation].
837 BOOKMARK is a bookmark name (a string) or a bookmark record.
839 \\{bookmark-edit-annotation-mode-map}"
840 (interactive)
841 (kill-all-local-variables)
842 (make-local-variable 'bookmark-annotation-name)
843 (setq bookmark-annotation-name bookmark)
844 (use-local-map bookmark-edit-annotation-mode-map)
845 (setq major-mode 'bookmark-edit-annotation-mode
846 mode-name "Edit Bookmark Annotation")
847 (insert (funcall bookmark-edit-annotation-text-func bookmark))
848 (let ((annotation (bookmark-get-annotation bookmark)))
849 (if (and annotation (not (string-equal annotation "")))
850 (insert annotation)))
851 (run-mode-hooks 'text-mode-hook))
854 (defun bookmark-send-edited-annotation ()
855 "Use buffer contents as annotation for a bookmark.
856 Lines beginning with `#' are ignored."
857 (interactive)
858 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
859 (error "Not in bookmark-edit-annotation-mode"))
860 (goto-char (point-min))
861 (while (< (point) (point-max))
862 (if (looking-at "^#")
863 (bookmark-kill-line t)
864 (forward-line 1)))
865 ;; Take no chances with text properties.
866 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
867 (bookmark bookmark-annotation-name))
868 (bookmark-set-annotation bookmark annotation)
869 (bookmark-bmenu-surreptitiously-rebuild-list))
870 (kill-buffer (current-buffer)))
873 (defun bookmark-edit-annotation (bookmark)
874 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
875 BOOKMARK is a bookmark name (a string) or a bookmark record."
876 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
877 (bookmark-edit-annotation-mode bookmark))
880 (defun bookmark-insert-current-bookmark ()
881 "Insert this buffer's value of `bookmark-current-bookmark'.
882 Default to file name if it's nil."
883 (interactive)
884 (let ((str
885 (with-current-buffer bookmark-current-buffer
886 (or bookmark-current-bookmark
887 (bookmark-buffer-name)))))
888 (insert str)))
891 (defun bookmark-buffer-name ()
892 "Return the name of the current buffer (or its file, if any) in a
893 way that is suitable as a bookmark name."
894 (cond
895 ;; Or are we a file?
896 (buffer-file-name (file-name-nondirectory buffer-file-name))
897 ;; Or are we a directory?
898 ((and (boundp 'dired-directory) dired-directory)
899 (let* ((dirname (if (stringp dired-directory)
900 dired-directory
901 (car dired-directory)))
902 (idx (1- (length dirname))))
903 ;; Strip the trailing slash.
904 (if (= ?/ (aref dirname idx))
905 (file-name-nondirectory (substring dirname 0 idx))
906 ;; Else return the current-buffer
907 (buffer-name (current-buffer)))))
908 ;; If all else fails, use the buffer's name.
910 (buffer-name (current-buffer)))))
913 (defun bookmark-yank-word ()
914 "Get the next word from the buffer and append it to the name of the
915 bookmark currently being set, advancing point by one word."
916 (interactive)
917 (let ((string (with-current-buffer bookmark-current-buffer
918 (goto-char bookmark-yank-point)
919 (buffer-substring-no-properties
920 (point)
921 (progn
922 (forward-word 1)
923 (setq bookmark-yank-point (point)))))))
924 (insert string)))
926 (defun bookmark-buffer-file-name ()
927 "Return the current buffer's file in a way useful for bookmarks."
928 ;; Abbreviate the path, both so it's shorter and so it's more
929 ;; portable. E.g., the user's home dir might be a different
930 ;; path on different machines, but "~/" will still reach it.
931 (abbreviate-file-name
932 (cond
933 (buffer-file-name buffer-file-name)
934 ((and (boundp 'dired-directory) dired-directory)
935 (if (stringp dired-directory)
936 dired-directory
937 (car dired-directory)))
938 (t (error "Buffer not visiting a file or directory")))))
941 (defun bookmark-maybe-load-default-file ()
942 "If bookmarks have not been loaded from the default place, load them."
943 (and (not bookmarks-already-loaded)
944 (null bookmark-alist)
945 (prog2
946 (and
947 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
948 ;; to be renamed.
949 (file-exists-p (expand-file-name bookmark-old-default-file))
950 (not (file-exists-p (expand-file-name bookmark-default-file)))
951 (rename-file (expand-file-name bookmark-old-default-file)
952 (expand-file-name bookmark-default-file)))
953 ;; return t so the `and' will continue...
956 (file-readable-p (expand-file-name bookmark-default-file))
957 (bookmark-load bookmark-default-file t t)
958 (setq bookmarks-already-loaded t)))
961 (defun bookmark-maybe-sort-alist ()
962 "Return `bookmark-alist' for display.
963 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
964 (if bookmark-sort-flag
965 (sort (copy-alist bookmark-alist)
966 (function
967 (lambda (x y) (string-lessp (car x) (car y)))))
968 bookmark-alist))
971 (defvar bookmark-after-jump-hook nil
972 "Hook run after `bookmark-jump' jumps to a bookmark.
973 Useful for example to unhide text in `outline-mode'.")
975 (defun bookmark--jump-via (bookmark display-function)
976 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
977 Bookmark may be a bookmark name (a string) or a bookmark record.
979 After calling DISPLAY-FUNCTION, set window point to the point specified
980 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
981 any annotations for this bookmark."
982 (bookmark-handle-bookmark bookmark)
983 (save-current-buffer
984 (funcall display-function (current-buffer)))
985 (let ((win (get-buffer-window (current-buffer) 0)))
986 (if win (set-window-point win (point))))
987 ;; FIXME: we used to only run bookmark-after-jump-hook in
988 ;; `bookmark-jump' itself, but in none of the other commands.
989 (run-hooks 'bookmark-after-jump-hook)
990 (if bookmark-automatically-show-annotations
991 ;; if there is an annotation for this bookmark,
992 ;; show it in a buffer.
993 (bookmark-show-annotation bookmark)))
996 ;;;###autoload
997 (defun bookmark-jump (bookmark &optional display-func)
998 "Jump to bookmark BOOKMARK (a point in some file).
999 You may have a problem using this function if the value of variable
1000 `bookmark-alist' is nil. If that happens, you need to load in some
1001 bookmarks. See help on function `bookmark-load' for more about
1002 this.
1004 If the file pointed to by BOOKMARK no longer exists, you will be asked
1005 if you wish to give the bookmark a new location, and `bookmark-jump'
1006 will then jump to the new location, as well as recording it in place
1007 of the old one in the permanent bookmark record.
1009 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1010 the latter is usually only used by programmatic callers.
1012 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1013 bookmark. It defaults to `switch-to-buffer'; a typical other value
1014 would be, e.g., `switch-to-buffer-other-window'."
1015 (interactive
1016 (list (bookmark-completing-read "Jump to bookmark"
1017 bookmark-current-bookmark)))
1018 (unless bookmark
1019 (error "No bookmark specified"))
1020 (bookmark-maybe-historicize-string bookmark)
1021 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1024 ;;;###autoload
1025 (defun bookmark-jump-other-window (bookmark)
1026 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1027 (interactive
1028 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1029 bookmark-current-bookmark)))
1030 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1033 (defun bookmark-jump-noselect (bookmark)
1034 "Return the location pointed to by the bookmark BOOKMARK.
1035 The return value has the form (BUFFER . POINT).
1037 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1039 Note: this function is deprecated and is present for Emacs 22
1040 compatibility only."
1041 (save-excursion
1042 (bookmark-handle-bookmark bookmark)
1043 (cons (current-buffer) (point))))
1045 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1047 (defun bookmark-handle-bookmark (bookmark)
1048 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1049 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1051 Changes current buffer and point and returns nil, or signals a `file-error'.
1053 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1054 that file no longer exists, then offer interactively to relocate BOOKMARK.
1056 (condition-case err
1057 (funcall (or (bookmark-get-handler bookmark)
1058 'bookmark-default-handler)
1059 (bookmark-get-bookmark bookmark))
1060 (file-error
1061 ;; We were unable to find the marked file, so ask if user wants to
1062 ;; relocate the bookmark, else remind them to consider deletion.
1063 (when (stringp bookmark)
1064 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1065 ;; or a bookmark object. If it's an object, we assume it's a
1066 ;; bookmark used internally by some other package.
1067 (let ((file (bookmark-get-filename bookmark)))
1068 (when file ;Don't know how to relocate if there's no `file'.
1069 ;; If file is not a dir, directory-file-name just returns file.
1070 (let ((display-name (directory-file-name file)))
1071 (ding)
1072 ;; Dialog boxes can accept a file target, but usually don't
1073 ;; know how to accept a directory target (at least, this
1074 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1075 ;; true on Windows as well). So we suppress file dialogs
1076 ;; when relocating.
1077 (let ((use-dialog-box nil)
1078 (use-file-dialog nil))
1079 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1080 bookmark "\"? "))
1081 (progn
1082 (bookmark-relocate bookmark)
1083 ;; Try again.
1084 (funcall (or (bookmark-get-handler bookmark)
1085 'bookmark-default-handler)
1086 (bookmark-get-bookmark bookmark)))
1087 (message
1088 "Bookmark not relocated; consider removing it \(%s\)."
1089 bookmark)
1090 (signal (car err) (cdr err))))))))))
1091 ;; Added by db.
1092 (when (stringp bookmark)
1093 (setq bookmark-current-bookmark bookmark))
1094 nil)
1096 (put 'bookmark-error-no-filename
1097 'error-conditions
1098 '(error bookmark-errors bookmark-error-no-filename))
1099 (put 'bookmark-error-no-filename
1100 'error-message
1101 "Bookmark has no associated file (or directory)")
1103 (defun bookmark-default-handler (bmk-record)
1104 "Default handler to jump to a particular bookmark location.
1105 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1106 Changes current buffer and point and returns nil, or signals a `file-error'."
1107 (let ((file (bookmark-get-filename bmk-record))
1108 (forward-str (bookmark-get-front-context-string bmk-record))
1109 (behind-str (bookmark-get-rear-context-string bmk-record))
1110 (place (bookmark-get-position bmk-record)))
1111 (if (not file)
1112 (signal 'bookmark-error-no-filename (list 'stringp file))
1113 (set-buffer (find-file-noselect file))
1114 (if place (goto-char place))
1115 ;; Go searching forward first. Then, if forward-str exists and
1116 ;; was found in the file, we can search backward for behind-str.
1117 ;; Rationale is that if text was inserted between the two in the
1118 ;; file, it's better to be put before it so you can read it,
1119 ;; rather than after and remain perhaps unaware of the changes.
1120 (if forward-str
1121 (if (search-forward forward-str (point-max) t)
1122 (goto-char (match-beginning 0))))
1123 (if behind-str
1124 (if (search-backward behind-str (point-min) t)
1125 (goto-char (match-end 0)))))
1126 nil))
1128 ;;;###autoload
1129 (defun bookmark-relocate (bookmark)
1130 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1131 BOOKMARK is a bookmark name (a string), not a bookmark record.
1133 This makes an already existing bookmark point to that file, instead of
1134 the one it used to point at. Useful when a file has been renamed
1135 after a bookmark was set in it."
1136 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1137 (bookmark-maybe-historicize-string bookmark)
1138 (bookmark-maybe-load-default-file)
1139 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1140 (newloc (expand-file-name
1141 (read-file-name
1142 (format "Relocate %s to: " bookmark)
1143 (file-name-directory bmrk-filename)))))
1144 (bookmark-set-filename bookmark newloc)
1145 (setq bookmark-alist-modification-count
1146 (1+ bookmark-alist-modification-count))
1147 (if (bookmark-time-to-save-p)
1148 (bookmark-save))
1149 (bookmark-bmenu-surreptitiously-rebuild-list)))
1152 ;;;###autoload
1153 (defun bookmark-insert-location (bookmark &optional no-history)
1154 "Insert the name of the file associated with BOOKMARK.
1155 BOOKMARK is a bookmark name (a string), not a bookmark record.
1157 Optional second arg NO-HISTORY means don't record this in the
1158 minibuffer history list `bookmark-history'."
1159 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1160 (or no-history (bookmark-maybe-historicize-string bookmark))
1161 (let ((start (point)))
1162 (prog1
1163 (insert (bookmark-location bookmark)) ; *Return this line*
1164 (if (and (display-color-p) (display-mouse-p))
1165 (add-text-properties
1166 start
1167 (save-excursion (re-search-backward
1168 "[^ \t]")
1169 (1+ (point)))
1170 '(mouse-face highlight
1171 follow-link t
1172 help-echo "mouse-2: go to this bookmark in other window"))))))
1174 ;;;###autoload
1175 (defalias 'bookmark-locate 'bookmark-insert-location)
1177 (defun bookmark-location (bookmark)
1178 "Return the name of the file associated with BOOKMARK, or nil if none.
1179 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1180 (bookmark-maybe-load-default-file)
1181 (bookmark-get-filename bookmark))
1184 ;;;###autoload
1185 (defun bookmark-rename (old &optional new)
1186 "Change the name of OLD bookmark to NEW name.
1187 If called from keyboard, prompt for OLD and NEW. If called from
1188 menubar, select OLD from a menu and prompt for NEW.
1190 Both OLD and NEW are bookmark names (strings), never bookmark records.
1192 If called from Lisp, prompt for NEW if only OLD was passed as an
1193 argument. If called with two strings, then no prompting is done. You
1194 must pass at least OLD when calling from Lisp.
1196 While you are entering the new name, consecutive C-w's insert
1197 consecutive words from the text of the buffer into the new bookmark
1198 name."
1199 (interactive (list (bookmark-completing-read "Old bookmark name")))
1200 (bookmark-maybe-historicize-string old)
1201 (bookmark-maybe-load-default-file)
1203 (setq bookmark-yank-point (point))
1204 (setq bookmark-current-buffer (current-buffer))
1205 (let ((newname
1206 (or new ; use second arg, if non-nil
1207 (read-from-minibuffer
1208 "New name: "
1210 (let ((now-map (copy-keymap minibuffer-local-map)))
1211 (define-key now-map "\C-w" 'bookmark-yank-word)
1212 now-map)
1214 'bookmark-history))))
1215 (bookmark-set-name old newname)
1216 (setq bookmark-current-bookmark newname)
1217 (bookmark-bmenu-surreptitiously-rebuild-list)
1218 (setq bookmark-alist-modification-count
1219 (1+ bookmark-alist-modification-count))
1220 (if (bookmark-time-to-save-p)
1221 (bookmark-save))))
1224 ;;;###autoload
1225 (defun bookmark-insert (bookmark)
1226 "Insert the text of the file pointed to by bookmark BOOKMARK.
1227 BOOKMARK is a bookmark name (a string), not a bookmark record.
1229 You may have a problem using this function if the value of variable
1230 `bookmark-alist' is nil. If that happens, you need to load in some
1231 bookmarks. See help on function `bookmark-load' for more about
1232 this."
1233 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1234 (bookmark-maybe-historicize-string bookmark)
1235 (bookmark-maybe-load-default-file)
1236 (let ((orig-point (point))
1237 (str-to-insert
1238 (save-current-buffer
1239 (bookmark-handle-bookmark bookmark)
1240 (buffer-string))))
1241 (insert str-to-insert)
1242 (push-mark)
1243 (goto-char orig-point)))
1246 ;;;###autoload
1247 (defun bookmark-delete (bookmark &optional batch)
1248 "Delete BOOKMARK from the bookmark list.
1249 BOOKMARK is a bookmark name (a string), not a bookmark record.
1251 Removes only the first instance of a bookmark with that name. If
1252 there are one or more other bookmarks with the same name, they will
1253 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1254 one most recently used in this file, if any\).
1255 Optional second arg BATCH means don't update the bookmark list buffer,
1256 probably because we were called from there."
1257 (interactive
1258 (list (bookmark-completing-read "Delete bookmark"
1259 bookmark-current-bookmark)))
1260 (bookmark-maybe-historicize-string bookmark)
1261 (bookmark-maybe-load-default-file)
1262 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1263 (setq bookmark-alist (delq will-go bookmark-alist))
1264 ;; Added by db, nil bookmark-current-bookmark if the last
1265 ;; occurrence has been deleted
1266 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1267 (setq bookmark-current-bookmark nil)))
1268 (unless batch
1269 (bookmark-bmenu-surreptitiously-rebuild-list))
1270 (setq bookmark-alist-modification-count
1271 (1+ bookmark-alist-modification-count))
1272 (when (bookmark-time-to-save-p)
1273 (bookmark-save)))
1276 (defun bookmark-time-to-save-p (&optional final-time)
1277 "Return t if it is time to save bookmarks to disk, nil otherwise.
1278 Optional argument FINAL-TIME means this is being called when Emacs
1279 is being killed, so save even if `bookmark-save-flag' is a number and
1280 is greater than `bookmark-alist-modification-count'."
1281 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1282 (cond (final-time
1283 (and (> bookmark-alist-modification-count 0)
1284 bookmark-save-flag))
1285 ((numberp bookmark-save-flag)
1286 (>= bookmark-alist-modification-count bookmark-save-flag))
1288 nil)))
1291 ;;;###autoload
1292 (defun bookmark-write ()
1293 "Write bookmarks to a file (reading the file name with the minibuffer).
1294 Don't use this in Lisp programs; use `bookmark-save' instead."
1295 (interactive)
1296 (bookmark-maybe-load-default-file)
1297 (bookmark-save t))
1300 ;;;###autoload
1301 (defun bookmark-save (&optional parg file)
1302 "Save currently defined bookmarks.
1303 Saves by default in the file defined by the variable
1304 `bookmark-default-file'. With a prefix arg, save it in file FILE
1305 \(second argument\).
1307 If you are calling this from Lisp, the two arguments are PARG and
1308 FILE, and if you just want it to write to the default file, then
1309 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1310 instead. If you pass in one argument, and it is non-nil, then the
1311 user will be interactively queried for a file to save in.
1313 When you want to load in the bookmarks from a file, use
1314 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1315 for a file, defaulting to the file defined by variable
1316 `bookmark-default-file'."
1317 (interactive "P")
1318 (bookmark-maybe-load-default-file)
1319 (cond
1320 ((and (null parg) (null file))
1321 ;;whether interactive or not, write to default file
1322 (bookmark-write-file bookmark-default-file))
1323 ((and (null parg) file)
1324 ;;whether interactive or not, write to given file
1325 (bookmark-write-file file))
1326 ((and parg (not file))
1327 ;;have been called interactively w/ prefix arg
1328 (let ((file (read-file-name "File to save bookmarks in: ")))
1329 (bookmark-write-file file)))
1330 (t ; someone called us with prefix-arg *and* a file, so just write to file
1331 (bookmark-write-file file)))
1332 ;; signal that we have synced the bookmark file by setting this to
1333 ;; 0. If there was an error at any point before, it will not get
1334 ;; set, which is what we want.
1335 (setq bookmark-alist-modification-count 0))
1339 (defun bookmark-write-file (file)
1340 "Write `bookmark-alist' to FILE."
1341 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1342 (with-current-buffer (get-buffer-create " *Bookmarks*")
1343 (goto-char (point-min))
1344 (delete-region (point-min) (point-max))
1345 (let ((print-length nil)
1346 (print-level nil))
1347 (bookmark-insert-file-format-version-stamp)
1348 (insert "(")
1349 ;; Rather than a single call to `pp' we make one per bookmark.
1350 ;; Apparently `pp' has a poor algorithmic complexity, so this
1351 ;; scales a lot better. bug#4485.
1352 (dolist (i bookmark-alist) (pp i (current-buffer)))
1353 (insert ")")
1354 (let ((version-control
1355 (cond
1356 ((null bookmark-version-control) nil)
1357 ((eq 'never bookmark-version-control) 'never)
1358 ((eq 'nospecial bookmark-version-control) version-control)
1359 (t t))))
1360 (condition-case nil
1361 (write-region (point-min) (point-max) file)
1362 (file-error (message "Can't write %s" file)))
1363 (kill-buffer (current-buffer))
1364 (bookmark-maybe-message
1365 "Saving bookmarks to file %s...done" file)))))
1368 (defun bookmark-import-new-list (new-list)
1369 "Add NEW-LIST of bookmarks to `bookmark-alist', rename new bookmarks
1370 with \"<N>\" extensions where they collide with existing bookmark names."
1371 (let ((lst new-list)
1372 (names (bookmark-all-names)))
1373 (while lst
1374 (let* ((full-record (car lst)))
1375 (bookmark-maybe-rename full-record names)
1376 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1377 (setq names (cons (bookmark-name-from-full-record full-record) names))
1378 (setq lst (cdr lst))))))
1381 (defun bookmark-maybe-rename (full-record names)
1382 "If bookmark record FULL-RECORD collides with anything in NAMES, give
1383 FULL-RECORD a new name. This is a helper for `bookmark-import-new-list'."
1384 (let ((found-name (bookmark-name-from-full-record full-record)))
1385 (if (member found-name names)
1386 ;; We've got a conflict, so generate a new name
1387 (let ((count 2)
1388 (new-name found-name))
1389 (while (member new-name names)
1390 (setq new-name (concat found-name (format "<%d>" count)))
1391 (setq count (1+ count)))
1392 (bookmark-set-name full-record new-name)))))
1395 ;;;###autoload
1396 (defun bookmark-load (file &optional overwrite no-msg)
1397 "Load bookmarks from FILE (which must be in bookmark format).
1398 Appends loaded bookmarks to the front of the list of bookmarks. If
1399 optional second argument OVERWRITE is non-nil, existing bookmarks are
1400 destroyed. Optional third arg NO-MSG means don't display any messages
1401 while loading.
1403 If you load a file that doesn't contain a proper bookmark alist, you
1404 will corrupt Emacs's bookmark list. Generally, you should only load
1405 in files that were created with the bookmark functions in the first
1406 place. Your own personal bookmark file, `~/.emacs.bmk', is
1407 maintained automatically by Emacs; you shouldn't need to load it
1408 explicitly.
1410 If you load a file containing bookmarks with the same names as
1411 bookmarks already present in your Emacs, the new bookmarks will get
1412 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1413 method buffers use to resolve name collisions."
1414 (interactive
1415 (list (read-file-name
1416 (format "Load bookmarks from: (%s) "
1417 bookmark-default-file)
1418 ;;Default might not be used often,
1419 ;;but there's no better default, and
1420 ;;I guess it's better than none at all.
1421 "~/" bookmark-default-file 'confirm)))
1422 (setq file (expand-file-name file))
1423 (if (not (file-readable-p file))
1424 (error "Cannot read bookmark file %s" file)
1425 (if (null no-msg)
1426 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1427 (with-current-buffer (let ((enable-local-variables nil))
1428 (find-file-noselect file))
1429 (goto-char (point-min))
1430 (bookmark-maybe-upgrade-file-format)
1431 (let ((blist (bookmark-alist-from-buffer)))
1432 (if (listp blist)
1433 (progn
1434 (if overwrite
1435 (progn
1436 (setq bookmark-alist blist)
1437 (setq bookmark-alist-modification-count 0))
1438 ;; else
1439 (bookmark-import-new-list blist)
1440 (setq bookmark-alist-modification-count
1441 (1+ bookmark-alist-modification-count)))
1442 (if (string-equal
1443 (expand-file-name bookmark-default-file)
1444 file)
1445 (setq bookmarks-already-loaded t))
1446 (bookmark-bmenu-surreptitiously-rebuild-list))
1447 (error "Invalid bookmark list in %s" file)))
1448 (kill-buffer (current-buffer)))
1449 (if (null no-msg)
1450 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1454 ;;; Code supporting the dired-like bookmark menu.
1455 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1458 (defvar bookmark-bmenu-bookmark-column nil)
1461 (defvar bookmark-bmenu-hidden-bookmarks ())
1464 (defvar bookmark-bmenu-mode-map nil)
1467 (if bookmark-bmenu-mode-map
1469 (setq bookmark-bmenu-mode-map (make-keymap))
1470 (suppress-keymap bookmark-bmenu-mode-map t)
1471 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1472 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1473 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1474 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1475 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1476 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1477 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1478 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1479 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1480 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1481 (define-key bookmark-bmenu-mode-map "\C-o"
1482 'bookmark-bmenu-switch-other-window)
1483 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1484 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1485 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1486 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1487 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1488 (define-key bookmark-bmenu-mode-map " " 'next-line)
1489 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1490 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1491 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1492 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1493 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1494 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1495 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1496 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1497 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1498 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1499 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1500 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1501 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1502 (define-key bookmark-bmenu-mode-map [mouse-2]
1503 'bookmark-bmenu-other-window-with-mouse))
1507 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1508 ;; data.
1509 (put 'bookmark-bmenu-mode 'mode-class 'special)
1512 ;; todo: need to display whether or not bookmark exists as a buffer in
1513 ;; flag column.
1515 ;; Format:
1516 ;; FLAGS BOOKMARK [ LOCATION ]
1519 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1520 "Rebuild the Bookmark List if it exists.
1521 Don't affect the buffer ring order."
1522 (if (get-buffer "*Bookmark List*")
1523 (save-excursion
1524 (save-window-excursion
1525 (bookmark-bmenu-list)))))
1528 ;;;###autoload
1529 (defun bookmark-bmenu-list ()
1530 "Display a list of existing bookmarks.
1531 The list is displayed in a buffer named `*Bookmark List*'.
1532 The leftmost column displays a D if the bookmark is flagged for
1533 deletion, or > if it is flagged for displaying."
1534 (interactive)
1535 (bookmark-maybe-load-default-file)
1536 (if (called-interactively-p 'interactive)
1537 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1538 (set-buffer (get-buffer-create "*Bookmark List*")))
1539 (let ((inhibit-read-only t))
1540 (erase-buffer)
1541 (insert "% Bookmark\n- --------\n")
1542 (add-text-properties (point-min) (point)
1543 '(font-lock-face bookmark-menu-heading))
1544 (mapc
1545 (lambda (full-record)
1546 ;; if a bookmark has an annotation, prepend a "*"
1547 ;; in the list of bookmarks.
1548 (let ((annotation (bookmark-get-annotation
1549 (bookmark-name-from-full-record full-record))))
1550 (if (and annotation (not (string-equal annotation "")))
1551 (insert " *")
1552 (insert " "))
1553 (let ((start (point)))
1554 (insert (bookmark-name-from-full-record full-record))
1555 (if (and (display-color-p) (display-mouse-p))
1556 (add-text-properties
1557 start
1558 (save-excursion (re-search-backward
1559 "[^ \t]")
1560 (1+ (point)))
1561 '(mouse-face highlight
1562 follow-link t
1563 help-echo "mouse-2: go to this bookmark in other window")))
1564 (insert "\n")
1566 (bookmark-maybe-sort-alist)))
1567 (goto-char (point-min))
1568 (forward-line 2)
1569 (bookmark-bmenu-mode)
1570 (if bookmark-bmenu-toggle-filenames
1571 (bookmark-bmenu-toggle-filenames t)))
1573 ;;;###autoload
1574 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1575 ;;;###autoload
1576 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1580 (defun bookmark-bmenu-mode ()
1581 "Major mode for editing a list of bookmarks.
1582 Each line describes one of the bookmarks in Emacs.
1583 Letters do not insert themselves; instead, they are commands.
1584 Bookmark names preceded by a \"*\" have annotations.
1585 \\<bookmark-bmenu-mode-map>
1586 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1587 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1588 Also show bookmarks marked using m in other windows.
1589 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1590 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1591 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1592 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1593 together with bookmark selected before this one in another window.
1594 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1595 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1596 so the bookmark menu bookmark remains visible in its window.
1597 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1598 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1599 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1600 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1601 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1602 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1603 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1604 With a prefix arg, prompts for a file to save in.
1605 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1606 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1607 With prefix argument, also move up one line.
1608 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1609 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1610 in another buffer.
1611 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1612 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1613 (kill-all-local-variables)
1614 (use-local-map bookmark-bmenu-mode-map)
1615 (setq truncate-lines t)
1616 (setq buffer-read-only t)
1617 (setq major-mode 'bookmark-bmenu-mode)
1618 (setq mode-name "Bookmark Menu")
1619 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1622 (defun bookmark-bmenu-toggle-filenames (&optional show)
1623 "Toggle whether filenames are shown in the bookmark list.
1624 Optional argument SHOW means show them unconditionally."
1625 (interactive)
1626 (cond
1627 (show
1628 (setq bookmark-bmenu-toggle-filenames nil)
1629 (bookmark-bmenu-show-filenames)
1630 (setq bookmark-bmenu-toggle-filenames t))
1631 (bookmark-bmenu-toggle-filenames
1632 (bookmark-bmenu-hide-filenames)
1633 (setq bookmark-bmenu-toggle-filenames nil))
1635 (bookmark-bmenu-show-filenames)
1636 (setq bookmark-bmenu-toggle-filenames t))))
1639 (defun bookmark-bmenu-show-filenames (&optional force)
1640 "In an interactive bookmark list, show filenames along with bookmarks.
1642 If FORCE is non-nil, force a redisplay showing the filenames; this is
1643 used mainly for debugging, and should not be necessary in normal usage."
1644 (if (and (not force) bookmark-bmenu-toggle-filenames)
1645 nil ;already shown, so do nothing
1646 (save-excursion
1647 (save-window-excursion
1648 (goto-char (point-min))
1649 (forward-line 2)
1650 (setq bookmark-bmenu-hidden-bookmarks ())
1651 (let ((inhibit-read-only t))
1652 (while (< (point) (point-max))
1653 (let ((bmrk (bookmark-bmenu-bookmark)))
1654 (setq bookmark-bmenu-hidden-bookmarks
1655 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1656 (let ((start (save-excursion (end-of-line) (point))))
1657 (move-to-column bookmark-bmenu-file-column t)
1658 ;; Strip off `mouse-face' from the white spaces region.
1659 (if (and (display-color-p) (display-mouse-p))
1660 (remove-text-properties start (point)
1661 '(mouse-face nil help-echo nil))))
1662 (delete-region (point) (progn (end-of-line) (point)))
1663 (insert " ")
1664 ;; Pass the NO-HISTORY arg:
1665 (bookmark-insert-location bmrk t)
1666 (forward-line 1))))))))
1669 (defun bookmark-bmenu-hide-filenames (&optional force)
1670 "In an interactive bookmark list, hide the filenames of the bookmarks.
1672 If FORCE is non-nil, force a redisplay hiding the filenames; this is
1673 used mainly for debugging, and should not be necessary in normal usage."
1674 (if (and (not force) bookmark-bmenu-toggle-filenames)
1675 ;; nothing to hide if above is nil
1676 (save-excursion
1677 (save-window-excursion
1678 (goto-char (point-min))
1679 (forward-line 2)
1680 (setq bookmark-bmenu-hidden-bookmarks
1681 (nreverse bookmark-bmenu-hidden-bookmarks))
1682 (save-excursion
1683 (goto-char (point-min))
1684 (search-forward "Bookmark")
1685 (backward-word 1)
1686 (setq bookmark-bmenu-bookmark-column (current-column)))
1687 (save-excursion
1688 (let ((inhibit-read-only t))
1689 (while bookmark-bmenu-hidden-bookmarks
1690 (move-to-column bookmark-bmenu-bookmark-column t)
1691 (bookmark-kill-line)
1692 (let ((start (point)))
1693 (insert (car bookmark-bmenu-hidden-bookmarks))
1694 (if (and (display-color-p) (display-mouse-p))
1695 (add-text-properties
1696 start
1697 (save-excursion (re-search-backward
1698 "[^ \t]")
1699 (1+ (point)))
1700 '(mouse-face highlight
1701 follow-link t
1702 help-echo
1703 "mouse-2: go to this bookmark in other window"))))
1704 (setq bookmark-bmenu-hidden-bookmarks
1705 (cdr bookmark-bmenu-hidden-bookmarks))
1706 (forward-line 1))))))))
1709 (defun bookmark-bmenu-check-position ()
1710 "Return non-nil if on a line with a bookmark (the actual value
1711 returned is `bookmark-alist'). Else reposition and try again; else if
1712 still no bookmark, return nil."
1713 ;; FIXME: I don't believe this doc string. As far as I can tell,
1714 ;; this function always just returns bookmark-alist. So what is
1715 ;; it for, really? -kfogel, 2009-10-04
1716 (cond ((< (count-lines (point-min) (point)) 2)
1717 (goto-char (point-min))
1718 (forward-line 2)
1719 bookmark-alist)
1720 ((and (bolp) (eobp))
1721 (beginning-of-line 0)
1722 bookmark-alist)
1724 bookmark-alist)))
1727 (defun bookmark-bmenu-bookmark ()
1728 "Return the bookmark for this line in an interactive bookmark list buffer."
1729 ;; return a string which is bookmark of this line.
1730 (if (bookmark-bmenu-check-position)
1731 (save-excursion
1732 (save-window-excursion
1733 (goto-char (point-min))
1734 (search-forward "Bookmark")
1735 (backward-word 1)
1736 (setq bookmark-bmenu-bookmark-column (current-column)))))
1737 (if bookmark-bmenu-toggle-filenames
1738 (bookmark-bmenu-hide-filenames))
1739 (save-excursion
1740 (save-window-excursion
1741 (beginning-of-line)
1742 (forward-char bookmark-bmenu-bookmark-column)
1743 (prog1
1744 (buffer-substring-no-properties (point)
1745 (progn
1746 (end-of-line)
1747 (point)))
1748 ;; well, this is certainly crystal-clear:
1749 (if bookmark-bmenu-toggle-filenames
1750 (bookmark-bmenu-toggle-filenames t))))))
1753 (defun bookmark-show-annotation (bookmark)
1754 "Display the annotation for bookmark named BOOKMARK in a buffer,
1755 if an annotation exists."
1756 (let ((annotation (bookmark-get-annotation bookmark)))
1757 (if (and annotation (not (string-equal annotation "")))
1758 (save-excursion
1759 (let ((old-buf (current-buffer)))
1760 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1761 (delete-region (point-min) (point-max))
1762 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1763 (insert annotation)
1764 (goto-char (point-min))
1765 (pop-to-buffer old-buf))))))
1768 (defun bookmark-show-all-annotations ()
1769 "Display the annotations for all bookmarks in a buffer."
1770 (let ((old-buf (current-buffer)))
1771 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1772 (delete-region (point-min) (point-max))
1773 (mapc
1774 (lambda (full-record)
1775 (let* ((name (bookmark-name-from-full-record full-record))
1776 (ann (bookmark-get-annotation name)))
1777 (insert (concat name ":\n"))
1778 (if (and ann (not (string-equal ann "")))
1779 ;; insert the annotation, indented by 4 spaces.
1780 (progn
1781 (save-excursion (insert ann) (unless (bolp)
1782 (insert "\n")))
1783 (while (< (point) (point-max))
1784 (beginning-of-line) ; paranoia
1785 (insert " ")
1786 (forward-line)
1787 (end-of-line))))))
1788 bookmark-alist)
1789 (goto-char (point-min))
1790 (pop-to-buffer old-buf)))
1793 (defun bookmark-bmenu-mark ()
1794 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1795 (interactive)
1796 (beginning-of-line)
1797 (if (bookmark-bmenu-check-position)
1798 (let ((inhibit-read-only t))
1799 (delete-char 1)
1800 (insert ?>)
1801 (forward-line 1)
1802 (bookmark-bmenu-check-position))))
1805 (defun bookmark-bmenu-select ()
1806 "Select this line's bookmark; also display bookmarks marked with `>'.
1807 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1808 (interactive)
1809 (if (bookmark-bmenu-check-position)
1810 (let ((bmrk (bookmark-bmenu-bookmark))
1811 (menu (current-buffer))
1812 (others ())
1813 tem)
1814 (goto-char (point-min))
1815 (while (re-search-forward "^>" nil t)
1816 (setq tem (bookmark-bmenu-bookmark))
1817 (let ((inhibit-read-only t))
1818 (delete-char -1)
1819 (insert ?\s))
1820 (or (string-equal tem bmrk)
1821 (member tem others)
1822 (setq others (cons tem others))))
1823 (setq others (nreverse others)
1824 tem (/ (1- (frame-height)) (1+ (length others))))
1825 (delete-other-windows)
1826 (bookmark-jump bmrk)
1827 (bury-buffer menu)
1828 (if others
1829 (while others
1830 (split-window nil tem)
1831 (other-window 1)
1832 (bookmark-jump (car others))
1833 (setq others (cdr others)))
1834 (other-window 1)))))
1837 (defun bookmark-bmenu-save (parg)
1838 "Save the current list into a bookmark file.
1839 With a prefix arg, prompts for a file to save them in."
1840 (interactive "P")
1841 (save-excursion
1842 (save-window-excursion
1843 (bookmark-save parg))))
1846 (defun bookmark-bmenu-load ()
1847 "Load the bookmark file and rebuild the bookmark menu-buffer."
1848 (interactive)
1849 (if (bookmark-bmenu-check-position)
1850 (save-excursion
1851 (save-window-excursion
1852 ;; This will call `bookmark-bmenu-list'
1853 (call-interactively 'bookmark-load)))))
1856 (defun bookmark-bmenu-1-window ()
1857 "Select this line's bookmark, alone, in full frame."
1858 (interactive)
1859 (if (bookmark-bmenu-check-position)
1860 (progn
1861 (bookmark-jump (bookmark-bmenu-bookmark))
1862 (bury-buffer (other-buffer))
1863 (delete-other-windows))))
1866 (defun bookmark-bmenu-2-window ()
1867 "Select this line's bookmark, with previous buffer in second window."
1868 (interactive)
1869 (if (bookmark-bmenu-check-position)
1870 (let ((bmrk (bookmark-bmenu-bookmark))
1871 (menu (current-buffer))
1872 (pop-up-windows t))
1873 (delete-other-windows)
1874 (switch-to-buffer (other-buffer))
1875 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1876 (bookmark--jump-via bmrk 'pop-to-buffer))
1877 (bury-buffer menu))))
1880 (defun bookmark-bmenu-this-window ()
1881 "Select this line's bookmark in this window."
1882 (interactive)
1883 (if (bookmark-bmenu-check-position)
1884 (bookmark-jump (bookmark-bmenu-bookmark))))
1887 (defun bookmark-bmenu-other-window ()
1888 "Select this line's bookmark in other window, leaving bookmark menu visible."
1889 (interactive)
1890 (let ((bookmark (bookmark-bmenu-bookmark)))
1891 (if (bookmark-bmenu-check-position)
1892 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1893 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1896 (defun bookmark-bmenu-switch-other-window ()
1897 "Make the other window select this line's bookmark.
1898 The current window remains selected."
1899 (interactive)
1900 (let ((bookmark (bookmark-bmenu-bookmark))
1901 (pop-up-windows t)
1902 same-window-buffer-names
1903 same-window-regexps)
1904 (if (bookmark-bmenu-check-position)
1905 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1906 (bookmark--jump-via bookmark 'display-buffer)))))
1908 (defun bookmark-bmenu-other-window-with-mouse (event)
1909 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1910 (interactive "e")
1911 (with-current-buffer (window-buffer (posn-window (event-end event)))
1912 (save-excursion
1913 (goto-char (posn-point (event-end event)))
1914 (bookmark-bmenu-other-window))))
1917 (defun bookmark-bmenu-show-annotation ()
1918 "Show the annotation for the current bookmark in another window."
1919 (interactive)
1920 (let ((bookmark (bookmark-bmenu-bookmark)))
1921 (if (bookmark-bmenu-check-position)
1922 (bookmark-show-annotation bookmark))))
1925 (defun bookmark-bmenu-show-all-annotations ()
1926 "Show the annotation for all bookmarks in another window."
1927 (interactive)
1928 (bookmark-show-all-annotations))
1931 (defun bookmark-bmenu-edit-annotation ()
1932 "Edit the annotation for the current bookmark in another window."
1933 (interactive)
1934 (let ((bookmark (bookmark-bmenu-bookmark)))
1935 (if (bookmark-bmenu-check-position)
1936 (bookmark-edit-annotation bookmark))))
1939 (defun bookmark-bmenu-unmark (&optional backup)
1940 "Cancel all requested operations on bookmark on this line and move down.
1941 Optional BACKUP means move up."
1942 (interactive "P")
1943 (beginning-of-line)
1944 (if (bookmark-bmenu-check-position)
1945 (progn
1946 (let ((inhibit-read-only t))
1947 (delete-char 1)
1948 ;; any flags to reset according to circumstances? How about a
1949 ;; flag indicating whether this bookmark is being visited?
1950 ;; well, we don't have this now, so maybe later.
1951 (insert " "))
1952 (forward-line (if backup -1 1))
1953 (bookmark-bmenu-check-position))))
1956 (defun bookmark-bmenu-backup-unmark ()
1957 "Move up and cancel all requested operations on bookmark on line above."
1958 (interactive)
1959 (forward-line -1)
1960 (if (bookmark-bmenu-check-position)
1961 (progn
1962 (bookmark-bmenu-unmark)
1963 (forward-line -1)
1964 (bookmark-bmenu-check-position))))
1967 (defun bookmark-bmenu-delete ()
1968 "Mark bookmark on this line to be deleted.
1969 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1970 (interactive)
1971 (beginning-of-line)
1972 (if (bookmark-bmenu-check-position)
1973 (let ((inhibit-read-only t))
1974 (delete-char 1)
1975 (insert ?D)
1976 (forward-line 1)
1977 (bookmark-bmenu-check-position))))
1980 (defun bookmark-bmenu-delete-backwards ()
1981 "Mark bookmark on this line to be deleted, then move up one line.
1982 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1983 (interactive)
1984 (bookmark-bmenu-delete)
1985 (forward-line -2)
1986 (if (bookmark-bmenu-check-position)
1987 (forward-line 1))
1988 (bookmark-bmenu-check-position))
1991 (defun bookmark-bmenu-execute-deletions ()
1992 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1993 (interactive)
1994 (message "Deleting bookmarks...")
1995 (let ((hide-em bookmark-bmenu-toggle-filenames)
1996 (o-point (point))
1997 (o-str (save-excursion
1998 (beginning-of-line)
1999 (if (looking-at "^D")
2001 (buffer-substring
2002 (point)
2003 (progn (end-of-line) (point))))))
2004 (o-col (current-column)))
2005 (if hide-em (bookmark-bmenu-hide-filenames))
2006 (setq bookmark-bmenu-toggle-filenames nil)
2007 (goto-char (point-min))
2008 (forward-line 1)
2009 (while (re-search-forward "^D" (point-max) t)
2010 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2011 (bookmark-bmenu-list)
2012 (setq bookmark-bmenu-toggle-filenames hide-em)
2013 (if bookmark-bmenu-toggle-filenames
2014 (bookmark-bmenu-toggle-filenames t))
2015 (if o-str
2016 (progn
2017 (goto-char (point-min))
2018 (search-forward o-str)
2019 (beginning-of-line)
2020 (forward-char o-col))
2021 (goto-char o-point))
2022 (beginning-of-line)
2023 (message "Deleting bookmarks...done")
2027 (defun bookmark-bmenu-rename ()
2028 "Rename bookmark on current line. Prompts for a new name."
2029 (interactive)
2030 (if (bookmark-bmenu-check-position)
2031 (let ((bmrk (bookmark-bmenu-bookmark))
2032 (thispoint (point)))
2033 (bookmark-rename bmrk)
2034 (goto-char thispoint))))
2037 (defun bookmark-bmenu-locate ()
2038 "Display location of this bookmark. Displays in the minibuffer."
2039 (interactive)
2040 (if (bookmark-bmenu-check-position)
2041 (let ((bmrk (bookmark-bmenu-bookmark)))
2042 (message "%s" (bookmark-location bmrk)))))
2044 (defun bookmark-bmenu-relocate ()
2045 "Change the file path of the bookmark on the current line,
2046 prompting with completion for the new path."
2047 (interactive)
2048 (if (bookmark-bmenu-check-position)
2049 (let ((bmrk (bookmark-bmenu-bookmark))
2050 (thispoint (point)))
2051 (bookmark-relocate bmrk)
2052 (goto-char thispoint))))
2055 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2057 (defun bookmark-menu-popup-paned-menu (event name entries)
2058 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2059 That is, ENTRIES is a list of strings which appear as the choices
2060 in the menu.
2061 The number of panes depends on the number of entries.
2062 The visible entries are truncated to `bookmark-menu-length', but the
2063 strings returned are not."
2064 (let ((f-height (/ (frame-height) 2))
2065 (pane-list nil)
2066 (iter 0))
2067 (while entries
2068 (let (lst
2069 (count 0))
2070 (while (and (< count f-height) entries)
2071 (let ((str (car entries)))
2072 (push (cons
2073 (if (> (length str) bookmark-menu-length)
2074 (substring str 0 bookmark-menu-length)
2075 str)
2076 str)
2077 lst)
2078 (setq entries (cdr entries))
2079 (setq count (1+ count))))
2080 (setq iter (1+ iter))
2081 (push (cons
2082 (format "-*- %s (%d) -*-" name iter)
2083 (nreverse lst))
2084 pane-list)))
2086 ;; Popup the menu and return the string.
2087 (x-popup-menu event (cons (concat "-*- " name " -*-")
2088 (nreverse pane-list)))))
2091 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2092 ;; following works, and for explaining what to do to make it work.
2094 ;; We MUST autoload EACH form used to set up this variable's value, so
2095 ;; that the whole job is done in loaddefs.el.
2097 ;; Emacs menubar stuff.
2099 ;;;###autoload
2100 (defvar menu-bar-bookmark-map
2101 (let ((map (make-sparse-keymap "Bookmark functions")))
2102 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2103 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2104 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2105 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2106 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2107 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2108 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2109 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2110 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2111 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2112 map))
2114 ;;;###autoload
2115 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2117 ;; make bookmarks appear toward the right side of the menu.
2118 (if (boundp 'menu-bar-final-items)
2119 (if menu-bar-final-items
2120 (setq menu-bar-final-items
2121 (cons 'bookmark menu-bar-final-items)))
2122 (setq menu-bar-final-items '(bookmark)))
2124 ;;;; end bookmark menu stuff ;;;;
2127 ;; Load Hook
2128 (defvar bookmark-load-hook nil
2129 "Hook run at the end of loading bookmark.")
2131 ;; Exit Hook, called from kill-emacs-hook
2132 (defvar bookmark-exit-hook nil
2133 "Hook run when Emacs exits.")
2135 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2137 (defun bookmark-exit-hook-internal ()
2138 "Save bookmark state, if necessary, at Emacs exit time.
2139 This also runs `bookmark-exit-hook'."
2140 (run-hooks 'bookmark-exit-hook)
2141 (and bookmark-alist
2142 (bookmark-time-to-save-p t)
2143 (bookmark-save)))
2145 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2147 (defun bookmark-unload-function ()
2148 "Unload the Bookmark library."
2149 (when bookmark-save-flag (bookmark-save))
2150 ;; continue standard unloading
2151 nil)
2154 (run-hooks 'bookmark-load-hook)
2156 (provide 'bookmark)
2158 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2159 ;;; bookmark.el ends here