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