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