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