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