(set-visited-file-name, file-expand-wildcards): Fix docstring.
[emacs.git] / lisp / bookmark.el
blob3ed66f229a87e9ae03c621aee15adb4fb8e905b0
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2003 Free Software Foundation
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This package is for setting "bookmarks" in files. A bookmark
30 ;; associates a string with a location in a certain file. Thus, you
31 ;; can navigate your way to that location by providing the string.
32 ;; See the "User Variables" section for customizations.
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?
80 ;;; Code:
82 (require 'pp)
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom 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."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "*The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "*File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "*Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "*Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "*nil means don't show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "*Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "*Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "*Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'bookmark)
200 ;;; No user-serviceable parts beyond this point.
202 ;; Is it XEmacs?
203 (defconst bookmark-xemacsp
204 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
207 ;; Added for lucid emacs compatibility, db
208 (or (fboundp 'defalias) (fset 'defalias 'fset))
210 ;; suggested for lucid compatibility by david hughes:
211 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
213 ;; This variable is probably obsolete now...
214 (or (boundp 'baud-rate)
215 ;; some random value higher than 9600
216 (setq baud-rate 19200))
220 ;;; Keymap stuff:
222 ;; Set up these bindings dumping time *only*;
223 ;; if the user alters them, don't override the user when loading bookmark.el.
225 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
226 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
227 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
229 ;;;###autoload
230 (defvar bookmark-map nil
231 "Keymap containing bindings to bookmark functions.
232 It is not bound to any key by default: to bind it
233 so that you have a bookmark prefix, just use `global-set-key' and bind a
234 key of your choice to `bookmark-map'. All interactive bookmark
235 functions have a binding in this keymap.")
237 ;;;###autoload (define-prefix-command 'bookmark-map)
239 ;; Read the help on all of these functions for details...
240 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
241 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
242 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
243 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
244 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
245 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
246 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
247 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
248 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
249 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
250 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
251 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
254 ;;; The annotation maps.
255 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
256 "Keymap for composing an annotation for a bookmark.")
258 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
259 'bookmark-send-annotation)
263 ;;; Core variables and data structures:
264 (defvar bookmark-alist ()
265 "Association list of bookmarks and their records.
266 You probably don't want to change the value of this alist yourself;
267 instead, let the various bookmark functions do it for you.
269 The format of the alist is
271 \(BOOKMARK1 BOOKMARK2 ...\)
273 where each BOOKMARK is of the form
275 \(NAME
276 \(filename . FILE\)
277 \(front-context-string . FRONT-STR\)
278 \(rear-context-string . REAR-STR\)
279 \(position . POS\)
280 \(info-node . POS\)
281 \(annotation . ANNOTATION\)\)
283 So the cdr of each bookmark is an alist too.
284 `info-node' is optional, by the way.")
287 (defvar bookmarks-already-loaded nil)
290 ;; more stuff added by db.
292 (defvar bookmark-current-bookmark nil
293 "Name of bookmark most recently used in the current file.
294 It is buffer local, used to make moving a bookmark forward
295 through a file easier.")
297 (make-variable-buffer-local 'bookmark-current-bookmark)
300 (defvar bookmark-alist-modification-count 0
301 "Number of modifications to bookmark list since it was last saved.")
304 (defvar bookmark-search-size 16
305 "Length of the context strings recorded on either side of a bookmark.")
308 (defvar bookmark-current-point 0)
309 (defvar bookmark-yank-point 0)
310 (defvar bookmark-current-buffer nil)
314 ;; Helper functions.
316 ;; Only functions on this page and the next one (file formats) need to
317 ;; know anything about the format of bookmark-alist entries.
318 ;; Everyone else should go through them.
320 (defun bookmark-name-from-full-record (full-record)
321 "Return name of FULL-RECORD \(an alist element instead of a string\)."
322 (car full-record))
325 (defun bookmark-all-names ()
326 "Return a list of all current bookmark names."
327 (bookmark-maybe-load-default-file)
328 (mapcar
329 (lambda (full-record)
330 (bookmark-name-from-full-record full-record))
331 bookmark-alist))
334 (defun bookmark-get-bookmark (bookmark)
335 "Return the full entry for BOOKMARK in bookmark-alist.
336 If BOOKMARK is not a string, return nil."
337 (when (stringp bookmark)
338 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
341 (defun bookmark-get-bookmark-record (bookmark)
342 "Return the guts of the entry for BOOKMARK in bookmark-alist.
343 That is, all information but the name."
344 (car (cdr (bookmark-get-bookmark bookmark))))
347 (defun bookmark-set-name (bookmark newname)
348 "Set BOOKMARK's name to NEWNAME."
349 (setcar
350 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
351 newname))
354 (defun bookmark-get-annotation (bookmark)
355 "Return the annotation of BOOKMARK, or nil if none."
356 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
359 (defun bookmark-set-annotation (bookmark ann)
360 "Set the annotation of BOOKMARK to ANN."
361 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
362 (if cell
363 (setcdr cell ann)
364 (nconc (bookmark-get-bookmark-record bookmark)
365 (list (cons 'annotation ann))))))
368 (defun bookmark-get-filename (bookmark)
369 "Return the full filename of BOOKMARK."
370 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
373 (defun bookmark-set-filename (bookmark filename)
374 "Set the full filename of BOOKMARK to FILENAME."
375 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
376 (if cell
377 (setcdr cell filename)
378 (nconc (bookmark-get-bookmark-record bookmark)
379 (list (cons 'filename filename))))))
382 (defun bookmark-get-position (bookmark)
383 "Return the position \(i.e.: point\) of BOOKMARK."
384 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
387 (defun bookmark-set-position (bookmark position)
388 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
389 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
390 (if cell
391 (setcdr cell position)
392 (nconc (bookmark-get-bookmark-record bookmark)
393 (list (cons 'position position))))))
396 (defun bookmark-get-front-context-string (bookmark)
397 "Return the front-context-string of BOOKMARK."
398 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
401 (defun bookmark-set-front-context-string (bookmark string)
402 "Set the front-context-string of BOOKMARK to STRING."
403 (let ((cell (assq 'front-context-string
404 (bookmark-get-bookmark-record bookmark))))
405 (if cell
406 (setcdr cell string)
407 (nconc (bookmark-get-bookmark-record bookmark)
408 (list (cons 'front-context-string string))))))
411 (defun bookmark-get-rear-context-string (bookmark)
412 "Return the rear-context-string of BOOKMARK."
413 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
416 (defun bookmark-set-rear-context-string (bookmark string)
417 "Set the rear-context-string of BOOKMARK to STRING."
418 (let ((cell (assq 'rear-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 'rear-context-string string))))))
426 (defun bookmark-get-info-node (bookmark)
427 "Get the info node associated with BOOKMARK."
428 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
431 (defun bookmark-set-info-node (bookmark node)
432 "Set the Info node of BOOKMARK to NODE."
433 (let ((cell (assq 'info-node
434 (bookmark-get-bookmark-record bookmark))))
435 (if cell
436 (setcdr cell node)
437 (nconc (bookmark-get-bookmark-record bookmark)
438 (list (cons 'info-node node)))))
440 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
441 (sit-for 4)
445 (defvar bookmark-history nil
446 "The history list for bookmark functions.")
449 (defun bookmark-completing-read (prompt &optional default)
450 "Prompting with PROMPT, read a bookmark name in completion.
451 PROMPT will get a \": \" stuck on the end no matter what, so you
452 probably don't want to include one yourself.
453 Optional second arg DEFAULT is a string to return if the user enters
454 the empty string."
455 (bookmark-maybe-load-default-file) ; paranoia
456 (if (listp last-nonmenu-event)
457 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
458 (let* ((completion-ignore-case bookmark-completion-ignore-case)
459 (default default)
460 (prompt (if default
461 (concat prompt (format " (%s): " default))
462 (concat prompt ": ")))
463 (str
464 (completing-read prompt
465 bookmark-alist
469 'bookmark-history)))
470 (if (string-equal "" str) default str))))
473 (defmacro bookmark-maybe-historicize-string (string)
474 "Put STRING into the bookmark prompt history, if caller non-interactive.
475 We need this because sometimes bookmark functions are invoked from
476 menus, so `completing-read' never gets a chance to set `bookmark-history'."
477 `(or
478 (interactive-p)
479 (setq bookmark-history (cons ,string bookmark-history))))
482 (defun bookmark-make (name &optional annotation overwrite info-node)
483 "Make a bookmark named NAME.
484 Optional second arg ANNOTATION gives it an annotation.
485 Optional third arg OVERWRITE means replace any existing bookmarks with
486 this name.
487 Optional fourth arg INFO-NODE means this bookmark is at info node
488 INFO-NODE, so record this fact in the bookmark's entry."
489 (bookmark-maybe-load-default-file)
490 (let ((stripped-name (copy-sequence name)))
491 (or bookmark-xemacsp
492 ;; XEmacs's `set-text-properties' doesn't work on
493 ;; free-standing strings, apparently.
494 (set-text-properties 0 (length stripped-name) nil stripped-name))
495 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
496 ;; already existing bookmark under that name and
497 ;; no prefix arg means just overwrite old bookmark
498 (setcdr (bookmark-get-bookmark stripped-name)
499 (list (bookmark-make-cell annotation info-node)))
501 ;; otherwise just cons it onto the front (either the bookmark
502 ;; doesn't exist already, or there is no prefix arg. In either
503 ;; case, we want the new bookmark consed onto the alist...)
505 (setq bookmark-alist
506 (cons
507 (list stripped-name
508 (bookmark-make-cell annotation info-node))
509 bookmark-alist)))
511 ;; Added by db
512 (setq bookmark-current-bookmark stripped-name)
513 (setq bookmark-alist-modification-count
514 (1+ bookmark-alist-modification-count))
515 (if (bookmark-time-to-save-p)
516 (bookmark-save))))
519 (defun bookmark-make-cell (annotation &optional info-node)
520 "Return the record part of a new bookmark, given ANNOTATION.
521 Must be at the correct position in the buffer in which the bookmark is
522 being set. This might change someday.
523 Optional second arg INFO-NODE means this bookmark is at info node
524 INFO-NODE, so record this fact in the bookmark's entry."
525 (let ((the-record
526 `((filename . ,(bookmark-buffer-file-name))
527 (front-context-string
528 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
529 (buffer-substring-no-properties
530 (point)
531 (+ (point) bookmark-search-size))
532 nil))
533 (rear-context-string
534 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
535 (buffer-substring-no-properties
536 (point)
537 (- (point) bookmark-search-size))
538 nil))
539 (position . ,(point)))))
541 ;; Now fill in the optional parts:
543 ;; Take no chances with text properties
544 (set-text-properties 0 (length annotation) nil annotation)
545 (set-text-properties 0 (length info-node) nil info-node)
547 (if annotation
548 (nconc the-record (list (cons 'annotation annotation))))
549 (if info-node
550 (nconc the-record (list (cons 'info-node info-node))))
552 ;; Finally, return the completed record.
553 the-record))
557 ;;; File format stuff
559 ;; The OLD format of the bookmark-alist was:
561 ;; ((bookmark-name (filename
562 ;; string-in-front
563 ;; string-behind
564 ;; point))
565 ;; ...)
567 ;; The NEW format of the bookmark-alist is:
569 ;; ((bookmark-name ((filename . FILENAME)
570 ;; (front-context-string . string-in-front)
571 ;; (rear-context-string . string-behind)
572 ;; (position . POINT)
573 ;; (annotation . annotation)
574 ;; (whatever . VALUE)
575 ;; ...
576 ;; ))
577 ;; ...)
580 ;; I switched to using an internal as well as external alist because I
581 ;; felt that would be a more flexible framework in which to add
582 ;; features. It means that the order in which values appear doesn't
583 ;; matter, and it means that arbitrary values can be added without
584 ;; risk of interfering with existing ones.
586 ;; BOOKMARK-NAME is the string the user gives the bookmark and
587 ;; accesses it by from then on.
589 ;; FILENAME is the location of the file in which the bookmark is set.
591 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
592 ;; context in front of the point at which the bookmark is set.
594 ;; STRING-BEHIND is the same thing, but after the point.
596 ;; The context strings exist so that modifications to a file don't
597 ;; necessarily cause a bookmark's position to be invalidated.
598 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
599 ;; case the file has changed since the bookmark was set. It will
600 ;; attempt to place the user before the changes, if there were any.
601 ;; ANNOTATION is the annotation for the bookmark; it may not exist
602 ;; (for backward compatibility), be nil (no annotation), or be a
603 ;; string.
606 (defconst bookmark-file-format-version 1
607 "The current version of the format used by bookmark files.
608 You should never need to change this.")
611 (defconst bookmark-end-of-version-stamp-marker
612 "-*- End Of Bookmark File Format Version Stamp -*-\n"
613 "This string marks the end of the version stamp in a bookmark file.")
616 (defun bookmark-alist-from-buffer ()
617 "Return a bookmark-alist (in any format) from the current buffer.
618 The buffer must of course contain bookmark format information.
619 Does not care from where in the buffer it is called, and does not
620 affect point."
621 (save-excursion
622 (goto-char (point-min))
623 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
624 (read (current-buffer))
625 ;; Else we're dealing with format version 0
626 (if (search-forward "(" nil t)
627 (progn
628 (forward-char -1)
629 (read (current-buffer)))
630 ;; Else no hope of getting information here.
631 (error "Not bookmark format")))))
634 (defun bookmark-upgrade-version-0-alist (old-list)
635 "Upgrade a version 0 alist OLD-LIST to the current version."
636 (mapcar
637 (lambda (bookmark)
638 (let* ((name (car bookmark))
639 (record (car (cdr bookmark)))
640 (filename (nth 0 record))
641 (front-str (nth 1 record))
642 (rear-str (nth 2 record))
643 (position (nth 3 record))
644 (ann (nth 4 record)))
645 (list
646 name
647 `((filename . ,filename)
648 (front-context-string . ,(or front-str ""))
649 (rear-context-string . ,(or rear-str ""))
650 (position . ,position)
651 (annotation . ,ann)))))
652 old-list))
655 (defun bookmark-upgrade-file-format-from-0 ()
656 "Upgrade a bookmark file of format 0 (the original format) to format 1.
657 This expects to be called from point-min in a bookmark file."
658 (message "Upgrading bookmark format from 0 to %d..."
659 bookmark-file-format-version)
660 (let* ((old-list (bookmark-alist-from-buffer))
661 (new-list (bookmark-upgrade-version-0-alist old-list)))
662 (delete-region (point-min) (point-max))
663 (bookmark-insert-file-format-version-stamp)
664 (pp new-list (current-buffer))
665 (save-buffer))
666 (goto-char (point-min))
667 (message "Upgrading bookmark format from 0 to %d...done"
668 bookmark-file-format-version)
672 (defun bookmark-grok-file-format-version ()
673 "Return an integer which is the file-format version of this bookmark file.
674 This expects to be called from point-min in a bookmark file."
675 (if (looking-at "^;;;;")
676 (save-excursion
677 (save-match-data
678 (re-search-forward "[0-9]")
679 (forward-char -1)
680 (read (current-buffer))))
681 ;; Else this is format version 0, the original one, which didn't
682 ;; even have version stamps.
686 (defun bookmark-maybe-upgrade-file-format ()
687 "Check the file-format version of this bookmark file.
688 If the version is not up-to-date, upgrade it automatically.
689 This expects to be called from point-min in a bookmark file."
690 (let ((version (bookmark-grok-file-format-version)))
691 (cond
692 ((= version bookmark-file-format-version)
693 ) ; home free -- version is current
694 ((= version 0)
695 (bookmark-upgrade-file-format-from-0))
697 (error "Bookmark file format version strangeness")))))
700 (defun bookmark-insert-file-format-version-stamp ()
701 "Insert text indicating current version of bookmark file format."
702 (insert
703 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
704 bookmark-file-format-version))
705 (insert ";;; This format is meant to be slightly human-readable;\n"
706 ";;; nevertheless, you probably don't want to edit it.\n"
707 ";;; "
708 bookmark-end-of-version-stamp-marker))
711 ;;; end file-format stuff
714 ;;; Core code:
716 ;;;###autoload
717 (defun bookmark-set (&optional name parg)
718 "Set a bookmark named NAME inside a file.
719 If name is nil, then the user will be prompted.
720 With prefix arg, will not overwrite a bookmark that has the same name
721 as NAME if such a bookmark already exists, but instead will \"push\"
722 the new bookmark onto the bookmark alist. Thus the most recently set
723 bookmark with name NAME would be the one in effect at any given time,
724 but the others are still there, should you decide to delete the most
725 recent one.
727 To yank words from the text of the buffer and use them as part of the
728 bookmark name, type C-w while setting a bookmark. Successive C-w's
729 yank successive words.
731 Typing C-u inserts the name of the last bookmark used in the buffer
732 \(as an aid in using a single bookmark name to track your progress
733 through a large file\). If no bookmark was used, then C-u inserts the
734 name of the file being visited.
736 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
737 and it removes only the first instance of a bookmark with that name from
738 the list of bookmarks.\)"
739 (interactive (list nil current-prefix-arg))
741 (bookmark-buffer-file-name)
742 (error "Buffer not visiting a file or directory"))
744 (bookmark-maybe-load-default-file)
746 (setq bookmark-current-point (point))
747 (setq bookmark-yank-point (point))
748 (setq bookmark-current-buffer (current-buffer))
750 (let* ((default (or bookmark-current-bookmark
751 (bookmark-buffer-name)))
752 (str
753 (or name
754 (read-from-minibuffer
755 (format "Set bookmark (%s): " default)
757 (let ((now-map (copy-keymap minibuffer-local-map)))
758 (define-key now-map "\C-w" 'bookmark-yank-word)
759 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
760 now-map))))
761 (annotation nil))
762 (and (string-equal str "") (setq str default))
763 ;; Ask for an annotation buffer for this bookmark
764 (if bookmark-use-annotations
765 (bookmark-read-annotation parg str)
766 (bookmark-make str annotation parg (bookmark-info-current-node))
767 (setq bookmark-current-bookmark str)
768 (bookmark-bmenu-surreptitiously-rebuild-list)
769 (goto-char bookmark-current-point))))
772 (defun bookmark-info-current-node ()
773 "If in Info-mode, return current node name (a string), else nil."
774 (if (eq major-mode 'Info-mode)
775 Info-current-node))
778 (defun bookmark-kill-line (&optional newline-too)
779 "Kill from point to end of line.
780 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
781 Does not affect the kill-ring."
782 (let ((eol (save-excursion (end-of-line) (point))))
783 (delete-region (point) eol)
784 (if (and newline-too (looking-at "\n"))
785 (delete-char 1))))
788 ;; Defvars to avoid compilation warnings:
789 (defvar bookmark-annotation-paragraph nil)
790 (defvar bookmark-annotation-name nil)
791 (defvar bookmark-annotation-buffer nil)
792 (defvar bookmark-annotation-file nil)
793 (defvar bookmark-annotation-point nil)
796 (defun bookmark-send-annotation ()
797 "Use buffer contents as the annotation for a bookmark.
798 Exclude lines that begin with `#'.
799 Store the annotation text in the bookmark list with
800 the bookmark (and file, and point) specified in buffer local variables."
801 (interactive)
802 (if (not (eq major-mode 'bookmark-read-annotation-mode))
803 (error "Not in bookmark-read-annotation-mode"))
804 (goto-char (point-min))
805 (while (< (point) (point-max))
806 (if (looking-at "^#")
807 (bookmark-kill-line t)
808 (forward-line 1)))
809 (let ((annotation (buffer-string))
810 (parg bookmark-annotation-paragraph)
811 (bookmark bookmark-annotation-name)
812 (pt bookmark-annotation-point)
813 (buf bookmark-annotation-buffer))
814 ;; for bookmark-make-cell to work, we need to be
815 ;; in the relevant buffer, at the relevant point.
816 ;; Actually, bookmark-make-cell should probably be re-written,
817 ;; to avoid this need. Should I handle the error if a buffer is
818 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
819 (save-excursion
820 (pop-to-buffer buf)
821 (goto-char pt)
822 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
823 (setq bookmark-current-bookmark bookmark))
824 (bookmark-bmenu-surreptitiously-rebuild-list)
825 (goto-char bookmark-current-point))
826 (kill-buffer (current-buffer)))
829 (defun bookmark-default-annotation-text (bookmark)
830 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
831 "# All lines which start with a '#' will be deleted.\n"
832 "# Type C-c C-c when done.\n#\n"
833 "# Author: " (user-full-name) " <" (user-login-name) "@"
834 (system-name) ">\n"
835 "# Date: " (current-time-string) "\n"))
838 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
839 "Function to return default text to use for a bookmark annotation.
840 It takes the name of the bookmark, as a string, as an arg.")
842 (defun bookmark-read-annotation-mode (buf point parg bookmark)
843 "Mode for composing annotations for a bookmark.
844 Wants BUF POINT PARG and BOOKMARK.
845 When you have finished composing, type \\[bookmark-send-annotation] to send
846 the annotation.
848 \\{bookmark-read-annotation-mode-map}
850 (interactive)
851 (kill-all-local-variables)
852 (make-local-variable 'bookmark-annotation-paragraph)
853 (make-local-variable 'bookmark-annotation-name)
854 (make-local-variable 'bookmark-annotation-buffer)
855 (make-local-variable 'bookmark-annotation-file)
856 (make-local-variable 'bookmark-annotation-point)
857 (setq bookmark-annotation-paragraph parg)
858 (setq bookmark-annotation-name bookmark)
859 (setq bookmark-annotation-buffer buf)
860 (setq bookmark-annotation-file (buffer-file-name buf))
861 (setq bookmark-annotation-point point)
862 (use-local-map bookmark-read-annotation-mode-map)
863 (setq major-mode 'bookmark-read-annotation-mode)
864 (insert (funcall bookmark-read-annotation-text-func bookmark))
865 (run-hooks 'text-mode-hook))
868 (defun bookmark-read-annotation (parg bookmark)
869 "Pop up a buffer for entering a bookmark annotation.
870 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
871 (let ((buf (current-buffer))
872 (point (point)))
873 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
874 (bookmark-read-annotation-mode buf point parg bookmark)))
877 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
878 "Keymap for editing an annotation of a bookmark.")
881 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
882 'bookmark-send-edited-annotation)
885 (defun bookmark-edit-annotation-mode (bookmark)
886 "Mode for editing the annotation of bookmark BOOKMARK.
887 When you have finished composing, type \\[bookmark-send-annotation].
889 \\{bookmark-edit-annotation-mode-map}
891 (interactive)
892 (kill-all-local-variables)
893 (make-local-variable 'bookmark-annotation-name)
894 (setq bookmark-annotation-name bookmark)
895 (use-local-map bookmark-edit-annotation-mode-map)
896 (setq major-mode 'bookmark-edit-annotation-mode
897 mode-name "Edit Bookmark Annotation")
898 (insert (funcall bookmark-read-annotation-text-func bookmark))
899 (let ((annotation (bookmark-get-annotation bookmark)))
900 (if (and annotation (not (string-equal annotation "")))
901 (insert annotation)))
902 (run-hooks 'text-mode-hook))
905 (defun bookmark-send-edited-annotation ()
906 "Use buffer contents as annotation for a bookmark.
907 Lines beginning with `#' are ignored."
908 (interactive)
909 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
910 (error "Not in bookmark-edit-annotation-mode"))
911 (goto-char (point-min))
912 (while (< (point) (point-max))
913 (if (looking-at "^#")
914 (bookmark-kill-line t)
915 (forward-line 1)))
916 (let ((annotation (buffer-string))
917 (bookmark bookmark-annotation-name))
918 (bookmark-set-annotation bookmark annotation)
919 (bookmark-bmenu-surreptitiously-rebuild-list)
920 (goto-char bookmark-current-point))
921 (kill-buffer (current-buffer)))
924 (defun bookmark-edit-annotation (bookmark)
925 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
926 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
927 (bookmark-edit-annotation-mode bookmark))
930 (defun bookmark-insert-current-bookmark ()
931 "Insert this buffer's value of bookmark-current-bookmark.
932 Default to file name if it's nil."
933 (interactive)
934 (let ((str
935 (save-excursion
936 (set-buffer bookmark-current-buffer)
937 bookmark-current-bookmark)))
938 (if str (insert str) (bookmark-insert-buffer-name))))
941 (defun bookmark-insert-buffer-name ()
942 "Insert the current file name into the bookmark name being set.
943 The directory part of the file name is not used."
944 (interactive)
945 (let ((str
946 (save-excursion
947 (set-buffer bookmark-current-buffer)
948 (bookmark-buffer-name))))
949 (insert str)))
952 (defun bookmark-buffer-name ()
953 "Return the name of the current buffer's file, non-directory.
954 In Info, return the current node."
955 (cond
956 ;; Are we in Info?
957 ((string-equal mode-name "Info") Info-current-node)
958 ;; Or are we a file?
959 (buffer-file-name (file-name-nondirectory buffer-file-name))
960 ;; Or are we a directory?
961 ((and (boundp 'dired-directory) dired-directory)
962 (let* ((dirname (if (stringp dired-directory)
963 dired-directory
964 (car dired-directory)))
965 (idx (1- (length dirname))))
966 ;; Strip the trailing slash.
967 (if (= ?/ (aref dirname idx))
968 (file-name-nondirectory (substring dirname 0 idx))
969 ;; Else return the current-buffer
970 (buffer-name (current-buffer)))))
971 ;; If all else fails, use the buffer's name.
973 (buffer-name (current-buffer)))))
976 (defun bookmark-yank-word ()
977 (interactive)
978 ;; get the next word from the buffer and append it to the name of
979 ;; the bookmark currently being set.
980 (let ((string (save-excursion
981 (set-buffer bookmark-current-buffer)
982 (goto-char bookmark-yank-point)
983 (buffer-substring-no-properties
984 (point)
985 (progn
986 (forward-word 1)
987 (setq bookmark-yank-point (point)))))))
988 (insert string)))
991 (defun bookmark-buffer-file-name ()
992 "Return the current buffer's file in a way useful for bookmarks.
993 For example, if this is a Info buffer, return the Info file's name."
994 (if (eq major-mode 'Info-mode)
995 Info-current-file
997 buffer-file-name
998 (if (and (boundp 'dired-directory) dired-directory)
999 (if (stringp dired-directory)
1000 dired-directory
1001 (car dired-directory))))))
1004 (defun bookmark-maybe-load-default-file ()
1005 (and (not bookmarks-already-loaded)
1006 (null bookmark-alist)
1007 (prog2
1008 (and
1009 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1010 ;; to be renamed.
1011 (file-exists-p (expand-file-name bookmark-old-default-file))
1012 (not (file-exists-p (expand-file-name bookmark-default-file)))
1013 (rename-file (expand-file-name bookmark-old-default-file)
1014 (expand-file-name bookmark-default-file)))
1015 ;; return t so the `and' will continue...
1018 (file-readable-p (expand-file-name bookmark-default-file))
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 (list (bookmark-completing-read "Jump to bookmark"
1047 bookmark-current-bookmark)))
1048 (bookmark-maybe-historicize-string bookmark)
1049 (let ((cell (bookmark-jump-noselect bookmark)))
1050 (and cell
1051 (switch-to-buffer (car cell))
1052 (goto-char (cdr cell))
1053 (if bookmark-automatically-show-annotations
1054 ;; if there is an annotation for this bookmark,
1055 ;; show it in a buffer.
1056 (bookmark-show-annotation bookmark)))))
1059 (defun bookmark-file-or-variation-thereof (file)
1060 "Return FILE (a string) if it exists, or return a reasonable
1061 variation of FILE if that exists. Reasonable variations are checked
1062 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1063 nor a reasonable variation thereof, then still return FILE if it can
1064 be retrieved from a VC backend, else return nil."
1065 (if (file-exists-p file)
1066 file
1068 (progn (require 'info) ; ensure Info-suffix-list is bound
1069 (catch 'found
1070 (mapc (lambda (elt)
1071 (let ((suffixed-file (concat file (car elt))))
1072 (if (file-exists-p suffixed-file)
1073 (throw 'found suffixed-file))))
1074 Info-suffix-list)
1075 nil))
1076 ;; Last possibility: try VC
1077 (if (vc-backend file) file))))
1080 (defun bookmark-jump-noselect (str)
1081 ;; a leetle helper for bookmark-jump :-)
1082 ;; returns (BUFFER . POINT)
1083 (bookmark-maybe-load-default-file)
1084 (let* ((file (expand-file-name (bookmark-get-filename str)))
1085 (forward-str (bookmark-get-front-context-string str))
1086 (behind-str (bookmark-get-rear-context-string str))
1087 (place (bookmark-get-position str))
1088 (info-node (bookmark-get-info-node str))
1089 (orig-file file)
1091 (if (setq file (bookmark-file-or-variation-thereof file))
1092 (save-excursion
1093 (save-window-excursion
1094 (if info-node
1095 ;; Info nodes must be visited with care.
1096 (progn
1097 (require 'info)
1098 (Info-find-node file info-node))
1099 ;; Else no Info. Can do an ordinary find-file:
1100 (set-buffer (find-file-noselect file))
1101 (goto-char place))
1103 ;; Go searching forward first. Then, if forward-str exists and
1104 ;; was found in the file, we can search backward for behind-str.
1105 ;; Rationale is that if text was inserted between the two in the
1106 ;; file, it's better to be put before it so you can read it,
1107 ;; rather than after and remain perhaps unaware of the changes.
1108 (if forward-str
1109 (if (search-forward forward-str (point-max) t)
1110 (goto-char (match-beginning 0))))
1111 (if behind-str
1112 (if (search-backward behind-str (point-min) t)
1113 (goto-char (match-end 0))))
1114 ;; added by db
1115 (setq bookmark-current-bookmark str)
1116 (cons (current-buffer) (point))))
1118 ;; Else unable to find the marked file, so ask if user wants to
1119 ;; relocate the bookmark, else remind them to consider deletion.
1120 (ding)
1121 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1122 " nonexistent. Relocate \""
1124 "\"? "))
1125 (progn
1126 (bookmark-relocate str)
1127 ;; gasp! It's a recursive function call in Emacs Lisp!
1128 (bookmark-jump-noselect str))
1129 (message
1130 "Bookmark not relocated; consider removing it \(%s\)." str)
1131 nil))))
1134 ;;;###autoload
1135 (defun bookmark-relocate (bookmark)
1136 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1137 This makes an already existing bookmark point to that file, instead of
1138 the one it used to point at. Useful when a file has been renamed
1139 after a bookmark was set in it."
1140 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1141 (bookmark-maybe-historicize-string bookmark)
1142 (bookmark-maybe-load-default-file)
1143 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1144 (newloc (expand-file-name
1145 (read-file-name
1146 (format "Relocate %s to: " bookmark)
1147 (file-name-directory bmrk-filename)))))
1148 (bookmark-set-filename bookmark newloc)
1149 (bookmark-bmenu-surreptitiously-rebuild-list)))
1152 ;;;###autoload
1153 (defun bookmark-insert-location (bookmark &optional no-history)
1154 "Insert the name of the file associated with BOOKMARK.
1155 Optional second arg NO-HISTORY means don't record this in the
1156 minibuffer history list `bookmark-history'."
1157 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1158 (or no-history (bookmark-maybe-historicize-string bookmark))
1159 (let ((start (point)))
1160 (prog1
1161 (insert (bookmark-location bookmark)) ; *Return this line*
1162 (if (and (display-color-p) (display-mouse-p))
1163 (add-text-properties start
1164 (save-excursion (re-search-backward
1165 "[^ \t]")
1166 (1+ (point)))
1167 '(mouse-face highlight
1168 help-echo "mouse-2: go to this bookmark"))))))
1170 ;;;###autoload
1171 (defalias 'bookmark-locate 'bookmark-insert-location)
1173 (defun bookmark-location (bookmark)
1174 "Return the name of the file associated with BOOKMARK."
1175 (bookmark-maybe-load-default-file)
1176 (bookmark-get-filename bookmark))
1179 ;;;###autoload
1180 (defun bookmark-rename (old &optional new)
1181 "Change the name of OLD bookmark to NEW name.
1182 If called from keyboard, prompt for OLD and NEW. If called from
1183 menubar, select OLD from a menu and prompt for NEW.
1185 If called from Lisp, prompt for NEW if only OLD was passed as an
1186 argument. If called with two strings, then no prompting is done. You
1187 must pass at least OLD when calling from Lisp.
1189 While you are entering the new name, consecutive C-w's insert
1190 consecutive words from the text of the buffer into the new bookmark
1191 name."
1192 (interactive (list (bookmark-completing-read "Old bookmark name")))
1193 (bookmark-maybe-historicize-string old)
1194 (bookmark-maybe-load-default-file)
1196 (setq bookmark-current-point (point))
1197 (setq bookmark-yank-point (point))
1198 (setq bookmark-current-buffer (current-buffer))
1199 (let ((newname
1200 (or new ; use second arg, if non-nil
1201 (read-from-minibuffer
1202 "New name: "
1204 (let ((now-map (copy-keymap minibuffer-local-map)))
1205 (define-key now-map "\C-w" 'bookmark-yank-word)
1206 now-map)
1208 'bookmark-history))))
1209 (bookmark-set-name old newname)
1210 (setq bookmark-current-bookmark newname)
1211 (bookmark-bmenu-surreptitiously-rebuild-list)
1212 (setq bookmark-alist-modification-count
1213 (1+ bookmark-alist-modification-count))
1214 (if (bookmark-time-to-save-p)
1215 (bookmark-save))))
1218 ;;;###autoload
1219 (defun bookmark-insert (bookmark)
1220 "Insert the text of the file pointed to by bookmark BOOKMARK.
1221 You may have a problem using this function if the value of variable
1222 `bookmark-alist' is nil. If that happens, you need to load in some
1223 bookmarks. See help on function `bookmark-load' for more about
1224 this."
1225 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1226 (bookmark-maybe-historicize-string bookmark)
1227 (bookmark-maybe-load-default-file)
1228 (let ((orig-point (point))
1229 (str-to-insert
1230 (save-excursion
1231 (set-buffer (car (bookmark-jump-noselect bookmark)))
1232 (buffer-string))))
1233 (insert str-to-insert)
1234 (push-mark)
1235 (goto-char orig-point)))
1238 ;;;###autoload
1239 (defun bookmark-delete (bookmark &optional batch)
1240 "Delete BOOKMARK from the bookmark list.
1241 Removes only the first instance of a bookmark with that name. If
1242 there are one or more other bookmarks with the same name, they will
1243 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1244 one most recently used in this file, if any\).
1245 Optional second arg BATCH means don't update the bookmark list buffer,
1246 probably because we were called from there."
1247 (interactive
1248 (list (bookmark-completing-read "Delete bookmark"
1249 bookmark-current-bookmark)))
1250 (bookmark-maybe-historicize-string bookmark)
1251 (bookmark-maybe-load-default-file)
1252 (let ((will-go (bookmark-get-bookmark bookmark)))
1253 (setq bookmark-alist (delq will-go bookmark-alist))
1254 ;; Added by db, nil bookmark-current-bookmark if the last
1255 ;; occurrence has been deleted
1256 (or (bookmark-get-bookmark bookmark-current-bookmark)
1257 (setq bookmark-current-bookmark nil)))
1258 ;; Don't rebuild the list
1259 (if batch
1261 (bookmark-bmenu-surreptitiously-rebuild-list)
1262 (setq bookmark-alist-modification-count
1263 (1+ bookmark-alist-modification-count))
1264 (if (bookmark-time-to-save-p)
1265 (bookmark-save))))
1268 (defun bookmark-time-to-save-p (&optional last-time)
1269 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1270 ;; finds out whether it's time to save bookmarks to a file, by
1271 ;; examining the value of variable bookmark-save-flag, and maybe
1272 ;; bookmark-alist-modification-count. Returns t if they should be
1273 ;; saved, nil otherwise. if last-time is non-nil, then this is
1274 ;; being called when emacs is killed.
1275 (cond (last-time
1276 (and (> bookmark-alist-modification-count 0)
1277 bookmark-save-flag))
1278 ((numberp bookmark-save-flag)
1279 (>= bookmark-alist-modification-count bookmark-save-flag))
1281 nil)))
1284 ;;;###autoload
1285 (defun bookmark-write ()
1286 "Write bookmarks to a file (reading the file name with the minibuffer).
1287 Don't use this in Lisp programs; use `bookmark-save' instead."
1288 (interactive)
1289 (bookmark-maybe-load-default-file)
1290 (bookmark-save t))
1293 ;;;###autoload
1294 (defun bookmark-save (&optional parg file)
1295 "Save currently defined bookmarks.
1296 Saves by default in the file defined by the variable
1297 `bookmark-default-file'. With a prefix arg, save it in file FILE
1298 \(second argument\).
1300 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1301 and FILE, and if you just want it to write to the default file, then
1302 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1303 instead. If you pass in one argument, and it is non-nil, then the
1304 user will be interactively queried for a file to save in.
1306 When you want to load in the bookmarks from a file, use
1307 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1308 for a file, defaulting to the file defined by variable
1309 `bookmark-default-file'."
1310 (interactive "P")
1311 (bookmark-maybe-load-default-file)
1312 (cond
1313 ((and (null parg) (null file))
1314 ;;whether interactive or not, write to default file
1315 (bookmark-write-file bookmark-default-file))
1316 ((and (null parg) file)
1317 ;;whether interactive or not, write to given file
1318 (bookmark-write-file file))
1319 ((and parg (not file))
1320 ;;have been called interactively w/ prefix arg
1321 (let ((file (read-file-name "File to save bookmarks in: ")))
1322 (bookmark-write-file file)))
1323 (t ; someone called us with prefix-arg *and* a file, so just write to file
1324 (bookmark-write-file file)))
1325 ;; signal that we have synced the bookmark file by setting this to
1326 ;; 0. If there was an error at any point before, it will not get
1327 ;; set, which is what we want.
1328 (setq bookmark-alist-modification-count 0))
1332 (defun bookmark-write-file (file)
1333 (save-excursion
1334 (save-window-excursion
1335 (if (>= baud-rate 9600)
1336 (message "Saving bookmarks to file %s..." file))
1337 (set-buffer (let ((enable-local-variables nil))
1338 (find-file-noselect file)))
1339 (goto-char (point-min))
1340 (let ((print-length nil)
1341 (print-level nil))
1342 (delete-region (point-min) (point-max))
1343 (bookmark-insert-file-format-version-stamp)
1344 (pp bookmark-alist (current-buffer))
1345 (let ((version-control
1346 (cond
1347 ((null bookmark-version-control) nil)
1348 ((eq 'never bookmark-version-control) 'never)
1349 ((eq 'nospecial bookmark-version-control) version-control)
1351 t))))
1352 (write-file file)
1353 (kill-buffer (current-buffer))
1354 (if (>= baud-rate 9600)
1355 (message "Saving bookmarks to file %s...done" file)))))))
1358 (defun bookmark-import-new-list (new-list)
1359 ;; Walk over the new list, adding each individual bookmark
1360 ;; carefully. "Carefully" means checking against the existing
1361 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1362 ;; as necessary.
1363 (let ((lst new-list)
1364 (names (bookmark-all-names)))
1365 (while lst
1366 (let* ((full-record (car lst)))
1367 (bookmark-maybe-rename full-record names)
1368 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1369 (setq names (cons (bookmark-name-from-full-record full-record) names))
1370 (setq lst (cdr lst))))))
1373 (defun bookmark-maybe-rename (full-record names)
1374 ;; just a helper for bookmark-import-new-list; it is only for
1375 ;; readability that this is not inlined.
1377 ;; Once this has found a free name, it sets full-record to that
1378 ;; name.
1379 (let ((found-name (bookmark-name-from-full-record full-record)))
1380 (if (member found-name names)
1381 ;; We've got a conflict, so generate a new name
1382 (let ((count 2)
1383 (new-name found-name))
1384 (while (member new-name names)
1385 (setq new-name (concat found-name (format "<%d>" count)))
1386 (setq count (1+ count)))
1387 (bookmark-set-name full-record new-name)))))
1390 ;;;###autoload
1391 (defun bookmark-load (file &optional overwrite no-msg)
1392 "Load bookmarks from FILE (which must be in bookmark format).
1393 Appends loaded bookmarks to the front of the list of bookmarks. If
1394 optional second argument OVERWRITE is non-nil, existing bookmarks are
1395 destroyed. Optional third arg NO-MSG means don't display any messages
1396 while loading.
1398 If you load a file that doesn't contain a proper bookmark alist, you
1399 will corrupt Emacs's bookmark list. Generally, you should only load
1400 in files that were created with the bookmark functions in the first
1401 place. Your own personal bookmark file, `~/.emacs.bmk', is
1402 maintained automatically by Emacs; you shouldn't need to load it
1403 explicitly.
1405 If you load a file containing bookmarks with the same names as
1406 bookmarks already present in your Emacs, the new bookmarks will get
1407 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1408 method buffers use to resolve name collisions."
1409 (interactive
1410 (list (read-file-name
1411 (format "Load bookmarks from: (%s) "
1412 bookmark-default-file)
1413 ;;Default might not be used often,
1414 ;;but there's no better default, and
1415 ;;I guess it's better than none at all.
1416 "~/" bookmark-default-file 'confirm)))
1417 (setq file (expand-file-name file))
1418 (if (file-readable-p file)
1419 (save-excursion
1420 (save-window-excursion
1421 (if (and (null no-msg) (>= baud-rate 9600))
1422 (message "Loading bookmarks from %s..." file))
1423 (set-buffer (let ((enable-local-variables nil))
1424 (find-file-noselect file)))
1425 (goto-char (point-min))
1426 (bookmark-maybe-upgrade-file-format)
1427 (let ((blist (bookmark-alist-from-buffer)))
1428 (if (listp blist)
1429 (progn
1430 (if overwrite
1431 (progn
1432 (setq bookmark-alist blist)
1433 (setq bookmark-alist-modification-count 0))
1434 ;; else
1435 (bookmark-import-new-list blist)
1436 (setq bookmark-alist-modification-count
1437 (1+ bookmark-alist-modification-count)))
1438 (if (string-equal
1439 (expand-file-name bookmark-default-file)
1440 file)
1441 (setq bookmarks-already-loaded t))
1442 (bookmark-bmenu-surreptitiously-rebuild-list))
1443 (error "Invalid bookmark list in %s" file)))
1444 (kill-buffer (current-buffer)))
1445 (if (and (null no-msg) (>= baud-rate 9600))
1446 (message "Loading bookmarks from %s...done" file)))
1447 (error "Cannot read bookmark file %s" file)))
1451 ;;; Code supporting the dired-like bookmark menu. Prefix is
1452 ;;; "bookmark-bmenu" for "buffer-menu":
1455 (defvar bookmark-bmenu-bookmark-column nil)
1458 (defvar bookmark-bmenu-hidden-bookmarks ())
1461 (defvar bookmark-bmenu-mode-map nil)
1464 (if bookmark-bmenu-mode-map
1466 (setq bookmark-bmenu-mode-map (make-keymap))
1467 (suppress-keymap bookmark-bmenu-mode-map t)
1468 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1469 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1470 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1471 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1472 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1473 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1474 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1475 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1476 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1477 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1478 (define-key bookmark-bmenu-mode-map "\C-o"
1479 'bookmark-bmenu-switch-other-window)
1480 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1481 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1482 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1483 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1484 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1485 (define-key bookmark-bmenu-mode-map " " 'next-line)
1486 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1487 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1488 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1489 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1490 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1491 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1492 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1493 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1494 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1495 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1496 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1497 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1498 (define-key bookmark-bmenu-mode-map [mouse-2]
1499 'bookmark-bmenu-other-window-with-mouse))
1503 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1504 ;; data.
1505 (put 'bookmark-bmenu-mode 'mode-class 'special)
1508 ;; todo: need to display whether or not bookmark exists as a buffer in
1509 ;; flag column.
1511 ;; Format:
1512 ;; FLAGS BOOKMARK [ LOCATION ]
1515 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1516 "Rebuild the Bookmark List if it exists.
1517 Don't affect the buffer ring order."
1518 (if (get-buffer "*Bookmark List*")
1519 (save-excursion
1520 (save-window-excursion
1521 (bookmark-bmenu-list)))))
1524 ;;;###autoload
1525 (defun bookmark-bmenu-list ()
1526 "Display a list of existing bookmarks.
1527 The list is displayed in a buffer named `*Bookmark List*'.
1528 The leftmost column displays a D if the bookmark is flagged for
1529 deletion, or > if it is flagged for displaying."
1530 (interactive)
1531 (bookmark-maybe-load-default-file)
1532 (if (interactive-p)
1533 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1534 (set-buffer (get-buffer-create "*Bookmark List*")))
1535 (let ((inhibit-read-only t))
1536 (erase-buffer)
1537 (insert "% Bookmark\n- --------\n")
1538 (bookmark-maybe-sort-alist)
1539 (mapcar
1540 (lambda (full-record)
1541 ;; if a bookmark has an annotation, prepend a "*"
1542 ;; in the list of bookmarks.
1543 (let ((annotation (bookmark-get-annotation
1544 (bookmark-name-from-full-record full-record))))
1545 (if (and annotation (not (string-equal annotation "")))
1546 (insert " *")
1547 (insert " "))
1548 (let ((start (point)))
1549 (insert (bookmark-name-from-full-record full-record))
1550 (if (and (display-color-p) (display-mouse-p))
1551 (add-text-properties start
1552 (save-excursion (re-search-backward
1553 "[^ \t]")
1554 (1+ (point)))
1555 '(mouse-face highlight
1556 help-echo "mouse-2: go to this bookmark")))
1557 (insert "\n")
1559 bookmark-alist))
1560 (goto-char (point-min))
1561 (forward-line 2)
1562 (bookmark-bmenu-mode)
1563 (if bookmark-bmenu-toggle-filenames
1564 (bookmark-bmenu-toggle-filenames t)))
1566 ;;;###autoload
1567 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1568 ;;;###autoload
1569 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1573 (defun bookmark-bmenu-mode ()
1574 "Major mode for editing a list of bookmarks.
1575 Each line describes one of the bookmarks in Emacs.
1576 Letters do not insert themselves; instead, they are commands.
1577 Bookmark names preceded by a \"*\" have annotations.
1578 \\<bookmark-bmenu-mode-map>
1579 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1580 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1581 Also show bookmarks marked using m in other windows.
1582 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1583 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1584 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1585 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1586 together with bookmark selected before this one in another window.
1587 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1588 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1589 so the bookmark menu bookmark remains visible in its window.
1590 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1591 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1592 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1593 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1594 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1595 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1596 With a prefix arg, prompts for a file to save in.
1597 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1598 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1599 With prefix argument, also move up one line.
1600 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1601 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1602 in another buffer.
1603 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1604 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1605 (kill-all-local-variables)
1606 (use-local-map bookmark-bmenu-mode-map)
1607 (setq truncate-lines t)
1608 (setq buffer-read-only t)
1609 (setq major-mode 'bookmark-bmenu-mode)
1610 (setq mode-name "Bookmark Menu")
1611 (run-hooks 'bookmark-bmenu-mode-hook))
1614 (defun bookmark-bmenu-toggle-filenames (&optional show)
1615 "Toggle whether filenames are shown in the bookmark list.
1616 Optional argument SHOW means show them unconditionally."
1617 (interactive)
1618 (cond
1619 (show
1620 (setq bookmark-bmenu-toggle-filenames nil)
1621 (bookmark-bmenu-show-filenames)
1622 (setq bookmark-bmenu-toggle-filenames t))
1623 (bookmark-bmenu-toggle-filenames
1624 (bookmark-bmenu-hide-filenames)
1625 (setq bookmark-bmenu-toggle-filenames nil))
1627 (bookmark-bmenu-show-filenames)
1628 (setq bookmark-bmenu-toggle-filenames t))))
1631 (defun bookmark-bmenu-show-filenames (&optional force)
1632 (if (and (not force) bookmark-bmenu-toggle-filenames)
1633 nil ;already shown, so do nothing
1634 (save-excursion
1635 (save-window-excursion
1636 (goto-char (point-min))
1637 (forward-line 2)
1638 (setq bookmark-bmenu-hidden-bookmarks ())
1639 (let ((inhibit-read-only t))
1640 (while (< (point) (point-max))
1641 (let ((bmrk (bookmark-bmenu-bookmark)))
1642 (setq bookmark-bmenu-hidden-bookmarks
1643 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1644 (let ((start (save-excursion (end-of-line) (point))))
1645 (move-to-column bookmark-bmenu-file-column t)
1646 ;; Strip off `mouse-face' from the white spaces region.
1647 (if (and (display-color-p) (display-mouse-p))
1648 (remove-text-properties start (point)
1649 '(mouse-face nil help-echo nil))))
1650 (delete-region (point) (progn (end-of-line) (point)))
1651 (insert " ")
1652 ;; Pass the NO-HISTORY arg:
1653 (bookmark-insert-location bmrk t)
1654 (forward-line 1))))))))
1657 (defun bookmark-bmenu-hide-filenames (&optional force)
1658 (if (and (not force) bookmark-bmenu-toggle-filenames)
1659 ;; nothing to hide if above is nil
1660 (save-excursion
1661 (save-window-excursion
1662 (goto-char (point-min))
1663 (forward-line 2)
1664 (setq bookmark-bmenu-hidden-bookmarks
1665 (nreverse bookmark-bmenu-hidden-bookmarks))
1666 (save-excursion
1667 (goto-char (point-min))
1668 (search-forward "Bookmark")
1669 (backward-word 1)
1670 (setq bookmark-bmenu-bookmark-column (current-column)))
1671 (save-excursion
1672 (let ((inhibit-read-only t))
1673 (while bookmark-bmenu-hidden-bookmarks
1674 (move-to-column bookmark-bmenu-bookmark-column t)
1675 (bookmark-kill-line)
1676 (let ((start (point)))
1677 (insert (car bookmark-bmenu-hidden-bookmarks))
1678 (if (and (display-color-p) (display-mouse-p))
1679 (add-text-properties start
1680 (save-excursion (re-search-backward
1681 "[^ \t]")
1682 (1+ (point)))
1683 '(mouse-face highlight
1684 help-echo
1685 "mouse-2: go to this bookmark"))))
1686 (setq bookmark-bmenu-hidden-bookmarks
1687 (cdr bookmark-bmenu-hidden-bookmarks))
1688 (forward-line 1))))))))
1691 (defun bookmark-bmenu-check-position ()
1692 ;; Returns non-nil if on a line with a bookmark.
1693 ;; (The actual value returned is bookmark-alist).
1694 ;; Else reposition and try again, else return nil.
1695 (cond ((< (count-lines (point-min) (point)) 2)
1696 (goto-char (point-min))
1697 (forward-line 2)
1698 bookmark-alist)
1699 ((and (bolp) (eobp))
1700 (beginning-of-line 0)
1701 bookmark-alist)
1703 bookmark-alist)))
1706 (defun bookmark-bmenu-bookmark ()
1707 ;; return a string which is bookmark of this line.
1708 (if (bookmark-bmenu-check-position)
1709 (save-excursion
1710 (save-window-excursion
1711 (goto-char (point-min))
1712 (search-forward "Bookmark")
1713 (backward-word 1)
1714 (setq bookmark-bmenu-bookmark-column (current-column)))))
1715 (if bookmark-bmenu-toggle-filenames
1716 (bookmark-bmenu-hide-filenames))
1717 (save-excursion
1718 (save-window-excursion
1719 (beginning-of-line)
1720 (forward-char bookmark-bmenu-bookmark-column)
1721 (prog1
1722 (buffer-substring-no-properties (point)
1723 (progn
1724 (end-of-line)
1725 (point)))
1726 ;; well, this is certainly crystal-clear:
1727 (if bookmark-bmenu-toggle-filenames
1728 (bookmark-bmenu-toggle-filenames t))))))
1731 (defun bookmark-show-annotation (bookmark)
1732 "Display the annotation for bookmark named BOOKMARK in a buffer,
1733 if an annotation exists."
1734 (let ((annotation (bookmark-get-annotation bookmark)))
1735 (if (and annotation (not (string-equal annotation "")))
1736 (save-excursion
1737 (let ((old-buf (current-buffer)))
1738 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1739 (delete-region (point-min) (point-max))
1740 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1741 (insert annotation)
1742 (goto-char (point-min))
1743 (pop-to-buffer old-buf))))))
1746 (defun bookmark-show-all-annotations ()
1747 "Display the annotations for all bookmarks in a buffer."
1748 (let ((old-buf (current-buffer)))
1749 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1750 (delete-region (point-min) (point-max))
1751 (mapcar
1752 (lambda (full-record)
1753 (let* ((name (bookmark-name-from-full-record full-record))
1754 (ann (bookmark-get-annotation name)))
1755 (insert (concat name ":\n"))
1756 (if (and ann (not (string-equal ann "")))
1757 ;; insert the annotation, indented by 4 spaces.
1758 (progn
1759 (save-excursion (insert ann))
1760 (while (< (point) (point-max))
1761 (beginning-of-line) ; paranoia
1762 (insert " ")
1763 (forward-line)
1764 (end-of-line))))))
1765 bookmark-alist)
1766 (goto-char (point-min))
1767 (pop-to-buffer old-buf)))
1770 (defun bookmark-bmenu-mark ()
1771 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1772 (interactive)
1773 (beginning-of-line)
1774 (if (bookmark-bmenu-check-position)
1775 (let ((inhibit-read-only t))
1776 (delete-char 1)
1777 (insert ?>)
1778 (forward-line 1)
1779 (bookmark-bmenu-check-position))))
1782 (defun bookmark-bmenu-select ()
1783 "Select this line's bookmark; also display bookmarks marked with `>'.
1784 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1785 (interactive)
1786 (if (bookmark-bmenu-check-position)
1787 (let ((bmrk (bookmark-bmenu-bookmark))
1788 (menu (current-buffer))
1789 (others ())
1790 tem)
1791 (goto-char (point-min))
1792 (while (re-search-forward "^>" nil t)
1793 (setq tem (bookmark-bmenu-bookmark))
1794 (let ((inhibit-read-only t))
1795 (delete-char -1)
1796 (insert ?\ ))
1797 (or (string-equal tem bmrk)
1798 (member tem others)
1799 (setq others (cons tem others))))
1800 (setq others (nreverse others)
1801 tem (/ (1- (frame-height)) (1+ (length others))))
1802 (delete-other-windows)
1803 (bookmark-jump bmrk)
1804 (bury-buffer menu)
1805 (if others
1806 (while others
1807 (split-window nil tem)
1808 (other-window 1)
1809 (bookmark-jump (car others))
1810 (setq others (cdr others)))
1811 (other-window 1)))))
1814 (defun bookmark-bmenu-save (parg)
1815 "Save the current list into a bookmark file.
1816 With a prefix arg, prompts for a file to save them in."
1817 (interactive "P")
1818 (save-excursion
1819 (save-window-excursion
1820 (bookmark-save parg))))
1823 (defun bookmark-bmenu-load ()
1824 "Load the bookmark file and rebuild the bookmark menu-buffer."
1825 (interactive)
1826 (if (bookmark-bmenu-check-position)
1827 (save-excursion
1828 (save-window-excursion
1829 ;; This will call `bookmark-bmenu-list'
1830 (call-interactively 'bookmark-load)))))
1833 (defun bookmark-bmenu-1-window ()
1834 "Select this line's bookmark, alone, in full frame."
1835 (interactive)
1836 (if (bookmark-bmenu-check-position)
1837 (progn
1838 (bookmark-jump (bookmark-bmenu-bookmark))
1839 (bury-buffer (other-buffer))
1840 (delete-other-windows))))
1843 (defun bookmark-bmenu-2-window ()
1844 "Select this line's bookmark, with previous buffer in second window."
1845 (interactive)
1846 (if (bookmark-bmenu-check-position)
1847 (let ((bmrk (bookmark-bmenu-bookmark))
1848 (menu (current-buffer))
1849 (pop-up-windows t))
1850 (delete-other-windows)
1851 (switch-to-buffer (other-buffer))
1852 (let* ((pair (bookmark-jump-noselect bmrk))
1853 (buff (car pair))
1854 (pos (cdr pair)))
1855 (pop-to-buffer buff)
1856 (goto-char pos))
1857 (bury-buffer menu))))
1860 (defun bookmark-bmenu-this-window ()
1861 "Select this line's bookmark in this window."
1862 (interactive)
1863 (if (bookmark-bmenu-check-position)
1864 (bookmark-jump (bookmark-bmenu-bookmark))))
1867 (defun bookmark-bmenu-other-window ()
1868 "Select this line's bookmark in other window, leaving bookmark menu visible."
1869 (interactive)
1870 (let ((bookmark (bookmark-bmenu-bookmark)))
1871 (if (bookmark-bmenu-check-position)
1872 (let* ((pair (bookmark-jump-noselect bookmark))
1873 (buff (car pair))
1874 (pos (cdr pair)))
1875 (switch-to-buffer-other-window buff)
1876 (goto-char pos)
1877 (set-window-point (get-buffer-window buff) pos)
1878 (bookmark-show-annotation bookmark)))))
1881 (defun bookmark-bmenu-switch-other-window ()
1882 "Make the other window select this line's bookmark.
1883 The current window remains selected."
1884 (interactive)
1885 (let ((bookmark (bookmark-bmenu-bookmark))
1886 (pop-up-windows t)
1887 same-window-buffer-names
1888 same-window-regexps)
1889 (if (bookmark-bmenu-check-position)
1890 (let* ((pair (bookmark-jump-noselect bookmark))
1891 (buff (car pair))
1892 (pos (cdr pair)))
1893 (display-buffer buff)
1894 (let ((o-buffer (current-buffer)))
1895 ;; save-excursion won't do
1896 (set-buffer buff)
1897 (goto-char pos)
1898 (set-window-point (get-buffer-window buff) pos)
1899 (set-buffer o-buffer))
1900 (bookmark-show-annotation bookmark)))))
1902 (defun bookmark-bmenu-other-window-with-mouse (event)
1903 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1904 (interactive "e")
1905 (save-excursion
1906 (set-buffer (window-buffer (posn-window (event-end event))))
1907 (save-excursion
1908 (goto-char (posn-point (event-end event)))
1909 (bookmark-bmenu-other-window))))
1912 (defun bookmark-bmenu-show-annotation ()
1913 "Show the annotation for the current bookmark in another window."
1914 (interactive)
1915 (let ((bookmark (bookmark-bmenu-bookmark)))
1916 (if (bookmark-bmenu-check-position)
1917 (bookmark-show-annotation bookmark))))
1920 (defun bookmark-bmenu-show-all-annotations ()
1921 "Show the annotation for all bookmarks in another window."
1922 (interactive)
1923 (bookmark-show-all-annotations))
1926 (defun bookmark-bmenu-edit-annotation ()
1927 "Edit the annotation for the current bookmark in another window."
1928 (interactive)
1929 (let ((bookmark (bookmark-bmenu-bookmark)))
1930 (if (bookmark-bmenu-check-position)
1931 (bookmark-edit-annotation bookmark))))
1934 (defun bookmark-bmenu-unmark (&optional backup)
1935 "Cancel all requested operations on bookmark on this line and move down.
1936 Optional BACKUP means move up."
1937 (interactive "P")
1938 (beginning-of-line)
1939 (if (bookmark-bmenu-check-position)
1940 (progn
1941 (let ((inhibit-read-only t))
1942 (delete-char 1)
1943 ;; any flags to reset according to circumstances? How about a
1944 ;; flag indicating whether this bookmark is being visited?
1945 ;; well, we don't have this now, so maybe later.
1946 (insert " "))
1947 (forward-line (if backup -1 1))
1948 (bookmark-bmenu-check-position))))
1951 (defun bookmark-bmenu-backup-unmark ()
1952 "Move up and cancel all requested operations on bookmark on line above."
1953 (interactive)
1954 (forward-line -1)
1955 (if (bookmark-bmenu-check-position)
1956 (progn
1957 (bookmark-bmenu-unmark)
1958 (forward-line -1)
1959 (bookmark-bmenu-check-position))))
1962 (defun bookmark-bmenu-delete ()
1963 "Mark bookmark on this line to be deleted.
1964 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1965 (interactive)
1966 (beginning-of-line)
1967 (if (bookmark-bmenu-check-position)
1968 (let ((inhibit-read-only t))
1969 (delete-char 1)
1970 (insert ?D)
1971 (forward-line 1)
1972 (bookmark-bmenu-check-position))))
1975 (defun bookmark-bmenu-delete-backwards ()
1976 "Mark bookmark on this line to be deleted, then move up one line.
1977 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1978 (interactive)
1979 (bookmark-bmenu-delete)
1980 (forward-line -2)
1981 (if (bookmark-bmenu-check-position)
1982 (forward-line 1))
1983 (bookmark-bmenu-check-position))
1986 (defun bookmark-bmenu-execute-deletions ()
1987 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1988 (interactive)
1989 (message "Deleting bookmarks...")
1990 (let ((hide-em bookmark-bmenu-toggle-filenames)
1991 (o-point (point))
1992 (o-str (save-excursion
1993 (beginning-of-line)
1994 (if (looking-at "^D")
1996 (buffer-substring
1997 (point)
1998 (progn (end-of-line) (point))))))
1999 (o-col (current-column)))
2000 (if hide-em (bookmark-bmenu-hide-filenames))
2001 (setq bookmark-bmenu-toggle-filenames nil)
2002 (goto-char (point-min))
2003 (forward-line 1)
2004 (while (re-search-forward "^D" (point-max) t)
2005 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2006 (bookmark-bmenu-list)
2007 (setq bookmark-bmenu-toggle-filenames hide-em)
2008 (if bookmark-bmenu-toggle-filenames
2009 (bookmark-bmenu-toggle-filenames t))
2010 (if o-str
2011 (progn
2012 (goto-char (point-min))
2013 (search-forward o-str)
2014 (beginning-of-line)
2015 (forward-char o-col))
2016 (goto-char o-point))
2017 (beginning-of-line)
2018 (setq bookmark-alist-modification-count
2019 (1+ bookmark-alist-modification-count))
2020 (if (bookmark-time-to-save-p)
2021 (bookmark-save))
2022 (message "Deleting bookmarks...done")
2026 (defun bookmark-bmenu-rename ()
2027 "Rename bookmark on current line. Prompts for a new name."
2028 (interactive)
2029 (if (bookmark-bmenu-check-position)
2030 (let ((bmrk (bookmark-bmenu-bookmark))
2031 (thispoint (point)))
2032 (bookmark-rename bmrk)
2033 (bookmark-bmenu-list)
2034 (goto-char thispoint))))
2037 (defun bookmark-bmenu-locate ()
2038 "Display location of this bookmark. Displays in the minibuffer."
2039 (interactive)
2040 (if (bookmark-bmenu-check-position)
2041 (let ((bmrk (bookmark-bmenu-bookmark)))
2042 (message (bookmark-location bmrk)))))
2046 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2048 (defun bookmark-menu-popup-paned-menu (event name entries)
2049 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2050 That is, ENTRIES is a list of strings which appear as the choices
2051 in the menu.
2052 The number of panes depends on the number of entries.
2053 The visible entries are truncated to `bookmark-menu-length', but the
2054 strings returned are not."
2055 (let ((f-height (/ (frame-height) 2))
2056 (pane-list nil)
2057 (iter 0))
2058 (while entries
2059 (let (lst
2060 (count 0))
2061 (while (and (< count f-height) entries)
2062 (let ((str (car entries)))
2063 (push (cons
2064 (if (> (length str) bookmark-menu-length)
2065 (substring str 0 bookmark-menu-length)
2066 str)
2067 str)
2068 lst)
2069 (setq entries (cdr entries))
2070 (setq count (1+ count))))
2071 (setq iter (1+ iter))
2072 (push (cons
2073 (format "-*- %s (%d) -*-" name iter)
2074 (nreverse lst))
2075 pane-list)))
2077 ;; Popup the menu and return the string.
2078 (x-popup-menu event (cons (concat "-*- " name " -*-")
2079 (nreverse pane-list)))))
2082 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2083 ;; following works, and for explaining what to do to make it work.
2085 ;; We MUST autoload EACH form used to set up this variable's value, so
2086 ;; that the whole job is done in loaddefs.el.
2088 ;; Emacs menubar stuff.
2090 ;;;###autoload
2091 (defvar menu-bar-bookmark-map
2092 (let ((map (make-sparse-keymap "Bookmark functions")))
2093 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2094 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2095 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2096 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2097 (define-key map [delete] '("Delete Bookmark" . bookmark-delete))
2098 (define-key map [rename] '("Rename Bookmark" . bookmark-rename))
2099 (define-key map [locate] '("Insert Location" . bookmark-locate))
2100 (define-key map [insert] '("Insert Contents" . bookmark-insert))
2101 (define-key map [set] '("Set Bookmark" . bookmark-set))
2102 (define-key map [jump] '("Jump to Bookmark" . bookmark-jump))
2103 map))
2105 ;;;###autoload
2106 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2108 ;; make bookmarks appear toward the right side of the menu.
2109 (if (boundp 'menu-bar-final-items)
2110 (if menu-bar-final-items
2111 (setq menu-bar-final-items
2112 (cons 'bookmark menu-bar-final-items)))
2113 (setq menu-bar-final-items '(bookmark)))
2115 ;;;; end bookmark menu stuff ;;;;
2118 ;;; Load Hook
2119 (defvar bookmark-load-hook nil
2120 "Hook run at the end of loading bookmark.")
2122 ;;; Exit Hook, called from kill-emacs-hook
2123 (defvar bookmark-exit-hook nil
2124 "Hook run when emacs exits.")
2126 (defun bookmark-exit-hook-internal ()
2127 "Save bookmark state, if necessary, at Emacs exit time.
2128 This also runs `bookmark-exit-hooks'."
2129 (run-hooks 'bookmark-exit-hooks)
2130 (and bookmark-alist
2131 (bookmark-time-to-save-p t)
2132 (bookmark-save)))
2134 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2137 (run-hooks 'bookmark-load-hook)
2139 (provide 'bookmark)
2141 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2142 ;;; bookmark.el ends here