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