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