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