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