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