(thumbs-call-convert): Use call-process directly
[emacs.git] / lisp / bookmark.el
blob9b8eb738f0cd6b80869dd5ae9372728cd4c612b2
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, 2007 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 3, 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 ;; Added for lucid emacs compatibility, db
211 (or (fboundp 'defalias) (fset 'defalias 'fset))
213 ;; suggested for lucid compatibility by david hughes:
214 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
217 ;;; Keymap stuff:
219 ;; Set up these bindings dumping time *only*;
220 ;; if the user alters them, don't override the user when loading bookmark.el.
222 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
223 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
224 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
226 ;;;###autoload
227 (defvar bookmark-map nil
228 "Keymap containing bindings to bookmark functions.
229 It is not bound to any key by default: to bind it
230 so that you have a bookmark prefix, just use `global-set-key' and bind a
231 key of your choice to `bookmark-map'. All interactive bookmark
232 functions have a binding in this keymap.")
234 ;;;###autoload (define-prefix-command 'bookmark-map)
236 ;; Read the help on all of these functions for details...
237 ;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
238 ;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ;"m"ark
239 ;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
240 ;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ;"g"o
241 ;;;###autoload (define-key bookmark-map "o" 'bookmark-jump-other-window)
242 ;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
243 ;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
244 ;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ;"f"ind
245 ;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
246 ;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
247 ;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
248 ;;;###autoload (define-key bookmark-map "w" 'bookmark-write)
249 ;;;###autoload (define-key bookmark-map "s" 'bookmark-save)
252 ;;; The annotation maps.
253 (defvar bookmark-read-annotation-mode-map (copy-keymap text-mode-map)
254 "Keymap for composing an annotation for a bookmark.")
256 (define-key bookmark-read-annotation-mode-map "\C-c\C-c"
257 'bookmark-send-annotation)
261 ;;; Core variables and data structures:
262 (defvar bookmark-alist ()
263 "Association list of bookmarks and their records.
264 You probably don't want to change the value of this alist yourself;
265 instead, let the various bookmark functions do it for you.
267 The format of the alist is
269 \(BOOKMARK1 BOOKMARK2 ...\)
271 where each BOOKMARK is of the form
273 \(NAME
274 \(filename . FILE\)
275 \(front-context-string . FRONT-STR\)
276 \(rear-context-string . REAR-STR\)
277 \(position . POS\)
278 \(info-node . POS\)
279 \(annotation . ANNOTATION\)\)
281 So the cdr of each bookmark is an alist too.
282 `info-node' is optional, by the way.")
285 (defvar bookmarks-already-loaded nil)
288 ;; more stuff added by db.
290 (defvar bookmark-current-bookmark nil
291 "Name of bookmark most recently used in the current file.
292 It is buffer local, used to make moving a bookmark forward
293 through a file easier.")
295 (make-variable-buffer-local 'bookmark-current-bookmark)
298 (defvar bookmark-alist-modification-count 0
299 "Number of modifications to bookmark list since it was last saved.")
302 (defvar bookmark-search-size 16
303 "Length of the context strings recorded on either side of a bookmark.")
306 (defvar bookmark-current-point 0)
307 (defvar bookmark-yank-point 0)
308 (defvar bookmark-current-buffer nil)
310 (defvar Info-current-node)
311 (defvar Info-suffix-list)
313 ;; Helper functions.
315 ;; Only functions on this page and the next one (file formats) need to
316 ;; know anything about the format of bookmark-alist entries.
317 ;; Everyone else should go through them.
319 (defun bookmark-name-from-full-record (full-record)
320 "Return name of FULL-RECORD \(an alist element instead of a string\)."
321 (car full-record))
324 (defun bookmark-all-names ()
325 "Return a list of all current bookmark names."
326 (bookmark-maybe-load-default-file)
327 (mapcar
328 (lambda (full-record)
329 (bookmark-name-from-full-record full-record))
330 bookmark-alist))
333 (defun bookmark-get-bookmark (bookmark)
334 "Return the full entry for BOOKMARK in `bookmark-alist'.
335 If BOOKMARK is not a string, return nil."
336 (when (stringp bookmark)
337 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
340 (defun bookmark-get-bookmark-record (bookmark)
341 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
342 That is, all information but the name."
343 (car (cdr (bookmark-get-bookmark bookmark))))
346 (defun bookmark-set-name (bookmark newname)
347 "Set BOOKMARK's name to NEWNAME."
348 (setcar
349 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
350 newname))
353 (defun bookmark-get-annotation (bookmark)
354 "Return the annotation of BOOKMARK, or nil if none."
355 (cdr (assq 'annotation (bookmark-get-bookmark-record bookmark))))
358 (defun bookmark-set-annotation (bookmark ann)
359 "Set the annotation of BOOKMARK to ANN."
360 (let ((cell (assq 'annotation (bookmark-get-bookmark-record bookmark))))
361 (if cell
362 (setcdr cell ann)
363 (nconc (bookmark-get-bookmark-record bookmark)
364 (list (cons 'annotation ann))))))
367 (defun bookmark-get-filename (bookmark)
368 "Return the full filename of BOOKMARK."
369 (cdr (assq 'filename (bookmark-get-bookmark-record bookmark))))
372 (defun bookmark-set-filename (bookmark filename)
373 "Set the full filename of BOOKMARK to FILENAME."
374 (let ((cell (assq 'filename (bookmark-get-bookmark-record bookmark))))
375 (if cell
376 (setcdr cell filename)
377 (nconc (bookmark-get-bookmark-record bookmark)
378 (list (cons 'filename filename))))
379 (setq bookmark-alist-modification-count
380 (1+ bookmark-alist-modification-count))
381 (if (bookmark-time-to-save-p)
382 (bookmark-save))))
385 (defun bookmark-get-position (bookmark)
386 "Return the position \(i.e.: point\) of BOOKMARK."
387 (cdr (assq 'position (bookmark-get-bookmark-record bookmark))))
390 (defun bookmark-set-position (bookmark position)
391 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
392 (let ((cell (assq 'position (bookmark-get-bookmark-record bookmark))))
393 (if cell
394 (setcdr cell position)
395 (nconc (bookmark-get-bookmark-record bookmark)
396 (list (cons 'position position))))))
399 (defun bookmark-get-front-context-string (bookmark)
400 "Return the front-context-string of BOOKMARK."
401 (cdr (assq 'front-context-string (bookmark-get-bookmark-record bookmark))))
404 (defun bookmark-set-front-context-string (bookmark string)
405 "Set the front-context-string of BOOKMARK to STRING."
406 (let ((cell (assq 'front-context-string
407 (bookmark-get-bookmark-record bookmark))))
408 (if cell
409 (setcdr cell string)
410 (nconc (bookmark-get-bookmark-record bookmark)
411 (list (cons 'front-context-string string))))))
414 (defun bookmark-get-rear-context-string (bookmark)
415 "Return the rear-context-string of BOOKMARK."
416 (cdr (assq 'rear-context-string (bookmark-get-bookmark-record bookmark))))
419 (defun bookmark-set-rear-context-string (bookmark string)
420 "Set the rear-context-string of BOOKMARK to STRING."
421 (let ((cell (assq 'rear-context-string
422 (bookmark-get-bookmark-record bookmark))))
423 (if cell
424 (setcdr cell string)
425 (nconc (bookmark-get-bookmark-record bookmark)
426 (list (cons 'rear-context-string string))))))
429 (defun bookmark-get-info-node (bookmark)
430 "Get the info node associated with BOOKMARK."
431 (cdr (assq 'info-node (bookmark-get-bookmark-record bookmark))))
434 (defun bookmark-set-info-node (bookmark node)
435 "Set the Info node of BOOKMARK to NODE."
436 (let ((cell (assq 'info-node
437 (bookmark-get-bookmark-record bookmark))))
438 (if cell
439 (setcdr cell node)
440 (nconc (bookmark-get-bookmark-record bookmark)
441 (list (cons 'info-node node)))))
443 (message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
444 (sit-for 4))
446 (defun bookmark-get-handler (bookmark)
447 (cdr (assq 'handler (bookmark-get-bookmark-record bookmark))))
449 (defvar bookmark-history nil
450 "The history list for bookmark functions.")
453 (defun bookmark-completing-read (prompt &optional default)
454 "Prompting with PROMPT, read a bookmark name in completion.
455 PROMPT will get a \": \" stuck on the end no matter what, so you
456 probably don't want to include one yourself.
457 Optional second arg DEFAULT is a string to return if the user enters
458 the empty string."
459 (bookmark-maybe-load-default-file) ; paranoia
460 (if (listp last-nonmenu-event)
461 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
462 (let* ((completion-ignore-case bookmark-completion-ignore-case)
463 (default default)
464 (prompt (if default
465 (concat prompt (format " (%s): " default))
466 (concat prompt ": ")))
467 (str
468 (completing-read prompt
469 bookmark-alist
473 'bookmark-history)))
474 (if (string-equal "" str) default str))))
477 (defmacro bookmark-maybe-historicize-string (string)
478 "Put STRING into the bookmark prompt history, if caller non-interactive.
479 We need this because sometimes bookmark functions are invoked from
480 menus, so `completing-read' never gets a chance to set `bookmark-history'."
481 `(or
482 (interactive-p)
483 (setq bookmark-history (cons ,string bookmark-history))))
485 (defvar bookmark-make-cell-function 'bookmark-make-cell-for-text-file
486 "A function that should be called to create the bookmark
487 record. Modes may set this variable buffer-locally to enable
488 bookmarking of non-text files like images or pdf documents.
490 The function will be called with two arguments: ANNOTATION and
491 INFO-NODE. See `bookmark-make-cell-for-text-file' for a
492 description.
494 The returned record may contain a special cons (handler
495 . some-function) which sets the handler function that should be
496 used to open this bookmark instead of `bookmark-jump-noselect'.
497 It should return a cons (BUFFER . POINT) indicating buffer
498 showing the bookmarked location and the value of point in that
499 buffer. Like `bookmark-jump-noselect' the buffer shouldn't be
500 selected by the handler.")
502 (defun bookmark-make (name &optional annotation overwrite info-node)
503 "Make a bookmark named NAME.
504 Optional second arg ANNOTATION gives it an annotation.
505 Optional third arg OVERWRITE means replace any existing bookmarks with
506 this name.
507 Optional fourth arg INFO-NODE means this bookmark is at info node
508 INFO-NODE, so record this fact in the bookmark's entry."
509 (bookmark-maybe-load-default-file)
510 (let ((stripped-name (copy-sequence name)))
511 (or (featurep 'xemacs)
512 ;; XEmacs's `set-text-properties' doesn't work on
513 ;; free-standing strings, apparently.
514 (set-text-properties 0 (length stripped-name) nil stripped-name))
515 (if (and (bookmark-get-bookmark stripped-name) (not overwrite))
516 ;; already existing bookmark under that name and
517 ;; no prefix arg means just overwrite old bookmark
518 (setcdr (bookmark-get-bookmark stripped-name)
519 (list (funcall bookmark-make-cell-function annotation info-node)))
521 ;; otherwise just cons it onto the front (either the bookmark
522 ;; doesn't exist already, or there is no prefix arg. In either
523 ;; case, we want the new bookmark consed onto the alist...)
525 (setq bookmark-alist
526 (cons
527 (list stripped-name
528 (funcall bookmark-make-cell-function annotation info-node))
529 bookmark-alist)))
531 ;; Added by db
532 (setq bookmark-current-bookmark stripped-name)
533 (setq bookmark-alist-modification-count
534 (1+ bookmark-alist-modification-count))
535 (if (bookmark-time-to-save-p)
536 (bookmark-save))))
539 (defun bookmark-make-cell-for-text-file (annotation &optional info-node)
540 "Return the record part of a new bookmark, given ANNOTATION.
541 Must be at the correct position in the buffer in which the bookmark is
542 being set. This might change someday.
543 Optional second arg INFO-NODE means this bookmark is at info node
544 INFO-NODE, so record this fact in the bookmark's entry."
545 (let ((the-record
546 `((filename . ,(bookmark-buffer-file-name))
547 (front-context-string
548 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
549 (buffer-substring-no-properties
550 (point)
551 (+ (point) bookmark-search-size))
552 nil))
553 (rear-context-string
554 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
555 (buffer-substring-no-properties
556 (point)
557 (- (point) bookmark-search-size))
558 nil))
559 (position . ,(point)))))
561 ;; Now fill in the optional parts:
563 ;; Take no chances with text properties
564 (set-text-properties 0 (length annotation) nil annotation)
565 (set-text-properties 0 (length info-node) nil info-node)
567 (if annotation
568 (nconc the-record (list (cons 'annotation annotation))))
569 (if info-node
570 (nconc the-record (list (cons 'info-node info-node))))
572 ;; Finally, return the completed record.
573 the-record))
577 ;;; File format stuff
579 ;; The OLD format of the bookmark-alist was:
581 ;; ((bookmark-name (filename
582 ;; string-in-front
583 ;; string-behind
584 ;; point))
585 ;; ...)
587 ;; The NEW format of the bookmark-alist is:
589 ;; ((bookmark-name ((filename . FILENAME)
590 ;; (front-context-string . string-in-front)
591 ;; (rear-context-string . string-behind)
592 ;; (position . POINT)
593 ;; (annotation . annotation)
594 ;; (whatever . VALUE)
595 ;; ...
596 ;; ))
597 ;; ...)
600 ;; I switched to using an internal as well as external alist because I
601 ;; felt that would be a more flexible framework in which to add
602 ;; features. It means that the order in which values appear doesn't
603 ;; matter, and it means that arbitrary values can be added without
604 ;; risk of interfering with existing ones.
606 ;; BOOKMARK-NAME is the string the user gives the bookmark and
607 ;; accesses it by from then on.
609 ;; FILENAME is the location of the file in which the bookmark is set.
611 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
612 ;; context in front of the point at which the bookmark is set.
614 ;; STRING-BEHIND is the same thing, but after the point.
616 ;; The context strings exist so that modifications to a file don't
617 ;; necessarily cause a bookmark's position to be invalidated.
618 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
619 ;; case the file has changed since the bookmark was set. It will
620 ;; attempt to place the user before the changes, if there were any.
621 ;; ANNOTATION is the annotation for the bookmark; it may not exist
622 ;; (for backward compatibility), be nil (no annotation), or be a
623 ;; string.
626 (defconst bookmark-file-format-version 1
627 "The current version of the format used by bookmark files.
628 You should never need to change this.")
631 (defconst bookmark-end-of-version-stamp-marker
632 "-*- End Of Bookmark File Format Version Stamp -*-\n"
633 "This string marks the end of the version stamp in a bookmark file.")
636 (defun bookmark-alist-from-buffer ()
637 "Return a `bookmark-alist' (in any format) from the current buffer.
638 The buffer must of course contain bookmark format information.
639 Does not care from where in the buffer it is called, and does not
640 affect point."
641 (save-excursion
642 (goto-char (point-min))
643 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
644 (read (current-buffer))
645 ;; Else we're dealing with format version 0
646 (if (search-forward "(" nil t)
647 (progn
648 (forward-char -1)
649 (read (current-buffer)))
650 ;; Else no hope of getting information here.
651 (error "Not bookmark format")))))
654 (defun bookmark-upgrade-version-0-alist (old-list)
655 "Upgrade a version 0 alist OLD-LIST to the current version."
656 (mapcar
657 (lambda (bookmark)
658 (let* ((name (car bookmark))
659 (record (car (cdr bookmark)))
660 (filename (nth 0 record))
661 (front-str (nth 1 record))
662 (rear-str (nth 2 record))
663 (position (nth 3 record))
664 (ann (nth 4 record)))
665 (list
666 name
667 `((filename . ,filename)
668 (front-context-string . ,(or front-str ""))
669 (rear-context-string . ,(or rear-str ""))
670 (position . ,position)
671 (annotation . ,ann)))))
672 old-list))
675 (defun bookmark-upgrade-file-format-from-0 ()
676 "Upgrade a bookmark file of format 0 (the original format) to format 1.
677 This expects to be called from `point-min' in a bookmark file."
678 (message "Upgrading bookmark format from 0 to %d..."
679 bookmark-file-format-version)
680 (let* ((old-list (bookmark-alist-from-buffer))
681 (new-list (bookmark-upgrade-version-0-alist old-list)))
682 (delete-region (point-min) (point-max))
683 (bookmark-insert-file-format-version-stamp)
684 (pp new-list (current-buffer))
685 (save-buffer))
686 (goto-char (point-min))
687 (message "Upgrading bookmark format from 0 to %d...done"
688 bookmark-file-format-version)
692 (defun bookmark-grok-file-format-version ()
693 "Return an integer which is the file-format version of this bookmark file.
694 This expects to be called from `point-min' in a bookmark file."
695 (if (looking-at "^;;;;")
696 (save-excursion
697 (save-match-data
698 (re-search-forward "[0-9]")
699 (forward-char -1)
700 (read (current-buffer))))
701 ;; Else this is format version 0, the original one, which didn't
702 ;; even have version stamps.
706 (defun bookmark-maybe-upgrade-file-format ()
707 "Check the file-format version of this bookmark file.
708 If the version is not up-to-date, upgrade it automatically.
709 This expects to be called from `point-min' in a bookmark file."
710 (let ((version (bookmark-grok-file-format-version)))
711 (cond
712 ((= version bookmark-file-format-version)
713 ) ; home free -- version is current
714 ((= version 0)
715 (bookmark-upgrade-file-format-from-0))
717 (error "Bookmark file format version strangeness")))))
720 (defun bookmark-insert-file-format-version-stamp ()
721 "Insert text indicating current version of bookmark file format."
722 (insert
723 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
724 bookmark-file-format-version))
725 (insert ";;; This format is meant to be slightly human-readable;\n"
726 ";;; nevertheless, you probably don't want to edit it.\n"
727 ";;; "
728 bookmark-end-of-version-stamp-marker))
731 ;;; end file-format stuff
734 ;;; Generic helpers.
736 (defun bookmark-maybe-message (fmt &rest args)
737 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
738 (if (>= baud-rate 9600)
739 (apply 'message fmt args)))
742 ;;; Core code:
744 ;;;###autoload
745 (defun bookmark-set (&optional name parg)
746 "Set a bookmark named NAME inside a file.
747 If name is nil, then the user will be prompted.
748 With prefix arg, will not overwrite a bookmark that has the same name
749 as NAME if such a bookmark already exists, but instead will \"push\"
750 the new bookmark onto the bookmark alist. Thus the most recently set
751 bookmark with name NAME would be the one in effect at any given time,
752 but the others are still there, should you decide to delete the most
753 recent one.
755 To yank words from the text of the buffer and use them as part of the
756 bookmark name, type C-w while setting a bookmark. Successive C-w's
757 yank successive words.
759 Typing C-u inserts the name of the last bookmark used in the buffer
760 \(as an aid in using a single bookmark name to track your progress
761 through a large file\). If no bookmark was used, then C-u inserts the
762 name of the file being visited.
764 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
765 and it removes only the first instance of a bookmark with that name from
766 the list of bookmarks.\)"
767 (interactive (list nil current-prefix-arg))
769 (bookmark-buffer-file-name)
770 (error "Buffer not visiting a file or directory"))
772 (bookmark-maybe-load-default-file)
774 (setq bookmark-current-point (point))
775 (setq bookmark-yank-point (point))
776 (setq bookmark-current-buffer (current-buffer))
778 (let* ((default (or bookmark-current-bookmark
779 (bookmark-buffer-name)))
780 (str
781 (or name
782 (read-from-minibuffer
783 (format "Set bookmark (%s): " default)
785 (let ((now-map (copy-keymap minibuffer-local-map)))
786 (define-key now-map "\C-w" 'bookmark-yank-word)
787 (define-key now-map "\C-u" 'bookmark-insert-current-bookmark)
788 now-map))))
789 (annotation nil))
790 (and (string-equal str "") (setq str default))
791 ;; Ask for an annotation buffer for this bookmark
792 (if bookmark-use-annotations
793 (bookmark-read-annotation parg str)
794 (bookmark-make str annotation parg (bookmark-info-current-node))
795 (setq bookmark-current-bookmark str)
796 (bookmark-bmenu-surreptitiously-rebuild-list)
797 (goto-char bookmark-current-point))))
800 (defun bookmark-info-current-node ()
801 "If in `Info-mode', return current node name (a string), else nil."
802 (if (eq major-mode 'Info-mode)
803 Info-current-node))
806 (defun bookmark-kill-line (&optional newline-too)
807 "Kill from point to end of line.
808 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
809 Does not affect the kill ring."
810 (let ((eol (save-excursion (end-of-line) (point))))
811 (delete-region (point) eol)
812 (if (and newline-too (looking-at "\n"))
813 (delete-char 1))))
816 ;; Defvars to avoid compilation warnings:
817 (defvar bookmark-annotation-paragraph nil)
818 (defvar bookmark-annotation-name nil)
819 (defvar bookmark-annotation-buffer nil)
820 (defvar bookmark-annotation-file nil)
821 (defvar bookmark-annotation-point nil)
824 (defun bookmark-send-annotation ()
825 "Use buffer contents as the annotation for a bookmark.
826 Exclude lines that begin with `#'.
827 Store the annotation text in the bookmark list with
828 the bookmark (and file, and point) specified in buffer local variables."
829 (interactive)
830 (if (not (eq major-mode 'bookmark-read-annotation-mode))
831 (error "Not in bookmark-read-annotation-mode"))
832 (goto-char (point-min))
833 (while (< (point) (point-max))
834 (if (looking-at "^#")
835 (bookmark-kill-line t)
836 (forward-line 1)))
837 (let ((annotation (buffer-string))
838 (parg bookmark-annotation-paragraph)
839 (bookmark bookmark-annotation-name)
840 (pt bookmark-annotation-point)
841 (buf bookmark-annotation-buffer))
842 ;; for bookmark-make-cell to work, we need to be
843 ;; in the relevant buffer, at the relevant point.
844 ;; Actually, bookmark-make-cell should probably be re-written,
845 ;; to avoid this need. Should I handle the error if a buffer is
846 ;; killed between "C-x r m" and a "C-c C-c" in the annotation buffer?
847 (save-excursion
848 (pop-to-buffer buf)
849 (goto-char pt)
850 (bookmark-make bookmark annotation parg (bookmark-info-current-node))
851 (setq bookmark-current-bookmark bookmark))
852 (bookmark-bmenu-surreptitiously-rebuild-list)
853 (goto-char bookmark-current-point))
854 (kill-buffer (current-buffer)))
857 (defun bookmark-default-annotation-text (bookmark)
858 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
859 "# All lines which start with a '#' will be deleted.\n"
860 "# Type C-c C-c when done.\n#\n"
861 "# Author: " (user-full-name) " <" (user-login-name) "@"
862 (system-name) ">\n"
863 "# Date: " (current-time-string) "\n"))
866 (defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
867 "Function to return default text to use for a bookmark annotation.
868 It takes one argument, the name of the bookmark, as a string.")
870 (defun bookmark-read-annotation-mode (buf point parg bookmark)
871 "Mode for composing annotations for a bookmark.
872 Wants BUF, POINT, PARG, and BOOKMARK.
873 When you have finished composing, type \\[bookmark-send-annotation] to send
874 the annotation.
876 \\{bookmark-read-annotation-mode-map}"
877 (interactive)
878 (kill-all-local-variables)
879 (make-local-variable 'bookmark-annotation-paragraph)
880 (make-local-variable 'bookmark-annotation-name)
881 (make-local-variable 'bookmark-annotation-buffer)
882 (make-local-variable 'bookmark-annotation-file)
883 (make-local-variable 'bookmark-annotation-point)
884 (setq bookmark-annotation-paragraph parg)
885 (setq bookmark-annotation-name bookmark)
886 (setq bookmark-annotation-buffer buf)
887 (setq bookmark-annotation-file (buffer-file-name buf))
888 (setq bookmark-annotation-point point)
889 (use-local-map bookmark-read-annotation-mode-map)
890 (setq major-mode 'bookmark-read-annotation-mode)
891 (insert (funcall bookmark-read-annotation-text-func bookmark))
892 (run-mode-hooks 'text-mode-hook))
895 (defun bookmark-read-annotation (parg bookmark)
896 "Pop up a buffer for entering a bookmark annotation.
897 Text surrounding the bookmark is PARG; the bookmark name is BOOKMARK."
898 (let ((buf (current-buffer))
899 (point (point)))
900 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
901 (bookmark-read-annotation-mode buf point parg bookmark)))
904 (defvar bookmark-edit-annotation-mode-map (copy-keymap text-mode-map)
905 "Keymap for editing an annotation of a bookmark.")
908 (define-key bookmark-edit-annotation-mode-map "\C-c\C-c"
909 'bookmark-send-edited-annotation)
912 (defun bookmark-edit-annotation-mode (bookmark)
913 "Mode for editing the annotation of bookmark BOOKMARK.
914 When you have finished composing, type \\[bookmark-send-annotation].
916 \\{bookmark-edit-annotation-mode-map}"
917 (interactive)
918 (kill-all-local-variables)
919 (make-local-variable 'bookmark-annotation-name)
920 (setq bookmark-annotation-name bookmark)
921 (use-local-map bookmark-edit-annotation-mode-map)
922 (setq major-mode 'bookmark-edit-annotation-mode
923 mode-name "Edit Bookmark Annotation")
924 (insert (funcall bookmark-read-annotation-text-func bookmark))
925 (let ((annotation (bookmark-get-annotation bookmark)))
926 (if (and annotation (not (string-equal annotation "")))
927 (insert annotation)))
928 (run-mode-hooks 'text-mode-hook))
931 (defun bookmark-send-edited-annotation ()
932 "Use buffer contents as annotation for a bookmark.
933 Lines beginning with `#' are ignored."
934 (interactive)
935 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
936 (error "Not in bookmark-edit-annotation-mode"))
937 (goto-char (point-min))
938 (while (< (point) (point-max))
939 (if (looking-at "^#")
940 (bookmark-kill-line t)
941 (forward-line 1)))
942 (let ((annotation (buffer-string))
943 (bookmark bookmark-annotation-name))
944 (bookmark-set-annotation bookmark annotation)
945 (bookmark-bmenu-surreptitiously-rebuild-list)
946 (goto-char bookmark-current-point))
947 (kill-buffer (current-buffer)))
950 (defun bookmark-edit-annotation (bookmark)
951 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
952 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
953 (bookmark-edit-annotation-mode bookmark))
956 (defun bookmark-insert-current-bookmark ()
957 "Insert this buffer's value of `bookmark-current-bookmark'.
958 Default to file name if it's nil."
959 (interactive)
960 (let ((str
961 (save-excursion
962 (set-buffer bookmark-current-buffer)
963 bookmark-current-bookmark)))
964 (if str (insert str) (bookmark-insert-buffer-name))))
967 (defun bookmark-insert-buffer-name ()
968 "Insert the current file name into the bookmark name being set.
969 The directory part of the file name is not used."
970 (interactive)
971 (let ((str
972 (save-excursion
973 (set-buffer bookmark-current-buffer)
974 (bookmark-buffer-name))))
975 (insert str)))
978 (defun bookmark-buffer-name ()
979 "Return the name of the current buffer's file, non-directory.
980 In Info, return the current node."
981 (cond
982 ;; Are we in Info?
983 ((string-equal mode-name "Info") Info-current-node)
984 ;; Or are we a file?
985 (buffer-file-name (file-name-nondirectory buffer-file-name))
986 ;; Or are we a directory?
987 ((and (boundp 'dired-directory) dired-directory)
988 (let* ((dirname (if (stringp dired-directory)
989 dired-directory
990 (car dired-directory)))
991 (idx (1- (length dirname))))
992 ;; Strip the trailing slash.
993 (if (= ?/ (aref dirname idx))
994 (file-name-nondirectory (substring dirname 0 idx))
995 ;; Else return the current-buffer
996 (buffer-name (current-buffer)))))
997 ;; If all else fails, use the buffer's name.
999 (buffer-name (current-buffer)))))
1002 (defun bookmark-yank-word ()
1003 (interactive)
1004 ;; get the next word from the buffer and append it to the name of
1005 ;; the bookmark currently being set.
1006 (let ((string (save-excursion
1007 (set-buffer bookmark-current-buffer)
1008 (goto-char bookmark-yank-point)
1009 (buffer-substring-no-properties
1010 (point)
1011 (progn
1012 (forward-word 1)
1013 (setq bookmark-yank-point (point)))))))
1014 (insert string)))
1017 (defvar Info-current-file)
1019 (defun bookmark-buffer-file-name ()
1020 "Return the current buffer's file in a way useful for bookmarks.
1021 For example, if this is a Info buffer, return the Info file's name."
1022 (cond
1023 ((eq major-mode 'Info-mode)
1024 Info-current-file)
1025 (buffer-file-name
1026 ;; Abbreviate the path, both so it's shorter and so it's more
1027 ;; portable. E.g., the user's home dir might be a different
1028 ;; path on different machines, but "~/" will still reach it.
1029 (abbreviate-file-name buffer-file-name))
1030 ((and (boundp 'dired-directory) dired-directory)
1031 (if (stringp dired-directory)
1032 dired-directory
1033 (car dired-directory)))))
1036 (defun bookmark-maybe-load-default-file ()
1037 (and (not bookmarks-already-loaded)
1038 (null bookmark-alist)
1039 (prog2
1040 (and
1041 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1042 ;; to be renamed.
1043 (file-exists-p (expand-file-name bookmark-old-default-file))
1044 (not (file-exists-p (expand-file-name bookmark-default-file)))
1045 (rename-file (expand-file-name bookmark-old-default-file)
1046 (expand-file-name bookmark-default-file)))
1047 ;; return t so the `and' will continue...
1050 (file-readable-p (expand-file-name bookmark-default-file))
1051 (bookmark-load bookmark-default-file t t)
1052 (setq bookmarks-already-loaded t)))
1055 (defun bookmark-maybe-sort-alist ()
1056 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
1057 ;;is non-nil, then return a sorted copy of the alist.
1058 (if bookmark-sort-flag
1059 (sort (copy-alist bookmark-alist)
1060 (function
1061 (lambda (x y) (string-lessp (car x) (car y)))))
1062 bookmark-alist))
1065 (defvar bookmark-after-jump-hook nil
1066 "Hook run after `bookmark-jump' jumps to a bookmark.
1067 Useful for example to unhide text in `outline-mode'.")
1069 ;;;###autoload
1070 (defun bookmark-jump (bookmark)
1071 "Jump to bookmark BOOKMARK (a point in some file).
1072 You may have a problem using this function if the value of variable
1073 `bookmark-alist' is nil. If that happens, you need to load in some
1074 bookmarks. See help on function `bookmark-load' for more about
1075 this.
1077 If the file pointed to by BOOKMARK no longer exists, you will be asked
1078 if you wish to give the bookmark a new location, and `bookmark-jump'
1079 will then jump to the new location, as well as recording it in place
1080 of the old one in the permanent bookmark record."
1081 (interactive
1082 (list (bookmark-completing-read "Jump to bookmark"
1083 bookmark-current-bookmark)))
1084 (unless bookmark
1085 (error "No bookmark specified"))
1086 (bookmark-maybe-historicize-string bookmark)
1087 (let ((cell (bookmark-jump-internal bookmark)))
1088 (and cell
1089 (switch-to-buffer (car cell))
1090 (goto-char (cdr cell))
1091 (progn (run-hooks 'bookmark-after-jump-hook) t)
1092 (if bookmark-automatically-show-annotations
1093 ;; if there is an annotation for this bookmark,
1094 ;; show it in a buffer.
1095 (bookmark-show-annotation bookmark)))))
1098 ;;;###autoload
1099 (defun bookmark-jump-other-window (bookmark)
1100 "Jump to BOOKMARK (a point in some file) in another window.
1101 See `bookmark-jump'."
1102 (interactive
1103 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
1104 bookmark-current-bookmark)))
1105 (if (> emacs-major-version 21)
1106 (list bkm) bkm)))
1107 (when bookmark
1108 (bookmark-maybe-historicize-string bookmark)
1109 (let ((cell (bookmark-jump-internal bookmark)))
1110 (and cell
1111 (switch-to-buffer-other-window (car cell))
1112 (goto-char (cdr cell))
1113 (if bookmark-automatically-show-annotations
1114 ;; if there is an annotation for this bookmark,
1115 ;; show it in a buffer.
1116 (bookmark-show-annotation bookmark))))))
1119 (defun bookmark-file-or-variation-thereof (file)
1120 "Return FILE (a string) if it exists, or return a reasonable
1121 variation of FILE if that exists. Reasonable variations are checked
1122 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1123 nor a reasonable variation thereof, then still return FILE if it can
1124 be retrieved from a VC backend, else return nil."
1125 (if (file-exists-p file)
1126 file
1128 (progn (require 'info) ; ensure Info-suffix-list is bound
1129 (catch 'found
1130 (mapc (lambda (elt)
1131 (let ((suffixed-file (concat file (car elt))))
1132 (if (file-exists-p suffixed-file)
1133 (throw 'found suffixed-file))))
1134 Info-suffix-list)
1135 nil))
1136 ;; Last possibility: try VC
1137 (if (vc-backend file) file))))
1139 (defun bookmark-jump-internal (bookmark)
1140 "Call BOOKMARK's handler or `bookmark-jump-noselect' if it has none."
1141 (funcall (or (bookmark-get-handler bookmark)
1142 'bookmark-jump-noselect)
1143 bookmark))
1145 (defun bookmark-jump-noselect (str)
1146 ;; a leetle helper for bookmark-jump :-)
1147 ;; returns (BUFFER . POINT)
1148 (bookmark-maybe-load-default-file)
1149 (let* ((file (expand-file-name (bookmark-get-filename str)))
1150 (forward-str (bookmark-get-front-context-string str))
1151 (behind-str (bookmark-get-rear-context-string str))
1152 (place (bookmark-get-position str))
1153 (info-node (bookmark-get-info-node str))
1154 (orig-file file)
1156 (if (setq file (bookmark-file-or-variation-thereof file))
1157 (save-excursion
1158 (save-window-excursion
1159 (if info-node
1160 ;; Info nodes must be visited with care.
1161 (progn
1162 (require 'info)
1163 (with-no-warnings
1164 (Info-find-node file info-node)))
1165 ;; Else no Info. Can do an ordinary find-file:
1166 (set-buffer (find-file-noselect file))
1167 (goto-char place))
1169 ;; Go searching forward first. Then, if forward-str exists and
1170 ;; was found in the file, we can search backward for behind-str.
1171 ;; Rationale is that if text was inserted between the two in the
1172 ;; file, it's better to be put before it so you can read it,
1173 ;; rather than after and remain perhaps unaware of the changes.
1174 (if forward-str
1175 (if (search-forward forward-str (point-max) t)
1176 (goto-char (match-beginning 0))))
1177 (if behind-str
1178 (if (search-backward behind-str (point-min) t)
1179 (goto-char (match-end 0))))
1180 ;; added by db
1181 (setq bookmark-current-bookmark str)
1182 (cons (current-buffer) (point))))
1184 ;; Else unable to find the marked file, so ask if user wants to
1185 ;; relocate the bookmark, else remind them to consider deletion.
1186 (ding)
1187 (if (y-or-n-p (concat (file-name-nondirectory orig-file)
1188 " nonexistent. Relocate \""
1190 "\"? "))
1191 (progn
1192 (bookmark-relocate str)
1193 ;; gasp! It's a recursive function call in Emacs Lisp!
1194 (bookmark-jump-noselect str))
1195 (message
1196 "Bookmark not relocated; consider removing it \(%s\)." str)
1197 nil))))
1200 ;;;###autoload
1201 (defun bookmark-relocate (bookmark)
1202 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1203 This makes an already existing bookmark point to that file, instead of
1204 the one it used to point at. Useful when a file has been renamed
1205 after a bookmark was set in it."
1206 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1207 (bookmark-maybe-historicize-string bookmark)
1208 (bookmark-maybe-load-default-file)
1209 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1210 (newloc (expand-file-name
1211 (read-file-name
1212 (format "Relocate %s to: " bookmark)
1213 (file-name-directory bmrk-filename)))))
1214 (bookmark-set-filename bookmark newloc)
1215 (bookmark-bmenu-surreptitiously-rebuild-list)))
1218 ;;;###autoload
1219 (defun bookmark-insert-location (bookmark &optional no-history)
1220 "Insert the name of the file associated with BOOKMARK.
1221 Optional second arg NO-HISTORY means don't record this in the
1222 minibuffer history list `bookmark-history'."
1223 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1224 (or no-history (bookmark-maybe-historicize-string bookmark))
1225 (let ((start (point)))
1226 (prog1
1227 (insert (bookmark-location bookmark)) ; *Return this line*
1228 (if (and (display-color-p) (display-mouse-p))
1229 (add-text-properties
1230 start
1231 (save-excursion (re-search-backward
1232 "[^ \t]")
1233 (1+ (point)))
1234 '(mouse-face highlight
1235 follow-link t
1236 help-echo "mouse-2: go to this bookmark in other window"))))))
1238 ;;;###autoload
1239 (defalias 'bookmark-locate 'bookmark-insert-location)
1241 (defun bookmark-location (bookmark)
1242 "Return the name of the file associated with BOOKMARK."
1243 (bookmark-maybe-load-default-file)
1244 (bookmark-get-filename bookmark))
1247 ;;;###autoload
1248 (defun bookmark-rename (old &optional new)
1249 "Change the name of OLD bookmark to NEW name.
1250 If called from keyboard, prompt for OLD and NEW. If called from
1251 menubar, select OLD from a menu and prompt for NEW.
1253 If called from Lisp, prompt for NEW if only OLD was passed as an
1254 argument. If called with two strings, then no prompting is done. You
1255 must pass at least OLD when calling from Lisp.
1257 While you are entering the new name, consecutive C-w's insert
1258 consecutive words from the text of the buffer into the new bookmark
1259 name."
1260 (interactive (list (bookmark-completing-read "Old bookmark name")))
1261 (bookmark-maybe-historicize-string old)
1262 (bookmark-maybe-load-default-file)
1264 (setq bookmark-current-point (point))
1265 (setq bookmark-yank-point (point))
1266 (setq bookmark-current-buffer (current-buffer))
1267 (let ((newname
1268 (or new ; use second arg, if non-nil
1269 (read-from-minibuffer
1270 "New name: "
1272 (let ((now-map (copy-keymap minibuffer-local-map)))
1273 (define-key now-map "\C-w" 'bookmark-yank-word)
1274 now-map)
1276 'bookmark-history))))
1277 (bookmark-set-name old newname)
1278 (setq bookmark-current-bookmark newname)
1279 (bookmark-bmenu-surreptitiously-rebuild-list)
1280 (setq bookmark-alist-modification-count
1281 (1+ bookmark-alist-modification-count))
1282 (if (bookmark-time-to-save-p)
1283 (bookmark-save))))
1286 ;;;###autoload
1287 (defun bookmark-insert (bookmark)
1288 "Insert the text of the file pointed to by bookmark BOOKMARK.
1289 You may have a problem using this function if the value of variable
1290 `bookmark-alist' is nil. If that happens, you need to load in some
1291 bookmarks. See help on function `bookmark-load' for more about
1292 this."
1293 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1294 (bookmark-maybe-historicize-string bookmark)
1295 (bookmark-maybe-load-default-file)
1296 (let ((orig-point (point))
1297 (str-to-insert
1298 (save-excursion
1299 (set-buffer (car (bookmark-jump-internal bookmark)))
1300 (buffer-string))))
1301 (insert str-to-insert)
1302 (push-mark)
1303 (goto-char orig-point)))
1306 ;;;###autoload
1307 (defun bookmark-delete (bookmark &optional batch)
1308 "Delete BOOKMARK from the bookmark list.
1309 Removes only the first instance of a bookmark with that name. If
1310 there are one or more other bookmarks with the same name, they will
1311 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1312 one most recently used in this file, if any\).
1313 Optional second arg BATCH means don't update the bookmark list buffer,
1314 probably because we were called from there."
1315 (interactive
1316 (list (bookmark-completing-read "Delete bookmark"
1317 bookmark-current-bookmark)))
1318 (bookmark-maybe-historicize-string bookmark)
1319 (bookmark-maybe-load-default-file)
1320 (let ((will-go (bookmark-get-bookmark bookmark)))
1321 (setq bookmark-alist (delq will-go bookmark-alist))
1322 ;; Added by db, nil bookmark-current-bookmark if the last
1323 ;; occurrence has been deleted
1324 (or (bookmark-get-bookmark bookmark-current-bookmark)
1325 (setq bookmark-current-bookmark nil)))
1326 ;; Don't rebuild the list
1327 (if batch
1329 (bookmark-bmenu-surreptitiously-rebuild-list)
1330 (setq bookmark-alist-modification-count
1331 (1+ bookmark-alist-modification-count))
1332 (if (bookmark-time-to-save-p)
1333 (bookmark-save))))
1336 (defun bookmark-time-to-save-p (&optional last-time)
1337 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1338 ;; finds out whether it's time to save bookmarks to a file, by
1339 ;; examining the value of variable bookmark-save-flag, and maybe
1340 ;; bookmark-alist-modification-count. Returns t if they should be
1341 ;; saved, nil otherwise. if last-time is non-nil, then this is
1342 ;; being called when emacs is killed.
1343 (cond (last-time
1344 (and (> bookmark-alist-modification-count 0)
1345 bookmark-save-flag))
1346 ((numberp bookmark-save-flag)
1347 (>= bookmark-alist-modification-count bookmark-save-flag))
1349 nil)))
1352 ;;;###autoload
1353 (defun bookmark-write ()
1354 "Write bookmarks to a file (reading the file name with the minibuffer).
1355 Don't use this in Lisp programs; use `bookmark-save' instead."
1356 (interactive)
1357 (bookmark-maybe-load-default-file)
1358 (bookmark-save t))
1361 ;;;###autoload
1362 (defun bookmark-save (&optional parg file)
1363 "Save currently defined bookmarks.
1364 Saves by default in the file defined by the variable
1365 `bookmark-default-file'. With a prefix arg, save it in file FILE
1366 \(second argument\).
1368 If you are calling this from Lisp, the two arguments are PARG and
1369 FILE, and if you just want it to write to the default file, then
1370 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1371 instead. If you pass in one argument, and it is non-nil, then the
1372 user will be interactively queried for a file to save in.
1374 When you want to load in the bookmarks from a file, use
1375 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1376 for a file, defaulting to the file defined by variable
1377 `bookmark-default-file'."
1378 (interactive "P")
1379 (bookmark-maybe-load-default-file)
1380 (cond
1381 ((and (null parg) (null file))
1382 ;;whether interactive or not, write to default file
1383 (bookmark-write-file bookmark-default-file))
1384 ((and (null parg) file)
1385 ;;whether interactive or not, write to given file
1386 (bookmark-write-file file))
1387 ((and parg (not file))
1388 ;;have been called interactively w/ prefix arg
1389 (let ((file (read-file-name "File to save bookmarks in: ")))
1390 (bookmark-write-file file)))
1391 (t ; someone called us with prefix-arg *and* a file, so just write to file
1392 (bookmark-write-file file)))
1393 ;; signal that we have synced the bookmark file by setting this to
1394 ;; 0. If there was an error at any point before, it will not get
1395 ;; set, which is what we want.
1396 (setq bookmark-alist-modification-count 0))
1400 (defun bookmark-write-file (file)
1401 (save-excursion
1402 (save-window-excursion
1403 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1404 (set-buffer (get-buffer-create " *Bookmarks*"))
1405 (goto-char (point-min))
1406 (delete-region (point-min) (point-max))
1407 (let ((print-length nil)
1408 (print-level nil))
1409 (bookmark-insert-file-format-version-stamp)
1410 (pp bookmark-alist (current-buffer))
1411 (let ((version-control
1412 (cond
1413 ((null bookmark-version-control) nil)
1414 ((eq 'never bookmark-version-control) 'never)
1415 ((eq 'nospecial bookmark-version-control) version-control)
1417 t))))
1418 (condition-case nil
1419 (write-region (point-min) (point-max) file)
1420 (file-error (message "Can't write %s" file)))
1421 (kill-buffer (current-buffer))
1422 (bookmark-maybe-message
1423 "Saving bookmarks to file %s...done" file))))))
1426 (defun bookmark-import-new-list (new-list)
1427 ;; Walk over the new list, adding each individual bookmark
1428 ;; carefully. "Carefully" means checking against the existing
1429 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1430 ;; as necessary.
1431 (let ((lst new-list)
1432 (names (bookmark-all-names)))
1433 (while lst
1434 (let* ((full-record (car lst)))
1435 (bookmark-maybe-rename full-record names)
1436 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1437 (setq names (cons (bookmark-name-from-full-record full-record) names))
1438 (setq lst (cdr lst))))))
1441 (defun bookmark-maybe-rename (full-record names)
1442 ;; just a helper for bookmark-import-new-list; it is only for
1443 ;; readability that this is not inlined.
1445 ;; Once this has found a free name, it sets full-record to that
1446 ;; name.
1447 (let ((found-name (bookmark-name-from-full-record full-record)))
1448 (if (member found-name names)
1449 ;; We've got a conflict, so generate a new name
1450 (let ((count 2)
1451 (new-name found-name))
1452 (while (member new-name names)
1453 (setq new-name (concat found-name (format "<%d>" count)))
1454 (setq count (1+ count)))
1455 (bookmark-set-name full-record new-name)))))
1458 ;;;###autoload
1459 (defun bookmark-load (file &optional overwrite no-msg)
1460 "Load bookmarks from FILE (which must be in bookmark format).
1461 Appends loaded bookmarks to the front of the list of bookmarks. If
1462 optional second argument OVERWRITE is non-nil, existing bookmarks are
1463 destroyed. Optional third arg NO-MSG means don't display any messages
1464 while loading.
1466 If you load a file that doesn't contain a proper bookmark alist, you
1467 will corrupt Emacs's bookmark list. Generally, you should only load
1468 in files that were created with the bookmark functions in the first
1469 place. Your own personal bookmark file, `~/.emacs.bmk', is
1470 maintained automatically by Emacs; you shouldn't need to load it
1471 explicitly.
1473 If you load a file containing bookmarks with the same names as
1474 bookmarks already present in your Emacs, the new bookmarks will get
1475 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1476 method buffers use to resolve name collisions."
1477 (interactive
1478 (list (read-file-name
1479 (format "Load bookmarks from: (%s) "
1480 bookmark-default-file)
1481 ;;Default might not be used often,
1482 ;;but there's no better default, and
1483 ;;I guess it's better than none at all.
1484 "~/" bookmark-default-file 'confirm)))
1485 (setq file (expand-file-name file))
1486 (if (file-readable-p file)
1487 (save-excursion
1488 (save-window-excursion
1489 (if (null no-msg)
1490 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1491 (set-buffer (let ((enable-local-variables nil))
1492 (find-file-noselect file)))
1493 (goto-char (point-min))
1494 (bookmark-maybe-upgrade-file-format)
1495 (let ((blist (bookmark-alist-from-buffer)))
1496 (if (listp blist)
1497 (progn
1498 (if overwrite
1499 (progn
1500 (setq bookmark-alist blist)
1501 (setq bookmark-alist-modification-count 0))
1502 ;; else
1503 (bookmark-import-new-list blist)
1504 (setq bookmark-alist-modification-count
1505 (1+ bookmark-alist-modification-count)))
1506 (if (string-equal
1507 (expand-file-name bookmark-default-file)
1508 file)
1509 (setq bookmarks-already-loaded t))
1510 (bookmark-bmenu-surreptitiously-rebuild-list))
1511 (error "Invalid bookmark list in %s" file)))
1512 (kill-buffer (current-buffer)))
1513 (if (null no-msg)
1514 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1515 (error "Cannot read bookmark file %s" file)))
1519 ;;; Code supporting the dired-like bookmark menu. Prefix is
1520 ;;; "bookmark-bmenu" for "buffer-menu":
1523 (defvar bookmark-bmenu-bookmark-column nil)
1526 (defvar bookmark-bmenu-hidden-bookmarks ())
1529 (defvar bookmark-bmenu-mode-map nil)
1532 (if bookmark-bmenu-mode-map
1534 (setq bookmark-bmenu-mode-map (make-keymap))
1535 (suppress-keymap bookmark-bmenu-mode-map t)
1536 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1537 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1538 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1539 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1540 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1541 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1542 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1543 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1544 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1545 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1546 (define-key bookmark-bmenu-mode-map "\C-o"
1547 'bookmark-bmenu-switch-other-window)
1548 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1549 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1550 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1551 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1552 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1553 (define-key bookmark-bmenu-mode-map " " 'next-line)
1554 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1555 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1556 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1557 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1558 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1559 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1560 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1561 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1562 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1563 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1564 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1565 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1566 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1567 (define-key bookmark-bmenu-mode-map [mouse-2]
1568 'bookmark-bmenu-other-window-with-mouse))
1572 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1573 ;; data.
1574 (put 'bookmark-bmenu-mode 'mode-class 'special)
1577 ;; todo: need to display whether or not bookmark exists as a buffer in
1578 ;; flag column.
1580 ;; Format:
1581 ;; FLAGS BOOKMARK [ LOCATION ]
1584 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1585 "Rebuild the Bookmark List if it exists.
1586 Don't affect the buffer ring order."
1587 (if (get-buffer "*Bookmark List*")
1588 (save-excursion
1589 (save-window-excursion
1590 (bookmark-bmenu-list)))))
1593 ;;;###autoload
1594 (defun bookmark-bmenu-list ()
1595 "Display a list of existing bookmarks.
1596 The list is displayed in a buffer named `*Bookmark List*'.
1597 The leftmost column displays a D if the bookmark is flagged for
1598 deletion, or > if it is flagged for displaying."
1599 (interactive)
1600 (bookmark-maybe-load-default-file)
1601 (if (interactive-p)
1602 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1603 (set-buffer (get-buffer-create "*Bookmark List*")))
1604 (let ((inhibit-read-only t))
1605 (erase-buffer)
1606 (insert "% Bookmark\n- --------\n")
1607 (add-text-properties (point-min) (point)
1608 '(font-lock-face bookmark-menu-heading))
1609 (mapc
1610 (lambda (full-record)
1611 ;; if a bookmark has an annotation, prepend a "*"
1612 ;; in the list of bookmarks.
1613 (let ((annotation (bookmark-get-annotation
1614 (bookmark-name-from-full-record full-record))))
1615 (if (and annotation (not (string-equal annotation "")))
1616 (insert " *")
1617 (insert " "))
1618 (let ((start (point)))
1619 (insert (bookmark-name-from-full-record full-record))
1620 (if (and (display-color-p) (display-mouse-p))
1621 (add-text-properties
1622 start
1623 (save-excursion (re-search-backward
1624 "[^ \t]")
1625 (1+ (point)))
1626 '(mouse-face highlight
1627 follow-link t
1628 help-echo "mouse-2: go to this bookmark in other window")))
1629 (insert "\n")
1631 (bookmark-maybe-sort-alist)))
1632 (goto-char (point-min))
1633 (forward-line 2)
1634 (bookmark-bmenu-mode)
1635 (if bookmark-bmenu-toggle-filenames
1636 (bookmark-bmenu-toggle-filenames t)))
1638 ;;;###autoload
1639 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1640 ;;;###autoload
1641 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1645 (defun bookmark-bmenu-mode ()
1646 "Major mode for editing a list of bookmarks.
1647 Each line describes one of the bookmarks in Emacs.
1648 Letters do not insert themselves; instead, they are commands.
1649 Bookmark names preceded by a \"*\" have annotations.
1650 \\<bookmark-bmenu-mode-map>
1651 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1652 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1653 Also show bookmarks marked using m in other windows.
1654 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1655 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1656 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1657 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1658 together with bookmark selected before this one in another window.
1659 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1660 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1661 so the bookmark menu bookmark remains visible in its window.
1662 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1663 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1664 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1665 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1666 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1667 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1668 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1669 With a prefix arg, prompts for a file to save in.
1670 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1671 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1672 With prefix argument, also move up one line.
1673 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1674 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1675 in another buffer.
1676 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1677 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1678 (kill-all-local-variables)
1679 (use-local-map bookmark-bmenu-mode-map)
1680 (setq truncate-lines t)
1681 (setq buffer-read-only t)
1682 (setq major-mode 'bookmark-bmenu-mode)
1683 (setq mode-name "Bookmark Menu")
1684 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1687 (defun bookmark-bmenu-toggle-filenames (&optional show)
1688 "Toggle whether filenames are shown in the bookmark list.
1689 Optional argument SHOW means show them unconditionally."
1690 (interactive)
1691 (cond
1692 (show
1693 (setq bookmark-bmenu-toggle-filenames nil)
1694 (bookmark-bmenu-show-filenames)
1695 (setq bookmark-bmenu-toggle-filenames t))
1696 (bookmark-bmenu-toggle-filenames
1697 (bookmark-bmenu-hide-filenames)
1698 (setq bookmark-bmenu-toggle-filenames nil))
1700 (bookmark-bmenu-show-filenames)
1701 (setq bookmark-bmenu-toggle-filenames t))))
1704 (defun bookmark-bmenu-show-filenames (&optional force)
1705 (if (and (not force) bookmark-bmenu-toggle-filenames)
1706 nil ;already shown, so do nothing
1707 (save-excursion
1708 (save-window-excursion
1709 (goto-char (point-min))
1710 (forward-line 2)
1711 (setq bookmark-bmenu-hidden-bookmarks ())
1712 (let ((inhibit-read-only t))
1713 (while (< (point) (point-max))
1714 (let ((bmrk (bookmark-bmenu-bookmark)))
1715 (setq bookmark-bmenu-hidden-bookmarks
1716 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1717 (let ((start (save-excursion (end-of-line) (point))))
1718 (move-to-column bookmark-bmenu-file-column t)
1719 ;; Strip off `mouse-face' from the white spaces region.
1720 (if (and (display-color-p) (display-mouse-p))
1721 (remove-text-properties start (point)
1722 '(mouse-face nil help-echo nil))))
1723 (delete-region (point) (progn (end-of-line) (point)))
1724 (insert " ")
1725 ;; Pass the NO-HISTORY arg:
1726 (bookmark-insert-location bmrk t)
1727 (forward-line 1))))))))
1730 (defun bookmark-bmenu-hide-filenames (&optional force)
1731 (if (and (not force) bookmark-bmenu-toggle-filenames)
1732 ;; nothing to hide if above is nil
1733 (save-excursion
1734 (save-window-excursion
1735 (goto-char (point-min))
1736 (forward-line 2)
1737 (setq bookmark-bmenu-hidden-bookmarks
1738 (nreverse bookmark-bmenu-hidden-bookmarks))
1739 (save-excursion
1740 (goto-char (point-min))
1741 (search-forward "Bookmark")
1742 (backward-word 1)
1743 (setq bookmark-bmenu-bookmark-column (current-column)))
1744 (save-excursion
1745 (let ((inhibit-read-only t))
1746 (while bookmark-bmenu-hidden-bookmarks
1747 (move-to-column bookmark-bmenu-bookmark-column t)
1748 (bookmark-kill-line)
1749 (let ((start (point)))
1750 (insert (car bookmark-bmenu-hidden-bookmarks))
1751 (if (and (display-color-p) (display-mouse-p))
1752 (add-text-properties
1753 start
1754 (save-excursion (re-search-backward
1755 "[^ \t]")
1756 (1+ (point)))
1757 '(mouse-face highlight
1758 follow-link t
1759 help-echo
1760 "mouse-2: go to this bookmark in other window"))))
1761 (setq bookmark-bmenu-hidden-bookmarks
1762 (cdr bookmark-bmenu-hidden-bookmarks))
1763 (forward-line 1))))))))
1766 (defun bookmark-bmenu-check-position ()
1767 ;; Returns non-nil if on a line with a bookmark.
1768 ;; (The actual value returned is bookmark-alist).
1769 ;; Else reposition and try again, else return nil.
1770 (cond ((< (count-lines (point-min) (point)) 2)
1771 (goto-char (point-min))
1772 (forward-line 2)
1773 bookmark-alist)
1774 ((and (bolp) (eobp))
1775 (beginning-of-line 0)
1776 bookmark-alist)
1778 bookmark-alist)))
1781 (defun bookmark-bmenu-bookmark ()
1782 ;; return a string which is bookmark of this line.
1783 (if (bookmark-bmenu-check-position)
1784 (save-excursion
1785 (save-window-excursion
1786 (goto-char (point-min))
1787 (search-forward "Bookmark")
1788 (backward-word 1)
1789 (setq bookmark-bmenu-bookmark-column (current-column)))))
1790 (if bookmark-bmenu-toggle-filenames
1791 (bookmark-bmenu-hide-filenames))
1792 (save-excursion
1793 (save-window-excursion
1794 (beginning-of-line)
1795 (forward-char bookmark-bmenu-bookmark-column)
1796 (prog1
1797 (buffer-substring-no-properties (point)
1798 (progn
1799 (end-of-line)
1800 (point)))
1801 ;; well, this is certainly crystal-clear:
1802 (if bookmark-bmenu-toggle-filenames
1803 (bookmark-bmenu-toggle-filenames t))))))
1806 (defun bookmark-show-annotation (bookmark)
1807 "Display the annotation for bookmark named BOOKMARK in a buffer,
1808 if an annotation exists."
1809 (let ((annotation (bookmark-get-annotation bookmark)))
1810 (if (and annotation (not (string-equal annotation "")))
1811 (save-excursion
1812 (let ((old-buf (current-buffer)))
1813 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1814 (delete-region (point-min) (point-max))
1815 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1816 (insert annotation)
1817 (goto-char (point-min))
1818 (pop-to-buffer old-buf))))))
1821 (defun bookmark-show-all-annotations ()
1822 "Display the annotations for all bookmarks in a buffer."
1823 (let ((old-buf (current-buffer)))
1824 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1825 (delete-region (point-min) (point-max))
1826 (mapc
1827 (lambda (full-record)
1828 (let* ((name (bookmark-name-from-full-record full-record))
1829 (ann (bookmark-get-annotation name)))
1830 (insert (concat name ":\n"))
1831 (if (and ann (not (string-equal ann "")))
1832 ;; insert the annotation, indented by 4 spaces.
1833 (progn
1834 (save-excursion (insert ann) (unless (bolp)
1835 (insert "\n")))
1836 (while (< (point) (point-max))
1837 (beginning-of-line) ; paranoia
1838 (insert " ")
1839 (forward-line)
1840 (end-of-line))))))
1841 bookmark-alist)
1842 (goto-char (point-min))
1843 (pop-to-buffer old-buf)))
1846 (defun bookmark-bmenu-mark ()
1847 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1848 (interactive)
1849 (beginning-of-line)
1850 (if (bookmark-bmenu-check-position)
1851 (let ((inhibit-read-only t))
1852 (delete-char 1)
1853 (insert ?>)
1854 (forward-line 1)
1855 (bookmark-bmenu-check-position))))
1858 (defun bookmark-bmenu-select ()
1859 "Select this line's bookmark; also display bookmarks marked with `>'.
1860 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1861 (interactive)
1862 (if (bookmark-bmenu-check-position)
1863 (let ((bmrk (bookmark-bmenu-bookmark))
1864 (menu (current-buffer))
1865 (others ())
1866 tem)
1867 (goto-char (point-min))
1868 (while (re-search-forward "^>" nil t)
1869 (setq tem (bookmark-bmenu-bookmark))
1870 (let ((inhibit-read-only t))
1871 (delete-char -1)
1872 (insert ?\s))
1873 (or (string-equal tem bmrk)
1874 (member tem others)
1875 (setq others (cons tem others))))
1876 (setq others (nreverse others)
1877 tem (/ (1- (frame-height)) (1+ (length others))))
1878 (delete-other-windows)
1879 (bookmark-jump bmrk)
1880 (bury-buffer menu)
1881 (if others
1882 (while others
1883 (split-window nil tem)
1884 (other-window 1)
1885 (bookmark-jump (car others))
1886 (setq others (cdr others)))
1887 (other-window 1)))))
1890 (defun bookmark-bmenu-save (parg)
1891 "Save the current list into a bookmark file.
1892 With a prefix arg, prompts for a file to save them in."
1893 (interactive "P")
1894 (save-excursion
1895 (save-window-excursion
1896 (bookmark-save parg))))
1899 (defun bookmark-bmenu-load ()
1900 "Load the bookmark file and rebuild the bookmark menu-buffer."
1901 (interactive)
1902 (if (bookmark-bmenu-check-position)
1903 (save-excursion
1904 (save-window-excursion
1905 ;; This will call `bookmark-bmenu-list'
1906 (call-interactively 'bookmark-load)))))
1909 (defun bookmark-bmenu-1-window ()
1910 "Select this line's bookmark, alone, in full frame."
1911 (interactive)
1912 (if (bookmark-bmenu-check-position)
1913 (progn
1914 (bookmark-jump (bookmark-bmenu-bookmark))
1915 (bury-buffer (other-buffer))
1916 (delete-other-windows))))
1919 (defun bookmark-bmenu-2-window ()
1920 "Select this line's bookmark, with previous buffer in second window."
1921 (interactive)
1922 (if (bookmark-bmenu-check-position)
1923 (let ((bmrk (bookmark-bmenu-bookmark))
1924 (menu (current-buffer))
1925 (pop-up-windows t))
1926 (delete-other-windows)
1927 (switch-to-buffer (other-buffer))
1928 (let* ((pair (bookmark-jump-internal bmrk))
1929 (buff (car pair))
1930 (pos (cdr pair)))
1931 (pop-to-buffer buff)
1932 (goto-char pos))
1933 (bury-buffer menu))))
1936 (defun bookmark-bmenu-this-window ()
1937 "Select this line's bookmark in this window."
1938 (interactive)
1939 (if (bookmark-bmenu-check-position)
1940 (bookmark-jump (bookmark-bmenu-bookmark))))
1943 (defun bookmark-bmenu-other-window ()
1944 "Select this line's bookmark in other window, leaving bookmark menu visible."
1945 (interactive)
1946 (let ((bookmark (bookmark-bmenu-bookmark)))
1947 (if (bookmark-bmenu-check-position)
1948 (let* ((pair (bookmark-jump-internal bookmark))
1949 (buff (car pair))
1950 (pos (cdr pair)))
1951 (switch-to-buffer-other-window buff)
1952 (goto-char pos)
1953 (set-window-point (get-buffer-window buff) pos)
1954 (bookmark-show-annotation bookmark)))))
1957 (defun bookmark-bmenu-switch-other-window ()
1958 "Make the other window select this line's bookmark.
1959 The current window remains selected."
1960 (interactive)
1961 (let ((bookmark (bookmark-bmenu-bookmark))
1962 (pop-up-windows t)
1963 same-window-buffer-names
1964 same-window-regexps)
1965 (if (bookmark-bmenu-check-position)
1966 (let* ((pair (bookmark-jump-internal bookmark))
1967 (buff (car pair))
1968 (pos (cdr pair)))
1969 (display-buffer buff)
1970 (let ((o-buffer (current-buffer)))
1971 ;; save-excursion won't do
1972 (set-buffer buff)
1973 (goto-char pos)
1974 (set-window-point (get-buffer-window buff) pos)
1975 (set-buffer o-buffer))
1976 (bookmark-show-annotation bookmark)))))
1978 (defun bookmark-bmenu-other-window-with-mouse (event)
1979 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1980 (interactive "e")
1981 (save-excursion
1982 (set-buffer (window-buffer (posn-window (event-end event))))
1983 (save-excursion
1984 (goto-char (posn-point (event-end event)))
1985 (bookmark-bmenu-other-window))))
1988 (defun bookmark-bmenu-show-annotation ()
1989 "Show the annotation for the current bookmark in another window."
1990 (interactive)
1991 (let ((bookmark (bookmark-bmenu-bookmark)))
1992 (if (bookmark-bmenu-check-position)
1993 (bookmark-show-annotation bookmark))))
1996 (defun bookmark-bmenu-show-all-annotations ()
1997 "Show the annotation for all bookmarks in another window."
1998 (interactive)
1999 (bookmark-show-all-annotations))
2002 (defun bookmark-bmenu-edit-annotation ()
2003 "Edit the annotation for the current bookmark in another window."
2004 (interactive)
2005 (let ((bookmark (bookmark-bmenu-bookmark)))
2006 (if (bookmark-bmenu-check-position)
2007 (bookmark-edit-annotation bookmark))))
2010 (defun bookmark-bmenu-unmark (&optional backup)
2011 "Cancel all requested operations on bookmark on this line and move down.
2012 Optional BACKUP means move up."
2013 (interactive "P")
2014 (beginning-of-line)
2015 (if (bookmark-bmenu-check-position)
2016 (progn
2017 (let ((inhibit-read-only t))
2018 (delete-char 1)
2019 ;; any flags to reset according to circumstances? How about a
2020 ;; flag indicating whether this bookmark is being visited?
2021 ;; well, we don't have this now, so maybe later.
2022 (insert " "))
2023 (forward-line (if backup -1 1))
2024 (bookmark-bmenu-check-position))))
2027 (defun bookmark-bmenu-backup-unmark ()
2028 "Move up and cancel all requested operations on bookmark on line above."
2029 (interactive)
2030 (forward-line -1)
2031 (if (bookmark-bmenu-check-position)
2032 (progn
2033 (bookmark-bmenu-unmark)
2034 (forward-line -1)
2035 (bookmark-bmenu-check-position))))
2038 (defun bookmark-bmenu-delete ()
2039 "Mark bookmark on this line to be deleted.
2040 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2041 (interactive)
2042 (beginning-of-line)
2043 (if (bookmark-bmenu-check-position)
2044 (let ((inhibit-read-only t))
2045 (delete-char 1)
2046 (insert ?D)
2047 (forward-line 1)
2048 (bookmark-bmenu-check-position))))
2051 (defun bookmark-bmenu-delete-backwards ()
2052 "Mark bookmark on this line to be deleted, then move up one line.
2053 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2054 (interactive)
2055 (bookmark-bmenu-delete)
2056 (forward-line -2)
2057 (if (bookmark-bmenu-check-position)
2058 (forward-line 1))
2059 (bookmark-bmenu-check-position))
2062 (defun bookmark-bmenu-execute-deletions ()
2063 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2064 (interactive)
2065 (message "Deleting bookmarks...")
2066 (let ((hide-em bookmark-bmenu-toggle-filenames)
2067 (o-point (point))
2068 (o-str (save-excursion
2069 (beginning-of-line)
2070 (if (looking-at "^D")
2072 (buffer-substring
2073 (point)
2074 (progn (end-of-line) (point))))))
2075 (o-col (current-column)))
2076 (if hide-em (bookmark-bmenu-hide-filenames))
2077 (setq bookmark-bmenu-toggle-filenames nil)
2078 (goto-char (point-min))
2079 (forward-line 1)
2080 (while (re-search-forward "^D" (point-max) t)
2081 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2082 (bookmark-bmenu-list)
2083 (setq bookmark-bmenu-toggle-filenames hide-em)
2084 (if bookmark-bmenu-toggle-filenames
2085 (bookmark-bmenu-toggle-filenames t))
2086 (if o-str
2087 (progn
2088 (goto-char (point-min))
2089 (search-forward o-str)
2090 (beginning-of-line)
2091 (forward-char o-col))
2092 (goto-char o-point))
2093 (beginning-of-line)
2094 (setq bookmark-alist-modification-count
2095 (1+ bookmark-alist-modification-count))
2096 (if (bookmark-time-to-save-p)
2097 (bookmark-save))
2098 (message "Deleting bookmarks...done")
2102 (defun bookmark-bmenu-rename ()
2103 "Rename bookmark on current line. Prompts for a new name."
2104 (interactive)
2105 (if (bookmark-bmenu-check-position)
2106 (let ((bmrk (bookmark-bmenu-bookmark))
2107 (thispoint (point)))
2108 (bookmark-rename bmrk)
2109 (bookmark-bmenu-list)
2110 (goto-char thispoint))))
2113 (defun bookmark-bmenu-locate ()
2114 "Display location of this bookmark. Displays in the minibuffer."
2115 (interactive)
2116 (if (bookmark-bmenu-check-position)
2117 (let ((bmrk (bookmark-bmenu-bookmark)))
2118 (message "%s" (bookmark-location bmrk)))))
2120 (defun bookmark-bmenu-relocate ()
2121 "Change the file path of the bookmark on the current line,
2122 prompting with completion for the new path."
2123 (interactive)
2124 (if (bookmark-bmenu-check-position)
2125 (let ((bmrk (bookmark-bmenu-bookmark))
2126 (thispoint (point)))
2127 (bookmark-relocate bmrk)
2128 (goto-char thispoint))))
2131 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2133 (defun bookmark-menu-popup-paned-menu (event name entries)
2134 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2135 That is, ENTRIES is a list of strings which appear as the choices
2136 in the menu.
2137 The number of panes depends on the number of entries.
2138 The visible entries are truncated to `bookmark-menu-length', but the
2139 strings returned are not."
2140 (let ((f-height (/ (frame-height) 2))
2141 (pane-list nil)
2142 (iter 0))
2143 (while entries
2144 (let (lst
2145 (count 0))
2146 (while (and (< count f-height) entries)
2147 (let ((str (car entries)))
2148 (push (cons
2149 (if (> (length str) bookmark-menu-length)
2150 (substring str 0 bookmark-menu-length)
2151 str)
2152 str)
2153 lst)
2154 (setq entries (cdr entries))
2155 (setq count (1+ count))))
2156 (setq iter (1+ iter))
2157 (push (cons
2158 (format "-*- %s (%d) -*-" name iter)
2159 (nreverse lst))
2160 pane-list)))
2162 ;; Popup the menu and return the string.
2163 (x-popup-menu event (cons (concat "-*- " name " -*-")
2164 (nreverse pane-list)))))
2167 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2168 ;; following works, and for explaining what to do to make it work.
2170 ;; We MUST autoload EACH form used to set up this variable's value, so
2171 ;; that the whole job is done in loaddefs.el.
2173 ;; Emacs menubar stuff.
2175 ;;;###autoload
2176 (defvar menu-bar-bookmark-map
2177 (let ((map (make-sparse-keymap "Bookmark functions")))
2178 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2179 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2180 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2181 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2182 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2183 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2184 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2185 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2186 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2187 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2188 map))
2190 ;;;###autoload
2191 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2193 ;; make bookmarks appear toward the right side of the menu.
2194 (if (boundp 'menu-bar-final-items)
2195 (if menu-bar-final-items
2196 (setq menu-bar-final-items
2197 (cons 'bookmark menu-bar-final-items)))
2198 (setq menu-bar-final-items '(bookmark)))
2200 ;;;; end bookmark menu stuff ;;;;
2203 ;;; Load Hook
2204 (defvar bookmark-load-hook nil
2205 "Hook run at the end of loading bookmark.")
2207 ;;; Exit Hook, called from kill-emacs-hook
2208 (defvar bookmark-exit-hook nil
2209 "Hook run when Emacs exits.")
2211 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2213 (defun bookmark-exit-hook-internal ()
2214 "Save bookmark state, if necessary, at Emacs exit time.
2215 This also runs `bookmark-exit-hook'."
2216 (run-hooks 'bookmark-exit-hook)
2217 (and bookmark-alist
2218 (bookmark-time-to-save-p t)
2219 (bookmark-save)))
2221 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2224 (run-hooks 'bookmark-load-hook)
2226 (provide 'bookmark)
2228 ;;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2229 ;;; bookmark.el ends here