Document `enable-dir-local-variables'.
[emacs.git] / lisp / bookmark.el
blobce0d6831a3a81e7495e8ffc9ee44e8d60ac4ca5d
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 default name if the function does not provide one.
485 (unless (stringp (car record))
486 (if (car record) (push nil record))
487 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name))))
488 ;; Set up defaults.
489 (bookmark-prop-set
490 record 'defaults
491 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
492 (list bookmark-current-bookmark
493 (car record)
494 (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 map))
756 ;;;###autoload
757 (defun bookmark-set (&optional name no-overwrite)
758 "Set a bookmark named NAME at the current location.
759 If name is nil, then prompt the user.
761 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
762 existing bookmark that has the same name as NAME, but instead push the
763 new bookmark onto the bookmark alist. The most recently set bookmark
764 with name NAME is thus the one in effect at any given time, but the
765 others are still there, should the user decide to delete the most
766 recent one.
768 To yank words from the text of the buffer and use them as part of the
769 bookmark name, type C-w while setting a bookmark. Successive C-w's
770 yank successive words.
772 Typing C-u inserts (at the bookmark name prompt) the name of the last
773 bookmark used in the document where the new bookmark is being set;
774 this helps you use a single bookmark name to track progress through a
775 large document. If there is no prior bookmark for this document, then
776 C-u inserts an appropriate name based on the buffer or file.
778 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
779 it removes only the first instance of a bookmark with that name from
780 the list of bookmarks.)"
781 (interactive (list nil current-prefix-arg))
782 (unwind-protect
783 (let* ((record (bookmark-make-record))
784 ;; `defaults' is a transient element of the
785 ;; extensible format described above in the section
786 ;; `File format stuff'. Bookmark record functions
787 ;; can use it to specify a list of default values
788 ;; accessible via M-n while reading a bookmark name.
789 (defaults (bookmark-prop-get record 'defaults))
790 (default (if (consp defaults) (car defaults) defaults)))
792 (if defaults
793 ;; Don't store default values in the record.
794 (setq record (assq-delete-all 'defaults record))
795 ;; When no defaults in the record, use its first element.
796 (setq defaults (car record) default defaults))
798 (bookmark-maybe-load-default-file)
799 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
800 ;; if they have been already set in another buffer. (e.g gnus-art).
801 (unless (and bookmark-yank-point
802 bookmark-current-buffer)
803 (setq bookmark-yank-point (point))
804 (setq bookmark-current-buffer (current-buffer)))
806 (let ((str
807 (or name
808 (read-from-minibuffer
809 (format "Set bookmark (%s): " default)
811 bookmark-minibuffer-read-name-map
812 nil nil defaults))))
813 (and (string-equal str "") (setq str default))
814 (bookmark-store str (cdr record) no-overwrite)
816 ;; Ask for an annotation buffer for this bookmark
817 (when bookmark-use-annotations
818 (bookmark-edit-annotation str))))
819 (setq bookmark-yank-point nil)
820 (setq bookmark-current-buffer nil)))
823 (defun bookmark-kill-line (&optional newline-too)
824 "Kill from point to end of line.
825 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
826 Does not affect the kill ring."
827 (let ((eol (line-end-position)))
828 (delete-region (point) eol)
829 (if (and newline-too (looking-at "\n"))
830 (delete-char 1))))
833 ;; Defvars to avoid compilation warnings:
834 (defvar bookmark-annotation-name nil
835 "Variable holding the name of the bookmark.
836 This is used in `bookmark-edit-annotation' to record the bookmark
837 whose annotation is being edited.")
840 (defun bookmark-default-annotation-text (bookmark-name)
841 "Return default annotation text for BOOKMARK-NAME.
842 The default annotation text is simply some text explaining how to use
843 annotations."
844 (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
845 "# All lines which start with a '#' will be deleted.\n"
846 "# Type C-c C-c when done.\n#\n"
847 "# Author: " (user-full-name) " <" (user-login-name) "@"
848 (system-name) ">\n"
849 "# Date: " (current-time-string) "\n"))
852 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
853 'bookmark-edit-annotation-text-func "23.1")
854 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
855 "Function to return default text to use for a bookmark annotation.
856 It takes one argument, the name of the bookmark, as a string.")
858 (defvar bookmark-edit-annotation-mode-map
859 (let ((map (make-sparse-keymap)))
860 (set-keymap-parent map text-mode-map)
861 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
862 map)
863 "Keymap for editing an annotation of a bookmark.")
865 (defun bookmark-insert-annotation (bookmark-name-or-record)
866 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
867 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
868 (if (and annotation (not (string-equal annotation "")))
869 (insert annotation))))
871 (define-derived-mode bookmark-edit-annotation-mode
872 text-mode "Edit Bookmark Annotation"
873 "Mode for editing the annotation of bookmarks.
874 When you have finished composing, type \\[bookmark-send-annotation].
876 \\{bookmark-edit-annotation-mode-map}")
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 (derived-mode-p '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-name bookmark-annotation-name))
893 (bookmark-set-annotation bookmark-name 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-name-or-record)
901 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
902 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
903 (bookmark-insert-annotation bookmark-name-or-record)
904 (bookmark-edit-annotation-mode)
905 (set (make-local-variable 'bookmark-annotation-name)
906 bookmark-name-or-record))
909 (defun bookmark-buffer-name ()
910 "Return the name of the current buffer in a form usable as a bookmark name.
911 If the buffer is associated with a file or directory, use that name."
912 (cond
913 ;; Or are we a file?
914 (buffer-file-name (file-name-nondirectory buffer-file-name))
915 ;; Or are we a directory?
916 ((and (boundp 'dired-directory) dired-directory)
917 (let* ((dirname (if (stringp dired-directory)
918 dired-directory
919 (car dired-directory)))
920 (idx (1- (length dirname))))
921 ;; Strip the trailing slash.
922 (if (= ?/ (aref dirname idx))
923 (file-name-nondirectory (substring dirname 0 idx))
924 ;; Else return the current-buffer
925 (buffer-name (current-buffer)))))
926 ;; If all else fails, use the buffer's name.
928 (buffer-name (current-buffer)))))
931 (defun bookmark-yank-word ()
932 "Get the next word from buffer `bookmark-current-buffer' and append
933 it to the name of the bookmark currently being set, advancing
934 `bookmark-yank-point' by one word."
935 (interactive)
936 (let ((string (with-current-buffer bookmark-current-buffer
937 (goto-char bookmark-yank-point)
938 (buffer-substring-no-properties
939 (point)
940 (progn
941 (forward-word 1)
942 (setq bookmark-yank-point (point)))))))
943 (insert string)))
945 (defun bookmark-buffer-file-name ()
946 "Return the current buffer's file in a way useful for bookmarks."
947 ;; Abbreviate the path, both so it's shorter and so it's more
948 ;; portable. E.g., the user's home dir might be a different
949 ;; path on different machines, but "~/" will still reach it.
950 (abbreviate-file-name
951 (cond
952 (buffer-file-name buffer-file-name)
953 ((and (boundp 'dired-directory) dired-directory)
954 (if (stringp dired-directory)
955 dired-directory
956 (car dired-directory)))
957 (t (error "Buffer not visiting a file or directory")))))
960 (defun bookmark-maybe-load-default-file ()
961 "If bookmarks have not been loaded from the default place, load them."
962 (and (not bookmarks-already-loaded)
963 (null bookmark-alist)
964 (prog2
965 (and
966 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
967 ;; to be renamed.
968 (file-exists-p bookmark-old-default-file)
969 (not (file-exists-p bookmark-default-file))
970 (rename-file bookmark-old-default-file
971 bookmark-default-file))
972 ;; return t so the `and' will continue...
975 (file-readable-p bookmark-default-file)
976 (bookmark-load bookmark-default-file t t)
977 (setq bookmarks-already-loaded t)))
980 (defun bookmark-maybe-sort-alist ()
981 "Return `bookmark-alist' for display.
982 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
983 (if bookmark-sort-flag
984 (sort (copy-alist bookmark-alist)
985 (function
986 (lambda (x y) (string-lessp (car x) (car y)))))
987 bookmark-alist))
990 (defvar bookmark-after-jump-hook nil
991 "Hook run after `bookmark-jump' jumps to a bookmark.
992 Useful for example to unhide text in `outline-mode'.")
994 (defun bookmark--jump-via (bookmark-name-or-record display-function)
995 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
996 current buffer as argument.
998 After calling DISPLAY-FUNCTION, set window point to the point specified
999 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1000 and then show any annotations for this bookmark."
1001 (bookmark-handle-bookmark bookmark-name-or-record)
1002 (save-current-buffer
1003 (funcall display-function (current-buffer)))
1004 (let ((win (get-buffer-window (current-buffer) 0)))
1005 (if win (set-window-point win (point))))
1006 ;; FIXME: we used to only run bookmark-after-jump-hook in
1007 ;; `bookmark-jump' itself, but in none of the other commands.
1008 (run-hooks 'bookmark-after-jump-hook)
1009 (if bookmark-automatically-show-annotations
1010 ;; if there is an annotation for this bookmark,
1011 ;; show it in a buffer.
1012 (bookmark-show-annotation bookmark-name-or-record)))
1015 ;;;###autoload
1016 (defun bookmark-jump (bookmark &optional display-func)
1017 "Jump to bookmark BOOKMARK (a point in some file).
1018 You may have a problem using this function if the value of variable
1019 `bookmark-alist' is nil. If that happens, you need to load in some
1020 bookmarks. See help on function `bookmark-load' for more about
1021 this.
1023 If the file pointed to by BOOKMARK no longer exists, you will be asked
1024 if you wish to give the bookmark a new location, and `bookmark-jump'
1025 will then jump to the new location, as well as recording it in place
1026 of the old one in the permanent bookmark record.
1028 BOOKMARK is usually a bookmark name (a string). It can also be a
1029 bookmark record, but this is usually only done by programmatic callers.
1031 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1032 bookmark. It defaults to `switch-to-buffer'. A typical value for
1033 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1034 (interactive
1035 (list (bookmark-completing-read "Jump to bookmark"
1036 bookmark-current-bookmark)))
1037 (unless bookmark
1038 (error "No bookmark specified"))
1039 (bookmark-maybe-historicize-string bookmark)
1040 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1043 ;;;###autoload
1044 (defun bookmark-jump-other-window (bookmark)
1045 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1046 (interactive
1047 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1048 bookmark-current-bookmark)))
1049 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1052 (defun bookmark-jump-noselect (bookmark)
1053 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1054 The return value has the form (BUFFER . POINT).
1056 Note: this function is deprecated and is present for Emacs 22
1057 compatibility only."
1058 (declare (obsolete bookmark-handle-bookmark "23.1"))
1059 (save-excursion
1060 (bookmark-handle-bookmark bookmark)
1061 (cons (current-buffer) (point))))
1063 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1064 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1065 if it has none. This changes current buffer and point and returns nil,
1066 or signals a `file-error'.
1068 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1069 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1070 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1071 (condition-case err
1072 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1073 'bookmark-default-handler)
1074 (bookmark-get-bookmark bookmark-name-or-record))
1075 (bookmark-error-no-filename ;file-error
1076 ;; We were unable to find the marked file, so ask if user wants to
1077 ;; relocate the bookmark, else remind them to consider deletion.
1078 (when (stringp bookmark-name-or-record)
1079 ;; `bookmark-name-or-record' can be either a bookmark name
1080 ;; (from `bookmark-alist') or a bookmark object. If it's an
1081 ;; object, we assume it's a bookmark used internally by some
1082 ;; other package.
1083 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1084 (when file ;Don't know how to relocate if there's no `file'.
1085 ;; If file is not a dir, directory-file-name just returns file.
1086 (let ((display-name (directory-file-name file)))
1087 (ding)
1088 ;; Dialog boxes can accept a file target, but usually don't
1089 ;; know how to accept a directory target (at least, this
1090 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1091 ;; true on Windows as well). So we suppress file dialogs
1092 ;; when relocating.
1093 (let ((use-dialog-box nil)
1094 (use-file-dialog nil))
1095 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1096 bookmark-name-or-record "\"? "))
1097 (progn
1098 (bookmark-relocate bookmark-name-or-record)
1099 ;; Try again.
1100 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1101 'bookmark-default-handler)
1102 (bookmark-get-bookmark bookmark-name-or-record)))
1103 (message
1104 "Bookmark not relocated; consider removing it (%s)."
1105 bookmark-name-or-record)
1106 (signal (car err) (cdr err))))))))))
1107 ;; Added by db.
1108 (when (stringp bookmark-name-or-record)
1109 (setq bookmark-current-bookmark bookmark-name-or-record))
1110 nil)
1112 (define-error 'bookmark-errors nil)
1113 (define-error 'bookmark-error-no-filename
1114 "Bookmark has no associated file (or directory)" 'bookmark-errors)
1116 (defun bookmark-default-handler (bmk-record)
1117 "Default handler to jump to a particular bookmark location.
1118 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1119 Changes current buffer and point and returns nil, or signals a `file-error'."
1120 (let ((file (bookmark-get-filename bmk-record))
1121 (buf (bookmark-prop-get bmk-record 'buffer))
1122 (forward-str (bookmark-get-front-context-string bmk-record))
1123 (behind-str (bookmark-get-rear-context-string bmk-record))
1124 (place (bookmark-get-position bmk-record)))
1125 (set-buffer
1126 (cond
1127 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1128 (find-file-noselect file))
1129 ;; No file found. See if buffer BUF have been created.
1130 ((and buf (get-buffer buf)))
1131 (t ;; If not, raise error.
1132 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1133 (if place (goto-char place))
1134 ;; Go searching forward first. Then, if forward-str exists and
1135 ;; was found in the file, we can search backward for behind-str.
1136 ;; Rationale is that if text was inserted between the two in the
1137 ;; file, it's better to be put before it so you can read it,
1138 ;; rather than after and remain perhaps unaware of the changes.
1139 (when (and forward-str (search-forward forward-str (point-max) t))
1140 (goto-char (match-beginning 0)))
1141 (when (and behind-str (search-backward behind-str (point-min) t))
1142 (goto-char (match-end 0)))
1143 nil))
1145 ;;;###autoload
1146 (defun bookmark-relocate (bookmark-name)
1147 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1149 This makes an already existing bookmark point to that file, instead of
1150 the one it used to point at. Useful when a file has been renamed
1151 after a bookmark was set in it."
1152 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1153 (bookmark-maybe-historicize-string bookmark-name)
1154 (bookmark-maybe-load-default-file)
1155 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1156 (newloc (abbreviate-file-name
1157 (expand-file-name
1158 (read-file-name
1159 (format "Relocate %s to: " bookmark-name)
1160 (file-name-directory bmrk-filename))))))
1161 (bookmark-set-filename bookmark-name newloc)
1162 (setq bookmark-alist-modification-count
1163 (1+ bookmark-alist-modification-count))
1164 (if (bookmark-time-to-save-p)
1165 (bookmark-save))
1166 (bookmark-bmenu-surreptitiously-rebuild-list)))
1169 ;;;###autoload
1170 (defun bookmark-insert-location (bookmark-name &optional no-history)
1171 "Insert the name of the file associated with BOOKMARK-NAME.
1173 Optional second arg NO-HISTORY means don't record this in the
1174 minibuffer history list `bookmark-history'."
1175 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1176 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1177 (insert (bookmark-location bookmark-name)))
1179 ;;;###autoload
1180 (defalias 'bookmark-locate 'bookmark-insert-location)
1182 (defun bookmark-location (bookmark-name-or-record)
1183 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1184 (bookmark-maybe-load-default-file)
1185 ;; We could call the `handler' and ask for it to construct a description
1186 ;; dynamically: it would open up several new possibilities, but it
1187 ;; would have the major disadvantage of forcing to load each and
1188 ;; every handler when the user calls bookmark-menu.
1189 (or (bookmark-prop-get bookmark-name-or-record 'location)
1190 (bookmark-get-filename bookmark-name-or-record)
1191 "-- Unknown location --"))
1194 ;;;###autoload
1195 (defun bookmark-rename (old-name &optional new-name)
1196 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1197 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1198 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1200 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1201 as an argument. If called with two strings, then no prompting is done.
1202 You must pass at least OLD-NAME when calling from Lisp.
1204 While you are entering the new name, consecutive C-w's insert
1205 consecutive words from the text of the buffer into the new bookmark
1206 name."
1207 (interactive (list (bookmark-completing-read "Old bookmark name")))
1208 (bookmark-maybe-historicize-string old-name)
1209 (bookmark-maybe-load-default-file)
1211 (setq bookmark-yank-point (point))
1212 (setq bookmark-current-buffer (current-buffer))
1213 (let ((final-new-name
1214 (or new-name ; use second arg, if non-nil
1215 (read-from-minibuffer
1216 "New name: "
1218 (let ((now-map (copy-keymap minibuffer-local-map)))
1219 (define-key now-map "\C-w" 'bookmark-yank-word)
1220 now-map)
1222 'bookmark-history))))
1223 (bookmark-set-name old-name final-new-name)
1224 (setq bookmark-current-bookmark final-new-name)
1225 (bookmark-bmenu-surreptitiously-rebuild-list)
1226 (setq bookmark-alist-modification-count
1227 (1+ bookmark-alist-modification-count))
1228 (if (bookmark-time-to-save-p)
1229 (bookmark-save))))
1232 ;;;###autoload
1233 (defun bookmark-insert (bookmark-name)
1234 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1235 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1237 You may have a problem using this function if the value of variable
1238 `bookmark-alist' is nil. If that happens, you need to load in some
1239 bookmarks. See help on function `bookmark-load' for more about
1240 this."
1241 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1242 (bookmark-maybe-historicize-string bookmark-name)
1243 (bookmark-maybe-load-default-file)
1244 (let ((orig-point (point))
1245 (str-to-insert
1246 (save-current-buffer
1247 (bookmark-handle-bookmark bookmark-name)
1248 (buffer-string))))
1249 (insert str-to-insert)
1250 (push-mark)
1251 (goto-char orig-point)))
1254 ;;;###autoload
1255 (defun bookmark-delete (bookmark-name &optional batch)
1256 "Delete BOOKMARK-NAME from the bookmark list.
1258 Removes only the first instance of a bookmark with that name. If
1259 there are one or more other bookmarks with the same name, they will
1260 not be deleted. Defaults to the \"current\" bookmark (that is, the
1261 one most recently used in this file, if any).
1262 Optional second arg BATCH means don't update the bookmark list buffer,
1263 probably because we were called from there."
1264 (interactive
1265 (list (bookmark-completing-read "Delete bookmark"
1266 bookmark-current-bookmark)))
1267 (bookmark-maybe-historicize-string bookmark-name)
1268 (bookmark-maybe-load-default-file)
1269 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1270 (setq bookmark-alist (delq will-go bookmark-alist))
1271 ;; Added by db, nil bookmark-current-bookmark if the last
1272 ;; occurrence has been deleted
1273 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1274 (setq bookmark-current-bookmark nil)))
1275 (unless batch
1276 (bookmark-bmenu-surreptitiously-rebuild-list))
1277 (setq bookmark-alist-modification-count
1278 (1+ bookmark-alist-modification-count))
1279 (when (bookmark-time-to-save-p)
1280 (bookmark-save)))
1283 (defun bookmark-time-to-save-p (&optional final-time)
1284 "Return t if it is time to save bookmarks to disk, nil otherwise.
1285 Optional argument FINAL-TIME means this is being called when Emacs
1286 is being killed, so save even if `bookmark-save-flag' is a number and
1287 is greater than `bookmark-alist-modification-count'."
1288 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1289 (cond (final-time
1290 (and (> bookmark-alist-modification-count 0)
1291 bookmark-save-flag))
1292 ((numberp bookmark-save-flag)
1293 (>= bookmark-alist-modification-count bookmark-save-flag))
1295 nil)))
1298 ;;;###autoload
1299 (defun bookmark-write ()
1300 "Write bookmarks to a file (reading the file name with the minibuffer).
1301 Don't use this in Lisp programs; use `bookmark-save' instead."
1302 (interactive)
1303 (bookmark-maybe-load-default-file)
1304 (bookmark-save t))
1307 ;;;###autoload
1308 (defun bookmark-save (&optional parg file)
1309 "Save currently defined bookmarks.
1310 Saves by default in the file defined by the variable
1311 `bookmark-default-file'. With a prefix arg, save it in file FILE
1312 \(second argument).
1314 If you are calling this from Lisp, the two arguments are PARG and
1315 FILE, and if you just want it to write to the default file, then
1316 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1317 instead. If you pass in one argument, and it is non-nil, then the
1318 user will be interactively queried for a file to save in.
1320 When you want to load in the bookmarks from a file, use
1321 `bookmark-load', \\[bookmark-load]. That function will prompt you
1322 for a file, defaulting to the file defined by variable
1323 `bookmark-default-file'."
1324 (interactive "P")
1325 (bookmark-maybe-load-default-file)
1326 (cond
1327 ((and (null parg) (null file))
1328 ;;whether interactive or not, write to default file
1329 (bookmark-write-file bookmark-default-file))
1330 ((and (null parg) file)
1331 ;;whether interactive or not, write to given file
1332 (bookmark-write-file file))
1333 ((and parg (not file))
1334 ;;have been called interactively w/ prefix arg
1335 (let ((file (read-file-name "File to save bookmarks in: ")))
1336 (bookmark-write-file file)))
1337 (t ; someone called us with prefix-arg *and* a file, so just write to file
1338 (bookmark-write-file file)))
1339 ;; signal that we have synced the bookmark file by setting this to
1340 ;; 0. If there was an error at any point before, it will not get
1341 ;; set, which is what we want.
1342 (setq bookmark-alist-modification-count 0))
1346 (defun bookmark-write-file (file)
1347 "Write `bookmark-alist' to FILE."
1348 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1349 (with-current-buffer (get-buffer-create " *Bookmarks*")
1350 (goto-char (point-min))
1351 (delete-region (point-min) (point-max))
1352 (let ((print-length nil)
1353 (print-level nil)
1354 ;; See bug #12503 for why we bind `print-circle'. Users
1355 ;; can define their own bookmark types, which can result in
1356 ;; arbitrary Lisp objects being stored in bookmark records,
1357 ;; and some users create objects containing circularities.
1358 (print-circle t))
1359 (bookmark-insert-file-format-version-stamp)
1360 (insert "(")
1361 ;; Rather than a single call to `pp' we make one per bookmark.
1362 ;; Apparently `pp' has a poor algorithmic complexity, so this
1363 ;; scales a lot better. bug#4485.
1364 (dolist (i bookmark-alist) (pp i (current-buffer)))
1365 (insert ")")
1366 (let ((version-control
1367 (cond
1368 ((null bookmark-version-control) nil)
1369 ((eq 'never bookmark-version-control) 'never)
1370 ((eq 'nospecial bookmark-version-control) version-control)
1371 (t t))))
1372 (condition-case nil
1373 (write-region (point-min) (point-max) file)
1374 (file-error (message "Can't write %s" file)))
1375 (kill-buffer (current-buffer))
1376 (bookmark-maybe-message
1377 "Saving bookmarks to file %s...done" file)))))
1380 (defun bookmark-import-new-list (new-list)
1381 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1382 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1383 they conflict with existing bookmark names."
1384 (let ((names (bookmark-all-names)))
1385 (dolist (full-record new-list)
1386 (bookmark-maybe-rename full-record names)
1387 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1388 (push (bookmark-name-from-full-record full-record) names))))
1391 (defun bookmark-maybe-rename (full-record names)
1392 "Rename bookmark FULL-RECORD if its current name is already used.
1393 This is a helper for `bookmark-import-new-list'."
1394 (let ((found-name (bookmark-name-from-full-record full-record)))
1395 (if (member found-name names)
1396 ;; We've got a conflict, so generate a new name
1397 (let ((count 2)
1398 (new-name found-name))
1399 (while (member new-name names)
1400 (setq new-name (concat found-name (format "<%d>" count)))
1401 (setq count (1+ count)))
1402 (bookmark-set-name full-record new-name)))))
1405 ;;;###autoload
1406 (defun bookmark-load (file &optional overwrite no-msg)
1407 "Load bookmarks from FILE (which must be in bookmark format).
1408 Appends loaded bookmarks to the front of the list of bookmarks. If
1409 optional second argument OVERWRITE is non-nil, existing bookmarks are
1410 destroyed. Optional third arg NO-MSG means don't display any messages
1411 while loading.
1413 If you load a file that doesn't contain a proper bookmark alist, you
1414 will corrupt Emacs's bookmark list. Generally, you should only load
1415 in files that were created with the bookmark functions in the first
1416 place. Your own personal bookmark file, `~/.emacs.bmk', is
1417 maintained automatically by Emacs; you shouldn't need to load it
1418 explicitly.
1420 If you load a file containing bookmarks with the same names as
1421 bookmarks already present in your Emacs, the new bookmarks will get
1422 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1423 method buffers use to resolve name collisions."
1424 (interactive
1425 (list (read-file-name
1426 (format "Load bookmarks from: (%s) "
1427 bookmark-default-file)
1428 ;;Default might not be used often,
1429 ;;but there's no better default, and
1430 ;;I guess it's better than none at all.
1431 "~/" bookmark-default-file 'confirm)))
1432 (setq file (abbreviate-file-name (expand-file-name file)))
1433 (if (not (file-readable-p file))
1434 (error "Cannot read bookmark file %s" file)
1435 (if (null no-msg)
1436 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1437 (with-current-buffer (let ((enable-local-variables nil))
1438 (find-file-noselect file))
1439 (goto-char (point-min))
1440 (bookmark-maybe-upgrade-file-format)
1441 (let ((blist (bookmark-alist-from-buffer)))
1442 (if (listp blist)
1443 (progn
1444 (if overwrite
1445 (progn
1446 (setq bookmark-alist blist)
1447 (setq bookmark-alist-modification-count 0))
1448 ;; else
1449 (bookmark-import-new-list blist)
1450 (setq bookmark-alist-modification-count
1451 (1+ bookmark-alist-modification-count)))
1452 (if (string-equal
1453 (abbreviate-file-name
1454 (expand-file-name bookmark-default-file))
1455 file)
1456 (setq bookmarks-already-loaded t))
1457 (bookmark-bmenu-surreptitiously-rebuild-list))
1458 (error "Invalid bookmark list in %s" file)))
1459 (kill-buffer (current-buffer)))
1460 (if (null no-msg)
1461 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1465 ;;; Code supporting the dired-like bookmark menu.
1466 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1469 (defvar bookmark-bmenu-hidden-bookmarks ())
1472 (defvar bookmark-bmenu-mode-map
1473 (let ((map (make-keymap)))
1474 (set-keymap-parent map special-mode-map)
1475 (define-key map "v" 'bookmark-bmenu-select)
1476 (define-key map "w" 'bookmark-bmenu-locate)
1477 (define-key map "2" 'bookmark-bmenu-2-window)
1478 (define-key map "1" 'bookmark-bmenu-1-window)
1479 (define-key map "j" 'bookmark-bmenu-this-window)
1480 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1481 (define-key map "f" 'bookmark-bmenu-this-window)
1482 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1483 (define-key map "o" 'bookmark-bmenu-other-window)
1484 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1485 (define-key map "s" 'bookmark-bmenu-save)
1486 (define-key map "k" 'bookmark-bmenu-delete)
1487 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1488 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1489 (define-key map "d" 'bookmark-bmenu-delete)
1490 (define-key map " " 'next-line)
1491 (define-key map "n" 'next-line)
1492 (define-key map "p" 'previous-line)
1493 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1494 (define-key map "u" 'bookmark-bmenu-unmark)
1495 (define-key map "m" 'bookmark-bmenu-mark)
1496 (define-key map "l" 'bookmark-bmenu-load)
1497 (define-key map "r" 'bookmark-bmenu-rename)
1498 (define-key map "R" 'bookmark-bmenu-relocate)
1499 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1500 (define-key map "a" 'bookmark-bmenu-show-annotation)
1501 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1502 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1503 (define-key map "/" 'bookmark-bmenu-search)
1504 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1505 map))
1507 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1508 ;; data.
1509 (put 'bookmark-bmenu-mode 'mode-class 'special)
1512 ;; todo: need to display whether or not bookmark exists as a buffer in
1513 ;; flag column.
1515 ;; Format:
1516 ;; FLAGS BOOKMARK [ LOCATION ]
1519 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1520 "Rebuild the Bookmark List if it exists.
1521 Don't affect the buffer ring order."
1522 (if (get-buffer "*Bookmark List*")
1523 (save-excursion
1524 (save-window-excursion
1525 (bookmark-bmenu-list)))))
1528 ;;;###autoload
1529 (defun bookmark-bmenu-list ()
1530 "Display a list of existing bookmarks.
1531 The list is displayed in a buffer named `*Bookmark List*'.
1532 The leftmost column displays a D if the bookmark is flagged for
1533 deletion, or > if it is flagged for displaying."
1534 (interactive)
1535 (bookmark-maybe-load-default-file)
1536 (let ((buf (get-buffer-create "*Bookmark List*")))
1537 (if (called-interactively-p 'interactive)
1538 (switch-to-buffer buf)
1539 (set-buffer buf)))
1540 (let ((inhibit-read-only t))
1541 (erase-buffer)
1542 (if (not bookmark-bmenu-use-header-line)
1543 (insert "% Bookmark\n- --------\n"))
1544 (add-text-properties (point-min) (point)
1545 '(font-lock-face bookmark-menu-heading))
1546 (dolist (full-record (bookmark-maybe-sort-alist))
1547 (let ((name (bookmark-name-from-full-record full-record))
1548 (annotation (bookmark-get-annotation full-record))
1549 (start (point))
1550 end)
1551 ;; if a bookmark has an annotation, prepend a "*"
1552 ;; in the list of bookmarks.
1553 (insert (if (and annotation (not (string-equal annotation "")))
1554 " *" " ")
1555 name)
1556 (setq end (point))
1557 (put-text-property
1558 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1559 (when (display-mouse-p)
1560 (add-text-properties
1561 (+ bookmark-bmenu-marks-width start) end
1562 '(font-lock-face bookmark-menu-bookmark
1563 mouse-face highlight
1564 follow-link t
1565 help-echo "mouse-2: go to this bookmark in other window")))
1566 (insert "\n")))
1567 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1568 (goto-char (point-min))
1569 (bookmark-bmenu-mode)
1570 (if bookmark-bmenu-use-header-line
1571 (bookmark-bmenu-set-header)
1572 (forward-line bookmark-bmenu-inline-header-height))
1573 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1574 (bookmark-bmenu-toggle-filenames t))))
1576 ;;;###autoload
1577 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1578 ;;;###autoload
1579 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1581 (defun bookmark-bmenu-set-header ()
1582 "Sets the immutable header line."
1583 (let ((header (concat "%% " "Bookmark")))
1584 (when bookmark-bmenu-toggle-filenames
1585 (setq header (concat header
1586 (make-string (- bookmark-bmenu-file-column
1587 (- (length header) 3)) ?\s)
1588 "File")))
1589 (let ((pos 0))
1590 (while (string-match "[ \t\n]+" header pos)
1591 (setq pos (match-end 0))
1592 (put-text-property (match-beginning 0) pos 'display
1593 (list 'space :align-to (- pos 1))
1594 header)))
1595 (put-text-property 0 2 'face 'fixed-pitch header)
1596 (setq header (concat (propertize " " 'display '(space :align-to 0))
1597 header))
1598 ;; Code derived from `buff-menu.el'.
1599 (setq header-line-format header)))
1601 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1602 "Major mode for editing a list of bookmarks.
1603 Each line describes one of the bookmarks in Emacs.
1604 Letters do not insert themselves; instead, they are commands.
1605 Bookmark names preceded by a \"*\" have annotations.
1606 \\<bookmark-bmenu-mode-map>
1607 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1608 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1609 Also show bookmarks marked using m in other windows.
1610 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1611 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1612 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1613 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1614 together with bookmark selected before this one in another window.
1615 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1616 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1617 so the bookmark menu bookmark remains visible in its window.
1618 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1619 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1620 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1621 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1622 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1623 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1624 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1625 With a prefix arg, prompts for a file to save in.
1626 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1627 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1628 With prefix argument, also move up one line.
1629 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1630 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1631 in another buffer.
1632 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1633 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1634 (setq truncate-lines t)
1635 (setq buffer-read-only t))
1638 (defun bookmark-bmenu-toggle-filenames (&optional show)
1639 "Toggle whether filenames are shown in the bookmark list.
1640 Optional argument SHOW means show them unconditionally."
1641 (interactive)
1642 (cond
1643 (show
1644 (setq bookmark-bmenu-toggle-filenames nil)
1645 (bookmark-bmenu-show-filenames)
1646 (setq bookmark-bmenu-toggle-filenames t))
1647 (bookmark-bmenu-toggle-filenames
1648 (bookmark-bmenu-hide-filenames)
1649 (setq bookmark-bmenu-toggle-filenames nil))
1651 (bookmark-bmenu-show-filenames)
1652 (setq bookmark-bmenu-toggle-filenames t)))
1653 (when bookmark-bmenu-use-header-line
1654 (bookmark-bmenu-set-header)))
1657 (defun bookmark-bmenu-show-filenames (&optional force)
1658 "In an interactive bookmark list, show filenames along with bookmarks.
1659 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1660 mainly for debugging, and should not be necessary in normal use."
1661 (if (and (not force) bookmark-bmenu-toggle-filenames)
1662 nil ;already shown, so do nothing
1663 (with-buffer-modified-unmodified
1664 (save-excursion
1665 (save-window-excursion
1666 (goto-char (point-min))
1667 (if (not bookmark-bmenu-use-header-line)
1668 (forward-line bookmark-bmenu-inline-header-height))
1669 (setq bookmark-bmenu-hidden-bookmarks ())
1670 (let ((inhibit-read-only t))
1671 (while (< (point) (point-max))
1672 (let ((bmrk (bookmark-bmenu-bookmark)))
1673 (push bmrk bookmark-bmenu-hidden-bookmarks)
1674 (let ((start (line-end-position)))
1675 (move-to-column bookmark-bmenu-file-column t)
1676 ;; Strip off `mouse-face' from the white spaces region.
1677 (if (display-mouse-p)
1678 (remove-text-properties start (point)
1679 '(mouse-face nil help-echo nil))))
1680 (delete-region (point) (progn (end-of-line) (point)))
1681 (insert " ")
1682 ;; Pass the NO-HISTORY arg:
1683 (bookmark-insert-location bmrk t)
1684 (forward-line 1)))))))))
1687 (defun bookmark-bmenu-hide-filenames (&optional force)
1688 "In an interactive bookmark list, hide the filenames of the bookmarks.
1689 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1690 mainly for debugging, and should not be necessary in normal use."
1691 (when (and (not force) bookmark-bmenu-toggle-filenames)
1692 ;; nothing to hide if above is nil
1693 (with-buffer-modified-unmodified
1694 (save-excursion
1695 (goto-char (point-min))
1696 (if (not bookmark-bmenu-use-header-line)
1697 (forward-line bookmark-bmenu-inline-header-height))
1698 (setq bookmark-bmenu-hidden-bookmarks
1699 (nreverse bookmark-bmenu-hidden-bookmarks))
1700 (let ((inhibit-read-only t))
1701 (while bookmark-bmenu-hidden-bookmarks
1702 (move-to-column bookmark-bmenu-marks-width t)
1703 (bookmark-kill-line)
1704 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1705 (start (point)))
1706 (insert name)
1707 (put-text-property start (point) 'bookmark-name-prop name)
1708 (if (display-mouse-p)
1709 (add-text-properties
1710 start (point)
1711 '(font-lock-face bookmark-menu-bookmark
1712 mouse-face highlight
1713 follow-link t help-echo
1714 "mouse-2: go to this bookmark in other window"))))
1715 (forward-line 1)))))))
1718 (defun bookmark-bmenu-ensure-position ()
1719 "If point is not on a bookmark line, move it to one.
1720 If before the first bookmark line, move to the first; if after the
1721 last full line, move to the last full line. The return value is undefined."
1722 (cond ((and (not bookmark-bmenu-use-header-line)
1723 (< (count-lines (point-min) (point))
1724 bookmark-bmenu-inline-header-height))
1725 (goto-char (point-min))
1726 (forward-line bookmark-bmenu-inline-header-height))
1727 ((and (bolp) (eobp))
1728 (beginning-of-line 0))))
1731 (defun bookmark-bmenu-bookmark ()
1732 "Return the bookmark for this line in an interactive bookmark list buffer."
1733 (bookmark-bmenu-ensure-position)
1734 (save-excursion
1735 (beginning-of-line)
1736 (forward-char bookmark-bmenu-marks-width)
1737 (get-text-property (point) 'bookmark-name-prop)))
1740 (defun bookmark-show-annotation (bookmark-name-or-record)
1741 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1742 if an annotation exists."
1743 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1744 (when (and annotation (not (string-equal annotation "")))
1745 (save-excursion
1746 (let ((old-buf (current-buffer)))
1747 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1748 (delete-region (point-min) (point-max))
1749 (insert annotation)
1750 (goto-char (point-min))
1751 (switch-to-buffer-other-window old-buf))))))
1754 (defun bookmark-show-all-annotations ()
1755 "Display the annotations for all bookmarks in a buffer."
1756 (save-selected-window
1757 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1758 (delete-region (point-min) (point-max))
1759 (dolist (full-record bookmark-alist)
1760 (let* ((name (bookmark-name-from-full-record full-record))
1761 (ann (bookmark-get-annotation full-record)))
1762 (insert (concat name ":\n"))
1763 (if (and ann (not (string-equal ann "")))
1764 ;; insert the annotation, indented by 4 spaces.
1765 (progn
1766 (save-excursion (insert ann) (unless (bolp)
1767 (insert "\n")))
1768 (while (< (point) (point-max))
1769 (beginning-of-line) ; paranoia
1770 (insert " ")
1771 (forward-line)
1772 (end-of-line))))))
1773 (goto-char (point-min))))
1776 (defun bookmark-bmenu-mark ()
1777 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1778 (interactive)
1779 (beginning-of-line)
1780 (bookmark-bmenu-ensure-position)
1781 (with-buffer-modified-unmodified
1782 (let ((inhibit-read-only t))
1783 (delete-char 1)
1784 (insert ?>)
1785 (forward-line 1)
1786 (bookmark-bmenu-ensure-position))))
1789 (defun bookmark-bmenu-select ()
1790 "Select this line's bookmark; also display bookmarks marked with `>'.
1791 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1792 (interactive)
1793 (let ((bmrk (bookmark-bmenu-bookmark))
1794 (menu (current-buffer))
1795 (others ())
1796 tem)
1797 (goto-char (point-min))
1798 (while (re-search-forward "^>" nil t)
1799 (setq tem (bookmark-bmenu-bookmark))
1800 (let ((inhibit-read-only t))
1801 (delete-char -1)
1802 (insert ?\s))
1803 (or (string-equal tem bmrk)
1804 (member tem others)
1805 (setq others (cons tem others))))
1806 (setq others (nreverse others)
1807 tem (/ (1- (frame-height)) (1+ (length others))))
1808 (delete-other-windows)
1809 (bookmark-jump bmrk)
1810 (bury-buffer menu)
1811 (if others
1812 (while others
1813 (split-window nil tem)
1814 (other-window 1)
1815 (bookmark-jump (car others))
1816 (setq others (cdr others)))
1817 (other-window 1))))
1820 (defun bookmark-bmenu-any-marks ()
1821 "Return non-nil if any bookmarks are marked in the marks column."
1822 (save-excursion
1823 (goto-char (point-min))
1824 (bookmark-bmenu-ensure-position)
1825 (catch 'found-mark
1826 (while (not (eobp))
1827 (beginning-of-line)
1828 (if (looking-at "^\\S-")
1829 (throw 'found-mark t)
1830 (forward-line 1)))
1831 nil)))
1834 (defun bookmark-bmenu-save (parg)
1835 "Save the current list into a bookmark file.
1836 With a prefix arg, prompts for a file to save them in."
1837 (interactive "P")
1838 (save-excursion
1839 (save-window-excursion
1840 (bookmark-save parg)
1841 (set-buffer-modified-p nil))))
1844 (defun bookmark-bmenu-load ()
1845 "Load the bookmark file and rebuild the bookmark menu-buffer."
1846 (interactive)
1847 (bookmark-bmenu-ensure-position)
1848 (save-excursion
1849 (save-window-excursion
1850 ;; This will call `bookmark-bmenu-list'
1851 (call-interactively 'bookmark-load))))
1854 (defun bookmark-bmenu-1-window ()
1855 "Select this line's bookmark, alone, in full frame."
1856 (interactive)
1857 (bookmark-jump (bookmark-bmenu-bookmark))
1858 (bury-buffer (other-buffer))
1859 (delete-other-windows))
1862 (defun bookmark-bmenu-2-window ()
1863 "Select this line's bookmark, with previous buffer in second window."
1864 (interactive)
1865 (let ((bmrk (bookmark-bmenu-bookmark))
1866 (menu (current-buffer))
1867 (pop-up-windows t))
1868 (delete-other-windows)
1869 (switch-to-buffer (other-buffer) nil t)
1870 (bookmark--jump-via bmrk 'pop-to-buffer)
1871 (bury-buffer menu)))
1874 (defun bookmark-bmenu-this-window ()
1875 "Select this line's bookmark in this window."
1876 (interactive)
1877 (bookmark-jump (bookmark-bmenu-bookmark)))
1880 (defun bookmark-bmenu-other-window ()
1881 "Select this line's bookmark in other window, leaving bookmark menu visible."
1882 (interactive)
1883 (let ((bookmark (bookmark-bmenu-bookmark)))
1884 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1887 (defun bookmark-bmenu-switch-other-window ()
1888 "Make the other window select this line's bookmark.
1889 The current window remains selected."
1890 (interactive)
1891 (let ((bookmark (bookmark-bmenu-bookmark))
1892 (fun (lambda (b) (display-buffer b t))))
1893 (bookmark--jump-via bookmark fun)))
1895 (defun bookmark-bmenu-other-window-with-mouse (event)
1896 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1897 (interactive "e")
1898 (with-current-buffer (window-buffer (posn-window (event-end event)))
1899 (save-excursion
1900 (goto-char (posn-point (event-end event)))
1901 (bookmark-bmenu-other-window))))
1904 (defun bookmark-bmenu-show-annotation ()
1905 "Show the annotation for the current bookmark in another window."
1906 (interactive)
1907 (let ((bookmark (bookmark-bmenu-bookmark)))
1908 (bookmark-show-annotation bookmark)))
1911 (defun bookmark-bmenu-show-all-annotations ()
1912 "Show the annotation for all bookmarks in another window."
1913 (interactive)
1914 (bookmark-show-all-annotations))
1917 (defun bookmark-bmenu-edit-annotation ()
1918 "Edit the annotation for the current bookmark in another window."
1919 (interactive)
1920 (let ((bookmark (bookmark-bmenu-bookmark)))
1921 (bookmark-edit-annotation bookmark)))
1924 (defun bookmark-bmenu-unmark (&optional backup)
1925 "Cancel all requested operations on bookmark on this line and move down.
1926 Optional BACKUP means move up."
1927 (interactive "P")
1928 (beginning-of-line)
1929 (bookmark-bmenu-ensure-position)
1930 (with-buffer-modified-unmodified
1931 (let ((inhibit-read-only t))
1932 (delete-char 1)
1933 ;; any flags to reset according to circumstances? How about a
1934 ;; flag indicating whether this bookmark is being visited?
1935 ;; well, we don't have this now, so maybe later.
1936 (insert " "))
1937 (forward-line (if backup -1 1))
1938 (bookmark-bmenu-ensure-position)))
1941 (defun bookmark-bmenu-backup-unmark ()
1942 "Move up and cancel all requested operations on bookmark on line above."
1943 (interactive)
1944 (forward-line -1)
1945 (bookmark-bmenu-ensure-position)
1946 (bookmark-bmenu-unmark)
1947 (forward-line -1)
1948 (bookmark-bmenu-ensure-position))
1951 (defun bookmark-bmenu-delete ()
1952 "Mark bookmark on this line to be deleted.
1953 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1954 (interactive)
1955 (beginning-of-line)
1956 (bookmark-bmenu-ensure-position)
1957 (with-buffer-modified-unmodified
1958 (let ((inhibit-read-only t))
1959 (delete-char 1)
1960 (insert ?D)
1961 (forward-line 1)
1962 (bookmark-bmenu-ensure-position))))
1965 (defun bookmark-bmenu-delete-backwards ()
1966 "Mark bookmark on this line to be deleted, then move up one line.
1967 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1968 (interactive)
1969 (bookmark-bmenu-delete)
1970 (forward-line -2)
1971 (bookmark-bmenu-ensure-position)
1972 (forward-line 1)
1973 (bookmark-bmenu-ensure-position))
1976 (defun bookmark-bmenu-execute-deletions ()
1977 "Delete bookmarks flagged `D'."
1978 (interactive)
1979 (message "Deleting bookmarks...")
1980 (let ((o-point (point))
1981 (o-str (save-excursion
1982 (beginning-of-line)
1983 (unless (looking-at "^D")
1984 (buffer-substring
1985 (point)
1986 (progn (end-of-line) (point))))))
1987 (o-col (current-column)))
1988 (goto-char (point-min))
1989 (unless bookmark-bmenu-use-header-line
1990 (forward-line 1))
1991 (while (re-search-forward "^D" (point-max) t)
1992 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1993 (bookmark-bmenu-list)
1994 (if o-str
1995 (progn
1996 (goto-char (point-min))
1997 (search-forward o-str)
1998 (beginning-of-line)
1999 (forward-char o-col))
2000 (goto-char o-point))
2001 (beginning-of-line)
2002 (message "Deleting bookmarks...done")
2006 (defun bookmark-bmenu-rename ()
2007 "Rename bookmark on current line. Prompts for a new name."
2008 (interactive)
2009 (let ((bmrk (bookmark-bmenu-bookmark))
2010 (thispoint (point)))
2011 (bookmark-rename bmrk)
2012 (goto-char thispoint)))
2015 (defun bookmark-bmenu-locate ()
2016 "Display location of this bookmark. Displays in the minibuffer."
2017 (interactive)
2018 (let ((bmrk (bookmark-bmenu-bookmark)))
2019 (message "%s" (bookmark-location bmrk))))
2021 (defun bookmark-bmenu-relocate ()
2022 "Change the file path of the bookmark on the current line,
2023 prompting with completion for the new path."
2024 (interactive)
2025 (let ((bmrk (bookmark-bmenu-bookmark))
2026 (thispoint (point)))
2027 (bookmark-relocate bmrk)
2028 (goto-char thispoint)))
2030 ;;; Bookmark-bmenu search
2032 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2033 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2034 (let ((bookmark-alist
2035 (cl-loop for i in bookmark-alist
2036 when (string-match regexp (car i)) collect i into new
2037 finally return new)))
2038 (bookmark-bmenu-list)))
2041 ;;;###autoload
2042 (defun bookmark-bmenu-search ()
2043 "Incremental search of bookmarks, hiding the non-matches as we go."
2044 (interactive)
2045 (let ((bmk (bookmark-bmenu-bookmark))
2046 (timer nil))
2047 (unwind-protect
2048 (minibuffer-with-setup-hook
2049 (lambda ()
2050 (setq timer (run-with-idle-timer
2051 bookmark-search-delay 'repeat
2052 #'(lambda (buf)
2053 (with-current-buffer buf
2054 (bookmark-bmenu-filter-alist-by-regexp
2055 (minibuffer-contents))))
2056 (current-buffer))))
2057 (read-string "Pattern: ")
2058 (when timer (cancel-timer timer) (setq timer nil)))
2059 (when timer ;; Signalled an error or a `quit'.
2060 (cancel-timer timer)
2061 (bookmark-bmenu-list)
2062 (bookmark-bmenu-goto-bookmark bmk)))))
2064 (defun bookmark-bmenu-goto-bookmark (name)
2065 "Move point to bookmark with name NAME."
2066 (goto-char (point-min))
2067 (while (not (equal name (bookmark-bmenu-bookmark)))
2068 (forward-line 1))
2069 (forward-line 0))
2073 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2075 (defun bookmark-menu-popup-paned-menu (event name entries)
2076 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2077 That is, ENTRIES is a list of strings which appear as the choices
2078 in the menu.
2079 The number of panes depends on the number of entries.
2080 The visible entries are truncated to `bookmark-menu-length', but the
2081 strings returned are not."
2082 (let ((f-height (/ (frame-height) 2))
2083 (pane-list nil)
2084 (iter 0))
2085 (while entries
2086 (let (lst
2087 (count 0))
2088 (while (and (< count f-height) entries)
2089 (let ((str (car entries)))
2090 (push (cons
2091 (if (> (length str) bookmark-menu-length)
2092 (substring str 0 bookmark-menu-length)
2093 str)
2094 str)
2095 lst)
2096 (setq entries (cdr entries))
2097 (setq count (1+ count))))
2098 (setq iter (1+ iter))
2099 (push (cons
2100 (format "-*- %s (%d) -*-" name iter)
2101 (nreverse lst))
2102 pane-list)))
2104 ;; Popup the menu and return the string.
2105 (x-popup-menu event (cons (concat "-*- " name " -*-")
2106 (nreverse pane-list)))))
2109 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2110 ;; following works, and for explaining what to do to make it work.
2112 ;; We MUST autoload EACH form used to set up this variable's value, so
2113 ;; that the whole job is done in loaddefs.el.
2115 ;; Emacs menubar stuff.
2117 ;;;###autoload
2118 (defvar menu-bar-bookmark-map
2119 (let ((map (make-sparse-keymap "Bookmark functions")))
2120 (bindings--define-key map [load]
2121 '(menu-item "Load a Bookmark File..." bookmark-load
2122 :help "Load bookmarks from a bookmark file)"))
2123 (bindings--define-key map [write]
2124 '(menu-item "Save Bookmarks As..." bookmark-write
2125 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2126 (bindings--define-key map [save]
2127 '(menu-item "Save Bookmarks" bookmark-save
2128 :help "Save currently defined bookmarks"))
2129 (bindings--define-key map [edit]
2130 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2131 :help "Display a list of existing bookmarks"))
2132 (bindings--define-key map [delete]
2133 '(menu-item "Delete Bookmark..." bookmark-delete
2134 :help "Delete a bookmark from the bookmark list"))
2135 (bindings--define-key map [rename]
2136 '(menu-item "Rename Bookmark..." bookmark-rename
2137 :help "Change the name of a bookmark"))
2138 (bindings--define-key map [locate]
2139 '(menu-item "Insert Location..." bookmark-locate
2140 :help "Insert the name of the file associated with a bookmark"))
2141 (bindings--define-key map [insert]
2142 '(menu-item "Insert Contents..." bookmark-insert
2143 :help "Insert the text of the file pointed to by a bookmark"))
2144 (bindings--define-key map [set]
2145 '(menu-item "Set Bookmark..." bookmark-set
2146 :help "Set a bookmark named inside a file."))
2147 (bindings--define-key map [jump]
2148 '(menu-item "Jump to Bookmark..." bookmark-jump
2149 :help "Jump to a bookmark (a point in some file)"))
2150 map))
2152 ;;;###autoload
2153 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2155 ;; make bookmarks appear toward the right side of the menu.
2156 (if (boundp 'menu-bar-final-items)
2157 (if menu-bar-final-items
2158 (push 'bookmark menu-bar-final-items))
2159 (setq menu-bar-final-items '(bookmark)))
2161 ;;;; end bookmark menu stuff ;;;;
2164 ;; Load Hook
2165 (defvar bookmark-load-hook nil
2166 "Hook run at the end of loading library `bookmark.el'.")
2168 ;; Exit Hook, called from kill-emacs-hook
2169 (define-obsolete-variable-alias 'bookmark-exit-hooks
2170 'bookmark-exit-hook "22.1")
2171 (defvar bookmark-exit-hook nil
2172 "Hook run when Emacs exits.")
2174 (defun bookmark-exit-hook-internal ()
2175 "Save bookmark state, if necessary, at Emacs exit time.
2176 This also runs `bookmark-exit-hook'."
2177 (run-hooks 'bookmark-exit-hook)
2178 (and (bookmark-time-to-save-p t)
2179 (bookmark-save)))
2181 (unless noninteractive
2182 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2184 (defun bookmark-unload-function ()
2185 "Unload the Bookmark library."
2186 (when bookmark-save-flag (bookmark-save))
2187 ;; continue standard unloading
2188 nil)
2191 (run-hooks 'bookmark-load-hook)
2193 (provide 'bookmark)
2195 ;;; bookmark.el ends here