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