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