Update copyright. Improve `revision' info.
[emacs.git] / lisp / bookmark.el
blob97217a10e79695798b81dc46859ae6975b432f05
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001 Free Software Foundation
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This package is for setting "bookmarks" in files. A bookmark
30 ;; associates a string with a location in a certain file. Thus, you
31 ;; can navigate your way to that location by providing the string.
32 ;; See the "User Variables" section for customizations.
34 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
35 ;; then implementing the bookmark-current-bookmark idea. He even
36 ;; sent *patches*, bless his soul...
38 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
39 ;; fixing and improving bookmark-time-to-save-p.
41 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
42 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
43 ;; and the menu-bar).
45 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
46 ;; suggestions and the code to implement them (like
47 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
48 ;; stuff).
50 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
51 ;; for his eminently sensible suggestion to separate bookmark-jump
52 ;; into bookmark-jump and bookmark-jump-noselect, which made many
53 ;; other things cleaner as well.
55 ;; Thanks to Roland McGrath for encouragement and help with defining
56 ;; autoloads on the menu-bar.
58 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
59 ;; values in bookmark-jump and bookmark-set. Everybody please keep
60 ;; all the keystrokes they save thereby and send them to him at the
61 ;; end of each year :-) (No, seriously, thanks Jonathan!)
63 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
64 ;; thinking up the annotations feature and implementing it so well.
66 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
67 ;; <olstad@msc.edu>.
69 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
70 ;; reported and fixed.
72 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
74 ;; Enough with the credits already, get on to the good stuff:
76 ;; FAVORITE CHINESE RESTAURANT:
77 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
78 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
80 ;;; Code:
82 (require 'pp)
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "*If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom bookmark-save-flag t
108 "*Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "*The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "*File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "*Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "*Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "*Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "*nil means don't show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "*Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "*Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "*Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'boolean)
200 ;;; No user-serviceable parts beyond this point.
202 ;; Is it XEmacs?
203 (defconst bookmark-xemacsp
204 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
207 ;; Added for lucid emacs compatibility, db
208 (or (fboundp 'defalias) (fset 'defalias 'fset))
210 ;; suggested for lucid compatibility by david hughes:
211 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
213 ;; This variable is probably obsolete now...
214 (or (boundp 'baud-rate)
215 ;; some random value higher than 9600
216 (setq baud-rate 19200))
220 ;;; Keymap stuff:
222 ;; Set up these bindings dumping time *only*;
223 ;; if the user alters them, don't override the user when loading bookmark.el.
225 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
226 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
227 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
229 ;;;###autoload
230 (defvar bookmark-map nil
231 "Keymap containing bindings to bookmark functions.
232 It is not bound to any key by default: to bind it
233 so that you have a bookmark prefix, just use `global-set-key' and bind a
234 key of your choice to `bookmark-map'. All interactive bookmark
235 functions have a binding in this keymap.")
237 ;;;###autoload
238 (define-prefix-command 'bookmark-map)
240 ;; Read the help on all of these functions for details...
241 ;;;###autoload
242 (define-key bookmark-map "x" 'bookmark-set)
243 ;;;###autoload
244 (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
245 ;;;###autoload
246 (define-key bookmark-map "j" 'bookmark-jump)
247 ;;;###autoload
248 (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
249 ;;;###autoload
250 (define-key bookmark-map "i" 'bookmark-insert)
251 ;;;###autoload
252 (define-key bookmark-map "e" 'edit-bookmarks)
253 ;;;###autoload
254 (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
255 ;;;###autoload
256 (define-key bookmark-map "r" 'bookmark-rename)
257 ;;;###autoload
258 (define-key bookmark-map "d" 'bookmark-delete)
259 ;;;###autoload
260 (define-key bookmark-map "l" 'bookmark-load)
261 ;;;###autoload
262 (define-key bookmark-map "w" 'bookmark-write)
263 ;;;###autoload
264 (define-key bookmark-map "s" 'bookmark-save)
267 ;;; The annotation maps.
268 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
269 "Keymap for composing an annotation for a bookmark.")
271 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
272 'bookmark-send-annotation)
276 ;;; Core variables and data structures:
277 (defvar bookmark-alist ()
278 "Association list of bookmarks and their records.
279 You probably don't want to change the value of this alist yourself;
280 instead, let the various bookmark functions do it for you.
282 The format of the alist is
284 \(BOOKMARK1 BOOKMARK2 ...\)
286 where each BOOKMARK is of the form
288 \(NAME
289 \(filename . FILE\)
290 \(front-context-string . FRONT-STR\)
291 \(rear-context-string . REAR-STR\)
292 \(position . POS\)
293 \(info-node . POS\)
294 \(annotation . ANNOTATION\)\)
296 So the cdr of each bookmark is an alist too.
297 `info-node' is optional, by the way.")
300 (defvar bookmarks-already-loaded nil)
303 ;; more stuff added by db.
305 (defvar bookmark-current-bookmark nil
306 "Name of bookmark most recently used in the current file.
307 It is buffer local, used to make moving a bookmark forward
308 through a file easier.")
310 (make-variable-buffer-local 'bookmark-current-bookmark)
313 (defvar bookmark-alist-modification-count 0
314 "Number of modifications to bookmark list since it was last saved.")
317 (defvar bookmark-search-size 16
318 "Length of the context strings recorded on either side of a bookmark.")
321 (defvar bookmark-current-point 0)
322 (defvar bookmark-yank-point 0)
323 (defvar bookmark-current-buffer nil)
327 ;; Helper functions.
329 ;; Only functions on this page and the next one (file formats) need to
330 ;; know anything about the format of bookmark-alist entries.
331 ;; Everyone else should go through them.
333 (defun bookmark-name-from-full-record (full-record)
334 "Return name of FULL-RECORD \(an alist element instead of a string\)."
335 (car full-record))
338 (defun bookmark-all-names ()
339 "Return a list of all current bookmark names."
340 (bookmark-maybe-load-default-file)
341 (mapcar
342 (lambda (full-record)
343 (bookmark-name-from-full-record full-record))
344 bookmark-alist))
347 (defun bookmark-get-bookmark (bookmark)
348 "Return the full entry for BOOKMARK in bookmark-alist.
349 If BOOKMARK is not a string, return nil."
350 (when (stringp bookmark)
351 (apply (if bookmark-completion-ignore-case
352 #'assoc-ignore-case
353 #'assoc)
354 (list bookmark bookmark-alist))))
357 (defun bookmark-get-bookmark-record (bookmark)
358 "Return the guts of the entry for BOOKMARK in bookmark-alist.
359 That is, all information but the name."
360 (car (cdr (bookmark-get-bookmark bookmark))))
363 (defun bookmark-set-name (bookmark newname)
364 "Set BOOKMARK's name to NEWNAME."
365 (setcar
366 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
367 newname))
370 (defun bookmark-get-annotation (bookmark)
371 "Return the annotation of BOOKMARK, or nil if none."
372 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
375 (defun bookmark-set-annotation (bookmark ann)
376 "Set the annotation of BOOKMARK to ANN."
377 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
378 (if cell
379 (setcdr cell ann)
380 (nconc (bookmark-get-bookmark-record bookmark)
381 (list (cons 'annotation ann))))))
384 (defun bookmark-get-filename (bookmark)
385 "Return the full filename of BOOKMARK."
386 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
389 (defun bookmark-set-filename (bookmark filename)
390 "Set the full filename of BOOKMARK to FILENAME."
391 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
392 (if cell
393 (setcdr cell filename)
394 (nconc (bookmark-get-bookmark-record bookmark)
395 (list (cons 'filename filename))))))
398 (defun bookmark-get-position (bookmark)
399 "Return the position \(i.e.: point\) of BOOKMARK."
400 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
403 (defun bookmark-set-position (bookmark position)
404 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
405 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
406 (if cell
407 (setcdr cell position)
408 (nconc (bookmark-get-bookmark-record bookmark)
409 (list (cons 'position position))))))
412 (defun bookmark-get-front-context-string (bookmark)
413 "Return the front-context-string of BOOKMARK."
414 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
417 (defun bookmark-set-front-context-string (bookmark string)
418 "Set the front-context-string of BOOKMARK to STRING."
419 (let ((cell (assq 'front-context-string
420 (bookmark-get-bookmark-record bookmark))))
421 (if cell
422 (setcdr cell string)
423 (nconc (bookmark-get-bookmark-record bookmark)
424 (list (cons 'front-context-string string))))))
427 (defun bookmark-get-rear-context-string (bookmark)
428 "Return the rear-context-string of BOOKMARK."
429 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
432 (defun bookmark-set-rear-context-string (bookmark string)
433 "Set the rear-context-string of BOOKMARK to STRING."
434 (let ((cell (assq 'rear-context-string
435 (bookmark-get-bookmark-record bookmark))))
436 (if cell
437 (setcdr cell string)
438 (nconc (bookmark-get-bookmark-record bookmark)
439 (list (cons 'rear-context-string string))))))
442 (defun bookmark-get-info-node (bookmark)
443 "Get the info node associated with BOOKMARK."
444 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
447 (defun bookmark-set-info-node (bookmark node)
448 "Set the Info node of BOOKMARK to NODE."
449 (let ((cell (assq 'info-node
450 (bookmark-get-bookmark-record bookmark))))
451 (if cell
452 (setcdr cell node)
453 (nconc (bookmark-get-bookmark-record bookmark)
454 (list (cons 'info-node node)))))
456 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
457 (sit-for 4)
461 (defvar bookmark-history nil
462 "The history list for bookmark functions.")
465 (defun bookmark-completing-read (prompt &optional default)
466 "Prompting with PROMPT, read a bookmark name in completion.
467 PROMPT will get a \": \" stuck on the end no matter what, so you
468 probably don't want to include one yourself.
469 Optional second arg DEFAULT is a string to return if the user enters
470 the empty string."
471 (bookmark-maybe-load-default-file) ; paranoia
472 (let* ((completion-ignore-case bookmark-completion-ignore-case)
473 (default default)
474 (prompt (if default
475 (concat prompt (format " (%s): " default))
476 (concat prompt ": ")))
477 (str
478 (completing-read prompt
479 bookmark-alist
483 'bookmark-history)))
484 (if (string-equal "" str)
485 (list default)
486 (list str))))
489 (defmacro bookmark-maybe-historicize-string (string)
490 "Put STRING into the bookmark prompt history, if caller non-interactive.
491 We need this because sometimes bookmark functions are invoked from
492 menus, so `completing-read' never gets a chance to set `bookmark-history'."
493 `(or
494 (interactive-p)
495 (setq bookmark-history (cons ,string bookmark-history))))
498 (defun bookmark-make (name &optional annotation overwrite info-node)
499 "Make a bookmark named NAME.
500 Optional second arg ANNOTATION gives it an annotation.
501 Optional third arg OVERWRITE means replace any existing bookmarks with
502 this name.
503 Optional fourth arg INFO-NODE means this bookmark is at info node
504 INFO-NODE, so record this fact in the bookmark's entry."
505 (bookmark-maybe-load-default-file)
506 (let ((stripped-name (copy-sequence name)))
507 (or bookmark-xemacsp
508 ;; XEmacs's `set-text-properties' doesn't work on
509 ;; free-standing strings, apparently.
510 (set-text-properties 0 (length stripped-name) nil stripped-name))
511 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
512 ;; already existing bookmark under that name and
513 ;; no prefix arg means just overwrite old bookmark
514 (setcdr (bookmark-get-bookmark stripped-name)
515 (list (bookmark-make-cell annotation info-node)))
517 ;; otherwise just cons it onto the front (either the bookmark
518 ;; doesn't exist already, or there is no prefix arg. In either
519 ;; case, we want the new bookmark consed onto the alist...)
521 (setq bookmark-alist
522 (cons
523 (list stripped-name
524 (bookmark-make-cell annotation info-node))
525 bookmark-alist)))
527 ;; Added by db
528 (setq bookmark-current-bookmark stripped-name)
529 (setq bookmark-alist-modification-count
530 (1+ bookmark-alist-modification-count))
531 (if (bookmark-time-to-save-p)
532 (bookmark-save))))
535 (defun bookmark-make-cell (annotation &optional info-node)
536 "Return the record part of a new bookmark, given ANNOTATION.
537 Must be at the correct position in the buffer in which the bookmark is
538 being set. This might change someday.
539 Optional second arg INFO-NODE means this bookmark is at info node
540 INFO-NODE, so record this fact in the bookmark's entry."
541 (let ((the-record
542 `((filename . ,(bookmark-buffer-file-name))
543 (front-context-string
544 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
545 (buffer-substring-no-properties
546 (point)
547 (+ (point) bookmark-search-size))
548 nil))
549 (rear-context-string
550 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
551 (buffer-substring-no-properties
552 (point)
553 (- (point) bookmark-search-size))
554 nil))
555 (position . ,(point)))))
557 ;; Now fill in the optional parts:
559 ;; Take no chances with text properties
560 (set-text-properties 0 (length annotation) nil annotation)
561 (set-text-properties 0 (length info-node) nil info-node)
563 (if annotation
564 (nconc the-record (list (cons 'annotation annotation))))
565 (if info-node
566 (nconc the-record (list (cons 'info-node info-node))))
568 ;; Finally, return the completed record.
569 the-record))
573 ;;; File format stuff
575 ;; The OLD format of the bookmark-alist was:
577 ;; ((bookmark-name (filename
578 ;; string-in-front
579 ;; string-behind
580 ;; point))
581 ;; ...)
583 ;; The NEW format of the bookmark-alist is:
585 ;; ((bookmark-name ((filename . FILENAME)
586 ;; (front-context-string . string-in-front)
587 ;; (rear-context-string . string-behind)
588 ;; (position . POINT)
589 ;; (annotation . annotation)
590 ;; (whatever . VALUE)
591 ;; ...
592 ;; ))
593 ;; ...)
596 ;; I switched to using an internal as well as external alist because I
597 ;; felt that would be a more flexible framework in which to add
598 ;; features. It means that the order in which values appear doesn't
599 ;; matter, and it means that arbitrary values can be added without
600 ;; risk of interfering with existing ones.
602 ;; BOOKMARK-NAME is the string the user gives the bookmark and
603 ;; accesses it by from then on.
605 ;; FILENAME is the location of the file in which the bookmark is set.
607 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
608 ;; context in front of the point at which the bookmark is set.
610 ;; STRING-BEHIND is the same thing, but after the point.
612 ;; The context strings exist so that modifications to a file don't
613 ;; necessarily cause a bookmark's position to be invalidated.
614 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
615 ;; case the file has changed since the bookmark was set. It will
616 ;; attempt to place the user before the changes, if there were any.
617 ;; ANNOTATION is the annotation for the bookmark; it may not exist
618 ;; (for backward compatibility), be nil (no annotation), or be a
619 ;; string.
622 (defconst bookmark-file-format-version 1
623 "The current version of the format used by bookmark files.
624 You should never need to change this.")
627 (defconst bookmark-end-of-version-stamp-marker
628 "-*- End Of Bookmark File Format Version Stamp -*-\n"
629 "This string marks the end of the version stamp in a bookmark file.")
632 (defun bookmark-alist-from-buffer ()
633 "Return a bookmark-alist (in any format) from the current buffer.
634 The buffer must of course contain bookmark format information.
635 Does not care from where in the buffer it is called, and does not
636 affect point."
637 (save-excursion
638 (goto-char (point-min))
639 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
640 (read (current-buffer))
641 ;; Else we're dealing with format version 0
642 (if (search-forward "(" nil t)
643 (progn
644 (forward-char -1)
645 (read (current-buffer)))
646 ;; Else no hope of getting information here.
647 (error "Not bookmark format")))))
650 (defun bookmark-upgrade-version-0-alist (old-list)
651 "Upgrade a version 0 alist OLD-LIST to the current version."
652 (mapcar
653 (lambda (bookmark)
654 (let* ((name (car bookmark))
655 (record (car (cdr bookmark)))
656 (filename (nth 0 record))
657 (front-str (nth 1 record))
658 (rear-str (nth 2 record))
659 (position (nth 3 record))
660 (ann (nth 4 record)))
661 (list
662 name
663 `((filename . ,filename)
664 (front-context-string . ,(or front-str ""))
665 (rear-context-string . ,(or rear-str ""))
666 (position . ,position)
667 (annotation . ,ann)))))
668 old-list))
671 (defun bookmark-upgrade-file-format-from-0 ()
672 "Upgrade a bookmark file of format 0 (the original format) to format 1.
673 This expects to be called from point-min in a bookmark file."
674 (message "Upgrading bookmark format from 0 to %d..."
675 bookmark-file-format-version)
676 (let* ((old-list (bookmark-alist-from-buffer))
677 (new-list (bookmark-upgrade-version-0-alist old-list)))
678 (delete-region (point-min) (point-max))
679 (bookmark-insert-file-format-version-stamp)
680 (pp new-list (current-buffer))
681 (save-buffer))
682 (goto-char (point-min))
683 (message "Upgrading bookmark format from 0 to %d...done"
684 bookmark-file-format-version)
688 (defun bookmark-grok-file-format-version ()
689 "Return an integer which is the file-format version of this bookmark file.
690 This expects to be called from point-min in a bookmark file."
691 (if (looking-at "^;;;;")
692 (save-excursion
693 (save-match-data
694 (re-search-forward "[0-9]")
695 (forward-char -1)
696 (read (current-buffer))))
697 ;; Else this is format version 0, the original one, which didn't
698 ;; even have version stamps.
702 (defun bookmark-maybe-upgrade-file-format ()
703 "Check the file-format version of this bookmark file.
704 If the version is not up-to-date, upgrade it automatically.
705 This expects to be called from point-min in a bookmark file."
706 (let ((version (bookmark-grok-file-format-version)))
707 (cond
708 ((= version bookmark-file-format-version)
709 ) ; home free -- version is current
710 ((= version 0)
711 (bookmark-upgrade-file-format-from-0))
713 (error "Bookmark file format version strangeness")))))
716 (defun bookmark-insert-file-format-version-stamp ()
717 "Insert text indicating current version of bookmark file format."
718 (insert
719 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
720 bookmark-file-format-version))
721 (insert ";;; This format is meant to be slightly human-readable;\n"
722 ";;; nevertheless, you probably don't want to edit it.\n"
723 ";;; "
724 bookmark-end-of-version-stamp-marker))
727 ;;; end file-format stuff
730 ;;; Core code:
732 ;;;###autoload
733 (defun bookmark-set (&optional name parg)
734 "Set a bookmark named NAME inside a file.
735 If name is nil, then the user will be prompted.
736 With prefix arg, will not overwrite a bookmark that has the same name
737 as NAME if such a bookmark already exists, but instead will \"push\"
738 the new bookmark onto the bookmark alist. Thus the most recently set
739 bookmark with name NAME would be the one in effect at any given time,
740 but the others are still there, should you decide to delete the most
741 recent one.
743 To yank words from the text of the buffer and use them as part of the
744 bookmark name, type C-w while setting a bookmark. Successive C-w's
745 yank successive words.
747 Typing C-u inserts the name of the last bookmark used in the buffer
748 \(as an aid in using a single bookmark name to track your progress
749 through a large file\). If no bookmark was used, then C-u inserts the
750 name of the file being visited.
752 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
753 and it removes only the first instance of a bookmark with that name from
754 the list of bookmarks.\)"
755 (interactive (list nil current-prefix-arg))
757 (bookmark-buffer-file-name)
758 (error "Buffer not visiting a file or directory"))
760 (bookmark-maybe-load-default-file)
762 (setq bookmark-current-point (point))
763 (setq bookmark-yank-point (point))
764 (setq bookmark-current-buffer (current-buffer))
766 (let* ((default (or bookmark-current-bookmark
767 (bookmark-buffer-name)))
768 (str
769 (or name
770 (read-from-minibuffer
771 (format "Set bookmark (%s): " default)
773 (let ((now-map (copy-keymap minibuffer-local-map)))
774 (define-key now-map "\C-w" 'bookmark-yank-word)
775 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
776 now-map))))
777 (annotation nil))
778 (and (string-equal str "") (setq str default))
779 ;; Ask for an annotation buffer for this bookmark
780 (if bookmark-use-annotations
781 (bookmark-read-annotation parg str)
782 (bookmark-make str annotation parg (bookmark-info-current-node))
783 (setq bookmark-current-bookmark str)
784 (bookmark-bmenu-surreptitiously-rebuild-list)
785 (goto-char bookmark-current-point))))
788 (defun bookmark-info-current-node ()
789 "If in Info-mode, return current node name (a string), else nil."
790 (if (eq major-mode 'Info-mode)
791 Info-current-node))
794 (defun bookmark-kill-line (&optional newline-too)
795 "Kill from point to end of line.
796 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
797 Does not affect the kill-ring."
798 (let ((eol (save-excursion (end-of-line) (point))))
799 (delete-region (point) eol)
800 (if (and newline-too (looking-at "\n"))
801 (delete-char 1))))
804 ;; Defvars to avoid compilation warnings:
805 (defvar bookmark-annotation-paragraph nil)
806 (defvar bookmark-annotation-name nil)
807 (defvar bookmark-annotation-buffer nil)
808 (defvar bookmark-annotation-file nil)
809 (defvar bookmark-annotation-point nil)
812 (defun bookmark-send-annotation ()
813 "Use buffer contents as the annotation for a bookmark.
814 Exclude lines that begin with `#'.
815 Store the annotation text in the bookmark list with
816 the bookmark (and file, and point) specified in buffer local variables."
817 (interactive)
818 (if (not (eq major-mode 'bookmark-read-annotation-mode))
819 (error "Not in bookmark-read-annotation-mode"))
820 (goto-char (point-min))
821 (while (< (point) (point-max))
822 (if (looking-at "^#")
823 (bookmark-kill-line t)
824 (forward-line 1)))
825 (let ((annotation (buffer-string))
826 (parg bookmark-annotation-paragraph)
827 (bookmark bookmark-annotation-name)
828 (pt bookmark-annotation-point)
829 (buf bookmark-annotation-buffer))
830 ;; for bookmark-make-cell to work, we need to be
831 ;; in the relevant buffer, at the relevant point.
832 ;; Actually, bookmark-make-cell should probably be re-written,
833 ;; to avoid this need. Should I handle the error if a buffer is
834 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
835 (save-excursion
836 (pop-to-buffer buf)
837 (goto-char pt)
838 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
839 (setq bookmark-current-bookmark bookmark))
840 (bookmark-bmenu-surreptitiously-rebuild-list)
841 (goto-char bookmark-current-point))
842 (kill-buffer (current-buffer)))
845 (defun bookmark-default-annotation-text (bookmark)
846 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
847 "# All lines which start with a '#' will be deleted.\n"
848 "# Type C-c C-c when done.\n#\n"
849 "# Author: " (user-full-name) " <" (user-login-name) "@"
850 (system-name) ">\n"
851 "# Date: " (current-time-string) "\n"))
854 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
855 "Function to return default text to use for a bookmark annotation.
856 It takes the name of the bookmark, as a string, as an arg.")
858 (defun bookmark-read-annotation-mode (buf point parg bookmark)
859 "Mode for composing annotations for a bookmark.
860 Wants BUF POINT PARG and BOOKMARK.
861 When you have finished composing, type \\[bookmark-send-annotation] to send
862 the annotation.
864 \\{bookmark-read-annotation-mode-map}
866 (interactive)
867 (kill-all-local-variables)
868 (make-local-variable 'bookmark-annotation-paragraph)
869 (make-local-variable 'bookmark-annotation-name)
870 (make-local-variable 'bookmark-annotation-buffer)
871 (make-local-variable 'bookmark-annotation-file)
872 (make-local-variable 'bookmark-annotation-point)
873 (setq bookmark-annotation-paragraph parg)
874 (setq bookmark-annotation-name bookmark)
875 (setq bookmark-annotation-buffer buf)
876 (setq bookmark-annotation-file (buffer-file-name buf))
877 (setq bookmark-annotation-point point)
878 (use-local-map bookmark-read-annotation-mode-map)
879 (setq major-mode 'bookmark-read-annotation-mode)
880 (insert (funcall bookmark-read-annotation-text-func bookmark))
881 (run-hooks 'text-mode-hook))
884 (defun bookmark-read-annotation (parg bookmark)
885 "Pop up a buffer for entering a bookmark annotation.
886 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
887 (let ((buf (current-buffer))
888 (point (point)))
889 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
890 (bookmark-read-annotation-mode buf point parg bookmark)))
893 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
894 "Keymap for editing an annotation of a bookmark.")
897 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
898 'bookmark-send-edited-annotation)
901 (defun bookmark-edit-annotation-mode (bookmark)
902 "Mode for editing the annotation of bookmark BOOKMARK.
903 When you have finished composing, type \\[bookmark-send-annotation].
905 \\{bookmark-edit-annotation-mode-map}
907 (interactive)
908 (kill-all-local-variables)
909 (make-local-variable 'bookmark-annotation-name)
910 (setq bookmark-annotation-name bookmark)
911 (use-local-map bookmark-edit-annotation-mode-map)
912 (setq major-mode 'bookmark-edit-annotation-mode)
913 (insert (funcall bookmark-read-annotation-text-func bookmark))
914 (let ((annotation (bookmark-get-annotation bookmark)))
915 (if (and annotation (not (string-equal annotation "")))
916 (insert annotation)))
917 (run-hooks 'text-mode-hook))
920 (defun bookmark-send-edited-annotation ()
921 "Use buffer contents (minus beginning with `#' as annotation for a bookmark."
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 (let ((buf (current-buffer))
941 (point (point)))
942 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
943 (bookmark-edit-annotation-mode bookmark)))
946 (defun bookmark-insert-current-bookmark ()
947 "Insert this buffer's value of bookmark-current-bookmark.
948 Default to file name if it's nil."
949 (interactive)
950 (let ((str
951 (save-excursion
952 (set-buffer bookmark-current-buffer)
953 bookmark-current-bookmark)))
954 (if str (insert str) (bookmark-insert-buffer-name))))
957 (defun bookmark-insert-buffer-name ()
958 "Insert the current file name into the bookmark name being set.
959 The directory part of the file name is not used."
960 (interactive)
961 (let ((str
962 (save-excursion
963 (set-buffer bookmark-current-buffer)
964 (bookmark-buffer-name))))
965 (insert str)))
968 (defun bookmark-buffer-name ()
969 "Return the name of the current buffer's file, non-directory.
970 In Info, return the current node."
971 (cond
972 ;; Are we in Info?
973 ((string-equal mode-name "Info") Info-current-node)
974 ;; Or are we a file?
975 (buffer-file-name (file-name-nondirectory buffer-file-name))
976 ;; Or are we a directory?
977 ((and (boundp 'dired-directory) dired-directory)
978 (let* ((dirname (if (stringp dired-directory)
979 dired-directory
980 (car dired-directory)))
981 (idx (1- (length dirname))))
982 ;; Strip the trailing slash.
983 (if (= ?/ (aref dirname idx))
984 (file-name-nondirectory (substring dirname 0 idx))
985 ;; Else return the current-buffer
986 (buffer-name (current-buffer)))))
987 ;; If all else fails, use the buffer's name.
989 (buffer-name (current-buffer)))))
992 (defun bookmark-yank-word ()
993 (interactive)
994 ;; get the next word from the buffer and append it to the name of
995 ;; the bookmark currently being set.
996 (let ((string (save-excursion
997 (set-buffer bookmark-current-buffer)
998 (goto-char bookmark-yank-point)
999 (buffer-substring-no-properties
1000 (point)
1001 (progn
1002 (forward-word 1)
1003 (setq bookmark-yank-point (point)))))))
1004 (insert string)))
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 ;;;###autoload
1050 (defun bookmark-jump (bookmark)
1051 "Jump to bookmark BOOKMARK (a point in some file).
1052 You may have a problem using this function if the value of variable
1053 `bookmark-alist' is nil. If that happens, you need to load in some
1054 bookmarks. See help on function `bookmark-load' for more about
1055 this.
1057 If the file pointed to by BOOKMARK no longer exists, you will be asked
1058 if you wish to give the bookmark a new location, and bookmark-jump
1059 will then jump to the new location, as well as recording it in place
1060 of the old one in the permanent bookmark record."
1061 (interactive
1062 (bookmark-completing-read "Jump to bookmark" bookmark-current-bookmark))
1063 (bookmark-maybe-historicize-string bookmark)
1064 (let ((cell (bookmark-jump-noselect bookmark)))
1065 (and cell
1066 (switch-to-buffer (car cell))
1067 (goto-char (cdr cell))
1068 (if bookmark-automatically-show-annotations
1069 ;; if there is an annotation for this bookmark,
1070 ;; show it in a buffer.
1071 (bookmark-show-annotation bookmark)))))
1074 (defun bookmark-file-or-variation-thereof (file)
1075 "Return FILE (a string) if it exists, or return a reasonable
1076 variation of FILE if that exists. Reasonable variations are checked
1077 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1078 nor a reasonable variation thereof, then still return FILE if it can
1079 be retrieved from a VC backend, else return nil."
1080 (if (file-exists-p file)
1081 file
1083 (progn (require 'info) ; ensure Info-suffix-list is bound
1084 (catch 'found
1085 (mapc (lambda (elt)
1086 (let ((suffixed-file (concat file (car elt))))
1087 (if (file-exists-p suffixed-file)
1088 (throw 'found suffixed-file))))
1089 Info-suffix-list)
1090 nil))
1091 ;; Last possibility: try VC
1092 (if (vc-backend file) file))))
1095 (defun bookmark-jump-noselect (str)
1096 ;; a leetle helper for bookmark-jump :-)
1097 ;; returns (BUFFER . POINT)
1098 (bookmark-maybe-load-default-file)
1099 (let* ((file (expand-file-name (bookmark-get-filename str)))
1100 (forward-str (bookmark-get-front-context-string str))
1101 (behind-str (bookmark-get-rear-context-string str))
1102 (place (bookmark-get-position str))
1103 (info-node (bookmark-get-info-node str))
1104 (orig-file file)
1106 (if (setq file (bookmark-file-or-variation-thereof file))
1107 (save-excursion
1108 (save-window-excursion
1109 (if info-node
1110 ;; Info nodes must be visited with care.
1111 (progn
1112 (require 'info)
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 (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 (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 (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 (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 (bookmark-completing-read "Delete bookmark" bookmark-current-bookmark))
1264 (bookmark-maybe-historicize-string bookmark)
1265 (bookmark-maybe-load-default-file)
1266 (let ((will-go (bookmark-get-bookmark bookmark)))
1267 (setq bookmark-alist (delq will-go bookmark-alist))
1268 ;; Added by db, nil bookmark-current-bookmark if the last
1269 ;; occurrence has been deleted
1270 (or (bookmark-get-bookmark bookmark-current-bookmark)
1271 (setq bookmark-current-bookmark nil)))
1272 ;; Don't rebuild the list
1273 (if batch
1275 (bookmark-bmenu-surreptitiously-rebuild-list)
1276 (setq bookmark-alist-modification-count
1277 (1+ bookmark-alist-modification-count))
1278 (if (bookmark-time-to-save-p)
1279 (bookmark-save))))
1282 (defun bookmark-time-to-save-p (&optional last-time)
1283 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1284 ;; finds out whether it's time to save bookmarks to a file, by
1285 ;; examining the value of variable bookmark-save-flag, and maybe
1286 ;; bookmark-alist-modification-count. Returns t if they should be
1287 ;; saved, nil otherwise. if last-time is non-nil, then this is
1288 ;; being called when emacs is killed.
1289 (cond (last-time
1290 (and (> bookmark-alist-modification-count 0)
1291 bookmark-save-flag))
1292 ((numberp bookmark-save-flag)
1293 (>= bookmark-alist-modification-count bookmark-save-flag))
1295 nil)))
1298 ;;;###autoload
1299 (defun bookmark-write ()
1300 "Write bookmarks to a file (reading the file name with the minibuffer).
1301 Don't use this in Lisp programs; use `bookmark-save' instead."
1302 (interactive)
1303 (bookmark-maybe-load-default-file)
1304 (bookmark-save t))
1307 ;;;###autoload
1308 (defun bookmark-save (&optional parg file)
1309 "Save currently defined bookmarks.
1310 Saves by default in the file defined by the variable
1311 `bookmark-default-file'. With a prefix arg, save it in file FILE
1312 \(second argument\).
1314 If you are calling this from Lisp, the two arguments are PREFIX-ARG
1315 and FILE, and if you just want it to write to the default file, then
1316 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1317 instead. If you pass in one argument, and it is non-nil, then the
1318 user will be interactively queried for a file to save in.
1320 When you want to load in the bookmarks from a file, use
1321 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1322 for a file, defaulting to the file defined by variable
1323 `bookmark-default-file'."
1324 (interactive "P")
1325 (bookmark-maybe-load-default-file)
1326 (cond
1327 ((and (null parg) (null file))
1328 ;;whether interactive or not, write to default file
1329 (bookmark-write-file bookmark-default-file))
1330 ((and (null parg) file)
1331 ;;whether interactive or not, write to given file
1332 (bookmark-write-file file))
1333 ((and parg (not file))
1334 ;;have been called interactively w/ prefix arg
1335 (let ((file (read-file-name "File to save bookmarks in: ")))
1336 (bookmark-write-file file)))
1337 (t ; someone called us with prefix-arg *and* a file, so just write to file
1338 (bookmark-write-file file)))
1339 ;; signal that we have synced the bookmark file by setting this to
1340 ;; 0. If there was an error at any point before, it will not get
1341 ;; set, which is what we want.
1342 (setq bookmark-alist-modification-count 0))
1346 (defun bookmark-write-file (file)
1347 (save-excursion
1348 (save-window-excursion
1349 (if (>= baud-rate 9600)
1350 (message "Saving bookmarks to file %s..." file))
1351 (set-buffer (let ((enable-local-variables nil))
1352 (find-file-noselect file)))
1353 (goto-char (point-min))
1354 (let ((print-length nil)
1355 (print-level nil))
1356 (delete-region (point-min) (point-max))
1357 (bookmark-insert-file-format-version-stamp)
1358 (pp bookmark-alist (current-buffer))
1359 (let ((version-control
1360 (cond
1361 ((null bookmark-version-control) nil)
1362 ((eq 'never bookmark-version-control) 'never)
1363 ((eq 'nospecial bookmark-version-control) version-control)
1365 t))))
1366 (write-file file)
1367 (kill-buffer (current-buffer))
1368 (if (>= baud-rate 9600)
1369 (message "Saving bookmarks to file %s...done" file)))))))
1372 (defun bookmark-import-new-list (new-list)
1373 ;; Walk over the new list, adding each individual bookmark
1374 ;; carefully. "Carefully" means checking against the existing
1375 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1376 ;; as necessary.
1377 (let ((lst new-list)
1378 (names (bookmark-all-names)))
1379 (while lst
1380 (let* ((full-record (car lst)))
1381 (bookmark-maybe-rename full-record names)
1382 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1383 (setq names (cons (bookmark-name-from-full-record full-record) names))
1384 (setq lst (cdr lst))))))
1387 (defun bookmark-maybe-rename (full-record names)
1388 ;; just a helper for bookmark-import-new-list; it is only for
1389 ;; readability that this is not inlined.
1391 ;; Once this has found a free name, it sets full-record to that
1392 ;; name.
1393 (let ((found-name (bookmark-name-from-full-record full-record)))
1394 (if (member found-name names)
1395 ;; We've got a conflict, so generate a new name
1396 (let ((count 2)
1397 (new-name found-name))
1398 (while (member new-name names)
1399 (setq new-name (concat found-name (format "<%d>" count)))
1400 (setq count (1+ count)))
1401 (bookmark-set-name full-record new-name)))))
1404 ;;;###autoload
1405 (defun bookmark-load (file &optional overwrite no-msg)
1406 "Load bookmarks from FILE (which must be in bookmark format).
1407 Appends loaded bookmarks to the front of the list of bookmarks. If
1408 optional second argument OVERWRITE is non-nil, existing bookmarks are
1409 destroyed. Optional third arg NO-MSG means don't display any messages
1410 while loading.
1412 If you load a file that doesn't contain a proper bookmark alist, you
1413 will corrupt Emacs's bookmark list. Generally, you should only load
1414 in files that were created with the bookmark functions in the first
1415 place. Your own personal bookmark file, `~/.emacs.bmk', is
1416 maintained automatically by Emacs; you shouldn't need to load it
1417 explicitly.
1419 If you load a file containing bookmarks with the same names as
1420 bookmarks already present in your Emacs, the new bookmarks will get
1421 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1422 method buffers use to resolve name collisions."
1423 (interactive
1424 (list (read-file-name
1425 (format "Load bookmarks from: (%s) "
1426 bookmark-default-file)
1427 ;;Default might not be used often,
1428 ;;but there's no better default, and
1429 ;;I guess it's better than none at all.
1430 "~/" bookmark-default-file 'confirm)))
1431 (setq file (expand-file-name file))
1432 (if (file-readable-p file)
1433 (save-excursion
1434 (save-window-excursion
1435 (if (and (null no-msg) (>= baud-rate 9600))
1436 (message "Loading bookmarks from %s..." file))
1437 (set-buffer (let ((enable-local-variables nil))
1438 (find-file-noselect file)))
1439 (goto-char (point-min))
1440 (bookmark-maybe-upgrade-file-format)
1441 (let ((blist (bookmark-alist-from-buffer)))
1442 (if (listp blist)
1443 (progn
1444 (if overwrite
1445 (progn
1446 (setq bookmark-alist blist)
1447 (setq bookmark-alist-modification-count 0))
1448 ;; else
1449 (bookmark-import-new-list blist)
1450 (setq bookmark-alist-modification-count
1451 (1+ bookmark-alist-modification-count)))
1452 (if (string-equal
1453 (expand-file-name bookmark-default-file)
1454 file)
1455 (setq bookmarks-already-loaded t))
1456 (bookmark-bmenu-surreptitiously-rebuild-list))
1457 (error "Invalid bookmark list in %s" file)))
1458 (kill-buffer (current-buffer)))
1459 (if (and (null no-msg) (>= baud-rate 9600))
1460 (message "Loading bookmarks from %s...done" file)))
1461 (error "Cannot read bookmark file %s" file)))
1465 ;;; Code supporting the dired-like bookmark menu. Prefix is
1466 ;;; "bookmark-bmenu" for "buffer-menu":
1469 (defvar bookmark-bmenu-bookmark-column nil)
1472 (defvar bookmark-bmenu-hidden-bookmarks ())
1475 (defvar bookmark-bmenu-mode-map nil)
1478 (if bookmark-bmenu-mode-map
1480 (setq bookmark-bmenu-mode-map (make-keymap))
1481 (suppress-keymap bookmark-bmenu-mode-map t)
1482 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1483 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1484 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1485 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1486 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1487 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1488 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1489 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1490 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1491 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1492 (define-key bookmark-bmenu-mode-map "\C-o"
1493 'bookmark-bmenu-switch-other-window)
1494 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1495 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1496 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1497 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1498 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1499 (define-key bookmark-bmenu-mode-map " " 'next-line)
1500 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1501 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1502 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1503 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1504 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1505 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1506 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1507 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1508 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1509 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1510 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1511 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1512 (define-key bookmark-bmenu-mode-map [mouse-2]
1513 'bookmark-bmenu-other-window-with-mouse))
1517 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1518 ;; data.
1519 (put 'bookmark-bmenu-mode 'mode-class 'special)
1522 ;; todo: need to display whether or not bookmark exists as a buffer in
1523 ;; flag column.
1525 ;; Format:
1526 ;; FLAGS BOOKMARK [ LOCATION ]
1529 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1530 "Rebuild the Bookmark List if it exists.
1531 Don't affect the buffer ring order."
1532 (if (get-buffer "*Bookmark List*")
1533 (save-excursion
1534 (save-window-excursion
1535 (bookmark-bmenu-list)))))
1538 ;;;###autoload
1539 (defun bookmark-bmenu-list ()
1540 "Display a list of existing bookmarks.
1541 The list is displayed in a buffer named `*Bookmark List*'.
1542 The leftmost column displays a D if the bookmark is flagged for
1543 deletion, or > if it is flagged for displaying."
1544 (interactive)
1545 (bookmark-maybe-load-default-file)
1546 (if (interactive-p)
1547 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1548 (set-buffer (get-buffer-create "*Bookmark List*")))
1549 (let ((buffer-read-only nil))
1550 (delete-region (point-max) (point-min))
1551 (goto-char (point-min)) ;sure are playing it safe...
1552 (insert "% Bookmark\n- --------\n")
1553 (bookmark-maybe-sort-alist)
1554 (mapcar
1555 (lambda (full-record)
1556 ;; if a bookmark has an annotation, prepend a "*"
1557 ;; in the list of bookmarks.
1558 (let ((annotation (bookmark-get-annotation
1559 (bookmark-name-from-full-record full-record))))
1560 (if (and annotation (not (string-equal annotation "")))
1561 (insert " *")
1562 (insert " "))
1563 (let ((start (point)))
1564 (insert (bookmark-name-from-full-record full-record))
1565 (if (and (display-color-p) (display-mouse-p))
1566 (add-text-properties start
1567 (save-excursion (re-search-backward
1568 "[^ \t]")
1569 (1+ (point)))
1570 '(mouse-face highlight
1571 help-echo "mouse-2: go to this bookmark")))
1572 (insert "\n")
1574 bookmark-alist))
1575 (goto-char (point-min))
1576 (forward-line 2)
1577 (bookmark-bmenu-mode)
1578 (if bookmark-bmenu-toggle-filenames
1579 (bookmark-bmenu-toggle-filenames t)))
1581 ;;;###autoload
1582 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1583 ;;;###autoload
1584 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1588 (defun bookmark-bmenu-mode ()
1589 "Major mode for editing a list of bookmarks.
1590 Each line describes one of the bookmarks in Emacs.
1591 Letters do not insert themselves; instead, they are commands.
1592 Bookmark names preceded by a \"*\" have annotations.
1593 \\<bookmark-bmenu-mode-map>
1594 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1595 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1596 Also show bookmarks marked using m in other windows.
1597 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1598 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1599 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1600 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1601 together with bookmark selected before this one in another window.
1602 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1603 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1604 so the bookmark menu bookmark remains visible in its window.
1605 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1606 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1607 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1608 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1609 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1610 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1611 With a prefix arg, prompts for a file to save in.
1612 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1613 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1614 With prefix argument, also move up one line.
1615 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1616 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1617 in another buffer.
1618 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1619 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1620 (kill-all-local-variables)
1621 (use-local-map bookmark-bmenu-mode-map)
1622 (setq truncate-lines t)
1623 (setq buffer-read-only t)
1624 (setq major-mode 'bookmark-bmenu-mode)
1625 (setq mode-name "Bookmark Menu")
1626 (run-hooks 'bookmark-bmenu-mode-hook))
1629 (defun bookmark-bmenu-toggle-filenames (&optional show)
1630 "Toggle whether filenames are shown in the bookmark list.
1631 Optional argument SHOW means show them unconditionally."
1632 (interactive)
1633 (cond
1634 (show
1635 (setq bookmark-bmenu-toggle-filenames nil)
1636 (bookmark-bmenu-show-filenames)
1637 (setq bookmark-bmenu-toggle-filenames t))
1638 (bookmark-bmenu-toggle-filenames
1639 (bookmark-bmenu-hide-filenames)
1640 (setq bookmark-bmenu-toggle-filenames nil))
1642 (bookmark-bmenu-show-filenames)
1643 (setq bookmark-bmenu-toggle-filenames t))))
1646 (defun bookmark-bmenu-show-filenames (&optional force)
1647 (if (and (not force) bookmark-bmenu-toggle-filenames)
1648 nil ;already shown, so do nothing
1649 (save-excursion
1650 (save-window-excursion
1651 (goto-char (point-min))
1652 (forward-line 2)
1653 (setq bookmark-bmenu-hidden-bookmarks ())
1654 (let ((buffer-read-only nil))
1655 (while (< (point) (point-max))
1656 (let ((bmrk (bookmark-bmenu-bookmark)))
1657 (setq bookmark-bmenu-hidden-bookmarks
1658 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1659 (let ((start (save-excursion (end-of-line) (point))))
1660 (move-to-column bookmark-bmenu-file-column t)
1661 ;; Strip off `mouse-face' from the white spaces region.
1662 (if (and (display-color-p) (display-mouse-p))
1663 (remove-text-properties start (point)
1664 '(mouse-face nil help-echo nil))))
1665 (delete-region (point) (progn (end-of-line) (point)))
1666 (insert " ")
1667 ;; Pass the NO-HISTORY arg:
1668 (bookmark-insert-location bmrk t)
1669 (forward-line 1))))))))
1672 (defun bookmark-bmenu-hide-filenames (&optional force)
1673 (if (and (not force) bookmark-bmenu-toggle-filenames)
1674 ;; nothing to hide if above is nil
1675 (save-excursion
1676 (save-window-excursion
1677 (goto-char (point-min))
1678 (forward-line 2)
1679 (setq bookmark-bmenu-hidden-bookmarks
1680 (nreverse bookmark-bmenu-hidden-bookmarks))
1681 (save-excursion
1682 (goto-char (point-min))
1683 (search-forward "Bookmark")
1684 (backward-word 1)
1685 (setq bookmark-bmenu-bookmark-column (current-column)))
1686 (save-excursion
1687 (let ((buffer-read-only nil))
1688 (while bookmark-bmenu-hidden-bookmarks
1689 (move-to-column bookmark-bmenu-bookmark-column t)
1690 (bookmark-kill-line)
1691 (let ((start (point)))
1692 (insert (car bookmark-bmenu-hidden-bookmarks))
1693 (if (and (display-color-p) (display-mouse-p))
1694 (add-text-properties start
1695 (save-excursion (re-search-backward
1696 "[^ \t]")
1697 (1+ (point)))
1698 '(mouse-face highlight
1699 help-echo
1700 "mouse-2: go to this bookmark"))))
1701 (setq bookmark-bmenu-hidden-bookmarks
1702 (cdr bookmark-bmenu-hidden-bookmarks))
1703 (forward-line 1))))))))
1706 (defun bookmark-bmenu-check-position ()
1707 ;; Returns non-nil if on a line with a bookmark.
1708 ;; (The actual value returned is bookmark-alist).
1709 ;; Else reposition and try again, else return nil.
1710 (cond ((< (count-lines (point-min) (point)) 2)
1711 (goto-char (point-min))
1712 (forward-line 2)
1713 bookmark-alist)
1714 ((and (bolp) (eobp))
1715 (beginning-of-line 0)
1716 bookmark-alist)
1718 bookmark-alist)))
1721 (defun bookmark-bmenu-bookmark ()
1722 ;; return a string which is bookmark of this line.
1723 (if (bookmark-bmenu-check-position)
1724 (save-excursion
1725 (save-window-excursion
1726 (goto-char (point-min))
1727 (search-forward "Bookmark")
1728 (backward-word 1)
1729 (setq bookmark-bmenu-bookmark-column (current-column)))))
1730 (if bookmark-bmenu-toggle-filenames
1731 (bookmark-bmenu-hide-filenames))
1732 (save-excursion
1733 (save-window-excursion
1734 (beginning-of-line)
1735 (forward-char bookmark-bmenu-bookmark-column)
1736 (prog1
1737 (buffer-substring-no-properties (point)
1738 (progn
1739 (end-of-line)
1740 (point)))
1741 ;; well, this is certainly crystal-clear:
1742 (if bookmark-bmenu-toggle-filenames
1743 (bookmark-bmenu-toggle-filenames t))))))
1746 (defun bookmark-show-annotation (bookmark)
1747 "Display the annotation for bookmark named BOOKMARK in a buffer,
1748 if an annotation exists."
1749 (let ((annotation (bookmark-get-annotation bookmark)))
1750 (if (and annotation (not (string-equal annotation "")))
1751 (save-excursion
1752 (let ((old-buf (current-buffer)))
1753 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1754 (delete-region (point-min) (point-max))
1755 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1756 (insert annotation)
1757 (goto-char (point-min))
1758 (pop-to-buffer old-buf))))))
1761 (defun bookmark-show-all-annotations ()
1762 "Display the annotations for all bookmarks in a buffer."
1763 (let ((old-buf (current-buffer)))
1764 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1765 (delete-region (point-min) (point-max))
1766 (mapcar
1767 (lambda (full-record)
1768 (let* ((name (bookmark-name-from-full-record full-record))
1769 (ann (bookmark-get-annotation name)))
1770 (insert (concat name ":\n"))
1771 (if (and ann (not (string-equal ann "")))
1772 ;; insert the annotation, indented by 4 spaces.
1773 (progn
1774 (save-excursion (insert ann))
1775 (while (< (point) (point-max))
1776 (beginning-of-line) ; paranoia
1777 (insert " ")
1778 (forward-line)
1779 (end-of-line))))))
1780 bookmark-alist)
1781 (goto-char (point-min))
1782 (pop-to-buffer old-buf)))
1785 (defun bookmark-bmenu-mark ()
1786 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1787 (interactive)
1788 (beginning-of-line)
1789 (if (bookmark-bmenu-check-position)
1790 (let ((buffer-read-only nil))
1791 (delete-char 1)
1792 (insert ?>)
1793 (forward-line 1)
1794 (bookmark-bmenu-check-position))))
1797 (defun bookmark-bmenu-select ()
1798 "Select this line's bookmark; also display bookmarks marked with `>'.
1799 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1800 (interactive)
1801 (if (bookmark-bmenu-check-position)
1802 (let ((bmrk (bookmark-bmenu-bookmark))
1803 (menu (current-buffer))
1804 (others ())
1805 tem)
1806 (goto-char (point-min))
1807 (while (re-search-forward "^>" nil t)
1808 (setq tem (bookmark-bmenu-bookmark))
1809 (let ((buffer-read-only nil))
1810 (delete-char -1)
1811 (insert ?\ ))
1812 (or (string-equal tem bmrk)
1813 (member tem others)
1814 (setq others (cons tem others))))
1815 (setq others (nreverse others)
1816 tem (/ (1- (frame-height)) (1+ (length others))))
1817 (delete-other-windows)
1818 (bookmark-jump bmrk)
1819 (bury-buffer menu)
1820 (if others
1821 (while others
1822 (split-window nil tem)
1823 (other-window 1)
1824 (bookmark-jump (car others))
1825 (setq others (cdr others)))
1826 (other-window 1)))))
1829 (defun bookmark-bmenu-save (parg)
1830 "Save the current list into a bookmark file.
1831 With a prefix arg, prompts for a file to save them in."
1832 (interactive "P")
1833 (save-excursion
1834 (save-window-excursion
1835 (bookmark-save parg))))
1838 (defun bookmark-bmenu-load ()
1839 "Load the bookmark file and rebuild the bookmark menu-buffer."
1840 (interactive)
1841 (if (bookmark-bmenu-check-position)
1842 (save-excursion
1843 (save-window-excursion
1844 ;; This will call `bookmark-bmenu-list'
1845 (call-interactively 'bookmark-load)))))
1848 (defun bookmark-bmenu-1-window ()
1849 "Select this line's bookmark, alone, in full frame."
1850 (interactive)
1851 (if (bookmark-bmenu-check-position)
1852 (progn
1853 (bookmark-jump (bookmark-bmenu-bookmark))
1854 (bury-buffer (other-buffer))
1855 (delete-other-windows))))
1858 (defun bookmark-bmenu-2-window ()
1859 "Select this line's bookmark, with previous buffer in second window."
1860 (interactive)
1861 (if (bookmark-bmenu-check-position)
1862 (let ((bmrk (bookmark-bmenu-bookmark))
1863 (menu (current-buffer))
1864 (pop-up-windows t))
1865 (delete-other-windows)
1866 (switch-to-buffer (other-buffer))
1867 (let* ((pair (bookmark-jump-noselect bmrk))
1868 (buff (car pair))
1869 (pos (cdr pair)))
1870 (pop-to-buffer buff)
1871 (goto-char pos))
1872 (bury-buffer menu))))
1875 (defun bookmark-bmenu-this-window ()
1876 "Select this line's bookmark in this window."
1877 (interactive)
1878 (if (bookmark-bmenu-check-position)
1879 (bookmark-jump (bookmark-bmenu-bookmark))))
1882 (defun bookmark-bmenu-other-window ()
1883 "Select this line's bookmark in other window, leaving bookmark menu visible."
1884 (interactive)
1885 (let ((bookmark (bookmark-bmenu-bookmark)))
1886 (if (bookmark-bmenu-check-position)
1887 (let* ((pair (bookmark-jump-noselect bookmark))
1888 (buff (car pair))
1889 (pos (cdr pair)))
1890 (switch-to-buffer-other-window buff)
1891 (goto-char pos)
1892 (set-window-point (get-buffer-window buff) pos)
1893 (bookmark-show-annotation bookmark)))))
1896 (defun bookmark-bmenu-switch-other-window ()
1897 "Make the other window select this line's bookmark.
1898 The current window remains selected."
1899 (interactive)
1900 (let ((bookmark (bookmark-bmenu-bookmark))
1901 (pop-up-windows t)
1902 same-window-buffer-names
1903 same-window-regexps)
1904 (if (bookmark-bmenu-check-position)
1905 (let* ((pair (bookmark-jump-noselect bookmark))
1906 (buff (car pair))
1907 (pos (cdr pair)))
1908 (display-buffer buff)
1909 (let ((o-buffer (current-buffer)))
1910 ;; save-excursion won't do
1911 (set-buffer buff)
1912 (goto-char pos)
1913 (set-window-point (get-buffer-window buff) pos)
1914 (set-buffer o-buffer))
1915 (bookmark-show-annotation bookmark)))))
1917 (defun bookmark-bmenu-other-window-with-mouse (event)
1918 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1919 (interactive "e")
1920 (save-excursion
1921 (set-buffer (window-buffer (posn-window (event-end event))))
1922 (save-excursion
1923 (goto-char (posn-point (event-end event)))
1924 (bookmark-bmenu-other-window))))
1927 (defun bookmark-bmenu-show-annotation ()
1928 "Show the annotation for the current bookmark in another window."
1929 (interactive)
1930 (let ((bookmark (bookmark-bmenu-bookmark)))
1931 (if (bookmark-bmenu-check-position)
1932 (bookmark-show-annotation bookmark))))
1935 (defun bookmark-bmenu-show-all-annotations ()
1936 "Show the annotation for all bookmarks in another window."
1937 (interactive)
1938 (bookmark-show-all-annotations))
1941 (defun bookmark-bmenu-edit-annotation ()
1942 "Edit the annotation for the current bookmark in another window."
1943 (interactive)
1944 (let ((bookmark (bookmark-bmenu-bookmark)))
1945 (if (bookmark-bmenu-check-position)
1946 (bookmark-edit-annotation bookmark))))
1949 (defun bookmark-bmenu-unmark (&optional backup)
1950 "Cancel all requested operations on bookmark on this line and move down.
1951 Optional BACKUP means move up."
1952 (interactive "P")
1953 (beginning-of-line)
1954 (if (bookmark-bmenu-check-position)
1955 (progn
1956 (let ((buffer-read-only nil))
1957 (delete-char 1)
1958 ;; any flags to reset according to circumstances? How about a
1959 ;; flag indicating whether this bookmark is being visited?
1960 ;; well, we don't have this now, so maybe later.
1961 (insert " "))
1962 (forward-line (if backup -1 1))
1963 (bookmark-bmenu-check-position))))
1966 (defun bookmark-bmenu-backup-unmark ()
1967 "Move up and cancel all requested operations on bookmark on line above."
1968 (interactive)
1969 (forward-line -1)
1970 (if (bookmark-bmenu-check-position)
1971 (progn
1972 (bookmark-bmenu-unmark)
1973 (forward-line -1)
1974 (bookmark-bmenu-check-position))))
1977 (defun bookmark-bmenu-delete ()
1978 "Mark bookmark on this line to be deleted.
1979 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1980 (interactive)
1981 (beginning-of-line)
1982 (if (bookmark-bmenu-check-position)
1983 (let ((buffer-read-only nil))
1984 (delete-char 1)
1985 (insert ?D)
1986 (forward-line 1)
1987 (bookmark-bmenu-check-position))))
1990 (defun bookmark-bmenu-delete-backwards ()
1991 "Mark bookmark on this line to be deleted, then move up one line.
1992 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1993 (interactive)
1994 (bookmark-bmenu-delete)
1995 (forward-line -2)
1996 (if (bookmark-bmenu-check-position)
1997 (forward-line 1))
1998 (bookmark-bmenu-check-position))
2001 (defun bookmark-bmenu-execute-deletions ()
2002 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2003 (interactive)
2004 (message "Deleting bookmarks...")
2005 (let ((hide-em bookmark-bmenu-toggle-filenames)
2006 (o-point (point))
2007 (o-str (save-excursion
2008 (beginning-of-line)
2009 (if (looking-at "^D")
2011 (buffer-substring
2012 (point)
2013 (progn (end-of-line) (point))))))
2014 (o-col (current-column)))
2015 (if hide-em (bookmark-bmenu-hide-filenames))
2016 (setq bookmark-bmenu-toggle-filenames nil)
2017 (goto-char (point-min))
2018 (forward-line 1)
2019 (while (re-search-forward "^D" (point-max) t)
2020 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2021 (bookmark-bmenu-list)
2022 (setq bookmark-bmenu-toggle-filenames hide-em)
2023 (if bookmark-bmenu-toggle-filenames
2024 (bookmark-bmenu-toggle-filenames t))
2025 (if o-str
2026 (progn
2027 (goto-char (point-min))
2028 (search-forward o-str)
2029 (beginning-of-line)
2030 (forward-char o-col))
2031 (goto-char o-point))
2032 (beginning-of-line)
2033 (setq bookmark-alist-modification-count
2034 (1+ bookmark-alist-modification-count))
2035 (if (bookmark-time-to-save-p)
2036 (bookmark-save))
2037 (message "Deleting bookmarks...done")
2041 (defun bookmark-bmenu-rename ()
2042 "Rename bookmark on current line. Prompts for a new name."
2043 (interactive)
2044 (if (bookmark-bmenu-check-position)
2045 (let ((bmrk (bookmark-bmenu-bookmark))
2046 (thispoint (point)))
2047 (bookmark-rename bmrk)
2048 (bookmark-bmenu-list)
2049 (goto-char thispoint))))
2052 (defun bookmark-bmenu-locate ()
2053 "Display location of this bookmark. Displays in the minibuffer."
2054 (interactive)
2055 (if (bookmark-bmenu-check-position)
2056 (let ((bmrk (bookmark-bmenu-bookmark)))
2057 (message (bookmark-location bmrk)))))
2061 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2063 (defun bookmark-menu-build-paned-menu (name entries)
2064 "Build a multi-paned menu named NAME from the strings in ENTRIES.
2065 That is, ENTRIES is a list of strings which appear as the choices
2066 in the menu. The number of panes depends on the number of entries.
2067 The visible entries are truncated to `bookmark-menu-length', but the
2068 strings returned are not."
2069 (let* ((f-height (/ (frame-height) 2))
2070 (pane-list
2071 (let (temp-pane-list
2072 (iter 0))
2073 (while entries
2074 (let (lst
2075 (count 0))
2076 (while (and (< count f-height) entries)
2077 (let ((str (car entries)))
2078 (setq lst (cons
2079 (cons
2080 (if (> (length str) bookmark-menu-length)
2081 (substring str 0 bookmark-menu-length)
2082 str)
2083 str)
2084 lst))
2085 (setq entries (cdr entries))
2086 (setq count (1+ count))))
2087 (setq iter (1+ iter))
2088 (setq
2089 temp-pane-list
2090 (cons
2091 (cons
2092 (format "-*- %s (%d) -*-" name iter)
2093 (nreverse lst))
2094 temp-pane-list))))
2095 (nreverse temp-pane-list))))
2097 ;; Return the menu:
2098 (cons (concat "-*- " name " -*-") pane-list)))
2101 (defun bookmark-menu-popup-paned-menu (event name entries)
2102 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2103 That is, ENTRIES is a list of strings which appear as the choices
2104 in the menu.
2105 The number of panes depends on the number of entries."
2106 (interactive "e")
2107 (x-popup-menu event (bookmark-menu-build-paned-menu name entries)))
2110 (defun bookmark-menu-popup-paned-bookmark-menu (event name)
2111 "Pop up menu of bookmarks, return chosen bookmark.
2112 Pop up at EVENT, menu's name is NAME.
2113 The number of panes depends on the number of bookmarks."
2114 (bookmark-menu-popup-paned-menu event name (bookmark-all-names)))
2117 (defun bookmark-popup-menu-and-apply-function (func-sym menu-label event)
2118 ;; help function for making menus that need to apply a bookmark
2119 ;; function to a string.
2120 (let* ((choice (bookmark-menu-popup-paned-bookmark-menu
2121 event menu-label)))
2122 (if choice (apply func-sym (list choice)))))
2125 ;;;###autoload
2126 (defun bookmark-menu-insert (event)
2127 "Insert the text of the file pointed to by bookmark BOOKMARK.
2128 You may have a problem using this function if the value of variable
2129 `bookmark-alist' is nil. If that happens, you need to load in some
2130 bookmarks. See help on function `bookmark-load' for more about
2131 this.
2133 Warning: this function only takes an EVENT as argument. Use the
2134 corresponding bookmark function from Lisp \(the one without the
2135 \"-menu-\" in its name\)."
2136 (interactive "e")
2137 (bookmark-popup-menu-and-apply-function
2138 'bookmark-insert "Insert Bookmark Contents" event))
2141 ;;;###autoload
2142 (defun bookmark-menu-jump (event)
2143 "Jump to bookmark BOOKMARK (a point in some file).
2144 You may have a problem using this function if the value of variable
2145 `bookmark-alist' is nil. If that happens, you need to load in some
2146 bookmarks. See help on function `bookmark-load' for more about
2147 this.
2149 Warning: this function only takes an EVENT as argument. Use the
2150 corresponding bookmark function from Lisp \(the one without the
2151 \"-menu-\" in its name\)."
2152 (interactive "e")
2153 (bookmark-popup-menu-and-apply-function
2154 'bookmark-jump "Jump to Bookmark" event))
2157 ;;;###autoload
2158 (defun bookmark-menu-locate (event)
2159 "Insert the name of the file associated with BOOKMARK.
2160 \(This is not the same as the contents of that file\).
2162 Warning: this function only takes an EVENT as argument. Use the
2163 corresponding bookmark function from Lisp \(the one without the
2164 \"-menu-\" in its name\)."
2165 (interactive "e")
2166 (bookmark-popup-menu-and-apply-function
2167 'bookmark-insert-location "Insert Bookmark Location" event))
2170 ;;;###autoload
2171 (defun bookmark-menu-rename (event)
2172 "Change the name of OLD-BOOKMARK to NEWNAME.
2173 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
2174 If called from menubar, OLD-BOOKMARK is selected from a menu, and
2175 prompts for NEWNAME.
2176 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
2177 passed as an argument. If called with two strings, then no prompting
2178 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
2180 While you are entering the new name, consecutive C-w's insert
2181 consecutive words from the text of the buffer into the new bookmark
2182 name.
2184 Warning: this function only takes an EVENT as argument. Use the
2185 corresponding bookmark function from Lisp \(the one without the
2186 \"-menu-\" in its name\)."
2187 (interactive "e")
2188 (bookmark-popup-menu-and-apply-function
2189 'bookmark-rename "Rename Bookmark" event))
2192 ;;;###autoload
2193 (defun bookmark-menu-delete (event)
2194 "Delete the bookmark named NAME from the bookmark list.
2195 Removes only the first instance of a bookmark with that name. If
2196 there are one or more other bookmarks with the same name, they will
2197 not be deleted. Defaults to the \"current\" bookmark \(that is, the
2198 one most recently used in this file, if any\).
2200 Warning: this function only takes an EVENT as argument. Use the
2201 corresponding bookmark function from Lisp \(the one without the
2202 \"-menu-\" in its name\)."
2203 (interactive "e")
2204 (bookmark-popup-menu-and-apply-function
2205 'bookmark-delete "Delete Bookmark" event))
2208 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2209 ;; following works, and for explaining what to do to make it work.
2211 ;; We MUST autoload EACH form used to set up this variable's value, so
2212 ;; that the whole job is done in loaddefs.el.
2214 ;; Emacs menubar stuff.
2216 ;;;###autoload
2217 (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions"))
2219 ;;;###autoload
2220 (defalias 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
2222 ;; make bookmarks appear toward the right side of the menu.
2223 (if (boundp 'menu-bar-final-items)
2224 (if menu-bar-final-items
2225 (setq menu-bar-final-items
2226 (cons 'bookmark menu-bar-final-items)))
2227 (setq menu-bar-final-items '(bookmark)))
2229 ;;;###autoload
2230 (define-key menu-bar-bookmark-map [load]
2231 '("Load a Bookmark File..." . bookmark-load))
2233 ;;;###autoload
2234 (define-key menu-bar-bookmark-map [write]
2235 '("Save Bookmarks As..." . bookmark-write))
2237 ;;;###autoload
2238 (define-key menu-bar-bookmark-map [save]
2239 '("Save Bookmarks" . bookmark-save))
2241 ;;;###autoload
2242 (define-key menu-bar-bookmark-map [edit]
2243 '("Edit Bookmark List" . bookmark-bmenu-list))
2245 ;;;###autoload
2246 (define-key menu-bar-bookmark-map [delete]
2247 '("Delete Bookmark" . bookmark-menu-delete))
2249 ;;;###autoload
2250 (define-key menu-bar-bookmark-map [rename]
2251 '("Rename Bookmark" . bookmark-menu-rename))
2253 ;;;###autoload
2254 (define-key menu-bar-bookmark-map [locate]
2255 '("Insert Location" . bookmark-menu-locate))
2257 ;;;###autoload
2258 (define-key menu-bar-bookmark-map [insert]
2259 '("Insert Contents" . bookmark-menu-insert))
2261 ;;;###autoload
2262 (define-key menu-bar-bookmark-map [set]
2263 '("Set Bookmark" . bookmark-set))
2265 ;;;###autoload
2266 (define-key menu-bar-bookmark-map [jump]
2267 '("Jump to Bookmark" . bookmark-menu-jump))
2269 ;;;; end bookmark menu stuff ;;;;
2272 ;;; Load Hook
2273 (defvar bookmark-load-hook nil
2274 "Hook to run at the end of loading bookmark.")
2276 ;;; Exit Hook, called from kill-emacs-hook
2277 (defvar bookmark-exit-hook nil
2278 "Hook to run when emacs exits")
2280 (defun bookmark-exit-hook-internal ()
2281 "Save bookmark state, if necessary, at Emacs exit time.
2282 This also runs `bookmark-exit-hooks'."
2283 (and
2284 (progn (run-hooks 'bookmark-exit-hooks) t)
2285 bookmark-alist
2286 (bookmark-time-to-save-p t)
2287 (bookmark-save)))
2289 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2292 (run-hooks 'bookmark-load-hook)
2294 (provide 'bookmark)
2296 ;;; bookmark.el ends here