Trivial bytecomp.el change.
[emacs.git] / lisp / bookmark.el
blob0eec76f4da7d0729c3fb79f41615bf7702144103
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, 2010 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.
34 ;;; Code:
36 (require 'pp)
37 (eval-when-compile (require 'cl))
39 ;;; Misc comments:
41 ;; If variable bookmark-use-annotations is non-nil, an annotation is
42 ;; queried for when setting a bookmark.
44 ;; The bookmark list is sorted lexically by default, but you can turn
45 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
46 ;; the list will be presented in the order it is recorded
47 ;; (chronologically), which is actually fairly useful as well.
49 ;;; User Variables
51 (defgroup bookmark nil
52 "Setting, annotation and jumping to bookmarks."
53 :group 'matching)
56 (defcustom bookmark-use-annotations nil
57 "If non-nil, saving a bookmark queries for an annotation in a buffer."
58 :type 'boolean
59 :group 'bookmark)
62 (defcustom bookmark-save-flag t
63 "Controls when Emacs saves bookmarks to a file.
64 --> nil means never save bookmarks, except when `bookmark-save' is
65 explicitly called (\\[bookmark-save]).
66 --> t means save bookmarks when Emacs is killed.
67 --> Otherwise, it should be a number that is the frequency with which
68 the bookmark list is saved (i.e.: the number of times which
69 Emacs' bookmark list may be modified before it is automatically
70 saved.). If it is a number, Emacs will also automatically save
71 bookmarks when it is killed.
73 Therefore, the way to get it to save every time you make or delete a
74 bookmark is to set this variable to 1 (or 0, which produces the same
75 behavior.)
77 To specify the file in which to save them, modify the variable
78 `bookmark-default-file', which is `~/.emacs.bmk' by default."
79 :type '(choice (const nil) integer (other t))
80 :group 'bookmark)
83 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
84 "The `.emacs.bmk' file used to be called this name.")
87 ;; defvarred to avoid a compilation warning:
88 (defvar bookmark-file nil
89 "Old name for `bookmark-default-file'.")
91 (defcustom bookmark-default-file
92 (if bookmark-file
93 ;; In case user set `bookmark-file' in her .emacs:
94 bookmark-file
95 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
96 "File in which to save bookmarks by default."
97 :type 'file
98 :group 'bookmark)
101 (defcustom bookmark-version-control 'nospecial
102 "Whether or not to make numbered backups of the bookmark file.
103 It can have four values: t, nil, `never', and `nospecial'.
104 The first three have the same meaning that they do for the
105 variable `version-control', and the final value `nospecial' means just
106 use the value of `version-control'."
107 :type '(choice (const nil) (const never) (const nospecial)
108 (other t))
109 :group 'bookmark)
112 (defcustom bookmark-completion-ignore-case t
113 "Non-nil means bookmark functions ignore case in completion."
114 :type 'boolean
115 :group 'bookmark)
118 (defcustom bookmark-sort-flag t
119 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
120 Otherwise they will be displayed in LIFO order (that is, most
121 recently set ones come first, oldest ones come last)."
122 :type 'boolean
123 :group 'bookmark)
126 (defcustom bookmark-automatically-show-annotations t
127 "Non-nil means show annotations when jumping to a bookmark."
128 :type 'boolean
129 :group 'bookmark)
132 (defconst bookmark-bmenu-header-height 2
133 "Number of lines used for the *Bookmark List* header.")
135 (defconst bookmark-bmenu-marks-width 2
136 "Number of columns (chars) used for the *Bookmark List* marks column,
137 including the annotations column.")
139 (defcustom bookmark-bmenu-file-column 30
140 "Column at which to display filenames in a buffer listing bookmarks.
141 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
142 :type 'integer
143 :group 'bookmark)
146 (defcustom bookmark-bmenu-toggle-filenames t
147 "Non-nil means show filenames when listing bookmarks.
148 This may result in truncated bookmark names. To disable this, put the
149 following in your `.emacs' file:
151 \(setq bookmark-bmenu-toggle-filenames nil)"
152 :type 'boolean
153 :group 'bookmark)
156 (defcustom bookmark-menu-length 70
157 "Maximum length of a bookmark name displayed on a popup menu."
158 :type 'integer
159 :group 'bookmark)
161 ;; FIXME: Is it really worth a customization option?
162 (defcustom bookmark-search-delay 0.2
163 "Time before `bookmark-bmenu-search' updates the display."
164 :group 'bookmark
165 :type 'integer)
167 (defface bookmark-menu-heading
168 '((t (:inherit font-lock-type-face)))
169 "Face used to highlight the heading in bookmark menu buffers."
170 :group 'bookmark
171 :version "22.1")
174 ;;; No user-serviceable parts beyond this point.
176 ;; Added for lucid emacs compatibility, db
177 (or (fboundp 'defalias) (fset 'defalias 'fset))
179 ;; suggested for lucid compatibility by david hughes:
180 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
183 ;;; Keymap stuff:
185 ;; Set up these bindings dumping time *only*;
186 ;; if the user alters them, don't override the user when loading bookmark.el.
188 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
189 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
190 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
192 ;;;###autoload
193 (defvar bookmark-map
194 (let ((map (make-sparse-keymap)))
195 ;; Read the help on all of these functions for details...
196 (define-key map "x" 'bookmark-set)
197 (define-key map "m" 'bookmark-set) ;"m"ark
198 (define-key map "j" 'bookmark-jump)
199 (define-key map "g" 'bookmark-jump) ;"g"o
200 (define-key map "o" 'bookmark-jump-other-window)
201 (define-key map "i" 'bookmark-insert)
202 (define-key map "e" 'edit-bookmarks)
203 (define-key map "f" 'bookmark-insert-location) ;"f"ind
204 (define-key map "r" 'bookmark-rename)
205 (define-key map "d" 'bookmark-delete)
206 (define-key map "l" 'bookmark-load)
207 (define-key map "w" 'bookmark-write)
208 (define-key map "s" 'bookmark-save)
209 map)
210 "Keymap containing bindings to bookmark functions.
211 It is not bound to any key by default: to bind it
212 so that you have a bookmark prefix, just use `global-set-key' and bind a
213 key of your choice to `bookmark-map'. All interactive bookmark
214 functions have a binding in this keymap.")
216 ;;;###autoload (fset 'bookmark-map bookmark-map)
219 ;;; Core variables and data structures:
220 (defvar bookmark-alist ()
221 "Association list of bookmarks and their records.
222 Bookmark functions update the value automatically.
223 You probably do NOT want to change the value yourself.
225 The value is an alist with entries of the form
227 (BOOKMARK-NAME . PARAM-ALIST)
229 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
231 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
233 PARAM-ALIST is an alist of bookmark information. The order of the
234 entries in PARAM-ALIST is not important. The possible entries are
235 described below. An entry with a key but null value means the entry
236 is not used.
238 (filename . FILENAME)
239 (position . POS)
240 (front-context-string . STR-AFTER-POS)
241 (rear-context-string . STR-BEFORE-POS)
242 (handler . HANDLER)
243 (annotation . ANNOTATION)
245 FILENAME names the bookmarked file.
246 POS is the bookmarked buffer position (position in the file).
247 STR-AFTER-POS is buffer text that immediately follows POS.
248 STR-BEFORE-POS is buffer text that immediately precedes POS.
249 ANNOTATION is a string that describes the bookmark.
250 See options `bookmark-use-annotations' and
251 `bookmark-automatically-show-annotations'.
252 HANDLER is a function that provides the bookmark-jump behavior for a
253 specific kind of bookmark. This is the case for Info bookmarks,
254 for instance. HANDLER must accept a bookmark as argument.")
256 (defvar bookmarks-already-loaded nil
257 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
260 ;; more stuff added by db.
262 (defvar bookmark-current-bookmark nil
263 "Name of bookmark most recently used in the current file.
264 It is buffer local, used to make moving a bookmark forward
265 through a file easier.")
267 (make-variable-buffer-local 'bookmark-current-bookmark)
270 (defvar bookmark-alist-modification-count 0
271 "Number of modifications to bookmark list since it was last saved.")
274 (defvar bookmark-search-size 16
275 "Length of the context strings recorded on either side of a bookmark.")
278 (defvar bookmark-current-buffer nil
279 "The buffer in which a bookmark is currently being set or renamed.
280 Functions that insert strings into the minibuffer use this to know
281 the source buffer for that information; see `bookmark-yank-word' and
282 `bookmark-insert-current-bookmark' for example.")
285 (defvar bookmark-yank-point 0
286 "The next point from which to pull source text for `bookmark-yank-word'.
287 This point is in `bookmark-current-buffer'.")
290 (defvar bookmark-quit-flag nil
291 "Non nil make `bookmark-bmenu-search' quit immediately.")
293 ;; Helper functions and macros.
295 (defmacro with-buffer-modified-unmodified (&rest body)
296 "Run BODY while preserving the buffer's `buffer-modified-p' state."
297 (let ((was-modified (make-symbol "was-modified")))
298 `(let ((,was-modified (buffer-modified-p)))
299 (unwind-protect
300 (progn ,@body)
301 (set-buffer-modified-p ,was-modified)))))
303 ;; Only functions below, in this page and the next one (file formats),
304 ;; need to know anything about the format of bookmark-alist entries.
305 ;; Everyone else should go through them.
307 (defun bookmark-name-from-full-record (full-record)
308 "Return name of FULL-RECORD (an alist element instead of a string)."
309 (car full-record))
312 (defun bookmark-all-names ()
313 "Return a list of all current bookmark names."
314 (bookmark-maybe-load-default-file)
315 (mapcar 'bookmark-name-from-full-record bookmark-alist))
318 (defun bookmark-get-bookmark (bookmark &optional noerror)
319 "Return the bookmark record corresponding to BOOKMARK.
320 If BOOKMARK is a string, look for the corresponding bookmark record in
321 `bookmark-alist'; return it if found, otherwise error. Else if
322 BOOKMARK is already a bookmark record, just return it."
323 (cond
324 ((consp bookmark) bookmark)
325 ((stringp bookmark)
326 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
327 (unless noerror (error "Invalid bookmark %s" bookmark))))))
330 (defun bookmark-get-bookmark-record (bookmark)
331 "Return the record portion of the entry for BOOKMARK in
332 `bookmark-alist' (that is, all information but the name).
333 BOOKMARK may be a bookmark name (a string) or a bookmark record."
334 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
335 ;; The bookmark objects can either look like (NAME ALIST) or
336 ;; (NAME . ALIST), so we have to distinguish the two here.
337 (if (and (null (cdr alist)) (consp (caar alist)))
338 (car alist) alist)))
341 (defun bookmark-set-name (bookmark newname)
342 "Set BOOKMARK's name to NEWNAME.
343 BOOKMARK may be a bookmark name (a string) or a bookmark record."
344 (setcar
345 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
346 newname))
348 (defun bookmark-prop-get (bookmark prop)
349 "Return the property PROP of BOOKMARK, or nil if none.
350 BOOKMARK may be a bookmark name (a string) or a bookmark record."
351 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
353 (defun bookmark-prop-set (bookmark prop val)
354 "Set the property PROP of BOOKMARK to VAL.
355 BOOKMARK may be a bookmark name (a string) or a bookmark record."
356 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
357 (if cell
358 (setcdr cell val)
359 (nconc (bookmark-get-bookmark-record bookmark)
360 (list (cons prop val))))))
362 (defun bookmark-get-annotation (bookmark)
363 "Return the annotation of BOOKMARK, or nil if none.
364 BOOKMARK may be a bookmark name (a string) or a bookmark record."
365 (bookmark-prop-get bookmark 'annotation))
367 (defun bookmark-set-annotation (bookmark ann)
368 "Set the annotation of BOOKMARK to ANN.
369 BOOKMARK may be a bookmark name (a string) or a bookmark record."
370 (bookmark-prop-set bookmark 'annotation ann))
373 (defun bookmark-get-filename (bookmark)
374 "Return the full filename of BOOKMARK, or nil if none.
375 BOOKMARK may be a bookmark name (a string) or a bookmark record."
376 (bookmark-prop-get bookmark 'filename))
379 (defun bookmark-set-filename (bookmark filename)
380 "Set the full filename of BOOKMARK to FILENAME.
381 BOOKMARK may be a bookmark name (a string) or a bookmark record."
382 (bookmark-prop-set bookmark 'filename filename))
385 (defun bookmark-get-position (bookmark)
386 "Return the position (i.e.: point) of BOOKMARK, or nil if none.
387 BOOKMARK may be a bookmark name (a string) or a bookmark record."
388 (bookmark-prop-get bookmark 'position))
391 (defun bookmark-set-position (bookmark position)
392 "Set the position (i.e.: point) of BOOKMARK to POSITION.
393 BOOKMARK may be a bookmark name (a string) or a bookmark record."
394 (bookmark-prop-set bookmark 'position position))
397 (defun bookmark-get-front-context-string (bookmark)
398 "Return the front-context-string of BOOKMARK, or nil if none.
399 BOOKMARK may be a bookmark name (a string) or a bookmark record."
400 (bookmark-prop-get bookmark 'front-context-string))
403 (defun bookmark-set-front-context-string (bookmark string)
404 "Set the front-context-string of BOOKMARK to STRING.
405 BOOKMARK may be a bookmark name (a string) or a bookmark record."
406 (bookmark-prop-set bookmark 'front-context-string string))
409 (defun bookmark-get-rear-context-string (bookmark)
410 "Return the rear-context-string of BOOKMARK, or nil if none.
411 BOOKMARK may be a bookmark name (a string) or a bookmark record."
412 (bookmark-prop-get bookmark 'rear-context-string))
415 (defun bookmark-set-rear-context-string (bookmark string)
416 "Set the rear-context-string of BOOKMARK to STRING.
417 BOOKMARK may be a bookmark name (a string) or a bookmark record."
418 (bookmark-prop-set bookmark 'rear-context-string string))
421 (defun bookmark-get-handler (bookmark)
422 "Return the handler function for BOOKMARK, or nil if none.
423 BOOKMARK may be a bookmark name (a string) or a bookmark record."
424 (bookmark-prop-get bookmark 'handler))
426 (defvar bookmark-history nil
427 "The history list for bookmark functions.")
430 (defun bookmark-completing-read (prompt &optional default)
431 "Prompting with PROMPT, read a bookmark name in completion.
432 PROMPT will get a \": \" stuck on the end no matter what, so you
433 probably don't want to include one yourself.
434 Optional second arg DEFAULT is a string to return if the user enters
435 the empty string."
436 (bookmark-maybe-load-default-file) ; paranoia
437 (if (listp last-nonmenu-event)
438 (bookmark-menu-popup-paned-menu t prompt
439 (if bookmark-sort-flag
440 (sort (bookmark-all-names)
441 'string-lessp)
442 (bookmark-all-names)))
443 (let* ((completion-ignore-case bookmark-completion-ignore-case)
444 (default default)
445 (prompt (concat prompt (if default
446 (format " (%s): " default)
447 ": ")))
448 (str
449 (completing-read prompt
450 bookmark-alist
454 'bookmark-history)))
455 (if (string-equal "" str) default str))))
458 (defmacro bookmark-maybe-historicize-string (string)
459 "Put STRING into the bookmark prompt history, if caller non-interactive.
460 We need this because sometimes bookmark functions are invoked from
461 menus, so `completing-read' never gets a chance to set `bookmark-history'."
462 `(or
463 (called-interactively-p 'interactive)
464 (setq bookmark-history (cons ,string bookmark-history))))
466 (defvar bookmark-make-record-function 'bookmark-make-record-default
467 "A function that should be called to create a bookmark record.
468 Modes may set this variable buffer-locally to enable bookmarking of
469 locations that should be treated specially, such as Info nodes,
470 news posts, images, pdf documents, etc.
472 The function will be called with no arguments.
473 It should signal a user error if it is unable to construct a record for
474 the current location.
476 The returned record should be a cons cell of the form (NAME . ALIST)
477 where ALIST is as described in `bookmark-alist' and may typically contain
478 a special cons (handler . HANDLER-FUNC) which specifies the handler function
479 that should be used instead of `bookmark-default-handler' to open this
480 bookmark. See the documentation for `bookmark-alist' for more.
482 NAME is a suggested name for the constructed bookmark. It can be nil
483 in which case a default heuristic will be used. The function can also
484 equivalently just return ALIST without NAME.")
486 (defun bookmark-make-record ()
487 "Return a new bookmark record (NAME . ALIST) for the current location."
488 (let ((record (funcall bookmark-make-record-function)))
489 ;; Set up default name.
490 (if (stringp (car record))
491 ;; The function already provided a default name.
492 record
493 (if (car record) (push nil record))
494 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
495 record)))
497 (defun bookmark-store (name alist no-overwrite)
498 "Store the bookmark NAME with data ALIST.
499 If NO-OVERWRITE is non-nil and another bookmark of the same name already
500 exists in `bookmark-alist', record the new bookmark without throwing away the
501 old one."
502 (bookmark-maybe-load-default-file)
503 (let ((stripped-name (copy-sequence name)))
504 (or (featurep 'xemacs)
505 ;; XEmacs's `set-text-properties' doesn't work on
506 ;; free-standing strings, apparently.
507 (set-text-properties 0 (length stripped-name) nil stripped-name))
508 (if (and (not no-overwrite)
509 (bookmark-get-bookmark stripped-name 'noerror))
510 ;; already existing bookmark under that name and
511 ;; no prefix arg means just overwrite old bookmark
512 ;; Use the new (NAME . ALIST) format.
513 (setcdr (bookmark-get-bookmark stripped-name) alist)
515 ;; otherwise just cons it onto the front (either the bookmark
516 ;; doesn't exist already, or there is no prefix arg. In either
517 ;; case, we want the new bookmark consed onto the alist...)
519 (push (cons stripped-name alist) bookmark-alist))
521 ;; Added by db
522 (setq bookmark-current-bookmark stripped-name)
523 (setq bookmark-alist-modification-count
524 (1+ bookmark-alist-modification-count))
525 (if (bookmark-time-to-save-p)
526 (bookmark-save))
528 (setq bookmark-current-bookmark stripped-name)
529 (bookmark-bmenu-surreptitiously-rebuild-list)))
531 (defun bookmark-make-record-default (&optional no-file no-context posn)
532 "Return the record describing the location of a new bookmark.
533 Point should be at the buffer in which the bookmark is being set,
534 and normally should be at the position where the bookmark is desired,
535 but see the optional arguments for other possibilities.
537 If NO-FILE is non-nil, then only return the subset of the
538 record that pertains to the location within the buffer, leaving off
539 the part that records the filename.
541 If NO-CONTEXT is non-nil, do not include the front- and rear-context
542 strings in the record -- the position is enough.
544 If POSN is non-nil, record POSN as the point instead of `(point)'."
545 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
546 ,@(unless no-context `((front-context-string
547 . ,(if (>= (- (point-max) (point))
548 bookmark-search-size)
549 (buffer-substring-no-properties
550 (point)
551 (+ (point) bookmark-search-size))
552 nil))))
553 ,@(unless no-context `((rear-context-string
554 . ,(if (>= (- (point) (point-min))
555 bookmark-search-size)
556 (buffer-substring-no-properties
557 (point)
558 (- (point) bookmark-search-size))
559 nil))))
560 (position . ,(or posn (point)))))
563 ;;; File format stuff
565 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
566 ;; the bookmark file format -- please don't. The current format
567 ;; should be extensible enough. If you feel the need to change it,
568 ;; please discuss it with other Emacs developers first.
570 ;; The format of `bookmark-alist' has changed twice in its lifetime.
571 ;; This comment describes the three formats, FIRST, SECOND, and
572 ;; CURRENT.
574 ;; The FIRST format was used prior to Emacs 20:
576 ;; ((BOOKMARK-NAME (FILENAME
577 ;; STRING-IN-FRONT
578 ;; STRING-BEHIND
579 ;; POINT))
580 ;; ...)
582 ;; The SECOND format was introduced in Emacs 20:
584 ;; ((BOOKMARK-NAME ((filename . FILENAME)
585 ;; (position . POS)
586 ;; (front-context-string . STR-AFTER-POS)
587 ;; (rear-context-string . STR-BEFORE-POS)
588 ;; (annotation . ANNOTATION)
589 ;; (whatever . VALUE)
590 ;; ...
591 ;; ))
592 ;; ...)
594 ;; The CURRENT format was introduced in Emacs 22:
596 ;; ((BOOKMARK-NAME (filename . FILENAME)
597 ;; (position . POS)
598 ;; (front-context-string . STR-AFTER-POS)
599 ;; (rear-context-string . STR-BEFORE-POS)
600 ;; (annotation . ANNOTATION)
601 ;; (whatever . VALUE)
602 ;; ...
603 ;; )
604 ;; ...)
606 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
607 ;; bookmark record is a list of entry information. FIRST and SECOND
608 ;; differ in the form of the record information: FIRST uses a list of
609 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
610 ;; the list elements matters. In the SECOND format, the order of the
611 ;; alist elements is unimportant. The SECOND format facilitates the
612 ;; addition of new kinds of elements, to support new kinds of
613 ;; bookmarks or code evolution.
615 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
616 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
617 ;; no longer a cons. Why that change was made remains a mystery --
618 ;; just be aware of it. (Be aware too that this explanatory comment
619 ;; was incorrect in Emacs 22 and Emacs 23.1.)
621 ;; To deal with the change from FIRST format to SECOND, conversion
622 ;; code was added, and it is still in use. See
623 ;; `bookmark-maybe-upgrade-file-format'.
625 ;; No conversion from SECOND to CURRENT is done. Instead, the code
626 ;; handles both formats OK. It must continue to do so.
628 ;; See the doc string of `bookmark-alist' for information about the
629 ;; elements that define a bookmark (e.g. `filename').
632 (defconst bookmark-file-format-version 1
633 "The current version of the format used by bookmark files.
634 You should never need to change this.")
637 (defconst bookmark-end-of-version-stamp-marker
638 "-*- End Of Bookmark File Format Version Stamp -*-\n"
639 "This string marks the end of the version stamp in a bookmark file.")
642 (defun bookmark-alist-from-buffer ()
643 "Return a `bookmark-alist' (in any format) from the current buffer.
644 The buffer must of course contain bookmark format information.
645 Does not care from where in the buffer it is called, and does not
646 affect point."
647 (save-excursion
648 (goto-char (point-min))
649 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
650 (read (current-buffer))
651 ;; Else we're dealing with format version 0
652 (if (search-forward "(" nil t)
653 (progn
654 (forward-char -1)
655 (read (current-buffer)))
656 ;; Else no hope of getting information here.
657 (error "Not bookmark format")))))
660 (defun bookmark-upgrade-version-0-alist (old-list)
661 "Upgrade a version 0 alist OLD-LIST to the current version."
662 (mapcar
663 (lambda (bookmark)
664 (let* ((name (car bookmark))
665 (record (car (cdr bookmark)))
666 (filename (nth 0 record))
667 (front-str (nth 1 record))
668 (rear-str (nth 2 record))
669 (position (nth 3 record))
670 (ann (nth 4 record)))
671 (list
672 name
673 `((filename . ,filename)
674 (front-context-string . ,(or front-str ""))
675 (rear-context-string . ,(or rear-str ""))
676 (position . ,position)
677 (annotation . ,ann)))))
678 old-list))
681 (defun bookmark-upgrade-file-format-from-0 ()
682 "Upgrade a bookmark file of format 0 (the original format) to format 1.
683 This expects to be called from `point-min' in a bookmark file."
684 (message "Upgrading bookmark format from 0 to %d..."
685 bookmark-file-format-version)
686 (let* ((old-list (bookmark-alist-from-buffer))
687 (new-list (bookmark-upgrade-version-0-alist old-list)))
688 (delete-region (point-min) (point-max))
689 (bookmark-insert-file-format-version-stamp)
690 (pp new-list (current-buffer))
691 (save-buffer))
692 (goto-char (point-min))
693 (message "Upgrading bookmark format from 0 to %d...done"
694 bookmark-file-format-version)
698 (defun bookmark-grok-file-format-version ()
699 "Return an integer which is the file-format version of this bookmark file.
700 This expects to be called from `point-min' in a bookmark file."
701 (if (looking-at "^;;;;")
702 (save-excursion
703 (save-match-data
704 (re-search-forward "[0-9]")
705 (forward-char -1)
706 (read (current-buffer))))
707 ;; Else this is format version 0, the original one, which didn't
708 ;; even have version stamps.
712 (defun bookmark-maybe-upgrade-file-format ()
713 "Check the file-format version of this bookmark file.
714 If the version is not up-to-date, upgrade it automatically.
715 This expects to be called from `point-min' in a bookmark file."
716 (let ((version (bookmark-grok-file-format-version)))
717 (cond
718 ((= version bookmark-file-format-version)
719 ) ; home free -- version is current
720 ((= version 0)
721 (bookmark-upgrade-file-format-from-0))
723 (error "Bookmark file format version strangeness")))))
726 (defun bookmark-insert-file-format-version-stamp ()
727 "Insert text indicating current version of bookmark file format."
728 (insert
729 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
730 bookmark-file-format-version))
731 (insert ";;; This format is meant to be slightly human-readable;\n"
732 ";;; nevertheless, you probably don't want to edit it.\n"
733 ";;; "
734 bookmark-end-of-version-stamp-marker))
737 ;;; end file-format stuff
740 ;;; Generic helpers.
742 (defun bookmark-maybe-message (fmt &rest args)
743 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
744 (if (>= baud-rate 9600)
745 (apply 'message fmt args)))
748 ;;; Core code:
750 (defvar bookmark-minibuffer-read-name-map
751 (let ((map (make-sparse-keymap)))
752 (set-keymap-parent map minibuffer-local-map)
753 (define-key map "\C-w" 'bookmark-yank-word)
754 ;; This C-u binding might not be very useful any more now that we
755 ;; provide access to the default via the standard M-n binding.
756 ;; Maybe we should just remove it? --Stef-08
757 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
758 map))
760 ;;;###autoload
761 (defun bookmark-set (&optional name no-overwrite)
762 "Set a bookmark named NAME at the current location.
763 If name is nil, then prompt the user.
765 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
766 existing bookmark that has the same name as NAME, but instead push the
767 new bookmark onto the bookmark alist. The most recently set bookmark
768 with name NAME is thus the one in effect at any given time, but the
769 others are still there, should the user decide to delete the most
770 recent one.
772 To yank words from the text of the buffer and use them as part of the
773 bookmark name, type C-w while setting a bookmark. Successive C-w's
774 yank successive words.
776 Typing C-u inserts (at the bookmark name prompt) the name of the last
777 bookmark used in the document where the new bookmark is being set;
778 this helps you use a single bookmark name to track progress through a
779 large document. If there is no prior bookmark for this document, then
780 C-u inserts an appropriate name based on the buffer or file.
782 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
783 it removes only the first instance of a bookmark with that name from
784 the list of bookmarks.)"
785 (interactive (list nil current-prefix-arg))
786 (unwind-protect
787 (let* ((record (bookmark-make-record))
788 (default (car record)))
790 (bookmark-maybe-load-default-file)
791 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
792 ;; if they have been already set in another buffer. (e.g gnus-art).
793 (unless (and bookmark-yank-point
794 bookmark-current-buffer)
795 (setq bookmark-yank-point (point))
796 (setq bookmark-current-buffer (current-buffer)))
798 (let ((str
799 (or name
800 (read-from-minibuffer
801 (format "Set bookmark (%s): " default)
803 bookmark-minibuffer-read-name-map
804 nil nil default))))
805 (and (string-equal str "") (setq str default))
806 (bookmark-store str (cdr record) no-overwrite)
808 ;; Ask for an annotation buffer for this bookmark
809 (when bookmark-use-annotations
810 (bookmark-edit-annotation str))))
811 (setq bookmark-yank-point nil)
812 (setq bookmark-current-buffer nil)))
815 (defun bookmark-kill-line (&optional newline-too)
816 "Kill from point to end of line.
817 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
818 Does not affect the kill ring."
819 (let ((eol (save-excursion (end-of-line) (point))))
820 (delete-region (point) eol)
821 (if (and newline-too (looking-at "\n"))
822 (delete-char 1))))
825 ;; Defvars to avoid compilation warnings:
826 (defvar bookmark-annotation-name nil
827 "Variable holding the name of the bookmark.
828 This is used in `bookmark-edit-annotation' to record the bookmark
829 whose annotation is being edited.")
832 (defun bookmark-default-annotation-text (bookmark)
833 "Return default annotation text for BOOKMARK (a string, not a record).
834 The default annotation text is simply some text explaining how to use
835 annotations."
836 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
837 "# All lines which start with a '#' will be deleted.\n"
838 "# Type C-c C-c when done.\n#\n"
839 "# Author: " (user-full-name) " <" (user-login-name) "@"
840 (system-name) ">\n"
841 "# Date: " (current-time-string) "\n"))
844 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
845 "Function to return default text to use for a bookmark annotation.
846 It takes one argument, the name of the bookmark, as a string.")
847 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
848 'bookmark-edit-annotation-text-func "23.1")
850 (defvar bookmark-edit-annotation-mode-map
851 (let ((map (make-sparse-keymap)))
852 (set-keymap-parent map text-mode-map)
853 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
854 map)
855 "Keymap for editing an annotation of a bookmark.")
858 (defun bookmark-edit-annotation-mode (bookmark)
859 "Mode for editing the annotation of bookmark BOOKMARK.
860 When you have finished composing, type \\[bookmark-send-annotation].
862 BOOKMARK is a bookmark name (a string) or a bookmark record.
864 \\{bookmark-edit-annotation-mode-map}"
865 (interactive)
866 (kill-all-local-variables)
867 (make-local-variable 'bookmark-annotation-name)
868 (setq bookmark-annotation-name bookmark)
869 (use-local-map bookmark-edit-annotation-mode-map)
870 (setq major-mode 'bookmark-edit-annotation-mode
871 mode-name "Edit Bookmark Annotation")
872 (insert (funcall bookmark-edit-annotation-text-func bookmark))
873 (let ((annotation (bookmark-get-annotation bookmark)))
874 (if (and annotation (not (string-equal annotation "")))
875 (insert annotation)))
876 (run-mode-hooks 'text-mode-hook))
879 (defun bookmark-send-edited-annotation ()
880 "Use buffer contents as annotation for a bookmark.
881 Lines beginning with `#' are ignored."
882 (interactive)
883 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
884 (error "Not in bookmark-edit-annotation-mode"))
885 (goto-char (point-min))
886 (while (< (point) (point-max))
887 (if (looking-at "^#")
888 (bookmark-kill-line t)
889 (forward-line 1)))
890 ;; Take no chances with text properties.
891 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
892 (bookmark bookmark-annotation-name))
893 (bookmark-set-annotation bookmark annotation)
894 (setq bookmark-alist-modification-count
895 (1+ bookmark-alist-modification-count))
896 (bookmark-bmenu-surreptitiously-rebuild-list))
897 (kill-buffer (current-buffer)))
900 (defun bookmark-edit-annotation (bookmark)
901 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
902 BOOKMARK is a bookmark name (a string) or a bookmark record."
903 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
904 (bookmark-edit-annotation-mode bookmark))
907 (defun bookmark-insert-current-bookmark ()
908 "Insert into the bookmark name currently being set the value of
909 `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
910 to the buffer's file name if `bookmark-current-bookmark' is nil."
911 (interactive)
912 (let ((str
913 (with-current-buffer bookmark-current-buffer
914 (or bookmark-current-bookmark
915 (bookmark-buffer-name)))))
916 (insert str)))
919 (defun bookmark-buffer-name ()
920 "Return the name of the current buffer in a form usable as a bookmark name.
921 If the buffer is associated with a file or directory, use that name."
922 (cond
923 ;; Or are we a file?
924 (buffer-file-name (file-name-nondirectory buffer-file-name))
925 ;; Or are we a directory?
926 ((and (boundp 'dired-directory) dired-directory)
927 (let* ((dirname (if (stringp dired-directory)
928 dired-directory
929 (car dired-directory)))
930 (idx (1- (length dirname))))
931 ;; Strip the trailing slash.
932 (if (= ?/ (aref dirname idx))
933 (file-name-nondirectory (substring dirname 0 idx))
934 ;; Else return the current-buffer
935 (buffer-name (current-buffer)))))
936 ;; If all else fails, use the buffer's name.
938 (buffer-name (current-buffer)))))
941 (defun bookmark-yank-word ()
942 "Get the next word from buffer `bookmark-current-buffer' and append
943 it to the name of the bookmark currently being set, advancing
944 `bookmark-yank-point' by one word."
945 (interactive)
946 (let ((string (with-current-buffer bookmark-current-buffer
947 (goto-char bookmark-yank-point)
948 (buffer-substring-no-properties
949 (point)
950 (progn
951 (forward-word 1)
952 (setq bookmark-yank-point (point)))))))
953 (insert string)))
955 (defun bookmark-buffer-file-name ()
956 "Return the current buffer's file in a way useful for bookmarks."
957 ;; Abbreviate the path, both so it's shorter and so it's more
958 ;; portable. E.g., the user's home dir might be a different
959 ;; path on different machines, but "~/" will still reach it.
960 (abbreviate-file-name
961 (cond
962 (buffer-file-name buffer-file-name)
963 ((and (boundp 'dired-directory) dired-directory)
964 (if (stringp dired-directory)
965 dired-directory
966 (car dired-directory)))
967 (t (error "Buffer not visiting a file or directory")))))
970 (defun bookmark-maybe-load-default-file ()
971 "If bookmarks have not been loaded from the default place, load them."
972 (and (not bookmarks-already-loaded)
973 (null bookmark-alist)
974 (prog2
975 (and
976 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
977 ;; to be renamed.
978 (file-exists-p bookmark-old-default-file)
979 (not (file-exists-p bookmark-default-file))
980 (rename-file bookmark-old-default-file
981 bookmark-default-file))
982 ;; return t so the `and' will continue...
985 (file-readable-p bookmark-default-file)
986 (bookmark-load bookmark-default-file t t)
987 (setq bookmarks-already-loaded t)))
990 (defun bookmark-maybe-sort-alist ()
991 "Return `bookmark-alist' for display.
992 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
993 (if bookmark-sort-flag
994 (sort (copy-alist bookmark-alist)
995 (function
996 (lambda (x y) (string-lessp (car x) (car y)))))
997 bookmark-alist))
1000 (defvar bookmark-after-jump-hook nil
1001 "Hook run after `bookmark-jump' jumps to a bookmark.
1002 Useful for example to unhide text in `outline-mode'.")
1004 (defun bookmark--jump-via (bookmark display-function)
1005 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
1006 Bookmark may be a bookmark name (a string) or a bookmark record.
1008 After calling DISPLAY-FUNCTION, set window point to the point specified
1009 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
1010 any annotations for this bookmark."
1011 (bookmark-handle-bookmark bookmark)
1012 (save-current-buffer
1013 (funcall display-function (current-buffer)))
1014 (let ((win (get-buffer-window (current-buffer) 0)))
1015 (if win (set-window-point win (point))))
1016 ;; FIXME: we used to only run bookmark-after-jump-hook in
1017 ;; `bookmark-jump' itself, but in none of the other commands.
1018 (run-hooks 'bookmark-after-jump-hook)
1019 (if bookmark-automatically-show-annotations
1020 ;; if there is an annotation for this bookmark,
1021 ;; show it in a buffer.
1022 (bookmark-show-annotation bookmark)))
1025 ;;;###autoload
1026 (defun bookmark-jump (bookmark &optional display-func)
1027 "Jump to bookmark BOOKMARK (a point in some file).
1028 You may have a problem using this function if the value of variable
1029 `bookmark-alist' is nil. If that happens, you need to load in some
1030 bookmarks. See help on function `bookmark-load' for more about
1031 this.
1033 If the file pointed to by BOOKMARK no longer exists, you will be asked
1034 if you wish to give the bookmark a new location, and `bookmark-jump'
1035 will then jump to the new location, as well as recording it in place
1036 of the old one in the permanent bookmark record.
1038 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1039 the latter is usually only used by programmatic callers.
1041 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1042 bookmark. It defaults to `switch-to-buffer'. A typical value for
1043 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1044 (interactive
1045 (list (bookmark-completing-read "Jump to bookmark"
1046 bookmark-current-bookmark)))
1047 (unless bookmark
1048 (error "No bookmark specified"))
1049 (bookmark-maybe-historicize-string bookmark)
1050 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1053 ;;;###autoload
1054 (defun bookmark-jump-other-window (bookmark)
1055 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1056 (interactive
1057 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1058 bookmark-current-bookmark)))
1059 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1062 (defun bookmark-jump-noselect (bookmark)
1063 "Return the location pointed to by the bookmark BOOKMARK.
1064 The return value has the form (BUFFER . POINT).
1066 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1068 Note: this function is deprecated and is present for Emacs 22
1069 compatibility only."
1070 (save-excursion
1071 (bookmark-handle-bookmark bookmark)
1072 (cons (current-buffer) (point))))
1074 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1076 (defun bookmark-handle-bookmark (bookmark)
1077 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1078 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1080 Changes current buffer and point and returns nil, or signals a `file-error'.
1082 If BOOKMARK has no file, this is a no-op. If BOOKMARK has a file, but
1083 that file no longer exists, then offer interactively to relocate BOOKMARK."
1084 (condition-case err
1085 (funcall (or (bookmark-get-handler bookmark)
1086 'bookmark-default-handler)
1087 (bookmark-get-bookmark bookmark))
1088 (bookmark-error-no-filename ;file-error
1089 ;; We were unable to find the marked file, so ask if user wants to
1090 ;; relocate the bookmark, else remind them to consider deletion.
1091 (when (stringp bookmark)
1092 ;; `bookmark' can be either a bookmark name (from `bookmark-alist')
1093 ;; or a bookmark object. If it's an object, we assume it's a
1094 ;; bookmark used internally by some other package.
1095 (let ((file (bookmark-get-filename bookmark)))
1096 (when file ;Don't know how to relocate if there's no `file'.
1097 ;; If file is not a dir, directory-file-name just returns file.
1098 (let ((display-name (directory-file-name file)))
1099 (ding)
1100 ;; Dialog boxes can accept a file target, but usually don't
1101 ;; know how to accept a directory target (at least, this
1102 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1103 ;; true on Windows as well). So we suppress file dialogs
1104 ;; when relocating.
1105 (let ((use-dialog-box nil)
1106 (use-file-dialog nil))
1107 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1108 bookmark "\"? "))
1109 (progn
1110 (bookmark-relocate bookmark)
1111 ;; Try again.
1112 (funcall (or (bookmark-get-handler bookmark)
1113 'bookmark-default-handler)
1114 (bookmark-get-bookmark bookmark)))
1115 (message
1116 "Bookmark not relocated; consider removing it (%s)."
1117 bookmark)
1118 (signal (car err) (cdr err))))))))))
1119 ;; Added by db.
1120 (when (stringp bookmark)
1121 (setq bookmark-current-bookmark bookmark))
1122 nil)
1124 (put 'bookmark-error-no-filename
1125 'error-conditions
1126 '(error bookmark-errors bookmark-error-no-filename))
1127 (put 'bookmark-error-no-filename
1128 'error-message
1129 "Bookmark has no associated file (or directory)")
1131 (defun bookmark-default-handler (bmk-record)
1132 "Default handler to jump to a particular bookmark location.
1133 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1134 Changes current buffer and point and returns nil, or signals a `file-error'."
1135 (let ((file (bookmark-get-filename bmk-record))
1136 (buf (bookmark-prop-get bmk-record 'buffer))
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 (set-buffer
1141 (cond
1142 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1143 (find-file-noselect file))
1144 ;; No file found. See if buffer BUF have been created.
1145 ((and buf (get-buffer buf)))
1146 (t ;; If not, raise error.
1147 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1148 (if place (goto-char place))
1149 ;; Go searching forward first. Then, if forward-str exists and
1150 ;; was found in the file, we can search backward for behind-str.
1151 ;; Rationale is that if text was inserted between the two in the
1152 ;; file, it's better to be put before it so you can read it,
1153 ;; rather than after and remain perhaps unaware of the changes.
1154 (when (and forward-str (search-forward forward-str (point-max) t))
1155 (goto-char (match-beginning 0)))
1156 (when (and behind-str (search-backward behind-str (point-min) t))
1157 (goto-char (match-end 0)))
1158 nil))
1160 ;;;###autoload
1161 (defun bookmark-relocate (bookmark)
1162 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1163 BOOKMARK is a bookmark name (a string), not a bookmark record.
1165 This makes an already existing bookmark point to that file, instead of
1166 the one it used to point at. Useful when a file has been renamed
1167 after a bookmark was set in it."
1168 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1169 (bookmark-maybe-historicize-string bookmark)
1170 (bookmark-maybe-load-default-file)
1171 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1172 (newloc (abbreviate-file-name
1173 (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))
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 a description of the location of BOOKMARK.
1212 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1213 (bookmark-maybe-load-default-file)
1214 ;; We could call the `handler' and ask for it to construct a description
1215 ;; dynamically: it would open up several new possibilities, but it
1216 ;; would have the major disadvantage of forcing to load each and
1217 ;; every handler when the user calls bookmark-menu.
1218 (or (bookmark-prop-get bookmark 'location)
1219 (bookmark-get-filename bookmark)
1220 "-- Unknown location --"))
1223 ;;;###autoload
1224 (defun bookmark-rename (old &optional new)
1225 "Change the name of OLD bookmark to NEW name.
1226 If called from keyboard, prompt for OLD and NEW. If called from
1227 menubar, select OLD from a menu and prompt for NEW.
1229 Both OLD and NEW are bookmark names (strings), never bookmark records.
1231 If called from Lisp, prompt for NEW if only OLD was passed as an
1232 argument. If called with two strings, then no prompting is done. You
1233 must pass at least OLD when calling from Lisp.
1235 While you are entering the new name, consecutive C-w's insert
1236 consecutive words from the text of the buffer into the new bookmark
1237 name."
1238 (interactive (list (bookmark-completing-read "Old bookmark name")))
1239 (bookmark-maybe-historicize-string old)
1240 (bookmark-maybe-load-default-file)
1242 (setq bookmark-yank-point (point))
1243 (setq bookmark-current-buffer (current-buffer))
1244 (let ((newname
1245 (or new ; use second arg, if non-nil
1246 (read-from-minibuffer
1247 "New name: "
1249 (let ((now-map (copy-keymap minibuffer-local-map)))
1250 (define-key now-map "\C-w" 'bookmark-yank-word)
1251 now-map)
1253 'bookmark-history))))
1254 (bookmark-set-name old newname)
1255 (setq bookmark-current-bookmark newname)
1256 (bookmark-bmenu-surreptitiously-rebuild-list)
1257 (setq bookmark-alist-modification-count
1258 (1+ bookmark-alist-modification-count))
1259 (if (bookmark-time-to-save-p)
1260 (bookmark-save))))
1263 ;;;###autoload
1264 (defun bookmark-insert (bookmark)
1265 "Insert the text of the file pointed to by bookmark BOOKMARK.
1266 BOOKMARK is a bookmark name (a string), not a bookmark record.
1268 You may have a problem using this function if the value of variable
1269 `bookmark-alist' is nil. If that happens, you need to load in some
1270 bookmarks. See help on function `bookmark-load' for more about
1271 this."
1272 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1273 (bookmark-maybe-historicize-string bookmark)
1274 (bookmark-maybe-load-default-file)
1275 (let ((orig-point (point))
1276 (str-to-insert
1277 (save-current-buffer
1278 (bookmark-handle-bookmark bookmark)
1279 (buffer-string))))
1280 (insert str-to-insert)
1281 (push-mark)
1282 (goto-char orig-point)))
1285 ;;;###autoload
1286 (defun bookmark-delete (bookmark &optional batch)
1287 "Delete BOOKMARK from the bookmark list.
1288 BOOKMARK is a bookmark name (a string), not a bookmark record.
1290 Removes only the first instance of a bookmark with that name. If
1291 there are one or more other bookmarks with the same name, they will
1292 not be deleted. Defaults to the \"current\" bookmark (that is, the
1293 one most recently used in this file, if any).
1294 Optional second arg BATCH means don't update the bookmark list buffer,
1295 probably because we were called from there."
1296 (interactive
1297 (list (bookmark-completing-read "Delete bookmark"
1298 bookmark-current-bookmark)))
1299 (bookmark-maybe-historicize-string bookmark)
1300 (bookmark-maybe-load-default-file)
1301 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1302 (setq bookmark-alist (delq will-go bookmark-alist))
1303 ;; Added by db, nil bookmark-current-bookmark if the last
1304 ;; occurrence has been deleted
1305 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1306 (setq bookmark-current-bookmark nil)))
1307 (unless batch
1308 (bookmark-bmenu-surreptitiously-rebuild-list))
1309 (setq bookmark-alist-modification-count
1310 (1+ bookmark-alist-modification-count))
1311 (when (bookmark-time-to-save-p)
1312 (bookmark-save)))
1315 (defun bookmark-time-to-save-p (&optional final-time)
1316 "Return t if it is time to save bookmarks to disk, nil otherwise.
1317 Optional argument FINAL-TIME means this is being called when Emacs
1318 is being killed, so save even if `bookmark-save-flag' is a number and
1319 is greater than `bookmark-alist-modification-count'."
1320 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1321 (cond (final-time
1322 (and (> bookmark-alist-modification-count 0)
1323 bookmark-save-flag))
1324 ((numberp bookmark-save-flag)
1325 (>= bookmark-alist-modification-count bookmark-save-flag))
1327 nil)))
1330 ;;;###autoload
1331 (defun bookmark-write ()
1332 "Write bookmarks to a file (reading the file name with the minibuffer).
1333 Don't use this in Lisp programs; use `bookmark-save' instead."
1334 (interactive)
1335 (bookmark-maybe-load-default-file)
1336 (bookmark-save t))
1339 ;;;###autoload
1340 (defun bookmark-save (&optional parg file)
1341 "Save currently defined bookmarks.
1342 Saves by default in the file defined by the variable
1343 `bookmark-default-file'. With a prefix arg, save it in file FILE
1344 \(second argument).
1346 If you are calling this from Lisp, the two arguments are PARG and
1347 FILE, and if you just want it to write to the default file, then
1348 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1349 instead. If you pass in one argument, and it is non-nil, then the
1350 user will be interactively queried for a file to save in.
1352 When you want to load in the bookmarks from a file, use
1353 `bookmark-load', \\[bookmark-load]. That function will prompt you
1354 for a file, defaulting to the file defined by variable
1355 `bookmark-default-file'."
1356 (interactive "P")
1357 (bookmark-maybe-load-default-file)
1358 (cond
1359 ((and (null parg) (null file))
1360 ;;whether interactive or not, write to default file
1361 (bookmark-write-file bookmark-default-file))
1362 ((and (null parg) file)
1363 ;;whether interactive or not, write to given file
1364 (bookmark-write-file file))
1365 ((and parg (not file))
1366 ;;have been called interactively w/ prefix arg
1367 (let ((file (read-file-name "File to save bookmarks in: ")))
1368 (bookmark-write-file file)))
1369 (t ; someone called us with prefix-arg *and* a file, so just write to file
1370 (bookmark-write-file file)))
1371 ;; signal that we have synced the bookmark file by setting this to
1372 ;; 0. If there was an error at any point before, it will not get
1373 ;; set, which is what we want.
1374 (setq bookmark-alist-modification-count 0))
1378 (defun bookmark-write-file (file)
1379 "Write `bookmark-alist' to FILE."
1380 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1381 (with-current-buffer (get-buffer-create " *Bookmarks*")
1382 (goto-char (point-min))
1383 (delete-region (point-min) (point-max))
1384 (let ((print-length nil)
1385 (print-level nil))
1386 (bookmark-insert-file-format-version-stamp)
1387 (insert "(")
1388 ;; Rather than a single call to `pp' we make one per bookmark.
1389 ;; Apparently `pp' has a poor algorithmic complexity, so this
1390 ;; scales a lot better. bug#4485.
1391 (dolist (i bookmark-alist) (pp i (current-buffer)))
1392 (insert ")")
1393 (let ((version-control
1394 (cond
1395 ((null bookmark-version-control) nil)
1396 ((eq 'never bookmark-version-control) 'never)
1397 ((eq 'nospecial bookmark-version-control) version-control)
1398 (t t))))
1399 (condition-case nil
1400 (write-region (point-min) (point-max) file)
1401 (file-error (message "Can't write %s" file)))
1402 (kill-buffer (current-buffer))
1403 (bookmark-maybe-message
1404 "Saving bookmarks to file %s...done" file)))))
1407 (defun bookmark-import-new-list (new-list)
1408 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1409 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1410 they conflict with existing bookmark names."
1411 (let ((names (bookmark-all-names)))
1412 (dolist (full-record new-list)
1413 (bookmark-maybe-rename full-record names)
1414 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1415 (push (bookmark-name-from-full-record full-record) names))))
1418 (defun bookmark-maybe-rename (full-record names)
1419 "Rename bookmark FULL-RECORD if its current name is already used.
1420 This is a helper for `bookmark-import-new-list'."
1421 (let ((found-name (bookmark-name-from-full-record full-record)))
1422 (if (member found-name names)
1423 ;; We've got a conflict, so generate a new name
1424 (let ((count 2)
1425 (new-name found-name))
1426 (while (member new-name names)
1427 (setq new-name (concat found-name (format "<%d>" count)))
1428 (setq count (1+ count)))
1429 (bookmark-set-name full-record new-name)))))
1432 ;;;###autoload
1433 (defun bookmark-load (file &optional overwrite no-msg)
1434 "Load bookmarks from FILE (which must be in bookmark format).
1435 Appends loaded bookmarks to the front of the list of bookmarks. If
1436 optional second argument OVERWRITE is non-nil, existing bookmarks are
1437 destroyed. Optional third arg NO-MSG means don't display any messages
1438 while loading.
1440 If you load a file that doesn't contain a proper bookmark alist, you
1441 will corrupt Emacs's bookmark list. Generally, you should only load
1442 in files that were created with the bookmark functions in the first
1443 place. Your own personal bookmark file, `~/.emacs.bmk', is
1444 maintained automatically by Emacs; you shouldn't need to load it
1445 explicitly.
1447 If you load a file containing bookmarks with the same names as
1448 bookmarks already present in your Emacs, the new bookmarks will get
1449 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1450 method buffers use to resolve name collisions."
1451 (interactive
1452 (list (read-file-name
1453 (format "Load bookmarks from: (%s) "
1454 bookmark-default-file)
1455 ;;Default might not be used often,
1456 ;;but there's no better default, and
1457 ;;I guess it's better than none at all.
1458 "~/" bookmark-default-file 'confirm)))
1459 (setq file (abbreviate-file-name (expand-file-name file)))
1460 (if (not (file-readable-p file))
1461 (error "Cannot read bookmark file %s" file)
1462 (if (null no-msg)
1463 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1464 (with-current-buffer (let ((enable-local-variables nil))
1465 (find-file-noselect file))
1466 (goto-char (point-min))
1467 (bookmark-maybe-upgrade-file-format)
1468 (let ((blist (bookmark-alist-from-buffer)))
1469 (if (listp blist)
1470 (progn
1471 (if overwrite
1472 (progn
1473 (setq bookmark-alist blist)
1474 (setq bookmark-alist-modification-count 0))
1475 ;; else
1476 (bookmark-import-new-list blist)
1477 (setq bookmark-alist-modification-count
1478 (1+ bookmark-alist-modification-count)))
1479 (if (string-equal
1480 (abbreviate-file-name
1481 (expand-file-name bookmark-default-file))
1482 file)
1483 (setq bookmarks-already-loaded t))
1484 (bookmark-bmenu-surreptitiously-rebuild-list))
1485 (error "Invalid bookmark list in %s" file)))
1486 (kill-buffer (current-buffer)))
1487 (if (null no-msg)
1488 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1492 ;;; Code supporting the dired-like bookmark menu.
1493 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1496 (defvar bookmark-bmenu-hidden-bookmarks ())
1499 (defvar bookmark-bmenu-mode-map
1500 (let ((map (make-keymap)))
1501 (suppress-keymap map t)
1502 (define-key map "q" 'quit-window)
1503 (define-key map "v" 'bookmark-bmenu-select)
1504 (define-key map "w" 'bookmark-bmenu-locate)
1505 (define-key map "2" 'bookmark-bmenu-2-window)
1506 (define-key map "1" 'bookmark-bmenu-1-window)
1507 (define-key map "j" 'bookmark-bmenu-this-window)
1508 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1509 (define-key map "f" 'bookmark-bmenu-this-window)
1510 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1511 (define-key map "o" 'bookmark-bmenu-other-window)
1512 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1513 (define-key map "s" 'bookmark-bmenu-save)
1514 (define-key map "k" 'bookmark-bmenu-delete)
1515 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1516 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1517 (define-key map "d" 'bookmark-bmenu-delete)
1518 (define-key map " " 'next-line)
1519 (define-key map "n" 'next-line)
1520 (define-key map "p" 'previous-line)
1521 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1522 (define-key map "?" 'describe-mode)
1523 (define-key map "u" 'bookmark-bmenu-unmark)
1524 (define-key map "m" 'bookmark-bmenu-mark)
1525 (define-key map "l" 'bookmark-bmenu-load)
1526 (define-key map "r" 'bookmark-bmenu-rename)
1527 (define-key map "R" 'bookmark-bmenu-relocate)
1528 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1529 (define-key map "a" 'bookmark-bmenu-show-annotation)
1530 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1531 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1532 ;; The original binding of M-g hides the M-g prefix map.
1533 ;; If someone has a better idea than M-g s, I'm open to suggestions.
1534 (define-key map [?\M-g ?s] 'bookmark-bmenu-search)
1535 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1536 map))
1538 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1539 ;; data.
1540 (put 'bookmark-bmenu-mode 'mode-class 'special)
1543 ;; todo: need to display whether or not bookmark exists as a buffer in
1544 ;; flag column.
1546 ;; Format:
1547 ;; FLAGS BOOKMARK [ LOCATION ]
1550 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1551 "Rebuild the Bookmark List if it exists.
1552 Don't affect the buffer ring order."
1553 (if (get-buffer "*Bookmark List*")
1554 (save-excursion
1555 (save-window-excursion
1556 (bookmark-bmenu-list)))))
1559 ;;;###autoload
1560 (defun bookmark-bmenu-list ()
1561 "Display a list of existing bookmarks.
1562 The list is displayed in a buffer named `*Bookmark List*'.
1563 The leftmost column displays a D if the bookmark is flagged for
1564 deletion, or > if it is flagged for displaying."
1565 (interactive)
1566 (bookmark-maybe-load-default-file)
1567 (let ((buf (get-buffer-create "*Bookmark List*")))
1568 (if (called-interactively-p 'interactive)
1569 (if (or (window-dedicated-p) (window-minibuffer-p))
1570 (pop-to-buffer buf)
1571 (switch-to-buffer buf))
1572 (set-buffer buf)))
1573 (let ((inhibit-read-only t))
1574 (erase-buffer)
1575 (insert "% Bookmark\n- --------\n")
1576 (add-text-properties (point-min) (point)
1577 '(font-lock-face bookmark-menu-heading))
1578 (dolist (full-record (bookmark-maybe-sort-alist))
1579 (let ((name (bookmark-name-from-full-record full-record))
1580 (annotation (bookmark-get-annotation full-record))
1581 (start (point))
1582 end)
1583 ;; if a bookmark has an annotation, prepend a "*"
1584 ;; in the list of bookmarks.
1585 (insert (if (and annotation (not (string-equal annotation "")))
1586 " *" " ")
1587 name)
1588 (setq end (point))
1589 (put-text-property
1590 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1591 (when (display-mouse-p)
1592 (add-text-properties
1593 (+ bookmark-bmenu-marks-width start) end
1594 '(mouse-face highlight
1595 follow-link t
1596 help-echo "mouse-2: go to this bookmark in other window")))
1597 (insert "\n")))
1598 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1599 (goto-char (point-min))
1600 (forward-line 2)
1601 (bookmark-bmenu-mode)
1602 (if bookmark-bmenu-toggle-filenames
1603 (bookmark-bmenu-toggle-filenames t))))
1605 ;;;###autoload
1606 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1607 ;;;###autoload
1608 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1612 (defun bookmark-bmenu-mode ()
1613 "Major mode for editing a list of bookmarks.
1614 Each line describes one of the bookmarks in Emacs.
1615 Letters do not insert themselves; instead, they are commands.
1616 Bookmark names preceded by a \"*\" have annotations.
1617 \\<bookmark-bmenu-mode-map>
1618 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1619 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1620 Also show bookmarks marked using m in other windows.
1621 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1622 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1623 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1624 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1625 together with bookmark selected before this one in another window.
1626 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1627 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1628 so the bookmark menu bookmark remains visible in its window.
1629 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1630 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1631 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1632 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1633 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1634 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1635 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1636 With a prefix arg, prompts for a file to save in.
1637 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1638 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1639 With prefix argument, also move up one line.
1640 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1641 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1642 in another buffer.
1643 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1644 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1645 (kill-all-local-variables)
1646 (use-local-map bookmark-bmenu-mode-map)
1647 (setq truncate-lines t)
1648 (setq buffer-read-only t)
1649 (setq major-mode 'bookmark-bmenu-mode)
1650 (setq mode-name "Bookmark Menu")
1651 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1654 (defun bookmark-bmenu-toggle-filenames (&optional show)
1655 "Toggle whether filenames are shown in the bookmark list.
1656 Optional argument SHOW means show them unconditionally."
1657 (interactive)
1658 (cond
1659 (show
1660 (setq bookmark-bmenu-toggle-filenames nil)
1661 (bookmark-bmenu-show-filenames)
1662 (setq bookmark-bmenu-toggle-filenames t))
1663 (bookmark-bmenu-toggle-filenames
1664 (bookmark-bmenu-hide-filenames)
1665 (setq bookmark-bmenu-toggle-filenames nil))
1667 (bookmark-bmenu-show-filenames)
1668 (setq bookmark-bmenu-toggle-filenames t))))
1671 (defun bookmark-bmenu-show-filenames (&optional force)
1672 "In an interactive bookmark list, show filenames along with bookmarks.
1673 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1674 mainly for debugging, and should not be necessary in normal use."
1675 (if (and (not force) bookmark-bmenu-toggle-filenames)
1676 nil ;already shown, so do nothing
1677 (with-buffer-modified-unmodified
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 (push bmrk bookmark-bmenu-hidden-bookmarks)
1687 (let ((start (save-excursion (end-of-line) (point))))
1688 (move-to-column bookmark-bmenu-file-column t)
1689 ;; Strip off `mouse-face' from the white spaces region.
1690 (if (display-mouse-p)
1691 (remove-text-properties start (point)
1692 '(mouse-face nil help-echo nil))))
1693 (delete-region (point) (progn (end-of-line) (point)))
1694 (insert " ")
1695 ;; Pass the NO-HISTORY arg:
1696 (bookmark-insert-location bmrk t)
1697 (forward-line 1)))))))))
1700 (defun bookmark-bmenu-hide-filenames (&optional force)
1701 "In an interactive bookmark list, hide the filenames of the bookmarks.
1702 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1703 mainly for debugging, and should not be necessary in normal use."
1704 (when (and (not force) bookmark-bmenu-toggle-filenames)
1705 ;; nothing to hide if above is nil
1706 (with-buffer-modified-unmodified
1707 (save-excursion
1708 (goto-char (point-min))
1709 (forward-line 2)
1710 (setq bookmark-bmenu-hidden-bookmarks
1711 (nreverse bookmark-bmenu-hidden-bookmarks))
1712 (let ((inhibit-read-only t))
1713 (while bookmark-bmenu-hidden-bookmarks
1714 (move-to-column bookmark-bmenu-marks-width t)
1715 (bookmark-kill-line)
1716 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1717 (start (point)))
1718 (insert name)
1719 (put-text-property start (point) 'bookmark-name-prop name)
1720 (if (display-mouse-p)
1721 (add-text-properties
1722 start (point)
1723 '(mouse-face
1724 highlight follow-link t help-echo
1725 "mouse-2: go to this bookmark in other window"))))
1726 (forward-line 1)))))))
1729 (defun bookmark-bmenu-ensure-position ()
1730 "If point is not on a bookmark line, move it to one.
1731 If before the first bookmark line, move to the first; if after the
1732 last full line, move to the last full line. The return value is undefined."
1733 (cond ((< (count-lines (point-min) (point)) bookmark-bmenu-header-height)
1734 (goto-char (point-min))
1735 (forward-line bookmark-bmenu-header-height))
1736 ((and (bolp) (eobp))
1737 (beginning-of-line 0))))
1740 (defun bookmark-bmenu-bookmark ()
1741 "Return the bookmark for this line in an interactive bookmark list buffer."
1742 (bookmark-bmenu-ensure-position)
1743 (save-excursion
1744 (beginning-of-line)
1745 (forward-char bookmark-bmenu-marks-width)
1746 (get-text-property (point) 'bookmark-name-prop)))
1749 (defun bookmark-show-annotation (bookmark)
1750 "Display the annotation for bookmark named BOOKMARK in a buffer,
1751 if an annotation exists."
1752 (let ((annotation (bookmark-get-annotation bookmark)))
1753 (when (and annotation (not (string-equal annotation "")))
1754 (save-excursion
1755 (let ((old-buf (current-buffer)))
1756 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1757 (delete-region (point-min) (point-max))
1758 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1759 (insert annotation)
1760 (goto-char (point-min))
1761 (switch-to-buffer-other-window old-buf))))))
1764 (defun bookmark-show-all-annotations ()
1765 "Display the annotations for all bookmarks in a buffer."
1766 (save-selected-window
1767 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1768 (delete-region (point-min) (point-max))
1769 (dolist (full-record bookmark-alist)
1770 (let* ((name (bookmark-name-from-full-record full-record))
1771 (ann (bookmark-get-annotation full-record)))
1772 (insert (concat name ":\n"))
1773 (if (and ann (not (string-equal ann "")))
1774 ;; insert the annotation, indented by 4 spaces.
1775 (progn
1776 (save-excursion (insert ann) (unless (bolp)
1777 (insert "\n")))
1778 (while (< (point) (point-max))
1779 (beginning-of-line) ; paranoia
1780 (insert " ")
1781 (forward-line)
1782 (end-of-line))))))
1783 (goto-char (point-min))))
1786 (defun bookmark-bmenu-mark ()
1787 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1788 (interactive)
1789 (beginning-of-line)
1790 (bookmark-bmenu-ensure-position)
1791 (with-buffer-modified-unmodified
1792 (let ((inhibit-read-only t))
1793 (delete-char 1)
1794 (insert ?>)
1795 (forward-line 1)
1796 (bookmark-bmenu-ensure-position))))
1799 (defun bookmark-bmenu-select ()
1800 "Select this line's bookmark; also display bookmarks marked with `>'.
1801 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1802 (interactive)
1803 (let ((bmrk (bookmark-bmenu-bookmark))
1804 (menu (current-buffer))
1805 (others ())
1806 tem)
1807 (goto-char (point-min))
1808 (while (re-search-forward "^>" nil t)
1809 (setq tem (bookmark-bmenu-bookmark))
1810 (let ((inhibit-read-only t))
1811 (delete-char -1)
1812 (insert ?\s))
1813 (or (string-equal tem bmrk)
1814 (member tem others)
1815 (setq others (cons tem others))))
1816 (setq others (nreverse others)
1817 tem (/ (1- (frame-height)) (1+ (length others))))
1818 (delete-other-windows)
1819 (bookmark-jump bmrk)
1820 (bury-buffer menu)
1821 (if others
1822 (while others
1823 (split-window nil tem)
1824 (other-window 1)
1825 (bookmark-jump (car others))
1826 (setq others (cdr others)))
1827 (other-window 1))))
1830 (defun bookmark-bmenu-any-marks ()
1831 "Return non-nil if any bookmarks are marked in the marks column."
1832 (save-excursion
1833 (goto-char (point-min))
1834 (bookmark-bmenu-ensure-position)
1835 (catch 'found-mark
1836 (while (not (eobp))
1837 (beginning-of-line)
1838 (if (looking-at "^\\S-")
1839 (throw 'found-mark t)
1840 (forward-line 1)))
1841 nil)))
1844 (defun bookmark-bmenu-save (parg)
1845 "Save the current list into a bookmark file.
1846 With a prefix arg, prompts for a file to save them in."
1847 (interactive "P")
1848 (save-excursion
1849 (save-window-excursion
1850 (bookmark-save parg)
1851 (set-buffer-modified-p nil))))
1854 (defun bookmark-bmenu-load ()
1855 "Load the bookmark file and rebuild the bookmark menu-buffer."
1856 (interactive)
1857 (bookmark-bmenu-ensure-position)
1858 (save-excursion
1859 (save-window-excursion
1860 ;; This will call `bookmark-bmenu-list'
1861 (call-interactively 'bookmark-load))))
1864 (defun bookmark-bmenu-1-window ()
1865 "Select this line's bookmark, alone, in full frame."
1866 (interactive)
1867 (bookmark-jump (bookmark-bmenu-bookmark))
1868 (bury-buffer (other-buffer))
1869 (delete-other-windows))
1872 (defun bookmark-bmenu-2-window ()
1873 "Select this line's bookmark, with previous buffer in second window."
1874 (interactive)
1875 (let ((bmrk (bookmark-bmenu-bookmark))
1876 (menu (current-buffer))
1877 (pop-up-windows t))
1878 (delete-other-windows)
1879 (switch-to-buffer (other-buffer))
1880 (bookmark--jump-via bmrk 'pop-to-buffer)
1881 (bury-buffer menu)))
1884 (defun bookmark-bmenu-this-window ()
1885 "Select this line's bookmark in this window."
1886 (interactive)
1887 (bookmark-jump (bookmark-bmenu-bookmark)))
1890 (defun bookmark-bmenu-other-window ()
1891 "Select this line's bookmark in other window, leaving bookmark menu visible."
1892 (interactive)
1893 (let ((bookmark (bookmark-bmenu-bookmark)))
1894 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1897 (defun bookmark-bmenu-switch-other-window ()
1898 "Make the other window select this line's bookmark.
1899 The current window remains selected."
1900 (interactive)
1901 (let ((bookmark (bookmark-bmenu-bookmark))
1902 (pop-up-windows t)
1903 same-window-buffer-names
1904 same-window-regexps)
1905 (bookmark--jump-via bookmark 'display-buffer)))
1907 (defun bookmark-bmenu-other-window-with-mouse (event)
1908 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1909 (interactive "e")
1910 (with-current-buffer (window-buffer (posn-window (event-end event)))
1911 (save-excursion
1912 (goto-char (posn-point (event-end event)))
1913 (bookmark-bmenu-other-window))))
1916 (defun bookmark-bmenu-show-annotation ()
1917 "Show the annotation for the current bookmark in another window."
1918 (interactive)
1919 (let ((bookmark (bookmark-bmenu-bookmark)))
1920 (bookmark-show-annotation bookmark)))
1923 (defun bookmark-bmenu-show-all-annotations ()
1924 "Show the annotation for all bookmarks in another window."
1925 (interactive)
1926 (bookmark-show-all-annotations))
1929 (defun bookmark-bmenu-edit-annotation ()
1930 "Edit the annotation for the current bookmark in another window."
1931 (interactive)
1932 (let ((bookmark (bookmark-bmenu-bookmark)))
1933 (bookmark-edit-annotation bookmark)))
1936 (defun bookmark-bmenu-unmark (&optional backup)
1937 "Cancel all requested operations on bookmark on this line and move down.
1938 Optional BACKUP means move up."
1939 (interactive "P")
1940 (beginning-of-line)
1941 (bookmark-bmenu-ensure-position)
1942 (with-buffer-modified-unmodified
1943 (let ((inhibit-read-only t))
1944 (delete-char 1)
1945 ;; any flags to reset according to circumstances? How about a
1946 ;; flag indicating whether this bookmark is being visited?
1947 ;; well, we don't have this now, so maybe later.
1948 (insert " "))
1949 (forward-line (if backup -1 1))
1950 (bookmark-bmenu-ensure-position)))
1953 (defun bookmark-bmenu-backup-unmark ()
1954 "Move up and cancel all requested operations on bookmark on line above."
1955 (interactive)
1956 (forward-line -1)
1957 (bookmark-bmenu-ensure-position)
1958 (bookmark-bmenu-unmark)
1959 (forward-line -1)
1960 (bookmark-bmenu-ensure-position))
1963 (defun bookmark-bmenu-delete ()
1964 "Mark bookmark on this line to be deleted.
1965 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1966 (interactive)
1967 (beginning-of-line)
1968 (bookmark-bmenu-ensure-position)
1969 (with-buffer-modified-unmodified
1970 (let ((inhibit-read-only t))
1971 (delete-char 1)
1972 (insert ?D)
1973 (forward-line 1)
1974 (bookmark-bmenu-ensure-position))))
1977 (defun bookmark-bmenu-delete-backwards ()
1978 "Mark bookmark on this line to be deleted, then move up one line.
1979 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1980 (interactive)
1981 (bookmark-bmenu-delete)
1982 (forward-line -2)
1983 (bookmark-bmenu-ensure-position)
1984 (forward-line 1)
1985 (bookmark-bmenu-ensure-position))
1988 (defun bookmark-bmenu-execute-deletions ()
1989 "Delete bookmarks flagged `D'."
1990 (interactive)
1991 (message "Deleting bookmarks...")
1992 (let ((o-point (point))
1993 (o-str (save-excursion
1994 (beginning-of-line)
1995 (unless (looking-at "^D")
1996 (buffer-substring
1997 (point)
1998 (progn (end-of-line) (point))))))
1999 (o-col (current-column)))
2000 (goto-char (point-min))
2001 (forward-line 1)
2002 (while (re-search-forward "^D" (point-max) t)
2003 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2004 (bookmark-bmenu-list)
2005 (if o-str
2006 (progn
2007 (goto-char (point-min))
2008 (search-forward o-str)
2009 (beginning-of-line)
2010 (forward-char o-col))
2011 (goto-char o-point))
2012 (beginning-of-line)
2013 (message "Deleting bookmarks...done")
2017 (defun bookmark-bmenu-rename ()
2018 "Rename bookmark on current line. Prompts for a new name."
2019 (interactive)
2020 (let ((bmrk (bookmark-bmenu-bookmark))
2021 (thispoint (point)))
2022 (bookmark-rename bmrk)
2023 (goto-char thispoint)))
2026 (defun bookmark-bmenu-locate ()
2027 "Display location of this bookmark. Displays in the minibuffer."
2028 (interactive)
2029 (let ((bmrk (bookmark-bmenu-bookmark)))
2030 (message "%s" (bookmark-location bmrk))))
2032 (defun bookmark-bmenu-relocate ()
2033 "Change the file path of the bookmark on the current line,
2034 prompting with completion for the new path."
2035 (interactive)
2036 (let ((bmrk (bookmark-bmenu-bookmark))
2037 (thispoint (point)))
2038 (bookmark-relocate bmrk)
2039 (goto-char thispoint)))
2041 ;;; Bookmark-bmenu search
2043 ;; Store keyboard input for incremental search.
2044 (defvar bookmark-search-pattern)
2046 (defun bookmark-read-search-input ()
2047 "Read each keyboard input and add it to `bookmark-search-pattern'."
2048 (let ((prompt (propertize "Pattern: " 'face 'minibuffer-prompt))
2049 ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
2050 (tmp-list ()))
2051 (while
2052 (let ((char (read-key (concat prompt bookmark-search-pattern))))
2053 (case char
2054 ((?\e ?\r) nil) ; RET or ESC break the search loop.
2055 (?\C-g (setq bookmark-quit-flag t) nil)
2056 (?\d (pop tmp-list) t) ; Delete last char of pattern with DEL
2058 (if (characterp char)
2059 (push char tmp-list)
2060 (setq unread-command-events
2061 (nconc (mapcar 'identity
2062 (this-single-command-raw-keys))
2063 unread-command-events))
2064 nil))))
2065 (setq bookmark-search-pattern
2066 (apply 'string (reverse tmp-list))))))
2069 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2070 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2071 (let ((bookmark-alist
2072 (loop for i in bookmark-alist
2073 when (string-match regexp (car i)) collect i into new
2074 finally return new)))
2075 (bookmark-bmenu-list)))
2078 ;;;###autoload
2079 (defun bookmark-bmenu-search ()
2080 "Incremental search of bookmarks, hiding the non-matches as we go."
2081 (interactive)
2082 (let ((bmk (bookmark-bmenu-bookmark))
2083 (bookmark-search-pattern "")
2084 (timer (run-with-idle-timer
2085 bookmark-search-delay 'repeat
2086 #'(lambda ()
2087 (bookmark-bmenu-filter-alist-by-regexp
2088 bookmark-search-pattern)))))
2089 (unwind-protect
2090 (bookmark-read-search-input)
2091 (cancel-timer timer)
2092 (message nil)
2093 (when bookmark-quit-flag ; C-g hit restore menu list.
2094 (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
2095 (setq bookmark-quit-flag nil))))
2097 (defun bookmark-bmenu-goto-bookmark (name)
2098 "Move point to bookmark with name NAME."
2099 (goto-char (point-min))
2100 (while (not (equal name (bookmark-bmenu-bookmark)))
2101 (forward-line 1))
2102 (forward-line 0))
2106 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2108 (defun bookmark-menu-popup-paned-menu (event name entries)
2109 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2110 That is, ENTRIES is a list of strings which appear as the choices
2111 in the menu.
2112 The number of panes depends on the number of entries.
2113 The visible entries are truncated to `bookmark-menu-length', but the
2114 strings returned are not."
2115 (let ((f-height (/ (frame-height) 2))
2116 (pane-list nil)
2117 (iter 0))
2118 (while entries
2119 (let (lst
2120 (count 0))
2121 (while (and (< count f-height) entries)
2122 (let ((str (car entries)))
2123 (push (cons
2124 (if (> (length str) bookmark-menu-length)
2125 (substring str 0 bookmark-menu-length)
2126 str)
2127 str)
2128 lst)
2129 (setq entries (cdr entries))
2130 (setq count (1+ count))))
2131 (setq iter (1+ iter))
2132 (push (cons
2133 (format "-*- %s (%d) -*-" name iter)
2134 (nreverse lst))
2135 pane-list)))
2137 ;; Popup the menu and return the string.
2138 (x-popup-menu event (cons (concat "-*- " name " -*-")
2139 (nreverse pane-list)))))
2142 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2143 ;; following works, and for explaining what to do to make it work.
2145 ;; We MUST autoload EACH form used to set up this variable's value, so
2146 ;; that the whole job is done in loaddefs.el.
2148 ;; Emacs menubar stuff.
2150 ;;;###autoload
2151 (defvar menu-bar-bookmark-map
2152 (let ((map (make-sparse-keymap "Bookmark functions")))
2153 (define-key map [load]
2154 `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
2155 :help ,(purecopy "Load bookmarks from a bookmark file)")))
2156 (define-key map [write]
2157 `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
2158 :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
2159 (define-key map [save]
2160 `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
2161 :help ,(purecopy "Save currently defined bookmarks")))
2162 (define-key map [edit]
2163 `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
2164 :help ,(purecopy "Display a list of existing bookmarks")))
2165 (define-key map [delete]
2166 `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
2167 :help ,(purecopy "Delete a bookmark from the bookmark list")))
2168 (define-key map [rename]
2169 `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
2170 :help ,(purecopy "Change the name of a bookmark")))
2171 (define-key map [locate]
2172 `(menu-item ,(purecopy "Insert Location...") bookmark-locate
2173 :help ,(purecopy "Insert the name of the file associated with a bookmark")))
2174 (define-key map [insert]
2175 `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
2176 :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
2177 (define-key map [set]
2178 `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
2179 :help ,(purecopy "Set a bookmark named inside a file.")))
2180 (define-key map [jump]
2181 `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
2182 :help ,(purecopy "Jump to a bookmark (a point in some file)")))
2183 map))
2185 ;;;###autoload
2186 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2188 ;; make bookmarks appear toward the right side of the menu.
2189 (if (boundp 'menu-bar-final-items)
2190 (if menu-bar-final-items
2191 (push 'bookmark menu-bar-final-items))
2192 (setq menu-bar-final-items '(bookmark)))
2194 ;;;; end bookmark menu stuff ;;;;
2197 ;; Load Hook
2198 (defvar bookmark-load-hook nil
2199 "Hook run at the end of loading library `bookmark.el'.")
2201 ;; Exit Hook, called from kill-emacs-hook
2202 (defvar bookmark-exit-hook nil
2203 "Hook run when Emacs exits.")
2205 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2207 (defun bookmark-exit-hook-internal ()
2208 "Save bookmark state, if necessary, at Emacs exit time.
2209 This also runs `bookmark-exit-hook'."
2210 (run-hooks 'bookmark-exit-hook)
2211 (and bookmark-alist
2212 (bookmark-time-to-save-p t)
2213 (bookmark-save)))
2215 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2217 (defun bookmark-unload-function ()
2218 "Unload the Bookmark library."
2219 (when bookmark-save-flag (bookmark-save))
2220 ;; continue standard unloading
2221 nil)
2224 (run-hooks 'bookmark-load-hook)
2226 (provide 'bookmark)
2228 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2229 ;;; bookmark.el ends here