(r_alloc_reinit): New function.
[emacs.git] / lisp / bookmark.el
blobb41d9fc1158ed358359cd46412cc79e824f770ae
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 ;; Author's Update Number: see variable `bookmark-version'.
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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, 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 (defconst bookmark-version "2.6.4"
86 "Version number of bookmark.el. This is not related to the version
87 of Emacs bookmark comes with; it is used solely by bookmark's
88 maintainers to avoid version confusion.")
90 ;;; Misc comments:
92 ;; If variable bookmark-use-annotations is non-nil, an annotation is
93 ;; queried for when setting a bookmark.
95 ;; The bookmark list is sorted lexically by default, but you can turn
96 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
97 ;; the list will be presented in the order it is recorded
98 ;; (chronologically), which is actually fairly useful as well.
100 ;;; User Variables
102 (defgroup bookmark nil
103 "Setting, annotation and jumping to bookmarks."
104 :group 'matching)
107 (defcustom bookmark-use-annotations nil
108 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
109 :type 'boolean
110 :group 'bookmark)
113 (defcustom bookmark-save-flag t
114 "*Controls when Emacs saves bookmarks to a file.
115 --> nil means never save bookmarks, except when `bookmark-save' is
116 explicitly called \(\\[bookmark-save]\).
117 --> t means save bookmarks when Emacs is killed.
118 --> Otherwise, it should be a number that is the frequency with which
119 the bookmark list is saved \(i.e.: the number of times which
120 Emacs' bookmark list may be modified before it is automatically
121 saved.\). If it is a number, Emacs will also automatically save
122 bookmarks when it is killed.
124 Therefore, the way to get it to save every time you make or delete a
125 bookmark is to set this variable to 1 \(or 0, which produces the same
126 behavior.\)
128 To specify the file in which to save them, modify the variable
129 `bookmark-default-file', which is `~/.emacs.bmk' by default."
130 :type '(choice (const nil) (const t) integer)
131 :group 'bookmark)
134 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
135 "*The `.emacs.bmk' file used to be called this name.")
138 ;; defvarred to avoid a compilation warning:
139 (defvar bookmark-file nil
140 "Old name for `bookmark-default-file'.")
142 (defcustom bookmark-default-file
143 (if bookmark-file
144 ;; In case user set `bookmark-file' in her .emacs:
145 bookmark-file
146 (convert-standard-filename "~/.emacs.bmk"))
147 "*File in which to save bookmarks by default."
148 :type 'file
149 :group 'bookmark)
152 (defcustom bookmark-version-control 'nospecial
153 "*Whether or not to make numbered backups of the bookmark file.
154 It can have four values: t, nil, `never', and `nospecial'.
155 The first three have the same meaning that they do for the
156 variable `version-control', and the final value `nospecial' means just
157 use the value of `version-control'."
158 :type '(choice (const t) (const nil) (const never) (const nospecial))
159 :group 'bookmark)
162 (defcustom bookmark-completion-ignore-case t
163 "*Non-nil means bookmark functions ignore case in completion."
164 :type 'boolean
165 :group 'bookmark)
168 (defcustom bookmark-sort-flag t
169 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
170 Otherwise they will be displayed in LIFO order (that is, most
171 recently set ones come first, oldest ones come last)."
172 :type 'boolean
173 :group 'bookmark)
176 (defcustom bookmark-automatically-show-annotations t
177 "*Nil means don't show annotations when jumping to a bookmark."
178 :type 'boolean
179 :group 'bookmark)
182 (defcustom bookmark-bmenu-file-column 30
183 "*Column at which to display filenames in a buffer listing bookmarks.
184 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
185 :type 'integer
186 :group 'bookmark)
189 (defcustom bookmark-bmenu-toggle-filenames t
190 "*Non-nil means show filenames when listing bookmarks.
191 This may result in truncated bookmark names. To disable this, put the
192 following in your `.emacs' file:
194 \(setq bookmark-bmenu-toggle-filenames nil\)"
195 :type 'boolean
196 :group 'bookmark)
199 (defcustom bookmark-menu-length 70
200 "*Maximum length of a bookmark name displayed on a popup menu."
201 :type 'integer
202 :group 'boolean)
205 ;;; No user-serviceable parts beyond this point.
207 ;; Is it XEmacs?
208 (defconst bookmark-xemacsp
209 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
212 ;; Added for lucid emacs compatibility, db
213 (or (fboundp 'defalias) (fset 'defalias 'fset))
215 ;; suggested for lucid compatibility by david hughes:
216 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
218 ;; This variable is probably obsolete now...
219 (or (boundp 'baud-rate)
220 ;; some random value higher than 9600
221 (setq baud-rate 19200))
223 ;; XEmacs apparently call this `buffer-substring-without-properties',
224 ;; sigh.
225 (or (fboundp 'buffer-substring-no-properties)
226 (if (fboundp 'buffer-substring-without-properties)
227 (fset 'buffer-substring-no-properties
228 'buffer-substring-without-properties)
229 (fset 'buffer-substring-no-properties 'buffer-substring)))
232 ;;; Keymap stuff:
234 ;; Set up these bindings dumping time *only*;
235 ;; if the user alters them, don't override the user when loading bookmark.el.
237 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
238 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
239 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
241 ;;;###autoload
242 (defvar bookmark-map nil
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
249 ;;;###autoload
250 (define-prefix-command 'bookmark-map)
252 ;; Read the help on all of these functions for details...
253 ;;;###autoload
254 (define-key bookmark-map "x" 'bookmark-set)
255 ;;;###autoload
256 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
257 ;;;###autoload
258 (define-key bookmark-map "j" 'bookmark-jump)
259 ;;;###autoload
260 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
261 ;;;###autoload
262 (define-key bookmark-map "i" 'bookmark-insert)
263 ;;;###autoload
264 (define-key bookmark-map "e" 'edit-bookmarks)
265 ;;;###autoload
266 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
267 ;;;###autoload
268 (define-key bookmark-map "r" 'bookmark-rename)
269 ;;;###autoload
270 (define-key bookmark-map "d" 'bookmark-delete)
271 ;;;###autoload
272 (define-key bookmark-map "l" 'bookmark-load)
273 ;;;###autoload
274 (define-key bookmark-map "w" 'bookmark-write)
275 ;;;###autoload
276 (define-key bookmark-map "s" 'bookmark-save)
279 ;;; The annotation maps.
280 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
281 "Keymap for composing an annotation for a bookmark.")
283 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
284 'bookmark-send-annotation)
288 ;;; Core variables and data structures:
289 (defvar bookmark-alist ()
290 "Association list of bookmarks and their records.
291 You probably don't want to change the value of this alist yourself;
292 instead, let the various bookmark functions do it for you.
294 The format of the alist is
296 \(BOOKMARK1 BOOKMARK2 ...\)
298 where each BOOKMARK is of the form
300 \(NAME
301 \(filename . FILE\)
302 \(front-context-string . FRONT-STR\)
303 \(rear-context-string . REAR-STR\)
304 \(position . POS\)
305 \(info-node . POS\)
306 \(annotation . ANNOTATION\)\)
308 So the cdr of each bookmark is an alist too.
309 `info-node' is optional, by the way.")
312 (defvar bookmarks-already-loaded nil)
315 ;; just add the hook to make sure that people don't lose bookmarks
316 ;; when they kill Emacs, unless they don't want to save them.
317 ;;;###autoload
318 (add-hook 'kill-emacs-hook
319 (function
320 (lambda () (and (featurep 'bookmark)
321 bookmark-alist
322 (bookmark-time-to-save-p t)
323 (bookmark-save)))))
325 ;; more stuff added by db.
327 (defvar bookmark-current-bookmark nil
328 "Name of bookmark most recently used in the current file.
329 It is buffer local, used to make moving a bookmark forward
330 through a file easier.")
332 (make-variable-buffer-local 'bookmark-current-bookmark)
335 (defvar bookmark-alist-modification-count 0
336 "Number of modifications to bookmark list since it was last saved.")
339 (defvar bookmark-search-size 16
340 "Length of the context strings recorded on either side of a bookmark.")
343 (defvar bookmark-current-point 0)
344 (defvar bookmark-yank-point 0)
345 (defvar bookmark-current-buffer nil)
349 ;; Helper functions.
351 ;; Only functions on this page and the next one (file formats) need to
352 ;; know anything about the format of bookmark-alist entries.
353 ;; Everyone else should go through them.
355 (defun bookmark-name-from-full-record (full-record)
356 "Return name of FULL-RECORD \(an alist element instead of a string\)."
357 (car full-record))
360 (defun bookmark-all-names ()
361 "Return a list of all current bookmark names."
362 (bookmark-maybe-load-default-file)
363 (mapcar
364 (lambda (full-record)
365 (bookmark-name-from-full-record full-record))
366 bookmark-alist))
369 (defun bookmark-get-bookmark (bookmark)
370 "Return the full entry for BOOKMARK in bookmark-alist."
371 (assoc bookmark bookmark-alist))
374 (defun bookmark-get-bookmark-record (bookmark)
375 "Return the guts of the entry for BOOKMARK in bookmark-alist.
376 That is, all information but the name."
377 (car (cdr (bookmark-get-bookmark bookmark))))
380 (defun bookmark-set-name (bookmark newname)
381 "Set BOOKMARK's name to NEWNAME."
382 (setcar (bookmark-get-bookmark bookmark) newname))
385 (defun bookmark-get-annotation (bookmark)
386 "Return the annotation of BOOKMARK, or nil if none."
387 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
390 (defun bookmark-set-annotation (bookmark ann)
391 "Set the annotation of BOOKMARK to ANN."
392 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
393 (if cell
394 (setcdr cell ann)
395 (nconc (bookmark-get-bookmark-record bookmark)
396 (list (cons 'annotation ann))))))
399 (defun bookmark-get-filename (bookmark)
400 "Return the full filename of BOOKMARK."
401 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
404 (defun bookmark-set-filename (bookmark filename)
405 "Set the full filename of BOOKMARK to FILENAME."
406 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
407 (if cell
408 (setcdr cell filename)
409 (nconc (bookmark-get-bookmark-record bookmark)
410 (list (cons 'filename filename))))))
413 (defun bookmark-get-position (bookmark)
414 "Return the position \(i.e.: point\) of BOOKMARK."
415 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
418 (defun bookmark-set-position (bookmark position)
419 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
420 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
421 (if cell
422 (setcdr cell position)
423 (nconc (bookmark-get-bookmark-record bookmark)
424 (list (cons 'position position))))))
427 (defun bookmark-get-front-context-string (bookmark)
428 "Return the front-context-string of BOOKMARK."
429 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
432 (defun bookmark-set-front-context-string (bookmark string)
433 "Set the front-context-string of BOOKMARK to STRING."
434 (let ((cell (assq 'front-context-string
435 (bookmark-get-bookmark-record bookmark))))
436 (if cell
437 (setcdr cell string)
438 (nconc (bookmark-get-bookmark-record bookmark)
439 (list (cons 'front-context-string string))))))
442 (defun bookmark-get-rear-context-string (bookmark)
443 "Return the rear-context-string of BOOKMARK."
444 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
447 (defun bookmark-set-rear-context-string (bookmark string)
448 "Set the rear-context-string of BOOKMARK to STRING."
449 (let ((cell (assq 'rear-context-string
450 (bookmark-get-bookmark-record bookmark))))
451 (if cell
452 (setcdr cell string)
453 (nconc (bookmark-get-bookmark-record bookmark)
454 (list (cons 'rear-context-string string))))))
457 (defun bookmark-get-info-node (bookmark)
458 "Get the info node associated with BOOKMARK."
459 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
462 (defun bookmark-set-info-node (bookmark node)
463 "Set the Info node of BOOKMARK to NODE."
464 (let ((cell (assq 'info-node
465 (bookmark-get-bookmark-record bookmark))))
466 (if cell
467 (setcdr cell node)
468 (nconc (bookmark-get-bookmark-record bookmark)
469 (list (cons 'info-node node)))))
471 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
472 (sit-for 4)
476 (defvar bookmark-history nil
477 "The history list for bookmark functions.")
480 (defun bookmark-completing-read (prompt &optional default)
481 "Prompting with PROMPT, read a bookmark name in completion.
482 PROMPT will get a \": \" stuck on the end no matter what, so you
483 probably don't want to include one yourself.
484 Optional second arg DEFAULT is a string to return if the user enters
485 the empty string."
486 (bookmark-maybe-load-default-file) ; paranoia
487 (let* ((completion-ignore-case bookmark-completion-ignore-case)
488 (default default)
489 (prompt (if default
490 (concat prompt (format " (%s): " default))
491 (concat prompt ": ")))
492 (str
493 (completing-read prompt
494 bookmark-alist
498 'bookmark-history)))
499 (if (string-equal "" str)
500 (list default)
501 (list str))))
504 (defmacro bookmark-maybe-historicize-string (string)
505 "Put STRING into the bookmark prompt history, if caller non-interactive.
506 We need this because sometimes bookmark functions are invoked from
507 menus, so `completing-read' never gets a chance to set `bookmark-history'."
508 (` (or
509 (interactive-p)
510 (setq bookmark-history (cons (, string) bookmark-history)))))
513 (defun bookmark-make (name &optional annotation overwrite info-node)
514 "Make a bookmark named NAME.
515 Optional second arg ANNOTATION gives it an annotation.
516 Optional third arg OVERWRITE means replace any existing bookmarks with
517 this name.
518 Optional fourth arg INFO-NODE means this bookmark is at info node
519 INFO-NODE, so record this fact in the bookmark's entry."
520 (bookmark-maybe-load-default-file)
521 (let ((stripped-name (copy-sequence name)))
522 (or bookmark-xemacsp
523 ;; XEmacs's `set-text-properties' doesn't work on
524 ;; free-standing strings, apparently.
525 (set-text-properties 0 (length stripped-name) nil stripped-name))
526 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
527 ;; already existing bookmark under that name and
528 ;; no prefix arg means just overwrite old bookmark
529 (setcdr (bookmark-get-bookmark stripped-name)
530 (list (bookmark-make-cell annotation info-node)))
532 ;; otherwise just cons it onto the front (either the bookmark
533 ;; doesn't exist already, or there is no prefix arg. In either
534 ;; case, we want the new bookmark consed onto the alist...)
536 (setq bookmark-alist
537 (cons
538 (list stripped-name
539 (bookmark-make-cell annotation info-node))
540 bookmark-alist)))
542 ;; Added by db
543 (setq bookmark-current-bookmark stripped-name)
544 (setq bookmark-alist-modification-count
545 (1+ bookmark-alist-modification-count))
546 (if (bookmark-time-to-save-p)
547 (bookmark-save))))
550 (defun bookmark-make-cell (annotation &optional info-node)
551 "Return the record part of a new bookmark, given ANNOTATION.
552 Must be at the correct position in the buffer in which the bookmark is
553 being set. This might change someday.
554 Optional second arg INFO-NODE means this bookmark is at info node
555 INFO-NODE, so record this fact in the bookmark's entry."
556 (let ((the-record
557 (` ((filename . (, (bookmark-buffer-file-name)))
558 (front-context-string
559 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
560 (buffer-substring-no-properties
561 (point)
562 (+ (point) bookmark-search-size))
563 nil)))
564 (rear-context-string
565 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
566 (buffer-substring-no-properties
567 (point)
568 (- (point) bookmark-search-size))
569 nil)))
570 (position . (, (point)))
571 ))))
573 ;; Now fill in the optional parts:
574 (if annotation
575 (nconc the-record (list (cons 'annotation annotation))))
576 (if info-node
577 (nconc the-record (list (cons 'info-node info-node))))
579 ;; Finally, return the completed record.
580 the-record))
584 ;;; File format stuff
586 ;; The OLD format of the bookmark-alist was:
588 ;; ((bookmark-name (filename
589 ;; string-in-front
590 ;; string-behind
591 ;; point))
592 ;; ...)
594 ;; The NEW format of the bookmark-alist is:
596 ;; ((bookmark-name ((filename . FILENAME)
597 ;; (front-context-string . string-in-front)
598 ;; (rear-context-string . string-behind)
599 ;; (position . POINT)
600 ;; (annotation . annotation)
601 ;; (whatever . VALUE)
602 ;; ...
603 ;; ))
604 ;; ...)
607 ;; I switched to using an internal as well as external alist because I
608 ;; felt that would be a more flexible framework in which to add
609 ;; features. It means that the order in which values appear doesn't
610 ;; matter, and it means that arbitrary values can be added without
611 ;; risk of interfering with existing ones.
613 ;; BOOKMARK-NAME is the string the user gives the bookmark and
614 ;; accesses it by from then on.
616 ;; FILENAME is the location of the file in which the bookmark is set.
618 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
619 ;; context in front of the point at which the bookmark is set.
621 ;; STRING-BEHIND is the same thing, but after the point.
623 ;; The context strings exist so that modifications to a file don't
624 ;; necessarily cause a bookmark's position to be invalidated.
625 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
626 ;; case the file has changed since the bookmark was set. It will
627 ;; attempt to place the user before the changes, if there were any.
628 ;; ANNOTATION is the annotation for the bookmark; it may not exist
629 ;; (for backward compatibility), be nil (no annotation), or be a
630 ;; string.
633 (defconst bookmark-file-format-version 1
634 "The current version of the format used by bookmark files.
635 You should never need to change this.")
638 (defconst bookmark-end-of-version-stamp-marker
639 "-*- End Of Bookmark File Format Version Stamp -*-\n"
640 "This string marks the end of the version stamp in a bookmark file.")
643 (defun bookmark-alist-from-buffer ()
644 "Return a bookmark-alist (in any format) from the current buffer.
645 The buffer must of course contain bookmark format information.
646 Does not care from where in the buffer it is called, and does not
647 affect point."
648 (save-excursion
649 (goto-char (point-min))
650 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
651 (read (current-buffer))
652 ;; Else we're dealing with format version 0
653 (if (search-forward "(" nil t)
654 (progn
655 (forward-char -1)
656 (read (current-buffer)))
657 ;; Else no hope of getting information here.
658 (error "Not bookmark format")))))
661 (defun bookmark-upgrade-version-0-alist (old-list)
662 "Upgrade a version 0 alist OLD-LIST to the current version."
663 (mapcar
664 (lambda (bookmark)
665 (let* ((name (car bookmark))
666 (record (car (cdr bookmark)))
667 (filename (nth 0 record))
668 (front-str (nth 1 record))
669 (rear-str (nth 2 record))
670 (position (nth 3 record))
671 (ann (nth 4 record)))
672 (list
673 name
674 (` ((filename . (, filename))
675 (front-context-string . (, (or front-str "")))
676 (rear-context-string . (, (or rear-str "")))
677 (position . (, position))
678 (annotation . (, ann)))))))
679 old-list))
682 (defun bookmark-upgrade-file-format-from-0 ()
683 "Upgrade a bookmark file of format 0 (the original format) to format 1.
684 This expects to be called from point-min in a bookmark file."
685 (message "Upgrading bookmark format from 0 to %d..."
686 bookmark-file-format-version)
687 (let* ((old-list (bookmark-alist-from-buffer))
688 (new-list (bookmark-upgrade-version-0-alist old-list)))
689 (delete-region (point-min) (point-max))
690 (bookmark-insert-file-format-version-stamp)
691 (pp new-list (current-buffer))
692 (save-buffer))
693 (goto-char (point-min))
694 (message "Upgrading bookmark format from 0 to %d...done"
695 bookmark-file-format-version)
699 (defun bookmark-grok-file-format-version ()
700 "Return an integer which is the file-format version of this bookmark file.
701 This expects to be called from point-min in a bookmark file."
702 (if (looking-at "^;;;;")
703 (save-excursion
704 (save-match-data
705 (re-search-forward "[0-9]")
706 (forward-char -1)
707 (read (current-buffer))))
708 ;; Else this is format version 0, the original one, which didn't
709 ;; even have version stamps.
713 (defun bookmark-maybe-upgrade-file-format ()
714 "Check the file-format version of this bookmark file.
715 If the version is not up-to-date, upgrade it automatically.
716 This expects to be called from point-min in a bookmark file."
717 (let ((version (bookmark-grok-file-format-version)))
718 (cond
719 ((= version bookmark-file-format-version)
720 ) ; home free -- version is current
721 ((= version 0)
722 (bookmark-upgrade-file-format-from-0))
724 (error "Bookmark file format version strangeness")))))
727 (defun bookmark-insert-file-format-version-stamp ()
728 "Insert text indicating current version of bookmark file format."
729 (insert
730 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
731 bookmark-file-format-version))
732 (insert ";;; This format is meant to be slightly human-readable;\n"
733 ";;; nevertheless, you probably don't want to edit it.\n"
734 ";;; "
735 bookmark-end-of-version-stamp-marker))
738 ;;; end file-format stuff
741 ;;; Core code:
743 ;;;###autoload
744 (defun bookmark-set (&optional name parg)
745 "Set a bookmark named NAME inside a file.
746 If name is nil, then the user will be prompted.
747 With prefix arg, will not overwrite a bookmark that has the same name
748 as NAME if such a bookmark already exists, but instead will \"push\"
749 the new bookmark onto the bookmark alist. Thus the most recently set
750 bookmark with name NAME would be the one in effect at any given time,
751 but the others are still there, should you decide to delete the most
752 recent one.
754 To yank words from the text of the buffer and use them as part of the
755 bookmark name, type C-w while setting a bookmark. Successive C-w's
756 yank successive words.
758 Typing C-u inserts the name of the last bookmark used in the buffer
759 \(as an aid in using a single bookmark name to track your progress
760 through a large file\). If no bookmark was used, then C-u inserts the
761 name of the file being visited.
763 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
764 and it removes only the first instance of a bookmark with that name from
765 the list of bookmarks.\)"
766 (interactive (list nil current-prefix-arg))
768 (bookmark-buffer-file-name)
769 (error "Buffer not visiting a file or directory"))
771 (bookmark-maybe-load-default-file)
773 (setq bookmark-current-point (point))
774 (setq bookmark-yank-point (point))
775 (setq bookmark-current-buffer (current-buffer))
777 (let* ((default (or bookmark-current-bookmark
778 (bookmark-buffer-name)))
779 (str
780 (or name
781 (read-from-minibuffer
782 (format "Set bookmark (%s): " default)
784 (let ((now-map (copy-keymap minibuffer-local-map)))
785 (progn (define-key now-map "\C-w"
786 'bookmark-yank-word)
787 (define-key now-map "\C-u"
788 '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 (progn
796 (bookmark-make str annotation parg (bookmark-info-current-node))
797 (setq bookmark-current-bookmark str)
798 (bookmark-bmenu-surreptitiously-rebuild-list)
799 (goto-char bookmark-current-point)))))
802 (defun bookmark-info-current-node ()
803 "If in Info-mode, return current node name (a string), else nil."
804 (if (eq major-mode 'Info-mode)
805 Info-current-node))
808 (defun bookmark-kill-line (&optional newline-too)
809 "Kill from point to end of line.
810 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
811 Does not affect the kill-ring."
812 (let ((eol (save-excursion (end-of-line) (point))))
813 (delete-region (point) eol)
814 (if (and newline-too (looking-at "\n"))
815 (delete-char 1))))
818 ;; Defvars to avoid compilation warnings:
819 (defvar bookmark-annotation-paragraph nil)
820 (defvar bookmark-annotation-name nil)
821 (defvar bookmark-annotation-buffer nil)
822 (defvar bookmark-annotation-file nil)
823 (defvar bookmark-annotation-point nil)
826 (defun bookmark-send-annotation ()
827 "Use buffer contents as the annotation for a bookmark.
828 Exclude lines that begin with `#'.
829 Store the annotation text in the bookmark list with
830 the bookmark (and file, and point) specified in buffer local variables."
831 (interactive)
832 (if (not (eq major-mode 'bookmark-read-annotation-mode))
833 (error "Not in bookmark-read-annotation-mode"))
834 (goto-char (point-min))
835 (while (< (point) (point-max))
836 (if (looking-at "^#")
837 (bookmark-kill-line t)
838 (forward-line 1)))
839 (let ((annotation (buffer-substring (point-min) (point-max)))
840 (parg bookmark-annotation-paragraph)
841 (bookmark bookmark-annotation-name)
842 (pt bookmark-annotation-point)
843 (buf bookmark-annotation-buffer))
844 ;; for bookmark-make-cell to work, we need to be
845 ;; in the relevant buffer, at the relevant point.
846 ;; Actually, bookmark-make-cell should probably be re-written,
847 ;; to avoid this need. Should I handle the error if a buffer is
848 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
849 (save-excursion
850 (pop-to-buffer buf)
851 (goto-char pt)
852 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
853 (setq bookmark-current-bookmark bookmark))
854 (bookmark-bmenu-surreptitiously-rebuild-list)
855 (goto-char bookmark-current-point))
856 (kill-buffer (current-buffer)))
859 (defun bookmark-default-annotation-text (bookmark)
860 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
861 "# All lines which start with a '#' will be deleted.\n"
862 "# Type C-c C-c when done.\n#\n"
863 "# Author: " (user-full-name) " <" (user-login-name) "@"
864 (system-name) ">\n"
865 "# Date: " (current-time-string) "\n"))
868 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
869 "Function to return default text to use for a bookmark annotation.
870 It takes the name of the bookmark, as a string, as an arg.")
872 (defun bookmark-read-annotation-mode (buf point parg bookmark)
873 "Mode for composing annotations for a bookmark.
874 Wants BUF POINT PARG and BOOKMARK.
875 When you have finished composing, type \\[bookmark-send-annotation] to send
876 the annotation.
878 \\{bookmark-read-annotation-mode-map}
880 (interactive)
881 (kill-all-local-variables)
882 (make-local-variable 'bookmark-annotation-paragraph)
883 (make-local-variable 'bookmark-annotation-name)
884 (make-local-variable 'bookmark-annotation-buffer)
885 (make-local-variable 'bookmark-annotation-file)
886 (make-local-variable 'bookmark-annotation-point)
887 (setq bookmark-annotation-paragraph parg)
888 (setq bookmark-annotation-name bookmark)
889 (setq bookmark-annotation-buffer buf)
890 (setq bookmark-annotation-file (buffer-file-name buf))
891 (setq bookmark-annotation-point point)
892 (use-local-map bookmark-read-annotation-mode-map)
893 (setq major-mode 'bookmark-read-annotation-mode)
894 (insert (funcall bookmark-read-annotation-text-func bookmark))
895 (run-hooks 'text-mode-hook))
898 (defun bookmark-read-annotation (parg bookmark)
899 "Pop up a buffer for entering a bookmark annotation.
900 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
901 (let ((buf (current-buffer))
902 (point (point)))
903 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
904 (bookmark-read-annotation-mode buf point parg bookmark)))
907 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
908 "Keymap for editing an annotation of a bookmark.")
911 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
912 'bookmark-send-edited-annotation)
915 (defun bookmark-edit-annotation-mode (bookmark)
916 "Mode for editing the annotation of bookmark BOOKMARK.
917 When you have finished composing, type \\[bookmark-send-annotation].
919 \\{bookmark-edit-annotation-mode-map}
921 (interactive)
922 (kill-all-local-variables)
923 (make-local-variable 'bookmark-annotation-name)
924 (setq bookmark-annotation-name bookmark)
925 (use-local-map bookmark-edit-annotation-mode-map)
926 (setq major-mode 'bookmark-edit-annotation-mode)
927 (insert (funcall bookmark-read-annotation-text-func bookmark))
928 (let ((annotation (bookmark-get-annotation bookmark)))
929 (if (and (not (eq annotation nil))
930 (not (string-equal annotation "")))
931 (insert annotation)))
932 (run-hooks 'text-mode-hook))
935 (defun bookmark-send-edited-annotation ()
936 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
937 (interactive)
938 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
939 (error "Not in bookmark-edit-annotation-mode"))
940 (goto-char (point-min))
941 (while (< (point) (point-max))
942 (if (looking-at "^#")
943 (bookmark-kill-line t)
944 (forward-line 1)))
945 (let ((annotation (buffer-substring (point-min) (point-max)))
946 (bookmark bookmark-annotation-name))
947 (bookmark-set-annotation bookmark annotation)
948 (bookmark-bmenu-surreptitiously-rebuild-list)
949 (goto-char bookmark-current-point))
950 (kill-buffer (current-buffer)))
953 (defun bookmark-edit-annotation (bookmark)
954 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
955 (let ((buf (current-buffer))
956 (point (point)))
957 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
958 (bookmark-edit-annotation-mode bookmark)))
961 (defun bookmark-insert-current-bookmark ()
962 "Insert this buffer's value of bookmark-current-bookmark.
963 Default to file name if it's nil."
964 (interactive)
965 (let ((str
966 (save-excursion
967 (set-buffer bookmark-current-buffer)
968 bookmark-current-bookmark)))
969 (if str (insert str) (bookmark-insert-buffer-name))))
972 (defun bookmark-insert-buffer-name ()
973 "Insert the current file name into the bookmark name being set.
974 The directory part of the file name is not used."
975 (interactive)
976 (let ((str
977 (save-excursion
978 (set-buffer bookmark-current-buffer)
979 (bookmark-buffer-name))))
980 (insert str)))
983 (defun bookmark-buffer-name ()
984 "Return the name of the current buffer's file, non-directory.
985 In Info, return the current node."
986 (cond
987 ;; Are we in Info?
988 ((string-equal mode-name "Info") Info-current-node)
989 ;; Or are we a file?
990 (buffer-file-name (file-name-nondirectory buffer-file-name))
991 ;; Or are we a directory?
992 ((and (boundp 'dired-directory) dired-directory)
993 (let* ((dirname (if (stringp dired-directory)
994 dired-directory
995 (car dired-directory)))
996 (idx (1- (length dirname))))
997 ;; Strip the trailing slash.
998 (if (= ?/ (aref dirname idx))
999 (file-name-nondirectory (substring dirname 0 idx))
1000 ;; Else return the current-buffer
1001 (buffer-name (current-buffer)))))
1002 ;; If all else fails, use the buffer's name.
1004 (buffer-name (current-buffer)))))
1007 (defun bookmark-yank-word ()
1008 (interactive)
1009 ;; get the next word from the buffer and append it to the name of
1010 ;; the bookmark currently being set.
1011 (let ((string (save-excursion
1012 (set-buffer bookmark-current-buffer)
1013 (goto-char bookmark-yank-point)
1014 (buffer-substring-no-properties
1015 (point)
1016 (save-excursion
1017 (forward-word 1)
1018 (setq bookmark-yank-point (point)))))))
1019 (insert string)))
1022 (defun bookmark-buffer-file-name ()
1023 "Return the current buffer's file in a way useful for bookmarks.
1024 For example, if this is a Info buffer, return the Info file's name."
1025 (if (eq major-mode 'Info-mode)
1026 Info-current-file
1028 buffer-file-name
1029 (if (and (boundp 'dired-directory) dired-directory)
1030 (if (stringp dired-directory)
1031 dired-directory
1032 (car dired-directory))))))
1035 (defun bookmark-maybe-load-default-file ()
1036 (and (not bookmarks-already-loaded)
1037 (null bookmark-alist)
1038 (prog2
1039 (and
1040 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1041 ;; to be renamed.
1042 (file-exists-p (expand-file-name bookmark-old-default-file))
1043 (not (file-exists-p (expand-file-name bookmark-default-file)))
1044 (rename-file (expand-file-name bookmark-old-default-file)
1045 (expand-file-name bookmark-default-file)))
1046 ;; return t so the `and' will continue...
1049 (file-readable-p (expand-file-name bookmark-default-file))
1050 (progn
1051 (bookmark-load bookmark-default-file t t)
1052 (setq bookmarks-already-loaded t))))
1055 (defun bookmark-maybe-sort-alist ()
1056 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1057 ;;is non-nil, then return a sorted copy of the alist.
1058 (if bookmark-sort-flag
1059 (setq bookmark-alist
1060 (sort (copy-alist bookmark-alist)
1061 (function
1062 (lambda (x y) (string-lessp (car x) (car y))))))))
1065 ;;;###autoload
1066 (defun bookmark-jump (bookmark)
1067 "Jump to bookmark BOOKMARK (a point in some file).
1068 You may have a problem using this function if the value of variable
1069 `bookmark-alist' is nil. If that happens, you need to load in some
1070 bookmarks. See help on function `bookmark-load' for more about
1071 this.
1073 If the file pointed to by BOOKMARK no longer exists, you will be asked
1074 if you wish to give the bookmark a new location, and bookmark-jump
1075 will then jump to the new location, as well as recording it in place
1076 of the old one in the permanent bookmark record."
1077 (interactive
1078 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1079 (bookmark-maybe-historicize-string bookmark)
1080 (let ((cell (bookmark-jump-noselect bookmark)))
1081 (and cell
1082 (switch-to-buffer (car cell))
1083 (goto-char (cdr cell))
1084 (if bookmark-automatically-show-annotations
1085 ;; if there is an annotation for this bookmark,
1086 ;; show it in a buffer.
1087 (bookmark-show-annotation bookmark)))))
1090 (defun bookmark-jump-noselect (str)
1091 ;; a leetle helper for bookmark-jump :-)
1092 ;; returns (BUFFER . POINT)
1093 (bookmark-maybe-load-default-file)
1094 (let* ((file (expand-file-name (bookmark-get-filename str)))
1095 (forward-str (bookmark-get-front-context-string str))
1096 (behind-str (bookmark-get-rear-context-string str))
1097 (place (bookmark-get-position str))
1098 (info-node (bookmark-get-info-node str))
1099 (orig-file file)
1101 (if (or
1102 (file-exists-p file)
1103 ;; else try some common compression extensions
1104 ;; and Emacs better handle it right!
1105 ;; Sigh: I think it may *not* be handled at the moment. What
1106 ;; to do about this?
1107 (setq file
1109 (let ((altname (concat file ".Z")))
1110 (and (file-exists-p altname)
1111 altname))
1112 (let ((altname (concat file ".gz")))
1113 (and (file-exists-p altname)
1114 altname))
1115 (let ((altname (concat file ".z")))
1116 (and (file-exists-p altname)
1117 altname)))))
1118 (save-excursion
1119 (if info-node
1120 ;; Info nodes must be visited with care.
1121 (progn
1122 (require 'info)
1123 (Info-find-node file info-node))
1124 ;; Else no Info. Can do an ordinary find-file:
1125 (set-buffer (find-file-noselect file))
1126 (goto-char place))
1128 ;; Go searching forward first. Then, if forward-str exists and
1129 ;; was found in the file, we can search backward for behind-str.
1130 ;; Rationale is that if text was inserted between the two in the
1131 ;; file, it's better to be put before it so you can read it,
1132 ;; rather than after and remain perhaps unaware of the changes.
1133 (if forward-str
1134 (if (search-forward forward-str (point-max) t)
1135 (goto-char (match-beginning 0))))
1136 (if behind-str
1137 (if (search-backward behind-str (point-min) t)
1138 (goto-char (match-end 0))))
1139 ;; added by db
1140 (setq bookmark-current-bookmark str)
1141 (cons (current-buffer) (point)))
1142 (progn
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)))
1174 ;;;###autoload
1175 (defun bookmark-insert-location (bookmark &optional no-history)
1176 "Insert the name of the file associated with BOOKMARK.
1177 Optional second arg NO-HISTORY means don't record this in the
1178 minibuffer history list `bookmark-history'."
1179 (interactive (bookmark-completing-read "Insert bookmark location"))
1180 (or no-history (bookmark-maybe-historicize-string bookmark))
1181 (let ((start (point)))
1182 (prog1
1183 (insert (bookmark-location bookmark)) ; *Return this line*
1184 (if window-system
1185 (put-text-property start
1186 (save-excursion (re-search-backward
1187 "[^ \t]")
1188 (1+ (point)))
1189 'mouse-face 'highlight)))))
1191 ;;;###autoload
1192 (defalias 'bookmark-locate 'bookmark-insert-location)
1194 (defun bookmark-location (bookmark)
1195 "Return the name of the file associated with BOOKMARK."
1196 (bookmark-maybe-load-default-file)
1197 (bookmark-get-filename bookmark))
1200 ;;;###autoload
1201 (defun bookmark-rename (old &optional new)
1202 "Change the name of OLD bookmark to NEW name.
1203 If called from keyboard, prompt for OLD and NEW. If called from
1204 menubar, select OLD from a menu and prompt for NEW.
1206 If called from Lisp, prompt for NEW if only OLD was passed as an
1207 argument. If called with two strings, then no prompting is done. You
1208 must pass at least OLD when calling from Lisp.
1210 While you are entering the new name, consecutive C-w's insert
1211 consecutive words from the text of the buffer into the new bookmark
1212 name."
1213 (interactive (bookmark-completing-read "Old bookmark name"))
1214 (bookmark-maybe-historicize-string old)
1215 (bookmark-maybe-load-default-file)
1216 (progn
1217 (setq bookmark-current-point (point))
1218 (setq bookmark-yank-point (point))
1219 (setq bookmark-current-buffer (current-buffer))
1220 (let ((newname
1221 (or new ; use second arg, if non-nil
1222 (read-from-minibuffer
1223 "New name: "
1225 (let ((now-map (copy-keymap minibuffer-local-map)))
1226 (define-key now-map "\C-w" 'bookmark-yank-word)
1227 now-map)
1229 'bookmark-history))))
1230 (progn
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-substring (point-min) (point-max)))))
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 ;;;###autoload
1379 (defun bookmark-load (file &optional revert no-msg)
1380 "Load bookmarks from FILE (which must be in bookmark format).
1381 Appends loaded bookmarks to the front of the list of bookmarks. If
1382 optional second argument REVERT is non-nil, existing bookmarks are
1383 destroyed. Optional third arg NO-MSG means don't display any messages
1384 while loading.
1386 If you load a file that doesn't contain a proper bookmark alist, you
1387 will corrupt Emacs's bookmark list. Generally, you should only load
1388 in files that were created with the bookmark functions in the first
1389 place. Your own personal bookmark file, `~/.emacs.bmk', is
1390 maintained automatically by Emacs; you shouldn't need to load it
1391 explicitly."
1392 (interactive
1393 (list (read-file-name
1394 (format "Load bookmarks from: (%s) "
1395 bookmark-default-file)
1396 ;;Default might not be used often,
1397 ;;but there's no better default, and
1398 ;;I guess it's better than none at all.
1399 "~/" bookmark-default-file 'confirm)))
1400 (setq file (expand-file-name file))
1401 (if (file-readable-p file)
1402 (save-excursion
1403 (save-window-excursion
1404 (if (and (null no-msg) (>= baud-rate 9600))
1405 (message "Loading bookmarks from %s..." file))
1406 (set-buffer (let ((enable-local-variables nil))
1407 (find-file-noselect file)))
1408 (goto-char (point-min))
1409 (bookmark-maybe-upgrade-file-format)
1410 (let ((blist (bookmark-alist-from-buffer)))
1411 (if (listp blist)
1412 (progn
1413 (if (not revert)
1414 (setq bookmark-alist-modification-count
1415 (1+ bookmark-alist-modification-count))
1416 (setq bookmark-alist-modification-count 0))
1417 (setq bookmark-alist
1418 (append blist (if (not revert) bookmark-alist)))
1419 (bookmark-bmenu-surreptitiously-rebuild-list))
1420 (error "Invalid bookmark list in %s" file)))
1421 (kill-buffer (current-buffer)))
1422 (if (and (null no-msg) (>= baud-rate 9600))
1423 (message "Loading bookmarks from %s...done" file)))
1424 (error "Cannot read bookmark file %s" file)))
1428 ;;; Code supporting the dired-like bookmark menu. Prefix is
1429 ;;; "bookmark-bmenu" for "buffer-menu":
1432 (defvar bookmark-bmenu-bookmark-column nil)
1435 (defvar bookmark-bmenu-hidden-bookmarks ())
1438 (defvar bookmark-bmenu-mode-map nil)
1441 (if bookmark-bmenu-mode-map
1443 (setq bookmark-bmenu-mode-map (make-keymap))
1444 (suppress-keymap bookmark-bmenu-mode-map t)
1445 (define-key bookmark-bmenu-mode-map "q" 'bookmark-bmenu-quit)
1446 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1447 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1448 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1449 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1450 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1451 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1452 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1453 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1454 (define-key bookmark-bmenu-mode-map "\C-o"
1455 'bookmark-bmenu-switch-other-window)
1456 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1457 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1458 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1459 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1460 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1461 (define-key bookmark-bmenu-mode-map " " 'next-line)
1462 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1463 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1464 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1465 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1466 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1467 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1468 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1469 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1470 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1471 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1472 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1473 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1474 (define-key bookmark-bmenu-mode-map [mouse-2]
1475 'bookmark-bmenu-other-window-with-mouse))
1479 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1480 ;; data.
1481 (put 'bookmark-bmenu-mode 'mode-class 'special)
1484 ;; todo: need to display whether or not bookmark exists as a buffer in
1485 ;; flag column.
1487 ;; Format:
1488 ;; FLAGS BOOKMARK [ LOCATION ]
1491 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1492 "Rebuild the Bookmark List if it exists.
1493 Don't affect the buffer ring order."
1494 (if (get-buffer "*Bookmark List*")
1495 (save-excursion
1496 (save-window-excursion
1497 (bookmark-bmenu-list)))))
1500 ;;;###autoload
1501 (defun bookmark-bmenu-list ()
1502 "Display a list of existing bookmarks.
1503 The list is displayed in a buffer named `*Bookmark List*'.
1504 The leftmost column displays a D if the bookmark is flagged for
1505 deletion, or > if it is flagged for displaying."
1506 (interactive)
1507 (bookmark-maybe-load-default-file)
1508 (if (interactive-p)
1509 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1510 (set-buffer (get-buffer-create "*Bookmark List*")))
1511 (let ((buffer-read-only nil))
1512 (delete-region (point-max) (point-min))
1513 (goto-char (point-min)) ;sure are playing it safe...
1514 (insert "% Bookmark\n- --------\n")
1515 (bookmark-maybe-sort-alist)
1516 (mapcar
1517 (lambda (full-record)
1518 ;; if a bookmark has an annotation, prepend a "*"
1519 ;; in the list of bookmarks.
1520 (let ((annotation (bookmark-get-annotation
1521 (bookmark-name-from-full-record full-record))))
1522 (if (and (not (eq annotation nil))
1523 (not (string-equal annotation "")))
1524 (insert " *")
1525 (insert " "))
1526 (let ((start (point)))
1527 (insert (bookmark-name-from-full-record full-record))
1528 (if window-system
1529 (put-text-property start
1530 (save-excursion (re-search-backward
1531 "[^ \t]")
1532 (1+ (point)))
1533 'mouse-face 'highlight))
1534 (insert "\n")
1536 bookmark-alist))
1537 (goto-char (point-min))
1538 (forward-line 2)
1539 (bookmark-bmenu-mode)
1540 (if bookmark-bmenu-toggle-filenames
1541 (bookmark-bmenu-toggle-filenames t)))
1543 ;;;###autoload
1544 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1545 ;;;###autoload
1546 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1550 (defun bookmark-bmenu-mode ()
1551 "Major mode for editing a list of bookmarks.
1552 Each line describes one of the bookmarks in Emacs.
1553 Letters do not insert themselves; instead, they are commands.
1554 Bookmark names preceded by a \"*\" have annotations.
1555 \\<bookmark-bmenu-mode-map>
1556 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1557 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1558 Also show bookmarks marked using m in other windows.
1559 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1560 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1561 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1562 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1563 together with bookmark selected before this one in another window.
1564 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1565 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1566 so the bookmark menu bookmark remains visible in its window.
1567 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1568 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1569 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1570 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1571 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1572 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1573 With a prefix arg, prompts for a file to save in.
1574 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1575 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1576 With prefix argument, also move up one line.
1577 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1578 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1579 in another buffer.
1580 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1581 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1582 (kill-all-local-variables)
1583 (use-local-map bookmark-bmenu-mode-map)
1584 (setq truncate-lines t)
1585 (setq buffer-read-only t)
1586 (setq major-mode 'bookmark-bmenu-mode)
1587 (setq mode-name "Bookmark Menu")
1588 (run-hooks 'bookmark-bmenu-mode-hook))
1591 (defun bookmark-bmenu-toggle-filenames (&optional show)
1592 "Toggle whether filenames are shown in the bookmark list.
1593 Optional argument SHOW means show them unconditionally."
1594 (interactive)
1595 (cond
1596 (show
1597 (setq bookmark-bmenu-toggle-filenames nil)
1598 (bookmark-bmenu-show-filenames)
1599 (setq bookmark-bmenu-toggle-filenames t))
1600 (bookmark-bmenu-toggle-filenames
1601 (bookmark-bmenu-hide-filenames)
1602 (setq bookmark-bmenu-toggle-filenames nil))
1604 (bookmark-bmenu-show-filenames)
1605 (setq bookmark-bmenu-toggle-filenames t))))
1608 (defun bookmark-bmenu-show-filenames (&optional force)
1609 (if (and (not force) bookmark-bmenu-toggle-filenames)
1610 nil ;already shown, so do nothing
1611 (save-excursion
1612 (save-window-excursion
1613 (goto-char (point-min))
1614 (forward-line 2)
1615 (setq bookmark-bmenu-hidden-bookmarks ())
1616 (let ((buffer-read-only nil))
1617 (while (< (point) (point-max))
1618 (let ((bmrk (bookmark-bmenu-bookmark)))
1619 (setq bookmark-bmenu-hidden-bookmarks
1620 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1621 (let ((start (save-excursion (end-of-line) (point))))
1622 (move-to-column bookmark-bmenu-file-column t)
1623 ;; Strip off `mouse-face' from the white spaces region.
1624 (if window-system
1625 (remove-text-properties start (point)
1626 '(mouse-face))))
1627 (delete-region (point) (progn (end-of-line) (point)))
1628 (insert " ")
1629 ;; Pass the NO-HISTORY arg:
1630 (bookmark-insert-location bmrk t)
1631 (forward-line 1))))))))
1634 (defun bookmark-bmenu-hide-filenames (&optional force)
1635 (if (and (not force) bookmark-bmenu-toggle-filenames)
1636 ;; nothing to hide if above is nil
1637 (save-excursion
1638 (save-window-excursion
1639 (goto-char (point-min))
1640 (forward-line 2)
1641 (setq bookmark-bmenu-hidden-bookmarks
1642 (nreverse bookmark-bmenu-hidden-bookmarks))
1643 (save-excursion
1644 (goto-char (point-min))
1645 (search-forward "Bookmark")
1646 (backward-word 1)
1647 (setq bookmark-bmenu-bookmark-column (current-column)))
1648 (save-excursion
1649 (let ((buffer-read-only nil))
1650 (while bookmark-bmenu-hidden-bookmarks
1651 (move-to-column bookmark-bmenu-bookmark-column t)
1652 (bookmark-kill-line)
1653 (let ((start (point)))
1654 (insert (car bookmark-bmenu-hidden-bookmarks))
1655 (if window-system
1656 (put-text-property start
1657 (save-excursion (re-search-backward
1658 "[^ \t]")
1659 (1+ (point)))
1660 'mouse-face 'highlight)))
1661 (setq bookmark-bmenu-hidden-bookmarks
1662 (cdr bookmark-bmenu-hidden-bookmarks))
1663 (forward-line 1))))))))
1666 ;; if you look at this next function from far away, it resembles a
1667 ;; gun. But only with this comment above...
1668 (defun bookmark-bmenu-check-position ()
1669 ;; Returns t if on a line with a bookmark.
1670 ;; Otherwise, repositions and returns t.
1671 ;; written by David Hughes <djh@harston.cv.com>
1672 ;; Mucho thanks, David! -karl
1673 (cond ((< (count-lines (point-min) (point)) 2)
1674 (goto-char (point-min))
1675 (forward-line 2)
1677 ((and (bolp) (eobp))
1678 (beginning-of-line 0)
1681 t)))
1684 (defun bookmark-bmenu-bookmark ()
1685 ;; return a string which is bookmark of this line.
1686 (if (bookmark-bmenu-check-position)
1687 (save-excursion
1688 (save-window-excursion
1689 (goto-char (point-min))
1690 (search-forward "Bookmark")
1691 (backward-word 1)
1692 (setq bookmark-bmenu-bookmark-column (current-column)))))
1693 (if bookmark-bmenu-toggle-filenames
1694 (bookmark-bmenu-hide-filenames))
1695 (save-excursion
1696 (save-window-excursion
1697 (beginning-of-line)
1698 (forward-char bookmark-bmenu-bookmark-column)
1699 (prog1
1700 (buffer-substring-no-properties (point)
1701 (progn
1702 (end-of-line)
1703 (point)))
1704 ;; well, this is certainly crystal-clear:
1705 (if bookmark-bmenu-toggle-filenames
1706 (bookmark-bmenu-toggle-filenames t))))))
1709 (defun bookmark-show-annotation (bookmark)
1710 "Display the annotation for bookmark named BOOKMARK in a buffer,
1711 if an annotation exists."
1712 (let ((annotation (bookmark-get-annotation bookmark)))
1713 (if (and (not (eq annotation nil))
1714 (not (string-equal annotation "")))
1715 (progn
1716 (save-excursion
1717 (let ((old-buf (current-buffer)))
1718 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1719 (delete-region (point-min) (point-max))
1720 ; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1721 (insert annotation)
1722 (goto-char (point-min))
1723 (pop-to-buffer old-buf)))))))
1726 (defun bookmark-show-all-annotations ()
1727 "Display the annotations for all bookmarks in a buffer."
1728 (let ((old-buf (current-buffer)))
1729 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1730 (delete-region (point-min) (point-max))
1731 (mapcar
1732 (lambda (full-record)
1733 (let* ((name (bookmark-name-from-full-record full-record))
1734 (ann (bookmark-get-annotation name)))
1735 (insert (concat name ":\n"))
1736 (if (and (not (eq ann nil)) (not (string-equal ann "")))
1737 ;; insert the annotation, indented by 4 spaces.
1738 (progn
1739 (save-excursion (insert ann))
1740 (while (< (point) (point-max))
1741 (beginning-of-line) ; paranoia
1742 (insert " ")
1743 (forward-line)
1744 (end-of-line))))))
1745 bookmark-alist)
1746 (goto-char (point-min))
1747 (pop-to-buffer old-buf)))
1750 (defun bookmark-bmenu-mark ()
1751 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1752 (interactive)
1753 (beginning-of-line)
1754 (if (bookmark-bmenu-check-position)
1755 (let ((buffer-read-only nil))
1756 (delete-char 1)
1757 (insert ?>)
1758 (forward-line 1))))
1761 (defun bookmark-bmenu-select ()
1762 "Select this line's bookmark; also display bookmarks marked with `>'.
1763 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1764 (interactive)
1765 (if (bookmark-bmenu-check-position)
1766 (let ((bmrk (bookmark-bmenu-bookmark))
1767 (menu (current-buffer))
1768 (others ())
1769 tem)
1770 (goto-char (point-min))
1771 (while (re-search-forward "^>" nil t)
1772 (setq tem (bookmark-bmenu-bookmark))
1773 (let ((buffer-read-only nil))
1774 (delete-char -1)
1775 (insert ?\ ))
1776 (or (string-equal tem bmrk)
1777 (member tem others)
1778 (setq others (cons tem others))))
1779 (setq others (nreverse others)
1780 tem (/ (1- (frame-height)) (1+ (length others))))
1781 (delete-other-windows)
1782 (bookmark-jump bmrk)
1783 (bury-buffer menu)
1784 (if others
1785 (while others
1786 (split-window nil tem)
1787 (other-window 1)
1788 (bookmark-jump (car others))
1789 (setq others (cdr others)))
1790 (other-window 1)))))
1793 (defun bookmark-bmenu-save (parg)
1794 "Save the current list into a bookmark file.
1795 With a prefix arg, prompts for a file to save them in."
1796 (interactive "P")
1797 (save-excursion
1798 (save-window-excursion
1799 (bookmark-save parg))))
1802 (defun bookmark-bmenu-load ()
1803 "Load the bookmark file and rebuild the bookmark menu-buffer."
1804 (interactive)
1805 (if (bookmark-bmenu-check-position)
1806 (save-excursion
1807 (save-window-excursion
1808 ;; This will call `bookmark-bmenu-list'
1809 (call-interactively 'bookmark-load)))))
1812 (defun bookmark-bmenu-1-window ()
1813 "Select this line's bookmark, alone, in full frame."
1814 (interactive)
1815 (if (bookmark-bmenu-check-position)
1816 (progn
1817 (bookmark-jump (bookmark-bmenu-bookmark))
1818 (bury-buffer (other-buffer))
1819 (delete-other-windows))))
1822 (defun bookmark-bmenu-2-window ()
1823 "Select this line's bookmark, with previous buffer in second window."
1824 (interactive)
1825 (if (bookmark-bmenu-check-position)
1826 (let ((bmrk (bookmark-bmenu-bookmark))
1827 (menu (current-buffer))
1828 (pop-up-windows t))
1829 (delete-other-windows)
1830 (switch-to-buffer (other-buffer))
1831 (let* ((pair (bookmark-jump-noselect bmrk))
1832 (buff (car pair))
1833 (pos (cdr pair)))
1834 (pop-to-buffer buff)
1835 (goto-char pos))
1836 (bury-buffer menu))))
1839 (defun bookmark-bmenu-this-window ()
1840 "Select this line's bookmark in this window."
1841 (interactive)
1842 (if (bookmark-bmenu-check-position)
1843 (bookmark-jump (bookmark-bmenu-bookmark))))
1846 (defun bookmark-bmenu-other-window ()
1847 "Select this line's bookmark in other window, leaving bookmark menu visible."
1848 (interactive)
1849 (let ((bookmark (bookmark-bmenu-bookmark)))
1850 (if (bookmark-bmenu-check-position)
1851 (let* ((pair (bookmark-jump-noselect bookmark))
1852 (buff (car pair))
1853 (pos (cdr pair)))
1854 (switch-to-buffer-other-window buff)
1855 (goto-char pos)
1856 (set-window-point (get-buffer-window buff) pos)
1857 (bookmark-show-annotation bookmark)))))
1860 (defun bookmark-bmenu-switch-other-window ()
1861 "Make the other window select this line's bookmark.
1862 The current window remains selected."
1863 (interactive)
1864 (let ((bookmark (bookmark-bmenu-bookmark)))
1865 (if (bookmark-bmenu-check-position)
1866 (let* ((pair (bookmark-jump-noselect bookmark))
1867 (buff (car pair))
1868 (pos (cdr pair)))
1869 (display-buffer buff)
1870 (let ((o-buffer (current-buffer)))
1871 ;; save-excursion won't do
1872 (set-buffer buff)
1873 (goto-char pos)
1874 (set-window-point (get-buffer-window buff) pos)
1875 (set-buffer o-buffer))
1876 (bookmark-show-annotation bookmark)))))
1878 (defun bookmark-bmenu-other-window-with-mouse (event)
1879 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1880 (interactive "e")
1881 (save-excursion
1882 (set-buffer (window-buffer (posn-window (event-end event))))
1883 (save-excursion
1884 (goto-char (posn-point (event-end event)))
1885 (bookmark-bmenu-other-window))))
1888 (defun bookmark-bmenu-show-annotation ()
1889 "Show the annotation for the current bookmark in another window."
1890 (interactive)
1891 (let ((bookmark (bookmark-bmenu-bookmark)))
1892 (if (bookmark-bmenu-check-position)
1893 (bookmark-show-annotation bookmark))))
1896 (defun bookmark-bmenu-show-all-annotations ()
1897 "Show the annotation for all bookmarks in another window."
1898 (interactive)
1899 (bookmark-show-all-annotations))
1902 (defun bookmark-bmenu-edit-annotation ()
1903 "Edit the annotation for the current bookmark in another window."
1904 (interactive)
1905 (let ((bookmark (bookmark-bmenu-bookmark)))
1906 (if (bookmark-bmenu-check-position)
1907 (bookmark-edit-annotation bookmark))))
1910 (defun bookmark-bmenu-quit ()
1911 "Quit the bookmark menu."
1912 (interactive)
1913 (let ((buffer (current-buffer)))
1914 (switch-to-buffer (other-buffer))
1915 (bury-buffer buffer)))
1918 (defun bookmark-bmenu-unmark (&optional backup)
1919 "Cancel all requested operations on bookmark on this line and move down.
1920 Optional BACKUP means move up."
1921 (interactive "P")
1922 (beginning-of-line)
1923 (if (bookmark-bmenu-check-position)
1924 (progn
1925 (let ((buffer-read-only nil))
1926 (delete-char 1)
1927 ;; any flags to reset according to circumstances? How about a
1928 ;; flag indicating whether this bookmark is being visited?
1929 ;; well, we don't have this now, so maybe later.
1930 (insert " "))
1931 (forward-line (if backup -1 1)))))
1934 (defun bookmark-bmenu-backup-unmark ()
1935 "Move up and cancel all requested operations on bookmark on line above."
1936 (interactive)
1937 (forward-line -1)
1938 (if (bookmark-bmenu-check-position)
1939 (progn
1940 (bookmark-bmenu-unmark)
1941 (forward-line -1))))
1944 (defun bookmark-bmenu-delete ()
1945 "Mark bookmark on this line to be deleted.
1946 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1947 (interactive)
1948 (beginning-of-line)
1949 (if (bookmark-bmenu-check-position)
1950 (let ((buffer-read-only nil))
1951 (delete-char 1)
1952 (insert ?D)
1953 (forward-line 1))))
1956 (defun bookmark-bmenu-delete-backwards ()
1957 "Mark bookmark on this line to be deleted, then move up one line.
1958 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1959 (interactive)
1960 (bookmark-bmenu-delete)
1961 (forward-line -2)
1962 (if (bookmark-bmenu-check-position)
1963 (forward-line 1)))
1966 (defun bookmark-bmenu-execute-deletions ()
1967 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1968 (interactive)
1969 (message "Deleting bookmarks...")
1970 (let ((hide-em bookmark-bmenu-toggle-filenames)
1971 (o-point (point))
1972 (o-str (save-excursion
1973 (beginning-of-line)
1974 (if (looking-at "^D")
1976 (buffer-substring
1977 (point)
1978 (progn (end-of-line) (point))))))
1979 (o-col (current-column)))
1980 (if hide-em (bookmark-bmenu-hide-filenames))
1981 (setq bookmark-bmenu-toggle-filenames nil)
1982 (goto-char (point-min))
1983 (forward-line 1)
1984 (while (re-search-forward "^D" (point-max) t)
1985 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1986 (bookmark-bmenu-list)
1987 (setq bookmark-bmenu-toggle-filenames hide-em)
1988 (if bookmark-bmenu-toggle-filenames
1989 (bookmark-bmenu-toggle-filenames t))
1990 (if o-str
1991 (progn
1992 (goto-char (point-min))
1993 (search-forward o-str)
1994 (beginning-of-line)
1995 (forward-char o-col))
1996 (goto-char o-point))
1997 (beginning-of-line)
1998 (setq bookmark-alist-modification-count
1999 (1+ bookmark-alist-modification-count))
2000 (if (bookmark-time-to-save-p)
2001 (bookmark-save))
2002 (message "Deleting bookmarks...done")
2006 (defun bookmark-bmenu-rename ()
2007 "Rename bookmark on current line. Prompts for a new name."
2008 (interactive)
2009 (if (bookmark-bmenu-check-position)
2010 (let ((bmrk (bookmark-bmenu-bookmark))
2011 (thispoint (point)))
2012 (bookmark-rename bmrk)
2013 (bookmark-bmenu-list)
2014 (goto-char thispoint))))
2017 (defun bookmark-bmenu-locate ()
2018 "Display location of this bookmark. Displays in the minibuffer."
2019 (interactive)
2020 (if (bookmark-bmenu-check-position)
2021 (let ((bmrk (bookmark-bmenu-bookmark)))
2022 (message (bookmark-location bmrk)))))
2026 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2028 (defun bookmark-menu-build-paned-menu (name entries)
2029 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2030 That is, ENTRIES is a list of strings which appear as the choices
2031 in the menu. The number of panes depends on the number of entries.
2032 The visible entries are truncated to `bookmark-menu-length', but the
2033 strings returned are not."
2034 (let* ((f-height (/ (frame-height) 2))
2035 (pane-list
2036 (let (temp-pane-list
2037 (iter 0))
2038 (while entries
2039 (let (lst
2040 (count 0))
2041 (while (and (< count f-height) entries)
2042 (let ((str (car entries)))
2043 (setq lst (cons
2044 (cons
2045 (if (> (length str) bookmark-menu-length)
2046 (substring str 0 bookmark-menu-length)
2047 str)
2048 str)
2049 lst))
2050 (setq entries (cdr entries))
2051 (setq count (1+ count))))
2052 (setq iter (1+ iter))
2053 (setq
2054 temp-pane-list
2055 (cons
2056 (cons
2057 (format "-*- %s (%d) -*-" name iter)
2058 (nreverse lst))
2059 temp-pane-list))))
2060 (nreverse temp-pane-list))))
2062 ;; Return the menu:
2063 (cons (concat "-*- " name " -*-") pane-list)))
2066 (defun bookmark-build-xemacs-menu (name entries function)
2067 "Build a menu named NAME from the strings in ENTRIES.
2068 That is, ENTRIES is a list of strings that appear as the choices
2069 in the menu.
2070 The visible entries are truncated to `bookmark-menu-length', but the
2071 strings returned are not."
2072 (let* (lst
2073 (pane-list
2074 (progn
2075 (while entries
2076 (let ((str (car entries)))
2077 (setq lst (cons
2078 (vector
2079 (if (> (length str) bookmark-menu-length)
2080 (substring str 0 bookmark-menu-length)
2081 str)
2082 (list function str)
2084 lst))
2085 (setq entries (cdr entries))))
2086 (nreverse lst))))
2088 ;; Return the menu:
2089 (append (if popup-menu-titles (list (concat "-*- " name " -*-")))
2090 pane-list)))
2093 (defun bookmark-menu-popup-paned-menu (event name entries)
2094 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2095 That is, ENTRIES is a list of strings which appear as the choices
2096 in the menu.
2097 The number of panes depends on the number of entries."
2098 (interactive "e")
2099 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2102 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2103 "Pop up menu of bookmarks, return chosen bookmark.
2104 Pop up at EVENT, menu's name is NAME.
2105 The number of panes depends on the number of bookmarks."
2106 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2109 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2110 ;; help function for making menus that need to apply a bookmark
2111 ;; function to a string.
2112 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2113 event menu-label)))
2114 (if choice (apply func-sym (list choice)))))
2117 ;;;###autoload
2118 (defun bookmark-menu-insert (event)
2119 "Insert the text of the file pointed to by bookmark BOOKMARK.
2120 You may have a problem using this function if the value of variable
2121 `bookmark-alist' is nil. If that happens, you need to load in some
2122 bookmarks. See help on function `bookmark-load' for more about
2123 this.
2125 Warning: this function only takes an EVENT as argument. Use the
2126 corresponding bookmark function from Lisp \(the one without the
2127 \"-menu-\" in its name\)."
2128 (interactive "e")
2129 (bookmark-popup-menu-and-apply-function
2130 'bookmark-insert "Insert Bookmark Contents" event))
2133 ;;;###autoload
2134 (defun bookmark-menu-jump (event)
2135 "Jump to bookmark BOOKMARK (a point in some file).
2136 You may have a problem using this function if the value of variable
2137 `bookmark-alist' is nil. If that happens, you need to load in some
2138 bookmarks. See help on function `bookmark-load' for more about
2139 this.
2141 Warning: this function only takes an EVENT as argument. Use the
2142 corresponding bookmark function from Lisp \(the one without the
2143 \"-menu-\" in its name\)."
2144 (interactive "e")
2145 (bookmark-popup-menu-and-apply-function
2146 'bookmark-jump "Jump to Bookmark" event))
2149 ;;;###autoload
2150 (defun bookmark-menu-locate (event)
2151 "Insert the name of the file associated with BOOKMARK.
2152 \(This is not the same as the contents of that file\).
2154 Warning: this function only takes an EVENT as argument. Use the
2155 corresponding bookmark function from Lisp \(the one without the
2156 \"-menu-\" in its name\)."
2157 (interactive "e")
2158 (bookmark-popup-menu-and-apply-function
2159 'bookmark-insert-location "Insert Bookmark Location" event))
2162 ;;;###autoload
2163 (defun bookmark-menu-rename (event)
2164 "Change the name of OLD-BOOKMARK to NEWNAME.
2165 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2166 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2167 prompts for NEWNAME.
2168 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2169 passed as an argument. If called with two strings, then no prompting
2170 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2172 While you are entering the new name, consecutive C-w's insert
2173 consecutive words from the text of the buffer into the new bookmark
2174 name.
2176 Warning: this function only takes an EVENT as argument. Use the
2177 corresponding bookmark function from Lisp \(the one without the
2178 \"-menu-\" in its name\)."
2179 (interactive "e")
2180 (bookmark-popup-menu-and-apply-function
2181 'bookmark-rename "Rename Bookmark" event))
2184 ;;;###autoload
2185 (defun bookmark-menu-delete (event)
2186 "Delete the bookmark named NAME from the bookmark list.
2187 Removes only the first instance of a bookmark with that name. If
2188 there are one or more other bookmarks with the same name, they will
2189 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2190 one most recently used in this file, if any\).
2192 Warning: this function only takes an EVENT as argument. Use the
2193 corresponding bookmark function from Lisp \(the one without the
2194 \"-menu-\" in its name\)."
2195 (interactive "e")
2196 (bookmark-popup-menu-and-apply-function
2197 'bookmark-delete "Delete Bookmark" event))
2200 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2201 ;; following works, and for explaining what to do to make it work.
2203 ;; We MUST autoload EACH form used to set up this variable's value, so
2204 ;; that the whole job is done in loaddefs.el.
2206 ;; Emacs menubar stuff.
2208 ;;;###autoload
2209 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2211 ;;;###autoload
2212 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2214 ;; make bookmarks appear toward the right side of the menu.
2215 (if (boundp 'menu-bar-final-items)
2216 (if menu-bar-final-items
2217 (setq menu-bar-final-items
2218 (cons 'bookmark menu-bar-final-items)))
2219 (setq menu-bar-final-items '(bookmark)))
2221 ;;;###autoload
2222 (define-key menu-bar-bookmark-map [load]
2223 '("Load a Bookmark File..." . bookmark-load))
2225 ;;;###autoload
2226 (define-key menu-bar-bookmark-map [write]
2227 '("Save Bookmarks As..." . bookmark-write))
2229 ;;;###autoload
2230 (define-key menu-bar-bookmark-map [save]
2231 '("Save Bookmarks" . bookmark-save))
2233 ;;;###autoload
2234 (define-key menu-bar-bookmark-map [edit]
2235 '("Edit Bookmark List" . bookmark-bmenu-list))
2237 ;;;###autoload
2238 (define-key menu-bar-bookmark-map [delete]
2239 '("Delete Bookmark" . bookmark-menu-delete))
2241 ;;;###autoload
2242 (define-key menu-bar-bookmark-map [rename]
2243 '("Rename Bookmark" . bookmark-menu-rename))
2245 ;;;###autoload
2246 (define-key menu-bar-bookmark-map [locate]
2247 '("Insert Location" . bookmark-menu-locate))
2249 ;;;###autoload
2250 (define-key menu-bar-bookmark-map [insert]
2251 '("Insert Contents" . bookmark-menu-insert))
2253 ;;;###autoload
2254 (define-key menu-bar-bookmark-map [set]
2255 '("Set Bookmark" . bookmark-set))
2257 ;;;###autoload
2258 (define-key menu-bar-bookmark-map [jump]
2259 '("Jump to Bookmark" . bookmark-menu-jump))
2261 ;;;; end bookmark menu stuff ;;;;
2264 ;;; Load Hook
2265 (defvar bookmark-load-hook nil
2266 "Hook to run at the end of loading bookmark.")
2268 (run-hooks 'bookmark-load-hook)
2270 (provide 'bookmark)
2272 ;;; bookmark.el ends here