(bookmark-bmenu-search): Clear echo area when exiting.
[emacs.git] / lisp / bookmark.el
blob03d0c585a8f6ea15c8f475fb0637caf4346ce23c
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)
199 ;; FIXME: Is it really worth a customization option?
200 (defcustom bookmark-search-delay 0.2
201 "Time before `bookmark-bmenu-search' updates the display."
202 :group 'bookmark
203 :type 'integer)
205 (defface bookmark-menu-heading
206 '((t (:inherit font-lock-type-face)))
207 "Face used to highlight the heading in bookmark menu buffers."
208 :group 'bookmark
209 :version "22.1")
212 ;;; No user-serviceable parts beyond this point.
214 ;; Added for lucid emacs compatibility, db
215 (or (fboundp 'defalias) (fset 'defalias 'fset))
217 ;; suggested for lucid compatibility by david hughes:
218 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
221 ;;; Keymap stuff:
223 ;; Set up these bindings dumping time *only*;
224 ;; if the user alters them, don't override the user when loading bookmark.el.
226 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
227 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
228 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
230 ;;;###autoload
231 (defvar bookmark-map
232 (let ((map (make-sparse-keymap)))
233 ;; Read the help on all of these functions for details...
234 (define-key map "x" 'bookmark-set)
235 (define-key map "m" 'bookmark-set) ;"m"ark
236 (define-key map "j" 'bookmark-jump)
237 (define-key map "g" 'bookmark-jump) ;"g"o
238 (define-key map "o" 'bookmark-jump-other-window)
239 (define-key map "i" 'bookmark-insert)
240 (define-key map "e" 'edit-bookmarks)
241 (define-key map "f" 'bookmark-insert-location) ;"f"ind
242 (define-key map "r" 'bookmark-rename)
243 (define-key map "d" 'bookmark-delete)
244 (define-key map "l" 'bookmark-load)
245 (define-key map "w" 'bookmark-write)
246 (define-key map "s" 'bookmark-save)
247 map)
248 "Keymap containing bindings to bookmark functions.
249 It is not bound to any key by default: to bind it
250 so that you have a bookmark prefix, just use `global-set-key' and bind a
251 key of your choice to `bookmark-map'. All interactive bookmark
252 functions have a binding in this keymap.")
254 ;;;###autoload (fset 'bookmark-map bookmark-map)
257 ;;; Core variables and data structures:
258 (defvar bookmark-alist ()
259 "Association list of bookmarks and their records.
260 Bookmark functions update the value automatically.
261 You probably do NOT want to change the value yourself.
263 The value is an alist with entries of the form
265 (BOOKMARK-NAME . PARAM-ALIST)
267 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
269 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
271 PARAM-ALIST is an alist of bookmark information. The order of the
272 entries in PARAM-ALIST is not important. The possible entries are
273 described below. An entry with a key but null value means the entry
274 is not used.
276 (filename . FILENAME)
277 (position . POS)
278 (front-context-string . STR-AFTER-POS)
279 (rear-context-string . STR-BEFORE-POS)
280 (handler . HANDLER)
281 (annotation . ANNOTATION)
283 FILENAME names the bookmarked file.
284 POS is the bookmarked buffer position (position in the file).
285 STR-AFTER-POS is buffer text that immediately follows POS.
286 STR-BEFORE-POS is buffer text that immediately precedes POS.
287 ANNOTATION is a string that describes the bookmark.
288 See options `bookmark-use-annotations' and
289 `bookmark-automatically-show-annotations'.
290 HANDLER is a function that provides the bookmark-jump behavior for a
291 specific kind of bookmark. This is the case for Info bookmarks,
292 for instance. HANDLER must accept a bookmark as argument.")
294 (defvar bookmarks-already-loaded nil
295 "Non-nil iff bookmarks have been loaded from `bookmark-default-file'.")
298 ;; more stuff added by db.
300 (defvar bookmark-current-bookmark nil
301 "Name of bookmark most recently used in the current file.
302 It is buffer local, used to make moving a bookmark forward
303 through a file easier.")
305 (make-variable-buffer-local 'bookmark-current-bookmark)
308 (defvar bookmark-alist-modification-count 0
309 "Number of modifications to bookmark list since it was last saved.")
312 (defvar bookmark-search-size 16
313 "Length of the context strings recorded on either side of a bookmark.")
316 (defvar bookmark-current-buffer nil
317 "The buffer in which a bookmark is currently being set or renamed.
318 Functions that insert strings into the minibuffer use this to know
319 the source buffer for that information; see `bookmark-yank-word' and
320 `bookmark-insert-current-bookmark' for example.")
323 (defvar bookmark-yank-point 0
324 "The next point from which to pull source text for `bookmark-yank-word'.
325 This point is in `bookmark-curent-buffer'.")
328 (defvar bookmark-quit-flag nil
329 "Non nil make `bookmark-bmenu-search' quit immediately.")
331 ;; Helper functions.
333 ;; Only functions on this page and the next one (file formats) need to
334 ;; know anything about the format of bookmark-alist entries.
335 ;; Everyone else should go through them.
338 (defun bookmark-name-from-full-record (full-record)
339 "Return name of FULL-RECORD \(an alist element instead of a string\)."
340 (car full-record))
343 (defun bookmark-all-names ()
344 "Return a list of all current bookmark names."
345 (bookmark-maybe-load-default-file)
346 (mapcar 'bookmark-name-from-full-record bookmark-alist))
349 (defun bookmark-get-bookmark (bookmark &optional noerror)
350 "Return the bookmark record corresponding to BOOKMARK.
351 If BOOKMARK is a string, look for the corresponding bookmark record in
352 `bookmark-alist'; return it if found, otherwise error. Else if
353 BOOKMARK is already a bookmark record, just return it."
354 (cond
355 ((consp bookmark) bookmark)
356 ((stringp bookmark)
357 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
358 (unless noerror (error "Invalid bookmark %s" bookmark))))))
361 (defun bookmark-get-bookmark-record (bookmark)
362 "Return the record portion of the entry for BOOKMARK in
363 `bookmark-alist' (that is, all information but the name).
364 BOOKMARK may be a bookmark name (a string) or a bookmark record."
365 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
366 ;; The bookmark objects can either look like (NAME ALIST) or
367 ;; (NAME . ALIST), so we have to distinguish the two here.
368 (if (and (null (cdr alist)) (consp (caar alist)))
369 (car alist) alist)))
372 (defun bookmark-set-name (bookmark newname)
373 "Set BOOKMARK's name to NEWNAME.
374 BOOKMARK may be a bookmark name (a string) or a bookmark record."
375 (setcar
376 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
377 newname))
379 (defun bookmark-prop-get (bookmark prop)
380 "Return the property PROP of BOOKMARK, or nil if none.
381 BOOKMARK may be a bookmark name (a string) or a bookmark record."
382 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
384 (defun bookmark-prop-set (bookmark prop val)
385 "Set the property PROP of BOOKMARK to VAL.
386 BOOKMARK may be a bookmark name (a string) or a bookmark record."
387 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
388 (if cell
389 (setcdr cell val)
390 (nconc (bookmark-get-bookmark-record bookmark)
391 (list (cons prop val))))))
393 (defun bookmark-get-annotation (bookmark)
394 "Return the annotation of BOOKMARK, or nil if none.
395 BOOKMARK may be a bookmark name (a string) or a bookmark record."
396 (bookmark-prop-get bookmark 'annotation))
398 (defun bookmark-set-annotation (bookmark ann)
399 "Set the annotation of BOOKMARK to ANN.
400 BOOKMARK may be a bookmark name (a string) or a bookmark record."
401 (bookmark-prop-set bookmark 'annotation ann))
404 (defun bookmark-get-filename (bookmark)
405 "Return the full filename of BOOKMARK, or nil if none.
406 BOOKMARK may be a bookmark name (a string) or a bookmark record."
407 (bookmark-prop-get bookmark 'filename))
410 (defun bookmark-set-filename (bookmark filename)
411 "Set the full filename of BOOKMARK to FILENAME.
412 BOOKMARK may be a bookmark name (a string) or a bookmark record."
413 (bookmark-prop-set bookmark 'filename filename))
416 (defun bookmark-get-position (bookmark)
417 "Return the position \(i.e.: point\) of BOOKMARK, or nil if none.
418 BOOKMARK may be a bookmark name (a string) or a bookmark record."
419 (bookmark-prop-get bookmark 'position))
422 (defun bookmark-set-position (bookmark position)
423 "Set the position \(i.e.: point\) of BOOKMARK to POSITION.
424 BOOKMARK may be a bookmark name (a string) or a bookmark record."
425 (bookmark-prop-set bookmark 'position position))
428 (defun bookmark-get-front-context-string (bookmark)
429 "Return the front-context-string of BOOKMARK, or nil if none.
430 BOOKMARK may be a bookmark name (a string) or a bookmark record."
431 (bookmark-prop-get bookmark 'front-context-string))
434 (defun bookmark-set-front-context-string (bookmark string)
435 "Set the front-context-string of BOOKMARK to STRING.
436 BOOKMARK may be a bookmark name (a string) or a bookmark record."
437 (bookmark-prop-set bookmark 'front-context-string string))
440 (defun bookmark-get-rear-context-string (bookmark)
441 "Return the rear-context-string of BOOKMARK, or nil if none.
442 BOOKMARK may be a bookmark name (a string) or a bookmark record."
443 (bookmark-prop-get bookmark 'rear-context-string))
446 (defun bookmark-set-rear-context-string (bookmark string)
447 "Set the rear-context-string of BOOKMARK to STRING.
448 BOOKMARK may be a bookmark name (a string) or a bookmark record."
449 (bookmark-prop-set bookmark 'rear-context-string string))
452 (defun bookmark-get-handler (bookmark)
453 "Return the handler function for BOOKMARK, or nil if none.
454 BOOKMARK may be a bookmark name (a string) or a bookmark record."
455 (bookmark-prop-get bookmark 'handler))
457 (defvar bookmark-history nil
458 "The history list for bookmark functions.")
461 (defun bookmark-completing-read (prompt &optional default)
462 "Prompting with PROMPT, read a bookmark name in completion.
463 PROMPT will get a \": \" stuck on the end no matter what, so you
464 probably don't want to include one yourself.
465 Optional second arg DEFAULT is a string to return if the user enters
466 the empty string."
467 (bookmark-maybe-load-default-file) ; paranoia
468 (if (listp last-nonmenu-event)
469 (bookmark-menu-popup-paned-menu t prompt
470 (if bookmark-sort-flag
471 (sort (bookmark-all-names)
472 'string-lessp)
473 (bookmark-all-names)))
474 (let* ((completion-ignore-case bookmark-completion-ignore-case)
475 (default default)
476 (prompt (concat prompt (if default
477 (format " (%s): " default)
478 ": ")))
479 (str
480 (completing-read prompt
481 bookmark-alist
485 'bookmark-history)))
486 (if (string-equal "" str) default str))))
489 (defmacro bookmark-maybe-historicize-string (string)
490 "Put STRING into the bookmark prompt history, if caller non-interactive.
491 We need this because sometimes bookmark functions are invoked from
492 menus, so `completing-read' never gets a chance to set `bookmark-history'."
493 `(or
494 (called-interactively-p 'interactive)
495 (setq bookmark-history (cons ,string bookmark-history))))
497 (defvar bookmark-make-record-function 'bookmark-make-record-default
498 "A function that should be called to create a bookmark record.
499 Modes may set this variable buffer-locally to enable bookmarking of
500 locations that should be treated specially, such as Info nodes,
501 news posts, images, pdf documents, etc.
503 The function will be called with no arguments.
504 It should signal a user error if it is unable to construct a record for
505 the current location.
507 The returned record should be a cons cell of the form (NAME . ALIST)
508 where ALIST is as described in `bookmark-alist' and may typically contain
509 a special cons (handler . HANDLER-FUNC) which specifies the handler function
510 that should be used instead of `bookmark-default-handler' to open this
511 bookmark. See the documentation for `bookmark-alist' for more.
513 NAME is a suggested name for the constructed bookmark. It can be nil
514 in which case a default heuristic will be used. The function can also
515 equivalently just return ALIST without NAME.")
517 (defun bookmark-make-record ()
518 "Return a new bookmark record (NAME . ALIST) for the current location."
519 (let ((record (funcall bookmark-make-record-function)))
520 ;; Set up default name.
521 (if (stringp (car record))
522 ;; The function already provided a default name.
523 record
524 (if (car record) (push nil record))
525 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
526 record)))
528 (defun bookmark-store (name alist no-overwrite)
529 "Store the bookmark NAME with data ALIST.
530 If NO-OVERWRITE is non-nil and another bookmark of the same name already
531 exists in `bookmark-alist', record the new bookmark without throwing away the
532 old one."
533 (bookmark-maybe-load-default-file)
534 (let ((stripped-name (copy-sequence name)))
535 (or (featurep 'xemacs)
536 ;; XEmacs's `set-text-properties' doesn't work on
537 ;; free-standing strings, apparently.
538 (set-text-properties 0 (length stripped-name) nil stripped-name))
539 (if (and (not no-overwrite)
540 (bookmark-get-bookmark stripped-name 'noerror))
541 ;; already existing bookmark under that name and
542 ;; no prefix arg means just overwrite old bookmark
543 ;; Use the new (NAME . ALIST) format.
544 (setcdr (bookmark-get-bookmark stripped-name) alist)
546 ;; otherwise just cons it onto the front (either the bookmark
547 ;; doesn't exist already, or there is no prefix arg. In either
548 ;; case, we want the new bookmark consed onto the alist...)
550 (push (cons stripped-name alist) bookmark-alist))
552 ;; Added by db
553 (setq bookmark-current-bookmark stripped-name)
554 (setq bookmark-alist-modification-count
555 (1+ bookmark-alist-modification-count))
556 (if (bookmark-time-to-save-p)
557 (bookmark-save))
559 (setq bookmark-current-bookmark stripped-name)
560 (bookmark-bmenu-surreptitiously-rebuild-list)))
562 (defun bookmark-make-record-default (&optional point-only)
563 "Return the record describing the location of a new bookmark.
564 Must be at the correct position in the buffer in which the bookmark is
565 being set.
566 If POINT-ONLY is non-nil, then only return the subset of the
567 record that pertains to the location within the buffer."
568 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
569 (front-context-string
570 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
571 (buffer-substring-no-properties
572 (point)
573 (+ (point) bookmark-search-size))
574 nil))
575 (rear-context-string
576 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
577 (buffer-substring-no-properties
578 (point)
579 (- (point) bookmark-search-size))
580 nil))
581 (position . ,(point))))
584 ;;; File format stuff
586 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
587 ;; the bookmark file format -- please don't. The current format
588 ;; should be extensible enough. If you feel the need to change it,
589 ;; please discuss it with other Emacs developers first.
591 ;; The format of `bookmark-alist' has changed twice in its lifetime.
592 ;; This comment describes the three formats, FIRST, SECOND, and
593 ;; CURRENT.
595 ;; The FIRST format was used prior to Emacs 20:
597 ;; ((BOOKMARK-NAME (FILENAME
598 ;; STRING-IN-FRONT
599 ;; STRING-BEHIND
600 ;; POINT))
601 ;; ...)
603 ;; The SECOND format was introduced in Emacs 20:
605 ;; ((BOOKMARK-NAME ((filename . FILENAME)
606 ;; (position . POS)
607 ;; (front-context-string . STR-AFTER-POS)
608 ;; (rear-context-string . STR-BEFORE-POS)
609 ;; (annotation . ANNOTATION)
610 ;; (whatever . VALUE)
611 ;; ...
612 ;; ))
613 ;; ...)
615 ;; The CURRENT format was introduced in Emacs 22:
617 ;; ((BOOKMARK-NAME (filename . FILENAME)
618 ;; (position . POS)
619 ;; (front-context-string . STR-AFTER-POS)
620 ;; (rear-context-string . STR-BEFORE-POS)
621 ;; (annotation . ANNOTATION)
622 ;; (whatever . VALUE)
623 ;; ...
624 ;; )
625 ;; ...)
627 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
628 ;; bookmark record is a list of entry information. FIRST and SECOND
629 ;; differ in the form of the record information: FIRST uses a list of
630 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
631 ;; the list elements matters. In the SECOND format, the order of the
632 ;; alist elements is unimportant. The SECOND format facilitates the
633 ;; addition of new kinds of elements, to support new kinds of
634 ;; bookmarks or code evolution.
636 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
637 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
638 ;; no longer a cons. Why that change was made remains a mystery --
639 ;; just be aware of it. (Be aware too that this explanatory comment
640 ;; was incorrect in Emacs 22 and Emacs 23.1.)
642 ;; To deal with the change from FIRST format to SECOND, conversion
643 ;; code was added, and it is still in use. See
644 ;; `bookmark-maybe-upgrade-file-format'.
646 ;; No conversion from SECOND to CURRENT is done. Instead, the code
647 ;; handles both formats OK. It must continue to do so.
649 ;; See the doc string of `bookmark-alist' for information about the
650 ;; elements that define a bookmark (e.g. `filename').
653 (defconst bookmark-file-format-version 1
654 "The current version of the format used by bookmark files.
655 You should never need to change this.")
658 (defconst bookmark-end-of-version-stamp-marker
659 "-*- End Of Bookmark File Format Version Stamp -*-\n"
660 "This string marks the end of the version stamp in a bookmark file.")
663 (defun bookmark-alist-from-buffer ()
664 "Return a `bookmark-alist' (in any format) from the current buffer.
665 The buffer must of course contain bookmark format information.
666 Does not care from where in the buffer it is called, and does not
667 affect point."
668 (save-excursion
669 (goto-char (point-min))
670 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
671 (read (current-buffer))
672 ;; Else we're dealing with format version 0
673 (if (search-forward "(" nil t)
674 (progn
675 (forward-char -1)
676 (read (current-buffer)))
677 ;; Else no hope of getting information here.
678 (error "Not bookmark format")))))
681 (defun bookmark-upgrade-version-0-alist (old-list)
682 "Upgrade a version 0 alist OLD-LIST to the current version."
683 (mapcar
684 (lambda (bookmark)
685 (let* ((name (car bookmark))
686 (record (car (cdr bookmark)))
687 (filename (nth 0 record))
688 (front-str (nth 1 record))
689 (rear-str (nth 2 record))
690 (position (nth 3 record))
691 (ann (nth 4 record)))
692 (list
693 name
694 `((filename . ,filename)
695 (front-context-string . ,(or front-str ""))
696 (rear-context-string . ,(or rear-str ""))
697 (position . ,position)
698 (annotation . ,ann)))))
699 old-list))
702 (defun bookmark-upgrade-file-format-from-0 ()
703 "Upgrade a bookmark file of format 0 (the original format) to format 1.
704 This expects to be called from `point-min' in a bookmark file."
705 (message "Upgrading bookmark format from 0 to %d..."
706 bookmark-file-format-version)
707 (let* ((old-list (bookmark-alist-from-buffer))
708 (new-list (bookmark-upgrade-version-0-alist old-list)))
709 (delete-region (point-min) (point-max))
710 (bookmark-insert-file-format-version-stamp)
711 (pp new-list (current-buffer))
712 (save-buffer))
713 (goto-char (point-min))
714 (message "Upgrading bookmark format from 0 to %d...done"
715 bookmark-file-format-version)
719 (defun bookmark-grok-file-format-version ()
720 "Return an integer which is the file-format version of this bookmark file.
721 This expects to be called from `point-min' in a bookmark file."
722 (if (looking-at "^;;;;")
723 (save-excursion
724 (save-match-data
725 (re-search-forward "[0-9]")
726 (forward-char -1)
727 (read (current-buffer))))
728 ;; Else this is format version 0, the original one, which didn't
729 ;; even have version stamps.
733 (defun bookmark-maybe-upgrade-file-format ()
734 "Check the file-format version of this bookmark file.
735 If the version is not up-to-date, upgrade it automatically.
736 This expects to be called from `point-min' in a bookmark file."
737 (let ((version (bookmark-grok-file-format-version)))
738 (cond
739 ((= version bookmark-file-format-version)
740 ) ; home free -- version is current
741 ((= version 0)
742 (bookmark-upgrade-file-format-from-0))
744 (error "Bookmark file format version strangeness")))))
747 (defun bookmark-insert-file-format-version-stamp ()
748 "Insert text indicating current version of bookmark file format."
749 (insert
750 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
751 bookmark-file-format-version))
752 (insert ";;; This format is meant to be slightly human-readable;\n"
753 ";;; nevertheless, you probably don't want to edit it.\n"
754 ";;; "
755 bookmark-end-of-version-stamp-marker))
758 ;;; end file-format stuff
761 ;;; Generic helpers.
763 (defun bookmark-maybe-message (fmt &rest args)
764 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
765 (if (>= baud-rate 9600)
766 (apply 'message fmt args)))
769 ;;; Core code:
771 (defvar bookmark-minibuffer-read-name-map
772 (let ((map (make-sparse-keymap)))
773 (set-keymap-parent map minibuffer-local-map)
774 (define-key map "\C-w" 'bookmark-yank-word)
775 ;; This C-u binding might not be very useful any more now that we
776 ;; provide access to the default via the standard M-n binding.
777 ;; Maybe we should just remove it? --Stef-08
778 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
779 map))
781 ;;;###autoload
782 (defun bookmark-set (&optional name no-overwrite)
783 "Set a bookmark named NAME at the current location.
784 If name is nil, then prompt the user.
786 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
787 existing bookmark that has the same name as NAME, but instead push the
788 new bookmark onto the bookmark alist. The most recently set bookmark
789 with name NAME is thus the one in effect at any given time, but the
790 others are still there, should the user decide to delete the most
791 recent one.
793 To yank words from the text of the buffer and use them as part of the
794 bookmark name, type C-w while setting a bookmark. Successive C-w's
795 yank successive words.
797 Typing C-u inserts (at the bookmark name prompt) the name of the last
798 bookmark used in the document where the new bookmark is being set;
799 this helps you use a single bookmark name to track progress through a
800 large document. If there is no prior bookmark for this document, then
801 C-u inserts an appropriate name based on the buffer or file.
803 Use \\[bookmark-delete] to remove bookmarks \(you give it a name and
804 it removes only the first instance of a bookmark with that name from
805 the list of bookmarks.\)"
806 (interactive (list nil current-prefix-arg))
807 (let* ((record (bookmark-make-record))
808 (default (car record)))
810 (bookmark-maybe-load-default-file)
812 (setq bookmark-yank-point (point))
813 (setq bookmark-current-buffer (current-buffer))
815 (let ((str
816 (or name
817 (read-from-minibuffer
818 (format "Set bookmark (%s): " default)
820 bookmark-minibuffer-read-name-map
821 nil nil default))))
822 (and (string-equal str "") (setq str default))
823 (bookmark-store str (cdr record) no-overwrite)
825 ;; Ask for an annotation buffer for this bookmark
826 (when bookmark-use-annotations
827 (bookmark-edit-annotation str)))))
829 (defun bookmark-kill-line (&optional newline-too)
830 "Kill from point to end of line.
831 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
832 Does not affect the kill ring."
833 (let ((eol (save-excursion (end-of-line) (point))))
834 (delete-region (point) eol)
835 (if (and newline-too (looking-at "\n"))
836 (delete-char 1))))
839 ;; Defvars to avoid compilation warnings:
840 (defvar bookmark-annotation-name nil
841 "Variable holding the name of the bookmark.
842 This is used in `bookmark-edit-annotation' to record the bookmark
843 whose annotation is being edited.")
846 (defun bookmark-default-annotation-text (bookmark)
847 "Return default annotation text for BOOKMARK (a string, not a record).
848 The default annotation text is simply some text explaining how to use
849 annotations."
850 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
851 "# All lines which start with a '#' will be deleted.\n"
852 "# Type C-c C-c when done.\n#\n"
853 "# Author: " (user-full-name) " <" (user-login-name) "@"
854 (system-name) ">\n"
855 "# Date: " (current-time-string) "\n"))
858 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
859 "Function to return default text to use for a bookmark annotation.
860 It takes one argument, the name of the bookmark, as a string.")
861 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
862 'bookmark-edit-annotation-text-func "23.1")
864 (defvar bookmark-edit-annotation-mode-map
865 (let ((map (make-sparse-keymap)))
866 (set-keymap-parent map text-mode-map)
867 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
868 map)
869 "Keymap for editing an annotation of a bookmark.")
872 (defun bookmark-edit-annotation-mode (bookmark)
873 "Mode for editing the annotation of bookmark BOOKMARK.
874 When you have finished composing, type \\[bookmark-send-annotation].
876 BOOKMARK is a bookmark name (a string) or a bookmark record.
878 \\{bookmark-edit-annotation-mode-map}"
879 (interactive)
880 (kill-all-local-variables)
881 (make-local-variable 'bookmark-annotation-name)
882 (setq bookmark-annotation-name bookmark)
883 (use-local-map bookmark-edit-annotation-mode-map)
884 (setq major-mode 'bookmark-edit-annotation-mode
885 mode-name "Edit Bookmark Annotation")
886 (insert (funcall bookmark-edit-annotation-text-func bookmark))
887 (let ((annotation (bookmark-get-annotation bookmark)))
888 (if (and annotation (not (string-equal annotation "")))
889 (insert annotation)))
890 (run-mode-hooks 'text-mode-hook))
893 (defun bookmark-send-edited-annotation ()
894 "Use buffer contents as annotation for a bookmark.
895 Lines beginning with `#' are ignored."
896 (interactive)
897 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
898 (error "Not in bookmark-edit-annotation-mode"))
899 (goto-char (point-min))
900 (while (< (point) (point-max))
901 (if (looking-at "^#")
902 (bookmark-kill-line t)
903 (forward-line 1)))
904 ;; Take no chances with text properties.
905 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
906 (bookmark bookmark-annotation-name))
907 (bookmark-set-annotation bookmark annotation)
908 (bookmark-bmenu-surreptitiously-rebuild-list))
909 (kill-buffer (current-buffer)))
912 (defun bookmark-edit-annotation (bookmark)
913 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
914 BOOKMARK is a bookmark name (a string) or a bookmark record."
915 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
916 (bookmark-edit-annotation-mode bookmark))
919 (defun bookmark-insert-current-bookmark ()
920 "Insert into the bookmark name currently being set the value of
921 `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
922 to the buffer's file name if `bookmark-current-bookmark' is nil."
923 (interactive)
924 (let ((str
925 (with-current-buffer bookmark-current-buffer
926 (or bookmark-current-bookmark
927 (bookmark-buffer-name)))))
928 (insert str)))
931 (defun bookmark-buffer-name ()
932 "Return the name of the current buffer in a form usable as a bookmark name.
933 If the buffer is associated with a file or directory, use that name."
934 (cond
935 ;; Or are we a file?
936 (buffer-file-name (file-name-nondirectory buffer-file-name))
937 ;; Or are we a directory?
938 ((and (boundp 'dired-directory) dired-directory)
939 (let* ((dirname (if (stringp dired-directory)
940 dired-directory
941 (car dired-directory)))
942 (idx (1- (length dirname))))
943 ;; Strip the trailing slash.
944 (if (= ?/ (aref dirname idx))
945 (file-name-nondirectory (substring dirname 0 idx))
946 ;; Else return the current-buffer
947 (buffer-name (current-buffer)))))
948 ;; If all else fails, use the buffer's name.
950 (buffer-name (current-buffer)))))
953 (defun bookmark-yank-word ()
954 "Get the next word from buffer `bookmark-current-buffer' and append
955 it to the name of the bookmark currently being set, advancing
956 `bookmark-yank-point' by one word."
957 (interactive)
958 (let ((string (with-current-buffer bookmark-current-buffer
959 (goto-char bookmark-yank-point)
960 (buffer-substring-no-properties
961 (point)
962 (progn
963 (forward-word 1)
964 (setq bookmark-yank-point (point)))))))
965 (insert string)))
967 (defun bookmark-buffer-file-name ()
968 "Return the current buffer's file in a way useful for bookmarks."
969 ;; Abbreviate the path, both so it's shorter and so it's more
970 ;; portable. E.g., the user's home dir might be a different
971 ;; path on different machines, but "~/" will still reach it.
972 (abbreviate-file-name
973 (cond
974 (buffer-file-name buffer-file-name)
975 ((and (boundp 'dired-directory) dired-directory)
976 (if (stringp dired-directory)
977 dired-directory
978 (car dired-directory)))
979 (t (error "Buffer not visiting a file or directory")))))
982 (defun bookmark-maybe-load-default-file ()
983 "If bookmarks have not been loaded from the default place, load them."
984 (and (not bookmarks-already-loaded)
985 (null bookmark-alist)
986 (prog2
987 (and
988 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
989 ;; to be renamed.
990 (file-exists-p (expand-file-name bookmark-old-default-file))
991 (not (file-exists-p (expand-file-name bookmark-default-file)))
992 (rename-file (expand-file-name bookmark-old-default-file)
993 (expand-file-name bookmark-default-file)))
994 ;; return t so the `and' will continue...
997 (file-readable-p (expand-file-name bookmark-default-file))
998 (bookmark-load bookmark-default-file t t)
999 (setq bookmarks-already-loaded t)))
1002 (defun bookmark-maybe-sort-alist ()
1003 "Return `bookmark-alist' for display.
1004 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
1005 (if bookmark-sort-flag
1006 (sort (copy-alist bookmark-alist)
1007 (function
1008 (lambda (x y) (string-lessp (car x) (car y)))))
1009 bookmark-alist))
1012 (defvar bookmark-after-jump-hook nil
1013 "Hook run after `bookmark-jump' jumps to a bookmark.
1014 Useful for example to unhide text in `outline-mode'.")
1016 (defun bookmark--jump-via (bookmark display-function)
1017 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
1018 Bookmark may be a bookmark name (a string) or a bookmark record.
1020 After calling DISPLAY-FUNCTION, set window point to the point specified
1021 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
1022 any annotations for this bookmark."
1023 (bookmark-handle-bookmark bookmark)
1024 (save-current-buffer
1025 (funcall display-function (current-buffer)))
1026 (let ((win (get-buffer-window (current-buffer) 0)))
1027 (if win (set-window-point win (point))))
1028 ;; FIXME: we used to only run bookmark-after-jump-hook in
1029 ;; `bookmark-jump' itself, but in none of the other commands.
1030 (run-hooks 'bookmark-after-jump-hook)
1031 (if bookmark-automatically-show-annotations
1032 ;; if there is an annotation for this bookmark,
1033 ;; show it in a buffer.
1034 (bookmark-show-annotation bookmark)))
1037 ;;;###autoload
1038 (defun bookmark-jump (bookmark &optional display-func)
1039 "Jump to bookmark BOOKMARK (a point in some file).
1040 You may have a problem using this function if the value of variable
1041 `bookmark-alist' is nil. If that happens, you need to load in some
1042 bookmarks. See help on function `bookmark-load' for more about
1043 this.
1045 If the file pointed to by BOOKMARK no longer exists, you will be asked
1046 if you wish to give the bookmark a new location, and `bookmark-jump'
1047 will then jump to the new location, as well as recording it in place
1048 of the old one in the permanent bookmark record.
1050 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1051 the latter is usually only used by programmatic callers.
1053 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1054 bookmark. It defaults to `switch-to-buffer'. A typical value for
1055 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1056 (interactive
1057 (list (bookmark-completing-read "Jump to bookmark"
1058 bookmark-current-bookmark)))
1059 (unless bookmark
1060 (error "No bookmark specified"))
1061 (bookmark-maybe-historicize-string bookmark)
1062 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1065 ;;;###autoload
1066 (defun bookmark-jump-other-window (bookmark)
1067 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1068 (interactive
1069 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1070 bookmark-current-bookmark)))
1071 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1074 (defun bookmark-jump-noselect (bookmark)
1075 "Return the location pointed to by the bookmark BOOKMARK.
1076 The return value has the form (BUFFER . POINT).
1078 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1080 Note: this function is deprecated and is present for Emacs 22
1081 compatibility only."
1082 (save-excursion
1083 (bookmark-handle-bookmark bookmark)
1084 (cons (current-buffer) (point))))
1086 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1088 (defun bookmark-handle-bookmark (bookmark)
1089 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1090 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1092 Changes current buffer and point and returns nil, or signals a `file-error'.
1094 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1095 that file no longer exists, then offer interactively to relocate BOOKMARK."
1096 (condition-case err
1097 (funcall (or (bookmark-get-handler bookmark)
1098 'bookmark-default-handler)
1099 (bookmark-get-bookmark bookmark))
1100 (file-error
1101 ;; We were unable to find the marked file, so ask if user wants to
1102 ;; relocate the bookmark, else remind them to consider deletion.
1103 (when (stringp bookmark)
1104 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1105 ;; or a bookmark object. If it's an object, we assume it's a
1106 ;; bookmark used internally by some other package.
1107 (let ((file (bookmark-get-filename bookmark)))
1108 (when file ;Don't know how to relocate if there's no `file'.
1109 ;; If file is not a dir, directory-file-name just returns file.
1110 (let ((display-name (directory-file-name file)))
1111 (ding)
1112 ;; Dialog boxes can accept a file target, but usually don't
1113 ;; know how to accept a directory target (at least, this
1114 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1115 ;; true on Windows as well). So we suppress file dialogs
1116 ;; when relocating.
1117 (let ((use-dialog-box nil)
1118 (use-file-dialog nil))
1119 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1120 bookmark "\"? "))
1121 (progn
1122 (bookmark-relocate bookmark)
1123 ;; Try again.
1124 (funcall (or (bookmark-get-handler bookmark)
1125 'bookmark-default-handler)
1126 (bookmark-get-bookmark bookmark)))
1127 (message
1128 "Bookmark not relocated; consider removing it \(%s\)."
1129 bookmark)
1130 (signal (car err) (cdr err))))))))))
1131 ;; Added by db.
1132 (when (stringp bookmark)
1133 (setq bookmark-current-bookmark bookmark))
1134 nil)
1136 (put 'bookmark-error-no-filename
1137 'error-conditions
1138 '(error bookmark-errors bookmark-error-no-filename))
1139 (put 'bookmark-error-no-filename
1140 'error-message
1141 "Bookmark has no associated file (or directory)")
1143 (defun bookmark-default-handler (bmk-record)
1144 "Default handler to jump to a particular bookmark location.
1145 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1146 Changes current buffer and point and returns nil, or signals a `file-error'."
1147 (let ((file (bookmark-get-filename bmk-record))
1148 (forward-str (bookmark-get-front-context-string bmk-record))
1149 (behind-str (bookmark-get-rear-context-string bmk-record))
1150 (place (bookmark-get-position bmk-record)))
1151 (if (not file)
1152 (signal 'bookmark-error-no-filename (list 'stringp file))
1153 (set-buffer (find-file-noselect file))
1154 (if place (goto-char place))
1155 ;; Go searching forward first. Then, if forward-str exists and
1156 ;; was found in the file, we can search backward for behind-str.
1157 ;; Rationale is that if text was inserted between the two in the
1158 ;; file, it's better to be put before it so you can read it,
1159 ;; rather than after and remain perhaps unaware of the changes.
1160 (if forward-str
1161 (if (search-forward forward-str (point-max) t)
1162 (goto-char (match-beginning 0))))
1163 (if behind-str
1164 (if (search-backward behind-str (point-min) t)
1165 (goto-char (match-end 0)))))
1166 nil))
1168 ;;;###autoload
1169 (defun bookmark-relocate (bookmark)
1170 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1171 BOOKMARK is a bookmark name (a string), not a bookmark record.
1173 This makes an already existing bookmark point to that file, instead of
1174 the one it used to point at. Useful when a file has been renamed
1175 after a bookmark was set in it."
1176 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1177 (bookmark-maybe-historicize-string bookmark)
1178 (bookmark-maybe-load-default-file)
1179 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1180 (newloc (expand-file-name
1181 (read-file-name
1182 (format "Relocate %s to: " bookmark)
1183 (file-name-directory bmrk-filename)))))
1184 (bookmark-set-filename bookmark newloc)
1185 (setq bookmark-alist-modification-count
1186 (1+ bookmark-alist-modification-count))
1187 (if (bookmark-time-to-save-p)
1188 (bookmark-save))
1189 (bookmark-bmenu-surreptitiously-rebuild-list)))
1192 ;;;###autoload
1193 (defun bookmark-insert-location (bookmark &optional no-history)
1194 "Insert the name of the file associated with BOOKMARK.
1195 BOOKMARK is a bookmark name (a string), not a bookmark record.
1197 Optional second arg NO-HISTORY means don't record this in the
1198 minibuffer history list `bookmark-history'."
1199 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1200 (or no-history (bookmark-maybe-historicize-string bookmark))
1201 (let ((start (point)))
1202 (prog1
1203 (insert (bookmark-location bookmark)) ; *Return this line*
1204 (if (display-mouse-p)
1205 (add-text-properties
1206 start
1207 (save-excursion (re-search-backward
1208 "[^ \t]")
1209 (1+ (point)))
1210 '(mouse-face highlight
1211 follow-link t
1212 help-echo "mouse-2: go to this bookmark in other window"))))))
1214 ;;;###autoload
1215 (defalias 'bookmark-locate 'bookmark-insert-location)
1217 (defun bookmark-location (bookmark)
1218 "Return the name of the file associated with BOOKMARK, or nil if none.
1219 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1220 (bookmark-maybe-load-default-file)
1221 (bookmark-get-filename bookmark))
1224 ;;;###autoload
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
1238 name."
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))
1245 (let ((newname
1246 (or new ; use second arg, if non-nil
1247 (read-from-minibuffer
1248 "New name: "
1250 (let ((now-map (copy-keymap minibuffer-local-map)))
1251 (define-key now-map "\C-w" 'bookmark-yank-word)
1252 now-map)
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)
1261 (bookmark-save))))
1264 ;;;###autoload
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
1272 this."
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))
1277 (str-to-insert
1278 (save-current-buffer
1279 (bookmark-handle-bookmark bookmark)
1280 (buffer-string))))
1281 (insert str-to-insert)
1282 (push-mark)
1283 (goto-char orig-point)))
1286 ;;;###autoload
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."
1297 (interactive
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)))
1308 (unless batch
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)
1313 (bookmark-save)))
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>
1322 (cond (final-time
1323 (and (> bookmark-alist-modification-count 0)
1324 bookmark-save-flag))
1325 ((numberp bookmark-save-flag)
1326 (>= bookmark-alist-modification-count bookmark-save-flag))
1328 nil)))
1331 ;;;###autoload
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."
1335 (interactive)
1336 (bookmark-maybe-load-default-file)
1337 (bookmark-save t))
1340 ;;;###autoload
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
1345 \(second argument\).
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'."
1357 (interactive "P")
1358 (bookmark-maybe-load-default-file)
1359 (cond
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)
1386 (print-level nil))
1387 (bookmark-insert-file-format-version-stamp)
1388 (insert "(")
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)))
1393 (insert ")")
1394 (let ((version-control
1395 (cond
1396 ((null bookmark-version-control) nil)
1397 ((eq 'never bookmark-version-control) 'never)
1398 ((eq 'nospecial bookmark-version-control) version-control)
1399 (t t))))
1400 (condition-case nil
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
1425 (let ((count 2)
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)))))
1433 ;;;###autoload
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
1439 while loading.
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
1446 explicitly.
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."
1452 (interactive
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 (expand-file-name file))
1461 (if (not (file-readable-p file))
1462 (error "Cannot read bookmark file %s" file)
1463 (if (null no-msg)
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)))
1470 (if (listp blist)
1471 (progn
1472 (if overwrite
1473 (progn
1474 (setq bookmark-alist blist)
1475 (setq bookmark-alist-modification-count 0))
1476 ;; else
1477 (bookmark-import-new-list blist)
1478 (setq bookmark-alist-modification-count
1479 (1+ bookmark-alist-modification-count)))
1480 (if (string-equal
1481 (expand-file-name bookmark-default-file)
1482 file)
1483 (setq bookmarks-already-loaded t))
1484 (bookmark-bmenu-surreptitiously-rebuild-list))
1485 (error "Invalid bookmark list in %s" file)))
1486 (kill-buffer (current-buffer)))
1487 (if (null no-msg)
1488 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1492 ;;; Code supporting the dired-like bookmark menu.
1493 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1496 (defvar bookmark-bmenu-hidden-bookmarks ())
1499 (defvar bookmark-bmenu-mode-map
1500 (let ((map (make-keymap)))
1501 (suppress-keymap map t)
1502 (define-key map "q" 'quit-window)
1503 (define-key map "v" 'bookmark-bmenu-select)
1504 (define-key map "w" 'bookmark-bmenu-locate)
1505 (define-key map "2" 'bookmark-bmenu-2-window)
1506 (define-key map "1" 'bookmark-bmenu-1-window)
1507 (define-key map "j" 'bookmark-bmenu-this-window)
1508 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1509 (define-key map "f" 'bookmark-bmenu-this-window)
1510 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1511 (define-key map "o" 'bookmark-bmenu-other-window)
1512 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1513 (define-key map "s" 'bookmark-bmenu-save)
1514 (define-key map "k" 'bookmark-bmenu-delete)
1515 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1516 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1517 (define-key map "d" 'bookmark-bmenu-delete)
1518 (define-key map " " 'next-line)
1519 (define-key map "n" 'next-line)
1520 (define-key map "p" 'previous-line)
1521 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1522 (define-key map "?" 'describe-mode)
1523 (define-key map "u" 'bookmark-bmenu-unmark)
1524 (define-key map "m" 'bookmark-bmenu-mark)
1525 (define-key map "l" 'bookmark-bmenu-load)
1526 (define-key map "r" 'bookmark-bmenu-rename)
1527 (define-key map "R" 'bookmark-bmenu-relocate)
1528 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1529 (define-key map "a" 'bookmark-bmenu-show-annotation)
1530 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1531 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1532 ;; The original binding of M-g hides the M-g prefix map.
1533 ;; If someone has a better idea than M-g s, I'm open to suggestions.
1534 (define-key map [?\M-g ?s] 'bookmark-bmenu-search)
1535 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1536 map))
1538 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1539 ;; data.
1540 (put 'bookmark-bmenu-mode 'mode-class 'special)
1543 ;; todo: need to display whether or not bookmark exists as a buffer in
1544 ;; flag column.
1546 ;; Format:
1547 ;; FLAGS BOOKMARK [ LOCATION ]
1550 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1551 "Rebuild the Bookmark List if it exists.
1552 Don't affect the buffer ring order."
1553 (if (get-buffer "*Bookmark List*")
1554 (save-excursion
1555 (save-window-excursion
1556 (bookmark-bmenu-list)))))
1559 ;;;###autoload
1560 (defun bookmark-bmenu-list ()
1561 "Display a list of existing bookmarks.
1562 The list is displayed in a buffer named `*Bookmark List*'.
1563 The leftmost column displays a D if the bookmark is flagged for
1564 deletion, or > if it is flagged for displaying."
1565 (interactive)
1566 (bookmark-maybe-load-default-file)
1567 (let ((buf (get-buffer-create "*Bookmark List*")))
1568 (if (called-interactively-p 'interactive)
1569 (if (or (window-dedicated-p) (window-minibuffer-p))
1570 (pop-to-buffer buf)
1571 (switch-to-buffer buf))
1572 (set-buffer buf)))
1573 (let ((inhibit-read-only t))
1574 (erase-buffer)
1575 (insert "% Bookmark\n- --------\n")
1576 (add-text-properties (point-min) (point)
1577 '(font-lock-face bookmark-menu-heading))
1578 (dolist (full-record (bookmark-maybe-sort-alist))
1579 (let ((name (bookmark-name-from-full-record full-record))
1580 (annotation (bookmark-get-annotation full-record))
1581 (start (point))
1582 end)
1583 ;; if a bookmark has an annotation, prepend a "*"
1584 ;; in the list of bookmarks.
1585 (insert (if (and annotation (not (string-equal annotation "")))
1586 " *" " ")
1587 name)
1588 (setq end (point))
1589 (put-text-property start (+ 2 start) 'bookmark-name-prop name)
1590 (when (display-mouse-p)
1591 (add-text-properties
1592 (+ 2 start) end
1593 '(mouse-face highlight
1594 follow-link t
1595 help-echo "mouse-2: go to this bookmark in other window")))
1596 (insert "\n")))
1597 (goto-char (point-min))
1598 (forward-line 2)
1599 (bookmark-bmenu-mode)
1600 (if bookmark-bmenu-toggle-filenames
1601 (bookmark-bmenu-toggle-filenames t))))
1603 ;;;###autoload
1604 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1605 ;;;###autoload
1606 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1610 (defun bookmark-bmenu-mode ()
1611 "Major mode for editing a list of bookmarks.
1612 Each line describes one of the bookmarks in Emacs.
1613 Letters do not insert themselves; instead, they are commands.
1614 Bookmark names preceded by a \"*\" have annotations.
1615 \\<bookmark-bmenu-mode-map>
1616 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1617 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1618 Also show bookmarks marked using m in other windows.
1619 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1620 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1621 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1622 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1623 together with bookmark selected before this one in another window.
1624 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1625 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1626 so the bookmark menu bookmark remains visible in its window.
1627 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1628 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1629 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1630 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1631 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1632 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1633 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1634 With a prefix arg, prompts for a file to save in.
1635 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1636 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1637 With prefix argument, also move up one line.
1638 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1639 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1640 in another buffer.
1641 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1642 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1643 (kill-all-local-variables)
1644 (use-local-map bookmark-bmenu-mode-map)
1645 (setq truncate-lines t)
1646 (setq buffer-read-only t)
1647 (setq major-mode 'bookmark-bmenu-mode)
1648 (setq mode-name "Bookmark Menu")
1649 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1652 (defun bookmark-bmenu-toggle-filenames (&optional show)
1653 "Toggle whether filenames are shown in the bookmark list.
1654 Optional argument SHOW means show them unconditionally."
1655 (interactive)
1656 (cond
1657 (show
1658 (setq bookmark-bmenu-toggle-filenames nil)
1659 (bookmark-bmenu-show-filenames)
1660 (setq bookmark-bmenu-toggle-filenames t))
1661 (bookmark-bmenu-toggle-filenames
1662 (bookmark-bmenu-hide-filenames)
1663 (setq bookmark-bmenu-toggle-filenames nil))
1665 (bookmark-bmenu-show-filenames)
1666 (setq bookmark-bmenu-toggle-filenames t))))
1669 (defun bookmark-bmenu-show-filenames (&optional force)
1670 "In an interactive bookmark list, show filenames along with bookmarks.
1671 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1672 mainly for debugging, and should not be necessary in normal use."
1673 (if (and (not force) bookmark-bmenu-toggle-filenames)
1674 nil ;already shown, so do nothing
1675 (save-excursion
1676 (save-window-excursion
1677 (goto-char (point-min))
1678 (forward-line 2)
1679 (setq bookmark-bmenu-hidden-bookmarks ())
1680 (let ((inhibit-read-only t))
1681 (while (< (point) (point-max))
1682 (let ((bmrk (bookmark-bmenu-bookmark)))
1683 (push bmrk bookmark-bmenu-hidden-bookmarks)
1684 (let ((start (save-excursion (end-of-line) (point))))
1685 (move-to-column bookmark-bmenu-file-column t)
1686 ;; Strip off `mouse-face' from the white spaces region.
1687 (if (display-mouse-p)
1688 (remove-text-properties start (point)
1689 '(mouse-face nil help-echo nil))))
1690 (delete-region (point) (progn (end-of-line) (point)))
1691 (insert " ")
1692 ;; Pass the NO-HISTORY arg:
1693 (bookmark-insert-location bmrk t)
1694 (forward-line 1))))))))
1697 (defun bookmark-bmenu-hide-filenames (&optional force)
1698 "In an interactive bookmark list, hide the filenames of the bookmarks.
1699 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1700 mainly for debugging, and should not be necessary in normal use."
1701 (when (and (not force) bookmark-bmenu-toggle-filenames)
1702 ;; nothing to hide if above is nil
1703 (save-excursion
1704 (goto-char (point-min))
1705 (forward-line 2)
1706 (setq bookmark-bmenu-hidden-bookmarks
1707 (nreverse bookmark-bmenu-hidden-bookmarks))
1708 (let ((inhibit-read-only t)
1709 (column (save-excursion
1710 (goto-char (point-min))
1711 (search-forward "Bookmark")
1712 (backward-word 1)
1713 (setq bookmark-bmenu-bookmark-column (current-column)))))
1714 (while bookmark-bmenu-hidden-bookmarks
1715 (move-to-column column t)
1716 (bookmark-kill-line)
1717 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1718 (start (point)))
1719 (insert name)
1720 (if (display-mouse-p)
1721 (add-text-properties
1722 start (point)
1723 '(mouse-face highlight
1724 follow-link t
1725 help-echo
1726 "mouse-2: go to this bookmark in other window"))))
1727 (forward-line 1))))))
1730 (defun bookmark-bmenu-check-position ()
1731 "If point is not on a bookmark line, move it to one.
1732 If before the first bookmark line, move it to the first.
1733 If after the last, move it to the last.
1734 Return `bookmark-alist'"
1735 ;; FIXME: The doc string originally implied that this returns nil if
1736 ;; not on a bookmark, which is false. Is there any real reason to
1737 ;; return `bookmark-alist'? This seems to be called in a few places
1738 ;; as a check of whether point is on a bookmark line. Those
1739 ;; "checks" are in fact no-ops, since this never returns nil.
1740 ;; -dadams, 2009-10-10
1741 (cond ((< (count-lines (point-min) (point)) 2)
1742 (goto-char (point-min))
1743 (forward-line 2)
1744 bookmark-alist)
1745 ((and (bolp) (eobp))
1746 (beginning-of-line 0)
1747 bookmark-alist)
1749 bookmark-alist)))
1752 (defun bookmark-bmenu-bookmark ()
1753 "Return the bookmark for this line in an interactive bookmark list buffer."
1754 (when (bookmark-bmenu-check-position)
1755 (get-text-property (line-beginning-position) 'bookmark-name-prop)))
1758 (defun bookmark-show-annotation (bookmark)
1759 "Display the annotation for bookmark named BOOKMARK in a buffer,
1760 if an annotation exists."
1761 (let ((annotation (bookmark-get-annotation bookmark)))
1762 (if (and annotation (not (string-equal annotation "")))
1763 (save-excursion
1764 (let ((old-buf (current-buffer)))
1765 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1766 (delete-region (point-min) (point-max))
1767 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1768 (insert annotation)
1769 (goto-char (point-min))
1770 (pop-to-buffer old-buf))))))
1773 (defun bookmark-show-all-annotations ()
1774 "Display the annotations for all bookmarks in a buffer."
1775 (save-selected-window
1776 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1777 (delete-region (point-min) (point-max))
1778 (dolist (full-record bookmark-alist)
1779 (let* ((name (bookmark-name-from-full-record full-record))
1780 (ann (bookmark-get-annotation full-record)))
1781 (insert (concat name ":\n"))
1782 (if (and ann (not (string-equal ann "")))
1783 ;; insert the annotation, indented by 4 spaces.
1784 (progn
1785 (save-excursion (insert ann) (unless (bolp)
1786 (insert "\n")))
1787 (while (< (point) (point-max))
1788 (beginning-of-line) ; paranoia
1789 (insert " ")
1790 (forward-line)
1791 (end-of-line))))))
1792 (goto-char (point-min))))
1795 (defun bookmark-bmenu-mark ()
1796 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1797 (interactive)
1798 (beginning-of-line)
1799 (if (bookmark-bmenu-check-position)
1800 (let ((inhibit-read-only t))
1801 (delete-char 1)
1802 (insert ?>)
1803 (forward-line 1)
1804 (bookmark-bmenu-check-position))))
1807 (defun bookmark-bmenu-select ()
1808 "Select this line's bookmark; also display bookmarks marked with `>'.
1809 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1810 (interactive)
1811 (if (bookmark-bmenu-check-position)
1812 (let ((bmrk (bookmark-bmenu-bookmark))
1813 (menu (current-buffer))
1814 (others ())
1815 tem)
1816 (goto-char (point-min))
1817 (while (re-search-forward "^>" nil t)
1818 (setq tem (bookmark-bmenu-bookmark))
1819 (let ((inhibit-read-only t))
1820 (delete-char -1)
1821 (insert ?\s))
1822 (or (string-equal tem bmrk)
1823 (member tem others)
1824 (setq others (cons tem others))))
1825 (setq others (nreverse others)
1826 tem (/ (1- (frame-height)) (1+ (length others))))
1827 (delete-other-windows)
1828 (bookmark-jump bmrk)
1829 (bury-buffer menu)
1830 (if others
1831 (while others
1832 (split-window nil tem)
1833 (other-window 1)
1834 (bookmark-jump (car others))
1835 (setq others (cdr others)))
1836 (other-window 1)))))
1839 (defun bookmark-bmenu-save (parg)
1840 "Save the current list into a bookmark file.
1841 With a prefix arg, prompts for a file to save them in."
1842 (interactive "P")
1843 (save-excursion
1844 (save-window-excursion
1845 (bookmark-save parg))))
1848 (defun bookmark-bmenu-load ()
1849 "Load the bookmark file and rebuild the bookmark menu-buffer."
1850 (interactive)
1851 (if (bookmark-bmenu-check-position)
1852 (save-excursion
1853 (save-window-excursion
1854 ;; This will call `bookmark-bmenu-list'
1855 (call-interactively 'bookmark-load)))))
1858 (defun bookmark-bmenu-1-window ()
1859 "Select this line's bookmark, alone, in full frame."
1860 (interactive)
1861 (if (bookmark-bmenu-check-position)
1862 (progn
1863 (bookmark-jump (bookmark-bmenu-bookmark))
1864 (bury-buffer (other-buffer))
1865 (delete-other-windows))))
1868 (defun bookmark-bmenu-2-window ()
1869 "Select this line's bookmark, with previous buffer in second window."
1870 (interactive)
1871 (if (bookmark-bmenu-check-position)
1872 (let ((bmrk (bookmark-bmenu-bookmark))
1873 (menu (current-buffer))
1874 (pop-up-windows t))
1875 (delete-other-windows)
1876 (switch-to-buffer (other-buffer))
1877 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1878 (bookmark--jump-via bmrk 'pop-to-buffer))
1879 (bury-buffer menu))))
1882 (defun bookmark-bmenu-this-window ()
1883 "Select this line's bookmark in this window."
1884 (interactive)
1885 (if (bookmark-bmenu-check-position)
1886 (bookmark-jump (bookmark-bmenu-bookmark))))
1889 (defun bookmark-bmenu-other-window ()
1890 "Select this line's bookmark in other window, leaving bookmark menu visible."
1891 (interactive)
1892 (let ((bookmark (bookmark-bmenu-bookmark)))
1893 (if (bookmark-bmenu-check-position)
1894 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
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."
1901 (interactive)
1902 (let ((bookmark (bookmark-bmenu-bookmark))
1903 (pop-up-windows t)
1904 same-window-buffer-names
1905 same-window-regexps)
1906 (if (bookmark-bmenu-check-position)
1907 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1908 (bookmark--jump-via bookmark 'display-buffer)))))
1910 (defun bookmark-bmenu-other-window-with-mouse (event)
1911 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1912 (interactive "e")
1913 (with-current-buffer (window-buffer (posn-window (event-end event)))
1914 (save-excursion
1915 (goto-char (posn-point (event-end event)))
1916 (bookmark-bmenu-other-window))))
1919 (defun bookmark-bmenu-show-annotation ()
1920 "Show the annotation for the current bookmark in another window."
1921 (interactive)
1922 (let ((bookmark (bookmark-bmenu-bookmark)))
1923 (if (bookmark-bmenu-check-position)
1924 (bookmark-show-annotation bookmark))))
1927 (defun bookmark-bmenu-show-all-annotations ()
1928 "Show the annotation for all bookmarks in another window."
1929 (interactive)
1930 (bookmark-show-all-annotations))
1933 (defun bookmark-bmenu-edit-annotation ()
1934 "Edit the annotation for the current bookmark in another window."
1935 (interactive)
1936 (let ((bookmark (bookmark-bmenu-bookmark)))
1937 (if (bookmark-bmenu-check-position)
1938 (bookmark-edit-annotation bookmark))))
1941 (defun bookmark-bmenu-unmark (&optional backup)
1942 "Cancel all requested operations on bookmark on this line and move down.
1943 Optional BACKUP means move up."
1944 (interactive "P")
1945 (beginning-of-line)
1946 (if (bookmark-bmenu-check-position)
1947 (progn
1948 (let ((inhibit-read-only t))
1949 (delete-char 1)
1950 ;; any flags to reset according to circumstances? How about a
1951 ;; flag indicating whether this bookmark is being visited?
1952 ;; well, we don't have this now, so maybe later.
1953 (insert " "))
1954 (forward-line (if backup -1 1))
1955 (bookmark-bmenu-check-position))))
1958 (defun bookmark-bmenu-backup-unmark ()
1959 "Move up and cancel all requested operations on bookmark on line above."
1960 (interactive)
1961 (forward-line -1)
1962 (if (bookmark-bmenu-check-position)
1963 (progn
1964 (bookmark-bmenu-unmark)
1965 (forward-line -1)
1966 (bookmark-bmenu-check-position))))
1969 (defun bookmark-bmenu-delete ()
1970 "Mark bookmark on this line to be deleted.
1971 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1972 (interactive)
1973 (beginning-of-line)
1974 (if (bookmark-bmenu-check-position)
1975 (let ((inhibit-read-only t))
1976 (delete-char 1)
1977 (insert ?D)
1978 (forward-line 1)
1979 (bookmark-bmenu-check-position))))
1982 (defun bookmark-bmenu-delete-backwards ()
1983 "Mark bookmark on this line to be deleted, then move up one line.
1984 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1985 (interactive)
1986 (bookmark-bmenu-delete)
1987 (forward-line -2)
1988 (if (bookmark-bmenu-check-position)
1989 (forward-line 1))
1990 (bookmark-bmenu-check-position))
1993 (defun bookmark-bmenu-execute-deletions ()
1994 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1995 (interactive)
1996 (message "Deleting bookmarks...")
1997 (let ((o-point (point))
1998 (o-str (save-excursion
1999 (beginning-of-line)
2000 (unless (looking-at "^D")
2001 (buffer-substring
2002 (point)
2003 (progn (end-of-line) (point))))))
2004 (o-col (current-column)))
2005 (goto-char (point-min))
2006 (forward-line 1)
2007 (while (re-search-forward "^D" (point-max) t)
2008 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2009 (bookmark-bmenu-list)
2010 (if o-str
2011 (progn
2012 (goto-char (point-min))
2013 (search-forward o-str)
2014 (beginning-of-line)
2015 (forward-char o-col))
2016 (goto-char o-point))
2017 (beginning-of-line)
2018 (message "Deleting bookmarks...done")
2022 (defun bookmark-bmenu-rename ()
2023 "Rename bookmark on current line. Prompts for a new name."
2024 (interactive)
2025 (if (bookmark-bmenu-check-position)
2026 (let ((bmrk (bookmark-bmenu-bookmark))
2027 (thispoint (point)))
2028 (bookmark-rename bmrk)
2029 (goto-char thispoint))))
2032 (defun bookmark-bmenu-locate ()
2033 "Display location of this bookmark. Displays in the minibuffer."
2034 (interactive)
2035 (if (bookmark-bmenu-check-position)
2036 (let ((bmrk (bookmark-bmenu-bookmark)))
2037 (message "%s" (bookmark-location bmrk)))))
2039 (defun bookmark-bmenu-relocate ()
2040 "Change the file path of the bookmark on the current line,
2041 prompting with completion for the new path."
2042 (interactive)
2043 (if (bookmark-bmenu-check-position)
2044 (let ((bmrk (bookmark-bmenu-bookmark))
2045 (thispoint (point)))
2046 (bookmark-relocate bmrk)
2047 (goto-char thispoint))))
2049 ;;; Bookmark-bmenu search
2051 ;; Store keyboard input for incremental search.
2052 (defvar bookmark-search-pattern)
2054 (defun bookmark-read-search-input ()
2055 "Read each keyboard input and add it to `bookmark-search-pattern'."
2056 (let ((prompt (propertize "Pattern: " 'face 'minibuffer-prompt))
2057 ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
2058 (tmp-list ()))
2059 (while
2060 (let ((char (read-key (concat prompt bookmark-search-pattern))))
2061 (case char
2062 ((?\e ?\r) nil) ; RET or ESC break the search loop.
2063 (?\C-g (setq bookmark-quit-flag t) nil)
2064 (?\d (pop tmp-list) t) ; Delete last char of pattern with DEL
2066 (if (characterp char)
2067 (push char tmp-list)
2068 (setq unread-command-events
2069 (nconc (mapcar 'identity
2070 (this-single-command-raw-keys))
2071 unread-command-events))
2072 nil))))
2073 (setq bookmark-search-pattern
2074 (apply 'string (reverse tmp-list))))))
2077 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2078 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2079 (let ((bookmark-alist
2080 (loop for i in bookmark-alist
2081 when (string-match regexp (car i)) collect i into new
2082 finally return new)))
2083 (bookmark-bmenu-list)))
2086 ;;;###autoload
2087 (defun bookmark-bmenu-search ()
2088 "Incremental search of bookmarks, hiding the non-matches as we go."
2089 (interactive)
2090 (let ((bmk (bookmark-bmenu-bookmark))
2091 (bookmark-search-pattern "")
2092 (timer (run-with-idle-timer
2093 bookmark-search-delay 'repeat
2094 #'(lambda ()
2095 (bookmark-bmenu-filter-alist-by-regexp
2096 bookmark-search-pattern)))))
2097 (unwind-protect
2098 (bookmark-read-search-input)
2099 (cancel-timer timer)
2100 (message nil)
2101 (when bookmark-quit-flag ; C-g hit restore menu list.
2102 (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
2103 (setq bookmark-quit-flag nil))))
2105 (defun bookmark-bmenu-goto-bookmark (name)
2106 "Move point to bookmark with name NAME."
2107 (goto-char (point-min))
2108 (bookmark-bmenu-check-position)
2109 (while (not (equal name (bookmark-bmenu-bookmark)))
2110 (forward-line 1))
2111 (forward-line 0))
2115 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2117 (defun bookmark-menu-popup-paned-menu (event name entries)
2118 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2119 That is, ENTRIES is a list of strings which appear as the choices
2120 in the menu.
2121 The number of panes depends on the number of entries.
2122 The visible entries are truncated to `bookmark-menu-length', but the
2123 strings returned are not."
2124 (let ((f-height (/ (frame-height) 2))
2125 (pane-list nil)
2126 (iter 0))
2127 (while entries
2128 (let (lst
2129 (count 0))
2130 (while (and (< count f-height) entries)
2131 (let ((str (car entries)))
2132 (push (cons
2133 (if (> (length str) bookmark-menu-length)
2134 (substring str 0 bookmark-menu-length)
2135 str)
2136 str)
2137 lst)
2138 (setq entries (cdr entries))
2139 (setq count (1+ count))))
2140 (setq iter (1+ iter))
2141 (push (cons
2142 (format "-*- %s (%d) -*-" name iter)
2143 (nreverse lst))
2144 pane-list)))
2146 ;; Popup the menu and return the string.
2147 (x-popup-menu event (cons (concat "-*- " name " -*-")
2148 (nreverse pane-list)))))
2151 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2152 ;; following works, and for explaining what to do to make it work.
2154 ;; We MUST autoload EACH form used to set up this variable's value, so
2155 ;; that the whole job is done in loaddefs.el.
2157 ;; Emacs menubar stuff.
2159 ;;;###autoload
2160 (defvar menu-bar-bookmark-map
2161 (let ((map (make-sparse-keymap "Bookmark functions")))
2162 (define-key map [load]
2163 `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
2164 :help ,(purecopy "Load bookmarks from a bookmark file)")))
2165 (define-key map [write]
2166 `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
2167 :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
2168 (define-key map [save]
2169 `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
2170 :help ,(purecopy "Save currently defined bookmarks")))
2171 (define-key map [edit]
2172 `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
2173 :help ,(purecopy "Display a list of existing bookmarks")))
2174 (define-key map [delete]
2175 `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
2176 :help ,(purecopy "Delete a bookmark from the bookmark list")))
2177 (define-key map [rename]
2178 `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
2179 :help ,(purecopy "Change the name of a bookmark")))
2180 (define-key map [locate]
2181 `(menu-item ,(purecopy "Insert Location...") bookmark-locate
2182 :help ,(purecopy "Insert the name of the file associated with a bookmark")))
2183 (define-key map [insert]
2184 `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
2185 :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
2186 (define-key map [set]
2187 `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
2188 :help ,(purecopy "Set a bookmark named inside a file.")))
2189 (define-key map [jump]
2190 `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
2191 :help ,(purecopy "Jump to a bookmark (a point in some file)")))
2192 map))
2194 ;;;###autoload
2195 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2197 ;; make bookmarks appear toward the right side of the menu.
2198 (if (boundp 'menu-bar-final-items)
2199 (if menu-bar-final-items
2200 (push 'bookmark menu-bar-final-items))
2201 (setq menu-bar-final-items '(bookmark)))
2203 ;;;; end bookmark menu stuff ;;;;
2206 ;; Load Hook
2207 (defvar bookmark-load-hook nil
2208 "Hook run at the end of loading bookmark.")
2210 ;; Exit Hook, called from kill-emacs-hook
2211 (defvar bookmark-exit-hook nil
2212 "Hook run when Emacs exits.")
2214 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2216 (defun bookmark-exit-hook-internal ()
2217 "Save bookmark state, if necessary, at Emacs exit time.
2218 This also runs `bookmark-exit-hook'."
2219 (run-hooks 'bookmark-exit-hook)
2220 (and bookmark-alist
2221 (bookmark-time-to-save-p t)
2222 (bookmark-save)))
2224 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2226 (defun bookmark-unload-function ()
2227 "Unload the Bookmark library."
2228 (when bookmark-save-flag (bookmark-save))
2229 ;; continue standard unloading
2230 nil)
2233 (run-hooks 'bookmark-load-hook)
2235 (provide 'bookmark)
2237 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2238 ;;; bookmark.el ends here