Add defgroup's; use defcustom for user vars.
[emacs.git] / lisp / bookmark.el
blobff42e8f24fe83bbf7547af4638316eddc81a315c
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 will query for an annotation in a
109 buffer."
110 :type 'boolean
111 :group 'bookmark)
114 (defcustom bookmark-save-flag t
115 "*Controls when Emacs saves bookmarks to a file.
116 --> Nil means never save bookmarks, except when `bookmark-save' is
117 explicitly called \(\\[bookmark-save]\).
118 --> t means save bookmarks when Emacs is killed.
119 --> Otherwise, it should be a number that is the frequency with which
120 the bookmark list is saved \(i.e.: the number of times which
121 Emacs' bookmark list may be modified before it is automatically
122 saved.\). If it is a number, Emacs will also automatically save
123 bookmarks when it is killed.
125 Therefore, the way to get it to save every time you make or delete a
126 bookmark is to set this variable to 1 \(or 0, which produces the same
127 behavior.\)
129 To specify the file in which to save them, modify the variable
130 bookmark-default-file, which is `~/.emacs.bmk' by default."
131 :type '(choice (const nil) (const t) integer)
132 :group 'bookmark)
135 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
136 "*The .emacs.bmk file used to be called this.")
139 ;; defvarred to avoid a compilation warning:
140 (defvar bookmark-file nil
141 "Old name for `bookmark-default-file'.")
143 (defcustom bookmark-default-file
144 (if bookmark-file
145 ;; In case user set `bookmark-file' in her .emacs:
146 bookmark-file
147 (convert-standard-filename "~/.emacs.bmk"))
148 "*File in which to save bookmarks by default."
149 :type 'file
150 :group 'bookmark)
153 (defcustom bookmark-version-control 'nospecial
154 "*Whether or not to make numbered backups of the bookmark file.
155 It can have four values: t, nil, `never', and `nospecial'.
156 The first three have the same meaning that they do for the
157 variable `version-control', and the final value `nospecial' means just
158 use the value of `version-control'."
159 :type '(choice (const t) (const nil) (const never) (const nospecial))
160 :group 'bookmark)
163 (defcustom bookmark-completion-ignore-case t
164 "*Non-nil means bookmark functions ignore case in completion."
165 :type 'boolean
166 :group 'bookmark)
169 (defcustom bookmark-sort-flag t
170 "*Non-nil means that bookmarks will be displayed sorted by bookmark
171 name. Otherwise they will be displayed in LIFO order (that is, most
172 recently set ones come first, oldest ones come last)."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-automatically-show-annotations t
178 "*Nil means don't show annotations when jumping to a bookmark."
179 :type 'boolean
180 :group 'bookmark)
183 (defcustom bookmark-bmenu-file-column 30
184 "*Column at which to display filenames in a buffer listing bookmarks.
185 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
186 :type 'integer
187 :group 'bookmark)
190 (defcustom bookmark-bmenu-toggle-filenames t
191 "*Non-nil means show filenames when listing bookmarks.
192 This may result in truncated bookmark names. To disable this, put the
193 following in your .emacs:
195 \(setq bookmark-bmenu-toggle-filenames nil\)"
196 :type 'boolean
197 :group 'bookmark)
200 (defcustom bookmark-menu-length 70
201 "*Maximum length of a bookmark name displayed on a popup menu."
202 :type 'integer
203 :group 'boolean)
206 ;;; No user-serviceable parts beyond this point.
208 ;; Is it XEmacs?
209 (defconst bookmark-xemacsp
210 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
213 ;; Added for lucid emacs compatibility, db
214 (or (fboundp 'defalias) (fset 'defalias 'fset))
216 ;; suggested for lucid compatibility by david hughes:
217 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
219 ;; This variable is probably obsolete now...
220 (or (boundp 'baud-rate)
221 ;; some random value higher than 9600
222 (setq baud-rate 19200))
224 ;; XEmacs apparently call this `buffer-substring-without-properties',
225 ;; sigh.
226 (or (fboundp 'buffer-substring-no-properties)
227 (if (fboundp 'buffer-substring-without-properties)
228 (fset 'buffer-substring-no-properties
229 'buffer-substring-without-properties)
230 (fset 'buffer-substring-no-properties 'buffer-substring)))
233 ;;; Keymap stuff:
234 ;; some people have C-x r set to rmail or whatever. We don't want to
235 ;; assume that C-x r is a prefix map just because it's distributed
236 ;; that way...
237 ;; These are the distribution keybindings suggested by RMS, everything
238 ;; else will be done with M-x or the menubar:
239 ;;;###autoload
240 (if (symbolp (key-binding "\C-xr"))
242 (progn (define-key ctl-x-map "rb" 'bookmark-jump)
243 (define-key ctl-x-map "rm" 'bookmark-set)
244 (define-key ctl-x-map "rl" 'bookmark-bmenu-list)))
246 ;; define the map, so it can be bound by those who desire to do so:
248 ;;;###autoload
249 (defvar bookmark-map nil
250 "Keymap containing bindings to bookmark functions.
251 It is not bound to any key by default: to bind it
252 so that you have a bookmark prefix, just use `global-set-key' and bind a
253 key of your choice to `bookmark-map'. All interactive bookmark
254 functions have a binding in this keymap.")
256 ;;;###autoload
257 (define-prefix-command 'bookmark-map)
259 ;; Read the help on all of these functions for details...
260 ;;;###autoload
261 (define-key bookmark-map "x" 'bookmark-set)
262 ;;;###autoload
263 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
264 ;;;###autoload
265 (define-key bookmark-map "j" 'bookmark-jump)
266 ;;;###autoload
267 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
268 ;;;###autoload
269 (define-key bookmark-map "i" 'bookmark-insert)
270 ;;;###autoload
271 (define-key bookmark-map "e" 'edit-bookmarks)
272 ;;;###autoload
273 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
274 ;;;###autoload
275 (define-key bookmark-map "r" 'bookmark-rename)
276 ;;;###autoload
277 (define-key bookmark-map "d" 'bookmark-delete)
278 ;;;###autoload
279 (define-key bookmark-map "l" 'bookmark-load)
280 ;;;###autoload
281 (define-key bookmark-map "w" 'bookmark-write)
282 ;;;###autoload
283 (define-key bookmark-map "s" 'bookmark-save)
286 ;;; The annotation maps.
287 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
288 "Keymap for composing an annotation for a bookmark.")
290 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
291 'bookmark-send-annotation)
295 ;;; Core variables and data structures:
296 (defvar bookmark-alist ()
297 "Association list of bookmarks and their records.
298 You probably don't want to change the value of this alist yourself;
299 instead, let the various bookmark functions do it for you.
301 The format of the alist is
303 \(BOOKMARK1 BOOKMARK2 ...\)
305 where each BOOKMARK is of the form
307 \(NAME
308 \(filename . FILE\)
309 \(front-context-string . FRONT-STR\)
310 \(rear-context-string . REAR-STR\)
311 \(position . POS\)
312 \(info-node . POS\)
313 \(annotation . ANNOTATION\)\)
315 So the cdr of each bookmark is an alist too.
316 `info-node' is optional, by the way.")
319 (defvar bookmarks-already-loaded nil)
322 ;; just add the hook to make sure that people don't lose bookmarks
323 ;; when they kill Emacs, unless they don't want to save them.
324 ;;;###autoload
325 (add-hook 'kill-emacs-hook
326 (function
327 (lambda () (and (featurep 'bookmark)
328 bookmark-alist
329 (bookmark-time-to-save-p t)
330 (bookmark-save)))))
332 ;; more stuff added by db.
334 (defvar bookmark-current-bookmark nil
335 "Name of bookmark most recently used in the current file.
336 It is buffer local, used to make moving a bookmark forward
337 through a file easier.")
339 (make-variable-buffer-local 'bookmark-current-bookmark)
342 (defvar bookmark-alist-modification-count 0
343 "Number of modifications to bookmark list since it was last saved.")
346 (defvar bookmark-search-size 16
347 "Length of the context strings recorded on either side of a bookmark.")
350 (defvar bookmark-current-point 0)
351 (defvar bookmark-yank-point 0)
352 (defvar bookmark-current-buffer nil)
356 ;; Helper functions.
358 ;; Only functions on this page and the next one (file formats) need to
359 ;; know anything about the format of bookmark-alist entries.
360 ;; Everyone else should go through them.
362 (defun bookmark-name-from-full-record (full-record)
363 "Return name of FULL-RECORD \(an alist element instead of a string\)."
364 (car full-record))
367 (defun bookmark-all-names ()
368 "Return a list of all current bookmark names."
369 (bookmark-maybe-load-default-file)
370 (mapcar
371 (lambda (full-record)
372 (bookmark-name-from-full-record full-record))
373 bookmark-alist))
376 (defun bookmark-get-bookmark (bookmark)
377 "Return the full entry for BOOKMARK in bookmark-alist."
378 (assoc bookmark bookmark-alist))
381 (defun bookmark-get-bookmark-record (bookmark)
382 "Return the guts of the entry for BOOKMARK in bookmark-alist.
383 That is, all information but the name."
384 (car (cdr (bookmark-get-bookmark bookmark))))
387 (defun bookmark-set-name (bookmark newname)
388 "Set BOOKMARK's name to NEWNAME."
389 (setcar (bookmark-get-bookmark bookmark) newname))
392 (defun bookmark-get-annotation (bookmark)
393 "Return the annotation of BOOKMARK, or nil if none."
394 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
397 (defun bookmark-set-annotation (bookmark ann)
398 "Set the annotation of BOOKMARK to ANN."
399 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
400 (if cell
401 (setcdr cell ann)
402 (nconc (bookmark-get-bookmark-record bookmark)
403 (list (cons 'annotation ann))))))
406 (defun bookmark-get-filename (bookmark)
407 "Return the full filename of BOOKMARK."
408 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
411 (defun bookmark-set-filename (bookmark filename)
412 "Set the full filename of BOOKMARK to FILENAME."
413 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
414 (if cell
415 (setcdr cell filename)
416 (nconc (bookmark-get-bookmark-record bookmark)
417 (list (cons 'filename filename))))))
420 (defun bookmark-get-position (bookmark)
421 "Return the position \(i.e.: point\) of BOOKMARK."
422 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
425 (defun bookmark-set-position (bookmark position)
426 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
427 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
428 (if cell
429 (setcdr cell position)
430 (nconc (bookmark-get-bookmark-record bookmark)
431 (list (cons 'position position))))))
434 (defun bookmark-get-front-context-string (bookmark)
435 "Return the front-context-string of BOOKMARK."
436 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
439 (defun bookmark-set-front-context-string (bookmark string)
440 "Set the front-context-string of BOOKMARK to STRING."
441 (let ((cell (assq 'front-context-string
442 (bookmark-get-bookmark-record bookmark))))
443 (if cell
444 (setcdr cell string)
445 (nconc (bookmark-get-bookmark-record bookmark)
446 (list (cons 'front-context-string string))))))
449 (defun bookmark-get-rear-context-string (bookmark)
450 "Return the rear-context-string of BOOKMARK."
451 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
454 (defun bookmark-set-rear-context-string (bookmark string)
455 "Set the rear-context-string of BOOKMARK to STRING."
456 (let ((cell (assq 'rear-context-string
457 (bookmark-get-bookmark-record bookmark))))
458 (if cell
459 (setcdr cell string)
460 (nconc (bookmark-get-bookmark-record bookmark)
461 (list (cons 'rear-context-string string))))))
464 (defun bookmark-get-info-node (bookmark)
465 "Get the info node associated with BOOKMARK."
466 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
469 (defun bookmark-set-info-node (bookmark node)
470 "Set the Info node of BOOKMARK to NODE."
471 (let ((cell (assq 'info-node
472 (bookmark-get-bookmark-record bookmark))))
473 (if cell
474 (setcdr cell node)
475 (nconc (bookmark-get-bookmark-record bookmark)
476 (list (cons 'info-node node)))))
478 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
479 (sit-for 4)
483 (defvar bookmark-history nil
484 "The history list for bookmark functions.")
487 (defun bookmark-completing-read (prompt &optional default)
488 "Prompting with PROMPT, read a bookmark name in completion.
489 PROMPT will get a \": \" stuck on the end no matter what, so you
490 probably don't want to include one yourself.
491 Optional second arg DEFAULT is a string to return if the user enters
492 the empty string."
493 (bookmark-maybe-load-default-file) ; paranoia
494 (let* ((completion-ignore-case bookmark-completion-ignore-case)
495 (default default)
496 (prompt (if default
497 (concat prompt (format " (%s): " default))
498 (concat prompt ": ")))
499 (str
500 (completing-read prompt
501 bookmark-alist
505 'bookmark-history)))
506 (if (string-equal "" str)
507 (list default)
508 (list str))))
511 (defmacro bookmark-maybe-historicize-string (string)
512 "Put STRING into the bookmark prompt history, if caller non-interactive.
513 We need this because sometimes bookmark functions are invoked from
514 menus, so `completing-read' never gets a chance to set `bookmark-history'."
515 (` (or
516 (interactive-p)
517 (setq bookmark-history (cons (, string) bookmark-history)))))
520 (defun bookmark-make (name &optional annotation overwrite info-node)
521 "Make a bookmark named NAME.
522 Optional second arg ANNOTATION gives it an annotation.
523 Optional third arg OVERWRITE means replace any existing bookmarks with
524 this name.
525 Optional fourth arg INFO-NODE means this bookmark is at info node
526 INFO-NODE, so record this fact in the bookmark's entry."
527 (bookmark-maybe-load-default-file)
528 (let ((stripped-name (copy-sequence name)))
529 (or bookmark-xemacsp
530 ;; XEmacs's `set-text-properties' doesn't work on
531 ;; free-standing strings, apparently.
532 (set-text-properties 0 (length stripped-name) nil stripped-name))
533 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
534 ;; already existing bookmark under that name and
535 ;; no prefix arg means just overwrite old bookmark
536 (setcdr (bookmark-get-bookmark stripped-name)
537 (list (bookmark-make-cell annotation info-node)))
539 ;; otherwise just cons it onto the front (either the bookmark
540 ;; doesn't exist already, or there is no prefix arg. In either
541 ;; case, we want the new bookmark consed onto the alist...)
543 (setq bookmark-alist
544 (cons
545 (list stripped-name
546 (bookmark-make-cell annotation info-node))
547 bookmark-alist)))
549 ;; Added by db
550 (setq bookmark-current-bookmark stripped-name)
551 (setq bookmark-alist-modification-count
552 (1+ bookmark-alist-modification-count))
553 (if (bookmark-time-to-save-p)
554 (bookmark-save))))
557 (defun bookmark-make-cell (annotation &optional info-node)
558 "Return the record part of a new bookmark, given ANNOTATION.
559 Must be at the correct position in the buffer in which the bookmark is
560 being set. This might change someday.
561 Optional second arg INFO-NODE means this bookmark is at info node
562 INFO-NODE, so record this fact in the bookmark's entry."
563 (let ((the-record
564 (` ((filename . (, (bookmark-buffer-file-name)))
565 (front-context-string
566 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
567 (buffer-substring-no-properties
568 (point)
569 (+ (point) bookmark-search-size))
570 nil)))
571 (rear-context-string
572 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
573 (buffer-substring-no-properties
574 (point)
575 (- (point) bookmark-search-size))
576 nil)))
577 (position . (, (point)))
578 ))))
580 ;; Now fill in the optional parts:
581 (if annotation
582 (nconc the-record (list (cons 'annotation annotation))))
583 (if info-node
584 (nconc the-record (list (cons 'info-node info-node))))
586 ;; Finally, return the completed record.
587 the-record))
591 ;;; File format stuff
593 ;; The OLD format of the bookmark-alist was:
595 ;; ((bookmark-name (filename
596 ;; string-in-front
597 ;; string-behind
598 ;; point))
599 ;; ...)
601 ;; The NEW format of the bookmark-alist is:
603 ;; ((bookmark-name ((filename . FILENAME)
604 ;; (front-context-string . string-in-front)
605 ;; (rear-context-string . string-behind)
606 ;; (position . POINT)
607 ;; (annotation . annotation)
608 ;; (whatever . VALUE)
609 ;; ...
610 ;; ))
611 ;; ...)
614 ;; I switched to using an internal as well as external alist because I
615 ;; felt that would be a more flexible framework in which to add
616 ;; features. It means that the order in which values appear doesn't
617 ;; matter, and it means that arbitrary values can be added without
618 ;; risk of interfering with existing ones.
620 ;; BOOKMARK-NAME is the string the user gives the bookmark and
621 ;; accesses it by from then on.
623 ;; FILENAME is the location of the file in which the bookmark is set.
625 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
626 ;; context in front of the point at which the bookmark is set.
628 ;; STRING-BEHIND is the same thing, but after the point.
630 ;; The context strings exist so that modifications to a file don't
631 ;; necessarily cause a bookmark's position to be invalidated.
632 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
633 ;; case the file has changed since the bookmark was set. It will
634 ;; attempt to place the user before the changes, if there were any.
635 ;; ANNOTATION is the annotation for the bookmark; it may not exist
636 ;; (for backward compatibility), be nil (no annotation), or be a
637 ;; string.
640 (defconst bookmark-file-format-version 1
641 "The current version of the format used by bookmark files.
642 You should never need to change this.")
645 (defconst bookmark-end-of-version-stamp-marker
646 "-*- End Of Bookmark File Format Version Stamp -*-\n"
647 "This string marks the end of the version stamp in a bookmark file.")
650 (defun bookmark-alist-from-buffer ()
651 "Return a bookmark-alist (in any format) from the current buffer.
652 The buffer must of course contain bookmark format information.
653 Does not care from where in the buffer it is called, and does not
654 affect point."
655 (save-excursion
656 (goto-char (point-min))
657 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
658 (read (current-buffer))
659 ;; Else we're dealing with format version 0
660 (if (search-forward "(" nil t)
661 (progn
662 (forward-char -1)
663 (read (current-buffer)))
664 ;; Else no hope of getting information here.
665 (error "Not bookmark format")))))
668 (defun bookmark-upgrade-version-0-alist (old-list)
669 "Upgrade a version 0 alist OLD-LIST to the current version."
670 (mapcar
671 (lambda (bookmark)
672 (let* ((name (car bookmark))
673 (record (car (cdr bookmark)))
674 (filename (nth 0 record))
675 (front-str (nth 1 record))
676 (rear-str (nth 2 record))
677 (position (nth 3 record))
678 (ann (nth 4 record)))
679 (list
680 name
681 (` ((filename . (, filename))
682 (front-context-string . (, (or front-str "")))
683 (rear-context-string . (, (or rear-str "")))
684 (position . (, position))
685 (annotation . (, ann)))))))
686 old-list))
689 (defun bookmark-upgrade-file-format-from-0 ()
690 "Upgrade a bookmark file of format 0 (the original format) to format 1.
691 This expects to be called from point-min in a bookmark file."
692 (message "Upgrading bookmark format from 0 to %d..."
693 bookmark-file-format-version)
694 (let* ((old-list (bookmark-alist-from-buffer))
695 (new-list (bookmark-upgrade-version-0-alist old-list)))
696 (delete-region (point-min) (point-max))
697 (bookmark-insert-file-format-version-stamp)
698 (pp new-list (current-buffer))
699 (save-buffer))
700 (goto-char (point-min))
701 (message "Upgrading bookmark format from 0 to %d...done"
702 bookmark-file-format-version)
706 (defun bookmark-grok-file-format-version ()
707 "Return an integer which is the file-format version of this bookmark file.
708 This expects to be called from point-min in a bookmark file."
709 (if (looking-at "^;;;;")
710 (save-excursion
711 (save-match-data
712 (re-search-forward "[0-9]")
713 (forward-char -1)
714 (read (current-buffer))))
715 ;; Else this is format version 0, the original one, which didn't
716 ;; even have version stamps.
720 (defun bookmark-maybe-upgrade-file-format ()
721 "Check the file-format version of this bookmark file.
722 If the version is not up-to-date, upgrade it automatically.
723 This expects to be called from point-min in a bookmark file."
724 (let ((version (bookmark-grok-file-format-version)))
725 (cond
726 ((= version bookmark-file-format-version)
727 ) ; home free -- version is current
728 ((= version 0)
729 (bookmark-upgrade-file-format-from-0))
731 (error "Bookmark file format version strangeness")))))
734 (defun bookmark-insert-file-format-version-stamp ()
735 "Insert text indicating current version of bookmark file format."
736 (insert
737 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
738 bookmark-file-format-version))
739 (insert ";;; This format is meant to be slightly human-readable;\n"
740 ";;; nevertheless, you probably don't want to edit it.\n"
741 ";;; "
742 bookmark-end-of-version-stamp-marker))
745 ;;; end file-format stuff
748 ;;; Core code:
750 ;;;###autoload
751 (defun bookmark-set (&optional name parg)
752 "Set a bookmark named NAME inside a file.
753 If name is nil, then the user will be prompted.
754 With prefix arg, will not overwrite a bookmark that has the same name
755 as NAME if such a bookmark already exists, but instead will \"push\"
756 the new bookmark onto the bookmark alist. Thus the most recently set
757 bookmark with name NAME would be the one in effect at any given time,
758 but the others are still there, should you decide to delete the most
759 recent one.
761 To yank words from the text of the buffer and use them as part of the
762 bookmark name, type C-w while setting a bookmark. Successive C-w's
763 yank successive words.
765 Typing C-u inserts the name of the last bookmark used in the buffer
766 \(as an aid in using a single bookmark name to track your progress
767 through a large file\). If no bookmark was used, then C-u inserts the
768 name of the file being visited.
770 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
771 and it removes only the first instance of a bookmark with that name from
772 the list of bookmarks.\)"
773 (interactive (list nil current-prefix-arg))
775 (bookmark-buffer-file-name)
776 (error "Buffer not visiting a file or directory"))
778 (bookmark-maybe-load-default-file)
780 (setq bookmark-current-point (point))
781 (setq bookmark-yank-point (point))
782 (setq bookmark-current-buffer (current-buffer))
784 (let* ((default (or bookmark-current-bookmark
785 (bookmark-buffer-name)))
786 (str
787 (or name
788 (read-from-minibuffer
789 (format "Set bookmark (%s): " default)
791 (let ((now-map (copy-keymap minibuffer-local-map)))
792 (progn (define-key now-map "\C-w"
793 'bookmark-yank-word)
794 (define-key now-map "\C-u"
795 'bookmark-insert-current-bookmark))
796 now-map))))
797 (annotation nil))
798 (and (string-equal str "") (setq str default))
799 ;; Ask for an annotation buffer for this bookmark
800 (if bookmark-use-annotations
801 (bookmark-read-annotation parg str)
802 (progn
803 (bookmark-make str annotation parg (bookmark-info-current-node))
804 (setq bookmark-current-bookmark str)
805 (bookmark-bmenu-surreptitiously-rebuild-list)
806 (goto-char bookmark-current-point)))))
809 (defun bookmark-info-current-node ()
810 "If in Info-mode, return current node name (a string), else nil."
811 (if (eq major-mode 'Info-mode)
812 Info-current-node))
815 (defun bookmark-kill-line (&optional newline-too)
816 "Kill from point to end of line.
817 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
818 Does not affect the kill-ring."
819 (let ((eol (save-excursion (end-of-line) (point))))
820 (delete-region (point) eol)
821 (if (and newline-too (looking-at "\n"))
822 (delete-char 1))))
825 ;; Defvars to avoid compilation warnings:
826 (defvar bookmark-annotation-paragraph nil)
827 (defvar bookmark-annotation-name nil)
828 (defvar bookmark-annotation-buffer nil)
829 (defvar bookmark-annotation-file nil)
830 (defvar bookmark-annotation-point nil)
833 (defun bookmark-send-annotation ()
834 "Use buffer contents as the annotation for a bookmark.
835 Exclude lines that begin with `#'.
836 Store the annotation text in the bookmark list with
837 the bookmark (and file, and point) specified in buffer local variables."
838 (interactive)
839 (if (not (eq major-mode 'bookmark-read-annotation-mode))
840 (error "Not in bookmark-read-annotation-mode"))
841 (goto-char (point-min))
842 (while (< (point) (point-max))
843 (if (looking-at "^#")
844 (bookmark-kill-line t)
845 (forward-line 1)))
846 (let ((annotation (buffer-substring (point-min) (point-max)))
847 (parg bookmark-annotation-paragraph)
848 (bookmark bookmark-annotation-name)
849 (pt bookmark-annotation-point)
850 (buf bookmark-annotation-buffer))
851 ;; for bookmark-make-cell to work, we need to be
852 ;; in the relevant buffer, at the relevant point.
853 ;; Actually, bookmark-make-cell should probably be re-written,
854 ;; to avoid this need. Should I handle the error if a buffer is
855 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
856 (save-excursion
857 (pop-to-buffer buf)
858 (goto-char pt)
859 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
860 (setq bookmark-current-bookmark bookmark))
861 (bookmark-bmenu-surreptitiously-rebuild-list)
862 (goto-char bookmark-current-point))
863 (kill-buffer (current-buffer)))
866 (defun bookmark-default-annotation-text (bookmark)
867 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
868 "# All lines which start with a '#' will be deleted.\n"
869 "# Type C-c C-c when done.\n#\n"
870 "# Author: " (user-full-name) " <" (user-login-name) "@"
871 (system-name) ">\n"
872 "# Date: " (current-time-string) "\n"))
875 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
876 "Function to return default text to use for a bookmark annotation.
877 It takes the name of the bookmark, as a string, as an arg.")
879 (defun bookmark-read-annotation-mode (buf point parg bookmark)
880 "Mode for composing annotations for a bookmark.
881 Wants BUF POINT PARG and BOOKMARK.
882 When you have finished composing, type \\[bookmark-send-annotation] to send
883 the annotation.
885 \\{bookmark-read-annotation-mode-map}
887 (interactive)
888 (kill-all-local-variables)
889 (make-local-variable 'bookmark-annotation-paragraph)
890 (make-local-variable 'bookmark-annotation-name)
891 (make-local-variable 'bookmark-annotation-buffer)
892 (make-local-variable 'bookmark-annotation-file)
893 (make-local-variable 'bookmark-annotation-point)
894 (setq bookmark-annotation-paragraph parg)
895 (setq bookmark-annotation-name bookmark)
896 (setq bookmark-annotation-buffer buf)
897 (setq bookmark-annotation-file (buffer-file-name buf))
898 (setq bookmark-annotation-point point)
899 (use-local-map bookmark-read-annotation-mode-map)
900 (setq major-mode 'bookmark-read-annotation-mode)
901 (insert (funcall bookmark-read-annotation-text-func bookmark))
902 (run-hooks 'text-mode-hook))
905 (defun bookmark-read-annotation (parg bookmark)
906 "Pop up a buffer for entering a bookmark annotation.
907 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
908 (let ((buf (current-buffer))
909 (point (point)))
910 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
911 (bookmark-read-annotation-mode buf point parg bookmark)))
914 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
915 "Keymap for editing an annotation of a bookmark.")
918 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
919 'bookmark-send-edited-annotation)
922 (defun bookmark-edit-annotation-mode (bookmark)
923 "Mode for editing the annotation of bookmark BOOKMARK.
924 When you have finished composing, type \\[bookmark-send-annotation].
926 \\{bookmark-edit-annotation-mode-map}
928 (interactive)
929 (kill-all-local-variables)
930 (make-local-variable 'bookmark-annotation-name)
931 (setq bookmark-annotation-name bookmark)
932 (use-local-map bookmark-edit-annotation-mode-map)
933 (setq major-mode 'bookmark-edit-annotation-mode)
934 (insert (funcall bookmark-read-annotation-text-func bookmark))
935 (let ((annotation (bookmark-get-annotation bookmark)))
936 (if (and (not (eq annotation nil))
937 (not (string-equal annotation "")))
938 (insert annotation)))
939 (run-hooks 'text-mode-hook))
942 (defun bookmark-send-edited-annotation ()
943 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
944 (interactive)
945 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
946 (error "Not in bookmark-edit-annotation-mode"))
947 (goto-char (point-min))
948 (while (< (point) (point-max))
949 (if (looking-at "^#")
950 (bookmark-kill-line t)
951 (forward-line 1)))
952 (let ((annotation (buffer-substring (point-min) (point-max)))
953 (bookmark bookmark-annotation-name))
954 (bookmark-set-annotation bookmark annotation)
955 (bookmark-bmenu-surreptitiously-rebuild-list)
956 (goto-char bookmark-current-point))
957 (kill-buffer (current-buffer)))
960 (defun bookmark-edit-annotation (bookmark)
961 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
962 (let ((buf (current-buffer))
963 (point (point)))
964 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
965 (bookmark-edit-annotation-mode bookmark)))
968 (defun bookmark-insert-current-bookmark ()
969 "Insert this buffer's value of bookmark-current-bookmark.
970 Default to file name if it's nil."
971 (interactive)
972 (let ((str
973 (save-excursion
974 (set-buffer bookmark-current-buffer)
975 bookmark-current-bookmark)))
976 (if str (insert str) (bookmark-insert-buffer-name))))
979 (defun bookmark-insert-buffer-name ()
980 "Insert the current file name into the bookmark name being set.
981 The directory part of the file name is not used."
982 (interactive)
983 (let ((str
984 (save-excursion
985 (set-buffer bookmark-current-buffer)
986 (bookmark-buffer-name))))
987 (insert str)))
990 (defun bookmark-buffer-name ()
991 "Return the name of the current buffer's file, non-directory.
992 In Info, return the current node."
993 (cond
994 ;; Are we in Info?
995 ((string-equal mode-name "Info") Info-current-node)
996 ;; Or are we a file?
997 (buffer-file-name (file-name-nondirectory buffer-file-name))
998 ;; Or are we a directory?
999 ((and (boundp 'dired-directory) dired-directory)
1000 (let* ((dirname (if (stringp dired-directory)
1001 dired-directory
1002 (car dired-directory)))
1003 (idx (1- (length dirname))))
1004 ;; Strip the trailing slash.
1005 (if (= ?/ (aref dirname idx))
1006 (file-name-nondirectory (substring dirname 0 idx))
1007 ;; Else return the current-buffer
1008 (buffer-name (current-buffer)))))
1009 ;; If all else fails, use the buffer's name.
1011 (buffer-name (current-buffer)))))
1014 (defun bookmark-yank-word ()
1015 (interactive)
1016 ;; get the next word from the buffer and append it to the name of
1017 ;; the bookmark currently being set.
1018 (let ((string (save-excursion
1019 (set-buffer bookmark-current-buffer)
1020 (goto-char bookmark-yank-point)
1021 (buffer-substring-no-properties
1022 (point)
1023 (save-excursion
1024 (forward-word 1)
1025 (setq bookmark-yank-point (point)))))))
1026 (insert string)))
1029 (defun bookmark-buffer-file-name ()
1030 "Return the current buffer's file in a way useful for bookmarks.
1031 For example, if this is a Info buffer, return the Info file's name."
1032 (if (eq major-mode 'Info-mode)
1033 Info-current-file
1035 buffer-file-name
1036 (if (and (boundp 'dired-directory) dired-directory)
1037 (if (stringp dired-directory)
1038 dired-directory
1039 (car dired-directory))))))
1042 (defun bookmark-maybe-load-default-file ()
1043 (and (not bookmarks-already-loaded)
1044 (null bookmark-alist)
1045 (prog2
1046 (and
1047 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1048 ;; to be renamed.
1049 (file-exists-p (expand-file-name bookmark-old-default-file))
1050 (not (file-exists-p (expand-file-name bookmark-default-file)))
1051 (rename-file (expand-file-name bookmark-old-default-file)
1052 (expand-file-name bookmark-default-file)))
1053 ;; return t so the `and' will continue...
1056 (file-readable-p (expand-file-name bookmark-default-file))
1057 (progn
1058 (bookmark-load bookmark-default-file t t)
1059 (setq bookmarks-already-loaded t))))
1062 (defun bookmark-maybe-sort-alist ()
1063 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1064 ;;is non-nil, then return a sorted copy of the alist.
1065 (if bookmark-sort-flag
1066 (setq bookmark-alist
1067 (sort (copy-alist bookmark-alist)
1068 (function
1069 (lambda (x y) (string-lessp (car x) (car y))))))))
1072 ;;;###autoload
1073 (defun bookmark-jump (bookmark)
1074 "Jump to bookmark BOOKMARK (a point in some file).
1075 You may have a problem using this function if the value of variable
1076 `bookmark-alist' is nil. If that happens, you need to load in some
1077 bookmarks. See help on function `bookmark-load' for more about
1078 this.
1080 If the file pointed to by BOOKMARK no longer exists, you will be asked
1081 if you wish to give the bookmark a new location, and bookmark-jump
1082 will then jump to the new location, as well as recording it in place
1083 of the old one in the permanent bookmark record."
1084 (interactive
1085 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1086 (bookmark-maybe-historicize-string bookmark)
1087 (let ((cell (bookmark-jump-noselect bookmark)))
1088 (and cell
1089 (switch-to-buffer (car cell))
1090 (goto-char (cdr cell))
1091 (if bookmark-automatically-show-annotations
1092 ;; if there is an annotation for this bookmark,
1093 ;; show it in a buffer.
1094 (bookmark-show-annotation bookmark)))))
1097 (defun bookmark-jump-noselect (str)
1098 ;; a leetle helper for bookmark-jump :-)
1099 ;; returns (BUFFER . POINT)
1100 (bookmark-maybe-load-default-file)
1101 (let* ((file (expand-file-name (bookmark-get-filename str)))
1102 (forward-str (bookmark-get-front-context-string str))
1103 (behind-str (bookmark-get-rear-context-string str))
1104 (place (bookmark-get-position str))
1105 (info-node (bookmark-get-info-node str))
1106 (orig-file file)
1108 (if (or
1109 (file-exists-p file)
1110 ;; else try some common compression extensions
1111 ;; and Emacs better handle it right!
1112 ;; Sigh: I think it may *not* be handled at the moment. What
1113 ;; to do about this?
1114 (setq file
1116 (let ((altname (concat file ".Z")))
1117 (and (file-exists-p altname)
1118 altname))
1119 (let ((altname (concat file ".gz")))
1120 (and (file-exists-p altname)
1121 altname))
1122 (let ((altname (concat file ".z")))
1123 (and (file-exists-p altname)
1124 altname)))))
1125 (save-excursion
1126 (if info-node
1127 ;; Info nodes must be visited with care.
1128 (progn
1129 (require 'info)
1130 (Info-find-node file info-node))
1131 ;; Else no Info. Can do an ordinary find-file:
1132 (set-buffer (find-file-noselect file))
1133 (goto-char place))
1135 ;; Go searching forward first. Then, if forward-str exists and
1136 ;; was found in the file, we can search backward for behind-str.
1137 ;; Rationale is that if text was inserted between the two in the
1138 ;; file, it's better to be put before it so you can read it,
1139 ;; rather than after and remain perhaps unaware of the changes.
1140 (if forward-str
1141 (if (search-forward forward-str (point-max) t)
1142 (goto-char (match-beginning 0))))
1143 (if behind-str
1144 (if (search-backward behind-str (point-min) t)
1145 (goto-char (match-end 0))))
1146 ;; added by db
1147 (setq bookmark-current-bookmark str)
1148 (cons (current-buffer) (point)))
1149 (progn
1150 (ding)
1151 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1152 " nonexistent. Relocate \""
1154 "\"? "))
1155 (progn
1156 (bookmark-relocate str)
1157 ;; gasp! It's a recursive function call in Emacs Lisp!
1158 (bookmark-jump-noselect str))
1159 (message
1160 "Bookmark not relocated; consider removing it \(%s\)." str)
1161 nil)))))
1164 ;;;###autoload
1165 (defun bookmark-relocate (bookmark)
1166 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1167 This makes an already existing bookmark point to that file, instead of
1168 the one it used to point at. Useful when a file has been renamed
1169 after a bookmark was set in it."
1170 (interactive (bookmark-completing-read "Bookmark to relocate"))
1171 (bookmark-maybe-historicize-string bookmark)
1172 (bookmark-maybe-load-default-file)
1173 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1174 (newloc (expand-file-name
1175 (read-file-name
1176 (format "Relocate %s to: " bookmark)
1177 (file-name-directory bmrk-filename)))))
1178 (bookmark-set-filename bookmark newloc)))
1181 ;;;###autoload
1182 (defun bookmark-insert-location (bookmark &optional no-history)
1183 "Insert the name of the file associated with BOOKMARK.
1184 Optional second arg NO-HISTORY means don't record this in the
1185 minibuffer history list `bookmark-history'."
1186 (interactive (bookmark-completing-read "Insert bookmark location"))
1187 (or no-history (bookmark-maybe-historicize-string bookmark))
1188 (let ((start (point)))
1189 (prog1
1190 (insert (bookmark-location bookmark)) ; *Return this line*
1191 (if window-system
1192 (put-text-property start
1193 (save-excursion (re-search-backward
1194 "[^ \t]")
1195 (1+ (point)))
1196 'mouse-face 'highlight)))))
1198 ;;;###autoload
1199 (defalias 'bookmark-locate 'bookmark-insert-location)
1201 (defun bookmark-location (bookmark)
1202 "Return the name of the file associated with BOOKMARK."
1203 (bookmark-maybe-load-default-file)
1204 (bookmark-get-filename bookmark))
1207 ;;;###autoload
1208 (defun bookmark-rename (old &optional new)
1209 "Change the name of OLD bookmark to NEW name.
1210 If called from keyboard, prompt for OLD and NEW. If called from
1211 menubar, select OLD from a menu and prompt for NEW.
1213 If called from Lisp, prompt for NEW if only OLD was passed as an
1214 argument. If called with two strings, then no prompting is done. You
1215 must pass at least OLD when calling from Lisp.
1217 While you are entering the new name, consecutive C-w's insert
1218 consecutive words from the text of the buffer into the new bookmark
1219 name."
1220 (interactive (bookmark-completing-read "Old bookmark name"))
1221 (bookmark-maybe-historicize-string old)
1222 (bookmark-maybe-load-default-file)
1223 (progn
1224 (setq bookmark-current-point (point))
1225 (setq bookmark-yank-point (point))
1226 (setq bookmark-current-buffer (current-buffer))
1227 (let ((newname
1228 (or new ; use second arg, if non-nil
1229 (read-from-minibuffer
1230 "New name: "
1232 (let ((now-map (copy-keymap minibuffer-local-map)))
1233 (define-key now-map "\C-w" 'bookmark-yank-word)
1234 now-map)
1236 'bookmark-history))))
1237 (progn
1238 (bookmark-set-name old newname)
1239 (setq bookmark-current-bookmark newname)
1240 (bookmark-bmenu-surreptitiously-rebuild-list)
1241 (setq bookmark-alist-modification-count
1242 (1+ bookmark-alist-modification-count))
1243 (if (bookmark-time-to-save-p)
1244 (bookmark-save))))))
1247 ;;;###autoload
1248 (defun bookmark-insert (bookmark)
1249 "Insert the text of the file pointed to by bookmark BOOKMARK.
1250 You may have a problem using this function if the value of variable
1251 `bookmark-alist' is nil. If that happens, you need to load in some
1252 bookmarks. See help on function `bookmark-load' for more about
1253 this."
1254 (interactive (bookmark-completing-read "Insert bookmark contents"))
1255 (bookmark-maybe-historicize-string bookmark)
1256 (bookmark-maybe-load-default-file)
1257 (let ((orig-point (point))
1258 (str-to-insert
1259 (save-excursion
1260 (set-buffer (car (bookmark-jump-noselect bookmark)))
1261 (buffer-substring (point-min) (point-max)))))
1262 (insert str-to-insert)
1263 (push-mark)
1264 (goto-char orig-point)))
1267 ;;;###autoload
1268 (defun bookmark-delete (bookmark &optional batch)
1269 "Delete BOOKMARK from the bookmark list.
1270 Removes only the first instance of a bookmark with that name. If
1271 there are one or more other bookmarks with the same name, they will
1272 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1273 one most recently used in this file, if any\).
1274 Optional second arg BATCH means don't update the bookmark list buffer,
1275 probably because we were called from there."
1276 (interactive
1277 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1278 (bookmark-maybe-historicize-string bookmark)
1279 (bookmark-maybe-load-default-file)
1280 (let ((will-go (bookmark-get-bookmark bookmark)))
1281 (setq bookmark-alist (delq will-go bookmark-alist))
1282 ;; Added by db, nil bookmark-current-bookmark if the last
1283 ;; occurrence has been deleted
1284 (or (bookmark-get-bookmark bookmark-current-bookmark)
1285 (setq bookmark-current-bookmark nil)))
1286 ;; Don't rebuild the list
1287 (if batch
1289 (bookmark-bmenu-surreptitiously-rebuild-list)
1290 (setq bookmark-alist-modification-count
1291 (1+ bookmark-alist-modification-count))
1292 (if (bookmark-time-to-save-p)
1293 (bookmark-save))))
1296 (defun bookmark-time-to-save-p (&optional last-time)
1297 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1298 ;; finds out whether it's time to save bookmarks to a file, by
1299 ;; examining the value of variable bookmark-save-flag, and maybe
1300 ;; bookmark-alist-modification-count. Returns t if they should be
1301 ;; saved, nil otherwise. if last-time is non-nil, then this is
1302 ;; being called when emacs is killed.
1303 (cond (last-time
1304 (and (> bookmark-alist-modification-count 0)
1305 bookmark-save-flag))
1306 ((numberp bookmark-save-flag)
1307 (>= bookmark-alist-modification-count bookmark-save-flag))
1309 nil)))
1312 ;;;###autoload
1313 (defun bookmark-write ()
1314 "Write bookmarks to a file (reading the file name with the minibuffer).
1315 Don't use this in Lisp programs; use `bookmark-save' instead."
1316 (interactive)
1317 (bookmark-maybe-load-default-file)
1318 (bookmark-save t))
1321 ;;;###autoload
1322 (defun bookmark-save (&optional parg file)
1323 "Save currently defined bookmarks.
1324 Saves by default in the file defined by the variable
1325 `bookmark-default-file'. With a prefix arg, save it in file FILE
1326 \(second argument\).
1328 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1329 and FILE, and if you just want it to write to the default file, then
1330 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1331 instead. If you pass in one argument, and it is non-nil, then the
1332 user will be interactively queried for a file to save in.
1334 When you want to load in the bookmarks from a file, use
1335 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1336 for a file, defaulting to the file defined by variable
1337 `bookmark-default-file'."
1338 (interactive "P")
1339 (bookmark-maybe-load-default-file)
1340 (cond
1341 ((and (null parg) (null file))
1342 ;;whether interactive or not, write to default file
1343 (bookmark-write-file bookmark-default-file))
1344 ((and (null parg) file)
1345 ;;whether interactive or not, write to given file
1346 (bookmark-write-file file))
1347 ((and parg (not file))
1348 ;;have been called interactively w/ prefix arg
1349 (let ((file (read-file-name "File to save bookmarks in: ")))
1350 (bookmark-write-file file)))
1351 (t ; someone called us with prefix-arg *and* a file, so just write to file
1352 (bookmark-write-file file)))
1353 ;; signal that we have synced the bookmark file by setting this to
1354 ;; 0. If there was an error at any point before, it will not get
1355 ;; set, which is what we want.
1356 (setq bookmark-alist-modification-count 0))
1360 (defun bookmark-write-file (file)
1361 (save-excursion
1362 (save-window-excursion
1363 (if (>= baud-rate 9600)
1364 (message "Saving bookmarks to file %s..." file))
1365 (set-buffer (let ((enable-local-variables nil))
1366 (find-file-noselect file)))
1367 (goto-char (point-min))
1368 (delete-region (point-min) (point-max))
1369 (bookmark-insert-file-format-version-stamp)
1370 (pp bookmark-alist (current-buffer))
1371 (let ((version-control
1372 (cond
1373 ((null bookmark-version-control) nil)
1374 ((eq 'never bookmark-version-control) 'never)
1375 ((eq 'nospecial bookmark-version-control) version-control)
1377 t))))
1378 (write-file file)
1379 (kill-buffer (current-buffer))
1380 (if (>= baud-rate 9600)
1381 (message "Saving bookmarks to file %s...done" file))
1382 ))))
1385 ;;;###autoload
1386 (defun bookmark-load (file &optional revert no-msg)
1387 "Load bookmarks from FILE (which must be in bookmark format).
1388 Appends loaded bookmarks to the front of the list of bookmarks. If
1389 optional second argument REVERT is non-nil, existing bookmarks are
1390 destroyed. Optional third arg NO-MSG means don't display any messages
1391 while loading.
1393 If you load a file that doesn't contain a proper bookmark alist, you
1394 will corrupt Emacs's bookmark list. Generally, you should only load
1395 in files that were created with the bookmark functions in the first
1396 place. Your own personal bookmark file, `~/.emacs.bmk', is
1397 maintained automatically by Emacs; you shouldn't need to load it
1398 explicitly."
1399 (interactive
1400 (list (read-file-name
1401 (format "Load bookmarks from: (%s) "
1402 bookmark-default-file)
1403 ;;Default might not be used often,
1404 ;;but there's no better default, and
1405 ;;I guess it's better than none at all.
1406 "~/" bookmark-default-file 'confirm)))
1407 (setq file (expand-file-name file))
1408 (if (file-readable-p file)
1409 (save-excursion
1410 (save-window-excursion
1411 (if (and (null no-msg) (>= baud-rate 9600))
1412 (message "Loading bookmarks from %s..." file))
1413 (set-buffer (let ((enable-local-variables nil))
1414 (find-file-noselect file)))
1415 (goto-char (point-min))
1416 (bookmark-maybe-upgrade-file-format)
1417 (let ((blist (bookmark-alist-from-buffer)))
1418 (if (listp blist)
1419 (progn
1420 (if (not revert)
1421 (setq bookmark-alist-modification-count
1422 (1+ bookmark-alist-modification-count))
1423 (setq bookmark-alist-modification-count 0))
1424 (setq bookmark-alist
1425 (append blist (if (not revert) bookmark-alist)))
1426 (bookmark-bmenu-surreptitiously-rebuild-list))
1427 (error "Invalid bookmark list in %s" file)))
1428 (kill-buffer (current-buffer)))
1429 (if (and (null no-msg) (>= baud-rate 9600))
1430 (message "Loading bookmarks from %s...done" file)))
1431 (error "Cannot read bookmark file %s" file)))
1435 ;;; Code supporting the dired-like bookmark menu. Prefix is
1436 ;;; "bookmark-bmenu" for "buffer-menu":
1439 (defvar bookmark-bmenu-bookmark-column nil)
1442 (defvar bookmark-bmenu-hidden-bookmarks ())
1445 (defvar bookmark-bmenu-mode-map nil)
1448 (if bookmark-bmenu-mode-map
1450 (setq bookmark-bmenu-mode-map (make-keymap))
1451 (suppress-keymap bookmark-bmenu-mode-map t)
1452 (define-key bookmark-bmenu-mode-map "q" 'bookmark-bmenu-quit)
1453 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1454 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1455 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1456 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1457 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1458 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1459 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1460 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1461 (define-key bookmark-bmenu-mode-map "\C-o"
1462 'bookmark-bmenu-switch-other-window)
1463 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1464 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1465 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1466 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1467 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1468 (define-key bookmark-bmenu-mode-map " " 'next-line)
1469 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1470 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1471 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1472 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1473 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1474 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1475 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1476 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1477 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1478 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1479 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1480 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1481 (define-key bookmark-bmenu-mode-map [mouse-2]
1482 'bookmark-bmenu-other-window-with-mouse))
1486 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1487 ;; data.
1488 (put 'bookmark-bmenu-mode 'mode-class 'special)
1491 ;; todo: need to display whether or not bookmark exists as a buffer in
1492 ;; flag column.
1494 ;; Format:
1495 ;; FLAGS BOOKMARK [ LOCATION ]
1498 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1499 "Rebuild the Bookmark List if it exists.
1500 Don't affect the buffer ring order."
1501 (if (get-buffer "*Bookmark List*")
1502 (save-excursion
1503 (save-window-excursion
1504 (bookmark-bmenu-list)))))
1507 ;;;###autoload
1508 (defun bookmark-bmenu-list ()
1509 "Display a list of existing bookmarks.
1510 The list is displayed in a buffer named `*Bookmark List*'.
1511 The leftmost column displays a D if the bookmark is flagged for
1512 deletion, or > if it is flagged for displaying."
1513 (interactive)
1514 (bookmark-maybe-load-default-file)
1515 (if (interactive-p)
1516 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1517 (set-buffer (get-buffer-create "*Bookmark List*")))
1518 (let ((buffer-read-only nil))
1519 (delete-region (point-max) (point-min))
1520 (goto-char (point-min)) ;sure are playing it safe...
1521 (insert "% Bookmark\n- --------\n")
1522 (bookmark-maybe-sort-alist)
1523 (mapcar
1524 (lambda (full-record)
1525 ;; if a bookmark has an annotation, prepend a "*"
1526 ;; in the list of bookmarks.
1527 (let ((annotation (bookmark-get-annotation
1528 (bookmark-name-from-full-record full-record))))
1529 (if (and (not (eq annotation nil))
1530 (not (string-equal annotation "")))
1531 (insert " *")
1532 (insert " "))
1533 (let ((start (point)))
1534 (insert (bookmark-name-from-full-record full-record))
1535 (if window-system
1536 (put-text-property start
1537 (save-excursion (re-search-backward
1538 "[^ \t]")
1539 (1+ (point)))
1540 'mouse-face 'highlight))
1541 (insert "\n")
1543 bookmark-alist))
1544 (goto-char (point-min))
1545 (forward-line 2)
1546 (bookmark-bmenu-mode)
1547 (if bookmark-bmenu-toggle-filenames
1548 (bookmark-bmenu-toggle-filenames t)))
1550 ;;;###autoload
1551 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1552 ;;;###autoload
1553 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1557 (defun bookmark-bmenu-mode ()
1558 "Major mode for editing a list of bookmarks.
1559 Each line describes one of the bookmarks in Emacs.
1560 Letters do not insert themselves; instead, they are commands.
1561 Bookmark names preceded by a \"*\" have annotations.
1562 \\<bookmark-bmenu-mode-map>
1563 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1564 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1565 Also show bookmarks marked using m in other windows.
1566 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1567 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1568 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1569 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1570 together with bookmark selected before this one in another window.
1571 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1572 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1573 so the bookmark menu bookmark remains visible in its window.
1574 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1575 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1576 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1577 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1578 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1579 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1580 With a prefix arg, prompts for a file to save in.
1581 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1582 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1583 With prefix argument, also move up one line.
1584 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1585 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1586 in another buffer.
1587 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1588 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1589 (kill-all-local-variables)
1590 (use-local-map bookmark-bmenu-mode-map)
1591 (setq truncate-lines t)
1592 (setq buffer-read-only t)
1593 (setq major-mode 'bookmark-bmenu-mode)
1594 (setq mode-name "Bookmark Menu")
1595 (run-hooks 'bookmark-bmenu-mode-hook))
1598 (defun bookmark-bmenu-toggle-filenames (&optional show)
1599 "Toggle whether filenames are shown in the bookmark list.
1600 Optional argument SHOW means show them unconditionally."
1601 (interactive)
1602 (cond
1603 (show
1604 (setq bookmark-bmenu-toggle-filenames nil)
1605 (bookmark-bmenu-show-filenames)
1606 (setq bookmark-bmenu-toggle-filenames t))
1607 (bookmark-bmenu-toggle-filenames
1608 (bookmark-bmenu-hide-filenames)
1609 (setq bookmark-bmenu-toggle-filenames nil))
1611 (bookmark-bmenu-show-filenames)
1612 (setq bookmark-bmenu-toggle-filenames t))))
1615 (defun bookmark-bmenu-show-filenames (&optional force)
1616 (if (and (not force) bookmark-bmenu-toggle-filenames)
1617 nil ;already shown, so do nothing
1618 (save-excursion
1619 (save-window-excursion
1620 (goto-char (point-min))
1621 (forward-line 2)
1622 (setq bookmark-bmenu-hidden-bookmarks ())
1623 (let ((buffer-read-only nil))
1624 (while (< (point) (point-max))
1625 (let ((bmrk (bookmark-bmenu-bookmark)))
1626 (setq bookmark-bmenu-hidden-bookmarks
1627 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1628 (let ((start (save-excursion (end-of-line) (point))))
1629 (move-to-column bookmark-bmenu-file-column t)
1630 ;; Strip off `mouse-face' from the white spaces region.
1631 (if window-system
1632 (remove-text-properties start (point)
1633 '(mouse-face))))
1634 (delete-region (point) (progn (end-of-line) (point)))
1635 (insert " ")
1636 ;; Pass the NO-HISTORY arg:
1637 (bookmark-insert-location bmrk t)
1638 (forward-line 1))))))))
1641 (defun bookmark-bmenu-hide-filenames (&optional force)
1642 (if (and (not force) bookmark-bmenu-toggle-filenames)
1643 ;; nothing to hide if above is nil
1644 (save-excursion
1645 (save-window-excursion
1646 (goto-char (point-min))
1647 (forward-line 2)
1648 (setq bookmark-bmenu-hidden-bookmarks
1649 (nreverse bookmark-bmenu-hidden-bookmarks))
1650 (save-excursion
1651 (goto-char (point-min))
1652 (search-forward "Bookmark")
1653 (backward-word 1)
1654 (setq bookmark-bmenu-bookmark-column (current-column)))
1655 (save-excursion
1656 (let ((buffer-read-only nil))
1657 (while bookmark-bmenu-hidden-bookmarks
1658 (move-to-column bookmark-bmenu-bookmark-column t)
1659 (bookmark-kill-line)
1660 (let ((start (point)))
1661 (insert (car bookmark-bmenu-hidden-bookmarks))
1662 (if window-system
1663 (put-text-property start
1664 (save-excursion (re-search-backward
1665 "[^ \t]")
1666 (1+ (point)))
1667 'mouse-face 'highlight)))
1668 (setq bookmark-bmenu-hidden-bookmarks
1669 (cdr bookmark-bmenu-hidden-bookmarks))
1670 (forward-line 1))))))))
1673 ;; if you look at this next function from far away, it resembles a
1674 ;; gun. But only with this comment above...
1675 (defun bookmark-bmenu-check-position ()
1676 ;; Returns t if on a line with a bookmark.
1677 ;; Otherwise, repositions and returns t.
1678 ;; written by David Hughes <djh@harston.cv.com>
1679 ;; Mucho thanks, David! -karl
1680 (cond ((< (count-lines (point-min) (point)) 2)
1681 (goto-char (point-min))
1682 (forward-line 2)
1684 ((and (bolp) (eobp))
1685 (beginning-of-line 0)
1688 t)))
1691 (defun bookmark-bmenu-bookmark ()
1692 ;; return a string which is bookmark of this line.
1693 (if (bookmark-bmenu-check-position)
1694 (save-excursion
1695 (save-window-excursion
1696 (goto-char (point-min))
1697 (search-forward "Bookmark")
1698 (backward-word 1)
1699 (setq bookmark-bmenu-bookmark-column (current-column)))))
1700 (if bookmark-bmenu-toggle-filenames
1701 (bookmark-bmenu-hide-filenames))
1702 (save-excursion
1703 (save-window-excursion
1704 (beginning-of-line)
1705 (forward-char bookmark-bmenu-bookmark-column)
1706 (prog1
1707 (buffer-substring-no-properties (point)
1708 (progn
1709 (end-of-line)
1710 (point)))
1711 ;; well, this is certainly crystal-clear:
1712 (if bookmark-bmenu-toggle-filenames
1713 (bookmark-bmenu-toggle-filenames t))))))
1716 (defun bookmark-show-annotation (bookmark)
1717 "Display the annotation for bookmark named BOOKMARK in a buffer,
1718 if an annotation exists."
1719 (let ((annotation (bookmark-get-annotation bookmark)))
1720 (if (and (not (eq annotation nil))
1721 (not (string-equal annotation "")))
1722 (progn
1723 (save-excursion
1724 (let ((old-buf (current-buffer)))
1725 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1726 (delete-region (point-min) (point-max))
1727 ; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1728 (insert annotation)
1729 (goto-char (point-min))
1730 (pop-to-buffer old-buf)))))))
1733 (defun bookmark-show-all-annotations ()
1734 "Display the annotations for all bookmarks in a buffer."
1735 (let ((old-buf (current-buffer)))
1736 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1737 (delete-region (point-min) (point-max))
1738 (mapcar
1739 (lambda (full-record)
1740 (let* ((name (bookmark-name-from-full-record full-record))
1741 (ann (bookmark-get-annotation name)))
1742 (insert (concat name ":\n"))
1743 (if (and (not (eq ann nil)) (not (string-equal ann "")))
1744 ;; insert the annotation, indented by 4 spaces.
1745 (progn
1746 (save-excursion (insert ann))
1747 (while (< (point) (point-max))
1748 (beginning-of-line) ; paranoia
1749 (insert " ")
1750 (forward-line)
1751 (end-of-line))))))
1752 bookmark-alist)
1753 (goto-char (point-min))
1754 (pop-to-buffer old-buf)))
1757 (defun bookmark-bmenu-mark ()
1758 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1759 (interactive)
1760 (beginning-of-line)
1761 (if (bookmark-bmenu-check-position)
1762 (let ((buffer-read-only nil))
1763 (delete-char 1)
1764 (insert ?>)
1765 (forward-line 1))))
1768 (defun bookmark-bmenu-select ()
1769 "Select this line's bookmark; also display bookmarks marked with `>'.
1770 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1771 (interactive)
1772 (if (bookmark-bmenu-check-position)
1773 (let ((bmrk (bookmark-bmenu-bookmark))
1774 (menu (current-buffer))
1775 (others ())
1776 tem)
1777 (goto-char (point-min))
1778 (while (re-search-forward "^>" nil t)
1779 (setq tem (bookmark-bmenu-bookmark))
1780 (let ((buffer-read-only nil))
1781 (delete-char -1)
1782 (insert ?\ ))
1783 (or (string-equal tem bmrk)
1784 (member tem others)
1785 (setq others (cons tem others))))
1786 (setq others (nreverse others)
1787 tem (/ (1- (frame-height)) (1+ (length others))))
1788 (delete-other-windows)
1789 (bookmark-jump bmrk)
1790 (bury-buffer menu)
1791 (if others
1792 (while others
1793 (split-window nil tem)
1794 (other-window 1)
1795 (bookmark-jump (car others))
1796 (setq others (cdr others)))
1797 (other-window 1)))))
1800 (defun bookmark-bmenu-save (parg)
1801 "Save the current list into a bookmark file.
1802 With a prefix arg, prompts for a file to save them in."
1803 (interactive "P")
1804 (save-excursion
1805 (save-window-excursion
1806 (bookmark-save parg))))
1809 (defun bookmark-bmenu-load ()
1810 "Load the bookmark file and rebuild the bookmark menu-buffer."
1811 (interactive)
1812 (if (bookmark-bmenu-check-position)
1813 (save-excursion
1814 (save-window-excursion
1815 ;; This will call `bookmark-bmenu-list'
1816 (call-interactively 'bookmark-load)))))
1819 (defun bookmark-bmenu-1-window ()
1820 "Select this line's bookmark, alone, in full frame."
1821 (interactive)
1822 (if (bookmark-bmenu-check-position)
1823 (progn
1824 (bookmark-jump (bookmark-bmenu-bookmark))
1825 (bury-buffer (other-buffer))
1826 (delete-other-windows))))
1829 (defun bookmark-bmenu-2-window ()
1830 "Select this line's bookmark, with previous buffer in second window."
1831 (interactive)
1832 (if (bookmark-bmenu-check-position)
1833 (let ((bmrk (bookmark-bmenu-bookmark))
1834 (menu (current-buffer))
1835 (pop-up-windows t))
1836 (delete-other-windows)
1837 (switch-to-buffer (other-buffer))
1838 (let* ((pair (bookmark-jump-noselect bmrk))
1839 (buff (car pair))
1840 (pos (cdr pair)))
1841 (pop-to-buffer buff)
1842 (goto-char pos))
1843 (bury-buffer menu))))
1846 (defun bookmark-bmenu-this-window ()
1847 "Select this line's bookmark in this window."
1848 (interactive)
1849 (if (bookmark-bmenu-check-position)
1850 (bookmark-jump (bookmark-bmenu-bookmark))))
1853 (defun bookmark-bmenu-other-window ()
1854 "Select this line's bookmark in other window, leaving bookmark menu visible."
1855 (interactive)
1856 (let ((bookmark (bookmark-bmenu-bookmark)))
1857 (if (bookmark-bmenu-check-position)
1858 (let* ((pair (bookmark-jump-noselect bookmark))
1859 (buff (car pair))
1860 (pos (cdr pair)))
1861 (switch-to-buffer-other-window buff)
1862 (goto-char pos)
1863 (set-window-point (get-buffer-window buff) pos)
1864 (bookmark-show-annotation bookmark)))))
1867 (defun bookmark-bmenu-switch-other-window ()
1868 "Make the other window select this line's bookmark.
1869 The current window remains selected."
1870 (interactive)
1871 (let ((bookmark (bookmark-bmenu-bookmark)))
1872 (if (bookmark-bmenu-check-position)
1873 (let* ((pair (bookmark-jump-noselect bookmark))
1874 (buff (car pair))
1875 (pos (cdr pair)))
1876 (display-buffer buff)
1877 (let ((o-buffer (current-buffer)))
1878 ;; save-excursion won't do
1879 (set-buffer buff)
1880 (goto-char pos)
1881 (set-window-point (get-buffer-window buff) pos)
1882 (set-buffer o-buffer))
1883 (bookmark-show-annotation bookmark)))))
1885 (defun bookmark-bmenu-other-window-with-mouse (event)
1886 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1887 (interactive "e")
1888 (save-excursion
1889 (set-buffer (window-buffer (posn-window (event-end event))))
1890 (save-excursion
1891 (goto-char (posn-point (event-end event)))
1892 (bookmark-bmenu-other-window))))
1895 (defun bookmark-bmenu-show-annotation ()
1896 "Show the annotation for the current bookmark in another window."
1897 (interactive)
1898 (let ((bookmark (bookmark-bmenu-bookmark)))
1899 (if (bookmark-bmenu-check-position)
1900 (bookmark-show-annotation bookmark))))
1903 (defun bookmark-bmenu-show-all-annotations ()
1904 "Show the annotation for all bookmarks in another window."
1905 (interactive)
1906 (bookmark-show-all-annotations))
1909 (defun bookmark-bmenu-edit-annotation ()
1910 "Edit the annotation for the current bookmark in another window."
1911 (interactive)
1912 (let ((bookmark (bookmark-bmenu-bookmark)))
1913 (if (bookmark-bmenu-check-position)
1914 (bookmark-edit-annotation bookmark))))
1917 (defun bookmark-bmenu-quit ()
1918 "Quit the bookmark menu."
1919 (interactive)
1920 (let ((buffer (current-buffer)))
1921 (switch-to-buffer (other-buffer))
1922 (bury-buffer buffer)))
1925 (defun bookmark-bmenu-unmark (&optional backup)
1926 "Cancel all requested operations on bookmark on this line and move down.
1927 Optional BACKUP means move up."
1928 (interactive "P")
1929 (beginning-of-line)
1930 (if (bookmark-bmenu-check-position)
1931 (progn
1932 (let ((buffer-read-only nil))
1933 (delete-char 1)
1934 ;; any flags to reset according to circumstances? How about a
1935 ;; flag indicating whether this bookmark is being visited?
1936 ;; well, we don't have this now, so maybe later.
1937 (insert " "))
1938 (forward-line (if backup -1 1)))))
1941 (defun bookmark-bmenu-backup-unmark ()
1942 "Move up and cancel all requested operations on bookmark on line above."
1943 (interactive)
1944 (forward-line -1)
1945 (if (bookmark-bmenu-check-position)
1946 (progn
1947 (bookmark-bmenu-unmark)
1948 (forward-line -1))))
1951 (defun bookmark-bmenu-delete ()
1952 "Mark bookmark on this line to be deleted.
1953 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1954 (interactive)
1955 (beginning-of-line)
1956 (if (bookmark-bmenu-check-position)
1957 (let ((buffer-read-only nil))
1958 (delete-char 1)
1959 (insert ?D)
1960 (forward-line 1))))
1963 (defun bookmark-bmenu-delete-backwards ()
1964 "Mark bookmark on this line to be deleted, then move up one line.
1965 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1966 (interactive)
1967 (bookmark-bmenu-delete)
1968 (forward-line -2)
1969 (if (bookmark-bmenu-check-position)
1970 (forward-line 1)))
1973 (defun bookmark-bmenu-execute-deletions ()
1974 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1975 (interactive)
1976 (message "Deleting bookmarks...")
1977 (let ((hide-em bookmark-bmenu-toggle-filenames)
1978 (o-point (point))
1979 (o-str (save-excursion
1980 (beginning-of-line)
1981 (if (looking-at "^D")
1983 (buffer-substring
1984 (point)
1985 (progn (end-of-line) (point))))))
1986 (o-col (current-column)))
1987 (if hide-em (bookmark-bmenu-hide-filenames))
1988 (setq bookmark-bmenu-toggle-filenames nil)
1989 (goto-char (point-min))
1990 (forward-line 1)
1991 (while (re-search-forward "^D" (point-max) t)
1992 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1993 (bookmark-bmenu-list)
1994 (setq bookmark-bmenu-toggle-filenames hide-em)
1995 (if bookmark-bmenu-toggle-filenames
1996 (bookmark-bmenu-toggle-filenames t))
1997 (if o-str
1998 (progn
1999 (goto-char (point-min))
2000 (search-forward o-str)
2001 (beginning-of-line)
2002 (forward-char o-col))
2003 (goto-char o-point))
2004 (beginning-of-line)
2005 (setq bookmark-alist-modification-count
2006 (1+ bookmark-alist-modification-count))
2007 (if (bookmark-time-to-save-p)
2008 (bookmark-save))
2009 (message "Deleting bookmarks...done")
2013 (defun bookmark-bmenu-rename ()
2014 "Rename bookmark on current line. Prompts for a new name."
2015 (interactive)
2016 (if (bookmark-bmenu-check-position)
2017 (let ((bmrk (bookmark-bmenu-bookmark))
2018 (thispoint (point)))
2019 (bookmark-rename bmrk)
2020 (bookmark-bmenu-list)
2021 (goto-char thispoint))))
2024 (defun bookmark-bmenu-locate ()
2025 "Display location of this bookmark. Displays in the minibuffer."
2026 (interactive)
2027 (if (bookmark-bmenu-check-position)
2028 (let ((bmrk (bookmark-bmenu-bookmark)))
2029 (message (bookmark-location bmrk)))))
2033 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2035 (defun bookmark-menu-build-paned-menu (name entries)
2036 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2037 That is, ENTRIES is a list of strings which appear as the choices
2038 in the menu. The number of panes depends on the number of entries.
2039 The visible entries are truncated to `bookmark-menu-length', but the
2040 strings returned are not."
2041 (let* ((f-height (/ (frame-height) 2))
2042 (pane-list
2043 (let (temp-pane-list
2044 (iter 0))
2045 (while entries
2046 (let (lst
2047 (count 0))
2048 (while (and (< count f-height) entries)
2049 (let ((str (car entries)))
2050 (setq lst (cons
2051 (cons
2052 (if (> (length str) bookmark-menu-length)
2053 (substring str 0 bookmark-menu-length)
2054 str)
2055 str)
2056 lst))
2057 (setq entries (cdr entries))
2058 (setq count (1+ count))))
2059 (setq iter (1+ iter))
2060 (setq
2061 temp-pane-list
2062 (cons
2063 (cons
2064 (format "-*- %s (%d) -*-" name iter)
2065 (nreverse lst))
2066 temp-pane-list))))
2067 (nreverse temp-pane-list))))
2069 ;; Return the menu:
2070 (cons (concat "-*- " name " -*-") pane-list)))
2073 (defun bookmark-build-xemacs-menu (name entries function)
2074 "Build a menu named NAME from the strings in ENTRIES.
2075 That is, ENTRIES is a list of strings that appear as the choices
2076 in the menu.
2077 The visible entries are truncated to `bookmark-menu-length', but the
2078 strings returned are not."
2079 (let* (lst
2080 (pane-list
2081 (progn
2082 (while entries
2083 (let ((str (car entries)))
2084 (setq lst (cons
2085 (vector
2086 (if (> (length str) bookmark-menu-length)
2087 (substring str 0 bookmark-menu-length)
2088 str)
2089 (list function str)
2091 lst))
2092 (setq entries (cdr entries))))
2093 (nreverse lst))))
2095 ;; Return the menu:
2096 (append (if popup-menu-titles (list (concat "-*- " name " -*-")))
2097 pane-list)))
2100 (defun bookmark-menu-popup-paned-menu (event name entries)
2101 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2102 That is, ENTRIES is a list of strings which appear as the choices
2103 in the menu.
2104 The number of panes depends on the number of entries."
2105 (interactive "e")
2106 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2109 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2110 "Pop up menu of bookmarks, return chosen bookmark.
2111 Pop up at EVENT, menu's name is NAME.
2112 The number of panes depends on the number of bookmarks."
2113 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2116 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2117 ;; help function for making menus that need to apply a bookmark
2118 ;; function to a string.
2119 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2120 event menu-label)))
2121 (if choice (apply func-sym (list choice)))))
2124 ;;;###autoload
2125 (defun bookmark-menu-insert (event)
2126 "Insert the text of the file pointed to by bookmark BOOKMARK.
2127 You may have a problem using this function if the value of variable
2128 `bookmark-alist' is nil. If that happens, you need to load in some
2129 bookmarks. See help on function `bookmark-load' for more about
2130 this.
2132 Warning: this function only takes an EVENT as argument. Use the
2133 corresponding bookmark function from Lisp \(the one without the
2134 \"-menu-\" in its name\)."
2135 (interactive "e")
2136 (bookmark-popup-menu-and-apply-function
2137 'bookmark-insert "Insert Bookmark Contents" event))
2140 ;;;###autoload
2141 (defun bookmark-menu-jump (event)
2142 "Jump to bookmark BOOKMARK (a point in some file).
2143 You may have a problem using this function if the value of variable
2144 `bookmark-alist' is nil. If that happens, you need to load in some
2145 bookmarks. See help on function `bookmark-load' for more about
2146 this.
2148 Warning: this function only takes an EVENT as argument. Use the
2149 corresponding bookmark function from Lisp \(the one without the
2150 \"-menu-\" in its name\)."
2151 (interactive "e")
2152 (bookmark-popup-menu-and-apply-function
2153 'bookmark-jump "Jump to Bookmark" event))
2156 ;;;###autoload
2157 (defun bookmark-menu-locate (event)
2158 "Insert the name of the file associated with BOOKMARK.
2159 \(This is not the same as the contents of that file\).
2161 Warning: this function only takes an EVENT as argument. Use the
2162 corresponding bookmark function from Lisp \(the one without the
2163 \"-menu-\" in its name\)."
2164 (interactive "e")
2165 (bookmark-popup-menu-and-apply-function
2166 'bookmark-insert-location "Insert Bookmark Location" event))
2169 ;;;###autoload
2170 (defun bookmark-menu-rename (event)
2171 "Change the name of OLD-BOOKMARK to NEWNAME.
2172 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2173 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2174 prompts for NEWNAME.
2175 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2176 passed as an argument. If called with two strings, then no prompting
2177 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2179 While you are entering the new name, consecutive C-w's insert
2180 consecutive words from the text of the buffer into the new bookmark
2181 name.
2183 Warning: this function only takes an EVENT as argument. Use the
2184 corresponding bookmark function from Lisp \(the one without the
2185 \"-menu-\" in its name\)."
2186 (interactive "e")
2187 (bookmark-popup-menu-and-apply-function
2188 'bookmark-rename "Rename Bookmark" event))
2191 ;;;###autoload
2192 (defun bookmark-menu-delete (event)
2193 "Delete the bookmark named NAME from the bookmark list.
2194 Removes only the first instance of a bookmark with that name. If
2195 there are one or more other bookmarks with the same name, they will
2196 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2197 one most recently used in this file, if any\).
2199 Warning: this function only takes an EVENT as argument. Use the
2200 corresponding bookmark function from Lisp \(the one without the
2201 \"-menu-\" in its name\)."
2202 (interactive "e")
2203 (bookmark-popup-menu-and-apply-function
2204 'bookmark-delete "Delete Bookmark" event))
2207 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2208 ;; following works, and for explaining what to do to make it work.
2210 ;; We MUST autoload EACH form used to set up this variable's value, so
2211 ;; that the whole job is done in loaddefs.el.
2213 ;; Emacs menubar stuff.
2215 ;;;###autoload
2216 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2218 ;;;###autoload
2219 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2221 ;; make bookmarks appear toward the right side of the menu.
2222 (if (boundp 'menu-bar-final-items)
2223 (if menu-bar-final-items
2224 (setq menu-bar-final-items
2225 (cons 'bookmark menu-bar-final-items)))
2226 (setq menu-bar-final-items '(bookmark)))
2228 ;;;###autoload
2229 (define-key menu-bar-bookmark-map [load]
2230 '("Load a Bookmark File..." . bookmark-load))
2232 ;;;###autoload
2233 (define-key menu-bar-bookmark-map [write]
2234 '("Save Bookmarks As..." . bookmark-write))
2236 ;;;###autoload
2237 (define-key menu-bar-bookmark-map [save]
2238 '("Save Bookmarks" . bookmark-save))
2240 ;;;###autoload
2241 (define-key menu-bar-bookmark-map [edit]
2242 '("Edit Bookmark List" . bookmark-bmenu-list))
2244 ;;;###autoload
2245 (define-key menu-bar-bookmark-map [delete]
2246 '("Delete Bookmark" . bookmark-menu-delete))
2248 ;;;###autoload
2249 (define-key menu-bar-bookmark-map [rename]
2250 '("Rename Bookmark" . bookmark-menu-rename))
2252 ;;;###autoload
2253 (define-key menu-bar-bookmark-map [locate]
2254 '("Insert Location" . bookmark-menu-locate))
2256 ;;;###autoload
2257 (define-key menu-bar-bookmark-map [insert]
2258 '("Insert Contents" . bookmark-menu-insert))
2260 ;;;###autoload
2261 (define-key menu-bar-bookmark-map [set]
2262 '("Set Bookmark" . bookmark-set))
2264 ;;;###autoload
2265 (define-key menu-bar-bookmark-map [jump]
2266 '("Jump to Bookmark" . bookmark-menu-jump))
2268 ;;;; end bookmark menu stuff ;;;;
2271 ;;; Load Hook
2272 (defvar bookmark-load-hook nil
2273 "Hook to run at the end of loading bookmark.")
2275 (run-hooks 'bookmark-load-hook)
2277 (provide 'bookmark)
2279 ;;; bookmark.el ends here