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