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