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