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