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