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