(display-message-or-buffer): Pass the correct buffer to `display-buffer'.
[bpt/emacs.git] / lisp / bookmark.el
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
2
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001 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 ;; more stuff added by db.
304
305 (defvar bookmark-current-bookmark nil
306 "Name of bookmark most recently used in the current file.
307 It is buffer local, used to make moving a bookmark forward
308 through a file easier.")
309
310 (make-variable-buffer-local 'bookmark-current-bookmark)
311
312
313 (defvar bookmark-alist-modification-count 0
314 "Number of modifications to bookmark list since it was last saved.")
315
316
317 (defvar bookmark-search-size 16
318 "Length of the context strings recorded on either side of a bookmark.")
319
320
321 (defvar bookmark-current-point 0)
322 (defvar bookmark-yank-point 0)
323 (defvar bookmark-current-buffer nil)
324
325
326 \f
327 ;; Helper functions.
328
329 ;; Only functions on this page and the next one (file formats) need to
330 ;; know anything about the format of bookmark-alist entries.
331 ;; Everyone else should go through them.
332
333 (defun bookmark-name-from-full-record (full-record)
334 "Return name of FULL-RECORD \(an alist element instead of a string\)."
335 (car full-record))
336
337
338 (defun bookmark-all-names ()
339 "Return a list of all current bookmark names."
340 (bookmark-maybe-load-default-file)
341 (mapcar
342 (lambda (full-record)
343 (bookmark-name-from-full-record full-record))
344 bookmark-alist))
345
346
347 (defun bookmark-get-bookmark (bookmark)
348 "Return the full entry for BOOKMARK in bookmark-alist.
349 If BOOKMARK is not a string, return nil."
350 (when (stringp bookmark)
351 (apply (if bookmark-completion-ignore-case
352 #'assoc-ignore-case
353 #'assoc)
354 (list bookmark bookmark-alist))))
355
356
357 (defun bookmark-get-bookmark-record (bookmark)
358 "Return the guts of the entry for BOOKMARK in bookmark-alist.
359 That is, all information but the name."
360 (car (cdr (bookmark-get-bookmark bookmark))))
361
362
363 (defun bookmark-set-name (bookmark newname)
364 "Set BOOKMARK's name to NEWNAME."
365 (setcar
366 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
367 newname))
368
369
370 (defun bookmark-get-annotation (bookmark)
371 "Return the annotation of BOOKMARK, or nil if none."
372 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
373
374
375 (defun bookmark-set-annotation (bookmark ann)
376 "Set the annotation of BOOKMARK to ANN."
377 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
378 (if cell
379 (setcdr cell ann)
380 (nconc (bookmark-get-bookmark-record bookmark)
381 (list (cons 'annotation ann))))))
382
383
384 (defun bookmark-get-filename (bookmark)
385 "Return the full filename of BOOKMARK."
386 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
387
388
389 (defun bookmark-set-filename (bookmark filename)
390 "Set the full filename of BOOKMARK to FILENAME."
391 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
392 (if cell
393 (setcdr cell filename)
394 (nconc (bookmark-get-bookmark-record bookmark)
395 (list (cons 'filename filename))))))
396
397
398 (defun bookmark-get-position (bookmark)
399 "Return the position \(i.e.: point\) of BOOKMARK."
400 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
401
402
403 (defun bookmark-set-position (bookmark position)
404 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
405 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
406 (if cell
407 (setcdr cell position)
408 (nconc (bookmark-get-bookmark-record bookmark)
409 (list (cons 'position position))))))
410
411
412 (defun bookmark-get-front-context-string (bookmark)
413 "Return the front-context-string of BOOKMARK."
414 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
415
416
417 (defun bookmark-set-front-context-string (bookmark string)
418 "Set the front-context-string of BOOKMARK to STRING."
419 (let ((cell (assq 'front-context-string
420 (bookmark-get-bookmark-record bookmark))))
421 (if cell
422 (setcdr cell string)
423 (nconc (bookmark-get-bookmark-record bookmark)
424 (list (cons 'front-context-string string))))))
425
426
427 (defun bookmark-get-rear-context-string (bookmark)
428 "Return the rear-context-string of BOOKMARK."
429 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
430
431
432 (defun bookmark-set-rear-context-string (bookmark string)
433 "Set the rear-context-string of BOOKMARK to STRING."
434 (let ((cell (assq 'rear-context-string
435 (bookmark-get-bookmark-record bookmark))))
436 (if cell
437 (setcdr cell string)
438 (nconc (bookmark-get-bookmark-record bookmark)
439 (list (cons 'rear-context-string string))))))
440
441
442 (defun bookmark-get-info-node (bookmark)
443 "Get the info node associated with BOOKMARK."
444 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
445
446
447 (defun bookmark-set-info-node (bookmark node)
448 "Set the Info node of BOOKMARK to NODE."
449 (let ((cell (assq 'info-node
450 (bookmark-get-bookmark-record bookmark))))
451 (if cell
452 (setcdr cell node)
453 (nconc (bookmark-get-bookmark-record bookmark)
454 (list (cons 'info-node node)))))
455
456 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
457 (sit-for 4)
458 )
459
460
461 (defvar bookmark-history nil
462 "The history list for bookmark functions.")
463
464
465 (defun bookmark-completing-read (prompt &optional default)
466 "Prompting with PROMPT, read a bookmark name in completion.
467 PROMPT will get a \": \" stuck on the end no matter what, so you
468 probably don't want to include one yourself.
469 Optional second arg DEFAULT is a string to return if the user enters
470 the empty string."
471 (bookmark-maybe-load-default-file) ; paranoia
472 (let* ((completion-ignore-case bookmark-completion-ignore-case)
473 (default default)
474 (prompt (if default
475 (concat prompt (format " (%s): " default))
476 (concat prompt ": ")))
477 (str
478 (completing-read prompt
479 bookmark-alist
480 nil
481 0
482 nil
483 'bookmark-history)))
484 (if (string-equal "" str)
485 (list default)
486 (list str))))
487
488
489 (defmacro bookmark-maybe-historicize-string (string)
490 "Put STRING into the bookmark prompt history, if caller non-interactive.
491 We need this because sometimes bookmark functions are invoked from
492 menus, so `completing-read' never gets a chance to set `bookmark-history'."
493 `(or
494 (interactive-p)
495 (setq bookmark-history (cons ,string bookmark-history))))
496
497
498 (defun bookmark-make (name &optional annotation overwrite info-node)
499 "Make a bookmark named NAME.
500 Optional second arg ANNOTATION gives it an annotation.
501 Optional third arg OVERWRITE means replace any existing bookmarks with
502 this name.
503 Optional fourth arg INFO-NODE means this bookmark is at info node
504 INFO-NODE, so record this fact in the bookmark's entry."
505 (bookmark-maybe-load-default-file)
506 (let ((stripped-name (copy-sequence name)))
507 (or bookmark-xemacsp
508 ;; XEmacs's `set-text-properties' doesn't work on
509 ;; free-standing strings, apparently.
510 (set-text-properties 0 (length stripped-name) nil stripped-name))
511 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
512 ;; already existing bookmark under that name and
513 ;; no prefix arg means just overwrite old bookmark
514 (setcdr (bookmark-get-bookmark stripped-name)
515 (list (bookmark-make-cell annotation info-node)))
516
517 ;; otherwise just cons it onto the front (either the bookmark
518 ;; doesn't exist already, or there is no prefix arg. In either
519 ;; case, we want the new bookmark consed onto the alist...)
520
521 (setq bookmark-alist
522 (cons
523 (list stripped-name
524 (bookmark-make-cell annotation info-node))
525 bookmark-alist)))
526
527 ;; Added by db
528 (setq bookmark-current-bookmark stripped-name)
529 (setq bookmark-alist-modification-count
530 (1+ bookmark-alist-modification-count))
531 (if (bookmark-time-to-save-p)
532 (bookmark-save))))
533
534
535 (defun bookmark-make-cell (annotation &optional info-node)
536 "Return the record part of a new bookmark, given ANNOTATION.
537 Must be at the correct position in the buffer in which the bookmark is
538 being set. This might change someday.
539 Optional second arg INFO-NODE means this bookmark is at info node
540 INFO-NODE, so record this fact in the bookmark's entry."
541 (let ((the-record
542 (` ((filename . (, (bookmark-buffer-file-name)))
543 (front-context-string
544 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
545 (buffer-substring-no-properties
546 (point)
547 (+ (point) bookmark-search-size))
548 nil)))
549 (rear-context-string
550 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
551 (buffer-substring-no-properties
552 (point)
553 (- (point) bookmark-search-size))
554 nil)))
555 (position . (, (point)))
556 ))))
557
558 ;; Now fill in the optional parts:
559
560 ;; Take no chances with text properties
561 (set-text-properties 0 (length annotation) nil annotation)
562 (set-text-properties 0 (length info-node) nil info-node)
563
564 (if annotation
565 (nconc the-record (list (cons 'annotation annotation))))
566 (if info-node
567 (nconc the-record (list (cons 'info-node info-node))))
568
569 ;; Finally, return the completed record.
570 the-record))
571
572
573 \f
574 ;;; File format stuff
575
576 ;; The OLD format of the bookmark-alist was:
577 ;;
578 ;; ((bookmark-name (filename
579 ;; string-in-front
580 ;; string-behind
581 ;; point))
582 ;; ...)
583 ;;
584 ;; The NEW format of the bookmark-alist is:
585 ;;
586 ;; ((bookmark-name ((filename . FILENAME)
587 ;; (front-context-string . string-in-front)
588 ;; (rear-context-string . string-behind)
589 ;; (position . POINT)
590 ;; (annotation . annotation)
591 ;; (whatever . VALUE)
592 ;; ...
593 ;; ))
594 ;; ...)
595 ;;
596 ;;
597 ;; I switched to using an internal as well as external alist because I
598 ;; felt that would be a more flexible framework in which to add
599 ;; features. It means that the order in which values appear doesn't
600 ;; matter, and it means that arbitrary values can be added without
601 ;; risk of interfering with existing ones.
602 ;;
603 ;; BOOKMARK-NAME is the string the user gives the bookmark and
604 ;; accesses it by from then on.
605 ;;
606 ;; FILENAME is the location of the file in which the bookmark is set.
607 ;;
608 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
609 ;; context in front of the point at which the bookmark is set.
610 ;;
611 ;; STRING-BEHIND is the same thing, but after the point.
612 ;;
613 ;; The context strings exist so that modifications to a file don't
614 ;; necessarily cause a bookmark's position to be invalidated.
615 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
616 ;; case the file has changed since the bookmark was set. It will
617 ;; attempt to place the user before the changes, if there were any.
618 ;; ANNOTATION is the annotation for the bookmark; it may not exist
619 ;; (for backward compatibility), be nil (no annotation), or be a
620 ;; string.
621
622
623 (defconst bookmark-file-format-version 1
624 "The current version of the format used by bookmark files.
625 You should never need to change this.")
626
627
628 (defconst bookmark-end-of-version-stamp-marker
629 "-*- End Of Bookmark File Format Version Stamp -*-\n"
630 "This string marks the end of the version stamp in a bookmark file.")
631
632
633 (defun bookmark-alist-from-buffer ()
634 "Return a bookmark-alist (in any format) from the current buffer.
635 The buffer must of course contain bookmark format information.
636 Does not care from where in the buffer it is called, and does not
637 affect point."
638 (save-excursion
639 (goto-char (point-min))
640 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
641 (read (current-buffer))
642 ;; Else we're dealing with format version 0
643 (if (search-forward "(" nil t)
644 (progn
645 (forward-char -1)
646 (read (current-buffer)))
647 ;; Else no hope of getting information here.
648 (error "Not bookmark format")))))
649
650
651 (defun bookmark-upgrade-version-0-alist (old-list)
652 "Upgrade a version 0 alist OLD-LIST to the current version."
653 (mapcar
654 (lambda (bookmark)
655 (let* ((name (car bookmark))
656 (record (car (cdr bookmark)))
657 (filename (nth 0 record))
658 (front-str (nth 1 record))
659 (rear-str (nth 2 record))
660 (position (nth 3 record))
661 (ann (nth 4 record)))
662 (list
663 name
664 (` ((filename . (, filename))
665 (front-context-string . (, (or front-str "")))
666 (rear-context-string . (, (or rear-str "")))
667 (position . (, position))
668 (annotation . (, ann)))))))
669 old-list))
670
671
672 (defun bookmark-upgrade-file-format-from-0 ()
673 "Upgrade a bookmark file of format 0 (the original format) to format 1.
674 This expects to be called from point-min in a bookmark file."
675 (message "Upgrading bookmark format from 0 to %d..."
676 bookmark-file-format-version)
677 (let* ((old-list (bookmark-alist-from-buffer))
678 (new-list (bookmark-upgrade-version-0-alist old-list)))
679 (delete-region (point-min) (point-max))
680 (bookmark-insert-file-format-version-stamp)
681 (pp new-list (current-buffer))
682 (save-buffer))
683 (goto-char (point-min))
684 (message "Upgrading bookmark format from 0 to %d...done"
685 bookmark-file-format-version)
686 )
687
688
689 (defun bookmark-grok-file-format-version ()
690 "Return an integer which is the file-format version of this bookmark file.
691 This expects to be called from point-min in a bookmark file."
692 (if (looking-at "^;;;;")
693 (save-excursion
694 (save-match-data
695 (re-search-forward "[0-9]")
696 (forward-char -1)
697 (read (current-buffer))))
698 ;; Else this is format version 0, the original one, which didn't
699 ;; even have version stamps.
700 0))
701
702
703 (defun bookmark-maybe-upgrade-file-format ()
704 "Check the file-format version of this bookmark file.
705 If the version is not up-to-date, upgrade it automatically.
706 This expects to be called from point-min in a bookmark file."
707 (let ((version (bookmark-grok-file-format-version)))
708 (cond
709 ((= version bookmark-file-format-version)
710 ) ; home free -- version is current
711 ((= version 0)
712 (bookmark-upgrade-file-format-from-0))
713 (t
714 (error "Bookmark file format version strangeness")))))
715
716
717 (defun bookmark-insert-file-format-version-stamp ()
718 "Insert text indicating current version of bookmark file format."
719 (insert
720 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
721 bookmark-file-format-version))
722 (insert ";;; This format is meant to be slightly human-readable;\n"
723 ";;; nevertheless, you probably don't want to edit it.\n"
724 ";;; "
725 bookmark-end-of-version-stamp-marker))
726
727
728 ;;; end file-format stuff
729
730 \f
731 ;;; Core code:
732
733 ;;;###autoload
734 (defun bookmark-set (&optional name parg)
735 "Set a bookmark named NAME inside a file.
736 If name is nil, then the user will be prompted.
737 With prefix arg, will not overwrite a bookmark that has the same name
738 as NAME if such a bookmark already exists, but instead will \"push\"
739 the new bookmark onto the bookmark alist. Thus the most recently set
740 bookmark with name NAME would be the one in effect at any given time,
741 but the others are still there, should you decide to delete the most
742 recent one.
743
744 To yank words from the text of the buffer and use them as part of the
745 bookmark name, type C-w while setting a bookmark. Successive C-w's
746 yank successive words.
747
748 Typing C-u inserts the name of the last bookmark used in the buffer
749 \(as an aid in using a single bookmark name to track your progress
750 through a large file\). If no bookmark was used, then C-u inserts the
751 name of the file being visited.
752
753 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
754 and it removes only the first instance of a bookmark with that name from
755 the list of bookmarks.\)"
756 (interactive (list nil current-prefix-arg))
757 (or
758 (bookmark-buffer-file-name)
759 (error "Buffer not visiting a file or directory"))
760
761 (bookmark-maybe-load-default-file)
762
763 (setq bookmark-current-point (point))
764 (setq bookmark-yank-point (point))
765 (setq bookmark-current-buffer (current-buffer))
766
767 (let* ((default (or bookmark-current-bookmark
768 (bookmark-buffer-name)))
769 (str
770 (or name
771 (read-from-minibuffer
772 (format "Set bookmark (%s): " default)
773 nil
774 (let ((now-map (copy-keymap minibuffer-local-map)))
775 (define-key now-map "\C-w" 'bookmark-yank-word)
776 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
777 now-map))))
778 (annotation nil))
779 (and (string-equal str "") (setq str default))
780 ;; Ask for an annotation buffer for this bookmark
781 (if bookmark-use-annotations
782 (bookmark-read-annotation parg str)
783 (bookmark-make str annotation parg (bookmark-info-current-node))
784 (setq bookmark-current-bookmark str)
785 (bookmark-bmenu-surreptitiously-rebuild-list)
786 (goto-char bookmark-current-point))))
787
788
789 (defun bookmark-info-current-node ()
790 "If in Info-mode, return current node name (a string), else nil."
791 (if (eq major-mode 'Info-mode)
792 Info-current-node))
793
794
795 (defun bookmark-kill-line (&optional newline-too)
796 "Kill from point to end of line.
797 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
798 Does not affect the kill-ring."
799 (let ((eol (save-excursion (end-of-line) (point))))
800 (delete-region (point) eol)
801 (if (and newline-too (looking-at "\n"))
802 (delete-char 1))))
803
804
805 ;; Defvars to avoid compilation warnings:
806 (defvar bookmark-annotation-paragraph nil)
807 (defvar bookmark-annotation-name nil)
808 (defvar bookmark-annotation-buffer nil)
809 (defvar bookmark-annotation-file nil)
810 (defvar bookmark-annotation-point nil)
811
812
813 (defun bookmark-send-annotation ()
814 "Use buffer contents as the annotation for a bookmark.
815 Exclude lines that begin with `#'.
816 Store the annotation text in the bookmark list with
817 the bookmark (and file, and point) specified in buffer local variables."
818 (interactive)
819 (if (not (eq major-mode 'bookmark-read-annotation-mode))
820 (error "Not in bookmark-read-annotation-mode"))
821 (goto-char (point-min))
822 (while (< (point) (point-max))
823 (if (looking-at "^#")
824 (bookmark-kill-line t)
825 (forward-line 1)))
826 (let ((annotation (buffer-string))
827 (parg bookmark-annotation-paragraph)
828 (bookmark bookmark-annotation-name)
829 (pt bookmark-annotation-point)
830 (buf bookmark-annotation-buffer))
831 ;; for bookmark-make-cell to work, we need to be
832 ;; in the relevant buffer, at the relevant point.
833 ;; Actually, bookmark-make-cell should probably be re-written,
834 ;; to avoid this need. Should I handle the error if a buffer is
835 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
836 (save-excursion
837 (pop-to-buffer buf)
838 (goto-char pt)
839 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
840 (setq bookmark-current-bookmark bookmark))
841 (bookmark-bmenu-surreptitiously-rebuild-list)
842 (goto-char bookmark-current-point))
843 (kill-buffer (current-buffer)))
844
845
846 (defun bookmark-default-annotation-text (bookmark)
847 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
848 "# All lines which start with a '#' will be deleted.\n"
849 "# Type C-c C-c when done.\n#\n"
850 "# Author: " (user-full-name) " <" (user-login-name) "@"
851 (system-name) ">\n"
852 "# Date: " (current-time-string) "\n"))
853
854
855 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
856 "Function to return default text to use for a bookmark annotation.
857 It takes the name of the bookmark, as a string, as an arg.")
858
859 (defun bookmark-read-annotation-mode (buf point parg bookmark)
860 "Mode for composing annotations for a bookmark.
861 Wants BUF POINT PARG and BOOKMARK.
862 When you have finished composing, type \\[bookmark-send-annotation] to send
863 the annotation.
864
865 \\{bookmark-read-annotation-mode-map}
866 "
867 (interactive)
868 (kill-all-local-variables)
869 (make-local-variable 'bookmark-annotation-paragraph)
870 (make-local-variable 'bookmark-annotation-name)
871 (make-local-variable 'bookmark-annotation-buffer)
872 (make-local-variable 'bookmark-annotation-file)
873 (make-local-variable 'bookmark-annotation-point)
874 (setq bookmark-annotation-paragraph parg)
875 (setq bookmark-annotation-name bookmark)
876 (setq bookmark-annotation-buffer buf)
877 (setq bookmark-annotation-file (buffer-file-name buf))
878 (setq bookmark-annotation-point point)
879 (use-local-map bookmark-read-annotation-mode-map)
880 (setq major-mode 'bookmark-read-annotation-mode)
881 (insert (funcall bookmark-read-annotation-text-func bookmark))
882 (run-hooks 'text-mode-hook))
883
884
885 (defun bookmark-read-annotation (parg bookmark)
886 "Pop up a buffer for entering a bookmark annotation.
887 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
888 (let ((buf (current-buffer))
889 (point (point)))
890 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
891 (bookmark-read-annotation-mode buf point parg bookmark)))
892
893
894 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
895 "Keymap for editing an annotation of a bookmark.")
896
897
898 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
899 'bookmark-send-edited-annotation)
900
901
902 (defun bookmark-edit-annotation-mode (bookmark)
903 "Mode for editing the annotation of bookmark BOOKMARK.
904 When you have finished composing, type \\[bookmark-send-annotation].
905
906 \\{bookmark-edit-annotation-mode-map}
907 "
908 (interactive)
909 (kill-all-local-variables)
910 (make-local-variable 'bookmark-annotation-name)
911 (setq bookmark-annotation-name bookmark)
912 (use-local-map bookmark-edit-annotation-mode-map)
913 (setq major-mode 'bookmark-edit-annotation-mode)
914 (insert (funcall bookmark-read-annotation-text-func bookmark))
915 (let ((annotation (bookmark-get-annotation bookmark)))
916 (if (and annotation (not (string-equal annotation "")))
917 (insert annotation)))
918 (run-hooks 'text-mode-hook))
919
920
921 (defun bookmark-send-edited-annotation ()
922 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
923 (interactive)
924 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
925 (error "Not in bookmark-edit-annotation-mode"))
926 (goto-char (point-min))
927 (while (< (point) (point-max))
928 (if (looking-at "^#")
929 (bookmark-kill-line t)
930 (forward-line 1)))
931 (let ((annotation (buffer-string))
932 (bookmark bookmark-annotation-name))
933 (bookmark-set-annotation bookmark annotation)
934 (bookmark-bmenu-surreptitiously-rebuild-list)
935 (goto-char bookmark-current-point))
936 (kill-buffer (current-buffer)))
937
938
939 (defun bookmark-edit-annotation (bookmark)
940 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
941 (let ((buf (current-buffer))
942 (point (point)))
943 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
944 (bookmark-edit-annotation-mode bookmark)))
945
946
947 (defun bookmark-insert-current-bookmark ()
948 "Insert this buffer's value of bookmark-current-bookmark.
949 Default to file name if it's nil."
950 (interactive)
951 (let ((str
952 (save-excursion
953 (set-buffer bookmark-current-buffer)
954 bookmark-current-bookmark)))
955 (if str (insert str) (bookmark-insert-buffer-name))))
956
957
958 (defun bookmark-insert-buffer-name ()
959 "Insert the current file name into the bookmark name being set.
960 The directory part of the file name is not used."
961 (interactive)
962 (let ((str
963 (save-excursion
964 (set-buffer bookmark-current-buffer)
965 (bookmark-buffer-name))))
966 (insert str)))
967
968
969 (defun bookmark-buffer-name ()
970 "Return the name of the current buffer's file, non-directory.
971 In Info, return the current node."
972 (cond
973 ;; Are we in Info?
974 ((string-equal mode-name "Info") Info-current-node)
975 ;; Or are we a file?
976 (buffer-file-name (file-name-nondirectory buffer-file-name))
977 ;; Or are we a directory?
978 ((and (boundp 'dired-directory) dired-directory)
979 (let* ((dirname (if (stringp dired-directory)
980 dired-directory
981 (car dired-directory)))
982 (idx (1- (length dirname))))
983 ;; Strip the trailing slash.
984 (if (= ?/ (aref dirname idx))
985 (file-name-nondirectory (substring dirname 0 idx))
986 ;; Else return the current-buffer
987 (buffer-name (current-buffer)))))
988 ;; If all else fails, use the buffer's name.
989 (t
990 (buffer-name (current-buffer)))))
991
992
993 (defun bookmark-yank-word ()
994 (interactive)
995 ;; get the next word from the buffer and append it to the name of
996 ;; the bookmark currently being set.
997 (let ((string (save-excursion
998 (set-buffer bookmark-current-buffer)
999 (goto-char bookmark-yank-point)
1000 (buffer-substring-no-properties
1001 (point)
1002 (progn
1003 (forward-word 1)
1004 (setq bookmark-yank-point (point)))))))
1005 (insert string)))
1006
1007
1008 (defun bookmark-buffer-file-name ()
1009 "Return the current buffer's file in a way useful for bookmarks.
1010 For example, if this is a Info buffer, return the Info file's name."
1011 (if (eq major-mode 'Info-mode)
1012 Info-current-file
1013 (or
1014 buffer-file-name
1015 (if (and (boundp 'dired-directory) dired-directory)
1016 (if (stringp dired-directory)
1017 dired-directory
1018 (car dired-directory))))))
1019
1020
1021 (defun bookmark-maybe-load-default-file ()
1022 (and (not bookmarks-already-loaded)
1023 (null bookmark-alist)
1024 (prog2
1025 (and
1026 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1027 ;; to be renamed.
1028 (file-exists-p (expand-file-name bookmark-old-default-file))
1029 (not (file-exists-p (expand-file-name bookmark-default-file)))
1030 (rename-file (expand-file-name bookmark-old-default-file)
1031 (expand-file-name bookmark-default-file)))
1032 ;; return t so the `and' will continue...
1033 t)
1034
1035 (file-readable-p (expand-file-name bookmark-default-file))
1036 (bookmark-load bookmark-default-file t t)
1037 (setq bookmarks-already-loaded t)))
1038
1039
1040 (defun bookmark-maybe-sort-alist ()
1041 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1042 ;;is non-nil, then return a sorted copy of the alist.
1043 (if bookmark-sort-flag
1044 (setq bookmark-alist
1045 (sort (copy-alist bookmark-alist)
1046 (function
1047 (lambda (x y) (string-lessp (car x) (car y))))))))
1048
1049
1050 ;;;###autoload
1051 (defun bookmark-jump (bookmark)
1052 "Jump to bookmark BOOKMARK (a point in some file).
1053 You may have a problem using this function if the value of variable
1054 `bookmark-alist' is nil. If that happens, you need to load in some
1055 bookmarks. See help on function `bookmark-load' for more about
1056 this.
1057
1058 If the file pointed to by BOOKMARK no longer exists, you will be asked
1059 if you wish to give the bookmark a new location, and bookmark-jump
1060 will then jump to the new location, as well as recording it in place
1061 of the old one in the permanent bookmark record."
1062 (interactive
1063 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1064 (bookmark-maybe-historicize-string bookmark)
1065 (let ((cell (bookmark-jump-noselect bookmark)))
1066 (and cell
1067 (switch-to-buffer (car cell))
1068 (goto-char (cdr cell))
1069 (if bookmark-automatically-show-annotations
1070 ;; if there is an annotation for this bookmark,
1071 ;; show it in a buffer.
1072 (bookmark-show-annotation bookmark)))))
1073
1074
1075 (defun bookmark-file-or-variation-thereof (file)
1076 "Return FILE (a string) if it exists in any reasonable variation, else nil.
1077 Reasonable variations are FILE.gz, FILE.Z, FILE.info, FILE.info.gz, etc."
1078 (cond
1079 ((file-exists-p file) file)
1080 ((file-exists-p (concat file ".Z")) (concat file ".Z"))
1081 ((file-exists-p (concat file ".gz")) (concat file ".gz"))
1082 ((file-exists-p (concat file ".z")) (concat file ".z"))
1083 ((file-exists-p (concat file ".info")) (concat file ".info"))
1084 ((file-exists-p (concat file ".info.gz")) (concat file ".info.gz"))
1085 ((file-exists-p (concat file ".info.Z")) (concat file ".info.Z"))
1086 ((file-exists-p (concat file ".info.z")) (concat file ".info.z"))
1087 ((vc-backend file) file) ; maybe VC has it?
1088 (t nil)))
1089
1090
1091 (defun bookmark-jump-noselect (str)
1092 ;; a leetle helper for bookmark-jump :-)
1093 ;; returns (BUFFER . POINT)
1094 (bookmark-maybe-load-default-file)
1095 (let* ((file (expand-file-name (bookmark-get-filename str)))
1096 (forward-str (bookmark-get-front-context-string str))
1097 (behind-str (bookmark-get-rear-context-string str))
1098 (place (bookmark-get-position str))
1099 (info-node (bookmark-get-info-node str))
1100 (orig-file file)
1101 )
1102 (if (setq file (bookmark-file-or-variation-thereof file))
1103 (save-excursion
1104 (save-window-excursion
1105 (if info-node
1106 ;; Info nodes must be visited with care.
1107 (progn
1108 (require 'info)
1109 (Info-find-node file info-node))
1110 ;; Else no Info. Can do an ordinary find-file:
1111 (set-buffer (find-file-noselect file))
1112 (goto-char place))
1113
1114 ;; Go searching forward first. Then, if forward-str exists and
1115 ;; was found in the file, we can search backward for behind-str.
1116 ;; Rationale is that if text was inserted between the two in the
1117 ;; file, it's better to be put before it so you can read it,
1118 ;; rather than after and remain perhaps unaware of the changes.
1119 (if forward-str
1120 (if (search-forward forward-str (point-max) t)
1121 (goto-char (match-beginning 0))))
1122 (if behind-str
1123 (if (search-backward behind-str (point-min) t)
1124 (goto-char (match-end 0))))
1125 ;; added by db
1126 (setq bookmark-current-bookmark str)
1127 (cons (current-buffer) (point))))
1128
1129 ;; Else unable to find the marked file, so ask if user wants to
1130 ;; relocate the bookmark, else remind them to consider deletion.
1131 (ding)
1132 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1133 " nonexistent. Relocate \""
1134 str
1135 "\"? "))
1136 (progn
1137 (bookmark-relocate str)
1138 ;; gasp! It's a recursive function call in Emacs Lisp!
1139 (bookmark-jump-noselect str))
1140 (message
1141 "Bookmark not relocated; consider removing it \(%s\)." str)
1142 nil))))
1143
1144
1145 ;;;###autoload
1146 (defun bookmark-relocate (bookmark)
1147 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1148 This makes an already existing bookmark point to that file, instead of
1149 the one it used to point at. Useful when a file has been renamed
1150 after a bookmark was set in it."
1151 (interactive (bookmark-completing-read "Bookmark to relocate"))
1152 (bookmark-maybe-historicize-string bookmark)
1153 (bookmark-maybe-load-default-file)
1154 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1155 (newloc (expand-file-name
1156 (read-file-name
1157 (format "Relocate %s to: " bookmark)
1158 (file-name-directory bmrk-filename)))))
1159 (bookmark-set-filename bookmark newloc)
1160 (bookmark-bmenu-surreptitiously-rebuild-list)))
1161
1162
1163 ;;;###autoload
1164 (defun bookmark-insert-location (bookmark &optional no-history)
1165 "Insert the name of the file associated with BOOKMARK.
1166 Optional second arg NO-HISTORY means don't record this in the
1167 minibuffer history list `bookmark-history'."
1168 (interactive (bookmark-completing-read "Insert bookmark location"))
1169 (or no-history (bookmark-maybe-historicize-string bookmark))
1170 (let ((start (point)))
1171 (prog1
1172 (insert (bookmark-location bookmark)) ; *Return this line*
1173 (if (and (display-color-p) (display-mouse-p))
1174 (add-text-properties start
1175 (save-excursion (re-search-backward
1176 "[^ \t]")
1177 (1+ (point)))
1178 '(mouse-face highlight
1179 help-echo "mouse-2: go to this bookmark"))))))
1180
1181 ;;;###autoload
1182 (defalias 'bookmark-locate 'bookmark-insert-location)
1183
1184 (defun bookmark-location (bookmark)
1185 "Return the name of the file associated with BOOKMARK."
1186 (bookmark-maybe-load-default-file)
1187 (bookmark-get-filename bookmark))
1188
1189
1190 ;;;###autoload
1191 (defun bookmark-rename (old &optional new)
1192 "Change the name of OLD bookmark to NEW name.
1193 If called from keyboard, prompt for OLD and NEW. If called from
1194 menubar, select OLD from a menu and prompt for NEW.
1195
1196 If called from Lisp, prompt for NEW if only OLD was passed as an
1197 argument. If called with two strings, then no prompting is done. You
1198 must pass at least OLD when calling from Lisp.
1199
1200 While you are entering the new name, consecutive C-w's insert
1201 consecutive words from the text of the buffer into the new bookmark
1202 name."
1203 (interactive (bookmark-completing-read "Old bookmark name"))
1204 (bookmark-maybe-historicize-string old)
1205 (bookmark-maybe-load-default-file)
1206
1207 (setq bookmark-current-point (point))
1208 (setq bookmark-yank-point (point))
1209 (setq bookmark-current-buffer (current-buffer))
1210 (let ((newname
1211 (or new ; use second arg, if non-nil
1212 (read-from-minibuffer
1213 "New name: "
1214 nil
1215 (let ((now-map (copy-keymap minibuffer-local-map)))
1216 (define-key now-map "\C-w" 'bookmark-yank-word)
1217 now-map)
1218 nil
1219 'bookmark-history))))
1220 (bookmark-set-name old newname)
1221 (setq bookmark-current-bookmark newname)
1222 (bookmark-bmenu-surreptitiously-rebuild-list)
1223 (setq bookmark-alist-modification-count
1224 (1+ bookmark-alist-modification-count))
1225 (if (bookmark-time-to-save-p)
1226 (bookmark-save))))
1227
1228
1229 ;;;###autoload
1230 (defun bookmark-insert (bookmark)
1231 "Insert the text of the file pointed to by bookmark BOOKMARK.
1232 You may have a problem using this function if the value of variable
1233 `bookmark-alist' is nil. If that happens, you need to load in some
1234 bookmarks. See help on function `bookmark-load' for more about
1235 this."
1236 (interactive (bookmark-completing-read "Insert bookmark contents"))
1237 (bookmark-maybe-historicize-string bookmark)
1238 (bookmark-maybe-load-default-file)
1239 (let ((orig-point (point))
1240 (str-to-insert
1241 (save-excursion
1242 (set-buffer (car (bookmark-jump-noselect bookmark)))
1243 (buffer-string))))
1244 (insert str-to-insert)
1245 (push-mark)
1246 (goto-char orig-point)))
1247
1248
1249 ;;;###autoload
1250 (defun bookmark-delete (bookmark &optional batch)
1251 "Delete BOOKMARK from the bookmark list.
1252 Removes only the first instance of a bookmark with that name. If
1253 there are one or more other bookmarks with the same name, they will
1254 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1255 one most recently used in this file, if any\).
1256 Optional second arg BATCH means don't update the bookmark list buffer,
1257 probably because we were called from there."
1258 (interactive
1259 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1260 (bookmark-maybe-historicize-string bookmark)
1261 (bookmark-maybe-load-default-file)
1262 (let ((will-go (bookmark-get-bookmark bookmark)))
1263 (setq bookmark-alist (delq will-go bookmark-alist))
1264 ;; Added by db, nil bookmark-current-bookmark if the last
1265 ;; occurrence has been deleted
1266 (or (bookmark-get-bookmark bookmark-current-bookmark)
1267 (setq bookmark-current-bookmark nil)))
1268 ;; Don't rebuild the list
1269 (if batch
1270 nil
1271 (bookmark-bmenu-surreptitiously-rebuild-list)
1272 (setq bookmark-alist-modification-count
1273 (1+ bookmark-alist-modification-count))
1274 (if (bookmark-time-to-save-p)
1275 (bookmark-save))))
1276
1277
1278 (defun bookmark-time-to-save-p (&optional last-time)
1279 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1280 ;; finds out whether it's time to save bookmarks to a file, by
1281 ;; examining the value of variable bookmark-save-flag, and maybe
1282 ;; bookmark-alist-modification-count. Returns t if they should be
1283 ;; saved, nil otherwise. if last-time is non-nil, then this is
1284 ;; being called when emacs is killed.
1285 (cond (last-time
1286 (and (> bookmark-alist-modification-count 0)
1287 bookmark-save-flag))
1288 ((numberp bookmark-save-flag)
1289 (>= bookmark-alist-modification-count bookmark-save-flag))
1290 (t
1291 nil)))
1292
1293
1294 ;;;###autoload
1295 (defun bookmark-write ()
1296 "Write bookmarks to a file (reading the file name with the minibuffer).
1297 Don't use this in Lisp programs; use `bookmark-save' instead."
1298 (interactive)
1299 (bookmark-maybe-load-default-file)
1300 (bookmark-save t))
1301
1302
1303 ;;;###autoload
1304 (defun bookmark-save (&optional parg file)
1305 "Save currently defined bookmarks.
1306 Saves by default in the file defined by the variable
1307 `bookmark-default-file'. With a prefix arg, save it in file FILE
1308 \(second argument\).
1309
1310 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1311 and FILE, and if you just want it to write to the default file, then
1312 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1313 instead. If you pass in one argument, and it is non-nil, then the
1314 user will be interactively queried for a file to save in.
1315
1316 When you want to load in the bookmarks from a file, use
1317 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1318 for a file, defaulting to the file defined by variable
1319 `bookmark-default-file'."
1320 (interactive "P")
1321 (bookmark-maybe-load-default-file)
1322 (cond
1323 ((and (null parg) (null file))
1324 ;;whether interactive or not, write to default file
1325 (bookmark-write-file bookmark-default-file))
1326 ((and (null parg) file)
1327 ;;whether interactive or not, write to given file
1328 (bookmark-write-file file))
1329 ((and parg (not file))
1330 ;;have been called interactively w/ prefix arg
1331 (let ((file (read-file-name "File to save bookmarks in: ")))
1332 (bookmark-write-file file)))
1333 (t ; someone called us with prefix-arg *and* a file, so just write to file
1334 (bookmark-write-file file)))
1335 ;; signal that we have synced the bookmark file by setting this to
1336 ;; 0. If there was an error at any point before, it will not get
1337 ;; set, which is what we want.
1338 (setq bookmark-alist-modification-count 0))
1339
1340
1341 \f
1342 (defun bookmark-write-file (file)
1343 (save-excursion
1344 (save-window-excursion
1345 (if (>= baud-rate 9600)
1346 (message "Saving bookmarks to file %s..." file))
1347 (set-buffer (let ((enable-local-variables nil))
1348 (find-file-noselect file)))
1349 (goto-char (point-min))
1350 (let ((print-length nil)
1351 (print-level nil))
1352 (delete-region (point-min) (point-max))
1353 (bookmark-insert-file-format-version-stamp)
1354 (pp bookmark-alist (current-buffer))
1355 (let ((version-control
1356 (cond
1357 ((null bookmark-version-control) nil)
1358 ((eq 'never bookmark-version-control) 'never)
1359 ((eq 'nospecial bookmark-version-control) version-control)
1360 (t
1361 t))))
1362 (write-file file)
1363 (kill-buffer (current-buffer))
1364 (if (>= baud-rate 9600)
1365 (message "Saving bookmarks to file %s...done" file)))))))
1366
1367
1368 (defun bookmark-import-new-list (new-list)
1369 ;; Walk over the new list, adding each individual bookmark
1370 ;; carefully. "Carefully" means checking against the existing
1371 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1372 ;; as necessary.
1373 (let ((lst new-list)
1374 (names (bookmark-all-names)))
1375 (while lst
1376 (let* ((full-record (car lst)))
1377 (bookmark-maybe-rename full-record names)
1378 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1379 (setq names (cons (bookmark-name-from-full-record full-record) names))
1380 (setq lst (cdr lst))))))
1381
1382
1383 (defun bookmark-maybe-rename (full-record names)
1384 ;; just a helper for bookmark-import-new-list; it is only for
1385 ;; readability that this is not inlined.
1386 ;;
1387 ;; Once this has found a free name, it sets full-record to that
1388 ;; name.
1389 (let ((found-name (bookmark-name-from-full-record full-record)))
1390 (if (member found-name names)
1391 ;; We've got a conflict, so generate a new name
1392 (let ((count 2)
1393 (new-name found-name))
1394 (while (member new-name names)
1395 (setq new-name (concat found-name (format "<%d>" count)))
1396 (setq count (1+ count)))
1397 (bookmark-set-name full-record new-name)))))
1398
1399
1400 ;;;###autoload
1401 (defun bookmark-load (file &optional overwrite no-msg)
1402 "Load bookmarks from FILE (which must be in bookmark format).
1403 Appends loaded bookmarks to the front of the list of bookmarks. If
1404 optional second argument OVERWRITE is non-nil, existing bookmarks are
1405 destroyed. Optional third arg NO-MSG means don't display any messages
1406 while loading.
1407
1408 If you load a file that doesn't contain a proper bookmark alist, you
1409 will corrupt Emacs's bookmark list. Generally, you should only load
1410 in files that were created with the bookmark functions in the first
1411 place. Your own personal bookmark file, `~/.emacs.bmk', is
1412 maintained automatically by Emacs; you shouldn't need to load it
1413 explicitly.
1414
1415 If you load a file containing bookmarks with the same names as
1416 bookmarks already present in your Emacs, the new bookmarks will get
1417 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1418 method buffers use to resolve name collisions."
1419 (interactive
1420 (list (read-file-name
1421 (format "Load bookmarks from: (%s) "
1422 bookmark-default-file)
1423 ;;Default might not be used often,
1424 ;;but there's no better default, and
1425 ;;I guess it's better than none at all.
1426 "~/" bookmark-default-file 'confirm)))
1427 (setq file (expand-file-name file))
1428 (if (file-readable-p file)
1429 (save-excursion
1430 (save-window-excursion
1431 (if (and (null no-msg) (>= baud-rate 9600))
1432 (message "Loading bookmarks from %s..." file))
1433 (set-buffer (let ((enable-local-variables nil))
1434 (find-file-noselect file)))
1435 (goto-char (point-min))
1436 (bookmark-maybe-upgrade-file-format)
1437 (let ((blist (bookmark-alist-from-buffer)))
1438 (if (listp blist)
1439 (progn
1440 (if overwrite
1441 (progn
1442 (setq bookmark-alist blist)
1443 (setq bookmark-alist-modification-count 0))
1444 ;; else
1445 (bookmark-import-new-list blist)
1446 (setq bookmark-alist-modification-count
1447 (1+ bookmark-alist-modification-count)))
1448 (if (string-equal
1449 (expand-file-name bookmark-default-file)
1450 file)
1451 (setq bookmarks-already-loaded t))
1452 (bookmark-bmenu-surreptitiously-rebuild-list))
1453 (error "Invalid bookmark list in %s" file)))
1454 (kill-buffer (current-buffer)))
1455 (if (and (null no-msg) (>= baud-rate 9600))
1456 (message "Loading bookmarks from %s...done" file)))
1457 (error "Cannot read bookmark file %s" file)))
1458
1459
1460 \f
1461 ;;; Code supporting the dired-like bookmark menu. Prefix is
1462 ;;; "bookmark-bmenu" for "buffer-menu":
1463
1464
1465 (defvar bookmark-bmenu-bookmark-column nil)
1466
1467
1468 (defvar bookmark-bmenu-hidden-bookmarks ())
1469
1470
1471 (defvar bookmark-bmenu-mode-map nil)
1472
1473
1474 (if bookmark-bmenu-mode-map
1475 nil
1476 (setq bookmark-bmenu-mode-map (make-keymap))
1477 (suppress-keymap bookmark-bmenu-mode-map t)
1478 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1479 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1480 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1481 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1482 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1483 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1484 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1485 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1486 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1487 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1488 (define-key bookmark-bmenu-mode-map "\C-o"
1489 'bookmark-bmenu-switch-other-window)
1490 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1491 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1492 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1493 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1494 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1495 (define-key bookmark-bmenu-mode-map " " 'next-line)
1496 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1497 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1498 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1499 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1500 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1501 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1502 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1503 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1504 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1505 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1506 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1507 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1508 (define-key bookmark-bmenu-mode-map [mouse-2]
1509 'bookmark-bmenu-other-window-with-mouse))
1510
1511
1512
1513 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1514 ;; data.
1515 (put 'bookmark-bmenu-mode 'mode-class 'special)
1516
1517
1518 ;; todo: need to display whether or not bookmark exists as a buffer in
1519 ;; flag column.
1520
1521 ;; Format:
1522 ;; FLAGS BOOKMARK [ LOCATION ]
1523
1524
1525 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1526 "Rebuild the Bookmark List if it exists.
1527 Don't affect the buffer ring order."
1528 (if (get-buffer "*Bookmark List*")
1529 (save-excursion
1530 (save-window-excursion
1531 (bookmark-bmenu-list)))))
1532
1533
1534 ;;;###autoload
1535 (defun bookmark-bmenu-list ()
1536 "Display a list of existing bookmarks.
1537 The list is displayed in a buffer named `*Bookmark List*'.
1538 The leftmost column displays a D if the bookmark is flagged for
1539 deletion, or > if it is flagged for displaying."
1540 (interactive)
1541 (bookmark-maybe-load-default-file)
1542 (if (interactive-p)
1543 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1544 (set-buffer (get-buffer-create "*Bookmark List*")))
1545 (let ((buffer-read-only nil))
1546 (delete-region (point-max) (point-min))
1547 (goto-char (point-min)) ;sure are playing it safe...
1548 (insert "% Bookmark\n- --------\n")
1549 (bookmark-maybe-sort-alist)
1550 (mapcar
1551 (lambda (full-record)
1552 ;; if a bookmark has an annotation, prepend a "*"
1553 ;; in the list of bookmarks.
1554 (let ((annotation (bookmark-get-annotation
1555 (bookmark-name-from-full-record full-record))))
1556 (if (and annotation (not (string-equal annotation "")))
1557 (insert " *")
1558 (insert " "))
1559 (let ((start (point)))
1560 (insert (bookmark-name-from-full-record full-record))
1561 (if (and (display-color-p) (display-mouse-p))
1562 (add-text-properties start
1563 (save-excursion (re-search-backward
1564 "[^ \t]")
1565 (1+ (point)))
1566 '(mouse-face highlight
1567 help-echo "mouse-2: go to this bookmark")))
1568 (insert "\n")
1569 )))
1570 bookmark-alist))
1571 (goto-char (point-min))
1572 (forward-line 2)
1573 (bookmark-bmenu-mode)
1574 (if bookmark-bmenu-toggle-filenames
1575 (bookmark-bmenu-toggle-filenames t)))
1576
1577 ;;;###autoload
1578 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1579 ;;;###autoload
1580 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1581
1582
1583
1584 (defun bookmark-bmenu-mode ()
1585 "Major mode for editing a list of bookmarks.
1586 Each line describes one of the bookmarks in Emacs.
1587 Letters do not insert themselves; instead, they are commands.
1588 Bookmark names preceded by a \"*\" have annotations.
1589 \\<bookmark-bmenu-mode-map>
1590 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1591 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1592 Also show bookmarks marked using m in other windows.
1593 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1594 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1595 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1596 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1597 together with bookmark selected before this one in another window.
1598 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1599 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1600 so the bookmark menu bookmark remains visible in its window.
1601 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1602 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1603 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1604 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1605 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1606 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1607 With a prefix arg, prompts for a file to save in.
1608 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1609 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1610 With prefix argument, also move up one line.
1611 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1612 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1613 in another buffer.
1614 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1615 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1616 (kill-all-local-variables)
1617 (use-local-map bookmark-bmenu-mode-map)
1618 (setq truncate-lines t)
1619 (setq buffer-read-only t)
1620 (setq major-mode 'bookmark-bmenu-mode)
1621 (setq mode-name "Bookmark Menu")
1622 (run-hooks 'bookmark-bmenu-mode-hook))
1623
1624
1625 (defun bookmark-bmenu-toggle-filenames (&optional show)
1626 "Toggle whether filenames are shown in the bookmark list.
1627 Optional argument SHOW means show them unconditionally."
1628 (interactive)
1629 (cond
1630 (show
1631 (setq bookmark-bmenu-toggle-filenames nil)
1632 (bookmark-bmenu-show-filenames)
1633 (setq bookmark-bmenu-toggle-filenames t))
1634 (bookmark-bmenu-toggle-filenames
1635 (bookmark-bmenu-hide-filenames)
1636 (setq bookmark-bmenu-toggle-filenames nil))
1637 (t
1638 (bookmark-bmenu-show-filenames)
1639 (setq bookmark-bmenu-toggle-filenames t))))
1640
1641
1642 (defun bookmark-bmenu-show-filenames (&optional force)
1643 (if (and (not force) bookmark-bmenu-toggle-filenames)
1644 nil ;already shown, so do nothing
1645 (save-excursion
1646 (save-window-excursion
1647 (goto-char (point-min))
1648 (forward-line 2)
1649 (setq bookmark-bmenu-hidden-bookmarks ())
1650 (let ((buffer-read-only nil))
1651 (while (< (point) (point-max))
1652 (let ((bmrk (bookmark-bmenu-bookmark)))
1653 (setq bookmark-bmenu-hidden-bookmarks
1654 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1655 (let ((start (save-excursion (end-of-line) (point))))
1656 (move-to-column bookmark-bmenu-file-column t)
1657 ;; Strip off `mouse-face' from the white spaces region.
1658 (if (and (display-color-p) (display-mouse-p))
1659 (remove-text-properties start (point)
1660 '(mouse-face nil help-echo nil))))
1661 (delete-region (point) (progn (end-of-line) (point)))
1662 (insert " ")
1663 ;; Pass the NO-HISTORY arg:
1664 (bookmark-insert-location bmrk t)
1665 (forward-line 1))))))))
1666
1667
1668 (defun bookmark-bmenu-hide-filenames (&optional force)
1669 (if (and (not force) bookmark-bmenu-toggle-filenames)
1670 ;; nothing to hide if above is nil
1671 (save-excursion
1672 (save-window-excursion
1673 (goto-char (point-min))
1674 (forward-line 2)
1675 (setq bookmark-bmenu-hidden-bookmarks
1676 (nreverse bookmark-bmenu-hidden-bookmarks))
1677 (save-excursion
1678 (goto-char (point-min))
1679 (search-forward "Bookmark")
1680 (backward-word 1)
1681 (setq bookmark-bmenu-bookmark-column (current-column)))
1682 (save-excursion
1683 (let ((buffer-read-only nil))
1684 (while bookmark-bmenu-hidden-bookmarks
1685 (move-to-column bookmark-bmenu-bookmark-column t)
1686 (bookmark-kill-line)
1687 (let ((start (point)))
1688 (insert (car bookmark-bmenu-hidden-bookmarks))
1689 (if (and (display-color-p) (display-mouse-p))
1690 (add-text-properties start
1691 (save-excursion (re-search-backward
1692 "[^ \t]")
1693 (1+ (point)))
1694 '(mouse-face highlight
1695 help-echo
1696 "mouse-2: go to this bookmark"))))
1697 (setq bookmark-bmenu-hidden-bookmarks
1698 (cdr bookmark-bmenu-hidden-bookmarks))
1699 (forward-line 1))))))))
1700
1701
1702 (defun bookmark-bmenu-check-position ()
1703 ;; Returns non-nil if on a line with a bookmark.
1704 ;; (The actual value returned is bookmark-alist).
1705 ;; Else reposition and try again, else return nil.
1706 (cond ((< (count-lines (point-min) (point)) 2)
1707 (goto-char (point-min))
1708 (forward-line 2)
1709 bookmark-alist)
1710 ((and (bolp) (eobp))
1711 (beginning-of-line 0)
1712 bookmark-alist)
1713 (t
1714 bookmark-alist)))
1715
1716
1717 (defun bookmark-bmenu-bookmark ()
1718 ;; return a string which is bookmark of this line.
1719 (if (bookmark-bmenu-check-position)
1720 (save-excursion
1721 (save-window-excursion
1722 (goto-char (point-min))
1723 (search-forward "Bookmark")
1724 (backward-word 1)
1725 (setq bookmark-bmenu-bookmark-column (current-column)))))
1726 (if bookmark-bmenu-toggle-filenames
1727 (bookmark-bmenu-hide-filenames))
1728 (save-excursion
1729 (save-window-excursion
1730 (beginning-of-line)
1731 (forward-char bookmark-bmenu-bookmark-column)
1732 (prog1
1733 (buffer-substring-no-properties (point)
1734 (progn
1735 (end-of-line)
1736 (point)))
1737 ;; well, this is certainly crystal-clear:
1738 (if bookmark-bmenu-toggle-filenames
1739 (bookmark-bmenu-toggle-filenames t))))))
1740
1741
1742 (defun bookmark-show-annotation (bookmark)
1743 "Display the annotation for bookmark named BOOKMARK in a buffer,
1744 if an annotation exists."
1745 (let ((annotation (bookmark-get-annotation bookmark)))
1746 (if (and annotation (not (string-equal annotation "")))
1747 (save-excursion
1748 (let ((old-buf (current-buffer)))
1749 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1750 (delete-region (point-min) (point-max))
1751 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1752 (insert annotation)
1753 (goto-char (point-min))
1754 (pop-to-buffer old-buf))))))
1755
1756
1757 (defun bookmark-show-all-annotations ()
1758 "Display the annotations for all bookmarks in a buffer."
1759 (let ((old-buf (current-buffer)))
1760 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1761 (delete-region (point-min) (point-max))
1762 (mapcar
1763 (lambda (full-record)
1764 (let* ((name (bookmark-name-from-full-record full-record))
1765 (ann (bookmark-get-annotation name)))
1766 (insert (concat name ":\n"))
1767 (if (and ann (not (string-equal ann "")))
1768 ;; insert the annotation, indented by 4 spaces.
1769 (progn
1770 (save-excursion (insert ann))
1771 (while (< (point) (point-max))
1772 (beginning-of-line) ; paranoia
1773 (insert " ")
1774 (forward-line)
1775 (end-of-line))))))
1776 bookmark-alist)
1777 (goto-char (point-min))
1778 (pop-to-buffer old-buf)))
1779
1780
1781 (defun bookmark-bmenu-mark ()
1782 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1783 (interactive)
1784 (beginning-of-line)
1785 (if (bookmark-bmenu-check-position)
1786 (let ((buffer-read-only nil))
1787 (delete-char 1)
1788 (insert ?>)
1789 (forward-line 1)
1790 (bookmark-bmenu-check-position))))
1791
1792
1793 (defun bookmark-bmenu-select ()
1794 "Select this line's bookmark; also display bookmarks marked with `>'.
1795 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1796 (interactive)
1797 (if (bookmark-bmenu-check-position)
1798 (let ((bmrk (bookmark-bmenu-bookmark))
1799 (menu (current-buffer))
1800 (others ())
1801 tem)
1802 (goto-char (point-min))
1803 (while (re-search-forward "^>" nil t)
1804 (setq tem (bookmark-bmenu-bookmark))
1805 (let ((buffer-read-only nil))
1806 (delete-char -1)
1807 (insert ?\ ))
1808 (or (string-equal tem bmrk)
1809 (member tem others)
1810 (setq others (cons tem others))))
1811 (setq others (nreverse others)
1812 tem (/ (1- (frame-height)) (1+ (length others))))
1813 (delete-other-windows)
1814 (bookmark-jump bmrk)
1815 (bury-buffer menu)
1816 (if others
1817 (while others
1818 (split-window nil tem)
1819 (other-window 1)
1820 (bookmark-jump (car others))
1821 (setq others (cdr others)))
1822 (other-window 1)))))
1823
1824
1825 (defun bookmark-bmenu-save (parg)
1826 "Save the current list into a bookmark file.
1827 With a prefix arg, prompts for a file to save them in."
1828 (interactive "P")
1829 (save-excursion
1830 (save-window-excursion
1831 (bookmark-save parg))))
1832
1833
1834 (defun bookmark-bmenu-load ()
1835 "Load the bookmark file and rebuild the bookmark menu-buffer."
1836 (interactive)
1837 (if (bookmark-bmenu-check-position)
1838 (save-excursion
1839 (save-window-excursion
1840 ;; This will call `bookmark-bmenu-list'
1841 (call-interactively 'bookmark-load)))))
1842
1843
1844 (defun bookmark-bmenu-1-window ()
1845 "Select this line's bookmark, alone, in full frame."
1846 (interactive)
1847 (if (bookmark-bmenu-check-position)
1848 (progn
1849 (bookmark-jump (bookmark-bmenu-bookmark))
1850 (bury-buffer (other-buffer))
1851 (delete-other-windows))))
1852
1853
1854 (defun bookmark-bmenu-2-window ()
1855 "Select this line's bookmark, with previous buffer in second window."
1856 (interactive)
1857 (if (bookmark-bmenu-check-position)
1858 (let ((bmrk (bookmark-bmenu-bookmark))
1859 (menu (current-buffer))
1860 (pop-up-windows t))
1861 (delete-other-windows)
1862 (switch-to-buffer (other-buffer))
1863 (let* ((pair (bookmark-jump-noselect bmrk))
1864 (buff (car pair))
1865 (pos (cdr pair)))
1866 (pop-to-buffer buff)
1867 (goto-char pos))
1868 (bury-buffer menu))))
1869
1870
1871 (defun bookmark-bmenu-this-window ()
1872 "Select this line's bookmark in this window."
1873 (interactive)
1874 (if (bookmark-bmenu-check-position)
1875 (bookmark-jump (bookmark-bmenu-bookmark))))
1876
1877
1878 (defun bookmark-bmenu-other-window ()
1879 "Select this line's bookmark in other window, leaving bookmark menu visible."
1880 (interactive)
1881 (let ((bookmark (bookmark-bmenu-bookmark)))
1882 (if (bookmark-bmenu-check-position)
1883 (let* ((pair (bookmark-jump-noselect bookmark))
1884 (buff (car pair))
1885 (pos (cdr pair)))
1886 (switch-to-buffer-other-window buff)
1887 (goto-char pos)
1888 (set-window-point (get-buffer-window buff) pos)
1889 (bookmark-show-annotation bookmark)))))
1890
1891
1892 (defun bookmark-bmenu-switch-other-window ()
1893 "Make the other window select this line's bookmark.
1894 The current window remains selected."
1895 (interactive)
1896 (let ((bookmark (bookmark-bmenu-bookmark))
1897 (pop-up-windows t)
1898 same-window-buffer-names
1899 same-window-regexps)
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 ;;; Exit Hook, called from kill-emacs-hook
2273 (defvar bookmark-exit-hook nil
2274 "Hook to run when emacs exits")
2275
2276 (defun bookmark-exit-hook-internal ()
2277 "Save bookmark state, if necessary, at Emacs exit time.
2278 This also runs `bookmark-exit-hooks'."
2279 (and
2280 (progn (run-hooks 'bookmark-exit-hooks) t)
2281 bookmark-alist
2282 (bookmark-time-to-save-p t)
2283 (bookmark-save)))
2284
2285 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2286
2287
2288 (run-hooks 'bookmark-load-hook)
2289
2290 (provide 'bookmark)
2291
2292 ;;; bookmark.el ends here