Small updates for make-manuals admin scripts
[emacs.git] / lisp / bookmark.el
blob9ee3ff65b57f052fb4bcbb63d55abb2666f59d48
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)
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 (lambda (string pred action)
437 (if (eq action 'metadata)
438 '(metadata (category . bookmark))
439 (complete-with-action
440 action bookmark-alist string pred)))
444 'bookmark-history)))
445 (if (string-equal "" str) default str))))
448 (defmacro bookmark-maybe-historicize-string (string)
449 "Put STRING into the bookmark prompt history, if caller non-interactive.
450 We need this because sometimes bookmark functions are invoked from
451 menus, so `completing-read' never gets a chance to set `bookmark-history'."
452 `(or
453 (called-interactively-p 'interactive)
454 (setq bookmark-history (cons ,string bookmark-history))))
456 (defvar bookmark-make-record-function 'bookmark-make-record-default
457 "A function that should be called to create a bookmark record.
458 Modes may set this variable buffer-locally to enable bookmarking of
459 locations that should be treated specially, such as Info nodes,
460 news posts, images, pdf documents, etc.
462 The function will be called with no arguments.
463 It should signal a user error if it is unable to construct a record for
464 the current location.
466 The returned record should be a cons cell of the form (NAME . ALIST)
467 where ALIST is as described in `bookmark-alist' and may typically contain
468 a special cons (handler . HANDLER-FUNC) which specifies the handler function
469 that should be used instead of `bookmark-default-handler' to open this
470 bookmark. See the documentation for `bookmark-alist' for more.
472 NAME is a suggested name for the constructed bookmark. It can be nil
473 in which case a default heuristic will be used. The function can also
474 equivalently just return ALIST without NAME.")
476 (defun bookmark-make-record ()
477 "Return a new bookmark record (NAME . ALIST) for the current location."
478 (let ((record (funcall bookmark-make-record-function)))
479 ;; Set up defaults.
480 (bookmark-prop-set
481 record 'defaults
482 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
483 (list bookmark-current-bookmark
484 (bookmark-buffer-name))))))
485 ;; Set up default name.
486 (if (stringp (car record))
487 ;; The function already provided a default name.
488 record
489 (if (car record) (push nil record))
490 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
491 record)))
493 (defun bookmark-store (name alist no-overwrite)
494 "Store the bookmark NAME with data ALIST.
495 If NO-OVERWRITE is non-nil and another bookmark of the same name already
496 exists in `bookmark-alist', record the new bookmark without throwing away the
497 old one."
498 (bookmark-maybe-load-default-file)
499 (let ((stripped-name (copy-sequence name)))
500 (or (featurep 'xemacs)
501 ;; XEmacs's `set-text-properties' doesn't work on
502 ;; free-standing strings, apparently.
503 (set-text-properties 0 (length stripped-name) nil stripped-name))
504 (if (and (not no-overwrite)
505 (bookmark-get-bookmark stripped-name 'noerror))
506 ;; already existing bookmark under that name and
507 ;; no prefix arg means just overwrite old bookmark
508 ;; Use the new (NAME . ALIST) format.
509 (setcdr (bookmark-get-bookmark stripped-name) alist)
511 ;; otherwise just cons it onto the front (either the bookmark
512 ;; doesn't exist already, or there is no prefix arg. In either
513 ;; case, we want the new bookmark consed onto the alist...)
515 (push (cons stripped-name alist) bookmark-alist))
517 ;; Added by db
518 (setq bookmark-current-bookmark stripped-name)
519 (setq bookmark-alist-modification-count
520 (1+ bookmark-alist-modification-count))
521 (if (bookmark-time-to-save-p)
522 (bookmark-save))
524 (setq bookmark-current-bookmark stripped-name)
525 (bookmark-bmenu-surreptitiously-rebuild-list)))
527 (defun bookmark-make-record-default (&optional no-file no-context posn)
528 "Return the record describing the location of a new bookmark.
529 Point should be at the buffer in which the bookmark is being set,
530 and normally should be at the position where the bookmark is desired,
531 but see the optional arguments for other possibilities.
533 If NO-FILE is non-nil, then only return the subset of the
534 record that pertains to the location within the buffer, leaving off
535 the part that records the filename.
537 If NO-CONTEXT is non-nil, do not include the front- and rear-context
538 strings in the record -- the position is enough.
540 If POSN is non-nil, record POSN as the point instead of `(point)'."
541 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
542 ,@(unless no-context `((front-context-string
543 . ,(if (>= (- (point-max) (point))
544 bookmark-search-size)
545 (buffer-substring-no-properties
546 (point)
547 (+ (point) bookmark-search-size))
548 nil))))
549 ,@(unless no-context `((rear-context-string
550 . ,(if (>= (- (point) (point-min))
551 bookmark-search-size)
552 (buffer-substring-no-properties
553 (point)
554 (- (point) bookmark-search-size))
555 nil))))
556 (position . ,(or posn (point)))))
559 ;;; File format stuff
561 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
562 ;; the bookmark file format -- please don't. The current format
563 ;; should be extensible enough. If you feel the need to change it,
564 ;; please discuss it with other Emacs developers first.
566 ;; The format of `bookmark-alist' has changed twice in its lifetime.
567 ;; This comment describes the three formats, FIRST, SECOND, and
568 ;; CURRENT.
570 ;; The FIRST format was used prior to Emacs 20:
572 ;; ((BOOKMARK-NAME (FILENAME
573 ;; STRING-IN-FRONT
574 ;; STRING-BEHIND
575 ;; POINT))
576 ;; ...)
578 ;; The SECOND format was introduced in Emacs 20:
580 ;; ((BOOKMARK-NAME ((filename . FILENAME)
581 ;; (position . POS)
582 ;; (front-context-string . STR-AFTER-POS)
583 ;; (rear-context-string . STR-BEFORE-POS)
584 ;; (annotation . ANNOTATION)
585 ;; (whatever . VALUE)
586 ;; ...
587 ;; ))
588 ;; ...)
590 ;; The CURRENT format was introduced in Emacs 22:
592 ;; ((BOOKMARK-NAME (filename . FILENAME)
593 ;; (position . POS)
594 ;; (front-context-string . STR-AFTER-POS)
595 ;; (rear-context-string . STR-BEFORE-POS)
596 ;; (annotation . ANNOTATION)
597 ;; (whatever . VALUE)
598 ;; ...
599 ;; )
600 ;; ...)
602 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
603 ;; bookmark record is a list of entry information. FIRST and SECOND
604 ;; differ in the form of the record information: FIRST uses a list of
605 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
606 ;; the list elements matters. In the SECOND format, the order of the
607 ;; alist elements is unimportant. The SECOND format facilitates the
608 ;; addition of new kinds of elements, to support new kinds of
609 ;; bookmarks or code evolution.
611 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
612 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
613 ;; no longer a cons. Why that change was made remains a mystery --
614 ;; just be aware of it. (Be aware too that this explanatory comment
615 ;; was incorrect in Emacs 22 and Emacs 23.1.)
617 ;; To deal with the change from FIRST format to SECOND, conversion
618 ;; code was added, and it is still in use. See
619 ;; `bookmark-maybe-upgrade-file-format'.
621 ;; No conversion from SECOND to CURRENT is done. Instead, the code
622 ;; handles both formats OK. It must continue to do so.
624 ;; See the doc string of `bookmark-alist' for information about the
625 ;; elements that define a bookmark (e.g. `filename').
628 (defconst bookmark-file-format-version 1
629 "The current version of the format used by bookmark files.
630 You should never need to change this.")
633 (defconst bookmark-end-of-version-stamp-marker
634 "-*- End Of Bookmark File Format Version Stamp -*-\n"
635 "This string marks the end of the version stamp in a bookmark file.")
638 (defun bookmark-alist-from-buffer ()
639 "Return a `bookmark-alist' (in any format) from the current buffer.
640 The buffer must of course contain bookmark format information.
641 Does not care from where in the buffer it is called, and does not
642 affect point."
643 (save-excursion
644 (goto-char (point-min))
645 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
646 (read (current-buffer))
647 ;; Else we're dealing with format version 0
648 (if (search-forward "(" nil t)
649 (progn
650 (forward-char -1)
651 (read (current-buffer)))
652 ;; Else no hope of getting information here.
653 (error "Not bookmark format")))))
656 (defun bookmark-upgrade-version-0-alist (old-list)
657 "Upgrade a version 0 alist OLD-LIST to the current version."
658 (mapcar
659 (lambda (bookmark)
660 (let* ((name (car bookmark))
661 (record (car (cdr bookmark)))
662 (filename (nth 0 record))
663 (front-str (nth 1 record))
664 (rear-str (nth 2 record))
665 (position (nth 3 record))
666 (ann (nth 4 record)))
667 (list
668 name
669 `((filename . ,filename)
670 (front-context-string . ,(or front-str ""))
671 (rear-context-string . ,(or rear-str ""))
672 (position . ,position)
673 (annotation . ,ann)))))
674 old-list))
677 (defun bookmark-upgrade-file-format-from-0 ()
678 "Upgrade a bookmark file of format 0 (the original format) to format 1.
679 This expects to be called from `point-min' in a bookmark file."
680 (message "Upgrading bookmark format from 0 to %d..."
681 bookmark-file-format-version)
682 (let* ((old-list (bookmark-alist-from-buffer))
683 (new-list (bookmark-upgrade-version-0-alist old-list)))
684 (delete-region (point-min) (point-max))
685 (bookmark-insert-file-format-version-stamp)
686 (pp new-list (current-buffer))
687 (save-buffer))
688 (goto-char (point-min))
689 (message "Upgrading bookmark format from 0 to %d...done"
690 bookmark-file-format-version)
694 (defun bookmark-grok-file-format-version ()
695 "Return an integer which is the file-format version of this bookmark file.
696 This expects to be called from `point-min' in a bookmark file."
697 (if (looking-at "^;;;;")
698 (save-excursion
699 (save-match-data
700 (re-search-forward "[0-9]")
701 (forward-char -1)
702 (read (current-buffer))))
703 ;; Else this is format version 0, the original one, which didn't
704 ;; even have version stamps.
708 (defun bookmark-maybe-upgrade-file-format ()
709 "Check the file-format version of this bookmark file.
710 If the version is not up-to-date, upgrade it automatically.
711 This expects to be called from `point-min' in a bookmark file."
712 (let ((version (bookmark-grok-file-format-version)))
713 (cond
714 ((= version bookmark-file-format-version)
715 ) ; home free -- version is current
716 ((= version 0)
717 (bookmark-upgrade-file-format-from-0))
719 (error "Bookmark file format version strangeness")))))
722 (defun bookmark-insert-file-format-version-stamp ()
723 "Insert text indicating current version of bookmark file format."
724 (insert
725 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
726 bookmark-file-format-version))
727 (insert ";;; This format is meant to be slightly human-readable;\n"
728 ";;; nevertheless, you probably don't want to edit it.\n"
729 ";;; "
730 bookmark-end-of-version-stamp-marker))
733 ;;; end file-format stuff
736 ;;; Generic helpers.
738 (defun bookmark-maybe-message (fmt &rest args)
739 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
740 (if (>= baud-rate 9600)
741 (apply 'message fmt args)))
744 ;;; Core code:
746 (defvar bookmark-minibuffer-read-name-map
747 (let ((map (make-sparse-keymap)))
748 (set-keymap-parent map minibuffer-local-map)
749 (define-key map "\C-w" 'bookmark-yank-word)
750 map))
752 ;;;###autoload
753 (defun bookmark-set (&optional name no-overwrite)
754 "Set a bookmark named NAME at the current location.
755 If name is nil, then prompt the user.
757 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
758 existing bookmark that has the same name as NAME, but instead push the
759 new bookmark onto the bookmark alist. The most recently set bookmark
760 with name NAME is thus the one in effect at any given time, but the
761 others are still there, should the user decide to delete the most
762 recent one.
764 To yank words from the text of the buffer and use them as part of the
765 bookmark name, type C-w while setting a bookmark. Successive C-w's
766 yank successive words.
768 Typing C-u inserts (at the bookmark name prompt) the name of the last
769 bookmark used in the document where the new bookmark is being set;
770 this helps you use a single bookmark name to track progress through a
771 large document. If there is no prior bookmark for this document, then
772 C-u inserts an appropriate name based on the buffer or file.
774 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
775 it removes only the first instance of a bookmark with that name from
776 the list of bookmarks.)"
777 (interactive (list nil current-prefix-arg))
778 (unwind-protect
779 (let* ((record (bookmark-make-record))
780 ;; `defaults' is a transient element of the
781 ;; extensible format described above in the section
782 ;; `File format stuff'. Bookmark record functions
783 ;; can use it to specify a list of default values
784 ;; accessible via M-n while reading a bookmark name.
785 (defaults (bookmark-prop-get record 'defaults))
786 (default (if (consp defaults) (car defaults) defaults)))
788 (if defaults
789 ;; Don't store default values in the record.
790 (setq record (assq-delete-all 'defaults record))
791 ;; When no defaults in the record, use its first element.
792 (setq defaults (car record) default defaults))
794 (bookmark-maybe-load-default-file)
795 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
796 ;; if they have been already set in another buffer. (e.g gnus-art).
797 (unless (and bookmark-yank-point
798 bookmark-current-buffer)
799 (setq bookmark-yank-point (point))
800 (setq bookmark-current-buffer (current-buffer)))
802 (let ((str
803 (or name
804 (read-from-minibuffer
805 (format "Set bookmark (%s): " default)
807 bookmark-minibuffer-read-name-map
808 nil nil defaults))))
809 (and (string-equal str "") (setq str default))
810 (bookmark-store str (cdr record) no-overwrite)
812 ;; Ask for an annotation buffer for this bookmark
813 (when bookmark-use-annotations
814 (bookmark-edit-annotation str))))
815 (setq bookmark-yank-point nil)
816 (setq bookmark-current-buffer nil)))
819 (defun bookmark-kill-line (&optional newline-too)
820 "Kill from point to end of line.
821 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
822 Does not affect the kill ring."
823 (let ((eol (line-end-position)))
824 (delete-region (point) eol)
825 (if (and newline-too (looking-at "\n"))
826 (delete-char 1))))
829 ;; Defvars to avoid compilation warnings:
830 (defvar bookmark-annotation-name nil
831 "Variable holding the name of the bookmark.
832 This is used in `bookmark-edit-annotation' to record the bookmark
833 whose annotation is being edited.")
836 (defun bookmark-default-annotation-text (bookmark-name)
837 "Return default annotation text for BOOKMARK-NAME.
838 The default annotation text is simply some text explaining how to use
839 annotations."
840 (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
841 "# All lines which start with a '#' will be deleted.\n"
842 "# Type C-c C-c when done.\n#\n"
843 "# Author: " (user-full-name) " <" (user-login-name) "@"
844 (system-name) ">\n"
845 "# Date: " (current-time-string) "\n"))
848 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
849 'bookmark-edit-annotation-text-func "23.1")
850 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
851 "Function to return default text to use for a bookmark annotation.
852 It takes one argument, the name of the bookmark, as a string.")
854 (defvar bookmark-edit-annotation-mode-map
855 (let ((map (make-sparse-keymap)))
856 (set-keymap-parent map text-mode-map)
857 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
858 map)
859 "Keymap for editing an annotation of a bookmark.")
862 (defun bookmark-edit-annotation-mode (bookmark-name-or-record)
863 "Mode for editing the annotation of bookmark BOOKMARK-NAME-OR-RECORD.
864 When you have finished composing, type \\[bookmark-send-annotation].
866 \\{bookmark-edit-annotation-mode-map}"
867 (interactive)
868 (kill-all-local-variables)
869 (make-local-variable 'bookmark-annotation-name)
870 (setq bookmark-annotation-name bookmark-name-or-record)
871 (use-local-map bookmark-edit-annotation-mode-map)
872 (setq major-mode 'bookmark-edit-annotation-mode
873 mode-name "Edit Bookmark Annotation")
874 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
875 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
876 (if (and annotation (not (string-equal annotation "")))
877 (insert annotation)))
878 (run-mode-hooks 'text-mode-hook))
881 (defun bookmark-send-edited-annotation ()
882 "Use buffer contents as annotation for a bookmark.
883 Lines beginning with `#' are ignored."
884 (interactive)
885 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
886 (error "Not in bookmark-edit-annotation-mode"))
887 (goto-char (point-min))
888 (while (< (point) (point-max))
889 (if (looking-at "^#")
890 (bookmark-kill-line t)
891 (forward-line 1)))
892 ;; Take no chances with text properties.
893 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
894 (bookmark-name bookmark-annotation-name))
895 (bookmark-set-annotation bookmark-name annotation)
896 (setq bookmark-alist-modification-count
897 (1+ bookmark-alist-modification-count))
898 (bookmark-bmenu-surreptitiously-rebuild-list))
899 (kill-buffer (current-buffer)))
902 (defun bookmark-edit-annotation (bookmark-name-or-record)
903 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
904 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
905 (bookmark-edit-annotation-mode bookmark-name-or-record))
908 (defun bookmark-buffer-name ()
909 "Return the name of the current buffer in a form usable as a bookmark name.
910 If the buffer is associated with a file or directory, use that name."
911 (cond
912 ;; Or are we a file?
913 (buffer-file-name (file-name-nondirectory buffer-file-name))
914 ;; Or are we a directory?
915 ((and (boundp 'dired-directory) dired-directory)
916 (let* ((dirname (if (stringp dired-directory)
917 dired-directory
918 (car dired-directory)))
919 (idx (1- (length dirname))))
920 ;; Strip the trailing slash.
921 (if (= ?/ (aref dirname idx))
922 (file-name-nondirectory (substring dirname 0 idx))
923 ;; Else return the current-buffer
924 (buffer-name (current-buffer)))))
925 ;; If all else fails, use the buffer's name.
927 (buffer-name (current-buffer)))))
930 (defun bookmark-yank-word ()
931 "Get the next word from buffer `bookmark-current-buffer' and append
932 it to the name of the bookmark currently being set, advancing
933 `bookmark-yank-point' by one word."
934 (interactive)
935 (let ((string (with-current-buffer bookmark-current-buffer
936 (goto-char bookmark-yank-point)
937 (buffer-substring-no-properties
938 (point)
939 (progn
940 (forward-word 1)
941 (setq bookmark-yank-point (point)))))))
942 (insert string)))
944 (defun bookmark-buffer-file-name ()
945 "Return the current buffer's file in a way useful for bookmarks."
946 ;; Abbreviate the path, both so it's shorter and so it's more
947 ;; portable. E.g., the user's home dir might be a different
948 ;; path on different machines, but "~/" will still reach it.
949 (abbreviate-file-name
950 (cond
951 (buffer-file-name buffer-file-name)
952 ((and (boundp 'dired-directory) dired-directory)
953 (if (stringp dired-directory)
954 dired-directory
955 (car dired-directory)))
956 (t (error "Buffer not visiting a file or directory")))))
959 (defun bookmark-maybe-load-default-file ()
960 "If bookmarks have not been loaded from the default place, load them."
961 (and (not bookmarks-already-loaded)
962 (null bookmark-alist)
963 (prog2
964 (and
965 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
966 ;; to be renamed.
967 (file-exists-p bookmark-old-default-file)
968 (not (file-exists-p bookmark-default-file))
969 (rename-file bookmark-old-default-file
970 bookmark-default-file))
971 ;; return t so the `and' will continue...
974 (file-readable-p bookmark-default-file)
975 (bookmark-load bookmark-default-file t t)
976 (setq bookmarks-already-loaded t)))
979 (defun bookmark-maybe-sort-alist ()
980 "Return `bookmark-alist' for display.
981 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
982 (if bookmark-sort-flag
983 (sort (copy-alist bookmark-alist)
984 (function
985 (lambda (x y) (string-lessp (car x) (car y)))))
986 bookmark-alist))
989 (defvar bookmark-after-jump-hook nil
990 "Hook run after `bookmark-jump' jumps to a bookmark.
991 Useful for example to unhide text in `outline-mode'.")
993 (defun bookmark--jump-via (bookmark-name-or-record display-function)
994 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
995 current buffer as argument.
997 After calling DISPLAY-FUNCTION, set window point to the point specified
998 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
999 and then show any annotations for this bookmark."
1000 (bookmark-handle-bookmark bookmark-name-or-record)
1001 (save-current-buffer
1002 (funcall display-function (current-buffer)))
1003 (let ((win (get-buffer-window (current-buffer) 0)))
1004 (if win (set-window-point win (point))))
1005 ;; FIXME: we used to only run bookmark-after-jump-hook in
1006 ;; `bookmark-jump' itself, but in none of the other commands.
1007 (run-hooks 'bookmark-after-jump-hook)
1008 (if bookmark-automatically-show-annotations
1009 ;; if there is an annotation for this bookmark,
1010 ;; show it in a buffer.
1011 (bookmark-show-annotation bookmark-name-or-record)))
1014 ;;;###autoload
1015 (defun bookmark-jump (bookmark &optional display-func)
1016 "Jump to bookmark BOOKMARK (a point in some file).
1017 You may have a problem using this function if the value of variable
1018 `bookmark-alist' is nil. If that happens, you need to load in some
1019 bookmarks. See help on function `bookmark-load' for more about
1020 this.
1022 If the file pointed to by BOOKMARK no longer exists, you will be asked
1023 if you wish to give the bookmark a new location, and `bookmark-jump'
1024 will then jump to the new location, as well as recording it in place
1025 of the old one in the permanent bookmark record.
1027 BOOKMARK is usually a bookmark name (a string). It can also be a
1028 bookmark record, but this is usually only done by programmatic callers.
1030 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1031 bookmark. It defaults to `switch-to-buffer'. A typical value for
1032 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1033 (interactive
1034 (list (bookmark-completing-read "Jump to bookmark"
1035 bookmark-current-bookmark)))
1036 (unless bookmark
1037 (error "No bookmark specified"))
1038 (bookmark-maybe-historicize-string bookmark)
1039 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1042 ;;;###autoload
1043 (defun bookmark-jump-other-window (bookmark)
1044 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1045 (interactive
1046 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1047 bookmark-current-bookmark)))
1048 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1051 (defun bookmark-jump-noselect (bookmark)
1052 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1053 The return value has the form (BUFFER . POINT).
1055 Note: this function is deprecated and is present for Emacs 22
1056 compatibility only."
1057 (declare (obsolete bookmark-handle-bookmark "23.1"))
1058 (save-excursion
1059 (bookmark-handle-bookmark bookmark)
1060 (cons (current-buffer) (point))))
1062 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1063 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1064 if it has none. This changes current buffer and point and returns nil,
1065 or signals a `file-error'.
1067 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1068 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1069 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1070 (condition-case err
1071 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1072 'bookmark-default-handler)
1073 (bookmark-get-bookmark bookmark-name-or-record))
1074 (bookmark-error-no-filename ;file-error
1075 ;; We were unable to find the marked file, so ask if user wants to
1076 ;; relocate the bookmark, else remind them to consider deletion.
1077 (when (stringp bookmark-name-or-record)
1078 ;; `bookmark-name-or-record' can be either a bookmark name
1079 ;; (from `bookmark-alist') or a bookmark object. If it's an
1080 ;; object, we assume it's a bookmark used internally by some
1081 ;; other package.
1082 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1083 (when file ;Don't know how to relocate if there's no `file'.
1084 ;; If file is not a dir, directory-file-name just returns file.
1085 (let ((display-name (directory-file-name file)))
1086 (ding)
1087 ;; Dialog boxes can accept a file target, but usually don't
1088 ;; know how to accept a directory target (at least, this
1089 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1090 ;; true on Windows as well). So we suppress file dialogs
1091 ;; when relocating.
1092 (let ((use-dialog-box nil)
1093 (use-file-dialog nil))
1094 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1095 bookmark-name-or-record "\"? "))
1096 (progn
1097 (bookmark-relocate bookmark-name-or-record)
1098 ;; Try again.
1099 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1100 'bookmark-default-handler)
1101 (bookmark-get-bookmark bookmark-name-or-record)))
1102 (message
1103 "Bookmark not relocated; consider removing it (%s)."
1104 bookmark-name-or-record)
1105 (signal (car err) (cdr err))))))))))
1106 ;; Added by db.
1107 (when (stringp bookmark-name-or-record)
1108 (setq bookmark-current-bookmark bookmark-name-or-record))
1109 nil)
1111 (put 'bookmark-error-no-filename
1112 'error-conditions
1113 '(error bookmark-errors bookmark-error-no-filename))
1114 (put 'bookmark-error-no-filename
1115 'error-message
1116 "Bookmark has no associated file (or directory)")
1118 (defun bookmark-default-handler (bmk-record)
1119 "Default handler to jump to a particular bookmark location.
1120 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1121 Changes current buffer and point and returns nil, or signals a `file-error'."
1122 (let ((file (bookmark-get-filename bmk-record))
1123 (buf (bookmark-prop-get bmk-record 'buffer))
1124 (forward-str (bookmark-get-front-context-string bmk-record))
1125 (behind-str (bookmark-get-rear-context-string bmk-record))
1126 (place (bookmark-get-position bmk-record)))
1127 (set-buffer
1128 (cond
1129 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1130 (find-file-noselect file))
1131 ;; No file found. See if buffer BUF have been created.
1132 ((and buf (get-buffer buf)))
1133 (t ;; If not, raise error.
1134 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1135 (if place (goto-char place))
1136 ;; Go searching forward first. Then, if forward-str exists and
1137 ;; was found in the file, we can search backward for behind-str.
1138 ;; Rationale is that if text was inserted between the two in the
1139 ;; file, it's better to be put before it so you can read it,
1140 ;; rather than after and remain perhaps unaware of the changes.
1141 (when (and forward-str (search-forward forward-str (point-max) t))
1142 (goto-char (match-beginning 0)))
1143 (when (and behind-str (search-backward behind-str (point-min) t))
1144 (goto-char (match-end 0)))
1145 nil))
1147 ;;;###autoload
1148 (defun bookmark-relocate (bookmark-name)
1149 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1151 This makes an already existing bookmark point to that file, instead of
1152 the one it used to point at. Useful when a file has been renamed
1153 after a bookmark was set in it."
1154 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1155 (bookmark-maybe-historicize-string bookmark-name)
1156 (bookmark-maybe-load-default-file)
1157 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1158 (newloc (abbreviate-file-name
1159 (expand-file-name
1160 (read-file-name
1161 (format "Relocate %s to: " bookmark-name)
1162 (file-name-directory bmrk-filename))))))
1163 (bookmark-set-filename bookmark-name newloc)
1164 (setq bookmark-alist-modification-count
1165 (1+ bookmark-alist-modification-count))
1166 (if (bookmark-time-to-save-p)
1167 (bookmark-save))
1168 (bookmark-bmenu-surreptitiously-rebuild-list)))
1171 ;;;###autoload
1172 (defun bookmark-insert-location (bookmark-name &optional no-history)
1173 "Insert the name of the file associated with BOOKMARK-NAME.
1175 Optional second arg NO-HISTORY means don't record this in the
1176 minibuffer history list `bookmark-history'."
1177 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1178 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1179 (let ((start (point)))
1180 (prog1
1181 (insert (bookmark-location bookmark-name))
1182 (if (display-mouse-p)
1183 (add-text-properties
1184 start
1185 (save-excursion (re-search-backward
1186 "[^ \t]")
1187 (1+ (point)))
1188 '(mouse-face highlight
1189 follow-link t
1190 help-echo "mouse-2: go to this bookmark in other window"))))))
1192 ;;;###autoload
1193 (defalias 'bookmark-locate 'bookmark-insert-location)
1195 (defun bookmark-location (bookmark-name-or-record)
1196 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1197 (bookmark-maybe-load-default-file)
1198 ;; We could call the `handler' and ask for it to construct a description
1199 ;; dynamically: it would open up several new possibilities, but it
1200 ;; would have the major disadvantage of forcing to load each and
1201 ;; every handler when the user calls bookmark-menu.
1202 (or (bookmark-prop-get bookmark-name-or-record 'location)
1203 (bookmark-get-filename bookmark-name-or-record)
1204 "-- Unknown location --"))
1207 ;;;###autoload
1208 (defun bookmark-rename (old-name &optional new-name)
1209 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1210 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1211 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1213 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1214 as an argument. If called with two strings, then no prompting is done.
1215 You must pass at least OLD-NAME when calling from Lisp.
1217 While you are entering the new name, consecutive C-w's insert
1218 consecutive words from the text of the buffer into the new bookmark
1219 name."
1220 (interactive (list (bookmark-completing-read "Old bookmark name")))
1221 (bookmark-maybe-historicize-string old-name)
1222 (bookmark-maybe-load-default-file)
1224 (setq bookmark-yank-point (point))
1225 (setq bookmark-current-buffer (current-buffer))
1226 (let ((final-new-name
1227 (or new-name ; use second arg, if non-nil
1228 (read-from-minibuffer
1229 "New name: "
1231 (let ((now-map (copy-keymap minibuffer-local-map)))
1232 (define-key now-map "\C-w" 'bookmark-yank-word)
1233 now-map)
1235 'bookmark-history))))
1236 (bookmark-set-name old-name final-new-name)
1237 (setq bookmark-current-bookmark final-new-name)
1238 (bookmark-bmenu-surreptitiously-rebuild-list)
1239 (setq bookmark-alist-modification-count
1240 (1+ bookmark-alist-modification-count))
1241 (if (bookmark-time-to-save-p)
1242 (bookmark-save))))
1245 ;;;###autoload
1246 (defun bookmark-insert (bookmark-name)
1247 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1248 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1250 You may have a problem using this function if the value of variable
1251 `bookmark-alist' is nil. If that happens, you need to load in some
1252 bookmarks. See help on function `bookmark-load' for more about
1253 this."
1254 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1255 (bookmark-maybe-historicize-string bookmark-name)
1256 (bookmark-maybe-load-default-file)
1257 (let ((orig-point (point))
1258 (str-to-insert
1259 (save-current-buffer
1260 (bookmark-handle-bookmark bookmark-name)
1261 (buffer-string))))
1262 (insert str-to-insert)
1263 (push-mark)
1264 (goto-char orig-point)))
1267 ;;;###autoload
1268 (defun bookmark-delete (bookmark-name &optional batch)
1269 "Delete BOOKMARK-NAME from the bookmark list.
1271 Removes only the first instance of a bookmark with that name. If
1272 there are one or more other bookmarks with the same name, they will
1273 not be deleted. Defaults to the \"current\" bookmark (that is, the
1274 one most recently used in this file, if any).
1275 Optional second arg BATCH means don't update the bookmark list buffer,
1276 probably because we were called from there."
1277 (interactive
1278 (list (bookmark-completing-read "Delete bookmark"
1279 bookmark-current-bookmark)))
1280 (bookmark-maybe-historicize-string bookmark-name)
1281 (bookmark-maybe-load-default-file)
1282 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1283 (setq bookmark-alist (delq will-go bookmark-alist))
1284 ;; Added by db, nil bookmark-current-bookmark if the last
1285 ;; occurrence has been deleted
1286 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1287 (setq bookmark-current-bookmark nil)))
1288 (unless batch
1289 (bookmark-bmenu-surreptitiously-rebuild-list))
1290 (setq bookmark-alist-modification-count
1291 (1+ bookmark-alist-modification-count))
1292 (when (bookmark-time-to-save-p)
1293 (bookmark-save)))
1296 (defun bookmark-time-to-save-p (&optional final-time)
1297 "Return t if it is time to save bookmarks to disk, nil otherwise.
1298 Optional argument FINAL-TIME means this is being called when Emacs
1299 is being killed, so save even if `bookmark-save-flag' is a number and
1300 is greater than `bookmark-alist-modification-count'."
1301 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1302 (cond (final-time
1303 (and (> bookmark-alist-modification-count 0)
1304 bookmark-save-flag))
1305 ((numberp bookmark-save-flag)
1306 (>= bookmark-alist-modification-count bookmark-save-flag))
1308 nil)))
1311 ;;;###autoload
1312 (defun bookmark-write ()
1313 "Write bookmarks to a file (reading the file name with the minibuffer).
1314 Don't use this in Lisp programs; use `bookmark-save' instead."
1315 (interactive)
1316 (bookmark-maybe-load-default-file)
1317 (bookmark-save t))
1320 ;;;###autoload
1321 (defun bookmark-save (&optional parg file)
1322 "Save currently defined bookmarks.
1323 Saves by default in the file defined by the variable
1324 `bookmark-default-file'. With a prefix arg, save it in file FILE
1325 \(second argument).
1327 If you are calling this from Lisp, the two arguments are PARG and
1328 FILE, and if you just want it to write to the default file, then
1329 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1330 instead. If you pass in one argument, and it is non-nil, then the
1331 user will be interactively queried for a file to save in.
1333 When you want to load in the bookmarks from a file, use
1334 `bookmark-load', \\[bookmark-load]. That function will prompt you
1335 for a file, defaulting to the file defined by variable
1336 `bookmark-default-file'."
1337 (interactive "P")
1338 (bookmark-maybe-load-default-file)
1339 (cond
1340 ((and (null parg) (null file))
1341 ;;whether interactive or not, write to default file
1342 (bookmark-write-file bookmark-default-file))
1343 ((and (null parg) file)
1344 ;;whether interactive or not, write to given file
1345 (bookmark-write-file file))
1346 ((and parg (not file))
1347 ;;have been called interactively w/ prefix arg
1348 (let ((file (read-file-name "File to save bookmarks in: ")))
1349 (bookmark-write-file file)))
1350 (t ; someone called us with prefix-arg *and* a file, so just write to file
1351 (bookmark-write-file file)))
1352 ;; signal that we have synced the bookmark file by setting this to
1353 ;; 0. If there was an error at any point before, it will not get
1354 ;; set, which is what we want.
1355 (setq bookmark-alist-modification-count 0))
1359 (defun bookmark-write-file (file)
1360 "Write `bookmark-alist' to FILE."
1361 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1362 (with-current-buffer (get-buffer-create " *Bookmarks*")
1363 (goto-char (point-min))
1364 (delete-region (point-min) (point-max))
1365 (let ((print-length nil)
1366 (print-level nil)
1367 ;; See bug #12503 for why we bind `print-circle'. Users
1368 ;; can define their own bookmark types, which can result in
1369 ;; arbitrary Lisp objects being stored in bookmark records,
1370 ;; and some users create objects containing circularities.
1371 (print-circle t))
1372 (bookmark-insert-file-format-version-stamp)
1373 (insert "(")
1374 ;; Rather than a single call to `pp' we make one per bookmark.
1375 ;; Apparently `pp' has a poor algorithmic complexity, so this
1376 ;; scales a lot better. bug#4485.
1377 (dolist (i bookmark-alist) (pp i (current-buffer)))
1378 (insert ")")
1379 (let ((version-control
1380 (cond
1381 ((null bookmark-version-control) nil)
1382 ((eq 'never bookmark-version-control) 'never)
1383 ((eq 'nospecial bookmark-version-control) version-control)
1384 (t t))))
1385 (condition-case nil
1386 (write-region (point-min) (point-max) file)
1387 (file-error (message "Can't write %s" file)))
1388 (kill-buffer (current-buffer))
1389 (bookmark-maybe-message
1390 "Saving bookmarks to file %s...done" file)))))
1393 (defun bookmark-import-new-list (new-list)
1394 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1395 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1396 they conflict with existing bookmark names."
1397 (let ((names (bookmark-all-names)))
1398 (dolist (full-record new-list)
1399 (bookmark-maybe-rename full-record names)
1400 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1401 (push (bookmark-name-from-full-record full-record) names))))
1404 (defun bookmark-maybe-rename (full-record names)
1405 "Rename bookmark FULL-RECORD if its current name is already used.
1406 This is a helper for `bookmark-import-new-list'."
1407 (let ((found-name (bookmark-name-from-full-record full-record)))
1408 (if (member found-name names)
1409 ;; We've got a conflict, so generate a new name
1410 (let ((count 2)
1411 (new-name found-name))
1412 (while (member new-name names)
1413 (setq new-name (concat found-name (format "<%d>" count)))
1414 (setq count (1+ count)))
1415 (bookmark-set-name full-record new-name)))))
1418 ;;;###autoload
1419 (defun bookmark-load (file &optional overwrite no-msg)
1420 "Load bookmarks from FILE (which must be in bookmark format).
1421 Appends loaded bookmarks to the front of the list of bookmarks. If
1422 optional second argument OVERWRITE is non-nil, existing bookmarks are
1423 destroyed. Optional third arg NO-MSG means don't display any messages
1424 while loading.
1426 If you load a file that doesn't contain a proper bookmark alist, you
1427 will corrupt Emacs's bookmark list. Generally, you should only load
1428 in files that were created with the bookmark functions in the first
1429 place. Your own personal bookmark file, `~/.emacs.bmk', is
1430 maintained automatically by Emacs; you shouldn't need to load it
1431 explicitly.
1433 If you load a file containing bookmarks with the same names as
1434 bookmarks already present in your Emacs, the new bookmarks will get
1435 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1436 method buffers use to resolve name collisions."
1437 (interactive
1438 (list (read-file-name
1439 (format "Load bookmarks from: (%s) "
1440 bookmark-default-file)
1441 ;;Default might not be used often,
1442 ;;but there's no better default, and
1443 ;;I guess it's better than none at all.
1444 "~/" bookmark-default-file 'confirm)))
1445 (setq file (abbreviate-file-name (expand-file-name file)))
1446 (if (not (file-readable-p file))
1447 (error "Cannot read bookmark file %s" file)
1448 (if (null no-msg)
1449 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1450 (with-current-buffer (let ((enable-local-variables nil))
1451 (find-file-noselect file))
1452 (goto-char (point-min))
1453 (bookmark-maybe-upgrade-file-format)
1454 (let ((blist (bookmark-alist-from-buffer)))
1455 (if (listp blist)
1456 (progn
1457 (if overwrite
1458 (progn
1459 (setq bookmark-alist blist)
1460 (setq bookmark-alist-modification-count 0))
1461 ;; else
1462 (bookmark-import-new-list blist)
1463 (setq bookmark-alist-modification-count
1464 (1+ bookmark-alist-modification-count)))
1465 (if (string-equal
1466 (abbreviate-file-name
1467 (expand-file-name bookmark-default-file))
1468 file)
1469 (setq bookmarks-already-loaded t))
1470 (bookmark-bmenu-surreptitiously-rebuild-list))
1471 (error "Invalid bookmark list in %s" file)))
1472 (kill-buffer (current-buffer)))
1473 (if (null no-msg)
1474 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1478 ;;; Code supporting the dired-like bookmark menu.
1479 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1482 (defvar bookmark-bmenu-hidden-bookmarks ())
1485 (defvar bookmark-bmenu-mode-map
1486 (let ((map (make-keymap)))
1487 (set-keymap-parent map special-mode-map)
1488 (define-key map "v" 'bookmark-bmenu-select)
1489 (define-key map "w" 'bookmark-bmenu-locate)
1490 (define-key map "2" 'bookmark-bmenu-2-window)
1491 (define-key map "1" 'bookmark-bmenu-1-window)
1492 (define-key map "j" 'bookmark-bmenu-this-window)
1493 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1494 (define-key map "f" 'bookmark-bmenu-this-window)
1495 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1496 (define-key map "o" 'bookmark-bmenu-other-window)
1497 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1498 (define-key map "s" 'bookmark-bmenu-save)
1499 (define-key map "k" 'bookmark-bmenu-delete)
1500 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1501 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1502 (define-key map "d" 'bookmark-bmenu-delete)
1503 (define-key map " " 'next-line)
1504 (define-key map "n" 'next-line)
1505 (define-key map "p" 'previous-line)
1506 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1507 (define-key map "u" 'bookmark-bmenu-unmark)
1508 (define-key map "m" 'bookmark-bmenu-mark)
1509 (define-key map "l" 'bookmark-bmenu-load)
1510 (define-key map "r" 'bookmark-bmenu-rename)
1511 (define-key map "R" 'bookmark-bmenu-relocate)
1512 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1513 (define-key map "a" 'bookmark-bmenu-show-annotation)
1514 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1515 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1516 (define-key map "/" 'bookmark-bmenu-search)
1517 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1518 map))
1520 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1521 ;; data.
1522 (put 'bookmark-bmenu-mode 'mode-class 'special)
1525 ;; todo: need to display whether or not bookmark exists as a buffer in
1526 ;; flag column.
1528 ;; Format:
1529 ;; FLAGS BOOKMARK [ LOCATION ]
1532 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1533 "Rebuild the Bookmark List if it exists.
1534 Don't affect the buffer ring order."
1535 (if (get-buffer "*Bookmark List*")
1536 (save-excursion
1537 (save-window-excursion
1538 (bookmark-bmenu-list)))))
1541 ;;;###autoload
1542 (defun bookmark-bmenu-list ()
1543 "Display a list of existing bookmarks.
1544 The list is displayed in a buffer named `*Bookmark List*'.
1545 The leftmost column displays a D if the bookmark is flagged for
1546 deletion, or > if it is flagged for displaying."
1547 (interactive)
1548 (bookmark-maybe-load-default-file)
1549 (let ((buf (get-buffer-create "*Bookmark List*")))
1550 (if (called-interactively-p 'interactive)
1551 (switch-to-buffer buf)
1552 (set-buffer buf)))
1553 (let ((inhibit-read-only t))
1554 (erase-buffer)
1555 (insert "% Bookmark\n- --------\n")
1556 (add-text-properties (point-min) (point)
1557 '(font-lock-face bookmark-menu-heading))
1558 (dolist (full-record (bookmark-maybe-sort-alist))
1559 (let ((name (bookmark-name-from-full-record full-record))
1560 (annotation (bookmark-get-annotation full-record))
1561 (start (point))
1562 end)
1563 ;; if a bookmark has an annotation, prepend a "*"
1564 ;; in the list of bookmarks.
1565 (insert (if (and annotation (not (string-equal annotation "")))
1566 " *" " ")
1567 name)
1568 (setq end (point))
1569 (put-text-property
1570 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1571 (when (display-mouse-p)
1572 (add-text-properties
1573 (+ bookmark-bmenu-marks-width start) end
1574 '(mouse-face highlight
1575 follow-link t
1576 help-echo "mouse-2: go to this bookmark in other window")))
1577 (insert "\n")))
1578 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1579 (goto-char (point-min))
1580 (forward-line 2)
1581 (bookmark-bmenu-mode)
1582 (if bookmark-bmenu-toggle-filenames
1583 (bookmark-bmenu-toggle-filenames t))))
1585 ;;;###autoload
1586 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1587 ;;;###autoload
1588 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1592 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1593 "Major mode for editing a list of bookmarks.
1594 Each line describes one of the bookmarks in Emacs.
1595 Letters do not insert themselves; instead, they are commands.
1596 Bookmark names preceded by a \"*\" have annotations.
1597 \\<bookmark-bmenu-mode-map>
1598 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1599 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1600 Also show bookmarks marked using m in other windows.
1601 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1602 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1603 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1604 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1605 together with bookmark selected before this one in another window.
1606 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1607 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1608 so the bookmark menu bookmark remains visible in its window.
1609 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1610 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1611 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1612 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1613 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1614 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1615 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1616 With a prefix arg, prompts for a file to save in.
1617 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1618 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1619 With prefix argument, also move up one line.
1620 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1621 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1622 in another buffer.
1623 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1624 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1625 (setq truncate-lines t)
1626 (setq buffer-read-only t))
1629 (defun bookmark-bmenu-toggle-filenames (&optional show)
1630 "Toggle whether filenames are shown in the bookmark list.
1631 Optional argument SHOW means show them unconditionally."
1632 (interactive)
1633 (cond
1634 (show
1635 (setq bookmark-bmenu-toggle-filenames nil)
1636 (bookmark-bmenu-show-filenames)
1637 (setq bookmark-bmenu-toggle-filenames t))
1638 (bookmark-bmenu-toggle-filenames
1639 (bookmark-bmenu-hide-filenames)
1640 (setq bookmark-bmenu-toggle-filenames nil))
1642 (bookmark-bmenu-show-filenames)
1643 (setq bookmark-bmenu-toggle-filenames t))))
1646 (defun bookmark-bmenu-show-filenames (&optional force)
1647 "In an interactive bookmark list, show filenames along with bookmarks.
1648 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1649 mainly for debugging, and should not be necessary in normal use."
1650 (if (and (not force) bookmark-bmenu-toggle-filenames)
1651 nil ;already shown, so do nothing
1652 (with-buffer-modified-unmodified
1653 (save-excursion
1654 (save-window-excursion
1655 (goto-char (point-min))
1656 (forward-line 2)
1657 (setq bookmark-bmenu-hidden-bookmarks ())
1658 (let ((inhibit-read-only t))
1659 (while (< (point) (point-max))
1660 (let ((bmrk (bookmark-bmenu-bookmark)))
1661 (push bmrk bookmark-bmenu-hidden-bookmarks)
1662 (let ((start (line-end-position)))
1663 (move-to-column bookmark-bmenu-file-column t)
1664 ;; Strip off `mouse-face' from the white spaces region.
1665 (if (display-mouse-p)
1666 (remove-text-properties start (point)
1667 '(mouse-face nil help-echo nil))))
1668 (delete-region (point) (progn (end-of-line) (point)))
1669 (insert " ")
1670 ;; Pass the NO-HISTORY arg:
1671 (bookmark-insert-location bmrk t)
1672 (forward-line 1)))))))))
1675 (defun bookmark-bmenu-hide-filenames (&optional force)
1676 "In an interactive bookmark list, hide the filenames of the 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 (when (and (not force) bookmark-bmenu-toggle-filenames)
1680 ;; nothing to hide if above is nil
1681 (with-buffer-modified-unmodified
1682 (save-excursion
1683 (goto-char (point-min))
1684 (forward-line 2)
1685 (setq bookmark-bmenu-hidden-bookmarks
1686 (nreverse bookmark-bmenu-hidden-bookmarks))
1687 (let ((inhibit-read-only t))
1688 (while bookmark-bmenu-hidden-bookmarks
1689 (move-to-column bookmark-bmenu-marks-width t)
1690 (bookmark-kill-line)
1691 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1692 (start (point)))
1693 (insert name)
1694 (put-text-property start (point) 'bookmark-name-prop name)
1695 (if (display-mouse-p)
1696 (add-text-properties
1697 start (point)
1698 '(mouse-face
1699 highlight follow-link t help-echo
1700 "mouse-2: go to this bookmark in other window"))))
1701 (forward-line 1)))))))
1704 (defun bookmark-bmenu-ensure-position ()
1705 "If point is not on a bookmark line, move it to one.
1706 If before the first bookmark line, move to the first; if after the
1707 last full line, move to the last full line. The return value is undefined."
1708 (cond ((< (count-lines (point-min) (point)) bookmark-bmenu-header-height)
1709 (goto-char (point-min))
1710 (forward-line bookmark-bmenu-header-height))
1711 ((and (bolp) (eobp))
1712 (beginning-of-line 0))))
1715 (defun bookmark-bmenu-bookmark ()
1716 "Return the bookmark for this line in an interactive bookmark list buffer."
1717 (bookmark-bmenu-ensure-position)
1718 (save-excursion
1719 (beginning-of-line)
1720 (forward-char bookmark-bmenu-marks-width)
1721 (get-text-property (point) 'bookmark-name-prop)))
1724 (defun bookmark-show-annotation (bookmark-name-or-record)
1725 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1726 if an annotation exists."
1727 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1728 (when (and annotation (not (string-equal annotation "")))
1729 (save-excursion
1730 (let ((old-buf (current-buffer)))
1731 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1732 (delete-region (point-min) (point-max))
1733 (insert annotation)
1734 (goto-char (point-min))
1735 (switch-to-buffer-other-window old-buf))))))
1738 (defun bookmark-show-all-annotations ()
1739 "Display the annotations for all bookmarks in a buffer."
1740 (save-selected-window
1741 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1742 (delete-region (point-min) (point-max))
1743 (dolist (full-record bookmark-alist)
1744 (let* ((name (bookmark-name-from-full-record full-record))
1745 (ann (bookmark-get-annotation full-record)))
1746 (insert (concat name ":\n"))
1747 (if (and ann (not (string-equal ann "")))
1748 ;; insert the annotation, indented by 4 spaces.
1749 (progn
1750 (save-excursion (insert ann) (unless (bolp)
1751 (insert "\n")))
1752 (while (< (point) (point-max))
1753 (beginning-of-line) ; paranoia
1754 (insert " ")
1755 (forward-line)
1756 (end-of-line))))))
1757 (goto-char (point-min))))
1760 (defun bookmark-bmenu-mark ()
1761 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1762 (interactive)
1763 (beginning-of-line)
1764 (bookmark-bmenu-ensure-position)
1765 (with-buffer-modified-unmodified
1766 (let ((inhibit-read-only t))
1767 (delete-char 1)
1768 (insert ?>)
1769 (forward-line 1)
1770 (bookmark-bmenu-ensure-position))))
1773 (defun bookmark-bmenu-select ()
1774 "Select this line's bookmark; also display bookmarks marked with `>'.
1775 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1776 (interactive)
1777 (let ((bmrk (bookmark-bmenu-bookmark))
1778 (menu (current-buffer))
1779 (others ())
1780 tem)
1781 (goto-char (point-min))
1782 (while (re-search-forward "^>" nil t)
1783 (setq tem (bookmark-bmenu-bookmark))
1784 (let ((inhibit-read-only t))
1785 (delete-char -1)
1786 (insert ?\s))
1787 (or (string-equal tem bmrk)
1788 (member tem others)
1789 (setq others (cons tem others))))
1790 (setq others (nreverse others)
1791 tem (/ (1- (frame-height)) (1+ (length others))))
1792 (delete-other-windows)
1793 (bookmark-jump bmrk)
1794 (bury-buffer menu)
1795 (if others
1796 (while others
1797 (split-window nil tem)
1798 (other-window 1)
1799 (bookmark-jump (car others))
1800 (setq others (cdr others)))
1801 (other-window 1))))
1804 (defun bookmark-bmenu-any-marks ()
1805 "Return non-nil if any bookmarks are marked in the marks column."
1806 (save-excursion
1807 (goto-char (point-min))
1808 (bookmark-bmenu-ensure-position)
1809 (catch 'found-mark
1810 (while (not (eobp))
1811 (beginning-of-line)
1812 (if (looking-at "^\\S-")
1813 (throw 'found-mark t)
1814 (forward-line 1)))
1815 nil)))
1818 (defun bookmark-bmenu-save (parg)
1819 "Save the current list into a bookmark file.
1820 With a prefix arg, prompts for a file to save them in."
1821 (interactive "P")
1822 (save-excursion
1823 (save-window-excursion
1824 (bookmark-save parg)
1825 (set-buffer-modified-p nil))))
1828 (defun bookmark-bmenu-load ()
1829 "Load the bookmark file and rebuild the bookmark menu-buffer."
1830 (interactive)
1831 (bookmark-bmenu-ensure-position)
1832 (save-excursion
1833 (save-window-excursion
1834 ;; This will call `bookmark-bmenu-list'
1835 (call-interactively 'bookmark-load))))
1838 (defun bookmark-bmenu-1-window ()
1839 "Select this line's bookmark, alone, in full frame."
1840 (interactive)
1841 (bookmark-jump (bookmark-bmenu-bookmark))
1842 (bury-buffer (other-buffer))
1843 (delete-other-windows))
1846 (defun bookmark-bmenu-2-window ()
1847 "Select this line's bookmark, with previous buffer in second window."
1848 (interactive)
1849 (let ((bmrk (bookmark-bmenu-bookmark))
1850 (menu (current-buffer))
1851 (pop-up-windows t))
1852 (delete-other-windows)
1853 (switch-to-buffer (other-buffer) nil t)
1854 (bookmark--jump-via bmrk 'pop-to-buffer)
1855 (bury-buffer menu)))
1858 (defun bookmark-bmenu-this-window ()
1859 "Select this line's bookmark in this window."
1860 (interactive)
1861 (bookmark-jump (bookmark-bmenu-bookmark)))
1864 (defun bookmark-bmenu-other-window ()
1865 "Select this line's bookmark in other window, leaving bookmark menu visible."
1866 (interactive)
1867 (let ((bookmark (bookmark-bmenu-bookmark)))
1868 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1871 (defun bookmark-bmenu-switch-other-window ()
1872 "Make the other window select this line's bookmark.
1873 The current window remains selected."
1874 (interactive)
1875 (let ((bookmark (bookmark-bmenu-bookmark))
1876 (pop-up-windows t)
1877 same-window-buffer-names
1878 same-window-regexps)
1879 (bookmark--jump-via bookmark 'display-buffer)))
1881 (defun bookmark-bmenu-other-window-with-mouse (event)
1882 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1883 (interactive "e")
1884 (with-current-buffer (window-buffer (posn-window (event-end event)))
1885 (save-excursion
1886 (goto-char (posn-point (event-end event)))
1887 (bookmark-bmenu-other-window))))
1890 (defun bookmark-bmenu-show-annotation ()
1891 "Show the annotation for the current bookmark in another window."
1892 (interactive)
1893 (let ((bookmark (bookmark-bmenu-bookmark)))
1894 (bookmark-show-annotation bookmark)))
1897 (defun bookmark-bmenu-show-all-annotations ()
1898 "Show the annotation for all bookmarks in another window."
1899 (interactive)
1900 (bookmark-show-all-annotations))
1903 (defun bookmark-bmenu-edit-annotation ()
1904 "Edit the annotation for the current bookmark in another window."
1905 (interactive)
1906 (let ((bookmark (bookmark-bmenu-bookmark)))
1907 (bookmark-edit-annotation bookmark)))
1910 (defun bookmark-bmenu-unmark (&optional backup)
1911 "Cancel all requested operations on bookmark on this line and move down.
1912 Optional BACKUP means move up."
1913 (interactive "P")
1914 (beginning-of-line)
1915 (bookmark-bmenu-ensure-position)
1916 (with-buffer-modified-unmodified
1917 (let ((inhibit-read-only t))
1918 (delete-char 1)
1919 ;; any flags to reset according to circumstances? How about a
1920 ;; flag indicating whether this bookmark is being visited?
1921 ;; well, we don't have this now, so maybe later.
1922 (insert " "))
1923 (forward-line (if backup -1 1))
1924 (bookmark-bmenu-ensure-position)))
1927 (defun bookmark-bmenu-backup-unmark ()
1928 "Move up and cancel all requested operations on bookmark on line above."
1929 (interactive)
1930 (forward-line -1)
1931 (bookmark-bmenu-ensure-position)
1932 (bookmark-bmenu-unmark)
1933 (forward-line -1)
1934 (bookmark-bmenu-ensure-position))
1937 (defun bookmark-bmenu-delete ()
1938 "Mark bookmark on this line to be deleted.
1939 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1940 (interactive)
1941 (beginning-of-line)
1942 (bookmark-bmenu-ensure-position)
1943 (with-buffer-modified-unmodified
1944 (let ((inhibit-read-only t))
1945 (delete-char 1)
1946 (insert ?D)
1947 (forward-line 1)
1948 (bookmark-bmenu-ensure-position))))
1951 (defun bookmark-bmenu-delete-backwards ()
1952 "Mark bookmark on this line to be deleted, then move up one line.
1953 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
1954 (interactive)
1955 (bookmark-bmenu-delete)
1956 (forward-line -2)
1957 (bookmark-bmenu-ensure-position)
1958 (forward-line 1)
1959 (bookmark-bmenu-ensure-position))
1962 (defun bookmark-bmenu-execute-deletions ()
1963 "Delete bookmarks flagged `D'."
1964 (interactive)
1965 (message "Deleting bookmarks...")
1966 (let ((o-point (point))
1967 (o-str (save-excursion
1968 (beginning-of-line)
1969 (unless (looking-at "^D")
1970 (buffer-substring
1971 (point)
1972 (progn (end-of-line) (point))))))
1973 (o-col (current-column)))
1974 (goto-char (point-min))
1975 (forward-line 1)
1976 (while (re-search-forward "^D" (point-max) t)
1977 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1978 (bookmark-bmenu-list)
1979 (if o-str
1980 (progn
1981 (goto-char (point-min))
1982 (search-forward o-str)
1983 (beginning-of-line)
1984 (forward-char o-col))
1985 (goto-char o-point))
1986 (beginning-of-line)
1987 (message "Deleting bookmarks...done")
1991 (defun bookmark-bmenu-rename ()
1992 "Rename bookmark on current line. Prompts for a new name."
1993 (interactive)
1994 (let ((bmrk (bookmark-bmenu-bookmark))
1995 (thispoint (point)))
1996 (bookmark-rename bmrk)
1997 (goto-char thispoint)))
2000 (defun bookmark-bmenu-locate ()
2001 "Display location of this bookmark. Displays in the minibuffer."
2002 (interactive)
2003 (let ((bmrk (bookmark-bmenu-bookmark)))
2004 (message "%s" (bookmark-location bmrk))))
2006 (defun bookmark-bmenu-relocate ()
2007 "Change the file path of the bookmark on the current line,
2008 prompting with completion for the new path."
2009 (interactive)
2010 (let ((bmrk (bookmark-bmenu-bookmark))
2011 (thispoint (point)))
2012 (bookmark-relocate bmrk)
2013 (goto-char thispoint)))
2015 ;;; Bookmark-bmenu search
2017 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2018 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2019 (let ((bookmark-alist
2020 (cl-loop for i in bookmark-alist
2021 when (string-match regexp (car i)) collect i into new
2022 finally return new)))
2023 (bookmark-bmenu-list)))
2026 ;;;###autoload
2027 (defun bookmark-bmenu-search ()
2028 "Incremental search of bookmarks, hiding the non-matches as we go."
2029 (interactive)
2030 (let ((bmk (bookmark-bmenu-bookmark))
2031 (timer nil))
2032 (unwind-protect
2033 (minibuffer-with-setup-hook
2034 (lambda ()
2035 (setq timer (run-with-idle-timer
2036 bookmark-search-delay 'repeat
2037 #'(lambda (buf)
2038 (with-current-buffer buf
2039 (bookmark-bmenu-filter-alist-by-regexp
2040 (minibuffer-contents))))
2041 (current-buffer))))
2042 (read-string "Pattern: ")
2043 (when timer (cancel-timer timer) (setq timer nil)))
2044 (when timer ;; Signalled an error or a `quit'.
2045 (cancel-timer timer)
2046 (bookmark-bmenu-list)
2047 (bookmark-bmenu-goto-bookmark bmk)))))
2049 (defun bookmark-bmenu-goto-bookmark (name)
2050 "Move point to bookmark with name NAME."
2051 (goto-char (point-min))
2052 (while (not (equal name (bookmark-bmenu-bookmark)))
2053 (forward-line 1))
2054 (forward-line 0))
2058 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2060 (defun bookmark-menu-popup-paned-menu (event name entries)
2061 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2062 That is, ENTRIES is a list of strings which appear as the choices
2063 in the menu.
2064 The number of panes depends on the number of entries.
2065 The visible entries are truncated to `bookmark-menu-length', but the
2066 strings returned are not."
2067 (let ((f-height (/ (frame-height) 2))
2068 (pane-list nil)
2069 (iter 0))
2070 (while entries
2071 (let (lst
2072 (count 0))
2073 (while (and (< count f-height) entries)
2074 (let ((str (car entries)))
2075 (push (cons
2076 (if (> (length str) bookmark-menu-length)
2077 (substring str 0 bookmark-menu-length)
2078 str)
2079 str)
2080 lst)
2081 (setq entries (cdr entries))
2082 (setq count (1+ count))))
2083 (setq iter (1+ iter))
2084 (push (cons
2085 (format "-*- %s (%d) -*-" name iter)
2086 (nreverse lst))
2087 pane-list)))
2089 ;; Popup the menu and return the string.
2090 (x-popup-menu event (cons (concat "-*- " name " -*-")
2091 (nreverse pane-list)))))
2094 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2095 ;; following works, and for explaining what to do to make it work.
2097 ;; We MUST autoload EACH form used to set up this variable's value, so
2098 ;; that the whole job is done in loaddefs.el.
2100 ;; Emacs menubar stuff.
2102 ;;;###autoload
2103 (defvar menu-bar-bookmark-map
2104 (let ((map (make-sparse-keymap "Bookmark functions")))
2105 (bindings--define-key map [load]
2106 '(menu-item "Load a Bookmark File..." bookmark-load
2107 :help "Load bookmarks from a bookmark file)"))
2108 (bindings--define-key map [write]
2109 '(menu-item "Save Bookmarks As..." bookmark-write
2110 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2111 (bindings--define-key map [save]
2112 '(menu-item "Save Bookmarks" bookmark-save
2113 :help "Save currently defined bookmarks"))
2114 (bindings--define-key map [edit]
2115 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2116 :help "Display a list of existing bookmarks"))
2117 (bindings--define-key map [delete]
2118 '(menu-item "Delete Bookmark..." bookmark-delete
2119 :help "Delete a bookmark from the bookmark list"))
2120 (bindings--define-key map [rename]
2121 '(menu-item "Rename Bookmark..." bookmark-rename
2122 :help "Change the name of a bookmark"))
2123 (bindings--define-key map [locate]
2124 '(menu-item "Insert Location..." bookmark-locate
2125 :help "Insert the name of the file associated with a bookmark"))
2126 (bindings--define-key map [insert]
2127 '(menu-item "Insert Contents..." bookmark-insert
2128 :help "Insert the text of the file pointed to by a bookmark"))
2129 (bindings--define-key map [set]
2130 '(menu-item "Set Bookmark..." bookmark-set
2131 :help "Set a bookmark named inside a file."))
2132 (bindings--define-key map [jump]
2133 '(menu-item "Jump to Bookmark..." bookmark-jump
2134 :help "Jump to a bookmark (a point in some file)"))
2135 map))
2137 ;;;###autoload
2138 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2140 ;; make bookmarks appear toward the right side of the menu.
2141 (if (boundp 'menu-bar-final-items)
2142 (if menu-bar-final-items
2143 (push 'bookmark menu-bar-final-items))
2144 (setq menu-bar-final-items '(bookmark)))
2146 ;;;; end bookmark menu stuff ;;;;
2149 ;; Load Hook
2150 (defvar bookmark-load-hook nil
2151 "Hook run at the end of loading library `bookmark.el'.")
2153 ;; Exit Hook, called from kill-emacs-hook
2154 (define-obsolete-variable-alias 'bookmark-exit-hooks
2155 'bookmark-exit-hook "22.1")
2156 (defvar bookmark-exit-hook nil
2157 "Hook run when Emacs exits.")
2159 (defun bookmark-exit-hook-internal ()
2160 "Save bookmark state, if necessary, at Emacs exit time.
2161 This also runs `bookmark-exit-hook'."
2162 (run-hooks 'bookmark-exit-hook)
2163 (and bookmark-alist
2164 (bookmark-time-to-save-p t)
2165 (bookmark-save)))
2167 (unless noninteractive
2168 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2170 (defun bookmark-unload-function ()
2171 "Unload the Bookmark library."
2172 (when bookmark-save-flag (bookmark-save))
2173 ;; continue standard unloading
2174 nil)
2177 (run-hooks 'bookmark-load-hook)
2179 (provide 'bookmark)
2181 ;;; bookmark.el ends here