(bookmark-bmenu-list): Don't use switch-to-buffer if
[emacs.git] / lisp / bookmark.el
blob370f9de9cfaab9b18e5cae401768a081c9f06545
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 (bookmark-all-names))
463 (let* ((completion-ignore-case bookmark-completion-ignore-case)
464 (default default)
465 (prompt (if default
466 (concat prompt (format " (%s): " default))
467 (concat prompt ": ")))
468 (str
469 (completing-read prompt
470 bookmark-alist
474 'bookmark-history)))
475 (if (string-equal "" str) default str))))
478 (defmacro bookmark-maybe-historicize-string (string)
479 "Put STRING into the bookmark prompt history, if caller non-interactive.
480 We need this because sometimes bookmark functions are invoked from
481 menus, so `completing-read' never gets a chance to set `bookmark-history'."
482 `(or
483 (called-interactively-p 'interactive)
484 (setq bookmark-history (cons ,string bookmark-history))))
486 (defvar bookmark-make-record-function 'bookmark-make-record-default
487 "A function that should be called to create a bookmark record.
488 Modes may set this variable buffer-locally to enable bookmarking of
489 locations that should be treated specially, such as Info nodes,
490 news posts, images, pdf documents, etc.
492 The function will be called with no arguments.
493 It should signal a user error if it is unable to construct a record for
494 the current location.
496 The returned record should be a cons cell of the form (NAME . ALIST)
497 where ALIST is as described in `bookmark-alist' and may typically contain
498 a special cons (handler . HANDLER-FUNC) which specifies the handler function
499 that should be used instead of `bookmark-default-handler' to open this
500 bookmark. See the documentation for `bookmark-alist' for more.
502 NAME is a suggested name for the constructed bookmark. It can be nil
503 in which case a default heuristic will be used. The function can also
504 equivalently just return ALIST without NAME.")
506 (defun bookmark-make-record ()
507 "Return a new bookmark record (NAME . ALIST) for the current location."
508 (let ((record (funcall bookmark-make-record-function)))
509 ;; Set up default name.
510 (if (stringp (car record))
511 ;; The function already provided a default name.
512 record
513 (if (car record) (push nil record))
514 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
515 record)))
517 (defun bookmark-store (name alist no-overwrite)
518 "Store the bookmark NAME with data ALIST.
519 If NO-OVERWRITE is non-nil and another bookmark of the same name already
520 exists in `bookmark-alist', record the new bookmark without throwing away the
521 old one."
522 (bookmark-maybe-load-default-file)
523 (let ((stripped-name (copy-sequence name)))
524 (or (featurep 'xemacs)
525 ;; XEmacs's `set-text-properties' doesn't work on
526 ;; free-standing strings, apparently.
527 (set-text-properties 0 (length stripped-name) nil stripped-name))
528 (if (and (not no-overwrite)
529 (bookmark-get-bookmark stripped-name 'noerror))
530 ;; already existing bookmark under that name and
531 ;; no prefix arg means just overwrite old bookmark
532 ;; Use the new (NAME . ALIST) format.
533 (setcdr (bookmark-get-bookmark stripped-name) alist)
535 ;; otherwise just cons it onto the front (either the bookmark
536 ;; doesn't exist already, or there is no prefix arg. In either
537 ;; case, we want the new bookmark consed onto the alist...)
539 (push (cons stripped-name alist) bookmark-alist))
541 ;; Added by db
542 (setq bookmark-current-bookmark stripped-name)
543 (setq bookmark-alist-modification-count
544 (1+ bookmark-alist-modification-count))
545 (if (bookmark-time-to-save-p)
546 (bookmark-save))
548 (setq bookmark-current-bookmark stripped-name)
549 (bookmark-bmenu-surreptitiously-rebuild-list)))
551 (defun bookmark-make-record-default (&optional point-only)
552 "Return the record describing the location of a new bookmark.
553 Must be at the correct position in the buffer in which the bookmark is
554 being set.
555 If POINT-ONLY is non-nil, then only return the subset of the
556 record that pertains to the location within the buffer."
557 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
558 (front-context-string
559 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
560 (buffer-substring-no-properties
561 (point)
562 (+ (point) bookmark-search-size))
563 nil))
564 (rear-context-string
565 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
566 (buffer-substring-no-properties
567 (point)
568 (- (point) bookmark-search-size))
569 nil))
570 (position . ,(point))))
573 ;;; File format stuff
575 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
576 ;; the bookmark file format -- please don't. The current format
577 ;; should be extensible enough. If you feel the need to change it,
578 ;; please discuss it with other Emacs developers first.
580 ;; The format of `bookmark-alist' has changed twice in its lifetime.
581 ;; This comment describes the three formats, FIRST, SECOND, and
582 ;; CURRENT.
584 ;; The FIRST format was used prior to Emacs 20:
586 ;; ((BOOKMARK-NAME (FILENAME
587 ;; STRING-IN-FRONT
588 ;; STRING-BEHIND
589 ;; POINT))
590 ;; ...)
592 ;; The SECOND format was introduced in Emacs 20:
594 ;; ((BOOKMARK-NAME ((filename . FILENAME)
595 ;; (position . POS)
596 ;; (front-context-string . STR-AFTER-POS)
597 ;; (rear-context-string . STR-BEFORE-POS)
598 ;; (annotation . ANNOTATION)
599 ;; (whatever . VALUE)
600 ;; ...
601 ;; ))
602 ;; ...)
604 ;; The CURRENT format was introduced in Emacs 22:
606 ;; ((BOOKMARK-NAME (filename . FILENAME)
607 ;; (position . POS)
608 ;; (front-context-string . STR-AFTER-POS)
609 ;; (rear-context-string . STR-BEFORE-POS)
610 ;; (annotation . ANNOTATION)
611 ;; (whatever . VALUE)
612 ;; ...
613 ;; )
614 ;; ...)
616 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
617 ;; bookmark record is a list of entry information. FIRST and SECOND
618 ;; differ in the form of the record information: FIRST uses a list of
619 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
620 ;; the list elements matters. In the SECOND format, the order of the
621 ;; alist elements is unimportant. The SECOND format facilitates the
622 ;; addition of new kinds of elements, to support new kinds of
623 ;; bookmarks or code evolution.
625 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
626 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
627 ;; no longer a cons. Why that change was made remains a mystery --
628 ;; just be aware of it. (Be aware too that this explanatory comment
629 ;; was incorrect in Emacs 22 and Emacs 23.1.)
631 ;; To deal with the change from FIRST format to SECOND, conversion
632 ;; code was added, and it is still in use. See
633 ;; `bookmark-maybe-upgrade-file-format'.
635 ;; No conversion from SECOND to CURRENT is done. Instead, the code
636 ;; handles both formats OK. It must continue to do so.
638 ;; See the doc string of `bookmark-alist' for information about the
639 ;; elements that define a bookmark (e.g. `filename').
642 (defconst bookmark-file-format-version 1
643 "The current version of the format used by bookmark files.
644 You should never need to change this.")
647 (defconst bookmark-end-of-version-stamp-marker
648 "-*- End Of Bookmark File Format Version Stamp -*-\n"
649 "This string marks the end of the version stamp in a bookmark file.")
652 (defun bookmark-alist-from-buffer ()
653 "Return a `bookmark-alist' (in any format) from the current buffer.
654 The buffer must of course contain bookmark format information.
655 Does not care from where in the buffer it is called, and does not
656 affect point."
657 (save-excursion
658 (goto-char (point-min))
659 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
660 (read (current-buffer))
661 ;; Else we're dealing with format version 0
662 (if (search-forward "(" nil t)
663 (progn
664 (forward-char -1)
665 (read (current-buffer)))
666 ;; Else no hope of getting information here.
667 (error "Not bookmark format")))))
670 (defun bookmark-upgrade-version-0-alist (old-list)
671 "Upgrade a version 0 alist OLD-LIST to the current version."
672 (mapcar
673 (lambda (bookmark)
674 (let* ((name (car bookmark))
675 (record (car (cdr bookmark)))
676 (filename (nth 0 record))
677 (front-str (nth 1 record))
678 (rear-str (nth 2 record))
679 (position (nth 3 record))
680 (ann (nth 4 record)))
681 (list
682 name
683 `((filename . ,filename)
684 (front-context-string . ,(or front-str ""))
685 (rear-context-string . ,(or rear-str ""))
686 (position . ,position)
687 (annotation . ,ann)))))
688 old-list))
691 (defun bookmark-upgrade-file-format-from-0 ()
692 "Upgrade a bookmark file of format 0 (the original format) to format 1.
693 This expects to be called from `point-min' in a bookmark file."
694 (message "Upgrading bookmark format from 0 to %d..."
695 bookmark-file-format-version)
696 (let* ((old-list (bookmark-alist-from-buffer))
697 (new-list (bookmark-upgrade-version-0-alist old-list)))
698 (delete-region (point-min) (point-max))
699 (bookmark-insert-file-format-version-stamp)
700 (pp new-list (current-buffer))
701 (save-buffer))
702 (goto-char (point-min))
703 (message "Upgrading bookmark format from 0 to %d...done"
704 bookmark-file-format-version)
708 (defun bookmark-grok-file-format-version ()
709 "Return an integer which is the file-format version of this bookmark file.
710 This expects to be called from `point-min' in a bookmark file."
711 (if (looking-at "^;;;;")
712 (save-excursion
713 (save-match-data
714 (re-search-forward "[0-9]")
715 (forward-char -1)
716 (read (current-buffer))))
717 ;; Else this is format version 0, the original one, which didn't
718 ;; even have version stamps.
722 (defun bookmark-maybe-upgrade-file-format ()
723 "Check the file-format version of this bookmark file.
724 If the version is not up-to-date, upgrade it automatically.
725 This expects to be called from `point-min' in a bookmark file."
726 (let ((version (bookmark-grok-file-format-version)))
727 (cond
728 ((= version bookmark-file-format-version)
729 ) ; home free -- version is current
730 ((= version 0)
731 (bookmark-upgrade-file-format-from-0))
733 (error "Bookmark file format version strangeness")))))
736 (defun bookmark-insert-file-format-version-stamp ()
737 "Insert text indicating current version of bookmark file format."
738 (insert
739 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
740 bookmark-file-format-version))
741 (insert ";;; This format is meant to be slightly human-readable;\n"
742 ";;; nevertheless, you probably don't want to edit it.\n"
743 ";;; "
744 bookmark-end-of-version-stamp-marker))
747 ;;; end file-format stuff
750 ;;; Generic helpers.
752 (defun bookmark-maybe-message (fmt &rest args)
753 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
754 (if (>= baud-rate 9600)
755 (apply 'message fmt args)))
758 ;;; Core code:
760 (defvar bookmark-minibuffer-read-name-map
761 (let ((map (make-sparse-keymap)))
762 (set-keymap-parent map minibuffer-local-map)
763 (define-key map "\C-w" 'bookmark-yank-word)
764 ;; This C-u binding might not be very useful any more now that we
765 ;; provide access to the default via the standard M-n binding.
766 ;; Maybe we should just remove it? --Stef-08
767 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
768 map))
770 ;;;###autoload
771 (defun bookmark-set (&optional name no-overwrite)
772 "Set a bookmark named NAME at the current location.
773 If name is nil, then prompt the user.
775 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
776 existing bookmark that has the same name as NAME, but instead push the
777 new bookmark onto the bookmark alist. The most recently set bookmark
778 with name NAME is thus the one in effect at any given time, but the
779 others are still there, should the user decide to delete the most
780 recent one.
782 To yank words from the text of the buffer and use them as part of the
783 bookmark name, type C-w while setting a bookmark. Successive C-w's
784 yank successive words.
786 Typing C-u inserts (at the bookmark name prompt) the name of the last
787 bookmark used in the document where the new bookmark is being set;
788 this helps you use a single bookmark name to track progress through a
789 large document. If there is no prior bookmark for this document, then
790 C-u inserts an appropriate name based on the buffer or file.
792 Use \\[bookmark-delete] to remove bookmarks \(you give it a name and
793 it removes only the first instance of a bookmark with that name from
794 the list of bookmarks.\)"
795 (interactive (list nil current-prefix-arg))
796 (let* ((record (bookmark-make-record))
797 (default (car record)))
799 (bookmark-maybe-load-default-file)
801 (setq bookmark-yank-point (point))
802 (setq bookmark-current-buffer (current-buffer))
804 (let ((str
805 (or name
806 (read-from-minibuffer
807 (format "Set bookmark (%s): " default)
809 bookmark-minibuffer-read-name-map
810 nil nil default))))
811 (and (string-equal str "") (setq str default))
812 (bookmark-store str (cdr record) no-overwrite)
814 ;; Ask for an annotation buffer for this bookmark
815 (when bookmark-use-annotations
816 (bookmark-edit-annotation str)))))
818 (defun bookmark-kill-line (&optional newline-too)
819 "Kill from point to end of line.
820 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
821 Does not affect the kill ring."
822 (let ((eol (save-excursion (end-of-line) (point))))
823 (delete-region (point) eol)
824 (if (and newline-too (looking-at "\n"))
825 (delete-char 1))))
828 ;; Defvars to avoid compilation warnings:
829 (defvar bookmark-annotation-name nil
830 "Variable holding the name of the bookmark.
831 This is used in `bookmark-edit-annotation' to record the bookmark
832 whose annotation is being edited.")
835 (defun bookmark-default-annotation-text (bookmark)
836 "Return default annotation text for BOOKMARK (a string, not a record).
837 The default annotation text is simply some text explaining how to use
838 annotations."
839 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
840 "# All lines which start with a '#' will be deleted.\n"
841 "# Type C-c C-c when done.\n#\n"
842 "# Author: " (user-full-name) " <" (user-login-name) "@"
843 (system-name) ">\n"
844 "# Date: " (current-time-string) "\n"))
847 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
848 "Function to return default text to use for a bookmark annotation.
849 It takes one argument, the name of the bookmark, as a string.")
850 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
851 'bookmark-edit-annotation-text-func "23.1")
853 (defvar bookmark-edit-annotation-mode-map
854 (let ((map (make-sparse-keymap)))
855 (set-keymap-parent map text-mode-map)
856 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
857 map)
858 "Keymap for editing an annotation of a bookmark.")
861 (defun bookmark-edit-annotation-mode (bookmark)
862 "Mode for editing the annotation of bookmark BOOKMARK.
863 When you have finished composing, type \\[bookmark-send-annotation].
865 BOOKMARK is a bookmark name (a string) or a bookmark record.
867 \\{bookmark-edit-annotation-mode-map}"
868 (interactive)
869 (kill-all-local-variables)
870 (make-local-variable 'bookmark-annotation-name)
871 (setq bookmark-annotation-name bookmark)
872 (use-local-map bookmark-edit-annotation-mode-map)
873 (setq major-mode 'bookmark-edit-annotation-mode
874 mode-name "Edit Bookmark Annotation")
875 (insert (funcall bookmark-edit-annotation-text-func bookmark))
876 (let ((annotation (bookmark-get-annotation bookmark)))
877 (if (and annotation (not (string-equal annotation "")))
878 (insert annotation)))
879 (run-mode-hooks 'text-mode-hook))
882 (defun bookmark-send-edited-annotation ()
883 "Use buffer contents as annotation for a bookmark.
884 Lines beginning with `#' are ignored."
885 (interactive)
886 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
887 (error "Not in bookmark-edit-annotation-mode"))
888 (goto-char (point-min))
889 (while (< (point) (point-max))
890 (if (looking-at "^#")
891 (bookmark-kill-line t)
892 (forward-line 1)))
893 ;; Take no chances with text properties.
894 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
895 (bookmark bookmark-annotation-name))
896 (bookmark-set-annotation bookmark annotation)
897 (bookmark-bmenu-surreptitiously-rebuild-list))
898 (kill-buffer (current-buffer)))
901 (defun bookmark-edit-annotation (bookmark)
902 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
903 BOOKMARK is a bookmark name (a string) or a bookmark record."
904 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
905 (bookmark-edit-annotation-mode bookmark))
908 (defun bookmark-insert-current-bookmark ()
909 "Insert into the bookmark name currently being set the value of
910 `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
911 to the buffer's file name if `bookmark-current-bookmark' is nil."
912 (interactive)
913 (let ((str
914 (with-current-buffer bookmark-current-buffer
915 (or bookmark-current-bookmark
916 (bookmark-buffer-name)))))
917 (insert str)))
920 (defun bookmark-buffer-name ()
921 "Return the name of the current buffer in a form usable as a bookmark name.
922 If the buffer is associated with a file or directory, use that name."
923 (cond
924 ;; Or are we a file?
925 (buffer-file-name (file-name-nondirectory buffer-file-name))
926 ;; Or are we a directory?
927 ((and (boundp 'dired-directory) dired-directory)
928 (let* ((dirname (if (stringp dired-directory)
929 dired-directory
930 (car dired-directory)))
931 (idx (1- (length dirname))))
932 ;; Strip the trailing slash.
933 (if (= ?/ (aref dirname idx))
934 (file-name-nondirectory (substring dirname 0 idx))
935 ;; Else return the current-buffer
936 (buffer-name (current-buffer)))))
937 ;; If all else fails, use the buffer's name.
939 (buffer-name (current-buffer)))))
942 (defun bookmark-yank-word ()
943 "Get the next word from buffer `bookmark-current-buffer' and append
944 it to the name of the bookmark currently being set, advancing
945 `bookmark-yank-point' by one word."
946 (interactive)
947 (let ((string (with-current-buffer bookmark-current-buffer
948 (goto-char bookmark-yank-point)
949 (buffer-substring-no-properties
950 (point)
951 (progn
952 (forward-word 1)
953 (setq bookmark-yank-point (point)))))))
954 (insert string)))
956 (defun bookmark-buffer-file-name ()
957 "Return the current buffer's file in a way useful for bookmarks."
958 ;; Abbreviate the path, both so it's shorter and so it's more
959 ;; portable. E.g., the user's home dir might be a different
960 ;; path on different machines, but "~/" will still reach it.
961 (abbreviate-file-name
962 (cond
963 (buffer-file-name buffer-file-name)
964 ((and (boundp 'dired-directory) dired-directory)
965 (if (stringp dired-directory)
966 dired-directory
967 (car dired-directory)))
968 (t (error "Buffer not visiting a file or directory")))))
971 (defun bookmark-maybe-load-default-file ()
972 "If bookmarks have not been loaded from the default place, load them."
973 (and (not bookmarks-already-loaded)
974 (null bookmark-alist)
975 (prog2
976 (and
977 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
978 ;; to be renamed.
979 (file-exists-p (expand-file-name bookmark-old-default-file))
980 (not (file-exists-p (expand-file-name bookmark-default-file)))
981 (rename-file (expand-file-name bookmark-old-default-file)
982 (expand-file-name bookmark-default-file)))
983 ;; return t so the `and' will continue...
986 (file-readable-p (expand-file-name bookmark-default-file))
987 (bookmark-load bookmark-default-file t t)
988 (setq bookmarks-already-loaded t)))
991 (defun bookmark-maybe-sort-alist ()
992 "Return `bookmark-alist' for display.
993 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
994 (if bookmark-sort-flag
995 (sort (copy-alist bookmark-alist)
996 (function
997 (lambda (x y) (string-lessp (car x) (car y)))))
998 bookmark-alist))
1001 (defvar bookmark-after-jump-hook nil
1002 "Hook run after `bookmark-jump' jumps to a bookmark.
1003 Useful for example to unhide text in `outline-mode'.")
1005 (defun bookmark--jump-via (bookmark display-function)
1006 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
1007 Bookmark may be a bookmark name (a string) or a bookmark record.
1009 After calling DISPLAY-FUNCTION, set window point to the point specified
1010 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
1011 any annotations for this bookmark."
1012 (bookmark-handle-bookmark bookmark)
1013 (save-current-buffer
1014 (funcall display-function (current-buffer)))
1015 (let ((win (get-buffer-window (current-buffer) 0)))
1016 (if win (set-window-point win (point))))
1017 ;; FIXME: we used to only run bookmark-after-jump-hook in
1018 ;; `bookmark-jump' itself, but in none of the other commands.
1019 (run-hooks 'bookmark-after-jump-hook)
1020 (if bookmark-automatically-show-annotations
1021 ;; if there is an annotation for this bookmark,
1022 ;; show it in a buffer.
1023 (bookmark-show-annotation bookmark)))
1026 ;;;###autoload
1027 (defun bookmark-jump (bookmark &optional display-func)
1028 "Jump to bookmark BOOKMARK (a point in some file).
1029 You may have a problem using this function if the value of variable
1030 `bookmark-alist' is nil. If that happens, you need to load in some
1031 bookmarks. See help on function `bookmark-load' for more about
1032 this.
1034 If the file pointed to by BOOKMARK no longer exists, you will be asked
1035 if you wish to give the bookmark a new location, and `bookmark-jump'
1036 will then jump to the new location, as well as recording it in place
1037 of the old one in the permanent bookmark record.
1039 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1040 the latter is usually only used by programmatic callers.
1042 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1043 bookmark. It defaults to `switch-to-buffer'. A typical value for
1044 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1045 (interactive
1046 (list (bookmark-completing-read "Jump to bookmark"
1047 bookmark-current-bookmark)))
1048 (unless bookmark
1049 (error "No bookmark specified"))
1050 (bookmark-maybe-historicize-string bookmark)
1051 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1054 ;;;###autoload
1055 (defun bookmark-jump-other-window (bookmark)
1056 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1057 (interactive
1058 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1059 bookmark-current-bookmark)))
1060 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1063 (defun bookmark-jump-noselect (bookmark)
1064 "Return the location pointed to by the bookmark BOOKMARK.
1065 The return value has the form (BUFFER . POINT).
1067 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1069 Note: this function is deprecated and is present for Emacs 22
1070 compatibility only."
1071 (save-excursion
1072 (bookmark-handle-bookmark bookmark)
1073 (cons (current-buffer) (point))))
1075 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1077 (defun bookmark-handle-bookmark (bookmark)
1078 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1079 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1081 Changes current buffer and point and returns nil, or signals a `file-error'.
1083 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1084 that file no longer exists, then offer interactively to relocate BOOKMARK."
1085 (condition-case err
1086 (funcall (or (bookmark-get-handler bookmark)
1087 'bookmark-default-handler)
1088 (bookmark-get-bookmark bookmark))
1089 (file-error
1090 ;; We were unable to find the marked file, so ask if user wants to
1091 ;; relocate the bookmark, else remind them to consider deletion.
1092 (when (stringp bookmark)
1093 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1094 ;; or a bookmark object. If it's an object, we assume it's a
1095 ;; bookmark used internally by some other package.
1096 (let ((file (bookmark-get-filename bookmark)))
1097 (when file ;Don't know how to relocate if there's no `file'.
1098 ;; If file is not a dir, directory-file-name just returns file.
1099 (let ((display-name (directory-file-name file)))
1100 (ding)
1101 ;; Dialog boxes can accept a file target, but usually don't
1102 ;; know how to accept a directory target (at least, this
1103 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1104 ;; true on Windows as well). So we suppress file dialogs
1105 ;; when relocating.
1106 (let ((use-dialog-box nil)
1107 (use-file-dialog nil))
1108 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1109 bookmark "\"? "))
1110 (progn
1111 (bookmark-relocate bookmark)
1112 ;; Try again.
1113 (funcall (or (bookmark-get-handler bookmark)
1114 'bookmark-default-handler)
1115 (bookmark-get-bookmark bookmark)))
1116 (message
1117 "Bookmark not relocated; consider removing it \(%s\)."
1118 bookmark)
1119 (signal (car err) (cdr err))))))))))
1120 ;; Added by db.
1121 (when (stringp bookmark)
1122 (setq bookmark-current-bookmark bookmark))
1123 nil)
1125 (put 'bookmark-error-no-filename
1126 'error-conditions
1127 '(error bookmark-errors bookmark-error-no-filename))
1128 (put 'bookmark-error-no-filename
1129 'error-message
1130 "Bookmark has no associated file (or directory)")
1132 (defun bookmark-default-handler (bmk-record)
1133 "Default handler to jump to a particular bookmark location.
1134 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1135 Changes current buffer and point and returns nil, or signals a `file-error'."
1136 (let ((file (bookmark-get-filename bmk-record))
1137 (forward-str (bookmark-get-front-context-string bmk-record))
1138 (behind-str (bookmark-get-rear-context-string bmk-record))
1139 (place (bookmark-get-position bmk-record)))
1140 (if (not file)
1141 (signal 'bookmark-error-no-filename (list 'stringp file))
1142 (set-buffer (find-file-noselect file))
1143 (if place (goto-char place))
1144 ;; Go searching forward first. Then, if forward-str exists and
1145 ;; was found in the file, we can search backward for behind-str.
1146 ;; Rationale is that if text was inserted between the two in the
1147 ;; file, it's better to be put before it so you can read it,
1148 ;; rather than after and remain perhaps unaware of the changes.
1149 (if forward-str
1150 (if (search-forward forward-str (point-max) t)
1151 (goto-char (match-beginning 0))))
1152 (if behind-str
1153 (if (search-backward behind-str (point-min) t)
1154 (goto-char (match-end 0)))))
1155 nil))
1157 ;;;###autoload
1158 (defun bookmark-relocate (bookmark)
1159 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1160 BOOKMARK is a bookmark name (a string), not a bookmark record.
1162 This makes an already existing bookmark point to that file, instead of
1163 the one it used to point at. Useful when a file has been renamed
1164 after a bookmark was set in it."
1165 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1166 (bookmark-maybe-historicize-string bookmark)
1167 (bookmark-maybe-load-default-file)
1168 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1169 (newloc (expand-file-name
1170 (read-file-name
1171 (format "Relocate %s to: " bookmark)
1172 (file-name-directory bmrk-filename)))))
1173 (bookmark-set-filename bookmark newloc)
1174 (setq bookmark-alist-modification-count
1175 (1+ bookmark-alist-modification-count))
1176 (if (bookmark-time-to-save-p)
1177 (bookmark-save))
1178 (bookmark-bmenu-surreptitiously-rebuild-list)))
1181 ;;;###autoload
1182 (defun bookmark-insert-location (bookmark &optional no-history)
1183 "Insert the name of the file associated with BOOKMARK.
1184 BOOKMARK is a bookmark name (a string), not a bookmark record.
1186 Optional second arg NO-HISTORY means don't record this in the
1187 minibuffer history list `bookmark-history'."
1188 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1189 (or no-history (bookmark-maybe-historicize-string bookmark))
1190 (let ((start (point)))
1191 (prog1
1192 (insert (bookmark-location bookmark)) ; *Return this line*
1193 (if (and (display-color-p) (display-mouse-p))
1194 (add-text-properties
1195 start
1196 (save-excursion (re-search-backward
1197 "[^ \t]")
1198 (1+ (point)))
1199 '(mouse-face highlight
1200 follow-link t
1201 help-echo "mouse-2: go to this bookmark in other window"))))))
1203 ;;;###autoload
1204 (defalias 'bookmark-locate 'bookmark-insert-location)
1206 (defun bookmark-location (bookmark)
1207 "Return the name of the file associated with BOOKMARK, or nil if none.
1208 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1209 (bookmark-maybe-load-default-file)
1210 (bookmark-get-filename bookmark))
1213 ;;;###autoload
1214 (defun bookmark-rename (old &optional new)
1215 "Change the name of OLD bookmark to NEW name.
1216 If called from keyboard, prompt for OLD and NEW. If called from
1217 menubar, select OLD from a menu and prompt for NEW.
1219 Both OLD and NEW are bookmark names (strings), never bookmark records.
1221 If called from Lisp, prompt for NEW if only OLD was passed as an
1222 argument. If called with two strings, then no prompting is done. You
1223 must pass at least OLD when calling from Lisp.
1225 While you are entering the new name, consecutive C-w's insert
1226 consecutive words from the text of the buffer into the new bookmark
1227 name."
1228 (interactive (list (bookmark-completing-read "Old bookmark name")))
1229 (bookmark-maybe-historicize-string old)
1230 (bookmark-maybe-load-default-file)
1232 (setq bookmark-yank-point (point))
1233 (setq bookmark-current-buffer (current-buffer))
1234 (let ((newname
1235 (or new ; use second arg, if non-nil
1236 (read-from-minibuffer
1237 "New name: "
1239 (let ((now-map (copy-keymap minibuffer-local-map)))
1240 (define-key now-map "\C-w" 'bookmark-yank-word)
1241 now-map)
1243 'bookmark-history))))
1244 (bookmark-set-name old newname)
1245 (setq bookmark-current-bookmark newname)
1246 (bookmark-bmenu-surreptitiously-rebuild-list)
1247 (setq bookmark-alist-modification-count
1248 (1+ bookmark-alist-modification-count))
1249 (if (bookmark-time-to-save-p)
1250 (bookmark-save))))
1253 ;;;###autoload
1254 (defun bookmark-insert (bookmark)
1255 "Insert the text of the file pointed to by bookmark BOOKMARK.
1256 BOOKMARK is a bookmark name (a string), not a bookmark record.
1258 You may have a problem using this function if the value of variable
1259 `bookmark-alist' is nil. If that happens, you need to load in some
1260 bookmarks. See help on function `bookmark-load' for more about
1261 this."
1262 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1263 (bookmark-maybe-historicize-string bookmark)
1264 (bookmark-maybe-load-default-file)
1265 (let ((orig-point (point))
1266 (str-to-insert
1267 (save-current-buffer
1268 (bookmark-handle-bookmark bookmark)
1269 (buffer-string))))
1270 (insert str-to-insert)
1271 (push-mark)
1272 (goto-char orig-point)))
1275 ;;;###autoload
1276 (defun bookmark-delete (bookmark &optional batch)
1277 "Delete BOOKMARK from the bookmark list.
1278 BOOKMARK is a bookmark name (a string), not a bookmark record.
1280 Removes only the first instance of a bookmark with that name. If
1281 there are one or more other bookmarks with the same name, they will
1282 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1283 one most recently used in this file, if any\).
1284 Optional second arg BATCH means don't update the bookmark list buffer,
1285 probably because we were called from there."
1286 (interactive
1287 (list (bookmark-completing-read "Delete bookmark"
1288 bookmark-current-bookmark)))
1289 (bookmark-maybe-historicize-string bookmark)
1290 (bookmark-maybe-load-default-file)
1291 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1292 (setq bookmark-alist (delq will-go bookmark-alist))
1293 ;; Added by db, nil bookmark-current-bookmark if the last
1294 ;; occurrence has been deleted
1295 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1296 (setq bookmark-current-bookmark nil)))
1297 (unless batch
1298 (bookmark-bmenu-surreptitiously-rebuild-list))
1299 (setq bookmark-alist-modification-count
1300 (1+ bookmark-alist-modification-count))
1301 (when (bookmark-time-to-save-p)
1302 (bookmark-save)))
1305 (defun bookmark-time-to-save-p (&optional final-time)
1306 "Return t if it is time to save bookmarks to disk, nil otherwise.
1307 Optional argument FINAL-TIME means this is being called when Emacs
1308 is being killed, so save even if `bookmark-save-flag' is a number and
1309 is greater than `bookmark-alist-modification-count'."
1310 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1311 (cond (final-time
1312 (and (> bookmark-alist-modification-count 0)
1313 bookmark-save-flag))
1314 ((numberp bookmark-save-flag)
1315 (>= bookmark-alist-modification-count bookmark-save-flag))
1317 nil)))
1320 ;;;###autoload
1321 (defun bookmark-write ()
1322 "Write bookmarks to a file (reading the file name with the minibuffer).
1323 Don't use this in Lisp programs; use `bookmark-save' instead."
1324 (interactive)
1325 (bookmark-maybe-load-default-file)
1326 (bookmark-save t))
1329 ;;;###autoload
1330 (defun bookmark-save (&optional parg file)
1331 "Save currently defined bookmarks.
1332 Saves by default in the file defined by the variable
1333 `bookmark-default-file'. With a prefix arg, save it in file FILE
1334 \(second argument\).
1336 If you are calling this from Lisp, the two arguments are PARG and
1337 FILE, and if you just want it to write to the default file, then
1338 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1339 instead. If you pass in one argument, and it is non-nil, then the
1340 user will be interactively queried for a file to save in.
1342 When you want to load in the bookmarks from a file, use
1343 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1344 for a file, defaulting to the file defined by variable
1345 `bookmark-default-file'."
1346 (interactive "P")
1347 (bookmark-maybe-load-default-file)
1348 (cond
1349 ((and (null parg) (null file))
1350 ;;whether interactive or not, write to default file
1351 (bookmark-write-file bookmark-default-file))
1352 ((and (null parg) file)
1353 ;;whether interactive or not, write to given file
1354 (bookmark-write-file file))
1355 ((and parg (not file))
1356 ;;have been called interactively w/ prefix arg
1357 (let ((file (read-file-name "File to save bookmarks in: ")))
1358 (bookmark-write-file file)))
1359 (t ; someone called us with prefix-arg *and* a file, so just write to file
1360 (bookmark-write-file file)))
1361 ;; signal that we have synced the bookmark file by setting this to
1362 ;; 0. If there was an error at any point before, it will not get
1363 ;; set, which is what we want.
1364 (setq bookmark-alist-modification-count 0))
1368 (defun bookmark-write-file (file)
1369 "Write `bookmark-alist' to FILE."
1370 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1371 (with-current-buffer (get-buffer-create " *Bookmarks*")
1372 (goto-char (point-min))
1373 (delete-region (point-min) (point-max))
1374 (let ((print-length nil)
1375 (print-level nil))
1376 (bookmark-insert-file-format-version-stamp)
1377 (insert "(")
1378 ;; Rather than a single call to `pp' we make one per bookmark.
1379 ;; Apparently `pp' has a poor algorithmic complexity, so this
1380 ;; scales a lot better. bug#4485.
1381 (dolist (i bookmark-alist) (pp i (current-buffer)))
1382 (insert ")")
1383 (let ((version-control
1384 (cond
1385 ((null bookmark-version-control) nil)
1386 ((eq 'never bookmark-version-control) 'never)
1387 ((eq 'nospecial bookmark-version-control) version-control)
1388 (t t))))
1389 (condition-case nil
1390 (write-region (point-min) (point-max) file)
1391 (file-error (message "Can't write %s" file)))
1392 (kill-buffer (current-buffer))
1393 (bookmark-maybe-message
1394 "Saving bookmarks to file %s...done" file)))))
1397 (defun bookmark-import-new-list (new-list)
1398 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1399 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1400 they conflict with existing bookmark names."
1401 (let ((lst new-list)
1402 (names (bookmark-all-names)))
1403 (while lst
1404 (let* ((full-record (car lst)))
1405 (bookmark-maybe-rename full-record names)
1406 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1407 (setq names (cons (bookmark-name-from-full-record full-record) names))
1408 (setq lst (cdr lst))))))
1411 (defun bookmark-maybe-rename (full-record names)
1412 "Rename bookmark FULL-RECORD if its current name is already used.
1413 This is a helper for `bookmark-import-new-list'."
1414 (let ((found-name (bookmark-name-from-full-record full-record)))
1415 (if (member found-name names)
1416 ;; We've got a conflict, so generate a new name
1417 (let ((count 2)
1418 (new-name found-name))
1419 (while (member new-name names)
1420 (setq new-name (concat found-name (format "<%d>" count)))
1421 (setq count (1+ count)))
1422 (bookmark-set-name full-record new-name)))))
1425 ;;;###autoload
1426 (defun bookmark-load (file &optional overwrite no-msg)
1427 "Load bookmarks from FILE (which must be in bookmark format).
1428 Appends loaded bookmarks to the front of the list of bookmarks. If
1429 optional second argument OVERWRITE is non-nil, existing bookmarks are
1430 destroyed. Optional third arg NO-MSG means don't display any messages
1431 while loading.
1433 If you load a file that doesn't contain a proper bookmark alist, you
1434 will corrupt Emacs's bookmark list. Generally, you should only load
1435 in files that were created with the bookmark functions in the first
1436 place. Your own personal bookmark file, `~/.emacs.bmk', is
1437 maintained automatically by Emacs; you shouldn't need to load it
1438 explicitly.
1440 If you load a file containing bookmarks with the same names as
1441 bookmarks already present in your Emacs, the new bookmarks will get
1442 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1443 method buffers use to resolve name collisions."
1444 (interactive
1445 (list (read-file-name
1446 (format "Load bookmarks from: (%s) "
1447 bookmark-default-file)
1448 ;;Default might not be used often,
1449 ;;but there's no better default, and
1450 ;;I guess it's better than none at all.
1451 "~/" bookmark-default-file 'confirm)))
1452 (setq file (expand-file-name file))
1453 (if (not (file-readable-p file))
1454 (error "Cannot read bookmark file %s" file)
1455 (if (null no-msg)
1456 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1457 (with-current-buffer (let ((enable-local-variables nil))
1458 (find-file-noselect file))
1459 (goto-char (point-min))
1460 (bookmark-maybe-upgrade-file-format)
1461 (let ((blist (bookmark-alist-from-buffer)))
1462 (if (listp blist)
1463 (progn
1464 (if overwrite
1465 (progn
1466 (setq bookmark-alist blist)
1467 (setq bookmark-alist-modification-count 0))
1468 ;; else
1469 (bookmark-import-new-list blist)
1470 (setq bookmark-alist-modification-count
1471 (1+ bookmark-alist-modification-count)))
1472 (if (string-equal
1473 (expand-file-name bookmark-default-file)
1474 file)
1475 (setq bookmarks-already-loaded t))
1476 (bookmark-bmenu-surreptitiously-rebuild-list))
1477 (error "Invalid bookmark list in %s" file)))
1478 (kill-buffer (current-buffer)))
1479 (if (null no-msg)
1480 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1484 ;;; Code supporting the dired-like bookmark menu.
1485 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1488 (defvar bookmark-bmenu-bookmark-column nil)
1491 (defvar bookmark-bmenu-hidden-bookmarks ())
1494 (defvar bookmark-bmenu-mode-map nil)
1497 (if bookmark-bmenu-mode-map
1499 (setq bookmark-bmenu-mode-map (make-keymap))
1500 (suppress-keymap bookmark-bmenu-mode-map t)
1501 (define-key bookmark-bmenu-mode-map "q" 'quit-window)
1502 (define-key bookmark-bmenu-mode-map "v" 'bookmark-bmenu-select)
1503 (define-key bookmark-bmenu-mode-map "w" 'bookmark-bmenu-locate)
1504 (define-key bookmark-bmenu-mode-map "2" 'bookmark-bmenu-2-window)
1505 (define-key bookmark-bmenu-mode-map "1" 'bookmark-bmenu-1-window)
1506 (define-key bookmark-bmenu-mode-map "j" 'bookmark-bmenu-this-window)
1507 (define-key bookmark-bmenu-mode-map "\C-c\C-c" 'bookmark-bmenu-this-window)
1508 (define-key bookmark-bmenu-mode-map "f" 'bookmark-bmenu-this-window)
1509 (define-key bookmark-bmenu-mode-map "\C-m" 'bookmark-bmenu-this-window)
1510 (define-key bookmark-bmenu-mode-map "o" 'bookmark-bmenu-other-window)
1511 (define-key bookmark-bmenu-mode-map "\C-o"
1512 'bookmark-bmenu-switch-other-window)
1513 (define-key bookmark-bmenu-mode-map "s" 'bookmark-bmenu-save)
1514 (define-key bookmark-bmenu-mode-map "k" 'bookmark-bmenu-delete)
1515 (define-key bookmark-bmenu-mode-map "\C-d" 'bookmark-bmenu-delete-backwards)
1516 (define-key bookmark-bmenu-mode-map "x" 'bookmark-bmenu-execute-deletions)
1517 (define-key bookmark-bmenu-mode-map "d" 'bookmark-bmenu-delete)
1518 (define-key bookmark-bmenu-mode-map " " 'next-line)
1519 (define-key bookmark-bmenu-mode-map "n" 'next-line)
1520 (define-key bookmark-bmenu-mode-map "p" 'previous-line)
1521 (define-key bookmark-bmenu-mode-map "\177" 'bookmark-bmenu-backup-unmark)
1522 (define-key bookmark-bmenu-mode-map "?" 'describe-mode)
1523 (define-key bookmark-bmenu-mode-map "u" 'bookmark-bmenu-unmark)
1524 (define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
1525 (define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
1526 (define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
1527 (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
1528 (define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
1529 (define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
1530 (define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
1531 (define-key bookmark-bmenu-mode-map "e" 'bookmark-bmenu-edit-annotation)
1532 (define-key bookmark-bmenu-mode-map [mouse-2]
1533 'bookmark-bmenu-other-window-with-mouse))
1537 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1538 ;; data.
1539 (put 'bookmark-bmenu-mode 'mode-class 'special)
1542 ;; todo: need to display whether or not bookmark exists as a buffer in
1543 ;; flag column.
1545 ;; Format:
1546 ;; FLAGS BOOKMARK [ LOCATION ]
1549 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1550 "Rebuild the Bookmark List if it exists.
1551 Don't affect the buffer ring order."
1552 (if (get-buffer "*Bookmark List*")
1553 (save-excursion
1554 (save-window-excursion
1555 (bookmark-bmenu-list)))))
1558 ;;;###autoload
1559 (defun bookmark-bmenu-list ()
1560 "Display a list of existing bookmarks.
1561 The list is displayed in a buffer named `*Bookmark List*'.
1562 The leftmost column displays a D if the bookmark is flagged for
1563 deletion, or > if it is flagged for displaying."
1564 (interactive)
1565 (bookmark-maybe-load-default-file)
1566 (let ((buf (get-buffer-create "*Bookmark List*")))
1567 (if (called-interactively-p 'interactive)
1568 (if (or (window-dedicated-p) (window-minibuffer-p))
1569 (pop-to-buffer buf)
1570 (switch-to-buffer buf))
1571 (set-buffer buf)))
1572 (let ((inhibit-read-only t))
1573 (erase-buffer)
1574 (insert "% Bookmark\n- --------\n")
1575 (add-text-properties (point-min) (point)
1576 '(font-lock-face bookmark-menu-heading))
1577 (mapc
1578 (lambda (full-record)
1579 ;; if a bookmark has an annotation, prepend a "*"
1580 ;; in the list of bookmarks.
1581 (let ((annotation (bookmark-get-annotation
1582 (bookmark-name-from-full-record full-record))))
1583 (if (and annotation (not (string-equal annotation "")))
1584 (insert " *")
1585 (insert " "))
1586 (let ((start (point)))
1587 (insert (bookmark-name-from-full-record full-record))
1588 (if (and (display-color-p) (display-mouse-p))
1589 (add-text-properties
1590 start
1591 (save-excursion (re-search-backward
1592 "[^ \t]")
1593 (1+ (point)))
1594 '(mouse-face highlight
1595 follow-link t
1596 help-echo "mouse-2: go to this bookmark in other window")))
1597 (insert "\n")
1599 (bookmark-maybe-sort-alist)))
1600 (goto-char (point-min))
1601 (forward-line 2)
1602 (bookmark-bmenu-mode)
1603 (if bookmark-bmenu-toggle-filenames
1604 (bookmark-bmenu-toggle-filenames t)))
1606 ;;;###autoload
1607 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1608 ;;;###autoload
1609 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1613 (defun bookmark-bmenu-mode ()
1614 "Major mode for editing a list of bookmarks.
1615 Each line describes one of the bookmarks in Emacs.
1616 Letters do not insert themselves; instead, they are commands.
1617 Bookmark names preceded by a \"*\" have annotations.
1618 \\<bookmark-bmenu-mode-map>
1619 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1620 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1621 Also show bookmarks marked using m in other windows.
1622 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1623 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1624 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1625 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1626 together with bookmark selected before this one in another window.
1627 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1628 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1629 so the bookmark menu bookmark remains visible in its window.
1630 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1631 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1632 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1633 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1634 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1635 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1636 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1637 With a prefix arg, prompts for a file to save in.
1638 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1639 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1640 With prefix argument, also move up one line.
1641 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1642 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1643 in another buffer.
1644 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1645 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1646 (kill-all-local-variables)
1647 (use-local-map bookmark-bmenu-mode-map)
1648 (setq truncate-lines t)
1649 (setq buffer-read-only t)
1650 (setq major-mode 'bookmark-bmenu-mode)
1651 (setq mode-name "Bookmark Menu")
1652 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1655 (defun bookmark-bmenu-toggle-filenames (&optional show)
1656 "Toggle whether filenames are shown in the bookmark list.
1657 Optional argument SHOW means show them unconditionally."
1658 (interactive)
1659 (cond
1660 (show
1661 (setq bookmark-bmenu-toggle-filenames nil)
1662 (bookmark-bmenu-show-filenames)
1663 (setq bookmark-bmenu-toggle-filenames t))
1664 (bookmark-bmenu-toggle-filenames
1665 (bookmark-bmenu-hide-filenames)
1666 (setq bookmark-bmenu-toggle-filenames nil))
1668 (bookmark-bmenu-show-filenames)
1669 (setq bookmark-bmenu-toggle-filenames t))))
1672 (defun bookmark-bmenu-show-filenames (&optional force)
1673 "In an interactive bookmark list, show filenames along with bookmarks.
1674 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1675 mainly for debugging, and should not be necessary in normal use."
1676 (if (and (not force) bookmark-bmenu-toggle-filenames)
1677 nil ;already shown, so do nothing
1678 (save-excursion
1679 (save-window-excursion
1680 (goto-char (point-min))
1681 (forward-line 2)
1682 (setq bookmark-bmenu-hidden-bookmarks ())
1683 (let ((inhibit-read-only t))
1684 (while (< (point) (point-max))
1685 (let ((bmrk (bookmark-bmenu-bookmark)))
1686 (setq bookmark-bmenu-hidden-bookmarks
1687 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1688 (let ((start (save-excursion (end-of-line) (point))))
1689 (move-to-column bookmark-bmenu-file-column t)
1690 ;; Strip off `mouse-face' from the white spaces region.
1691 (if (and (display-color-p) (display-mouse-p))
1692 (remove-text-properties start (point)
1693 '(mouse-face nil help-echo nil))))
1694 (delete-region (point) (progn (end-of-line) (point)))
1695 (insert " ")
1696 ;; Pass the NO-HISTORY arg:
1697 (bookmark-insert-location bmrk t)
1698 (forward-line 1))))))))
1701 (defun bookmark-bmenu-hide-filenames (&optional force)
1702 "In an interactive bookmark list, hide the filenames of the bookmarks.
1703 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1704 mainly for debugging, and should not be necessary in normal use."
1705 (if (and (not force) bookmark-bmenu-toggle-filenames)
1706 ;; nothing to hide if above is nil
1707 (save-excursion
1708 (save-window-excursion
1709 (goto-char (point-min))
1710 (forward-line 2)
1711 (setq bookmark-bmenu-hidden-bookmarks
1712 (nreverse bookmark-bmenu-hidden-bookmarks))
1713 (save-excursion
1714 (goto-char (point-min))
1715 (search-forward "Bookmark")
1716 (backward-word 1)
1717 (setq bookmark-bmenu-bookmark-column (current-column)))
1718 (save-excursion
1719 (let ((inhibit-read-only t))
1720 (while bookmark-bmenu-hidden-bookmarks
1721 (move-to-column bookmark-bmenu-bookmark-column t)
1722 (bookmark-kill-line)
1723 (let ((start (point)))
1724 (insert (car bookmark-bmenu-hidden-bookmarks))
1725 (if (and (display-color-p) (display-mouse-p))
1726 (add-text-properties
1727 start
1728 (save-excursion (re-search-backward
1729 "[^ \t]")
1730 (1+ (point)))
1731 '(mouse-face highlight
1732 follow-link t
1733 help-echo
1734 "mouse-2: go to this bookmark in other window"))))
1735 (setq bookmark-bmenu-hidden-bookmarks
1736 (cdr bookmark-bmenu-hidden-bookmarks))
1737 (forward-line 1))))))))
1740 (defun bookmark-bmenu-check-position ()
1741 "If point is not on a bookmark line, move it to one.
1742 If before the first bookmark line, move it to the first.
1743 If after the last, move it to the last.
1744 Return `bookmark-alist'"
1745 ;; FIXME: The doc string originally implied that this returns nil if
1746 ;; not on a bookmark, which is false. Is there any real reason to
1747 ;; return `bookmark-alist'? This seems to be called in a few places
1748 ;; as a check of whether point is on a bookmark line. Those
1749 ;; "checks" are in fact no-ops, since this never returns nil.
1750 ;; -dadams, 2009-10-10
1751 (cond ((< (count-lines (point-min) (point)) 2)
1752 (goto-char (point-min))
1753 (forward-line 2)
1754 bookmark-alist)
1755 ((and (bolp) (eobp))
1756 (beginning-of-line 0)
1757 bookmark-alist)
1759 bookmark-alist)))
1762 (defun bookmark-bmenu-bookmark ()
1763 "Return the bookmark for this line in an interactive bookmark list buffer."
1764 (if (bookmark-bmenu-check-position)
1765 (save-excursion
1766 (save-window-excursion
1767 (goto-char (point-min))
1768 (search-forward "Bookmark")
1769 (backward-word 1)
1770 (setq bookmark-bmenu-bookmark-column (current-column)))))
1771 (if bookmark-bmenu-toggle-filenames
1772 (bookmark-bmenu-hide-filenames))
1773 (save-excursion
1774 (save-window-excursion
1775 (beginning-of-line)
1776 (forward-char bookmark-bmenu-bookmark-column)
1777 (prog1
1778 (buffer-substring-no-properties (point)
1779 (progn
1780 (end-of-line)
1781 (point)))
1782 ;; well, this is certainly crystal-clear:
1783 (if bookmark-bmenu-toggle-filenames
1784 (bookmark-bmenu-toggle-filenames t))))))
1787 (defun bookmark-show-annotation (bookmark)
1788 "Display the annotation for bookmark named BOOKMARK in a buffer,
1789 if an annotation exists."
1790 (let ((annotation (bookmark-get-annotation bookmark)))
1791 (if (and annotation (not (string-equal annotation "")))
1792 (save-excursion
1793 (let ((old-buf (current-buffer)))
1794 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1795 (delete-region (point-min) (point-max))
1796 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1797 (insert annotation)
1798 (goto-char (point-min))
1799 (pop-to-buffer old-buf))))))
1802 (defun bookmark-show-all-annotations ()
1803 "Display the annotations for all bookmarks in a buffer."
1804 (let ((old-buf (current-buffer)))
1805 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1806 (delete-region (point-min) (point-max))
1807 (mapc
1808 (lambda (full-record)
1809 (let* ((name (bookmark-name-from-full-record full-record))
1810 (ann (bookmark-get-annotation name)))
1811 (insert (concat name ":\n"))
1812 (if (and ann (not (string-equal ann "")))
1813 ;; insert the annotation, indented by 4 spaces.
1814 (progn
1815 (save-excursion (insert ann) (unless (bolp)
1816 (insert "\n")))
1817 (while (< (point) (point-max))
1818 (beginning-of-line) ; paranoia
1819 (insert " ")
1820 (forward-line)
1821 (end-of-line))))))
1822 bookmark-alist)
1823 (goto-char (point-min))
1824 (pop-to-buffer old-buf)))
1827 (defun bookmark-bmenu-mark ()
1828 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1829 (interactive)
1830 (beginning-of-line)
1831 (if (bookmark-bmenu-check-position)
1832 (let ((inhibit-read-only t))
1833 (delete-char 1)
1834 (insert ?>)
1835 (forward-line 1)
1836 (bookmark-bmenu-check-position))))
1839 (defun bookmark-bmenu-select ()
1840 "Select this line's bookmark; also display bookmarks marked with `>'.
1841 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1842 (interactive)
1843 (if (bookmark-bmenu-check-position)
1844 (let ((bmrk (bookmark-bmenu-bookmark))
1845 (menu (current-buffer))
1846 (others ())
1847 tem)
1848 (goto-char (point-min))
1849 (while (re-search-forward "^>" nil t)
1850 (setq tem (bookmark-bmenu-bookmark))
1851 (let ((inhibit-read-only t))
1852 (delete-char -1)
1853 (insert ?\s))
1854 (or (string-equal tem bmrk)
1855 (member tem others)
1856 (setq others (cons tem others))))
1857 (setq others (nreverse others)
1858 tem (/ (1- (frame-height)) (1+ (length others))))
1859 (delete-other-windows)
1860 (bookmark-jump bmrk)
1861 (bury-buffer menu)
1862 (if others
1863 (while others
1864 (split-window nil tem)
1865 (other-window 1)
1866 (bookmark-jump (car others))
1867 (setq others (cdr others)))
1868 (other-window 1)))))
1871 (defun bookmark-bmenu-save (parg)
1872 "Save the current list into a bookmark file.
1873 With a prefix arg, prompts for a file to save them in."
1874 (interactive "P")
1875 (save-excursion
1876 (save-window-excursion
1877 (bookmark-save parg))))
1880 (defun bookmark-bmenu-load ()
1881 "Load the bookmark file and rebuild the bookmark menu-buffer."
1882 (interactive)
1883 (if (bookmark-bmenu-check-position)
1884 (save-excursion
1885 (save-window-excursion
1886 ;; This will call `bookmark-bmenu-list'
1887 (call-interactively 'bookmark-load)))))
1890 (defun bookmark-bmenu-1-window ()
1891 "Select this line's bookmark, alone, in full frame."
1892 (interactive)
1893 (if (bookmark-bmenu-check-position)
1894 (progn
1895 (bookmark-jump (bookmark-bmenu-bookmark))
1896 (bury-buffer (other-buffer))
1897 (delete-other-windows))))
1900 (defun bookmark-bmenu-2-window ()
1901 "Select this line's bookmark, with previous buffer in second window."
1902 (interactive)
1903 (if (bookmark-bmenu-check-position)
1904 (let ((bmrk (bookmark-bmenu-bookmark))
1905 (menu (current-buffer))
1906 (pop-up-windows t))
1907 (delete-other-windows)
1908 (switch-to-buffer (other-buffer))
1909 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1910 (bookmark--jump-via bmrk 'pop-to-buffer))
1911 (bury-buffer menu))))
1914 (defun bookmark-bmenu-this-window ()
1915 "Select this line's bookmark in this window."
1916 (interactive)
1917 (if (bookmark-bmenu-check-position)
1918 (bookmark-jump (bookmark-bmenu-bookmark))))
1921 (defun bookmark-bmenu-other-window ()
1922 "Select this line's bookmark in other window, leaving bookmark menu visible."
1923 (interactive)
1924 (let ((bookmark (bookmark-bmenu-bookmark)))
1925 (if (bookmark-bmenu-check-position)
1926 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1927 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1930 (defun bookmark-bmenu-switch-other-window ()
1931 "Make the other window select this line's bookmark.
1932 The current window remains selected."
1933 (interactive)
1934 (let ((bookmark (bookmark-bmenu-bookmark))
1935 (pop-up-windows t)
1936 same-window-buffer-names
1937 same-window-regexps)
1938 (if (bookmark-bmenu-check-position)
1939 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1940 (bookmark--jump-via bookmark 'display-buffer)))))
1942 (defun bookmark-bmenu-other-window-with-mouse (event)
1943 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1944 (interactive "e")
1945 (with-current-buffer (window-buffer (posn-window (event-end event)))
1946 (save-excursion
1947 (goto-char (posn-point (event-end event)))
1948 (bookmark-bmenu-other-window))))
1951 (defun bookmark-bmenu-show-annotation ()
1952 "Show the annotation for the current bookmark in another window."
1953 (interactive)
1954 (let ((bookmark (bookmark-bmenu-bookmark)))
1955 (if (bookmark-bmenu-check-position)
1956 (bookmark-show-annotation bookmark))))
1959 (defun bookmark-bmenu-show-all-annotations ()
1960 "Show the annotation for all bookmarks in another window."
1961 (interactive)
1962 (bookmark-show-all-annotations))
1965 (defun bookmark-bmenu-edit-annotation ()
1966 "Edit the annotation for the current bookmark in another window."
1967 (interactive)
1968 (let ((bookmark (bookmark-bmenu-bookmark)))
1969 (if (bookmark-bmenu-check-position)
1970 (bookmark-edit-annotation bookmark))))
1973 (defun bookmark-bmenu-unmark (&optional backup)
1974 "Cancel all requested operations on bookmark on this line and move down.
1975 Optional BACKUP means move up."
1976 (interactive "P")
1977 (beginning-of-line)
1978 (if (bookmark-bmenu-check-position)
1979 (progn
1980 (let ((inhibit-read-only t))
1981 (delete-char 1)
1982 ;; any flags to reset according to circumstances? How about a
1983 ;; flag indicating whether this bookmark is being visited?
1984 ;; well, we don't have this now, so maybe later.
1985 (insert " "))
1986 (forward-line (if backup -1 1))
1987 (bookmark-bmenu-check-position))))
1990 (defun bookmark-bmenu-backup-unmark ()
1991 "Move up and cancel all requested operations on bookmark on line above."
1992 (interactive)
1993 (forward-line -1)
1994 (if (bookmark-bmenu-check-position)
1995 (progn
1996 (bookmark-bmenu-unmark)
1997 (forward-line -1)
1998 (bookmark-bmenu-check-position))))
2001 (defun bookmark-bmenu-delete ()
2002 "Mark bookmark on this line to be deleted.
2003 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2004 (interactive)
2005 (beginning-of-line)
2006 (if (bookmark-bmenu-check-position)
2007 (let ((inhibit-read-only t))
2008 (delete-char 1)
2009 (insert ?D)
2010 (forward-line 1)
2011 (bookmark-bmenu-check-position))))
2014 (defun bookmark-bmenu-delete-backwards ()
2015 "Mark bookmark on this line to be deleted, then move up one line.
2016 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2017 (interactive)
2018 (bookmark-bmenu-delete)
2019 (forward-line -2)
2020 (if (bookmark-bmenu-check-position)
2021 (forward-line 1))
2022 (bookmark-bmenu-check-position))
2025 (defun bookmark-bmenu-execute-deletions ()
2026 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
2027 (interactive)
2028 (message "Deleting bookmarks...")
2029 (let ((hide-em bookmark-bmenu-toggle-filenames)
2030 (o-point (point))
2031 (o-str (save-excursion
2032 (beginning-of-line)
2033 (if (looking-at "^D")
2035 (buffer-substring
2036 (point)
2037 (progn (end-of-line) (point))))))
2038 (o-col (current-column)))
2039 (if hide-em (bookmark-bmenu-hide-filenames))
2040 (setq bookmark-bmenu-toggle-filenames nil)
2041 (goto-char (point-min))
2042 (forward-line 1)
2043 (while (re-search-forward "^D" (point-max) t)
2044 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2045 (bookmark-bmenu-list)
2046 (setq bookmark-bmenu-toggle-filenames hide-em)
2047 (if bookmark-bmenu-toggle-filenames
2048 (bookmark-bmenu-toggle-filenames t))
2049 (if o-str
2050 (progn
2051 (goto-char (point-min))
2052 (search-forward o-str)
2053 (beginning-of-line)
2054 (forward-char o-col))
2055 (goto-char o-point))
2056 (beginning-of-line)
2057 (message "Deleting bookmarks...done")
2061 (defun bookmark-bmenu-rename ()
2062 "Rename bookmark on current line. Prompts for a new name."
2063 (interactive)
2064 (if (bookmark-bmenu-check-position)
2065 (let ((bmrk (bookmark-bmenu-bookmark))
2066 (thispoint (point)))
2067 (bookmark-rename bmrk)
2068 (goto-char thispoint))))
2071 (defun bookmark-bmenu-locate ()
2072 "Display location of this bookmark. Displays in the minibuffer."
2073 (interactive)
2074 (if (bookmark-bmenu-check-position)
2075 (let ((bmrk (bookmark-bmenu-bookmark)))
2076 (message "%s" (bookmark-location bmrk)))))
2078 (defun bookmark-bmenu-relocate ()
2079 "Change the file path of the bookmark on the current line,
2080 prompting with completion for the new path."
2081 (interactive)
2082 (if (bookmark-bmenu-check-position)
2083 (let ((bmrk (bookmark-bmenu-bookmark))
2084 (thispoint (point)))
2085 (bookmark-relocate bmrk)
2086 (goto-char thispoint))))
2089 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2091 (defun bookmark-menu-popup-paned-menu (event name entries)
2092 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2093 That is, ENTRIES is a list of strings which appear as the choices
2094 in the menu.
2095 The number of panes depends on the number of entries.
2096 The visible entries are truncated to `bookmark-menu-length', but the
2097 strings returned are not."
2098 (let ((f-height (/ (frame-height) 2))
2099 (pane-list nil)
2100 (iter 0))
2101 (while entries
2102 (let (lst
2103 (count 0))
2104 (while (and (< count f-height) entries)
2105 (let ((str (car entries)))
2106 (push (cons
2107 (if (> (length str) bookmark-menu-length)
2108 (substring str 0 bookmark-menu-length)
2109 str)
2110 str)
2111 lst)
2112 (setq entries (cdr entries))
2113 (setq count (1+ count))))
2114 (setq iter (1+ iter))
2115 (push (cons
2116 (format "-*- %s (%d) -*-" name iter)
2117 (nreverse lst))
2118 pane-list)))
2120 ;; Popup the menu and return the string.
2121 (x-popup-menu event (cons (concat "-*- " name " -*-")
2122 (nreverse pane-list)))))
2125 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2126 ;; following works, and for explaining what to do to make it work.
2128 ;; We MUST autoload EACH form used to set up this variable's value, so
2129 ;; that the whole job is done in loaddefs.el.
2131 ;; Emacs menubar stuff.
2133 ;;;###autoload
2134 (defvar menu-bar-bookmark-map
2135 (let ((map (make-sparse-keymap "Bookmark functions")))
2136 (define-key map [load]
2137 `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
2138 :help ,(purecopy "Load bookmarks from a bookmark file)")))
2139 (define-key map [write]
2140 `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
2141 :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
2142 (define-key map [save]
2143 `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
2144 :help ,(purecopy "Save currently defined bookmarks")))
2145 (define-key map [edit]
2146 `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
2147 :help ,(purecopy "Display a list of existing bookmarks")))
2148 (define-key map [delete]
2149 `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
2150 :help ,(purecopy "Delete a bookmark from the bookmark list")))
2151 (define-key map [rename]
2152 `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
2153 :help ,(purecopy "Change the name of a bookmark")))
2154 (define-key map [locate]
2155 `(menu-item ,(purecopy "Insert Location...") bookmark-locate
2156 :help ,(purecopy "Insert the name of the file associated with a bookmark")))
2157 (define-key map [insert]
2158 `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
2159 :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
2160 (define-key map [set]
2161 `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
2162 :help ,(purecopy "Set a bookmark named inside a file.")))
2163 (define-key map [jump]
2164 `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
2165 :help ,(purecopy "Jump to a bookmark (a point in some file)")))
2166 map))
2168 ;;;###autoload
2169 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2171 ;; make bookmarks appear toward the right side of the menu.
2172 (if (boundp 'menu-bar-final-items)
2173 (if menu-bar-final-items
2174 (setq menu-bar-final-items
2175 (cons 'bookmark menu-bar-final-items)))
2176 (setq menu-bar-final-items '(bookmark)))
2178 ;;;; end bookmark menu stuff ;;;;
2181 ;; Load Hook
2182 (defvar bookmark-load-hook nil
2183 "Hook run at the end of loading bookmark.")
2185 ;; Exit Hook, called from kill-emacs-hook
2186 (defvar bookmark-exit-hook nil
2187 "Hook run when Emacs exits.")
2189 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2191 (defun bookmark-exit-hook-internal ()
2192 "Save bookmark state, if necessary, at Emacs exit time.
2193 This also runs `bookmark-exit-hook'."
2194 (run-hooks 'bookmark-exit-hook)
2195 (and bookmark-alist
2196 (bookmark-time-to-save-p t)
2197 (bookmark-save)))
2199 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2201 (defun bookmark-unload-function ()
2202 "Unload the Bookmark library."
2203 (when bookmark-save-flag (bookmark-save))
2204 ;; continue standard unloading
2205 nil)
2208 (run-hooks 'bookmark-load-hook)
2210 (provide 'bookmark)
2212 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2213 ;;; bookmark.el ends here