(bookmark-write-file): Avoid calling `pp' with large
[emacs.git] / lisp / bookmark.el
blobbd8511cda71ddda3c4e1dd1b503b5ba0148d9744
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, 2009 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)
82 (eval-when-compile (require 'cl))
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom bookmark-save-flag t
108 "Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "Non-nil means show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'bookmark)
200 (defface bookmark-menu-heading
201 '((t (:inherit font-lock-type-face)))
202 "Face used to highlight the heading in bookmark menu buffers."
203 :group 'bookmark
204 :version "22.1")
207 ;;; No user-serviceable parts beyond this point.
209 ;; Added for lucid emacs compatibility, db
210 (or (fboundp 'defalias) (fset 'defalias 'fset))
212 ;; suggested for lucid compatibility by david hughes:
213 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
216 ;;; Keymap stuff:
218 ;; Set up these bindings dumping time *only*;
219 ;; if the user alters them, don't override the user when loading bookmark.el.
221 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
222 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
223 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
225 ;;;###autoload
226 (defvar bookmark-map
227 (let ((map (make-sparse-keymap)))
228 ;; Read the help on all of these functions for details...
229 (define-key map "x" 'bookmark-set)
230 (define-key map "m" 'bookmark-set) ;"m"ark
231 (define-key map "j" 'bookmark-jump)
232 (define-key map "g" 'bookmark-jump) ;"g"o
233 (define-key map "o" 'bookmark-jump-other-window)
234 (define-key map "i" 'bookmark-insert)
235 (define-key map "e" 'edit-bookmarks)
236 (define-key map "f" 'bookmark-insert-location) ;"f"ind
237 (define-key map "r" 'bookmark-rename)
238 (define-key map "d" 'bookmark-delete)
239 (define-key map "l" 'bookmark-load)
240 (define-key map "w" 'bookmark-write)
241 (define-key map "s" 'bookmark-save)
242 map)
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
249 ;;;###autoload (fset 'bookmark-map bookmark-map)
252 ;;; Core variables and data structures:
253 (defvar bookmark-alist ()
254 "Association list of bookmarks and their records.
255 You probably don't want to change the value of this alist yourself;
256 instead, let the various bookmark functions do it for you.
258 The format of the alist is
260 \(BOOKMARK1 BOOKMARK2 ...\)
262 where each BOOKMARK is of the form
264 (NAME PARAM-ALIST) or (NAME . PARAM-ALIST)
266 where the first form is the old deprecated one and the second is
267 the new favored one. PARAM-ALIST is typically of the form:
269 ((filename . FILE)
270 (front-context-string . FRONT-STR)
271 (rear-context-string . REAR-STR)
272 (position . POS)
273 (annotation . ANNOTATION)))")
276 (defvar bookmarks-already-loaded nil)
279 ;; more stuff added by db.
281 (defvar bookmark-current-bookmark nil
282 "Name of bookmark most recently used in the current file.
283 It is buffer local, used to make moving a bookmark forward
284 through a file easier.")
286 (make-variable-buffer-local 'bookmark-current-bookmark)
289 (defvar bookmark-alist-modification-count 0
290 "Number of modifications to bookmark list since it was last saved.")
293 (defvar bookmark-search-size 16
294 "Length of the context strings recorded on either side of a bookmark.")
297 (defvar bookmark-current-point 0)
298 (defvar bookmark-yank-point 0)
299 (defvar bookmark-current-buffer nil)
301 (defvar Info-suffix-list)
303 ;; Helper functions.
305 ;; Only functions on this page and the next one (file formats) need to
306 ;; know anything about the format of bookmark-alist entries.
307 ;; Everyone else should go through them.
310 (defun bookmark-name-from-full-record (full-record)
311 "Return name of FULL-RECORD \(an alist element instead of a string\)."
312 (car full-record))
315 (defun bookmark-all-names ()
316 "Return a list of all current bookmark names."
317 (bookmark-maybe-load-default-file)
318 (mapcar 'bookmark-name-from-full-record bookmark-alist))
321 (defun bookmark-get-bookmark (bookmark &optional noerror)
322 "Return the bookmark record corresponding to BOOKMARK.
323 If BOOKMARK is already a bookmark record, just return it,
324 Otherwise look for the corresponding bookmark in `bookmark-alist'."
325 (cond
326 ((consp bookmark) bookmark)
327 ((stringp bookmark)
328 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
329 (unless noerror (error "Invalid bookmark %s" bookmark))))))
332 (defun bookmark-get-bookmark-record (bookmark)
333 "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
334 That is, all information but the name."
335 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
336 ;; The bookmark objects can either look like (NAME ALIST) or
337 ;; (NAME . ALIST), so we have to distinguish the two here.
338 (if (and (null (cdr alist)) (consp (caar alist)))
339 (car alist) alist)))
342 (defun bookmark-set-name (bookmark newname)
343 "Set BOOKMARK's name to NEWNAME."
344 (setcar
345 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
346 newname))
348 (defun bookmark-prop-get (bookmark prop)
349 "Return the property PROP of BOOKMARK, or nil if none."
350 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
352 (defun bookmark-prop-set (bookmark prop val)
353 "Set the property PROP of BOOKMARK to VAL."
354 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
355 (if cell
356 (setcdr cell val)
357 (nconc (bookmark-get-bookmark-record bookmark)
358 (list (cons prop val))))))
360 (defun bookmark-get-annotation (bookmark)
361 "Return the annotation of BOOKMARK, or nil if none."
362 (bookmark-prop-get bookmark 'annotation))
364 (defun bookmark-set-annotation (bookmark ann)
365 "Set the annotation of BOOKMARK to ANN."
366 (bookmark-prop-set bookmark 'annotation ann))
369 (defun bookmark-get-filename (bookmark)
370 "Return the full filename of BOOKMARK."
371 (bookmark-prop-get bookmark 'filename))
374 (defun bookmark-set-filename (bookmark filename)
375 "Set the full filename of BOOKMARK to FILENAME."
376 (bookmark-prop-set bookmark 'filename filename))
379 (defun bookmark-get-position (bookmark)
380 "Return the position \(i.e.: point\) of BOOKMARK."
381 (bookmark-prop-get bookmark 'position))
384 (defun bookmark-set-position (bookmark position)
385 "Set the position \(i.e.: point\) of BOOKMARK to POSITION."
386 (bookmark-prop-set bookmark 'position position))
389 (defun bookmark-get-front-context-string (bookmark)
390 "Return the front-context-string of BOOKMARK."
391 (bookmark-prop-get bookmark 'front-context-string))
394 (defun bookmark-set-front-context-string (bookmark string)
395 "Set the front-context-string of BOOKMARK to STRING."
396 (bookmark-prop-set bookmark 'front-context-string string))
399 (defun bookmark-get-rear-context-string (bookmark)
400 "Return the rear-context-string of BOOKMARK."
401 (bookmark-prop-get bookmark 'rear-context-string))
404 (defun bookmark-set-rear-context-string (bookmark string)
405 "Set the rear-context-string of BOOKMARK to STRING."
406 (bookmark-prop-set bookmark 'rear-context-string string))
409 (defun bookmark-get-handler (bookmark)
410 (bookmark-prop-get bookmark 'handler))
412 (defvar bookmark-history nil
413 "The history list for bookmark functions.")
416 (defun bookmark-completing-read (prompt &optional default)
417 "Prompting with PROMPT, read a bookmark name in completion.
418 PROMPT will get a \": \" stuck on the end no matter what, so you
419 probably don't want to include one yourself.
420 Optional second arg DEFAULT is a string to return if the user enters
421 the empty string."
422 (bookmark-maybe-load-default-file) ; paranoia
423 (if (listp last-nonmenu-event)
424 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
425 (let* ((completion-ignore-case bookmark-completion-ignore-case)
426 (default default)
427 (prompt (if default
428 (concat prompt (format " (%s): " default))
429 (concat prompt ": ")))
430 (str
431 (completing-read prompt
432 bookmark-alist
436 'bookmark-history)))
437 (if (string-equal "" str) default str))))
440 (defmacro bookmark-maybe-historicize-string (string)
441 "Put STRING into the bookmark prompt history, if caller non-interactive.
442 We need this because sometimes bookmark functions are invoked from
443 menus, so `completing-read' never gets a chance to set `bookmark-history'."
444 `(or
445 (interactive-p)
446 (setq bookmark-history (cons ,string bookmark-history))))
448 (defvar bookmark-make-record-function 'bookmark-make-record-default
449 "A function that should be called to create a bookmark record.
450 Modes may set this variable buffer-locally to enable bookmarking of
451 locations that should be treated specially, such as Info nodes,
452 news posts, images, pdf documents, etc.
454 The function will be called with no arguments.
455 It should signal a user error if it is unable to construct a record for
456 the current location.
458 The returned record should be a cons cell of the form (NAME . ALIST)
459 where ALIST is as described in `bookmark-alist' and may typically contain
460 a special cons (handler . SOME-FUNCTION) which sets the handler function
461 that should be used to open this bookmark instead of
462 `bookmark-default-handler'. The handler should follow the same calling
463 convention as the one used by `bookmark-default-handler'.
465 NAME is a suggested name for the constructed bookmark. It can be nil
466 in which case a default heuristic will be used. The function can also
467 equivalently just return ALIST without NAME.")
469 (defun bookmark-make-record ()
470 "Return a new bookmark record (NAME . ALIST) for the current location."
471 (let ((record (funcall bookmark-make-record-function)))
472 ;; Set up default name.
473 (if (stringp (car record))
474 ;; The function already provided a default name.
475 record
476 (if (car record) (push nil record))
477 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
478 record)))
480 (defun bookmark-store (name alist no-overwrite)
481 "Store the bookmark NAME with data ALIST.
482 If NO-OVERWRITE is non-nil and another bookmark of the same name already
483 exists in `bookmark-alist', record the new bookmark without throwing away the
484 old one."
485 (bookmark-maybe-load-default-file)
486 (let ((stripped-name (copy-sequence name)))
487 (or (featurep 'xemacs)
488 ;; XEmacs's `set-text-properties' doesn't work on
489 ;; free-standing strings, apparently.
490 (set-text-properties 0 (length stripped-name) nil stripped-name))
491 (if (and (not no-overwrite)
492 (bookmark-get-bookmark stripped-name 'noerror))
493 ;; already existing bookmark under that name and
494 ;; no prefix arg means just overwrite old bookmark
495 ;; Use the new (NAME . ALIST) format.
496 (setcdr (bookmark-get-bookmark stripped-name) alist)
498 ;; otherwise just cons it onto the front (either the bookmark
499 ;; doesn't exist already, or there is no prefix arg. In either
500 ;; case, we want the new bookmark consed onto the alist...)
502 (push (cons stripped-name alist) bookmark-alist))
504 ;; Added by db
505 (setq bookmark-current-bookmark stripped-name)
506 (setq bookmark-alist-modification-count
507 (1+ bookmark-alist-modification-count))
508 (if (bookmark-time-to-save-p)
509 (bookmark-save))
511 (setq bookmark-current-bookmark stripped-name)
512 (bookmark-bmenu-surreptitiously-rebuild-list)))
514 (defun bookmark-make-record-default (&optional point-only)
515 "Return the record describing the location of a new bookmark.
516 Must be at the correct position in the buffer in which the bookmark is
517 being set.
518 If POINT-ONLY is non-nil, then only return the subset of the
519 record that pertains to the location within the buffer."
520 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
521 (front-context-string
522 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
523 (buffer-substring-no-properties
524 (point)
525 (+ (point) bookmark-search-size))
526 nil))
527 (rear-context-string
528 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
529 (buffer-substring-no-properties
530 (point)
531 (- (point) bookmark-search-size))
532 nil))
533 (position . ,(point))))
536 ;;; File format stuff
538 ;; The OLD format of the bookmark-alist was:
540 ;; ((BOOKMARK-NAME . (FILENAME
541 ;; STRING-IN-FRONT
542 ;; STRING-BEHIND
543 ;; POINT))
544 ;; ...)
546 ;; The NEW format of the bookmark-alist is:
548 ;; ((BOOKMARK-NAME (filename . FILENAME)
549 ;; (front-context-string . STRING-IN-FRONT)
550 ;; (rear-context-string . STRING-BEHIND)
551 ;; (position . POINT)
552 ;; (annotation . ANNOTATION)
553 ;; (whatever . VALUE)
554 ;; ...
555 ;; ))
556 ;; ...)
559 ;; I switched to using an internal as well as external alist because I
560 ;; felt that would be a more flexible framework in which to add
561 ;; features. It means that the order in which values appear doesn't
562 ;; matter, and it means that arbitrary values can be added without
563 ;; risk of interfering with existing ones.
565 ;; BOOKMARK-NAME is the string the user gives the bookmark and
566 ;; accesses it by from then on.
568 ;; FILENAME is the location of the file in which the bookmark is set.
570 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
571 ;; context in front of the point at which the bookmark is set.
573 ;; STRING-BEHIND is the same thing, but after the point.
575 ;; The context strings exist so that modifications to a file don't
576 ;; necessarily cause a bookmark's position to be invalidated.
577 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
578 ;; case the file has changed since the bookmark was set. It will
579 ;; attempt to place the user before the changes, if there were any.
580 ;; ANNOTATION is the annotation for the bookmark; it may not exist
581 ;; (for backward compatibility), be nil (no annotation), or be a
582 ;; string.
585 (defconst bookmark-file-format-version 1
586 "The current version of the format used by bookmark files.
587 You should never need to change this.")
590 (defconst bookmark-end-of-version-stamp-marker
591 "-*- End Of Bookmark File Format Version Stamp -*-\n"
592 "This string marks the end of the version stamp in a bookmark file.")
595 (defun bookmark-alist-from-buffer ()
596 "Return a `bookmark-alist' (in any format) from the current buffer.
597 The buffer must of course contain bookmark format information.
598 Does not care from where in the buffer it is called, and does not
599 affect point."
600 (save-excursion
601 (goto-char (point-min))
602 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
603 (read (current-buffer))
604 ;; Else we're dealing with format version 0
605 (if (search-forward "(" nil t)
606 (progn
607 (forward-char -1)
608 (read (current-buffer)))
609 ;; Else no hope of getting information here.
610 (error "Not bookmark format")))))
613 (defun bookmark-upgrade-version-0-alist (old-list)
614 "Upgrade a version 0 alist OLD-LIST to the current version."
615 (mapcar
616 (lambda (bookmark)
617 (let* ((name (car bookmark))
618 (record (car (cdr bookmark)))
619 (filename (nth 0 record))
620 (front-str (nth 1 record))
621 (rear-str (nth 2 record))
622 (position (nth 3 record))
623 (ann (nth 4 record)))
624 (list
625 name
626 `((filename . ,filename)
627 (front-context-string . ,(or front-str ""))
628 (rear-context-string . ,(or rear-str ""))
629 (position . ,position)
630 (annotation . ,ann)))))
631 old-list))
634 (defun bookmark-upgrade-file-format-from-0 ()
635 "Upgrade a bookmark file of format 0 (the original format) to format 1.
636 This expects to be called from `point-min' in a bookmark file."
637 (message "Upgrading bookmark format from 0 to %d..."
638 bookmark-file-format-version)
639 (let* ((old-list (bookmark-alist-from-buffer))
640 (new-list (bookmark-upgrade-version-0-alist old-list)))
641 (delete-region (point-min) (point-max))
642 (bookmark-insert-file-format-version-stamp)
643 (pp new-list (current-buffer))
644 (save-buffer))
645 (goto-char (point-min))
646 (message "Upgrading bookmark format from 0 to %d...done"
647 bookmark-file-format-version)
651 (defun bookmark-grok-file-format-version ()
652 "Return an integer which is the file-format version of this bookmark file.
653 This expects to be called from `point-min' in a bookmark file."
654 (if (looking-at "^;;;;")
655 (save-excursion
656 (save-match-data
657 (re-search-forward "[0-9]")
658 (forward-char -1)
659 (read (current-buffer))))
660 ;; Else this is format version 0, the original one, which didn't
661 ;; even have version stamps.
665 (defun bookmark-maybe-upgrade-file-format ()
666 "Check the file-format version of this bookmark file.
667 If the version is not up-to-date, upgrade it automatically.
668 This expects to be called from `point-min' in a bookmark file."
669 (let ((version (bookmark-grok-file-format-version)))
670 (cond
671 ((= version bookmark-file-format-version)
672 ) ; home free -- version is current
673 ((= version 0)
674 (bookmark-upgrade-file-format-from-0))
676 (error "Bookmark file format version strangeness")))))
679 (defun bookmark-insert-file-format-version-stamp ()
680 "Insert text indicating current version of bookmark file format."
681 (insert
682 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
683 bookmark-file-format-version))
684 (insert ";;; This format is meant to be slightly human-readable;\n"
685 ";;; nevertheless, you probably don't want to edit it.\n"
686 ";;; "
687 bookmark-end-of-version-stamp-marker))
690 ;;; end file-format stuff
693 ;;; Generic helpers.
695 (defun bookmark-maybe-message (fmt &rest args)
696 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
697 (if (>= baud-rate 9600)
698 (apply 'message fmt args)))
701 ;;; Core code:
703 (defvar bookmark-minibuffer-read-name-map
704 (let ((map (make-sparse-keymap)))
705 (set-keymap-parent map minibuffer-local-map)
706 (define-key map "\C-w" 'bookmark-yank-word)
707 ;; This C-u binding might not be very useful any more now that we
708 ;; provide access to the default via the standard M-n binding.
709 ;; Maybe we should just remove it? --Stef-08
710 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
711 map))
713 ;;;###autoload
714 (defun bookmark-set (&optional name parg)
715 "Set a bookmark named NAME inside a file.
716 If name is nil, then the user will be prompted.
717 With prefix arg, will not overwrite a bookmark that has the same name
718 as NAME if such a bookmark already exists, but instead will \"push\"
719 the new bookmark onto the bookmark alist. Thus the most recently set
720 bookmark with name NAME would be the one in effect at any given time,
721 but the others are still there, should you decide to delete the most
722 recent one.
724 To yank words from the text of the buffer and use them as part of the
725 bookmark name, type C-w while setting a bookmark. Successive C-w's
726 yank successive words.
728 Typing C-u inserts the name of the last bookmark used in the buffer
729 \(as an aid in using a single bookmark name to track your progress
730 through a large file\). If no bookmark was used, then C-u inserts the
731 name of the file being visited.
733 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
734 and it removes only the first instance of a bookmark with that name from
735 the list of bookmarks.\)"
736 (interactive (list nil current-prefix-arg))
737 (let* ((record (bookmark-make-record))
738 (default (car record)))
740 (bookmark-maybe-load-default-file)
742 (setq bookmark-current-point (point))
743 (setq bookmark-yank-point (point))
744 (setq bookmark-current-buffer (current-buffer))
746 (let ((str
747 (or name
748 (read-from-minibuffer
749 (format "Set bookmark (%s): " default)
751 bookmark-minibuffer-read-name-map
752 nil nil default))))
753 (and (string-equal str "") (setq str default))
754 (bookmark-store str (cdr record) parg)
756 ;; Ask for an annotation buffer for this bookmark
757 (if bookmark-use-annotations
758 (bookmark-edit-annotation str)
759 (goto-char bookmark-current-point)))))
761 (defun bookmark-kill-line (&optional newline-too)
762 "Kill from point to end of line.
763 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
764 Does not affect the kill ring."
765 (let ((eol (save-excursion (end-of-line) (point))))
766 (delete-region (point) eol)
767 (if (and newline-too (looking-at "\n"))
768 (delete-char 1))))
771 ;; Defvars to avoid compilation warnings:
772 (defvar bookmark-annotation-name nil
773 "Variable holding the name of the bookmark.
774 This is used in `bookmark-edit-annotation' to record the bookmark
775 whose annotation is being edited.")
778 (defun bookmark-default-annotation-text (bookmark)
779 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
780 "# All lines which start with a '#' will be deleted.\n"
781 "# Type C-c C-c when done.\n#\n"
782 "# Author: " (user-full-name) " <" (user-login-name) "@"
783 (system-name) ">\n"
784 "# Date: " (current-time-string) "\n"))
787 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
788 "Function to return default text to use for a bookmark annotation.
789 It takes one argument, the name of the bookmark, as a string.")
790 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
791 'bookmark-edit-annotation-text-func "23.1")
793 (defvar bookmark-edit-annotation-mode-map
794 (let ((map (make-sparse-keymap)))
795 (set-keymap-parent map text-mode-map)
796 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
797 map)
798 "Keymap for editing an annotation of a bookmark.")
801 (defun bookmark-edit-annotation-mode (bookmark)
802 "Mode for editing the annotation of bookmark BOOKMARK.
803 When you have finished composing, type \\[bookmark-send-annotation].
805 \\{bookmark-edit-annotation-mode-map}"
806 (interactive)
807 (kill-all-local-variables)
808 (make-local-variable 'bookmark-annotation-name)
809 (setq bookmark-annotation-name bookmark)
810 (use-local-map bookmark-edit-annotation-mode-map)
811 (setq major-mode 'bookmark-edit-annotation-mode
812 mode-name "Edit Bookmark Annotation")
813 (insert (funcall bookmark-edit-annotation-text-func bookmark))
814 (let ((annotation (bookmark-get-annotation bookmark)))
815 (if (and annotation (not (string-equal annotation "")))
816 (insert annotation)))
817 (run-mode-hooks 'text-mode-hook))
820 (defun bookmark-send-edited-annotation ()
821 "Use buffer contents as annotation for a bookmark.
822 Lines beginning with `#' are ignored."
823 (interactive)
824 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
825 (error "Not in bookmark-edit-annotation-mode"))
826 (goto-char (point-min))
827 (while (< (point) (point-max))
828 (if (looking-at "^#")
829 (bookmark-kill-line t)
830 (forward-line 1)))
831 ;; Take no chances with text properties.
832 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
833 (bookmark bookmark-annotation-name))
834 (bookmark-set-annotation bookmark annotation)
835 (bookmark-bmenu-surreptitiously-rebuild-list)
836 (goto-char bookmark-current-point))
837 (kill-buffer (current-buffer)))
840 (defun bookmark-edit-annotation (bookmark)
841 "Pop up a buffer for editing bookmark BOOKMARK's annotation."
842 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
843 (bookmark-edit-annotation-mode bookmark))
846 (defun bookmark-insert-current-bookmark ()
847 "Insert this buffer's value of `bookmark-current-bookmark'.
848 Default to file name if it's nil."
849 (interactive)
850 (let ((str
851 (with-current-buffer bookmark-current-buffer
852 (or bookmark-current-bookmark
853 (bookmark-buffer-name)))))
854 (insert str)))
857 (defun bookmark-buffer-name ()
858 "Return the name of the current buffer's file, non-directory."
859 (cond
860 ;; Or are we a file?
861 (buffer-file-name (file-name-nondirectory buffer-file-name))
862 ;; Or are we a directory?
863 ((and (boundp 'dired-directory) dired-directory)
864 (let* ((dirname (if (stringp dired-directory)
865 dired-directory
866 (car dired-directory)))
867 (idx (1- (length dirname))))
868 ;; Strip the trailing slash.
869 (if (= ?/ (aref dirname idx))
870 (file-name-nondirectory (substring dirname 0 idx))
871 ;; Else return the current-buffer
872 (buffer-name (current-buffer)))))
873 ;; If all else fails, use the buffer's name.
875 (buffer-name (current-buffer)))))
878 (defun bookmark-yank-word ()
879 (interactive)
880 ;; get the next word from the buffer and append it to the name of
881 ;; the bookmark currently being set.
882 (let ((string (with-current-buffer bookmark-current-buffer
883 (goto-char bookmark-yank-point)
884 (buffer-substring-no-properties
885 (point)
886 (progn
887 (forward-word 1)
888 (setq bookmark-yank-point (point)))))))
889 (insert string)))
891 (defun bookmark-buffer-file-name ()
892 "Return the current buffer's file in a way useful for bookmarks."
893 ;; Abbreviate the path, both so it's shorter and so it's more
894 ;; portable. E.g., the user's home dir might be a different
895 ;; path on different machines, but "~/" will still reach it.
896 (abbreviate-file-name
897 (cond
898 (buffer-file-name buffer-file-name)
899 ((and (boundp 'dired-directory) dired-directory)
900 (if (stringp dired-directory)
901 dired-directory
902 (car dired-directory)))
903 (t (error "Buffer not visiting a file or directory")))))
906 (defun bookmark-maybe-load-default-file ()
907 (and (not bookmarks-already-loaded)
908 (null bookmark-alist)
909 (prog2
910 (and
911 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
912 ;; to be renamed.
913 (file-exists-p (expand-file-name bookmark-old-default-file))
914 (not (file-exists-p (expand-file-name bookmark-default-file)))
915 (rename-file (expand-file-name bookmark-old-default-file)
916 (expand-file-name bookmark-default-file)))
917 ;; return t so the `and' will continue...
920 (file-readable-p (expand-file-name bookmark-default-file))
921 (bookmark-load bookmark-default-file t t)
922 (setq bookmarks-already-loaded t)))
925 (defun bookmark-maybe-sort-alist ()
926 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
927 ;;is non-nil, then return a sorted copy of the alist.
928 (if bookmark-sort-flag
929 (sort (copy-alist bookmark-alist)
930 (function
931 (lambda (x y) (string-lessp (car x) (car y)))))
932 bookmark-alist))
935 (defvar bookmark-after-jump-hook nil
936 "Hook run after `bookmark-jump' jumps to a bookmark.
937 Useful for example to unhide text in `outline-mode'.")
939 (defun bookmark--jump-via (bookmark display-function)
940 (bookmark-handle-bookmark bookmark)
941 (save-current-buffer
942 (funcall display-function (current-buffer)))
943 (let ((win (get-buffer-window (current-buffer) 0)))
944 (if win (set-window-point win (point))))
945 ;; FIXME: we used to only run bookmark-after-jump-hook in
946 ;; `bookmark-jump' itself, but in none of the other commands.
947 (run-hooks 'bookmark-after-jump-hook)
948 (if bookmark-automatically-show-annotations
949 ;; if there is an annotation for this bookmark,
950 ;; show it in a buffer.
951 (bookmark-show-annotation bookmark)))
954 ;;;###autoload
955 (defun bookmark-jump (bookmark)
956 "Jump to bookmark BOOKMARK (a point in some file).
957 You may have a problem using this function if the value of variable
958 `bookmark-alist' is nil. If that happens, you need to load in some
959 bookmarks. See help on function `bookmark-load' for more about
960 this.
962 If the file pointed to by BOOKMARK no longer exists, you will be asked
963 if you wish to give the bookmark a new location, and `bookmark-jump'
964 will then jump to the new location, as well as recording it in place
965 of the old one in the permanent bookmark record."
966 (interactive
967 (list (bookmark-completing-read "Jump to bookmark"
968 bookmark-current-bookmark)))
969 (unless bookmark
970 (error "No bookmark specified"))
971 (bookmark-maybe-historicize-string bookmark)
972 (bookmark--jump-via bookmark 'switch-to-buffer))
975 ;;;###autoload
976 (defun bookmark-jump-other-window (bookmark)
977 "Jump to BOOKMARK (a point in some file) in another window.
978 See `bookmark-jump'."
979 (interactive
980 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
981 bookmark-current-bookmark)))
982 (if (> emacs-major-version 21)
983 (list bkm) bkm)))
984 (when bookmark
985 (bookmark-maybe-historicize-string bookmark)
986 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
989 (defun bookmark-file-or-variation-thereof (file)
990 "Return FILE (a string) if it exists, or return a reasonable
991 variation of FILE if that exists. Reasonable variations are checked
992 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
993 nor a reasonable variation thereof, then still return FILE if it can
994 be retrieved from a VC backend, else return nil."
995 (if (file-exists-p file)
996 file
998 (progn (require 'info) ; ensure Info-suffix-list is bound
999 (catch 'found
1000 (mapc (lambda (elt)
1001 (let ((suffixed-file (concat file (car elt))))
1002 (if (file-exists-p suffixed-file)
1003 (throw 'found suffixed-file))))
1004 Info-suffix-list)
1005 nil))
1006 ;; Last possibility: try VC
1007 (if (vc-backend file) file))))
1009 (defun bookmark-jump-noselect (bookmark)
1010 "Return the location pointed to by the bookmark BOOKMARK.
1011 The return value has the form (BUFFER . POINT).
1013 Note: this function is deprecated and is present for Emacs 22
1014 compatibility only."
1015 (save-excursion
1016 (bookmark-handle-bookmark bookmark)
1017 (cons (current-buffer) (point))))
1019 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1021 (defun bookmark-handle-bookmark (bookmark)
1022 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1023 Changes current buffer and point and returns nil, or signals a `file-error'.
1024 BOOKMARK can be a bookmark record used internally by some other
1025 elisp package, or the name of a bookmark to be found in `bookmark-alist'."
1026 (condition-case err
1027 (funcall (or (bookmark-get-handler bookmark)
1028 'bookmark-default-handler)
1029 (bookmark-get-bookmark bookmark))
1030 (file-error
1031 ;; We were unable to find the marked file, so ask if user wants to
1032 ;; relocate the bookmark, else remind them to consider deletion.
1033 (when (stringp bookmark)
1034 ;; `bookmark' can either be a bookmark name (found in
1035 ;; `bookmark-alist') or a bookmark object. If it's an object, we
1036 ;; assume it's a bookmark used internally by some other package.
1037 (let ((file (bookmark-get-filename bookmark)))
1038 (when file ;Don't know how to relocate if there's no `file'.
1039 (setq file (expand-file-name file))
1040 (ding)
1041 (if (y-or-n-p (concat (file-name-nondirectory file)
1042 " nonexistent. Relocate \""
1043 bookmark
1044 "\"? "))
1045 (progn
1046 (bookmark-relocate bookmark)
1047 ;; Try again.
1048 (funcall (or (bookmark-get-handler bookmark)
1049 'bookmark-default-handler)
1050 (bookmark-get-bookmark bookmark)))
1051 (message
1052 "Bookmark not relocated; consider removing it \(%s\)." bookmark)
1053 (signal (car err) (cdr err))))))))
1054 ;; Added by db.
1055 (when (stringp bookmark)
1056 (setq bookmark-current-bookmark bookmark))
1057 nil)
1059 (defun bookmark-default-handler (bmk)
1060 "Default handler to jump to a particular bookmark location.
1061 BMK is a bookmark record.
1062 Changes current buffer and point and returns nil, or signals a `file-error'."
1063 (let* ((file (bookmark-get-filename bmk))
1064 (buf (bookmark-prop-get bmk 'buffer))
1065 (forward-str (bookmark-get-front-context-string bmk))
1066 (behind-str (bookmark-get-rear-context-string bmk))
1067 (place (bookmark-get-position bmk)))
1068 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1069 ;; but now that Info bookmarks are handled elsewhere it seems that we
1070 ;; should be able to get rid of it. --Stef
1071 (if (not (if buf (buffer-live-p buf)
1072 (setq file (bookmark-file-or-variation-thereof file))))
1073 (signal 'file-error
1074 `("Jumping to bookmark" "No such file or directory"
1075 (bookmark-get-filename bmk)))
1076 (set-buffer (or buf (find-file-noselect file)))
1077 (if place (goto-char place))
1079 ;; Go searching forward first. Then, if forward-str exists and
1080 ;; was found in the file, we can search backward for behind-str.
1081 ;; Rationale is that if text was inserted between the two in the
1082 ;; file, it's better to be put before it so you can read it,
1083 ;; rather than after and remain perhaps unaware of the changes.
1084 (if forward-str
1085 (if (search-forward forward-str (point-max) t)
1086 (goto-char (match-beginning 0))))
1087 (if behind-str
1088 (if (search-backward behind-str (point-min) t)
1089 (goto-char (match-end 0)))))
1090 nil))
1092 ;;;###autoload
1093 (defun bookmark-relocate (bookmark)
1094 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1095 This makes an already existing bookmark point to that file, instead of
1096 the one it used to point at. Useful when a file has been renamed
1097 after a bookmark was set in it."
1098 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1099 (bookmark-maybe-historicize-string bookmark)
1100 (bookmark-maybe-load-default-file)
1101 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1102 (newloc (expand-file-name
1103 (read-file-name
1104 (format "Relocate %s to: " bookmark)
1105 (file-name-directory bmrk-filename)))))
1106 (bookmark-set-filename bookmark newloc)
1107 (setq bookmark-alist-modification-count
1108 (1+ bookmark-alist-modification-count))
1109 (if (bookmark-time-to-save-p)
1110 (bookmark-save))
1111 (bookmark-bmenu-surreptitiously-rebuild-list)))
1114 ;;;###autoload
1115 (defun bookmark-insert-location (bookmark &optional no-history)
1116 "Insert the name of the file associated with BOOKMARK.
1117 Optional second arg NO-HISTORY means don't record this in the
1118 minibuffer history list `bookmark-history'."
1119 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1120 (or no-history (bookmark-maybe-historicize-string bookmark))
1121 (let ((start (point)))
1122 (prog1
1123 (insert (bookmark-location bookmark)) ; *Return this line*
1124 (if (and (display-color-p) (display-mouse-p))
1125 (add-text-properties
1126 start
1127 (save-excursion (re-search-backward
1128 "[^ \t]")
1129 (1+ (point)))
1130 '(mouse-face highlight
1131 follow-link t
1132 help-echo "mouse-2: go to this bookmark in other window"))))))
1134 ;;;###autoload
1135 (defalias 'bookmark-locate 'bookmark-insert-location)
1137 (defun bookmark-location (bookmark)
1138 "Return the name of the file associated with BOOKMARK."
1139 (bookmark-maybe-load-default-file)
1140 (bookmark-get-filename bookmark))
1143 ;;;###autoload
1144 (defun bookmark-rename (old &optional new)
1145 "Change the name of OLD bookmark to NEW name.
1146 If called from keyboard, prompt for OLD and NEW. If called from
1147 menubar, select OLD from a menu and prompt for NEW.
1149 If called from Lisp, prompt for NEW if only OLD was passed as an
1150 argument. If called with two strings, then no prompting is done. You
1151 must pass at least OLD when calling from Lisp.
1153 While you are entering the new name, consecutive C-w's insert
1154 consecutive words from the text of the buffer into the new bookmark
1155 name."
1156 (interactive (list (bookmark-completing-read "Old bookmark name")))
1157 (bookmark-maybe-historicize-string old)
1158 (bookmark-maybe-load-default-file)
1160 (setq bookmark-current-point (point))
1161 (setq bookmark-yank-point (point))
1162 (setq bookmark-current-buffer (current-buffer))
1163 (let ((newname
1164 (or new ; use second arg, if non-nil
1165 (read-from-minibuffer
1166 "New name: "
1168 (let ((now-map (copy-keymap minibuffer-local-map)))
1169 (define-key now-map "\C-w" 'bookmark-yank-word)
1170 now-map)
1172 'bookmark-history))))
1173 (bookmark-set-name old newname)
1174 (setq bookmark-current-bookmark newname)
1175 (bookmark-bmenu-surreptitiously-rebuild-list)
1176 (setq bookmark-alist-modification-count
1177 (1+ bookmark-alist-modification-count))
1178 (if (bookmark-time-to-save-p)
1179 (bookmark-save))))
1182 ;;;###autoload
1183 (defun bookmark-insert (bookmark)
1184 "Insert the text of the file pointed to by bookmark BOOKMARK.
1185 You may have a problem using this function if the value of variable
1186 `bookmark-alist' is nil. If that happens, you need to load in some
1187 bookmarks. See help on function `bookmark-load' for more about
1188 this."
1189 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1190 (bookmark-maybe-historicize-string bookmark)
1191 (bookmark-maybe-load-default-file)
1192 (let ((orig-point (point))
1193 (str-to-insert
1194 (save-current-buffer
1195 (bookmark-handle-bookmark bookmark)
1196 (buffer-string))))
1197 (insert str-to-insert)
1198 (push-mark)
1199 (goto-char orig-point)))
1202 ;;;###autoload
1203 (defun bookmark-delete (bookmark &optional batch)
1204 "Delete BOOKMARK from the bookmark list.
1205 Removes only the first instance of a bookmark with that name. If
1206 there are one or more other bookmarks with the same name, they will
1207 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1208 one most recently used in this file, if any\).
1209 Optional second arg BATCH means don't update the bookmark list buffer,
1210 probably because we were called from there."
1211 (interactive
1212 (list (bookmark-completing-read "Delete bookmark"
1213 bookmark-current-bookmark)))
1214 (bookmark-maybe-historicize-string bookmark)
1215 (bookmark-maybe-load-default-file)
1216 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1217 (setq bookmark-alist (delq will-go bookmark-alist))
1218 ;; Added by db, nil bookmark-current-bookmark if the last
1219 ;; occurrence has been deleted
1220 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1221 (setq bookmark-current-bookmark nil)))
1222 ;; Don't rebuild the list
1223 (if batch
1225 (bookmark-bmenu-surreptitiously-rebuild-list)
1226 (setq bookmark-alist-modification-count
1227 (1+ bookmark-alist-modification-count))
1228 (if (bookmark-time-to-save-p)
1229 (bookmark-save))))
1232 (defun bookmark-time-to-save-p (&optional last-time)
1233 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
1234 ;; finds out whether it's time to save bookmarks to a file, by
1235 ;; examining the value of variable bookmark-save-flag, and maybe
1236 ;; bookmark-alist-modification-count. Returns t if they should be
1237 ;; saved, nil otherwise. if last-time is non-nil, then this is
1238 ;; being called when emacs is killed.
1239 (cond (last-time
1240 (and (> bookmark-alist-modification-count 0)
1241 bookmark-save-flag))
1242 ((numberp bookmark-save-flag)
1243 (>= bookmark-alist-modification-count bookmark-save-flag))
1245 nil)))
1248 ;;;###autoload
1249 (defun bookmark-write ()
1250 "Write bookmarks to a file (reading the file name with the minibuffer).
1251 Don't use this in Lisp programs; use `bookmark-save' instead."
1252 (interactive)
1253 (bookmark-maybe-load-default-file)
1254 (bookmark-save t))
1257 ;;;###autoload
1258 (defun bookmark-save (&optional parg file)
1259 "Save currently defined bookmarks.
1260 Saves by default in the file defined by the variable
1261 `bookmark-default-file'. With a prefix arg, save it in file FILE
1262 \(second argument\).
1264 If you are calling this from Lisp, the two arguments are PARG and
1265 FILE, and if you just want it to write to the default file, then
1266 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1267 instead. If you pass in one argument, and it is non-nil, then the
1268 user will be interactively queried for a file to save in.
1270 When you want to load in the bookmarks from a file, use
1271 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1272 for a file, defaulting to the file defined by variable
1273 `bookmark-default-file'."
1274 (interactive "P")
1275 (bookmark-maybe-load-default-file)
1276 (cond
1277 ((and (null parg) (null file))
1278 ;;whether interactive or not, write to default file
1279 (bookmark-write-file bookmark-default-file))
1280 ((and (null parg) file)
1281 ;;whether interactive or not, write to given file
1282 (bookmark-write-file file))
1283 ((and parg (not file))
1284 ;;have been called interactively w/ prefix arg
1285 (let ((file (read-file-name "File to save bookmarks in: ")))
1286 (bookmark-write-file file)))
1287 (t ; someone called us with prefix-arg *and* a file, so just write to file
1288 (bookmark-write-file file)))
1289 ;; signal that we have synced the bookmark file by setting this to
1290 ;; 0. If there was an error at any point before, it will not get
1291 ;; set, which is what we want.
1292 (setq bookmark-alist-modification-count 0))
1296 (defun bookmark-write-file (file)
1297 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1298 (with-current-buffer (get-buffer-create " *Bookmarks*")
1299 (goto-char (point-min))
1300 (delete-region (point-min) (point-max))
1301 (let ((print-length nil)
1302 (print-level nil))
1303 (bookmark-insert-file-format-version-stamp)
1304 (insert "(")
1305 ;; Rather than a single call to `pp' we make one per bookmark.
1306 ;; Apparently `pp' has a poor algorithmic complexity, so this
1307 ;; scales a lot better. bug#4485.
1308 (dolist (i bookmark-alist) (pp i (current-buffer)))
1309 (insert ")")
1310 (let ((version-control
1311 (cond
1312 ((null bookmark-version-control) nil)
1313 ((eq 'never bookmark-version-control) 'never)
1314 ((eq 'nospecial bookmark-version-control) version-control)
1315 (t t))))
1316 (condition-case nil
1317 (write-region (point-min) (point-max) file)
1318 (file-error (message "Can't write %s" file)))
1319 (kill-buffer (current-buffer))
1320 (bookmark-maybe-message
1321 "Saving bookmarks to file %s...done" file)))))
1324 (defun bookmark-import-new-list (new-list)
1325 ;; Walk over the new list, adding each individual bookmark
1326 ;; carefully. "Carefully" means checking against the existing
1327 ;; bookmark-alist and renaming the new bookmarks with <N> extensions
1328 ;; as necessary.
1329 (let ((lst new-list)
1330 (names (bookmark-all-names)))
1331 (while lst
1332 (let* ((full-record (car lst)))
1333 (bookmark-maybe-rename full-record names)
1334 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1335 (setq names (cons (bookmark-name-from-full-record full-record) names))
1336 (setq lst (cdr lst))))))
1339 (defun bookmark-maybe-rename (full-record names)
1340 ;; just a helper for bookmark-import-new-list; it is only for
1341 ;; readability that this is not inlined.
1343 ;; Once this has found a free name, it sets full-record to that
1344 ;; name.
1345 (let ((found-name (bookmark-name-from-full-record full-record)))
1346 (if (member found-name names)
1347 ;; We've got a conflict, so generate a new name
1348 (let ((count 2)
1349 (new-name found-name))
1350 (while (member new-name names)
1351 (setq new-name (concat found-name (format "<%d>" count)))
1352 (setq count (1+ count)))
1353 (bookmark-set-name full-record new-name)))))
1356 ;;;###autoload
1357 (defun bookmark-load (file &optional overwrite no-msg)
1358 "Load bookmarks from FILE (which must be in bookmark format).
1359 Appends loaded bookmarks to the front of the list of bookmarks. If
1360 optional second argument OVERWRITE is non-nil, existing bookmarks are
1361 destroyed. Optional third arg NO-MSG means don't display any messages
1362 while loading.
1364 If you load a file that doesn't contain a proper bookmark alist, you
1365 will corrupt Emacs's bookmark list. Generally, you should only load
1366 in files that were created with the bookmark functions in the first
1367 place. Your own personal bookmark file, `~/.emacs.bmk', is
1368 maintained automatically by Emacs; you shouldn't need to load it
1369 explicitly.
1371 If you load a file containing bookmarks with the same names as
1372 bookmarks already present in your Emacs, the new bookmarks will get
1373 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1374 method buffers use to resolve name collisions."
1375 (interactive
1376 (list (read-file-name
1377 (format "Load bookmarks from: (%s) "
1378 bookmark-default-file)
1379 ;;Default might not be used often,
1380 ;;but there's no better default, and
1381 ;;I guess it's better than none at all.
1382 "~/" bookmark-default-file 'confirm)))
1383 (setq file (expand-file-name file))
1384 (if (not (file-readable-p file))
1385 (error "Cannot read bookmark file %s" file)
1386 (if (null no-msg)
1387 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1388 (with-current-buffer (let ((enable-local-variables nil))
1389 (find-file-noselect file))
1390 (goto-char (point-min))
1391 (bookmark-maybe-upgrade-file-format)
1392 (let ((blist (bookmark-alist-from-buffer)))
1393 (if (listp blist)
1394 (progn
1395 (if overwrite
1396 (progn
1397 (setq bookmark-alist blist)
1398 (setq bookmark-alist-modification-count 0))
1399 ;; else
1400 (bookmark-import-new-list blist)
1401 (setq bookmark-alist-modification-count
1402 (1+ bookmark-alist-modification-count)))
1403 (if (string-equal
1404 (expand-file-name bookmark-default-file)
1405 file)
1406 (setq bookmarks-already-loaded t))
1407 (bookmark-bmenu-surreptitiously-rebuild-list))
1408 (error "Invalid bookmark list in %s" file)))
1409 (kill-buffer (current-buffer)))
1410 (if (null no-msg)
1411 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1415 ;;; Code supporting the dired-like bookmark menu.
1416 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1419 (defvar bookmark-bmenu-bookmark-column nil)
1422 (defvar bookmark-bmenu-hidden-bookmarks ())
1425 (defvar bookmark-bmenu-mode-map nil)
1428 (if bookmark-bmenu-mode-map
1430 (setq bookmark-bmenu-mode-map (make-keymap))
1431 (suppress-keymap bookmark-bmenu-mode-map t)
1432 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1433 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1434 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1435 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1436 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1437 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1438 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1439 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1440 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1441 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1442 (define-key bookmark-bmenu-mode-map "\C-o"
1443 'bookmark-bmenu-switch-other-window)
1444 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1445 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1446 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1447 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1448 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1449 (define-key bookmark-bmenu-mode-map " " 'next-line)
1450 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1451 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1452 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1453 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1454 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1455 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1456 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1457 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1458 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1459 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1460 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1461 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1462 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1463 (define-key bookmark-bmenu-mode-map [mouse-2]
1464 'bookmark-bmenu-other-window-with-mouse))
1468 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1469 ;; data.
1470 (put 'bookmark-bmenu-mode 'mode-class 'special)
1473 ;; todo: need to display whether or not bookmark exists as a buffer in
1474 ;; flag column.
1476 ;; Format:
1477 ;; FLAGS BOOKMARK [ LOCATION ]
1480 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1481 "Rebuild the Bookmark List if it exists.
1482 Don't affect the buffer ring order."
1483 (if (get-buffer "*Bookmark List*")
1484 (save-excursion
1485 (save-window-excursion
1486 (bookmark-bmenu-list)))))
1489 ;;;###autoload
1490 (defun bookmark-bmenu-list ()
1491 "Display a list of existing bookmarks.
1492 The list is displayed in a buffer named `*Bookmark List*'.
1493 The leftmost column displays a D if the bookmark is flagged for
1494 deletion, or > if it is flagged for displaying."
1495 (interactive)
1496 (bookmark-maybe-load-default-file)
1497 (if (interactive-p)
1498 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
1499 (set-buffer (get-buffer-create "*Bookmark List*")))
1500 (let ((inhibit-read-only t))
1501 (erase-buffer)
1502 (insert "% Bookmark\n- --------\n")
1503 (add-text-properties (point-min) (point)
1504 '(font-lock-face bookmark-menu-heading))
1505 (mapc
1506 (lambda (full-record)
1507 ;; if a bookmark has an annotation, prepend a "*"
1508 ;; in the list of bookmarks.
1509 (let ((annotation (bookmark-get-annotation
1510 (bookmark-name-from-full-record full-record))))
1511 (if (and annotation (not (string-equal annotation "")))
1512 (insert " *")
1513 (insert " "))
1514 (let ((start (point)))
1515 (insert (bookmark-name-from-full-record full-record))
1516 (if (and (display-color-p) (display-mouse-p))
1517 (add-text-properties
1518 start
1519 (save-excursion (re-search-backward
1520 "[^ \t]")
1521 (1+ (point)))
1522 '(mouse-face highlight
1523 follow-link t
1524 help-echo "mouse-2: go to this bookmark in other window")))
1525 (insert "\n")
1527 (bookmark-maybe-sort-alist)))
1528 (goto-char (point-min))
1529 (forward-line 2)
1530 (bookmark-bmenu-mode)
1531 (if bookmark-bmenu-toggle-filenames
1532 (bookmark-bmenu-toggle-filenames t)))
1534 ;;;###autoload
1535 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1536 ;;;###autoload
1537 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1541 (defun bookmark-bmenu-mode ()
1542 "Major mode for editing a list of bookmarks.
1543 Each line describes one of the bookmarks in Emacs.
1544 Letters do not insert themselves; instead, they are commands.
1545 Bookmark names preceded by a \"*\" have annotations.
1546 \\<bookmark-bmenu-mode-map>
1547 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1548 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1549 Also show bookmarks marked using m in other windows.
1550 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1551 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1552 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1553 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1554 together with bookmark selected before this one in another window.
1555 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1556 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1557 so the bookmark menu bookmark remains visible in its window.
1558 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1559 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1560 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1561 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1562 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1563 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1564 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1565 With a prefix arg, prompts for a file to save in.
1566 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1567 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1568 With prefix argument, also move up one line.
1569 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1570 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1571 in another buffer.
1572 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1573 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1574 (kill-all-local-variables)
1575 (use-local-map bookmark-bmenu-mode-map)
1576 (setq truncate-lines t)
1577 (setq buffer-read-only t)
1578 (setq major-mode 'bookmark-bmenu-mode)
1579 (setq mode-name "Bookmark Menu")
1580 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1583 (defun bookmark-bmenu-toggle-filenames (&optional show)
1584 "Toggle whether filenames are shown in the bookmark list.
1585 Optional argument SHOW means show them unconditionally."
1586 (interactive)
1587 (cond
1588 (show
1589 (setq bookmark-bmenu-toggle-filenames nil)
1590 (bookmark-bmenu-show-filenames)
1591 (setq bookmark-bmenu-toggle-filenames t))
1592 (bookmark-bmenu-toggle-filenames
1593 (bookmark-bmenu-hide-filenames)
1594 (setq bookmark-bmenu-toggle-filenames nil))
1596 (bookmark-bmenu-show-filenames)
1597 (setq bookmark-bmenu-toggle-filenames t))))
1600 (defun bookmark-bmenu-show-filenames (&optional force)
1601 (if (and (not force) bookmark-bmenu-toggle-filenames)
1602 nil ;already shown, so do nothing
1603 (save-excursion
1604 (save-window-excursion
1605 (goto-char (point-min))
1606 (forward-line 2)
1607 (setq bookmark-bmenu-hidden-bookmarks ())
1608 (let ((inhibit-read-only t))
1609 (while (< (point) (point-max))
1610 (let ((bmrk (bookmark-bmenu-bookmark)))
1611 (setq bookmark-bmenu-hidden-bookmarks
1612 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1613 (let ((start (save-excursion (end-of-line) (point))))
1614 (move-to-column bookmark-bmenu-file-column t)
1615 ;; Strip off `mouse-face' from the white spaces region.
1616 (if (and (display-color-p) (display-mouse-p))
1617 (remove-text-properties start (point)
1618 '(mouse-face nil help-echo nil))))
1619 (delete-region (point) (progn (end-of-line) (point)))
1620 (insert " ")
1621 ;; Pass the NO-HISTORY arg:
1622 (bookmark-insert-location bmrk t)
1623 (forward-line 1))))))))
1626 (defun bookmark-bmenu-hide-filenames (&optional force)
1627 (if (and (not force) bookmark-bmenu-toggle-filenames)
1628 ;; nothing to hide if above is nil
1629 (save-excursion
1630 (save-window-excursion
1631 (goto-char (point-min))
1632 (forward-line 2)
1633 (setq bookmark-bmenu-hidden-bookmarks
1634 (nreverse bookmark-bmenu-hidden-bookmarks))
1635 (save-excursion
1636 (goto-char (point-min))
1637 (search-forward "Bookmark")
1638 (backward-word 1)
1639 (setq bookmark-bmenu-bookmark-column (current-column)))
1640 (save-excursion
1641 (let ((inhibit-read-only t))
1642 (while bookmark-bmenu-hidden-bookmarks
1643 (move-to-column bookmark-bmenu-bookmark-column t)
1644 (bookmark-kill-line)
1645 (let ((start (point)))
1646 (insert (car bookmark-bmenu-hidden-bookmarks))
1647 (if (and (display-color-p) (display-mouse-p))
1648 (add-text-properties
1649 start
1650 (save-excursion (re-search-backward
1651 "[^ \t]")
1652 (1+ (point)))
1653 '(mouse-face highlight
1654 follow-link t
1655 help-echo
1656 "mouse-2: go to this bookmark in other window"))))
1657 (setq bookmark-bmenu-hidden-bookmarks
1658 (cdr bookmark-bmenu-hidden-bookmarks))
1659 (forward-line 1))))))))
1662 (defun bookmark-bmenu-check-position ()
1663 ;; Returns non-nil if on a line with a bookmark.
1664 ;; (The actual value returned is bookmark-alist).
1665 ;; Else reposition and try again, else return nil.
1666 (cond ((< (count-lines (point-min) (point)) 2)
1667 (goto-char (point-min))
1668 (forward-line 2)
1669 bookmark-alist)
1670 ((and (bolp) (eobp))
1671 (beginning-of-line 0)
1672 bookmark-alist)
1674 bookmark-alist)))
1677 (defun bookmark-bmenu-bookmark ()
1678 ;; return a string which is bookmark of this line.
1679 (if (bookmark-bmenu-check-position)
1680 (save-excursion
1681 (save-window-excursion
1682 (goto-char (point-min))
1683 (search-forward "Bookmark")
1684 (backward-word 1)
1685 (setq bookmark-bmenu-bookmark-column (current-column)))))
1686 (if bookmark-bmenu-toggle-filenames
1687 (bookmark-bmenu-hide-filenames))
1688 (save-excursion
1689 (save-window-excursion
1690 (beginning-of-line)
1691 (forward-char bookmark-bmenu-bookmark-column)
1692 (prog1
1693 (buffer-substring-no-properties (point)
1694 (progn
1695 (end-of-line)
1696 (point)))
1697 ;; well, this is certainly crystal-clear:
1698 (if bookmark-bmenu-toggle-filenames
1699 (bookmark-bmenu-toggle-filenames t))))))
1702 (defun bookmark-show-annotation (bookmark)
1703 "Display the annotation for bookmark named BOOKMARK in a buffer,
1704 if an annotation exists."
1705 (let ((annotation (bookmark-get-annotation bookmark)))
1706 (if (and annotation (not (string-equal annotation "")))
1707 (save-excursion
1708 (let ((old-buf (current-buffer)))
1709 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1710 (delete-region (point-min) (point-max))
1711 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1712 (insert annotation)
1713 (goto-char (point-min))
1714 (pop-to-buffer old-buf))))))
1717 (defun bookmark-show-all-annotations ()
1718 "Display the annotations for all bookmarks in a buffer."
1719 (let ((old-buf (current-buffer)))
1720 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1721 (delete-region (point-min) (point-max))
1722 (mapc
1723 (lambda (full-record)
1724 (let* ((name (bookmark-name-from-full-record full-record))
1725 (ann (bookmark-get-annotation name)))
1726 (insert (concat name ":\n"))
1727 (if (and ann (not (string-equal ann "")))
1728 ;; insert the annotation, indented by 4 spaces.
1729 (progn
1730 (save-excursion (insert ann) (unless (bolp)
1731 (insert "\n")))
1732 (while (< (point) (point-max))
1733 (beginning-of-line) ; paranoia
1734 (insert " ")
1735 (forward-line)
1736 (end-of-line))))))
1737 bookmark-alist)
1738 (goto-char (point-min))
1739 (pop-to-buffer old-buf)))
1742 (defun bookmark-bmenu-mark ()
1743 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1744 (interactive)
1745 (beginning-of-line)
1746 (if (bookmark-bmenu-check-position)
1747 (let ((inhibit-read-only t))
1748 (delete-char 1)
1749 (insert ?>)
1750 (forward-line 1)
1751 (bookmark-bmenu-check-position))))
1754 (defun bookmark-bmenu-select ()
1755 "Select this line's bookmark; also display bookmarks marked with `>'.
1756 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1757 (interactive)
1758 (if (bookmark-bmenu-check-position)
1759 (let ((bmrk (bookmark-bmenu-bookmark))
1760 (menu (current-buffer))
1761 (others ())
1762 tem)
1763 (goto-char (point-min))
1764 (while (re-search-forward "^>" nil t)
1765 (setq tem (bookmark-bmenu-bookmark))
1766 (let ((inhibit-read-only t))
1767 (delete-char -1)
1768 (insert ?\s))
1769 (or (string-equal tem bmrk)
1770 (member tem others)
1771 (setq others (cons tem others))))
1772 (setq others (nreverse others)
1773 tem (/ (1- (frame-height)) (1+ (length others))))
1774 (delete-other-windows)
1775 (bookmark-jump bmrk)
1776 (bury-buffer menu)
1777 (if others
1778 (while others
1779 (split-window nil tem)
1780 (other-window 1)
1781 (bookmark-jump (car others))
1782 (setq others (cdr others)))
1783 (other-window 1)))))
1786 (defun bookmark-bmenu-save (parg)
1787 "Save the current list into a bookmark file.
1788 With a prefix arg, prompts for a file to save them in."
1789 (interactive "P")
1790 (save-excursion
1791 (save-window-excursion
1792 (bookmark-save parg))))
1795 (defun bookmark-bmenu-load ()
1796 "Load the bookmark file and rebuild the bookmark menu-buffer."
1797 (interactive)
1798 (if (bookmark-bmenu-check-position)
1799 (save-excursion
1800 (save-window-excursion
1801 ;; This will call `bookmark-bmenu-list'
1802 (call-interactively 'bookmark-load)))))
1805 (defun bookmark-bmenu-1-window ()
1806 "Select this line's bookmark, alone, in full frame."
1807 (interactive)
1808 (if (bookmark-bmenu-check-position)
1809 (progn
1810 (bookmark-jump (bookmark-bmenu-bookmark))
1811 (bury-buffer (other-buffer))
1812 (delete-other-windows))))
1815 (defun bookmark-bmenu-2-window ()
1816 "Select this line's bookmark, with previous buffer in second window."
1817 (interactive)
1818 (if (bookmark-bmenu-check-position)
1819 (let ((bmrk (bookmark-bmenu-bookmark))
1820 (menu (current-buffer))
1821 (pop-up-windows t))
1822 (delete-other-windows)
1823 (switch-to-buffer (other-buffer))
1824 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1825 (bookmark--jump-via bmrk 'pop-to-buffer))
1826 (bury-buffer menu))))
1829 (defun bookmark-bmenu-this-window ()
1830 "Select this line's bookmark in this window."
1831 (interactive)
1832 (if (bookmark-bmenu-check-position)
1833 (bookmark-jump (bookmark-bmenu-bookmark))))
1836 (defun bookmark-bmenu-other-window ()
1837 "Select this line's bookmark in other window, leaving bookmark menu visible."
1838 (interactive)
1839 (let ((bookmark (bookmark-bmenu-bookmark)))
1840 (if (bookmark-bmenu-check-position)
1841 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1842 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1845 (defun bookmark-bmenu-switch-other-window ()
1846 "Make the other window select this line's bookmark.
1847 The current window remains selected."
1848 (interactive)
1849 (let ((bookmark (bookmark-bmenu-bookmark))
1850 (pop-up-windows t)
1851 same-window-buffer-names
1852 same-window-regexps)
1853 (if (bookmark-bmenu-check-position)
1854 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1855 (bookmark--jump-via bookmark 'display-buffer)))))
1857 (defun bookmark-bmenu-other-window-with-mouse (event)
1858 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1859 (interactive "e")
1860 (with-current-buffer (window-buffer (posn-window (event-end event)))
1861 (save-excursion
1862 (goto-char (posn-point (event-end event)))
1863 (bookmark-bmenu-other-window))))
1866 (defun bookmark-bmenu-show-annotation ()
1867 "Show 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-show-annotation bookmark))))
1874 (defun bookmark-bmenu-show-all-annotations ()
1875 "Show the annotation for all bookmarks in another window."
1876 (interactive)
1877 (bookmark-show-all-annotations))
1880 (defun bookmark-bmenu-edit-annotation ()
1881 "Edit the annotation for the current bookmark in another window."
1882 (interactive)
1883 (let ((bookmark (bookmark-bmenu-bookmark)))
1884 (if (bookmark-bmenu-check-position)
1885 (bookmark-edit-annotation bookmark))))
1888 (defun bookmark-bmenu-unmark (&optional backup)
1889 "Cancel all requested operations on bookmark on this line and move down.
1890 Optional BACKUP means move up."
1891 (interactive "P")
1892 (beginning-of-line)
1893 (if (bookmark-bmenu-check-position)
1894 (progn
1895 (let ((inhibit-read-only t))
1896 (delete-char 1)
1897 ;; any flags to reset according to circumstances? How about a
1898 ;; flag indicating whether this bookmark is being visited?
1899 ;; well, we don't have this now, so maybe later.
1900 (insert " "))
1901 (forward-line (if backup -1 1))
1902 (bookmark-bmenu-check-position))))
1905 (defun bookmark-bmenu-backup-unmark ()
1906 "Move up and cancel all requested operations on bookmark on line above."
1907 (interactive)
1908 (forward-line -1)
1909 (if (bookmark-bmenu-check-position)
1910 (progn
1911 (bookmark-bmenu-unmark)
1912 (forward-line -1)
1913 (bookmark-bmenu-check-position))))
1916 (defun bookmark-bmenu-delete ()
1917 "Mark bookmark on this line to be deleted.
1918 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1919 (interactive)
1920 (beginning-of-line)
1921 (if (bookmark-bmenu-check-position)
1922 (let ((inhibit-read-only t))
1923 (delete-char 1)
1924 (insert ?D)
1925 (forward-line 1)
1926 (bookmark-bmenu-check-position))))
1929 (defun bookmark-bmenu-delete-backwards ()
1930 "Mark bookmark on this line to be deleted, then move up one line.
1931 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1932 (interactive)
1933 (bookmark-bmenu-delete)
1934 (forward-line -2)
1935 (if (bookmark-bmenu-check-position)
1936 (forward-line 1))
1937 (bookmark-bmenu-check-position))
1940 (defun bookmark-bmenu-execute-deletions ()
1941 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1942 (interactive)
1943 (message "Deleting bookmarks...")
1944 (let ((hide-em bookmark-bmenu-toggle-filenames)
1945 (o-point (point))
1946 (o-str (save-excursion
1947 (beginning-of-line)
1948 (if (looking-at "^D")
1950 (buffer-substring
1951 (point)
1952 (progn (end-of-line) (point))))))
1953 (o-col (current-column)))
1954 (if hide-em (bookmark-bmenu-hide-filenames))
1955 (setq bookmark-bmenu-toggle-filenames nil)
1956 (goto-char (point-min))
1957 (forward-line 1)
1958 (while (re-search-forward "^D" (point-max) t)
1959 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1960 (bookmark-bmenu-list)
1961 (setq bookmark-bmenu-toggle-filenames hide-em)
1962 (if bookmark-bmenu-toggle-filenames
1963 (bookmark-bmenu-toggle-filenames t))
1964 (if o-str
1965 (progn
1966 (goto-char (point-min))
1967 (search-forward o-str)
1968 (beginning-of-line)
1969 (forward-char o-col))
1970 (goto-char o-point))
1971 (beginning-of-line)
1972 (setq bookmark-alist-modification-count
1973 (1+ bookmark-alist-modification-count))
1974 (if (bookmark-time-to-save-p)
1975 (bookmark-save))
1976 (message "Deleting bookmarks...done")
1980 (defun bookmark-bmenu-rename ()
1981 "Rename bookmark on current line. Prompts for a new name."
1982 (interactive)
1983 (if (bookmark-bmenu-check-position)
1984 (let ((bmrk (bookmark-bmenu-bookmark))
1985 (thispoint (point)))
1986 (bookmark-rename bmrk)
1987 (bookmark-bmenu-list)
1988 (goto-char thispoint))))
1991 (defun bookmark-bmenu-locate ()
1992 "Display location of this bookmark. Displays in the minibuffer."
1993 (interactive)
1994 (if (bookmark-bmenu-check-position)
1995 (let ((bmrk (bookmark-bmenu-bookmark)))
1996 (message "%s" (bookmark-location bmrk)))))
1998 (defun bookmark-bmenu-relocate ()
1999 "Change the file path of the bookmark on the current line,
2000 prompting with completion for the new path."
2001 (interactive)
2002 (if (bookmark-bmenu-check-position)
2003 (let ((bmrk (bookmark-bmenu-bookmark))
2004 (thispoint (point)))
2005 (bookmark-relocate bmrk)
2006 (goto-char thispoint))))
2009 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2011 (defun bookmark-menu-popup-paned-menu (event name entries)
2012 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2013 That is, ENTRIES is a list of strings which appear as the choices
2014 in the menu.
2015 The number of panes depends on the number of entries.
2016 The visible entries are truncated to `bookmark-menu-length', but the
2017 strings returned are not."
2018 (let ((f-height (/ (frame-height) 2))
2019 (pane-list nil)
2020 (iter 0))
2021 (while entries
2022 (let (lst
2023 (count 0))
2024 (while (and (< count f-height) entries)
2025 (let ((str (car entries)))
2026 (push (cons
2027 (if (> (length str) bookmark-menu-length)
2028 (substring str 0 bookmark-menu-length)
2029 str)
2030 str)
2031 lst)
2032 (setq entries (cdr entries))
2033 (setq count (1+ count))))
2034 (setq iter (1+ iter))
2035 (push (cons
2036 (format "-*- %s (%d) -*-" name iter)
2037 (nreverse lst))
2038 pane-list)))
2040 ;; Popup the menu and return the string.
2041 (x-popup-menu event (cons (concat "-*- " name " -*-")
2042 (nreverse pane-list)))))
2045 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2046 ;; following works, and for explaining what to do to make it work.
2048 ;; We MUST autoload EACH form used to set up this variable's value, so
2049 ;; that the whole job is done in loaddefs.el.
2051 ;; Emacs menubar stuff.
2053 ;;;###autoload
2054 (defvar menu-bar-bookmark-map
2055 (let ((map (make-sparse-keymap "Bookmark functions")))
2056 (define-key map [load] '("Load a Bookmark File..." . bookmark-load))
2057 (define-key map [write] '("Save Bookmarks As..." . bookmark-write))
2058 (define-key map [save] '("Save Bookmarks" . bookmark-save))
2059 (define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
2060 (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
2061 (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
2062 (define-key map [locate] '("Insert Location..." . bookmark-locate))
2063 (define-key map [insert] '("Insert Contents..." . bookmark-insert))
2064 (define-key map [set] '("Set Bookmark..." . bookmark-set))
2065 (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
2066 map))
2068 ;;;###autoload
2069 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2071 ;; make bookmarks appear toward the right side of the menu.
2072 (if (boundp 'menu-bar-final-items)
2073 (if menu-bar-final-items
2074 (setq menu-bar-final-items
2075 (cons 'bookmark menu-bar-final-items)))
2076 (setq menu-bar-final-items '(bookmark)))
2078 ;;;; end bookmark menu stuff ;;;;
2081 ;; Load Hook
2082 (defvar bookmark-load-hook nil
2083 "Hook run at the end of loading bookmark.")
2085 ;; Exit Hook, called from kill-emacs-hook
2086 (defvar bookmark-exit-hook nil
2087 "Hook run when Emacs exits.")
2089 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2091 (defun bookmark-exit-hook-internal ()
2092 "Save bookmark state, if necessary, at Emacs exit time.
2093 This also runs `bookmark-exit-hook'."
2094 (run-hooks 'bookmark-exit-hook)
2095 (and bookmark-alist
2096 (bookmark-time-to-save-p t)
2097 (bookmark-save)))
2099 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2101 (defun bookmark-unload-function ()
2102 "Unload the Bookmark library."
2103 (when bookmark-save-flag (bookmark-save))
2104 ;; continue standard unloading
2105 nil)
2108 (run-hooks 'bookmark-load-hook)
2110 (provide 'bookmark)
2112 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2113 ;;; bookmark.el ends here