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