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