(Fdisplay_completion_list): Doc fix.
[emacs.git] / lisp / bookmark.el
blob86fa6e489f082a2404d1badc56ef8590ebb1af0c
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 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" for "mark"
244 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
245 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
246 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
247 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
248 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
249 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
250 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
251 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
252 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
253 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
256 ;;; The annotation maps.
257 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
258 "Keymap for composing an annotation for a bookmark.")
260 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
261 'bookmark-send-annotation)
265 ;;; Core variables and data structures:
266 (defvar bookmark-alist ()
267 "Association list of bookmarks and their records.
268 You probably don't want to change the value of this alist yourself;
269 instead, let the various bookmark functions do it for you.
271 The format of the alist is
273 \(BOOKMARK1 BOOKMARK2 ...\)
275 where each BOOKMARK is of the form
277 \(NAME
278 \(filename . FILE\)
279 \(front-context-string . FRONT-STR\)
280 \(rear-context-string . REAR-STR\)
281 \(position . POS\)
282 \(info-node . POS\)
283 \(annotation . ANNOTATION\)\)
285 So the cdr of each bookmark is an alist too.
286 `info-node' is optional, by the way.")
289 (defvar bookmarks-already-loaded nil)
292 ;; more stuff added by db.
294 (defvar bookmark-current-bookmark nil
295 "Name of bookmark most recently used in the current file.
296 It is buffer local, used to make moving a bookmark forward
297 through a file easier.")
299 (make-variable-buffer-local 'bookmark-current-bookmark)
302 (defvar bookmark-alist-modification-count 0
303 "Number of modifications to bookmark list since it was last saved.")
306 (defvar bookmark-search-size 16
307 "Length of the context strings recorded on either side of a bookmark.")
310 (defvar bookmark-current-point 0)
311 (defvar bookmark-yank-point 0)
312 (defvar bookmark-current-buffer nil)
314 (defvar Info-current-node)
315 (defvar Info-suffix-list)
317 ;; Helper functions.
319 ;; Only functions on this page and the next one (file formats) need to
320 ;; know anything about the format of bookmark-alist entries.
321 ;; Everyone else should go through them.
323 (defun bookmark-name-from-full-record (full-record)
324 "Return name of FULL-RECORD \(an alist element instead of a string\)."
325 (car full-record))
328 (defun bookmark-all-names ()
329 "Return a list of all current bookmark names."
330 (bookmark-maybe-load-default-file)
331 (mapcar
332 (lambda (full-record)
333 (bookmark-name-from-full-record full-record))
334 bookmark-alist))
337 (defun bookmark-get-bookmark (bookmark)
338 "Return the full entry for BOOKMARK in `bookmark-alist'.
339 If BOOKMARK is not a string, return nil."
340 (when (stringp bookmark)
341 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
344 (defun bookmark-get-bookmark-record (bookmark)
345 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
346 That is, all information but the name."
347 (car (cdr (bookmark-get-bookmark bookmark))))
350 (defun bookmark-set-name (bookmark newname)
351 "Set BOOKMARK's name to NEWNAME."
352 (setcar
353 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
354 newname))
357 (defun bookmark-get-annotation (bookmark)
358 "Return the annotation of BOOKMARK, or nil if none."
359 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
362 (defun bookmark-set-annotation (bookmark ann)
363 "Set the annotation of BOOKMARK to ANN."
364 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
365 (if cell
366 (setcdr cell ann)
367 (nconc (bookmark-get-bookmark-record bookmark)
368 (list (cons 'annotation ann))))))
371 (defun bookmark-get-filename (bookmark)
372 "Return the full filename of BOOKMARK."
373 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
376 (defun bookmark-set-filename (bookmark filename)
377 "Set the full filename of BOOKMARK to FILENAME."
378 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
379 (if cell
380 (setcdr cell filename)
381 (nconc (bookmark-get-bookmark-record bookmark)
382 (list (cons 'filename filename))))
383 (setq bookmark-alist-modification-count
384 (1+ bookmark-alist-modification-count))
385 (if (bookmark-time-to-save-p)
386 (bookmark-save))))
389 (defun bookmark-get-position (bookmark)
390 "Return the position \(i.e.: point\) of BOOKMARK."
391 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
394 (defun bookmark-set-position (bookmark position)
395 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
396 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
397 (if cell
398 (setcdr cell position)
399 (nconc (bookmark-get-bookmark-record bookmark)
400 (list (cons 'position position))))))
403 (defun bookmark-get-front-context-string (bookmark)
404 "Return the front-context-string of BOOKMARK."
405 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
408 (defun bookmark-set-front-context-string (bookmark string)
409 "Set the front-context-string of BOOKMARK to STRING."
410 (let ((cell (assq 'front-context-string
411 (bookmark-get-bookmark-record bookmark))))
412 (if cell
413 (setcdr cell string)
414 (nconc (bookmark-get-bookmark-record bookmark)
415 (list (cons 'front-context-string string))))))
418 (defun bookmark-get-rear-context-string (bookmark)
419 "Return the rear-context-string of BOOKMARK."
420 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
423 (defun bookmark-set-rear-context-string (bookmark string)
424 "Set the rear-context-string of BOOKMARK to STRING."
425 (let ((cell (assq 'rear-context-string
426 (bookmark-get-bookmark-record bookmark))))
427 (if cell
428 (setcdr cell string)
429 (nconc (bookmark-get-bookmark-record bookmark)
430 (list (cons 'rear-context-string string))))))
433 (defun bookmark-get-info-node (bookmark)
434 "Get the info node associated with BOOKMARK."
435 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
438 (defun bookmark-set-info-node (bookmark node)
439 "Set the Info node of BOOKMARK to NODE."
440 (let ((cell (assq 'info-node
441 (bookmark-get-bookmark-record bookmark))))
442 (if cell
443 (setcdr cell node)
444 (nconc (bookmark-get-bookmark-record bookmark)
445 (list (cons 'info-node node)))))
447 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
448 (sit-for 4))
451 (defvar bookmark-history nil
452 "The history list for bookmark functions.")
455 (defun bookmark-completing-read (prompt &optional default)
456 "Prompting with PROMPT, read a bookmark name in completion.
457 PROMPT will get a \": \" stuck on the end no matter what, so you
458 probably don't want to include one yourself.
459 Optional second arg DEFAULT is a string to return if the user enters
460 the empty string."
461 (bookmark-maybe-load-default-file) ; paranoia
462 (if (listp last-nonmenu-event)
463 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
464 (let* ((completion-ignore-case bookmark-completion-ignore-case)
465 (default default)
466 (prompt (if default
467 (concat prompt (format " (%s): " default))
468 (concat prompt ": ")))
469 (str
470 (completing-read prompt
471 bookmark-alist
475 'bookmark-history)))
476 (if (string-equal "" str) default str))))
479 (defmacro bookmark-maybe-historicize-string (string)
480 "Put STRING into the bookmark prompt history, if caller non-interactive.
481 We need this because sometimes bookmark functions are invoked from
482 menus, so `completing-read' never gets a chance to set `bookmark-history'."
483 `(or
484 (interactive-p)
485 (setq bookmark-history (cons ,string bookmark-history))))
488 (defun bookmark-make (name &optional annotation overwrite info-node)
489 "Make a bookmark named NAME.
490 Optional second arg ANNOTATION gives it an annotation.
491 Optional third arg OVERWRITE means replace any existing bookmarks with
492 this name.
493 Optional fourth arg INFO-NODE means this bookmark is at info node
494 INFO-NODE, so record this fact in the bookmark's entry."
495 (bookmark-maybe-load-default-file)
496 (let ((stripped-name (copy-sequence name)))
497 (or bookmark-xemacsp
498 ;; XEmacs's `set-text-properties' doesn't work on
499 ;; free-standing strings, apparently.
500 (set-text-properties 0 (length stripped-name) nil stripped-name))
501 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
502 ;; already existing bookmark under that name and
503 ;; no prefix arg means just overwrite old bookmark
504 (setcdr (bookmark-get-bookmark stripped-name)
505 (list (bookmark-make-cell annotation info-node)))
507 ;; otherwise just cons it onto the front (either the bookmark
508 ;; doesn't exist already, or there is no prefix arg. In either
509 ;; case, we want the new bookmark consed onto the alist...)
511 (setq bookmark-alist
512 (cons
513 (list stripped-name
514 (bookmark-make-cell annotation info-node))
515 bookmark-alist)))
517 ;; Added by db
518 (setq bookmark-current-bookmark stripped-name)
519 (setq bookmark-alist-modification-count
520 (1+ bookmark-alist-modification-count))
521 (if (bookmark-time-to-save-p)
522 (bookmark-save))))
525 (defun bookmark-make-cell (annotation &optional info-node)
526 "Return the record part of a new bookmark, given ANNOTATION.
527 Must be at the correct position in the buffer in which the bookmark is
528 being set. This might change someday.
529 Optional second arg INFO-NODE means this bookmark is at info node
530 INFO-NODE, so record this fact in the bookmark's entry."
531 (let ((the-record
532 `((filename . ,(bookmark-buffer-file-name))
533 (front-context-string
534 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
535 (buffer-substring-no-properties
536 (point)
537 (+ (point) bookmark-search-size))
538 nil))
539 (rear-context-string
540 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
541 (buffer-substring-no-properties
542 (point)
543 (- (point) bookmark-search-size))
544 nil))
545 (position . ,(point)))))
547 ;; Now fill in the optional parts:
549 ;; Take no chances with text properties
550 (set-text-properties 0 (length annotation) nil annotation)
551 (set-text-properties 0 (length info-node) nil info-node)
553 (if annotation
554 (nconc the-record (list (cons 'annotation annotation))))
555 (if info-node
556 (nconc the-record (list (cons 'info-node info-node))))
558 ;; Finally, return the completed record.
559 the-record))
563 ;;; File format stuff
565 ;; The OLD format of the bookmark-alist was:
567 ;; ((bookmark-name (filename
568 ;; string-in-front
569 ;; string-behind
570 ;; point))
571 ;; ...)
573 ;; The NEW format of the bookmark-alist is:
575 ;; ((bookmark-name ((filename . FILENAME)
576 ;; (front-context-string . string-in-front)
577 ;; (rear-context-string . string-behind)
578 ;; (position . POINT)
579 ;; (annotation . annotation)
580 ;; (whatever . VALUE)
581 ;; ...
582 ;; ))
583 ;; ...)
586 ;; I switched to using an internal as well as external alist because I
587 ;; felt that would be a more flexible framework in which to add
588 ;; features. It means that the order in which values appear doesn't
589 ;; matter, and it means that arbitrary values can be added without
590 ;; risk of interfering with existing ones.
592 ;; BOOKMARK-NAME is the string the user gives the bookmark and
593 ;; accesses it by from then on.
595 ;; FILENAME is the location of the file in which the bookmark is set.
597 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
598 ;; context in front of the point at which the bookmark is set.
600 ;; STRING-BEHIND is the same thing, but after the point.
602 ;; The context strings exist so that modifications to a file don't
603 ;; necessarily cause a bookmark's position to be invalidated.
604 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
605 ;; case the file has changed since the bookmark was set. It will
606 ;; attempt to place the user before the changes, if there were any.
607 ;; ANNOTATION is the annotation for the bookmark; it may not exist
608 ;; (for backward compatibility), be nil (no annotation), or be a
609 ;; string.
612 (defconst bookmark-file-format-version 1
613 "The current version of the format used by bookmark files.
614 You should never need to change this.")
617 (defconst bookmark-end-of-version-stamp-marker
618 "-*- End Of Bookmark File Format Version Stamp -*-\n"
619 "This string marks the end of the version stamp in a bookmark file.")
622 (defun bookmark-alist-from-buffer ()
623 "Return a `bookmark-alist' (in any format) from the current buffer.
624 The buffer must of course contain bookmark format information.
625 Does not care from where in the buffer it is called, and does not
626 affect point."
627 (save-excursion
628 (goto-char (point-min))
629 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
630 (read (current-buffer))
631 ;; Else we're dealing with format version 0
632 (if (search-forward "(" nil t)
633 (progn
634 (forward-char -1)
635 (read (current-buffer)))
636 ;; Else no hope of getting information here.
637 (error "Not bookmark format")))))
640 (defun bookmark-upgrade-version-0-alist (old-list)
641 "Upgrade a version 0 alist OLD-LIST to the current version."
642 (mapcar
643 (lambda (bookmark)
644 (let* ((name (car bookmark))
645 (record (car (cdr bookmark)))
646 (filename (nth 0 record))
647 (front-str (nth 1 record))
648 (rear-str (nth 2 record))
649 (position (nth 3 record))
650 (ann (nth 4 record)))
651 (list
652 name
653 `((filename . ,filename)
654 (front-context-string . ,(or front-str ""))
655 (rear-context-string . ,(or rear-str ""))
656 (position . ,position)
657 (annotation . ,ann)))))
658 old-list))
661 (defun bookmark-upgrade-file-format-from-0 ()
662 "Upgrade a bookmark file of format 0 (the original format) to format 1.
663 This expects to be called from `point-min' in a bookmark file."
664 (message "Upgrading bookmark format from 0 to %d..."
665 bookmark-file-format-version)
666 (let* ((old-list (bookmark-alist-from-buffer))
667 (new-list (bookmark-upgrade-version-0-alist old-list)))
668 (delete-region (point-min) (point-max))
669 (bookmark-insert-file-format-version-stamp)
670 (pp new-list (current-buffer))
671 (save-buffer))
672 (goto-char (point-min))
673 (message "Upgrading bookmark format from 0 to %d...done"
674 bookmark-file-format-version)
678 (defun bookmark-grok-file-format-version ()
679 "Return an integer which is the file-format version of this bookmark file.
680 This expects to be called from `point-min' in a bookmark file."
681 (if (looking-at "^;;;;")
682 (save-excursion
683 (save-match-data
684 (re-search-forward "[0-9]")
685 (forward-char -1)
686 (read (current-buffer))))
687 ;; Else this is format version 0, the original one, which didn't
688 ;; even have version stamps.
692 (defun bookmark-maybe-upgrade-file-format ()
693 "Check the file-format version of this bookmark file.
694 If the version is not up-to-date, upgrade it automatically.
695 This expects to be called from `point-min' in a bookmark file."
696 (let ((version (bookmark-grok-file-format-version)))
697 (cond
698 ((= version bookmark-file-format-version)
699 ) ; home free -- version is current
700 ((= version 0)
701 (bookmark-upgrade-file-format-from-0))
703 (error "Bookmark file format version strangeness")))))
706 (defun bookmark-insert-file-format-version-stamp ()
707 "Insert text indicating current version of bookmark file format."
708 (insert
709 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
710 bookmark-file-format-version))
711 (insert ";;; This format is meant to be slightly human-readable;\n"
712 ";;; nevertheless, you probably don't want to edit it.\n"
713 ";;; "
714 bookmark-end-of-version-stamp-marker))
717 ;;; end file-format stuff
720 ;;; Core code:
722 ;;;###autoload
723 (defun bookmark-set (&optional name parg)
724 "Set a bookmark named NAME inside a file.
725 If name is nil, then the user will be prompted.
726 With prefix arg, will not overwrite a bookmark that has the same name
727 as NAME if such a bookmark already exists, but instead will \"push\"
728 the new bookmark onto the bookmark alist. Thus the most recently set
729 bookmark with name NAME would be the one in effect at any given time,
730 but the others are still there, should you decide to delete the most
731 recent one.
733 To yank words from the text of the buffer and use them as part of the
734 bookmark name, type C-w while setting a bookmark. Successive C-w's
735 yank successive words.
737 Typing C-u inserts the name of the last bookmark used in the buffer
738 \(as an aid in using a single bookmark name to track your progress
739 through a large file\). If no bookmark was used, then C-u inserts the
740 name of the file being visited.
742 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
743 and it removes only the first instance of a bookmark with that name from
744 the list of bookmarks.\)"
745 (interactive (list nil current-prefix-arg))
747 (bookmark-buffer-file-name)
748 (error "Buffer not visiting a file or directory"))
750 (bookmark-maybe-load-default-file)
752 (setq bookmark-current-point (point))
753 (setq bookmark-yank-point (point))
754 (setq bookmark-current-buffer (current-buffer))
756 (let* ((default (or bookmark-current-bookmark
757 (bookmark-buffer-name)))
758 (str
759 (or name
760 (read-from-minibuffer
761 (format "Set bookmark (%s): " default)
763 (let ((now-map (copy-keymap minibuffer-local-map)))
764 (define-key now-map "\C-w" 'bookmark-yank-word)
765 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
766 now-map))))
767 (annotation nil))
768 (and (string-equal str "") (setq str default))
769 ;; Ask for an annotation buffer for this bookmark
770 (if bookmark-use-annotations
771 (bookmark-read-annotation parg str)
772 (bookmark-make str annotation parg (bookmark-info-current-node))
773 (setq bookmark-current-bookmark str)
774 (bookmark-bmenu-surreptitiously-rebuild-list)
775 (goto-char bookmark-current-point))))
778 (defun bookmark-info-current-node ()
779 "If in Info-mode, return current node name (a string), else nil."
780 (if (eq major-mode 'Info-mode)
781 Info-current-node))
784 (defun bookmark-kill-line (&optional newline-too)
785 "Kill from point to end of line.
786 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
787 Does not affect the kill ring."
788 (let ((eol (save-excursion (end-of-line) (point))))
789 (delete-region (point) eol)
790 (if (and newline-too (looking-at "\n"))
791 (delete-char 1))))
794 ;; Defvars to avoid compilation warnings:
795 (defvar bookmark-annotation-paragraph nil)
796 (defvar bookmark-annotation-name nil)
797 (defvar bookmark-annotation-buffer nil)
798 (defvar bookmark-annotation-file nil)
799 (defvar bookmark-annotation-point nil)
802 (defun bookmark-send-annotation ()
803 "Use buffer contents as the annotation for a bookmark.
804 Exclude lines that begin with `#'.
805 Store the annotation text in the bookmark list with
806 the bookmark (and file, and point) specified in buffer local variables."
807 (interactive)
808 (if (not (eq major-mode 'bookmark-read-annotation-mode))
809 (error "Not in bookmark-read-annotation-mode"))
810 (goto-char (point-min))
811 (while (< (point) (point-max))
812 (if (looking-at "^#")
813 (bookmark-kill-line t)
814 (forward-line 1)))
815 (let ((annotation (buffer-string))
816 (parg bookmark-annotation-paragraph)
817 (bookmark bookmark-annotation-name)
818 (pt bookmark-annotation-point)
819 (buf bookmark-annotation-buffer))
820 ;; for bookmark-make-cell to work, we need to be
821 ;; in the relevant buffer, at the relevant point.
822 ;; Actually, bookmark-make-cell should probably be re-written,
823 ;; to avoid this need. Should I handle the error if a buffer is
824 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
825 (save-excursion
826 (pop-to-buffer buf)
827 (goto-char pt)
828 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
829 (setq bookmark-current-bookmark bookmark))
830 (bookmark-bmenu-surreptitiously-rebuild-list)
831 (goto-char bookmark-current-point))
832 (kill-buffer (current-buffer)))
835 (defun bookmark-default-annotation-text (bookmark)
836 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
837 "# All lines which start with a '#' will be deleted.\n"
838 "# Type C-c C-c when done.\n#\n"
839 "# Author: " (user-full-name) " <" (user-login-name) "@"
840 (system-name) ">\n"
841 "# Date: " (current-time-string) "\n"))
844 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
845 "Function to return default text to use for a bookmark annotation.
846 It takes one argument, the name of the bookmark, as a string.")
848 (defun bookmark-read-annotation-mode (buf point parg bookmark)
849 "Mode for composing annotations for a bookmark.
850 Wants BUF, POINT, PARG, and BOOKMARK.
851 When you have finished composing, type \\[bookmark-send-annotation] to send
852 the annotation.
854 \\{bookmark-read-annotation-mode-map}
856 (interactive)
857 (kill-all-local-variables)
858 (make-local-variable 'bookmark-annotation-paragraph)
859 (make-local-variable 'bookmark-annotation-name)
860 (make-local-variable 'bookmark-annotation-buffer)
861 (make-local-variable 'bookmark-annotation-file)
862 (make-local-variable 'bookmark-annotation-point)
863 (setq bookmark-annotation-paragraph parg)
864 (setq bookmark-annotation-name bookmark)
865 (setq bookmark-annotation-buffer buf)
866 (setq bookmark-annotation-file (buffer-file-name buf))
867 (setq bookmark-annotation-point point)
868 (use-local-map bookmark-read-annotation-mode-map)
869 (setq major-mode 'bookmark-read-annotation-mode)
870 (insert (funcall bookmark-read-annotation-text-func bookmark))
871 (run-mode-hooks 'text-mode-hook))
874 (defun bookmark-read-annotation (parg bookmark)
875 "Pop up a buffer for entering a bookmark annotation.
876 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
877 (let ((buf (current-buffer))
878 (point (point)))
879 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
880 (bookmark-read-annotation-mode buf point parg bookmark)))
883 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
884 "Keymap for editing an annotation of a bookmark.")
887 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
888 'bookmark-send-edited-annotation)
891 (defun bookmark-edit-annotation-mode (bookmark)
892 "Mode for editing the annotation of bookmark BOOKMARK.
893 When you have finished composing, type \\[bookmark-send-annotation].
895 \\{bookmark-edit-annotation-mode-map}
897 (interactive)
898 (kill-all-local-variables)
899 (make-local-variable 'bookmark-annotation-name)
900 (setq bookmark-annotation-name bookmark)
901 (use-local-map bookmark-edit-annotation-mode-map)
902 (setq major-mode 'bookmark-edit-annotation-mode
903 mode-name "Edit Bookmark Annotation")
904 (insert (funcall bookmark-read-annotation-text-func bookmark))
905 (let ((annotation (bookmark-get-annotation bookmark)))
906 (if (and annotation (not (string-equal annotation "")))
907 (insert annotation)))
908 (run-mode-hooks 'text-mode-hook))
911 (defun bookmark-send-edited-annotation ()
912 "Use buffer contents as annotation for a bookmark.
913 Lines beginning with `#' are ignored."
914 (interactive)
915 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
916 (error "Not in bookmark-edit-annotation-mode"))
917 (goto-char (point-min))
918 (while (< (point) (point-max))
919 (if (looking-at "^#")
920 (bookmark-kill-line t)
921 (forward-line 1)))
922 (let ((annotation (buffer-string))
923 (bookmark bookmark-annotation-name))
924 (bookmark-set-annotation bookmark annotation)
925 (bookmark-bmenu-surreptitiously-rebuild-list)
926 (goto-char bookmark-current-point))
927 (kill-buffer (current-buffer)))
930 (defun bookmark-edit-annotation (bookmark)
931 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
932 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
933 (bookmark-edit-annotation-mode bookmark))
936 (defun bookmark-insert-current-bookmark ()
937 "Insert this buffer's value of `bookmark-current-bookmark'.
938 Default to file name if it's nil."
939 (interactive)
940 (let ((str
941 (save-excursion
942 (set-buffer bookmark-current-buffer)
943 bookmark-current-bookmark)))
944 (if str (insert str) (bookmark-insert-buffer-name))))
947 (defun bookmark-insert-buffer-name ()
948 "Insert the current file name into the bookmark name being set.
949 The directory part of the file name is not used."
950 (interactive)
951 (let ((str
952 (save-excursion
953 (set-buffer bookmark-current-buffer)
954 (bookmark-buffer-name))))
955 (insert str)))
958 (defun bookmark-buffer-name ()
959 "Return the name of the current buffer's file, non-directory.
960 In Info, return the current node."
961 (cond
962 ;; Are we in Info?
963 ((string-equal mode-name "Info") Info-current-node)
964 ;; Or are we a file?
965 (buffer-file-name (file-name-nondirectory buffer-file-name))
966 ;; Or are we a directory?
967 ((and (boundp 'dired-directory) dired-directory)
968 (let* ((dirname (if (stringp dired-directory)
969 dired-directory
970 (car dired-directory)))
971 (idx (1- (length dirname))))
972 ;; Strip the trailing slash.
973 (if (= ?/ (aref dirname idx))
974 (file-name-nondirectory (substring dirname 0 idx))
975 ;; Else return the current-buffer
976 (buffer-name (current-buffer)))))
977 ;; If all else fails, use the buffer's name.
979 (buffer-name (current-buffer)))))
982 (defun bookmark-yank-word ()
983 (interactive)
984 ;; get the next word from the buffer and append it to the name of
985 ;; the bookmark currently being set.
986 (let ((string (save-excursion
987 (set-buffer bookmark-current-buffer)
988 (goto-char bookmark-yank-point)
989 (buffer-substring-no-properties
990 (point)
991 (progn
992 (forward-word 1)
993 (setq bookmark-yank-point (point)))))))
994 (insert string)))
997 (defvar Info-current-file)
999 (defun bookmark-buffer-file-name ()
1000 "Return the current buffer's file in a way useful for bookmarks.
1001 For example, if this is a Info buffer, return the Info file's name."
1002 (if (eq major-mode 'Info-mode)
1003 Info-current-file
1005 buffer-file-name
1006 (if (and (boundp 'dired-directory) dired-directory)
1007 (if (stringp dired-directory)
1008 dired-directory
1009 (car dired-directory))))))
1012 (defun bookmark-maybe-load-default-file ()
1013 (and (not bookmarks-already-loaded)
1014 (null bookmark-alist)
1015 (prog2
1016 (and
1017 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1018 ;; to be renamed.
1019 (file-exists-p (expand-file-name bookmark-old-default-file))
1020 (not (file-exists-p (expand-file-name bookmark-default-file)))
1021 (rename-file (expand-file-name bookmark-old-default-file)
1022 (expand-file-name bookmark-default-file)))
1023 ;; return t so the `and' will continue...
1026 (file-readable-p (expand-file-name bookmark-default-file))
1027 (bookmark-load bookmark-default-file t t)
1028 (setq bookmarks-already-loaded t)))
1031 (defun bookmark-maybe-sort-alist ()
1032 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1033 ;;is non-nil, then return a sorted copy of the alist.
1034 (if bookmark-sort-flag
1035 (setq bookmark-alist
1036 (sort (copy-alist bookmark-alist)
1037 (function
1038 (lambda (x y) (string-lessp (car x) (car y))))))))
1041 (defvar bookmark-after-jump-hook nil
1042 "Hook run after `bookmark-jump' jumps to a bookmark.
1043 Useful for example to unhide text in `outline-mode'.")
1045 ;;;###autoload
1046 (defun bookmark-jump (bookmark)
1047 "Jump to bookmark BOOKMARK (a point in some file).
1048 You may have a problem using this function if the value of variable
1049 `bookmark-alist' is nil. If that happens, you need to load in some
1050 bookmarks. See help on function `bookmark-load' for more about
1051 this.
1053 If the file pointed to by BOOKMARK no longer exists, you will be asked
1054 if you wish to give the bookmark a new location, and `bookmark-jump'
1055 will then jump to the new location, as well as recording it in place
1056 of the old one in the permanent bookmark record."
1057 (interactive
1058 (list (bookmark-completing-read "Jump to bookmark"
1059 bookmark-current-bookmark)))
1060 (unless bookmark
1061 (error "No bookmark specified"))
1062 (bookmark-maybe-historicize-string bookmark)
1063 (let ((cell (bookmark-jump-noselect bookmark)))
1064 (and cell
1065 (switch-to-buffer (car cell))
1066 (goto-char (cdr cell))
1067 (progn (run-hooks 'bookmark-after-jump-hook) t)
1068 (if bookmark-automatically-show-annotations
1069 ;; if there is an annotation for this bookmark,
1070 ;; show it in a buffer.
1071 (bookmark-show-annotation bookmark)))))
1074 (defun bookmark-file-or-variation-thereof (file)
1075 "Return FILE (a string) if it exists, or return a reasonable
1076 variation of FILE if that exists. Reasonable variations are checked
1077 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1078 nor a reasonable variation thereof, then still return FILE if it can
1079 be retrieved from a VC backend, else return nil."
1080 (if (file-exists-p file)
1081 file
1083 (progn (require 'info) ; ensure Info-suffix-list is bound
1084 (catch 'found
1085 (mapc (lambda (elt)
1086 (let ((suffixed-file (concat file (car elt))))
1087 (if (file-exists-p suffixed-file)
1088 (throw 'found suffixed-file))))
1089 Info-suffix-list)
1090 nil))
1091 ;; Last possibility: try VC
1092 (if (vc-backend file) file))))
1095 (defun bookmark-jump-noselect (str)
1096 ;; a leetle helper for bookmark-jump :-)
1097 ;; returns (BUFFER . POINT)
1098 (bookmark-maybe-load-default-file)
1099 (let* ((file (expand-file-name (bookmark-get-filename str)))
1100 (forward-str (bookmark-get-front-context-string str))
1101 (behind-str (bookmark-get-rear-context-string str))
1102 (place (bookmark-get-position str))
1103 (info-node (bookmark-get-info-node str))
1104 (orig-file file)
1106 (if (setq file (bookmark-file-or-variation-thereof file))
1107 (save-excursion
1108 (save-window-excursion
1109 (if info-node
1110 ;; Info nodes must be visited with care.
1111 (progn
1112 (require 'info)
1113 (with-no-warnings
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 (list (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 (list (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 (add-text-properties
1180 start
1181 (save-excursion (re-search-backward
1182 "[^ \t]")
1183 (1+ (point)))
1184 '(mouse-face highlight
1185 follow-link t
1186 help-echo "mouse-2: go to this bookmark in other window"))))))
1188 ;;;###autoload
1189 (defalias 'bookmark-locate 'bookmark-insert-location)
1191 (defun bookmark-location (bookmark)
1192 "Return the name of the file associated with BOOKMARK."
1193 (bookmark-maybe-load-default-file)
1194 (bookmark-get-filename bookmark))
1197 ;;;###autoload
1198 (defun bookmark-rename (old &optional new)
1199 "Change the name of OLD bookmark to NEW name.
1200 If called from keyboard, prompt for OLD and NEW. If called from
1201 menubar, select OLD from a menu and prompt for NEW.
1203 If called from Lisp, prompt for NEW if only OLD was passed as an
1204 argument. If called with two strings, then no prompting is done. You
1205 must pass at least OLD when calling from Lisp.
1207 While you are entering the new name, consecutive C-w's insert
1208 consecutive words from the text of the buffer into the new bookmark
1209 name."
1210 (interactive (list (bookmark-completing-read "Old bookmark name")))
1211 (bookmark-maybe-historicize-string old)
1212 (bookmark-maybe-load-default-file)
1214 (setq bookmark-current-point (point))
1215 (setq bookmark-yank-point (point))
1216 (setq bookmark-current-buffer (current-buffer))
1217 (let ((newname
1218 (or new ; use second arg, if non-nil
1219 (read-from-minibuffer
1220 "New name: "
1222 (let ((now-map (copy-keymap minibuffer-local-map)))
1223 (define-key now-map "\C-w" 'bookmark-yank-word)
1224 now-map)
1226 'bookmark-history))))
1227 (bookmark-set-name old newname)
1228 (setq bookmark-current-bookmark newname)
1229 (bookmark-bmenu-surreptitiously-rebuild-list)
1230 (setq bookmark-alist-modification-count
1231 (1+ bookmark-alist-modification-count))
1232 (if (bookmark-time-to-save-p)
1233 (bookmark-save))))
1236 ;;;###autoload
1237 (defun bookmark-insert (bookmark)
1238 "Insert the text of the file pointed to by bookmark BOOKMARK.
1239 You may have a problem using this function if the value of variable
1240 `bookmark-alist' is nil. If that happens, you need to load in some
1241 bookmarks. See help on function `bookmark-load' for more about
1242 this."
1243 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1244 (bookmark-maybe-historicize-string bookmark)
1245 (bookmark-maybe-load-default-file)
1246 (let ((orig-point (point))
1247 (str-to-insert
1248 (save-excursion
1249 (set-buffer (car (bookmark-jump-noselect bookmark)))
1250 (buffer-string))))
1251 (insert str-to-insert)
1252 (push-mark)
1253 (goto-char orig-point)))
1256 ;;;###autoload
1257 (defun bookmark-delete (bookmark &optional batch)
1258 "Delete BOOKMARK from the bookmark list.
1259 Removes only the first instance of a bookmark with that name. If
1260 there are one or more other bookmarks with the same name, they will
1261 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1262 one most recently used in this file, if any\).
1263 Optional second arg BATCH means don't update the bookmark list buffer,
1264 probably because we were called from there."
1265 (interactive
1266 (list (bookmark-completing-read "Delete bookmark"
1267 bookmark-current-bookmark)))
1268 (bookmark-maybe-historicize-string bookmark)
1269 (bookmark-maybe-load-default-file)
1270 (let ((will-go (bookmark-get-bookmark bookmark)))
1271 (setq bookmark-alist (delq will-go bookmark-alist))
1272 ;; Added by db, nil bookmark-current-bookmark if the last
1273 ;; occurrence has been deleted
1274 (or (bookmark-get-bookmark bookmark-current-bookmark)
1275 (setq bookmark-current-bookmark nil)))
1276 ;; Don't rebuild the list
1277 (if batch
1279 (bookmark-bmenu-surreptitiously-rebuild-list)
1280 (setq bookmark-alist-modification-count
1281 (1+ bookmark-alist-modification-count))
1282 (if (bookmark-time-to-save-p)
1283 (bookmark-save))))
1286 (defun bookmark-time-to-save-p (&optional last-time)
1287 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1288 ;; finds out whether it's time to save bookmarks to a file, by
1289 ;; examining the value of variable bookmark-save-flag, and maybe
1290 ;; bookmark-alist-modification-count. Returns t if they should be
1291 ;; saved, nil otherwise. if last-time is non-nil, then this is
1292 ;; being called when emacs is killed.
1293 (cond (last-time
1294 (and (> bookmark-alist-modification-count 0)
1295 bookmark-save-flag))
1296 ((numberp bookmark-save-flag)
1297 (>= bookmark-alist-modification-count bookmark-save-flag))
1299 nil)))
1302 ;;;###autoload
1303 (defun bookmark-write ()
1304 "Write bookmarks to a file (reading the file name with the minibuffer).
1305 Don't use this in Lisp programs; use `bookmark-save' instead."
1306 (interactive)
1307 (bookmark-maybe-load-default-file)
1308 (bookmark-save t))
1311 ;;;###autoload
1312 (defun bookmark-save (&optional parg file)
1313 "Save currently defined bookmarks.
1314 Saves by default in the file defined by the variable
1315 `bookmark-default-file'. With a prefix arg, save it in file FILE
1316 \(second argument\).
1318 If you are calling this from Lisp, the two arguments are PARG and
1319 FILE, and if you just want it to write to the default file, then
1320 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1321 instead. If you pass in one argument, and it is non-nil, then the
1322 user will be interactively queried for a file to save in.
1324 When you want to load in the bookmarks from a file, use
1325 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1326 for a file, defaulting to the file defined by variable
1327 `bookmark-default-file'."
1328 (interactive "P")
1329 (bookmark-maybe-load-default-file)
1330 (cond
1331 ((and (null parg) (null file))
1332 ;;whether interactive or not, write to default file
1333 (bookmark-write-file bookmark-default-file))
1334 ((and (null parg) file)
1335 ;;whether interactive or not, write to given file
1336 (bookmark-write-file file))
1337 ((and parg (not file))
1338 ;;have been called interactively w/ prefix arg
1339 (let ((file (read-file-name "File to save bookmarks in: ")))
1340 (bookmark-write-file file)))
1341 (t ; someone called us with prefix-arg *and* a file, so just write to file
1342 (bookmark-write-file file)))
1343 ;; signal that we have synced the bookmark file by setting this to
1344 ;; 0. If there was an error at any point before, it will not get
1345 ;; set, which is what we want.
1346 (setq bookmark-alist-modification-count 0))
1350 (defun bookmark-write-file (file)
1351 (save-excursion
1352 (save-window-excursion
1353 (if (>= baud-rate 9600)
1354 (message "Saving bookmarks to file %s..." file))
1355 (set-buffer (let ((enable-local-variables nil))
1356 (find-file-noselect file)))
1357 (goto-char (point-min))
1358 (let ((print-length nil)
1359 (print-level nil))
1360 (delete-region (point-min) (point-max))
1361 (bookmark-insert-file-format-version-stamp)
1362 (pp bookmark-alist (current-buffer))
1363 (let ((version-control
1364 (cond
1365 ((null bookmark-version-control) nil)
1366 ((eq 'never bookmark-version-control) 'never)
1367 ((eq 'nospecial bookmark-version-control) version-control)
1369 t))))
1370 (condition-case nil
1371 (write-file file)
1372 (file-error (message "Can't write %s" file)))
1373 (kill-buffer (current-buffer))
1374 (if (>= baud-rate 9600)
1375 (message "Saving bookmarks to file %s...done" file)))))))
1378 (defun bookmark-import-new-list (new-list)
1379 ;; Walk over the new list, adding each individual bookmark
1380 ;; carefully. "Carefully" means checking against the existing
1381 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1382 ;; as necessary.
1383 (let ((lst new-list)
1384 (names (bookmark-all-names)))
1385 (while lst
1386 (let* ((full-record (car lst)))
1387 (bookmark-maybe-rename full-record names)
1388 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1389 (setq names (cons (bookmark-name-from-full-record full-record) names))
1390 (setq lst (cdr lst))))))
1393 (defun bookmark-maybe-rename (full-record names)
1394 ;; just a helper for bookmark-import-new-list; it is only for
1395 ;; readability that this is not inlined.
1397 ;; Once this has found a free name, it sets full-record to that
1398 ;; name.
1399 (let ((found-name (bookmark-name-from-full-record full-record)))
1400 (if (member found-name names)
1401 ;; We've got a conflict, so generate a new name
1402 (let ((count 2)
1403 (new-name found-name))
1404 (while (member new-name names)
1405 (setq new-name (concat found-name (format "<%d>" count)))
1406 (setq count (1+ count)))
1407 (bookmark-set-name full-record new-name)))))
1410 ;;;###autoload
1411 (defun bookmark-load (file &optional overwrite no-msg)
1412 "Load bookmarks from FILE (which must be in bookmark format).
1413 Appends loaded bookmarks to the front of the list of bookmarks. If
1414 optional second argument OVERWRITE is non-nil, existing bookmarks are
1415 destroyed. Optional third arg NO-MSG means don't display any messages
1416 while loading.
1418 If you load a file that doesn't contain a proper bookmark alist, you
1419 will corrupt Emacs's bookmark list. Generally, you should only load
1420 in files that were created with the bookmark functions in the first
1421 place. Your own personal bookmark file, `~/.emacs.bmk', is
1422 maintained automatically by Emacs; you shouldn't need to load it
1423 explicitly.
1425 If you load a file containing bookmarks with the same names as
1426 bookmarks already present in your Emacs, the new bookmarks will get
1427 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1428 method buffers use to resolve name collisions."
1429 (interactive
1430 (list (read-file-name
1431 (format "Load bookmarks from: (%s) "
1432 bookmark-default-file)
1433 ;;Default might not be used often,
1434 ;;but there's no better default, and
1435 ;;I guess it's better than none at all.
1436 "~/" bookmark-default-file 'confirm)))
1437 (setq file (expand-file-name file))
1438 (if (file-readable-p file)
1439 (save-excursion
1440 (save-window-excursion
1441 (if (and (null no-msg) (>= baud-rate 9600))
1442 (message "Loading bookmarks from %s..." file))
1443 (set-buffer (let ((enable-local-variables nil))
1444 (find-file-noselect file)))
1445 (goto-char (point-min))
1446 (bookmark-maybe-upgrade-file-format)
1447 (let ((blist (bookmark-alist-from-buffer)))
1448 (if (listp blist)
1449 (progn
1450 (if overwrite
1451 (progn
1452 (setq bookmark-alist blist)
1453 (setq bookmark-alist-modification-count 0))
1454 ;; else
1455 (bookmark-import-new-list blist)
1456 (setq bookmark-alist-modification-count
1457 (1+ bookmark-alist-modification-count)))
1458 (if (string-equal
1459 (expand-file-name bookmark-default-file)
1460 file)
1461 (setq bookmarks-already-loaded t))
1462 (bookmark-bmenu-surreptitiously-rebuild-list))
1463 (error "Invalid bookmark list in %s" file)))
1464 (kill-buffer (current-buffer)))
1465 (if (and (null no-msg) (>= baud-rate 9600))
1466 (message "Loading bookmarks from %s...done" file)))
1467 (error "Cannot read bookmark file %s" file)))
1471 ;;; Code supporting the dired-like bookmark menu. Prefix is
1472 ;;; "bookmark-bmenu" for "buffer-menu":
1475 (defvar bookmark-bmenu-bookmark-column nil)
1478 (defvar bookmark-bmenu-hidden-bookmarks ())
1481 (defvar bookmark-bmenu-mode-map nil)
1484 (if bookmark-bmenu-mode-map
1486 (setq bookmark-bmenu-mode-map (make-keymap))
1487 (suppress-keymap bookmark-bmenu-mode-map t)
1488 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1489 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1490 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1491 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1492 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1493 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1494 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1495 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1496 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1497 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1498 (define-key bookmark-bmenu-mode-map "\C-o"
1499 'bookmark-bmenu-switch-other-window)
1500 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1501 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1502 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1503 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1504 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1505 (define-key bookmark-bmenu-mode-map " " 'next-line)
1506 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1507 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1508 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1509 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1510 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1511 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1512 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1513 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1514 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1515 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1516 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1517 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1518 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1519 (define-key bookmark-bmenu-mode-map [mouse-2]
1520 'bookmark-bmenu-other-window-with-mouse))
1524 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1525 ;; data.
1526 (put 'bookmark-bmenu-mode 'mode-class 'special)
1529 ;; todo: need to display whether or not bookmark exists as a buffer in
1530 ;; flag column.
1532 ;; Format:
1533 ;; FLAGS BOOKMARK [ LOCATION ]
1536 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1537 "Rebuild the Bookmark List if it exists.
1538 Don't affect the buffer ring order."
1539 (if (get-buffer "*Bookmark List*")
1540 (save-excursion
1541 (save-window-excursion
1542 (bookmark-bmenu-list)))))
1545 ;;;###autoload
1546 (defun bookmark-bmenu-list ()
1547 "Display a list of existing bookmarks.
1548 The list is displayed in a buffer named `*Bookmark List*'.
1549 The leftmost column displays a D if the bookmark is flagged for
1550 deletion, or > if it is flagged for displaying."
1551 (interactive)
1552 (bookmark-maybe-load-default-file)
1553 (if (interactive-p)
1554 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1555 (set-buffer (get-buffer-create "*Bookmark List*")))
1556 (let ((inhibit-read-only t))
1557 (erase-buffer)
1558 (insert "% Bookmark\n- --------\n")
1559 (add-text-properties (point-min) (point)
1560 '(font-lock-face bookmark-menu-heading))
1561 (bookmark-maybe-sort-alist)
1562 (mapcar
1563 (lambda (full-record)
1564 ;; if a bookmark has an annotation, prepend a "*"
1565 ;; in the list of bookmarks.
1566 (let ((annotation (bookmark-get-annotation
1567 (bookmark-name-from-full-record full-record))))
1568 (if (and annotation (not (string-equal annotation "")))
1569 (insert " *")
1570 (insert " "))
1571 (let ((start (point)))
1572 (insert (bookmark-name-from-full-record full-record))
1573 (if (and (display-color-p) (display-mouse-p))
1574 (add-text-properties
1575 start
1576 (save-excursion (re-search-backward
1577 "[^ \t]")
1578 (1+ (point)))
1579 '(mouse-face highlight
1580 follow-link t
1581 help-echo "mouse-2: go to this bookmark in other window")))
1582 (insert "\n")
1584 bookmark-alist))
1585 (goto-char (point-min))
1586 (forward-line 2)
1587 (bookmark-bmenu-mode)
1588 (if bookmark-bmenu-toggle-filenames
1589 (bookmark-bmenu-toggle-filenames t)))
1591 ;;;###autoload
1592 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1593 ;;;###autoload
1594 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1598 (defun bookmark-bmenu-mode ()
1599 "Major mode for editing a list of bookmarks.
1600 Each line describes one of the bookmarks in Emacs.
1601 Letters do not insert themselves; instead, they are commands.
1602 Bookmark names preceded by a \"*\" have annotations.
1603 \\<bookmark-bmenu-mode-map>
1604 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1605 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1606 Also show bookmarks marked using m in other windows.
1607 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1608 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1609 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1610 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1611 together with bookmark selected before this one in another window.
1612 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1613 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1614 so the bookmark menu bookmark remains visible in its window.
1615 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1616 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1617 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1618 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1619 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1620 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1621 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1622 With a prefix arg, prompts for a file to save in.
1623 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1624 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1625 With prefix argument, also move up one line.
1626 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1627 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1628 in another buffer.
1629 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1630 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1631 (kill-all-local-variables)
1632 (use-local-map bookmark-bmenu-mode-map)
1633 (setq truncate-lines t)
1634 (setq buffer-read-only t)
1635 (setq major-mode 'bookmark-bmenu-mode)
1636 (setq mode-name "Bookmark Menu")
1637 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1640 (defun bookmark-bmenu-toggle-filenames (&optional show)
1641 "Toggle whether filenames are shown in the bookmark list.
1642 Optional argument SHOW means show them unconditionally."
1643 (interactive)
1644 (cond
1645 (show
1646 (setq bookmark-bmenu-toggle-filenames nil)
1647 (bookmark-bmenu-show-filenames)
1648 (setq bookmark-bmenu-toggle-filenames t))
1649 (bookmark-bmenu-toggle-filenames
1650 (bookmark-bmenu-hide-filenames)
1651 (setq bookmark-bmenu-toggle-filenames nil))
1653 (bookmark-bmenu-show-filenames)
1654 (setq bookmark-bmenu-toggle-filenames t))))
1657 (defun bookmark-bmenu-show-filenames (&optional force)
1658 (if (and (not force) bookmark-bmenu-toggle-filenames)
1659 nil ;already shown, so do nothing
1660 (save-excursion
1661 (save-window-excursion
1662 (goto-char (point-min))
1663 (forward-line 2)
1664 (setq bookmark-bmenu-hidden-bookmarks ())
1665 (let ((inhibit-read-only t))
1666 (while (< (point) (point-max))
1667 (let ((bmrk (bookmark-bmenu-bookmark)))
1668 (setq bookmark-bmenu-hidden-bookmarks
1669 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1670 (let ((start (save-excursion (end-of-line) (point))))
1671 (move-to-column bookmark-bmenu-file-column t)
1672 ;; Strip off `mouse-face' from the white spaces region.
1673 (if (and (display-color-p) (display-mouse-p))
1674 (remove-text-properties start (point)
1675 '(mouse-face nil help-echo nil))))
1676 (delete-region (point) (progn (end-of-line) (point)))
1677 (insert " ")
1678 ;; Pass the NO-HISTORY arg:
1679 (bookmark-insert-location bmrk t)
1680 (forward-line 1))))))))
1683 (defun bookmark-bmenu-hide-filenames (&optional force)
1684 (if (and (not force) bookmark-bmenu-toggle-filenames)
1685 ;; nothing to hide if above is nil
1686 (save-excursion
1687 (save-window-excursion
1688 (goto-char (point-min))
1689 (forward-line 2)
1690 (setq bookmark-bmenu-hidden-bookmarks
1691 (nreverse bookmark-bmenu-hidden-bookmarks))
1692 (save-excursion
1693 (goto-char (point-min))
1694 (search-forward "Bookmark")
1695 (backward-word 1)
1696 (setq bookmark-bmenu-bookmark-column (current-column)))
1697 (save-excursion
1698 (let ((inhibit-read-only t))
1699 (while bookmark-bmenu-hidden-bookmarks
1700 (move-to-column bookmark-bmenu-bookmark-column t)
1701 (bookmark-kill-line)
1702 (let ((start (point)))
1703 (insert (car bookmark-bmenu-hidden-bookmarks))
1704 (if (and (display-color-p) (display-mouse-p))
1705 (add-text-properties
1706 start
1707 (save-excursion (re-search-backward
1708 "[^ \t]")
1709 (1+ (point)))
1710 '(mouse-face highlight
1711 follow-link t
1712 help-echo
1713 "mouse-2: go to this bookmark in other window"))))
1714 (setq bookmark-bmenu-hidden-bookmarks
1715 (cdr bookmark-bmenu-hidden-bookmarks))
1716 (forward-line 1))))))))
1719 (defun bookmark-bmenu-check-position ()
1720 ;; Returns non-nil if on a line with a bookmark.
1721 ;; (The actual value returned is bookmark-alist).
1722 ;; Else reposition and try again, else return nil.
1723 (cond ((< (count-lines (point-min) (point)) 2)
1724 (goto-char (point-min))
1725 (forward-line 2)
1726 bookmark-alist)
1727 ((and (bolp) (eobp))
1728 (beginning-of-line 0)
1729 bookmark-alist)
1731 bookmark-alist)))
1734 (defun bookmark-bmenu-bookmark ()
1735 ;; return a string which is bookmark of this line.
1736 (if (bookmark-bmenu-check-position)
1737 (save-excursion
1738 (save-window-excursion
1739 (goto-char (point-min))
1740 (search-forward "Bookmark")
1741 (backward-word 1)
1742 (setq bookmark-bmenu-bookmark-column (current-column)))))
1743 (if bookmark-bmenu-toggle-filenames
1744 (bookmark-bmenu-hide-filenames))
1745 (save-excursion
1746 (save-window-excursion
1747 (beginning-of-line)
1748 (forward-char bookmark-bmenu-bookmark-column)
1749 (prog1
1750 (buffer-substring-no-properties (point)
1751 (progn
1752 (end-of-line)
1753 (point)))
1754 ;; well, this is certainly crystal-clear:
1755 (if bookmark-bmenu-toggle-filenames
1756 (bookmark-bmenu-toggle-filenames t))))))
1759 (defun bookmark-show-annotation (bookmark)
1760 "Display the annotation for bookmark named BOOKMARK in a buffer,
1761 if an annotation exists."
1762 (let ((annotation (bookmark-get-annotation bookmark)))
1763 (if (and annotation (not (string-equal annotation "")))
1764 (save-excursion
1765 (let ((old-buf (current-buffer)))
1766 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1767 (delete-region (point-min) (point-max))
1768 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1769 (insert annotation)
1770 (goto-char (point-min))
1771 (pop-to-buffer old-buf))))))
1774 (defun bookmark-show-all-annotations ()
1775 "Display the annotations for all bookmarks in a buffer."
1776 (let ((old-buf (current-buffer)))
1777 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1778 (delete-region (point-min) (point-max))
1779 (mapcar
1780 (lambda (full-record)
1781 (let* ((name (bookmark-name-from-full-record full-record))
1782 (ann (bookmark-get-annotation name)))
1783 (insert (concat name ":\n"))
1784 (if (and ann (not (string-equal ann "")))
1785 ;; insert the annotation, indented by 4 spaces.
1786 (progn
1787 (save-excursion (insert ann))
1788 (while (< (point) (point-max))
1789 (beginning-of-line) ; paranoia
1790 (insert " ")
1791 (forward-line)
1792 (end-of-line))))))
1793 bookmark-alist)
1794 (goto-char (point-min))
1795 (pop-to-buffer old-buf)))
1798 (defun bookmark-bmenu-mark ()
1799 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1800 (interactive)
1801 (beginning-of-line)
1802 (if (bookmark-bmenu-check-position)
1803 (let ((inhibit-read-only t))
1804 (delete-char 1)
1805 (insert ?>)
1806 (forward-line 1)
1807 (bookmark-bmenu-check-position))))
1810 (defun bookmark-bmenu-select ()
1811 "Select this line's bookmark; also display bookmarks marked with `>'.
1812 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1813 (interactive)
1814 (if (bookmark-bmenu-check-position)
1815 (let ((bmrk (bookmark-bmenu-bookmark))
1816 (menu (current-buffer))
1817 (others ())
1818 tem)
1819 (goto-char (point-min))
1820 (while (re-search-forward "^>" nil t)
1821 (setq tem (bookmark-bmenu-bookmark))
1822 (let ((inhibit-read-only t))
1823 (delete-char -1)
1824 (insert ?\s))
1825 (or (string-equal tem bmrk)
1826 (member tem others)
1827 (setq others (cons tem others))))
1828 (setq others (nreverse others)
1829 tem (/ (1- (frame-height)) (1+ (length others))))
1830 (delete-other-windows)
1831 (bookmark-jump bmrk)
1832 (bury-buffer menu)
1833 (if others
1834 (while others
1835 (split-window nil tem)
1836 (other-window 1)
1837 (bookmark-jump (car others))
1838 (setq others (cdr others)))
1839 (other-window 1)))))
1842 (defun bookmark-bmenu-save (parg)
1843 "Save the current list into a bookmark file.
1844 With a prefix arg, prompts for a file to save them in."
1845 (interactive "P")
1846 (save-excursion
1847 (save-window-excursion
1848 (bookmark-save parg))))
1851 (defun bookmark-bmenu-load ()
1852 "Load the bookmark file and rebuild the bookmark menu-buffer."
1853 (interactive)
1854 (if (bookmark-bmenu-check-position)
1855 (save-excursion
1856 (save-window-excursion
1857 ;; This will call `bookmark-bmenu-list'
1858 (call-interactively 'bookmark-load)))))
1861 (defun bookmark-bmenu-1-window ()
1862 "Select this line's bookmark, alone, in full frame."
1863 (interactive)
1864 (if (bookmark-bmenu-check-position)
1865 (progn
1866 (bookmark-jump (bookmark-bmenu-bookmark))
1867 (bury-buffer (other-buffer))
1868 (delete-other-windows))))
1871 (defun bookmark-bmenu-2-window ()
1872 "Select this line's bookmark, with previous buffer in second window."
1873 (interactive)
1874 (if (bookmark-bmenu-check-position)
1875 (let ((bmrk (bookmark-bmenu-bookmark))
1876 (menu (current-buffer))
1877 (pop-up-windows t))
1878 (delete-other-windows)
1879 (switch-to-buffer (other-buffer))
1880 (let* ((pair (bookmark-jump-noselect bmrk))
1881 (buff (car pair))
1882 (pos (cdr pair)))
1883 (pop-to-buffer buff)
1884 (goto-char pos))
1885 (bury-buffer menu))))
1888 (defun bookmark-bmenu-this-window ()
1889 "Select this line's bookmark in this window."
1890 (interactive)
1891 (if (bookmark-bmenu-check-position)
1892 (bookmark-jump (bookmark-bmenu-bookmark))))
1895 (defun bookmark-bmenu-other-window ()
1896 "Select this line's bookmark in other window, leaving bookmark menu visible."
1897 (interactive)
1898 (let ((bookmark (bookmark-bmenu-bookmark)))
1899 (if (bookmark-bmenu-check-position)
1900 (let* ((pair (bookmark-jump-noselect bookmark))
1901 (buff (car pair))
1902 (pos (cdr pair)))
1903 (switch-to-buffer-other-window buff)
1904 (goto-char pos)
1905 (set-window-point (get-buffer-window buff) pos)
1906 (bookmark-show-annotation bookmark)))))
1909 (defun bookmark-bmenu-switch-other-window ()
1910 "Make the other window select this line's bookmark.
1911 The current window remains selected."
1912 (interactive)
1913 (let ((bookmark (bookmark-bmenu-bookmark))
1914 (pop-up-windows t)
1915 same-window-buffer-names
1916 same-window-regexps)
1917 (if (bookmark-bmenu-check-position)
1918 (let* ((pair (bookmark-jump-noselect bookmark))
1919 (buff (car pair))
1920 (pos (cdr pair)))
1921 (display-buffer buff)
1922 (let ((o-buffer (current-buffer)))
1923 ;; save-excursion won't do
1924 (set-buffer buff)
1925 (goto-char pos)
1926 (set-window-point (get-buffer-window buff) pos)
1927 (set-buffer o-buffer))
1928 (bookmark-show-annotation bookmark)))))
1930 (defun bookmark-bmenu-other-window-with-mouse (event)
1931 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1932 (interactive "e")
1933 (save-excursion
1934 (set-buffer (window-buffer (posn-window (event-end event))))
1935 (save-excursion
1936 (goto-char (posn-point (event-end event)))
1937 (bookmark-bmenu-other-window))))
1940 (defun bookmark-bmenu-show-annotation ()
1941 "Show the annotation for the current bookmark in another window."
1942 (interactive)
1943 (let ((bookmark (bookmark-bmenu-bookmark)))
1944 (if (bookmark-bmenu-check-position)
1945 (bookmark-show-annotation bookmark))))
1948 (defun bookmark-bmenu-show-all-annotations ()
1949 "Show the annotation for all bookmarks in another window."
1950 (interactive)
1951 (bookmark-show-all-annotations))
1954 (defun bookmark-bmenu-edit-annotation ()
1955 "Edit the annotation for the current bookmark in another window."
1956 (interactive)
1957 (let ((bookmark (bookmark-bmenu-bookmark)))
1958 (if (bookmark-bmenu-check-position)
1959 (bookmark-edit-annotation bookmark))))
1962 (defun bookmark-bmenu-unmark (&optional backup)
1963 "Cancel all requested operations on bookmark on this line and move down.
1964 Optional BACKUP means move up."
1965 (interactive "P")
1966 (beginning-of-line)
1967 (if (bookmark-bmenu-check-position)
1968 (progn
1969 (let ((inhibit-read-only t))
1970 (delete-char 1)
1971 ;; any flags to reset according to circumstances? How about a
1972 ;; flag indicating whether this bookmark is being visited?
1973 ;; well, we don't have this now, so maybe later.
1974 (insert " "))
1975 (forward-line (if backup -1 1))
1976 (bookmark-bmenu-check-position))))
1979 (defun bookmark-bmenu-backup-unmark ()
1980 "Move up and cancel all requested operations on bookmark on line above."
1981 (interactive)
1982 (forward-line -1)
1983 (if (bookmark-bmenu-check-position)
1984 (progn
1985 (bookmark-bmenu-unmark)
1986 (forward-line -1)
1987 (bookmark-bmenu-check-position))))
1990 (defun bookmark-bmenu-delete ()
1991 "Mark bookmark on this line to be deleted.
1992 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1993 (interactive)
1994 (beginning-of-line)
1995 (if (bookmark-bmenu-check-position)
1996 (let ((inhibit-read-only t))
1997 (delete-char 1)
1998 (insert ?D)
1999 (forward-line 1)
2000 (bookmark-bmenu-check-position))))
2003 (defun bookmark-bmenu-delete-backwards ()
2004 "Mark bookmark on this line to be deleted, then move up one line.
2005 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2006 (interactive)
2007 (bookmark-bmenu-delete)
2008 (forward-line -2)
2009 (if (bookmark-bmenu-check-position)
2010 (forward-line 1))
2011 (bookmark-bmenu-check-position))
2014 (defun bookmark-bmenu-execute-deletions ()
2015 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2016 (interactive)
2017 (message "Deleting bookmarks...")
2018 (let ((hide-em bookmark-bmenu-toggle-filenames)
2019 (o-point (point))
2020 (o-str (save-excursion
2021 (beginning-of-line)
2022 (if (looking-at "^D")
2024 (buffer-substring
2025 (point)
2026 (progn (end-of-line) (point))))))
2027 (o-col (current-column)))
2028 (if hide-em (bookmark-bmenu-hide-filenames))
2029 (setq bookmark-bmenu-toggle-filenames nil)
2030 (goto-char (point-min))
2031 (forward-line 1)
2032 (while (re-search-forward "^D" (point-max) t)
2033 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2034 (bookmark-bmenu-list)
2035 (setq bookmark-bmenu-toggle-filenames hide-em)
2036 (if bookmark-bmenu-toggle-filenames
2037 (bookmark-bmenu-toggle-filenames t))
2038 (if o-str
2039 (progn
2040 (goto-char (point-min))
2041 (search-forward o-str)
2042 (beginning-of-line)
2043 (forward-char o-col))
2044 (goto-char o-point))
2045 (beginning-of-line)
2046 (setq bookmark-alist-modification-count
2047 (1+ bookmark-alist-modification-count))
2048 (if (bookmark-time-to-save-p)
2049 (bookmark-save))
2050 (message "Deleting bookmarks...done")
2054 (defun bookmark-bmenu-rename ()
2055 "Rename bookmark on current line. Prompts for a new name."
2056 (interactive)
2057 (if (bookmark-bmenu-check-position)
2058 (let ((bmrk (bookmark-bmenu-bookmark))
2059 (thispoint (point)))
2060 (bookmark-rename bmrk)
2061 (bookmark-bmenu-list)
2062 (goto-char thispoint))))
2065 (defun bookmark-bmenu-locate ()
2066 "Display location of this bookmark. Displays in the minibuffer."
2067 (interactive)
2068 (if (bookmark-bmenu-check-position)
2069 (let ((bmrk (bookmark-bmenu-bookmark)))
2070 (message "%s" (bookmark-location bmrk)))))
2072 (defun bookmark-bmenu-relocate ()
2073 "Change the file path of the bookmark on the current line,
2074 prompting with completion for the new path."
2075 (interactive)
2076 (if (bookmark-bmenu-check-position)
2077 (let ((bmrk (bookmark-bmenu-bookmark))
2078 (thispoint (point)))
2079 (bookmark-relocate bmrk)
2080 (goto-char thispoint))))
2083 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2085 (defun bookmark-menu-popup-paned-menu (event name entries)
2086 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2087 That is, ENTRIES is a list of strings which appear as the choices
2088 in the menu.
2089 The number of panes depends on the number of entries.
2090 The visible entries are truncated to `bookmark-menu-length', but the
2091 strings returned are not."
2092 (let ((f-height (/ (frame-height) 2))
2093 (pane-list nil)
2094 (iter 0))
2095 (while entries
2096 (let (lst
2097 (count 0))
2098 (while (and (< count f-height) entries)
2099 (let ((str (car entries)))
2100 (push (cons
2101 (if (> (length str) bookmark-menu-length)
2102 (substring str 0 bookmark-menu-length)
2103 str)
2104 str)
2105 lst)
2106 (setq entries (cdr entries))
2107 (setq count (1+ count))))
2108 (setq iter (1+ iter))
2109 (push (cons
2110 (format "-*- %s (%d) -*-" name iter)
2111 (nreverse lst))
2112 pane-list)))
2114 ;; Popup the menu and return the string.
2115 (x-popup-menu event (cons (concat "-*- " name " -*-")
2116 (nreverse pane-list)))))
2119 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2120 ;; following works, and for explaining what to do to make it work.
2122 ;; We MUST autoload EACH form used to set up this variable's value, so
2123 ;; that the whole job is done in loaddefs.el.
2125 ;; Emacs menubar stuff.
2127 ;;;###autoload
2128 (defvar menu-bar-bookmark-map
2129 (let ((map (make-sparse-keymap "Bookmark functions")))
2130 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2131 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2132 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2133 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2134 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2135 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2136 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2137 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2138 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2139 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2140 map))
2142 ;;;###autoload
2143 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2145 ;; make bookmarks appear toward the right side of the menu.
2146 (if (boundp 'menu-bar-final-items)
2147 (if menu-bar-final-items
2148 (setq menu-bar-final-items
2149 (cons 'bookmark menu-bar-final-items)))
2150 (setq menu-bar-final-items '(bookmark)))
2152 ;;;; end bookmark menu stuff ;;;;
2155 ;;; Load Hook
2156 (defvar bookmark-load-hook nil
2157 "Hook run at the end of loading bookmark.")
2159 ;;; Exit Hook, called from kill-emacs-hook
2160 (defvar bookmark-exit-hook nil
2161 "Hook run when Emacs exits.")
2163 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2165 (defun bookmark-exit-hook-internal ()
2166 "Save bookmark state, if necessary, at Emacs exit time.
2167 This also runs `bookmark-exit-hook'."
2168 (run-hooks 'bookmark-exit-hook)
2169 (and bookmark-alist
2170 (bookmark-time-to-save-p t)
2171 (bookmark-save)))
2173 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2176 (run-hooks 'bookmark-load-hook)
2178 (provide 'bookmark)
2180 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2181 ;;; bookmark.el ends here