* image.c (x_from_xcolors): Remove unused local.
[emacs.git] / lisp / bookmark.el
blobcab81c3b135409025f55b2e35d5f16bb2f5d2fe5
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993-1997, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package is for setting "bookmarks" in files. A bookmark
28 ;; associates a string with a location in a certain file. Thus, you
29 ;; can navigate your way to that location by providing the string.
30 ;; See the "User Variables" section for customizations.
33 ;;; Code:
35 (require 'pp)
36 (eval-when-compile (require 'cl-lib))
38 ;;; Misc comments:
40 ;; If variable bookmark-use-annotations is non-nil, an annotation is
41 ;; queried for when setting a bookmark.
43 ;; The bookmark list is sorted lexically by default, but you can turn
44 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
45 ;; the list will be presented in the order it is recorded
46 ;; (chronologically), which is actually fairly useful as well.
48 ;;; User Variables
50 (defgroup bookmark nil
51 "Setting, annotation and jumping to bookmarks."
52 :group 'matching)
55 (defcustom bookmark-use-annotations nil
56 "If non-nil, saving a bookmark queries for an annotation in a buffer."
57 :type 'boolean
58 :group 'bookmark)
61 (defcustom bookmark-save-flag t
62 "Controls when Emacs saves bookmarks to a file.
63 --> nil means never save bookmarks, except when `bookmark-save' is
64 explicitly called (\\[bookmark-save]).
65 --> t means save bookmarks when Emacs is killed.
66 --> Otherwise, it should be a number that is the frequency with which
67 the bookmark list is saved (i.e.: the number of times which
68 Emacs's bookmark list may be modified before it is automatically
69 saved.). If it is a number, Emacs will also automatically save
70 bookmarks when it is killed.
72 Therefore, the way to get it to save every time you make or delete a
73 bookmark is to set this variable to 1 (or 0, which produces the same
74 behavior.)
76 To specify the file in which to save them, modify the variable
77 `bookmark-default-file', which is `~/.emacs.bmk' by default."
78 :type '(choice (const nil) integer (other t))
79 :group 'bookmark)
82 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
83 "The `.emacs.bmk' file used to be called this name.")
86 ;; defvared to avoid a compilation warning:
87 (defvar bookmark-file nil
88 "Old name for `bookmark-default-file'.")
90 (defcustom bookmark-default-file
91 (if bookmark-file
92 ;; In case user set `bookmark-file' in her .emacs:
93 bookmark-file
94 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
95 "File in which to save bookmarks by default."
96 :type 'file
97 :group 'bookmark)
100 (defcustom bookmark-version-control 'nospecial
101 "Whether or not to make numbered backups of the bookmark file.
102 It can have four values: t, nil, `never', or `nospecial'.
103 The first three have the same meaning that they do for the
104 variable `version-control'; the value `nospecial' (the default) means
105 just use the value of `version-control'."
106 :type '(choice (const :tag "If existing" nil)
107 (const :tag "Never" never)
108 (const :tag "Use value of option `version-control'" nospecial)
109 (other :tag "Always" t))
110 :group 'bookmark)
113 (defcustom bookmark-completion-ignore-case t
114 "Non-nil means bookmark functions ignore case in completion."
115 :type 'boolean
116 :group 'bookmark)
119 (defcustom bookmark-sort-flag t
120 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
121 Otherwise they will be displayed in LIFO order (that is, most
122 recently set ones come first, oldest ones come last)."
123 :type 'boolean
124 :group 'bookmark)
127 (defcustom bookmark-automatically-show-annotations t
128 "Non-nil means show annotations when jumping to a bookmark."
129 :type 'boolean
130 :group 'bookmark)
132 (defcustom bookmark-bmenu-use-header-line t
133 "Non-nil means to use an immovable header line, as opposed to inline
134 text at the top of the buffer."
135 :type 'boolean
136 :group 'bookmark)
138 (defconst bookmark-bmenu-inline-header-height 2
139 "Number of lines used for the *Bookmark List* header
140 \(only significant when `bookmark-bmenu-use-header-line' is nil\).")
142 (defconst bookmark-bmenu-marks-width 2
143 "Number of columns (chars) used for the *Bookmark List* marks column,
144 including the annotations column.")
146 (defcustom bookmark-bmenu-file-column 30
147 "Column at which to display filenames in a buffer listing bookmarks.
148 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
149 :type 'integer
150 :group 'bookmark)
153 (defcustom bookmark-bmenu-toggle-filenames t
154 "Non-nil means show filenames when listing bookmarks.
155 A non-nil value may result in truncated bookmark names."
156 :type 'boolean
157 :group 'bookmark)
159 (defface bookmark-menu-bookmark
160 '((t (:weight bold)))
161 "Face used to highlight bookmark names in bookmark menu buffers."
162 :group 'bookmark)
164 (defcustom bookmark-menu-length 70
165 "Maximum length of a bookmark name displayed on a popup menu."
166 :type 'integer
167 :group 'bookmark)
169 ;; FIXME: Is it really worth a customization option?
170 (defcustom bookmark-search-delay 0.2
171 "Time before `bookmark-bmenu-search' updates the display."
172 :group 'bookmark
173 :type 'number)
175 (defface bookmark-menu-heading
176 '((t (:inherit font-lock-type-face)))
177 "Face used to highlight the heading in bookmark menu buffers."
178 :group 'bookmark
179 :version "22.1")
182 ;;; No user-serviceable parts beyond this point.
184 ;; Added for lucid emacs compatibility, db
185 (or (fboundp 'defalias) (fset 'defalias 'fset))
187 ;; suggested for lucid compatibility by david hughes:
188 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
191 ;;; Keymap stuff:
193 ;; Set up these bindings dumping time *only*;
194 ;; if the user alters them, don't override the user when loading bookmark.el.
196 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
197 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
198 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
200 ;;;###autoload
201 (defvar bookmark-map
202 (let ((map (make-sparse-keymap)))
203 ;; Read the help on all of these functions for details...
204 (define-key map "x" 'bookmark-set)
205 (define-key map "m" 'bookmark-set) ;"m"ark
206 (define-key map "j" 'bookmark-jump)
207 (define-key map "g" 'bookmark-jump) ;"g"o
208 (define-key map "o" 'bookmark-jump-other-window)
209 (define-key map "i" 'bookmark-insert)
210 (define-key map "e" 'edit-bookmarks)
211 (define-key map "f" 'bookmark-insert-location) ;"f"ind
212 (define-key map "r" 'bookmark-rename)
213 (define-key map "d" 'bookmark-delete)
214 (define-key map "l" 'bookmark-load)
215 (define-key map "w" 'bookmark-write)
216 (define-key map "s" 'bookmark-save)
217 map)
218 "Keymap containing bindings to bookmark functions.
219 It is not bound to any key by default: to bind it
220 so that you have a bookmark prefix, just use `global-set-key' and bind a
221 key of your choice to `bookmark-map'. All interactive bookmark
222 functions have a binding in this keymap.")
224 ;;;###autoload (fset 'bookmark-map bookmark-map)
227 ;;; Core variables and data structures:
228 (defvar bookmark-alist ()
229 "Association list of bookmarks and their records.
230 Bookmark functions update the value automatically.
231 You probably do NOT want to change the value yourself.
233 The value is an alist with entries of the form
235 (BOOKMARK-NAME . PARAM-ALIST)
237 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
239 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
241 PARAM-ALIST is an alist of bookmark information. The order of the
242 entries in PARAM-ALIST is not important. The possible entries are
243 described below. An entry with a key but null value means the entry
244 is not used.
246 (filename . FILENAME)
247 (position . POS)
248 (front-context-string . STR-AFTER-POS)
249 (rear-context-string . STR-BEFORE-POS)
250 (handler . HANDLER)
251 (annotation . ANNOTATION)
253 FILENAME names the bookmarked file.
254 POS is the bookmarked buffer position (position in the file).
255 STR-AFTER-POS is buffer text that immediately follows POS.
256 STR-BEFORE-POS is buffer text that immediately precedes POS.
257 ANNOTATION is a string that describes the bookmark.
258 See options `bookmark-use-annotations' and
259 `bookmark-automatically-show-annotations'.
260 HANDLER is a function that provides the bookmark-jump behavior for a
261 specific kind of bookmark. This is the case for Info bookmarks,
262 for instance. HANDLER must accept a bookmark as argument.")
264 (defvar bookmarks-already-loaded nil
265 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
268 ;; more stuff added by db.
270 (defvar bookmark-current-bookmark nil
271 "Name of bookmark most recently used in the current file.
272 It is buffer local, used to make moving a bookmark forward
273 through a file easier.")
275 (make-variable-buffer-local 'bookmark-current-bookmark)
278 (defvar bookmark-alist-modification-count 0
279 "Number of modifications to bookmark list since it was last saved.")
282 (defvar bookmark-search-size 16
283 "Length of the context strings recorded on either side of a bookmark.")
286 (defvar bookmark-current-buffer nil
287 "The buffer in which a bookmark is currently being set or renamed.
288 Functions that insert strings into the minibuffer use this to know
289 the source buffer for that information; see `bookmark-yank-word'
290 for example.")
293 (defvar bookmark-yank-point 0
294 "The next point from which to pull source text for `bookmark-yank-word'.
295 This point is in `bookmark-current-buffer'.")
298 (defvar bookmark-quit-flag nil
299 "Non nil make `bookmark-bmenu-search' quit immediately.")
301 ;; Helper functions and macros.
303 (defmacro with-buffer-modified-unmodified (&rest body)
304 "Run BODY while preserving the buffer's `buffer-modified-p' state."
305 (let ((was-modified (make-symbol "was-modified")))
306 `(let ((,was-modified (buffer-modified-p)))
307 (unwind-protect
308 (progn ,@body)
309 (set-buffer-modified-p ,was-modified)))))
311 ;; Only functions below, in this page and the next one (file formats),
312 ;; need to know anything about the format of bookmark-alist entries.
313 ;; Everyone else should go through them.
315 (defun bookmark-name-from-full-record (bookmark-record)
316 "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
317 one element from `bookmark-alist'."
318 (car bookmark-record))
321 (defun bookmark-all-names ()
322 "Return a list of all current bookmark names."
323 (bookmark-maybe-load-default-file)
324 (mapcar 'bookmark-name-from-full-record bookmark-alist))
327 (defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
328 "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
329 If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
330 bookmark record in `bookmark-alist'; return it if found, otherwise
331 error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
332 just return it."
333 (cond
334 ((consp bookmark-name-or-record) bookmark-name-or-record)
335 ((stringp bookmark-name-or-record)
336 (or (assoc-string bookmark-name-or-record bookmark-alist
337 bookmark-completion-ignore-case)
338 (unless noerror (error "Invalid bookmark %s"
339 bookmark-name-or-record))))))
342 (defun bookmark-get-bookmark-record (bookmark-name-or-record)
343 "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
344 `bookmark-alist' (that is, all information but the name)."
345 (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
346 ;; The bookmark objects can either look like (NAME ALIST) or
347 ;; (NAME . ALIST), so we have to distinguish the two here.
348 (if (and (null (cdr alist)) (consp (caar alist)))
349 (car alist) alist)))
352 (defun bookmark-set-name (bookmark-name-or-record newname)
353 "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
354 (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
356 (defun bookmark-prop-get (bookmark-name-or-record prop)
357 "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
358 (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
360 (defun bookmark-prop-set (bookmark-name-or-record prop val)
361 "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
362 (let ((cell (assq
363 prop (bookmark-get-bookmark-record bookmark-name-or-record))))
364 (if cell
365 (setcdr cell val)
366 (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
367 (list (cons prop val))))))
369 (defun bookmark-get-annotation (bookmark-name-or-record)
370 "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
371 (bookmark-prop-get bookmark-name-or-record 'annotation))
373 (defun bookmark-set-annotation (bookmark-name-or-record ann)
374 "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
375 (bookmark-prop-set bookmark-name-or-record 'annotation ann))
378 (defun bookmark-get-filename (bookmark-name-or-record)
379 "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
380 (bookmark-prop-get bookmark-name-or-record 'filename))
383 (defun bookmark-set-filename (bookmark-name-or-record filename)
384 "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
385 (bookmark-prop-set bookmark-name-or-record 'filename filename))
388 (defun bookmark-get-position (bookmark-name-or-record)
389 "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
390 (bookmark-prop-get bookmark-name-or-record 'position))
393 (defun bookmark-set-position (bookmark-name-or-record position)
394 "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
395 (bookmark-prop-set bookmark-name-or-record 'position position))
398 (defun bookmark-get-front-context-string (bookmark-name-or-record)
399 "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
400 (bookmark-prop-get bookmark-name-or-record 'front-context-string))
403 (defun bookmark-set-front-context-string (bookmark-name-or-record string)
404 "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
405 (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
408 (defun bookmark-get-rear-context-string (bookmark-name-or-record)
409 "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
410 (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
413 (defun bookmark-set-rear-context-string (bookmark-name-or-record string)
414 "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
415 (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
418 (defun bookmark-get-handler (bookmark-name-or-record)
419 "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
420 (bookmark-prop-get bookmark-name-or-record 'handler))
422 (defvar bookmark-history nil
423 "The history list for bookmark functions.")
426 (defun bookmark-completing-read (prompt &optional default)
427 "Prompting with PROMPT, read a bookmark name in completion.
428 PROMPT will get a \": \" stuck on the end no matter what, so you
429 probably don't want to include one yourself.
430 Optional arg DEFAULT is a string to return if the user input is empty.
431 If DEFAULT is nil then return empty string for empty input."
432 (bookmark-maybe-load-default-file) ; paranoia
433 (if (listp last-nonmenu-event)
434 (bookmark-menu-popup-paned-menu t prompt
435 (if bookmark-sort-flag
436 (sort (bookmark-all-names)
437 'string-lessp)
438 (bookmark-all-names)))
439 (let* ((completion-ignore-case bookmark-completion-ignore-case)
440 (default (unless (equal "" default) default))
441 (prompt (concat prompt (if default
442 (format " (%s): " default)
443 ": "))))
444 (completing-read prompt
445 (lambda (string pred action)
446 (if (eq action 'metadata)
447 '(metadata (category . bookmark))
448 (complete-with-action
449 action bookmark-alist string pred)))
450 nil 0 nil 'bookmark-history default))))
453 (defmacro bookmark-maybe-historicize-string (string)
454 "Put STRING into the bookmark prompt history, if caller non-interactive.
455 We need this because sometimes bookmark functions are invoked from
456 menus, so `completing-read' never gets a chance to set `bookmark-history'."
457 `(or
458 (called-interactively-p 'interactive)
459 (setq bookmark-history (cons ,string bookmark-history))))
461 (defvar bookmark-make-record-function 'bookmark-make-record-default
462 "A function that should be called to create a bookmark record.
463 Modes may set this variable buffer-locally to enable bookmarking of
464 locations that should be treated specially, such as Info nodes,
465 news posts, images, pdf documents, etc.
467 The function will be called with no arguments.
468 It should signal a user error if it is unable to construct a record for
469 the current location.
471 The returned record should be a cons cell of the form (NAME . ALIST)
472 where ALIST is as described in `bookmark-alist' and may typically contain
473 a special cons (handler . HANDLER-FUNC) which specifies the handler function
474 that should be used instead of `bookmark-default-handler' to open this
475 bookmark. See the documentation for `bookmark-alist' for more.
477 NAME is a suggested name for the constructed bookmark. It can be nil
478 in which case a default heuristic will be used. The function can also
479 equivalently just return ALIST without NAME.")
481 (defun bookmark-make-record ()
482 "Return a new bookmark record (NAME . ALIST) for the current location."
483 (let ((record (funcall bookmark-make-record-function)))
484 ;; Set up defaults.
485 (bookmark-prop-set
486 record 'defaults
487 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
488 (list bookmark-current-bookmark
489 (bookmark-buffer-name))))))
490 ;; Set up default name.
491 (if (stringp (car record))
492 ;; The function already provided a default name.
493 record
494 (if (car record) (push nil record))
495 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
496 record)))
498 (defun bookmark-store (name alist no-overwrite)
499 "Store the bookmark NAME with data ALIST.
500 If NO-OVERWRITE is non-nil and another bookmark of the same name already
501 exists in `bookmark-alist', record the new bookmark without throwing away the
502 old one."
503 (bookmark-maybe-load-default-file)
504 (let ((stripped-name (copy-sequence name)))
505 (or (featurep 'xemacs)
506 ;; XEmacs's `set-text-properties' doesn't work on
507 ;; free-standing strings, apparently.
508 (set-text-properties 0 (length stripped-name) nil stripped-name))
509 (if (and (not no-overwrite)
510 (bookmark-get-bookmark stripped-name 'noerror))
511 ;; already existing bookmark under that name and
512 ;; no prefix arg means just overwrite old bookmark
513 ;; Use the new (NAME . ALIST) format.
514 (setcdr (bookmark-get-bookmark stripped-name) alist)
516 ;; otherwise just cons it onto the front (either the bookmark
517 ;; doesn't exist already, or there is no prefix arg. In either
518 ;; case, we want the new bookmark consed onto the alist...)
520 (push (cons stripped-name alist) bookmark-alist))
522 ;; Added by db
523 (setq bookmark-current-bookmark stripped-name)
524 (setq bookmark-alist-modification-count
525 (1+ bookmark-alist-modification-count))
526 (if (bookmark-time-to-save-p)
527 (bookmark-save))
529 (setq bookmark-current-bookmark stripped-name)
530 (bookmark-bmenu-surreptitiously-rebuild-list)))
532 (defun bookmark-make-record-default (&optional no-file no-context posn)
533 "Return the record describing the location of a new bookmark.
534 Point should be at the buffer in which the bookmark is being set,
535 and normally should be at the position where the bookmark is desired,
536 but see the optional arguments for other possibilities.
538 If NO-FILE is non-nil, then only return the subset of the
539 record that pertains to the location within the buffer, leaving off
540 the part that records the filename.
542 If NO-CONTEXT is non-nil, do not include the front- and rear-context
543 strings in the record -- the position is enough.
545 If POSN is non-nil, record POSN as the point instead of `(point)'."
546 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
547 ,@(unless no-context `((front-context-string
548 . ,(if (>= (- (point-max) (point))
549 bookmark-search-size)
550 (buffer-substring-no-properties
551 (point)
552 (+ (point) bookmark-search-size))
553 nil))))
554 ,@(unless no-context `((rear-context-string
555 . ,(if (>= (- (point) (point-min))
556 bookmark-search-size)
557 (buffer-substring-no-properties
558 (point)
559 (- (point) bookmark-search-size))
560 nil))))
561 (position . ,(or posn (point)))))
564 ;;; File format stuff
566 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
567 ;; the bookmark file format -- please don't. The current format
568 ;; should be extensible enough. If you feel the need to change it,
569 ;; please discuss it with other Emacs developers first.
571 ;; The format of `bookmark-alist' has changed twice in its lifetime.
572 ;; This comment describes the three formats, FIRST, SECOND, and
573 ;; CURRENT.
575 ;; The FIRST format was used prior to Emacs 20:
577 ;; ((BOOKMARK-NAME (FILENAME
578 ;; STRING-IN-FRONT
579 ;; STRING-BEHIND
580 ;; POINT))
581 ;; ...)
583 ;; The SECOND format was introduced in Emacs 20:
585 ;; ((BOOKMARK-NAME ((filename . FILENAME)
586 ;; (position . POS)
587 ;; (front-context-string . STR-AFTER-POS)
588 ;; (rear-context-string . STR-BEFORE-POS)
589 ;; (annotation . ANNOTATION)
590 ;; (whatever . VALUE)
591 ;; ...
592 ;; ))
593 ;; ...)
595 ;; The CURRENT format was introduced in Emacs 22:
597 ;; ((BOOKMARK-NAME (filename . FILENAME)
598 ;; (position . POS)
599 ;; (front-context-string . STR-AFTER-POS)
600 ;; (rear-context-string . STR-BEFORE-POS)
601 ;; (annotation . ANNOTATION)
602 ;; (whatever . VALUE)
603 ;; ...
604 ;; )
605 ;; ...)
607 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
608 ;; bookmark record is a list of entry information. FIRST and SECOND
609 ;; differ in the form of the record information: FIRST uses a list of
610 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
611 ;; the list elements matters. In the SECOND format, the order of the
612 ;; alist elements is unimportant. The SECOND format facilitates the
613 ;; addition of new kinds of elements, to support new kinds of
614 ;; bookmarks or code evolution.
616 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
617 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
618 ;; no longer a cons. Why that change was made remains a mystery --
619 ;; just be aware of it. (Be aware too that this explanatory comment
620 ;; was incorrect in Emacs 22 and Emacs 23.1.)
622 ;; To deal with the change from FIRST format to SECOND, conversion
623 ;; code was added, and it is still in use. See
624 ;; `bookmark-maybe-upgrade-file-format'.
626 ;; No conversion from SECOND to CURRENT is done. Instead, the code
627 ;; handles both formats OK. It must continue to do so.
629 ;; See the doc string of `bookmark-alist' for information about the
630 ;; elements that define a bookmark (e.g. `filename').
633 (defconst bookmark-file-format-version 1
634 "The current version of the format used by bookmark files.
635 You should never need to change this.")
638 (defconst bookmark-end-of-version-stamp-marker
639 "-*- End Of Bookmark File Format Version Stamp -*-\n"
640 "This string marks the end of the version stamp in a bookmark file.")
643 (defun bookmark-alist-from-buffer ()
644 "Return a `bookmark-alist' (in any format) from the current buffer.
645 The buffer must of course contain bookmark format information.
646 Does not care from where in the buffer it is called, and does not
647 affect point."
648 (save-excursion
649 (goto-char (point-min))
650 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
651 (read (current-buffer))
652 ;; Else we're dealing with format version 0
653 (if (search-forward "(" nil t)
654 (progn
655 (forward-char -1)
656 (read (current-buffer)))
657 ;; Else no hope of getting information here.
658 (error "Not bookmark format")))))
661 (defun bookmark-upgrade-version-0-alist (old-list)
662 "Upgrade a version 0 alist OLD-LIST to the current version."
663 (mapcar
664 (lambda (bookmark)
665 (let* ((name (car bookmark))
666 (record (car (cdr bookmark)))
667 (filename (nth 0 record))
668 (front-str (nth 1 record))
669 (rear-str (nth 2 record))
670 (position (nth 3 record))
671 (ann (nth 4 record)))
672 (list
673 name
674 `((filename . ,filename)
675 (front-context-string . ,(or front-str ""))
676 (rear-context-string . ,(or rear-str ""))
677 (position . ,position)
678 (annotation . ,ann)))))
679 old-list))
682 (defun bookmark-upgrade-file-format-from-0 ()
683 "Upgrade a bookmark file of format 0 (the original format) to format 1.
684 This expects to be called from `point-min' in a bookmark file."
685 (message "Upgrading bookmark format from 0 to %d..."
686 bookmark-file-format-version)
687 (let* ((old-list (bookmark-alist-from-buffer))
688 (new-list (bookmark-upgrade-version-0-alist old-list)))
689 (delete-region (point-min) (point-max))
690 (bookmark-insert-file-format-version-stamp)
691 (pp new-list (current-buffer))
692 (save-buffer))
693 (goto-char (point-min))
694 (message "Upgrading bookmark format from 0 to %d...done"
695 bookmark-file-format-version)
699 (defun bookmark-grok-file-format-version ()
700 "Return an integer which is the file-format version of this bookmark file.
701 This expects to be called from `point-min' in a bookmark file."
702 (if (looking-at "^;;;;")
703 (save-excursion
704 (save-match-data
705 (re-search-forward "[0-9]")
706 (forward-char -1)
707 (read (current-buffer))))
708 ;; Else this is format version 0, the original one, which didn't
709 ;; even have version stamps.
713 (defun bookmark-maybe-upgrade-file-format ()
714 "Check the file-format version of this bookmark file.
715 If the version is not up-to-date, upgrade it automatically.
716 This expects to be called from `point-min' in a bookmark file."
717 (let ((version (bookmark-grok-file-format-version)))
718 (cond
719 ((= version bookmark-file-format-version)
720 ) ; home free -- version is current
721 ((= version 0)
722 (bookmark-upgrade-file-format-from-0))
724 (error "Bookmark file format version strangeness")))))
727 (defun bookmark-insert-file-format-version-stamp ()
728 "Insert text indicating current version of bookmark file format."
729 (insert
730 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
731 bookmark-file-format-version))
732 (insert ";;; This format is meant to be slightly human-readable;\n"
733 ";;; nevertheless, you probably don't want to edit it.\n"
734 ";;; "
735 bookmark-end-of-version-stamp-marker))
738 ;;; end file-format stuff
741 ;;; Generic helpers.
743 (defun bookmark-maybe-message (fmt &rest args)
744 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
745 (if (>= baud-rate 9600)
746 (apply 'message fmt args)))
749 ;;; Core code:
751 (defvar bookmark-minibuffer-read-name-map
752 (let ((map (make-sparse-keymap)))
753 (set-keymap-parent map minibuffer-local-map)
754 (define-key map "\C-w" 'bookmark-yank-word)
755 map))
757 ;;;###autoload
758 (defun bookmark-set (&optional name no-overwrite)
759 "Set a bookmark named NAME at the current location.
760 If name is nil, then prompt the user.
762 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
763 existing bookmark that has the same name as NAME, but instead push the
764 new bookmark onto the bookmark alist. The most recently set bookmark
765 with name NAME is thus the one in effect at any given time, but the
766 others are still there, should the user decide to delete the most
767 recent one.
769 To yank words from the text of the buffer and use them as part of the
770 bookmark name, type C-w while setting a bookmark. Successive C-w's
771 yank successive words.
773 Typing C-u inserts (at the bookmark name prompt) the name of the last
774 bookmark used in the document where the new bookmark is being set;
775 this helps you use a single bookmark name to track progress through a
776 large document. If there is no prior bookmark for this document, then
777 C-u inserts an appropriate name based on the buffer or file.
779 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
780 it removes only the first instance of a bookmark with that name from
781 the list of bookmarks.)"
782 (interactive (list nil current-prefix-arg))
783 (unwind-protect
784 (let* ((record (bookmark-make-record))
785 ;; `defaults' is a transient element of the
786 ;; extensible format described above in the section
787 ;; `File format stuff'. Bookmark record functions
788 ;; can use it to specify a list of default values
789 ;; accessible via M-n while reading a bookmark name.
790 (defaults (bookmark-prop-get record 'defaults))
791 (default (if (consp defaults) (car defaults) defaults)))
793 (if defaults
794 ;; Don't store default values in the record.
795 (setq record (assq-delete-all 'defaults record))
796 ;; When no defaults in the record, use its first element.
797 (setq defaults (car record) default defaults))
799 (bookmark-maybe-load-default-file)
800 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
801 ;; if they have been already set in another buffer. (e.g gnus-art).
802 (unless (and bookmark-yank-point
803 bookmark-current-buffer)
804 (setq bookmark-yank-point (point))
805 (setq bookmark-current-buffer (current-buffer)))
807 (let ((str
808 (or name
809 (read-from-minibuffer
810 (format "Set bookmark (%s): " default)
812 bookmark-minibuffer-read-name-map
813 nil nil defaults))))
814 (and (string-equal str "") (setq str default))
815 (bookmark-store str (cdr record) no-overwrite)
817 ;; Ask for an annotation buffer for this bookmark
818 (when bookmark-use-annotations
819 (bookmark-edit-annotation str))))
820 (setq bookmark-yank-point nil)
821 (setq bookmark-current-buffer nil)))
824 (defun bookmark-kill-line (&optional newline-too)
825 "Kill from point to end of line.
826 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
827 Does not affect the kill ring."
828 (let ((eol (line-end-position)))
829 (delete-region (point) eol)
830 (if (and newline-too (looking-at "\n"))
831 (delete-char 1))))
834 ;; Defvars to avoid compilation warnings:
835 (defvar bookmark-annotation-name nil
836 "Variable holding the name of the bookmark.
837 This is used in `bookmark-edit-annotation' to record the bookmark
838 whose annotation is being edited.")
841 (defun bookmark-default-annotation-text (bookmark-name)
842 "Return default annotation text for BOOKMARK-NAME.
843 The default annotation text is simply some text explaining how to use
844 annotations."
845 (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
846 "# All lines which start with a '#' will be deleted.\n"
847 "# Type C-c C-c when done.\n#\n"
848 "# Author: " (user-full-name) " <" (user-login-name) "@"
849 (system-name) ">\n"
850 "# Date: " (current-time-string) "\n"))
853 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
854 'bookmark-edit-annotation-text-func "23.1")
855 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
856 "Function to return default text to use for a bookmark annotation.
857 It takes one argument, the name of the bookmark, as a string.")
859 (defvar bookmark-edit-annotation-mode-map
860 (let ((map (make-sparse-keymap)))
861 (set-keymap-parent map text-mode-map)
862 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
863 map)
864 "Keymap for editing an annotation of a bookmark.")
867 (defun bookmark-edit-annotation-mode (bookmark-name-or-record)
868 "Mode for editing the annotation of bookmark BOOKMARK-NAME-OR-RECORD.
869 When you have finished composing, type \\[bookmark-send-annotation].
871 \\{bookmark-edit-annotation-mode-map}"
872 (interactive)
873 (kill-all-local-variables)
874 (make-local-variable 'bookmark-annotation-name)
875 (setq bookmark-annotation-name bookmark-name-or-record)
876 (use-local-map bookmark-edit-annotation-mode-map)
877 (setq major-mode 'bookmark-edit-annotation-mode
878 mode-name "Edit Bookmark Annotation")
879 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
880 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
881 (if (and annotation (not (string-equal annotation "")))
882 (insert annotation)))
883 (run-mode-hooks 'text-mode-hook))
886 (defun bookmark-send-edited-annotation ()
887 "Use buffer contents as annotation for a bookmark.
888 Lines beginning with `#' are ignored."
889 (interactive)
890 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
891 (error "Not in bookmark-edit-annotation-mode"))
892 (goto-char (point-min))
893 (while (< (point) (point-max))
894 (if (looking-at "^#")
895 (bookmark-kill-line t)
896 (forward-line 1)))
897 ;; Take no chances with text properties.
898 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
899 (bookmark-name bookmark-annotation-name))
900 (bookmark-set-annotation bookmark-name annotation)
901 (setq bookmark-alist-modification-count
902 (1+ bookmark-alist-modification-count))
903 (bookmark-bmenu-surreptitiously-rebuild-list))
904 (kill-buffer (current-buffer)))
907 (defun bookmark-edit-annotation (bookmark-name-or-record)
908 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
909 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
910 (bookmark-edit-annotation-mode bookmark-name-or-record))
913 (defun bookmark-buffer-name ()
914 "Return the name of the current buffer in a form usable as a bookmark name.
915 If the buffer is associated with a file or directory, use that name."
916 (cond
917 ;; Or are we a file?
918 (buffer-file-name (file-name-nondirectory buffer-file-name))
919 ;; Or are we a directory?
920 ((and (boundp 'dired-directory) dired-directory)
921 (let* ((dirname (if (stringp dired-directory)
922 dired-directory
923 (car dired-directory)))
924 (idx (1- (length dirname))))
925 ;; Strip the trailing slash.
926 (if (= ?/ (aref dirname idx))
927 (file-name-nondirectory (substring dirname 0 idx))
928 ;; Else return the current-buffer
929 (buffer-name (current-buffer)))))
930 ;; If all else fails, use the buffer's name.
932 (buffer-name (current-buffer)))))
935 (defun bookmark-yank-word ()
936 "Get the next word from buffer `bookmark-current-buffer' and append
937 it to the name of the bookmark currently being set, advancing
938 `bookmark-yank-point' by one word."
939 (interactive)
940 (let ((string (with-current-buffer bookmark-current-buffer
941 (goto-char bookmark-yank-point)
942 (buffer-substring-no-properties
943 (point)
944 (progn
945 (forward-word 1)
946 (setq bookmark-yank-point (point)))))))
947 (insert string)))
949 (defun bookmark-buffer-file-name ()
950 "Return the current buffer's file in a way useful for bookmarks."
951 ;; Abbreviate the path, both so it's shorter and so it's more
952 ;; portable. E.g., the user's home dir might be a different
953 ;; path on different machines, but "~/" will still reach it.
954 (abbreviate-file-name
955 (cond
956 (buffer-file-name buffer-file-name)
957 ((and (boundp 'dired-directory) dired-directory)
958 (if (stringp dired-directory)
959 dired-directory
960 (car dired-directory)))
961 (t (error "Buffer not visiting a file or directory")))))
964 (defun bookmark-maybe-load-default-file ()
965 "If bookmarks have not been loaded from the default place, load them."
966 (and (not bookmarks-already-loaded)
967 (null bookmark-alist)
968 (prog2
969 (and
970 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
971 ;; to be renamed.
972 (file-exists-p bookmark-old-default-file)
973 (not (file-exists-p bookmark-default-file))
974 (rename-file bookmark-old-default-file
975 bookmark-default-file))
976 ;; return t so the `and' will continue...
979 (file-readable-p bookmark-default-file)
980 (bookmark-load bookmark-default-file t t)
981 (setq bookmarks-already-loaded t)))
984 (defun bookmark-maybe-sort-alist ()
985 "Return `bookmark-alist' for display.
986 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
987 (if bookmark-sort-flag
988 (sort (copy-alist bookmark-alist)
989 (function
990 (lambda (x y) (string-lessp (car x) (car y)))))
991 bookmark-alist))
994 (defvar bookmark-after-jump-hook nil
995 "Hook run after `bookmark-jump' jumps to a bookmark.
996 Useful for example to unhide text in `outline-mode'.")
998 (defun bookmark--jump-via (bookmark-name-or-record display-function)
999 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
1000 current buffer as argument.
1002 After calling DISPLAY-FUNCTION, set window point to the point specified
1003 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1004 and then show any annotations for this bookmark."
1005 (bookmark-handle-bookmark bookmark-name-or-record)
1006 (save-current-buffer
1007 (funcall display-function (current-buffer)))
1008 (let ((win (get-buffer-window (current-buffer) 0)))
1009 (if win (set-window-point win (point))))
1010 ;; FIXME: we used to only run bookmark-after-jump-hook in
1011 ;; `bookmark-jump' itself, but in none of the other commands.
1012 (run-hooks 'bookmark-after-jump-hook)
1013 (if bookmark-automatically-show-annotations
1014 ;; if there is an annotation for this bookmark,
1015 ;; show it in a buffer.
1016 (bookmark-show-annotation bookmark-name-or-record)))
1019 ;;;###autoload
1020 (defun bookmark-jump (bookmark &optional display-func)
1021 "Jump to bookmark BOOKMARK (a point in some file).
1022 You may have a problem using this function if the value of variable
1023 `bookmark-alist' is nil. If that happens, you need to load in some
1024 bookmarks. See help on function `bookmark-load' for more about
1025 this.
1027 If the file pointed to by BOOKMARK no longer exists, you will be asked
1028 if you wish to give the bookmark a new location, and `bookmark-jump'
1029 will then jump to the new location, as well as recording it in place
1030 of the old one in the permanent bookmark record.
1032 BOOKMARK is usually a bookmark name (a string). It can also be a
1033 bookmark record, but this is usually only done by programmatic callers.
1035 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1036 bookmark. It defaults to `switch-to-buffer'. A typical value for
1037 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1038 (interactive
1039 (list (bookmark-completing-read "Jump to bookmark"
1040 bookmark-current-bookmark)))
1041 (unless bookmark
1042 (error "No bookmark specified"))
1043 (bookmark-maybe-historicize-string bookmark)
1044 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1047 ;;;###autoload
1048 (defun bookmark-jump-other-window (bookmark)
1049 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1050 (interactive
1051 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1052 bookmark-current-bookmark)))
1053 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1056 (defun bookmark-jump-noselect (bookmark)
1057 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1058 The return value has the form (BUFFER . POINT).
1060 Note: this function is deprecated and is present for Emacs 22
1061 compatibility only."
1062 (declare (obsolete bookmark-handle-bookmark "23.1"))
1063 (save-excursion
1064 (bookmark-handle-bookmark bookmark)
1065 (cons (current-buffer) (point))))
1067 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1068 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1069 if it has none. This changes current buffer and point and returns nil,
1070 or signals a `file-error'.
1072 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1073 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1074 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1075 (condition-case err
1076 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1077 'bookmark-default-handler)
1078 (bookmark-get-bookmark bookmark-name-or-record))
1079 (bookmark-error-no-filename ;file-error
1080 ;; We were unable to find the marked file, so ask if user wants to
1081 ;; relocate the bookmark, else remind them to consider deletion.
1082 (when (stringp bookmark-name-or-record)
1083 ;; `bookmark-name-or-record' can be either a bookmark name
1084 ;; (from `bookmark-alist') or a bookmark object. If it's an
1085 ;; object, we assume it's a bookmark used internally by some
1086 ;; other package.
1087 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1088 (when file ;Don't know how to relocate if there's no `file'.
1089 ;; If file is not a dir, directory-file-name just returns file.
1090 (let ((display-name (directory-file-name file)))
1091 (ding)
1092 ;; Dialog boxes can accept a file target, but usually don't
1093 ;; know how to accept a directory target (at least, this
1094 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1095 ;; true on Windows as well). So we suppress file dialogs
1096 ;; when relocating.
1097 (let ((use-dialog-box nil)
1098 (use-file-dialog nil))
1099 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1100 bookmark-name-or-record "\"? "))
1101 (progn
1102 (bookmark-relocate bookmark-name-or-record)
1103 ;; Try again.
1104 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1105 'bookmark-default-handler)
1106 (bookmark-get-bookmark bookmark-name-or-record)))
1107 (message
1108 "Bookmark not relocated; consider removing it (%s)."
1109 bookmark-name-or-record)
1110 (signal (car err) (cdr err))))))))))
1111 ;; Added by db.
1112 (when (stringp bookmark-name-or-record)
1113 (setq bookmark-current-bookmark bookmark-name-or-record))
1114 nil)
1116 (put 'bookmark-error-no-filename
1117 'error-conditions
1118 '(error bookmark-errors bookmark-error-no-filename))
1119 (put 'bookmark-error-no-filename
1120 'error-message
1121 "Bookmark has no associated file (or directory)")
1123 (defun bookmark-default-handler (bmk-record)
1124 "Default handler to jump to a particular bookmark location.
1125 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1126 Changes current buffer and point and returns nil, or signals a `file-error'."
1127 (let ((file (bookmark-get-filename bmk-record))
1128 (buf (bookmark-prop-get bmk-record 'buffer))
1129 (forward-str (bookmark-get-front-context-string bmk-record))
1130 (behind-str (bookmark-get-rear-context-string bmk-record))
1131 (place (bookmark-get-position bmk-record)))
1132 (set-buffer
1133 (cond
1134 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1135 (find-file-noselect file))
1136 ;; No file found. See if buffer BUF have been created.
1137 ((and buf (get-buffer buf)))
1138 (t ;; If not, raise error.
1139 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1140 (if place (goto-char place))
1141 ;; Go searching forward first. Then, if forward-str exists and
1142 ;; was found in the file, we can search backward for behind-str.
1143 ;; Rationale is that if text was inserted between the two in the
1144 ;; file, it's better to be put before it so you can read it,
1145 ;; rather than after and remain perhaps unaware of the changes.
1146 (when (and forward-str (search-forward forward-str (point-max) t))
1147 (goto-char (match-beginning 0)))
1148 (when (and behind-str (search-backward behind-str (point-min) t))
1149 (goto-char (match-end 0)))
1150 nil))
1152 ;;;###autoload
1153 (defun bookmark-relocate (bookmark-name)
1154 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1156 This makes an already existing bookmark point to that file, instead of
1157 the one it used to point at. Useful when a file has been renamed
1158 after a bookmark was set in it."
1159 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1160 (bookmark-maybe-historicize-string bookmark-name)
1161 (bookmark-maybe-load-default-file)
1162 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1163 (newloc (abbreviate-file-name
1164 (expand-file-name
1165 (read-file-name
1166 (format "Relocate %s to: " bookmark-name)
1167 (file-name-directory bmrk-filename))))))
1168 (bookmark-set-filename bookmark-name newloc)
1169 (setq bookmark-alist-modification-count
1170 (1+ bookmark-alist-modification-count))
1171 (if (bookmark-time-to-save-p)
1172 (bookmark-save))
1173 (bookmark-bmenu-surreptitiously-rebuild-list)))
1176 ;;;###autoload
1177 (defun bookmark-insert-location (bookmark-name &optional no-history)
1178 "Insert the name of the file associated with BOOKMARK-NAME.
1180 Optional second arg NO-HISTORY means don't record this in the
1181 minibuffer history list `bookmark-history'."
1182 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1183 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1184 (insert (bookmark-location bookmark-name)))
1186 ;;;###autoload
1187 (defalias 'bookmark-locate 'bookmark-insert-location)
1189 (defun bookmark-location (bookmark-name-or-record)
1190 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1191 (bookmark-maybe-load-default-file)
1192 ;; We could call the `handler' and ask for it to construct a description
1193 ;; dynamically: it would open up several new possibilities, but it
1194 ;; would have the major disadvantage of forcing to load each and
1195 ;; every handler when the user calls bookmark-menu.
1196 (or (bookmark-prop-get bookmark-name-or-record 'location)
1197 (bookmark-get-filename bookmark-name-or-record)
1198 "-- Unknown location --"))
1201 ;;;###autoload
1202 (defun bookmark-rename (old-name &optional new-name)
1203 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1204 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1205 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1207 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1208 as an argument. If called with two strings, then no prompting is done.
1209 You must pass at least OLD-NAME when calling from Lisp.
1211 While you are entering the new name, consecutive C-w's insert
1212 consecutive words from the text of the buffer into the new bookmark
1213 name."
1214 (interactive (list (bookmark-completing-read "Old bookmark name")))
1215 (bookmark-maybe-historicize-string old-name)
1216 (bookmark-maybe-load-default-file)
1218 (setq bookmark-yank-point (point))
1219 (setq bookmark-current-buffer (current-buffer))
1220 (let ((final-new-name
1221 (or new-name ; use second arg, if non-nil
1222 (read-from-minibuffer
1223 "New name: "
1225 (let ((now-map (copy-keymap minibuffer-local-map)))
1226 (define-key now-map "\C-w" 'bookmark-yank-word)
1227 now-map)
1229 'bookmark-history))))
1230 (bookmark-set-name old-name final-new-name)
1231 (setq bookmark-current-bookmark final-new-name)
1232 (bookmark-bmenu-surreptitiously-rebuild-list)
1233 (setq bookmark-alist-modification-count
1234 (1+ bookmark-alist-modification-count))
1235 (if (bookmark-time-to-save-p)
1236 (bookmark-save))))
1239 ;;;###autoload
1240 (defun bookmark-insert (bookmark-name)
1241 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1242 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1244 You may have a problem using this function if the value of variable
1245 `bookmark-alist' is nil. If that happens, you need to load in some
1246 bookmarks. See help on function `bookmark-load' for more about
1247 this."
1248 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1249 (bookmark-maybe-historicize-string bookmark-name)
1250 (bookmark-maybe-load-default-file)
1251 (let ((orig-point (point))
1252 (str-to-insert
1253 (save-current-buffer
1254 (bookmark-handle-bookmark bookmark-name)
1255 (buffer-string))))
1256 (insert str-to-insert)
1257 (push-mark)
1258 (goto-char orig-point)))
1261 ;;;###autoload
1262 (defun bookmark-delete (bookmark-name &optional batch)
1263 "Delete BOOKMARK-NAME from the bookmark list.
1265 Removes only the first instance of a bookmark with that name. If
1266 there are one or more other bookmarks with the same name, they will
1267 not be deleted. Defaults to the \"current\" bookmark (that is, the
1268 one most recently used in this file, if any).
1269 Optional second arg BATCH means don't update the bookmark list buffer,
1270 probably because we were called from there."
1271 (interactive
1272 (list (bookmark-completing-read "Delete bookmark"
1273 bookmark-current-bookmark)))
1274 (bookmark-maybe-historicize-string bookmark-name)
1275 (bookmark-maybe-load-default-file)
1276 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1277 (setq bookmark-alist (delq will-go bookmark-alist))
1278 ;; Added by db, nil bookmark-current-bookmark if the last
1279 ;; occurrence has been deleted
1280 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1281 (setq bookmark-current-bookmark nil)))
1282 (unless batch
1283 (bookmark-bmenu-surreptitiously-rebuild-list))
1284 (setq bookmark-alist-modification-count
1285 (1+ bookmark-alist-modification-count))
1286 (when (bookmark-time-to-save-p)
1287 (bookmark-save)))
1290 (defun bookmark-time-to-save-p (&optional final-time)
1291 "Return t if it is time to save bookmarks to disk, nil otherwise.
1292 Optional argument FINAL-TIME means this is being called when Emacs
1293 is being killed, so save even if `bookmark-save-flag' is a number and
1294 is greater than `bookmark-alist-modification-count'."
1295 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1296 (cond (final-time
1297 (and (> bookmark-alist-modification-count 0)
1298 bookmark-save-flag))
1299 ((numberp bookmark-save-flag)
1300 (>= bookmark-alist-modification-count bookmark-save-flag))
1302 nil)))
1305 ;;;###autoload
1306 (defun bookmark-write ()
1307 "Write bookmarks to a file (reading the file name with the minibuffer).
1308 Don't use this in Lisp programs; use `bookmark-save' instead."
1309 (interactive)
1310 (bookmark-maybe-load-default-file)
1311 (bookmark-save t))
1314 ;;;###autoload
1315 (defun bookmark-save (&optional parg file)
1316 "Save currently defined bookmarks.
1317 Saves by default in the file defined by the variable
1318 `bookmark-default-file'. With a prefix arg, save it in file FILE
1319 \(second argument).
1321 If you are calling this from Lisp, the two arguments are PARG and
1322 FILE, and if you just want it to write to the default file, then
1323 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1324 instead. If you pass in one argument, and it is non-nil, then the
1325 user will be interactively queried for a file to save in.
1327 When you want to load in the bookmarks from a file, use
1328 `bookmark-load', \\[bookmark-load]. That function will prompt you
1329 for a file, defaulting to the file defined by variable
1330 `bookmark-default-file'."
1331 (interactive "P")
1332 (bookmark-maybe-load-default-file)
1333 (cond
1334 ((and (null parg) (null file))
1335 ;;whether interactive or not, write to default file
1336 (bookmark-write-file bookmark-default-file))
1337 ((and (null parg) file)
1338 ;;whether interactive or not, write to given file
1339 (bookmark-write-file file))
1340 ((and parg (not file))
1341 ;;have been called interactively w/ prefix arg
1342 (let ((file (read-file-name "File to save bookmarks in: ")))
1343 (bookmark-write-file file)))
1344 (t ; someone called us with prefix-arg *and* a file, so just write to file
1345 (bookmark-write-file file)))
1346 ;; signal that we have synced the bookmark file by setting this to
1347 ;; 0. If there was an error at any point before, it will not get
1348 ;; set, which is what we want.
1349 (setq bookmark-alist-modification-count 0))
1353 (defun bookmark-write-file (file)
1354 "Write `bookmark-alist' to FILE."
1355 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1356 (with-current-buffer (get-buffer-create " *Bookmarks*")
1357 (goto-char (point-min))
1358 (delete-region (point-min) (point-max))
1359 (let ((print-length nil)
1360 (print-level nil)
1361 ;; See bug #12503 for why we bind `print-circle'. Users
1362 ;; can define their own bookmark types, which can result in
1363 ;; arbitrary Lisp objects being stored in bookmark records,
1364 ;; and some users create objects containing circularities.
1365 (print-circle t))
1366 (bookmark-insert-file-format-version-stamp)
1367 (insert "(")
1368 ;; Rather than a single call to `pp' we make one per bookmark.
1369 ;; Apparently `pp' has a poor algorithmic complexity, so this
1370 ;; scales a lot better. bug#4485.
1371 (dolist (i bookmark-alist) (pp i (current-buffer)))
1372 (insert ")")
1373 (let ((version-control
1374 (cond
1375 ((null bookmark-version-control) nil)
1376 ((eq 'never bookmark-version-control) 'never)
1377 ((eq 'nospecial bookmark-version-control) version-control)
1378 (t t))))
1379 (condition-case nil
1380 (write-region (point-min) (point-max) file)
1381 (file-error (message "Can't write %s" file)))
1382 (kill-buffer (current-buffer))
1383 (bookmark-maybe-message
1384 "Saving bookmarks to file %s...done" file)))))
1387 (defun bookmark-import-new-list (new-list)
1388 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1389 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1390 they conflict with existing bookmark names."
1391 (let ((names (bookmark-all-names)))
1392 (dolist (full-record new-list)
1393 (bookmark-maybe-rename full-record names)
1394 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1395 (push (bookmark-name-from-full-record full-record) names))))
1398 (defun bookmark-maybe-rename (full-record names)
1399 "Rename bookmark FULL-RECORD if its current name is already used.
1400 This is a helper for `bookmark-import-new-list'."
1401 (let ((found-name (bookmark-name-from-full-record full-record)))
1402 (if (member found-name names)
1403 ;; We've got a conflict, so generate a new name
1404 (let ((count 2)
1405 (new-name found-name))
1406 (while (member new-name names)
1407 (setq new-name (concat found-name (format "<%d>" count)))
1408 (setq count (1+ count)))
1409 (bookmark-set-name full-record new-name)))))
1412 ;;;###autoload
1413 (defun bookmark-load (file &optional overwrite no-msg)
1414 "Load bookmarks from FILE (which must be in bookmark format).
1415 Appends loaded bookmarks to the front of the list of bookmarks. If
1416 optional second argument OVERWRITE is non-nil, existing bookmarks are
1417 destroyed. Optional third arg NO-MSG means don't display any messages
1418 while loading.
1420 If you load a file that doesn't contain a proper bookmark alist, you
1421 will corrupt Emacs's bookmark list. Generally, you should only load
1422 in files that were created with the bookmark functions in the first
1423 place. Your own personal bookmark file, `~/.emacs.bmk', is
1424 maintained automatically by Emacs; you shouldn't need to load it
1425 explicitly.
1427 If you load a file containing bookmarks with the same names as
1428 bookmarks already present in your Emacs, the new bookmarks will get
1429 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1430 method buffers use to resolve name collisions."
1431 (interactive
1432 (list (read-file-name
1433 (format "Load bookmarks from: (%s) "
1434 bookmark-default-file)
1435 ;;Default might not be used often,
1436 ;;but there's no better default, and
1437 ;;I guess it's better than none at all.
1438 "~/" bookmark-default-file 'confirm)))
1439 (setq file (abbreviate-file-name (expand-file-name file)))
1440 (if (not (file-readable-p file))
1441 (error "Cannot read bookmark file %s" file)
1442 (if (null no-msg)
1443 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1444 (with-current-buffer (let ((enable-local-variables nil))
1445 (find-file-noselect file))
1446 (goto-char (point-min))
1447 (bookmark-maybe-upgrade-file-format)
1448 (let ((blist (bookmark-alist-from-buffer)))
1449 (if (listp blist)
1450 (progn
1451 (if overwrite
1452 (progn
1453 (setq bookmark-alist blist)
1454 (setq bookmark-alist-modification-count 0))
1455 ;; else
1456 (bookmark-import-new-list blist)
1457 (setq bookmark-alist-modification-count
1458 (1+ bookmark-alist-modification-count)))
1459 (if (string-equal
1460 (abbreviate-file-name
1461 (expand-file-name bookmark-default-file))
1462 file)
1463 (setq bookmarks-already-loaded t))
1464 (bookmark-bmenu-surreptitiously-rebuild-list))
1465 (error "Invalid bookmark list in %s" file)))
1466 (kill-buffer (current-buffer)))
1467 (if (null no-msg)
1468 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1472 ;;; Code supporting the dired-like bookmark menu.
1473 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1476 (defvar bookmark-bmenu-hidden-bookmarks ())
1479 (defvar bookmark-bmenu-mode-map
1480 (let ((map (make-keymap)))
1481 (set-keymap-parent map special-mode-map)
1482 (define-key map "v" 'bookmark-bmenu-select)
1483 (define-key map "w" 'bookmark-bmenu-locate)
1484 (define-key map "2" 'bookmark-bmenu-2-window)
1485 (define-key map "1" 'bookmark-bmenu-1-window)
1486 (define-key map "j" 'bookmark-bmenu-this-window)
1487 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1488 (define-key map "f" 'bookmark-bmenu-this-window)
1489 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1490 (define-key map "o" 'bookmark-bmenu-other-window)
1491 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1492 (define-key map "s" 'bookmark-bmenu-save)
1493 (define-key map "k" 'bookmark-bmenu-delete)
1494 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1495 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1496 (define-key map "d" 'bookmark-bmenu-delete)
1497 (define-key map " " 'next-line)
1498 (define-key map "n" 'next-line)
1499 (define-key map "p" 'previous-line)
1500 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1501 (define-key map "u" 'bookmark-bmenu-unmark)
1502 (define-key map "m" 'bookmark-bmenu-mark)
1503 (define-key map "l" 'bookmark-bmenu-load)
1504 (define-key map "r" 'bookmark-bmenu-rename)
1505 (define-key map "R" 'bookmark-bmenu-relocate)
1506 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1507 (define-key map "a" 'bookmark-bmenu-show-annotation)
1508 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1509 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1510 (define-key map "/" 'bookmark-bmenu-search)
1511 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1512 map))
1514 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1515 ;; data.
1516 (put 'bookmark-bmenu-mode 'mode-class 'special)
1519 ;; todo: need to display whether or not bookmark exists as a buffer in
1520 ;; flag column.
1522 ;; Format:
1523 ;; FLAGS BOOKMARK [ LOCATION ]
1526 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1527 "Rebuild the Bookmark List if it exists.
1528 Don't affect the buffer ring order."
1529 (if (get-buffer "*Bookmark List*")
1530 (save-excursion
1531 (save-window-excursion
1532 (bookmark-bmenu-list)))))
1535 ;;;###autoload
1536 (defun bookmark-bmenu-list ()
1537 "Display a list of existing bookmarks.
1538 The list is displayed in a buffer named `*Bookmark List*'.
1539 The leftmost column displays a D if the bookmark is flagged for
1540 deletion, or > if it is flagged for displaying."
1541 (interactive)
1542 (bookmark-maybe-load-default-file)
1543 (let ((buf (get-buffer-create "*Bookmark List*")))
1544 (if (called-interactively-p 'interactive)
1545 (switch-to-buffer buf)
1546 (set-buffer buf)))
1547 (let ((inhibit-read-only t))
1548 (erase-buffer)
1549 (if (not bookmark-bmenu-use-header-line)
1550 (insert "% Bookmark\n- --------\n"))
1551 (add-text-properties (point-min) (point)
1552 '(font-lock-face bookmark-menu-heading))
1553 (dolist (full-record (bookmark-maybe-sort-alist))
1554 (let ((name (bookmark-name-from-full-record full-record))
1555 (annotation (bookmark-get-annotation full-record))
1556 (start (point))
1557 end)
1558 ;; if a bookmark has an annotation, prepend a "*"
1559 ;; in the list of bookmarks.
1560 (insert (if (and annotation (not (string-equal annotation "")))
1561 " *" " ")
1562 name)
1563 (setq end (point))
1564 (put-text-property
1565 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1566 (when (display-mouse-p)
1567 (add-text-properties
1568 (+ bookmark-bmenu-marks-width start) end
1569 '(font-lock-face bookmark-menu-bookmark
1570 mouse-face highlight
1571 follow-link t
1572 help-echo "mouse-2: go to this bookmark in other window")))
1573 (insert "\n")))
1574 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1575 (goto-char (point-min))
1576 (bookmark-bmenu-mode)
1577 (if bookmark-bmenu-use-header-line
1578 (bookmark-bmenu-set-header)
1579 (forward-line bookmark-bmenu-inline-header-height))
1580 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1581 (bookmark-bmenu-toggle-filenames t))))
1583 ;;;###autoload
1584 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1585 ;;;###autoload
1586 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1588 (defun bookmark-bmenu-set-header ()
1589 "Sets the immutable header line."
1590 (let ((header (concat "%% " "Bookmark")))
1591 (when bookmark-bmenu-toggle-filenames
1592 (setq header (concat header
1593 (make-string (- bookmark-bmenu-file-column
1594 (- (length header) 3)) ?\s)
1595 "File")))
1596 (let ((pos 0))
1597 (while (string-match "[ \t\n]+" header pos)
1598 (setq pos (match-end 0))
1599 (put-text-property (match-beginning 0) pos 'display
1600 (list 'space :align-to (- pos 1))
1601 header)))
1602 (put-text-property 0 2 'face 'fixed-pitch header)
1603 (setq header (concat (propertize " " 'display '(space :align-to 0))
1604 header))
1605 ;; Code derived from `buff-menu.el'.
1606 (setq header-line-format header)))
1608 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1609 "Major mode for editing a list of bookmarks.
1610 Each line describes one of the bookmarks in Emacs.
1611 Letters do not insert themselves; instead, they are commands.
1612 Bookmark names preceded by a \"*\" have annotations.
1613 \\<bookmark-bmenu-mode-map>
1614 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1615 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1616 Also show bookmarks marked using m in other windows.
1617 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1618 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1619 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1620 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1621 together with bookmark selected before this one in another window.
1622 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1623 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1624 so the bookmark menu bookmark remains visible in its window.
1625 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1626 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1627 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1628 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1629 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1630 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1631 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1632 With a prefix arg, prompts for a file to save in.
1633 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1634 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1635 With prefix argument, also move up one line.
1636 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1637 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1638 in another buffer.
1639 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1640 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1641 (setq truncate-lines t)
1642 (setq buffer-read-only t))
1645 (defun bookmark-bmenu-toggle-filenames (&optional show)
1646 "Toggle whether filenames are shown in the bookmark list.
1647 Optional argument SHOW means show them unconditionally."
1648 (interactive)
1649 (cond
1650 (show
1651 (setq bookmark-bmenu-toggle-filenames nil)
1652 (bookmark-bmenu-show-filenames)
1653 (setq bookmark-bmenu-toggle-filenames t))
1654 (bookmark-bmenu-toggle-filenames
1655 (bookmark-bmenu-hide-filenames)
1656 (setq bookmark-bmenu-toggle-filenames nil))
1658 (bookmark-bmenu-show-filenames)
1659 (setq bookmark-bmenu-toggle-filenames t)))
1660 (when bookmark-bmenu-use-header-line
1661 (bookmark-bmenu-set-header)))
1664 (defun bookmark-bmenu-show-filenames (&optional force)
1665 "In an interactive bookmark list, show filenames along with bookmarks.
1666 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1667 mainly for debugging, and should not be necessary in normal use."
1668 (if (and (not force) bookmark-bmenu-toggle-filenames)
1669 nil ;already shown, so do nothing
1670 (with-buffer-modified-unmodified
1671 (save-excursion
1672 (save-window-excursion
1673 (goto-char (point-min))
1674 (if (not bookmark-bmenu-use-header-line)
1675 (forward-line bookmark-bmenu-inline-header-height))
1676 (setq bookmark-bmenu-hidden-bookmarks ())
1677 (let ((inhibit-read-only t))
1678 (while (< (point) (point-max))
1679 (let ((bmrk (bookmark-bmenu-bookmark)))
1680 (push bmrk bookmark-bmenu-hidden-bookmarks)
1681 (let ((start (line-end-position)))
1682 (move-to-column bookmark-bmenu-file-column t)
1683 ;; Strip off `mouse-face' from the white spaces region.
1684 (if (display-mouse-p)
1685 (remove-text-properties start (point)
1686 '(mouse-face nil help-echo nil))))
1687 (delete-region (point) (progn (end-of-line) (point)))
1688 (insert " ")
1689 ;; Pass the NO-HISTORY arg:
1690 (bookmark-insert-location bmrk t)
1691 (forward-line 1)))))))))
1694 (defun bookmark-bmenu-hide-filenames (&optional force)
1695 "In an interactive bookmark list, hide the filenames of the bookmarks.
1696 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1697 mainly for debugging, and should not be necessary in normal use."
1698 (when (and (not force) bookmark-bmenu-toggle-filenames)
1699 ;; nothing to hide if above is nil
1700 (with-buffer-modified-unmodified
1701 (save-excursion
1702 (goto-char (point-min))
1703 (if (not bookmark-bmenu-use-header-line)
1704 (forward-line bookmark-bmenu-inline-header-height))
1705 (setq bookmark-bmenu-hidden-bookmarks
1706 (nreverse bookmark-bmenu-hidden-bookmarks))
1707 (let ((inhibit-read-only t))
1708 (while bookmark-bmenu-hidden-bookmarks
1709 (move-to-column bookmark-bmenu-marks-width t)
1710 (bookmark-kill-line)
1711 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1712 (start (point)))
1713 (insert name)
1714 (put-text-property start (point) 'bookmark-name-prop name)
1715 (if (display-mouse-p)
1716 (add-text-properties
1717 start (point)
1718 '(font-lock-face bookmark-menu-bookmark
1719 mouse-face highlight
1720 follow-link t help-echo
1721 "mouse-2: go to this bookmark in other window"))))
1722 (forward-line 1)))))))
1725 (defun bookmark-bmenu-ensure-position ()
1726 "If point is not on a bookmark line, move it to one.
1727 If before the first bookmark line, move to the first; if after the
1728 last full line, move to the last full line. The return value is undefined."
1729 (cond ((and (not bookmark-bmenu-use-header-line)
1730 (< (count-lines (point-min) (point))
1731 bookmark-bmenu-inline-header-height))
1732 (goto-char (point-min))
1733 (forward-line bookmark-bmenu-inline-header-height))
1734 ((and (bolp) (eobp))
1735 (beginning-of-line 0))))
1738 (defun bookmark-bmenu-bookmark ()
1739 "Return the bookmark for this line in an interactive bookmark list buffer."
1740 (bookmark-bmenu-ensure-position)
1741 (save-excursion
1742 (beginning-of-line)
1743 (forward-char bookmark-bmenu-marks-width)
1744 (get-text-property (point) 'bookmark-name-prop)))
1747 (defun bookmark-show-annotation (bookmark-name-or-record)
1748 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1749 if an annotation exists."
1750 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1751 (when (and annotation (not (string-equal annotation "")))
1752 (save-excursion
1753 (let ((old-buf (current-buffer)))
1754 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1755 (delete-region (point-min) (point-max))
1756 (insert annotation)
1757 (goto-char (point-min))
1758 (switch-to-buffer-other-window old-buf))))))
1761 (defun bookmark-show-all-annotations ()
1762 "Display the annotations for all bookmarks in a buffer."
1763 (save-selected-window
1764 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1765 (delete-region (point-min) (point-max))
1766 (dolist (full-record bookmark-alist)
1767 (let* ((name (bookmark-name-from-full-record full-record))
1768 (ann (bookmark-get-annotation full-record)))
1769 (insert (concat name ":\n"))
1770 (if (and ann (not (string-equal ann "")))
1771 ;; insert the annotation, indented by 4 spaces.
1772 (progn
1773 (save-excursion (insert ann) (unless (bolp)
1774 (insert "\n")))
1775 (while (< (point) (point-max))
1776 (beginning-of-line) ; paranoia
1777 (insert " ")
1778 (forward-line)
1779 (end-of-line))))))
1780 (goto-char (point-min))))
1783 (defun bookmark-bmenu-mark ()
1784 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1785 (interactive)
1786 (beginning-of-line)
1787 (bookmark-bmenu-ensure-position)
1788 (with-buffer-modified-unmodified
1789 (let ((inhibit-read-only t))
1790 (delete-char 1)
1791 (insert ?>)
1792 (forward-line 1)
1793 (bookmark-bmenu-ensure-position))))
1796 (defun bookmark-bmenu-select ()
1797 "Select this line's bookmark; also display bookmarks marked with `>'.
1798 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1799 (interactive)
1800 (let ((bmrk (bookmark-bmenu-bookmark))
1801 (menu (current-buffer))
1802 (others ())
1803 tem)
1804 (goto-char (point-min))
1805 (while (re-search-forward "^>" nil t)
1806 (setq tem (bookmark-bmenu-bookmark))
1807 (let ((inhibit-read-only t))
1808 (delete-char -1)
1809 (insert ?\s))
1810 (or (string-equal tem bmrk)
1811 (member tem others)
1812 (setq others (cons tem others))))
1813 (setq others (nreverse others)
1814 tem (/ (1- (frame-height)) (1+ (length others))))
1815 (delete-other-windows)
1816 (bookmark-jump bmrk)
1817 (bury-buffer menu)
1818 (if others
1819 (while others
1820 (split-window nil tem)
1821 (other-window 1)
1822 (bookmark-jump (car others))
1823 (setq others (cdr others)))
1824 (other-window 1))))
1827 (defun bookmark-bmenu-any-marks ()
1828 "Return non-nil if any bookmarks are marked in the marks column."
1829 (save-excursion
1830 (goto-char (point-min))
1831 (bookmark-bmenu-ensure-position)
1832 (catch 'found-mark
1833 (while (not (eobp))
1834 (beginning-of-line)
1835 (if (looking-at "^\\S-")
1836 (throw 'found-mark t)
1837 (forward-line 1)))
1838 nil)))
1841 (defun bookmark-bmenu-save (parg)
1842 "Save the current list into a bookmark file.
1843 With a prefix arg, prompts for a file to save them in."
1844 (interactive "P")
1845 (save-excursion
1846 (save-window-excursion
1847 (bookmark-save parg)
1848 (set-buffer-modified-p nil))))
1851 (defun bookmark-bmenu-load ()
1852 "Load the bookmark file and rebuild the bookmark menu-buffer."
1853 (interactive)
1854 (bookmark-bmenu-ensure-position)
1855 (save-excursion
1856 (save-window-excursion
1857 ;; This will call `bookmark-bmenu-list'
1858 (call-interactively 'bookmark-load))))
1861 (defun bookmark-bmenu-1-window ()
1862 "Select this line's bookmark, alone, in full frame."
1863 (interactive)
1864 (bookmark-jump (bookmark-bmenu-bookmark))
1865 (bury-buffer (other-buffer))
1866 (delete-other-windows))
1869 (defun bookmark-bmenu-2-window ()
1870 "Select this line's bookmark, with previous buffer in second window."
1871 (interactive)
1872 (let ((bmrk (bookmark-bmenu-bookmark))
1873 (menu (current-buffer))
1874 (pop-up-windows t))
1875 (delete-other-windows)
1876 (switch-to-buffer (other-buffer) nil t)
1877 (bookmark--jump-via bmrk 'pop-to-buffer)
1878 (bury-buffer menu)))
1881 (defun bookmark-bmenu-this-window ()
1882 "Select this line's bookmark in this window."
1883 (interactive)
1884 (bookmark-jump (bookmark-bmenu-bookmark)))
1887 (defun bookmark-bmenu-other-window ()
1888 "Select this line's bookmark in other window, leaving bookmark menu visible."
1889 (interactive)
1890 (let ((bookmark (bookmark-bmenu-bookmark)))
1891 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1894 (defun bookmark-bmenu-switch-other-window ()
1895 "Make the other window select this line's bookmark.
1896 The current window remains selected."
1897 (interactive)
1898 (let ((bookmark (bookmark-bmenu-bookmark))
1899 (fun (lambda (b) (display-buffer b t))))
1900 (bookmark--jump-via bookmark fun)))
1902 (defun bookmark-bmenu-other-window-with-mouse (event)
1903 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1904 (interactive "e")
1905 (with-current-buffer (window-buffer (posn-window (event-end event)))
1906 (save-excursion
1907 (goto-char (posn-point (event-end event)))
1908 (bookmark-bmenu-other-window))))
1911 (defun bookmark-bmenu-show-annotation ()
1912 "Show the annotation for the current bookmark in another window."
1913 (interactive)
1914 (let ((bookmark (bookmark-bmenu-bookmark)))
1915 (bookmark-show-annotation bookmark)))
1918 (defun bookmark-bmenu-show-all-annotations ()
1919 "Show the annotation for all bookmarks in another window."
1920 (interactive)
1921 (bookmark-show-all-annotations))
1924 (defun bookmark-bmenu-edit-annotation ()
1925 "Edit the annotation for the current bookmark in another window."
1926 (interactive)
1927 (let ((bookmark (bookmark-bmenu-bookmark)))
1928 (bookmark-edit-annotation bookmark)))
1931 (defun bookmark-bmenu-unmark (&optional backup)
1932 "Cancel all requested operations on bookmark on this line and move down.
1933 Optional BACKUP means move up."
1934 (interactive "P")
1935 (beginning-of-line)
1936 (bookmark-bmenu-ensure-position)
1937 (with-buffer-modified-unmodified
1938 (let ((inhibit-read-only t))
1939 (delete-char 1)
1940 ;; any flags to reset according to circumstances? How about a
1941 ;; flag indicating whether this bookmark is being visited?
1942 ;; well, we don't have this now, so maybe later.
1943 (insert " "))
1944 (forward-line (if backup -1 1))
1945 (bookmark-bmenu-ensure-position)))
1948 (defun bookmark-bmenu-backup-unmark ()
1949 "Move up and cancel all requested operations on bookmark on line above."
1950 (interactive)
1951 (forward-line -1)
1952 (bookmark-bmenu-ensure-position)
1953 (bookmark-bmenu-unmark)
1954 (forward-line -1)
1955 (bookmark-bmenu-ensure-position))
1958 (defun bookmark-bmenu-delete ()
1959 "Mark bookmark on this line to be deleted.
1960 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1961 (interactive)
1962 (beginning-of-line)
1963 (bookmark-bmenu-ensure-position)
1964 (with-buffer-modified-unmodified
1965 (let ((inhibit-read-only t))
1966 (delete-char 1)
1967 (insert ?D)
1968 (forward-line 1)
1969 (bookmark-bmenu-ensure-position))))
1972 (defun bookmark-bmenu-delete-backwards ()
1973 "Mark bookmark on this line to be deleted, then move up one line.
1974 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1975 (interactive)
1976 (bookmark-bmenu-delete)
1977 (forward-line -2)
1978 (bookmark-bmenu-ensure-position)
1979 (forward-line 1)
1980 (bookmark-bmenu-ensure-position))
1983 (defun bookmark-bmenu-execute-deletions ()
1984 "Delete bookmarks flagged `D'."
1985 (interactive)
1986 (message "Deleting bookmarks...")
1987 (let ((o-point (point))
1988 (o-str (save-excursion
1989 (beginning-of-line)
1990 (unless (looking-at "^D")
1991 (buffer-substring
1992 (point)
1993 (progn (end-of-line) (point))))))
1994 (o-col (current-column)))
1995 (goto-char (point-min))
1996 (unless bookmark-bmenu-use-header-line
1997 (forward-line 1))
1998 (while (re-search-forward "^D" (point-max) t)
1999 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2000 (bookmark-bmenu-list)
2001 (if o-str
2002 (progn
2003 (goto-char (point-min))
2004 (search-forward o-str)
2005 (beginning-of-line)
2006 (forward-char o-col))
2007 (goto-char o-point))
2008 (beginning-of-line)
2009 (message "Deleting bookmarks...done")
2013 (defun bookmark-bmenu-rename ()
2014 "Rename bookmark on current line. Prompts for a new name."
2015 (interactive)
2016 (let ((bmrk (bookmark-bmenu-bookmark))
2017 (thispoint (point)))
2018 (bookmark-rename bmrk)
2019 (goto-char thispoint)))
2022 (defun bookmark-bmenu-locate ()
2023 "Display location of this bookmark. Displays in the minibuffer."
2024 (interactive)
2025 (let ((bmrk (bookmark-bmenu-bookmark)))
2026 (message "%s" (bookmark-location bmrk))))
2028 (defun bookmark-bmenu-relocate ()
2029 "Change the file path of the bookmark on the current line,
2030 prompting with completion for the new path."
2031 (interactive)
2032 (let ((bmrk (bookmark-bmenu-bookmark))
2033 (thispoint (point)))
2034 (bookmark-relocate bmrk)
2035 (goto-char thispoint)))
2037 ;;; Bookmark-bmenu search
2039 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2040 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2041 (let ((bookmark-alist
2042 (cl-loop for i in bookmark-alist
2043 when (string-match regexp (car i)) collect i into new
2044 finally return new)))
2045 (bookmark-bmenu-list)))
2048 ;;;###autoload
2049 (defun bookmark-bmenu-search ()
2050 "Incremental search of bookmarks, hiding the non-matches as we go."
2051 (interactive)
2052 (let ((bmk (bookmark-bmenu-bookmark))
2053 (timer nil))
2054 (unwind-protect
2055 (minibuffer-with-setup-hook
2056 (lambda ()
2057 (setq timer (run-with-idle-timer
2058 bookmark-search-delay 'repeat
2059 #'(lambda (buf)
2060 (with-current-buffer buf
2061 (bookmark-bmenu-filter-alist-by-regexp
2062 (minibuffer-contents))))
2063 (current-buffer))))
2064 (read-string "Pattern: ")
2065 (when timer (cancel-timer timer) (setq timer nil)))
2066 (when timer ;; Signalled an error or a `quit'.
2067 (cancel-timer timer)
2068 (bookmark-bmenu-list)
2069 (bookmark-bmenu-goto-bookmark bmk)))))
2071 (defun bookmark-bmenu-goto-bookmark (name)
2072 "Move point to bookmark with name NAME."
2073 (goto-char (point-min))
2074 (while (not (equal name (bookmark-bmenu-bookmark)))
2075 (forward-line 1))
2076 (forward-line 0))
2080 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2082 (defun bookmark-menu-popup-paned-menu (event name entries)
2083 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2084 That is, ENTRIES is a list of strings which appear as the choices
2085 in the menu.
2086 The number of panes depends on the number of entries.
2087 The visible entries are truncated to `bookmark-menu-length', but the
2088 strings returned are not."
2089 (let ((f-height (/ (frame-height) 2))
2090 (pane-list nil)
2091 (iter 0))
2092 (while entries
2093 (let (lst
2094 (count 0))
2095 (while (and (< count f-height) entries)
2096 (let ((str (car entries)))
2097 (push (cons
2098 (if (> (length str) bookmark-menu-length)
2099 (substring str 0 bookmark-menu-length)
2100 str)
2101 str)
2102 lst)
2103 (setq entries (cdr entries))
2104 (setq count (1+ count))))
2105 (setq iter (1+ iter))
2106 (push (cons
2107 (format "-*- %s (%d) -*-" name iter)
2108 (nreverse lst))
2109 pane-list)))
2111 ;; Popup the menu and return the string.
2112 (x-popup-menu event (cons (concat "-*- " name " -*-")
2113 (nreverse pane-list)))))
2116 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2117 ;; following works, and for explaining what to do to make it work.
2119 ;; We MUST autoload EACH form used to set up this variable's value, so
2120 ;; that the whole job is done in loaddefs.el.
2122 ;; Emacs menubar stuff.
2124 ;;;###autoload
2125 (defvar menu-bar-bookmark-map
2126 (let ((map (make-sparse-keymap "Bookmark functions")))
2127 (bindings--define-key map [load]
2128 '(menu-item "Load a Bookmark File..." bookmark-load
2129 :help "Load bookmarks from a bookmark file)"))
2130 (bindings--define-key map [write]
2131 '(menu-item "Save Bookmarks As..." bookmark-write
2132 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2133 (bindings--define-key map [save]
2134 '(menu-item "Save Bookmarks" bookmark-save
2135 :help "Save currently defined bookmarks"))
2136 (bindings--define-key map [edit]
2137 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2138 :help "Display a list of existing bookmarks"))
2139 (bindings--define-key map [delete]
2140 '(menu-item "Delete Bookmark..." bookmark-delete
2141 :help "Delete a bookmark from the bookmark list"))
2142 (bindings--define-key map [rename]
2143 '(menu-item "Rename Bookmark..." bookmark-rename
2144 :help "Change the name of a bookmark"))
2145 (bindings--define-key map [locate]
2146 '(menu-item "Insert Location..." bookmark-locate
2147 :help "Insert the name of the file associated with a bookmark"))
2148 (bindings--define-key map [insert]
2149 '(menu-item "Insert Contents..." bookmark-insert
2150 :help "Insert the text of the file pointed to by a bookmark"))
2151 (bindings--define-key map [set]
2152 '(menu-item "Set Bookmark..." bookmark-set
2153 :help "Set a bookmark named inside a file."))
2154 (bindings--define-key map [jump]
2155 '(menu-item "Jump to Bookmark..." bookmark-jump
2156 :help "Jump to a bookmark (a point in some file)"))
2157 map))
2159 ;;;###autoload
2160 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2162 ;; make bookmarks appear toward the right side of the menu.
2163 (if (boundp 'menu-bar-final-items)
2164 (if menu-bar-final-items
2165 (push 'bookmark menu-bar-final-items))
2166 (setq menu-bar-final-items '(bookmark)))
2168 ;;;; end bookmark menu stuff ;;;;
2171 ;; Load Hook
2172 (defvar bookmark-load-hook nil
2173 "Hook run at the end of loading library `bookmark.el'.")
2175 ;; Exit Hook, called from kill-emacs-hook
2176 (define-obsolete-variable-alias 'bookmark-exit-hooks
2177 'bookmark-exit-hook "22.1")
2178 (defvar bookmark-exit-hook nil
2179 "Hook run when Emacs exits.")
2181 (defun bookmark-exit-hook-internal ()
2182 "Save bookmark state, if necessary, at Emacs exit time.
2183 This also runs `bookmark-exit-hook'."
2184 (run-hooks 'bookmark-exit-hook)
2185 (and (bookmark-time-to-save-p t)
2186 (bookmark-save)))
2188 (unless noninteractive
2189 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2191 (defun bookmark-unload-function ()
2192 "Unload the Bookmark library."
2193 (when bookmark-save-flag (bookmark-save))
2194 ;; continue standard unloading
2195 nil)
2198 (run-hooks 'bookmark-load-hook)
2200 (provide 'bookmark)
2202 ;;; bookmark.el ends here