1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Karl Fogel <kfogel@red-bean.com>
8 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
10 ;; Keywords: bookmarks, placeholders, annotations
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This package is for setting "bookmarks" in files. A bookmark
30 ;; associates a string with a location in a certain file. Thus, you
31 ;; can navigate your way to that location by providing the string.
32 ;; See the "User Variables" section for customizations.
38 (eval-when-compile (require 'cl
))
42 ;; If variable bookmark-use-annotations is non-nil, an annotation is
43 ;; queried for when setting a bookmark.
45 ;; The bookmark list is sorted lexically by default, but you can turn
46 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
47 ;; the list will be presented in the order it is recorded
48 ;; (chronologically), which is actually fairly useful as well.
52 (defgroup bookmark nil
53 "Setting, annotation and jumping to bookmarks."
57 (defcustom bookmark-use-annotations nil
58 "If non-nil, saving a bookmark queries for an annotation in a buffer."
63 (defcustom bookmark-save-flag t
64 "Controls when Emacs saves bookmarks to a file.
65 --> nil means never save bookmarks, except when `bookmark-save' is
66 explicitly called (\\[bookmark-save]).
67 --> t means save bookmarks when Emacs is killed.
68 --> Otherwise, it should be a number that is the frequency with which
69 the bookmark list is saved (i.e.: the number of times which
70 Emacs' bookmark list may be modified before it is automatically
71 saved.). If it is a number, Emacs will also automatically save
72 bookmarks when it is killed.
74 Therefore, the way to get it to save every time you make or delete a
75 bookmark is to set this variable to 1 (or 0, which produces the same
78 To specify the file in which to save them, modify the variable
79 `bookmark-default-file', which is `~/.emacs.bmk' by default."
80 :type
'(choice (const nil
) integer
(other t
))
84 (defconst bookmark-old-default-file
"~/.emacs-bkmrks"
85 "The `.emacs.bmk' file used to be called this name.")
88 ;; defvarred to avoid a compilation warning:
89 (defvar bookmark-file nil
90 "Old name for `bookmark-default-file'.")
92 (defcustom bookmark-default-file
94 ;; In case user set `bookmark-file' in her .emacs:
96 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
97 "File in which to save bookmarks by default."
102 (defcustom bookmark-version-control
'nospecial
103 "Whether or not to make numbered backups of the bookmark file.
104 It can have four values: t, nil, `never', and `nospecial'.
105 The first three have the same meaning that they do for the
106 variable `version-control', and the final value `nospecial' means just
107 use the value of `version-control'."
108 :type
'(choice (const nil
) (const never
) (const nospecial
)
113 (defcustom bookmark-completion-ignore-case t
114 "Non-nil means bookmark functions ignore case in completion."
119 (defcustom bookmark-sort-flag t
120 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
121 Otherwise they will be displayed in LIFO order (that is, most
122 recently set ones come first, oldest ones come last)."
127 (defcustom bookmark-automatically-show-annotations t
128 "Non-nil means show annotations when jumping to a bookmark."
133 (defconst bookmark-bmenu-header-height
2
134 "Number of lines used for the *Bookmark List* header.")
136 (defconst bookmark-bmenu-marks-width
2
137 "Number of columns (chars) used for the *Bookmark List* marks column,
138 including the annotations column.")
140 (defcustom bookmark-bmenu-file-column
30
141 "Column at which to display filenames in a buffer listing bookmarks.
142 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
147 (defcustom bookmark-bmenu-toggle-filenames t
148 "Non-nil means show filenames when listing bookmarks.
149 This may result in truncated bookmark names. To disable this, put the
150 following in your `.emacs' file:
152 \(setq bookmark-bmenu-toggle-filenames nil)"
157 (defcustom bookmark-menu-length
70
158 "Maximum length of a bookmark name displayed on a popup menu."
162 ;; FIXME: Is it really worth a customization option?
163 (defcustom bookmark-search-delay
0.2
164 "Time before `bookmark-bmenu-search' updates the display."
168 (defface bookmark-menu-heading
169 '((t (:inherit font-lock-type-face
)))
170 "Face used to highlight the heading in bookmark menu buffers."
175 ;;; No user-serviceable parts beyond this point.
177 ;; Added for lucid emacs compatibility, db
178 (or (fboundp 'defalias
) (fset 'defalias
'fset
))
180 ;; suggested for lucid compatibility by david hughes:
181 (or (fboundp 'frame-height
) (defalias 'frame-height
'screen-height
))
186 ;; Set up these bindings dumping time *only*;
187 ;; if the user alters them, don't override the user when loading bookmark.el.
189 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
190 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
191 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
195 (let ((map (make-sparse-keymap)))
196 ;; Read the help on all of these functions for details...
197 (define-key map
"x" 'bookmark-set
)
198 (define-key map
"m" 'bookmark-set
) ;"m"ark
199 (define-key map
"j" 'bookmark-jump
)
200 (define-key map
"g" 'bookmark-jump
) ;"g"o
201 (define-key map
"o" 'bookmark-jump-other-window
)
202 (define-key map
"i" 'bookmark-insert
)
203 (define-key map
"e" 'edit-bookmarks
)
204 (define-key map
"f" 'bookmark-insert-location
) ;"f"ind
205 (define-key map
"r" 'bookmark-rename
)
206 (define-key map
"d" 'bookmark-delete
)
207 (define-key map
"l" 'bookmark-load
)
208 (define-key map
"w" 'bookmark-write
)
209 (define-key map
"s" 'bookmark-save
)
211 "Keymap containing bindings to bookmark functions.
212 It is not bound to any key by default: to bind it
213 so that you have a bookmark prefix, just use `global-set-key' and bind a
214 key of your choice to `bookmark-map'. All interactive bookmark
215 functions have a binding in this keymap.")
217 ;;;###autoload (fset 'bookmark-map bookmark-map)
220 ;;; Core variables and data structures:
221 (defvar bookmark-alist
()
222 "Association list of bookmarks and their records.
223 Bookmark functions update the value automatically.
224 You probably do NOT want to change the value yourself.
226 The value is an alist with entries of the form
228 (BOOKMARK-NAME . PARAM-ALIST)
230 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
232 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
234 PARAM-ALIST is an alist of bookmark information. The order of the
235 entries in PARAM-ALIST is not important. The possible entries are
236 described below. An entry with a key but null value means the entry
239 (filename . FILENAME)
241 (front-context-string . STR-AFTER-POS)
242 (rear-context-string . STR-BEFORE-POS)
244 (annotation . ANNOTATION)
246 FILENAME names the bookmarked file.
247 POS is the bookmarked buffer position (position in the file).
248 STR-AFTER-POS is buffer text that immediately follows POS.
249 STR-BEFORE-POS is buffer text that immediately precedes POS.
250 ANNOTATION is a string that describes the bookmark.
251 See options `bookmark-use-annotations' and
252 `bookmark-automatically-show-annotations'.
253 HANDLER is a function that provides the bookmark-jump behavior for a
254 specific kind of bookmark. This is the case for Info bookmarks,
255 for instance. HANDLER must accept a bookmark as argument.")
257 (defvar bookmarks-already-loaded nil
258 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
261 ;; more stuff added by db.
263 (defvar bookmark-current-bookmark nil
264 "Name of bookmark most recently used in the current file.
265 It is buffer local, used to make moving a bookmark forward
266 through a file easier.")
268 (make-variable-buffer-local 'bookmark-current-bookmark
)
271 (defvar bookmark-alist-modification-count
0
272 "Number of modifications to bookmark list since it was last saved.")
275 (defvar bookmark-search-size
16
276 "Length of the context strings recorded on either side of a bookmark.")
279 (defvar bookmark-current-buffer nil
280 "The buffer in which a bookmark is currently being set or renamed.
281 Functions that insert strings into the minibuffer use this to know
282 the source buffer for that information; see `bookmark-yank-word' and
283 `bookmark-insert-current-bookmark' for example.")
286 (defvar bookmark-yank-point
0
287 "The next point from which to pull source text for `bookmark-yank-word'.
288 This point is in `bookmark-current-buffer'.")
291 (defvar bookmark-quit-flag nil
292 "Non nil make `bookmark-bmenu-search' quit immediately.")
294 ;; Helper functions and macros.
296 (defmacro with-buffer-modified-unmodified
(&rest body
)
297 "Run BODY while preserving the buffer's `buffer-modified-p' state."
298 (let ((was-modified (make-symbol "was-modified")))
299 `(let ((,was-modified
(buffer-modified-p)))
302 (set-buffer-modified-p ,was-modified
)))))
304 ;; Only functions below, in this page and the next one (file formats),
305 ;; need to know anything about the format of bookmark-alist entries.
306 ;; Everyone else should go through them.
308 (defun bookmark-name-from-full-record (full-record)
309 "Return name of FULL-RECORD (an alist element instead of a string)."
313 (defun bookmark-all-names ()
314 "Return a list of all current bookmark names."
315 (bookmark-maybe-load-default-file)
316 (mapcar 'bookmark-name-from-full-record bookmark-alist
))
319 (defun bookmark-get-bookmark (bookmark &optional noerror
)
320 "Return the bookmark record corresponding to BOOKMARK.
321 If BOOKMARK is a string, look for the corresponding bookmark record in
322 `bookmark-alist'; return it if found, otherwise error. Else if
323 BOOKMARK is already a bookmark record, just return it."
325 ((consp bookmark
) bookmark
)
327 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case
)
328 (unless noerror
(error "Invalid bookmark %s" bookmark
))))))
331 (defun bookmark-get-bookmark-record (bookmark)
332 "Return the record portion of the entry for BOOKMARK in
333 `bookmark-alist' (that is, all information but the name).
334 BOOKMARK may be a bookmark name (a string) or a bookmark record."
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
)))
342 (defun bookmark-set-name (bookmark newname
)
343 "Set BOOKMARK's name to NEWNAME.
344 BOOKMARK may be a bookmark name (a string) or a bookmark record."
346 (if (stringp bookmark
) (bookmark-get-bookmark bookmark
) bookmark
)
349 (defun bookmark-prop-get (bookmark prop
)
350 "Return the property PROP of BOOKMARK, or nil if none.
351 BOOKMARK may be a bookmark name (a string) or a bookmark record."
352 (cdr (assq prop
(bookmark-get-bookmark-record bookmark
))))
354 (defun bookmark-prop-set (bookmark prop val
)
355 "Set the property PROP of BOOKMARK to VAL.
356 BOOKMARK may be a bookmark name (a string) or a bookmark record."
357 (let ((cell (assq prop
(bookmark-get-bookmark-record bookmark
))))
360 (nconc (bookmark-get-bookmark-record bookmark
)
361 (list (cons prop val
))))))
363 (defun bookmark-get-annotation (bookmark)
364 "Return the annotation of BOOKMARK, or nil if none.
365 BOOKMARK may be a bookmark name (a string) or a bookmark record."
366 (bookmark-prop-get bookmark
'annotation
))
368 (defun bookmark-set-annotation (bookmark ann
)
369 "Set the annotation of BOOKMARK to ANN.
370 BOOKMARK may be a bookmark name (a string) or a bookmark record."
371 (bookmark-prop-set bookmark
'annotation ann
))
374 (defun bookmark-get-filename (bookmark)
375 "Return the full filename of BOOKMARK, or nil if none.
376 BOOKMARK may be a bookmark name (a string) or a bookmark record."
377 (bookmark-prop-get bookmark
'filename
))
380 (defun bookmark-set-filename (bookmark filename
)
381 "Set the full filename of BOOKMARK to FILENAME.
382 BOOKMARK may be a bookmark name (a string) or a bookmark record."
383 (bookmark-prop-set bookmark
'filename filename
))
386 (defun bookmark-get-position (bookmark)
387 "Return the position (i.e.: point) of BOOKMARK, or nil if none.
388 BOOKMARK may be a bookmark name (a string) or a bookmark record."
389 (bookmark-prop-get bookmark
'position
))
392 (defun bookmark-set-position (bookmark position
)
393 "Set the position (i.e.: point) of BOOKMARK to POSITION.
394 BOOKMARK may be a bookmark name (a string) or a bookmark record."
395 (bookmark-prop-set bookmark
'position position
))
398 (defun bookmark-get-front-context-string (bookmark)
399 "Return the front-context-string of BOOKMARK, or nil if none.
400 BOOKMARK may be a bookmark name (a string) or a bookmark record."
401 (bookmark-prop-get bookmark
'front-context-string
))
404 (defun bookmark-set-front-context-string (bookmark string
)
405 "Set the front-context-string of BOOKMARK to STRING.
406 BOOKMARK may be a bookmark name (a string) or a bookmark record."
407 (bookmark-prop-set bookmark
'front-context-string string
))
410 (defun bookmark-get-rear-context-string (bookmark)
411 "Return the rear-context-string of BOOKMARK, or nil if none.
412 BOOKMARK may be a bookmark name (a string) or a bookmark record."
413 (bookmark-prop-get bookmark
'rear-context-string
))
416 (defun bookmark-set-rear-context-string (bookmark string
)
417 "Set the rear-context-string of BOOKMARK to STRING.
418 BOOKMARK may be a bookmark name (a string) or a bookmark record."
419 (bookmark-prop-set bookmark
'rear-context-string string
))
422 (defun bookmark-get-handler (bookmark)
423 "Return the handler function for BOOKMARK, or nil if none.
424 BOOKMARK may be a bookmark name (a string) or a bookmark record."
425 (bookmark-prop-get bookmark
'handler
))
427 (defvar bookmark-history nil
428 "The history list for bookmark functions.")
431 (defun bookmark-completing-read (prompt &optional default
)
432 "Prompting with PROMPT, read a bookmark name in completion.
433 PROMPT will get a \": \" stuck on the end no matter what, so you
434 probably don't want to include one yourself.
435 Optional second arg DEFAULT is a string to return if the user enters
437 (bookmark-maybe-load-default-file) ; paranoia
438 (if (listp last-nonmenu-event
)
439 (bookmark-menu-popup-paned-menu t prompt
440 (if bookmark-sort-flag
441 (sort (bookmark-all-names)
443 (bookmark-all-names)))
444 (let* ((completion-ignore-case bookmark-completion-ignore-case
)
446 (prompt (concat prompt
(if default
447 (format " (%s): " default
)
450 (completing-read prompt
456 (if (string-equal "" str
) default str
))))
459 (defmacro bookmark-maybe-historicize-string
(string)
460 "Put STRING into the bookmark prompt history, if caller non-interactive.
461 We need this because sometimes bookmark functions are invoked from
462 menus, so `completing-read' never gets a chance to set `bookmark-history'."
464 (called-interactively-p 'interactive
)
465 (setq bookmark-history
(cons ,string bookmark-history
))))
467 (defvar bookmark-make-record-function
'bookmark-make-record-default
468 "A function that should be called to create a bookmark record.
469 Modes may set this variable buffer-locally to enable bookmarking of
470 locations that should be treated specially, such as Info nodes,
471 news posts, images, pdf documents, etc.
473 The function will be called with no arguments.
474 It should signal a user error if it is unable to construct a record for
475 the current location.
477 The returned record should be a cons cell of the form (NAME . ALIST)
478 where ALIST is as described in `bookmark-alist' and may typically contain
479 a special cons (handler . HANDLER-FUNC) which specifies the handler function
480 that should be used instead of `bookmark-default-handler' to open this
481 bookmark. See the documentation for `bookmark-alist' for more.
483 NAME is a suggested name for the constructed bookmark. It can be nil
484 in which case a default heuristic will be used. The function can also
485 equivalently just return ALIST without NAME.")
487 (defun bookmark-make-record ()
488 "Return a new bookmark record (NAME . ALIST) for the current location."
489 (let ((record (funcall bookmark-make-record-function
)))
490 ;; Set up default name.
491 (if (stringp (car record
))
492 ;; The function already provided a default name.
494 (if (car record
) (push nil record
))
495 (setcar record
(or bookmark-current-bookmark
(bookmark-buffer-name)))
498 (defun bookmark-store (name alist no-overwrite
)
499 "Store the bookmark NAME with data ALIST.
500 If NO-OVERWRITE is non-nil and another bookmark of the same name already
501 exists in `bookmark-alist', record the new bookmark without throwing away the
503 (bookmark-maybe-load-default-file)
504 (let ((stripped-name (copy-sequence name
)))
505 (or (featurep 'xemacs
)
506 ;; XEmacs's `set-text-properties' doesn't work on
507 ;; free-standing strings, apparently.
508 (set-text-properties 0 (length stripped-name
) nil stripped-name
))
509 (if (and (not no-overwrite
)
510 (bookmark-get-bookmark stripped-name
'noerror
))
511 ;; already existing bookmark under that name and
512 ;; no prefix arg means just overwrite old bookmark
513 ;; Use the new (NAME . ALIST) format.
514 (setcdr (bookmark-get-bookmark stripped-name
) alist
)
516 ;; otherwise just cons it onto the front (either the bookmark
517 ;; doesn't exist already, or there is no prefix arg. In either
518 ;; case, we want the new bookmark consed onto the alist...)
520 (push (cons stripped-name alist
) bookmark-alist
))
523 (setq bookmark-current-bookmark stripped-name
)
524 (setq bookmark-alist-modification-count
525 (1+ bookmark-alist-modification-count
))
526 (if (bookmark-time-to-save-p)
529 (setq bookmark-current-bookmark stripped-name
)
530 (bookmark-bmenu-surreptitiously-rebuild-list)))
532 (defun bookmark-make-record-default (&optional no-file no-context posn
)
533 "Return the record describing the location of a new bookmark.
534 Point should be at the buffer in which the bookmark is being set,
535 and normally should be at the position where the bookmark is desired,
536 but see the optional arguments for other possibilities.
538 If NO-FILE is non-nil, then only return the subset of the
539 record that pertains to the location within the buffer, leaving off
540 the part that records the filename.
542 If NO-CONTEXT is non-nil, do not include the front- and rear-context
543 strings in the record -- the position is enough.
545 If POSN is non-nil, record POSN as the point instead of `(point)'."
546 `(,@(unless no-file
`((filename .
,(bookmark-buffer-file-name))))
547 ,@(unless no-context
`((front-context-string
548 .
,(if (>= (- (point-max) (point))
549 bookmark-search-size
)
550 (buffer-substring-no-properties
552 (+ (point) bookmark-search-size
))
554 ,@(unless no-context
`((rear-context-string
555 .
,(if (>= (- (point) (point-min))
556 bookmark-search-size
)
557 (buffer-substring-no-properties
559 (- (point) bookmark-search-size
))
561 (position .
,(or posn
(point)))))
564 ;;; File format stuff
566 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
567 ;; the bookmark file format -- please don't. The current format
568 ;; should be extensible enough. If you feel the need to change it,
569 ;; please discuss it with other Emacs developers first.
571 ;; The format of `bookmark-alist' has changed twice in its lifetime.
572 ;; This comment describes the three formats, FIRST, SECOND, and
575 ;; The FIRST format was used prior to Emacs 20:
577 ;; ((BOOKMARK-NAME (FILENAME
583 ;; The SECOND format was introduced in Emacs 20:
585 ;; ((BOOKMARK-NAME ((filename . FILENAME)
587 ;; (front-context-string . STR-AFTER-POS)
588 ;; (rear-context-string . STR-BEFORE-POS)
589 ;; (annotation . ANNOTATION)
590 ;; (whatever . VALUE)
595 ;; The CURRENT format was introduced in Emacs 22:
597 ;; ((BOOKMARK-NAME (filename . FILENAME)
599 ;; (front-context-string . STR-AFTER-POS)
600 ;; (rear-context-string . STR-BEFORE-POS)
601 ;; (annotation . ANNOTATION)
602 ;; (whatever . VALUE)
607 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
608 ;; bookmark record is a list of entry information. FIRST and SECOND
609 ;; differ in the form of the record information: FIRST uses a list of
610 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
611 ;; the list elements matters. In the SECOND format, the order of the
612 ;; alist elements is unimportant. The SECOND format facilitates the
613 ;; addition of new kinds of elements, to support new kinds of
614 ;; bookmarks or code evolution.
616 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
617 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
618 ;; no longer a cons. Why that change was made remains a mystery --
619 ;; just be aware of it. (Be aware too that this explanatory comment
620 ;; was incorrect in Emacs 22 and Emacs 23.1.)
622 ;; To deal with the change from FIRST format to SECOND, conversion
623 ;; code was added, and it is still in use. See
624 ;; `bookmark-maybe-upgrade-file-format'.
626 ;; No conversion from SECOND to CURRENT is done. Instead, the code
627 ;; handles both formats OK. It must continue to do so.
629 ;; See the doc string of `bookmark-alist' for information about the
630 ;; elements that define a bookmark (e.g. `filename').
633 (defconst bookmark-file-format-version
1
634 "The current version of the format used by bookmark files.
635 You should never need to change this.")
638 (defconst bookmark-end-of-version-stamp-marker
639 "-*- End Of Bookmark File Format Version Stamp -*-\n"
640 "This string marks the end of the version stamp in a bookmark file.")
643 (defun bookmark-alist-from-buffer ()
644 "Return a `bookmark-alist' (in any format) from the current buffer.
645 The buffer must of course contain bookmark format information.
646 Does not care from where in the buffer it is called, and does not
649 (goto-char (point-min))
650 (if (search-forward bookmark-end-of-version-stamp-marker nil t
)
651 (read (current-buffer))
652 ;; Else we're dealing with format version 0
653 (if (search-forward "(" nil t
)
656 (read (current-buffer)))
657 ;; Else no hope of getting information here.
658 (error "Not bookmark format")))))
661 (defun bookmark-upgrade-version-0-alist (old-list)
662 "Upgrade a version 0 alist OLD-LIST to the current version."
665 (let* ((name (car bookmark
))
666 (record (car (cdr bookmark
)))
667 (filename (nth 0 record
))
668 (front-str (nth 1 record
))
669 (rear-str (nth 2 record
))
670 (position (nth 3 record
))
671 (ann (nth 4 record
)))
674 `((filename .
,filename
)
675 (front-context-string .
,(or front-str
""))
676 (rear-context-string .
,(or rear-str
""))
677 (position .
,position
)
678 (annotation .
,ann
)))))
682 (defun bookmark-upgrade-file-format-from-0 ()
683 "Upgrade a bookmark file of format 0 (the original format) to format 1.
684 This expects to be called from `point-min' in a bookmark file."
685 (message "Upgrading bookmark format from 0 to %d..."
686 bookmark-file-format-version
)
687 (let* ((old-list (bookmark-alist-from-buffer))
688 (new-list (bookmark-upgrade-version-0-alist old-list
)))
689 (delete-region (point-min) (point-max))
690 (bookmark-insert-file-format-version-stamp)
691 (pp new-list
(current-buffer))
693 (goto-char (point-min))
694 (message "Upgrading bookmark format from 0 to %d...done"
695 bookmark-file-format-version
)
699 (defun bookmark-grok-file-format-version ()
700 "Return an integer which is the file-format version of this bookmark file.
701 This expects to be called from `point-min' in a bookmark file."
702 (if (looking-at "^;;;;")
705 (re-search-forward "[0-9]")
707 (read (current-buffer))))
708 ;; Else this is format version 0, the original one, which didn't
709 ;; even have version stamps.
713 (defun bookmark-maybe-upgrade-file-format ()
714 "Check the file-format version of this bookmark file.
715 If the version is not up-to-date, upgrade it automatically.
716 This expects to be called from `point-min' in a bookmark file."
717 (let ((version (bookmark-grok-file-format-version)))
719 ((= version bookmark-file-format-version
)
720 ) ; home free -- version is current
722 (bookmark-upgrade-file-format-from-0))
724 (error "Bookmark file format version strangeness")))))
727 (defun bookmark-insert-file-format-version-stamp ()
728 "Insert text indicating current version of bookmark file format."
730 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
731 bookmark-file-format-version
))
732 (insert ";;; This format is meant to be slightly human-readable;\n"
733 ";;; nevertheless, you probably don't want to edit it.\n"
735 bookmark-end-of-version-stamp-marker
))
738 ;;; end file-format stuff
743 (defun bookmark-maybe-message (fmt &rest args
)
744 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
745 (if (>= baud-rate
9600)
746 (apply 'message fmt args
)))
751 (defvar bookmark-minibuffer-read-name-map
752 (let ((map (make-sparse-keymap)))
753 (set-keymap-parent map minibuffer-local-map
)
754 (define-key map
"\C-w" 'bookmark-yank-word
)
755 ;; This C-u binding might not be very useful any more now that we
756 ;; provide access to the default via the standard M-n binding.
757 ;; Maybe we should just remove it? --Stef-08
758 (define-key map
"\C-u" 'bookmark-insert-current-bookmark
)
762 (defun bookmark-set (&optional name no-overwrite
)
763 "Set a bookmark named NAME at the current location.
764 If name is nil, then prompt the user.
766 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
767 existing bookmark that has the same name as NAME, but instead push the
768 new bookmark onto the bookmark alist. The most recently set bookmark
769 with name NAME is thus the one in effect at any given time, but the
770 others are still there, should the user decide to delete the most
773 To yank words from the text of the buffer and use them as part of the
774 bookmark name, type C-w while setting a bookmark. Successive C-w's
775 yank successive words.
777 Typing C-u inserts (at the bookmark name prompt) the name of the last
778 bookmark used in the document where the new bookmark is being set;
779 this helps you use a single bookmark name to track progress through a
780 large document. If there is no prior bookmark for this document, then
781 C-u inserts an appropriate name based on the buffer or file.
783 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
784 it removes only the first instance of a bookmark with that name from
785 the list of bookmarks.)"
786 (interactive (list nil current-prefix-arg
))
788 (let* ((record (bookmark-make-record))
789 (default (car record
)))
791 (bookmark-maybe-load-default-file)
792 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
793 ;; if they have been already set in another buffer. (e.g gnus-art).
794 (unless (and bookmark-yank-point
795 bookmark-current-buffer
)
796 (setq bookmark-yank-point
(point))
797 (setq bookmark-current-buffer
(current-buffer)))
801 (read-from-minibuffer
802 (format "Set bookmark (%s): " default
)
804 bookmark-minibuffer-read-name-map
806 (and (string-equal str
"") (setq str default
))
807 (bookmark-store str
(cdr record
) no-overwrite
)
809 ;; Ask for an annotation buffer for this bookmark
810 (when bookmark-use-annotations
811 (bookmark-edit-annotation str
))))
812 (setq bookmark-yank-point nil
)
813 (setq bookmark-current-buffer nil
)))
816 (defun bookmark-kill-line (&optional newline-too
)
817 "Kill from point to end of line.
818 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
819 Does not affect the kill ring."
820 (let ((eol (line-end-position)))
821 (delete-region (point) eol
)
822 (if (and newline-too
(looking-at "\n"))
826 ;; Defvars to avoid compilation warnings:
827 (defvar bookmark-annotation-name nil
828 "Variable holding the name of the bookmark.
829 This is used in `bookmark-edit-annotation' to record the bookmark
830 whose annotation is being edited.")
833 (defun bookmark-default-annotation-text (bookmark)
834 "Return default annotation text for BOOKMARK (a string, not a record).
835 The default annotation text is simply some text explaining how to use
837 (concat "# Type the annotation for bookmark '" bookmark
"' here.\n"
838 "# All lines which start with a '#' will be deleted.\n"
839 "# Type C-c C-c when done.\n#\n"
840 "# Author: " (user-full-name) " <" (user-login-name) "@"
842 "# Date: " (current-time-string) "\n"))
845 (defvar bookmark-edit-annotation-text-func
'bookmark-default-annotation-text
846 "Function to return default text to use for a bookmark annotation.
847 It takes one argument, the name of the bookmark, as a string.")
848 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
849 'bookmark-edit-annotation-text-func
"23.1")
851 (defvar bookmark-edit-annotation-mode-map
852 (let ((map (make-sparse-keymap)))
853 (set-keymap-parent map text-mode-map
)
854 (define-key map
"\C-c\C-c" 'bookmark-send-edited-annotation
)
856 "Keymap for editing an annotation of a bookmark.")
859 (defun bookmark-edit-annotation-mode (bookmark)
860 "Mode for editing the annotation of bookmark BOOKMARK.
861 When you have finished composing, type \\[bookmark-send-annotation].
863 BOOKMARK is a bookmark name (a string) or a bookmark record.
865 \\{bookmark-edit-annotation-mode-map}"
867 (kill-all-local-variables)
868 (make-local-variable 'bookmark-annotation-name
)
869 (setq bookmark-annotation-name bookmark
)
870 (use-local-map bookmark-edit-annotation-mode-map
)
871 (setq major-mode
'bookmark-edit-annotation-mode
872 mode-name
"Edit Bookmark Annotation")
873 (insert (funcall bookmark-edit-annotation-text-func bookmark
))
874 (let ((annotation (bookmark-get-annotation bookmark
)))
875 (if (and annotation
(not (string-equal annotation
"")))
876 (insert annotation
)))
877 (run-mode-hooks 'text-mode-hook
))
880 (defun bookmark-send-edited-annotation ()
881 "Use buffer contents as annotation for a bookmark.
882 Lines beginning with `#' are ignored."
884 (if (not (eq major-mode
'bookmark-edit-annotation-mode
))
885 (error "Not in bookmark-edit-annotation-mode"))
886 (goto-char (point-min))
887 (while (< (point) (point-max))
888 (if (looking-at "^#")
889 (bookmark-kill-line t
)
891 ;; Take no chances with text properties.
892 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
893 (bookmark bookmark-annotation-name
))
894 (bookmark-set-annotation bookmark annotation
)
895 (setq bookmark-alist-modification-count
896 (1+ bookmark-alist-modification-count
))
897 (bookmark-bmenu-surreptitiously-rebuild-list))
898 (kill-buffer (current-buffer)))
901 (defun bookmark-edit-annotation (bookmark)
902 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
903 BOOKMARK is a bookmark name (a string) or a bookmark record."
904 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
905 (bookmark-edit-annotation-mode bookmark
))
908 (defun bookmark-insert-current-bookmark ()
909 "Insert into the bookmark name currently being set the value of
910 `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
911 to the buffer's file name if `bookmark-current-bookmark' is nil."
914 (with-current-buffer bookmark-current-buffer
915 (or bookmark-current-bookmark
916 (bookmark-buffer-name)))))
920 (defun bookmark-buffer-name ()
921 "Return the name of the current buffer in a form usable as a bookmark name.
922 If the buffer is associated with a file or directory, use that name."
925 (buffer-file-name (file-name-nondirectory buffer-file-name
))
926 ;; Or are we a directory?
927 ((and (boundp 'dired-directory
) dired-directory
)
928 (let* ((dirname (if (stringp dired-directory
)
930 (car dired-directory
)))
931 (idx (1- (length dirname
))))
932 ;; Strip the trailing slash.
933 (if (= ?
/ (aref dirname idx
))
934 (file-name-nondirectory (substring dirname
0 idx
))
935 ;; Else return the current-buffer
936 (buffer-name (current-buffer)))))
937 ;; If all else fails, use the buffer's name.
939 (buffer-name (current-buffer)))))
942 (defun bookmark-yank-word ()
943 "Get the next word from buffer `bookmark-current-buffer' and append
944 it to the name of the bookmark currently being set, advancing
945 `bookmark-yank-point' by one word."
947 (let ((string (with-current-buffer bookmark-current-buffer
948 (goto-char bookmark-yank-point
)
949 (buffer-substring-no-properties
953 (setq bookmark-yank-point
(point)))))))
956 (defun bookmark-buffer-file-name ()
957 "Return the current buffer's file in a way useful for bookmarks."
958 ;; Abbreviate the path, both so it's shorter and so it's more
959 ;; portable. E.g., the user's home dir might be a different
960 ;; path on different machines, but "~/" will still reach it.
961 (abbreviate-file-name
963 (buffer-file-name buffer-file-name
)
964 ((and (boundp 'dired-directory
) dired-directory
)
965 (if (stringp dired-directory
)
967 (car dired-directory
)))
968 (t (error "Buffer not visiting a file or directory")))))
971 (defun bookmark-maybe-load-default-file ()
972 "If bookmarks have not been loaded from the default place, load them."
973 (and (not bookmarks-already-loaded
)
974 (null bookmark-alist
)
977 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
979 (file-exists-p bookmark-old-default-file
)
980 (not (file-exists-p bookmark-default-file
))
981 (rename-file bookmark-old-default-file
982 bookmark-default-file
))
983 ;; return t so the `and' will continue...
986 (file-readable-p bookmark-default-file
)
987 (bookmark-load bookmark-default-file t t
)
988 (setq bookmarks-already-loaded t
)))
991 (defun bookmark-maybe-sort-alist ()
992 "Return `bookmark-alist' for display.
993 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
994 (if bookmark-sort-flag
995 (sort (copy-alist bookmark-alist
)
997 (lambda (x y
) (string-lessp (car x
) (car y
)))))
1001 (defvar bookmark-after-jump-hook nil
1002 "Hook run after `bookmark-jump' jumps to a bookmark.
1003 Useful for example to unhide text in `outline-mode'.")
1005 (defun bookmark--jump-via (bookmark display-function
)
1006 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
1007 Bookmark may be a bookmark name (a string) or a bookmark record.
1009 After calling DISPLAY-FUNCTION, set window point to the point specified
1010 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
1011 any annotations for this bookmark."
1012 (bookmark-handle-bookmark bookmark
)
1013 (save-current-buffer
1014 (funcall display-function
(current-buffer)))
1015 (let ((win (get-buffer-window (current-buffer) 0)))
1016 (if win
(set-window-point win
(point))))
1017 ;; FIXME: we used to only run bookmark-after-jump-hook in
1018 ;; `bookmark-jump' itself, but in none of the other commands.
1019 (run-hooks 'bookmark-after-jump-hook
)
1020 (if bookmark-automatically-show-annotations
1021 ;; if there is an annotation for this bookmark,
1022 ;; show it in a buffer.
1023 (bookmark-show-annotation bookmark
)))
1027 (defun bookmark-jump (bookmark &optional display-func
)
1028 "Jump to bookmark BOOKMARK (a point in some file).
1029 You may have a problem using this function if the value of variable
1030 `bookmark-alist' is nil. If that happens, you need to load in some
1031 bookmarks. See help on function `bookmark-load' for more about
1034 If the file pointed to by BOOKMARK no longer exists, you will be asked
1035 if you wish to give the bookmark a new location, and `bookmark-jump'
1036 will then jump to the new location, as well as recording it in place
1037 of the old one in the permanent bookmark record.
1039 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1040 the latter is usually only used by programmatic callers.
1042 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1043 bookmark. It defaults to `switch-to-buffer'. A typical value for
1044 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1046 (list (bookmark-completing-read "Jump to bookmark"
1047 bookmark-current-bookmark
)))
1049 (error "No bookmark specified"))
1050 (bookmark-maybe-historicize-string bookmark
)
1051 (bookmark--jump-via bookmark
(or display-func
'switch-to-buffer
)))
1055 (defun bookmark-jump-other-window (bookmark)
1056 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1058 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1059 bookmark-current-bookmark
)))
1060 (bookmark-jump bookmark
'switch-to-buffer-other-window
))
1063 (defun bookmark-jump-noselect (bookmark)
1064 "Return the location pointed to by the bookmark BOOKMARK.
1065 The return value has the form (BUFFER . POINT).
1067 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1069 Note: this function is deprecated and is present for Emacs 22
1070 compatibility only."
1072 (bookmark-handle-bookmark bookmark
)
1073 (cons (current-buffer) (point))))
1075 (make-obsolete 'bookmark-jump-noselect
'bookmark-handle-bookmark
"23.1")
1077 (defun bookmark-handle-bookmark (bookmark)
1078 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1079 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1081 Changes current buffer and point and returns nil, or signals a `file-error'.
1083 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1084 that file no longer exists, then offer interactively to relocate BOOKMARK."
1086 (funcall (or (bookmark-get-handler bookmark
)
1087 'bookmark-default-handler
)
1088 (bookmark-get-bookmark bookmark
))
1089 (bookmark-error-no-filename ;file-error
1090 ;; We were unable to find the marked file, so ask if user wants to
1091 ;; relocate the bookmark, else remind them to consider deletion.
1092 (when (stringp bookmark
)
1093 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1094 ;; or a bookmark object. If it's an object, we assume it's a
1095 ;; bookmark used internally by some other package.
1096 (let ((file (bookmark-get-filename bookmark
)))
1097 (when file
;Don't know how to relocate if there's no `file'.
1098 ;; If file is not a dir, directory-file-name just returns file.
1099 (let ((display-name (directory-file-name file
)))
1101 ;; Dialog boxes can accept a file target, but usually don't
1102 ;; know how to accept a directory target (at least, this
1103 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1104 ;; true on Windows as well). So we suppress file dialogs
1106 (let ((use-dialog-box nil
)
1107 (use-file-dialog nil
))
1108 (if (y-or-n-p (concat display-name
" nonexistent. Relocate \""
1111 (bookmark-relocate bookmark
)
1113 (funcall (or (bookmark-get-handler bookmark
)
1114 'bookmark-default-handler
)
1115 (bookmark-get-bookmark bookmark
)))
1117 "Bookmark not relocated; consider removing it (%s)."
1119 (signal (car err
) (cdr err
))))))))))
1121 (when (stringp bookmark
)
1122 (setq bookmark-current-bookmark bookmark
))
1125 (put 'bookmark-error-no-filename
1127 '(error bookmark-errors bookmark-error-no-filename
))
1128 (put 'bookmark-error-no-filename
1130 "Bookmark has no associated file (or directory)")
1132 (defun bookmark-default-handler (bmk-record)
1133 "Default handler to jump to a particular bookmark location.
1134 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1135 Changes current buffer and point and returns nil, or signals a `file-error'."
1136 (let ((file (bookmark-get-filename bmk-record
))
1137 (buf (bookmark-prop-get bmk-record
'buffer
))
1138 (forward-str (bookmark-get-front-context-string bmk-record
))
1139 (behind-str (bookmark-get-rear-context-string bmk-record
))
1140 (place (bookmark-get-position bmk-record
)))
1143 ((and file
(file-readable-p file
) (not (buffer-live-p buf
)))
1144 (find-file-noselect file
))
1145 ;; No file found. See if buffer BUF have been created.
1146 ((and buf
(get-buffer buf
)))
1147 (t ;; If not, raise error.
1148 (signal 'bookmark-error-no-filename
(list 'stringp file
)))))
1149 (if place
(goto-char place
))
1150 ;; Go searching forward first. Then, if forward-str exists and
1151 ;; was found in the file, we can search backward for behind-str.
1152 ;; Rationale is that if text was inserted between the two in the
1153 ;; file, it's better to be put before it so you can read it,
1154 ;; rather than after and remain perhaps unaware of the changes.
1155 (when (and forward-str
(search-forward forward-str
(point-max) t
))
1156 (goto-char (match-beginning 0)))
1157 (when (and behind-str
(search-backward behind-str
(point-min) t
))
1158 (goto-char (match-end 0)))
1162 (defun bookmark-relocate (bookmark)
1163 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1164 BOOKMARK is a bookmark name (a string), not a bookmark record.
1166 This makes an already existing bookmark point to that file, instead of
1167 the one it used to point at. Useful when a file has been renamed
1168 after a bookmark was set in it."
1169 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1170 (bookmark-maybe-historicize-string bookmark
)
1171 (bookmark-maybe-load-default-file)
1172 (let* ((bmrk-filename (bookmark-get-filename bookmark
))
1173 (newloc (abbreviate-file-name
1176 (format "Relocate %s to: " bookmark
)
1177 (file-name-directory bmrk-filename
))))))
1178 (bookmark-set-filename bookmark newloc
)
1179 (setq bookmark-alist-modification-count
1180 (1+ bookmark-alist-modification-count
))
1181 (if (bookmark-time-to-save-p)
1183 (bookmark-bmenu-surreptitiously-rebuild-list)))
1187 (defun bookmark-insert-location (bookmark &optional no-history
)
1188 "Insert the name of the file associated with BOOKMARK.
1189 BOOKMARK is a bookmark name (a string), not a bookmark record.
1191 Optional second arg NO-HISTORY means don't record this in the
1192 minibuffer history list `bookmark-history'."
1193 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1194 (or no-history
(bookmark-maybe-historicize-string bookmark
))
1195 (let ((start (point)))
1197 (insert (bookmark-location bookmark
))
1198 (if (display-mouse-p)
1199 (add-text-properties
1201 (save-excursion (re-search-backward
1204 '(mouse-face highlight
1206 help-echo
"mouse-2: go to this bookmark in other window"))))))
1209 (defalias 'bookmark-locate
'bookmark-insert-location
)
1211 (defun bookmark-location (bookmark)
1212 "Return a description of the location of BOOKMARK.
1213 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1214 (bookmark-maybe-load-default-file)
1215 ;; We could call the `handler' and ask for it to construct a description
1216 ;; dynamically: it would open up several new possibilities, but it
1217 ;; would have the major disadvantage of forcing to load each and
1218 ;; every handler when the user calls bookmark-menu.
1219 (or (bookmark-prop-get bookmark
'location
)
1220 (bookmark-get-filename bookmark
)
1221 "-- Unknown location --"))
1225 (defun bookmark-rename (old &optional new
)
1226 "Change the name of OLD bookmark to NEW name.
1227 If called from keyboard, prompt for OLD and NEW. If called from
1228 menubar, select OLD from a menu and prompt for NEW.
1230 Both OLD and NEW are bookmark names (strings), never bookmark records.
1232 If called from Lisp, prompt for NEW if only OLD was passed as an
1233 argument. If called with two strings, then no prompting is done. You
1234 must pass at least OLD when calling from Lisp.
1236 While you are entering the new name, consecutive C-w's insert
1237 consecutive words from the text of the buffer into the new bookmark
1239 (interactive (list (bookmark-completing-read "Old bookmark name")))
1240 (bookmark-maybe-historicize-string old
)
1241 (bookmark-maybe-load-default-file)
1243 (setq bookmark-yank-point
(point))
1244 (setq bookmark-current-buffer
(current-buffer))
1246 (or new
; use second arg, if non-nil
1247 (read-from-minibuffer
1250 (let ((now-map (copy-keymap minibuffer-local-map
)))
1251 (define-key now-map
"\C-w" 'bookmark-yank-word
)
1254 'bookmark-history
))))
1255 (bookmark-set-name old newname
)
1256 (setq bookmark-current-bookmark newname
)
1257 (bookmark-bmenu-surreptitiously-rebuild-list)
1258 (setq bookmark-alist-modification-count
1259 (1+ bookmark-alist-modification-count
))
1260 (if (bookmark-time-to-save-p)
1265 (defun bookmark-insert (bookmark)
1266 "Insert the text of the file pointed to by bookmark BOOKMARK.
1267 BOOKMARK is a bookmark name (a string), not a bookmark record.
1269 You may have a problem using this function if the value of variable
1270 `bookmark-alist' is nil. If that happens, you need to load in some
1271 bookmarks. See help on function `bookmark-load' for more about
1273 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1274 (bookmark-maybe-historicize-string bookmark
)
1275 (bookmark-maybe-load-default-file)
1276 (let ((orig-point (point))
1278 (save-current-buffer
1279 (bookmark-handle-bookmark bookmark
)
1281 (insert str-to-insert
)
1283 (goto-char orig-point
)))
1287 (defun bookmark-delete (bookmark &optional batch
)
1288 "Delete BOOKMARK from the bookmark list.
1289 BOOKMARK is a bookmark name (a string), not a bookmark record.
1291 Removes only the first instance of a bookmark with that name. If
1292 there are one or more other bookmarks with the same name, they will
1293 not be deleted. Defaults to the \"current\" bookmark (that is, the
1294 one most recently used in this file, if any).
1295 Optional second arg BATCH means don't update the bookmark list buffer,
1296 probably because we were called from there."
1298 (list (bookmark-completing-read "Delete bookmark"
1299 bookmark-current-bookmark
)))
1300 (bookmark-maybe-historicize-string bookmark
)
1301 (bookmark-maybe-load-default-file)
1302 (let ((will-go (bookmark-get-bookmark bookmark
'noerror
)))
1303 (setq bookmark-alist
(delq will-go bookmark-alist
))
1304 ;; Added by db, nil bookmark-current-bookmark if the last
1305 ;; occurrence has been deleted
1306 (or (bookmark-get-bookmark bookmark-current-bookmark
'noerror
)
1307 (setq bookmark-current-bookmark nil
)))
1309 (bookmark-bmenu-surreptitiously-rebuild-list))
1310 (setq bookmark-alist-modification-count
1311 (1+ bookmark-alist-modification-count
))
1312 (when (bookmark-time-to-save-p)
1316 (defun bookmark-time-to-save-p (&optional final-time
)
1317 "Return t if it is time to save bookmarks to disk, nil otherwise.
1318 Optional argument FINAL-TIME means this is being called when Emacs
1319 is being killed, so save even if `bookmark-save-flag' is a number and
1320 is greater than `bookmark-alist-modification-count'."
1321 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1323 (and (> bookmark-alist-modification-count
0)
1324 bookmark-save-flag
))
1325 ((numberp bookmark-save-flag
)
1326 (>= bookmark-alist-modification-count bookmark-save-flag
))
1332 (defun bookmark-write ()
1333 "Write bookmarks to a file (reading the file name with the minibuffer).
1334 Don't use this in Lisp programs; use `bookmark-save' instead."
1336 (bookmark-maybe-load-default-file)
1341 (defun bookmark-save (&optional parg file
)
1342 "Save currently defined bookmarks.
1343 Saves by default in the file defined by the variable
1344 `bookmark-default-file'. With a prefix arg, save it in file FILE
1347 If you are calling this from Lisp, the two arguments are PARG and
1348 FILE, and if you just want it to write to the default file, then
1349 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1350 instead. If you pass in one argument, and it is non-nil, then the
1351 user will be interactively queried for a file to save in.
1353 When you want to load in the bookmarks from a file, use
1354 `bookmark-load', \\[bookmark-load]. That function will prompt you
1355 for a file, defaulting to the file defined by variable
1356 `bookmark-default-file'."
1358 (bookmark-maybe-load-default-file)
1360 ((and (null parg
) (null file
))
1361 ;;whether interactive or not, write to default file
1362 (bookmark-write-file bookmark-default-file
))
1363 ((and (null parg
) file
)
1364 ;;whether interactive or not, write to given file
1365 (bookmark-write-file file
))
1366 ((and parg
(not file
))
1367 ;;have been called interactively w/ prefix arg
1368 (let ((file (read-file-name "File to save bookmarks in: ")))
1369 (bookmark-write-file file
)))
1370 (t ; someone called us with prefix-arg *and* a file, so just write to file
1371 (bookmark-write-file file
)))
1372 ;; signal that we have synced the bookmark file by setting this to
1373 ;; 0. If there was an error at any point before, it will not get
1374 ;; set, which is what we want.
1375 (setq bookmark-alist-modification-count
0))
1379 (defun bookmark-write-file (file)
1380 "Write `bookmark-alist' to FILE."
1381 (bookmark-maybe-message "Saving bookmarks to file %s..." file
)
1382 (with-current-buffer (get-buffer-create " *Bookmarks*")
1383 (goto-char (point-min))
1384 (delete-region (point-min) (point-max))
1385 (let ((print-length nil
)
1387 (bookmark-insert-file-format-version-stamp)
1389 ;; Rather than a single call to `pp' we make one per bookmark.
1390 ;; Apparently `pp' has a poor algorithmic complexity, so this
1391 ;; scales a lot better. bug#4485.
1392 (dolist (i bookmark-alist
) (pp i
(current-buffer)))
1394 (let ((version-control
1396 ((null bookmark-version-control
) nil
)
1397 ((eq 'never bookmark-version-control
) 'never
)
1398 ((eq 'nospecial bookmark-version-control
) version-control
)
1401 (write-region (point-min) (point-max) file
)
1402 (file-error (message "Can't write %s" file
)))
1403 (kill-buffer (current-buffer))
1404 (bookmark-maybe-message
1405 "Saving bookmarks to file %s...done" file
)))))
1408 (defun bookmark-import-new-list (new-list)
1409 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1410 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1411 they conflict with existing bookmark names."
1412 (let ((names (bookmark-all-names)))
1413 (dolist (full-record new-list
)
1414 (bookmark-maybe-rename full-record names
)
1415 (setq bookmark-alist
(nconc bookmark-alist
(list full-record
)))
1416 (push (bookmark-name-from-full-record full-record
) names
))))
1419 (defun bookmark-maybe-rename (full-record names
)
1420 "Rename bookmark FULL-RECORD if its current name is already used.
1421 This is a helper for `bookmark-import-new-list'."
1422 (let ((found-name (bookmark-name-from-full-record full-record
)))
1423 (if (member found-name names
)
1424 ;; We've got a conflict, so generate a new name
1426 (new-name found-name
))
1427 (while (member new-name names
)
1428 (setq new-name
(concat found-name
(format "<%d>" count
)))
1429 (setq count
(1+ count
)))
1430 (bookmark-set-name full-record new-name
)))))
1434 (defun bookmark-load (file &optional overwrite no-msg
)
1435 "Load bookmarks from FILE (which must be in bookmark format).
1436 Appends loaded bookmarks to the front of the list of bookmarks. If
1437 optional second argument OVERWRITE is non-nil, existing bookmarks are
1438 destroyed. Optional third arg NO-MSG means don't display any messages
1441 If you load a file that doesn't contain a proper bookmark alist, you
1442 will corrupt Emacs's bookmark list. Generally, you should only load
1443 in files that were created with the bookmark functions in the first
1444 place. Your own personal bookmark file, `~/.emacs.bmk', is
1445 maintained automatically by Emacs; you shouldn't need to load it
1448 If you load a file containing bookmarks with the same names as
1449 bookmarks already present in your Emacs, the new bookmarks will get
1450 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1451 method buffers use to resolve name collisions."
1453 (list (read-file-name
1454 (format "Load bookmarks from: (%s) "
1455 bookmark-default-file
)
1456 ;;Default might not be used often,
1457 ;;but there's no better default, and
1458 ;;I guess it's better than none at all.
1459 "~/" bookmark-default-file
'confirm
)))
1460 (setq file
(abbreviate-file-name (expand-file-name file
)))
1461 (if (not (file-readable-p file
))
1462 (error "Cannot read bookmark file %s" file
)
1464 (bookmark-maybe-message "Loading bookmarks from %s..." file
))
1465 (with-current-buffer (let ((enable-local-variables nil
))
1466 (find-file-noselect file
))
1467 (goto-char (point-min))
1468 (bookmark-maybe-upgrade-file-format)
1469 (let ((blist (bookmark-alist-from-buffer)))
1474 (setq bookmark-alist blist
)
1475 (setq bookmark-alist-modification-count
0))
1477 (bookmark-import-new-list blist
)
1478 (setq bookmark-alist-modification-count
1479 (1+ bookmark-alist-modification-count
)))
1481 (abbreviate-file-name
1482 (expand-file-name bookmark-default-file
))
1484 (setq bookmarks-already-loaded t
))
1485 (bookmark-bmenu-surreptitiously-rebuild-list))
1486 (error "Invalid bookmark list in %s" file
)))
1487 (kill-buffer (current-buffer)))
1489 (bookmark-maybe-message "Loading bookmarks from %s...done" file
))))
1493 ;;; Code supporting the dired-like bookmark menu.
1494 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1497 (defvar bookmark-bmenu-hidden-bookmarks
())
1500 (defvar bookmark-bmenu-mode-map
1501 (let ((map (make-keymap)))
1502 (suppress-keymap map t
)
1503 (define-key map
"q" 'quit-window
)
1504 (define-key map
"v" 'bookmark-bmenu-select
)
1505 (define-key map
"w" 'bookmark-bmenu-locate
)
1506 (define-key map
"2" 'bookmark-bmenu-2-window
)
1507 (define-key map
"1" 'bookmark-bmenu-1-window
)
1508 (define-key map
"j" 'bookmark-bmenu-this-window
)
1509 (define-key map
"\C-c\C-c" 'bookmark-bmenu-this-window
)
1510 (define-key map
"f" 'bookmark-bmenu-this-window
)
1511 (define-key map
"\C-m" 'bookmark-bmenu-this-window
)
1512 (define-key map
"o" 'bookmark-bmenu-other-window
)
1513 (define-key map
"\C-o" 'bookmark-bmenu-switch-other-window
)
1514 (define-key map
"s" 'bookmark-bmenu-save
)
1515 (define-key map
"k" 'bookmark-bmenu-delete
)
1516 (define-key map
"\C-d" 'bookmark-bmenu-delete-backwards
)
1517 (define-key map
"x" 'bookmark-bmenu-execute-deletions
)
1518 (define-key map
"d" 'bookmark-bmenu-delete
)
1519 (define-key map
" " 'next-line
)
1520 (define-key map
"n" 'next-line
)
1521 (define-key map
"p" 'previous-line
)
1522 (define-key map
"\177" 'bookmark-bmenu-backup-unmark
)
1523 (define-key map
"?" 'describe-mode
)
1524 (define-key map
"u" 'bookmark-bmenu-unmark
)
1525 (define-key map
"m" 'bookmark-bmenu-mark
)
1526 (define-key map
"l" 'bookmark-bmenu-load
)
1527 (define-key map
"r" 'bookmark-bmenu-rename
)
1528 (define-key map
"R" 'bookmark-bmenu-relocate
)
1529 (define-key map
"t" 'bookmark-bmenu-toggle-filenames
)
1530 (define-key map
"a" 'bookmark-bmenu-show-annotation
)
1531 (define-key map
"A" 'bookmark-bmenu-show-all-annotations
)
1532 (define-key map
"e" 'bookmark-bmenu-edit-annotation
)
1533 ;; The original binding of M-g hides the M-g prefix map.
1534 ;; If someone has a better idea than M-g s, I'm open to suggestions.
1535 (define-key map
[?\M-g ?s
] 'bookmark-bmenu-search
)
1536 (define-key map
[mouse-2
] 'bookmark-bmenu-other-window-with-mouse
)
1539 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1541 (put 'bookmark-bmenu-mode
'mode-class
'special
)
1544 ;; todo: need to display whether or not bookmark exists as a buffer in
1548 ;; FLAGS BOOKMARK [ LOCATION ]
1551 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1552 "Rebuild the Bookmark List if it exists.
1553 Don't affect the buffer ring order."
1554 (if (get-buffer "*Bookmark List*")
1556 (save-window-excursion
1557 (bookmark-bmenu-list)))))
1561 (defun bookmark-bmenu-list ()
1562 "Display a list of existing bookmarks.
1563 The list is displayed in a buffer named `*Bookmark List*'.
1564 The leftmost column displays a D if the bookmark is flagged for
1565 deletion, or > if it is flagged for displaying."
1567 (bookmark-maybe-load-default-file)
1568 (let ((buf (get-buffer-create "*Bookmark List*")))
1569 (if (called-interactively-p 'interactive
)
1570 (if (or (window-dedicated-p) (window-minibuffer-p))
1572 (switch-to-buffer buf
))
1574 (let ((inhibit-read-only t
))
1576 (insert "% Bookmark\n- --------\n")
1577 (add-text-properties (point-min) (point)
1578 '(font-lock-face bookmark-menu-heading
))
1579 (dolist (full-record (bookmark-maybe-sort-alist))
1580 (let ((name (bookmark-name-from-full-record full-record
))
1581 (annotation (bookmark-get-annotation full-record
))
1584 ;; if a bookmark has an annotation, prepend a "*"
1585 ;; in the list of bookmarks.
1586 (insert (if (and annotation
(not (string-equal annotation
"")))
1591 (+ bookmark-bmenu-marks-width start
) end
'bookmark-name-prop name
)
1592 (when (display-mouse-p)
1593 (add-text-properties
1594 (+ bookmark-bmenu-marks-width start
) end
1595 '(mouse-face highlight
1597 help-echo
"mouse-2: go to this bookmark in other window")))
1599 (set-buffer-modified-p (not (= bookmark-alist-modification-count
0)))
1600 (goto-char (point-min))
1602 (bookmark-bmenu-mode)
1603 (if bookmark-bmenu-toggle-filenames
1604 (bookmark-bmenu-toggle-filenames t
))))
1607 (defalias 'list-bookmarks
'bookmark-bmenu-list
)
1609 (defalias 'edit-bookmarks
'bookmark-bmenu-list
)
1613 (defun bookmark-bmenu-mode ()
1614 "Major mode for editing a list of bookmarks.
1615 Each line describes one of the bookmarks in Emacs.
1616 Letters do not insert themselves; instead, they are commands.
1617 Bookmark names preceded by a \"*\" have annotations.
1618 \\<bookmark-bmenu-mode-map>
1619 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1620 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1621 Also show bookmarks marked using m in other windows.
1622 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1623 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1624 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1625 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1626 together with bookmark selected before this one in another window.
1627 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1628 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1629 so the bookmark menu bookmark remains visible in its window.
1630 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1631 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1632 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1633 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1634 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1635 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1636 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1637 With a prefix arg, prompts for a file to save in.
1638 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1639 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1640 With prefix argument, also move up one line.
1641 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1642 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1644 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1645 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1646 (kill-all-local-variables)
1647 (use-local-map bookmark-bmenu-mode-map
)
1648 (setq truncate-lines t
)
1649 (setq buffer-read-only t
)
1650 (setq major-mode
'bookmark-bmenu-mode
)
1651 (setq mode-name
"Bookmark Menu")
1652 (run-mode-hooks 'bookmark-bmenu-mode-hook
))
1655 (defun bookmark-bmenu-toggle-filenames (&optional show
)
1656 "Toggle whether filenames are shown in the bookmark list.
1657 Optional argument SHOW means show them unconditionally."
1661 (setq bookmark-bmenu-toggle-filenames nil
)
1662 (bookmark-bmenu-show-filenames)
1663 (setq bookmark-bmenu-toggle-filenames t
))
1664 (bookmark-bmenu-toggle-filenames
1665 (bookmark-bmenu-hide-filenames)
1666 (setq bookmark-bmenu-toggle-filenames nil
))
1668 (bookmark-bmenu-show-filenames)
1669 (setq bookmark-bmenu-toggle-filenames t
))))
1672 (defun bookmark-bmenu-show-filenames (&optional force
)
1673 "In an interactive bookmark list, show filenames along with bookmarks.
1674 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1675 mainly for debugging, and should not be necessary in normal use."
1676 (if (and (not force
) bookmark-bmenu-toggle-filenames
)
1677 nil
;already shown, so do nothing
1678 (with-buffer-modified-unmodified
1680 (save-window-excursion
1681 (goto-char (point-min))
1683 (setq bookmark-bmenu-hidden-bookmarks
())
1684 (let ((inhibit-read-only t
))
1685 (while (< (point) (point-max))
1686 (let ((bmrk (bookmark-bmenu-bookmark)))
1687 (push bmrk bookmark-bmenu-hidden-bookmarks
)
1688 (let ((start (line-end-position)))
1689 (move-to-column bookmark-bmenu-file-column t
)
1690 ;; Strip off `mouse-face' from the white spaces region.
1691 (if (display-mouse-p)
1692 (remove-text-properties start
(point)
1693 '(mouse-face nil help-echo nil
))))
1694 (delete-region (point) (progn (end-of-line) (point)))
1696 ;; Pass the NO-HISTORY arg:
1697 (bookmark-insert-location bmrk t
)
1698 (forward-line 1)))))))))
1701 (defun bookmark-bmenu-hide-filenames (&optional force
)
1702 "In an interactive bookmark list, hide the filenames of the bookmarks.
1703 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1704 mainly for debugging, and should not be necessary in normal use."
1705 (when (and (not force
) bookmark-bmenu-toggle-filenames
)
1706 ;; nothing to hide if above is nil
1707 (with-buffer-modified-unmodified
1709 (goto-char (point-min))
1711 (setq bookmark-bmenu-hidden-bookmarks
1712 (nreverse bookmark-bmenu-hidden-bookmarks
))
1713 (let ((inhibit-read-only t
))
1714 (while bookmark-bmenu-hidden-bookmarks
1715 (move-to-column bookmark-bmenu-marks-width t
)
1716 (bookmark-kill-line)
1717 (let ((name (pop bookmark-bmenu-hidden-bookmarks
))
1720 (put-text-property start
(point) 'bookmark-name-prop name
)
1721 (if (display-mouse-p)
1722 (add-text-properties
1725 highlight follow-link t help-echo
1726 "mouse-2: go to this bookmark in other window"))))
1727 (forward-line 1)))))))
1730 (defun bookmark-bmenu-ensure-position ()
1731 "If point is not on a bookmark line, move it to one.
1732 If before the first bookmark line, move to the first; if after the
1733 last full line, move to the last full line. The return value is undefined."
1734 (cond ((< (count-lines (point-min) (point)) bookmark-bmenu-header-height
)
1735 (goto-char (point-min))
1736 (forward-line bookmark-bmenu-header-height
))
1737 ((and (bolp) (eobp))
1738 (beginning-of-line 0))))
1741 (defun bookmark-bmenu-bookmark ()
1742 "Return the bookmark for this line in an interactive bookmark list buffer."
1743 (bookmark-bmenu-ensure-position)
1746 (forward-char bookmark-bmenu-marks-width
)
1747 (get-text-property (point) 'bookmark-name-prop
)))
1750 (defun bookmark-show-annotation (bookmark)
1751 "Display the annotation for bookmark named BOOKMARK in a buffer,
1752 if an annotation exists."
1753 (let ((annotation (bookmark-get-annotation bookmark
)))
1754 (when (and annotation
(not (string-equal annotation
"")))
1756 (let ((old-buf (current-buffer)))
1757 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t
)
1758 (delete-region (point-min) (point-max))
1759 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1761 (goto-char (point-min))
1762 (switch-to-buffer-other-window old-buf
))))))
1765 (defun bookmark-show-all-annotations ()
1766 "Display the annotations for all bookmarks in a buffer."
1767 (save-selected-window
1768 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t
)
1769 (delete-region (point-min) (point-max))
1770 (dolist (full-record bookmark-alist
)
1771 (let* ((name (bookmark-name-from-full-record full-record
))
1772 (ann (bookmark-get-annotation full-record
)))
1773 (insert (concat name
":\n"))
1774 (if (and ann
(not (string-equal ann
"")))
1775 ;; insert the annotation, indented by 4 spaces.
1777 (save-excursion (insert ann
) (unless (bolp)
1779 (while (< (point) (point-max))
1780 (beginning-of-line) ; paranoia
1784 (goto-char (point-min))))
1787 (defun bookmark-bmenu-mark ()
1788 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1791 (bookmark-bmenu-ensure-position)
1792 (with-buffer-modified-unmodified
1793 (let ((inhibit-read-only t
))
1797 (bookmark-bmenu-ensure-position))))
1800 (defun bookmark-bmenu-select ()
1801 "Select this line's bookmark; also display bookmarks marked with `>'.
1802 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1804 (let ((bmrk (bookmark-bmenu-bookmark))
1805 (menu (current-buffer))
1808 (goto-char (point-min))
1809 (while (re-search-forward "^>" nil t
)
1810 (setq tem
(bookmark-bmenu-bookmark))
1811 (let ((inhibit-read-only t
))
1814 (or (string-equal tem bmrk
)
1816 (setq others
(cons tem others
))))
1817 (setq others
(nreverse others
)
1818 tem
(/ (1- (frame-height)) (1+ (length others
))))
1819 (delete-other-windows)
1820 (bookmark-jump bmrk
)
1824 (split-window nil tem
)
1826 (bookmark-jump (car others
))
1827 (setq others
(cdr others
)))
1831 (defun bookmark-bmenu-any-marks ()
1832 "Return non-nil if any bookmarks are marked in the marks column."
1834 (goto-char (point-min))
1835 (bookmark-bmenu-ensure-position)
1839 (if (looking-at "^\\S-")
1840 (throw 'found-mark t
)
1845 (defun bookmark-bmenu-save (parg)
1846 "Save the current list into a bookmark file.
1847 With a prefix arg, prompts for a file to save them in."
1850 (save-window-excursion
1851 (bookmark-save parg
)
1852 (set-buffer-modified-p nil
))))
1855 (defun bookmark-bmenu-load ()
1856 "Load the bookmark file and rebuild the bookmark menu-buffer."
1858 (bookmark-bmenu-ensure-position)
1860 (save-window-excursion
1861 ;; This will call `bookmark-bmenu-list'
1862 (call-interactively 'bookmark-load
))))
1865 (defun bookmark-bmenu-1-window ()
1866 "Select this line's bookmark, alone, in full frame."
1868 (bookmark-jump (bookmark-bmenu-bookmark))
1869 (bury-buffer (other-buffer))
1870 (delete-other-windows))
1873 (defun bookmark-bmenu-2-window ()
1874 "Select this line's bookmark, with previous buffer in second window."
1876 (let ((bmrk (bookmark-bmenu-bookmark))
1877 (menu (current-buffer))
1879 (delete-other-windows)
1880 (switch-to-buffer (other-buffer))
1881 (bookmark--jump-via bmrk
'pop-to-buffer
)
1882 (bury-buffer menu
)))
1885 (defun bookmark-bmenu-this-window ()
1886 "Select this line's bookmark in this window."
1888 (bookmark-jump (bookmark-bmenu-bookmark)))
1891 (defun bookmark-bmenu-other-window ()
1892 "Select this line's bookmark in other window, leaving bookmark menu visible."
1894 (let ((bookmark (bookmark-bmenu-bookmark)))
1895 (bookmark--jump-via bookmark
'switch-to-buffer-other-window
)))
1898 (defun bookmark-bmenu-switch-other-window ()
1899 "Make the other window select this line's bookmark.
1900 The current window remains selected."
1902 (let ((bookmark (bookmark-bmenu-bookmark))
1904 same-window-buffer-names
1905 same-window-regexps
)
1906 (bookmark--jump-via bookmark
'display-buffer
)))
1908 (defun bookmark-bmenu-other-window-with-mouse (event)
1909 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1911 (with-current-buffer (window-buffer (posn-window (event-end event
)))
1913 (goto-char (posn-point (event-end event
)))
1914 (bookmark-bmenu-other-window))))
1917 (defun bookmark-bmenu-show-annotation ()
1918 "Show the annotation for the current bookmark in another window."
1920 (let ((bookmark (bookmark-bmenu-bookmark)))
1921 (bookmark-show-annotation bookmark
)))
1924 (defun bookmark-bmenu-show-all-annotations ()
1925 "Show the annotation for all bookmarks in another window."
1927 (bookmark-show-all-annotations))
1930 (defun bookmark-bmenu-edit-annotation ()
1931 "Edit the annotation for the current bookmark in another window."
1933 (let ((bookmark (bookmark-bmenu-bookmark)))
1934 (bookmark-edit-annotation bookmark
)))
1937 (defun bookmark-bmenu-unmark (&optional backup
)
1938 "Cancel all requested operations on bookmark on this line and move down.
1939 Optional BACKUP means move up."
1942 (bookmark-bmenu-ensure-position)
1943 (with-buffer-modified-unmodified
1944 (let ((inhibit-read-only t
))
1946 ;; any flags to reset according to circumstances? How about a
1947 ;; flag indicating whether this bookmark is being visited?
1948 ;; well, we don't have this now, so maybe later.
1950 (forward-line (if backup -
1 1))
1951 (bookmark-bmenu-ensure-position)))
1954 (defun bookmark-bmenu-backup-unmark ()
1955 "Move up and cancel all requested operations on bookmark on line above."
1958 (bookmark-bmenu-ensure-position)
1959 (bookmark-bmenu-unmark)
1961 (bookmark-bmenu-ensure-position))
1964 (defun bookmark-bmenu-delete ()
1965 "Mark bookmark on this line to be deleted.
1966 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1969 (bookmark-bmenu-ensure-position)
1970 (with-buffer-modified-unmodified
1971 (let ((inhibit-read-only t
))
1975 (bookmark-bmenu-ensure-position))))
1978 (defun bookmark-bmenu-delete-backwards ()
1979 "Mark bookmark on this line to be deleted, then move up one line.
1980 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1982 (bookmark-bmenu-delete)
1984 (bookmark-bmenu-ensure-position)
1986 (bookmark-bmenu-ensure-position))
1989 (defun bookmark-bmenu-execute-deletions ()
1990 "Delete bookmarks flagged `D'."
1992 (message "Deleting bookmarks...")
1993 (let ((o-point (point))
1994 (o-str (save-excursion
1996 (unless (looking-at "^D")
1999 (progn (end-of-line) (point))))))
2000 (o-col (current-column)))
2001 (goto-char (point-min))
2003 (while (re-search-forward "^D" (point-max) t
)
2004 (bookmark-delete (bookmark-bmenu-bookmark) t
)) ; pass BATCH arg
2005 (bookmark-bmenu-list)
2008 (goto-char (point-min))
2009 (search-forward o-str
)
2011 (forward-char o-col
))
2012 (goto-char o-point
))
2014 (message "Deleting bookmarks...done")
2018 (defun bookmark-bmenu-rename ()
2019 "Rename bookmark on current line. Prompts for a new name."
2021 (let ((bmrk (bookmark-bmenu-bookmark))
2022 (thispoint (point)))
2023 (bookmark-rename bmrk
)
2024 (goto-char thispoint
)))
2027 (defun bookmark-bmenu-locate ()
2028 "Display location of this bookmark. Displays in the minibuffer."
2030 (let ((bmrk (bookmark-bmenu-bookmark)))
2031 (message "%s" (bookmark-location bmrk
))))
2033 (defun bookmark-bmenu-relocate ()
2034 "Change the file path of the bookmark on the current line,
2035 prompting with completion for the new path."
2037 (let ((bmrk (bookmark-bmenu-bookmark))
2038 (thispoint (point)))
2039 (bookmark-relocate bmrk
)
2040 (goto-char thispoint
)))
2042 ;;; Bookmark-bmenu search
2044 ;; Store keyboard input for incremental search.
2045 (defvar bookmark-search-pattern
)
2047 (defun bookmark-read-search-input ()
2048 "Read each keyboard input and add it to `bookmark-search-pattern'."
2049 (let ((prompt (propertize "Pattern: " 'face
'minibuffer-prompt
))
2050 ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
2053 (let ((char (read-key (concat prompt bookmark-search-pattern
))))
2055 ((?\e ?
\r) nil
) ; RET or ESC break the search loop.
2056 (?\C-g
(setq bookmark-quit-flag t
) nil
)
2057 (?\d
(pop tmp-list
) t
) ; Delete last char of pattern with DEL
2059 (if (characterp char
)
2060 (push char tmp-list
)
2061 (setq unread-command-events
2062 (nconc (mapcar 'identity
2063 (this-single-command-raw-keys))
2064 unread-command-events
))
2066 (setq bookmark-search-pattern
2067 (apply 'string
(reverse tmp-list
))))))
2070 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2071 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2072 (let ((bookmark-alist
2073 (loop for i in bookmark-alist
2074 when
(string-match regexp
(car i
)) collect i into new
2075 finally return new
)))
2076 (bookmark-bmenu-list)))
2080 (defun bookmark-bmenu-search ()
2081 "Incremental search of bookmarks, hiding the non-matches as we go."
2083 (let ((bmk (bookmark-bmenu-bookmark))
2084 (bookmark-search-pattern "")
2085 (timer (run-with-idle-timer
2086 bookmark-search-delay
'repeat
2088 (bookmark-bmenu-filter-alist-by-regexp
2089 bookmark-search-pattern
)))))
2091 (bookmark-read-search-input)
2092 (cancel-timer timer
)
2094 (when bookmark-quit-flag
; C-g hit restore menu list.
2095 (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk
))
2096 (setq bookmark-quit-flag nil
))))
2098 (defun bookmark-bmenu-goto-bookmark (name)
2099 "Move point to bookmark with name NAME."
2100 (goto-char (point-min))
2101 (while (not (equal name
(bookmark-bmenu-bookmark)))
2107 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2109 (defun bookmark-menu-popup-paned-menu (event name entries
)
2110 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2111 That is, ENTRIES is a list of strings which appear as the choices
2113 The number of panes depends on the number of entries.
2114 The visible entries are truncated to `bookmark-menu-length', but the
2115 strings returned are not."
2116 (let ((f-height (/ (frame-height) 2))
2122 (while (and (< count f-height
) entries
)
2123 (let ((str (car entries
)))
2125 (if (> (length str
) bookmark-menu-length
)
2126 (substring str
0 bookmark-menu-length
)
2130 (setq entries
(cdr entries
))
2131 (setq count
(1+ count
))))
2132 (setq iter
(1+ iter
))
2134 (format "-*- %s (%d) -*-" name iter
)
2138 ;; Popup the menu and return the string.
2139 (x-popup-menu event
(cons (concat "-*- " name
" -*-")
2140 (nreverse pane-list
)))))
2143 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2144 ;; following works, and for explaining what to do to make it work.
2146 ;; We MUST autoload EACH form used to set up this variable's value, so
2147 ;; that the whole job is done in loaddefs.el.
2149 ;; Emacs menubar stuff.
2152 (defvar menu-bar-bookmark-map
2153 (let ((map (make-sparse-keymap "Bookmark functions")))
2154 (define-key map
[load]
2155 `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
2156 :help ,(purecopy "Load bookmarks from a bookmark file)")))
2157 (define-key map [write]
2158 `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
2159 :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
2160 (define-key map [save]
2161 `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
2162 :help ,(purecopy "Save currently defined bookmarks")))
2163 (define-key map [edit]
2164 `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
2165 :help ,(purecopy "Display a list of existing bookmarks")))
2166 (define-key map [delete]
2167 `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
2168 :help ,(purecopy "Delete a bookmark from the bookmark list")))
2169 (define-key map [rename]
2170 `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
2171 :help ,(purecopy "Change the name of a bookmark")))
2172 (define-key map [locate]
2173 `(menu-item ,(purecopy "Insert Location...") bookmark-locate
2174 :help ,(purecopy "Insert the name of the file associated with a bookmark")))
2175 (define-key map [insert]
2176 `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
2177 :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
2178 (define-key map [set]
2179 `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
2180 :help ,(purecopy "Set a bookmark named inside a file.")))
2181 (define-key map [jump]
2182 `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
2183 :help ,(purecopy "Jump to a bookmark (a point in some file)")))
2187 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2189 ;; make bookmarks appear toward the right side of the menu.
2190 (if (boundp 'menu-bar-final-items)
2191 (if menu-bar-final-items
2192 (push 'bookmark menu-bar-final-items))
2193 (setq menu-bar-final-items '(bookmark)))
2195 ;;;; end bookmark menu stuff ;;;;
2199 (defvar bookmark-load-hook nil
2200 "Hook run at the end of loading library `bookmark.el'.")
2202 ;; Exit Hook, called from kill-emacs-hook
2203 (defvar bookmark-exit-hook nil
2204 "Hook run when Emacs exits.")
2206 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2208 (defun bookmark-exit-hook-internal ()
2209 "Save bookmark state, if necessary, at Emacs exit time.
2210 This also runs `bookmark-exit-hook'."
2211 (run-hooks 'bookmark-exit-hook)
2213 (bookmark-time-to-save-p t)
2216 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2218 (defun bookmark-unload-function ()
2219 "Unload the Bookmark library."
2220 (when bookmark-save-flag (bookmark-save))
2221 ;; continue standard unloading
2225 (run-hooks 'bookmark-load-hook)
2229 ;;; bookmark.el ends here