("latin-1-prefix"): Change ~s to give \e,A'\e(B and
[emacs.git] / lisp / bookmark.el
blob1aaefcba15a14d4fa933400b73c7c2272a456c7a
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later.
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This package is for setting "bookmarks" in files. A bookmark
30 ;; associates a string with a location in a certain file. Thus, you
31 ;; can navigate your way to that location by providing the string.
32 ;; See the "User Variables" section for customizations.
34 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
35 ;; then implementing the bookmark-current-bookmark idea. He even
36 ;; sent *patches*, bless his soul...
38 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
39 ;; fixing and improving bookmark-time-to-save-p.
41 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
42 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
43 ;; and the menu-bar).
45 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
46 ;; suggestions and the code to implement them (like
47 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
48 ;; stuff).
50 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
51 ;; for his eminently sensible suggestion to separate bookmark-jump
52 ;; into bookmark-jump and bookmark-jump-noselect, which made many
53 ;; other things cleaner as well.
55 ;; Thanks to Roland McGrath for encouragement and help with defining
56 ;; autoloads on the menu-bar.
58 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
59 ;; values in bookmark-jump and bookmark-set. Everybody please keep
60 ;; all the keystrokes they save thereby and send them to him at the
61 ;; end of each year :-) (No, seriously, thanks Jonathan!)
63 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
64 ;; thinking up the annotations feature and implementing it so well.
66 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
67 ;; <olstad@msc.edu>.
69 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
70 ;; reported and fixed.
72 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
74 ;; Enough with the credits already, get on to the good stuff:
76 ;; FAVORITE CHINESE RESTAURANT:
77 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
78 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
80 ;;;; Code:
82 (require 'pp)
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 "*Nil means don't 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 'boolean)
200 ;;; No user-serviceable parts beyond this point.
202 ;; Is it XEmacs?
203 (defconst bookmark-xemacsp
204 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
207 ;; Added for lucid emacs compatibility, db
208 (or (fboundp 'defalias) (fset 'defalias 'fset))
210 ;; suggested for lucid compatibility by david hughes:
211 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
213 ;; This variable is probably obsolete now...
214 (or (boundp 'baud-rate)
215 ;; some random value higher than 9600
216 (setq baud-rate 19200))
220 ;;; Keymap stuff:
222 ;; Set up these bindings dumping time *only*;
223 ;; if the user alters them, don't override the user when loading bookmark.el.
225 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
226 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
227 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
229 ;;;###autoload
230 (defvar bookmark-map nil
231 "Keymap containing bindings to bookmark functions.
232 It is not bound to any key by default: to bind it
233 so that you have a bookmark prefix, just use `global-set-key' and bind a
234 key of your choice to `bookmark-map'. All interactive bookmark
235 functions have a binding in this keymap.")
237 ;;;###autoload
238 (define-prefix-command 'bookmark-map)
240 ;; Read the help on all of these functions for details...
241 ;;;###autoload
242 (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload
244 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
245 ;;;###autoload
246 (define-key bookmark-map "j" 'bookmark-jump)
247 ;;;###autoload
248 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
249 ;;;###autoload
250 (define-key bookmark-map "i" 'bookmark-insert)
251 ;;;###autoload
252 (define-key bookmark-map "e" 'edit-bookmarks)
253 ;;;###autoload
254 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
255 ;;;###autoload
256 (define-key bookmark-map "r" 'bookmark-rename)
257 ;;;###autoload
258 (define-key bookmark-map "d" 'bookmark-delete)
259 ;;;###autoload
260 (define-key bookmark-map "l" 'bookmark-load)
261 ;;;###autoload
262 (define-key bookmark-map "w" 'bookmark-write)
263 ;;;###autoload
264 (define-key bookmark-map "s" 'bookmark-save)
267 ;;; The annotation maps.
268 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
269 "Keymap for composing an annotation for a bookmark.")
271 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
272 'bookmark-send-annotation)
276 ;;; Core variables and data structures:
277 (defvar bookmark-alist ()
278 "Association list of bookmarks and their records.
279 You probably don't want to change the value of this alist yourself;
280 instead, let the various bookmark functions do it for you.
282 The format of the alist is
284 \(BOOKMARK1 BOOKMARK2 ...\)
286 where each BOOKMARK is of the form
288 \(NAME
289 \(filename . FILE\)
290 \(front-context-string . FRONT-STR\)
291 \(rear-context-string . REAR-STR\)
292 \(position . POS\)
293 \(info-node . POS\)
294 \(annotation . ANNOTATION\)\)
296 So the cdr of each bookmark is an alist too.
297 `info-node' is optional, by the way.")
300 (defvar bookmarks-already-loaded nil)
303 ;; just add the hook to make sure that people don't lose bookmarks
304 ;; when they kill Emacs, unless they don't want to save them.
305 ;;;###autoload
306 (add-hook 'kill-emacs-hook
307 (function
308 (lambda () (and (featurep 'bookmark)
309 bookmark-alist
310 (bookmark-time-to-save-p t)
311 (bookmark-save)))))
313 ;; more stuff added by db.
315 (defvar bookmark-current-bookmark nil
316 "Name of bookmark most recently used in the current file.
317 It is buffer local, used to make moving a bookmark forward
318 through a file easier.")
320 (make-variable-buffer-local 'bookmark-current-bookmark)
323 (defvar bookmark-alist-modification-count 0
324 "Number of modifications to bookmark list since it was last saved.")
327 (defvar bookmark-search-size 16
328 "Length of the context strings recorded on either side of a bookmark.")
331 (defvar bookmark-current-point 0)
332 (defvar bookmark-yank-point 0)
333 (defvar bookmark-current-buffer nil)
337 ;; Helper functions.
339 ;; Only functions on this page and the next one (file formats) need to
340 ;; know anything about the format of bookmark-alist entries.
341 ;; Everyone else should go through them.
343 (defun bookmark-name-from-full-record (full-record)
344 "Return name of FULL-RECORD \(an alist element instead of a string\)."
345 (car full-record))
348 (defun bookmark-all-names ()
349 "Return a list of all current bookmark names."
350 (bookmark-maybe-load-default-file)
351 (mapcar
352 (lambda (full-record)
353 (bookmark-name-from-full-record full-record))
354 bookmark-alist))
357 (defun bookmark-get-bookmark (bookmark)
358 "Return the full entry for BOOKMARK in bookmark-alist."
359 (assoc bookmark bookmark-alist))
362 (defun bookmark-get-bookmark-record (bookmark)
363 "Return the guts of the entry for BOOKMARK in bookmark-alist.
364 That is, all information but the name."
365 (car (cdr (bookmark-get-bookmark bookmark))))
368 (defun bookmark-set-name (bookmark newname)
369 "Set BOOKMARK's name to NEWNAME."
370 (setcar
371 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
372 newname))
375 (defun bookmark-get-annotation (bookmark)
376 "Return the annotation of BOOKMARK, or nil if none."
377 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
380 (defun bookmark-set-annotation (bookmark ann)
381 "Set the annotation of BOOKMARK to ANN."
382 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
383 (if cell
384 (setcdr cell ann)
385 (nconc (bookmark-get-bookmark-record bookmark)
386 (list (cons 'annotation ann))))))
389 (defun bookmark-get-filename (bookmark)
390 "Return the full filename of BOOKMARK."
391 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
394 (defun bookmark-set-filename (bookmark filename)
395 "Set the full filename of BOOKMARK to FILENAME."
396 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
397 (if cell
398 (setcdr cell filename)
399 (nconc (bookmark-get-bookmark-record bookmark)
400 (list (cons 'filename filename))))))
403 (defun bookmark-get-position (bookmark)
404 "Return the position \(i.e.: point\) of BOOKMARK."
405 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
408 (defun bookmark-set-position (bookmark position)
409 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
410 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
411 (if cell
412 (setcdr cell position)
413 (nconc (bookmark-get-bookmark-record bookmark)
414 (list (cons 'position position))))))
417 (defun bookmark-get-front-context-string (bookmark)
418 "Return the front-context-string of BOOKMARK."
419 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
422 (defun bookmark-set-front-context-string (bookmark string)
423 "Set the front-context-string of BOOKMARK to STRING."
424 (let ((cell (assq 'front-context-string
425 (bookmark-get-bookmark-record bookmark))))
426 (if cell
427 (setcdr cell string)
428 (nconc (bookmark-get-bookmark-record bookmark)
429 (list (cons 'front-context-string string))))))
432 (defun bookmark-get-rear-context-string (bookmark)
433 "Return the rear-context-string of BOOKMARK."
434 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
437 (defun bookmark-set-rear-context-string (bookmark string)
438 "Set the rear-context-string of BOOKMARK to STRING."
439 (let ((cell (assq 'rear-context-string
440 (bookmark-get-bookmark-record bookmark))))
441 (if cell
442 (setcdr cell string)
443 (nconc (bookmark-get-bookmark-record bookmark)
444 (list (cons 'rear-context-string string))))))
447 (defun bookmark-get-info-node (bookmark)
448 "Get the info node associated with BOOKMARK."
449 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
452 (defun bookmark-set-info-node (bookmark node)
453 "Set the Info node of BOOKMARK to NODE."
454 (let ((cell (assq 'info-node
455 (bookmark-get-bookmark-record bookmark))))
456 (if cell
457 (setcdr cell node)
458 (nconc (bookmark-get-bookmark-record bookmark)
459 (list (cons 'info-node node)))))
461 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
462 (sit-for 4)
466 (defvar bookmark-history nil
467 "The history list for bookmark functions.")
470 (defun bookmark-completing-read (prompt &optional default)
471 "Prompting with PROMPT, read a bookmark name in completion.
472 PROMPT will get a \": \" stuck on the end no matter what, so you
473 probably don't want to include one yourself.
474 Optional second arg DEFAULT is a string to return if the user enters
475 the empty string."
476 (bookmark-maybe-load-default-file) ; paranoia
477 (let* ((completion-ignore-case bookmark-completion-ignore-case)
478 (default default)
479 (prompt (if default
480 (concat prompt (format " (%s): " default))
481 (concat prompt ": ")))
482 (str
483 (completing-read prompt
484 bookmark-alist
488 'bookmark-history)))
489 (if (string-equal "" str)
490 (list default)
491 (list str))))
494 (defmacro bookmark-maybe-historicize-string (string)
495 "Put STRING into the bookmark prompt history, if caller non-interactive.
496 We need this because sometimes bookmark functions are invoked from
497 menus, so `completing-read' never gets a chance to set `bookmark-history'."
498 (` (or
499 (interactive-p)
500 (setq bookmark-history (cons (, string) bookmark-history)))))
503 (defun bookmark-make (name &optional annotation overwrite info-node)
504 "Make a bookmark named NAME.
505 Optional second arg ANNOTATION gives it an annotation.
506 Optional third arg OVERWRITE means replace any existing bookmarks with
507 this name.
508 Optional fourth arg INFO-NODE means this bookmark is at info node
509 INFO-NODE, so record this fact in the bookmark's entry."
510 (bookmark-maybe-load-default-file)
511 (let ((stripped-name (copy-sequence name)))
512 (or bookmark-xemacsp
513 ;; XEmacs's `set-text-properties' doesn't work on
514 ;; free-standing strings, apparently.
515 (set-text-properties 0 (length stripped-name) nil stripped-name))
516 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
517 ;; already existing bookmark under that name and
518 ;; no prefix arg means just overwrite old bookmark
519 (setcdr (bookmark-get-bookmark stripped-name)
520 (list (bookmark-make-cell annotation info-node)))
522 ;; otherwise just cons it onto the front (either the bookmark
523 ;; doesn't exist already, or there is no prefix arg. In either
524 ;; case, we want the new bookmark consed onto the alist...)
526 (setq bookmark-alist
527 (cons
528 (list stripped-name
529 (bookmark-make-cell annotation info-node))
530 bookmark-alist)))
532 ;; Added by db
533 (setq bookmark-current-bookmark stripped-name)
534 (setq bookmark-alist-modification-count
535 (1+ bookmark-alist-modification-count))
536 (if (bookmark-time-to-save-p)
537 (bookmark-save))))
540 (defun bookmark-make-cell (annotation &optional info-node)
541 "Return the record part of a new bookmark, given ANNOTATION.
542 Must be at the correct position in the buffer in which the bookmark is
543 being set. This might change someday.
544 Optional second arg INFO-NODE means this bookmark is at info node
545 INFO-NODE, so record this fact in the bookmark's entry."
546 (let ((the-record
547 (` ((filename . (, (bookmark-buffer-file-name)))
548 (front-context-string
549 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
550 (buffer-substring-no-properties
551 (point)
552 (+ (point) bookmark-search-size))
553 nil)))
554 (rear-context-string
555 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
556 (buffer-substring-no-properties
557 (point)
558 (- (point) bookmark-search-size))
559 nil)))
560 (position . (, (point)))
561 ))))
563 ;; Now fill in the optional parts:
565 ;; Take no chances with text properties
566 (set-text-properties 0 (length annotation) nil annotation)
567 (set-text-properties 0 (length info-node) nil info-node)
569 (if annotation
570 (nconc the-record (list (cons 'annotation annotation))))
571 (if info-node
572 (nconc the-record (list (cons 'info-node info-node))))
574 ;; Finally, return the completed record.
575 the-record))
579 ;;; File format stuff
581 ;; The OLD format of the bookmark-alist was:
583 ;; ((bookmark-name (filename
584 ;; string-in-front
585 ;; string-behind
586 ;; point))
587 ;; ...)
589 ;; The NEW format of the bookmark-alist is:
591 ;; ((bookmark-name ((filename . FILENAME)
592 ;; (front-context-string . string-in-front)
593 ;; (rear-context-string . string-behind)
594 ;; (position . POINT)
595 ;; (annotation . annotation)
596 ;; (whatever . VALUE)
597 ;; ...
598 ;; ))
599 ;; ...)
602 ;; I switched to using an internal as well as external alist because I
603 ;; felt that would be a more flexible framework in which to add
604 ;; features. It means that the order in which values appear doesn't
605 ;; matter, and it means that arbitrary values can be added without
606 ;; risk of interfering with existing ones.
608 ;; BOOKMARK-NAME is the string the user gives the bookmark and
609 ;; accesses it by from then on.
611 ;; FILENAME is the location of the file in which the bookmark is set.
613 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
614 ;; context in front of the point at which the bookmark is set.
616 ;; STRING-BEHIND is the same thing, but after the point.
618 ;; The context strings exist so that modifications to a file don't
619 ;; necessarily cause a bookmark's position to be invalidated.
620 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
621 ;; case the file has changed since the bookmark was set. It will
622 ;; attempt to place the user before the changes, if there were any.
623 ;; ANNOTATION is the annotation for the bookmark; it may not exist
624 ;; (for backward compatibility), be nil (no annotation), or be a
625 ;; string.
628 (defconst bookmark-file-format-version 1
629 "The current version of the format used by bookmark files.
630 You should never need to change this.")
633 (defconst bookmark-end-of-version-stamp-marker
634 "-*- End Of Bookmark File Format Version Stamp -*-\n"
635 "This string marks the end of the version stamp in a bookmark file.")
638 (defun bookmark-alist-from-buffer ()
639 "Return a bookmark-alist (in any format) from the current buffer.
640 The buffer must of course contain bookmark format information.
641 Does not care from where in the buffer it is called, and does not
642 affect point."
643 (save-excursion
644 (goto-char (point-min))
645 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
646 (read (current-buffer))
647 ;; Else we're dealing with format version 0
648 (if (search-forward "(" nil t)
649 (progn
650 (forward-char -1)
651 (read (current-buffer)))
652 ;; Else no hope of getting information here.
653 (error "Not bookmark format")))))
656 (defun bookmark-upgrade-version-0-alist (old-list)
657 "Upgrade a version 0 alist OLD-LIST to the current version."
658 (mapcar
659 (lambda (bookmark)
660 (let* ((name (car bookmark))
661 (record (car (cdr bookmark)))
662 (filename (nth 0 record))
663 (front-str (nth 1 record))
664 (rear-str (nth 2 record))
665 (position (nth 3 record))
666 (ann (nth 4 record)))
667 (list
668 name
669 (` ((filename . (, filename))
670 (front-context-string . (, (or front-str "")))
671 (rear-context-string . (, (or rear-str "")))
672 (position . (, position))
673 (annotation . (, ann)))))))
674 old-list))
677 (defun bookmark-upgrade-file-format-from-0 ()
678 "Upgrade a bookmark file of format 0 (the original format) to format 1.
679 This expects to be called from point-min in a bookmark file."
680 (message "Upgrading bookmark format from 0 to %d..."
681 bookmark-file-format-version)
682 (let* ((old-list (bookmark-alist-from-buffer))
683 (new-list (bookmark-upgrade-version-0-alist old-list)))
684 (delete-region (point-min) (point-max))
685 (bookmark-insert-file-format-version-stamp)
686 (pp new-list (current-buffer))
687 (save-buffer))
688 (goto-char (point-min))
689 (message "Upgrading bookmark format from 0 to %d...done"
690 bookmark-file-format-version)
694 (defun bookmark-grok-file-format-version ()
695 "Return an integer which is the file-format version of this bookmark file.
696 This expects to be called from point-min in a bookmark file."
697 (if (looking-at "^;;;;")
698 (save-excursion
699 (save-match-data
700 (re-search-forward "[0-9]")
701 (forward-char -1)
702 (read (current-buffer))))
703 ;; Else this is format version 0, the original one, which didn't
704 ;; even have version stamps.
708 (defun bookmark-maybe-upgrade-file-format ()
709 "Check the file-format version of this bookmark file.
710 If the version is not up-to-date, upgrade it automatically.
711 This expects to be called from point-min in a bookmark file."
712 (let ((version (bookmark-grok-file-format-version)))
713 (cond
714 ((= version bookmark-file-format-version)
715 ) ; home free -- version is current
716 ((= version 0)
717 (bookmark-upgrade-file-format-from-0))
719 (error "Bookmark file format version strangeness")))))
722 (defun bookmark-insert-file-format-version-stamp ()
723 "Insert text indicating current version of bookmark file format."
724 (insert
725 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
726 bookmark-file-format-version))
727 (insert ";;; This format is meant to be slightly human-readable;\n"
728 ";;; nevertheless, you probably don't want to edit it.\n"
729 ";;; "
730 bookmark-end-of-version-stamp-marker))
733 ;;; end file-format stuff
736 ;;; Core code:
738 ;;;###autoload
739 (defun bookmark-set (&optional name parg)
740 "Set a bookmark named NAME inside a file.
741 If name is nil, then the user will be prompted.
742 With prefix arg, will not overwrite a bookmark that has the same name
743 as NAME if such a bookmark already exists, but instead will \"push\"
744 the new bookmark onto the bookmark alist. Thus the most recently set
745 bookmark with name NAME would be the one in effect at any given time,
746 but the others are still there, should you decide to delete the most
747 recent one.
749 To yank words from the text of the buffer and use them as part of the
750 bookmark name, type C-w while setting a bookmark. Successive C-w's
751 yank successive words.
753 Typing C-u inserts the name of the last bookmark used in the buffer
754 \(as an aid in using a single bookmark name to track your progress
755 through a large file\). If no bookmark was used, then C-u inserts the
756 name of the file being visited.
758 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
759 and it removes only the first instance of a bookmark with that name from
760 the list of bookmarks.\)"
761 (interactive (list nil current-prefix-arg))
763 (bookmark-buffer-file-name)
764 (error "Buffer not visiting a file or directory"))
766 (bookmark-maybe-load-default-file)
768 (setq bookmark-current-point (point))
769 (setq bookmark-yank-point (point))
770 (setq bookmark-current-buffer (current-buffer))
772 (let* ((default (or bookmark-current-bookmark
773 (bookmark-buffer-name)))
774 (str
775 (or name
776 (read-from-minibuffer
777 (format "Set bookmark (%s): " default)
779 (let ((now-map (copy-keymap minibuffer-local-map)))
780 (define-key now-map "\C-w" 'bookmark-yank-word)
781 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
782 now-map))))
783 (annotation nil))
784 (and (string-equal str "") (setq str default))
785 ;; Ask for an annotation buffer for this bookmark
786 (if bookmark-use-annotations
787 (bookmark-read-annotation parg str)
788 (bookmark-make str annotation parg (bookmark-info-current-node))
789 (setq bookmark-current-bookmark str)
790 (bookmark-bmenu-surreptitiously-rebuild-list)
791 (goto-char bookmark-current-point))))
794 (defun bookmark-info-current-node ()
795 "If in Info-mode, return current node name (a string), else nil."
796 (if (eq major-mode 'Info-mode)
797 Info-current-node))
800 (defun bookmark-kill-line (&optional newline-too)
801 "Kill from point to end of line.
802 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
803 Does not affect the kill-ring."
804 (let ((eol (save-excursion (end-of-line) (point))))
805 (delete-region (point) eol)
806 (if (and newline-too (looking-at "\n"))
807 (delete-char 1))))
810 ;; Defvars to avoid compilation warnings:
811 (defvar bookmark-annotation-paragraph nil)
812 (defvar bookmark-annotation-name nil)
813 (defvar bookmark-annotation-buffer nil)
814 (defvar bookmark-annotation-file nil)
815 (defvar bookmark-annotation-point nil)
818 (defun bookmark-send-annotation ()
819 "Use buffer contents as the annotation for a bookmark.
820 Exclude lines that begin with `#'.
821 Store the annotation text in the bookmark list with
822 the bookmark (and file, and point) specified in buffer local variables."
823 (interactive)
824 (if (not (eq major-mode 'bookmark-read-annotation-mode))
825 (error "Not in bookmark-read-annotation-mode"))
826 (goto-char (point-min))
827 (while (< (point) (point-max))
828 (if (looking-at "^#")
829 (bookmark-kill-line t)
830 (forward-line 1)))
831 (let ((annotation (buffer-string))
832 (parg bookmark-annotation-paragraph)
833 (bookmark bookmark-annotation-name)
834 (pt bookmark-annotation-point)
835 (buf bookmark-annotation-buffer))
836 ;; for bookmark-make-cell to work, we need to be
837 ;; in the relevant buffer, at the relevant point.
838 ;; Actually, bookmark-make-cell should probably be re-written,
839 ;; to avoid this need. Should I handle the error if a buffer is
840 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
841 (save-excursion
842 (pop-to-buffer buf)
843 (goto-char pt)
844 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
845 (setq bookmark-current-bookmark bookmark))
846 (bookmark-bmenu-surreptitiously-rebuild-list)
847 (goto-char bookmark-current-point))
848 (kill-buffer (current-buffer)))
851 (defun bookmark-default-annotation-text (bookmark)
852 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
853 "# All lines which start with a '#' will be deleted.\n"
854 "# Type C-c C-c when done.\n#\n"
855 "# Author: " (user-full-name) " <" (user-login-name) "@"
856 (system-name) ">\n"
857 "# Date: " (current-time-string) "\n"))
860 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
861 "Function to return default text to use for a bookmark annotation.
862 It takes the name of the bookmark, as a string, as an arg.")
864 (defun bookmark-read-annotation-mode (buf point parg bookmark)
865 "Mode for composing annotations for a bookmark.
866 Wants BUF POINT PARG and BOOKMARK.
867 When you have finished composing, type \\[bookmark-send-annotation] to send
868 the annotation.
870 \\{bookmark-read-annotation-mode-map}
872 (interactive)
873 (kill-all-local-variables)
874 (make-local-variable 'bookmark-annotation-paragraph)
875 (make-local-variable 'bookmark-annotation-name)
876 (make-local-variable 'bookmark-annotation-buffer)
877 (make-local-variable 'bookmark-annotation-file)
878 (make-local-variable 'bookmark-annotation-point)
879 (setq bookmark-annotation-paragraph parg)
880 (setq bookmark-annotation-name bookmark)
881 (setq bookmark-annotation-buffer buf)
882 (setq bookmark-annotation-file (buffer-file-name buf))
883 (setq bookmark-annotation-point point)
884 (use-local-map bookmark-read-annotation-mode-map)
885 (setq major-mode 'bookmark-read-annotation-mode)
886 (insert (funcall bookmark-read-annotation-text-func bookmark))
887 (run-hooks 'text-mode-hook))
890 (defun bookmark-read-annotation (parg bookmark)
891 "Pop up a buffer for entering a bookmark annotation.
892 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
893 (let ((buf (current-buffer))
894 (point (point)))
895 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
896 (bookmark-read-annotation-mode buf point parg bookmark)))
899 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
900 "Keymap for editing an annotation of a bookmark.")
903 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
904 'bookmark-send-edited-annotation)
907 (defun bookmark-edit-annotation-mode (bookmark)
908 "Mode for editing the annotation of bookmark BOOKMARK.
909 When you have finished composing, type \\[bookmark-send-annotation].
911 \\{bookmark-edit-annotation-mode-map}
913 (interactive)
914 (kill-all-local-variables)
915 (make-local-variable 'bookmark-annotation-name)
916 (setq bookmark-annotation-name bookmark)
917 (use-local-map bookmark-edit-annotation-mode-map)
918 (setq major-mode 'bookmark-edit-annotation-mode)
919 (insert (funcall bookmark-read-annotation-text-func bookmark))
920 (let ((annotation (bookmark-get-annotation bookmark)))
921 (if (and annotation (not (string-equal annotation "")))
922 (insert annotation)))
923 (run-hooks 'text-mode-hook))
926 (defun bookmark-send-edited-annotation ()
927 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
928 (interactive)
929 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
930 (error "Not in bookmark-edit-annotation-mode"))
931 (goto-char (point-min))
932 (while (< (point) (point-max))
933 (if (looking-at "^#")
934 (bookmark-kill-line t)
935 (forward-line 1)))
936 (let ((annotation (buffer-string))
937 (bookmark bookmark-annotation-name))
938 (bookmark-set-annotation bookmark annotation)
939 (bookmark-bmenu-surreptitiously-rebuild-list)
940 (goto-char bookmark-current-point))
941 (kill-buffer (current-buffer)))
944 (defun bookmark-edit-annotation (bookmark)
945 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
946 (let ((buf (current-buffer))
947 (point (point)))
948 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
949 (bookmark-edit-annotation-mode bookmark)))
952 (defun bookmark-insert-current-bookmark ()
953 "Insert this buffer's value of bookmark-current-bookmark.
954 Default to file name if it's nil."
955 (interactive)
956 (let ((str
957 (save-excursion
958 (set-buffer bookmark-current-buffer)
959 bookmark-current-bookmark)))
960 (if str (insert str) (bookmark-insert-buffer-name))))
963 (defun bookmark-insert-buffer-name ()
964 "Insert the current file name into the bookmark name being set.
965 The directory part of the file name is not used."
966 (interactive)
967 (let ((str
968 (save-excursion
969 (set-buffer bookmark-current-buffer)
970 (bookmark-buffer-name))))
971 (insert str)))
974 (defun bookmark-buffer-name ()
975 "Return the name of the current buffer's file, non-directory.
976 In Info, return the current node."
977 (cond
978 ;; Are we in Info?
979 ((string-equal mode-name "Info") Info-current-node)
980 ;; Or are we a file?
981 (buffer-file-name (file-name-nondirectory buffer-file-name))
982 ;; Or are we a directory?
983 ((and (boundp 'dired-directory) dired-directory)
984 (let* ((dirname (if (stringp dired-directory)
985 dired-directory
986 (car dired-directory)))
987 (idx (1- (length dirname))))
988 ;; Strip the trailing slash.
989 (if (= ?/ (aref dirname idx))
990 (file-name-nondirectory (substring dirname 0 idx))
991 ;; Else return the current-buffer
992 (buffer-name (current-buffer)))))
993 ;; If all else fails, use the buffer's name.
995 (buffer-name (current-buffer)))))
998 (defun bookmark-yank-word ()
999 (interactive)
1000 ;; get the next word from the buffer and append it to the name of
1001 ;; the bookmark currently being set.
1002 (let ((string (save-excursion
1003 (set-buffer bookmark-current-buffer)
1004 (goto-char bookmark-yank-point)
1005 (buffer-substring-no-properties
1006 (point)
1007 (progn
1008 (forward-word 1)
1009 (setq bookmark-yank-point (point)))))))
1010 (insert string)))
1013 (defun bookmark-buffer-file-name ()
1014 "Return the current buffer's file in a way useful for bookmarks.
1015 For example, if this is a Info buffer, return the Info file's name."
1016 (if (eq major-mode 'Info-mode)
1017 Info-current-file
1019 buffer-file-name
1020 (if (and (boundp 'dired-directory) dired-directory)
1021 (if (stringp dired-directory)
1022 dired-directory
1023 (car dired-directory))))))
1026 (defun bookmark-maybe-load-default-file ()
1027 (and (not bookmarks-already-loaded)
1028 (null bookmark-alist)
1029 (prog2
1030 (and
1031 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1032 ;; to be renamed.
1033 (file-exists-p (expand-file-name bookmark-old-default-file))
1034 (not (file-exists-p (expand-file-name bookmark-default-file)))
1035 (rename-file (expand-file-name bookmark-old-default-file)
1036 (expand-file-name bookmark-default-file)))
1037 ;; return t so the `and' will continue...
1040 (file-readable-p (expand-file-name bookmark-default-file))
1041 (bookmark-load bookmark-default-file t t)
1042 (setq bookmarks-already-loaded t)))
1045 (defun bookmark-maybe-sort-alist ()
1046 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1047 ;;is non-nil, then return a sorted copy of the alist.
1048 (if bookmark-sort-flag
1049 (setq bookmark-alist
1050 (sort (copy-alist bookmark-alist)
1051 (function
1052 (lambda (x y) (string-lessp (car x) (car y))))))))
1055 ;;;###autoload
1056 (defun bookmark-jump (bookmark)
1057 "Jump to bookmark BOOKMARK (a point in some file).
1058 You may have a problem using this function if the value of variable
1059 `bookmark-alist' is nil. If that happens, you need to load in some
1060 bookmarks. See help on function `bookmark-load' for more about
1061 this.
1063 If the file pointed to by BOOKMARK no longer exists, you will be asked
1064 if you wish to give the bookmark a new location, and bookmark-jump
1065 will then jump to the new location, as well as recording it in place
1066 of the old one in the permanent bookmark record."
1067 (interactive
1068 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1069 (bookmark-maybe-historicize-string bookmark)
1070 (let ((cell (bookmark-jump-noselect bookmark)))
1071 (and cell
1072 (switch-to-buffer (car cell))
1073 (goto-char (cdr cell))
1074 (if bookmark-automatically-show-annotations
1075 ;; if there is an annotation for this bookmark,
1076 ;; show it in a buffer.
1077 (bookmark-show-annotation bookmark)))))
1080 (defun bookmark-file-or-variation-thereof (file)
1081 "Return FILE (a string) if it exists in any reasonable variation, else nil.
1082 Reasonable variations are FILE.gz, FILE.Z, FILE.info, FILE.info.gz, etc."
1083 (cond
1084 ((file-exists-p file) file)
1085 ((file-exists-p (concat file ".Z")) (concat file ".Z"))
1086 ((file-exists-p (concat file ".gz")) (concat file ".gz"))
1087 ((file-exists-p (concat file ".z")) (concat file ".z"))
1088 ((file-exists-p (concat file ".info")) (concat file ".info"))
1089 ((file-exists-p (concat file ".info.gz")) (concat file ".info.gz"))
1090 ((file-exists-p (concat file ".info.Z")) (concat file ".info.Z"))
1091 ((file-exists-p (concat file ".info.z")) (concat file ".info.z"))
1092 ((vc-backend file) file) ; maybe VC has it?
1093 (t nil)))
1096 (defun bookmark-jump-noselect (str)
1097 ;; a leetle helper for bookmark-jump :-)
1098 ;; returns (BUFFER . POINT)
1099 (bookmark-maybe-load-default-file)
1100 (let* ((file (expand-file-name (bookmark-get-filename str)))
1101 (forward-str (bookmark-get-front-context-string str))
1102 (behind-str (bookmark-get-rear-context-string str))
1103 (place (bookmark-get-position str))
1104 (info-node (bookmark-get-info-node str))
1105 (orig-file file)
1107 (if (setq file (bookmark-file-or-variation-thereof file))
1108 (save-excursion
1109 (save-window-excursion
1110 (if info-node
1111 ;; Info nodes must be visited with care.
1112 (progn
1113 (require 'info)
1114 (Info-find-node file info-node))
1115 ;; Else no Info. Can do an ordinary find-file:
1116 (set-buffer (find-file-noselect file))
1117 (goto-char place))
1119 ;; Go searching forward first. Then, if forward-str exists and
1120 ;; was found in the file, we can search backward for behind-str.
1121 ;; Rationale is that if text was inserted between the two in the
1122 ;; file, it's better to be put before it so you can read it,
1123 ;; rather than after and remain perhaps unaware of the changes.
1124 (if forward-str
1125 (if (search-forward forward-str (point-max) t)
1126 (goto-char (match-beginning 0))))
1127 (if behind-str
1128 (if (search-backward behind-str (point-min) t)
1129 (goto-char (match-end 0))))
1130 ;; added by db
1131 (setq bookmark-current-bookmark str)
1132 (cons (current-buffer) (point))))
1134 ;; Else unable to find the marked file, so ask if user wants to
1135 ;; relocate the bookmark, else remind them to consider deletion.
1136 (ding)
1137 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1138 " nonexistent. Relocate \""
1140 "\"? "))
1141 (progn
1142 (bookmark-relocate str)
1143 ;; gasp! It's a recursive function call in Emacs Lisp!
1144 (bookmark-jump-noselect str))
1145 (message
1146 "Bookmark not relocated; consider removing it \(%s\)." str)
1147 nil))))
1150 ;;;###autoload
1151 (defun bookmark-relocate (bookmark)
1152 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1153 This makes an already existing bookmark point to that file, instead of
1154 the one it used to point at. Useful when a file has been renamed
1155 after a bookmark was set in it."
1156 (interactive (bookmark-completing-read "Bookmark to relocate"))
1157 (bookmark-maybe-historicize-string bookmark)
1158 (bookmark-maybe-load-default-file)
1159 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1160 (newloc (expand-file-name
1161 (read-file-name
1162 (format "Relocate %s to: " bookmark)
1163 (file-name-directory bmrk-filename)))))
1164 (bookmark-set-filename bookmark newloc)
1165 (bookmark-bmenu-surreptitiously-rebuild-list)))
1168 ;;;###autoload
1169 (defun bookmark-insert-location (bookmark &optional no-history)
1170 "Insert the name of the file associated with BOOKMARK.
1171 Optional second arg NO-HISTORY means don't record this in the
1172 minibuffer history list `bookmark-history'."
1173 (interactive (bookmark-completing-read "Insert bookmark location"))
1174 (or no-history (bookmark-maybe-historicize-string bookmark))
1175 (let ((start (point)))
1176 (prog1
1177 (insert (bookmark-location bookmark)) ; *Return this line*
1178 (if (and (display-color-p) (display-mouse-p))
1179 (put-text-property start
1180 (save-excursion (re-search-backward
1181 "[^ \t]")
1182 (1+ (point)))
1183 'mouse-face 'highlight)))))
1185 ;;;###autoload
1186 (defalias 'bookmark-locate 'bookmark-insert-location)
1188 (defun bookmark-location (bookmark)
1189 "Return the name of the file associated with BOOKMARK."
1190 (bookmark-maybe-load-default-file)
1191 (bookmark-get-filename bookmark))
1194 ;;;###autoload
1195 (defun bookmark-rename (old &optional new)
1196 "Change the name of OLD bookmark to NEW name.
1197 If called from keyboard, prompt for OLD and NEW. If called from
1198 menubar, select OLD from a menu and prompt for NEW.
1200 If called from Lisp, prompt for NEW if only OLD was passed as an
1201 argument. If called with two strings, then no prompting is done. You
1202 must pass at least OLD when calling from Lisp.
1204 While you are entering the new name, consecutive C-w's insert
1205 consecutive words from the text of the buffer into the new bookmark
1206 name."
1207 (interactive (bookmark-completing-read "Old bookmark name"))
1208 (bookmark-maybe-historicize-string old)
1209 (bookmark-maybe-load-default-file)
1211 (setq bookmark-current-point (point))
1212 (setq bookmark-yank-point (point))
1213 (setq bookmark-current-buffer (current-buffer))
1214 (let ((newname
1215 (or new ; use second arg, if non-nil
1216 (read-from-minibuffer
1217 "New name: "
1219 (let ((now-map (copy-keymap minibuffer-local-map)))
1220 (define-key now-map "\C-w" 'bookmark-yank-word)
1221 now-map)
1223 'bookmark-history))))
1224 (bookmark-set-name old newname)
1225 (setq bookmark-current-bookmark newname)
1226 (bookmark-bmenu-surreptitiously-rebuild-list)
1227 (setq bookmark-alist-modification-count
1228 (1+ bookmark-alist-modification-count))
1229 (if (bookmark-time-to-save-p)
1230 (bookmark-save))))
1233 ;;;###autoload
1234 (defun bookmark-insert (bookmark)
1235 "Insert the text of the file pointed to by bookmark BOOKMARK.
1236 You may have a problem using this function if the value of variable
1237 `bookmark-alist' is nil. If that happens, you need to load in some
1238 bookmarks. See help on function `bookmark-load' for more about
1239 this."
1240 (interactive (bookmark-completing-read "Insert bookmark contents"))
1241 (bookmark-maybe-historicize-string bookmark)
1242 (bookmark-maybe-load-default-file)
1243 (let ((orig-point (point))
1244 (str-to-insert
1245 (save-excursion
1246 (set-buffer (car (bookmark-jump-noselect bookmark)))
1247 (buffer-string))))
1248 (insert str-to-insert)
1249 (push-mark)
1250 (goto-char orig-point)))
1253 ;;;###autoload
1254 (defun bookmark-delete (bookmark &optional batch)
1255 "Delete BOOKMARK from the bookmark list.
1256 Removes only the first instance of a bookmark with that name. If
1257 there are one or more other bookmarks with the same name, they will
1258 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1259 one most recently used in this file, if any\).
1260 Optional second arg BATCH means don't update the bookmark list buffer,
1261 probably because we were called from there."
1262 (interactive
1263 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1264 (bookmark-maybe-historicize-string bookmark)
1265 (bookmark-maybe-load-default-file)
1266 (let ((will-go (bookmark-get-bookmark bookmark)))
1267 (setq bookmark-alist (delq will-go bookmark-alist))
1268 ;; Added by db, nil bookmark-current-bookmark if the last
1269 ;; occurrence has been deleted
1270 (or (bookmark-get-bookmark bookmark-current-bookmark)
1271 (setq bookmark-current-bookmark nil)))
1272 ;; Don't rebuild the list
1273 (if batch
1275 (bookmark-bmenu-surreptitiously-rebuild-list)
1276 (setq bookmark-alist-modification-count
1277 (1+ bookmark-alist-modification-count))
1278 (if (bookmark-time-to-save-p)
1279 (bookmark-save))))
1282 (defun bookmark-time-to-save-p (&optional last-time)
1283 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1284 ;; finds out whether it's time to save bookmarks to a file, by
1285 ;; examining the value of variable bookmark-save-flag, and maybe
1286 ;; bookmark-alist-modification-count. Returns t if they should be
1287 ;; saved, nil otherwise. if last-time is non-nil, then this is
1288 ;; being called when emacs is killed.
1289 (cond (last-time
1290 (and (> bookmark-alist-modification-count 0)
1291 bookmark-save-flag))
1292 ((numberp bookmark-save-flag)
1293 (>= bookmark-alist-modification-count bookmark-save-flag))
1295 nil)))
1298 ;;;###autoload
1299 (defun bookmark-write ()
1300 "Write bookmarks to a file (reading the file name with the minibuffer).
1301 Don't use this in Lisp programs; use `bookmark-save' instead."
1302 (interactive)
1303 (bookmark-maybe-load-default-file)
1304 (bookmark-save t))
1307 ;;;###autoload
1308 (defun bookmark-save (&optional parg file)
1309 "Save currently defined bookmarks.
1310 Saves by default in the file defined by the variable
1311 `bookmark-default-file'. With a prefix arg, save it in file FILE
1312 \(second argument\).
1314 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1315 and FILE, and if you just want it to write to the default file, then
1316 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1317 instead. If you pass in one argument, and it is non-nil, then the
1318 user will be interactively queried for a file to save in.
1320 When you want to load in the bookmarks from a file, use
1321 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1322 for a file, defaulting to the file defined by variable
1323 `bookmark-default-file'."
1324 (interactive "P")
1325 (bookmark-maybe-load-default-file)
1326 (cond
1327 ((and (null parg) (null file))
1328 ;;whether interactive or not, write to default file
1329 (bookmark-write-file bookmark-default-file))
1330 ((and (null parg) file)
1331 ;;whether interactive or not, write to given file
1332 (bookmark-write-file file))
1333 ((and parg (not file))
1334 ;;have been called interactively w/ prefix arg
1335 (let ((file (read-file-name "File to save bookmarks in: ")))
1336 (bookmark-write-file file)))
1337 (t ; someone called us with prefix-arg *and* a file, so just write to file
1338 (bookmark-write-file file)))
1339 ;; signal that we have synced the bookmark file by setting this to
1340 ;; 0. If there was an error at any point before, it will not get
1341 ;; set, which is what we want.
1342 (setq bookmark-alist-modification-count 0))
1346 (defun bookmark-write-file (file)
1347 (save-excursion
1348 (save-window-excursion
1349 (if (>= baud-rate 9600)
1350 (message "Saving bookmarks to file %s..." file))
1351 (set-buffer (let ((enable-local-variables nil))
1352 (find-file-noselect file)))
1353 (goto-char (point-min))
1354 (delete-region (point-min) (point-max))
1355 (bookmark-insert-file-format-version-stamp)
1356 (pp bookmark-alist (current-buffer))
1357 (let ((version-control
1358 (cond
1359 ((null bookmark-version-control) nil)
1360 ((eq 'never bookmark-version-control) 'never)
1361 ((eq 'nospecial bookmark-version-control) version-control)
1363 t))))
1364 (write-file file)
1365 (kill-buffer (current-buffer))
1366 (if (>= baud-rate 9600)
1367 (message "Saving bookmarks to file %s...done" file))
1368 ))))
1371 (defun bookmark-import-new-list (new-list)
1372 ;; Walk over the new list, adding each individual bookmark
1373 ;; carefully. "Carefully" means checking against the existing
1374 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1375 ;; as necessary.
1376 (let ((lst new-list)
1377 (names (bookmark-all-names)))
1378 (while lst
1379 (let* ((full-record (car lst)))
1380 (bookmark-maybe-rename full-record names)
1381 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1382 (setq names (cons (bookmark-name-from-full-record full-record) names))
1383 (setq lst (cdr lst))))))
1386 (defun bookmark-maybe-rename (full-record names)
1387 ;; just a helper for bookmark-import-new-list; it is only for
1388 ;; readability that this is not inlined.
1390 ;; Once this has found a free name, it sets full-record to that
1391 ;; name.
1392 (let ((found-name (bookmark-name-from-full-record full-record)))
1393 (if (member found-name names)
1394 ;; We've got a conflict, so generate a new name
1395 (let ((count 2)
1396 (new-name found-name))
1397 (while (member new-name names)
1398 (setq new-name (concat found-name (format "<%d>" count)))
1399 (setq count (1+ count)))
1400 (bookmark-set-name full-record new-name)))))
1403 ;;;###autoload
1404 (defun bookmark-load (file &optional overwrite no-msg)
1405 "Load bookmarks from FILE (which must be in bookmark format).
1406 Appends loaded bookmarks to the front of the list of bookmarks. If
1407 optional second argument OVERWRITE is non-nil, existing bookmarks are
1408 destroyed. Optional third arg NO-MSG means don't display any messages
1409 while loading.
1411 If you load a file that doesn't contain a proper bookmark alist, you
1412 will corrupt Emacs's bookmark list. Generally, you should only load
1413 in files that were created with the bookmark functions in the first
1414 place. Your own personal bookmark file, `~/.emacs.bmk', is
1415 maintained automatically by Emacs; you shouldn't need to load it
1416 explicitly.
1418 If you load a file containing bookmarks with the same names as
1419 bookmarks already present in your Emacs, the new bookmarks will get
1420 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1421 method buffers use to resolve name collisions."
1422 (interactive
1423 (list (read-file-name
1424 (format "Load bookmarks from: (%s) "
1425 bookmark-default-file)
1426 ;;Default might not be used often,
1427 ;;but there's no better default, and
1428 ;;I guess it's better than none at all.
1429 "~/" bookmark-default-file 'confirm)))
1430 (setq file (expand-file-name file))
1431 (if (file-readable-p file)
1432 (save-excursion
1433 (save-window-excursion
1434 (if (and (null no-msg) (>= baud-rate 9600))
1435 (message "Loading bookmarks from %s..." file))
1436 (set-buffer (let ((enable-local-variables nil))
1437 (find-file-noselect file)))
1438 (goto-char (point-min))
1439 (bookmark-maybe-upgrade-file-format)
1440 (let ((blist (bookmark-alist-from-buffer)))
1441 (if (listp blist)
1442 (progn
1443 (if overwrite
1444 (progn
1445 (setq bookmark-alist blist)
1446 (setq bookmark-alist-modification-count 0))
1447 ;; else
1448 (bookmark-import-new-list blist)
1449 (setq bookmark-alist-modification-count
1450 (1+ bookmark-alist-modification-count)))
1451 (if (string-equal
1452 (expand-file-name bookmark-default-file)
1453 file)
1454 (setq bookmarks-already-loaded t))
1455 (bookmark-bmenu-surreptitiously-rebuild-list))
1456 (error "Invalid bookmark list in %s" file)))
1457 (kill-buffer (current-buffer)))
1458 (if (and (null no-msg) (>= baud-rate 9600))
1459 (message "Loading bookmarks from %s...done" file)))
1460 (error "Cannot read bookmark file %s" file)))
1464 ;;; Code supporting the dired-like bookmark menu. Prefix is
1465 ;;; "bookmark-bmenu" for "buffer-menu":
1468 (defvar bookmark-bmenu-bookmark-column nil)
1471 (defvar bookmark-bmenu-hidden-bookmarks ())
1474 (defvar bookmark-bmenu-mode-map nil)
1477 (if bookmark-bmenu-mode-map
1479 (setq bookmark-bmenu-mode-map (make-keymap))
1480 (suppress-keymap bookmark-bmenu-mode-map t)
1481 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1482 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1483 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1484 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1485 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1486 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1487 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1488 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1489 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1490 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1491 (define-key bookmark-bmenu-mode-map "\C-o"
1492 'bookmark-bmenu-switch-other-window)
1493 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1494 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1495 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1496 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1497 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1498 (define-key bookmark-bmenu-mode-map " " 'next-line)
1499 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1500 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1501 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1502 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1503 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1504 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1505 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1506 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1507 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1508 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1509 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1510 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1511 (define-key bookmark-bmenu-mode-map [mouse-2]
1512 'bookmark-bmenu-other-window-with-mouse))
1516 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1517 ;; data.
1518 (put 'bookmark-bmenu-mode 'mode-class 'special)
1521 ;; todo: need to display whether or not bookmark exists as a buffer in
1522 ;; flag column.
1524 ;; Format:
1525 ;; FLAGS BOOKMARK [ LOCATION ]
1528 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1529 "Rebuild the Bookmark List if it exists.
1530 Don't affect the buffer ring order."
1531 (if (get-buffer "*Bookmark List*")
1532 (save-excursion
1533 (save-window-excursion
1534 (bookmark-bmenu-list)))))
1537 ;;;###autoload
1538 (defun bookmark-bmenu-list ()
1539 "Display a list of existing bookmarks.
1540 The list is displayed in a buffer named `*Bookmark List*'.
1541 The leftmost column displays a D if the bookmark is flagged for
1542 deletion, or > if it is flagged for displaying."
1543 (interactive)
1544 (bookmark-maybe-load-default-file)
1545 (if (interactive-p)
1546 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1547 (set-buffer (get-buffer-create "*Bookmark List*")))
1548 (let ((buffer-read-only nil))
1549 (delete-region (point-max) (point-min))
1550 (goto-char (point-min)) ;sure are playing it safe...
1551 (insert "% Bookmark\n- --------\n")
1552 (bookmark-maybe-sort-alist)
1553 (mapcar
1554 (lambda (full-record)
1555 ;; if a bookmark has an annotation, prepend a "*"
1556 ;; in the list of bookmarks.
1557 (let ((annotation (bookmark-get-annotation
1558 (bookmark-name-from-full-record full-record))))
1559 (if (and annotation (not (string-equal annotation "")))
1560 (insert " *")
1561 (insert " "))
1562 (let ((start (point)))
1563 (insert (bookmark-name-from-full-record full-record))
1564 (if (and (display-color-p) (display-mouse-p))
1565 (put-text-property start
1566 (save-excursion (re-search-backward
1567 "[^ \t]")
1568 (1+ (point)))
1569 'mouse-face 'highlight))
1570 (insert "\n")
1572 bookmark-alist))
1573 (goto-char (point-min))
1574 (forward-line 2)
1575 (bookmark-bmenu-mode)
1576 (if bookmark-bmenu-toggle-filenames
1577 (bookmark-bmenu-toggle-filenames t)))
1579 ;;;###autoload
1580 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1581 ;;;###autoload
1582 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1586 (defun bookmark-bmenu-mode ()
1587 "Major mode for editing a list of bookmarks.
1588 Each line describes one of the bookmarks in Emacs.
1589 Letters do not insert themselves; instead, they are commands.
1590 Bookmark names preceded by a \"*\" have annotations.
1591 \\<bookmark-bmenu-mode-map>
1592 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1593 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1594 Also show bookmarks marked using m in other windows.
1595 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1596 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1597 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1598 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1599 together with bookmark selected before this one in another window.
1600 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1601 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1602 so the bookmark menu bookmark remains visible in its window.
1603 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1604 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1605 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1606 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1607 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1608 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1609 With a prefix arg, prompts for a file to save in.
1610 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1611 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1612 With prefix argument, also move up one line.
1613 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1614 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1615 in another buffer.
1616 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1617 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1618 (kill-all-local-variables)
1619 (use-local-map bookmark-bmenu-mode-map)
1620 (setq truncate-lines t)
1621 (setq buffer-read-only t)
1622 (setq major-mode 'bookmark-bmenu-mode)
1623 (setq mode-name "Bookmark Menu")
1624 (run-hooks 'bookmark-bmenu-mode-hook))
1627 (defun bookmark-bmenu-toggle-filenames (&optional show)
1628 "Toggle whether filenames are shown in the bookmark list.
1629 Optional argument SHOW means show them unconditionally."
1630 (interactive)
1631 (cond
1632 (show
1633 (setq bookmark-bmenu-toggle-filenames nil)
1634 (bookmark-bmenu-show-filenames)
1635 (setq bookmark-bmenu-toggle-filenames t))
1636 (bookmark-bmenu-toggle-filenames
1637 (bookmark-bmenu-hide-filenames)
1638 (setq bookmark-bmenu-toggle-filenames nil))
1640 (bookmark-bmenu-show-filenames)
1641 (setq bookmark-bmenu-toggle-filenames t))))
1644 (defun bookmark-bmenu-show-filenames (&optional force)
1645 (if (and (not force) bookmark-bmenu-toggle-filenames)
1646 nil ;already shown, so do nothing
1647 (save-excursion
1648 (save-window-excursion
1649 (goto-char (point-min))
1650 (forward-line 2)
1651 (setq bookmark-bmenu-hidden-bookmarks ())
1652 (let ((buffer-read-only nil))
1653 (while (< (point) (point-max))
1654 (let ((bmrk (bookmark-bmenu-bookmark)))
1655 (setq bookmark-bmenu-hidden-bookmarks
1656 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1657 (let ((start (save-excursion (end-of-line) (point))))
1658 (move-to-column bookmark-bmenu-file-column t)
1659 ;; Strip off `mouse-face' from the white spaces region.
1660 (if (and (display-color-p) (display-mouse-p))
1661 (remove-text-properties start (point)
1662 '(mouse-face))))
1663 (delete-region (point) (progn (end-of-line) (point)))
1664 (insert " ")
1665 ;; Pass the NO-HISTORY arg:
1666 (bookmark-insert-location bmrk t)
1667 (forward-line 1))))))))
1670 (defun bookmark-bmenu-hide-filenames (&optional force)
1671 (if (and (not force) bookmark-bmenu-toggle-filenames)
1672 ;; nothing to hide if above is nil
1673 (save-excursion
1674 (save-window-excursion
1675 (goto-char (point-min))
1676 (forward-line 2)
1677 (setq bookmark-bmenu-hidden-bookmarks
1678 (nreverse bookmark-bmenu-hidden-bookmarks))
1679 (save-excursion
1680 (goto-char (point-min))
1681 (search-forward "Bookmark")
1682 (backward-word 1)
1683 (setq bookmark-bmenu-bookmark-column (current-column)))
1684 (save-excursion
1685 (let ((buffer-read-only nil))
1686 (while bookmark-bmenu-hidden-bookmarks
1687 (move-to-column bookmark-bmenu-bookmark-column t)
1688 (bookmark-kill-line)
1689 (let ((start (point)))
1690 (insert (car bookmark-bmenu-hidden-bookmarks))
1691 (if (and (display-color-p) (display-mouse-p))
1692 (put-text-property start
1693 (save-excursion (re-search-backward
1694 "[^ \t]")
1695 (1+ (point)))
1696 'mouse-face 'highlight)))
1697 (setq bookmark-bmenu-hidden-bookmarks
1698 (cdr bookmark-bmenu-hidden-bookmarks))
1699 (forward-line 1))))))))
1702 (defun bookmark-bmenu-check-position ()
1703 ;; Returns non-nil if on a line with a bookmark.
1704 ;; (The actual value returned is bookmark-alist).
1705 ;; Else reposition and try again, else return nil.
1706 (cond ((< (count-lines (point-min) (point)) 2)
1707 (goto-char (point-min))
1708 (forward-line 2)
1709 bookmark-alist)
1710 ((and (bolp) (eobp))
1711 (beginning-of-line 0)
1712 bookmark-alist)
1714 bookmark-alist)))
1717 (defun bookmark-bmenu-bookmark ()
1718 ;; return a string which is bookmark of this line.
1719 (if (bookmark-bmenu-check-position)
1720 (save-excursion
1721 (save-window-excursion
1722 (goto-char (point-min))
1723 (search-forward "Bookmark")
1724 (backward-word 1)
1725 (setq bookmark-bmenu-bookmark-column (current-column)))))
1726 (if bookmark-bmenu-toggle-filenames
1727 (bookmark-bmenu-hide-filenames))
1728 (save-excursion
1729 (save-window-excursion
1730 (beginning-of-line)
1731 (forward-char bookmark-bmenu-bookmark-column)
1732 (prog1
1733 (buffer-substring-no-properties (point)
1734 (progn
1735 (end-of-line)
1736 (point)))
1737 ;; well, this is certainly crystal-clear:
1738 (if bookmark-bmenu-toggle-filenames
1739 (bookmark-bmenu-toggle-filenames t))))))
1742 (defun bookmark-show-annotation (bookmark)
1743 "Display the annotation for bookmark named BOOKMARK in a buffer,
1744 if an annotation exists."
1745 (let ((annotation (bookmark-get-annotation bookmark)))
1746 (if (and annotation (not (string-equal annotation "")))
1747 (save-excursion
1748 (let ((old-buf (current-buffer)))
1749 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1750 (delete-region (point-min) (point-max))
1751 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1752 (insert annotation)
1753 (goto-char (point-min))
1754 (pop-to-buffer old-buf))))))
1757 (defun bookmark-show-all-annotations ()
1758 "Display the annotations for all bookmarks in a buffer."
1759 (let ((old-buf (current-buffer)))
1760 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1761 (delete-region (point-min) (point-max))
1762 (mapcar
1763 (lambda (full-record)
1764 (let* ((name (bookmark-name-from-full-record full-record))
1765 (ann (bookmark-get-annotation name)))
1766 (insert (concat name ":\n"))
1767 (if (and ann (not (string-equal ann "")))
1768 ;; insert the annotation, indented by 4 spaces.
1769 (progn
1770 (save-excursion (insert ann))
1771 (while (< (point) (point-max))
1772 (beginning-of-line) ; paranoia
1773 (insert " ")
1774 (forward-line)
1775 (end-of-line))))))
1776 bookmark-alist)
1777 (goto-char (point-min))
1778 (pop-to-buffer old-buf)))
1781 (defun bookmark-bmenu-mark ()
1782 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1783 (interactive)
1784 (beginning-of-line)
1785 (if (bookmark-bmenu-check-position)
1786 (let ((buffer-read-only nil))
1787 (delete-char 1)
1788 (insert ?>)
1789 (forward-line 1)
1790 (bookmark-bmenu-check-position))))
1793 (defun bookmark-bmenu-select ()
1794 "Select this line's bookmark; also display bookmarks marked with `>'.
1795 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1796 (interactive)
1797 (if (bookmark-bmenu-check-position)
1798 (let ((bmrk (bookmark-bmenu-bookmark))
1799 (menu (current-buffer))
1800 (others ())
1801 tem)
1802 (goto-char (point-min))
1803 (while (re-search-forward "^>" nil t)
1804 (setq tem (bookmark-bmenu-bookmark))
1805 (let ((buffer-read-only nil))
1806 (delete-char -1)
1807 (insert ?\ ))
1808 (or (string-equal tem bmrk)
1809 (member tem others)
1810 (setq others (cons tem others))))
1811 (setq others (nreverse others)
1812 tem (/ (1- (frame-height)) (1+ (length others))))
1813 (delete-other-windows)
1814 (bookmark-jump bmrk)
1815 (bury-buffer menu)
1816 (if others
1817 (while others
1818 (split-window nil tem)
1819 (other-window 1)
1820 (bookmark-jump (car others))
1821 (setq others (cdr others)))
1822 (other-window 1)))))
1825 (defun bookmark-bmenu-save (parg)
1826 "Save the current list into a bookmark file.
1827 With a prefix arg, prompts for a file to save them in."
1828 (interactive "P")
1829 (save-excursion
1830 (save-window-excursion
1831 (bookmark-save parg))))
1834 (defun bookmark-bmenu-load ()
1835 "Load the bookmark file and rebuild the bookmark menu-buffer."
1836 (interactive)
1837 (if (bookmark-bmenu-check-position)
1838 (save-excursion
1839 (save-window-excursion
1840 ;; This will call `bookmark-bmenu-list'
1841 (call-interactively 'bookmark-load)))))
1844 (defun bookmark-bmenu-1-window ()
1845 "Select this line's bookmark, alone, in full frame."
1846 (interactive)
1847 (if (bookmark-bmenu-check-position)
1848 (progn
1849 (bookmark-jump (bookmark-bmenu-bookmark))
1850 (bury-buffer (other-buffer))
1851 (delete-other-windows))))
1854 (defun bookmark-bmenu-2-window ()
1855 "Select this line's bookmark, with previous buffer in second window."
1856 (interactive)
1857 (if (bookmark-bmenu-check-position)
1858 (let ((bmrk (bookmark-bmenu-bookmark))
1859 (menu (current-buffer))
1860 (pop-up-windows t))
1861 (delete-other-windows)
1862 (switch-to-buffer (other-buffer))
1863 (let* ((pair (bookmark-jump-noselect bmrk))
1864 (buff (car pair))
1865 (pos (cdr pair)))
1866 (pop-to-buffer buff)
1867 (goto-char pos))
1868 (bury-buffer menu))))
1871 (defun bookmark-bmenu-this-window ()
1872 "Select this line's bookmark in this window."
1873 (interactive)
1874 (if (bookmark-bmenu-check-position)
1875 (bookmark-jump (bookmark-bmenu-bookmark))))
1878 (defun bookmark-bmenu-other-window ()
1879 "Select this line's bookmark in other window, leaving bookmark menu visible."
1880 (interactive)
1881 (let ((bookmark (bookmark-bmenu-bookmark)))
1882 (if (bookmark-bmenu-check-position)
1883 (let* ((pair (bookmark-jump-noselect bookmark))
1884 (buff (car pair))
1885 (pos (cdr pair)))
1886 (switch-to-buffer-other-window buff)
1887 (goto-char pos)
1888 (set-window-point (get-buffer-window buff) pos)
1889 (bookmark-show-annotation bookmark)))))
1892 (defun bookmark-bmenu-switch-other-window ()
1893 "Make the other window select this line's bookmark.
1894 The current window remains selected."
1895 (interactive)
1896 (let ((bookmark (bookmark-bmenu-bookmark))
1897 (pop-up-windows t)
1898 same-window-buffer-names
1899 same-window-regexps)
1900 (if (bookmark-bmenu-check-position)
1901 (let* ((pair (bookmark-jump-noselect bookmark))
1902 (buff (car pair))
1903 (pos (cdr pair)))
1904 (display-buffer buff)
1905 (let ((o-buffer (current-buffer)))
1906 ;; save-excursion won't do
1907 (set-buffer buff)
1908 (goto-char pos)
1909 (set-window-point (get-buffer-window buff) pos)
1910 (set-buffer o-buffer))
1911 (bookmark-show-annotation bookmark)))))
1913 (defun bookmark-bmenu-other-window-with-mouse (event)
1914 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1915 (interactive "e")
1916 (save-excursion
1917 (set-buffer (window-buffer (posn-window (event-end event))))
1918 (save-excursion
1919 (goto-char (posn-point (event-end event)))
1920 (bookmark-bmenu-other-window))))
1923 (defun bookmark-bmenu-show-annotation ()
1924 "Show the annotation for the current bookmark in another window."
1925 (interactive)
1926 (let ((bookmark (bookmark-bmenu-bookmark)))
1927 (if (bookmark-bmenu-check-position)
1928 (bookmark-show-annotation bookmark))))
1931 (defun bookmark-bmenu-show-all-annotations ()
1932 "Show the annotation for all bookmarks in another window."
1933 (interactive)
1934 (bookmark-show-all-annotations))
1937 (defun bookmark-bmenu-edit-annotation ()
1938 "Edit the annotation for the current bookmark in another window."
1939 (interactive)
1940 (let ((bookmark (bookmark-bmenu-bookmark)))
1941 (if (bookmark-bmenu-check-position)
1942 (bookmark-edit-annotation bookmark))))
1945 (defun bookmark-bmenu-unmark (&optional backup)
1946 "Cancel all requested operations on bookmark on this line and move down.
1947 Optional BACKUP means move up."
1948 (interactive "P")
1949 (beginning-of-line)
1950 (if (bookmark-bmenu-check-position)
1951 (progn
1952 (let ((buffer-read-only nil))
1953 (delete-char 1)
1954 ;; any flags to reset according to circumstances? How about a
1955 ;; flag indicating whether this bookmark is being visited?
1956 ;; well, we don't have this now, so maybe later.
1957 (insert " "))
1958 (forward-line (if backup -1 1))
1959 (bookmark-bmenu-check-position))))
1962 (defun bookmark-bmenu-backup-unmark ()
1963 "Move up and cancel all requested operations on bookmark on line above."
1964 (interactive)
1965 (forward-line -1)
1966 (if (bookmark-bmenu-check-position)
1967 (progn
1968 (bookmark-bmenu-unmark)
1969 (forward-line -1)
1970 (bookmark-bmenu-check-position))))
1973 (defun bookmark-bmenu-delete ()
1974 "Mark bookmark on this line to be deleted.
1975 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1976 (interactive)
1977 (beginning-of-line)
1978 (if (bookmark-bmenu-check-position)
1979 (let ((buffer-read-only nil))
1980 (delete-char 1)
1981 (insert ?D)
1982 (forward-line 1)
1983 (bookmark-bmenu-check-position))))
1986 (defun bookmark-bmenu-delete-backwards ()
1987 "Mark bookmark on this line to be deleted, then move up one line.
1988 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1989 (interactive)
1990 (bookmark-bmenu-delete)
1991 (forward-line -2)
1992 (if (bookmark-bmenu-check-position)
1993 (forward-line 1))
1994 (bookmark-bmenu-check-position))
1997 (defun bookmark-bmenu-execute-deletions ()
1998 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1999 (interactive)
2000 (message "Deleting bookmarks...")
2001 (let ((hide-em bookmark-bmenu-toggle-filenames)
2002 (o-point (point))
2003 (o-str (save-excursion
2004 (beginning-of-line)
2005 (if (looking-at "^D")
2007 (buffer-substring
2008 (point)
2009 (progn (end-of-line) (point))))))
2010 (o-col (current-column)))
2011 (if hide-em (bookmark-bmenu-hide-filenames))
2012 (setq bookmark-bmenu-toggle-filenames nil)
2013 (goto-char (point-min))
2014 (forward-line 1)
2015 (while (re-search-forward "^D" (point-max) t)
2016 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2017 (bookmark-bmenu-list)
2018 (setq bookmark-bmenu-toggle-filenames hide-em)
2019 (if bookmark-bmenu-toggle-filenames
2020 (bookmark-bmenu-toggle-filenames t))
2021 (if o-str
2022 (progn
2023 (goto-char (point-min))
2024 (search-forward o-str)
2025 (beginning-of-line)
2026 (forward-char o-col))
2027 (goto-char o-point))
2028 (beginning-of-line)
2029 (setq bookmark-alist-modification-count
2030 (1+ bookmark-alist-modification-count))
2031 (if (bookmark-time-to-save-p)
2032 (bookmark-save))
2033 (message "Deleting bookmarks...done")
2037 (defun bookmark-bmenu-rename ()
2038 "Rename bookmark on current line. Prompts for a new name."
2039 (interactive)
2040 (if (bookmark-bmenu-check-position)
2041 (let ((bmrk (bookmark-bmenu-bookmark))
2042 (thispoint (point)))
2043 (bookmark-rename bmrk)
2044 (bookmark-bmenu-list)
2045 (goto-char thispoint))))
2048 (defun bookmark-bmenu-locate ()
2049 "Display location of this bookmark. Displays in the minibuffer."
2050 (interactive)
2051 (if (bookmark-bmenu-check-position)
2052 (let ((bmrk (bookmark-bmenu-bookmark)))
2053 (message (bookmark-location bmrk)))))
2057 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2059 (defun bookmark-menu-build-paned-menu (name entries)
2060 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2061 That is, ENTRIES is a list of strings which appear as the choices
2062 in the menu. The number of panes depends on the number of entries.
2063 The visible entries are truncated to `bookmark-menu-length', but the
2064 strings returned are not."
2065 (let* ((f-height (/ (frame-height) 2))
2066 (pane-list
2067 (let (temp-pane-list
2068 (iter 0))
2069 (while entries
2070 (let (lst
2071 (count 0))
2072 (while (and (< count f-height) entries)
2073 (let ((str (car entries)))
2074 (setq lst (cons
2075 (cons
2076 (if (> (length str) bookmark-menu-length)
2077 (substring str 0 bookmark-menu-length)
2078 str)
2079 str)
2080 lst))
2081 (setq entries (cdr entries))
2082 (setq count (1+ count))))
2083 (setq iter (1+ iter))
2084 (setq
2085 temp-pane-list
2086 (cons
2087 (cons
2088 (format "-*- %s (%d) -*-" name iter)
2089 (nreverse lst))
2090 temp-pane-list))))
2091 (nreverse temp-pane-list))))
2093 ;; Return the menu:
2094 (cons (concat "-*- " name " -*-") pane-list)))
2097 (defun bookmark-menu-popup-paned-menu (event name entries)
2098 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2099 That is, ENTRIES is a list of strings which appear as the choices
2100 in the menu.
2101 The number of panes depends on the number of entries."
2102 (interactive "e")
2103 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2106 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2107 "Pop up menu of bookmarks, return chosen bookmark.
2108 Pop up at EVENT, menu's name is NAME.
2109 The number of panes depends on the number of bookmarks."
2110 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2113 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2114 ;; help function for making menus that need to apply a bookmark
2115 ;; function to a string.
2116 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2117 event menu-label)))
2118 (if choice (apply func-sym (list choice)))))
2121 ;;;###autoload
2122 (defun bookmark-menu-insert (event)
2123 "Insert the text of the file pointed to by bookmark BOOKMARK.
2124 You may have a problem using this function if the value of variable
2125 `bookmark-alist' is nil. If that happens, you need to load in some
2126 bookmarks. See help on function `bookmark-load' for more about
2127 this.
2129 Warning: this function only takes an EVENT as argument. Use the
2130 corresponding bookmark function from Lisp \(the one without the
2131 \"-menu-\" in its name\)."
2132 (interactive "e")
2133 (bookmark-popup-menu-and-apply-function
2134 'bookmark-insert "Insert Bookmark Contents" event))
2137 ;;;###autoload
2138 (defun bookmark-menu-jump (event)
2139 "Jump to bookmark BOOKMARK (a point in some file).
2140 You may have a problem using this function if the value of variable
2141 `bookmark-alist' is nil. If that happens, you need to load in some
2142 bookmarks. See help on function `bookmark-load' for more about
2143 this.
2145 Warning: this function only takes an EVENT as argument. Use the
2146 corresponding bookmark function from Lisp \(the one without the
2147 \"-menu-\" in its name\)."
2148 (interactive "e")
2149 (bookmark-popup-menu-and-apply-function
2150 'bookmark-jump "Jump to Bookmark" event))
2153 ;;;###autoload
2154 (defun bookmark-menu-locate (event)
2155 "Insert the name of the file associated with BOOKMARK.
2156 \(This is not the same as the contents of that file\).
2158 Warning: this function only takes an EVENT as argument. Use the
2159 corresponding bookmark function from Lisp \(the one without the
2160 \"-menu-\" in its name\)."
2161 (interactive "e")
2162 (bookmark-popup-menu-and-apply-function
2163 'bookmark-insert-location "Insert Bookmark Location" event))
2166 ;;;###autoload
2167 (defun bookmark-menu-rename (event)
2168 "Change the name of OLD-BOOKMARK to NEWNAME.
2169 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2170 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2171 prompts for NEWNAME.
2172 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2173 passed as an argument. If called with two strings, then no prompting
2174 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2176 While you are entering the new name, consecutive C-w's insert
2177 consecutive words from the text of the buffer into the new bookmark
2178 name.
2180 Warning: this function only takes an EVENT as argument. Use the
2181 corresponding bookmark function from Lisp \(the one without the
2182 \"-menu-\" in its name\)."
2183 (interactive "e")
2184 (bookmark-popup-menu-and-apply-function
2185 'bookmark-rename "Rename Bookmark" event))
2188 ;;;###autoload
2189 (defun bookmark-menu-delete (event)
2190 "Delete the bookmark named NAME from the bookmark list.
2191 Removes only the first instance of a bookmark with that name. If
2192 there are one or more other bookmarks with the same name, they will
2193 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2194 one most recently used in this file, if any\).
2196 Warning: this function only takes an EVENT as argument. Use the
2197 corresponding bookmark function from Lisp \(the one without the
2198 \"-menu-\" in its name\)."
2199 (interactive "e")
2200 (bookmark-popup-menu-and-apply-function
2201 'bookmark-delete "Delete Bookmark" event))
2204 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2205 ;; following works, and for explaining what to do to make it work.
2207 ;; We MUST autoload EACH form used to set up this variable's value, so
2208 ;; that the whole job is done in loaddefs.el.
2210 ;; Emacs menubar stuff.
2212 ;;;###autoload
2213 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2215 ;;;###autoload
2216 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2218 ;; make bookmarks appear toward the right side of the menu.
2219 (if (boundp 'menu-bar-final-items)
2220 (if menu-bar-final-items
2221 (setq menu-bar-final-items
2222 (cons 'bookmark menu-bar-final-items)))
2223 (setq menu-bar-final-items '(bookmark)))
2225 ;;;###autoload
2226 (define-key menu-bar-bookmark-map [load]
2227 '("Load a Bookmark File..." . bookmark-load))
2229 ;;;###autoload
2230 (define-key menu-bar-bookmark-map [write]
2231 '("Save Bookmarks As..." . bookmark-write))
2233 ;;;###autoload
2234 (define-key menu-bar-bookmark-map [save]
2235 '("Save Bookmarks" . bookmark-save))
2237 ;;;###autoload
2238 (define-key menu-bar-bookmark-map [edit]
2239 '("Edit Bookmark List" . bookmark-bmenu-list))
2241 ;;;###autoload
2242 (define-key menu-bar-bookmark-map [delete]
2243 '("Delete Bookmark" . bookmark-menu-delete))
2245 ;;;###autoload
2246 (define-key menu-bar-bookmark-map [rename]
2247 '("Rename Bookmark" . bookmark-menu-rename))
2249 ;;;###autoload
2250 (define-key menu-bar-bookmark-map [locate]
2251 '("Insert Location" . bookmark-menu-locate))
2253 ;;;###autoload
2254 (define-key menu-bar-bookmark-map [insert]
2255 '("Insert Contents" . bookmark-menu-insert))
2257 ;;;###autoload
2258 (define-key menu-bar-bookmark-map [set]
2259 '("Set Bookmark" . bookmark-set))
2261 ;;;###autoload
2262 (define-key menu-bar-bookmark-map [jump]
2263 '("Jump to Bookmark" . bookmark-menu-jump))
2265 ;;;; end bookmark menu stuff ;;;;
2268 ;;; Load Hook
2269 (defvar bookmark-load-hook nil
2270 "Hook to run at the end of loading bookmark.")
2272 (run-hooks 'bookmark-load-hook)
2274 (provide 'bookmark)
2276 ;;; bookmark.el ends here