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