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