Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / bookmark.el
blobec8e20d2c7acbf34fe5ba23841777890dedfeb95
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, 2008 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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package is for setting "bookmarks" in files. A bookmark
29 ;; associates a string with a location in a certain file. Thus, you
30 ;; can navigate your way to that location by providing the string.
31 ;; See the "User Variables" section for customizations.
33 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
34 ;; then implementing the bookmark-current-bookmark idea. He even
35 ;; sent *patches*, bless his soul...
37 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
38 ;; fixing and improving bookmark-time-to-save-p.
40 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
41 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
42 ;; and the menu-bar).
44 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
45 ;; suggestions and the code to implement them (like
46 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
47 ;; stuff).
49 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
50 ;; for his eminently sensible suggestion to separate bookmark-jump
51 ;; into bookmark-jump and bookmark-jump-noselect, which made many
52 ;; other things cleaner as well.
54 ;; Thanks to Roland McGrath for encouragement and help with defining
55 ;; autoloads on the menu-bar.
57 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
58 ;; values in bookmark-jump and bookmark-set. Everybody please keep
59 ;; all the keystrokes they save thereby and send them to him at the
60 ;; end of each year :-) (No, seriously, thanks Jonathan!)
62 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
63 ;; thinking up the annotations feature and implementing it so well.
65 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
66 ;; <olstad@msc.edu>.
68 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
69 ;; reported and fixed.
71 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
73 ;; Enough with the credits already, get on to the good stuff:
75 ;; FAVORITE CHINESE RESTAURANT:
76 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
77 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
79 ;;; Code:
81 (require 'pp)
83 ;;; Misc comments:
85 ;; If variable bookmark-use-annotations is non-nil, an annotation is
86 ;; queried for when setting a bookmark.
88 ;; The bookmark list is sorted lexically by default, but you can turn
89 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
90 ;; the list will be presented in the order it is recorded
91 ;; (chronologically), which is actually fairly useful as well.
93 ;;; User Variables
95 (defgroup bookmark nil
96 "Setting, annotation and jumping to bookmarks."
97 :group 'matching)
100 (defcustom bookmark-use-annotations nil
101 "If non-nil, saving a bookmark queries for an annotation in a buffer."
102 :type 'boolean
103 :group 'bookmark)
106 (defcustom bookmark-save-flag t
107 "Controls when Emacs saves bookmarks to a file.
108 --> nil means never save bookmarks, except when `bookmark-save' is
109 explicitly called \(\\[bookmark-save]\).
110 --> t means save bookmarks when Emacs is killed.
111 --> Otherwise, it should be a number that is the frequency with which
112 the bookmark list is saved \(i.e.: the number of times which
113 Emacs' bookmark list may be modified before it is automatically
114 saved.\). If it is a number, Emacs will also automatically save
115 bookmarks when it is killed.
117 Therefore, the way to get it to save every time you make or delete a
118 bookmark is to set this variable to 1 \(or 0, which produces the same
119 behavior.\)
121 To specify the file in which to save them, modify the variable
122 `bookmark-default-file', which is `~/.emacs.bmk' by default."
123 :type '(choice (const nil) integer (other t))
124 :group 'bookmark)
127 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
128 "*The `.emacs.bmk' file used to be called this name.")
131 ;; defvarred to avoid a compilation warning:
132 (defvar bookmark-file nil
133 "Old name for `bookmark-default-file'.")
135 (defcustom bookmark-default-file
136 (if bookmark-file
137 ;; In case user set `bookmark-file' in her .emacs:
138 bookmark-file
139 (convert-standard-filename "~/.emacs.bmk"))
140 "File in which to save bookmarks by default."
141 :type 'file
142 :group 'bookmark)
145 (defcustom bookmark-version-control 'nospecial
146 "Whether or not to make numbered backups of the bookmark file.
147 It can have four values: t, nil, `never', and `nospecial'.
148 The first three have the same meaning that they do for the
149 variable `version-control', and the final value `nospecial' means just
150 use the value of `version-control'."
151 :type '(choice (const nil) (const never) (const nospecial)
152 (other t))
153 :group 'bookmark)
156 (defcustom bookmark-completion-ignore-case t
157 "Non-nil means bookmark functions ignore case in completion."
158 :type 'boolean
159 :group 'bookmark)
162 (defcustom bookmark-sort-flag t
163 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
164 Otherwise they will be displayed in LIFO order (that is, most
165 recently set ones come first, oldest ones come last)."
166 :type 'boolean
167 :group 'bookmark)
170 (defcustom bookmark-automatically-show-annotations t
171 "Non-nil means show annotations when jumping to a bookmark."
172 :type 'boolean
173 :group 'bookmark)
176 (defcustom bookmark-bmenu-file-column 30
177 "Column at which to display filenames in a buffer listing bookmarks.
178 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
179 :type 'integer
180 :group 'bookmark)
183 (defcustom bookmark-bmenu-toggle-filenames t
184 "Non-nil means show filenames when listing bookmarks.
185 This may result in truncated bookmark names. To disable this, put the
186 following in your `.emacs' file:
188 \(setq bookmark-bmenu-toggle-filenames nil\)"
189 :type 'boolean
190 :group 'bookmark)
193 (defcustom bookmark-menu-length 70
194 "Maximum length of a bookmark name displayed on a popup menu."
195 :type 'integer
196 :group 'bookmark)
199 (defface bookmark-menu-heading
200 '((t (:inherit font-lock-type-face)))
201 "Face used to highlight the heading in bookmark menu buffers."
202 :group 'bookmark
203 :version "22.1")
206 ;;; No user-serviceable parts beyond this point.
208 ;; Added for lucid emacs compatibility, db
209 (or (fboundp 'defalias) (fset 'defalias 'fset))
211 ;; suggested for lucid compatibility by david hughes:
212 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
215 ;;; Keymap stuff:
217 ;; Set up these bindings dumping time *only*;
218 ;; if the user alters them, don't override the user when loading bookmark.el.
220 ;;;###autoload (define-key ctl-x-map "rb" 'bookmark-jump)
221 ;;;###autoload (define-key ctl-x-map "rm" 'bookmark-set)
222 ;;;###autoload (define-key ctl-x-map "rl" 'bookmark-bmenu-list)
224 ;;;###autoload
225 (defvar bookmark-map
226 (let ((map (make-sparse-keymap)))
227 ;; Read the help on all of these functions for details...
228 (define-key map "x" 'bookmark-set)
229 (define-key map "m" 'bookmark-set) ;"m"ark
230 (define-key map "j" 'bookmark-jump)
231 (define-key map "g" 'bookmark-jump) ;"g"o
232 (define-key map "o" 'bookmark-jump-other-window)
233 (define-key map "i" 'bookmark-insert)
234 (define-key map "e" 'edit-bookmarks)
235 (define-key map "f" 'bookmark-insert-location) ;"f"ind
236 (define-key map "r" 'bookmark-rename)
237 (define-key map "d" 'bookmark-delete)
238 (define-key map "l" 'bookmark-load)
239 (define-key map "w" 'bookmark-write)
240 (define-key map "s" 'bookmark-save)
241 map)
242 "Keymap containing bindings to bookmark functions.
243 It is not bound to any key by default: to bind it
244 so that you have a bookmark prefix, just use `global-set-key' and bind a
245 key of your choice to `bookmark-map'. All interactive bookmark
246 functions have a binding in this keymap.")
248 ;;;###autoload (fset 'bookmark-map bookmark-map)
251 ;;; Core variables and data structures:
252 (defvar bookmark-alist ()
253 "Association list of bookmarks and their records.
254 You probably don't want to change the value of this alist yourself;
255 instead, let the various bookmark functions do it for you.
257 The format of the alist is
259 \(BOOKMARK1 BOOKMARK2 ...\)
261 where each BOOKMARK is typically of the form
263 \(NAME
264 (filename . FILE)
265 (front-context-string . FRONT-STR)
266 (rear-context-string . REAR-STR)
267 (position . POS)
268 (annotation . ANNOTATION))
270 So the cdr of each bookmark is an alist too.")
273 (defvar bookmarks-already-loaded nil)
276 ;; more stuff added by db.
278 (defvar bookmark-current-bookmark nil
279 "Name of bookmark most recently used in the current file.
280 It is buffer local, used to make moving a bookmark forward
281 through a file easier.")
283 (make-variable-buffer-local 'bookmark-current-bookmark)
286 (defvar bookmark-alist-modification-count 0
287 "Number of modifications to bookmark list since it was last saved.")
290 (defvar bookmark-search-size 16
291 "Length of the context strings recorded on either side of a bookmark.")
294 (defvar bookmark-current-point 0)
295 (defvar bookmark-yank-point 0)
296 (defvar bookmark-current-buffer nil)
298 (defvar Info-suffix-list)
300 ;; Helper functions.
302 ;; Only functions on this page and the next one (file formats) need to
303 ;; know anything about the format of bookmark-alist entries.
304 ;; Everyone else should go through them.
307 (defun bookmark-name-from-full-record (full-record)
308 "Return name of FULL-RECORD \(an alist element instead of a string\)."
309 (car full-record))
312 (defun bookmark-all-names ()
313 "Return a list of all current bookmark names."
314 (bookmark-maybe-load-default-file)
315 (mapcar
316 (lambda (full-record)
317 (bookmark-name-from-full-record full-record))
318 bookmark-alist))
321 (defun bookmark-get-bookmark (bookmark)
322 "Return the full entry for BOOKMARK in `bookmark-alist'.
323 If BOOKMARK is not a string, return nil."
324 (when (stringp bookmark)
325 (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
328 (defun bookmark-get-bookmark-record (bookmark)
329 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
330 That is, all information but the name."
331 (car (cdr (bookmark-get-bookmark bookmark))))
334 (defun bookmark-set-name (bookmark newname)
335 "Set BOOKMARK's name to NEWNAME."
336 (setcar
337 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
338 newname))
340 (defun bookmark-prop-get (bookmark prop)
341 "Return the property PROP of BOOKMARK, or nil if none."
342 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
344 (defun bookmark-prop-set (bookmark prop val)
345 "Set the property PROP of BOOKMARK to VAL."
346 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
347 (if cell
348 (setcdr cell val)
349 (nconc (bookmark-get-bookmark-record bookmark)
350 (list (cons prop val))))))
352 (defun bookmark-get-annotation (bookmark)
353 "Return the annotation of BOOKMARK, or nil if none."
354 (bookmark-prop-get bookmark 'annotation))
356 (defun bookmark-set-annotation (bookmark ann)
357 "Set the annotation of BOOKMARK to ANN."
358 (bookmark-prop-set bookmark 'annotation ann))
361 (defun bookmark-get-filename (bookmark)
362 "Return the full filename of BOOKMARK."
363 (bookmark-prop-get bookmark 'filename))
366 (defun bookmark-set-filename (bookmark filename)
367 "Set the full filename of BOOKMARK to FILENAME."
368 (bookmark-prop-set bookmark 'filename filename)
369 (setq bookmark-alist-modification-count
370 (1+ bookmark-alist-modification-count))
371 (if (bookmark-time-to-save-p)
372 (bookmark-save)))
375 (defun bookmark-get-position (bookmark)
376 "Return the position \(i.e.: point\) of BOOKMARK."
377 (bookmark-prop-get bookmark 'position))
380 (defun bookmark-set-position (bookmark position)
381 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
382 (bookmark-prop-set bookmark 'position position))
385 (defun bookmark-get-front-context-string (bookmark)
386 "Return the front-context-string of BOOKMARK."
387 (bookmark-prop-get bookmark 'front-context-string))
390 (defun bookmark-set-front-context-string (bookmark string)
391 "Set the front-context-string of BOOKMARK to STRING."
392 (bookmark-prop-set bookmark 'front-context-string string))
395 (defun bookmark-get-rear-context-string (bookmark)
396 "Return the rear-context-string of BOOKMARK."
397 (bookmark-prop-get bookmark 'rear-context-string))
400 (defun bookmark-set-rear-context-string (bookmark string)
401 "Set the rear-context-string of BOOKMARK to STRING."
402 (bookmark-prop-set bookmark 'rear-context-string string))
405 (defun bookmark-get-handler (bookmark)
406 (bookmark-prop-get bookmark 'handler))
408 (defvar bookmark-history nil
409 "The history list for bookmark functions.")
412 (defun bookmark-completing-read (prompt &optional default)
413 "Prompting with PROMPT, read a bookmark name in completion.
414 PROMPT will get a \": \" stuck on the end no matter what, so you
415 probably don't want to include one yourself.
416 Optional second arg DEFAULT is a string to return if the user enters
417 the empty string."
418 (bookmark-maybe-load-default-file) ; paranoia
419 (if (listp last-nonmenu-event)
420 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
421 (let* ((completion-ignore-case bookmark-completion-ignore-case)
422 (default default)
423 (prompt (if default
424 (concat prompt (format " (%s): " default))
425 (concat prompt ": ")))
426 (str
427 (completing-read prompt
428 bookmark-alist
432 'bookmark-history)))
433 (if (string-equal "" str) default str))))
436 (defmacro bookmark-maybe-historicize-string (string)
437 "Put STRING into the bookmark prompt history, if caller non-interactive.
438 We need this because sometimes bookmark functions are invoked from
439 menus, so `completing-read' never gets a chance to set `bookmark-history'."
440 `(or
441 (interactive-p)
442 (setq bookmark-history (cons ,string bookmark-history))))
444 (defvar bookmark-make-record-function 'bookmark-make-record-for-text-file
445 "A function that should be called to create a bookmark record.
446 Modes may set this variable buffer-locally to enable bookmarking of
447 locations that should be treated specially, such as Info nodes,
448 news posts, images, pdf documents, etc.
450 The function will be called with no arguments.
452 The returned record should be a cons cell of the form (NAME . ALIST)
453 where ALIST is as described in `bookmark-alist' and may typically contain
454 a special cons (handler . SOME-FUNCTION) which sets the handler function
455 that should be used to open this bookmark instead of
456 `bookmark-default-handler'. The handler should return an alist like the
457 one that function returns, and (of course) should likewise
458 not select the buffer.
459 It should signal a user error if it is unable to construct a record for the current
460 location.
462 NAME is a suggested name for the constructed bookmark. It can be nil
463 in which case a default heuristic will be used.")
465 (defun bookmark-make-record ()
466 "Return a new bookmark record (NAME . ALIST) for the current location."
467 (let ((record (funcall bookmark-make-record-function)))
468 ;; Set up default name.
469 (if (stringp (car record))
470 ;; The function already provided a default name.
471 record
472 (if (car record) (push nil record))
473 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
474 record)))
476 (defun bookmark-store (name alist no-overwrite)
477 "Store the bookmark NAME with data ALIST.
478 If NO-OVERWRITE is non-nil and another bookmark of the same name already
479 exists in `bookmark-alist', record the new bookmark without throwing away the
480 old one."
481 (bookmark-maybe-load-default-file)
482 (let ((stripped-name (copy-sequence name)))
483 (or (featurep 'xemacs)
484 ;; XEmacs's `set-text-properties' doesn't work on
485 ;; free-standing strings, apparently.
486 (set-text-properties 0 (length stripped-name) nil stripped-name))
487 (if (and (bookmark-get-bookmark stripped-name) (not no-overwrite))
488 ;; already existing bookmark under that name and
489 ;; no prefix arg means just overwrite old bookmark
490 (setcdr (bookmark-get-bookmark stripped-name) (list alist))
492 ;; otherwise just cons it onto the front (either the bookmark
493 ;; doesn't exist already, or there is no prefix arg. In either
494 ;; case, we want the new bookmark consed onto the alist...)
496 (push (list stripped-name alist) bookmark-alist))
498 ;; Added by db
499 (setq bookmark-current-bookmark stripped-name)
500 (setq bookmark-alist-modification-count
501 (1+ bookmark-alist-modification-count))
502 (if (bookmark-time-to-save-p)
503 (bookmark-save))
505 (setq bookmark-current-bookmark stripped-name)
506 (bookmark-bmenu-surreptitiously-rebuild-list)))
508 (defun bookmark-make-record-for-text-file ()
509 "Return the record describing the location of a new bookmark.
510 Must be at the correct position in the buffer in which the bookmark is
511 being set (this might change someday)."
512 `((filename . ,(bookmark-buffer-file-name))
513 (front-context-string
514 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
515 (buffer-substring-no-properties
516 (point)
517 (+ (point) bookmark-search-size))
518 nil))
519 (rear-context-string
520 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
521 (buffer-substring-no-properties
522 (point)
523 (- (point) bookmark-search-size))
524 nil))
525 (position . ,(point))))
528 ;;; File format stuff
530 ;; The OLD format of the bookmark-alist was:
532 ;; ((BOOKMARK-NAME . (FILENAME
533 ;; STRING-IN-FRONT
534 ;; STRING-BEHIND
535 ;; POINT))
536 ;; ...)
538 ;; The NEW format of the bookmark-alist is:
540 ;; ((BOOKMARK-NAME (filename . FILENAME)
541 ;; (front-context-string . STRING-IN-FRONT)
542 ;; (rear-context-string . STRING-BEHIND)
543 ;; (position . POINT)
544 ;; (annotation . ANNOTATION)
545 ;; (whatever . VALUE)
546 ;; ...
547 ;; ))
548 ;; ...)
551 ;; I switched to using an internal as well as external alist because I
552 ;; felt that would be a more flexible framework in which to add
553 ;; features. It means that the order in which values appear doesn't
554 ;; matter, and it means that arbitrary values can be added without
555 ;; risk of interfering with existing ones.
557 ;; BOOKMARK-NAME is the string the user gives the bookmark and
558 ;; accesses it by from then on.
560 ;; FILENAME is the location of the file in which the bookmark is set.
562 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
563 ;; context in front of the point at which the bookmark is set.
565 ;; STRING-BEHIND is the same thing, but after the point.
567 ;; The context strings exist so that modifications to a file don't
568 ;; necessarily cause a bookmark's position to be invalidated.
569 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
570 ;; case the file has changed since the bookmark was set. It will
571 ;; attempt to place the user before the changes, if there were any.
572 ;; ANNOTATION is the annotation for the bookmark; it may not exist
573 ;; (for backward compatibility), be nil (no annotation), or be a
574 ;; string.
577 (defconst bookmark-file-format-version 1
578 "The current version of the format used by bookmark files.
579 You should never need to change this.")
582 (defconst bookmark-end-of-version-stamp-marker
583 "-*- End Of Bookmark File Format Version Stamp -*-\n"
584 "This string marks the end of the version stamp in a bookmark file.")
587 (defun bookmark-alist-from-buffer ()
588 "Return a `bookmark-alist' (in any format) from the current buffer.
589 The buffer must of course contain bookmark format information.
590 Does not care from where in the buffer it is called, and does not
591 affect point."
592 (save-excursion
593 (goto-char (point-min))
594 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
595 (read (current-buffer))
596 ;; Else we're dealing with format version 0
597 (if (search-forward "(" nil t)
598 (progn
599 (forward-char -1)
600 (read (current-buffer)))
601 ;; Else no hope of getting information here.
602 (error "Not bookmark format")))))
605 (defun bookmark-upgrade-version-0-alist (old-list)
606 "Upgrade a version 0 alist OLD-LIST to the current version."
607 (mapcar
608 (lambda (bookmark)
609 (let* ((name (car bookmark))
610 (record (car (cdr bookmark)))
611 (filename (nth 0 record))
612 (front-str (nth 1 record))
613 (rear-str (nth 2 record))
614 (position (nth 3 record))
615 (ann (nth 4 record)))
616 (list
617 name
618 `((filename . ,filename)
619 (front-context-string . ,(or front-str ""))
620 (rear-context-string . ,(or rear-str ""))
621 (position . ,position)
622 (annotation . ,ann)))))
623 old-list))
626 (defun bookmark-upgrade-file-format-from-0 ()
627 "Upgrade a bookmark file of format 0 (the original format) to format 1.
628 This expects to be called from `point-min' in a bookmark file."
629 (message "Upgrading bookmark format from 0 to %d..."
630 bookmark-file-format-version)
631 (let* ((old-list (bookmark-alist-from-buffer))
632 (new-list (bookmark-upgrade-version-0-alist old-list)))
633 (delete-region (point-min) (point-max))
634 (bookmark-insert-file-format-version-stamp)
635 (pp new-list (current-buffer))
636 (save-buffer))
637 (goto-char (point-min))
638 (message "Upgrading bookmark format from 0 to %d...done"
639 bookmark-file-format-version)
643 (defun bookmark-grok-file-format-version ()
644 "Return an integer which is the file-format version of this bookmark file.
645 This expects to be called from `point-min' in a bookmark file."
646 (if (looking-at "^;;;;")
647 (save-excursion
648 (save-match-data
649 (re-search-forward "[0-9]")
650 (forward-char -1)
651 (read (current-buffer))))
652 ;; Else this is format version 0, the original one, which didn't
653 ;; even have version stamps.
657 (defun bookmark-maybe-upgrade-file-format ()
658 "Check the file-format version of this bookmark file.
659 If the version is not up-to-date, upgrade it automatically.
660 This expects to be called from `point-min' in a bookmark file."
661 (let ((version (bookmark-grok-file-format-version)))
662 (cond
663 ((= version bookmark-file-format-version)
664 ) ; home free -- version is current
665 ((= version 0)
666 (bookmark-upgrade-file-format-from-0))
668 (error "Bookmark file format version strangeness")))))
671 (defun bookmark-insert-file-format-version-stamp ()
672 "Insert text indicating current version of bookmark file format."
673 (insert
674 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
675 bookmark-file-format-version))
676 (insert ";;; This format is meant to be slightly human-readable;\n"
677 ";;; nevertheless, you probably don't want to edit it.\n"
678 ";;; "
679 bookmark-end-of-version-stamp-marker))
682 ;;; end file-format stuff
685 ;;; Generic helpers.
687 (defun bookmark-maybe-message (fmt &rest args)
688 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
689 (if (>= baud-rate 9600)
690 (apply 'message fmt args)))
693 ;;; Core code:
695 (defvar bookmark-minibuffer-read-name-map
696 (let ((map (make-sparse-keymap)))
697 (set-keymap-parent map minibuffer-local-map)
698 (define-key map "\C-w" 'bookmark-yank-word)
699 ;; This C-u binding might not be very useful any more now that we
700 ;; provide access to the default via the standard M-n binding.
701 ;; Maybe we should just remove it? --Stef-08
702 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
703 map))
705 ;;;###autoload
706 (defun bookmark-set (&optional name parg)
707 "Set a bookmark named NAME inside a file.
708 If name is nil, then the user will be prompted.
709 With prefix arg, will not overwrite a bookmark that has the same name
710 as NAME if such a bookmark already exists, but instead will \"push\"
711 the new bookmark onto the bookmark alist. Thus the most recently set
712 bookmark with name NAME would be the one in effect at any given time,
713 but the others are still there, should you decide to delete the most
714 recent one.
716 To yank words from the text of the buffer and use them as part of the
717 bookmark name, type C-w while setting a bookmark. Successive C-w's
718 yank successive words.
720 Typing C-u inserts the name of the last bookmark used in the buffer
721 \(as an aid in using a single bookmark name to track your progress
722 through a large file\). If no bookmark was used, then C-u inserts the
723 name of the file being visited.
725 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
726 and it removes only the first instance of a bookmark with that name from
727 the list of bookmarks.\)"
728 (interactive (list nil current-prefix-arg))
729 (let* ((record (bookmark-make-record))
730 (default (car record)))
732 (bookmark-maybe-load-default-file)
734 (setq bookmark-current-point (point))
735 (setq bookmark-yank-point (point))
736 (setq bookmark-current-buffer (current-buffer))
738 (let ((str
739 (or name
740 (read-from-minibuffer
741 (format "Set bookmark (%s): " default)
743 bookmark-minibuffer-read-name-map
744 nil nil default))))
745 (and (string-equal str "") (setq str default))
746 (bookmark-store str (cdr record) parg)
748 ;; Ask for an annotation buffer for this bookmark
749 (if bookmark-use-annotations
750 (bookmark-edit-annotation str)
751 (goto-char bookmark-current-point)))))
753 (defun bookmark-kill-line (&optional newline-too)
754 "Kill from point to end of line.
755 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
756 Does not affect the kill ring."
757 (let ((eol (save-excursion (end-of-line) (point))))
758 (delete-region (point) eol)
759 (if (and newline-too (looking-at "\n"))
760 (delete-char 1))))
763 ;; Defvars to avoid compilation warnings:
764 (defvar bookmark-annotation-name nil
765 "Variable holding the name of the bookmark.
766 This is used in `bookmark-edit-annotation' to record the bookmark
767 whose annotation is being edited.")
770 (defun bookmark-default-annotation-text (bookmark)
771 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
772 "# All lines which start with a '#' will be deleted.\n"
773 "# Type C-c C-c when done.\n#\n"
774 "# Author: " (user-full-name) " <" (user-login-name) "@"
775 (system-name) ">\n"
776 "# Date: " (current-time-string) "\n"))
779 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
780 "Function to return default text to use for a bookmark annotation.
781 It takes one argument, the name of the bookmark, as a string.")
782 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
783 'bookmark-edit-annotation-text-func "23.1")
785 (defvar bookmark-edit-annotation-mode-map
786 (let ((map (make-sparse-keymap)))
787 (set-keymap-parent map text-mode-map)
788 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
789 map)
790 "Keymap for editing an annotation of a bookmark.")
793 (defun bookmark-edit-annotation-mode (bookmark)
794 "Mode for editing the annotation of bookmark BOOKMARK.
795 When you have finished composing, type \\[bookmark-send-annotation].
797 \\{bookmark-edit-annotation-mode-map}"
798 (interactive)
799 (kill-all-local-variables)
800 (make-local-variable 'bookmark-annotation-name)
801 (setq bookmark-annotation-name bookmark)
802 (use-local-map bookmark-edit-annotation-mode-map)
803 (setq major-mode 'bookmark-edit-annotation-mode
804 mode-name "Edit Bookmark Annotation")
805 (insert (funcall bookmark-edit-annotation-text-func bookmark))
806 (let ((annotation (bookmark-get-annotation bookmark)))
807 (if (and annotation (not (string-equal annotation "")))
808 (insert annotation)))
809 (run-mode-hooks 'text-mode-hook))
812 (defun bookmark-send-edited-annotation ()
813 "Use buffer contents as annotation for a bookmark.
814 Lines beginning with `#' are ignored."
815 (interactive)
816 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
817 (error "Not in bookmark-edit-annotation-mode"))
818 (goto-char (point-min))
819 (while (< (point) (point-max))
820 (if (looking-at "^#")
821 (bookmark-kill-line t)
822 (forward-line 1)))
823 ;; Take no chances with text properties.
824 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
825 (bookmark bookmark-annotation-name))
826 (bookmark-set-annotation bookmark annotation)
827 (bookmark-bmenu-surreptitiously-rebuild-list)
828 (goto-char bookmark-current-point))
829 (kill-buffer (current-buffer)))
832 (defun bookmark-edit-annotation (bookmark)
833 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
834 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
835 (bookmark-edit-annotation-mode bookmark))
838 (defun bookmark-insert-current-bookmark ()
839 "Insert this buffer's value of `bookmark-current-bookmark'.
840 Default to file name if it's nil."
841 (interactive)
842 (let ((str
843 (with-current-buffer bookmark-current-buffer
844 (or bookmark-current-bookmark
845 (bookmark-buffer-name)))))
846 (insert str)))
849 (defun bookmark-buffer-name ()
850 "Return the name of the current buffer's file, non-directory."
851 (cond
852 ;; Or are we a file?
853 (buffer-file-name (file-name-nondirectory buffer-file-name))
854 ;; Or are we a directory?
855 ((and (boundp 'dired-directory) dired-directory)
856 (let* ((dirname (if (stringp dired-directory)
857 dired-directory
858 (car dired-directory)))
859 (idx (1- (length dirname))))
860 ;; Strip the trailing slash.
861 (if (= ?/ (aref dirname idx))
862 (file-name-nondirectory (substring dirname 0 idx))
863 ;; Else return the current-buffer
864 (buffer-name (current-buffer)))))
865 ;; If all else fails, use the buffer's name.
867 (buffer-name (current-buffer)))))
870 (defun bookmark-yank-word ()
871 (interactive)
872 ;; get the next word from the buffer and append it to the name of
873 ;; the bookmark currently being set.
874 (let ((string (save-excursion
875 (set-buffer bookmark-current-buffer)
876 (goto-char bookmark-yank-point)
877 (buffer-substring-no-properties
878 (point)
879 (progn
880 (forward-word 1)
881 (setq bookmark-yank-point (point)))))))
882 (insert string)))
884 (defun bookmark-buffer-file-name ()
885 "Return the current buffer's file in a way useful for bookmarks."
886 (cond
887 (buffer-file-name
888 ;; Abbreviate the path, both so it's shorter and so it's more
889 ;; portable. E.g., the user's home dir might be a different
890 ;; path on different machines, but "~/" will still reach it.
891 (abbreviate-file-name buffer-file-name))
892 ((and (boundp 'dired-directory) dired-directory)
893 (if (stringp dired-directory)
894 dired-directory
895 (car dired-directory)))
896 (t (error "Buffer not visiting a file or directory"))))
899 (defun bookmark-maybe-load-default-file ()
900 (and (not bookmarks-already-loaded)
901 (null bookmark-alist)
902 (prog2
903 (and
904 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
905 ;; to be renamed.
906 (file-exists-p (expand-file-name bookmark-old-default-file))
907 (not (file-exists-p (expand-file-name bookmark-default-file)))
908 (rename-file (expand-file-name bookmark-old-default-file)
909 (expand-file-name bookmark-default-file)))
910 ;; return t so the `and' will continue...
913 (file-readable-p (expand-file-name bookmark-default-file))
914 (bookmark-load bookmark-default-file t t)
915 (setq bookmarks-already-loaded t)))
918 (defun bookmark-maybe-sort-alist ()
919 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
920 ;;is non-nil, then return a sorted copy of the alist.
921 (if bookmark-sort-flag
922 (sort (copy-alist bookmark-alist)
923 (function
924 (lambda (x y) (string-lessp (car x) (car y)))))
925 bookmark-alist))
928 (defvar bookmark-after-jump-hook nil
929 "Hook run after `bookmark-jump' jumps to a bookmark.
930 Useful for example to unhide text in `outline-mode'.")
932 ;;;###autoload
933 (defun bookmark-jump (bookmark)
934 "Jump to bookmark BOOKMARK (a point in some file).
935 You may have a problem using this function if the value of variable
936 `bookmark-alist' is nil. If that happens, you need to load in some
937 bookmarks. See help on function `bookmark-load' for more about
938 this.
940 If the file pointed to by BOOKMARK no longer exists, you will be asked
941 if you wish to give the bookmark a new location, and `bookmark-jump'
942 will then jump to the new location, as well as recording it in place
943 of the old one in the permanent bookmark record."
944 (interactive
945 (list (bookmark-completing-read "Jump to bookmark"
946 bookmark-current-bookmark)))
947 (unless bookmark
948 (error "No bookmark specified"))
949 (bookmark-maybe-historicize-string bookmark)
950 (let ((alist (bookmark-jump-noselect bookmark)))
951 (and alist
952 (switch-to-buffer (cadr (assq 'buffer alist)))
953 (goto-char (cadr (assq 'position alist)))
954 (progn (run-hooks 'bookmark-after-jump-hook) t)
955 (if bookmark-automatically-show-annotations
956 ;; if there is an annotation for this bookmark,
957 ;; show it in a buffer.
958 (bookmark-show-annotation bookmark)))))
961 ;;;###autoload
962 (defun bookmark-jump-other-window (bookmark)
963 "Jump to BOOKMARK (a point in some file) in another window.
964 See `bookmark-jump'."
965 (interactive
966 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
967 bookmark-current-bookmark)))
968 (if (> emacs-major-version 21)
969 (list bkm) bkm)))
970 (when bookmark
971 (bookmark-maybe-historicize-string bookmark)
972 (let ((alist (bookmark-jump-noselect bookmark)))
973 (and alist
974 (switch-to-buffer-other-window (cadr (assq 'buffer alist)))
975 (goto-char (cadr (assq 'position alist)))
976 (if bookmark-automatically-show-annotations
977 ;; if there is an annotation for this bookmark,
978 ;; show it in a buffer.
979 (bookmark-show-annotation bookmark))))))
982 (defun bookmark-file-or-variation-thereof (file)
983 "Return FILE (a string) if it exists, or return a reasonable
984 variation of FILE if that exists. Reasonable variations are checked
985 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
986 nor a reasonable variation thereof, then still return FILE if it can
987 be retrieved from a VC backend, else return nil."
988 (if (file-exists-p file)
989 file
991 (progn (require 'info) ; ensure Info-suffix-list is bound
992 (catch 'found
993 (mapc (lambda (elt)
994 (let ((suffixed-file (concat file (car elt))))
995 (if (file-exists-p suffixed-file)
996 (throw 'found suffixed-file))))
997 Info-suffix-list)
998 nil))
999 ;; Last possibility: try VC
1000 (if (vc-backend file) file))))
1002 (defun bookmark-jump-noselect (bookmark)
1003 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none."
1004 (let ((found (funcall (or (bookmark-get-handler bookmark)
1005 'bookmark-default-handler)
1006 bookmark)))
1007 (unless found
1008 ;; Else unable to find the marked file, so ask if user wants to
1009 ;; relocate the bookmark, else remind them to consider deletion.
1010 (let ((file (bookmark-get-filename bookmark)))
1011 (when file ;Don't know how to relocate if there's no `file'.
1012 (setq file (expand-file-name file))
1013 (ding)
1014 (if (y-or-n-p (concat (file-name-nondirectory file)
1015 " nonexistent. Relocate \""
1016 bookmark
1017 "\"? "))
1018 (progn
1019 (bookmark-relocate bookmark)
1020 ;; Try again.
1021 (setq found (funcall (or (bookmark-get-handler bookmark)
1022 'bookmark-default-handler)
1023 bookmark)))
1024 (message
1025 "Bookmark not relocated; consider removing it \(%s\)." bookmark)))))
1026 (when found
1027 ;; Added by db.
1028 (setq bookmark-current-bookmark bookmark)
1029 found)))
1031 (defun bookmark-default-handler (str)
1032 ;; Helper for bookmark-jump. STR is a bookmark name, of the sort
1033 ;; accepted by `bookmark-get-bookmark'.
1035 ;; Return an alist '((buffer BUFFER) (position POSITION) ...)
1036 ;; indicating the bookmarked point within the specied buffer. Any
1037 ;; elements not documented here should be ignored.
1038 (bookmark-maybe-load-default-file)
1039 (let* ((file (expand-file-name (bookmark-get-filename str)))
1040 (forward-str (bookmark-get-front-context-string str))
1041 (behind-str (bookmark-get-rear-context-string str))
1042 (place (bookmark-get-position str)))
1043 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1044 ;; but now that Info bookmarks are handled elsewhere it seems that we
1045 ;; should be able to get rid of it. --Stef
1046 (if (setq file (bookmark-file-or-variation-thereof file))
1047 (with-current-buffer (find-file-noselect file)
1048 (goto-char place)
1050 ;; Go searching forward first. Then, if forward-str exists and
1051 ;; was found in the file, we can search backward for behind-str.
1052 ;; Rationale is that if text was inserted between the two in the
1053 ;; file, it's better to be put before it so you can read it,
1054 ;; rather than after and remain perhaps unaware of the changes.
1055 (if forward-str
1056 (if (search-forward forward-str (point-max) t)
1057 (goto-char (match-beginning 0))))
1058 (if behind-str
1059 (if (search-backward behind-str (point-min) t)
1060 (goto-char (match-end 0))))
1061 `((buffer ,(current-buffer)) (position ,(point)))))))
1064 ;;;###autoload
1065 (defun bookmark-relocate (bookmark)
1066 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1067 This makes an already existing bookmark point to that file, instead of
1068 the one it used to point at. Useful when a file has been renamed
1069 after a bookmark was set in it."
1070 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1071 (bookmark-maybe-historicize-string bookmark)
1072 (bookmark-maybe-load-default-file)
1073 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1074 (newloc (expand-file-name
1075 (read-file-name
1076 (format "Relocate %s to: " bookmark)
1077 (file-name-directory bmrk-filename)))))
1078 (bookmark-set-filename bookmark newloc)
1079 (bookmark-bmenu-surreptitiously-rebuild-list)))
1082 ;;;###autoload
1083 (defun bookmark-insert-location (bookmark &optional no-history)
1084 "Insert the name of the file associated with BOOKMARK.
1085 Optional second arg NO-HISTORY means don't record this in the
1086 minibuffer history list `bookmark-history'."
1087 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1088 (or no-history (bookmark-maybe-historicize-string bookmark))
1089 (let ((start (point)))
1090 (prog1
1091 (insert (bookmark-location bookmark)) ; *Return this line*
1092 (if (and (display-color-p) (display-mouse-p))
1093 (add-text-properties
1094 start
1095 (save-excursion (re-search-backward
1096 "[^ \t]")
1097 (1+ (point)))
1098 '(mouse-face highlight
1099 follow-link t
1100 help-echo "mouse-2: go to this bookmark in other window"))))))
1102 ;;;###autoload
1103 (defalias 'bookmark-locate 'bookmark-insert-location)
1105 (defun bookmark-location (bookmark)
1106 "Return the name of the file associated with BOOKMARK."
1107 (bookmark-maybe-load-default-file)
1108 (bookmark-get-filename bookmark))
1111 ;;;###autoload
1112 (defun bookmark-rename (old &optional new)
1113 "Change the name of OLD bookmark to NEW name.
1114 If called from keyboard, prompt for OLD and NEW. If called from
1115 menubar, select OLD from a menu and prompt for NEW.
1117 If called from Lisp, prompt for NEW if only OLD was passed as an
1118 argument. If called with two strings, then no prompting is done. You
1119 must pass at least OLD when calling from Lisp.
1121 While you are entering the new name, consecutive C-w's insert
1122 consecutive words from the text of the buffer into the new bookmark
1123 name."
1124 (interactive (list (bookmark-completing-read "Old bookmark name")))
1125 (bookmark-maybe-historicize-string old)
1126 (bookmark-maybe-load-default-file)
1128 (setq bookmark-current-point (point))
1129 (setq bookmark-yank-point (point))
1130 (setq bookmark-current-buffer (current-buffer))
1131 (let ((newname
1132 (or new ; use second arg, if non-nil
1133 (read-from-minibuffer
1134 "New name: "
1136 (let ((now-map (copy-keymap minibuffer-local-map)))
1137 (define-key now-map "\C-w" 'bookmark-yank-word)
1138 now-map)
1140 'bookmark-history))))
1141 (bookmark-set-name old newname)
1142 (setq bookmark-current-bookmark newname)
1143 (bookmark-bmenu-surreptitiously-rebuild-list)
1144 (setq bookmark-alist-modification-count
1145 (1+ bookmark-alist-modification-count))
1146 (if (bookmark-time-to-save-p)
1147 (bookmark-save))))
1150 ;;;###autoload
1151 (defun bookmark-insert (bookmark)
1152 "Insert the text of the file pointed to by bookmark BOOKMARK.
1153 You may have a problem using this function if the value of variable
1154 `bookmark-alist' is nil. If that happens, you need to load in some
1155 bookmarks. See help on function `bookmark-load' for more about
1156 this."
1157 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1158 (bookmark-maybe-historicize-string bookmark)
1159 (bookmark-maybe-load-default-file)
1160 (let ((orig-point (point))
1161 (str-to-insert
1162 (save-excursion
1163 (set-buffer (cadr (assq 'buffer (bookmark-jump-noselect bookmark))))
1164 (buffer-string))))
1165 (insert str-to-insert)
1166 (push-mark)
1167 (goto-char orig-point)))
1170 ;;;###autoload
1171 (defun bookmark-delete (bookmark &optional batch)
1172 "Delete BOOKMARK from the bookmark list.
1173 Removes only the first instance of a bookmark with that name. If
1174 there are one or more other bookmarks with the same name, they will
1175 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1176 one most recently used in this file, if any\).
1177 Optional second arg BATCH means don't update the bookmark list buffer,
1178 probably because we were called from there."
1179 (interactive
1180 (list (bookmark-completing-read "Delete bookmark"
1181 bookmark-current-bookmark)))
1182 (bookmark-maybe-historicize-string bookmark)
1183 (bookmark-maybe-load-default-file)
1184 (let ((will-go (bookmark-get-bookmark bookmark)))
1185 (setq bookmark-alist (delq will-go bookmark-alist))
1186 ;; Added by db, nil bookmark-current-bookmark if the last
1187 ;; occurrence has been deleted
1188 (or (bookmark-get-bookmark bookmark-current-bookmark)
1189 (setq bookmark-current-bookmark nil)))
1190 ;; Don't rebuild the list
1191 (if batch
1193 (bookmark-bmenu-surreptitiously-rebuild-list)
1194 (setq bookmark-alist-modification-count
1195 (1+ bookmark-alist-modification-count))
1196 (if (bookmark-time-to-save-p)
1197 (bookmark-save))))
1200 (defun bookmark-time-to-save-p (&optional last-time)
1201 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1202 ;; finds out whether it's time to save bookmarks to a file, by
1203 ;; examining the value of variable bookmark-save-flag, and maybe
1204 ;; bookmark-alist-modification-count. Returns t if they should be
1205 ;; saved, nil otherwise. if last-time is non-nil, then this is
1206 ;; being called when emacs is killed.
1207 (cond (last-time
1208 (and (> bookmark-alist-modification-count 0)
1209 bookmark-save-flag))
1210 ((numberp bookmark-save-flag)
1211 (>= bookmark-alist-modification-count bookmark-save-flag))
1213 nil)))
1216 ;;;###autoload
1217 (defun bookmark-write ()
1218 "Write bookmarks to a file (reading the file name with the minibuffer).
1219 Don't use this in Lisp programs; use `bookmark-save' instead."
1220 (interactive)
1221 (bookmark-maybe-load-default-file)
1222 (bookmark-save t))
1225 ;;;###autoload
1226 (defun bookmark-save (&optional parg file)
1227 "Save currently defined bookmarks.
1228 Saves by default in the file defined by the variable
1229 `bookmark-default-file'. With a prefix arg, save it in file FILE
1230 \(second argument\).
1232 If you are calling this from Lisp, the two arguments are PARG and
1233 FILE, and if you just want it to write to the default file, then
1234 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1235 instead. If you pass in one argument, and it is non-nil, then the
1236 user will be interactively queried for a file to save in.
1238 When you want to load in the bookmarks from a file, use
1239 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1240 for a file, defaulting to the file defined by variable
1241 `bookmark-default-file'."
1242 (interactive "P")
1243 (bookmark-maybe-load-default-file)
1244 (cond
1245 ((and (null parg) (null file))
1246 ;;whether interactive or not, write to default file
1247 (bookmark-write-file bookmark-default-file))
1248 ((and (null parg) file)
1249 ;;whether interactive or not, write to given file
1250 (bookmark-write-file file))
1251 ((and parg (not file))
1252 ;;have been called interactively w/ prefix arg
1253 (let ((file (read-file-name "File to save bookmarks in: ")))
1254 (bookmark-write-file file)))
1255 (t ; someone called us with prefix-arg *and* a file, so just write to file
1256 (bookmark-write-file file)))
1257 ;; signal that we have synced the bookmark file by setting this to
1258 ;; 0. If there was an error at any point before, it will not get
1259 ;; set, which is what we want.
1260 (setq bookmark-alist-modification-count 0))
1264 (defun bookmark-write-file (file)
1265 (save-excursion
1266 (save-window-excursion
1267 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1268 (set-buffer (get-buffer-create " *Bookmarks*"))
1269 (goto-char (point-min))
1270 (delete-region (point-min) (point-max))
1271 (let ((print-length nil)
1272 (print-level nil))
1273 (bookmark-insert-file-format-version-stamp)
1274 (pp bookmark-alist (current-buffer))
1275 (let ((version-control
1276 (cond
1277 ((null bookmark-version-control) nil)
1278 ((eq 'never bookmark-version-control) 'never)
1279 ((eq 'nospecial bookmark-version-control) version-control)
1281 t))))
1282 (condition-case nil
1283 (write-region (point-min) (point-max) file)
1284 (file-error (message "Can't write %s" file)))
1285 (kill-buffer (current-buffer))
1286 (bookmark-maybe-message
1287 "Saving bookmarks to file %s...done" file))))))
1290 (defun bookmark-import-new-list (new-list)
1291 ;; Walk over the new list, adding each individual bookmark
1292 ;; carefully. "Carefully" means checking against the existing
1293 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1294 ;; as necessary.
1295 (let ((lst new-list)
1296 (names (bookmark-all-names)))
1297 (while lst
1298 (let* ((full-record (car lst)))
1299 (bookmark-maybe-rename full-record names)
1300 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1301 (setq names (cons (bookmark-name-from-full-record full-record) names))
1302 (setq lst (cdr lst))))))
1305 (defun bookmark-maybe-rename (full-record names)
1306 ;; just a helper for bookmark-import-new-list; it is only for
1307 ;; readability that this is not inlined.
1309 ;; Once this has found a free name, it sets full-record to that
1310 ;; name.
1311 (let ((found-name (bookmark-name-from-full-record full-record)))
1312 (if (member found-name names)
1313 ;; We've got a conflict, so generate a new name
1314 (let ((count 2)
1315 (new-name found-name))
1316 (while (member new-name names)
1317 (setq new-name (concat found-name (format "<%d>" count)))
1318 (setq count (1+ count)))
1319 (bookmark-set-name full-record new-name)))))
1322 ;;;###autoload
1323 (defun bookmark-load (file &optional overwrite no-msg)
1324 "Load bookmarks from FILE (which must be in bookmark format).
1325 Appends loaded bookmarks to the front of the list of bookmarks. If
1326 optional second argument OVERWRITE is non-nil, existing bookmarks are
1327 destroyed. Optional third arg NO-MSG means don't display any messages
1328 while loading.
1330 If you load a file that doesn't contain a proper bookmark alist, you
1331 will corrupt Emacs's bookmark list. Generally, you should only load
1332 in files that were created with the bookmark functions in the first
1333 place. Your own personal bookmark file, `~/.emacs.bmk', is
1334 maintained automatically by Emacs; you shouldn't need to load it
1335 explicitly.
1337 If you load a file containing bookmarks with the same names as
1338 bookmarks already present in your Emacs, the new bookmarks will get
1339 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1340 method buffers use to resolve name collisions."
1341 (interactive
1342 (list (read-file-name
1343 (format "Load bookmarks from: (%s) "
1344 bookmark-default-file)
1345 ;;Default might not be used often,
1346 ;;but there's no better default, and
1347 ;;I guess it's better than none at all.
1348 "~/" bookmark-default-file 'confirm)))
1349 (setq file (expand-file-name file))
1350 (if (file-readable-p file)
1351 (save-excursion
1352 (save-window-excursion
1353 (if (null no-msg)
1354 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1355 (set-buffer (let ((enable-local-variables nil))
1356 (find-file-noselect file)))
1357 (goto-char (point-min))
1358 (bookmark-maybe-upgrade-file-format)
1359 (let ((blist (bookmark-alist-from-buffer)))
1360 (if (listp blist)
1361 (progn
1362 (if overwrite
1363 (progn
1364 (setq bookmark-alist blist)
1365 (setq bookmark-alist-modification-count 0))
1366 ;; else
1367 (bookmark-import-new-list blist)
1368 (setq bookmark-alist-modification-count
1369 (1+ bookmark-alist-modification-count)))
1370 (if (string-equal
1371 (expand-file-name bookmark-default-file)
1372 file)
1373 (setq bookmarks-already-loaded t))
1374 (bookmark-bmenu-surreptitiously-rebuild-list))
1375 (error "Invalid bookmark list in %s" file)))
1376 (kill-buffer (current-buffer)))
1377 (if (null no-msg)
1378 (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
1379 (error "Cannot read bookmark file %s" file)))
1383 ;;; Code supporting the dired-like bookmark menu. Prefix is
1384 ;;; "bookmark-bmenu" for "buffer-menu":
1387 (defvar bookmark-bmenu-bookmark-column nil)
1390 (defvar bookmark-bmenu-hidden-bookmarks ())
1393 (defvar bookmark-bmenu-mode-map nil)
1396 (if bookmark-bmenu-mode-map
1398 (setq bookmark-bmenu-mode-map (make-keymap))
1399 (suppress-keymap bookmark-bmenu-mode-map t)
1400 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1401 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1402 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1403 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1404 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1405 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1406 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1407 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1408 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1409 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1410 (define-key bookmark-bmenu-mode-map "\C-o"
1411 'bookmark-bmenu-switch-other-window)
1412 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1413 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1414 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1415 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1416 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1417 (define-key bookmark-bmenu-mode-map " " 'next-line)
1418 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1419 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1420 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1421 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1422 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1423 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1424 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1425 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1426 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1427 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1428 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1429 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1430 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1431 (define-key bookmark-bmenu-mode-map [mouse-2]
1432 'bookmark-bmenu-other-window-with-mouse))
1436 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1437 ;; data.
1438 (put 'bookmark-bmenu-mode 'mode-class 'special)
1441 ;; todo: need to display whether or not bookmark exists as a buffer in
1442 ;; flag column.
1444 ;; Format:
1445 ;; FLAGS BOOKMARK [ LOCATION ]
1448 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1449 "Rebuild the Bookmark List if it exists.
1450 Don't affect the buffer ring order."
1451 (if (get-buffer "*Bookmark List*")
1452 (save-excursion
1453 (save-window-excursion
1454 (bookmark-bmenu-list)))))
1457 ;;;###autoload
1458 (defun bookmark-bmenu-list ()
1459 "Display a list of existing bookmarks.
1460 The list is displayed in a buffer named `*Bookmark List*'.
1461 The leftmost column displays a D if the bookmark is flagged for
1462 deletion, or > if it is flagged for displaying."
1463 (interactive)
1464 (bookmark-maybe-load-default-file)
1465 (if (interactive-p)
1466 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1467 (set-buffer (get-buffer-create "*Bookmark List*")))
1468 (let ((inhibit-read-only t))
1469 (erase-buffer)
1470 (insert "% Bookmark\n- --------\n")
1471 (add-text-properties (point-min) (point)
1472 '(font-lock-face bookmark-menu-heading))
1473 (mapc
1474 (lambda (full-record)
1475 ;; if a bookmark has an annotation, prepend a "*"
1476 ;; in the list of bookmarks.
1477 (let ((annotation (bookmark-get-annotation
1478 (bookmark-name-from-full-record full-record))))
1479 (if (and annotation (not (string-equal annotation "")))
1480 (insert " *")
1481 (insert " "))
1482 (let ((start (point)))
1483 (insert (bookmark-name-from-full-record full-record))
1484 (if (and (display-color-p) (display-mouse-p))
1485 (add-text-properties
1486 start
1487 (save-excursion (re-search-backward
1488 "[^ \t]")
1489 (1+ (point)))
1490 '(mouse-face highlight
1491 follow-link t
1492 help-echo "mouse-2: go to this bookmark in other window")))
1493 (insert "\n")
1495 (bookmark-maybe-sort-alist)))
1496 (goto-char (point-min))
1497 (forward-line 2)
1498 (bookmark-bmenu-mode)
1499 (if bookmark-bmenu-toggle-filenames
1500 (bookmark-bmenu-toggle-filenames t)))
1502 ;;;###autoload
1503 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1504 ;;;###autoload
1505 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1509 (defun bookmark-bmenu-mode ()
1510 "Major mode for editing a list of bookmarks.
1511 Each line describes one of the bookmarks in Emacs.
1512 Letters do not insert themselves; instead, they are commands.
1513 Bookmark names preceded by a \"*\" have annotations.
1514 \\<bookmark-bmenu-mode-map>
1515 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1516 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1517 Also show bookmarks marked using m in other windows.
1518 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1519 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1520 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1521 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1522 together with bookmark selected before this one in another window.
1523 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1524 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1525 so the bookmark menu bookmark remains visible in its window.
1526 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1527 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1528 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1529 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1530 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1531 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1532 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1533 With a prefix arg, prompts for a file to save in.
1534 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1535 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1536 With prefix argument, also move up one line.
1537 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1538 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1539 in another buffer.
1540 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1541 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1542 (kill-all-local-variables)
1543 (use-local-map bookmark-bmenu-mode-map)
1544 (setq truncate-lines t)
1545 (setq buffer-read-only t)
1546 (setq major-mode 'bookmark-bmenu-mode)
1547 (setq mode-name "Bookmark Menu")
1548 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1551 (defun bookmark-bmenu-toggle-filenames (&optional show)
1552 "Toggle whether filenames are shown in the bookmark list.
1553 Optional argument SHOW means show them unconditionally."
1554 (interactive)
1555 (cond
1556 (show
1557 (setq bookmark-bmenu-toggle-filenames nil)
1558 (bookmark-bmenu-show-filenames)
1559 (setq bookmark-bmenu-toggle-filenames t))
1560 (bookmark-bmenu-toggle-filenames
1561 (bookmark-bmenu-hide-filenames)
1562 (setq bookmark-bmenu-toggle-filenames nil))
1564 (bookmark-bmenu-show-filenames)
1565 (setq bookmark-bmenu-toggle-filenames t))))
1568 (defun bookmark-bmenu-show-filenames (&optional force)
1569 (if (and (not force) bookmark-bmenu-toggle-filenames)
1570 nil ;already shown, so do nothing
1571 (save-excursion
1572 (save-window-excursion
1573 (goto-char (point-min))
1574 (forward-line 2)
1575 (setq bookmark-bmenu-hidden-bookmarks ())
1576 (let ((inhibit-read-only t))
1577 (while (< (point) (point-max))
1578 (let ((bmrk (bookmark-bmenu-bookmark)))
1579 (setq bookmark-bmenu-hidden-bookmarks
1580 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1581 (let ((start (save-excursion (end-of-line) (point))))
1582 (move-to-column bookmark-bmenu-file-column t)
1583 ;; Strip off `mouse-face' from the white spaces region.
1584 (if (and (display-color-p) (display-mouse-p))
1585 (remove-text-properties start (point)
1586 '(mouse-face nil help-echo nil))))
1587 (delete-region (point) (progn (end-of-line) (point)))
1588 (insert " ")
1589 ;; Pass the NO-HISTORY arg:
1590 (bookmark-insert-location bmrk t)
1591 (forward-line 1))))))))
1594 (defun bookmark-bmenu-hide-filenames (&optional force)
1595 (if (and (not force) bookmark-bmenu-toggle-filenames)
1596 ;; nothing to hide if above is nil
1597 (save-excursion
1598 (save-window-excursion
1599 (goto-char (point-min))
1600 (forward-line 2)
1601 (setq bookmark-bmenu-hidden-bookmarks
1602 (nreverse bookmark-bmenu-hidden-bookmarks))
1603 (save-excursion
1604 (goto-char (point-min))
1605 (search-forward "Bookmark")
1606 (backward-word 1)
1607 (setq bookmark-bmenu-bookmark-column (current-column)))
1608 (save-excursion
1609 (let ((inhibit-read-only t))
1610 (while bookmark-bmenu-hidden-bookmarks
1611 (move-to-column bookmark-bmenu-bookmark-column t)
1612 (bookmark-kill-line)
1613 (let ((start (point)))
1614 (insert (car bookmark-bmenu-hidden-bookmarks))
1615 (if (and (display-color-p) (display-mouse-p))
1616 (add-text-properties
1617 start
1618 (save-excursion (re-search-backward
1619 "[^ \t]")
1620 (1+ (point)))
1621 '(mouse-face highlight
1622 follow-link t
1623 help-echo
1624 "mouse-2: go to this bookmark in other window"))))
1625 (setq bookmark-bmenu-hidden-bookmarks
1626 (cdr bookmark-bmenu-hidden-bookmarks))
1627 (forward-line 1))))))))
1630 (defun bookmark-bmenu-check-position ()
1631 ;; Returns non-nil if on a line with a bookmark.
1632 ;; (The actual value returned is bookmark-alist).
1633 ;; Else reposition and try again, else return nil.
1634 (cond ((< (count-lines (point-min) (point)) 2)
1635 (goto-char (point-min))
1636 (forward-line 2)
1637 bookmark-alist)
1638 ((and (bolp) (eobp))
1639 (beginning-of-line 0)
1640 bookmark-alist)
1642 bookmark-alist)))
1645 (defun bookmark-bmenu-bookmark ()
1646 ;; return a string which is bookmark of this line.
1647 (if (bookmark-bmenu-check-position)
1648 (save-excursion
1649 (save-window-excursion
1650 (goto-char (point-min))
1651 (search-forward "Bookmark")
1652 (backward-word 1)
1653 (setq bookmark-bmenu-bookmark-column (current-column)))))
1654 (if bookmark-bmenu-toggle-filenames
1655 (bookmark-bmenu-hide-filenames))
1656 (save-excursion
1657 (save-window-excursion
1658 (beginning-of-line)
1659 (forward-char bookmark-bmenu-bookmark-column)
1660 (prog1
1661 (buffer-substring-no-properties (point)
1662 (progn
1663 (end-of-line)
1664 (point)))
1665 ;; well, this is certainly crystal-clear:
1666 (if bookmark-bmenu-toggle-filenames
1667 (bookmark-bmenu-toggle-filenames t))))))
1670 (defun bookmark-show-annotation (bookmark)
1671 "Display the annotation for bookmark named BOOKMARK in a buffer,
1672 if an annotation exists."
1673 (let ((annotation (bookmark-get-annotation bookmark)))
1674 (if (and annotation (not (string-equal annotation "")))
1675 (save-excursion
1676 (let ((old-buf (current-buffer)))
1677 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1678 (delete-region (point-min) (point-max))
1679 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1680 (insert annotation)
1681 (goto-char (point-min))
1682 (pop-to-buffer old-buf))))))
1685 (defun bookmark-show-all-annotations ()
1686 "Display the annotations for all bookmarks in a buffer."
1687 (let ((old-buf (current-buffer)))
1688 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1689 (delete-region (point-min) (point-max))
1690 (mapc
1691 (lambda (full-record)
1692 (let* ((name (bookmark-name-from-full-record full-record))
1693 (ann (bookmark-get-annotation name)))
1694 (insert (concat name ":\n"))
1695 (if (and ann (not (string-equal ann "")))
1696 ;; insert the annotation, indented by 4 spaces.
1697 (progn
1698 (save-excursion (insert ann) (unless (bolp)
1699 (insert "\n")))
1700 (while (< (point) (point-max))
1701 (beginning-of-line) ; paranoia
1702 (insert " ")
1703 (forward-line)
1704 (end-of-line))))))
1705 bookmark-alist)
1706 (goto-char (point-min))
1707 (pop-to-buffer old-buf)))
1710 (defun bookmark-bmenu-mark ()
1711 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1712 (interactive)
1713 (beginning-of-line)
1714 (if (bookmark-bmenu-check-position)
1715 (let ((inhibit-read-only t))
1716 (delete-char 1)
1717 (insert ?>)
1718 (forward-line 1)
1719 (bookmark-bmenu-check-position))))
1722 (defun bookmark-bmenu-select ()
1723 "Select this line's bookmark; also display bookmarks marked with `>'.
1724 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1725 (interactive)
1726 (if (bookmark-bmenu-check-position)
1727 (let ((bmrk (bookmark-bmenu-bookmark))
1728 (menu (current-buffer))
1729 (others ())
1730 tem)
1731 (goto-char (point-min))
1732 (while (re-search-forward "^>" nil t)
1733 (setq tem (bookmark-bmenu-bookmark))
1734 (let ((inhibit-read-only t))
1735 (delete-char -1)
1736 (insert ?\s))
1737 (or (string-equal tem bmrk)
1738 (member tem others)
1739 (setq others (cons tem others))))
1740 (setq others (nreverse others)
1741 tem (/ (1- (frame-height)) (1+ (length others))))
1742 (delete-other-windows)
1743 (bookmark-jump bmrk)
1744 (bury-buffer menu)
1745 (if others
1746 (while others
1747 (split-window nil tem)
1748 (other-window 1)
1749 (bookmark-jump (car others))
1750 (setq others (cdr others)))
1751 (other-window 1)))))
1754 (defun bookmark-bmenu-save (parg)
1755 "Save the current list into a bookmark file.
1756 With a prefix arg, prompts for a file to save them in."
1757 (interactive "P")
1758 (save-excursion
1759 (save-window-excursion
1760 (bookmark-save parg))))
1763 (defun bookmark-bmenu-load ()
1764 "Load the bookmark file and rebuild the bookmark menu-buffer."
1765 (interactive)
1766 (if (bookmark-bmenu-check-position)
1767 (save-excursion
1768 (save-window-excursion
1769 ;; This will call `bookmark-bmenu-list'
1770 (call-interactively 'bookmark-load)))))
1773 (defun bookmark-bmenu-1-window ()
1774 "Select this line's bookmark, alone, in full frame."
1775 (interactive)
1776 (if (bookmark-bmenu-check-position)
1777 (progn
1778 (bookmark-jump (bookmark-bmenu-bookmark))
1779 (bury-buffer (other-buffer))
1780 (delete-other-windows))))
1783 (defun bookmark-bmenu-2-window ()
1784 "Select this line's bookmark, with previous buffer in second window."
1785 (interactive)
1786 (if (bookmark-bmenu-check-position)
1787 (let ((bmrk (bookmark-bmenu-bookmark))
1788 (menu (current-buffer))
1789 (pop-up-windows t))
1790 (delete-other-windows)
1791 (switch-to-buffer (other-buffer))
1792 (let* ((alist (bookmark-jump-noselect bmrk))
1793 (buff (cadr (assq 'buffer alist)))
1794 (pos (cadr (assq 'position alist))))
1795 (pop-to-buffer buff)
1796 (goto-char pos))
1797 (bury-buffer menu))))
1800 (defun bookmark-bmenu-this-window ()
1801 "Select this line's bookmark in this window."
1802 (interactive)
1803 (if (bookmark-bmenu-check-position)
1804 (bookmark-jump (bookmark-bmenu-bookmark))))
1807 (defun bookmark-bmenu-other-window ()
1808 "Select this line's bookmark in other window, leaving bookmark menu visible."
1809 (interactive)
1810 (let ((bookmark (bookmark-bmenu-bookmark)))
1811 (if (bookmark-bmenu-check-position)
1812 (let* ((alist (bookmark-jump-noselect bookmark))
1813 (buff (cadr (assq 'buffer alist)))
1814 (pos (cadr (assq 'position alist))))
1815 (switch-to-buffer-other-window buff)
1816 (goto-char pos)
1817 (set-window-point (get-buffer-window buff) pos)
1818 (bookmark-show-annotation bookmark)))))
1821 (defun bookmark-bmenu-switch-other-window ()
1822 "Make the other window select this line's bookmark.
1823 The current window remains selected."
1824 (interactive)
1825 (let ((bookmark (bookmark-bmenu-bookmark))
1826 (pop-up-windows t)
1827 same-window-buffer-names
1828 same-window-regexps)
1829 (if (bookmark-bmenu-check-position)
1830 (let* ((alist (bookmark-jump-noselect bookmark))
1831 (buff (cadr (assq 'buffer alist)))
1832 (pos (cadr (assq 'position alist))))
1833 (display-buffer buff)
1834 (let ((o-buffer (current-buffer)))
1835 ;; save-excursion won't do
1836 (set-buffer buff)
1837 (goto-char pos)
1838 (set-window-point (get-buffer-window buff) pos)
1839 (set-buffer o-buffer))
1840 (bookmark-show-annotation bookmark)))))
1842 (defun bookmark-bmenu-other-window-with-mouse (event)
1843 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1844 (interactive "e")
1845 (save-excursion
1846 (set-buffer (window-buffer (posn-window (event-end event))))
1847 (save-excursion
1848 (goto-char (posn-point (event-end event)))
1849 (bookmark-bmenu-other-window))))
1852 (defun bookmark-bmenu-show-annotation ()
1853 "Show the annotation for the current bookmark in another window."
1854 (interactive)
1855 (let ((bookmark (bookmark-bmenu-bookmark)))
1856 (if (bookmark-bmenu-check-position)
1857 (bookmark-show-annotation bookmark))))
1860 (defun bookmark-bmenu-show-all-annotations ()
1861 "Show the annotation for all bookmarks in another window."
1862 (interactive)
1863 (bookmark-show-all-annotations))
1866 (defun bookmark-bmenu-edit-annotation ()
1867 "Edit the annotation for the current bookmark in another window."
1868 (interactive)
1869 (let ((bookmark (bookmark-bmenu-bookmark)))
1870 (if (bookmark-bmenu-check-position)
1871 (bookmark-edit-annotation bookmark))))
1874 (defun bookmark-bmenu-unmark (&optional backup)
1875 "Cancel all requested operations on bookmark on this line and move down.
1876 Optional BACKUP means move up."
1877 (interactive "P")
1878 (beginning-of-line)
1879 (if (bookmark-bmenu-check-position)
1880 (progn
1881 (let ((inhibit-read-only t))
1882 (delete-char 1)
1883 ;; any flags to reset according to circumstances? How about a
1884 ;; flag indicating whether this bookmark is being visited?
1885 ;; well, we don't have this now, so maybe later.
1886 (insert " "))
1887 (forward-line (if backup -1 1))
1888 (bookmark-bmenu-check-position))))
1891 (defun bookmark-bmenu-backup-unmark ()
1892 "Move up and cancel all requested operations on bookmark on line above."
1893 (interactive)
1894 (forward-line -1)
1895 (if (bookmark-bmenu-check-position)
1896 (progn
1897 (bookmark-bmenu-unmark)
1898 (forward-line -1)
1899 (bookmark-bmenu-check-position))))
1902 (defun bookmark-bmenu-delete ()
1903 "Mark bookmark on this line to be deleted.
1904 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1905 (interactive)
1906 (beginning-of-line)
1907 (if (bookmark-bmenu-check-position)
1908 (let ((inhibit-read-only t))
1909 (delete-char 1)
1910 (insert ?D)
1911 (forward-line 1)
1912 (bookmark-bmenu-check-position))))
1915 (defun bookmark-bmenu-delete-backwards ()
1916 "Mark bookmark on this line to be deleted, then move up one line.
1917 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1918 (interactive)
1919 (bookmark-bmenu-delete)
1920 (forward-line -2)
1921 (if (bookmark-bmenu-check-position)
1922 (forward-line 1))
1923 (bookmark-bmenu-check-position))
1926 (defun bookmark-bmenu-execute-deletions ()
1927 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1928 (interactive)
1929 (message "Deleting bookmarks...")
1930 (let ((hide-em bookmark-bmenu-toggle-filenames)
1931 (o-point (point))
1932 (o-str (save-excursion
1933 (beginning-of-line)
1934 (if (looking-at "^D")
1936 (buffer-substring
1937 (point)
1938 (progn (end-of-line) (point))))))
1939 (o-col (current-column)))
1940 (if hide-em (bookmark-bmenu-hide-filenames))
1941 (setq bookmark-bmenu-toggle-filenames nil)
1942 (goto-char (point-min))
1943 (forward-line 1)
1944 (while (re-search-forward "^D" (point-max) t)
1945 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1946 (bookmark-bmenu-list)
1947 (setq bookmark-bmenu-toggle-filenames hide-em)
1948 (if bookmark-bmenu-toggle-filenames
1949 (bookmark-bmenu-toggle-filenames t))
1950 (if o-str
1951 (progn
1952 (goto-char (point-min))
1953 (search-forward o-str)
1954 (beginning-of-line)
1955 (forward-char o-col))
1956 (goto-char o-point))
1957 (beginning-of-line)
1958 (setq bookmark-alist-modification-count
1959 (1+ bookmark-alist-modification-count))
1960 (if (bookmark-time-to-save-p)
1961 (bookmark-save))
1962 (message "Deleting bookmarks...done")
1966 (defun bookmark-bmenu-rename ()
1967 "Rename bookmark on current line. Prompts for a new name."
1968 (interactive)
1969 (if (bookmark-bmenu-check-position)
1970 (let ((bmrk (bookmark-bmenu-bookmark))
1971 (thispoint (point)))
1972 (bookmark-rename bmrk)
1973 (bookmark-bmenu-list)
1974 (goto-char thispoint))))
1977 (defun bookmark-bmenu-locate ()
1978 "Display location of this bookmark. Displays in the minibuffer."
1979 (interactive)
1980 (if (bookmark-bmenu-check-position)
1981 (let ((bmrk (bookmark-bmenu-bookmark)))
1982 (message "%s" (bookmark-location bmrk)))))
1984 (defun bookmark-bmenu-relocate ()
1985 "Change the file path of the bookmark on the current line,
1986 prompting with completion for the new path."
1987 (interactive)
1988 (if (bookmark-bmenu-check-position)
1989 (let ((bmrk (bookmark-bmenu-bookmark))
1990 (thispoint (point)))
1991 (bookmark-relocate bmrk)
1992 (goto-char thispoint))))
1995 ;;; Menu bar stuff. Prefix is "bookmark-menu".
1997 (defun bookmark-menu-popup-paned-menu (event name entries)
1998 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
1999 That is, ENTRIES is a list of strings which appear as the choices
2000 in the menu.
2001 The number of panes depends on the number of entries.
2002 The visible entries are truncated to `bookmark-menu-length', but the
2003 strings returned are not."
2004 (let ((f-height (/ (frame-height) 2))
2005 (pane-list nil)
2006 (iter 0))
2007 (while entries
2008 (let (lst
2009 (count 0))
2010 (while (and (< count f-height) entries)
2011 (let ((str (car entries)))
2012 (push (cons
2013 (if (> (length str) bookmark-menu-length)
2014 (substring str 0 bookmark-menu-length)
2015 str)
2016 str)
2017 lst)
2018 (setq entries (cdr entries))
2019 (setq count (1+ count))))
2020 (setq iter (1+ iter))
2021 (push (cons
2022 (format "-*- %s (%d) -*-" name iter)
2023 (nreverse lst))
2024 pane-list)))
2026 ;; Popup the menu and return the string.
2027 (x-popup-menu event (cons (concat "-*- " name " -*-")
2028 (nreverse pane-list)))))
2031 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2032 ;; following works, and for explaining what to do to make it work.
2034 ;; We MUST autoload EACH form used to set up this variable's value, so
2035 ;; that the whole job is done in loaddefs.el.
2037 ;; Emacs menubar stuff.
2039 ;;;###autoload
2040 (defvar menu-bar-bookmark-map
2041 (let ((map (make-sparse-keymap "Bookmark functions")))
2042 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2043 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2044 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2045 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2046 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2047 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2048 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2049 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2050 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2051 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2052 map))
2054 ;;;###autoload
2055 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2057 ;; make bookmarks appear toward the right side of the menu.
2058 (if (boundp 'menu-bar-final-items)
2059 (if menu-bar-final-items
2060 (setq menu-bar-final-items
2061 (cons 'bookmark menu-bar-final-items)))
2062 (setq menu-bar-final-items '(bookmark)))
2064 ;;;; end bookmark menu stuff ;;;;
2067 ;;; Load Hook
2068 (defvar bookmark-load-hook nil
2069 "Hook run at the end of loading bookmark.")
2071 ;;; Exit Hook, called from kill-emacs-hook
2072 (defvar bookmark-exit-hook nil
2073 "Hook run when Emacs exits.")
2075 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2077 (defun bookmark-exit-hook-internal ()
2078 "Save bookmark state, if necessary, at Emacs exit time.
2079 This also runs `bookmark-exit-hook'."
2080 (run-hooks 'bookmark-exit-hook)
2081 (and bookmark-alist
2082 (bookmark-time-to-save-p t)
2083 (bookmark-save)))
2085 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2088 (run-hooks 'bookmark-load-hook)
2090 (provide 'bookmark)
2092 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2093 ;;; bookmark.el ends here