(bookmark-alist-from-buffer): Fix error text.
[emacs.git] / lisp / bookmark.el
blobe3fbe7c41fcf02bf5a698b4ca8dd3d5de0909f6f
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later.
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Author's Update Number: see variable `bookmark-version'.
9 ;; Keywords: bookmarks, placeholders, annotations
11 ;; This file is part of GNU Emacs.
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.
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.
23 ;; You should have received a copy of the GNU General Public License
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.
28 ;;; Commentary:
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.
35 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
36 ;; then implementing the bookmark-current-bookmark idea. He even
37 ;; sent *patches*, bless his soul...
39 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
40 ;; fixing and improving bookmark-time-to-save-p.
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
44 ;; and the menu-bar).
46 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
47 ;; suggestions and the code to implement them (like
48 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
49 ;; stuff).
51 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
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.
56 ;; Thanks to Roland McGrath for encouragement and help with defining
57 ;; autoloads on the menu-bar.
59 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
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!)
64 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
65 ;; thinking up the annotations feature and implementing it so well.
67 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
68 ;; <olstad@msc.edu>.
70 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
71 ;; reported and fixed.
73 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
75 ;; Enough with the credits already, get on to the good stuff:
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?
81 ;;;; Code:
83 (require 'pp)
85 (defconst bookmark-version "2.6.20"
86 "Version number of bookmark.el. This is not related to the version
87 of Emacs bookmark comes with; it is used solely by bookmark's
88 maintainers to avoid version confusion.")
90 ;;; Misc comments:
92 ;; If variable bookmark-use-annotations is non-nil, an annotation is
93 ;; queried for when setting a bookmark.
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.
100 ;;; User Variables
102 (defvar bookmark-use-annotations nil
103 "*If non-nil, saving a bookmark will query for an annotation in a
104 buffer.")
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.
112 --> Otherwise, it should be a number that is the frequency with which
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.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 bookmark-default-file, which is `~/.emacs.bmk' by default.")
126 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
127 "*The .emacs.bmk file used to be called this.")
130 ;; defvarred to avoid a compilation warning:
131 (defvar bookmark-file nil
132 "Old name for `bookmark-default-file'.")
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.")
144 (defvar bookmark-version-control 'nospecial
145 "*Whether or not to make numbered backups of the bookmark file.
146 It can have four values: t, nil, `never', and `nospecial'.
147 The first three have the same meaning that they do for the
148 variable `version-control', and the final value `nospecial' means just
149 use the value of `version-control'.")
152 (defvar bookmark-completion-ignore-case t
153 "*Non-nil means bookmark functions ignore case in completion.")
156 (defvar bookmark-sort-flag t
157 "*Non-nil means that bookmarks will be displayed sorted by bookmark
158 name. Otherwise they will be displayed in LIFO order (that is, most
159 recently set ones come first, oldest ones come last).")
162 (defvar bookmark-automatically-show-annotations t
163 "*Nil means don't show annotations when jumping to a bookmark.")
166 (defvar bookmark-bmenu-file-column 30
167 "*Column at which to display filenames in a buffer listing bookmarks.
168 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames].")
171 (defvar bookmark-bmenu-toggle-filenames t
172 "*Non-nil means show filenames when listing bookmarks.
173 This may result in truncated bookmark names. To disable this, put the
174 following in your .emacs:
176 \(setq bookmark-bmenu-toggle-filenames nil\)")
179 (defvar bookmark-menu-length 70
180 "*Maximum length of a bookmark name displayed on a popup menu.")
183 ;;; No user-serviceable parts beyond this point.
185 ;; Is it XEmacs?
186 (defconst bookmark-xemacsp
187 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
190 ;; Added for lucid emacs compatibility, db
191 (or (fboundp 'defalias) (fset 'defalias 'fset))
193 ;; suggested for lucid compatibility by david hughes:
194 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
196 ;; This variable is probably obsolete now...
197 (or (boundp 'baud-rate)
198 ;; some random value higher than 9600
199 (setq baud-rate 19200))
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)))
210 ;;; Keymap stuff:
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
215 ;; else will be done with M-x or the menubar:
216 ;;;###autoload
217 (if (symbolp (key-binding "\C-xr"))
219 (progn (define-key ctl-x-map "rb" 'bookmark-jump)
220 (define-key ctl-x-map "rm" 'bookmark-set)
221 (define-key ctl-x-map "rl" 'bookmark-bmenu-list)))
223 ;; define the map, so it can be bound by those who desire to do so:
225 ;;;###autoload
226 (defvar bookmark-map nil
227 "Keymap containing bindings to bookmark functions.
228 It is not bound to any key by default: to bind it
229 so that you have a bookmark prefix, just use `global-set-key' and bind a
230 key of your choice to `bookmark-map'. All interactive bookmark
231 functions have a binding in this keymap.")
233 ;;;###autoload
234 (define-prefix-command 'bookmark-map)
236 ;; Read the help on all of these functions for details...
237 ;;;###autoload
238 (define-key bookmark-map "x" 'bookmark-set)
239 ;;;###autoload
240 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
241 ;;;###autoload
242 (define-key bookmark-map "j" 'bookmark-jump)
243 ;;;###autoload
244 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
245 ;;;###autoload
246 (define-key bookmark-map "i" 'bookmark-insert)
247 ;;;###autoload
248 (define-key bookmark-map "e" 'edit-bookmarks)
249 ;;;###autoload
250 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
251 ;;;###autoload
252 (define-key bookmark-map "r" 'bookmark-rename)
253 ;;;###autoload
254 (define-key bookmark-map "d" 'bookmark-delete)
255 ;;;###autoload
256 (define-key bookmark-map "l" 'bookmark-load)
257 ;;;###autoload
258 (define-key bookmark-map "w" 'bookmark-write)
259 ;;;###autoload
260 (define-key bookmark-map "s" 'bookmark-save)
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.")
267 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
268 'bookmark-send-annotation)
272 ;;; Core variables and data structures:
273 (defvar bookmark-alist ()
274 "Association list of bookmarks and their records.
275 You probably don't want to change the value of this alist yourself;
276 instead, let the various bookmark functions do it for you.
278 The format of the alist is
280 \(BOOKMARK1 BOOKMARK2 ...\)
282 where each BOOKMARK is of the form
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\)\)
292 So the cdr of each bookmark is an alist too.
293 `info-node' is optional, by the way.")
296 (defvar bookmarks-already-loaded nil)
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.
301 ;;;###autoload
302 (add-hook 'kill-emacs-hook
303 (function
304 (lambda () (and (featurep 'bookmark)
305 bookmark-alist
306 (bookmark-time-to-save-p t)
307 (bookmark-save)))))
309 ;; more stuff added by db.
311 (defvar bookmark-current-bookmark nil
312 "Name of bookmark most recently used in the current file.
313 It is buffer local, used to make moving a bookmark forward
314 through a file easier.")
316 (make-variable-buffer-local 'bookmark-current-bookmark)
319 (defvar bookmark-alist-modification-count 0
320 "Number of modifications to bookmark list since it was last saved.")
323 (defvar bookmark-search-size 16
324 "Length of the context strings recorded on either side of a bookmark.")
327 (defvar bookmark-current-point 0)
328 (defvar bookmark-yank-point 0)
329 (defvar bookmark-current-buffer nil)
333 ;; Helper functions.
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.
339 (defun bookmark-name-from-full-record (full-record)
340 "Return name of FULL-RECORD \(an alist element instead of a string\)."
341 (car full-record))
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))
353 (defun bookmark-get-bookmark (bookmark)
354 "Return the full entry for BOOKMARK in bookmark-alist."
355 (assoc bookmark bookmark-alist))
358 (defun bookmark-get-bookmark-record (bookmark)
359 "Return the guts of the entry for BOOKMARK in bookmark-alist.
360 That is, all information but the name."
361 (car (cdr (bookmark-get-bookmark bookmark))))
364 (defun bookmark-set-name (bookmark newname)
365 "Set BOOKMARK's name to NEWNAME."
366 (setcar (bookmark-get-bookmark bookmark) newname))
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))))
374 (defun bookmark-set-annotation (bookmark ann)
375 "Set the annotation of BOOKMARK to ANN."
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))))))
383 (defun bookmark-get-filename (bookmark)
384 "Return the full filename of BOOKMARK."
385 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
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))))))
397 (defun bookmark-get-position (bookmark)
398 "Return the position \(i.e.: point\) of BOOKMARK."
399 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
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))))))
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))))
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))))))
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))))
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))))))
441 (defun bookmark-get-info-node (bookmark)
442 "Get the info node associated with BOOKMARK."
443 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
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))))))
456 (defvar bookmark-history nil
457 "The history list for bookmark functions.")
460 (defun bookmark-completing-read (prompt &optional default)
461 "Prompting with PROMPT, read a bookmark name in completion.
462 PROMPT will get a \": \" stuck on the end no matter what, so you
463 probably don't want to include one yourself.
464 Optional second arg DEFAULT is a string to return if the user enters
465 the 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
478 'bookmark-history)))
479 (if (string-equal "" str)
480 (list default)
481 (list str))))
484 (defmacro bookmark-maybe-historicize-string (string)
485 "Put STRING into the bookmark prompt history, if caller non-interactive.
486 We need this because sometimes bookmark functions are invoked from
487 menus, so `completing-read' never gets a chance to set `bookmark-history'."
488 (` (or
489 (interactive-p)
490 (setq bookmark-history (cons (, string) bookmark-history)))))
493 (defun bookmark-make (name &optional annotation overwrite)
494 "Make a bookmark named NAME.
495 Optional second arg ANNOTATION gives it an annotation.
496 Optional third arg OVERWRITE means replace any existing bookmarks with
497 this name."
498 (bookmark-maybe-load-default-file)
499 (let ((stripped-name (copy-sequence name)))
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))
504 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
505 ;; already existing bookmark under that name and
506 ;; no prefix arg means just overwrite old bookmark
507 (setcdr (bookmark-get-bookmark stripped-name)
508 (list (bookmark-make-cell annotation)))
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...)
514 (setq bookmark-alist
515 (cons
516 (list stripped-name
517 (bookmark-make-cell annotation))
518 bookmark-alist)))
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))))
528 (defun bookmark-make-cell (annotation)
529 "Return the record part of a new bookmark, given ANNOTATION.
530 Must be at the correct position in the buffer in which the bookmark is
531 being set. This will change soon."
532 (` ((filename . (, (bookmark-buffer-file-name)))
533 (front-context-string
534 . (, (if (>= (- (point-max) (point)) bookmark-search-size)
535 (buffer-substring-no-properties
536 (point)
537 (+ (point) bookmark-search-size))
538 nil)))
539 (rear-context-string
540 . (, (if (>= (- (point) (point-min)) bookmark-search-size)
541 (buffer-substring-no-properties
542 (point)
543 (- (point) bookmark-search-size))
544 nil)))
545 (position . (, (point)))
546 (annotation . (, annotation)))))
549 ;;; File format stuff
551 ;; The OLD format of the bookmark-alist was:
553 ;; ((bookmark-name (filename
554 ;; string-in-front
555 ;; string-behind
556 ;; point))
557 ;; ...)
559 ;; The NEW format of the bookmark-alist is:
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 ;; ...)
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.
578 ;; BOOKMARK-NAME is the string the user gives the bookmark and
579 ;; accesses it by from then on.
581 ;; FILENAME is the location of the file in which the bookmark is set.
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.
586 ;; STRING-BEHIND is the same thing, but after the point.
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.
597 ;; ANNOTATION is an annotation for the bookmark.
600 (defconst bookmark-file-format-version 1
601 "The current version of the format used by bookmark files.
602 You should never need to change this.")
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.")
610 (defun bookmark-alist-from-buffer ()
611 "Return a bookmark-alist (in any format) from the current buffer.
612 The buffer must of course contain bookmark format information.
613 Does not care from where in the buffer it is called, and does not
614 affect 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")))))
628 (defun bookmark-upgrade-version-0-alist (old-list)
629 "Upgrade a version 0 alist OLD-LIST to the current version."
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))
649 (defun bookmark-upgrade-file-format-from-0 ()
650 "Upgrade a bookmark file of format 0 (the original format) to format 1.
651 This 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))
661 (message "Upgrading bookmark format from 0 to %d...done"
662 bookmark-file-format-version)
666 (defun bookmark-grok-file-format-version ()
667 "Return an integer which is the file-format version of this bookmark file.
668 This 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.
680 (defun bookmark-maybe-upgrade-file-format ()
681 "Check the file-format version of this bookmark file.
682 If the version is not up-to-date, upgrade it automatically.
683 This 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))
691 (error "Bookmark file format version strangeness")))))
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))
705 ;;; end file-format stuff
708 ;;; Core code:
710 ;;;###autoload
711 (defun bookmark-set (&optional name parg)
712 "Set a bookmark named NAME inside a file.
713 If name is nil, then the user will be prompted.
714 With prefix arg, will not overwrite a bookmark that has the same name
715 as NAME if such a bookmark already exists, but instead will \"push\"
716 the new bookmark onto the bookmark alist. Thus the most recently set
717 bookmark with name NAME would be the one in effect at any given time,
718 but the others are still there, should you decide to delete the most
719 recent one.
721 To yank words from the text of the buffer and use them as part of the
722 bookmark name, type C-w while setting a bookmark. Successive C-w's
723 yank successive words.
725 Typing 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
727 through a large file\). If no bookmark was used, then C-u inserts the
728 name of the file being visited.
730 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
731 and it removes only the first instance of a bookmark with that name from
732 the list of bookmarks.\)"
733 (interactive (list nil current-prefix-arg))
735 (bookmark-buffer-file-name)
736 (error "Buffer not visiting a file or directory"))
738 (bookmark-maybe-load-default-file)
740 (setq bookmark-current-point (point))
741 (setq bookmark-yank-point (point))
742 (setq bookmark-current-buffer (current-buffer))
744 (let* ((default (or bookmark-current-bookmark
745 (bookmark-buffer-name)))
746 (str
747 (or name
748 (read-from-minibuffer
749 (format "Set bookmark (%s): " default)
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))))
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)))))
772 (defun bookmark-kill-line (&optional newline-too)
773 "Kill from point to end of line.
774 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
775 Does 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))))
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)
790 (defun bookmark-send-annotation ()
791 "After remove lines beginning with '#', use the contents of this buffer
792 as the annotation for a bookmark, and store it in the bookmark list with
793 the 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)))
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"))
831 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
832 "A variable containing a function which returns the text to insert
833 into an annotation composition buffer. It takes the name of the bookmark,
834 as a string, as an arg.")
837 (defun bookmark-read-annotation-mode (buf point parg bookmark)
838 "Mode for composing annotations for a bookmark.
839 Wants BUF POINT PARG and BOOKMARK.
840 When you have finished composing, type \\[bookmark-send-annotation] to send
841 the annotation.
843 \\{bookmark-read-annotation-mode-map}
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))
863 (defun bookmark-read-annotation (parg bookmark)
864 "Pop up a buffer for entering a bookmark annotation. Text surrounding
865 the 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)))
872 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
873 "Keymap for editing an annotation of a bookmark.")
876 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
877 'bookmark-send-edited-annotation)
880 (defun bookmark-edit-annotation-mode (bookmark)
881 "Mode for editing the annotation of bookmark BOOKMARK.
882 When you have finished composing, type \\[bookmark-send-annotation].
884 \\{bookmark-edit-annotation-mode-map}
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))
900 (defun bookmark-send-edited-annotation ()
901 "After remove lines beginning with '#', use the contents of this buffer
902 as 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)))
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)))
927 (defun bookmark-insert-current-bookmark ()
928 "Insert this buffer's value of bookmark-current-bookmark, default
929 to file name if it's nil."
930 (interactive)
931 (let ((str
932 (save-excursion
933 (set-buffer bookmark-current-buffer)
934 bookmark-current-bookmark)))
935 (if str (insert str) (bookmark-insert-buffer-name))))
938 (defun bookmark-insert-buffer-name ()
939 "Insert the name (sans path) of the current file into the bookmark
940 name that is being set."
941 (interactive)
942 (let ((str
943 (save-excursion
944 (set-buffer bookmark-current-buffer)
945 (bookmark-buffer-name))))
946 (insert str)))
949 (defun bookmark-buffer-name ()
950 "Return the name of the current buffer's file, non-directory.
951 In 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.
970 (buffer-name (current-buffer)))))
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
978 (set-buffer bookmark-current-buffer)
979 (goto-char bookmark-yank-point)
980 (buffer-substring-no-properties
981 (point)
982 (save-excursion
983 (forward-word 1)
984 (setq bookmark-yank-point (point)))))))
985 (insert string)))
988 (defun bookmark-buffer-file-name ()
989 "Return the current buffer's file in a way useful for bookmarks.
990 For example, if this is a Info buffer, return the Info file's name."
991 (if (eq major-mode 'Info-mode)
992 Info-current-file
994 buffer-file-name
995 (if (and (boundp 'dired-directory) dired-directory)
996 (if (stringp dired-directory)
997 dired-directory
998 (car dired-directory))))))
1001 (defun bookmark-maybe-load-default-file ()
1002 (and (not bookmarks-already-loaded)
1003 (null bookmark-alist)
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...
1016 (file-readable-p (expand-file-name bookmark-default-file))
1017 (progn
1018 (bookmark-load bookmark-default-file t t)
1019 (setq bookmarks-already-loaded t))))
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))))))))
1032 ;;;###autoload
1033 (defun bookmark-jump (bookmark)
1034 "Jump to bookmark BOOKMARK (a point in some file).
1035 You 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
1037 bookmarks. See help on function `bookmark-load' for more about
1038 this.
1040 If the file pointed to by BOOKMARK no longer exists, you will be asked
1041 if you wish to give the bookmark a new location, and bookmark-jump
1042 will then jump to the new location, as well as recording it in place
1043 of the old one in the permanent bookmark record."
1044 (interactive
1045 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1046 (bookmark-maybe-historicize-string bookmark)
1047 (let ((cell (bookmark-jump-noselect bookmark)))
1048 (and cell
1049 (switch-to-buffer (car cell))
1050 (goto-char (cdr cell))
1051 (if bookmark-automatically-show-annotations
1052 ;; if there is an annotation for this bookmark,
1053 ;; show it in a buffer.
1054 (bookmark-show-annotation bookmark)))))
1057 (defun bookmark-jump-noselect (str)
1058 ;; a leetle helper for bookmark-jump :-)
1059 ;; returns (BUFFER . POINT)
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)
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
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.
1088 (progn
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))
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 \""
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)))))
1124 ;;;###autoload
1125 (defun bookmark-relocate (bookmark)
1126 "Relocate BOOKMARK -- prompts for a filename, and makes an already
1127 existing bookmark point to that file, instead of the one it used to
1128 point at. Useful when a file has been renamed after a bookmark was
1129 set in it."
1130 (interactive (bookmark-completing-read "Bookmark to relocate"))
1131 (bookmark-maybe-historicize-string bookmark)
1132 (bookmark-maybe-load-default-file)
1133 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1134 (newloc (expand-file-name
1135 (read-file-name
1136 (format "Relocate %s to: " bookmark)
1137 (file-name-directory bmrk-filename)))))
1138 (bookmark-set-filename bookmark newloc)))
1141 ;;;###autoload
1142 (defun bookmark-insert-location (bookmark &optional no-history)
1143 "Insert the name of the file associated with BOOKMARK.
1144 Optional second arg NO-HISTORY means don't record this in the
1145 minibuffer history list `bookmark-history'."
1146 (interactive (bookmark-completing-read "Insert bookmark location"))
1147 (or no-history (bookmark-maybe-historicize-string bookmark))
1148 (insert (bookmark-location bookmark)))
1151 (defun bookmark-location (bookmark)
1152 "Return the name of the file associated with BOOKMARK."
1153 (bookmark-maybe-load-default-file)
1154 (bookmark-get-filename bookmark))
1157 ;;;###autoload
1158 (defun bookmark-rename (old &optional new)
1159 "Change the name of OLD bookmark to NEW name. If called from
1160 keyboard, prompts for OLD and NEW. If called from menubar, OLD is
1161 selected from a menu, and prompts for NEW.
1163 If called from Lisp, prompts for NEW if only OLD was passed as an
1164 argument. If called with two strings, then no prompting is done. You
1165 must pass at least OLD when calling from Lisp.
1167 While you are entering the new name, consecutive C-w's insert
1168 consecutive words from the text of the buffer into the new bookmark
1169 name."
1170 (interactive (bookmark-completing-read "Old bookmark name"))
1171 (bookmark-maybe-historicize-string old)
1172 (bookmark-maybe-load-default-file)
1173 (progn
1174 (setq bookmark-current-point (point))
1175 (setq bookmark-yank-point (point))
1176 (setq bookmark-current-buffer (current-buffer))
1177 (let ((newname
1178 (or new ; use second arg, if non-nil
1179 (read-from-minibuffer
1180 "New name: "
1182 (let ((now-map (copy-keymap minibuffer-local-map)))
1183 (define-key now-map "\C-w" 'bookmark-yank-word)
1184 now-map)
1186 'bookmark-history))))
1187 (progn
1188 (bookmark-set-name old newname)
1189 (setq bookmark-current-bookmark newname)
1190 (bookmark-bmenu-surreptitiously-rebuild-list)
1191 (setq bookmark-alist-modification-count
1192 (1+ bookmark-alist-modification-count))
1193 (if (bookmark-time-to-save-p)
1194 (bookmark-save))))))
1197 ;;;###autoload
1198 (defun bookmark-insert (bookmark)
1199 "Insert the text of the file pointed to by bookmark BOOKMARK.
1200 You 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
1202 bookmarks. See help on function `bookmark-load' for more about
1203 this."
1204 (interactive (bookmark-completing-read "Insert bookmark contents"))
1205 (bookmark-maybe-historicize-string bookmark)
1206 (bookmark-maybe-load-default-file)
1207 (let ((orig-point (point))
1208 (str-to-insert
1209 (save-excursion
1210 (set-buffer (car (bookmark-jump-noselect bookmark)))
1211 (buffer-substring (point-min) (point-max)))))
1212 (insert str-to-insert)
1213 (push-mark)
1214 (goto-char orig-point)))
1217 ;;;###autoload
1218 (defun bookmark-delete (bookmark &optional batch)
1219 "Delete BOOKMARK from the bookmark list.
1220 Removes only the first instance of a bookmark with that name. If
1221 there are one or more other bookmarks with the same name, they will
1222 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1223 one most recently used in this file, if any\).
1224 Optional second arg BATCH means don't update the bookmark list buffer,
1225 probably because we were called from there."
1226 (interactive
1227 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1228 (bookmark-maybe-historicize-string bookmark)
1229 (bookmark-maybe-load-default-file)
1230 (let ((will-go (bookmark-get-bookmark bookmark)))
1231 (setq bookmark-alist (delq will-go bookmark-alist))
1232 ;; Added by db, nil bookmark-current-bookmark if the last
1233 ;; occurrence has been deleted
1234 (or (bookmark-get-bookmark bookmark-current-bookmark)
1235 (setq bookmark-current-bookmark nil)))
1236 ;; Don't rebuild the list
1237 (if batch
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))))
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))
1259 nil)))
1262 ;;;###autoload
1263 (defun bookmark-write ()
1264 "Write bookmarks to a file \(for which the user will be prompted
1265 interactively\). Don't use this in Lisp programs; use bookmark-save
1266 instead."
1267 (interactive)
1268 (bookmark-maybe-load-default-file)
1269 (bookmark-save t))
1272 ;;;###autoload
1273 (defun bookmark-save (&optional parg file)
1274 "Save currently defined bookmarks.
1275 Saves by default in the file defined by the variable
1276 `bookmark-default-file'. With a prefix arg, save it in file FILE
1277 \(second argument\).
1279 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1280 and FILE, and if you just want it to write to the default file, then
1281 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1282 instead. If you pass in one argument, and it is non-nil, then the
1283 user will be interactively queried for a file to save in.
1285 When you want to load in the bookmarks from a file, use
1286 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1287 for a file, defaulting to the file defined by variable
1288 `bookmark-default-file'."
1289 (interactive "P")
1290 (bookmark-maybe-load-default-file)
1291 (cond
1292 ((and (null parg) (null file))
1293 ;;whether interactive or not, write to default file
1294 (bookmark-write-file bookmark-default-file))
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))
1311 (defun bookmark-write-file (file)
1312 (save-excursion
1313 (save-window-excursion
1314 (if (>= baud-rate 9600)
1315 (message "Saving bookmarks to file %s..." file))
1316 (set-buffer (let ((enable-local-variables nil))
1317 (find-file-noselect file)))
1318 (goto-char (point-min))
1319 (delete-region (point-min) (point-max))
1320 (bookmark-insert-file-format-version-stamp)
1321 (pp bookmark-alist (current-buffer))
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)
1328 t))))
1329 (write-file file)
1330 (kill-buffer (current-buffer))
1331 (if (>= baud-rate 9600)
1332 (message "Saving bookmarks to file %s...done" file))
1333 ))))
1336 ;;;###autoload
1337 (defun bookmark-load (file &optional revert no-msg)
1338 "Load bookmarks from FILE (which must be in bookmark format).
1339 Appends loaded bookmarks to the front of the list of bookmarks. If
1340 optional second argument REVERT is non-nil, existing bookmarks are
1341 destroyed. Optional third arg NO-MSG means don't display any messages
1342 while loading.
1344 If you load a file that doesn't contain a proper bookmark alist, you
1345 will corrupt Emacs's bookmark list. Generally, you should only load
1346 in files that were created with the bookmark functions in the first
1347 place. Your own personal bookmark file, `~/.emacs.bmk', is
1348 maintained automatically by Emacs; you shouldn't need to load it
1349 explicitly."
1350 (interactive
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)))
1358 (setq file (expand-file-name file))
1359 (if (file-readable-p file)
1360 (save-excursion
1361 (save-window-excursion
1362 (if (and (null no-msg) (>= baud-rate 9600))
1363 (message "Loading bookmarks from %s..." file))
1364 (set-buffer (let ((enable-local-variables nil))
1365 (find-file-noselect file)))
1366 (goto-char (point-min))
1367 (bookmark-maybe-upgrade-file-format)
1368 (let ((blist (bookmark-alist-from-buffer)))
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)))
1377 (bookmark-bmenu-surreptitiously-rebuild-list))
1378 (error "Invalid bookmark list in %s" file)))
1379 (kill-buffer (current-buffer)))
1380 (if (and (null no-msg) (>= baud-rate 9600))
1381 (message "Loading bookmarks from %s...done" file)))
1382 (error "Cannot read bookmark file %s" file)))
1386 ;;; Code supporting the dired-like bookmark menu. Prefix is
1387 ;;; "bookmark-bmenu" for "buffer-menu":
1390 (defvar bookmark-bmenu-bookmark-column nil)
1393 (defvar bookmark-bmenu-hidden-bookmarks ())
1396 (defvar bookmark-bmenu-mode-map nil)
1399 (if bookmark-bmenu-mode-map
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)
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))
1433 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1434 ;; data.
1435 (put 'bookmark-bmenu-mode 'mode-class 'special)
1438 ;; todo: need to display whether or not bookmark exists as a buffer in
1439 ;; flag column.
1441 ;; Format:
1442 ;; FLAGS BOOKMARK [ LOCATION ]
1445 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1446 "Rebuild the Bookmark List if it exists.
1447 Don't affect the buffer ring order."
1448 (if (get-buffer "*Bookmark List*")
1449 (save-excursion
1450 (save-window-excursion
1451 (bookmark-bmenu-list)))))
1454 ;;;###autoload
1455 (defun bookmark-bmenu-list ()
1456 "Display a list of existing bookmarks.
1457 The list is displayed in a buffer named `*Bookmark List*'.
1458 The leftmost column displays a D if the bookmark is flagged for
1459 deletion, or > if it is flagged for displaying."
1460 (interactive)
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*")))
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)
1470 (mapcar
1471 (lambda (full-record)
1472 ;; if a bookmark has an annotation, prepend a "*"
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))
1482 (goto-char (point-min))
1483 (forward-line 2)
1484 (bookmark-bmenu-mode)
1485 (if bookmark-bmenu-toggle-filenames
1486 (bookmark-bmenu-toggle-filenames t)))
1488 ;;;###autoload
1489 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1490 ;;;###autoload
1491 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1495 (defun bookmark-bmenu-mode ()
1496 "Major mode for editing a list of bookmarks.
1497 Each line describes one of the bookmarks in Emacs.
1498 Letters do not insert themselves; instead, they are commands.
1499 Bookmark names preceded by a \"*\" have annotations.
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,
1508 together with bookmark selected before this one in another window.
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,
1511 so the bookmark menu bookmark remains visible in its window.
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.
1516 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1517 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1518 With a prefix arg, prompts for a file to save in.
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.
1521 With prefix argument, also move up one line.
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."
1527 (kill-all-local-variables)
1528 (use-local-map bookmark-bmenu-mode-map)
1529 (setq truncate-lines t)
1530 (setq buffer-read-only t)
1531 (setq major-mode 'bookmark-bmenu-mode)
1532 (setq mode-name "Bookmark Menu")
1533 (run-hooks 'bookmark-bmenu-mode-hook))
1536 (defun bookmark-bmenu-toggle-filenames (&optional show)
1537 "Toggle whether filenames are shown in the bookmark list.
1538 Optional argument SHOW means show them unconditionally."
1539 (interactive)
1540 (cond
1541 (show
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))
1549 (bookmark-bmenu-show-filenames)
1550 (setq bookmark-bmenu-toggle-filenames t))))
1553 (defun bookmark-bmenu-show-filenames (&optional force)
1554 (if (and (not force) bookmark-bmenu-toggle-filenames)
1555 nil ;already shown, so do nothing
1556 (save-excursion
1557 (save-window-excursion
1558 (goto-char (point-min))
1559 (forward-line 2)
1560 (setq bookmark-bmenu-hidden-bookmarks ())
1561 (let ((buffer-read-only nil))
1562 (while (< (point) (point-max))
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)
1567 (delete-region (point) (progn (end-of-line) (point)))
1568 (insert " ")
1569 ;; Pass the NO-HISTORY arg:
1570 (bookmark-insert-location bmrk t)
1571 (forward-line 1))))))))
1574 (defun bookmark-bmenu-hide-filenames (&optional force)
1575 (if (and (not force) bookmark-bmenu-toggle-filenames)
1576 ;; nothing to hide if above is nil
1577 (save-excursion
1578 (save-window-excursion
1579 (goto-char (point-min))
1580 (forward-line 2)
1581 (setq bookmark-bmenu-hidden-bookmarks
1582 (nreverse bookmark-bmenu-hidden-bookmarks))
1583 (save-excursion
1584 (goto-char (point-min))
1585 (search-forward "Bookmark")
1586 (backward-word 1)
1587 (setq bookmark-bmenu-bookmark-column (current-column)))
1588 (save-excursion
1589 (let ((buffer-read-only nil))
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))
1596 (forward-line 1))))))))
1599 ;; if you look at this next function from far away, it resembles a
1600 ;; gun. But only with this comment above...
1601 (defun bookmark-bmenu-check-position ()
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)
1610 ((and (bolp) (eobp))
1611 (beginning-of-line 0)
1614 t)))
1617 (defun bookmark-bmenu-bookmark ()
1618 ;; return a string which is bookmark of this line.
1619 (if (bookmark-bmenu-check-position)
1620 (save-excursion
1621 (save-window-excursion
1622 (goto-char (point-min))
1623 (search-forward "Bookmark")
1624 (backward-word 1)
1625 (setq bookmark-bmenu-bookmark-column (current-column)))))
1626 (if bookmark-bmenu-toggle-filenames
1627 (bookmark-bmenu-hide-filenames))
1628 (save-excursion
1629 (save-window-excursion
1630 (beginning-of-line)
1631 (forward-char bookmark-bmenu-bookmark-column)
1632 (prog1
1633 (buffer-substring (point)
1634 (progn
1635 (end-of-line)
1636 (point)))
1637 ;; well, this is certainly crystal-clear:
1638 (if bookmark-bmenu-toggle-filenames
1639 (bookmark-bmenu-toggle-filenames t))))))
1642 (defun bookmark-show-annotation (bookmark)
1643 "Display the annotation for bookmark named BOOKMARK in a buffer,
1644 if 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)))))))
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)))
1683 (defun bookmark-bmenu-mark ()
1684 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select] command."
1685 (interactive)
1686 (beginning-of-line)
1687 (if (bookmark-bmenu-check-position)
1688 (let ((buffer-read-only nil))
1689 (delete-char 1)
1690 (insert ?>)
1691 (forward-line 1))))
1694 (defun bookmark-bmenu-select ()
1695 "Select this line's bookmark; also display bookmarks marked with `>'.
1696 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1697 (interactive)
1698 (if (bookmark-bmenu-check-position)
1699 (let ((bmrk (bookmark-bmenu-bookmark))
1700 (menu (current-buffer))
1701 (others ())
1702 tem)
1703 (goto-char (point-min))
1704 (while (re-search-forward "^>" nil t)
1705 (setq tem (bookmark-bmenu-bookmark))
1706 (let ((buffer-read-only nil))
1707 (delete-char -1)
1708 (insert ?\ ))
1709 (or (string-equal tem bmrk)
1710 (member tem others)
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)
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)))
1723 (other-window 1)))))
1726 (defun bookmark-bmenu-save (parg)
1727 "Save the current list into a bookmark file.
1728 With 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))))
1735 (defun bookmark-bmenu-load ()
1736 "Load the bookmark file and rebuild the bookmark menu-buffer."
1737 (interactive)
1738 (if (bookmark-bmenu-check-position)
1739 (save-excursion
1740 (save-window-excursion
1741 ;; This will call `bookmark-bmenu-list'
1742 (call-interactively 'bookmark-load)))))
1745 (defun bookmark-bmenu-1-window ()
1746 "Select this line's bookmark, alone, in full frame."
1747 (interactive)
1748 (if (bookmark-bmenu-check-position)
1749 (progn
1750 (bookmark-jump (bookmark-bmenu-bookmark))
1751 (bury-buffer (other-buffer))
1752 (delete-other-windows))))
1755 (defun bookmark-bmenu-2-window ()
1756 "Select this line's bookmark, with previous buffer in second window."
1757 (interactive)
1758 (if (bookmark-bmenu-check-position)
1759 (let ((bmrk (bookmark-bmenu-bookmark))
1760 (menu (current-buffer))
1761 (pop-up-windows t))
1762 (delete-other-windows)
1763 (switch-to-buffer (other-buffer))
1764 (let* ((pair (bookmark-jump-noselect bmrk))
1765 (buff (car pair))
1766 (pos (cdr pair)))
1767 (pop-to-buffer buff)
1768 (goto-char pos))
1769 (bury-buffer menu))))
1772 (defun bookmark-bmenu-this-window ()
1773 "Select this line's bookmark in this window."
1774 (interactive)
1775 (if (bookmark-bmenu-check-position)
1776 (bookmark-jump (bookmark-bmenu-bookmark))))
1779 (defun bookmark-bmenu-other-window ()
1780 "Select this line's bookmark in other window, leaving bookmark menu visible."
1781 (interactive)
1782 (let ((bookmark (bookmark-bmenu-bookmark)))
1783 (if (bookmark-bmenu-check-position)
1784 (let* ((pair (bookmark-jump-noselect bookmark))
1785 (buff (car pair))
1786 (pos (cdr pair)))
1787 (switch-to-buffer-other-window buff)
1788 (goto-char pos)
1789 (set-window-point (get-buffer-window buff) pos)
1790 (bookmark-show-annotation bookmark)))))
1793 (defun bookmark-bmenu-switch-other-window ()
1794 "Make the other window select this line's bookmark.
1795 The 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))
1809 (bookmark-show-annotation bookmark)))))
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))))
1820 (defun bookmark-bmenu-show-all-annotations ()
1821 "Show the annotation for all bookmarks in another window."
1822 (interactive)
1823 (bookmark-show-all-annotations))
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))))
1834 (defun bookmark-bmenu-quit ()
1835 "Quit the bookmark menu."
1836 (interactive)
1837 (let ((buffer (current-buffer)))
1838 (switch-to-buffer (other-buffer))
1839 (bury-buffer buffer)))
1842 (defun bookmark-bmenu-unmark (&optional backup)
1843 "Cancel all requested operations on bookmark on this line and move down.
1844 Optional BACKUP means move up."
1845 (interactive "P")
1846 (beginning-of-line)
1847 (if (bookmark-bmenu-check-position)
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)))))
1858 (defun bookmark-bmenu-backup-unmark ()
1859 "Move up and cancel all requested operations on bookmark on line above."
1860 (interactive)
1861 (forward-line -1)
1862 (if (bookmark-bmenu-check-position)
1863 (progn
1864 (bookmark-bmenu-unmark)
1865 (forward-line -1))))
1868 (defun bookmark-bmenu-delete ()
1869 "Mark bookmark on this line to be deleted by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions] command."
1870 (interactive)
1871 (beginning-of-line)
1872 (if (bookmark-bmenu-check-position)
1873 (let ((buffer-read-only nil))
1874 (delete-char 1)
1875 (insert ?D)
1876 (forward-line 1))))
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
1881 and then move up one line"
1882 (interactive)
1883 (bookmark-bmenu-delete)
1884 (forward-line -2)
1885 (if (bookmark-bmenu-check-position)
1886 (forward-line 1)))
1889 (defun bookmark-bmenu-execute-deletions ()
1890 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1891 (interactive)
1892 (message "Deleting bookmarks...")
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")
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)
1905 (goto-char (point-min))
1906 (forward-line 1)
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)
1924 (bookmark-save))
1925 (message "Deleting bookmarks...done")
1929 (defun bookmark-bmenu-rename ()
1930 "Rename bookmark on current line. Prompts for a new name."
1931 (interactive)
1932 (if (bookmark-bmenu-check-position)
1933 (let ((bmrk (bookmark-bmenu-bookmark))
1934 (thispoint (point)))
1935 (bookmark-rename bmrk)
1936 (bookmark-bmenu-list)
1937 (goto-char thispoint))))
1940 (defun bookmark-bmenu-locate ()
1941 "Display location of this bookmark. Displays in the minibuffer."
1942 (interactive)
1943 (if (bookmark-bmenu-check-position)
1944 (let ((bmrk (bookmark-bmenu-bookmark)))
1945 (message (bookmark-location bmrk)))))
1949 ;;; Menu bar stuff. Prefix is "bookmark-menu".
1951 (defun bookmark-menu-build-paned-menu (name entries)
1952 "Build a multi-paned menu named NAME from the strings in ENTRIES.
1953 That is, ENTRIES is a list of strings which appear as the choices
1954 in the menu. The number of panes depends on the number of entries.
1955 The visible entries are truncated to `bookmark-menu-length', but the
1956 strings 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))))
1985 ;; Return the menu:
1986 (cons (concat "-*- " name " -*-") pane-list)))
1989 (defun bookmark-build-xemacs-menu (name entries function)
1990 "Build a menu named NAME from the strings in ENTRIES.
1991 That is, ENTRIES is a list of strings that appear as the choices
1992 in the menu.
1993 The visible entries are truncated to `bookmark-menu-length', but the
1994 strings 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)
2007 lst))
2008 (setq entries (cdr entries))))
2009 (nreverse lst))))
2011 ;; Return the menu:
2012 (append (if popup-menu-titles (list (concat "-*- " name " -*-")))
2013 pane-list)))
2016 (defun bookmark-menu-popup-paned-menu (event name entries)
2017 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2018 That is, ENTRIES is a list of strings which appear as the choices
2019 in the menu.
2020 The number of panes depends on the number of entries."
2021 (interactive "e")
2022 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2025 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2026 "Pop up menu of bookmarks, return chosen bookmark.
2027 Pop up at EVENT, menu's name is NAME.
2028 The number of panes depends on the number of bookmarks."
2029 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2032 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2033 ;; help function for making menus that need to apply a bookmark
2034 ;; function to a string.
2035 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2036 event menu-label)))
2037 (if choice (apply func-sym (list choice)))))
2040 ;;;###autoload
2041 (defun bookmark-menu-insert (event)
2042 "Insert the text of the file pointed to by bookmark BOOKMARK.
2043 You 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
2045 bookmarks. See help on function `bookmark-load' for more about
2046 this.
2048 Warning: this function only takes an EVENT as argument. Use the
2049 corresponding bookmark function from Lisp \(the one without the
2050 \"-menu-\" in its name\)."
2051 (interactive "e")
2052 (bookmark-popup-menu-and-apply-function
2053 'bookmark-insert "Insert Bookmark Contents" event))
2056 ;;;###autoload
2057 (defun bookmark-menu-jump (event)
2058 "Jump to bookmark BOOKMARK (a point in some file).
2059 You 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
2061 bookmarks. See help on function `bookmark-load' for more about
2062 this.
2064 Warning: this function only takes an EVENT as argument. Use the
2065 corresponding bookmark function from Lisp \(the one without the
2066 \"-menu-\" in its name\)."
2067 (interactive "e")
2068 (bookmark-popup-menu-and-apply-function
2069 'bookmark-jump "Jump to Bookmark" event))
2072 ;;;###autoload
2073 (defun bookmark-menu-locate (event)
2074 "Insert the name of the file associated with BOOKMARK.
2075 \(This is not the same as the contents of that file\).
2077 Warning: this function only takes an EVENT as argument. Use the
2078 corresponding bookmark function from Lisp \(the one without the
2079 \"-menu-\" in its name\)."
2080 (interactive "e")
2081 (bookmark-popup-menu-and-apply-function
2082 'bookmark-insert-location "Insert Bookmark Location" event))
2085 ;;;###autoload
2086 (defun bookmark-menu-rename (event)
2087 "Change the name of OLD-BOOKMARK to NEWNAME.
2088 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2089 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2090 prompts for NEWNAME.
2091 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2092 passed as an argument. If called with two strings, then no prompting
2093 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2095 While you are entering the new name, consecutive C-w's insert
2096 consecutive words from the text of the buffer into the new bookmark
2097 name.
2099 Warning: this function only takes an EVENT as argument. Use the
2100 corresponding bookmark function from Lisp \(the one without the
2101 \"-menu-\" in its name\)."
2102 (interactive "e")
2103 (bookmark-popup-menu-and-apply-function
2104 'bookmark-rename "Rename Bookmark" event))
2107 ;;;###autoload
2108 (defun bookmark-menu-delete (event)
2109 "Delete the bookmark named NAME from the bookmark list.
2110 Removes only the first instance of a bookmark with that name. If
2111 there are one or more other bookmarks with the same name, they will
2112 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2113 one most recently used in this file, if any\).
2115 Warning: this function only takes an EVENT as argument. Use the
2116 corresponding bookmark function from Lisp \(the one without the
2117 \"-menu-\" in its name\)."
2118 (interactive "e")
2119 (bookmark-popup-menu-and-apply-function
2120 'bookmark-delete "Delete Bookmark" event))
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.
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.
2129 ;; FSF Emacs menubar stuff
2130 ;; The odd conditional structure is due to the limitations of autoload
2131 ;; cookies.
2133 ;;;###autoload
2134 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2136 ;;;###autoload
2137 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
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)))
2146 ;;;###autoload
2147 (define-key menu-bar-bookmark-map [load]
2148 '("Load a Bookmark File..." . bookmark-load))
2150 ;;;###autoload
2151 (define-key menu-bar-bookmark-map [write]
2152 '("Save Bookmarks As..." . bookmark-write))
2154 ;;;###autoload
2155 (define-key menu-bar-bookmark-map [save]
2156 '("Save Bookmarks" . bookmark-save))
2158 ;;;###autoload
2159 (define-key menu-bar-bookmark-map [edit]
2160 '("Edit Bookmark List" . bookmark-bmenu-list))
2162 ;;;###autoload
2163 (define-key menu-bar-bookmark-map [delete]
2164 '("Delete Bookmark" . bookmark-menu-delete))
2166 ;;;###autoload
2167 (define-key menu-bar-bookmark-map [rename]
2168 '("Rename Bookmark" . bookmark-menu-rename))
2170 ;;;###autoload
2171 (define-key menu-bar-bookmark-map [locate]
2172 '("Insert Location" . bookmark-menu-locate))
2174 ;;;###autoload
2175 (define-key menu-bar-bookmark-map [insert]
2176 '("Insert Contents" . bookmark-menu-insert))
2178 ;;;###autoload
2179 (define-key menu-bar-bookmark-map [set]
2180 '("Set Bookmark" . bookmark-set))
2182 ;;;###autoload
2183 (define-key menu-bar-bookmark-map [jump]
2184 '("Jump to Bookmark" . bookmark-menu-jump))
2186 ;;;; end bookmark menu stuff ;;;;
2189 ;;; Load Hook
2190 (defvar bookmark-load-hook nil
2191 "Hook to run at the end of loading bookmark.")
2193 (run-hooks 'bookmark-load-hook)
2195 (provide 'bookmark)
2197 ;;; bookmark.el ends here