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