Merge from emacs--rel--22
[emacs.git] / lisp / bookmark.el
blobf8440731419da3cd9f1d600fcff1972cd115c149
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 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 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This package is for setting "bookmarks" in files. A bookmark
31 ;; associates a string with a location in a certain file. Thus, you
32 ;; can navigate your way to that location by providing the string.
33 ;; See the "User Variables" section for customizations.
35 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
36 ;; then implementing the bookmark-current-bookmark idea. He even
37 ;; sent *patches*, bless his soul...
39 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
40 ;; fixing and improving bookmark-time-to-save-p.
42 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
43 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
44 ;; and the menu-bar).
46 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
47 ;; suggestions and the code to implement them (like
48 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
49 ;; stuff).
51 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
52 ;; for his eminently sensible suggestion to separate bookmark-jump
53 ;; into bookmark-jump and bookmark-jump-noselect, which made many
54 ;; other things cleaner as well.
56 ;; Thanks to Roland McGrath for encouragement and help with defining
57 ;; autoloads on the menu-bar.
59 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
60 ;; values in bookmark-jump and bookmark-set. Everybody please keep
61 ;; all the keystrokes they save thereby and send them to him at the
62 ;; end of each year :-) (No, seriously, thanks Jonathan!)
64 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
65 ;; thinking up the annotations feature and implementing it so well.
67 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
68 ;; <olstad@msc.edu>.
70 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
71 ;; reported and fixed.
73 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
75 ;; Enough with the credits already, get on to the good stuff:
77 ;; FAVORITE CHINESE RESTAURANT:
78 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
79 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
81 ;;; Code:
83 (require 'pp)
85 ;;; Misc comments:
87 ;; If variable bookmark-use-annotations is non-nil, an annotation is
88 ;; queried for when setting a bookmark.
90 ;; The bookmark list is sorted lexically by default, but you can turn
91 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
92 ;; the list will be presented in the order it is recorded
93 ;; (chronologically), which is actually fairly useful as well.
95 ;;; User Variables
97 (defgroup bookmark nil
98 "Setting, annotation and jumping to bookmarks."
99 :group 'matching)
102 (defcustom bookmark-use-annotations nil
103 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
104 :type 'boolean
105 :group 'bookmark)
108 (defcustom bookmark-save-flag t
109 "*Controls when Emacs saves bookmarks to a file.
110 --> nil means never save bookmarks, except when `bookmark-save' is
111 explicitly called \(\\[bookmark-save]\).
112 --> t means save bookmarks when Emacs is killed.
113 --> Otherwise, it should be a number that is the frequency with which
114 the bookmark list is saved \(i.e.: the number of times which
115 Emacs' bookmark list may be modified before it is automatically
116 saved.\). If it is a number, Emacs will also automatically save
117 bookmarks when it is killed.
119 Therefore, the way to get it to save every time you make or delete a
120 bookmark is to set this variable to 1 \(or 0, which produces the same
121 behavior.\)
123 To specify the file in which to save them, modify the variable
124 `bookmark-default-file', which is `~/.emacs.bmk' by default."
125 :type '(choice (const nil) integer (other t))
126 :group 'bookmark)
129 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
130 "*The `.emacs.bmk' file used to be called this name.")
133 ;; defvarred to avoid a compilation warning:
134 (defvar bookmark-file nil
135 "Old name for `bookmark-default-file'.")
137 (defcustom bookmark-default-file
138 (if bookmark-file
139 ;; In case user set `bookmark-file' in her .emacs:
140 bookmark-file
141 (convert-standard-filename "~/.emacs.bmk"))
142 "*File in which to save bookmarks by default."
143 :type 'file
144 :group 'bookmark)
147 (defcustom bookmark-version-control 'nospecial
148 "*Whether or not to make numbered backups of the bookmark file.
149 It can have four values: t, nil, `never', and `nospecial'.
150 The first three have the same meaning that they do for the
151 variable `version-control', and the final value `nospecial' means just
152 use the value of `version-control'."
153 :type '(choice (const nil) (const never) (const nospecial)
154 (other t))
155 :group 'bookmark)
158 (defcustom bookmark-completion-ignore-case t
159 "*Non-nil means bookmark functions ignore case in completion."
160 :type 'boolean
161 :group 'bookmark)
164 (defcustom bookmark-sort-flag t
165 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
166 Otherwise they will be displayed in LIFO order (that is, most
167 recently set ones come first, oldest ones come last)."
168 :type 'boolean
169 :group 'bookmark)
172 (defcustom bookmark-automatically-show-annotations t
173 "*Non-nil means show annotations when jumping to a bookmark."
174 :type 'boolean
175 :group 'bookmark)
178 (defcustom bookmark-bmenu-file-column 30
179 "*Column at which to display filenames in a buffer listing bookmarks.
180 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
181 :type 'integer
182 :group 'bookmark)
185 (defcustom bookmark-bmenu-toggle-filenames t
186 "*Non-nil means show filenames when listing bookmarks.
187 This may result in truncated bookmark names. To disable this, put the
188 following in your `.emacs' file:
190 \(setq bookmark-bmenu-toggle-filenames nil\)"
191 :type 'boolean
192 :group 'bookmark)
195 (defcustom bookmark-menu-length 70
196 "*Maximum length of a bookmark name displayed on a popup menu."
197 :type 'integer
198 :group 'bookmark)
201 (defface bookmark-menu-heading
202 '((t (:inherit font-lock-type-face)))
203 "Face used to highlight the heading in bookmark menu buffers."
204 :group 'bookmark
205 :version "22.1")
208 ;;; No user-serviceable parts beyond this point.
210 ;; Is it XEmacs?
211 (defconst bookmark-xemacsp
212 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
215 ;; Added for lucid emacs compatibility, db
216 (or (fboundp 'defalias) (fset 'defalias 'fset))
218 ;; suggested for lucid compatibility by david hughes:
219 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
222 ;;; Keymap stuff:
224 ;; Set up these bindings dumping time *only*;
225 ;; if the user alters them, don't override the user when loading bookmark.el.
227 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
228 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
229 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
231 ;;;###autoload
232 (defvar bookmark-map nil
233 "Keymap containing bindings to bookmark functions.
234 It is not bound to any key by default: to bind it
235 so that you have a bookmark prefix, just use `global-set-key' and bind a
236 key of your choice to `bookmark-map'. All interactive bookmark
237 functions have a binding in this keymap.")
239 ;;;###autoload (define-prefix-command 'bookmark-map)
241 ;; Read the help on all of these functions for details...
242 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ;"m"ark
244 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
245 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ;"g"o
246 ;;;###autoload (define-key bookmark-map "o" 'bookmark-jump-other-window)
247 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
248 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
249 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ;"f"ind
250 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
251 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
252 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
253 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
254 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
257 ;;; The annotation maps.
258 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
259 "Keymap for composing an annotation for a bookmark.")
261 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
262 'bookmark-send-annotation)
266 ;;; Core variables and data structures:
267 (defvar bookmark-alist ()
268 "Association list of bookmarks and their records.
269 You probably don't want to change the value of this alist yourself;
270 instead, let the various bookmark functions do it for you.
272 The format of the alist is
274 \(BOOKMARK1 BOOKMARK2 ...\)
276 where each BOOKMARK is of the form
278 \(NAME
279 \(filename . FILE\)
280 \(front-context-string . FRONT-STR\)
281 \(rear-context-string . REAR-STR\)
282 \(position . POS\)
283 \(info-node . POS\)
284 \(annotation . ANNOTATION\)\)
286 So the cdr of each bookmark is an alist too.
287 `info-node' is optional, by the way.")
290 (defvar bookmarks-already-loaded nil)
293 ;; more stuff added by db.
295 (defvar bookmark-current-bookmark nil
296 "Name of bookmark most recently used in the current file.
297 It is buffer local, used to make moving a bookmark forward
298 through a file easier.")
300 (make-variable-buffer-local 'bookmark-current-bookmark)
303 (defvar bookmark-alist-modification-count 0
304 "Number of modifications to bookmark list since it was last saved.")
307 (defvar bookmark-search-size 16
308 "Length of the context strings recorded on either side of a bookmark.")
311 (defvar bookmark-current-point 0)
312 (defvar bookmark-yank-point 0)
313 (defvar bookmark-current-buffer nil)
315 (defvar Info-current-node)
316 (defvar Info-suffix-list)
318 ;; Helper functions.
320 ;; Only functions on this page and the next one (file formats) need to
321 ;; know anything about the format of bookmark-alist entries.
322 ;; Everyone else should go through them.
324 (defun bookmark-name-from-full-record (full-record)
325 "Return name of FULL-RECORD \(an alist element instead of a string\)."
326 (car full-record))
329 (defun bookmark-all-names ()
330 "Return a list of all current bookmark names."
331 (bookmark-maybe-load-default-file)
332 (mapcar
333 (lambda (full-record)
334 (bookmark-name-from-full-record full-record))
335 bookmark-alist))
338 (defun bookmark-get-bookmark (bookmark)
339 "Return the full entry for BOOKMARK in `bookmark-alist'.
340 If BOOKMARK is not a string, return nil."
341 (when (stringp bookmark)
342 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
345 (defun bookmark-get-bookmark-record (bookmark)
346 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
347 That is, all information but the name."
348 (car (cdr (bookmark-get-bookmark bookmark))))
351 (defun bookmark-set-name (bookmark newname)
352 "Set BOOKMARK's name to NEWNAME."
353 (setcar
354 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
355 newname))
358 (defun bookmark-get-annotation (bookmark)
359 "Return the annotation of BOOKMARK, or nil if none."
360 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
363 (defun bookmark-set-annotation (bookmark ann)
364 "Set the annotation of BOOKMARK to ANN."
365 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
366 (if cell
367 (setcdr cell ann)
368 (nconc (bookmark-get-bookmark-record bookmark)
369 (list (cons 'annotation ann))))))
372 (defun bookmark-get-filename (bookmark)
373 "Return the full filename of BOOKMARK."
374 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
377 (defun bookmark-set-filename (bookmark filename)
378 "Set the full filename of BOOKMARK to FILENAME."
379 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
380 (if cell
381 (setcdr cell filename)
382 (nconc (bookmark-get-bookmark-record bookmark)
383 (list (cons 'filename filename))))
384 (setq bookmark-alist-modification-count
385 (1+ bookmark-alist-modification-count))
386 (if (bookmark-time-to-save-p)
387 (bookmark-save))))
390 (defun bookmark-get-position (bookmark)
391 "Return the position \(i.e.: point\) of BOOKMARK."
392 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
395 (defun bookmark-set-position (bookmark position)
396 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
397 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
398 (if cell
399 (setcdr cell position)
400 (nconc (bookmark-get-bookmark-record bookmark)
401 (list (cons 'position position))))))
404 (defun bookmark-get-front-context-string (bookmark)
405 "Return the front-context-string of BOOKMARK."
406 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
409 (defun bookmark-set-front-context-string (bookmark string)
410 "Set the front-context-string of BOOKMARK to STRING."
411 (let ((cell (assq 'front-context-string
412 (bookmark-get-bookmark-record bookmark))))
413 (if cell
414 (setcdr cell string)
415 (nconc (bookmark-get-bookmark-record bookmark)
416 (list (cons 'front-context-string string))))))
419 (defun bookmark-get-rear-context-string (bookmark)
420 "Return the rear-context-string of BOOKMARK."
421 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
424 (defun bookmark-set-rear-context-string (bookmark string)
425 "Set the rear-context-string of BOOKMARK to STRING."
426 (let ((cell (assq 'rear-context-string
427 (bookmark-get-bookmark-record bookmark))))
428 (if cell
429 (setcdr cell string)
430 (nconc (bookmark-get-bookmark-record bookmark)
431 (list (cons 'rear-context-string string))))))
434 (defun bookmark-get-info-node (bookmark)
435 "Get the info node associated with BOOKMARK."
436 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
439 (defun bookmark-set-info-node (bookmark node)
440 "Set the Info node of BOOKMARK to NODE."
441 (let ((cell (assq 'info-node
442 (bookmark-get-bookmark-record bookmark))))
443 (if cell
444 (setcdr cell node)
445 (nconc (bookmark-get-bookmark-record bookmark)
446 (list (cons 'info-node node)))))
448 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
449 (sit-for 4))
452 (defvar bookmark-history nil
453 "The history list for bookmark functions.")
456 (defun bookmark-completing-read (prompt &optional default)
457 "Prompting with PROMPT, read a bookmark name in completion.
458 PROMPT will get a \": \" stuck on the end no matter what, so you
459 probably don't want to include one yourself.
460 Optional second arg DEFAULT is a string to return if the user enters
461 the empty string."
462 (bookmark-maybe-load-default-file) ; paranoia
463 (if (listp last-nonmenu-event)
464 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
465 (let* ((completion-ignore-case bookmark-completion-ignore-case)
466 (default default)
467 (prompt (if default
468 (concat prompt (format " (%s): " default))
469 (concat prompt ": ")))
470 (str
471 (completing-read prompt
472 bookmark-alist
476 'bookmark-history)))
477 (if (string-equal "" str) default str))))
480 (defmacro bookmark-maybe-historicize-string (string)
481 "Put STRING into the bookmark prompt history, if caller non-interactive.
482 We need this because sometimes bookmark functions are invoked from
483 menus, so `completing-read' never gets a chance to set `bookmark-history'."
484 `(or
485 (interactive-p)
486 (setq bookmark-history (cons ,string bookmark-history))))
489 (defun bookmark-make (name &optional annotation overwrite info-node)
490 "Make a bookmark named NAME.
491 Optional second arg ANNOTATION gives it an annotation.
492 Optional third arg OVERWRITE means replace any existing bookmarks with
493 this name.
494 Optional fourth arg INFO-NODE means this bookmark is at info node
495 INFO-NODE, so record this fact in the bookmark's entry."
496 (bookmark-maybe-load-default-file)
497 (let ((stripped-name (copy-sequence name)))
498 (or bookmark-xemacsp
499 ;; XEmacs's `set-text-properties' doesn't work on
500 ;; free-standing strings, apparently.
501 (set-text-properties 0 (length stripped-name) nil stripped-name))
502 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
503 ;; already existing bookmark under that name and
504 ;; no prefix arg means just overwrite old bookmark
505 (setcdr (bookmark-get-bookmark stripped-name)
506 (list (bookmark-make-cell annotation info-node)))
508 ;; otherwise just cons it onto the front (either the bookmark
509 ;; doesn't exist already, or there is no prefix arg. In either
510 ;; case, we want the new bookmark consed onto the alist...)
512 (setq bookmark-alist
513 (cons
514 (list stripped-name
515 (bookmark-make-cell annotation info-node))
516 bookmark-alist)))
518 ;; Added by db
519 (setq bookmark-current-bookmark stripped-name)
520 (setq bookmark-alist-modification-count
521 (1+ bookmark-alist-modification-count))
522 (if (bookmark-time-to-save-p)
523 (bookmark-save))))
526 (defun bookmark-make-cell (annotation &optional info-node)
527 "Return the record part of a new bookmark, given ANNOTATION.
528 Must be at the correct position in the buffer in which the bookmark is
529 being set. This might change someday.
530 Optional second arg INFO-NODE means this bookmark is at info node
531 INFO-NODE, so record this fact in the bookmark's entry."
532 (let ((the-record
533 `((filename . ,(bookmark-buffer-file-name))
534 (front-context-string
535 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
536 (buffer-substring-no-properties
537 (point)
538 (+ (point) bookmark-search-size))
539 nil))
540 (rear-context-string
541 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
542 (buffer-substring-no-properties
543 (point)
544 (- (point) bookmark-search-size))
545 nil))
546 (position . ,(point)))))
548 ;; Now fill in the optional parts:
550 ;; Take no chances with text properties
551 (set-text-properties 0 (length annotation) nil annotation)
552 (set-text-properties 0 (length info-node) nil info-node)
554 (if annotation
555 (nconc the-record (list (cons 'annotation annotation))))
556 (if info-node
557 (nconc the-record (list (cons 'info-node info-node))))
559 ;; Finally, return the completed record.
560 the-record))
564 ;;; File format stuff
566 ;; The OLD format of the bookmark-alist was:
568 ;; ((bookmark-name (filename
569 ;; string-in-front
570 ;; string-behind
571 ;; point))
572 ;; ...)
574 ;; The NEW format of the bookmark-alist is:
576 ;; ((bookmark-name ((filename . FILENAME)
577 ;; (front-context-string . string-in-front)
578 ;; (rear-context-string . string-behind)
579 ;; (position . POINT)
580 ;; (annotation . annotation)
581 ;; (whatever . VALUE)
582 ;; ...
583 ;; ))
584 ;; ...)
587 ;; I switched to using an internal as well as external alist because I
588 ;; felt that would be a more flexible framework in which to add
589 ;; features. It means that the order in which values appear doesn't
590 ;; matter, and it means that arbitrary values can be added without
591 ;; risk of interfering with existing ones.
593 ;; BOOKMARK-NAME is the string the user gives the bookmark and
594 ;; accesses it by from then on.
596 ;; FILENAME is the location of the file in which the bookmark is set.
598 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
599 ;; context in front of the point at which the bookmark is set.
601 ;; STRING-BEHIND is the same thing, but after the point.
603 ;; The context strings exist so that modifications to a file don't
604 ;; necessarily cause a bookmark's position to be invalidated.
605 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
606 ;; case the file has changed since the bookmark was set. It will
607 ;; attempt to place the user before the changes, if there were any.
608 ;; ANNOTATION is the annotation for the bookmark; it may not exist
609 ;; (for backward compatibility), be nil (no annotation), or be a
610 ;; string.
613 (defconst bookmark-file-format-version 1
614 "The current version of the format used by bookmark files.
615 You should never need to change this.")
618 (defconst bookmark-end-of-version-stamp-marker
619 "-*- End Of Bookmark File Format Version Stamp -*-\n"
620 "This string marks the end of the version stamp in a bookmark file.")
623 (defun bookmark-alist-from-buffer ()
624 "Return a `bookmark-alist' (in any format) from the current buffer.
625 The buffer must of course contain bookmark format information.
626 Does not care from where in the buffer it is called, and does not
627 affect point."
628 (save-excursion
629 (goto-char (point-min))
630 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
631 (read (current-buffer))
632 ;; Else we're dealing with format version 0
633 (if (search-forward "(" nil t)
634 (progn
635 (forward-char -1)
636 (read (current-buffer)))
637 ;; Else no hope of getting information here.
638 (error "Not bookmark format")))))
641 (defun bookmark-upgrade-version-0-alist (old-list)
642 "Upgrade a version 0 alist OLD-LIST to the current version."
643 (mapcar
644 (lambda (bookmark)
645 (let* ((name (car bookmark))
646 (record (car (cdr bookmark)))
647 (filename (nth 0 record))
648 (front-str (nth 1 record))
649 (rear-str (nth 2 record))
650 (position (nth 3 record))
651 (ann (nth 4 record)))
652 (list
653 name
654 `((filename . ,filename)
655 (front-context-string . ,(or front-str ""))
656 (rear-context-string . ,(or rear-str ""))
657 (position . ,position)
658 (annotation . ,ann)))))
659 old-list))
662 (defun bookmark-upgrade-file-format-from-0 ()
663 "Upgrade a bookmark file of format 0 (the original format) to format 1.
664 This expects to be called from `point-min' in a bookmark file."
665 (message "Upgrading bookmark format from 0 to %d..."
666 bookmark-file-format-version)
667 (let* ((old-list (bookmark-alist-from-buffer))
668 (new-list (bookmark-upgrade-version-0-alist old-list)))
669 (delete-region (point-min) (point-max))
670 (bookmark-insert-file-format-version-stamp)
671 (pp new-list (current-buffer))
672 (save-buffer))
673 (goto-char (point-min))
674 (message "Upgrading bookmark format from 0 to %d...done"
675 bookmark-file-format-version)
679 (defun bookmark-grok-file-format-version ()
680 "Return an integer which is the file-format version of this bookmark file.
681 This expects to be called from `point-min' in a bookmark file."
682 (if (looking-at "^;;;;")
683 (save-excursion
684 (save-match-data
685 (re-search-forward "[0-9]")
686 (forward-char -1)
687 (read (current-buffer))))
688 ;; Else this is format version 0, the original one, which didn't
689 ;; even have version stamps.
693 (defun bookmark-maybe-upgrade-file-format ()
694 "Check the file-format version of this bookmark file.
695 If the version is not up-to-date, upgrade it automatically.
696 This expects to be called from `point-min' in a bookmark file."
697 (let ((version (bookmark-grok-file-format-version)))
698 (cond
699 ((= version bookmark-file-format-version)
700 ) ; home free -- version is current
701 ((= version 0)
702 (bookmark-upgrade-file-format-from-0))
704 (error "Bookmark file format version strangeness")))))
707 (defun bookmark-insert-file-format-version-stamp ()
708 "Insert text indicating current version of bookmark file format."
709 (insert
710 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
711 bookmark-file-format-version))
712 (insert ";;; This format is meant to be slightly human-readable;\n"
713 ";;; nevertheless, you probably don't want to edit it.\n"
714 ";;; "
715 bookmark-end-of-version-stamp-marker))
718 ;;; end file-format stuff
721 ;;; Generic helpers.
723 (defun bookmark-maybe-message (fmt &rest args)
724 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
725 (if (>= baud-rate 9600)
726 (apply 'message fmt args)))
729 ;;; Core code:
731 ;;;###autoload
732 (defun bookmark-set (&optional name parg)
733 "Set a bookmark named NAME inside a file.
734 If name is nil, then the user will be prompted.
735 With prefix arg, will not overwrite a bookmark that has the same name
736 as NAME if such a bookmark already exists, but instead will \"push\"
737 the new bookmark onto the bookmark alist. Thus the most recently set
738 bookmark with name NAME would be the one in effect at any given time,
739 but the others are still there, should you decide to delete the most
740 recent one.
742 To yank words from the text of the buffer and use them as part of the
743 bookmark name, type C-w while setting a bookmark. Successive C-w's
744 yank successive words.
746 Typing C-u inserts the name of the last bookmark used in the buffer
747 \(as an aid in using a single bookmark name to track your progress
748 through a large file\). If no bookmark was used, then C-u inserts the
749 name of the file being visited.
751 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
752 and it removes only the first instance of a bookmark with that name from
753 the list of bookmarks.\)"
754 (interactive (list nil current-prefix-arg))
756 (bookmark-buffer-file-name)
757 (error "Buffer not visiting a file or directory"))
759 (bookmark-maybe-load-default-file)
761 (setq bookmark-current-point (point))
762 (setq bookmark-yank-point (point))
763 (setq bookmark-current-buffer (current-buffer))
765 (let* ((default (or bookmark-current-bookmark
766 (bookmark-buffer-name)))
767 (str
768 (or name
769 (read-from-minibuffer
770 (format "Set bookmark (%s): " default)
772 (let ((now-map (copy-keymap minibuffer-local-map)))
773 (define-key now-map "\C-w" 'bookmark-yank-word)
774 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
775 now-map))))
776 (annotation nil))
777 (and (string-equal str "") (setq str default))
778 ;; Ask for an annotation buffer for this bookmark
779 (if bookmark-use-annotations
780 (bookmark-read-annotation parg str)
781 (bookmark-make str annotation parg (bookmark-info-current-node))
782 (setq bookmark-current-bookmark str)
783 (bookmark-bmenu-surreptitiously-rebuild-list)
784 (goto-char bookmark-current-point))))
787 (defun bookmark-info-current-node ()
788 "If in Info-mode, return current node name (a string), else nil."
789 (if (eq major-mode 'Info-mode)
790 Info-current-node))
793 (defun bookmark-kill-line (&optional newline-too)
794 "Kill from point to end of line.
795 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
796 Does not affect the kill ring."
797 (let ((eol (save-excursion (end-of-line) (point))))
798 (delete-region (point) eol)
799 (if (and newline-too (looking-at "\n"))
800 (delete-char 1))))
803 ;; Defvars to avoid compilation warnings:
804 (defvar bookmark-annotation-paragraph nil)
805 (defvar bookmark-annotation-name nil)
806 (defvar bookmark-annotation-buffer nil)
807 (defvar bookmark-annotation-file nil)
808 (defvar bookmark-annotation-point nil)
811 (defun bookmark-send-annotation ()
812 "Use buffer contents as the annotation for a bookmark.
813 Exclude lines that begin with `#'.
814 Store the annotation text in the bookmark list with
815 the bookmark (and file, and point) specified in buffer local variables."
816 (interactive)
817 (if (not (eq major-mode 'bookmark-read-annotation-mode))
818 (error "Not in bookmark-read-annotation-mode"))
819 (goto-char (point-min))
820 (while (< (point) (point-max))
821 (if (looking-at "^#")
822 (bookmark-kill-line t)
823 (forward-line 1)))
824 (let ((annotation (buffer-string))
825 (parg bookmark-annotation-paragraph)
826 (bookmark bookmark-annotation-name)
827 (pt bookmark-annotation-point)
828 (buf bookmark-annotation-buffer))
829 ;; for bookmark-make-cell to work, we need to be
830 ;; in the relevant buffer, at the relevant point.
831 ;; Actually, bookmark-make-cell should probably be re-written,
832 ;; to avoid this need. Should I handle the error if a buffer is
833 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
834 (save-excursion
835 (pop-to-buffer buf)
836 (goto-char pt)
837 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
838 (setq bookmark-current-bookmark bookmark))
839 (bookmark-bmenu-surreptitiously-rebuild-list)
840 (goto-char bookmark-current-point))
841 (kill-buffer (current-buffer)))
844 (defun bookmark-default-annotation-text (bookmark)
845 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
846 "# All lines which start with a '#' will be deleted.\n"
847 "# Type C-c C-c when done.\n#\n"
848 "# Author: " (user-full-name) " <" (user-login-name) "@"
849 (system-name) ">\n"
850 "# Date: " (current-time-string) "\n"))
853 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
854 "Function to return default text to use for a bookmark annotation.
855 It takes one argument, the name of the bookmark, as a string.")
857 (defun bookmark-read-annotation-mode (buf point parg bookmark)
858 "Mode for composing annotations for a bookmark.
859 Wants BUF, POINT, PARG, and BOOKMARK.
860 When you have finished composing, type \\[bookmark-send-annotation] to send
861 the annotation.
863 \\{bookmark-read-annotation-mode-map}
865 (interactive)
866 (kill-all-local-variables)
867 (make-local-variable 'bookmark-annotation-paragraph)
868 (make-local-variable 'bookmark-annotation-name)
869 (make-local-variable 'bookmark-annotation-buffer)
870 (make-local-variable 'bookmark-annotation-file)
871 (make-local-variable 'bookmark-annotation-point)
872 (setq bookmark-annotation-paragraph parg)
873 (setq bookmark-annotation-name bookmark)
874 (setq bookmark-annotation-buffer buf)
875 (setq bookmark-annotation-file (buffer-file-name buf))
876 (setq bookmark-annotation-point point)
877 (use-local-map bookmark-read-annotation-mode-map)
878 (setq major-mode 'bookmark-read-annotation-mode)
879 (insert (funcall bookmark-read-annotation-text-func bookmark))
880 (run-mode-hooks 'text-mode-hook))
883 (defun bookmark-read-annotation (parg bookmark)
884 "Pop up a buffer for entering a bookmark annotation.
885 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
886 (let ((buf (current-buffer))
887 (point (point)))
888 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
889 (bookmark-read-annotation-mode buf point parg bookmark)))
892 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
893 "Keymap for editing an annotation of a bookmark.")
896 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
897 'bookmark-send-edited-annotation)
900 (defun bookmark-edit-annotation-mode (bookmark)
901 "Mode for editing the annotation of bookmark BOOKMARK.
902 When you have finished composing, type \\[bookmark-send-annotation].
904 \\{bookmark-edit-annotation-mode-map}
906 (interactive)
907 (kill-all-local-variables)
908 (make-local-variable 'bookmark-annotation-name)
909 (setq bookmark-annotation-name bookmark)
910 (use-local-map bookmark-edit-annotation-mode-map)
911 (setq major-mode 'bookmark-edit-annotation-mode
912 mode-name "Edit Bookmark Annotation")
913 (insert (funcall bookmark-read-annotation-text-func bookmark))
914 (let ((annotation (bookmark-get-annotation bookmark)))
915 (if (and annotation (not (string-equal annotation "")))
916 (insert annotation)))
917 (run-mode-hooks 'text-mode-hook))
920 (defun bookmark-send-edited-annotation ()
921 "Use buffer contents as annotation for a bookmark.
922 Lines beginning with `#' are ignored."
923 (interactive)
924 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
925 (error "Not in bookmark-edit-annotation-mode"))
926 (goto-char (point-min))
927 (while (< (point) (point-max))
928 (if (looking-at "^#")
929 (bookmark-kill-line t)
930 (forward-line 1)))
931 (let ((annotation (buffer-string))
932 (bookmark bookmark-annotation-name))
933 (bookmark-set-annotation bookmark annotation)
934 (bookmark-bmenu-surreptitiously-rebuild-list)
935 (goto-char bookmark-current-point))
936 (kill-buffer (current-buffer)))
939 (defun bookmark-edit-annotation (bookmark)
940 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
941 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
942 (bookmark-edit-annotation-mode bookmark))
945 (defun bookmark-insert-current-bookmark ()
946 "Insert this buffer's value of `bookmark-current-bookmark'.
947 Default to file name if it's nil."
948 (interactive)
949 (let ((str
950 (save-excursion
951 (set-buffer bookmark-current-buffer)
952 bookmark-current-bookmark)))
953 (if str (insert str) (bookmark-insert-buffer-name))))
956 (defun bookmark-insert-buffer-name ()
957 "Insert the current file name into the bookmark name being set.
958 The directory part of the file name is not used."
959 (interactive)
960 (let ((str
961 (save-excursion
962 (set-buffer bookmark-current-buffer)
963 (bookmark-buffer-name))))
964 (insert str)))
967 (defun bookmark-buffer-name ()
968 "Return the name of the current buffer's file, non-directory.
969 In Info, return the current node."
970 (cond
971 ;; Are we in Info?
972 ((string-equal mode-name "Info") Info-current-node)
973 ;; Or are we a file?
974 (buffer-file-name (file-name-nondirectory buffer-file-name))
975 ;; Or are we a directory?
976 ((and (boundp 'dired-directory) dired-directory)
977 (let* ((dirname (if (stringp dired-directory)
978 dired-directory
979 (car dired-directory)))
980 (idx (1- (length dirname))))
981 ;; Strip the trailing slash.
982 (if (= ?/ (aref dirname idx))
983 (file-name-nondirectory (substring dirname 0 idx))
984 ;; Else return the current-buffer
985 (buffer-name (current-buffer)))))
986 ;; If all else fails, use the buffer's name.
988 (buffer-name (current-buffer)))))
991 (defun bookmark-yank-word ()
992 (interactive)
993 ;; get the next word from the buffer and append it to the name of
994 ;; the bookmark currently being set.
995 (let ((string (save-excursion
996 (set-buffer bookmark-current-buffer)
997 (goto-char bookmark-yank-point)
998 (buffer-substring-no-properties
999 (point)
1000 (progn
1001 (forward-word 1)
1002 (setq bookmark-yank-point (point)))))))
1003 (insert string)))
1006 (defvar Info-current-file)
1008 (defun bookmark-buffer-file-name ()
1009 "Return the current buffer's file in a way useful for bookmarks.
1010 For example, if this is a Info buffer, return the Info file's name."
1011 (cond
1012 ((eq major-mode 'Info-mode)
1013 Info-current-file)
1014 (buffer-file-name
1015 ;; Abbreviate the path, both so it's shorter and so it's more
1016 ;; portable. E.g., the user's home dir might be a different
1017 ;; path on different machines, but "~/" will still reach it.
1018 (abbreviate-file-name buffer-file-name))
1019 ((and (boundp 'dired-directory) dired-directory)
1020 (if (stringp dired-directory)
1021 dired-directory
1022 (car dired-directory)))))
1025 (defun bookmark-maybe-load-default-file ()
1026 (and (not bookmarks-already-loaded)
1027 (null bookmark-alist)
1028 (prog2
1029 (and
1030 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1031 ;; to be renamed.
1032 (file-exists-p (expand-file-name bookmark-old-default-file))
1033 (not (file-exists-p (expand-file-name bookmark-default-file)))
1034 (rename-file (expand-file-name bookmark-old-default-file)
1035 (expand-file-name bookmark-default-file)))
1036 ;; return t so the `and' will continue...
1039 (file-readable-p (expand-file-name bookmark-default-file))
1040 (bookmark-load bookmark-default-file t t)
1041 (setq bookmarks-already-loaded t)))
1044 (defun bookmark-maybe-sort-alist ()
1045 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1046 ;;is non-nil, then return a sorted copy of the alist.
1047 (if bookmark-sort-flag
1048 (sort (copy-alist bookmark-alist)
1049 (function
1050 (lambda (x y) (string-lessp (car x) (car y)))))
1051 bookmark-alist))
1054 (defvar bookmark-after-jump-hook nil
1055 "Hook run after `bookmark-jump' jumps to a bookmark.
1056 Useful for example to unhide text in `outline-mode'.")
1058 ;;;###autoload
1059 (defun bookmark-jump (bookmark)
1060 "Jump to bookmark BOOKMARK (a point in some file).
1061 You may have a problem using this function if the value of variable
1062 `bookmark-alist' is nil. If that happens, you need to load in some
1063 bookmarks. See help on function `bookmark-load' for more about
1064 this.
1066 If the file pointed to by BOOKMARK no longer exists, you will be asked
1067 if you wish to give the bookmark a new location, and `bookmark-jump'
1068 will then jump to the new location, as well as recording it in place
1069 of the old one in the permanent bookmark record."
1070 (interactive
1071 (list (bookmark-completing-read "Jump to bookmark"
1072 bookmark-current-bookmark)))
1073 (unless bookmark
1074 (error "No bookmark specified"))
1075 (bookmark-maybe-historicize-string bookmark)
1076 (let ((cell (bookmark-jump-noselect bookmark)))
1077 (and cell
1078 (switch-to-buffer (car cell))
1079 (goto-char (cdr cell))
1080 (progn (run-hooks 'bookmark-after-jump-hook) t)
1081 (if bookmark-automatically-show-annotations
1082 ;; if there is an annotation for this bookmark,
1083 ;; show it in a buffer.
1084 (bookmark-show-annotation bookmark)))))
1087 ;;;###autoload
1088 (defun bookmark-jump-other-window (bookmark)
1089 "Jump to BOOKMARK (a point in some file) in another window.
1090 See `bookmark-jump'."
1091 (interactive
1092 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
1093 bookmark-current-bookmark)))
1094 (if (> emacs-major-version 21)
1095 (list bkm) bkm)))
1096 (when bookmark
1097 (bookmark-maybe-historicize-string bookmark)
1098 (let ((cell (bookmark-jump-noselect bookmark)))
1099 (and cell
1100 (switch-to-buffer-other-window (car cell))
1101 (goto-char (cdr cell))
1102 (if bookmark-automatically-show-annotations
1103 ;; if there is an annotation for this bookmark,
1104 ;; show it in a buffer.
1105 (bookmark-show-annotation bookmark))))))
1108 (defun bookmark-file-or-variation-thereof (file)
1109 "Return FILE (a string) if it exists, or return a reasonable
1110 variation of FILE if that exists. Reasonable variations are checked
1111 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1112 nor a reasonable variation thereof, then still return FILE if it can
1113 be retrieved from a VC backend, else return nil."
1114 (if (file-exists-p file)
1115 file
1117 (progn (require 'info) ; ensure Info-suffix-list is bound
1118 (catch 'found
1119 (mapc (lambda (elt)
1120 (let ((suffixed-file (concat file (car elt))))
1121 (if (file-exists-p suffixed-file)
1122 (throw 'found suffixed-file))))
1123 Info-suffix-list)
1124 nil))
1125 ;; Last possibility: try VC
1126 (if (vc-backend file) file))))
1129 (defun bookmark-jump-noselect (str)
1130 ;; a leetle helper for bookmark-jump :-)
1131 ;; returns (BUFFER . POINT)
1132 (bookmark-maybe-load-default-file)
1133 (let* ((file (expand-file-name (bookmark-get-filename str)))
1134 (forward-str (bookmark-get-front-context-string str))
1135 (behind-str (bookmark-get-rear-context-string str))
1136 (place (bookmark-get-position str))
1137 (info-node (bookmark-get-info-node str))
1138 (orig-file file)
1140 (if (setq file (bookmark-file-or-variation-thereof file))
1141 (save-excursion
1142 (save-window-excursion
1143 (if info-node
1144 ;; Info nodes must be visited with care.
1145 (progn
1146 (require 'info)
1147 (with-no-warnings
1148 (Info-find-node file info-node)))
1149 ;; Else no Info. Can do an ordinary find-file:
1150 (set-buffer (find-file-noselect file))
1151 (goto-char place))
1153 ;; Go searching forward first. Then, if forward-str exists and
1154 ;; was found in the file, we can search backward for behind-str.
1155 ;; Rationale is that if text was inserted between the two in the
1156 ;; file, it's better to be put before it so you can read it,
1157 ;; rather than after and remain perhaps unaware of the changes.
1158 (if forward-str
1159 (if (search-forward forward-str (point-max) t)
1160 (goto-char (match-beginning 0))))
1161 (if behind-str
1162 (if (search-backward behind-str (point-min) t)
1163 (goto-char (match-end 0))))
1164 ;; added by db
1165 (setq bookmark-current-bookmark str)
1166 (cons (current-buffer) (point))))
1168 ;; Else unable to find the marked file, so ask if user wants to
1169 ;; relocate the bookmark, else remind them to consider deletion.
1170 (ding)
1171 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1172 " nonexistent. Relocate \""
1174 "\"? "))
1175 (progn
1176 (bookmark-relocate str)
1177 ;; gasp! It's a recursive function call in Emacs Lisp!
1178 (bookmark-jump-noselect str))
1179 (message
1180 "Bookmark not relocated; consider removing it \(%s\)." str)
1181 nil))))
1184 ;;;###autoload
1185 (defun bookmark-relocate (bookmark)
1186 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1187 This makes an already existing bookmark point to that file, instead of
1188 the one it used to point at. Useful when a file has been renamed
1189 after a bookmark was set in it."
1190 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1191 (bookmark-maybe-historicize-string bookmark)
1192 (bookmark-maybe-load-default-file)
1193 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1194 (newloc (expand-file-name
1195 (read-file-name
1196 (format "Relocate %s to: " bookmark)
1197 (file-name-directory bmrk-filename)))))
1198 (bookmark-set-filename bookmark newloc)
1199 (bookmark-bmenu-surreptitiously-rebuild-list)))
1202 ;;;###autoload
1203 (defun bookmark-insert-location (bookmark &optional no-history)
1204 "Insert the name of the file associated with BOOKMARK.
1205 Optional second arg NO-HISTORY means don't record this in the
1206 minibuffer history list `bookmark-history'."
1207 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1208 (or no-history (bookmark-maybe-historicize-string bookmark))
1209 (let ((start (point)))
1210 (prog1
1211 (insert (bookmark-location bookmark)) ; *Return this line*
1212 (if (and (display-color-p) (display-mouse-p))
1213 (add-text-properties
1214 start
1215 (save-excursion (re-search-backward
1216 "[^ \t]")
1217 (1+ (point)))
1218 '(mouse-face highlight
1219 follow-link t
1220 help-echo "mouse-2: go to this bookmark in other window"))))))
1222 ;;;###autoload
1223 (defalias 'bookmark-locate 'bookmark-insert-location)
1225 (defun bookmark-location (bookmark)
1226 "Return the name of the file associated with BOOKMARK."
1227 (bookmark-maybe-load-default-file)
1228 (bookmark-get-filename bookmark))
1231 ;;;###autoload
1232 (defun bookmark-rename (old &optional new)
1233 "Change the name of OLD bookmark to NEW name.
1234 If called from keyboard, prompt for OLD and NEW. If called from
1235 menubar, select OLD from a menu and prompt for NEW.
1237 If called from Lisp, prompt for NEW if only OLD was passed as an
1238 argument. If called with two strings, then no prompting is done. You
1239 must pass at least OLD when calling from Lisp.
1241 While you are entering the new name, consecutive C-w's insert
1242 consecutive words from the text of the buffer into the new bookmark
1243 name."
1244 (interactive (list (bookmark-completing-read "Old bookmark name")))
1245 (bookmark-maybe-historicize-string old)
1246 (bookmark-maybe-load-default-file)
1248 (setq bookmark-current-point (point))
1249 (setq bookmark-yank-point (point))
1250 (setq bookmark-current-buffer (current-buffer))
1251 (let ((newname
1252 (or new ; use second arg, if non-nil
1253 (read-from-minibuffer
1254 "New name: "
1256 (let ((now-map (copy-keymap minibuffer-local-map)))
1257 (define-key now-map "\C-w" 'bookmark-yank-word)
1258 now-map)
1260 'bookmark-history))))
1261 (bookmark-set-name old newname)
1262 (setq bookmark-current-bookmark newname)
1263 (bookmark-bmenu-surreptitiously-rebuild-list)
1264 (setq bookmark-alist-modification-count
1265 (1+ bookmark-alist-modification-count))
1266 (if (bookmark-time-to-save-p)
1267 (bookmark-save))))
1270 ;;;###autoload
1271 (defun bookmark-insert (bookmark)
1272 "Insert the text of the file pointed to by bookmark BOOKMARK.
1273 You may have a problem using this function if the value of variable
1274 `bookmark-alist' is nil. If that happens, you need to load in some
1275 bookmarks. See help on function `bookmark-load' for more about
1276 this."
1277 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1278 (bookmark-maybe-historicize-string bookmark)
1279 (bookmark-maybe-load-default-file)
1280 (let ((orig-point (point))
1281 (str-to-insert
1282 (save-excursion
1283 (set-buffer (car (bookmark-jump-noselect bookmark)))
1284 (buffer-string))))
1285 (insert str-to-insert)
1286 (push-mark)
1287 (goto-char orig-point)))
1290 ;;;###autoload
1291 (defun bookmark-delete (bookmark &optional batch)
1292 "Delete BOOKMARK from the bookmark list.
1293 Removes only the first instance of a bookmark with that name. If
1294 there are one or more other bookmarks with the same name, they will
1295 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1296 one most recently used in this file, if any\).
1297 Optional second arg BATCH means don't update the bookmark list buffer,
1298 probably because we were called from there."
1299 (interactive
1300 (list (bookmark-completing-read "Delete bookmark"
1301 bookmark-current-bookmark)))
1302 (bookmark-maybe-historicize-string bookmark)
1303 (bookmark-maybe-load-default-file)
1304 (let ((will-go (bookmark-get-bookmark bookmark)))
1305 (setq bookmark-alist (delq will-go bookmark-alist))
1306 ;; Added by db, nil bookmark-current-bookmark if the last
1307 ;; occurrence has been deleted
1308 (or (bookmark-get-bookmark bookmark-current-bookmark)
1309 (setq bookmark-current-bookmark nil)))
1310 ;; Don't rebuild the list
1311 (if batch
1313 (bookmark-bmenu-surreptitiously-rebuild-list)
1314 (setq bookmark-alist-modification-count
1315 (1+ bookmark-alist-modification-count))
1316 (if (bookmark-time-to-save-p)
1317 (bookmark-save))))
1320 (defun bookmark-time-to-save-p (&optional last-time)
1321 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1322 ;; finds out whether it's time to save bookmarks to a file, by
1323 ;; examining the value of variable bookmark-save-flag, and maybe
1324 ;; bookmark-alist-modification-count. Returns t if they should be
1325 ;; saved, nil otherwise. if last-time is non-nil, then this is
1326 ;; being called when emacs is killed.
1327 (cond (last-time
1328 (and (> bookmark-alist-modification-count 0)
1329 bookmark-save-flag))
1330 ((numberp bookmark-save-flag)
1331 (>= bookmark-alist-modification-count bookmark-save-flag))
1333 nil)))
1336 ;;;###autoload
1337 (defun bookmark-write ()
1338 "Write bookmarks to a file (reading the file name with the minibuffer).
1339 Don't use this in Lisp programs; use `bookmark-save' instead."
1340 (interactive)
1341 (bookmark-maybe-load-default-file)
1342 (bookmark-save t))
1345 ;;;###autoload
1346 (defun bookmark-save (&optional parg file)
1347 "Save currently defined bookmarks.
1348 Saves by default in the file defined by the variable
1349 `bookmark-default-file'. With a prefix arg, save it in file FILE
1350 \(second argument\).
1352 If you are calling this from Lisp, the two arguments are PARG and
1353 FILE, and if you just want it to write to the default file, then
1354 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1355 instead. If you pass in one argument, and it is non-nil, then the
1356 user will be interactively queried for a file to save in.
1358 When you want to load in the bookmarks from a file, use
1359 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1360 for a file, defaulting to the file defined by variable
1361 `bookmark-default-file'."
1362 (interactive "P")
1363 (bookmark-maybe-load-default-file)
1364 (cond
1365 ((and (null parg) (null file))
1366 ;;whether interactive or not, write to default file
1367 (bookmark-write-file bookmark-default-file))
1368 ((and (null parg) file)
1369 ;;whether interactive or not, write to given file
1370 (bookmark-write-file file))
1371 ((and parg (not file))
1372 ;;have been called interactively w/ prefix arg
1373 (let ((file (read-file-name "File to save bookmarks in: ")))
1374 (bookmark-write-file file)))
1375 (t ; someone called us with prefix-arg *and* a file, so just write to file
1376 (bookmark-write-file file)))
1377 ;; signal that we have synced the bookmark file by setting this to
1378 ;; 0. If there was an error at any point before, it will not get
1379 ;; set, which is what we want.
1380 (setq bookmark-alist-modification-count 0))
1384 (defun bookmark-write-file (file)
1385 (save-excursion
1386 (save-window-excursion
1387 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1388 (set-buffer (get-buffer-create " *Bookmarks*"))
1389 (goto-char (point-min))
1390 (delete-region (point-min) (point-max))
1391 (let ((print-length nil)
1392 (print-level nil))
1393 (bookmark-insert-file-format-version-stamp)
1394 (pp bookmark-alist (current-buffer))
1395 (let ((version-control
1396 (cond
1397 ((null bookmark-version-control) nil)
1398 ((eq 'never bookmark-version-control) 'never)
1399 ((eq 'nospecial bookmark-version-control) version-control)
1401 t))))
1402 (condition-case nil
1403 (write-region (point-min) (point-max) file)
1404 (file-error (message "Can't write %s" file)))
1405 (kill-buffer (current-buffer))
1406 (bookmark-maybe-message
1407 "Saving bookmarks to file %s...done" file))))))
1410 (defun bookmark-import-new-list (new-list)
1411 ;; Walk over the new list, adding each individual bookmark
1412 ;; carefully. "Carefully" means checking against the existing
1413 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1414 ;; as necessary.
1415 (let ((lst new-list)
1416 (names (bookmark-all-names)))
1417 (while lst
1418 (let* ((full-record (car lst)))
1419 (bookmark-maybe-rename full-record names)
1420 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1421 (setq names (cons (bookmark-name-from-full-record full-record) names))
1422 (setq lst (cdr lst))))))
1425 (defun bookmark-maybe-rename (full-record names)
1426 ;; just a helper for bookmark-import-new-list; it is only for
1427 ;; readability that this is not inlined.
1429 ;; Once this has found a free name, it sets full-record to that
1430 ;; name.
1431 (let ((found-name (bookmark-name-from-full-record full-record)))
1432 (if (member found-name names)
1433 ;; We've got a conflict, so generate a new name
1434 (let ((count 2)
1435 (new-name found-name))
1436 (while (member new-name names)
1437 (setq new-name (concat found-name (format "<%d>" count)))
1438 (setq count (1+ count)))
1439 (bookmark-set-name full-record new-name)))))
1442 ;;;###autoload
1443 (defun bookmark-load (file &optional overwrite no-msg)
1444 "Load bookmarks from FILE (which must be in bookmark format).
1445 Appends loaded bookmarks to the front of the list of bookmarks. If
1446 optional second argument OVERWRITE is non-nil, existing bookmarks are
1447 destroyed. Optional third arg NO-MSG means don't display any messages
1448 while loading.
1450 If you load a file that doesn't contain a proper bookmark alist, you
1451 will corrupt Emacs's bookmark list. Generally, you should only load
1452 in files that were created with the bookmark functions in the first
1453 place. Your own personal bookmark file, `~/.emacs.bmk', is
1454 maintained automatically by Emacs; you shouldn't need to load it
1455 explicitly.
1457 If you load a file containing bookmarks with the same names as
1458 bookmarks already present in your Emacs, the new bookmarks will get
1459 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1460 method buffers use to resolve name collisions."
1461 (interactive
1462 (list (read-file-name
1463 (format "Load bookmarks from: (%s) "
1464 bookmark-default-file)
1465 ;;Default might not be used often,
1466 ;;but there's no better default, and
1467 ;;I guess it's better than none at all.
1468 "~/" bookmark-default-file 'confirm)))
1469 (setq file (expand-file-name file))
1470 (if (file-readable-p file)
1471 (save-excursion
1472 (save-window-excursion
1473 (if (null no-msg)
1474 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1475 (set-buffer (let ((enable-local-variables nil))
1476 (find-file-noselect file)))
1477 (goto-char (point-min))
1478 (bookmark-maybe-upgrade-file-format)
1479 (let ((blist (bookmark-alist-from-buffer)))
1480 (if (listp blist)
1481 (progn
1482 (if overwrite
1483 (progn
1484 (setq bookmark-alist blist)
1485 (setq bookmark-alist-modification-count 0))
1486 ;; else
1487 (bookmark-import-new-list blist)
1488 (setq bookmark-alist-modification-count
1489 (1+ bookmark-alist-modification-count)))
1490 (if (string-equal
1491 (expand-file-name bookmark-default-file)
1492 file)
1493 (setq bookmarks-already-loaded t))
1494 (bookmark-bmenu-surreptitiously-rebuild-list))
1495 (error "Invalid bookmark list in %s" file)))
1496 (kill-buffer (current-buffer)))
1497 (if (null no-msg)
1498 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1499 (error "Cannot read bookmark file %s" file)))
1503 ;;; Code supporting the dired-like bookmark menu. Prefix is
1504 ;;; "bookmark-bmenu" for "buffer-menu":
1507 (defvar bookmark-bmenu-bookmark-column nil)
1510 (defvar bookmark-bmenu-hidden-bookmarks ())
1513 (defvar bookmark-bmenu-mode-map nil)
1516 (if bookmark-bmenu-mode-map
1518 (setq bookmark-bmenu-mode-map (make-keymap))
1519 (suppress-keymap bookmark-bmenu-mode-map t)
1520 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1521 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1522 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1523 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1524 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1525 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1526 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1527 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1528 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1529 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1530 (define-key bookmark-bmenu-mode-map "\C-o"
1531 'bookmark-bmenu-switch-other-window)
1532 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1533 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1534 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1535 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1536 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1537 (define-key bookmark-bmenu-mode-map " " 'next-line)
1538 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1539 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1540 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1541 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1542 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1543 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1544 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1545 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1546 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1547 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1548 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1549 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1550 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1551 (define-key bookmark-bmenu-mode-map [mouse-2]
1552 'bookmark-bmenu-other-window-with-mouse))
1556 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1557 ;; data.
1558 (put 'bookmark-bmenu-mode 'mode-class 'special)
1561 ;; todo: need to display whether or not bookmark exists as a buffer in
1562 ;; flag column.
1564 ;; Format:
1565 ;; FLAGS BOOKMARK [ LOCATION ]
1568 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1569 "Rebuild the Bookmark List if it exists.
1570 Don't affect the buffer ring order."
1571 (if (get-buffer "*Bookmark List*")
1572 (save-excursion
1573 (save-window-excursion
1574 (bookmark-bmenu-list)))))
1577 ;;;###autoload
1578 (defun bookmark-bmenu-list ()
1579 "Display a list of existing bookmarks.
1580 The list is displayed in a buffer named `*Bookmark List*'.
1581 The leftmost column displays a D if the bookmark is flagged for
1582 deletion, or > if it is flagged for displaying."
1583 (interactive)
1584 (bookmark-maybe-load-default-file)
1585 (if (interactive-p)
1586 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1587 (set-buffer (get-buffer-create "*Bookmark List*")))
1588 (let ((inhibit-read-only t))
1589 (erase-buffer)
1590 (insert "% Bookmark\n- --------\n")
1591 (add-text-properties (point-min) (point)
1592 '(font-lock-face bookmark-menu-heading))
1593 (mapcar
1594 (lambda (full-record)
1595 ;; if a bookmark has an annotation, prepend a "*"
1596 ;; in the list of bookmarks.
1597 (let ((annotation (bookmark-get-annotation
1598 (bookmark-name-from-full-record full-record))))
1599 (if (and annotation (not (string-equal annotation "")))
1600 (insert " *")
1601 (insert " "))
1602 (let ((start (point)))
1603 (insert (bookmark-name-from-full-record full-record))
1604 (if (and (display-color-p) (display-mouse-p))
1605 (add-text-properties
1606 start
1607 (save-excursion (re-search-backward
1608 "[^ \t]")
1609 (1+ (point)))
1610 '(mouse-face highlight
1611 follow-link t
1612 help-echo "mouse-2: go to this bookmark in other window")))
1613 (insert "\n")
1615 (bookmark-maybe-sort-alist)))
1616 (goto-char (point-min))
1617 (forward-line 2)
1618 (bookmark-bmenu-mode)
1619 (if bookmark-bmenu-toggle-filenames
1620 (bookmark-bmenu-toggle-filenames t)))
1622 ;;;###autoload
1623 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1624 ;;;###autoload
1625 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1629 (defun bookmark-bmenu-mode ()
1630 "Major mode for editing a list of bookmarks.
1631 Each line describes one of the bookmarks in Emacs.
1632 Letters do not insert themselves; instead, they are commands.
1633 Bookmark names preceded by a \"*\" have annotations.
1634 \\<bookmark-bmenu-mode-map>
1635 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1636 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1637 Also show bookmarks marked using m in other windows.
1638 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1639 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1640 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1641 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1642 together with bookmark selected before this one in another window.
1643 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1644 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1645 so the bookmark menu bookmark remains visible in its window.
1646 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1647 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1648 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1649 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1650 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1651 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1652 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1653 With a prefix arg, prompts for a file to save in.
1654 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1655 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1656 With prefix argument, also move up one line.
1657 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1658 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1659 in another buffer.
1660 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1661 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1662 (kill-all-local-variables)
1663 (use-local-map bookmark-bmenu-mode-map)
1664 (setq truncate-lines t)
1665 (setq buffer-read-only t)
1666 (setq major-mode 'bookmark-bmenu-mode)
1667 (setq mode-name "Bookmark Menu")
1668 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1671 (defun bookmark-bmenu-toggle-filenames (&optional show)
1672 "Toggle whether filenames are shown in the bookmark list.
1673 Optional argument SHOW means show them unconditionally."
1674 (interactive)
1675 (cond
1676 (show
1677 (setq bookmark-bmenu-toggle-filenames nil)
1678 (bookmark-bmenu-show-filenames)
1679 (setq bookmark-bmenu-toggle-filenames t))
1680 (bookmark-bmenu-toggle-filenames
1681 (bookmark-bmenu-hide-filenames)
1682 (setq bookmark-bmenu-toggle-filenames nil))
1684 (bookmark-bmenu-show-filenames)
1685 (setq bookmark-bmenu-toggle-filenames t))))
1688 (defun bookmark-bmenu-show-filenames (&optional force)
1689 (if (and (not force) bookmark-bmenu-toggle-filenames)
1690 nil ;already shown, so do nothing
1691 (save-excursion
1692 (save-window-excursion
1693 (goto-char (point-min))
1694 (forward-line 2)
1695 (setq bookmark-bmenu-hidden-bookmarks ())
1696 (let ((inhibit-read-only t))
1697 (while (< (point) (point-max))
1698 (let ((bmrk (bookmark-bmenu-bookmark)))
1699 (setq bookmark-bmenu-hidden-bookmarks
1700 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1701 (let ((start (save-excursion (end-of-line) (point))))
1702 (move-to-column bookmark-bmenu-file-column t)
1703 ;; Strip off `mouse-face' from the white spaces region.
1704 (if (and (display-color-p) (display-mouse-p))
1705 (remove-text-properties start (point)
1706 '(mouse-face nil help-echo nil))))
1707 (delete-region (point) (progn (end-of-line) (point)))
1708 (insert " ")
1709 ;; Pass the NO-HISTORY arg:
1710 (bookmark-insert-location bmrk t)
1711 (forward-line 1))))))))
1714 (defun bookmark-bmenu-hide-filenames (&optional force)
1715 (if (and (not force) bookmark-bmenu-toggle-filenames)
1716 ;; nothing to hide if above is nil
1717 (save-excursion
1718 (save-window-excursion
1719 (goto-char (point-min))
1720 (forward-line 2)
1721 (setq bookmark-bmenu-hidden-bookmarks
1722 (nreverse bookmark-bmenu-hidden-bookmarks))
1723 (save-excursion
1724 (goto-char (point-min))
1725 (search-forward "Bookmark")
1726 (backward-word 1)
1727 (setq bookmark-bmenu-bookmark-column (current-column)))
1728 (save-excursion
1729 (let ((inhibit-read-only t))
1730 (while bookmark-bmenu-hidden-bookmarks
1731 (move-to-column bookmark-bmenu-bookmark-column t)
1732 (bookmark-kill-line)
1733 (let ((start (point)))
1734 (insert (car bookmark-bmenu-hidden-bookmarks))
1735 (if (and (display-color-p) (display-mouse-p))
1736 (add-text-properties
1737 start
1738 (save-excursion (re-search-backward
1739 "[^ \t]")
1740 (1+ (point)))
1741 '(mouse-face highlight
1742 follow-link t
1743 help-echo
1744 "mouse-2: go to this bookmark in other window"))))
1745 (setq bookmark-bmenu-hidden-bookmarks
1746 (cdr bookmark-bmenu-hidden-bookmarks))
1747 (forward-line 1))))))))
1750 (defun bookmark-bmenu-check-position ()
1751 ;; Returns non-nil if on a line with a bookmark.
1752 ;; (The actual value returned is bookmark-alist).
1753 ;; Else reposition and try again, else return nil.
1754 (cond ((< (count-lines (point-min) (point)) 2)
1755 (goto-char (point-min))
1756 (forward-line 2)
1757 bookmark-alist)
1758 ((and (bolp) (eobp))
1759 (beginning-of-line 0)
1760 bookmark-alist)
1762 bookmark-alist)))
1765 (defun bookmark-bmenu-bookmark ()
1766 ;; return a string which is bookmark of this line.
1767 (if (bookmark-bmenu-check-position)
1768 (save-excursion
1769 (save-window-excursion
1770 (goto-char (point-min))
1771 (search-forward "Bookmark")
1772 (backward-word 1)
1773 (setq bookmark-bmenu-bookmark-column (current-column)))))
1774 (if bookmark-bmenu-toggle-filenames
1775 (bookmark-bmenu-hide-filenames))
1776 (save-excursion
1777 (save-window-excursion
1778 (beginning-of-line)
1779 (forward-char bookmark-bmenu-bookmark-column)
1780 (prog1
1781 (buffer-substring-no-properties (point)
1782 (progn
1783 (end-of-line)
1784 (point)))
1785 ;; well, this is certainly crystal-clear:
1786 (if bookmark-bmenu-toggle-filenames
1787 (bookmark-bmenu-toggle-filenames t))))))
1790 (defun bookmark-show-annotation (bookmark)
1791 "Display the annotation for bookmark named BOOKMARK in a buffer,
1792 if an annotation exists."
1793 (let ((annotation (bookmark-get-annotation bookmark)))
1794 (if (and annotation (not (string-equal annotation "")))
1795 (save-excursion
1796 (let ((old-buf (current-buffer)))
1797 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1798 (delete-region (point-min) (point-max))
1799 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1800 (insert annotation)
1801 (goto-char (point-min))
1802 (pop-to-buffer old-buf))))))
1805 (defun bookmark-show-all-annotations ()
1806 "Display the annotations for all bookmarks in a buffer."
1807 (let ((old-buf (current-buffer)))
1808 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1809 (delete-region (point-min) (point-max))
1810 (mapcar
1811 (lambda (full-record)
1812 (let* ((name (bookmark-name-from-full-record full-record))
1813 (ann (bookmark-get-annotation name)))
1814 (insert (concat name ":\n"))
1815 (if (and ann (not (string-equal ann "")))
1816 ;; insert the annotation, indented by 4 spaces.
1817 (progn
1818 (save-excursion (insert ann) (unless (bolp)
1819 (insert "\n")))
1820 (while (< (point) (point-max))
1821 (beginning-of-line) ; paranoia
1822 (insert " ")
1823 (forward-line)
1824 (end-of-line))))))
1825 bookmark-alist)
1826 (goto-char (point-min))
1827 (pop-to-buffer old-buf)))
1830 (defun bookmark-bmenu-mark ()
1831 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1832 (interactive)
1833 (beginning-of-line)
1834 (if (bookmark-bmenu-check-position)
1835 (let ((inhibit-read-only t))
1836 (delete-char 1)
1837 (insert ?>)
1838 (forward-line 1)
1839 (bookmark-bmenu-check-position))))
1842 (defun bookmark-bmenu-select ()
1843 "Select this line's bookmark; also display bookmarks marked with `>'.
1844 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1845 (interactive)
1846 (if (bookmark-bmenu-check-position)
1847 (let ((bmrk (bookmark-bmenu-bookmark))
1848 (menu (current-buffer))
1849 (others ())
1850 tem)
1851 (goto-char (point-min))
1852 (while (re-search-forward "^>" nil t)
1853 (setq tem (bookmark-bmenu-bookmark))
1854 (let ((inhibit-read-only t))
1855 (delete-char -1)
1856 (insert ?\s))
1857 (or (string-equal tem bmrk)
1858 (member tem others)
1859 (setq others (cons tem others))))
1860 (setq others (nreverse others)
1861 tem (/ (1- (frame-height)) (1+ (length others))))
1862 (delete-other-windows)
1863 (bookmark-jump bmrk)
1864 (bury-buffer menu)
1865 (if others
1866 (while others
1867 (split-window nil tem)
1868 (other-window 1)
1869 (bookmark-jump (car others))
1870 (setq others (cdr others)))
1871 (other-window 1)))))
1874 (defun bookmark-bmenu-save (parg)
1875 "Save the current list into a bookmark file.
1876 With a prefix arg, prompts for a file to save them in."
1877 (interactive "P")
1878 (save-excursion
1879 (save-window-excursion
1880 (bookmark-save parg))))
1883 (defun bookmark-bmenu-load ()
1884 "Load the bookmark file and rebuild the bookmark menu-buffer."
1885 (interactive)
1886 (if (bookmark-bmenu-check-position)
1887 (save-excursion
1888 (save-window-excursion
1889 ;; This will call `bookmark-bmenu-list'
1890 (call-interactively 'bookmark-load)))))
1893 (defun bookmark-bmenu-1-window ()
1894 "Select this line's bookmark, alone, in full frame."
1895 (interactive)
1896 (if (bookmark-bmenu-check-position)
1897 (progn
1898 (bookmark-jump (bookmark-bmenu-bookmark))
1899 (bury-buffer (other-buffer))
1900 (delete-other-windows))))
1903 (defun bookmark-bmenu-2-window ()
1904 "Select this line's bookmark, with previous buffer in second window."
1905 (interactive)
1906 (if (bookmark-bmenu-check-position)
1907 (let ((bmrk (bookmark-bmenu-bookmark))
1908 (menu (current-buffer))
1909 (pop-up-windows t))
1910 (delete-other-windows)
1911 (switch-to-buffer (other-buffer))
1912 (let* ((pair (bookmark-jump-noselect bmrk))
1913 (buff (car pair))
1914 (pos (cdr pair)))
1915 (pop-to-buffer buff)
1916 (goto-char pos))
1917 (bury-buffer menu))))
1920 (defun bookmark-bmenu-this-window ()
1921 "Select this line's bookmark in this window."
1922 (interactive)
1923 (if (bookmark-bmenu-check-position)
1924 (bookmark-jump (bookmark-bmenu-bookmark))))
1927 (defun bookmark-bmenu-other-window ()
1928 "Select this line's bookmark in other window, leaving bookmark menu visible."
1929 (interactive)
1930 (let ((bookmark (bookmark-bmenu-bookmark)))
1931 (if (bookmark-bmenu-check-position)
1932 (let* ((pair (bookmark-jump-noselect bookmark))
1933 (buff (car pair))
1934 (pos (cdr pair)))
1935 (switch-to-buffer-other-window buff)
1936 (goto-char pos)
1937 (set-window-point (get-buffer-window buff) pos)
1938 (bookmark-show-annotation bookmark)))))
1941 (defun bookmark-bmenu-switch-other-window ()
1942 "Make the other window select this line's bookmark.
1943 The current window remains selected."
1944 (interactive)
1945 (let ((bookmark (bookmark-bmenu-bookmark))
1946 (pop-up-windows t)
1947 same-window-buffer-names
1948 same-window-regexps)
1949 (if (bookmark-bmenu-check-position)
1950 (let* ((pair (bookmark-jump-noselect bookmark))
1951 (buff (car pair))
1952 (pos (cdr pair)))
1953 (display-buffer buff)
1954 (let ((o-buffer (current-buffer)))
1955 ;; save-excursion won't do
1956 (set-buffer buff)
1957 (goto-char pos)
1958 (set-window-point (get-buffer-window buff) pos)
1959 (set-buffer o-buffer))
1960 (bookmark-show-annotation bookmark)))))
1962 (defun bookmark-bmenu-other-window-with-mouse (event)
1963 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1964 (interactive "e")
1965 (save-excursion
1966 (set-buffer (window-buffer (posn-window (event-end event))))
1967 (save-excursion
1968 (goto-char (posn-point (event-end event)))
1969 (bookmark-bmenu-other-window))))
1972 (defun bookmark-bmenu-show-annotation ()
1973 "Show the annotation for the current bookmark in another window."
1974 (interactive)
1975 (let ((bookmark (bookmark-bmenu-bookmark)))
1976 (if (bookmark-bmenu-check-position)
1977 (bookmark-show-annotation bookmark))))
1980 (defun bookmark-bmenu-show-all-annotations ()
1981 "Show the annotation for all bookmarks in another window."
1982 (interactive)
1983 (bookmark-show-all-annotations))
1986 (defun bookmark-bmenu-edit-annotation ()
1987 "Edit the annotation for the current bookmark in another window."
1988 (interactive)
1989 (let ((bookmark (bookmark-bmenu-bookmark)))
1990 (if (bookmark-bmenu-check-position)
1991 (bookmark-edit-annotation bookmark))))
1994 (defun bookmark-bmenu-unmark (&optional backup)
1995 "Cancel all requested operations on bookmark on this line and move down.
1996 Optional BACKUP means move up."
1997 (interactive "P")
1998 (beginning-of-line)
1999 (if (bookmark-bmenu-check-position)
2000 (progn
2001 (let ((inhibit-read-only t))
2002 (delete-char 1)
2003 ;; any flags to reset according to circumstances? How about a
2004 ;; flag indicating whether this bookmark is being visited?
2005 ;; well, we don't have this now, so maybe later.
2006 (insert " "))
2007 (forward-line (if backup -1 1))
2008 (bookmark-bmenu-check-position))))
2011 (defun bookmark-bmenu-backup-unmark ()
2012 "Move up and cancel all requested operations on bookmark on line above."
2013 (interactive)
2014 (forward-line -1)
2015 (if (bookmark-bmenu-check-position)
2016 (progn
2017 (bookmark-bmenu-unmark)
2018 (forward-line -1)
2019 (bookmark-bmenu-check-position))))
2022 (defun bookmark-bmenu-delete ()
2023 "Mark bookmark on this line to be deleted.
2024 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2025 (interactive)
2026 (beginning-of-line)
2027 (if (bookmark-bmenu-check-position)
2028 (let ((inhibit-read-only t))
2029 (delete-char 1)
2030 (insert ?D)
2031 (forward-line 1)
2032 (bookmark-bmenu-check-position))))
2035 (defun bookmark-bmenu-delete-backwards ()
2036 "Mark bookmark on this line to be deleted, then move up one line.
2037 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2038 (interactive)
2039 (bookmark-bmenu-delete)
2040 (forward-line -2)
2041 (if (bookmark-bmenu-check-position)
2042 (forward-line 1))
2043 (bookmark-bmenu-check-position))
2046 (defun bookmark-bmenu-execute-deletions ()
2047 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2048 (interactive)
2049 (message "Deleting bookmarks...")
2050 (let ((hide-em bookmark-bmenu-toggle-filenames)
2051 (o-point (point))
2052 (o-str (save-excursion
2053 (beginning-of-line)
2054 (if (looking-at "^D")
2056 (buffer-substring
2057 (point)
2058 (progn (end-of-line) (point))))))
2059 (o-col (current-column)))
2060 (if hide-em (bookmark-bmenu-hide-filenames))
2061 (setq bookmark-bmenu-toggle-filenames nil)
2062 (goto-char (point-min))
2063 (forward-line 1)
2064 (while (re-search-forward "^D" (point-max) t)
2065 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2066 (bookmark-bmenu-list)
2067 (setq bookmark-bmenu-toggle-filenames hide-em)
2068 (if bookmark-bmenu-toggle-filenames
2069 (bookmark-bmenu-toggle-filenames t))
2070 (if o-str
2071 (progn
2072 (goto-char (point-min))
2073 (search-forward o-str)
2074 (beginning-of-line)
2075 (forward-char o-col))
2076 (goto-char o-point))
2077 (beginning-of-line)
2078 (setq bookmark-alist-modification-count
2079 (1+ bookmark-alist-modification-count))
2080 (if (bookmark-time-to-save-p)
2081 (bookmark-save))
2082 (message "Deleting bookmarks...done")
2086 (defun bookmark-bmenu-rename ()
2087 "Rename bookmark on current line. Prompts for a new name."
2088 (interactive)
2089 (if (bookmark-bmenu-check-position)
2090 (let ((bmrk (bookmark-bmenu-bookmark))
2091 (thispoint (point)))
2092 (bookmark-rename bmrk)
2093 (bookmark-bmenu-list)
2094 (goto-char thispoint))))
2097 (defun bookmark-bmenu-locate ()
2098 "Display location of this bookmark. Displays in the minibuffer."
2099 (interactive)
2100 (if (bookmark-bmenu-check-position)
2101 (let ((bmrk (bookmark-bmenu-bookmark)))
2102 (message "%s" (bookmark-location bmrk)))))
2104 (defun bookmark-bmenu-relocate ()
2105 "Change the file path of the bookmark on the current line,
2106 prompting with completion for the new path."
2107 (interactive)
2108 (if (bookmark-bmenu-check-position)
2109 (let ((bmrk (bookmark-bmenu-bookmark))
2110 (thispoint (point)))
2111 (bookmark-relocate bmrk)
2112 (goto-char thispoint))))
2115 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2117 (defun bookmark-menu-popup-paned-menu (event name entries)
2118 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2119 That is, ENTRIES is a list of strings which appear as the choices
2120 in the menu.
2121 The number of panes depends on the number of entries.
2122 The visible entries are truncated to `bookmark-menu-length', but the
2123 strings returned are not."
2124 (let ((f-height (/ (frame-height) 2))
2125 (pane-list nil)
2126 (iter 0))
2127 (while entries
2128 (let (lst
2129 (count 0))
2130 (while (and (< count f-height) entries)
2131 (let ((str (car entries)))
2132 (push (cons
2133 (if (> (length str) bookmark-menu-length)
2134 (substring str 0 bookmark-menu-length)
2135 str)
2136 str)
2137 lst)
2138 (setq entries (cdr entries))
2139 (setq count (1+ count))))
2140 (setq iter (1+ iter))
2141 (push (cons
2142 (format "-*- %s (%d) -*-" name iter)
2143 (nreverse lst))
2144 pane-list)))
2146 ;; Popup the menu and return the string.
2147 (x-popup-menu event (cons (concat "-*- " name " -*-")
2148 (nreverse pane-list)))))
2151 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2152 ;; following works, and for explaining what to do to make it work.
2154 ;; We MUST autoload EACH form used to set up this variable's value, so
2155 ;; that the whole job is done in loaddefs.el.
2157 ;; Emacs menubar stuff.
2159 ;;;###autoload
2160 (defvar menu-bar-bookmark-map
2161 (let ((map (make-sparse-keymap "Bookmark functions")))
2162 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2163 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2164 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2165 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2166 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2167 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2168 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2169 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2170 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2171 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2172 map))
2174 ;;;###autoload
2175 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2177 ;; make bookmarks appear toward the right side of the menu.
2178 (if (boundp 'menu-bar-final-items)
2179 (if menu-bar-final-items
2180 (setq menu-bar-final-items
2181 (cons 'bookmark menu-bar-final-items)))
2182 (setq menu-bar-final-items '(bookmark)))
2184 ;;;; end bookmark menu stuff ;;;;
2187 ;;; Load Hook
2188 (defvar bookmark-load-hook nil
2189 "Hook run at the end of loading bookmark.")
2191 ;;; Exit Hook, called from kill-emacs-hook
2192 (defvar bookmark-exit-hook nil
2193 "Hook run when Emacs exits.")
2195 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2197 (defun bookmark-exit-hook-internal ()
2198 "Save bookmark state, if necessary, at Emacs exit time.
2199 This also runs `bookmark-exit-hook'."
2200 (run-hooks 'bookmark-exit-hook)
2201 (and bookmark-alist
2202 (bookmark-time-to-save-p t)
2203 (bookmark-save)))
2205 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2208 (run-hooks 'bookmark-load-hook)
2210 (provide 'bookmark)
2212 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2213 ;;; bookmark.el ends here