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