(Fbyteorder, init_font, Ffont_get_system_font): Declare.
[emacs.git] / lisp / bookmark.el
bloba3c315acbc26c644c42dc8b1deb65fceecf68c56
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders, annotations
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package is for setting "bookmarks" in files. A bookmark
29 ;; associates a string with a location in a certain file. Thus, you
30 ;; can navigate your way to that location by providing the string.
31 ;; See the "User Variables" section for customizations.
33 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
34 ;; then implementing the bookmark-current-bookmark idea. He even
35 ;; sent *patches*, bless his soul...
37 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
38 ;; fixing and improving bookmark-time-to-save-p.
40 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
41 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
42 ;; and the menu-bar).
44 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
45 ;; suggestions and the code to implement them (like
46 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
47 ;; stuff).
49 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
50 ;; for his eminently sensible suggestion to separate bookmark-jump
51 ;; into bookmark-jump and bookmark-jump-noselect, which made many
52 ;; other things cleaner as well.
54 ;; Thanks to Roland McGrath for encouragement and help with defining
55 ;; autoloads on the menu-bar.
57 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
58 ;; values in bookmark-jump and bookmark-set. Everybody please keep
59 ;; all the keystrokes they save thereby and send them to him at the
60 ;; end of each year :-) (No, seriously, thanks Jonathan!)
62 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
63 ;; thinking up the annotations feature and implementing it so well.
65 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
66 ;; <olstad@msc.edu>.
68 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
69 ;; reported and fixed.
71 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
73 ;; Enough with the credits already, get on to the good stuff:
75 ;; FAVORITE CHINESE RESTAURANT:
76 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
77 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
79 ;;; Code:
81 (require 'pp)
82 (eval-when-compile (require 'cl))
84 ;;; Misc comments:
86 ;; If variable bookmark-use-annotations is non-nil, an annotation is
87 ;; queried for when setting a bookmark.
89 ;; The bookmark list is sorted lexically by default, but you can turn
90 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
91 ;; the list will be presented in the order it is recorded
92 ;; (chronologically), which is actually fairly useful as well.
94 ;;; User Variables
96 (defgroup bookmark nil
97 "Setting, annotation and jumping to bookmarks."
98 :group 'matching)
101 (defcustom bookmark-use-annotations nil
102 "If non-nil, saving a bookmark queries for an annotation in a buffer."
103 :type 'boolean
104 :group 'bookmark)
107 (defcustom bookmark-save-flag t
108 "Controls when Emacs saves bookmarks to a file.
109 --> nil means never save bookmarks, except when `bookmark-save' is
110 explicitly called \(\\[bookmark-save]\).
111 --> t means save bookmarks when Emacs is killed.
112 --> Otherwise, it should be a number that is the frequency with which
113 the bookmark list is saved \(i.e.: the number of times which
114 Emacs' bookmark list may be modified before it is automatically
115 saved.\). If it is a number, Emacs will also automatically save
116 bookmarks when it is killed.
118 Therefore, the way to get it to save every time you make or delete a
119 bookmark is to set this variable to 1 \(or 0, which produces the same
120 behavior.\)
122 To specify the file in which to save them, modify the variable
123 `bookmark-default-file', which is `~/.emacs.bmk' by default."
124 :type '(choice (const nil) integer (other t))
125 :group 'bookmark)
128 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
129 "The `.emacs.bmk' file used to be called this name.")
132 ;; defvarred to avoid a compilation warning:
133 (defvar bookmark-file nil
134 "Old name for `bookmark-default-file'.")
136 (defcustom bookmark-default-file
137 (if bookmark-file
138 ;; In case user set `bookmark-file' in her .emacs:
139 bookmark-file
140 (convert-standard-filename "~/.emacs.bmk"))
141 "File in which to save bookmarks by default."
142 :type 'file
143 :group 'bookmark)
146 (defcustom bookmark-version-control 'nospecial
147 "Whether or not to make numbered backups of the bookmark file.
148 It can have four values: t, nil, `never', and `nospecial'.
149 The first three have the same meaning that they do for the
150 variable `version-control', and the final value `nospecial' means just
151 use the value of `version-control'."
152 :type '(choice (const nil) (const never) (const nospecial)
153 (other t))
154 :group 'bookmark)
157 (defcustom bookmark-completion-ignore-case t
158 "Non-nil means bookmark functions ignore case in completion."
159 :type 'boolean
160 :group 'bookmark)
163 (defcustom bookmark-sort-flag t
164 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
165 Otherwise they will be displayed in LIFO order (that is, most
166 recently set ones come first, oldest ones come last)."
167 :type 'boolean
168 :group 'bookmark)
171 (defcustom bookmark-automatically-show-annotations t
172 "Non-nil means show annotations when jumping to a bookmark."
173 :type 'boolean
174 :group 'bookmark)
177 (defcustom bookmark-bmenu-file-column 30
178 "Column at which to display filenames in a buffer listing bookmarks.
179 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
180 :type 'integer
181 :group 'bookmark)
184 (defcustom bookmark-bmenu-toggle-filenames t
185 "Non-nil means show filenames when listing bookmarks.
186 This may result in truncated bookmark names. To disable this, put the
187 following in your `.emacs' file:
189 \(setq bookmark-bmenu-toggle-filenames nil\)"
190 :type 'boolean
191 :group 'bookmark)
194 (defcustom bookmark-menu-length 70
195 "Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer
197 :group 'bookmark)
200 (defface bookmark-menu-heading
201 '((t (:inherit font-lock-type-face)))
202 "Face used to highlight the heading in bookmark menu buffers."
203 :group 'bookmark
204 :version "22.1")
207 ;;; No user-serviceable parts beyond this point.
209 ;; Added for lucid emacs compatibility, db
210 (or (fboundp 'defalias) (fset 'defalias 'fset))
212 ;; suggested for lucid compatibility by david hughes:
213 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
216 ;;; Keymap stuff:
218 ;; Set up these bindings dumping time *only*;
219 ;; if the user alters them, don't override the user when loading bookmark.el.
221 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
222 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
223 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
225 ;;;###autoload
226 (defvar bookmark-map
227 (let ((map (make-sparse-keymap)))
228 ;; Read the help on all of these functions for details...
229 (define-key map "x" 'bookmark-set)
230 (define-key map "m" 'bookmark-set) ;"m"ark
231 (define-key map "j" 'bookmark-jump)
232 (define-key map "g" 'bookmark-jump) ;"g"o
233 (define-key map "o" 'bookmark-jump-other-window)
234 (define-key map "i" 'bookmark-insert)
235 (define-key map "e" 'edit-bookmarks)
236 (define-key map "f" 'bookmark-insert-location) ;"f"ind
237 (define-key map "r" 'bookmark-rename)
238 (define-key map "d" 'bookmark-delete)
239 (define-key map "l" 'bookmark-load)
240 (define-key map "w" 'bookmark-write)
241 (define-key map "s" 'bookmark-save)
242 map)
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
249 ;;;###autoload (fset 'bookmark-map bookmark-map)
252 ;;; Core variables and data structures:
253 (defvar bookmark-alist ()
254 "Association list of bookmarks and their records.
255 Bookmark functions update the value automatically.
256 You probably do NOT want to change the value yourself.
258 The value is an alist with entries of the form
260 (BOOKMARK-NAME . PARAM-ALIST)
262 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
264 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
266 PARAM-ALIST is an alist of bookmark information. The order of the
267 entries in PARAM-ALIST is not important. The possible entries are
268 described below. An entry with a key but null value means the entry
269 is not used.
271 (filename . FILENAME)
272 (position . POS)
273 (front-context-string . STR-AFTER-POS)
274 (rear-context-string . STR-BEFORE-POS)
275 (handler . HANDLER)
276 (annotation . ANNOTATION)
278 FILENAME names the bookmarked file.
279 POS is the bookmarked buffer position (position in the file).
280 STR-AFTER-POS is buffer text that immediately follows POS.
281 STR-BEFORE-POS is buffer text that immediately precedes POS.
282 ANNOTATION is a string that describes the bookmark.
283 See options `bookmark-use-annotations' and
284 `bookmark-automatically-show-annotations'.
285 HANDLER is a function that provides the bookmark-jump behavior for a
286 specific kind of bookmark. This is the case for Info bookmarks,
287 for instance. HANDLER must accept a bookmark as argument.")
289 (defvar bookmarks-already-loaded nil
290 "Non-nil iff bookmarks have been loaded from `bookmark-default-file'.")
293 ;; more stuff added by db.
295 (defvar bookmark-current-bookmark nil
296 "Name of bookmark most recently used in the current file.
297 It is buffer local, used to make moving a bookmark forward
298 through a file easier.")
300 (make-variable-buffer-local 'bookmark-current-bookmark)
303 (defvar bookmark-alist-modification-count 0
304 "Number of modifications to bookmark list since it was last saved.")
307 (defvar bookmark-search-size 16
308 "Length of the context strings recorded on either side of a bookmark.")
311 (defvar bookmark-current-buffer nil
312 "The buffer in which a bookmark is currently being set or renamed.
313 Functions that insert strings into the minibuffer use this to know
314 the source buffer for that information; see `bookmark-yank-word' and
315 `bookmark-insert-current-bookmark' for example.")
318 (defvar bookmark-yank-point 0
319 "The next point from which to pull source text for `bookmark-yank-word'.
320 This point is in `bookmark-curent-buffer'.")
324 ;; Helper functions.
326 ;; Only functions on this page and the next one (file formats) need to
327 ;; know anything about the format of bookmark-alist entries.
328 ;; Everyone else should go through them.
331 (defun bookmark-name-from-full-record (full-record)
332 "Return name of FULL-RECORD \(an alist element instead of a string\)."
333 (car full-record))
336 (defun bookmark-all-names ()
337 "Return a list of all current bookmark names."
338 (bookmark-maybe-load-default-file)
339 (mapcar 'bookmark-name-from-full-record bookmark-alist))
342 (defun bookmark-get-bookmark (bookmark &optional noerror)
343 "Return the bookmark record corresponding to BOOKMARK.
344 If BOOKMARK is a string, look for the corresponding bookmark record in
345 `bookmark-alist'; return it if found, otherwise error. Else if
346 BOOKMARK is already a bookmark record, just return it."
347 (cond
348 ((consp bookmark) bookmark)
349 ((stringp bookmark)
350 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
351 (unless noerror (error "Invalid bookmark %s" bookmark))))))
354 (defun bookmark-get-bookmark-record (bookmark)
355 "Return the record portion of the entry for BOOKMARK in
356 `bookmark-alist' (that is, all information but the name).
357 BOOKMARK may be a bookmark name (a string) or a bookmark record."
358 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
359 ;; The bookmark objects can either look like (NAME ALIST) or
360 ;; (NAME . ALIST), so we have to distinguish the two here.
361 (if (and (null (cdr alist)) (consp (caar alist)))
362 (car alist) alist)))
365 (defun bookmark-set-name (bookmark newname)
366 "Set BOOKMARK's name to NEWNAME.
367 BOOKMARK may be a bookmark name (a string) or a bookmark record."
368 (setcar
369 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
370 newname))
372 (defun bookmark-prop-get (bookmark prop)
373 "Return the property PROP of BOOKMARK, or nil if none.
374 BOOKMARK may be a bookmark name (a string) or a bookmark record."
375 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
377 (defun bookmark-prop-set (bookmark prop val)
378 "Set the property PROP of BOOKMARK to VAL.
379 BOOKMARK may be a bookmark name (a string) or a bookmark record."
380 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
381 (if cell
382 (setcdr cell val)
383 (nconc (bookmark-get-bookmark-record bookmark)
384 (list (cons prop val))))))
386 (defun bookmark-get-annotation (bookmark)
387 "Return the annotation of BOOKMARK, or nil if none.
388 BOOKMARK may be a bookmark name (a string) or a bookmark record."
389 (bookmark-prop-get bookmark 'annotation))
391 (defun bookmark-set-annotation (bookmark ann)
392 "Set the annotation of BOOKMARK to ANN.
393 BOOKMARK may be a bookmark name (a string) or a bookmark record."
394 (bookmark-prop-set bookmark 'annotation ann))
397 (defun bookmark-get-filename (bookmark)
398 "Return the full filename of BOOKMARK, or nil if none.
399 BOOKMARK may be a bookmark name (a string) or a bookmark record."
400 (bookmark-prop-get bookmark 'filename))
403 (defun bookmark-set-filename (bookmark filename)
404 "Set the full filename of BOOKMARK to FILENAME.
405 BOOKMARK may be a bookmark name (a string) or a bookmark record."
406 (bookmark-prop-set bookmark 'filename filename))
409 (defun bookmark-get-position (bookmark)
410 "Return the position \(i.e.: point\) of BOOKMARK, or nil if none.
411 BOOKMARK may be a bookmark name (a string) or a bookmark record."
412 (bookmark-prop-get bookmark 'position))
415 (defun bookmark-set-position (bookmark position)
416 "Set the position \(i.e.: point\) of BOOKMARK to POSITION.
417 BOOKMARK may be a bookmark name (a string) or a bookmark record."
418 (bookmark-prop-set bookmark 'position position))
421 (defun bookmark-get-front-context-string (bookmark)
422 "Return the front-context-string of BOOKMARK, or nil if none.
423 BOOKMARK may be a bookmark name (a string) or a bookmark record."
424 (bookmark-prop-get bookmark 'front-context-string))
427 (defun bookmark-set-front-context-string (bookmark string)
428 "Set the front-context-string of BOOKMARK to STRING.
429 BOOKMARK may be a bookmark name (a string) or a bookmark record."
430 (bookmark-prop-set bookmark 'front-context-string string))
433 (defun bookmark-get-rear-context-string (bookmark)
434 "Return the rear-context-string of BOOKMARK, or nil if none.
435 BOOKMARK may be a bookmark name (a string) or a bookmark record."
436 (bookmark-prop-get bookmark 'rear-context-string))
439 (defun bookmark-set-rear-context-string (bookmark string)
440 "Set the rear-context-string of BOOKMARK to STRING.
441 BOOKMARK may be a bookmark name (a string) or a bookmark record."
442 (bookmark-prop-set bookmark 'rear-context-string string))
445 (defun bookmark-get-handler (bookmark)
446 "Return the handler function for BOOKMARK, or nil if none.
447 BOOKMARK may be a bookmark name (a string) or a bookmark record."
448 (bookmark-prop-get bookmark 'handler))
450 (defvar bookmark-history nil
451 "The history list for bookmark functions.")
454 (defun bookmark-completing-read (prompt &optional default)
455 "Prompting with PROMPT, read a bookmark name in completion.
456 PROMPT will get a \": \" stuck on the end no matter what, so you
457 probably don't want to include one yourself.
458 Optional second arg DEFAULT is a string to return if the user enters
459 the empty string."
460 (bookmark-maybe-load-default-file) ; paranoia
461 (if (listp last-nonmenu-event)
462 (bookmark-menu-popup-paned-menu t prompt
463 (if bookmark-sort-flag
464 (sort (bookmark-all-names)
465 'string-lessp)
466 (bookmark-all-names)))
467 (let* ((completion-ignore-case bookmark-completion-ignore-case)
468 (default default)
469 (prompt (concat prompt (if default
470 (format " (%s): " default)
471 ": ")))
472 (str
473 (completing-read prompt
474 bookmark-alist
478 'bookmark-history)))
479 (if (string-equal "" str) default str))))
482 (defmacro bookmark-maybe-historicize-string (string)
483 "Put STRING into the bookmark prompt history, if caller non-interactive.
484 We need this because sometimes bookmark functions are invoked from
485 menus, so `completing-read' never gets a chance to set `bookmark-history'."
486 `(or
487 (called-interactively-p 'interactive)
488 (setq bookmark-history (cons ,string bookmark-history))))
490 (defvar bookmark-make-record-function 'bookmark-make-record-default
491 "A function that should be called to create a bookmark record.
492 Modes may set this variable buffer-locally to enable bookmarking of
493 locations that should be treated specially, such as Info nodes,
494 news posts, images, pdf documents, etc.
496 The function will be called with no arguments.
497 It should signal a user error if it is unable to construct a record for
498 the current location.
500 The returned record should be a cons cell of the form (NAME . ALIST)
501 where ALIST is as described in `bookmark-alist' and may typically contain
502 a special cons (handler . HANDLER-FUNC) which specifies the handler function
503 that should be used instead of `bookmark-default-handler' to open this
504 bookmark. See the documentation for `bookmark-alist' for more.
506 NAME is a suggested name for the constructed bookmark. It can be nil
507 in which case a default heuristic will be used. The function can also
508 equivalently just return ALIST without NAME.")
510 (defun bookmark-make-record ()
511 "Return a new bookmark record (NAME . ALIST) for the current location."
512 (let ((record (funcall bookmark-make-record-function)))
513 ;; Set up default name.
514 (if (stringp (car record))
515 ;; The function already provided a default name.
516 record
517 (if (car record) (push nil record))
518 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
519 record)))
521 (defun bookmark-store (name alist no-overwrite)
522 "Store the bookmark NAME with data ALIST.
523 If NO-OVERWRITE is non-nil and another bookmark of the same name already
524 exists in `bookmark-alist', record the new bookmark without throwing away the
525 old one."
526 (bookmark-maybe-load-default-file)
527 (let ((stripped-name (copy-sequence name)))
528 (or (featurep 'xemacs)
529 ;; XEmacs's `set-text-properties' doesn't work on
530 ;; free-standing strings, apparently.
531 (set-text-properties 0 (length stripped-name) nil stripped-name))
532 (if (and (not no-overwrite)
533 (bookmark-get-bookmark stripped-name 'noerror))
534 ;; already existing bookmark under that name and
535 ;; no prefix arg means just overwrite old bookmark
536 ;; Use the new (NAME . ALIST) format.
537 (setcdr (bookmark-get-bookmark stripped-name) alist)
539 ;; otherwise just cons it onto the front (either the bookmark
540 ;; doesn't exist already, or there is no prefix arg. In either
541 ;; case, we want the new bookmark consed onto the alist...)
543 (push (cons stripped-name alist) bookmark-alist))
545 ;; Added by db
546 (setq bookmark-current-bookmark stripped-name)
547 (setq bookmark-alist-modification-count
548 (1+ bookmark-alist-modification-count))
549 (if (bookmark-time-to-save-p)
550 (bookmark-save))
552 (setq bookmark-current-bookmark stripped-name)
553 (bookmark-bmenu-surreptitiously-rebuild-list)))
555 (defun bookmark-make-record-default (&optional point-only)
556 "Return the record describing the location of a new bookmark.
557 Must be at the correct position in the buffer in which the bookmark is
558 being set.
559 If POINT-ONLY is non-nil, then only return the subset of the
560 record that pertains to the location within the buffer."
561 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
562 (front-context-string
563 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
564 (buffer-substring-no-properties
565 (point)
566 (+ (point) bookmark-search-size))
567 nil))
568 (rear-context-string
569 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
570 (buffer-substring-no-properties
571 (point)
572 (- (point) bookmark-search-size))
573 nil))
574 (position . ,(point))))
577 ;;; File format stuff
579 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
580 ;; the bookmark file format -- please don't. The current format
581 ;; should be extensible enough. If you feel the need to change it,
582 ;; please discuss it with other Emacs developers first.
584 ;; The format of `bookmark-alist' has changed twice in its lifetime.
585 ;; This comment describes the three formats, FIRST, SECOND, and
586 ;; CURRENT.
588 ;; The FIRST format was used prior to Emacs 20:
590 ;; ((BOOKMARK-NAME (FILENAME
591 ;; STRING-IN-FRONT
592 ;; STRING-BEHIND
593 ;; POINT))
594 ;; ...)
596 ;; The SECOND format was introduced in Emacs 20:
598 ;; ((BOOKMARK-NAME ((filename . FILENAME)
599 ;; (position . POS)
600 ;; (front-context-string . STR-AFTER-POS)
601 ;; (rear-context-string . STR-BEFORE-POS)
602 ;; (annotation . ANNOTATION)
603 ;; (whatever . VALUE)
604 ;; ...
605 ;; ))
606 ;; ...)
608 ;; The CURRENT format was introduced in Emacs 22:
610 ;; ((BOOKMARK-NAME (filename . FILENAME)
611 ;; (position . POS)
612 ;; (front-context-string . STR-AFTER-POS)
613 ;; (rear-context-string . STR-BEFORE-POS)
614 ;; (annotation . ANNOTATION)
615 ;; (whatever . VALUE)
616 ;; ...
617 ;; )
618 ;; ...)
620 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
621 ;; bookmark record is a list of entry information. FIRST and SECOND
622 ;; differ in the form of the record information: FIRST uses a list of
623 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
624 ;; the list elements matters. In the SECOND format, the order of the
625 ;; alist elements is unimportant. The SECOND format facilitates the
626 ;; addition of new kinds of elements, to support new kinds of
627 ;; bookmarks or code evolution.
629 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
630 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
631 ;; no longer a cons. Why that change was made remains a mystery --
632 ;; just be aware of it. (Be aware too that this explanatory comment
633 ;; was incorrect in Emacs 22 and Emacs 23.1.)
635 ;; To deal with the change from FIRST format to SECOND, conversion
636 ;; code was added, and it is still in use. See
637 ;; `bookmark-maybe-upgrade-file-format'.
639 ;; No conversion from SECOND to CURRENT is done. Instead, the code
640 ;; handles both formats OK. It must continue to do so.
642 ;; See the doc string of `bookmark-alist' for information about the
643 ;; elements that define a bookmark (e.g. `filename').
646 (defconst bookmark-file-format-version 1
647 "The current version of the format used by bookmark files.
648 You should never need to change this.")
651 (defconst bookmark-end-of-version-stamp-marker
652 "-*- End Of Bookmark File Format Version Stamp -*-\n"
653 "This string marks the end of the version stamp in a bookmark file.")
656 (defun bookmark-alist-from-buffer ()
657 "Return a `bookmark-alist' (in any format) from the current buffer.
658 The buffer must of course contain bookmark format information.
659 Does not care from where in the buffer it is called, and does not
660 affect point."
661 (save-excursion
662 (goto-char (point-min))
663 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
664 (read (current-buffer))
665 ;; Else we're dealing with format version 0
666 (if (search-forward "(" nil t)
667 (progn
668 (forward-char -1)
669 (read (current-buffer)))
670 ;; Else no hope of getting information here.
671 (error "Not bookmark format")))))
674 (defun bookmark-upgrade-version-0-alist (old-list)
675 "Upgrade a version 0 alist OLD-LIST to the current version."
676 (mapcar
677 (lambda (bookmark)
678 (let* ((name (car bookmark))
679 (record (car (cdr bookmark)))
680 (filename (nth 0 record))
681 (front-str (nth 1 record))
682 (rear-str (nth 2 record))
683 (position (nth 3 record))
684 (ann (nth 4 record)))
685 (list
686 name
687 `((filename . ,filename)
688 (front-context-string . ,(or front-str ""))
689 (rear-context-string . ,(or rear-str ""))
690 (position . ,position)
691 (annotation . ,ann)))))
692 old-list))
695 (defun bookmark-upgrade-file-format-from-0 ()
696 "Upgrade a bookmark file of format 0 (the original format) to format 1.
697 This expects to be called from `point-min' in a bookmark file."
698 (message "Upgrading bookmark format from 0 to %d..."
699 bookmark-file-format-version)
700 (let* ((old-list (bookmark-alist-from-buffer))
701 (new-list (bookmark-upgrade-version-0-alist old-list)))
702 (delete-region (point-min) (point-max))
703 (bookmark-insert-file-format-version-stamp)
704 (pp new-list (current-buffer))
705 (save-buffer))
706 (goto-char (point-min))
707 (message "Upgrading bookmark format from 0 to %d...done"
708 bookmark-file-format-version)
712 (defun bookmark-grok-file-format-version ()
713 "Return an integer which is the file-format version of this bookmark file.
714 This expects to be called from `point-min' in a bookmark file."
715 (if (looking-at "^;;;;")
716 (save-excursion
717 (save-match-data
718 (re-search-forward "[0-9]")
719 (forward-char -1)
720 (read (current-buffer))))
721 ;; Else this is format version 0, the original one, which didn't
722 ;; even have version stamps.
726 (defun bookmark-maybe-upgrade-file-format ()
727 "Check the file-format version of this bookmark file.
728 If the version is not up-to-date, upgrade it automatically.
729 This expects to be called from `point-min' in a bookmark file."
730 (let ((version (bookmark-grok-file-format-version)))
731 (cond
732 ((= version bookmark-file-format-version)
733 ) ; home free -- version is current
734 ((= version 0)
735 (bookmark-upgrade-file-format-from-0))
737 (error "Bookmark file format version strangeness")))))
740 (defun bookmark-insert-file-format-version-stamp ()
741 "Insert text indicating current version of bookmark file format."
742 (insert
743 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
744 bookmark-file-format-version))
745 (insert ";;; This format is meant to be slightly human-readable;\n"
746 ";;; nevertheless, you probably don't want to edit it.\n"
747 ";;; "
748 bookmark-end-of-version-stamp-marker))
751 ;;; end file-format stuff
754 ;;; Generic helpers.
756 (defun bookmark-maybe-message (fmt &rest args)
757 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
758 (if (>= baud-rate 9600)
759 (apply 'message fmt args)))
762 ;;; Core code:
764 (defvar bookmark-minibuffer-read-name-map
765 (let ((map (make-sparse-keymap)))
766 (set-keymap-parent map minibuffer-local-map)
767 (define-key map "\C-w" 'bookmark-yank-word)
768 ;; This C-u binding might not be very useful any more now that we
769 ;; provide access to the default via the standard M-n binding.
770 ;; Maybe we should just remove it? --Stef-08
771 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
772 map))
774 ;;;###autoload
775 (defun bookmark-set (&optional name no-overwrite)
776 "Set a bookmark named NAME at the current location.
777 If name is nil, then prompt the user.
779 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
780 existing bookmark that has the same name as NAME, but instead push the
781 new bookmark onto the bookmark alist. The most recently set bookmark
782 with name NAME is thus the one in effect at any given time, but the
783 others are still there, should the user decide to delete the most
784 recent one.
786 To yank words from the text of the buffer and use them as part of the
787 bookmark name, type C-w while setting a bookmark. Successive C-w's
788 yank successive words.
790 Typing C-u inserts (at the bookmark name prompt) the name of the last
791 bookmark used in the document where the new bookmark is being set;
792 this helps you use a single bookmark name to track progress through a
793 large document. If there is no prior bookmark for this document, then
794 C-u inserts an appropriate name based on the buffer or file.
796 Use \\[bookmark-delete] to remove bookmarks \(you give it a name and
797 it removes only the first instance of a bookmark with that name from
798 the list of bookmarks.\)"
799 (interactive (list nil current-prefix-arg))
800 (let* ((record (bookmark-make-record))
801 (default (car record)))
803 (bookmark-maybe-load-default-file)
805 (setq bookmark-yank-point (point))
806 (setq bookmark-current-buffer (current-buffer))
808 (let ((str
809 (or name
810 (read-from-minibuffer
811 (format "Set bookmark (%s): " default)
813 bookmark-minibuffer-read-name-map
814 nil nil default))))
815 (and (string-equal str "") (setq str default))
816 (bookmark-store str (cdr record) no-overwrite)
818 ;; Ask for an annotation buffer for this bookmark
819 (when bookmark-use-annotations
820 (bookmark-edit-annotation str)))))
822 (defun bookmark-kill-line (&optional newline-too)
823 "Kill from point to end of line.
824 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
825 Does not affect the kill ring."
826 (let ((eol (save-excursion (end-of-line) (point))))
827 (delete-region (point) eol)
828 (if (and newline-too (looking-at "\n"))
829 (delete-char 1))))
832 ;; Defvars to avoid compilation warnings:
833 (defvar bookmark-annotation-name nil
834 "Variable holding the name of the bookmark.
835 This is used in `bookmark-edit-annotation' to record the bookmark
836 whose annotation is being edited.")
839 (defun bookmark-default-annotation-text (bookmark)
840 "Return default annotation text for BOOKMARK (a string, not a record).
841 The default annotation text is simply some text explaining how to use
842 annotations."
843 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
844 "# All lines which start with a '#' will be deleted.\n"
845 "# Type C-c C-c when done.\n#\n"
846 "# Author: " (user-full-name) " <" (user-login-name) "@"
847 (system-name) ">\n"
848 "# Date: " (current-time-string) "\n"))
851 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
852 "Function to return default text to use for a bookmark annotation.
853 It takes one argument, the name of the bookmark, as a string.")
854 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
855 'bookmark-edit-annotation-text-func "23.1")
857 (defvar bookmark-edit-annotation-mode-map
858 (let ((map (make-sparse-keymap)))
859 (set-keymap-parent map text-mode-map)
860 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
861 map)
862 "Keymap for editing an annotation of a bookmark.")
865 (defun bookmark-edit-annotation-mode (bookmark)
866 "Mode for editing the annotation of bookmark BOOKMARK.
867 When you have finished composing, type \\[bookmark-send-annotation].
869 BOOKMARK is a bookmark name (a string) or a bookmark record.
871 \\{bookmark-edit-annotation-mode-map}"
872 (interactive)
873 (kill-all-local-variables)
874 (make-local-variable 'bookmark-annotation-name)
875 (setq bookmark-annotation-name bookmark)
876 (use-local-map bookmark-edit-annotation-mode-map)
877 (setq major-mode 'bookmark-edit-annotation-mode
878 mode-name "Edit Bookmark Annotation")
879 (insert (funcall bookmark-edit-annotation-text-func bookmark))
880 (let ((annotation (bookmark-get-annotation bookmark)))
881 (if (and annotation (not (string-equal annotation "")))
882 (insert annotation)))
883 (run-mode-hooks 'text-mode-hook))
886 (defun bookmark-send-edited-annotation ()
887 "Use buffer contents as annotation for a bookmark.
888 Lines beginning with `#' are ignored."
889 (interactive)
890 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
891 (error "Not in bookmark-edit-annotation-mode"))
892 (goto-char (point-min))
893 (while (< (point) (point-max))
894 (if (looking-at "^#")
895 (bookmark-kill-line t)
896 (forward-line 1)))
897 ;; Take no chances with text properties.
898 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
899 (bookmark bookmark-annotation-name))
900 (bookmark-set-annotation bookmark annotation)
901 (bookmark-bmenu-surreptitiously-rebuild-list))
902 (kill-buffer (current-buffer)))
905 (defun bookmark-edit-annotation (bookmark)
906 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
907 BOOKMARK is a bookmark name (a string) or a bookmark record."
908 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
909 (bookmark-edit-annotation-mode bookmark))
912 (defun bookmark-insert-current-bookmark ()
913 "Insert into the bookmark name currently being set the value of
914 `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
915 to the buffer's file name if `bookmark-current-bookmark' is nil."
916 (interactive)
917 (let ((str
918 (with-current-buffer bookmark-current-buffer
919 (or bookmark-current-bookmark
920 (bookmark-buffer-name)))))
921 (insert str)))
924 (defun bookmark-buffer-name ()
925 "Return the name of the current buffer in a form usable as a bookmark name.
926 If the buffer is associated with a file or directory, use that name."
927 (cond
928 ;; Or are we a file?
929 (buffer-file-name (file-name-nondirectory buffer-file-name))
930 ;; Or are we a directory?
931 ((and (boundp 'dired-directory) dired-directory)
932 (let* ((dirname (if (stringp dired-directory)
933 dired-directory
934 (car dired-directory)))
935 (idx (1- (length dirname))))
936 ;; Strip the trailing slash.
937 (if (= ?/ (aref dirname idx))
938 (file-name-nondirectory (substring dirname 0 idx))
939 ;; Else return the current-buffer
940 (buffer-name (current-buffer)))))
941 ;; If all else fails, use the buffer's name.
943 (buffer-name (current-buffer)))))
946 (defun bookmark-yank-word ()
947 "Get the next word from buffer `bookmark-current-buffer' and append
948 it to the name of the bookmark currently being set, advancing
949 `bookmark-yank-point' by one word."
950 (interactive)
951 (let ((string (with-current-buffer bookmark-current-buffer
952 (goto-char bookmark-yank-point)
953 (buffer-substring-no-properties
954 (point)
955 (progn
956 (forward-word 1)
957 (setq bookmark-yank-point (point)))))))
958 (insert string)))
960 (defun bookmark-buffer-file-name ()
961 "Return the current buffer's file in a way useful for bookmarks."
962 ;; Abbreviate the path, both so it's shorter and so it's more
963 ;; portable. E.g., the user's home dir might be a different
964 ;; path on different machines, but "~/" will still reach it.
965 (abbreviate-file-name
966 (cond
967 (buffer-file-name buffer-file-name)
968 ((and (boundp 'dired-directory) dired-directory)
969 (if (stringp dired-directory)
970 dired-directory
971 (car dired-directory)))
972 (t (error "Buffer not visiting a file or directory")))))
975 (defun bookmark-maybe-load-default-file ()
976 "If bookmarks have not been loaded from the default place, load them."
977 (and (not bookmarks-already-loaded)
978 (null bookmark-alist)
979 (prog2
980 (and
981 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
982 ;; to be renamed.
983 (file-exists-p (expand-file-name bookmark-old-default-file))
984 (not (file-exists-p (expand-file-name bookmark-default-file)))
985 (rename-file (expand-file-name bookmark-old-default-file)
986 (expand-file-name bookmark-default-file)))
987 ;; return t so the `and' will continue...
990 (file-readable-p (expand-file-name bookmark-default-file))
991 (bookmark-load bookmark-default-file t t)
992 (setq bookmarks-already-loaded t)))
995 (defun bookmark-maybe-sort-alist ()
996 "Return `bookmark-alist' for display.
997 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
998 (if bookmark-sort-flag
999 (sort (copy-alist bookmark-alist)
1000 (function
1001 (lambda (x y) (string-lessp (car x) (car y)))))
1002 bookmark-alist))
1005 (defvar bookmark-after-jump-hook nil
1006 "Hook run after `bookmark-jump' jumps to a bookmark.
1007 Useful for example to unhide text in `outline-mode'.")
1009 (defun bookmark--jump-via (bookmark display-function)
1010 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
1011 Bookmark may be a bookmark name (a string) or a bookmark record.
1013 After calling DISPLAY-FUNCTION, set window point to the point specified
1014 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
1015 any annotations for this bookmark."
1016 (bookmark-handle-bookmark bookmark)
1017 (save-current-buffer
1018 (funcall display-function (current-buffer)))
1019 (let ((win (get-buffer-window (current-buffer) 0)))
1020 (if win (set-window-point win (point))))
1021 ;; FIXME: we used to only run bookmark-after-jump-hook in
1022 ;; `bookmark-jump' itself, but in none of the other commands.
1023 (run-hooks 'bookmark-after-jump-hook)
1024 (if bookmark-automatically-show-annotations
1025 ;; if there is an annotation for this bookmark,
1026 ;; show it in a buffer.
1027 (bookmark-show-annotation bookmark)))
1030 ;;;###autoload
1031 (defun bookmark-jump (bookmark &optional display-func)
1032 "Jump to bookmark BOOKMARK (a point in some file).
1033 You may have a problem using this function if the value of variable
1034 `bookmark-alist' is nil. If that happens, you need to load in some
1035 bookmarks. See help on function `bookmark-load' for more about
1036 this.
1038 If the file pointed to by BOOKMARK no longer exists, you will be asked
1039 if you wish to give the bookmark a new location, and `bookmark-jump'
1040 will then jump to the new location, as well as recording it in place
1041 of the old one in the permanent bookmark record.
1043 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1044 the latter is usually only used by programmatic callers.
1046 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1047 bookmark. It defaults to `switch-to-buffer'. A typical value for
1048 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1049 (interactive
1050 (list (bookmark-completing-read "Jump to bookmark"
1051 bookmark-current-bookmark)))
1052 (unless bookmark
1053 (error "No bookmark specified"))
1054 (bookmark-maybe-historicize-string bookmark)
1055 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1058 ;;;###autoload
1059 (defun bookmark-jump-other-window (bookmark)
1060 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1061 (interactive
1062 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1063 bookmark-current-bookmark)))
1064 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1067 (defun bookmark-jump-noselect (bookmark)
1068 "Return the location pointed to by the bookmark BOOKMARK.
1069 The return value has the form (BUFFER . POINT).
1071 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1073 Note: this function is deprecated and is present for Emacs 22
1074 compatibility only."
1075 (save-excursion
1076 (bookmark-handle-bookmark bookmark)
1077 (cons (current-buffer) (point))))
1079 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1081 (defun bookmark-handle-bookmark (bookmark)
1082 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1083 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1085 Changes current buffer and point and returns nil, or signals a `file-error'.
1087 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1088 that file no longer exists, then offer interactively to relocate BOOKMARK."
1089 (condition-case err
1090 (funcall (or (bookmark-get-handler bookmark)
1091 'bookmark-default-handler)
1092 (bookmark-get-bookmark bookmark))
1093 (file-error
1094 ;; We were unable to find the marked file, so ask if user wants to
1095 ;; relocate the bookmark, else remind them to consider deletion.
1096 (when (stringp bookmark)
1097 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1098 ;; or a bookmark object. If it's an object, we assume it's a
1099 ;; bookmark used internally by some other package.
1100 (let ((file (bookmark-get-filename bookmark)))
1101 (when file ;Don't know how to relocate if there's no `file'.
1102 ;; If file is not a dir, directory-file-name just returns file.
1103 (let ((display-name (directory-file-name file)))
1104 (ding)
1105 ;; Dialog boxes can accept a file target, but usually don't
1106 ;; know how to accept a directory target (at least, this
1107 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1108 ;; true on Windows as well). So we suppress file dialogs
1109 ;; when relocating.
1110 (let ((use-dialog-box nil)
1111 (use-file-dialog nil))
1112 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1113 bookmark "\"? "))
1114 (progn
1115 (bookmark-relocate bookmark)
1116 ;; Try again.
1117 (funcall (or (bookmark-get-handler bookmark)
1118 'bookmark-default-handler)
1119 (bookmark-get-bookmark bookmark)))
1120 (message
1121 "Bookmark not relocated; consider removing it \(%s\)."
1122 bookmark)
1123 (signal (car err) (cdr err))))))))))
1124 ;; Added by db.
1125 (when (stringp bookmark)
1126 (setq bookmark-current-bookmark bookmark))
1127 nil)
1129 (put 'bookmark-error-no-filename
1130 'error-conditions
1131 '(error bookmark-errors bookmark-error-no-filename))
1132 (put 'bookmark-error-no-filename
1133 'error-message
1134 "Bookmark has no associated file (or directory)")
1136 (defun bookmark-default-handler (bmk-record)
1137 "Default handler to jump to a particular bookmark location.
1138 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1139 Changes current buffer and point and returns nil, or signals a `file-error'."
1140 (let ((file (bookmark-get-filename bmk-record))
1141 (forward-str (bookmark-get-front-context-string bmk-record))
1142 (behind-str (bookmark-get-rear-context-string bmk-record))
1143 (place (bookmark-get-position bmk-record)))
1144 (if (not file)
1145 (signal 'bookmark-error-no-filename (list 'stringp file))
1146 (set-buffer (find-file-noselect file))
1147 (if place (goto-char place))
1148 ;; Go searching forward first. Then, if forward-str exists and
1149 ;; was found in the file, we can search backward for behind-str.
1150 ;; Rationale is that if text was inserted between the two in the
1151 ;; file, it's better to be put before it so you can read it,
1152 ;; rather than after and remain perhaps unaware of the changes.
1153 (if forward-str
1154 (if (search-forward forward-str (point-max) t)
1155 (goto-char (match-beginning 0))))
1156 (if behind-str
1157 (if (search-backward behind-str (point-min) t)
1158 (goto-char (match-end 0)))))
1159 nil))
1161 ;;;###autoload
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 (expand-file-name
1174 (read-file-name
1175 (format "Relocate %s to: " bookmark)
1176 (file-name-directory bmrk-filename)))))
1177 (bookmark-set-filename bookmark newloc)
1178 (setq bookmark-alist-modification-count
1179 (1+ bookmark-alist-modification-count))
1180 (if (bookmark-time-to-save-p)
1181 (bookmark-save))
1182 (bookmark-bmenu-surreptitiously-rebuild-list)))
1185 ;;;###autoload
1186 (defun bookmark-insert-location (bookmark &optional no-history)
1187 "Insert the name of the file associated with BOOKMARK.
1188 BOOKMARK is a bookmark name (a string), not a bookmark record.
1190 Optional second arg NO-HISTORY means don't record this in the
1191 minibuffer history list `bookmark-history'."
1192 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1193 (or no-history (bookmark-maybe-historicize-string bookmark))
1194 (let ((start (point)))
1195 (prog1
1196 (insert (bookmark-location bookmark)) ; *Return this line*
1197 (if (display-mouse-p)
1198 (add-text-properties
1199 start
1200 (save-excursion (re-search-backward
1201 "[^ \t]")
1202 (1+ (point)))
1203 '(mouse-face highlight
1204 follow-link t
1205 help-echo "mouse-2: go to this bookmark in other window"))))))
1207 ;;;###autoload
1208 (defalias 'bookmark-locate 'bookmark-insert-location)
1210 (defun bookmark-location (bookmark)
1211 "Return the name of the file associated with BOOKMARK, or nil if none.
1212 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1213 (bookmark-maybe-load-default-file)
1214 (bookmark-get-filename bookmark))
1217 ;;;###autoload
1218 (defun bookmark-rename (old &optional new)
1219 "Change the name of OLD bookmark to NEW name.
1220 If called from keyboard, prompt for OLD and NEW. If called from
1221 menubar, select OLD from a menu and prompt for NEW.
1223 Both OLD and NEW are bookmark names (strings), never bookmark records.
1225 If called from Lisp, prompt for NEW if only OLD was passed as an
1226 argument. If called with two strings, then no prompting is done. You
1227 must pass at least OLD when calling from Lisp.
1229 While you are entering the new name, consecutive C-w's insert
1230 consecutive words from the text of the buffer into the new bookmark
1231 name."
1232 (interactive (list (bookmark-completing-read "Old bookmark name")))
1233 (bookmark-maybe-historicize-string old)
1234 (bookmark-maybe-load-default-file)
1236 (setq bookmark-yank-point (point))
1237 (setq bookmark-current-buffer (current-buffer))
1238 (let ((newname
1239 (or new ; use second arg, if non-nil
1240 (read-from-minibuffer
1241 "New name: "
1243 (let ((now-map (copy-keymap minibuffer-local-map)))
1244 (define-key now-map "\C-w" 'bookmark-yank-word)
1245 now-map)
1247 'bookmark-history))))
1248 (bookmark-set-name old newname)
1249 (setq bookmark-current-bookmark newname)
1250 (bookmark-bmenu-surreptitiously-rebuild-list)
1251 (setq bookmark-alist-modification-count
1252 (1+ bookmark-alist-modification-count))
1253 (if (bookmark-time-to-save-p)
1254 (bookmark-save))))
1257 ;;;###autoload
1258 (defun bookmark-insert (bookmark)
1259 "Insert the text of the file pointed to by bookmark BOOKMARK.
1260 BOOKMARK is a bookmark name (a string), not a bookmark record.
1262 You may have a problem using this function if the value of variable
1263 `bookmark-alist' is nil. If that happens, you need to load in some
1264 bookmarks. See help on function `bookmark-load' for more about
1265 this."
1266 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1267 (bookmark-maybe-historicize-string bookmark)
1268 (bookmark-maybe-load-default-file)
1269 (let ((orig-point (point))
1270 (str-to-insert
1271 (save-current-buffer
1272 (bookmark-handle-bookmark bookmark)
1273 (buffer-string))))
1274 (insert str-to-insert)
1275 (push-mark)
1276 (goto-char orig-point)))
1279 ;;;###autoload
1280 (defun bookmark-delete (bookmark &optional batch)
1281 "Delete BOOKMARK from the bookmark list.
1282 BOOKMARK is a bookmark name (a string), not a bookmark record.
1284 Removes only the first instance of a bookmark with that name. If
1285 there are one or more other bookmarks with the same name, they will
1286 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1287 one most recently used in this file, if any\).
1288 Optional second arg BATCH means don't update the bookmark list buffer,
1289 probably because we were called from there."
1290 (interactive
1291 (list (bookmark-completing-read "Delete bookmark"
1292 bookmark-current-bookmark)))
1293 (bookmark-maybe-historicize-string bookmark)
1294 (bookmark-maybe-load-default-file)
1295 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1296 (setq bookmark-alist (delq will-go bookmark-alist))
1297 ;; Added by db, nil bookmark-current-bookmark if the last
1298 ;; occurrence has been deleted
1299 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1300 (setq bookmark-current-bookmark nil)))
1301 (unless batch
1302 (bookmark-bmenu-surreptitiously-rebuild-list))
1303 (setq bookmark-alist-modification-count
1304 (1+ bookmark-alist-modification-count))
1305 (when (bookmark-time-to-save-p)
1306 (bookmark-save)))
1309 (defun bookmark-time-to-save-p (&optional final-time)
1310 "Return t if it is time to save bookmarks to disk, nil otherwise.
1311 Optional argument FINAL-TIME means this is being called when Emacs
1312 is being killed, so save even if `bookmark-save-flag' is a number and
1313 is greater than `bookmark-alist-modification-count'."
1314 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1315 (cond (final-time
1316 (and (> bookmark-alist-modification-count 0)
1317 bookmark-save-flag))
1318 ((numberp bookmark-save-flag)
1319 (>= bookmark-alist-modification-count bookmark-save-flag))
1321 nil)))
1324 ;;;###autoload
1325 (defun bookmark-write ()
1326 "Write bookmarks to a file (reading the file name with the minibuffer).
1327 Don't use this in Lisp programs; use `bookmark-save' instead."
1328 (interactive)
1329 (bookmark-maybe-load-default-file)
1330 (bookmark-save t))
1333 ;;;###autoload
1334 (defun bookmark-save (&optional parg file)
1335 "Save currently defined bookmarks.
1336 Saves by default in the file defined by the variable
1337 `bookmark-default-file'. With a prefix arg, save it in file FILE
1338 \(second argument\).
1340 If you are calling this from Lisp, the two arguments are PARG and
1341 FILE, and if you just want it to write to the default file, then
1342 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1343 instead. If you pass in one argument, and it is non-nil, then the
1344 user will be interactively queried for a file to save in.
1346 When you want to load in the bookmarks from a file, use
1347 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1348 for a file, defaulting to the file defined by variable
1349 `bookmark-default-file'."
1350 (interactive "P")
1351 (bookmark-maybe-load-default-file)
1352 (cond
1353 ((and (null parg) (null file))
1354 ;;whether interactive or not, write to default file
1355 (bookmark-write-file bookmark-default-file))
1356 ((and (null parg) file)
1357 ;;whether interactive or not, write to given file
1358 (bookmark-write-file file))
1359 ((and parg (not file))
1360 ;;have been called interactively w/ prefix arg
1361 (let ((file (read-file-name "File to save bookmarks in: ")))
1362 (bookmark-write-file file)))
1363 (t ; someone called us with prefix-arg *and* a file, so just write to file
1364 (bookmark-write-file file)))
1365 ;; signal that we have synced the bookmark file by setting this to
1366 ;; 0. If there was an error at any point before, it will not get
1367 ;; set, which is what we want.
1368 (setq bookmark-alist-modification-count 0))
1372 (defun bookmark-write-file (file)
1373 "Write `bookmark-alist' to FILE."
1374 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1375 (with-current-buffer (get-buffer-create " *Bookmarks*")
1376 (goto-char (point-min))
1377 (delete-region (point-min) (point-max))
1378 (let ((print-length nil)
1379 (print-level nil))
1380 (bookmark-insert-file-format-version-stamp)
1381 (insert "(")
1382 ;; Rather than a single call to `pp' we make one per bookmark.
1383 ;; Apparently `pp' has a poor algorithmic complexity, so this
1384 ;; scales a lot better. bug#4485.
1385 (dolist (i bookmark-alist) (pp i (current-buffer)))
1386 (insert ")")
1387 (let ((version-control
1388 (cond
1389 ((null bookmark-version-control) nil)
1390 ((eq 'never bookmark-version-control) 'never)
1391 ((eq 'nospecial bookmark-version-control) version-control)
1392 (t t))))
1393 (condition-case nil
1394 (write-region (point-min) (point-max) file)
1395 (file-error (message "Can't write %s" file)))
1396 (kill-buffer (current-buffer))
1397 (bookmark-maybe-message
1398 "Saving bookmarks to file %s...done" file)))))
1401 (defun bookmark-import-new-list (new-list)
1402 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1403 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1404 they conflict with existing bookmark names."
1405 (let ((names (bookmark-all-names)))
1406 (dolist (full-record new-list)
1407 (bookmark-maybe-rename full-record names)
1408 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1409 (push (bookmark-name-from-full-record full-record) names))))
1412 (defun bookmark-maybe-rename (full-record names)
1413 "Rename bookmark FULL-RECORD if its current name is already used.
1414 This is a helper for `bookmark-import-new-list'."
1415 (let ((found-name (bookmark-name-from-full-record full-record)))
1416 (if (member found-name names)
1417 ;; We've got a conflict, so generate a new name
1418 (let ((count 2)
1419 (new-name found-name))
1420 (while (member new-name names)
1421 (setq new-name (concat found-name (format "<%d>" count)))
1422 (setq count (1+ count)))
1423 (bookmark-set-name full-record new-name)))))
1426 ;;;###autoload
1427 (defun bookmark-load (file &optional overwrite no-msg)
1428 "Load bookmarks from FILE (which must be in bookmark format).
1429 Appends loaded bookmarks to the front of the list of bookmarks. If
1430 optional second argument OVERWRITE is non-nil, existing bookmarks are
1431 destroyed. Optional third arg NO-MSG means don't display any messages
1432 while loading.
1434 If you load a file that doesn't contain a proper bookmark alist, you
1435 will corrupt Emacs's bookmark list. Generally, you should only load
1436 in files that were created with the bookmark functions in the first
1437 place. Your own personal bookmark file, `~/.emacs.bmk', is
1438 maintained automatically by Emacs; you shouldn't need to load it
1439 explicitly.
1441 If you load a file containing bookmarks with the same names as
1442 bookmarks already present in your Emacs, the new bookmarks will get
1443 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1444 method buffers use to resolve name collisions."
1445 (interactive
1446 (list (read-file-name
1447 (format "Load bookmarks from: (%s) "
1448 bookmark-default-file)
1449 ;;Default might not be used often,
1450 ;;but there's no better default, and
1451 ;;I guess it's better than none at all.
1452 "~/" bookmark-default-file 'confirm)))
1453 (setq file (expand-file-name file))
1454 (if (not (file-readable-p file))
1455 (error "Cannot read bookmark file %s" file)
1456 (if (null no-msg)
1457 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1458 (with-current-buffer (let ((enable-local-variables nil))
1459 (find-file-noselect file))
1460 (goto-char (point-min))
1461 (bookmark-maybe-upgrade-file-format)
1462 (let ((blist (bookmark-alist-from-buffer)))
1463 (if (listp blist)
1464 (progn
1465 (if overwrite
1466 (progn
1467 (setq bookmark-alist blist)
1468 (setq bookmark-alist-modification-count 0))
1469 ;; else
1470 (bookmark-import-new-list blist)
1471 (setq bookmark-alist-modification-count
1472 (1+ bookmark-alist-modification-count)))
1473 (if (string-equal
1474 (expand-file-name bookmark-default-file)
1475 file)
1476 (setq bookmarks-already-loaded t))
1477 (bookmark-bmenu-surreptitiously-rebuild-list))
1478 (error "Invalid bookmark list in %s" file)))
1479 (kill-buffer (current-buffer)))
1480 (if (null no-msg)
1481 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1485 ;;; Code supporting the dired-like bookmark menu.
1486 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1489 (defvar bookmark-bmenu-bookmark-column nil)
1492 (defvar bookmark-bmenu-hidden-bookmarks ())
1495 (defvar bookmark-bmenu-mode-map
1496 (let ((map (make-keymap)))
1497 (suppress-keymap map t)
1498 (define-key map "q" 'quit-window)
1499 (define-key map "v" 'bookmark-bmenu-select)
1500 (define-key map "w" 'bookmark-bmenu-locate)
1501 (define-key map "2" 'bookmark-bmenu-2-window)
1502 (define-key map "1" 'bookmark-bmenu-1-window)
1503 (define-key map "j" 'bookmark-bmenu-this-window)
1504 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1505 (define-key map "f" 'bookmark-bmenu-this-window)
1506 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1507 (define-key map "o" 'bookmark-bmenu-other-window)
1508 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1509 (define-key map "s" 'bookmark-bmenu-save)
1510 (define-key map "k" 'bookmark-bmenu-delete)
1511 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1512 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1513 (define-key map "d" 'bookmark-bmenu-delete)
1514 (define-key map " " 'next-line)
1515 (define-key map "n" 'next-line)
1516 (define-key map "p" 'previous-line)
1517 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1518 (define-key map "?" 'describe-mode)
1519 (define-key map "u" 'bookmark-bmenu-unmark)
1520 (define-key map "m" 'bookmark-bmenu-mark)
1521 (define-key map "l" 'bookmark-bmenu-load)
1522 (define-key map "r" 'bookmark-bmenu-rename)
1523 (define-key map "R" 'bookmark-bmenu-relocate)
1524 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1525 (define-key map "a" 'bookmark-bmenu-show-annotation)
1526 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1527 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1528 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1529 map))
1531 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1532 ;; data.
1533 (put 'bookmark-bmenu-mode 'mode-class 'special)
1536 ;; todo: need to display whether or not bookmark exists as a buffer in
1537 ;; flag column.
1539 ;; Format:
1540 ;; FLAGS BOOKMARK [ LOCATION ]
1543 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1544 "Rebuild the Bookmark List if it exists.
1545 Don't affect the buffer ring order."
1546 (if (get-buffer "*Bookmark List*")
1547 (save-excursion
1548 (save-window-excursion
1549 (bookmark-bmenu-list)))))
1552 ;;;###autoload
1553 (defun bookmark-bmenu-list ()
1554 "Display a list of existing bookmarks.
1555 The list is displayed in a buffer named `*Bookmark List*'.
1556 The leftmost column displays a D if the bookmark is flagged for
1557 deletion, or > if it is flagged for displaying."
1558 (interactive)
1559 (bookmark-maybe-load-default-file)
1560 (let ((buf (get-buffer-create "*Bookmark List*")))
1561 (if (called-interactively-p 'interactive)
1562 (if (or (window-dedicated-p) (window-minibuffer-p))
1563 (pop-to-buffer buf)
1564 (switch-to-buffer buf))
1565 (set-buffer buf)))
1566 (let ((inhibit-read-only t))
1567 (erase-buffer)
1568 (insert "% Bookmark\n- --------\n")
1569 (add-text-properties (point-min) (point)
1570 '(font-lock-face bookmark-menu-heading))
1571 (dolist (full-record (bookmark-maybe-sort-alist))
1572 ;; if a bookmark has an annotation, prepend a "*"
1573 ;; in the list of bookmarks.
1574 (let ((annotation (bookmark-get-annotation full-record)))
1575 (insert (if (and annotation (not (string-equal annotation "")))
1576 " *" " "))
1577 (let ((start (point)))
1578 (insert (bookmark-name-from-full-record full-record))
1579 (if (display-mouse-p)
1580 (add-text-properties
1581 start
1582 (save-excursion (re-search-backward
1583 "[^ \t]")
1584 (1+ (point)))
1585 '(mouse-face highlight
1586 follow-link t
1587 help-echo "mouse-2: go to this bookmark in other window")))
1588 (insert "\n")))))
1590 (goto-char (point-min))
1591 (forward-line 2)
1592 (bookmark-bmenu-mode)
1593 (if bookmark-bmenu-toggle-filenames
1594 (bookmark-bmenu-toggle-filenames t)))
1596 ;;;###autoload
1597 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1598 ;;;###autoload
1599 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1603 (defun bookmark-bmenu-mode ()
1604 "Major mode for editing a list of bookmarks.
1605 Each line describes one of the bookmarks in Emacs.
1606 Letters do not insert themselves; instead, they are commands.
1607 Bookmark names preceded by a \"*\" have annotations.
1608 \\<bookmark-bmenu-mode-map>
1609 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1610 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1611 Also show bookmarks marked using m in other windows.
1612 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1613 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1614 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1615 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1616 together with bookmark selected before this one in another window.
1617 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1618 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1619 so the bookmark menu bookmark remains visible in its window.
1620 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1621 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1622 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1623 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1624 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1625 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1626 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1627 With a prefix arg, prompts for a file to save in.
1628 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1629 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1630 With prefix argument, also move up one line.
1631 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1632 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1633 in another buffer.
1634 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1635 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1636 (kill-all-local-variables)
1637 (use-local-map bookmark-bmenu-mode-map)
1638 (setq truncate-lines t)
1639 (setq buffer-read-only t)
1640 (setq major-mode 'bookmark-bmenu-mode)
1641 (setq mode-name "Bookmark Menu")
1642 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1645 (defun bookmark-bmenu-toggle-filenames (&optional show)
1646 "Toggle whether filenames are shown in the bookmark list.
1647 Optional argument SHOW means show them unconditionally."
1648 (interactive)
1649 (cond
1650 (show
1651 (setq bookmark-bmenu-toggle-filenames nil)
1652 (bookmark-bmenu-show-filenames)
1653 (setq bookmark-bmenu-toggle-filenames t))
1654 (bookmark-bmenu-toggle-filenames
1655 (bookmark-bmenu-hide-filenames)
1656 (setq bookmark-bmenu-toggle-filenames nil))
1658 (bookmark-bmenu-show-filenames)
1659 (setq bookmark-bmenu-toggle-filenames t))))
1662 (defun bookmark-bmenu-show-filenames (&optional force)
1663 "In an interactive bookmark list, show filenames along with bookmarks.
1664 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1665 mainly for debugging, and should not be necessary in normal use."
1666 (if (and (not force) bookmark-bmenu-toggle-filenames)
1667 nil ;already shown, so do nothing
1668 (save-excursion
1669 (save-window-excursion
1670 (goto-char (point-min))
1671 (forward-line 2)
1672 (setq bookmark-bmenu-hidden-bookmarks ())
1673 (let ((inhibit-read-only t))
1674 (while (< (point) (point-max))
1675 (let ((bmrk (bookmark-bmenu-bookmark)))
1676 (setq bookmark-bmenu-hidden-bookmarks
1677 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1678 (let ((start (save-excursion (end-of-line) (point))))
1679 (move-to-column bookmark-bmenu-file-column t)
1680 ;; Strip off `mouse-face' from the white spaces region.
1681 (if (display-mouse-p)
1682 (remove-text-properties start (point)
1683 '(mouse-face nil help-echo nil))))
1684 (delete-region (point) (progn (end-of-line) (point)))
1685 (insert " ")
1686 ;; Pass the NO-HISTORY arg:
1687 (bookmark-insert-location bmrk t)
1688 (forward-line 1))))))))
1691 (defun bookmark-bmenu-hide-filenames (&optional force)
1692 "In an interactive bookmark list, hide the filenames of the bookmarks.
1693 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1694 mainly for debugging, and should not be necessary in normal use."
1695 (if (and (not force) bookmark-bmenu-toggle-filenames)
1696 ;; nothing to hide if above is nil
1697 (save-excursion
1698 (save-window-excursion
1699 (goto-char (point-min))
1700 (forward-line 2)
1701 (setq bookmark-bmenu-hidden-bookmarks
1702 (nreverse bookmark-bmenu-hidden-bookmarks))
1703 (save-excursion
1704 (goto-char (point-min))
1705 (search-forward "Bookmark")
1706 (backward-word 1)
1707 (setq bookmark-bmenu-bookmark-column (current-column)))
1708 (save-excursion
1709 (let ((inhibit-read-only t))
1710 (while bookmark-bmenu-hidden-bookmarks
1711 (move-to-column bookmark-bmenu-bookmark-column t)
1712 (bookmark-kill-line)
1713 (let ((start (point)))
1714 (insert (car bookmark-bmenu-hidden-bookmarks))
1715 (if (display-mouse-p)
1716 (add-text-properties
1717 start
1718 (save-excursion (re-search-backward
1719 "[^ \t]")
1720 (1+ (point)))
1721 '(mouse-face highlight
1722 follow-link t
1723 help-echo
1724 "mouse-2: go to this bookmark in other window"))))
1725 (setq bookmark-bmenu-hidden-bookmarks
1726 (cdr bookmark-bmenu-hidden-bookmarks))
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 (if (bookmark-bmenu-check-position)
1755 (save-excursion
1756 (save-window-excursion
1757 (goto-char (point-min))
1758 (search-forward "Bookmark")
1759 (backward-word 1)
1760 (setq bookmark-bmenu-bookmark-column (current-column)))))
1761 (if bookmark-bmenu-toggle-filenames
1762 (bookmark-bmenu-hide-filenames))
1763 (save-excursion
1764 (save-window-excursion
1765 (beginning-of-line)
1766 (forward-char bookmark-bmenu-bookmark-column)
1767 (prog1
1768 (buffer-substring-no-properties (point)
1769 (progn
1770 (end-of-line)
1771 (point)))
1772 ;; well, this is certainly crystal-clear:
1773 (if bookmark-bmenu-toggle-filenames
1774 (bookmark-bmenu-toggle-filenames t))))))
1777 (defun bookmark-show-annotation (bookmark)
1778 "Display the annotation for bookmark named BOOKMARK in a buffer,
1779 if an annotation exists."
1780 (let ((annotation (bookmark-get-annotation bookmark)))
1781 (if (and annotation (not (string-equal annotation "")))
1782 (save-excursion
1783 (let ((old-buf (current-buffer)))
1784 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1785 (delete-region (point-min) (point-max))
1786 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1787 (insert annotation)
1788 (goto-char (point-min))
1789 (pop-to-buffer old-buf))))))
1792 (defun bookmark-show-all-annotations ()
1793 "Display the annotations for all bookmarks in a buffer."
1794 (save-selected-window
1795 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1796 (delete-region (point-min) (point-max))
1797 (dolist (full-record bookmark-alist)
1798 (let* ((name (bookmark-name-from-full-record full-record))
1799 (ann (bookmark-get-annotation full-record)))
1800 (insert (concat name ":\n"))
1801 (if (and ann (not (string-equal ann "")))
1802 ;; insert the annotation, indented by 4 spaces.
1803 (progn
1804 (save-excursion (insert ann) (unless (bolp)
1805 (insert "\n")))
1806 (while (< (point) (point-max))
1807 (beginning-of-line) ; paranoia
1808 (insert " ")
1809 (forward-line)
1810 (end-of-line))))))
1811 (goto-char (point-min))))
1814 (defun bookmark-bmenu-mark ()
1815 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1816 (interactive)
1817 (beginning-of-line)
1818 (if (bookmark-bmenu-check-position)
1819 (let ((inhibit-read-only t))
1820 (delete-char 1)
1821 (insert ?>)
1822 (forward-line 1)
1823 (bookmark-bmenu-check-position))))
1826 (defun bookmark-bmenu-select ()
1827 "Select this line's bookmark; also display bookmarks marked with `>'.
1828 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1829 (interactive)
1830 (if (bookmark-bmenu-check-position)
1831 (let ((bmrk (bookmark-bmenu-bookmark))
1832 (menu (current-buffer))
1833 (others ())
1834 tem)
1835 (goto-char (point-min))
1836 (while (re-search-forward "^>" nil t)
1837 (setq tem (bookmark-bmenu-bookmark))
1838 (let ((inhibit-read-only t))
1839 (delete-char -1)
1840 (insert ?\s))
1841 (or (string-equal tem bmrk)
1842 (member tem others)
1843 (setq others (cons tem others))))
1844 (setq others (nreverse others)
1845 tem (/ (1- (frame-height)) (1+ (length others))))
1846 (delete-other-windows)
1847 (bookmark-jump bmrk)
1848 (bury-buffer menu)
1849 (if others
1850 (while others
1851 (split-window nil tem)
1852 (other-window 1)
1853 (bookmark-jump (car others))
1854 (setq others (cdr others)))
1855 (other-window 1)))))
1858 (defun bookmark-bmenu-save (parg)
1859 "Save the current list into a bookmark file.
1860 With a prefix arg, prompts for a file to save them in."
1861 (interactive "P")
1862 (save-excursion
1863 (save-window-excursion
1864 (bookmark-save parg))))
1867 (defun bookmark-bmenu-load ()
1868 "Load the bookmark file and rebuild the bookmark menu-buffer."
1869 (interactive)
1870 (if (bookmark-bmenu-check-position)
1871 (save-excursion
1872 (save-window-excursion
1873 ;; This will call `bookmark-bmenu-list'
1874 (call-interactively 'bookmark-load)))))
1877 (defun bookmark-bmenu-1-window ()
1878 "Select this line's bookmark, alone, in full frame."
1879 (interactive)
1880 (if (bookmark-bmenu-check-position)
1881 (progn
1882 (bookmark-jump (bookmark-bmenu-bookmark))
1883 (bury-buffer (other-buffer))
1884 (delete-other-windows))))
1887 (defun bookmark-bmenu-2-window ()
1888 "Select this line's bookmark, with previous buffer in second window."
1889 (interactive)
1890 (if (bookmark-bmenu-check-position)
1891 (let ((bmrk (bookmark-bmenu-bookmark))
1892 (menu (current-buffer))
1893 (pop-up-windows t))
1894 (delete-other-windows)
1895 (switch-to-buffer (other-buffer))
1896 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1897 (bookmark--jump-via bmrk 'pop-to-buffer))
1898 (bury-buffer menu))))
1901 (defun bookmark-bmenu-this-window ()
1902 "Select this line's bookmark in this window."
1903 (interactive)
1904 (if (bookmark-bmenu-check-position)
1905 (bookmark-jump (bookmark-bmenu-bookmark))))
1908 (defun bookmark-bmenu-other-window ()
1909 "Select this line's bookmark in other window, leaving bookmark menu visible."
1910 (interactive)
1911 (let ((bookmark (bookmark-bmenu-bookmark)))
1912 (if (bookmark-bmenu-check-position)
1913 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1914 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1917 (defun bookmark-bmenu-switch-other-window ()
1918 "Make the other window select this line's bookmark.
1919 The current window remains selected."
1920 (interactive)
1921 (let ((bookmark (bookmark-bmenu-bookmark))
1922 (pop-up-windows t)
1923 same-window-buffer-names
1924 same-window-regexps)
1925 (if (bookmark-bmenu-check-position)
1926 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1927 (bookmark--jump-via bookmark 'display-buffer)))))
1929 (defun bookmark-bmenu-other-window-with-mouse (event)
1930 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1931 (interactive "e")
1932 (with-current-buffer (window-buffer (posn-window (event-end event)))
1933 (save-excursion
1934 (goto-char (posn-point (event-end event)))
1935 (bookmark-bmenu-other-window))))
1938 (defun bookmark-bmenu-show-annotation ()
1939 "Show the annotation for the current bookmark in another window."
1940 (interactive)
1941 (let ((bookmark (bookmark-bmenu-bookmark)))
1942 (if (bookmark-bmenu-check-position)
1943 (bookmark-show-annotation bookmark))))
1946 (defun bookmark-bmenu-show-all-annotations ()
1947 "Show the annotation for all bookmarks in another window."
1948 (interactive)
1949 (bookmark-show-all-annotations))
1952 (defun bookmark-bmenu-edit-annotation ()
1953 "Edit the annotation for the current bookmark in another window."
1954 (interactive)
1955 (let ((bookmark (bookmark-bmenu-bookmark)))
1956 (if (bookmark-bmenu-check-position)
1957 (bookmark-edit-annotation bookmark))))
1960 (defun bookmark-bmenu-unmark (&optional backup)
1961 "Cancel all requested operations on bookmark on this line and move down.
1962 Optional BACKUP means move up."
1963 (interactive "P")
1964 (beginning-of-line)
1965 (if (bookmark-bmenu-check-position)
1966 (progn
1967 (let ((inhibit-read-only t))
1968 (delete-char 1)
1969 ;; any flags to reset according to circumstances? How about a
1970 ;; flag indicating whether this bookmark is being visited?
1971 ;; well, we don't have this now, so maybe later.
1972 (insert " "))
1973 (forward-line (if backup -1 1))
1974 (bookmark-bmenu-check-position))))
1977 (defun bookmark-bmenu-backup-unmark ()
1978 "Move up and cancel all requested operations on bookmark on line above."
1979 (interactive)
1980 (forward-line -1)
1981 (if (bookmark-bmenu-check-position)
1982 (progn
1983 (bookmark-bmenu-unmark)
1984 (forward-line -1)
1985 (bookmark-bmenu-check-position))))
1988 (defun bookmark-bmenu-delete ()
1989 "Mark bookmark on this line to be deleted.
1990 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1991 (interactive)
1992 (beginning-of-line)
1993 (if (bookmark-bmenu-check-position)
1994 (let ((inhibit-read-only t))
1995 (delete-char 1)
1996 (insert ?D)
1997 (forward-line 1)
1998 (bookmark-bmenu-check-position))))
2001 (defun bookmark-bmenu-delete-backwards ()
2002 "Mark bookmark on this line to be deleted, then move up one line.
2003 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2004 (interactive)
2005 (bookmark-bmenu-delete)
2006 (forward-line -2)
2007 (if (bookmark-bmenu-check-position)
2008 (forward-line 1))
2009 (bookmark-bmenu-check-position))
2012 (defun bookmark-bmenu-execute-deletions ()
2013 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2014 (interactive)
2015 (message "Deleting bookmarks...")
2016 (let ((hide-em bookmark-bmenu-toggle-filenames)
2017 (o-point (point))
2018 (o-str (save-excursion
2019 (beginning-of-line)
2020 (if (looking-at "^D")
2022 (buffer-substring
2023 (point)
2024 (progn (end-of-line) (point))))))
2025 (o-col (current-column)))
2026 (if hide-em (bookmark-bmenu-hide-filenames))
2027 (setq bookmark-bmenu-toggle-filenames nil)
2028 (goto-char (point-min))
2029 (forward-line 1)
2030 (while (re-search-forward "^D" (point-max) t)
2031 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2032 (bookmark-bmenu-list)
2033 (setq bookmark-bmenu-toggle-filenames hide-em)
2034 (if bookmark-bmenu-toggle-filenames
2035 (bookmark-bmenu-toggle-filenames t))
2036 (if o-str
2037 (progn
2038 (goto-char (point-min))
2039 (search-forward o-str)
2040 (beginning-of-line)
2041 (forward-char o-col))
2042 (goto-char o-point))
2043 (beginning-of-line)
2044 (message "Deleting bookmarks...done")
2048 (defun bookmark-bmenu-rename ()
2049 "Rename bookmark on current line. Prompts for a new name."
2050 (interactive)
2051 (if (bookmark-bmenu-check-position)
2052 (let ((bmrk (bookmark-bmenu-bookmark))
2053 (thispoint (point)))
2054 (bookmark-rename bmrk)
2055 (goto-char thispoint))))
2058 (defun bookmark-bmenu-locate ()
2059 "Display location of this bookmark. Displays in the minibuffer."
2060 (interactive)
2061 (if (bookmark-bmenu-check-position)
2062 (let ((bmrk (bookmark-bmenu-bookmark)))
2063 (message "%s" (bookmark-location bmrk)))))
2065 (defun bookmark-bmenu-relocate ()
2066 "Change the file path of the bookmark on the current line,
2067 prompting with completion for the new path."
2068 (interactive)
2069 (if (bookmark-bmenu-check-position)
2070 (let ((bmrk (bookmark-bmenu-bookmark))
2071 (thispoint (point)))
2072 (bookmark-relocate bmrk)
2073 (goto-char thispoint))))
2076 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2078 (defun bookmark-menu-popup-paned-menu (event name entries)
2079 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2080 That is, ENTRIES is a list of strings which appear as the choices
2081 in the menu.
2082 The number of panes depends on the number of entries.
2083 The visible entries are truncated to `bookmark-menu-length', but the
2084 strings returned are not."
2085 (let ((f-height (/ (frame-height) 2))
2086 (pane-list nil)
2087 (iter 0))
2088 (while entries
2089 (let (lst
2090 (count 0))
2091 (while (and (< count f-height) entries)
2092 (let ((str (car entries)))
2093 (push (cons
2094 (if (> (length str) bookmark-menu-length)
2095 (substring str 0 bookmark-menu-length)
2096 str)
2097 str)
2098 lst)
2099 (setq entries (cdr entries))
2100 (setq count (1+ count))))
2101 (setq iter (1+ iter))
2102 (push (cons
2103 (format "-*- %s (%d) -*-" name iter)
2104 (nreverse lst))
2105 pane-list)))
2107 ;; Popup the menu and return the string.
2108 (x-popup-menu event (cons (concat "-*- " name " -*-")
2109 (nreverse pane-list)))))
2112 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2113 ;; following works, and for explaining what to do to make it work.
2115 ;; We MUST autoload EACH form used to set up this variable's value, so
2116 ;; that the whole job is done in loaddefs.el.
2118 ;; Emacs menubar stuff.
2120 ;;;###autoload
2121 (defvar menu-bar-bookmark-map
2122 (let ((map (make-sparse-keymap "Bookmark functions")))
2123 (define-key map [load]
2124 `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
2125 :help ,(purecopy "Load bookmarks from a bookmark file)")))
2126 (define-key map [write]
2127 `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
2128 :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
2129 (define-key map [save]
2130 `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
2131 :help ,(purecopy "Save currently defined bookmarks")))
2132 (define-key map [edit]
2133 `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
2134 :help ,(purecopy "Display a list of existing bookmarks")))
2135 (define-key map [delete]
2136 `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
2137 :help ,(purecopy "Delete a bookmark from the bookmark list")))
2138 (define-key map [rename]
2139 `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
2140 :help ,(purecopy "Change the name of a bookmark")))
2141 (define-key map [locate]
2142 `(menu-item ,(purecopy "Insert Location...") bookmark-locate
2143 :help ,(purecopy "Insert the name of the file associated with a bookmark")))
2144 (define-key map [insert]
2145 `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
2146 :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
2147 (define-key map [set]
2148 `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
2149 :help ,(purecopy "Set a bookmark named inside a file.")))
2150 (define-key map [jump]
2151 `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
2152 :help ,(purecopy "Jump to a bookmark (a point in some file)")))
2153 map))
2155 ;;;###autoload
2156 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2158 ;; make bookmarks appear toward the right side of the menu.
2159 (if (boundp 'menu-bar-final-items)
2160 (if menu-bar-final-items
2161 (push 'bookmark menu-bar-final-items))
2162 (setq menu-bar-final-items '(bookmark)))
2164 ;;;; end bookmark menu stuff ;;;;
2167 ;; Load Hook
2168 (defvar bookmark-load-hook nil
2169 "Hook run at the end of loading bookmark.")
2171 ;; Exit Hook, called from kill-emacs-hook
2172 (defvar bookmark-exit-hook nil
2173 "Hook run when Emacs exits.")
2175 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2177 (defun bookmark-exit-hook-internal ()
2178 "Save bookmark state, if necessary, at Emacs exit time.
2179 This also runs `bookmark-exit-hook'."
2180 (run-hooks 'bookmark-exit-hook)
2181 (and bookmark-alist
2182 (bookmark-time-to-save-p t)
2183 (bookmark-save)))
2185 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2187 (defun bookmark-unload-function ()
2188 "Unload the Bookmark library."
2189 (when bookmark-save-flag (bookmark-save))
2190 ;; continue standard unloading
2191 nil)
2194 (run-hooks 'bookmark-load-hook)
2196 (provide 'bookmark)
2198 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2199 ;;; bookmark.el ends here