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