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