(terminal-init-xterm): Add more key bindings.
[emacs.git] / lisp / bookmark.el
blob104a9c6512fa4d6871940772c09d3c68890a260f
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, 2006 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 (defface bookmark-menu-heading
202 '((t (:inherit font-lock-type-face)))
203 "Face used to highlight the heading in bookmark menu buffers."
204 :group 'bookmark
205 :version "22.1")
208 ;;; No user-serviceable parts beyond this point.
210 ;; Is it XEmacs?
211 (defconst bookmark-xemacsp
212 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
215 ;; Added for lucid emacs compatibility, db
216 (or (fboundp 'defalias) (fset 'defalias 'fset))
218 ;; suggested for lucid compatibility by david hughes:
219 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
222 ;;; Keymap stuff:
224 ;; Set up these bindings dumping time *only*;
225 ;; if the user alters them, don't override the user when loading bookmark.el.
227 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
228 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
229 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
231 ;;;###autoload
232 (defvar bookmark-map nil
233 "Keymap containing bindings to bookmark functions.
234 It is not bound to any key by default: to bind it
235 so that you have a bookmark prefix, just use `global-set-key' and bind a
236 key of your choice to `bookmark-map'. All interactive bookmark
237 functions have a binding in this keymap.")
239 ;;;###autoload (define-prefix-command 'bookmark-map)
241 ;; Read the help on all of these functions for details...
242 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
244 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
245 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
246 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
247 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
248 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
249 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
250 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
251 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
252 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
253 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
256 ;;; The annotation maps.
257 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
258 "Keymap for composing an annotation for a bookmark.")
260 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
261 'bookmark-send-annotation)
265 ;;; Core variables and data structures:
266 (defvar bookmark-alist ()
267 "Association list of bookmarks and their records.
268 You probably don't want to change the value of this alist yourself;
269 instead, let the various bookmark functions do it for you.
271 The format of the alist is
273 \(BOOKMARK1 BOOKMARK2 ...\)
275 where each BOOKMARK is of the form
277 \(NAME
278 \(filename . FILE\)
279 \(front-context-string . FRONT-STR\)
280 \(rear-context-string . REAR-STR\)
281 \(position . POS\)
282 \(info-node . POS\)
283 \(annotation . ANNOTATION\)\)
285 So the cdr of each bookmark is an alist too.
286 `info-node' is optional, by the way.")
289 (defvar bookmarks-already-loaded nil)
292 ;; more stuff added by db.
294 (defvar bookmark-current-bookmark nil
295 "Name of bookmark most recently used in the current file.
296 It is buffer local, used to make moving a bookmark forward
297 through a file easier.")
299 (make-variable-buffer-local 'bookmark-current-bookmark)
302 (defvar bookmark-alist-modification-count 0
303 "Number of modifications to bookmark list since it was last saved.")
306 (defvar bookmark-search-size 16
307 "Length of the context strings recorded on either side of a bookmark.")
310 (defvar bookmark-current-point 0)
311 (defvar bookmark-yank-point 0)
312 (defvar bookmark-current-buffer nil)
314 (defvar Info-current-node)
315 (defvar Info-suffix-list)
317 ;; Helper functions.
319 ;; Only functions on this page and the next one (file formats) need to
320 ;; know anything about the format of bookmark-alist entries.
321 ;; Everyone else should go through them.
323 (defun bookmark-name-from-full-record (full-record)
324 "Return name of FULL-RECORD \(an alist element instead of a string\)."
325 (car full-record))
328 (defun bookmark-all-names ()
329 "Return a list of all current bookmark names."
330 (bookmark-maybe-load-default-file)
331 (mapcar
332 (lambda (full-record)
333 (bookmark-name-from-full-record full-record))
334 bookmark-alist))
337 (defun bookmark-get-bookmark (bookmark)
338 "Return the full entry for BOOKMARK in `bookmark-alist'.
339 If BOOKMARK is not a string, return nil."
340 (when (stringp bookmark)
341 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
344 (defun bookmark-get-bookmark-record (bookmark)
345 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
346 That is, all information but the name."
347 (car (cdr (bookmark-get-bookmark bookmark))))
350 (defun bookmark-set-name (bookmark newname)
351 "Set BOOKMARK's name to NEWNAME."
352 (setcar
353 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
354 newname))
357 (defun bookmark-get-annotation (bookmark)
358 "Return the annotation of BOOKMARK, or nil if none."
359 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
362 (defun bookmark-set-annotation (bookmark ann)
363 "Set the annotation of BOOKMARK to ANN."
364 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
365 (if cell
366 (setcdr cell ann)
367 (nconc (bookmark-get-bookmark-record bookmark)
368 (list (cons 'annotation ann))))))
371 (defun bookmark-get-filename (bookmark)
372 "Return the full filename of BOOKMARK."
373 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
376 (defun bookmark-set-filename (bookmark filename)
377 "Set the full filename of BOOKMARK to FILENAME."
378 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
379 (if cell
380 (setcdr cell filename)
381 (nconc (bookmark-get-bookmark-record bookmark)
382 (list (cons 'filename filename))))
383 (setq bookmark-alist-modification-count
384 (1+ bookmark-alist-modification-count))
385 (if (bookmark-time-to-save-p)
386 (bookmark-save))))
389 (defun bookmark-get-position (bookmark)
390 "Return the position \(i.e.: point\) of BOOKMARK."
391 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
394 (defun bookmark-set-position (bookmark position)
395 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
396 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
397 (if cell
398 (setcdr cell position)
399 (nconc (bookmark-get-bookmark-record bookmark)
400 (list (cons 'position position))))))
403 (defun bookmark-get-front-context-string (bookmark)
404 "Return the front-context-string of BOOKMARK."
405 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
408 (defun bookmark-set-front-context-string (bookmark string)
409 "Set the front-context-string of BOOKMARK to STRING."
410 (let ((cell (assq 'front-context-string
411 (bookmark-get-bookmark-record bookmark))))
412 (if cell
413 (setcdr cell string)
414 (nconc (bookmark-get-bookmark-record bookmark)
415 (list (cons 'front-context-string string))))))
418 (defun bookmark-get-rear-context-string (bookmark)
419 "Return the rear-context-string of BOOKMARK."
420 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
423 (defun bookmark-set-rear-context-string (bookmark string)
424 "Set the rear-context-string of BOOKMARK to STRING."
425 (let ((cell (assq 'rear-context-string
426 (bookmark-get-bookmark-record bookmark))))
427 (if cell
428 (setcdr cell string)
429 (nconc (bookmark-get-bookmark-record bookmark)
430 (list (cons 'rear-context-string string))))))
433 (defun bookmark-get-info-node (bookmark)
434 "Get the info node associated with BOOKMARK."
435 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
438 (defun bookmark-set-info-node (bookmark node)
439 "Set the Info node of BOOKMARK to NODE."
440 (let ((cell (assq 'info-node
441 (bookmark-get-bookmark-record bookmark))))
442 (if cell
443 (setcdr cell node)
444 (nconc (bookmark-get-bookmark-record bookmark)
445 (list (cons 'info-node node)))))
447 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
448 (sit-for 4))
451 (defvar bookmark-history nil
452 "The history list for bookmark functions.")
455 (defun bookmark-completing-read (prompt &optional default)
456 "Prompting with PROMPT, read a bookmark name in completion.
457 PROMPT will get a \": \" stuck on the end no matter what, so you
458 probably don't want to include one yourself.
459 Optional second arg DEFAULT is a string to return if the user enters
460 the empty string."
461 (bookmark-maybe-load-default-file) ; paranoia
462 (if (listp last-nonmenu-event)
463 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
464 (let* ((completion-ignore-case bookmark-completion-ignore-case)
465 (default default)
466 (prompt (if default
467 (concat prompt (format " (%s): " default))
468 (concat prompt ": ")))
469 (str
470 (completing-read prompt
471 bookmark-alist
475 'bookmark-history)))
476 (if (string-equal "" str) default str))))
479 (defmacro bookmark-maybe-historicize-string (string)
480 "Put STRING into the bookmark prompt history, if caller non-interactive.
481 We need this because sometimes bookmark functions are invoked from
482 menus, so `completing-read' never gets a chance to set `bookmark-history'."
483 `(or
484 (interactive-p)
485 (setq bookmark-history (cons ,string bookmark-history))))
488 (defun bookmark-make (name &optional annotation overwrite info-node)
489 "Make a bookmark named NAME.
490 Optional second arg ANNOTATION gives it an annotation.
491 Optional third arg OVERWRITE means replace any existing bookmarks with
492 this name.
493 Optional fourth arg INFO-NODE means this bookmark is at info node
494 INFO-NODE, so record this fact in the bookmark's entry."
495 (bookmark-maybe-load-default-file)
496 (let ((stripped-name (copy-sequence name)))
497 (or bookmark-xemacsp
498 ;; XEmacs's `set-text-properties' doesn't work on
499 ;; free-standing strings, apparently.
500 (set-text-properties 0 (length stripped-name) nil stripped-name))
501 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
502 ;; already existing bookmark under that name and
503 ;; no prefix arg means just overwrite old bookmark
504 (setcdr (bookmark-get-bookmark stripped-name)
505 (list (bookmark-make-cell annotation info-node)))
507 ;; otherwise just cons it onto the front (either the bookmark
508 ;; doesn't exist already, or there is no prefix arg. In either
509 ;; case, we want the new bookmark consed onto the alist...)
511 (setq bookmark-alist
512 (cons
513 (list stripped-name
514 (bookmark-make-cell annotation info-node))
515 bookmark-alist)))
517 ;; Added by db
518 (setq bookmark-current-bookmark stripped-name)
519 (setq bookmark-alist-modification-count
520 (1+ bookmark-alist-modification-count))
521 (if (bookmark-time-to-save-p)
522 (bookmark-save))))
525 (defun bookmark-make-cell (annotation &optional info-node)
526 "Return the record part of a new bookmark, given ANNOTATION.
527 Must be at the correct position in the buffer in which the bookmark is
528 being set. This might change someday.
529 Optional second arg INFO-NODE means this bookmark is at info node
530 INFO-NODE, so record this fact in the bookmark's entry."
531 (let ((the-record
532 `((filename . ,(bookmark-buffer-file-name))
533 (front-context-string
534 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
535 (buffer-substring-no-properties
536 (point)
537 (+ (point) bookmark-search-size))
538 nil))
539 (rear-context-string
540 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
541 (buffer-substring-no-properties
542 (point)
543 (- (point) bookmark-search-size))
544 nil))
545 (position . ,(point)))))
547 ;; Now fill in the optional parts:
549 ;; Take no chances with text properties
550 (set-text-properties 0 (length annotation) nil annotation)
551 (set-text-properties 0 (length info-node) nil info-node)
553 (if annotation
554 (nconc the-record (list (cons 'annotation annotation))))
555 (if info-node
556 (nconc the-record (list (cons 'info-node info-node))))
558 ;; Finally, return the completed record.
559 the-record))
563 ;;; File format stuff
565 ;; The OLD format of the bookmark-alist was:
567 ;; ((bookmark-name (filename
568 ;; string-in-front
569 ;; string-behind
570 ;; point))
571 ;; ...)
573 ;; The NEW format of the bookmark-alist is:
575 ;; ((bookmark-name ((filename . FILENAME)
576 ;; (front-context-string . string-in-front)
577 ;; (rear-context-string . string-behind)
578 ;; (position . POINT)
579 ;; (annotation . annotation)
580 ;; (whatever . VALUE)
581 ;; ...
582 ;; ))
583 ;; ...)
586 ;; I switched to using an internal as well as external alist because I
587 ;; felt that would be a more flexible framework in which to add
588 ;; features. It means that the order in which values appear doesn't
589 ;; matter, and it means that arbitrary values can be added without
590 ;; risk of interfering with existing ones.
592 ;; BOOKMARK-NAME is the string the user gives the bookmark and
593 ;; accesses it by from then on.
595 ;; FILENAME is the location of the file in which the bookmark is set.
597 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
598 ;; context in front of the point at which the bookmark is set.
600 ;; STRING-BEHIND is the same thing, but after the point.
602 ;; The context strings exist so that modifications to a file don't
603 ;; necessarily cause a bookmark's position to be invalidated.
604 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
605 ;; case the file has changed since the bookmark was set. It will
606 ;; attempt to place the user before the changes, if there were any.
607 ;; ANNOTATION is the annotation for the bookmark; it may not exist
608 ;; (for backward compatibility), be nil (no annotation), or be a
609 ;; string.
612 (defconst bookmark-file-format-version 1
613 "The current version of the format used by bookmark files.
614 You should never need to change this.")
617 (defconst bookmark-end-of-version-stamp-marker
618 "-*- End Of Bookmark File Format Version Stamp -*-\n"
619 "This string marks the end of the version stamp in a bookmark file.")
622 (defun bookmark-alist-from-buffer ()
623 "Return a `bookmark-alist' (in any format) from the current buffer.
624 The buffer must of course contain bookmark format information.
625 Does not care from where in the buffer it is called, and does not
626 affect point."
627 (save-excursion
628 (goto-char (point-min))
629 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
630 (read (current-buffer))
631 ;; Else we're dealing with format version 0
632 (if (search-forward "(" nil t)
633 (progn
634 (forward-char -1)
635 (read (current-buffer)))
636 ;; Else no hope of getting information here.
637 (error "Not bookmark format")))))
640 (defun bookmark-upgrade-version-0-alist (old-list)
641 "Upgrade a version 0 alist OLD-LIST to the current version."
642 (mapcar
643 (lambda (bookmark)
644 (let* ((name (car bookmark))
645 (record (car (cdr bookmark)))
646 (filename (nth 0 record))
647 (front-str (nth 1 record))
648 (rear-str (nth 2 record))
649 (position (nth 3 record))
650 (ann (nth 4 record)))
651 (list
652 name
653 `((filename . ,filename)
654 (front-context-string . ,(or front-str ""))
655 (rear-context-string . ,(or rear-str ""))
656 (position . ,position)
657 (annotation . ,ann)))))
658 old-list))
661 (defun bookmark-upgrade-file-format-from-0 ()
662 "Upgrade a bookmark file of format 0 (the original format) to format 1.
663 This expects to be called from `point-min' in a bookmark file."
664 (message "Upgrading bookmark format from 0 to %d..."
665 bookmark-file-format-version)
666 (let* ((old-list (bookmark-alist-from-buffer))
667 (new-list (bookmark-upgrade-version-0-alist old-list)))
668 (delete-region (point-min) (point-max))
669 (bookmark-insert-file-format-version-stamp)
670 (pp new-list (current-buffer))
671 (save-buffer))
672 (goto-char (point-min))
673 (message "Upgrading bookmark format from 0 to %d...done"
674 bookmark-file-format-version)
678 (defun bookmark-grok-file-format-version ()
679 "Return an integer which is the file-format version of this bookmark file.
680 This expects to be called from `point-min' in a bookmark file."
681 (if (looking-at "^;;;;")
682 (save-excursion
683 (save-match-data
684 (re-search-forward "[0-9]")
685 (forward-char -1)
686 (read (current-buffer))))
687 ;; Else this is format version 0, the original one, which didn't
688 ;; even have version stamps.
692 (defun bookmark-maybe-upgrade-file-format ()
693 "Check the file-format version of this bookmark file.
694 If the version is not up-to-date, upgrade it automatically.
695 This expects to be called from `point-min' in a bookmark file."
696 (let ((version (bookmark-grok-file-format-version)))
697 (cond
698 ((= version bookmark-file-format-version)
699 ) ; home free -- version is current
700 ((= version 0)
701 (bookmark-upgrade-file-format-from-0))
703 (error "Bookmark file format version strangeness")))))
706 (defun bookmark-insert-file-format-version-stamp ()
707 "Insert text indicating current version of bookmark file format."
708 (insert
709 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
710 bookmark-file-format-version))
711 (insert ";;; This format is meant to be slightly human-readable;\n"
712 ";;; nevertheless, you probably don't want to edit it.\n"
713 ";;; "
714 bookmark-end-of-version-stamp-marker))
717 ;;; end file-format stuff
720 ;;; Generic helpers.
722 (defun bookmark-maybe-message (fmt &rest args)
723 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
724 (if (>= baud-rate 9600)
725 (apply 'message fmt args)))
728 ;;; Core code:
730 ;;;###autoload
731 (defun bookmark-set (&optional name parg)
732 "Set a bookmark named NAME inside a file.
733 If name is nil, then the user will be prompted.
734 With prefix arg, will not overwrite a bookmark that has the same name
735 as NAME if such a bookmark already exists, but instead will \"push\"
736 the new bookmark onto the bookmark alist. Thus the most recently set
737 bookmark with name NAME would be the one in effect at any given time,
738 but the others are still there, should you decide to delete the most
739 recent one.
741 To yank words from the text of the buffer and use them as part of the
742 bookmark name, type C-w while setting a bookmark. Successive C-w's
743 yank successive words.
745 Typing C-u inserts the name of the last bookmark used in the buffer
746 \(as an aid in using a single bookmark name to track your progress
747 through a large file\). If no bookmark was used, then C-u inserts the
748 name of the file being visited.
750 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
751 and it removes only the first instance of a bookmark with that name from
752 the list of bookmarks.\)"
753 (interactive (list nil current-prefix-arg))
755 (bookmark-buffer-file-name)
756 (error "Buffer not visiting a file or directory"))
758 (bookmark-maybe-load-default-file)
760 (setq bookmark-current-point (point))
761 (setq bookmark-yank-point (point))
762 (setq bookmark-current-buffer (current-buffer))
764 (let* ((default (or bookmark-current-bookmark
765 (bookmark-buffer-name)))
766 (str
767 (or name
768 (read-from-minibuffer
769 (format "Set bookmark (%s): " default)
771 (let ((now-map (copy-keymap minibuffer-local-map)))
772 (define-key now-map "\C-w" 'bookmark-yank-word)
773 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
774 now-map))))
775 (annotation nil))
776 (and (string-equal str "") (setq str default))
777 ;; Ask for an annotation buffer for this bookmark
778 (if bookmark-use-annotations
779 (bookmark-read-annotation parg str)
780 (bookmark-make str annotation parg (bookmark-info-current-node))
781 (setq bookmark-current-bookmark str)
782 (bookmark-bmenu-surreptitiously-rebuild-list)
783 (goto-char bookmark-current-point))))
786 (defun bookmark-info-current-node ()
787 "If in Info-mode, return current node name (a string), else nil."
788 (if (eq major-mode 'Info-mode)
789 Info-current-node))
792 (defun bookmark-kill-line (&optional newline-too)
793 "Kill from point to end of line.
794 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
795 Does not affect the kill ring."
796 (let ((eol (save-excursion (end-of-line) (point))))
797 (delete-region (point) eol)
798 (if (and newline-too (looking-at "\n"))
799 (delete-char 1))))
802 ;; Defvars to avoid compilation warnings:
803 (defvar bookmark-annotation-paragraph nil)
804 (defvar bookmark-annotation-name nil)
805 (defvar bookmark-annotation-buffer nil)
806 (defvar bookmark-annotation-file nil)
807 (defvar bookmark-annotation-point nil)
810 (defun bookmark-send-annotation ()
811 "Use buffer contents as the annotation for a bookmark.
812 Exclude lines that begin with `#'.
813 Store the annotation text in the bookmark list with
814 the bookmark (and file, and point) specified in buffer local variables."
815 (interactive)
816 (if (not (eq major-mode 'bookmark-read-annotation-mode))
817 (error "Not in bookmark-read-annotation-mode"))
818 (goto-char (point-min))
819 (while (< (point) (point-max))
820 (if (looking-at "^#")
821 (bookmark-kill-line t)
822 (forward-line 1)))
823 (let ((annotation (buffer-string))
824 (parg bookmark-annotation-paragraph)
825 (bookmark bookmark-annotation-name)
826 (pt bookmark-annotation-point)
827 (buf bookmark-annotation-buffer))
828 ;; for bookmark-make-cell to work, we need to be
829 ;; in the relevant buffer, at the relevant point.
830 ;; Actually, bookmark-make-cell should probably be re-written,
831 ;; to avoid this need. Should I handle the error if a buffer is
832 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
833 (save-excursion
834 (pop-to-buffer buf)
835 (goto-char pt)
836 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
837 (setq bookmark-current-bookmark bookmark))
838 (bookmark-bmenu-surreptitiously-rebuild-list)
839 (goto-char bookmark-current-point))
840 (kill-buffer (current-buffer)))
843 (defun bookmark-default-annotation-text (bookmark)
844 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
845 "# All lines which start with a '#' will be deleted.\n"
846 "# Type C-c C-c when done.\n#\n"
847 "# Author: " (user-full-name) " <" (user-login-name) "@"
848 (system-name) ">\n"
849 "# Date: " (current-time-string) "\n"))
852 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
853 "Function to return default text to use for a bookmark annotation.
854 It takes one argument, the name of the bookmark, as a string.")
856 (defun bookmark-read-annotation-mode (buf point parg bookmark)
857 "Mode for composing annotations for a bookmark.
858 Wants BUF, POINT, PARG, and BOOKMARK.
859 When you have finished composing, type \\[bookmark-send-annotation] to send
860 the annotation.
862 \\{bookmark-read-annotation-mode-map}
864 (interactive)
865 (kill-all-local-variables)
866 (make-local-variable 'bookmark-annotation-paragraph)
867 (make-local-variable 'bookmark-annotation-name)
868 (make-local-variable 'bookmark-annotation-buffer)
869 (make-local-variable 'bookmark-annotation-file)
870 (make-local-variable 'bookmark-annotation-point)
871 (setq bookmark-annotation-paragraph parg)
872 (setq bookmark-annotation-name bookmark)
873 (setq bookmark-annotation-buffer buf)
874 (setq bookmark-annotation-file (buffer-file-name buf))
875 (setq bookmark-annotation-point point)
876 (use-local-map bookmark-read-annotation-mode-map)
877 (setq major-mode 'bookmark-read-annotation-mode)
878 (insert (funcall bookmark-read-annotation-text-func bookmark))
879 (run-mode-hooks 'text-mode-hook))
882 (defun bookmark-read-annotation (parg bookmark)
883 "Pop up a buffer for entering a bookmark annotation.
884 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
885 (let ((buf (current-buffer))
886 (point (point)))
887 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
888 (bookmark-read-annotation-mode buf point parg bookmark)))
891 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
892 "Keymap for editing an annotation of a bookmark.")
895 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
896 'bookmark-send-edited-annotation)
899 (defun bookmark-edit-annotation-mode (bookmark)
900 "Mode for editing the annotation of bookmark BOOKMARK.
901 When you have finished composing, type \\[bookmark-send-annotation].
903 \\{bookmark-edit-annotation-mode-map}
905 (interactive)
906 (kill-all-local-variables)
907 (make-local-variable 'bookmark-annotation-name)
908 (setq bookmark-annotation-name bookmark)
909 (use-local-map bookmark-edit-annotation-mode-map)
910 (setq major-mode 'bookmark-edit-annotation-mode
911 mode-name "Edit Bookmark Annotation")
912 (insert (funcall bookmark-read-annotation-text-func bookmark))
913 (let ((annotation (bookmark-get-annotation bookmark)))
914 (if (and annotation (not (string-equal annotation "")))
915 (insert annotation)))
916 (run-mode-hooks 'text-mode-hook))
919 (defun bookmark-send-edited-annotation ()
920 "Use buffer contents as annotation for a bookmark.
921 Lines beginning with `#' are ignored."
922 (interactive)
923 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
924 (error "Not in bookmark-edit-annotation-mode"))
925 (goto-char (point-min))
926 (while (< (point) (point-max))
927 (if (looking-at "^#")
928 (bookmark-kill-line t)
929 (forward-line 1)))
930 (let ((annotation (buffer-string))
931 (bookmark bookmark-annotation-name))
932 (bookmark-set-annotation bookmark annotation)
933 (bookmark-bmenu-surreptitiously-rebuild-list)
934 (goto-char bookmark-current-point))
935 (kill-buffer (current-buffer)))
938 (defun bookmark-edit-annotation (bookmark)
939 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
940 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
941 (bookmark-edit-annotation-mode bookmark))
944 (defun bookmark-insert-current-bookmark ()
945 "Insert this buffer's value of `bookmark-current-bookmark'.
946 Default to file name if it's nil."
947 (interactive)
948 (let ((str
949 (save-excursion
950 (set-buffer bookmark-current-buffer)
951 bookmark-current-bookmark)))
952 (if str (insert str) (bookmark-insert-buffer-name))))
955 (defun bookmark-insert-buffer-name ()
956 "Insert the current file name into the bookmark name being set.
957 The directory part of the file name is not used."
958 (interactive)
959 (let ((str
960 (save-excursion
961 (set-buffer bookmark-current-buffer)
962 (bookmark-buffer-name))))
963 (insert str)))
966 (defun bookmark-buffer-name ()
967 "Return the name of the current buffer's file, non-directory.
968 In Info, return the current node."
969 (cond
970 ;; Are we in Info?
971 ((string-equal mode-name "Info") Info-current-node)
972 ;; Or are we a file?
973 (buffer-file-name (file-name-nondirectory buffer-file-name))
974 ;; Or are we a directory?
975 ((and (boundp 'dired-directory) dired-directory)
976 (let* ((dirname (if (stringp dired-directory)
977 dired-directory
978 (car dired-directory)))
979 (idx (1- (length dirname))))
980 ;; Strip the trailing slash.
981 (if (= ?/ (aref dirname idx))
982 (file-name-nondirectory (substring dirname 0 idx))
983 ;; Else return the current-buffer
984 (buffer-name (current-buffer)))))
985 ;; If all else fails, use the buffer's name.
987 (buffer-name (current-buffer)))))
990 (defun bookmark-yank-word ()
991 (interactive)
992 ;; get the next word from the buffer and append it to the name of
993 ;; the bookmark currently being set.
994 (let ((string (save-excursion
995 (set-buffer bookmark-current-buffer)
996 (goto-char bookmark-yank-point)
997 (buffer-substring-no-properties
998 (point)
999 (progn
1000 (forward-word 1)
1001 (setq bookmark-yank-point (point)))))))
1002 (insert string)))
1005 (defvar Info-current-file)
1007 (defun bookmark-buffer-file-name ()
1008 "Return the current buffer's file in a way useful for bookmarks.
1009 For example, if this is a Info buffer, return the Info file's name."
1010 (if (eq major-mode 'Info-mode)
1011 Info-current-file
1013 buffer-file-name
1014 (if (and (boundp 'dired-directory) dired-directory)
1015 (if (stringp dired-directory)
1016 dired-directory
1017 (car dired-directory))))))
1020 (defun bookmark-maybe-load-default-file ()
1021 (and (not bookmarks-already-loaded)
1022 (null bookmark-alist)
1023 (prog2
1024 (and
1025 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1026 ;; to be renamed.
1027 (file-exists-p (expand-file-name bookmark-old-default-file))
1028 (not (file-exists-p (expand-file-name bookmark-default-file)))
1029 (rename-file (expand-file-name bookmark-old-default-file)
1030 (expand-file-name bookmark-default-file)))
1031 ;; return t so the `and' will continue...
1034 (file-readable-p (expand-file-name bookmark-default-file))
1035 (bookmark-load bookmark-default-file t t)
1036 (setq bookmarks-already-loaded t)))
1039 (defun bookmark-maybe-sort-alist ()
1040 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1041 ;;is non-nil, then return a sorted copy of the alist.
1042 (if bookmark-sort-flag
1043 (setq bookmark-alist
1044 (sort (copy-alist bookmark-alist)
1045 (function
1046 (lambda (x y) (string-lessp (car x) (car y))))))))
1049 (defvar bookmark-after-jump-hook nil
1050 "Hook run after `bookmark-jump' jumps to a bookmark.
1051 Useful for example to unhide text in `outline-mode'.")
1053 ;;;###autoload
1054 (defun bookmark-jump (bookmark)
1055 "Jump to bookmark BOOKMARK (a point in some file).
1056 You may have a problem using this function if the value of variable
1057 `bookmark-alist' is nil. If that happens, you need to load in some
1058 bookmarks. See help on function `bookmark-load' for more about
1059 this.
1061 If the file pointed to by BOOKMARK no longer exists, you will be asked
1062 if you wish to give the bookmark a new location, and `bookmark-jump'
1063 will then jump to the new location, as well as recording it in place
1064 of the old one in the permanent bookmark record."
1065 (interactive
1066 (list (bookmark-completing-read "Jump to bookmark"
1067 bookmark-current-bookmark)))
1068 (unless bookmark
1069 (error "No bookmark specified"))
1070 (bookmark-maybe-historicize-string bookmark)
1071 (let ((cell (bookmark-jump-noselect bookmark)))
1072 (and cell
1073 (switch-to-buffer (car cell))
1074 (goto-char (cdr cell))
1075 (progn (run-hooks 'bookmark-after-jump-hook) t)
1076 (if bookmark-automatically-show-annotations
1077 ;; if there is an annotation for this bookmark,
1078 ;; show it in a buffer.
1079 (bookmark-show-annotation bookmark)))))
1082 (defun bookmark-file-or-variation-thereof (file)
1083 "Return FILE (a string) if it exists, or return a reasonable
1084 variation of FILE if that exists. Reasonable variations are checked
1085 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1086 nor a reasonable variation thereof, then still return FILE if it can
1087 be retrieved from a VC backend, else return nil."
1088 (if (file-exists-p file)
1089 file
1091 (progn (require 'info) ; ensure Info-suffix-list is bound
1092 (catch 'found
1093 (mapc (lambda (elt)
1094 (let ((suffixed-file (concat file (car elt))))
1095 (if (file-exists-p suffixed-file)
1096 (throw 'found suffixed-file))))
1097 Info-suffix-list)
1098 nil))
1099 ;; Last possibility: try VC
1100 (if (vc-backend file) file))))
1103 (defun bookmark-jump-noselect (str)
1104 ;; a leetle helper for bookmark-jump :-)
1105 ;; returns (BUFFER . POINT)
1106 (bookmark-maybe-load-default-file)
1107 (let* ((file (expand-file-name (bookmark-get-filename str)))
1108 (forward-str (bookmark-get-front-context-string str))
1109 (behind-str (bookmark-get-rear-context-string str))
1110 (place (bookmark-get-position str))
1111 (info-node (bookmark-get-info-node str))
1112 (orig-file file)
1114 (if (setq file (bookmark-file-or-variation-thereof file))
1115 (save-excursion
1116 (save-window-excursion
1117 (if info-node
1118 ;; Info nodes must be visited with care.
1119 (progn
1120 (require 'info)
1121 (with-no-warnings
1122 (Info-find-node file info-node)))
1123 ;; Else no Info. Can do an ordinary find-file:
1124 (set-buffer (find-file-noselect file))
1125 (goto-char place))
1127 ;; Go searching forward first. Then, if forward-str exists and
1128 ;; was found in the file, we can search backward for behind-str.
1129 ;; Rationale is that if text was inserted between the two in the
1130 ;; file, it's better to be put before it so you can read it,
1131 ;; rather than after and remain perhaps unaware of the changes.
1132 (if forward-str
1133 (if (search-forward forward-str (point-max) t)
1134 (goto-char (match-beginning 0))))
1135 (if behind-str
1136 (if (search-backward behind-str (point-min) t)
1137 (goto-char (match-end 0))))
1138 ;; added by db
1139 (setq bookmark-current-bookmark str)
1140 (cons (current-buffer) (point))))
1142 ;; Else unable to find the marked file, so ask if user wants to
1143 ;; relocate the bookmark, else remind them to consider deletion.
1144 (ding)
1145 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1146 " nonexistent. Relocate \""
1148 "\"? "))
1149 (progn
1150 (bookmark-relocate str)
1151 ;; gasp! It's a recursive function call in Emacs Lisp!
1152 (bookmark-jump-noselect str))
1153 (message
1154 "Bookmark not relocated; consider removing it \(%s\)." str)
1155 nil))))
1158 ;;;###autoload
1159 (defun bookmark-relocate (bookmark)
1160 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1161 This makes an already existing bookmark point to that file, instead of
1162 the one it used to point at. Useful when a file has been renamed
1163 after a bookmark was set in it."
1164 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1165 (bookmark-maybe-historicize-string bookmark)
1166 (bookmark-maybe-load-default-file)
1167 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1168 (newloc (expand-file-name
1169 (read-file-name
1170 (format "Relocate %s to: " bookmark)
1171 (file-name-directory bmrk-filename)))))
1172 (bookmark-set-filename bookmark newloc)
1173 (bookmark-bmenu-surreptitiously-rebuild-list)))
1176 ;;;###autoload
1177 (defun bookmark-insert-location (bookmark &optional no-history)
1178 "Insert the name of the file associated with BOOKMARK.
1179 Optional second arg NO-HISTORY means don't record this in the
1180 minibuffer history list `bookmark-history'."
1181 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1182 (or no-history (bookmark-maybe-historicize-string bookmark))
1183 (let ((start (point)))
1184 (prog1
1185 (insert (bookmark-location bookmark)) ; *Return this line*
1186 (if (and (display-color-p) (display-mouse-p))
1187 (add-text-properties
1188 start
1189 (save-excursion (re-search-backward
1190 "[^ \t]")
1191 (1+ (point)))
1192 '(mouse-face highlight
1193 follow-link t
1194 help-echo "mouse-2: go to this bookmark in other window"))))))
1196 ;;;###autoload
1197 (defalias 'bookmark-locate 'bookmark-insert-location)
1199 (defun bookmark-location (bookmark)
1200 "Return the name of the file associated with BOOKMARK."
1201 (bookmark-maybe-load-default-file)
1202 (bookmark-get-filename bookmark))
1205 ;;;###autoload
1206 (defun bookmark-rename (old &optional new)
1207 "Change the name of OLD bookmark to NEW name.
1208 If called from keyboard, prompt for OLD and NEW. If called from
1209 menubar, select OLD from a menu and prompt for NEW.
1211 If called from Lisp, prompt for NEW if only OLD was passed as an
1212 argument. If called with two strings, then no prompting is done. You
1213 must pass at least OLD when calling from Lisp.
1215 While you are entering the new name, consecutive C-w's insert
1216 consecutive words from the text of the buffer into the new bookmark
1217 name."
1218 (interactive (list (bookmark-completing-read "Old bookmark name")))
1219 (bookmark-maybe-historicize-string old)
1220 (bookmark-maybe-load-default-file)
1222 (setq bookmark-current-point (point))
1223 (setq bookmark-yank-point (point))
1224 (setq bookmark-current-buffer (current-buffer))
1225 (let ((newname
1226 (or new ; use second arg, if non-nil
1227 (read-from-minibuffer
1228 "New name: "
1230 (let ((now-map (copy-keymap minibuffer-local-map)))
1231 (define-key now-map "\C-w" 'bookmark-yank-word)
1232 now-map)
1234 'bookmark-history))))
1235 (bookmark-set-name old newname)
1236 (setq bookmark-current-bookmark newname)
1237 (bookmark-bmenu-surreptitiously-rebuild-list)
1238 (setq bookmark-alist-modification-count
1239 (1+ bookmark-alist-modification-count))
1240 (if (bookmark-time-to-save-p)
1241 (bookmark-save))))
1244 ;;;###autoload
1245 (defun bookmark-insert (bookmark)
1246 "Insert the text of the file pointed to by bookmark BOOKMARK.
1247 You may have a problem using this function if the value of variable
1248 `bookmark-alist' is nil. If that happens, you need to load in some
1249 bookmarks. See help on function `bookmark-load' for more about
1250 this."
1251 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1252 (bookmark-maybe-historicize-string bookmark)
1253 (bookmark-maybe-load-default-file)
1254 (let ((orig-point (point))
1255 (str-to-insert
1256 (save-excursion
1257 (set-buffer (car (bookmark-jump-noselect bookmark)))
1258 (buffer-string))))
1259 (insert str-to-insert)
1260 (push-mark)
1261 (goto-char orig-point)))
1264 ;;;###autoload
1265 (defun bookmark-delete (bookmark &optional batch)
1266 "Delete BOOKMARK from the bookmark list.
1267 Removes only the first instance of a bookmark with that name. If
1268 there are one or more other bookmarks with the same name, they will
1269 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1270 one most recently used in this file, if any\).
1271 Optional second arg BATCH means don't update the bookmark list buffer,
1272 probably because we were called from there."
1273 (interactive
1274 (list (bookmark-completing-read "Delete bookmark"
1275 bookmark-current-bookmark)))
1276 (bookmark-maybe-historicize-string bookmark)
1277 (bookmark-maybe-load-default-file)
1278 (let ((will-go (bookmark-get-bookmark bookmark)))
1279 (setq bookmark-alist (delq will-go bookmark-alist))
1280 ;; Added by db, nil bookmark-current-bookmark if the last
1281 ;; occurrence has been deleted
1282 (or (bookmark-get-bookmark bookmark-current-bookmark)
1283 (setq bookmark-current-bookmark nil)))
1284 ;; Don't rebuild the list
1285 (if batch
1287 (bookmark-bmenu-surreptitiously-rebuild-list)
1288 (setq bookmark-alist-modification-count
1289 (1+ bookmark-alist-modification-count))
1290 (if (bookmark-time-to-save-p)
1291 (bookmark-save))))
1294 (defun bookmark-time-to-save-p (&optional last-time)
1295 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1296 ;; finds out whether it's time to save bookmarks to a file, by
1297 ;; examining the value of variable bookmark-save-flag, and maybe
1298 ;; bookmark-alist-modification-count. Returns t if they should be
1299 ;; saved, nil otherwise. if last-time is non-nil, then this is
1300 ;; being called when emacs is killed.
1301 (cond (last-time
1302 (and (> bookmark-alist-modification-count 0)
1303 bookmark-save-flag))
1304 ((numberp bookmark-save-flag)
1305 (>= bookmark-alist-modification-count bookmark-save-flag))
1307 nil)))
1310 ;;;###autoload
1311 (defun bookmark-write ()
1312 "Write bookmarks to a file (reading the file name with the minibuffer).
1313 Don't use this in Lisp programs; use `bookmark-save' instead."
1314 (interactive)
1315 (bookmark-maybe-load-default-file)
1316 (bookmark-save t))
1319 ;;;###autoload
1320 (defun bookmark-save (&optional parg file)
1321 "Save currently defined bookmarks.
1322 Saves by default in the file defined by the variable
1323 `bookmark-default-file'. With a prefix arg, save it in file FILE
1324 \(second argument\).
1326 If you are calling this from Lisp, the two arguments are PARG and
1327 FILE, and if you just want it to write to the default file, then
1328 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1329 instead. If you pass in one argument, and it is non-nil, then the
1330 user will be interactively queried for a file to save in.
1332 When you want to load in the bookmarks from a file, use
1333 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1334 for a file, defaulting to the file defined by variable
1335 `bookmark-default-file'."
1336 (interactive "P")
1337 (bookmark-maybe-load-default-file)
1338 (cond
1339 ((and (null parg) (null file))
1340 ;;whether interactive or not, write to default file
1341 (bookmark-write-file bookmark-default-file))
1342 ((and (null parg) file)
1343 ;;whether interactive or not, write to given file
1344 (bookmark-write-file file))
1345 ((and parg (not file))
1346 ;;have been called interactively w/ prefix arg
1347 (let ((file (read-file-name "File to save bookmarks in: ")))
1348 (bookmark-write-file file)))
1349 (t ; someone called us with prefix-arg *and* a file, so just write to file
1350 (bookmark-write-file file)))
1351 ;; signal that we have synced the bookmark file by setting this to
1352 ;; 0. If there was an error at any point before, it will not get
1353 ;; set, which is what we want.
1354 (setq bookmark-alist-modification-count 0))
1358 (defun bookmark-write-file (file)
1359 (save-excursion
1360 (save-window-excursion
1361 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1362 (set-buffer (get-buffer-create " *Bookmarks*"))
1363 (goto-char (point-min))
1364 (delete-region (point-min) (point-max))
1365 (let ((print-length nil)
1366 (print-level nil))
1367 (bookmark-insert-file-format-version-stamp)
1368 (pp bookmark-alist (current-buffer))
1369 (let ((version-control
1370 (cond
1371 ((null bookmark-version-control) nil)
1372 ((eq 'never bookmark-version-control) 'never)
1373 ((eq 'nospecial bookmark-version-control) version-control)
1375 t))))
1376 (condition-case nil
1377 (write-region (point-min) (point-max) file)
1378 (file-error (message "Can't write %s" file)))
1379 (kill-buffer (current-buffer))
1380 (bookmark-maybe-message
1381 "Saving bookmarks to file %s...done" file))))))
1384 (defun bookmark-import-new-list (new-list)
1385 ;; Walk over the new list, adding each individual bookmark
1386 ;; carefully. "Carefully" means checking against the existing
1387 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1388 ;; as necessary.
1389 (let ((lst new-list)
1390 (names (bookmark-all-names)))
1391 (while lst
1392 (let* ((full-record (car lst)))
1393 (bookmark-maybe-rename full-record names)
1394 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1395 (setq names (cons (bookmark-name-from-full-record full-record) names))
1396 (setq lst (cdr lst))))))
1399 (defun bookmark-maybe-rename (full-record names)
1400 ;; just a helper for bookmark-import-new-list; it is only for
1401 ;; readability that this is not inlined.
1403 ;; Once this has found a free name, it sets full-record to that
1404 ;; name.
1405 (let ((found-name (bookmark-name-from-full-record full-record)))
1406 (if (member found-name names)
1407 ;; We've got a conflict, so generate a new name
1408 (let ((count 2)
1409 (new-name found-name))
1410 (while (member new-name names)
1411 (setq new-name (concat found-name (format "<%d>" count)))
1412 (setq count (1+ count)))
1413 (bookmark-set-name full-record new-name)))))
1416 ;;;###autoload
1417 (defun bookmark-load (file &optional overwrite no-msg)
1418 "Load bookmarks from FILE (which must be in bookmark format).
1419 Appends loaded bookmarks to the front of the list of bookmarks. If
1420 optional second argument OVERWRITE is non-nil, existing bookmarks are
1421 destroyed. Optional third arg NO-MSG means don't display any messages
1422 while loading.
1424 If you load a file that doesn't contain a proper bookmark alist, you
1425 will corrupt Emacs's bookmark list. Generally, you should only load
1426 in files that were created with the bookmark functions in the first
1427 place. Your own personal bookmark file, `~/.emacs.bmk', is
1428 maintained automatically by Emacs; you shouldn't need to load it
1429 explicitly.
1431 If you load a file containing bookmarks with the same names as
1432 bookmarks already present in your Emacs, the new bookmarks will get
1433 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1434 method buffers use to resolve name collisions."
1435 (interactive
1436 (list (read-file-name
1437 (format "Load bookmarks from: (%s) "
1438 bookmark-default-file)
1439 ;;Default might not be used often,
1440 ;;but there's no better default, and
1441 ;;I guess it's better than none at all.
1442 "~/" bookmark-default-file 'confirm)))
1443 (setq file (expand-file-name file))
1444 (if (file-readable-p file)
1445 (save-excursion
1446 (save-window-excursion
1447 (if (null no-msg)
1448 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1449 (set-buffer (let ((enable-local-variables nil))
1450 (find-file-noselect file)))
1451 (goto-char (point-min))
1452 (bookmark-maybe-upgrade-file-format)
1453 (let ((blist (bookmark-alist-from-buffer)))
1454 (if (listp blist)
1455 (progn
1456 (if overwrite
1457 (progn
1458 (setq bookmark-alist blist)
1459 (setq bookmark-alist-modification-count 0))
1460 ;; else
1461 (bookmark-import-new-list blist)
1462 (setq bookmark-alist-modification-count
1463 (1+ bookmark-alist-modification-count)))
1464 (if (string-equal
1465 (expand-file-name bookmark-default-file)
1466 file)
1467 (setq bookmarks-already-loaded t))
1468 (bookmark-bmenu-surreptitiously-rebuild-list))
1469 (error "Invalid bookmark list in %s" file)))
1470 (kill-buffer (current-buffer)))
1471 (if (null no-msg)
1472 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1473 (error "Cannot read bookmark file %s" file)))
1477 ;;; Code supporting the dired-like bookmark menu. Prefix is
1478 ;;; "bookmark-bmenu" for "buffer-menu":
1481 (defvar bookmark-bmenu-bookmark-column nil)
1484 (defvar bookmark-bmenu-hidden-bookmarks ())
1487 (defvar bookmark-bmenu-mode-map nil)
1490 (if bookmark-bmenu-mode-map
1492 (setq bookmark-bmenu-mode-map (make-keymap))
1493 (suppress-keymap bookmark-bmenu-mode-map t)
1494 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1495 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1496 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1497 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1498 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1499 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1500 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1501 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1502 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1503 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1504 (define-key bookmark-bmenu-mode-map "\C-o"
1505 'bookmark-bmenu-switch-other-window)
1506 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1507 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1508 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1509 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1510 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1511 (define-key bookmark-bmenu-mode-map " " 'next-line)
1512 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1513 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1514 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1515 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1516 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1517 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1518 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1519 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1520 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1521 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1522 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1523 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1524 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1525 (define-key bookmark-bmenu-mode-map [mouse-2]
1526 'bookmark-bmenu-other-window-with-mouse))
1530 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1531 ;; data.
1532 (put 'bookmark-bmenu-mode 'mode-class 'special)
1535 ;; todo: need to display whether or not bookmark exists as a buffer in
1536 ;; flag column.
1538 ;; Format:
1539 ;; FLAGS BOOKMARK [ LOCATION ]
1542 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1543 "Rebuild the Bookmark List if it exists.
1544 Don't affect the buffer ring order."
1545 (if (get-buffer "*Bookmark List*")
1546 (save-excursion
1547 (save-window-excursion
1548 (bookmark-bmenu-list)))))
1551 ;;;###autoload
1552 (defun bookmark-bmenu-list ()
1553 "Display a list of existing bookmarks.
1554 The list is displayed in a buffer named `*Bookmark List*'.
1555 The leftmost column displays a D if the bookmark is flagged for
1556 deletion, or > if it is flagged for displaying."
1557 (interactive)
1558 (bookmark-maybe-load-default-file)
1559 (if (interactive-p)
1560 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1561 (set-buffer (get-buffer-create "*Bookmark List*")))
1562 (let ((inhibit-read-only t))
1563 (erase-buffer)
1564 (insert "% Bookmark\n- --------\n")
1565 (add-text-properties (point-min) (point)
1566 '(font-lock-face bookmark-menu-heading))
1567 (bookmark-maybe-sort-alist)
1568 (mapcar
1569 (lambda (full-record)
1570 ;; if a bookmark has an annotation, prepend a "*"
1571 ;; in the list of bookmarks.
1572 (let ((annotation (bookmark-get-annotation
1573 (bookmark-name-from-full-record full-record))))
1574 (if (and annotation (not (string-equal annotation "")))
1575 (insert " *")
1576 (insert " "))
1577 (let ((start (point)))
1578 (insert (bookmark-name-from-full-record full-record))
1579 (if (and (display-color-p) (display-mouse-p))
1580 (add-text-properties
1581 start
1582 (save-excursion (re-search-backward
1583 "[^ \t]")
1584 (1+ (point)))
1585 '(mouse-face highlight
1586 follow-link t
1587 help-echo "mouse-2: go to this bookmark in other window")))
1588 (insert "\n")
1590 bookmark-alist))
1591 (goto-char (point-min))
1592 (forward-line 2)
1593 (bookmark-bmenu-mode)
1594 (if bookmark-bmenu-toggle-filenames
1595 (bookmark-bmenu-toggle-filenames t)))
1597 ;;;###autoload
1598 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1599 ;;;###autoload
1600 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1604 (defun bookmark-bmenu-mode ()
1605 "Major mode for editing a list of bookmarks.
1606 Each line describes one of the bookmarks in Emacs.
1607 Letters do not insert themselves; instead, they are commands.
1608 Bookmark names preceded by a \"*\" have annotations.
1609 \\<bookmark-bmenu-mode-map>
1610 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1611 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1612 Also show bookmarks marked using m in other windows.
1613 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1614 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1615 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1616 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1617 together with bookmark selected before this one in another window.
1618 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1619 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1620 so the bookmark menu bookmark remains visible in its window.
1621 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1622 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1623 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1624 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1625 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1626 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1627 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1628 With a prefix arg, prompts for a file to save in.
1629 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1630 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1631 With prefix argument, also move up one line.
1632 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1633 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1634 in another buffer.
1635 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1636 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1637 (kill-all-local-variables)
1638 (use-local-map bookmark-bmenu-mode-map)
1639 (setq truncate-lines t)
1640 (setq buffer-read-only t)
1641 (setq major-mode 'bookmark-bmenu-mode)
1642 (setq mode-name "Bookmark Menu")
1643 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1646 (defun bookmark-bmenu-toggle-filenames (&optional show)
1647 "Toggle whether filenames are shown in the bookmark list.
1648 Optional argument SHOW means show them unconditionally."
1649 (interactive)
1650 (cond
1651 (show
1652 (setq bookmark-bmenu-toggle-filenames nil)
1653 (bookmark-bmenu-show-filenames)
1654 (setq bookmark-bmenu-toggle-filenames t))
1655 (bookmark-bmenu-toggle-filenames
1656 (bookmark-bmenu-hide-filenames)
1657 (setq bookmark-bmenu-toggle-filenames nil))
1659 (bookmark-bmenu-show-filenames)
1660 (setq bookmark-bmenu-toggle-filenames t))))
1663 (defun bookmark-bmenu-show-filenames (&optional force)
1664 (if (and (not force) bookmark-bmenu-toggle-filenames)
1665 nil ;already shown, so do nothing
1666 (save-excursion
1667 (save-window-excursion
1668 (goto-char (point-min))
1669 (forward-line 2)
1670 (setq bookmark-bmenu-hidden-bookmarks ())
1671 (let ((inhibit-read-only t))
1672 (while (< (point) (point-max))
1673 (let ((bmrk (bookmark-bmenu-bookmark)))
1674 (setq bookmark-bmenu-hidden-bookmarks
1675 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1676 (let ((start (save-excursion (end-of-line) (point))))
1677 (move-to-column bookmark-bmenu-file-column t)
1678 ;; Strip off `mouse-face' from the white spaces region.
1679 (if (and (display-color-p) (display-mouse-p))
1680 (remove-text-properties start (point)
1681 '(mouse-face nil help-echo nil))))
1682 (delete-region (point) (progn (end-of-line) (point)))
1683 (insert " ")
1684 ;; Pass the NO-HISTORY arg:
1685 (bookmark-insert-location bmrk t)
1686 (forward-line 1))))))))
1689 (defun bookmark-bmenu-hide-filenames (&optional force)
1690 (if (and (not force) bookmark-bmenu-toggle-filenames)
1691 ;; nothing to hide if above is nil
1692 (save-excursion
1693 (save-window-excursion
1694 (goto-char (point-min))
1695 (forward-line 2)
1696 (setq bookmark-bmenu-hidden-bookmarks
1697 (nreverse bookmark-bmenu-hidden-bookmarks))
1698 (save-excursion
1699 (goto-char (point-min))
1700 (search-forward "Bookmark")
1701 (backward-word 1)
1702 (setq bookmark-bmenu-bookmark-column (current-column)))
1703 (save-excursion
1704 (let ((inhibit-read-only t))
1705 (while bookmark-bmenu-hidden-bookmarks
1706 (move-to-column bookmark-bmenu-bookmark-column t)
1707 (bookmark-kill-line)
1708 (let ((start (point)))
1709 (insert (car bookmark-bmenu-hidden-bookmarks))
1710 (if (and (display-color-p) (display-mouse-p))
1711 (add-text-properties
1712 start
1713 (save-excursion (re-search-backward
1714 "[^ \t]")
1715 (1+ (point)))
1716 '(mouse-face highlight
1717 follow-link t
1718 help-echo
1719 "mouse-2: go to this bookmark in other window"))))
1720 (setq bookmark-bmenu-hidden-bookmarks
1721 (cdr bookmark-bmenu-hidden-bookmarks))
1722 (forward-line 1))))))))
1725 (defun bookmark-bmenu-check-position ()
1726 ;; Returns non-nil if on a line with a bookmark.
1727 ;; (The actual value returned is bookmark-alist).
1728 ;; Else reposition and try again, else return nil.
1729 (cond ((< (count-lines (point-min) (point)) 2)
1730 (goto-char (point-min))
1731 (forward-line 2)
1732 bookmark-alist)
1733 ((and (bolp) (eobp))
1734 (beginning-of-line 0)
1735 bookmark-alist)
1737 bookmark-alist)))
1740 (defun bookmark-bmenu-bookmark ()
1741 ;; return a string which is bookmark of this line.
1742 (if (bookmark-bmenu-check-position)
1743 (save-excursion
1744 (save-window-excursion
1745 (goto-char (point-min))
1746 (search-forward "Bookmark")
1747 (backward-word 1)
1748 (setq bookmark-bmenu-bookmark-column (current-column)))))
1749 (if bookmark-bmenu-toggle-filenames
1750 (bookmark-bmenu-hide-filenames))
1751 (save-excursion
1752 (save-window-excursion
1753 (beginning-of-line)
1754 (forward-char bookmark-bmenu-bookmark-column)
1755 (prog1
1756 (buffer-substring-no-properties (point)
1757 (progn
1758 (end-of-line)
1759 (point)))
1760 ;; well, this is certainly crystal-clear:
1761 (if bookmark-bmenu-toggle-filenames
1762 (bookmark-bmenu-toggle-filenames t))))))
1765 (defun bookmark-show-annotation (bookmark)
1766 "Display the annotation for bookmark named BOOKMARK in a buffer,
1767 if an annotation exists."
1768 (let ((annotation (bookmark-get-annotation bookmark)))
1769 (if (and annotation (not (string-equal annotation "")))
1770 (save-excursion
1771 (let ((old-buf (current-buffer)))
1772 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1773 (delete-region (point-min) (point-max))
1774 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1775 (insert annotation)
1776 (goto-char (point-min))
1777 (pop-to-buffer old-buf))))))
1780 (defun bookmark-show-all-annotations ()
1781 "Display the annotations for all bookmarks in a buffer."
1782 (let ((old-buf (current-buffer)))
1783 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1784 (delete-region (point-min) (point-max))
1785 (mapcar
1786 (lambda (full-record)
1787 (let* ((name (bookmark-name-from-full-record full-record))
1788 (ann (bookmark-get-annotation name)))
1789 (insert (concat name ":\n"))
1790 (if (and ann (not (string-equal ann "")))
1791 ;; insert the annotation, indented by 4 spaces.
1792 (progn
1793 (save-excursion (insert ann))
1794 (while (< (point) (point-max))
1795 (beginning-of-line) ; paranoia
1796 (insert " ")
1797 (forward-line)
1798 (end-of-line))))))
1799 bookmark-alist)
1800 (goto-char (point-min))
1801 (pop-to-buffer old-buf)))
1804 (defun bookmark-bmenu-mark ()
1805 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1806 (interactive)
1807 (beginning-of-line)
1808 (if (bookmark-bmenu-check-position)
1809 (let ((inhibit-read-only t))
1810 (delete-char 1)
1811 (insert ?>)
1812 (forward-line 1)
1813 (bookmark-bmenu-check-position))))
1816 (defun bookmark-bmenu-select ()
1817 "Select this line's bookmark; also display bookmarks marked with `>'.
1818 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1819 (interactive)
1820 (if (bookmark-bmenu-check-position)
1821 (let ((bmrk (bookmark-bmenu-bookmark))
1822 (menu (current-buffer))
1823 (others ())
1824 tem)
1825 (goto-char (point-min))
1826 (while (re-search-forward "^>" nil t)
1827 (setq tem (bookmark-bmenu-bookmark))
1828 (let ((inhibit-read-only t))
1829 (delete-char -1)
1830 (insert ?\s))
1831 (or (string-equal tem bmrk)
1832 (member tem others)
1833 (setq others (cons tem others))))
1834 (setq others (nreverse others)
1835 tem (/ (1- (frame-height)) (1+ (length others))))
1836 (delete-other-windows)
1837 (bookmark-jump bmrk)
1838 (bury-buffer menu)
1839 (if others
1840 (while others
1841 (split-window nil tem)
1842 (other-window 1)
1843 (bookmark-jump (car others))
1844 (setq others (cdr others)))
1845 (other-window 1)))))
1848 (defun bookmark-bmenu-save (parg)
1849 "Save the current list into a bookmark file.
1850 With a prefix arg, prompts for a file to save them in."
1851 (interactive "P")
1852 (save-excursion
1853 (save-window-excursion
1854 (bookmark-save parg))))
1857 (defun bookmark-bmenu-load ()
1858 "Load the bookmark file and rebuild the bookmark menu-buffer."
1859 (interactive)
1860 (if (bookmark-bmenu-check-position)
1861 (save-excursion
1862 (save-window-excursion
1863 ;; This will call `bookmark-bmenu-list'
1864 (call-interactively 'bookmark-load)))))
1867 (defun bookmark-bmenu-1-window ()
1868 "Select this line's bookmark, alone, in full frame."
1869 (interactive)
1870 (if (bookmark-bmenu-check-position)
1871 (progn
1872 (bookmark-jump (bookmark-bmenu-bookmark))
1873 (bury-buffer (other-buffer))
1874 (delete-other-windows))))
1877 (defun bookmark-bmenu-2-window ()
1878 "Select this line's bookmark, with previous buffer in second window."
1879 (interactive)
1880 (if (bookmark-bmenu-check-position)
1881 (let ((bmrk (bookmark-bmenu-bookmark))
1882 (menu (current-buffer))
1883 (pop-up-windows t))
1884 (delete-other-windows)
1885 (switch-to-buffer (other-buffer))
1886 (let* ((pair (bookmark-jump-noselect bmrk))
1887 (buff (car pair))
1888 (pos (cdr pair)))
1889 (pop-to-buffer buff)
1890 (goto-char pos))
1891 (bury-buffer menu))))
1894 (defun bookmark-bmenu-this-window ()
1895 "Select this line's bookmark in this window."
1896 (interactive)
1897 (if (bookmark-bmenu-check-position)
1898 (bookmark-jump (bookmark-bmenu-bookmark))))
1901 (defun bookmark-bmenu-other-window ()
1902 "Select this line's bookmark in other window, leaving bookmark menu visible."
1903 (interactive)
1904 (let ((bookmark (bookmark-bmenu-bookmark)))
1905 (if (bookmark-bmenu-check-position)
1906 (let* ((pair (bookmark-jump-noselect bookmark))
1907 (buff (car pair))
1908 (pos (cdr pair)))
1909 (switch-to-buffer-other-window buff)
1910 (goto-char pos)
1911 (set-window-point (get-buffer-window buff) pos)
1912 (bookmark-show-annotation bookmark)))))
1915 (defun bookmark-bmenu-switch-other-window ()
1916 "Make the other window select this line's bookmark.
1917 The current window remains selected."
1918 (interactive)
1919 (let ((bookmark (bookmark-bmenu-bookmark))
1920 (pop-up-windows t)
1921 same-window-buffer-names
1922 same-window-regexps)
1923 (if (bookmark-bmenu-check-position)
1924 (let* ((pair (bookmark-jump-noselect bookmark))
1925 (buff (car pair))
1926 (pos (cdr pair)))
1927 (display-buffer buff)
1928 (let ((o-buffer (current-buffer)))
1929 ;; save-excursion won't do
1930 (set-buffer buff)
1931 (goto-char pos)
1932 (set-window-point (get-buffer-window buff) pos)
1933 (set-buffer o-buffer))
1934 (bookmark-show-annotation bookmark)))))
1936 (defun bookmark-bmenu-other-window-with-mouse (event)
1937 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1938 (interactive "e")
1939 (save-excursion
1940 (set-buffer (window-buffer (posn-window (event-end event))))
1941 (save-excursion
1942 (goto-char (posn-point (event-end event)))
1943 (bookmark-bmenu-other-window))))
1946 (defun bookmark-bmenu-show-annotation ()
1947 "Show the annotation for the current bookmark in another window."
1948 (interactive)
1949 (let ((bookmark (bookmark-bmenu-bookmark)))
1950 (if (bookmark-bmenu-check-position)
1951 (bookmark-show-annotation bookmark))))
1954 (defun bookmark-bmenu-show-all-annotations ()
1955 "Show the annotation for all bookmarks in another window."
1956 (interactive)
1957 (bookmark-show-all-annotations))
1960 (defun bookmark-bmenu-edit-annotation ()
1961 "Edit the annotation for the current bookmark in another window."
1962 (interactive)
1963 (let ((bookmark (bookmark-bmenu-bookmark)))
1964 (if (bookmark-bmenu-check-position)
1965 (bookmark-edit-annotation bookmark))))
1968 (defun bookmark-bmenu-unmark (&optional backup)
1969 "Cancel all requested operations on bookmark on this line and move down.
1970 Optional BACKUP means move up."
1971 (interactive "P")
1972 (beginning-of-line)
1973 (if (bookmark-bmenu-check-position)
1974 (progn
1975 (let ((inhibit-read-only t))
1976 (delete-char 1)
1977 ;; any flags to reset according to circumstances? How about a
1978 ;; flag indicating whether this bookmark is being visited?
1979 ;; well, we don't have this now, so maybe later.
1980 (insert " "))
1981 (forward-line (if backup -1 1))
1982 (bookmark-bmenu-check-position))))
1985 (defun bookmark-bmenu-backup-unmark ()
1986 "Move up and cancel all requested operations on bookmark on line above."
1987 (interactive)
1988 (forward-line -1)
1989 (if (bookmark-bmenu-check-position)
1990 (progn
1991 (bookmark-bmenu-unmark)
1992 (forward-line -1)
1993 (bookmark-bmenu-check-position))))
1996 (defun bookmark-bmenu-delete ()
1997 "Mark bookmark on this line to be deleted.
1998 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1999 (interactive)
2000 (beginning-of-line)
2001 (if (bookmark-bmenu-check-position)
2002 (let ((inhibit-read-only t))
2003 (delete-char 1)
2004 (insert ?D)
2005 (forward-line 1)
2006 (bookmark-bmenu-check-position))))
2009 (defun bookmark-bmenu-delete-backwards ()
2010 "Mark bookmark on this line to be deleted, then move up one line.
2011 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2012 (interactive)
2013 (bookmark-bmenu-delete)
2014 (forward-line -2)
2015 (if (bookmark-bmenu-check-position)
2016 (forward-line 1))
2017 (bookmark-bmenu-check-position))
2020 (defun bookmark-bmenu-execute-deletions ()
2021 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2022 (interactive)
2023 (message "Deleting bookmarks...")
2024 (let ((hide-em bookmark-bmenu-toggle-filenames)
2025 (o-point (point))
2026 (o-str (save-excursion
2027 (beginning-of-line)
2028 (if (looking-at "^D")
2030 (buffer-substring
2031 (point)
2032 (progn (end-of-line) (point))))))
2033 (o-col (current-column)))
2034 (if hide-em (bookmark-bmenu-hide-filenames))
2035 (setq bookmark-bmenu-toggle-filenames nil)
2036 (goto-char (point-min))
2037 (forward-line 1)
2038 (while (re-search-forward "^D" (point-max) t)
2039 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2040 (bookmark-bmenu-list)
2041 (setq bookmark-bmenu-toggle-filenames hide-em)
2042 (if bookmark-bmenu-toggle-filenames
2043 (bookmark-bmenu-toggle-filenames t))
2044 (if o-str
2045 (progn
2046 (goto-char (point-min))
2047 (search-forward o-str)
2048 (beginning-of-line)
2049 (forward-char o-col))
2050 (goto-char o-point))
2051 (beginning-of-line)
2052 (setq bookmark-alist-modification-count
2053 (1+ bookmark-alist-modification-count))
2054 (if (bookmark-time-to-save-p)
2055 (bookmark-save))
2056 (message "Deleting bookmarks...done")
2060 (defun bookmark-bmenu-rename ()
2061 "Rename bookmark on current line. Prompts for a new name."
2062 (interactive)
2063 (if (bookmark-bmenu-check-position)
2064 (let ((bmrk (bookmark-bmenu-bookmark))
2065 (thispoint (point)))
2066 (bookmark-rename bmrk)
2067 (bookmark-bmenu-list)
2068 (goto-char thispoint))))
2071 (defun bookmark-bmenu-locate ()
2072 "Display location of this bookmark. Displays in the minibuffer."
2073 (interactive)
2074 (if (bookmark-bmenu-check-position)
2075 (let ((bmrk (bookmark-bmenu-bookmark)))
2076 (message "%s" (bookmark-location bmrk)))))
2078 (defun bookmark-bmenu-relocate ()
2079 "Change the file path of the bookmark on the current line,
2080 prompting with completion for the new path."
2081 (interactive)
2082 (if (bookmark-bmenu-check-position)
2083 (let ((bmrk (bookmark-bmenu-bookmark))
2084 (thispoint (point)))
2085 (bookmark-relocate bmrk)
2086 (goto-char thispoint))))
2089 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2091 (defun bookmark-menu-popup-paned-menu (event name entries)
2092 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2093 That is, ENTRIES is a list of strings which appear as the choices
2094 in the menu.
2095 The number of panes depends on the number of entries.
2096 The visible entries are truncated to `bookmark-menu-length', but the
2097 strings returned are not."
2098 (let ((f-height (/ (frame-height) 2))
2099 (pane-list nil)
2100 (iter 0))
2101 (while entries
2102 (let (lst
2103 (count 0))
2104 (while (and (< count f-height) entries)
2105 (let ((str (car entries)))
2106 (push (cons
2107 (if (> (length str) bookmark-menu-length)
2108 (substring str 0 bookmark-menu-length)
2109 str)
2110 str)
2111 lst)
2112 (setq entries (cdr entries))
2113 (setq count (1+ count))))
2114 (setq iter (1+ iter))
2115 (push (cons
2116 (format "-*- %s (%d) -*-" name iter)
2117 (nreverse lst))
2118 pane-list)))
2120 ;; Popup the menu and return the string.
2121 (x-popup-menu event (cons (concat "-*- " name " -*-")
2122 (nreverse pane-list)))))
2125 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2126 ;; following works, and for explaining what to do to make it work.
2128 ;; We MUST autoload EACH form used to set up this variable's value, so
2129 ;; that the whole job is done in loaddefs.el.
2131 ;; Emacs menubar stuff.
2133 ;;;###autoload
2134 (defvar menu-bar-bookmark-map
2135 (let ((map (make-sparse-keymap "Bookmark functions")))
2136 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2137 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2138 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2139 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2140 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2141 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2142 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2143 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2144 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2145 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2146 map))
2148 ;;;###autoload
2149 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2151 ;; make bookmarks appear toward the right side of the menu.
2152 (if (boundp 'menu-bar-final-items)
2153 (if menu-bar-final-items
2154 (setq menu-bar-final-items
2155 (cons 'bookmark menu-bar-final-items)))
2156 (setq menu-bar-final-items '(bookmark)))
2158 ;;;; end bookmark menu stuff ;;;;
2161 ;;; Load Hook
2162 (defvar bookmark-load-hook nil
2163 "Hook run at the end of loading bookmark.")
2165 ;;; Exit Hook, called from kill-emacs-hook
2166 (defvar bookmark-exit-hook nil
2167 "Hook run when Emacs exits.")
2169 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2171 (defun bookmark-exit-hook-internal ()
2172 "Save bookmark state, if necessary, at Emacs exit time.
2173 This also runs `bookmark-exit-hook'."
2174 (run-hooks 'bookmark-exit-hook)
2175 (and bookmark-alist
2176 (bookmark-time-to-save-p t)
2177 (bookmark-save)))
2179 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2182 (run-hooks 'bookmark-load-hook)
2184 (provide 'bookmark)
2186 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2187 ;;; bookmark.el ends here