root/trunk/lisp/bookmark.el

Revision 4220, 78.3 kB (checked in by miyoshi, 8 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
2
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders, annotations
10
11 ;; This file is part of GNU Emacs.
12
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 3, or (at your option)
16 ;; any later version.
17
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.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
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.
34
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...
38
39 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
40 ;; fixing and improving bookmark-time-to-save-p.
41
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).
45
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).
50
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.
55
56 ;; Thanks to Roland McGrath for encouragement and help with defining
57 ;; autoloads on the menu-bar.
58
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!)
63
64 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
65 ;; thinking up the annotations feature and implementing it so well.
66
67 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
68 ;; <olstad@msc.edu>.
69
70 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
71 ;; reported and fixed.
72
73 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
74
75 ;; Enough with the credits already, get on to the good stuff:
76
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?
80
81 ;;; Code:
82
83 (require 'pp)
84
85 ;;; Misc comments:
86 ;;
87 ;; If variable bookmark-use-annotations is non-nil, an annotation is
88 ;; queried for when setting a bookmark.
89 ;;
90 ;; The bookmark list is sorted lexically by default, but you can turn
91 ;; this off by setting bookmark-sort-flag to nil.  If it is nil, then
92 ;; the list will be presented in the order it is recorded
93 ;; (chronologically), which is actually fairly useful as well.
94
95 ;;; User Variables
96
97 (defgroup bookmark nil
98   "Setting, annotation and jumping to bookmarks."
99   :group 'matching)
100
101
102 (defcustom bookmark-use-annotations nil
103   "*If non-nil, saving a bookmark queries for an annotation in a buffer."
104   :type 'boolean
105   :group 'bookmark)
106
107
108 (defcustom bookmark-save-flag t
109   "*Controls when Emacs saves bookmarks to a file.
110 --> nil means never save bookmarks, except when `bookmark-save' is
111     explicitly called \(\\[bookmark-save]\).
112 --> t means save bookmarks when Emacs is killed.
113 --> Otherwise, it should be a number that is the frequency with which
114     the bookmark list is saved \(i.e.: the number of times which
115     Emacs' bookmark list may be modified before it is automatically
116     saved.\).  If it is a number, Emacs will also automatically save
117     bookmarks when it is killed.
118
119 Therefore, the way to get it to save every time you make or delete a
120 bookmark is to set this variable to 1 \(or 0, which produces the same
121 behavior.\)
122
123 To specify the file in which to save them, modify the variable
124 `bookmark-default-file', which is `~/.emacs.bmk' by default."
125   :type '(choice (const nil) integer (other t))
126   :group 'bookmark)
127
128
129 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
130   "*The `.emacs.bmk' file used to be called this name.")
131
132
133 ;; defvarred to avoid a compilation warning:
134 (defvar bookmark-file nil
135   "Old name for `bookmark-default-file'.")
136
137 (defcustom bookmark-default-file
138   (if bookmark-file
139       ;; In case user set `bookmark-file' in her .emacs:
140       bookmark-file
141     (convert-standard-filename "~/.emacs.bmk"))
142   "*File in which to save bookmarks by default."
143   :type 'file
144   :group 'bookmark)
145
146
147 (defcustom bookmark-version-control 'nospecial
148   "*Whether or not to make numbered backups of the bookmark file.
149 It can have four values: t, nil, `never', and `nospecial'.
150 The first three have the same meaning that they do for the
151 variable `version-control', and the final value `nospecial' means just
152 use the value of `version-control'."
153   :type '(choice (const nil) (const never) (const nospecial)
154                  (other t))
155   :group 'bookmark)
156
157
158 (defcustom bookmark-completion-ignore-case t
159   "*Non-nil means bookmark functions ignore case in completion."
160   :type 'boolean
161   :group 'bookmark)
162
163
164 (defcustom bookmark-sort-flag t
165   "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
166 Otherwise they will be displayed in LIFO order (that is, most
167 recently set ones come first, oldest ones come last)."
168   :type 'boolean
169   :group 'bookmark)
170
171
172 (defcustom bookmark-automatically-show-annotations t
173   "*Non-nil means show annotations when jumping to a bookmark."
174   :type 'boolean
175   :group 'bookmark)
176
177
178 (defcustom bookmark-bmenu-file-column 30
179   "*Column at which to display filenames in a buffer listing bookmarks.
180 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
181   :type 'integer
182   :group 'bookmark)
183
184
185 (defcustom bookmark-bmenu-toggle-filenames t
186   "*Non-nil means show filenames when listing bookmarks.
187 This may result in truncated bookmark names.  To disable this, put the
188 following in your `.emacs' file:
189
190 \(setq bookmark-bmenu-toggle-filenames nil\)"
191   :type 'boolean
192   :group 'bookmark)
193
194
195 (defcustom bookmark-menu-length 70
196   "*Maximum length of a bookmark name displayed on a popup menu."
197   :type 'integer
198   :group 'bookmark)
199
200
201 (defface bookmark-menu-heading
202   '((t (:inherit font-lock-type-face)))
203   "Face used to highlight the heading in bookmark menu buffers."
204   :group 'bookmark
205   :version "22.1")
206
207
208 ;;; No user-serviceable parts beyond this point.
209
210 ;; Is it XEmacs?
211 (defconst bookmark-xemacsp
212   (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
213
214
215 ;; Added  for lucid emacs  compatibility, db
216 (or (fboundp 'defalias)  (fset 'defalias 'fset))
217
218 ;; suggested for lucid compatibility by david hughes:
219 (or (fboundp 'frame-height)  (defalias 'frame-height 'screen-height))
220
221
222 ;;; Keymap stuff:
223
224 ;; Set up these bindings dumping time *only*;
225 ;; if the user alters them, don't override the user when loading bookmark.el.
226
227 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
228 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
229 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
230
231 ;;;###autoload
232 (defvar bookmark-map nil
233   "Keymap containing bindings to bookmark functions.
234 It is not bound to any key by default: to bind it
235 so that you have a bookmark prefix, just use `global-set-key' and bind a
236 key of your choice to `bookmark-map'.  All interactive bookmark
237 functions have a binding in this keymap.")
238
239 ;;;###autoload (define-prefix-command 'bookmark-map)
240
241 ;; Read the help on all of these functions for details...
242 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
244 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
245 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
246 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
247 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
248 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
249 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
250 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
251 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
252 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
253 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
254
255
256 ;;; The annotation maps.
257 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
258   "Keymap for composing an annotation for a bookmark.")
259
260 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
261   'bookmark-send-annotation)
262
263
264
265 ;;; Core variables and data structures:
266 (defvar bookmark-alist ()
267   "Association list of bookmarks and their records.
268 You probably don't want to change the value of this alist yourself;
269 instead, let the various bookmark functions do it for you.
270
271 The format of the alist is
272
273        \(BOOKMARK1 BOOKMARK2 ...\)
274
275 where each BOOKMARK is of the form
276
277 \(NAME
278   \(filename . FILE\)
279   \(front-context-string . FRONT-STR\)
280   \(rear-context-string  . REAR-STR\)
281   \(position . POS\)
282   \(info-node . POS\)
283   \(annotation . ANNOTATION\)\)
284
285 So the cdr of each bookmark is an alist too.
286 `info-node' is optional, by the way.")
287
288
289 (defvar bookmarks-already-loaded nil)
290
291
292 ;; more stuff added by db.
293
294 (defvar bookmark-current-bookmark nil
295   "Name of bookmark most recently used in the current file.
296 It is buffer local, used to make moving a bookmark forward
297 through a file easier.")
298
299 (make-variable-buffer-local 'bookmark-current-bookmark)
300
301
302 (defvar bookmark-alist-modification-count 0
303   "Number of modifications to bookmark list since it was last saved.")
304
305
306 (defvar bookmark-search-size 16
307   "Length of the context strings recorded on either side of a bookmark.")
308
309
310 (defvar bookmark-current-point 0)
311 (defvar bookmark-yank-point 0)
312 (defvar bookmark-current-buffer nil)
313
314 (defvar Info-current-node)
315 (defvar Info-suffix-list)
316
317 ;; Helper functions.
318
319 ;; Only functions on this page and the next one (file formats) need to
320 ;; know anything about the format of bookmark-alist entries.
321 ;; Everyone else should go through them.
322
323 (defun bookmark-name-from-full-record (full-record)
324   "Return name of FULL-RECORD \(an alist element instead of a string\)."
325   (car full-record))
326
327
328 (defun bookmark-all-names ()
329   "Return a list of all current bookmark names."
330   (bookmark-maybe-load-default-file)
331   (mapcar
332    (lambda (full-record)
333      (bookmark-name-from-full-record full-record))
334    bookmark-alist))
335
336
337 (defun bookmark-get-bookmark (bookmark)
338   "Return the full entry for BOOKMARK in `bookmark-alist'.
339 If BOOKMARK is not a string, return nil."
340   (when (stringp bookmark)
341     (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
342
343
344 (defun bookmark-get-bookmark-record (bookmark)
345   "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
346 That is, all information but the name."
347   (car (cdr (bookmark-get-bookmark bookmark))))
348
349
350 (defun bookmark-set-name (bookmark newname)
351   "Set BOOKMARK's name to NEWNAME."
352   (setcar
353    (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
354    newname))
355
356
357 (defun bookmark-get-annotation (bookmark)
358   "Return the annotation of BOOKMARK, or nil if none."
359   (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
360
361
362 (defun bookmark-set-annotation (bookmark ann)
363   "Set the annotation of BOOKMARK to ANN."
364   (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
365     (if cell
366         (setcdr cell ann)
367       (nconc (bookmark-get-bookmark-record bookmark)
368              (list (cons 'annotation ann))))))
369
370
371 (defun bookmark-get-filename (bookmark)
372   "Return the full filename of BOOKMARK."
373   (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
374
375
376 (defun bookmark-set-filename (bookmark filename)
377   "Set the full filename of BOOKMARK to FILENAME."
378   (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
379     (if cell
380         (setcdr cell filename)
381       (nconc (bookmark-get-bookmark-record bookmark)
382              (list (cons 'filename filename))))
383     (setq bookmark-alist-modification-count
384           (1+ bookmark-alist-modification-count))
385     (if (bookmark-time-to-save-p)
386         (bookmark-save))))
387
388
389 (defun bookmark-get-position (bookmark)
390   "Return the position \(i.e.: point\) of BOOKMARK."
391   (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
392
393
394 (defun bookmark-set-position (bookmark position)
395   "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
396   (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
397     (if cell
398         (setcdr cell position)
399       (nconc (bookmark-get-bookmark-record bookmark)
400              (list (cons 'position position))))))
401
402
403 (defun bookmark-get-front-context-string (bookmark)
404   "Return the front-context-string of BOOKMARK."
405   (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
406
407
408 (defun bookmark-set-front-context-string (bookmark string)
409   "Set the front-context-string of BOOKMARK to STRING."
410   (let ((cell (assq 'front-context-string
411                     (bookmark-get-bookmark-record bookmark))))
412     (if cell
413         (setcdr cell string)
414       (nconc (bookmark-get-bookmark-record bookmark)
415              (list (cons 'front-context-string string))))))
416
417
418 (defun bookmark-get-rear-context-string (bookmark)
419   "Return the rear-context-string of BOOKMARK."
420   (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
421
422
423 (defun bookmark-set-rear-context-string (bookmark string)
424   "Set the rear-context-string of BOOKMARK to STRING."
425   (let ((cell (assq 'rear-context-string
426                     (bookmark-get-bookmark-record bookmark))))
427     (if cell
428         (setcdr cell string)
429       (nconc (bookmark-get-bookmark-record bookmark)
430              (list (cons 'rear-context-string string))))))
431
432
433 (defun bookmark-get-info-node (bookmark)
434   "Get the info node associated with BOOKMARK."
435   (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
436
437
438 (defun bookmark-set-info-node (bookmark node)
439   "Set the Info node of BOOKMARK to NODE."
440   (let ((cell (assq 'info-node
441                     (bookmark-get-bookmark-record bookmark))))
442     (if cell
443         (setcdr cell node)
444       (nconc (bookmark-get-bookmark-record bookmark)
445              (list (cons 'info-node node)))))
446
447   (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
448   (sit-for 4))
449
450
451 (defvar bookmark-history nil
452   "The history list for bookmark functions.")
453
454
455 (defun bookmark-completing-read (prompt &optional default)
456   "Prompting with PROMPT, read a bookmark name in completion.
457 PROMPT will get a \": \" stuck on the end no matter what, so you
458 probably don't want to include one yourself.
459 Optional second arg DEFAULT is a string to return if the user enters
460 the empty string."
461   (bookmark-maybe-load-default-file) ; paranoia
462   (if (listp last-nonmenu-event)
463       (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
464     (let* ((completion-ignore-case bookmark-completion-ignore-case)
465            (default default)
466            (prompt (if default
467                        (concat prompt (format " (%s): " default))
468                      (concat prompt ": ")))
469            (str
470             (completing-read prompt
471                              bookmark-alist
472                              nil
473                              0
474                              nil
475                              'bookmark-history)))
476       (if (string-equal "" str) default str))))
477
478
479 (defmacro bookmark-maybe-historicize-string (string)
480   "Put STRING into the bookmark prompt history, if caller non-interactive.
481 We need this because sometimes bookmark functions are invoked from
482 menus, so `completing-read' never gets a chance to set `bookmark-history'."
483   `(or
484     (interactive-p)
485     (setq bookmark-history (cons ,string bookmark-history))))
486
487
488 (defun bookmark-make (name &optional annotation overwrite info-node)
489   "Make a bookmark named NAME.
490 Optional second arg ANNOTATION gives it an annotation.
491 Optional third arg OVERWRITE means replace any existing bookmarks with
492 this name.
493 Optional fourth arg INFO-NODE means this bookmark is at info node
494 INFO-NODE, so record this fact in the bookmark's entry."
495   (bookmark-maybe-load-default-file)
496   (let ((stripped-name (copy-sequence name)))
497     (or bookmark-xemacsp
498         ;; XEmacs's `set-text-properties' doesn't work on
499         ;; free-standing strings, apparently.
500         (set-text-properties 0 (length stripped-name) nil stripped-name))
501     (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
502         ;; already existing bookmark under that name and
503         ;; no prefix arg means just overwrite old bookmark
504         (setcdr (bookmark-get-bookmark stripped-name)
505                 (list (bookmark-make-cell annotation info-node)))
506
507       ;; otherwise just cons it onto the front (either the bookmark
508       ;; doesn't exist already, or there is no prefix arg.  In either
509       ;; case, we want the new bookmark consed onto the alist...)
510
511       (setq bookmark-alist
512             (cons
513              (list stripped-name
514                    (bookmark-make-cell annotation info-node))
515              bookmark-alist)))
516
517     ;; Added by db
518     (setq bookmark-current-bookmark stripped-name)
519     (setq bookmark-alist-modification-count
520           (1+ bookmark-alist-modification-count))
521     (if (bookmark-time-to-save-p)
522         (bookmark-save))))
523
524
525 (defun bookmark-make-cell (annotation &optional info-node)
526   "Return the record part of a new bookmark, given ANNOTATION.
527 Must be at the correct position in the buffer in which the bookmark is
528 being set.  This might change someday.
529 Optional second arg INFO-NODE means this bookmark is at info node
530 INFO-NODE, so record this fact in the bookmark's entry."
531   (let ((the-record
532          `((filename . ,(bookmark-buffer-file-name))
533            (front-context-string
534             . ,(if (>= (- (point-max) (point)) bookmark-search-size)
535                    (buffer-substring-no-properties
536                     (point)
537                     (+ (point) bookmark-search-size))
538                    nil))
539            (rear-context-string
540             . ,(if (>= (- (point) (point-min)) bookmark-search-size)
541                    (buffer-substring-no-properties
542                     (point)
543                     (- (point) bookmark-search-size))
544                    nil))
545            (position . ,(point)))))
546
547     ;; Now fill in the optional parts:
548
549     ;; Take no chances with text properties
550     (set-text-properties 0 (length annotation) nil annotation)
551     (set-text-properties 0 (length info-node) nil info-node)
552
553     (if annotation
554         (nconc the-record (list (cons 'annotation annotation))))
555     (if info-node
556         (nconc the-record (list (cons 'info-node info-node))))
557
558     ;; Finally, return the completed record.
559     the-record))
560
561
562
563 ;;; File format stuff
564
565 ;; The OLD format of the bookmark-alist was:
566 ;;
567 ;;       ((bookmark-name (filename
568 ;;                        string-in-front
569 ;;                        string-behind
570 ;;                        point))
571 ;;        ...)
572 ;;
573 ;; The NEW format of the bookmark-alist is:
574 ;;
575 ;;       ((bookmark-name ((filename . FILENAME)
576 ;;                        (front-context-string . string-in-front)
577 ;;                        (rear-context-string  . string-behind)
578 ;;                        (position . POINT)
579 ;;                        (annotation . annotation)
580 ;;                        (whatever   . VALUE)
581 ;;                        ...
582 ;;                        ))
583 ;;        ...)
584 ;;
585 ;;
586 ;; I switched to using an internal as well as external alist because I
587 ;; felt that would be a more flexible framework in which to add
588 ;; features.  It means that the order in which values appear doesn't
589 ;; matter, and it means that arbitrary values can be added without
590 ;; risk of interfering with existing ones.
591 ;;
592 ;; BOOKMARK-NAME is the string the user gives the bookmark and
593 ;; accesses it by from then on.
594 ;;
595 ;; FILENAME is the location of the file in which the bookmark is set.
596 ;;
597 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
598 ;; context in front of the point at which the bookmark is set.
599 ;;
600 ;; STRING-BEHIND is the same thing, but after the point.
601 ;;
602 ;; The context strings exist so that modifications to a file don't
603 ;; necessarily cause a bookmark's position to be invalidated.
604 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
605 ;; case the file has changed since the bookmark was set.  It will
606 ;; attempt to place the user before the changes, if there were any.
607 ;; ANNOTATION is the annotation for the bookmark; it may not exist
608 ;; (for backward compatibility), be nil (no annotation), or be a
609 ;; string.
610
611
612 (defconst bookmark-file-format-version 1
613   "The current version of the format used by bookmark files.
614 You should never need to change this.")
615
616
617 (defconst bookmark-end-of-version-stamp-marker
618   "-*- End Of Bookmark File Format Version Stamp -*-\n"
619   "This string marks the end of the version stamp in a bookmark file.")
620
621
622 (defun bookmark-alist-from-buffer ()
623   "Return a `bookmark-alist' (in any format) from the current buffer.
624 The buffer must of course contain bookmark format information.
625 Does not care from where in the buffer it is called, and does not
626 affect point."
627   (save-excursion
628     (goto-char (point-min))
629     (if (search-forward bookmark-end-of-version-stamp-marker nil t)
630         (read (current-buffer))
631       ;; Else we're dealing with format version 0
632       (if (search-forward "(" nil t)
633           (progn
634             (forward-char -1)
635             (read (current-buffer)))
636         ;; Else no hope of getting information here.
637         (error "Not bookmark format")))))
638
639
640 (defun bookmark-upgrade-version-0-alist (old-list)
641   "Upgrade a version 0 alist OLD-LIST to the current version."
642   (mapcar
643    (lambda (bookmark)
644      (let* ((name      (car bookmark))
645             (record    (car (cdr bookmark)))
646             (filename  (nth 0 record))
647             (front-str (nth 1 record))
648             (rear-str  (nth 2 record))
649             (position  (nth 3 record))
650             (ann       (nth 4 record)))
651        (list
652         name
653         `((filename             .    ,filename)
654           (front-context-string .    ,(or front-str ""))
655           (rear-context-string  .    ,(or rear-str  ""))
656           (position             .    ,position)
657           (annotation           .    ,ann)))))
658    old-list))
659
660
661 (defun bookmark-upgrade-file-format-from-0 ()
662   "Upgrade a bookmark file of format 0 (the original format) to format 1.
663 This expects to be called from `point-min' in a bookmark file."
664   (message "Upgrading bookmark format from 0 to %d..."
665            bookmark-file-format-version)
666   (let* ((old-list (bookmark-alist-from-buffer))
667          (new-list (bookmark-upgrade-version-0-alist old-list)))
668     (delete-region (point-min) (point-max))
669     (bookmark-insert-file-format-version-stamp)
670     (pp new-list (current-buffer))
671     (save-buffer))
672   (goto-char (point-min))
673   (message "Upgrading bookmark format from 0 to %d...done"
674            bookmark-file-format-version)
675   )
676
677
678 (defun bookmark-grok-file-format-version ()
679   "Return an integer which is the file-format version of this bookmark file.
680 This expects to be called from `point-min' in a bookmark file."
681   (if (looking-at "^;;;;")
682       (save-excursion
683         (save-match-data
684           (re-search-forward "[0-9]")
685           (forward-char -1)
686           (read (current-buffer))))
687     ;; Else this is format version 0, the original one, which didn't
688     ;; even have version stamps.
689     0))
690
691
692 (defun bookmark-maybe-upgrade-file-format ()
693   "Check the file-format version of this bookmark file.
694 If the version is not up-to-date, upgrade it automatically.
695 This expects to be called from `point-min' in a bookmark file."
696   (let ((version (bookmark-grok-file-format-version)))
697     (cond
698      ((= version bookmark-file-format-version)
699       ) ; home free -- version is current
700      ((= version 0)
701       (bookmark-upgrade-file-format-from-0))
702      (t
703       (error "Bookmark file format version strangeness")))))
704
705
706 (defun bookmark-insert-file-format-version-stamp ()
707   "Insert text indicating current version of bookmark file format."
708   (insert
709    (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
710            bookmark-file-format-version))
711   (insert ";;; This format is meant to be slightly human-readable;\n"
712           ";;; nevertheless, you probably don't want to edit it.\n"
713           ";;; "
714           bookmark-end-of-version-stamp-marker))
715
716
717 ;;; end file-format stuff
718
719
720 ;;; Generic helpers.
721
722 (defun bookmark-maybe-message (fmt &rest args)
723   "Apply `message' to FMT and ARGS, but only if the display is fast enough."
724   (if (>= baud-rate 9600)
725       (apply 'message fmt args)))
726
727
728 ;;; Core code:
729
730 ;;;###autoload
731 (defun bookmark-set (&optional name parg)
732   "Set a bookmark named NAME inside a file.
733 If name is nil, then the user will be prompted.
734 With prefix arg, will not overwrite a bookmark that has the same name
735 as NAME if such a bookmark already exists, but instead will \"push\"
736 the new bookmark onto the bookmark alist.  Thus the most recently set
737 bookmark with name NAME would be the one in effect at any given time,
738 but the others are still there, should you decide to delete the most
739 recent one.
740
741 To yank words from the text of the buffer and use them as part of the
742 bookmark name, type C-w while setting a bookmark.  Successive C-w's
743 yank successive words.
744
745 Typing C-u inserts the name of the last bookmark used in the buffer
746 \(as an aid in using a single bookmark name to track your progress
747 through a large file\).  If no bookmark was used, then C-u inserts the
748 name of the file being visited.
749
750 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
751 and it removes only the first instance of a bookmark with that name from
752 the list of bookmarks.\)"
753   (interactive (list nil current-prefix-arg))
754   (or
755    (bookmark-buffer-file-name)
756    (error "Buffer not visiting a file or directory"))
757
758   (bookmark-maybe-load-default-file)
759
760   (setq bookmark-current-point (point))
761   (setq bookmark-yank-point (point))
762   (setq bookmark-current-buffer (current-buffer))
763
764   (let* ((default (or bookmark-current-bookmark
765                       (bookmark-buffer-name)))
766          (str
767           (or name
768               (read-from-minibuffer
769                (format "Set bookmark (%s): " default)
770                nil
771                (let ((now-map (copy-keymap minibuffer-local-map)))
772                  (define-key now-map "\C-w" 'bookmark-yank-word)
773                  (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
774                  now-map))))
775          (annotation nil))
776     (and (string-equal str "") (setq str default))
777     ;; Ask for an annotation buffer for this bookmark
778     (if bookmark-use-annotations
779         (bookmark-read-annotation parg str)
780       (bookmark-make str annotation parg (bookmark-info-current-node))
781       (setq bookmark-current-bookmark str)
782       (bookmark-bmenu-surreptitiously-rebuild-list)
783       (goto-char bookmark-current-point))))
784
785
786 (defun bookmark-info-current-node ()
787   "If in Info-mode, return current node name (a string), else nil."
788   (if (eq major-mode 'Info-mode)
789       Info-current-node))
790
791
792 (defun bookmark-kill-line (&optional newline-too)
793   "Kill from point to end of line.
794 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
795 Does not affect the kill ring."
796   (let ((eol (save-excursion (end-of-line) (point))))
797     (delete-region (point) eol)
798     (if (and newline-too (looking-at "\n"))
799         (delete-char 1))))
800
801
802 ;; Defvars to avoid compilation warnings:
803 (defvar bookmark-annotation-paragraph nil)
804 (defvar bookmark-annotation-name nil)
805 (defvar bookmark-annotation-buffer nil)
806 (defvar bookmark-annotation-file nil)
807 (defvar bookmark-annotation-point nil)
808
809
810 (defun bookmark-send-annotation ()
811   "Use buffer contents as the annotation for a bookmark.
812 Exclude lines that begin with `#'.
813 Store the annotation text in the bookmark list with
814 the bookmark (and file, and point) specified in buffer local variables."
815   (interactive)
816   (if (not (eq major-mode 'bookmark-read-annotation-mode))
817       (error "Not in bookmark-read-annotation-mode"))
818   (goto-char (point-min))
819   (while (< (point) (point-max))
820     (if (looking-at "^#")
821         (bookmark-kill-line t)
822       (forward-line 1)))
823   (let ((annotation (buffer-string))
824         (parg bookmark-annotation-paragraph)
825         (bookmark bookmark-annotation-name)
826         (pt bookmark-annotation-point)
827         (buf bookmark-annotation-buffer))
828     ;; for bookmark-make-cell to work, we need to be
829     ;; in the relevant buffer, at the relevant point.
830     ;; Actually, bookmark-make-cell should probably be re-written,
831     ;; to avoid this need.  Should I handle the error if a buffer is
832     ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
833     (save-excursion
834       (pop-to-buffer buf)
835       (goto-char pt)
836       (bookmark-make bookmark annotation parg (bookmark-info-current-node))
837       (setq bookmark-current-bookmark bookmark))
838     (bookmark-bmenu-surreptitiously-rebuild-list)
839     (goto-char bookmark-current-point))
840   (kill-buffer (current-buffer)))
841
842
843 (defun bookmark-default-annotation-text (bookmark)
844   (concat "#  Type the annotation for bookmark '" bookmark "' here.\n"
845           "#  All lines which start with a '#' will be deleted.\n"
846           "#  Type C-c C-c when done.\n#\n"
847           "#  Author: " (user-full-name) " <" (user-login-name) "@"
848           (system-name) ">\n"
849           "#  Date:    " (current-time-string) "\n"))
850
851
852 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
853   "Function to return default text to use for a bookmark annotation.
854 It takes one argument, the name of the bookmark, as a string.")
855
856 (defun bookmark-read-annotation-mode (buf point parg bookmark)
857   "Mode for composing annotations for a bookmark.
858 Wants BUF, POINT, PARG, and BOOKMARK.
859 When you have finished composing, type \\[bookmark-send-annotation] to send
860 the annotation.
861
862 \\{bookmark-read-annotation-mode-map}
863 "
864   (interactive)
865   (kill-all-local-variables)
866   (make-local-variable 'bookmark-annotation-paragraph)
867   (make-local-variable 'bookmark-annotation-name)
868   (make-local-variable 'bookmark-annotation-buffer)
869   (make-local-variable 'bookmark-annotation-file)
870   (make-local-variable 'bookmark-annotation-point)
871   (setq bookmark-annotation-paragraph parg)
872   (setq bookmark-annotation-name bookmark)
873   (setq bookmark-annotation-buffer buf)
874   (setq bookmark-annotation-file (buffer-file-name buf))
875   (setq bookmark-annotation-point point)
876   (use-local-map bookmark-read-annotation-mode-map)
877   (setq major-mode 'bookmark-read-annotation-mode)
878   (insert (funcall bookmark-read-annotation-text-func bookmark))
879   (run-mode-hooks 'text-mode-hook))
880
881
882 (defun bookmark-read-annotation (parg bookmark)
883   "Pop up a buffer for entering a bookmark annotation.
884 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
885   (let ((buf (current-buffer))
886         (point (point)))
887     (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
888     (bookmark-read-annotation-mode buf point parg bookmark)))
889
890
891 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
892   "Keymap for editing an annotation of a bookmark.")
893
894
895 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
896   'bookmark-send-edited-annotation)
897
898
899 (defun bookmark-edit-annotation-mode (bookmark)
900   "Mode for editing the annotation of bookmark BOOKMARK.
901 When you have finished compos