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