* bookmark.el: Display the bookmark list header similarly to the
[emacs.git] / lisp / bookmark.el
blobfd6ae217bf80c13dff5f3ff59d471a088e2c643d
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)
160 (defcustom bookmark-menu-length 70
161 "Maximum length of a bookmark name displayed on a popup menu."
162 :type 'integer
163 :group 'bookmark)
165 ;; FIXME: Is it really worth a customization option?
166 (defcustom bookmark-search-delay 0.2
167 "Time before `bookmark-bmenu-search' updates the display."
168 :group 'bookmark
169 :type 'integer)
171 (defface bookmark-menu-heading
172 '((t (:inherit font-lock-type-face)))
173 "Face used to highlight the heading in bookmark menu buffers."
174 :group 'bookmark
175 :version "22.1")
178 ;;; No user-serviceable parts beyond this point.
180 ;; Added for lucid emacs compatibility, db
181 (or (fboundp 'defalias) (fset 'defalias 'fset))
183 ;; suggested for lucid compatibility by david hughes:
184 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
187 ;;; Keymap stuff:
189 ;; Set up these bindings dumping time *only*;
190 ;; if the user alters them, don't override the user when loading bookmark.el.
192 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
193 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
194 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
196 ;;;###autoload
197 (defvar bookmark-map
198 (let ((map (make-sparse-keymap)))
199 ;; Read the help on all of these functions for details...
200 (define-key map "x" 'bookmark-set)
201 (define-key map "m" 'bookmark-set) ;"m"ark
202 (define-key map "j" 'bookmark-jump)
203 (define-key map "g" 'bookmark-jump) ;"g"o
204 (define-key map "o" 'bookmark-jump-other-window)
205 (define-key map "i" 'bookmark-insert)
206 (define-key map "e" 'edit-bookmarks)
207 (define-key map "f" 'bookmark-insert-location) ;"f"ind
208 (define-key map "r" 'bookmark-rename)
209 (define-key map "d" 'bookmark-delete)
210 (define-key map "l" 'bookmark-load)
211 (define-key map "w" 'bookmark-write)
212 (define-key map "s" 'bookmark-save)
213 map)
214 "Keymap containing bindings to bookmark functions.
215 It is not bound to any key by default: to bind it
216 so that you have a bookmark prefix, just use `global-set-key' and bind a
217 key of your choice to `bookmark-map'. All interactive bookmark
218 functions have a binding in this keymap.")
220 ;;;###autoload (fset 'bookmark-map bookmark-map)
223 ;;; Core variables and data structures:
224 (defvar bookmark-alist ()
225 "Association list of bookmarks and their records.
226 Bookmark functions update the value automatically.
227 You probably do NOT want to change the value yourself.
229 The value is an alist with entries of the form
231 (BOOKMARK-NAME . PARAM-ALIST)
233 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
235 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
237 PARAM-ALIST is an alist of bookmark information. The order of the
238 entries in PARAM-ALIST is not important. The possible entries are
239 described below. An entry with a key but null value means the entry
240 is not used.
242 (filename . FILENAME)
243 (position . POS)
244 (front-context-string . STR-AFTER-POS)
245 (rear-context-string . STR-BEFORE-POS)
246 (handler . HANDLER)
247 (annotation . ANNOTATION)
249 FILENAME names the bookmarked file.
250 POS is the bookmarked buffer position (position in the file).
251 STR-AFTER-POS is buffer text that immediately follows POS.
252 STR-BEFORE-POS is buffer text that immediately precedes POS.
253 ANNOTATION is a string that describes the bookmark.
254 See options `bookmark-use-annotations' and
255 `bookmark-automatically-show-annotations'.
256 HANDLER is a function that provides the bookmark-jump behavior for a
257 specific kind of bookmark. This is the case for Info bookmarks,
258 for instance. HANDLER must accept a bookmark as argument.")
260 (defvar bookmarks-already-loaded nil
261 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
264 ;; more stuff added by db.
266 (defvar bookmark-current-bookmark nil
267 "Name of bookmark most recently used in the current file.
268 It is buffer local, used to make moving a bookmark forward
269 through a file easier.")
271 (make-variable-buffer-local 'bookmark-current-bookmark)
274 (defvar bookmark-alist-modification-count 0
275 "Number of modifications to bookmark list since it was last saved.")
278 (defvar bookmark-search-size 16
279 "Length of the context strings recorded on either side of a bookmark.")
282 (defvar bookmark-current-buffer nil
283 "The buffer in which a bookmark is currently being set or renamed.
284 Functions that insert strings into the minibuffer use this to know
285 the source buffer for that information; see `bookmark-yank-word'
286 for example.")
289 (defvar bookmark-yank-point 0
290 "The next point from which to pull source text for `bookmark-yank-word'.
291 This point is in `bookmark-current-buffer'.")
294 (defvar bookmark-quit-flag nil
295 "Non nil make `bookmark-bmenu-search' quit immediately.")
297 ;; Helper functions and macros.
299 (defmacro with-buffer-modified-unmodified (&rest body)
300 "Run BODY while preserving the buffer's `buffer-modified-p' state."
301 (let ((was-modified (make-symbol "was-modified")))
302 `(let ((,was-modified (buffer-modified-p)))
303 (unwind-protect
304 (progn ,@body)
305 (set-buffer-modified-p ,was-modified)))))
307 ;; Only functions below, in this page and the next one (file formats),
308 ;; need to know anything about the format of bookmark-alist entries.
309 ;; Everyone else should go through them.
311 (defun bookmark-name-from-full-record (bookmark-record)
312 "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
313 one element from `bookmark-alist'."
314 (car bookmark-record))
317 (defun bookmark-all-names ()
318 "Return a list of all current bookmark names."
319 (bookmark-maybe-load-default-file)
320 (mapcar 'bookmark-name-from-full-record bookmark-alist))
323 (defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
324 "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
325 If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
326 bookmark record in `bookmark-alist'; return it if found, otherwise
327 error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
328 just return it."
329 (cond
330 ((consp bookmark-name-or-record) bookmark-name-or-record)
331 ((stringp bookmark-name-or-record)
332 (or (assoc-string bookmark-name-or-record bookmark-alist
333 bookmark-completion-ignore-case)
334 (unless noerror (error "Invalid bookmark %s"
335 bookmark-name-or-record))))))
338 (defun bookmark-get-bookmark-record (bookmark-name-or-record)
339 "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
340 `bookmark-alist' (that is, all information but the name)."
341 (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
342 ;; The bookmark objects can either look like (NAME ALIST) or
343 ;; (NAME . ALIST), so we have to distinguish the two here.
344 (if (and (null (cdr alist)) (consp (caar alist)))
345 (car alist) alist)))
348 (defun bookmark-set-name (bookmark-name-or-record newname)
349 "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
350 (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
352 (defun bookmark-prop-get (bookmark-name-or-record prop)
353 "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
354 (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
356 (defun bookmark-prop-set (bookmark-name-or-record prop val)
357 "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
358 (let ((cell (assq
359 prop (bookmark-get-bookmark-record bookmark-name-or-record))))
360 (if cell
361 (setcdr cell val)
362 (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
363 (list (cons prop val))))))
365 (defun bookmark-get-annotation (bookmark-name-or-record)
366 "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
367 (bookmark-prop-get bookmark-name-or-record 'annotation))
369 (defun bookmark-set-annotation (bookmark-name-or-record ann)
370 "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
371 (bookmark-prop-set bookmark-name-or-record 'annotation ann))
374 (defun bookmark-get-filename (bookmark-name-or-record)
375 "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
376 (bookmark-prop-get bookmark-name-or-record 'filename))
379 (defun bookmark-set-filename (bookmark-name-or-record filename)
380 "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
381 (bookmark-prop-set bookmark-name-or-record 'filename filename))
384 (defun bookmark-get-position (bookmark-name-or-record)
385 "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
386 (bookmark-prop-get bookmark-name-or-record 'position))
389 (defun bookmark-set-position (bookmark-name-or-record position)
390 "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
391 (bookmark-prop-set bookmark-name-or-record 'position position))
394 (defun bookmark-get-front-context-string (bookmark-name-or-record)
395 "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
396 (bookmark-prop-get bookmark-name-or-record 'front-context-string))
399 (defun bookmark-set-front-context-string (bookmark-name-or-record string)
400 "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
401 (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
404 (defun bookmark-get-rear-context-string (bookmark-name-or-record)
405 "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
406 (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
409 (defun bookmark-set-rear-context-string (bookmark-name-or-record string)
410 "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
411 (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
414 (defun bookmark-get-handler (bookmark-name-or-record)
415 "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
416 (bookmark-prop-get bookmark-name-or-record 'handler))
418 (defvar bookmark-history nil
419 "The history list for bookmark functions.")
422 (defun bookmark-completing-read (prompt &optional default)
423 "Prompting with PROMPT, read a bookmark name in completion.
424 PROMPT will get a \": \" stuck on the end no matter what, so you
425 probably don't want to include one yourself.
426 Optional second arg DEFAULT is a string to return if the user enters
427 the empty string."
428 (bookmark-maybe-load-default-file) ; paranoia
429 (if (listp last-nonmenu-event)
430 (bookmark-menu-popup-paned-menu t prompt
431 (if bookmark-sort-flag
432 (sort (bookmark-all-names)
433 'string-lessp)
434 (bookmark-all-names)))
435 (let* ((completion-ignore-case bookmark-completion-ignore-case)
436 (default default)
437 (prompt (concat prompt (if default
438 (format " (%s): " default)
439 ": ")))
440 (str
441 (completing-read prompt
442 (lambda (string pred action)
443 (if (eq action 'metadata)
444 '(metadata (category . bookmark))
445 (complete-with-action
446 action bookmark-alist string pred)))
450 'bookmark-history)))
451 (if (string-equal "" str) default str))))
454 (defmacro bookmark-maybe-historicize-string (string)
455 "Put STRING into the bookmark prompt history, if caller non-interactive.
456 We need this because sometimes bookmark functions are invoked from
457 menus, so `completing-read' never gets a chance to set `bookmark-history'."
458 `(or
459 (called-interactively-p 'interactive)
460 (setq bookmark-history (cons ,string bookmark-history))))
462 (defvar bookmark-make-record-function 'bookmark-make-record-default
463 "A function that should be called to create a bookmark record.
464 Modes may set this variable buffer-locally to enable bookmarking of
465 locations that should be treated specially, such as Info nodes,
466 news posts, images, pdf documents, etc.
468 The function will be called with no arguments.
469 It should signal a user error if it is unable to construct a record for
470 the current location.
472 The returned record should be a cons cell of the form (NAME . ALIST)
473 where ALIST is as described in `bookmark-alist' and may typically contain
474 a special cons (handler . HANDLER-FUNC) which specifies the handler function
475 that should be used instead of `bookmark-default-handler' to open this
476 bookmark. See the documentation for `bookmark-alist' for more.
478 NAME is a suggested name for the constructed bookmark. It can be nil
479 in which case a default heuristic will be used. The function can also
480 equivalently just return ALIST without NAME.")
482 (defun bookmark-make-record ()
483 "Return a new bookmark record (NAME . ALIST) for the current location."
484 (let ((record (funcall bookmark-make-record-function)))
485 ;; Set up defaults.
486 (bookmark-prop-set
487 record 'defaults
488 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
489 (list bookmark-current-bookmark
490 (bookmark-buffer-name))))))
491 ;; Set up default name.
492 (if (stringp (car record))
493 ;; The function already provided a default name.
494 record
495 (if (car record) (push nil record))
496 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
497 record)))
499 (defun bookmark-store (name alist no-overwrite)
500 "Store the bookmark NAME with data ALIST.
501 If NO-OVERWRITE is non-nil and another bookmark of the same name already
502 exists in `bookmark-alist', record the new bookmark without throwing away the
503 old one."
504 (bookmark-maybe-load-default-file)
505 (let ((stripped-name (copy-sequence name)))
506 (or (featurep 'xemacs)
507 ;; XEmacs's `set-text-properties' doesn't work on
508 ;; free-standing strings, apparently.
509 (set-text-properties 0 (length stripped-name) nil stripped-name))
510 (if (and (not no-overwrite)
511 (bookmark-get-bookmark stripped-name 'noerror))
512 ;; already existing bookmark under that name and
513 ;; no prefix arg means just overwrite old bookmark
514 ;; Use the new (NAME . ALIST) format.
515 (setcdr (bookmark-get-bookmark stripped-name) alist)
517 ;; otherwise just cons it onto the front (either the bookmark
518 ;; doesn't exist already, or there is no prefix arg. In either
519 ;; case, we want the new bookmark consed onto the alist...)
521 (push (cons stripped-name alist) bookmark-alist))
523 ;; Added by db
524 (setq bookmark-current-bookmark stripped-name)
525 (setq bookmark-alist-modification-count
526 (1+ bookmark-alist-modification-count))
527 (if (bookmark-time-to-save-p)
528 (bookmark-save))
530 (setq bookmark-current-bookmark stripped-name)
531 (bookmark-bmenu-surreptitiously-rebuild-list)))
533 (defun bookmark-make-record-default (&optional no-file no-context posn)
534 "Return the record describing the location of a new bookmark.
535 Point should be at the buffer in which the bookmark is being set,
536 and normally should be at the position where the bookmark is desired,
537 but see the optional arguments for other possibilities.
539 If NO-FILE is non-nil, then only return the subset of the
540 record that pertains to the location within the buffer, leaving off
541 the part that records the filename.
543 If NO-CONTEXT is non-nil, do not include the front- and rear-context
544 strings in the record -- the position is enough.
546 If POSN is non-nil, record POSN as the point instead of `(point)'."
547 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
548 ,@(unless no-context `((front-context-string
549 . ,(if (>= (- (point-max) (point))
550 bookmark-search-size)
551 (buffer-substring-no-properties
552 (point)
553 (+ (point) bookmark-search-size))
554 nil))))
555 ,@(unless no-context `((rear-context-string
556 . ,(if (>= (- (point) (point-min))
557 bookmark-search-size)
558 (buffer-substring-no-properties
559 (point)
560 (- (point) bookmark-search-size))
561 nil))))
562 (position . ,(or posn (point)))))
565 ;;; File format stuff
567 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
568 ;; the bookmark file format -- please don't. The current format
569 ;; should be extensible enough. If you feel the need to change it,
570 ;; please discuss it with other Emacs developers first.
572 ;; The format of `bookmark-alist' has changed twice in its lifetime.
573 ;; This comment describes the three formats, FIRST, SECOND, and
574 ;; CURRENT.
576 ;; The FIRST format was used prior to Emacs 20:
578 ;; ((BOOKMARK-NAME (FILENAME
579 ;; STRING-IN-FRONT
580 ;; STRING-BEHIND
581 ;; POINT))
582 ;; ...)
584 ;; The SECOND format was introduced in Emacs 20:
586 ;; ((BOOKMARK-NAME ((filename . FILENAME)
587 ;; (position . POS)
588 ;; (front-context-string . STR-AFTER-POS)
589 ;; (rear-context-string . STR-BEFORE-POS)
590 ;; (annotation . ANNOTATION)
591 ;; (whatever . VALUE)
592 ;; ...
593 ;; ))
594 ;; ...)
596 ;; The CURRENT format was introduced in Emacs 22:
598 ;; ((BOOKMARK-NAME (filename . FILENAME)
599 ;; (position . POS)
600 ;; (front-context-string . STR-AFTER-POS)
601 ;; (rear-context-string . STR-BEFORE-POS)
602 ;; (annotation . ANNOTATION)
603 ;; (whatever . VALUE)
604 ;; ...
605 ;; )
606 ;; ...)
608 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
609 ;; bookmark record is a list of entry information. FIRST and SECOND
610 ;; differ in the form of the record information: FIRST uses a list of
611 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
612 ;; the list elements matters. In the SECOND format, the order of the
613 ;; alist elements is unimportant. The SECOND format facilitates the
614 ;; addition of new kinds of elements, to support new kinds of
615 ;; bookmarks or code evolution.
617 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
618 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
619 ;; no longer a cons. Why that change was made remains a mystery --
620 ;; just be aware of it. (Be aware too that this explanatory comment
621 ;; was incorrect in Emacs 22 and Emacs 23.1.)
623 ;; To deal with the change from FIRST format to SECOND, conversion
624 ;; code was added, and it is still in use. See
625 ;; `bookmark-maybe-upgrade-file-format'.
627 ;; No conversion from SECOND to CURRENT is done. Instead, the code
628 ;; handles both formats OK. It must continue to do so.
630 ;; See the doc string of `bookmark-alist' for information about the
631 ;; elements that define a bookmark (e.g. `filename').
634 (defconst bookmark-file-format-version 1
635 "The current version of the format used by bookmark files.
636 You should never need to change this.")
639 (defconst bookmark-end-of-version-stamp-marker
640 "-*- End Of Bookmark File Format Version Stamp -*-\n"
641 "This string marks the end of the version stamp in a bookmark file.")
644 (defun bookmark-alist-from-buffer ()
645 "Return a `bookmark-alist' (in any format) from the current buffer.
646 The buffer must of course contain bookmark format information.
647 Does not care from where in the buffer it is called, and does not
648 affect point."
649 (save-excursion
650 (goto-char (point-min))
651 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
652 (read (current-buffer))
653 ;; Else we're dealing with format version 0
654 (if (search-forward "(" nil t)
655 (progn
656 (forward-char -1)
657 (read (current-buffer)))
658 ;; Else no hope of getting information here.
659 (error "Not bookmark format")))))
662 (defun bookmark-upgrade-version-0-alist (old-list)
663 "Upgrade a version 0 alist OLD-LIST to the current version."
664 (mapcar
665 (lambda (bookmark)
666 (let* ((name (car bookmark))
667 (record (car (cdr bookmark)))
668 (filename (nth 0 record))
669 (front-str (nth 1 record))
670 (rear-str (nth 2 record))
671 (position (nth 3 record))
672 (ann (nth 4 record)))
673 (list
674 name
675 `((filename . ,filename)
676 (front-context-string . ,(or front-str ""))
677 (rear-context-string . ,(or rear-str ""))
678 (position . ,position)
679 (annotation . ,ann)))))
680 old-list))
683 (defun bookmark-upgrade-file-format-from-0 ()
684 "Upgrade a bookmark file of format 0 (the original format) to format 1.
685 This expects to be called from `point-min' in a bookmark file."
686 (message "Upgrading bookmark format from 0 to %d..."
687 bookmark-file-format-version)
688 (let* ((old-list (bookmark-alist-from-buffer))
689 (new-list (bookmark-upgrade-version-0-alist old-list)))
690 (delete-region (point-min) (point-max))
691 (bookmark-insert-file-format-version-stamp)
692 (pp new-list (current-buffer))
693 (save-buffer))
694 (goto-char (point-min))
695 (message "Upgrading bookmark format from 0 to %d...done"
696 bookmark-file-format-version)
700 (defun bookmark-grok-file-format-version ()
701 "Return an integer which is the file-format version of this bookmark file.
702 This expects to be called from `point-min' in a bookmark file."
703 (if (looking-at "^;;;;")
704 (save-excursion
705 (save-match-data
706 (re-search-forward "[0-9]")
707 (forward-char -1)
708 (read (current-buffer))))
709 ;; Else this is format version 0, the original one, which didn't
710 ;; even have version stamps.
714 (defun bookmark-maybe-upgrade-file-format ()
715 "Check the file-format version of this bookmark file.
716 If the version is not up-to-date, upgrade it automatically.
717 This expects to be called from `point-min' in a bookmark file."
718 (let ((version (bookmark-grok-file-format-version)))
719 (cond
720 ((= version bookmark-file-format-version)
721 ) ; home free -- version is current
722 ((= version 0)
723 (bookmark-upgrade-file-format-from-0))
725 (error "Bookmark file format version strangeness")))))
728 (defun bookmark-insert-file-format-version-stamp ()
729 "Insert text indicating current version of bookmark file format."
730 (insert
731 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
732 bookmark-file-format-version))
733 (insert ";;; This format is meant to be slightly human-readable;\n"
734 ";;; nevertheless, you probably don't want to edit it.\n"
735 ";;; "
736 bookmark-end-of-version-stamp-marker))
739 ;;; end file-format stuff
742 ;;; Generic helpers.
744 (defun bookmark-maybe-message (fmt &rest args)
745 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
746 (if (>= baud-rate 9600)
747 (apply 'message fmt args)))
750 ;;; Core code:
752 (defvar bookmark-minibuffer-read-name-map
753 (let ((map (make-sparse-keymap)))
754 (set-keymap-parent map minibuffer-local-map)
755 (define-key map "\C-w" 'bookmark-yank-word)
756 map))
758 ;;;###autoload
759 (defun bookmark-set (&optional name no-overwrite)
760 "Set a bookmark named NAME at the current location.
761 If name is nil, then prompt the user.
763 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
764 existing bookmark that has the same name as NAME, but instead push the
765 new bookmark onto the bookmark alist. The most recently set bookmark
766 with name NAME is thus the one in effect at any given time, but the
767 others are still there, should the user decide to delete the most
768 recent one.
770 To yank words from the text of the buffer and use them as part of the
771 bookmark name, type C-w while setting a bookmark. Successive C-w's
772 yank successive words.
774 Typing C-u inserts (at the bookmark name prompt) the name of the last
775 bookmark used in the document where the new bookmark is being set;
776 this helps you use a single bookmark name to track progress through a
777 large document. If there is no prior bookmark for this document, then
778 C-u inserts an appropriate name based on the buffer or file.
780 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
781 it removes only the first instance of a bookmark with that name from
782 the list of bookmarks.)"
783 (interactive (list nil current-prefix-arg))
784 (unwind-protect
785 (let* ((record (bookmark-make-record))
786 ;; `defaults' is a transient element of the
787 ;; extensible format described above in the section
788 ;; `File format stuff'. Bookmark record functions
789 ;; can use it to specify a list of default values
790 ;; accessible via M-n while reading a bookmark name.
791 (defaults (bookmark-prop-get record 'defaults))
792 (default (if (consp defaults) (car defaults) defaults)))
794 (if defaults
795 ;; Don't store default values in the record.
796 (setq record (assq-delete-all 'defaults record))
797 ;; When no defaults in the record, use its first element.
798 (setq defaults (car record) default defaults))
800 (bookmark-maybe-load-default-file)
801 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
802 ;; if they have been already set in another buffer. (e.g gnus-art).
803 (unless (and bookmark-yank-point
804 bookmark-current-buffer)
805 (setq bookmark-yank-point (point))
806 (setq bookmark-current-buffer (current-buffer)))
808 (let ((str
809 (or name
810 (read-from-minibuffer
811 (format "Set bookmark (%s): " default)
813 bookmark-minibuffer-read-name-map
814 nil nil defaults))))
815 (and (string-equal str "") (setq str default))
816 (bookmark-store str (cdr record) no-overwrite)
818 ;; Ask for an annotation buffer for this bookmark
819 (when bookmark-use-annotations
820 (bookmark-edit-annotation str))))
821 (setq bookmark-yank-point nil)
822 (setq bookmark-current-buffer nil)))
825 (defun bookmark-kill-line (&optional newline-too)
826 "Kill from point to end of line.
827 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
828 Does not affect the kill ring."
829 (let ((eol (line-end-position)))
830 (delete-region (point) eol)
831 (if (and newline-too (looking-at "\n"))
832 (delete-char 1))))
835 ;; Defvars to avoid compilation warnings:
836 (defvar bookmark-annotation-name nil
837 "Variable holding the name of the bookmark.
838 This is used in `bookmark-edit-annotation' to record the bookmark
839 whose annotation is being edited.")
842 (defun bookmark-default-annotation-text (bookmark-name)
843 "Return default annotation text for BOOKMARK-NAME.
844 The default annotation text is simply some text explaining how to use
845 annotations."
846 (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
847 "# All lines which start with a '#' will be deleted.\n"
848 "# Type C-c C-c when done.\n#\n"
849 "# Author: " (user-full-name) " <" (user-login-name) "@"
850 (system-name) ">\n"
851 "# Date: " (current-time-string) "\n"))
854 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
855 'bookmark-edit-annotation-text-func "23.1")
856 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
857 "Function to return default text to use for a bookmark annotation.
858 It takes one argument, the name of the bookmark, as a string.")
860 (defvar bookmark-edit-annotation-mode-map
861 (let ((map (make-sparse-keymap)))
862 (set-keymap-parent map text-mode-map)
863 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
864 map)
865 "Keymap for editing an annotation of a bookmark.")
868 (defun bookmark-edit-annotation-mode (bookmark-name-or-record)
869 "Mode for editing the annotation of bookmark BOOKMARK-NAME-OR-RECORD.
870 When you have finished composing, type \\[bookmark-send-annotation].
872 \\{bookmark-edit-annotation-mode-map}"
873 (interactive)
874 (kill-all-local-variables)
875 (make-local-variable 'bookmark-annotation-name)
876 (setq bookmark-annotation-name bookmark-name-or-record)
877 (use-local-map bookmark-edit-annotation-mode-map)
878 (setq major-mode 'bookmark-edit-annotation-mode
879 mode-name "Edit Bookmark Annotation")
880 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
881 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
882 (if (and annotation (not (string-equal annotation "")))
883 (insert annotation)))
884 (run-mode-hooks 'text-mode-hook))
887 (defun bookmark-send-edited-annotation ()
888 "Use buffer contents as annotation for a bookmark.
889 Lines beginning with `#' are ignored."
890 (interactive)
891 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
892 (error "Not in bookmark-edit-annotation-mode"))
893 (goto-char (point-min))
894 (while (< (point) (point-max))
895 (if (looking-at "^#")
896 (bookmark-kill-line t)
897 (forward-line 1)))
898 ;; Take no chances with text properties.
899 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
900 (bookmark-name bookmark-annotation-name))
901 (bookmark-set-annotation bookmark-name annotation)
902 (setq bookmark-alist-modification-count
903 (1+ bookmark-alist-modification-count))
904 (bookmark-bmenu-surreptitiously-rebuild-list))
905 (kill-buffer (current-buffer)))
908 (defun bookmark-edit-annotation (bookmark-name-or-record)
909 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
910 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
911 (bookmark-edit-annotation-mode bookmark-name-or-record))
914 (defun bookmark-buffer-name ()
915 "Return the name of the current buffer in a form usable as a bookmark name.
916 If the buffer is associated with a file or directory, use that name."
917 (cond
918 ;; Or are we a file?
919 (buffer-file-name (file-name-nondirectory buffer-file-name))
920 ;; Or are we a directory?
921 ((and (boundp 'dired-directory) dired-directory)
922 (let* ((dirname (if (stringp dired-directory)
923 dired-directory
924 (car dired-directory)))
925 (idx (1- (length dirname))))
926 ;; Strip the trailing slash.
927 (if (= ?/ (aref dirname idx))
928 (file-name-nondirectory (substring dirname 0 idx))
929 ;; Else return the current-buffer
930 (buffer-name (current-buffer)))))
931 ;; If all else fails, use the buffer's name.
933 (buffer-name (current-buffer)))))
936 (defun bookmark-yank-word ()
937 "Get the next word from buffer `bookmark-current-buffer' and append
938 it to the name of the bookmark currently being set, advancing
939 `bookmark-yank-point' by one word."
940 (interactive)
941 (let ((string (with-current-buffer bookmark-current-buffer
942 (goto-char bookmark-yank-point)
943 (buffer-substring-no-properties
944 (point)
945 (progn
946 (forward-word 1)
947 (setq bookmark-yank-point (point)))))))
948 (insert string)))
950 (defun bookmark-buffer-file-name ()
951 "Return the current buffer's file in a way useful for bookmarks."
952 ;; Abbreviate the path, both so it's shorter and so it's more
953 ;; portable. E.g., the user's home dir might be a different
954 ;; path on different machines, but "~/" will still reach it.
955 (abbreviate-file-name
956 (cond
957 (buffer-file-name buffer-file-name)
958 ((and (boundp 'dired-directory) dired-directory)
959 (if (stringp dired-directory)
960 dired-directory
961 (car dired-directory)))
962 (t (error "Buffer not visiting a file or directory")))))
965 (defun bookmark-maybe-load-default-file ()
966 "If bookmarks have not been loaded from the default place, load them."
967 (and (not bookmarks-already-loaded)
968 (null bookmark-alist)
969 (prog2
970 (and
971 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
972 ;; to be renamed.
973 (file-exists-p bookmark-old-default-file)
974 (not (file-exists-p bookmark-default-file))
975 (rename-file bookmark-old-default-file
976 bookmark-default-file))
977 ;; return t so the `and' will continue...
980 (file-readable-p bookmark-default-file)
981 (bookmark-load bookmark-default-file t t)
982 (setq bookmarks-already-loaded t)))
985 (defun bookmark-maybe-sort-alist ()
986 "Return `bookmark-alist' for display.
987 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
988 (if bookmark-sort-flag
989 (sort (copy-alist bookmark-alist)
990 (function
991 (lambda (x y) (string-lessp (car x) (car y)))))
992 bookmark-alist))
995 (defvar bookmark-after-jump-hook nil
996 "Hook run after `bookmark-jump' jumps to a bookmark.
997 Useful for example to unhide text in `outline-mode'.")
999 (defun bookmark--jump-via (bookmark-name-or-record display-function)
1000 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
1001 current buffer as argument.
1003 After calling DISPLAY-FUNCTION, set window point to the point specified
1004 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1005 and then show any annotations for this bookmark."
1006 (bookmark-handle-bookmark bookmark-name-or-record)
1007 (save-current-buffer
1008 (funcall display-function (current-buffer)))
1009 (let ((win (get-buffer-window (current-buffer) 0)))
1010 (if win (set-window-point win (point))))
1011 ;; FIXME: we used to only run bookmark-after-jump-hook in
1012 ;; `bookmark-jump' itself, but in none of the other commands.
1013 (run-hooks 'bookmark-after-jump-hook)
1014 (if bookmark-automatically-show-annotations
1015 ;; if there is an annotation for this bookmark,
1016 ;; show it in a buffer.
1017 (bookmark-show-annotation bookmark-name-or-record)))
1020 ;;;###autoload
1021 (defun bookmark-jump (bookmark &optional display-func)
1022 "Jump to bookmark BOOKMARK (a point in some file).
1023 You may have a problem using this function if the value of variable
1024 `bookmark-alist' is nil. If that happens, you need to load in some
1025 bookmarks. See help on function `bookmark-load' for more about
1026 this.
1028 If the file pointed to by BOOKMARK no longer exists, you will be asked
1029 if you wish to give the bookmark a new location, and `bookmark-jump'
1030 will then jump to the new location, as well as recording it in place
1031 of the old one in the permanent bookmark record.
1033 BOOKMARK is usually a bookmark name (a string). It can also be a
1034 bookmark record, but this is usually only done by programmatic callers.
1036 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1037 bookmark. It defaults to `switch-to-buffer'. A typical value for
1038 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1039 (interactive
1040 (list (bookmark-completing-read "Jump to bookmark"
1041 bookmark-current-bookmark)))
1042 (unless bookmark
1043 (error "No bookmark specified"))
1044 (bookmark-maybe-historicize-string bookmark)
1045 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1048 ;;;###autoload
1049 (defun bookmark-jump-other-window (bookmark)
1050 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1051 (interactive
1052 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1053 bookmark-current-bookmark)))
1054 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1057 (defun bookmark-jump-noselect (bookmark)
1058 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1059 The return value has the form (BUFFER . POINT).
1061 Note: this function is deprecated and is present for Emacs 22
1062 compatibility only."
1063 (declare (obsolete bookmark-handle-bookmark "23.1"))
1064 (save-excursion
1065 (bookmark-handle-bookmark bookmark)
1066 (cons (current-buffer) (point))))
1068 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1069 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1070 if it has none. This changes current buffer and point and returns nil,
1071 or signals a `file-error'.
1073 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1074 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1075 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1076 (condition-case err
1077 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1078 'bookmark-default-handler)
1079 (bookmark-get-bookmark bookmark-name-or-record))
1080 (bookmark-error-no-filename ;file-error
1081 ;; We were unable to find the marked file, so ask if user wants to
1082 ;; relocate the bookmark, else remind them to consider deletion.
1083 (when (stringp bookmark-name-or-record)
1084 ;; `bookmark-name-or-record' can be either a bookmark name
1085 ;; (from `bookmark-alist') or a bookmark object. If it's an
1086 ;; object, we assume it's a bookmark used internally by some
1087 ;; other package.
1088 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1089 (when file ;Don't know how to relocate if there's no `file'.
1090 ;; If file is not a dir, directory-file-name just returns file.
1091 (let ((display-name (directory-file-name file)))
1092 (ding)
1093 ;; Dialog boxes can accept a file target, but usually don't
1094 ;; know how to accept a directory target (at least, this
1095 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1096 ;; true on Windows as well). So we suppress file dialogs
1097 ;; when relocating.
1098 (let ((use-dialog-box nil)
1099 (use-file-dialog nil))
1100 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1101 bookmark-name-or-record "\"? "))
1102 (progn
1103 (bookmark-relocate bookmark-name-or-record)
1104 ;; Try again.
1105 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1106 'bookmark-default-handler)
1107 (bookmark-get-bookmark bookmark-name-or-record)))
1108 (message
1109 "Bookmark not relocated; consider removing it (%s)."
1110 bookmark-name-or-record)
1111 (signal (car err) (cdr err))))))))))
1112 ;; Added by db.
1113 (when (stringp bookmark-name-or-record)
1114 (setq bookmark-current-bookmark bookmark-name-or-record))
1115 nil)
1117 (put 'bookmark-error-no-filename
1118 'error-conditions
1119 '(error bookmark-errors bookmark-error-no-filename))
1120 (put 'bookmark-error-no-filename
1121 'error-message
1122 "Bookmark has no associated file (or directory)")
1124 (defun bookmark-default-handler (bmk-record)
1125 "Default handler to jump to a particular bookmark location.
1126 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1127 Changes current buffer and point and returns nil, or signals a `file-error'."
1128 (let ((file (bookmark-get-filename bmk-record))
1129 (buf (bookmark-prop-get bmk-record 'buffer))
1130 (forward-str (bookmark-get-front-context-string bmk-record))
1131 (behind-str (bookmark-get-rear-context-string bmk-record))
1132 (place (bookmark-get-position bmk-record)))
1133 (set-buffer
1134 (cond
1135 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1136 (find-file-noselect file))
1137 ;; No file found. See if buffer BUF have been created.
1138 ((and buf (get-buffer buf)))
1139 (t ;; If not, raise error.
1140 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1141 (if place (goto-char place))
1142 ;; Go searching forward first. Then, if forward-str exists and
1143 ;; was found in the file, we can search backward for behind-str.
1144 ;; Rationale is that if text was inserted between the two in the
1145 ;; file, it's better to be put before it so you can read it,
1146 ;; rather than after and remain perhaps unaware of the changes.
1147 (when (and forward-str (search-forward forward-str (point-max) t))
1148 (goto-char (match-beginning 0)))
1149 (when (and behind-str (search-backward behind-str (point-min) t))
1150 (goto-char (match-end 0)))
1151 nil))
1153 ;;;###autoload
1154 (defun bookmark-relocate (bookmark-name)
1155 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1157 This makes an already existing bookmark point to that file, instead of
1158 the one it used to point at. Useful when a file has been renamed
1159 after a bookmark was set in it."
1160 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1161 (bookmark-maybe-historicize-string bookmark-name)
1162 (bookmark-maybe-load-default-file)
1163 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1164 (newloc (abbreviate-file-name
1165 (expand-file-name
1166 (read-file-name
1167 (format "Relocate %s to: " bookmark-name)
1168 (file-name-directory bmrk-filename))))))
1169 (bookmark-set-filename bookmark-name newloc)
1170 (setq bookmark-alist-modification-count
1171 (1+ bookmark-alist-modification-count))
1172 (if (bookmark-time-to-save-p)
1173 (bookmark-save))
1174 (bookmark-bmenu-surreptitiously-rebuild-list)))
1177 ;;;###autoload
1178 (defun bookmark-insert-location (bookmark-name &optional no-history)
1179 "Insert the name of the file associated with BOOKMARK-NAME.
1181 Optional second arg NO-HISTORY means don't record this in the
1182 minibuffer history list `bookmark-history'."
1183 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1184 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1185 (let ((start (point)))
1186 (prog1
1187 (insert (bookmark-location bookmark-name))
1188 (if (display-mouse-p)
1189 (add-text-properties
1190 start
1191 (save-excursion (re-search-backward
1192 "[^ \t]")
1193 (1+ (point)))
1194 '(mouse-face highlight
1195 follow-link t
1196 help-echo "mouse-2: go to this bookmark in other window"))))))
1198 ;;;###autoload
1199 (defalias 'bookmark-locate 'bookmark-insert-location)
1201 (defun bookmark-location (bookmark-name-or-record)
1202 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1203 (bookmark-maybe-load-default-file)
1204 ;; We could call the `handler' and ask for it to construct a description
1205 ;; dynamically: it would open up several new possibilities, but it
1206 ;; would have the major disadvantage of forcing to load each and
1207 ;; every handler when the user calls bookmark-menu.
1208 (or (bookmark-prop-get bookmark-name-or-record 'location)
1209 (bookmark-get-filename bookmark-name-or-record)
1210 "-- Unknown location --"))
1213 ;;;###autoload
1214 (defun bookmark-rename (old-name &optional new-name)
1215 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1216 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1217 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1219 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1220 as an argument. If called with two strings, then no prompting is done.
1221 You must pass at least OLD-NAME when calling from Lisp.
1223 While you are entering the new name, consecutive C-w's insert
1224 consecutive words from the text of the buffer into the new bookmark
1225 name."
1226 (interactive (list (bookmark-completing-read "Old bookmark name")))
1227 (bookmark-maybe-historicize-string old-name)
1228 (bookmark-maybe-load-default-file)
1230 (setq bookmark-yank-point (point))
1231 (setq bookmark-current-buffer (current-buffer))
1232 (let ((final-new-name
1233 (or new-name ; use second arg, if non-nil
1234 (read-from-minibuffer
1235 "New name: "
1237 (let ((now-map (copy-keymap minibuffer-local-map)))
1238 (define-key now-map "\C-w" 'bookmark-yank-word)
1239 now-map)
1241 'bookmark-history))))
1242 (bookmark-set-name old-name final-new-name)
1243 (setq bookmark-current-bookmark final-new-name)
1244 (bookmark-bmenu-surreptitiously-rebuild-list)
1245 (setq bookmark-alist-modification-count
1246 (1+ bookmark-alist-modification-count))
1247 (if (bookmark-time-to-save-p)
1248 (bookmark-save))))
1251 ;;;###autoload
1252 (defun bookmark-insert (bookmark-name)
1253 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1254 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1256 You may have a problem using this function if the value of variable
1257 `bookmark-alist' is nil. If that happens, you need to load in some
1258 bookmarks. See help on function `bookmark-load' for more about
1259 this."
1260 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1261 (bookmark-maybe-historicize-string bookmark-name)
1262 (bookmark-maybe-load-default-file)
1263 (let ((orig-point (point))
1264 (str-to-insert
1265 (save-current-buffer
1266 (bookmark-handle-bookmark bookmark-name)
1267 (buffer-string))))
1268 (insert str-to-insert)
1269 (push-mark)
1270 (goto-char orig-point)))
1273 ;;;###autoload
1274 (defun bookmark-delete (bookmark-name &optional batch)
1275 "Delete BOOKMARK-NAME from the bookmark list.
1277 Removes only the first instance of a bookmark with that name. If
1278 there are one or more other bookmarks with the same name, they will
1279 not be deleted. Defaults to the \"current\" bookmark (that is, the
1280 one most recently used in this file, if any).
1281 Optional second arg BATCH means don't update the bookmark list buffer,
1282 probably because we were called from there."
1283 (interactive
1284 (list (bookmark-completing-read "Delete bookmark"
1285 bookmark-current-bookmark)))
1286 (bookmark-maybe-historicize-string bookmark-name)
1287 (bookmark-maybe-load-default-file)
1288 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1289 (setq bookmark-alist (delq will-go bookmark-alist))
1290 ;; Added by db, nil bookmark-current-bookmark if the last
1291 ;; occurrence has been deleted
1292 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1293 (setq bookmark-current-bookmark nil)))
1294 (unless batch
1295 (bookmark-bmenu-surreptitiously-rebuild-list))
1296 (setq bookmark-alist-modification-count
1297 (1+ bookmark-alist-modification-count))
1298 (when (bookmark-time-to-save-p)
1299 (bookmark-save)))
1302 (defun bookmark-time-to-save-p (&optional final-time)
1303 "Return t if it is time to save bookmarks to disk, nil otherwise.
1304 Optional argument FINAL-TIME means this is being called when Emacs
1305 is being killed, so save even if `bookmark-save-flag' is a number and
1306 is greater than `bookmark-alist-modification-count'."
1307 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1308 (cond (final-time
1309 (and (> bookmark-alist-modification-count 0)
1310 bookmark-save-flag))
1311 ((numberp bookmark-save-flag)
1312 (>= bookmark-alist-modification-count bookmark-save-flag))
1314 nil)))
1317 ;;;###autoload
1318 (defun bookmark-write ()
1319 "Write bookmarks to a file (reading the file name with the minibuffer).
1320 Don't use this in Lisp programs; use `bookmark-save' instead."
1321 (interactive)
1322 (bookmark-maybe-load-default-file)
1323 (bookmark-save t))
1326 ;;;###autoload
1327 (defun bookmark-save (&optional parg file)
1328 "Save currently defined bookmarks.
1329 Saves by default in the file defined by the variable
1330 `bookmark-default-file'. With a prefix arg, save it in file FILE
1331 \(second argument).
1333 If you are calling this from Lisp, the two arguments are PARG and
1334 FILE, and if you just want it to write to the default file, then
1335 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1336 instead. If you pass in one argument, and it is non-nil, then the
1337 user will be interactively queried for a file to save in.
1339 When you want to load in the bookmarks from a file, use
1340 `bookmark-load', \\[bookmark-load]. That function will prompt you
1341 for a file, defaulting to the file defined by variable
1342 `bookmark-default-file'."
1343 (interactive "P")
1344 (bookmark-maybe-load-default-file)
1345 (cond
1346 ((and (null parg) (null file))
1347 ;;whether interactive or not, write to default file
1348 (bookmark-write-file bookmark-default-file))
1349 ((and (null parg) file)
1350 ;;whether interactive or not, write to given file
1351 (bookmark-write-file file))
1352 ((and parg (not file))
1353 ;;have been called interactively w/ prefix arg
1354 (let ((file (read-file-name "File to save bookmarks in: ")))
1355 (bookmark-write-file file)))
1356 (t ; someone called us with prefix-arg *and* a file, so just write to file
1357 (bookmark-write-file file)))
1358 ;; signal that we have synced the bookmark file by setting this to
1359 ;; 0. If there was an error at any point before, it will not get
1360 ;; set, which is what we want.
1361 (setq bookmark-alist-modification-count 0))
1365 (defun bookmark-write-file (file)
1366 "Write `bookmark-alist' to FILE."
1367 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1368 (with-current-buffer (get-buffer-create " *Bookmarks*")
1369 (goto-char (point-min))
1370 (delete-region (point-min) (point-max))
1371 (let ((print-length nil)
1372 (print-level nil)
1373 ;; See bug #12503 for why we bind `print-circle'. Users
1374 ;; can define their own bookmark types, which can result in
1375 ;; arbitrary Lisp objects being stored in bookmark records,
1376 ;; and some users create objects containing circularities.
1377 (print-circle t))
1378 (bookmark-insert-file-format-version-stamp)
1379 (insert "(")
1380 ;; Rather than a single call to `pp' we make one per bookmark.
1381 ;; Apparently `pp' has a poor algorithmic complexity, so this
1382 ;; scales a lot better. bug#4485.
1383 (dolist (i bookmark-alist) (pp i (current-buffer)))
1384 (insert ")")
1385 (let ((version-control
1386 (cond
1387 ((null bookmark-version-control) nil)
1388 ((eq 'never bookmark-version-control) 'never)
1389 ((eq 'nospecial bookmark-version-control) version-control)
1390 (t t))))
1391 (condition-case nil
1392 (write-region (point-min) (point-max) file)
1393 (file-error (message "Can't write %s" file)))
1394 (kill-buffer (current-buffer))
1395 (bookmark-maybe-message
1396 "Saving bookmarks to file %s...done" file)))))
1399 (defun bookmark-import-new-list (new-list)
1400 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1401 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1402 they conflict with existing bookmark names."
1403 (let ((names (bookmark-all-names)))
1404 (dolist (full-record new-list)
1405 (bookmark-maybe-rename full-record names)
1406 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1407 (push (bookmark-name-from-full-record full-record) names))))
1410 (defun bookmark-maybe-rename (full-record names)
1411 "Rename bookmark FULL-RECORD if its current name is already used.
1412 This is a helper for `bookmark-import-new-list'."
1413 (let ((found-name (bookmark-name-from-full-record full-record)))
1414 (if (member found-name names)
1415 ;; We've got a conflict, so generate a new name
1416 (let ((count 2)
1417 (new-name found-name))
1418 (while (member new-name names)
1419 (setq new-name (concat found-name (format "<%d>" count)))
1420 (setq count (1+ count)))
1421 (bookmark-set-name full-record new-name)))))
1424 ;;;###autoload
1425 (defun bookmark-load (file &optional overwrite no-msg)
1426 "Load bookmarks from FILE (which must be in bookmark format).
1427 Appends loaded bookmarks to the front of the list of bookmarks. If
1428 optional second argument OVERWRITE is non-nil, existing bookmarks are
1429 destroyed. Optional third arg NO-MSG means don't display any messages
1430 while loading.
1432 If you load a file that doesn't contain a proper bookmark alist, you
1433 will corrupt Emacs's bookmark list. Generally, you should only load
1434 in files that were created with the bookmark functions in the first
1435 place. Your own personal bookmark file, `~/.emacs.bmk', is
1436 maintained automatically by Emacs; you shouldn't need to load it
1437 explicitly.
1439 If you load a file containing bookmarks with the same names as
1440 bookmarks already present in your Emacs, the new bookmarks will get
1441 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1442 method buffers use to resolve name collisions."
1443 (interactive
1444 (list (read-file-name
1445 (format "Load bookmarks from: (%s) "
1446 bookmark-default-file)
1447 ;;Default might not be used often,
1448 ;;but there's no better default, and
1449 ;;I guess it's better than none at all.
1450 "~/" bookmark-default-file 'confirm)))
1451 (setq file (abbreviate-file-name (expand-file-name file)))
1452 (if (not (file-readable-p file))
1453 (error "Cannot read bookmark file %s" file)
1454 (if (null no-msg)
1455 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1456 (with-current-buffer (let ((enable-local-variables nil))
1457 (find-file-noselect file))
1458 (goto-char (point-min))
1459 (bookmark-maybe-upgrade-file-format)
1460 (let ((blist (bookmark-alist-from-buffer)))
1461 (if (listp blist)
1462 (progn
1463 (if overwrite
1464 (progn
1465 (setq bookmark-alist blist)
1466 (setq bookmark-alist-modification-count 0))
1467 ;; else
1468 (bookmark-import-new-list blist)
1469 (setq bookmark-alist-modification-count
1470 (1+ bookmark-alist-modification-count)))
1471 (if (string-equal
1472 (abbreviate-file-name
1473 (expand-file-name bookmark-default-file))
1474 file)
1475 (setq bookmarks-already-loaded t))
1476 (bookmark-bmenu-surreptitiously-rebuild-list))
1477 (error "Invalid bookmark list in %s" file)))
1478 (kill-buffer (current-buffer)))
1479 (if (null no-msg)
1480 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1484 ;;; Code supporting the dired-like bookmark menu.
1485 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1488 (defvar bookmark-bmenu-hidden-bookmarks ())
1491 (defvar bookmark-bmenu-mode-map
1492 (let ((map (make-keymap)))
1493 (set-keymap-parent map special-mode-map)
1494 (define-key map "v" 'bookmark-bmenu-select)
1495 (define-key map "w" 'bookmark-bmenu-locate)
1496 (define-key map "2" 'bookmark-bmenu-2-window)
1497 (define-key map "1" 'bookmark-bmenu-1-window)
1498 (define-key map "j" 'bookmark-bmenu-this-window)
1499 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1500 (define-key map "f" 'bookmark-bmenu-this-window)
1501 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1502 (define-key map "o" 'bookmark-bmenu-other-window)
1503 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1504 (define-key map "s" 'bookmark-bmenu-save)
1505 (define-key map "k" 'bookmark-bmenu-delete)
1506 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1507 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1508 (define-key map "d" 'bookmark-bmenu-delete)
1509 (define-key map " " 'next-line)
1510 (define-key map "n" 'next-line)
1511 (define-key map "p" 'previous-line)
1512 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1513 (define-key map "u" 'bookmark-bmenu-unmark)
1514 (define-key map "m" 'bookmark-bmenu-mark)
1515 (define-key map "l" 'bookmark-bmenu-load)
1516 (define-key map "r" 'bookmark-bmenu-rename)
1517 (define-key map "R" 'bookmark-bmenu-relocate)
1518 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1519 (define-key map "a" 'bookmark-bmenu-show-annotation)
1520 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1521 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1522 (define-key map "/" 'bookmark-bmenu-search)
1523 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1524 map))
1526 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1527 ;; data.
1528 (put 'bookmark-bmenu-mode 'mode-class 'special)
1531 ;; todo: need to display whether or not bookmark exists as a buffer in
1532 ;; flag column.
1534 ;; Format:
1535 ;; FLAGS BOOKMARK [ LOCATION ]
1538 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1539 "Rebuild the Bookmark List if it exists.
1540 Don't affect the buffer ring order."
1541 (if (get-buffer "*Bookmark List*")
1542 (save-excursion
1543 (save-window-excursion
1544 (bookmark-bmenu-list)))))
1547 ;;;###autoload
1548 (defun bookmark-bmenu-list ()
1549 "Display a list of existing bookmarks.
1550 The list is displayed in a buffer named `*Bookmark List*'.
1551 The leftmost column displays a D if the bookmark is flagged for
1552 deletion, or > if it is flagged for displaying."
1553 (interactive)
1554 (bookmark-maybe-load-default-file)
1555 (let ((buf (get-buffer-create "*Bookmark List*")))
1556 (if (called-interactively-p 'interactive)
1557 (switch-to-buffer buf)
1558 (set-buffer buf)))
1559 (let ((inhibit-read-only t))
1560 (erase-buffer)
1561 (if (not bookmark-bmenu-use-header-line)
1562 (insert "% Bookmark\n- --------\n"))
1563 (add-text-properties (point-min) (point)
1564 '(font-lock-face bookmark-menu-heading))
1565 (dolist (full-record (bookmark-maybe-sort-alist))
1566 (let ((name (bookmark-name-from-full-record full-record))
1567 (annotation (bookmark-get-annotation full-record))
1568 (start (point))
1569 end)
1570 ;; if a bookmark has an annotation, prepend a "*"
1571 ;; in the list of bookmarks.
1572 (insert (if (and annotation (not (string-equal annotation "")))
1573 " *" " ")
1574 name)
1575 (setq end (point))
1576 (put-text-property
1577 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1578 (when (display-mouse-p)
1579 (add-text-properties
1580 (+ bookmark-bmenu-marks-width start) end
1581 '(mouse-face highlight
1582 follow-link t
1583 help-echo "mouse-2: go to this bookmark in other window")))
1584 (insert "\n")))
1585 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1586 (goto-char (point-min))
1587 (bookmark-bmenu-mode)
1588 (if bookmark-bmenu-use-header-line
1589 (bookmark-bmenu-set-header)
1590 (forward-line bookmark-bmenu-inline-header-height))
1591 (if bookmark-bmenu-toggle-filenames
1592 (bookmark-bmenu-toggle-filenames t))))
1594 ;;;###autoload
1595 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1596 ;;;###autoload
1597 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1599 (defun bookmark-bmenu-set-header ()
1600 "Sets the immutable header line."
1601 (let ((header (concat "%% " "Bookmark")))
1602 (when bookmark-bmenu-toggle-filenames
1603 (setq header (concat header
1604 (make-string (- bookmark-bmenu-file-column
1605 (- (length header) 3)) ?\s)
1606 "File")))
1607 (let ((pos 0))
1608 (while (string-match "[ \t\n]+" header pos)
1609 (setq pos (match-end 0))
1610 (put-text-property (match-beginning 0) pos 'display
1611 (list 'space :align-to (- pos 1))
1612 header)))
1613 (put-text-property 0 2 'face 'fixed-pitch header)
1614 (setq header (concat (propertize " " 'display '(space :align-to 0))
1615 header))
1616 ;; Code derived from `buff-menu.el'.
1617 (setq header-line-format header)))
1619 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1620 "Major mode for editing a list of bookmarks.
1621 Each line describes one of the bookmarks in Emacs.
1622 Letters do not insert themselves; instead, they are commands.
1623 Bookmark names preceded by a \"*\" have annotations.
1624 \\<bookmark-bmenu-mode-map>
1625 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1626 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1627 Also show bookmarks marked using m in other windows.
1628 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1629 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1630 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1631 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1632 together with bookmark selected before this one in another window.
1633 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1634 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1635 so the bookmark menu bookmark remains visible in its window.
1636 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1637 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1638 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1639 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1640 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1641 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1642 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1643 With a prefix arg, prompts for a file to save in.
1644 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1645 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1646 With prefix argument, also move up one line.
1647 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1648 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1649 in another buffer.
1650 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1651 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1652 (setq truncate-lines t)
1653 (setq buffer-read-only t))
1656 (defun bookmark-bmenu-toggle-filenames (&optional show)
1657 "Toggle whether filenames are shown in the bookmark list.
1658 Optional argument SHOW means show them unconditionally."
1659 (interactive)
1660 (cond
1661 (show
1662 (setq bookmark-bmenu-toggle-filenames nil)
1663 (bookmark-bmenu-show-filenames)
1664 (setq bookmark-bmenu-toggle-filenames t))
1665 (bookmark-bmenu-toggle-filenames
1666 (bookmark-bmenu-hide-filenames)
1667 (setq bookmark-bmenu-toggle-filenames nil))
1669 (bookmark-bmenu-show-filenames)
1670 (setq bookmark-bmenu-toggle-filenames t)))
1671 (when bookmark-bmenu-use-header-line
1672 (bookmark-bmenu-set-header)))
1675 (defun bookmark-bmenu-show-filenames (&optional force)
1676 "In an interactive bookmark list, show filenames along with bookmarks.
1677 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1678 mainly for debugging, and should not be necessary in normal use."
1679 (if (and (not force) bookmark-bmenu-toggle-filenames)
1680 nil ;already shown, so do nothing
1681 (with-buffer-modified-unmodified
1682 (save-excursion
1683 (save-window-excursion
1684 (goto-char (point-min))
1685 (if (not bookmark-bmenu-use-header-line)
1686 (forward-line bookmark-bmenu-inline-header-height))
1687 (setq bookmark-bmenu-hidden-bookmarks ())
1688 (let ((inhibit-read-only t))
1689 (while (< (point) (point-max))
1690 (let ((bmrk (bookmark-bmenu-bookmark)))
1691 (push bmrk bookmark-bmenu-hidden-bookmarks)
1692 (let ((start (line-end-position)))
1693 (move-to-column bookmark-bmenu-file-column t)
1694 ;; Strip off `mouse-face' from the white spaces region.
1695 (if (display-mouse-p)
1696 (remove-text-properties start (point)
1697 '(mouse-face nil help-echo nil))))
1698 (delete-region (point) (progn (end-of-line) (point)))
1699 (insert " ")
1700 ;; Pass the NO-HISTORY arg:
1701 (bookmark-insert-location bmrk t)
1702 (forward-line 1)))))))))
1705 (defun bookmark-bmenu-hide-filenames (&optional force)
1706 "In an interactive bookmark list, hide the filenames of the bookmarks.
1707 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1708 mainly for debugging, and should not be necessary in normal use."
1709 (when (and (not force) bookmark-bmenu-toggle-filenames)
1710 ;; nothing to hide if above is nil
1711 (with-buffer-modified-unmodified
1712 (save-excursion
1713 (goto-char (point-min))
1714 (if (not bookmark-bmenu-use-header-line)
1715 (forward-line bookmark-bmenu-inline-header-height))
1716 (setq bookmark-bmenu-hidden-bookmarks
1717 (nreverse bookmark-bmenu-hidden-bookmarks))
1718 (let ((inhibit-read-only t))
1719 (while bookmark-bmenu-hidden-bookmarks
1720 (move-to-column bookmark-bmenu-marks-width t)
1721 (bookmark-kill-line)
1722 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1723 (start (point)))
1724 (insert name)
1725 (put-text-property start (point) 'bookmark-name-prop name)
1726 (if (display-mouse-p)
1727 (add-text-properties
1728 start (point)
1729 '(mouse-face
1730 highlight follow-link t help-echo
1731 "mouse-2: go to this bookmark in other window"))))
1732 (forward-line 1)))))))
1735 (defun bookmark-bmenu-ensure-position ()
1736 "If point is not on a bookmark line, move it to one.
1737 If before the first bookmark line, move to the first; if after the
1738 last full line, move to the last full line. The return value is undefined."
1739 (cond ((and (not bookmark-bmenu-use-header-line)
1740 (< (count-lines (point-min) (point))
1741 bookmark-bmenu-inline-header-height))
1742 (goto-char (point-min))
1743 (forward-line bookmark-bmenu-inline-header-height))
1744 ((and (bolp) (eobp))
1745 (beginning-of-line 0))))
1748 (defun bookmark-bmenu-bookmark ()
1749 "Return the bookmark for this line in an interactive bookmark list buffer."
1750 (bookmark-bmenu-ensure-position)
1751 (save-excursion
1752 (beginning-of-line)
1753 (forward-char bookmark-bmenu-marks-width)
1754 (get-text-property (point) 'bookmark-name-prop)))
1757 (defun bookmark-show-annotation (bookmark-name-or-record)
1758 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1759 if an annotation exists."
1760 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1761 (when (and annotation (not (string-equal annotation "")))
1762 (save-excursion
1763 (let ((old-buf (current-buffer)))
1764 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1765 (delete-region (point-min) (point-max))
1766 (insert annotation)
1767 (goto-char (point-min))
1768 (switch-to-buffer-other-window old-buf))))))
1771 (defun bookmark-show-all-annotations ()
1772 "Display the annotations for all bookmarks in a buffer."
1773 (save-selected-window
1774 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1775 (delete-region (point-min) (point-max))
1776 (dolist (full-record bookmark-alist)
1777 (let* ((name (bookmark-name-from-full-record full-record))
1778 (ann (bookmark-get-annotation full-record)))
1779 (insert (concat name ":\n"))
1780 (if (and ann (not (string-equal ann "")))
1781 ;; insert the annotation, indented by 4 spaces.
1782 (progn
1783 (save-excursion (insert ann) (unless (bolp)
1784 (insert "\n")))
1785 (while (< (point) (point-max))
1786 (beginning-of-line) ; paranoia
1787 (insert " ")
1788 (forward-line)
1789 (end-of-line))))))
1790 (goto-char (point-min))))
1793 (defun bookmark-bmenu-mark ()
1794 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1795 (interactive)
1796 (beginning-of-line)
1797 (bookmark-bmenu-ensure-position)
1798 (with-buffer-modified-unmodified
1799 (let ((inhibit-read-only t))
1800 (delete-char 1)
1801 (insert ?>)
1802 (forward-line 1)
1803 (bookmark-bmenu-ensure-position))))
1806 (defun bookmark-bmenu-select ()
1807 "Select this line's bookmark; also display bookmarks marked with `>'.
1808 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1809 (interactive)
1810 (let ((bmrk (bookmark-bmenu-bookmark))
1811 (menu (current-buffer))
1812 (others ())
1813 tem)
1814 (goto-char (point-min))
1815 (while (re-search-forward "^>" nil t)
1816 (setq tem (bookmark-bmenu-bookmark))
1817 (let ((inhibit-read-only t))
1818 (delete-char -1)
1819 (insert ?\s))
1820 (or (string-equal tem bmrk)
1821 (member tem others)
1822 (setq others (cons tem others))))
1823 (setq others (nreverse others)
1824 tem (/ (1- (frame-height)) (1+ (length others))))
1825 (delete-other-windows)
1826 (bookmark-jump bmrk)
1827 (bury-buffer menu)
1828 (if others
1829 (while others
1830 (split-window nil tem)
1831 (other-window 1)
1832 (bookmark-jump (car others))
1833 (setq others (cdr others)))
1834 (other-window 1))))
1837 (defun bookmark-bmenu-any-marks ()
1838 "Return non-nil if any bookmarks are marked in the marks column."
1839 (save-excursion
1840 (goto-char (point-min))
1841 (bookmark-bmenu-ensure-position)
1842 (catch 'found-mark
1843 (while (not (eobp))
1844 (beginning-of-line)
1845 (if (looking-at "^\\S-")
1846 (throw 'found-mark t)
1847 (forward-line 1)))
1848 nil)))
1851 (defun bookmark-bmenu-save (parg)
1852 "Save the current list into a bookmark file.
1853 With a prefix arg, prompts for a file to save them in."
1854 (interactive "P")
1855 (save-excursion
1856 (save-window-excursion
1857 (bookmark-save parg)
1858 (set-buffer-modified-p nil))))
1861 (defun bookmark-bmenu-load ()
1862 "Load the bookmark file and rebuild the bookmark menu-buffer."
1863 (interactive)
1864 (bookmark-bmenu-ensure-position)
1865 (save-excursion
1866 (save-window-excursion
1867 ;; This will call `bookmark-bmenu-list'
1868 (call-interactively 'bookmark-load))))
1871 (defun bookmark-bmenu-1-window ()
1872 "Select this line's bookmark, alone, in full frame."
1873 (interactive)
1874 (bookmark-jump (bookmark-bmenu-bookmark))
1875 (bury-buffer (other-buffer))
1876 (delete-other-windows))
1879 (defun bookmark-bmenu-2-window ()
1880 "Select this line's bookmark, with previous buffer in second window."
1881 (interactive)
1882 (let ((bmrk (bookmark-bmenu-bookmark))
1883 (menu (current-buffer))
1884 (pop-up-windows t))
1885 (delete-other-windows)
1886 (switch-to-buffer (other-buffer) nil t)
1887 (bookmark--jump-via bmrk 'pop-to-buffer)
1888 (bury-buffer menu)))
1891 (defun bookmark-bmenu-this-window ()
1892 "Select this line's bookmark in this window."
1893 (interactive)
1894 (bookmark-jump (bookmark-bmenu-bookmark)))
1897 (defun bookmark-bmenu-other-window ()
1898 "Select this line's bookmark in other window, leaving bookmark menu visible."
1899 (interactive)
1900 (let ((bookmark (bookmark-bmenu-bookmark)))
1901 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1904 (defun bookmark-bmenu-switch-other-window ()
1905 "Make the other window select this line's bookmark.
1906 The current window remains selected."
1907 (interactive)
1908 (let ((bookmark (bookmark-bmenu-bookmark))
1909 (fun (lambda (b) (display-buffer b t))))
1910 (bookmark--jump-via bookmark fun)))
1912 (defun bookmark-bmenu-other-window-with-mouse (event)
1913 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1914 (interactive "e")
1915 (with-current-buffer (window-buffer (posn-window (event-end event)))
1916 (save-excursion
1917 (goto-char (posn-point (event-end event)))
1918 (bookmark-bmenu-other-window))))
1921 (defun bookmark-bmenu-show-annotation ()
1922 "Show the annotation for the current bookmark in another window."
1923 (interactive)
1924 (let ((bookmark (bookmark-bmenu-bookmark)))
1925 (bookmark-show-annotation bookmark)))
1928 (defun bookmark-bmenu-show-all-annotations ()
1929 "Show the annotation for all bookmarks in another window."
1930 (interactive)
1931 (bookmark-show-all-annotations))
1934 (defun bookmark-bmenu-edit-annotation ()
1935 "Edit the annotation for the current bookmark in another window."
1936 (interactive)
1937 (let ((bookmark (bookmark-bmenu-bookmark)))
1938 (bookmark-edit-annotation bookmark)))
1941 (defun bookmark-bmenu-unmark (&optional backup)
1942 "Cancel all requested operations on bookmark on this line and move down.
1943 Optional BACKUP means move up."
1944 (interactive "P")
1945 (beginning-of-line)
1946 (bookmark-bmenu-ensure-position)
1947 (with-buffer-modified-unmodified
1948 (let ((inhibit-read-only t))
1949 (delete-char 1)
1950 ;; any flags to reset according to circumstances? How about a
1951 ;; flag indicating whether this bookmark is being visited?
1952 ;; well, we don't have this now, so maybe later.
1953 (insert " "))
1954 (forward-line (if backup -1 1))
1955 (bookmark-bmenu-ensure-position)))
1958 (defun bookmark-bmenu-backup-unmark ()
1959 "Move up and cancel all requested operations on bookmark on line above."
1960 (interactive)
1961 (forward-line -1)
1962 (bookmark-bmenu-ensure-position)
1963 (bookmark-bmenu-unmark)
1964 (forward-line -1)
1965 (bookmark-bmenu-ensure-position))
1968 (defun bookmark-bmenu-delete ()
1969 "Mark bookmark on this line to be deleted.
1970 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1971 (interactive)
1972 (beginning-of-line)
1973 (bookmark-bmenu-ensure-position)
1974 (with-buffer-modified-unmodified
1975 (let ((inhibit-read-only t))
1976 (delete-char 1)
1977 (insert ?D)
1978 (forward-line 1)
1979 (bookmark-bmenu-ensure-position))))
1982 (defun bookmark-bmenu-delete-backwards ()
1983 "Mark bookmark on this line to be deleted, then move up one line.
1984 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1985 (interactive)
1986 (bookmark-bmenu-delete)
1987 (forward-line -2)
1988 (bookmark-bmenu-ensure-position)
1989 (forward-line 1)
1990 (bookmark-bmenu-ensure-position))
1993 (defun bookmark-bmenu-execute-deletions ()
1994 "Delete bookmarks flagged `D'."
1995 (interactive)
1996 (message "Deleting bookmarks...")
1997 (let ((o-point (point))
1998 (o-str (save-excursion
1999 (beginning-of-line)
2000 (unless (looking-at "^D")
2001 (buffer-substring
2002 (point)
2003 (progn (end-of-line) (point))))))
2004 (o-col (current-column)))
2005 (goto-char (point-min))
2006 (forward-line 1)
2007 (while (re-search-forward "^D" (point-max) t)
2008 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2009 (bookmark-bmenu-list)
2010 (if o-str
2011 (progn
2012 (goto-char (point-min))
2013 (search-forward o-str)
2014 (beginning-of-line)
2015 (forward-char o-col))
2016 (goto-char o-point))
2017 (beginning-of-line)
2018 (message "Deleting bookmarks...done")
2022 (defun bookmark-bmenu-rename ()
2023 "Rename bookmark on current line. Prompts for a new name."
2024 (interactive)
2025 (let ((bmrk (bookmark-bmenu-bookmark))
2026 (thispoint (point)))
2027 (bookmark-rename bmrk)
2028 (goto-char thispoint)))
2031 (defun bookmark-bmenu-locate ()
2032 "Display location of this bookmark. Displays in the minibuffer."
2033 (interactive)
2034 (let ((bmrk (bookmark-bmenu-bookmark)))
2035 (message "%s" (bookmark-location bmrk))))
2037 (defun bookmark-bmenu-relocate ()
2038 "Change the file path of the bookmark on the current line,
2039 prompting with completion for the new path."
2040 (interactive)
2041 (let ((bmrk (bookmark-bmenu-bookmark))
2042 (thispoint (point)))
2043 (bookmark-relocate bmrk)
2044 (goto-char thispoint)))
2046 ;;; Bookmark-bmenu search
2048 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2049 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2050 (let ((bookmark-alist
2051 (cl-loop for i in bookmark-alist
2052 when (string-match regexp (car i)) collect i into new
2053 finally return new)))
2054 (bookmark-bmenu-list)))
2057 ;;;###autoload
2058 (defun bookmark-bmenu-search ()
2059 "Incremental search of bookmarks, hiding the non-matches as we go."
2060 (interactive)
2061 (let ((bmk (bookmark-bmenu-bookmark))
2062 (timer nil))
2063 (unwind-protect
2064 (minibuffer-with-setup-hook
2065 (lambda ()
2066 (setq timer (run-with-idle-timer
2067 bookmark-search-delay 'repeat
2068 #'(lambda (buf)
2069 (with-current-buffer buf
2070 (bookmark-bmenu-filter-alist-by-regexp
2071 (minibuffer-contents))))
2072 (current-buffer))))
2073 (read-string "Pattern: ")
2074 (when timer (cancel-timer timer) (setq timer nil)))
2075 (when timer ;; Signalled an error or a `quit'.
2076 (cancel-timer timer)
2077 (bookmark-bmenu-list)
2078 (bookmark-bmenu-goto-bookmark bmk)))))
2080 (defun bookmark-bmenu-goto-bookmark (name)
2081 "Move point to bookmark with name NAME."
2082 (goto-char (point-min))
2083 (while (not (equal name (bookmark-bmenu-bookmark)))
2084 (forward-line 1))
2085 (forward-line 0))
2089 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2091 (defun bookmark-menu-popup-paned-menu (event name entries)
2092 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2093 That is, ENTRIES is a list of strings which appear as the choices
2094 in the menu.
2095 The number of panes depends on the number of entries.
2096 The visible entries are truncated to `bookmark-menu-length', but the
2097 strings returned are not."
2098 (let ((f-height (/ (frame-height) 2))
2099 (pane-list nil)
2100 (iter 0))
2101 (while entries
2102 (let (lst
2103 (count 0))
2104 (while (and (< count f-height) entries)
2105 (let ((str (car entries)))
2106 (push (cons
2107 (if (> (length str) bookmark-menu-length)
2108 (substring str 0 bookmark-menu-length)
2109 str)
2110 str)
2111 lst)
2112 (setq entries (cdr entries))
2113 (setq count (1+ count))))
2114 (setq iter (1+ iter))
2115 (push (cons
2116 (format "-*- %s (%d) -*-" name iter)
2117 (nreverse lst))
2118 pane-list)))
2120 ;; Popup the menu and return the string.
2121 (x-popup-menu event (cons (concat "-*- " name " -*-")
2122 (nreverse pane-list)))))
2125 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2126 ;; following works, and for explaining what to do to make it work.
2128 ;; We MUST autoload EACH form used to set up this variable's value, so
2129 ;; that the whole job is done in loaddefs.el.
2131 ;; Emacs menubar stuff.
2133 ;;;###autoload
2134 (defvar menu-bar-bookmark-map
2135 (let ((map (make-sparse-keymap "Bookmark functions")))
2136 (bindings--define-key map [load]
2137 '(menu-item "Load a Bookmark File..." bookmark-load
2138 :help "Load bookmarks from a bookmark file)"))
2139 (bindings--define-key map [write]
2140 '(menu-item "Save Bookmarks As..." bookmark-write
2141 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2142 (bindings--define-key map [save]
2143 '(menu-item "Save Bookmarks" bookmark-save
2144 :help "Save currently defined bookmarks"))
2145 (bindings--define-key map [edit]
2146 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2147 :help "Display a list of existing bookmarks"))
2148 (bindings--define-key map [delete]
2149 '(menu-item "Delete Bookmark..." bookmark-delete
2150 :help "Delete a bookmark from the bookmark list"))
2151 (bindings--define-key map [rename]
2152 '(menu-item "Rename Bookmark..." bookmark-rename
2153 :help "Change the name of a bookmark"))
2154 (bindings--define-key map [locate]
2155 '(menu-item "Insert Location..." bookmark-locate
2156 :help "Insert the name of the file associated with a bookmark"))
2157 (bindings--define-key map [insert]
2158 '(menu-item "Insert Contents..." bookmark-insert
2159 :help "Insert the text of the file pointed to by a bookmark"))
2160 (bindings--define-key map [set]
2161 '(menu-item "Set Bookmark..." bookmark-set
2162 :help "Set a bookmark named inside a file."))
2163 (bindings--define-key map [jump]
2164 '(menu-item "Jump to Bookmark..." bookmark-jump
2165 :help "Jump to a bookmark (a point in some file)"))
2166 map))
2168 ;;;###autoload
2169 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2171 ;; make bookmarks appear toward the right side of the menu.
2172 (if (boundp 'menu-bar-final-items)
2173 (if menu-bar-final-items
2174 (push 'bookmark menu-bar-final-items))
2175 (setq menu-bar-final-items '(bookmark)))
2177 ;;;; end bookmark menu stuff ;;;;
2180 ;; Load Hook
2181 (defvar bookmark-load-hook nil
2182 "Hook run at the end of loading library `bookmark.el'.")
2184 ;; Exit Hook, called from kill-emacs-hook
2185 (define-obsolete-variable-alias 'bookmark-exit-hooks
2186 'bookmark-exit-hook "22.1")
2187 (defvar bookmark-exit-hook nil
2188 "Hook run when Emacs exits.")
2190 (defun bookmark-exit-hook-internal ()
2191 "Save bookmark state, if necessary, at Emacs exit time.
2192 This also runs `bookmark-exit-hook'."
2193 (run-hooks 'bookmark-exit-hook)
2194 (and bookmark-alist
2195 (bookmark-time-to-save-p t)
2196 (bookmark-save)))
2198 (unless noninteractive
2199 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2201 (defun bookmark-unload-function ()
2202 "Unload the Bookmark library."
2203 (when bookmark-save-flag (bookmark-save))
2204 ;; continue standard unloading
2205 nil)
2208 (run-hooks 'bookmark-load-hook)
2210 (provide 'bookmark)
2212 ;;; bookmark.el ends here