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