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