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