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