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