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