(bookmark-save-flag, bookmark-version-control): Use
[emacs.git] / lisp / bookmark.el
blob65f117a0411c3ddc66efd84c688d4061264de06f
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))
218 ;; XEmacs apparently call this `buffer-substring-without-properties',
219 ;; sigh.
220 (or (fboundp 'buffer-substring-no-properties)
221 (if (fboundp 'buffer-substring-without-properties)
222 (fset 'buffer-substring-no-properties
223 'buffer-substring-without-properties)
224 (fset 'buffer-substring-no-properties 'buffer-substring)))
227 ;;; Keymap stuff:
229 ;; Set up these bindings dumping time *only*;
230 ;; if the user alters them, don't override the user when loading bookmark.el.
232 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
233 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
234 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
236 ;;;###autoload
237 (defvar bookmark-map nil
238 "Keymap containing bindings to bookmark functions.
239 It is not bound to any key by default: to bind it
240 so that you have a bookmark prefix, just use `global-set-key' and bind a
241 key of your choice to `bookmark-map'. All interactive bookmark
242 functions have a binding in this keymap.")
244 ;;;###autoload
245 (define-prefix-command 'bookmark-map)
247 ;; Read the help on all of these functions for details...
248 ;;;###autoload
249 (define-key bookmark-map "x" 'bookmark-set)
250 ;;;###autoload
251 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
252 ;;;###autoload
253 (define-key bookmark-map "j" 'bookmark-jump)
254 ;;;###autoload
255 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
256 ;;;###autoload
257 (define-key bookmark-map "i" 'bookmark-insert)
258 ;;;###autoload
259 (define-key bookmark-map "e" 'edit-bookmarks)
260 ;;;###autoload
261 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
262 ;;;###autoload
263 (define-key bookmark-map "r" 'bookmark-rename)
264 ;;;###autoload
265 (define-key bookmark-map "d" 'bookmark-delete)
266 ;;;###autoload
267 (define-key bookmark-map "l" 'bookmark-load)
268 ;;;###autoload
269 (define-key bookmark-map "w" 'bookmark-write)
270 ;;;###autoload
271 (define-key bookmark-map "s" 'bookmark-save)
274 ;;; The annotation maps.
275 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
276 "Keymap for composing an annotation for a bookmark.")
278 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
279 'bookmark-send-annotation)
283 ;;; Core variables and data structures:
284 (defvar bookmark-alist ()
285 "Association list of bookmarks and their records.
286 You probably don't want to change the value of this alist yourself;
287 instead, let the various bookmark functions do it for you.
289 The format of the alist is
291 \(BOOKMARK1 BOOKMARK2 ...\)
293 where each BOOKMARK is of the form
295 \(NAME
296 \(filename . FILE\)
297 \(front-context-string . FRONT-STR\)
298 \(rear-context-string . REAR-STR\)
299 \(position . POS\)
300 \(info-node . POS\)
301 \(annotation . ANNOTATION\)\)
303 So the cdr of each bookmark is an alist too.
304 `info-node' is optional, by the way.")
307 (defvar bookmarks-already-loaded nil)
310 ;; just add the hook to make sure that people don't lose bookmarks
311 ;; when they kill Emacs, unless they don't want to save them.
312 ;;;###autoload
313 (add-hook 'kill-emacs-hook
314 (function
315 (lambda () (and (featurep 'bookmark)
316 bookmark-alist
317 (bookmark-time-to-save-p t)
318 (bookmark-save)))))
320 ;; more stuff added by db.
322 (defvar bookmark-current-bookmark nil
323 "Name of bookmark most recently used in the current file.
324 It is buffer local, used to make moving a bookmark forward
325 through a file easier.")
327 (make-variable-buffer-local 'bookmark-current-bookmark)
330 (defvar bookmark-alist-modification-count 0
331 "Number of modifications to bookmark list since it was last saved.")
334 (defvar bookmark-search-size 16
335 "Length of the context strings recorded on either side of a bookmark.")
338 (defvar bookmark-current-point 0)
339 (defvar bookmark-yank-point 0)
340 (defvar bookmark-current-buffer nil)
344 ;; Helper functions.
346 ;; Only functions on this page and the next one (file formats) need to
347 ;; know anything about the format of bookmark-alist entries.
348 ;; Everyone else should go through them.
350 (defun bookmark-name-from-full-record (full-record)
351 "Return name of FULL-RECORD \(an alist element instead of a string\)."
352 (car full-record))
355 (defun bookmark-all-names ()
356 "Return a list of all current bookmark names."
357 (bookmark-maybe-load-default-file)
358 (mapcar
359 (lambda (full-record)
360 (bookmark-name-from-full-record full-record))
361 bookmark-alist))
364 (defun bookmark-get-bookmark (bookmark)
365 "Return the full entry for BOOKMARK in bookmark-alist."
366 (assoc bookmark bookmark-alist))
369 (defun bookmark-get-bookmark-record (bookmark)
370 "Return the guts of the entry for BOOKMARK in bookmark-alist.
371 That is, all information but the name."
372 (car (cdr (bookmark-get-bookmark bookmark))))
375 (defun bookmark-set-name (bookmark newname)
376 "Set BOOKMARK's name to NEWNAME."
377 (setcar
378 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
379 newname))
382 (defun bookmark-get-annotation (bookmark)
383 "Return the annotation of BOOKMARK, or nil if none."
384 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
387 (defun bookmark-set-annotation (bookmark ann)
388 "Set the annotation of BOOKMARK to ANN."
389 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
390 (if cell
391 (setcdr cell ann)
392 (nconc (bookmark-get-bookmark-record bookmark)
393 (list (cons 'annotation ann))))))
396 (defun bookmark-get-filename (bookmark)
397 "Return the full filename of BOOKMARK."
398 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
401 (defun bookmark-set-filename (bookmark filename)
402 "Set the full filename of BOOKMARK to FILENAME."
403 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
404 (if cell
405 (setcdr cell filename)
406 (nconc (bookmark-get-bookmark-record bookmark)
407 (list (cons 'filename filename))))))
410 (defun bookmark-get-position (bookmark)
411 "Return the position \(i.e.: point\) of BOOKMARK."
412 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
415 (defun bookmark-set-position (bookmark position)
416 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
417 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
418 (if cell
419 (setcdr cell position)
420 (nconc (bookmark-get-bookmark-record bookmark)
421 (list (cons 'position position))))))
424 (defun bookmark-get-front-context-string (bookmark)
425 "Return the front-context-string of BOOKMARK."
426 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
429 (defun bookmark-set-front-context-string (bookmark string)
430 "Set the front-context-string of BOOKMARK to STRING."
431 (let ((cell (assq 'front-context-string
432 (bookmark-get-bookmark-record bookmark))))
433 (if cell
434 (setcdr cell string)
435 (nconc (bookmark-get-bookmark-record bookmark)
436 (list (cons 'front-context-string string))))))
439 (defun bookmark-get-rear-context-string (bookmark)
440 "Return the rear-context-string of BOOKMARK."
441 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
444 (defun bookmark-set-rear-context-string (bookmark string)
445 "Set the rear-context-string of BOOKMARK to STRING."
446 (let ((cell (assq 'rear-context-string
447 (bookmark-get-bookmark-record bookmark))))
448 (if cell
449 (setcdr cell string)
450 (nconc (bookmark-get-bookmark-record bookmark)
451 (list (cons 'rear-context-string string))))))
454 (defun bookmark-get-info-node (bookmark)
455 "Get the info node associated with BOOKMARK."
456 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
459 (defun bookmark-set-info-node (bookmark node)
460 "Set the Info node of BOOKMARK to NODE."
461 (let ((cell (assq 'info-node
462 (bookmark-get-bookmark-record bookmark))))
463 (if cell
464 (setcdr cell node)
465 (nconc (bookmark-get-bookmark-record bookmark)
466 (list (cons 'info-node node)))))
468 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
469 (sit-for 4)
473 (defvar bookmark-history nil
474 "The history list for bookmark functions.")
477 (defun bookmark-completing-read (prompt &optional default)
478 "Prompting with PROMPT, read a bookmark name in completion.
479 PROMPT will get a \": \" stuck on the end no matter what, so you
480 probably don't want to include one yourself.
481 Optional second arg DEFAULT is a string to return if the user enters
482 the empty string."
483 (bookmark-maybe-load-default-file) ; paranoia
484 (let* ((completion-ignore-case bookmark-completion-ignore-case)
485 (default default)
486 (prompt (if default
487 (concat prompt (format " (%s): " default))
488 (concat prompt ": ")))
489 (str
490 (completing-read prompt
491 bookmark-alist
495 'bookmark-history)))
496 (if (string-equal "" str)
497 (list default)
498 (list str))))
501 (defmacro bookmark-maybe-historicize-string (string)
502 "Put STRING into the bookmark prompt history, if caller non-interactive.
503 We need this because sometimes bookmark functions are invoked from
504 menus, so `completing-read' never gets a chance to set `bookmark-history'."
505 (` (or
506 (interactive-p)
507 (setq bookmark-history (cons (, string) bookmark-history)))))
510 (defun bookmark-make (name &optional annotation overwrite info-node)
511 "Make a bookmark named NAME.
512 Optional second arg ANNOTATION gives it an annotation.
513 Optional third arg OVERWRITE means replace any existing bookmarks with
514 this name.
515 Optional fourth arg INFO-NODE means this bookmark is at info node
516 INFO-NODE, so record this fact in the bookmark's entry."
517 (bookmark-maybe-load-default-file)
518 (let ((stripped-name (copy-sequence name)))
519 (or bookmark-xemacsp
520 ;; XEmacs's `set-text-properties' doesn't work on
521 ;; free-standing strings, apparently.
522 (set-text-properties 0 (length stripped-name) nil stripped-name))
523 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
524 ;; already existing bookmark under that name and
525 ;; no prefix arg means just overwrite old bookmark
526 (setcdr (bookmark-get-bookmark stripped-name)
527 (list (bookmark-make-cell annotation info-node)))
529 ;; otherwise just cons it onto the front (either the bookmark
530 ;; doesn't exist already, or there is no prefix arg. In either
531 ;; case, we want the new bookmark consed onto the alist...)
533 (setq bookmark-alist
534 (cons
535 (list stripped-name
536 (bookmark-make-cell annotation info-node))
537 bookmark-alist)))
539 ;; Added by db
540 (setq bookmark-current-bookmark stripped-name)
541 (setq bookmark-alist-modification-count
542 (1+ bookmark-alist-modification-count))
543 (if (bookmark-time-to-save-p)
544 (bookmark-save))))
547 (defun bookmark-make-cell (annotation &optional info-node)
548 "Return the record part of a new bookmark, given ANNOTATION.
549 Must be at the correct position in the buffer in which the bookmark is
550 being set. This might change someday.
551 Optional second arg INFO-NODE means this bookmark is at info node
552 INFO-NODE, so record this fact in the bookmark's entry."
553 (let ((the-record
554 (` ((filename . (, (bookmark-buffer-file-name)))
555 (front-context-string
556 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
557 (buffer-substring-no-properties
558 (point)
559 (+ (point) bookmark-search-size))
560 nil)))
561 (rear-context-string
562 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
563 (buffer-substring-no-properties
564 (point)
565 (- (point) bookmark-search-size))
566 nil)))
567 (position . (, (point)))
568 ))))
570 ;; Now fill in the optional parts:
572 ;; Take no chances with text properties
573 (set-text-properties 0 (length annotation) nil annotation)
574 (set-text-properties 0 (length info-node) nil info-node)
576 (if annotation
577 (nconc the-record (list (cons 'annotation annotation))))
578 (if info-node
579 (nconc the-record (list (cons 'info-node info-node))))
581 ;; Finally, return the completed record.
582 the-record))
586 ;;; File format stuff
588 ;; The OLD format of the bookmark-alist was:
590 ;; ((bookmark-name (filename
591 ;; string-in-front
592 ;; string-behind
593 ;; point))
594 ;; ...)
596 ;; The NEW format of the bookmark-alist is:
598 ;; ((bookmark-name ((filename . FILENAME)
599 ;; (front-context-string . string-in-front)
600 ;; (rear-context-string . string-behind)
601 ;; (position . POINT)
602 ;; (annotation . annotation)
603 ;; (whatever . VALUE)
604 ;; ...
605 ;; ))
606 ;; ...)
609 ;; I switched to using an internal as well as external alist because I
610 ;; felt that would be a more flexible framework in which to add
611 ;; features. It means that the order in which values appear doesn't
612 ;; matter, and it means that arbitrary values can be added without
613 ;; risk of interfering with existing ones.
615 ;; BOOKMARK-NAME is the string the user gives the bookmark and
616 ;; accesses it by from then on.
618 ;; FILENAME is the location of the file in which the bookmark is set.
620 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
621 ;; context in front of the point at which the bookmark is set.
623 ;; STRING-BEHIND is the same thing, but after the point.
625 ;; The context strings exist so that modifications to a file don't
626 ;; necessarily cause a bookmark's position to be invalidated.
627 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
628 ;; case the file has changed since the bookmark was set. It will
629 ;; attempt to place the user before the changes, if there were any.
630 ;; ANNOTATION is the annotation for the bookmark; it may not exist
631 ;; (for backward compatibility), be nil (no annotation), or be a
632 ;; string.
635 (defconst bookmark-file-format-version 1
636 "The current version of the format used by bookmark files.
637 You should never need to change this.")
640 (defconst bookmark-end-of-version-stamp-marker
641 "-*- End Of Bookmark File Format Version Stamp -*-\n"
642 "This string marks the end of the version stamp in a bookmark file.")
645 (defun bookmark-alist-from-buffer ()
646 "Return a bookmark-alist (in any format) from the current buffer.
647 The buffer must of course contain bookmark format information.
648 Does not care from where in the buffer it is called, and does not
649 affect point."
650 (save-excursion
651 (goto-char (point-min))
652 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
653 (read (current-buffer))
654 ;; Else we're dealing with format version 0
655 (if (search-forward "(" nil t)
656 (progn
657 (forward-char -1)
658 (read (current-buffer)))
659 ;; Else no hope of getting information here.
660 (error "Not bookmark format")))))
663 (defun bookmark-upgrade-version-0-alist (old-list)
664 "Upgrade a version 0 alist OLD-LIST to the current version."
665 (mapcar
666 (lambda (bookmark)
667 (let* ((name (car bookmark))
668 (record (car (cdr bookmark)))
669 (filename (nth 0 record))
670 (front-str (nth 1 record))
671 (rear-str (nth 2 record))
672 (position (nth 3 record))
673 (ann (nth 4 record)))
674 (list
675 name
676 (` ((filename . (, filename))
677 (front-context-string . (, (or front-str "")))
678 (rear-context-string . (, (or rear-str "")))
679 (position . (, position))
680 (annotation . (, ann)))))))
681 old-list))
684 (defun bookmark-upgrade-file-format-from-0 ()
685 "Upgrade a bookmark file of format 0 (the original format) to format 1.
686 This expects to be called from point-min in a bookmark file."
687 (message "Upgrading bookmark format from 0 to %d..."
688 bookmark-file-format-version)
689 (let* ((old-list (bookmark-alist-from-buffer))
690 (new-list (bookmark-upgrade-version-0-alist old-list)))
691 (delete-region (point-min) (point-max))
692 (bookmark-insert-file-format-version-stamp)
693 (pp new-list (current-buffer))
694 (save-buffer))
695 (goto-char (point-min))
696 (message "Upgrading bookmark format from 0 to %d...done"
697 bookmark-file-format-version)
701 (defun bookmark-grok-file-format-version ()
702 "Return an integer which is the file-format version of this bookmark file.
703 This expects to be called from point-min in a bookmark file."
704 (if (looking-at "^;;;;")
705 (save-excursion
706 (save-match-data
707 (re-search-forward "[0-9]")
708 (forward-char -1)
709 (read (current-buffer))))
710 ;; Else this is format version 0, the original one, which didn't
711 ;; even have version stamps.
715 (defun bookmark-maybe-upgrade-file-format ()
716 "Check the file-format version of this bookmark file.
717 If the version is not up-to-date, upgrade it automatically.
718 This expects to be called from point-min in a bookmark file."
719 (let ((version (bookmark-grok-file-format-version)))
720 (cond
721 ((= version bookmark-file-format-version)
722 ) ; home free -- version is current
723 ((= version 0)
724 (bookmark-upgrade-file-format-from-0))
726 (error "Bookmark file format version strangeness")))))
729 (defun bookmark-insert-file-format-version-stamp ()
730 "Insert text indicating current version of bookmark file format."
731 (insert
732 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
733 bookmark-file-format-version))
734 (insert ";;; This format is meant to be slightly human-readable;\n"
735 ";;; nevertheless, you probably don't want to edit it.\n"
736 ";;; "
737 bookmark-end-of-version-stamp-marker))
740 ;;; end file-format stuff
743 ;;; Core code:
745 ;;;###autoload
746 (defun bookmark-set (&optional name parg)
747 "Set a bookmark named NAME inside a file.
748 If name is nil, then the user will be prompted.
749 With prefix arg, will not overwrite a bookmark that has the same name
750 as NAME if such a bookmark already exists, but instead will \"push\"
751 the new bookmark onto the bookmark alist. Thus the most recently set
752 bookmark with name NAME would be the one in effect at any given time,
753 but the others are still there, should you decide to delete the most
754 recent one.
756 To yank words from the text of the buffer and use them as part of the
757 bookmark name, type C-w while setting a bookmark. Successive C-w's
758 yank successive words.
760 Typing C-u inserts the name of the last bookmark used in the buffer
761 \(as an aid in using a single bookmark name to track your progress
762 through a large file\). If no bookmark was used, then C-u inserts the
763 name of the file being visited.
765 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
766 and it removes only the first instance of a bookmark with that name from
767 the list of bookmarks.\)"
768 (interactive (list nil current-prefix-arg))
770 (bookmark-buffer-file-name)
771 (error "Buffer not visiting a file or directory"))
773 (bookmark-maybe-load-default-file)
775 (setq bookmark-current-point (point))
776 (setq bookmark-yank-point (point))
777 (setq bookmark-current-buffer (current-buffer))
779 (let* ((default (or bookmark-current-bookmark
780 (bookmark-buffer-name)))
781 (str
782 (or name
783 (read-from-minibuffer
784 (format "Set bookmark (%s): " default)
786 (let ((now-map (copy-keymap minibuffer-local-map)))
787 (define-key now-map "\C-w" 'bookmark-yank-word)
788 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
789 now-map))))
790 (annotation nil))
791 (and (string-equal str "") (setq str default))
792 ;; Ask for an annotation buffer for this bookmark
793 (if bookmark-use-annotations
794 (bookmark-read-annotation parg str)
795 (bookmark-make str annotation parg (bookmark-info-current-node))
796 (setq bookmark-current-bookmark str)
797 (bookmark-bmenu-surreptitiously-rebuild-list)
798 (goto-char bookmark-current-point))))
801 (defun bookmark-info-current-node ()
802 "If in Info-mode, return current node name (a string), else nil."
803 (if (eq major-mode 'Info-mode)
804 Info-current-node))
807 (defun bookmark-kill-line (&optional newline-too)
808 "Kill from point to end of line.
809 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
810 Does not affect the kill-ring."
811 (let ((eol (save-excursion (end-of-line) (point))))
812 (delete-region (point) eol)
813 (if (and newline-too (looking-at "\n"))
814 (delete-char 1))))
817 ;; Defvars to avoid compilation warnings:
818 (defvar bookmark-annotation-paragraph nil)
819 (defvar bookmark-annotation-name nil)
820 (defvar bookmark-annotation-buffer nil)
821 (defvar bookmark-annotation-file nil)
822 (defvar bookmark-annotation-point nil)
825 (defun bookmark-send-annotation ()
826 "Use buffer contents as the annotation for a bookmark.
827 Exclude lines that begin with `#'.
828 Store the annotation text in the bookmark list with
829 the bookmark (and file, and point) specified in buffer local variables."
830 (interactive)
831 (if (not (eq major-mode 'bookmark-read-annotation-mode))
832 (error "Not in bookmark-read-annotation-mode"))
833 (goto-char (point-min))
834 (while (< (point) (point-max))
835 (if (looking-at "^#")
836 (bookmark-kill-line t)
837 (forward-line 1)))
838 (let ((annotation (buffer-string))
839 (parg bookmark-annotation-paragraph)
840 (bookmark bookmark-annotation-name)
841 (pt bookmark-annotation-point)
842 (buf bookmark-annotation-buffer))
843 ;; for bookmark-make-cell to work, we need to be
844 ;; in the relevant buffer, at the relevant point.
845 ;; Actually, bookmark-make-cell should probably be re-written,
846 ;; to avoid this need. Should I handle the error if a buffer is
847 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
848 (save-excursion
849 (pop-to-buffer buf)
850 (goto-char pt)
851 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
852 (setq bookmark-current-bookmark bookmark))
853 (bookmark-bmenu-surreptitiously-rebuild-list)
854 (goto-char bookmark-current-point))
855 (kill-buffer (current-buffer)))
858 (defun bookmark-default-annotation-text (bookmark)
859 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
860 "# All lines which start with a '#' will be deleted.\n"
861 "# Type C-c C-c when done.\n#\n"
862 "# Author: " (user-full-name) " <" (user-login-name) "@"
863 (system-name) ">\n"
864 "# Date: " (current-time-string) "\n"))
867 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
868 "Function to return default text to use for a bookmark annotation.
869 It takes the name of the bookmark, as a string, as an arg.")
871 (defun bookmark-read-annotation-mode (buf point parg bookmark)
872 "Mode for composing annotations for a bookmark.
873 Wants BUF POINT PARG and BOOKMARK.
874 When you have finished composing, type \\[bookmark-send-annotation] to send
875 the annotation.
877 \\{bookmark-read-annotation-mode-map}
879 (interactive)
880 (kill-all-local-variables)
881 (make-local-variable 'bookmark-annotation-paragraph)
882 (make-local-variable 'bookmark-annotation-name)
883 (make-local-variable 'bookmark-annotation-buffer)
884 (make-local-variable 'bookmark-annotation-file)
885 (make-local-variable 'bookmark-annotation-point)
886 (setq bookmark-annotation-paragraph parg)
887 (setq bookmark-annotation-name bookmark)
888 (setq bookmark-annotation-buffer buf)
889 (setq bookmark-annotation-file (buffer-file-name buf))
890 (setq bookmark-annotation-point point)
891 (use-local-map bookmark-read-annotation-mode-map)
892 (setq major-mode 'bookmark-read-annotation-mode)
893 (insert (funcall bookmark-read-annotation-text-func bookmark))
894 (run-hooks 'text-mode-hook))
897 (defun bookmark-read-annotation (parg bookmark)
898 "Pop up a buffer for entering a bookmark annotation.
899 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
900 (let ((buf (current-buffer))
901 (point (point)))
902 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
903 (bookmark-read-annotation-mode buf point parg bookmark)))
906 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
907 "Keymap for editing an annotation of a bookmark.")
910 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
911 'bookmark-send-edited-annotation)
914 (defun bookmark-edit-annotation-mode (bookmark)
915 "Mode for editing the annotation of bookmark BOOKMARK.
916 When you have finished composing, type \\[bookmark-send-annotation].
918 \\{bookmark-edit-annotation-mode-map}
920 (interactive)
921 (kill-all-local-variables)
922 (make-local-variable 'bookmark-annotation-name)
923 (setq bookmark-annotation-name bookmark)
924 (use-local-map bookmark-edit-annotation-mode-map)
925 (setq major-mode 'bookmark-edit-annotation-mode)
926 (insert (funcall bookmark-read-annotation-text-func bookmark))
927 (let ((annotation (bookmark-get-annotation bookmark)))
928 (if (and annotation (not (string-equal annotation "")))
929 (insert annotation)))
930 (run-hooks 'text-mode-hook))
933 (defun bookmark-send-edited-annotation ()
934 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
935 (interactive)
936 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
937 (error "Not in bookmark-edit-annotation-mode"))
938 (goto-char (point-min))
939 (while (< (point) (point-max))
940 (if (looking-at "^#")
941 (bookmark-kill-line t)
942 (forward-line 1)))
943 (let ((annotation (buffer-string))
944 (bookmark bookmark-annotation-name))
945 (bookmark-set-annotation bookmark annotation)
946 (bookmark-bmenu-surreptitiously-rebuild-list)
947 (goto-char bookmark-current-point))
948 (kill-buffer (current-buffer)))
951 (defun bookmark-edit-annotation (bookmark)
952 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
953 (let ((buf (current-buffer))
954 (point (point)))
955 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
956 (bookmark-edit-annotation-mode bookmark)))
959 (defun bookmark-insert-current-bookmark ()
960 "Insert this buffer's value of bookmark-current-bookmark.
961 Default to file name if it's nil."
962 (interactive)
963 (let ((str
964 (save-excursion
965 (set-buffer bookmark-current-buffer)
966 bookmark-current-bookmark)))
967 (if str (insert str) (bookmark-insert-buffer-name))))
970 (defun bookmark-insert-buffer-name ()
971 "Insert the current file name into the bookmark name being set.
972 The directory part of the file name is not used."
973 (interactive)
974 (let ((str
975 (save-excursion
976 (set-buffer bookmark-current-buffer)
977 (bookmark-buffer-name))))
978 (insert str)))
981 (defun bookmark-buffer-name ()
982 "Return the name of the current buffer's file, non-directory.
983 In Info, return the current node."
984 (cond
985 ;; Are we in Info?
986 ((string-equal mode-name "Info") Info-current-node)
987 ;; Or are we a file?
988 (buffer-file-name (file-name-nondirectory buffer-file-name))
989 ;; Or are we a directory?
990 ((and (boundp 'dired-directory) dired-directory)
991 (let* ((dirname (if (stringp dired-directory)
992 dired-directory
993 (car dired-directory)))
994 (idx (1- (length dirname))))
995 ;; Strip the trailing slash.
996 (if (= ?/ (aref dirname idx))
997 (file-name-nondirectory (substring dirname 0 idx))
998 ;; Else return the current-buffer
999 (buffer-name (current-buffer)))))
1000 ;; If all else fails, use the buffer's name.
1002 (buffer-name (current-buffer)))))
1005 (defun bookmark-yank-word ()
1006 (interactive)
1007 ;; get the next word from the buffer and append it to the name of
1008 ;; the bookmark currently being set.
1009 (let ((string (save-excursion
1010 (set-buffer bookmark-current-buffer)
1011 (goto-char bookmark-yank-point)
1012 (buffer-substring-no-properties
1013 (point)
1014 (progn
1015 (forward-word 1)
1016 (setq bookmark-yank-point (point)))))))
1017 (insert string)))
1020 (defun bookmark-buffer-file-name ()
1021 "Return the current buffer's file in a way useful for bookmarks.
1022 For example, if this is a Info buffer, return the Info file's name."
1023 (if (eq major-mode 'Info-mode)
1024 Info-current-file
1026 buffer-file-name
1027 (if (and (boundp 'dired-directory) dired-directory)
1028 (if (stringp dired-directory)
1029 dired-directory
1030 (car dired-directory))))))
1033 (defun bookmark-maybe-load-default-file ()
1034 (and (not bookmarks-already-loaded)
1035 (null bookmark-alist)
1036 (prog2
1037 (and
1038 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1039 ;; to be renamed.
1040 (file-exists-p (expand-file-name bookmark-old-default-file))
1041 (not (file-exists-p (expand-file-name bookmark-default-file)))
1042 (rename-file (expand-file-name bookmark-old-default-file)
1043 (expand-file-name bookmark-default-file)))
1044 ;; return t so the `and' will continue...
1047 (file-readable-p (expand-file-name bookmark-default-file))
1048 (bookmark-load bookmark-default-file t t)
1049 (setq bookmarks-already-loaded t)))
1052 (defun bookmark-maybe-sort-alist ()
1053 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1054 ;;is non-nil, then return a sorted copy of the alist.
1055 (if bookmark-sort-flag
1056 (setq bookmark-alist
1057 (sort (copy-alist bookmark-alist)
1058 (function
1059 (lambda (x y) (string-lessp (car x) (car y))))))))
1062 ;;;###autoload
1063 (defun bookmark-jump (bookmark)
1064 "Jump to bookmark BOOKMARK (a point in some file).
1065 You may have a problem using this function if the value of variable
1066 `bookmark-alist' is nil. If that happens, you need to load in some
1067 bookmarks. See help on function `bookmark-load' for more about
1068 this.
1070 If the file pointed to by BOOKMARK no longer exists, you will be asked
1071 if you wish to give the bookmark a new location, and bookmark-jump
1072 will then jump to the new location, as well as recording it in place
1073 of the old one in the permanent bookmark record."
1074 (interactive
1075 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1076 (bookmark-maybe-historicize-string bookmark)
1077 (let ((cell (bookmark-jump-noselect bookmark)))
1078 (and cell
1079 (switch-to-buffer (car cell))
1080 (goto-char (cdr cell))
1081 (if bookmark-automatically-show-annotations
1082 ;; if there is an annotation for this bookmark,
1083 ;; show it in a buffer.
1084 (bookmark-show-annotation bookmark)))))
1087 (defun bookmark-jump-noselect (str)
1088 ;; a leetle helper for bookmark-jump :-)
1089 ;; returns (BUFFER . POINT)
1090 (bookmark-maybe-load-default-file)
1091 (let* ((file (expand-file-name (bookmark-get-filename str)))
1092 (forward-str (bookmark-get-front-context-string str))
1093 (behind-str (bookmark-get-rear-context-string str))
1094 (place (bookmark-get-position str))
1095 (info-node (bookmark-get-info-node str))
1096 (orig-file file)
1098 (if (or
1099 (file-exists-p file)
1100 ;; else try some common compression extensions
1101 ;; and Emacs better handle it right!
1102 ;; Sigh: I think it may *not* be handled at the moment. What
1103 ;; to do about this?
1104 (setq file
1106 (let ((altname (concat file ".Z")))
1107 (and (file-exists-p altname)
1108 altname))
1109 (let ((altname (concat file ".gz")))
1110 (and (file-exists-p altname)
1111 altname))
1112 (let ((altname (concat file ".z")))
1113 (and (file-exists-p altname)
1114 altname)))))
1115 (save-excursion
1116 (save-window-excursion
1117 (if info-node
1118 ;; Info nodes must be visited with care.
1119 (progn
1120 (require 'info)
1121 (Info-find-node file info-node))
1122 ;; Else no Info. Can do an ordinary find-file:
1123 (set-buffer (find-file-noselect file))
1124 (goto-char place))
1126 ;; Go searching forward first. Then, if forward-str exists and
1127 ;; was found in the file, we can search backward for behind-str.
1128 ;; Rationale is that if text was inserted between the two in the
1129 ;; file, it's better to be put before it so you can read it,
1130 ;; rather than after and remain perhaps unaware of the changes.
1131 (if forward-str
1132 (if (search-forward forward-str (point-max) t)
1133 (goto-char (match-beginning 0))))
1134 (if behind-str
1135 (if (search-backward behind-str (point-min) t)
1136 (goto-char (match-end 0))))
1137 ;; added by db
1138 (setq bookmark-current-bookmark str)
1139 (cons (current-buffer) (point))))
1141 ;; Else unable to find the marked file, so ask if user wants to
1142 ;; relocate the bookmark, else remind them to consider deletion.
1143 (ding)
1144 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1145 " nonexistent. Relocate \""
1147 "\"? "))
1148 (progn
1149 (bookmark-relocate str)
1150 ;; gasp! It's a recursive function call in Emacs Lisp!
1151 (bookmark-jump-noselect str))
1152 (message
1153 "Bookmark not relocated; consider removing it \(%s\)." str)
1154 nil))))
1157 ;;;###autoload
1158 (defun bookmark-relocate (bookmark)
1159 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1160 This makes an already existing bookmark point to that file, instead of
1161 the one it used to point at. Useful when a file has been renamed
1162 after a bookmark was set in it."
1163 (interactive (bookmark-completing-read "Bookmark to relocate"))
1164 (bookmark-maybe-historicize-string bookmark)
1165 (bookmark-maybe-load-default-file)
1166 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1167 (newloc (expand-file-name
1168 (read-file-name
1169 (format "Relocate %s to: " bookmark)
1170 (file-name-directory bmrk-filename)))))
1171 (bookmark-set-filename bookmark newloc)
1172 (bookmark-bmenu-surreptitiously-rebuild-list)))
1175 ;;;###autoload
1176 (defun bookmark-insert-location (bookmark &optional no-history)
1177 "Insert the name of the file associated with BOOKMARK.
1178 Optional second arg NO-HISTORY means don't record this in the
1179 minibuffer history list `bookmark-history'."
1180 (interactive (bookmark-completing-read "Insert bookmark location"))
1181 (or no-history (bookmark-maybe-historicize-string bookmark))
1182 (let ((start (point)))
1183 (prog1
1184 (insert (bookmark-location bookmark)) ; *Return this line*
1185 (if window-system
1186 (put-text-property start
1187 (save-excursion (re-search-backward
1188 "[^ \t]")
1189 (1+ (point)))
1190 'mouse-face 'highlight)))))
1192 ;;;###autoload
1193 (defalias 'bookmark-locate 'bookmark-insert-location)
1195 (defun bookmark-location (bookmark)
1196 "Return the name of the file associated with BOOKMARK."
1197 (bookmark-maybe-load-default-file)
1198 (bookmark-get-filename bookmark))
1201 ;;;###autoload
1202 (defun bookmark-rename (old &optional new)
1203 "Change the name of OLD bookmark to NEW name.
1204 If called from keyboard, prompt for OLD and NEW. If called from
1205 menubar, select OLD from a menu and prompt for NEW.
1207 If called from Lisp, prompt for NEW if only OLD was passed as an
1208 argument. If called with two strings, then no prompting is done. You
1209 must pass at least OLD when calling from Lisp.
1211 While you are entering the new name, consecutive C-w's insert
1212 consecutive words from the text of the buffer into the new bookmark
1213 name."
1214 (interactive (bookmark-completing-read "Old bookmark name"))
1215 (bookmark-maybe-historicize-string old)
1216 (bookmark-maybe-load-default-file)
1218 (setq bookmark-current-point (point))
1219 (setq bookmark-yank-point (point))
1220 (setq bookmark-current-buffer (current-buffer))
1221 (let ((newname
1222 (or new ; use second arg, if non-nil
1223 (read-from-minibuffer
1224 "New name: "
1226 (let ((now-map (copy-keymap minibuffer-local-map)))
1227 (define-key now-map "\C-w" 'bookmark-yank-word)
1228 now-map)
1230 'bookmark-history))))
1231 (bookmark-set-name old newname)
1232 (setq bookmark-current-bookmark newname)
1233 (bookmark-bmenu-surreptitiously-rebuild-list)
1234 (setq bookmark-alist-modification-count
1235 (1+ bookmark-alist-modification-count))
1236 (if (bookmark-time-to-save-p)
1237 (bookmark-save))))
1240 ;;;###autoload
1241 (defun bookmark-insert (bookmark)
1242 "Insert the text of the file pointed to by bookmark BOOKMARK.
1243 You may have a problem using this function if the value of variable
1244 `bookmark-alist' is nil. If that happens, you need to load in some
1245 bookmarks. See help on function `bookmark-load' for more about
1246 this."
1247 (interactive (bookmark-completing-read "Insert bookmark contents"))
1248 (bookmark-maybe-historicize-string bookmark)
1249 (bookmark-maybe-load-default-file)
1250 (let ((orig-point (point))
1251 (str-to-insert
1252 (save-excursion
1253 (set-buffer (car (bookmark-jump-noselect bookmark)))
1254 (buffer-string))))
1255 (insert str-to-insert)
1256 (push-mark)
1257 (goto-char orig-point)))
1260 ;;;###autoload
1261 (defun bookmark-delete (bookmark &optional batch)
1262 "Delete BOOKMARK from the bookmark list.
1263 Removes only the first instance of a bookmark with that name. If
1264 there are one or more other bookmarks with the same name, they will
1265 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1266 one most recently used in this file, if any\).
1267 Optional second arg BATCH means don't update the bookmark list buffer,
1268 probably because we were called from there."
1269 (interactive
1270 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1271 (bookmark-maybe-historicize-string bookmark)
1272 (bookmark-maybe-load-default-file)
1273 (let ((will-go (bookmark-get-bookmark bookmark)))
1274 (setq bookmark-alist (delq will-go bookmark-alist))
1275 ;; Added by db, nil bookmark-current-bookmark if the last
1276 ;; occurrence has been deleted
1277 (or (bookmark-get-bookmark bookmark-current-bookmark)
1278 (setq bookmark-current-bookmark nil)))
1279 ;; Don't rebuild the list
1280 (if batch
1282 (bookmark-bmenu-surreptitiously-rebuild-list)
1283 (setq bookmark-alist-modification-count
1284 (1+ bookmark-alist-modification-count))
1285 (if (bookmark-time-to-save-p)
1286 (bookmark-save))))
1289 (defun bookmark-time-to-save-p (&optional last-time)
1290 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1291 ;; finds out whether it's time to save bookmarks to a file, by
1292 ;; examining the value of variable bookmark-save-flag, and maybe
1293 ;; bookmark-alist-modification-count. Returns t if they should be
1294 ;; saved, nil otherwise. if last-time is non-nil, then this is
1295 ;; being called when emacs is killed.
1296 (cond (last-time
1297 (and (> bookmark-alist-modification-count 0)
1298 bookmark-save-flag))
1299 ((numberp bookmark-save-flag)
1300 (>= bookmark-alist-modification-count bookmark-save-flag))
1302 nil)))
1305 ;;;###autoload
1306 (defun bookmark-write ()
1307 "Write bookmarks to a file (reading the file name with the minibuffer).
1308 Don't use this in Lisp programs; use `bookmark-save' instead."
1309 (interactive)
1310 (bookmark-maybe-load-default-file)
1311 (bookmark-save t))
1314 ;;;###autoload
1315 (defun bookmark-save (&optional parg file)
1316 "Save currently defined bookmarks.
1317 Saves by default in the file defined by the variable
1318 `bookmark-default-file'. With a prefix arg, save it in file FILE
1319 \(second argument\).
1321 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1322 and FILE, and if you just want it to write to the default file, then
1323 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1324 instead. If you pass in one argument, and it is non-nil, then the
1325 user will be interactively queried for a file to save in.
1327 When you want to load in the bookmarks from a file, use
1328 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1329 for a file, defaulting to the file defined by variable
1330 `bookmark-default-file'."
1331 (interactive "P")
1332 (bookmark-maybe-load-default-file)
1333 (cond
1334 ((and (null parg) (null file))
1335 ;;whether interactive or not, write to default file
1336 (bookmark-write-file bookmark-default-file))
1337 ((and (null parg) file)
1338 ;;whether interactive or not, write to given file
1339 (bookmark-write-file file))
1340 ((and parg (not file))
1341 ;;have been called interactively w/ prefix arg
1342 (let ((file (read-file-name "File to save bookmarks in: ")))
1343 (bookmark-write-file file)))
1344 (t ; someone called us with prefix-arg *and* a file, so just write to file
1345 (bookmark-write-file file)))
1346 ;; signal that we have synced the bookmark file by setting this to
1347 ;; 0. If there was an error at any point before, it will not get
1348 ;; set, which is what we want.
1349 (setq bookmark-alist-modification-count 0))
1353 (defun bookmark-write-file (file)
1354 (save-excursion
1355 (save-window-excursion
1356 (if (>= baud-rate 9600)
1357 (message "Saving bookmarks to file %s..." file))
1358 (set-buffer (let ((enable-local-variables nil))
1359 (find-file-noselect file)))
1360 (goto-char (point-min))
1361 (delete-region (point-min) (point-max))
1362 (bookmark-insert-file-format-version-stamp)
1363 (pp bookmark-alist (current-buffer))
1364 (let ((version-control
1365 (cond
1366 ((null bookmark-version-control) nil)
1367 ((eq 'never bookmark-version-control) 'never)
1368 ((eq 'nospecial bookmark-version-control) version-control)
1370 t))))
1371 (write-file file)
1372 (kill-buffer (current-buffer))
1373 (if (>= baud-rate 9600)
1374 (message "Saving bookmarks to file %s...done" file))
1375 ))))
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 "o" 'bookmark-bmenu-other-window)
1497 (define-key bookmark-bmenu-mode-map "\C-o"
1498 'bookmark-bmenu-switch-other-window)
1499 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1500 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1501 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1502 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1503 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1504 (define-key bookmark-bmenu-mode-map " " 'next-line)
1505 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1506 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1507 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1508 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1509 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1510 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1511 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1512 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1513 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1514 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1515 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1516 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1517 (define-key bookmark-bmenu-mode-map [mouse-2]
1518 'bookmark-bmenu-other-window-with-mouse))
1522 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1523 ;; data.
1524 (put 'bookmark-bmenu-mode 'mode-class 'special)
1527 ;; todo: need to display whether or not bookmark exists as a buffer in
1528 ;; flag column.
1530 ;; Format:
1531 ;; FLAGS BOOKMARK [ LOCATION ]
1534 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1535 "Rebuild the Bookmark List if it exists.
1536 Don't affect the buffer ring order."
1537 (if (get-buffer "*Bookmark List*")
1538 (save-excursion
1539 (save-window-excursion
1540 (bookmark-bmenu-list)))))
1543 ;;;###autoload
1544 (defun bookmark-bmenu-list ()
1545 "Display a list of existing bookmarks.
1546 The list is displayed in a buffer named `*Bookmark List*'.
1547 The leftmost column displays a D if the bookmark is flagged for
1548 deletion, or > if it is flagged for displaying."
1549 (interactive)
1550 (bookmark-maybe-load-default-file)
1551 (if (interactive-p)
1552 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1553 (set-buffer (get-buffer-create "*Bookmark List*")))
1554 (let ((buffer-read-only nil))
1555 (delete-region (point-max) (point-min))
1556 (goto-char (point-min)) ;sure are playing it safe...
1557 (insert "% Bookmark\n- --------\n")
1558 (bookmark-maybe-sort-alist)
1559 (mapcar
1560 (lambda (full-record)
1561 ;; if a bookmark has an annotation, prepend a "*"
1562 ;; in the list of bookmarks.
1563 (let ((annotation (bookmark-get-annotation
1564 (bookmark-name-from-full-record full-record))))
1565 (if (and annotation (not (string-equal annotation "")))
1566 (insert " *")
1567 (insert " "))
1568 (let ((start (point)))
1569 (insert (bookmark-name-from-full-record full-record))
1570 (if window-system
1571 (put-text-property start
1572 (save-excursion (re-search-backward
1573 "[^ \t]")
1574 (1+ (point)))
1575 'mouse-face 'highlight))
1576 (insert "\n")
1578 bookmark-alist))
1579 (goto-char (point-min))
1580 (forward-line 2)
1581 (bookmark-bmenu-mode)
1582 (if bookmark-bmenu-toggle-filenames
1583 (bookmark-bmenu-toggle-filenames t)))
1585 ;;;###autoload
1586 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1587 ;;;###autoload
1588 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1592 (defun bookmark-bmenu-mode ()
1593 "Major mode for editing a list of bookmarks.
1594 Each line describes one of the bookmarks in Emacs.
1595 Letters do not insert themselves; instead, they are commands.
1596 Bookmark names preceded by a \"*\" have annotations.
1597 \\<bookmark-bmenu-mode-map>
1598 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1599 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1600 Also show bookmarks marked using m in other windows.
1601 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1602 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1603 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1604 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1605 together with bookmark selected before this one in another window.
1606 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1607 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1608 so the bookmark menu bookmark remains visible in its window.
1609 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1610 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1611 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1612 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1613 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1614 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1615 With a prefix arg, prompts for a file to save in.
1616 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1617 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1618 With prefix argument, also move up one line.
1619 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1620 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1621 in another buffer.
1622 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1623 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1624 (kill-all-local-variables)
1625 (use-local-map bookmark-bmenu-mode-map)
1626 (setq truncate-lines t)
1627 (setq buffer-read-only t)
1628 (setq major-mode 'bookmark-bmenu-mode)
1629 (setq mode-name "Bookmark Menu")
1630 (run-hooks 'bookmark-bmenu-mode-hook))
1633 (defun bookmark-bmenu-toggle-filenames (&optional show)
1634 "Toggle whether filenames are shown in the bookmark list.
1635 Optional argument SHOW means show them unconditionally."
1636 (interactive)
1637 (cond
1638 (show
1639 (setq bookmark-bmenu-toggle-filenames nil)
1640 (bookmark-bmenu-show-filenames)
1641 (setq bookmark-bmenu-toggle-filenames t))
1642 (bookmark-bmenu-toggle-filenames
1643 (bookmark-bmenu-hide-filenames)
1644 (setq bookmark-bmenu-toggle-filenames nil))
1646 (bookmark-bmenu-show-filenames)
1647 (setq bookmark-bmenu-toggle-filenames t))))
1650 (defun bookmark-bmenu-show-filenames (&optional force)
1651 (if (and (not force) bookmark-bmenu-toggle-filenames)
1652 nil ;already shown, so do nothing
1653 (save-excursion
1654 (save-window-excursion
1655 (goto-char (point-min))
1656 (forward-line 2)
1657 (setq bookmark-bmenu-hidden-bookmarks ())
1658 (let ((buffer-read-only nil))
1659 (while (< (point) (point-max))
1660 (let ((bmrk (bookmark-bmenu-bookmark)))
1661 (setq bookmark-bmenu-hidden-bookmarks
1662 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1663 (let ((start (save-excursion (end-of-line) (point))))
1664 (move-to-column bookmark-bmenu-file-column t)
1665 ;; Strip off `mouse-face' from the white spaces region.
1666 (if window-system
1667 (remove-text-properties start (point)
1668 '(mouse-face))))
1669 (delete-region (point) (progn (end-of-line) (point)))
1670 (insert " ")
1671 ;; Pass the NO-HISTORY arg:
1672 (bookmark-insert-location bmrk t)
1673 (forward-line 1))))))))
1676 (defun bookmark-bmenu-hide-filenames (&optional force)
1677 (if (and (not force) bookmark-bmenu-toggle-filenames)
1678 ;; nothing to hide if above is nil
1679 (save-excursion
1680 (save-window-excursion
1681 (goto-char (point-min))
1682 (forward-line 2)
1683 (setq bookmark-bmenu-hidden-bookmarks
1684 (nreverse bookmark-bmenu-hidden-bookmarks))
1685 (save-excursion
1686 (goto-char (point-min))
1687 (search-forward "Bookmark")
1688 (backward-word 1)
1689 (setq bookmark-bmenu-bookmark-column (current-column)))
1690 (save-excursion
1691 (let ((buffer-read-only nil))
1692 (while bookmark-bmenu-hidden-bookmarks
1693 (move-to-column bookmark-bmenu-bookmark-column t)
1694 (bookmark-kill-line)
1695 (let ((start (point)))
1696 (insert (car bookmark-bmenu-hidden-bookmarks))
1697 (if window-system
1698 (put-text-property start
1699 (save-excursion (re-search-backward
1700 "[^ \t]")
1701 (1+ (point)))
1702 'mouse-face 'highlight)))
1703 (setq bookmark-bmenu-hidden-bookmarks
1704 (cdr bookmark-bmenu-hidden-bookmarks))
1705 (forward-line 1))))))))
1708 (defun bookmark-bmenu-check-position ()
1709 ;; Returns non-nil if on a line with a bookmark.
1710 ;; (The actual value returned is bookmark-alist).
1711 ;; Else reposition and try again, else return nil.
1712 (cond ((< (count-lines (point-min) (point)) 2)
1713 (goto-char (point-min))
1714 (forward-line 2)
1715 bookmark-alist)
1716 ((and (bolp) (eobp))
1717 (beginning-of-line 0)
1718 bookmark-alist)
1720 bookmark-alist)))
1723 (defun bookmark-bmenu-bookmark ()
1724 ;; return a string which is bookmark of this line.
1725 (if (bookmark-bmenu-check-position)
1726 (save-excursion
1727 (save-window-excursion
1728 (goto-char (point-min))
1729 (search-forward "Bookmark")
1730 (backward-word 1)
1731 (setq bookmark-bmenu-bookmark-column (current-column)))))
1732 (if bookmark-bmenu-toggle-filenames
1733 (bookmark-bmenu-hide-filenames))
1734 (save-excursion
1735 (save-window-excursion
1736 (beginning-of-line)
1737 (forward-char bookmark-bmenu-bookmark-column)
1738 (prog1
1739 (buffer-substring-no-properties (point)
1740 (progn
1741 (end-of-line)
1742 (point)))
1743 ;; well, this is certainly crystal-clear:
1744 (if bookmark-bmenu-toggle-filenames
1745 (bookmark-bmenu-toggle-filenames t))))))
1748 (defun bookmark-show-annotation (bookmark)
1749 "Display the annotation for bookmark named BOOKMARK in a buffer,
1750 if an annotation exists."
1751 (let ((annotation (bookmark-get-annotation bookmark)))
1752 (if (and annotation (not (string-equal annotation "")))
1753 (save-excursion
1754 (let ((old-buf (current-buffer)))
1755 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1756 (delete-region (point-min) (point-max))
1757 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1758 (insert annotation)
1759 (goto-char (point-min))
1760 (pop-to-buffer old-buf))))))
1763 (defun bookmark-show-all-annotations ()
1764 "Display the annotations for all bookmarks in a buffer."
1765 (let ((old-buf (current-buffer)))
1766 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1767 (delete-region (point-min) (point-max))
1768 (mapcar
1769 (lambda (full-record)
1770 (let* ((name (bookmark-name-from-full-record full-record))
1771 (ann (bookmark-get-annotation name)))
1772 (insert (concat name ":\n"))
1773 (if (and ann (not (string-equal ann "")))
1774 ;; insert the annotation, indented by 4 spaces.
1775 (progn
1776 (save-excursion (insert ann))
1777 (while (< (point) (point-max))
1778 (beginning-of-line) ; paranoia
1779 (insert " ")
1780 (forward-line)
1781 (end-of-line))))))
1782 bookmark-alist)
1783 (goto-char (point-min))
1784 (pop-to-buffer old-buf)))
1787 (defun bookmark-bmenu-mark ()
1788 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1789 (interactive)
1790 (beginning-of-line)
1791 (if (bookmark-bmenu-check-position)
1792 (let ((buffer-read-only nil))
1793 (delete-char 1)
1794 (insert ?>)
1795 (forward-line 1)
1796 (bookmark-bmenu-check-position))))
1799 (defun bookmark-bmenu-select ()
1800 "Select this line's bookmark; also display bookmarks marked with `>'.
1801 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1802 (interactive)
1803 (if (bookmark-bmenu-check-position)
1804 (let ((bmrk (bookmark-bmenu-bookmark))
1805 (menu (current-buffer))
1806 (others ())
1807 tem)
1808 (goto-char (point-min))
1809 (while (re-search-forward "^>" nil t)
1810 (setq tem (bookmark-bmenu-bookmark))
1811 (let ((buffer-read-only nil))
1812 (delete-char -1)
1813 (insert ?\ ))
1814 (or (string-equal tem bmrk)
1815 (member tem others)
1816 (setq others (cons tem others))))
1817 (setq others (nreverse others)
1818 tem (/ (1- (frame-height)) (1+ (length others))))
1819 (delete-other-windows)
1820 (bookmark-jump bmrk)
1821 (bury-buffer menu)
1822 (if others
1823 (while others
1824 (split-window nil tem)
1825 (other-window 1)
1826 (bookmark-jump (car others))
1827 (setq others (cdr others)))
1828 (other-window 1)))))
1831 (defun bookmark-bmenu-save (parg)
1832 "Save the current list into a bookmark file.
1833 With a prefix arg, prompts for a file to save them in."
1834 (interactive "P")
1835 (save-excursion
1836 (save-window-excursion
1837 (bookmark-save parg))))
1840 (defun bookmark-bmenu-load ()
1841 "Load the bookmark file and rebuild the bookmark menu-buffer."
1842 (interactive)
1843 (if (bookmark-bmenu-check-position)
1844 (save-excursion
1845 (save-window-excursion
1846 ;; This will call `bookmark-bmenu-list'
1847 (call-interactively 'bookmark-load)))))
1850 (defun bookmark-bmenu-1-window ()
1851 "Select this line's bookmark, alone, in full frame."
1852 (interactive)
1853 (if (bookmark-bmenu-check-position)
1854 (progn
1855 (bookmark-jump (bookmark-bmenu-bookmark))
1856 (bury-buffer (other-buffer))
1857 (delete-other-windows))))
1860 (defun bookmark-bmenu-2-window ()
1861 "Select this line's bookmark, with previous buffer in second window."
1862 (interactive)
1863 (if (bookmark-bmenu-check-position)
1864 (let ((bmrk (bookmark-bmenu-bookmark))
1865 (menu (current-buffer))
1866 (pop-up-windows t))
1867 (delete-other-windows)
1868 (switch-to-buffer (other-buffer))
1869 (let* ((pair (bookmark-jump-noselect bmrk))
1870 (buff (car pair))
1871 (pos (cdr pair)))
1872 (pop-to-buffer buff)
1873 (goto-char pos))
1874 (bury-buffer menu))))
1877 (defun bookmark-bmenu-this-window ()
1878 "Select this line's bookmark in this window."
1879 (interactive)
1880 (if (bookmark-bmenu-check-position)
1881 (bookmark-jump (bookmark-bmenu-bookmark))))
1884 (defun bookmark-bmenu-other-window ()
1885 "Select this line's bookmark in other window, leaving bookmark menu visible."
1886 (interactive)
1887 (let ((bookmark (bookmark-bmenu-bookmark)))
1888 (if (bookmark-bmenu-check-position)
1889 (let* ((pair (bookmark-jump-noselect bookmark))
1890 (buff (car pair))
1891 (pos (cdr pair)))
1892 (switch-to-buffer-other-window buff)
1893 (goto-char pos)
1894 (set-window-point (get-buffer-window buff) pos)
1895 (bookmark-show-annotation bookmark)))))
1898 (defun bookmark-bmenu-switch-other-window ()
1899 "Make the other window select this line's bookmark.
1900 The current window remains selected."
1901 (interactive)
1902 (let ((bookmark (bookmark-bmenu-bookmark))
1903 (pop-up-windows t)
1904 same-window-buffer-names
1905 same-window-regexps)
1906 (if (bookmark-bmenu-check-position)
1907 (let* ((pair (bookmark-jump-noselect bookmark))
1908 (buff (car pair))
1909 (pos (cdr pair)))
1910 (display-buffer buff)
1911 (let ((o-buffer (current-buffer)))
1912 ;; save-excursion won't do
1913 (set-buffer buff)
1914 (goto-char pos)
1915 (set-window-point (get-buffer-window buff) pos)
1916 (set-buffer o-buffer))
1917 (bookmark-show-annotation bookmark)))))
1919 (defun bookmark-bmenu-other-window-with-mouse (event)
1920 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1921 (interactive "e")
1922 (save-excursion
1923 (set-buffer (window-buffer (posn-window (event-end event))))
1924 (save-excursion
1925 (goto-char (posn-point (event-end event)))
1926 (bookmark-bmenu-other-window))))
1929 (defun bookmark-bmenu-show-annotation ()
1930 "Show the annotation for the current bookmark in another window."
1931 (interactive)
1932 (let ((bookmark (bookmark-bmenu-bookmark)))
1933 (if (bookmark-bmenu-check-position)
1934 (bookmark-show-annotation bookmark))))
1937 (defun bookmark-bmenu-show-all-annotations ()
1938 "Show the annotation for all bookmarks in another window."
1939 (interactive)
1940 (bookmark-show-all-annotations))
1943 (defun bookmark-bmenu-edit-annotation ()
1944 "Edit the annotation for the current bookmark in another window."
1945 (interactive)
1946 (let ((bookmark (bookmark-bmenu-bookmark)))
1947 (if (bookmark-bmenu-check-position)
1948 (bookmark-edit-annotation bookmark))))
1951 (defun bookmark-bmenu-unmark (&optional backup)
1952 "Cancel all requested operations on bookmark on this line and move down.
1953 Optional BACKUP means move up."
1954 (interactive "P")
1955 (beginning-of-line)
1956 (if (bookmark-bmenu-check-position)
1957 (progn
1958 (let ((buffer-read-only nil))
1959 (delete-char 1)
1960 ;; any flags to reset according to circumstances? How about a
1961 ;; flag indicating whether this bookmark is being visited?
1962 ;; well, we don't have this now, so maybe later.
1963 (insert " "))
1964 (forward-line (if backup -1 1))
1965 (bookmark-bmenu-check-position))))
1968 (defun bookmark-bmenu-backup-unmark ()
1969 "Move up and cancel all requested operations on bookmark on line above."
1970 (interactive)
1971 (forward-line -1)
1972 (if (bookmark-bmenu-check-position)
1973 (progn
1974 (bookmark-bmenu-unmark)
1975 (forward-line -1)
1976 (bookmark-bmenu-check-position))))
1979 (defun bookmark-bmenu-delete ()
1980 "Mark bookmark on this line to be deleted.
1981 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1982 (interactive)
1983 (beginning-of-line)
1984 (if (bookmark-bmenu-check-position)
1985 (let ((buffer-read-only nil))
1986 (delete-char 1)
1987 (insert ?D)
1988 (forward-line 1)
1989 (bookmark-bmenu-check-position))))
1992 (defun bookmark-bmenu-delete-backwards ()
1993 "Mark bookmark on this line to be deleted, then move up one line.
1994 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1995 (interactive)
1996 (bookmark-bmenu-delete)
1997 (forward-line -2)
1998 (if (bookmark-bmenu-check-position)
1999 (forward-line 1))
2000 (bookmark-bmenu-check-position))
2003 (defun bookmark-bmenu-execute-deletions ()
2004 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2005 (interactive)
2006 (message "Deleting bookmarks...")
2007 (let ((hide-em bookmark-bmenu-toggle-filenames)
2008 (o-point (point))
2009 (o-str (save-excursion
2010 (beginning-of-line)
2011 (if (looking-at "^D")
2013 (buffer-substring
2014 (point)
2015 (progn (end-of-line) (point))))))
2016 (o-col (current-column)))
2017 (if hide-em (bookmark-bmenu-hide-filenames))
2018 (setq bookmark-bmenu-toggle-filenames nil)
2019 (goto-char (point-min))
2020 (forward-line 1)
2021 (while (re-search-forward "^D" (point-max) t)
2022 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2023 (bookmark-bmenu-list)
2024 (setq bookmark-bmenu-toggle-filenames hide-em)
2025 (if bookmark-bmenu-toggle-filenames
2026 (bookmark-bmenu-toggle-filenames t))
2027 (if o-str
2028 (progn
2029 (goto-char (point-min))
2030 (search-forward o-str)
2031 (beginning-of-line)
2032 (forward-char o-col))
2033 (goto-char o-point))
2034 (beginning-of-line)
2035 (setq bookmark-alist-modification-count
2036 (1+ bookmark-alist-modification-count))
2037 (if (bookmark-time-to-save-p)
2038 (bookmark-save))
2039 (message "Deleting bookmarks...done")
2043 (defun bookmark-bmenu-rename ()
2044 "Rename bookmark on current line. Prompts for a new name."
2045 (interactive)
2046 (if (bookmark-bmenu-check-position)
2047 (let ((bmrk (bookmark-bmenu-bookmark))
2048 (thispoint (point)))
2049 (bookmark-rename bmrk)
2050 (bookmark-bmenu-list)
2051 (goto-char thispoint))))
2054 (defun bookmark-bmenu-locate ()
2055 "Display location of this bookmark. Displays in the minibuffer."
2056 (interactive)
2057 (if (bookmark-bmenu-check-position)
2058 (let ((bmrk (bookmark-bmenu-bookmark)))
2059 (message (bookmark-location bmrk)))))
2063 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2065 (defun bookmark-menu-build-paned-menu (name entries)
2066 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2067 That is, ENTRIES is a list of strings which appear as the choices
2068 in the menu. The number of panes depends on the number of entries.
2069 The visible entries are truncated to `bookmark-menu-length', but the
2070 strings returned are not."
2071 (let* ((f-height (/ (frame-height) 2))
2072 (pane-list
2073 (let (temp-pane-list
2074 (iter 0))
2075 (while entries
2076 (let (lst
2077 (count 0))
2078 (while (and (< count f-height) entries)
2079 (let ((str (car entries)))
2080 (setq lst (cons
2081 (cons
2082 (if (> (length str) bookmark-menu-length)
2083 (substring str 0 bookmark-menu-length)
2084 str)
2085 str)
2086 lst))
2087 (setq entries (cdr entries))
2088 (setq count (1+ count))))
2089 (setq iter (1+ iter))
2090 (setq
2091 temp-pane-list
2092 (cons
2093 (cons
2094 (format "-*- %s (%d) -*-" name iter)
2095 (nreverse lst))
2096 temp-pane-list))))
2097 (nreverse temp-pane-list))))
2099 ;; Return the menu:
2100 (cons (concat "-*- " name " -*-") pane-list)))
2103 (defun bookmark-menu-popup-paned-menu (event name entries)
2104 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2105 That is, ENTRIES is a list of strings which appear as the choices
2106 in the menu.
2107 The number of panes depends on the number of entries."
2108 (interactive "e")
2109 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2112 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2113 "Pop up menu of bookmarks, return chosen bookmark.
2114 Pop up at EVENT, menu's name is NAME.
2115 The number of panes depends on the number of bookmarks."
2116 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2119 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2120 ;; help function for making menus that need to apply a bookmark
2121 ;; function to a string.
2122 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2123 event menu-label)))
2124 (if choice (apply func-sym (list choice)))))
2127 ;;;###autoload
2128 (defun bookmark-menu-insert (event)
2129 "Insert the text of the file pointed to by bookmark BOOKMARK.
2130 You may have a problem using this function if the value of variable
2131 `bookmark-alist' is nil. If that happens, you need to load in some
2132 bookmarks. See help on function `bookmark-load' for more about
2133 this.
2135 Warning: this function only takes an EVENT as argument. Use the
2136 corresponding bookmark function from Lisp \(the one without the
2137 \"-menu-\" in its name\)."
2138 (interactive "e")
2139 (bookmark-popup-menu-and-apply-function
2140 'bookmark-insert "Insert Bookmark Contents" event))
2143 ;;;###autoload
2144 (defun bookmark-menu-jump (event)
2145 "Jump to bookmark BOOKMARK (a point in some file).
2146 You may have a problem using this function if the value of variable
2147 `bookmark-alist' is nil. If that happens, you need to load in some
2148 bookmarks. See help on function `bookmark-load' for more about
2149 this.
2151 Warning: this function only takes an EVENT as argument. Use the
2152 corresponding bookmark function from Lisp \(the one without the
2153 \"-menu-\" in its name\)."
2154 (interactive "e")
2155 (bookmark-popup-menu-and-apply-function
2156 'bookmark-jump "Jump to Bookmark" event))
2159 ;;;###autoload
2160 (defun bookmark-menu-locate (event)
2161 "Insert the name of the file associated with BOOKMARK.
2162 \(This is not the same as the contents of that file\).
2164 Warning: this function only takes an EVENT as argument. Use the
2165 corresponding bookmark function from Lisp \(the one without the
2166 \"-menu-\" in its name\)."
2167 (interactive "e")
2168 (bookmark-popup-menu-and-apply-function
2169 'bookmark-insert-location "Insert Bookmark Location" event))
2172 ;;;###autoload
2173 (defun bookmark-menu-rename (event)
2174 "Change the name of OLD-BOOKMARK to NEWNAME.
2175 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2176 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2177 prompts for NEWNAME.
2178 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2179 passed as an argument. If called with two strings, then no prompting
2180 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2182 While you are entering the new name, consecutive C-w's insert
2183 consecutive words from the text of the buffer into the new bookmark
2184 name.
2186 Warning: this function only takes an EVENT as argument. Use the
2187 corresponding bookmark function from Lisp \(the one without the
2188 \"-menu-\" in its name\)."
2189 (interactive "e")
2190 (bookmark-popup-menu-and-apply-function
2191 'bookmark-rename "Rename Bookmark" event))
2194 ;;;###autoload
2195 (defun bookmark-menu-delete (event)
2196 "Delete the bookmark named NAME from the bookmark list.
2197 Removes only the first instance of a bookmark with that name. If
2198 there are one or more other bookmarks with the same name, they will
2199 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2200 one most recently used in this file, if any\).
2202 Warning: this function only takes an EVENT as argument. Use the
2203 corresponding bookmark function from Lisp \(the one without the
2204 \"-menu-\" in its name\)."
2205 (interactive "e")
2206 (bookmark-popup-menu-and-apply-function
2207 'bookmark-delete "Delete Bookmark" event))
2210 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2211 ;; following works, and for explaining what to do to make it work.
2213 ;; We MUST autoload EACH form used to set up this variable's value, so
2214 ;; that the whole job is done in loaddefs.el.
2216 ;; Emacs menubar stuff.
2218 ;;;###autoload
2219 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2221 ;;;###autoload
2222 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2224 ;; make bookmarks appear toward the right side of the menu.
2225 (if (boundp 'menu-bar-final-items)
2226 (if menu-bar-final-items
2227 (setq menu-bar-final-items
2228 (cons 'bookmark menu-bar-final-items)))
2229 (setq menu-bar-final-items '(bookmark)))
2231 ;;;###autoload
2232 (define-key menu-bar-bookmark-map [load]
2233 '("Load a Bookmark File..." . bookmark-load))
2235 ;;;###autoload
2236 (define-key menu-bar-bookmark-map [write]
2237 '("Save Bookmarks As..." . bookmark-write))
2239 ;;;###autoload
2240 (define-key menu-bar-bookmark-map [save]
2241 '("Save Bookmarks" . bookmark-save))
2243 ;;;###autoload
2244 (define-key menu-bar-bookmark-map [edit]
2245 '("Edit Bookmark List" . bookmark-bmenu-list))
2247 ;;;###autoload
2248 (define-key menu-bar-bookmark-map [delete]
2249 '("Delete Bookmark" . bookmark-menu-delete))
2251 ;;;###autoload
2252 (define-key menu-bar-bookmark-map [rename]
2253 '("Rename Bookmark" . bookmark-menu-rename))
2255 ;;;###autoload
2256 (define-key menu-bar-bookmark-map [locate]
2257 '("Insert Location" . bookmark-menu-locate))
2259 ;;;###autoload
2260 (define-key menu-bar-bookmark-map [insert]
2261 '("Insert Contents" . bookmark-menu-insert))
2263 ;;;###autoload
2264 (define-key menu-bar-bookmark-map [set]
2265 '("Set Bookmark" . bookmark-set))
2267 ;;;###autoload
2268 (define-key menu-bar-bookmark-map [jump]
2269 '("Jump to Bookmark" . bookmark-menu-jump))
2271 ;;;; end bookmark menu stuff ;;;;
2274 ;;; Load Hook
2275 (defvar bookmark-load-hook nil
2276 "Hook to run at the end of loading bookmark.")
2278 (run-hooks 'bookmark-load-hook)
2280 (provide 'bookmark)
2282 ;;; bookmark.el ends here