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