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