Merge branch 'master' into comment-cache
[emacs.git] / lisp / bookmark.el
blob02dd8a9f6fcb552bec0560f69cf9c5f040ab441a
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
3 ;; Copyright (C) 1993-1997, 2001-2017 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'."
78 :type '(choice (const nil) integer (other t))
79 :group 'bookmark)
82 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
83 "The `.emacs.bmk' file used to be called this name.")
86 ;; defvared to avoid a compilation warning:
87 (defvar bookmark-file nil
88 "Old name for `bookmark-default-file'.")
90 (defcustom bookmark-default-file
91 (if bookmark-file
92 ;; In case user set `bookmark-file' in her .emacs:
93 bookmark-file
94 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
95 "File in which to save bookmarks by default."
96 :type 'file
97 :group 'bookmark)
100 (defcustom bookmark-version-control 'nospecial
101 "Whether or not to make numbered backups of the bookmark file.
102 It can have four values: t, nil, `never', or `nospecial'.
103 The first three have the same meaning that they do for the
104 variable `version-control'; the value `nospecial' (the default) means
105 just use the value of `version-control'."
106 :type '(choice (const :tag "If existing" nil)
107 (const :tag "Never" never)
108 (const :tag "Use value of option `version-control'" nospecial)
109 (other :tag "Always" t))
110 :group 'bookmark)
113 (defcustom bookmark-completion-ignore-case t
114 "Non-nil means bookmark functions ignore case in completion."
115 :type 'boolean
116 :group 'bookmark)
119 (defcustom bookmark-sort-flag t
120 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
121 Otherwise they will be displayed in LIFO order (that is, most
122 recently set ones come first, oldest ones come last)."
123 :type 'boolean
124 :group 'bookmark)
127 (defcustom bookmark-automatically-show-annotations t
128 "Non-nil means show annotations when jumping to a bookmark."
129 :type 'boolean
130 :group 'bookmark)
132 (defcustom bookmark-bmenu-use-header-line t
133 "Non-nil means to use an immovable header line.
134 This is as opposed to inline text at the top of the buffer."
135 :version "24.4"
136 :type 'boolean
137 :group 'bookmark)
139 (defconst bookmark-bmenu-inline-header-height 2
140 "Number of lines used for the *Bookmark List* header
141 \(only significant when `bookmark-bmenu-use-header-line' is nil).")
143 (defconst bookmark-bmenu-marks-width 2
144 "Number of columns (chars) used for the *Bookmark List* marks column,
145 including the annotations column.")
147 (defcustom bookmark-bmenu-file-column 30
148 "Column at which to display filenames in a buffer listing bookmarks.
149 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
150 :type 'integer
151 :group 'bookmark)
154 (defcustom bookmark-bmenu-toggle-filenames t
155 "Non-nil means show filenames when listing bookmarks.
156 A non-nil value may result in truncated bookmark names."
157 :type 'boolean
158 :group 'bookmark)
160 (defface bookmark-menu-bookmark
161 '((t (:weight bold)))
162 "Face used to highlight bookmark names in bookmark menu buffers."
163 :group 'bookmark)
165 (defcustom bookmark-menu-length 70
166 "Maximum length of a bookmark name displayed on a popup menu."
167 :type 'integer
168 :group 'bookmark)
170 ;; FIXME: Is it really worth a customization option?
171 (defcustom bookmark-search-delay 0.2
172 "Time before `bookmark-bmenu-search' updates the display."
173 :group 'bookmark
174 :type 'number)
176 (defface bookmark-menu-heading
177 '((t (:inherit font-lock-type-face)))
178 "Face used to highlight the heading in bookmark menu buffers."
179 :group 'bookmark
180 :version "22.1")
183 ;;; No user-serviceable parts beyond this point.
185 ;; Added for lucid emacs compatibility, db
186 (or (fboundp 'defalias) (fset 'defalias 'fset))
188 ;; suggested for lucid compatibility by david hughes:
189 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
192 ;;; Keymap stuff:
194 ;; Set up these bindings dumping time *only*;
195 ;; if the user alters them, don't override the user when loading bookmark.el.
197 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
198 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
199 ;;;###autoload (define-key ctl-x-r-map "M" 'bookmark-set-no-overwrite)
200 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
202 ;;;###autoload
203 (defvar bookmark-map
204 (let ((map (make-sparse-keymap)))
205 ;; Read the help on all of these functions for details...
206 (define-key map "x" 'bookmark-set)
207 (define-key map "m" 'bookmark-set) ;"m"ark
208 (define-key map "M" 'bookmark-set-no-overwrite) ;"M"aybe mark
209 (define-key map "j" 'bookmark-jump)
210 (define-key map "g" 'bookmark-jump) ;"g"o
211 (define-key map "o" 'bookmark-jump-other-window)
212 (define-key map "i" 'bookmark-insert)
213 (define-key map "e" 'edit-bookmarks)
214 (define-key map "f" 'bookmark-insert-location) ;"f"ind
215 (define-key map "r" 'bookmark-rename)
216 (define-key map "d" 'bookmark-delete)
217 (define-key map "l" 'bookmark-load)
218 (define-key map "w" 'bookmark-write)
219 (define-key map "s" 'bookmark-save)
220 map)
221 "Keymap containing bindings to bookmark functions.
222 It is not bound to any key by default: to bind it
223 so that you have a bookmark prefix, just use `global-set-key' and bind a
224 key of your choice to `bookmark-map'. All interactive bookmark
225 functions have a binding in this keymap.")
227 ;;;###autoload (fset 'bookmark-map bookmark-map)
230 ;;; Core variables and data structures:
231 (defvar bookmark-alist ()
232 "Association list of bookmarks and their records.
233 Bookmark functions update the value automatically.
234 You probably do NOT want to change the value yourself.
236 The value is an alist with entries of the form
238 (BOOKMARK-NAME . PARAM-ALIST)
240 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
242 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
244 PARAM-ALIST is an alist of bookmark information. The order of the
245 entries in PARAM-ALIST is not important. The possible entries are
246 described below. An entry with a key but null value means the entry
247 is not used.
249 (filename . FILENAME)
250 (position . POS)
251 (front-context-string . STR-AFTER-POS)
252 (rear-context-string . STR-BEFORE-POS)
253 (handler . HANDLER)
254 (annotation . ANNOTATION)
256 FILENAME names the bookmarked file.
257 POS is the bookmarked buffer position (position in the file).
258 STR-AFTER-POS is buffer text that immediately follows POS.
259 STR-BEFORE-POS is buffer text that immediately precedes POS.
260 ANNOTATION is a string that describes the bookmark.
261 See options `bookmark-use-annotations' and
262 `bookmark-automatically-show-annotations'.
263 HANDLER is a function that provides the bookmark-jump behavior for a
264 specific kind of bookmark. This is the case for Info bookmarks,
265 for instance. HANDLER must accept a bookmark as argument.")
267 (defvar bookmarks-already-loaded nil
268 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
270 (defvar bookmark-file-coding-system nil
271 "The coding-system of the last loaded or saved bookmark file.")
273 ;; more stuff added by db.
275 (defvar bookmark-current-bookmark nil
276 "Name of bookmark most recently used in the current file.
277 It is buffer local, used to make moving a bookmark forward
278 through a file easier.")
280 (make-variable-buffer-local 'bookmark-current-bookmark)
283 (defvar bookmark-alist-modification-count 0
284 "Number of modifications to bookmark list since it was last saved.")
287 (defvar bookmark-search-size 16
288 "Length of the context strings recorded on either side of a bookmark.")
291 (defvar bookmark-current-buffer nil
292 "The buffer in which a bookmark is currently being set or renamed.
293 Functions that insert strings into the minibuffer use this to know
294 the source buffer for that information; see `bookmark-yank-word'
295 for example.")
298 (defvar bookmark-yank-point 0
299 "The next point from which to pull source text for `bookmark-yank-word'.
300 This point is in `bookmark-current-buffer'.")
303 (defvar bookmark-quit-flag nil
304 "Non nil make `bookmark-bmenu-search' quit immediately.")
306 ;; Helper functions and macros.
308 (defmacro with-buffer-modified-unmodified (&rest body)
309 "Run BODY while preserving the buffer's `buffer-modified-p' state."
310 (let ((was-modified (make-symbol "was-modified")))
311 `(let ((,was-modified (buffer-modified-p)))
312 (unwind-protect
313 (progn ,@body)
314 (set-buffer-modified-p ,was-modified)))))
316 ;; Only functions below, in this page and the next one (file formats),
317 ;; need to know anything about the format of bookmark-alist entries.
318 ;; Everyone else should go through them.
320 (defun bookmark-name-from-full-record (bookmark-record)
321 "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
322 one element from `bookmark-alist'."
323 (car bookmark-record))
326 (defun bookmark-all-names ()
327 "Return a list of all current bookmark names."
328 (bookmark-maybe-load-default-file)
329 (mapcar 'bookmark-name-from-full-record bookmark-alist))
332 (defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
333 "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
334 If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
335 bookmark record in `bookmark-alist'; return it if found, otherwise
336 error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
337 just return it."
338 (cond
339 ((consp bookmark-name-or-record) bookmark-name-or-record)
340 ((stringp bookmark-name-or-record)
341 (or (assoc-string bookmark-name-or-record bookmark-alist
342 bookmark-completion-ignore-case)
343 (unless noerror (error "Invalid bookmark %s"
344 bookmark-name-or-record))))))
347 (defun bookmark-get-bookmark-record (bookmark-name-or-record)
348 "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
349 `bookmark-alist' (that is, all information but the name)."
350 (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
351 ;; The bookmark objects can either look like (NAME ALIST) or
352 ;; (NAME . ALIST), so we have to distinguish the two here.
353 (if (and (null (cdr alist)) (consp (caar alist)))
354 (car alist) alist)))
357 (defun bookmark-set-name (bookmark-name-or-record newname)
358 "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
359 (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
361 (defun bookmark-prop-get (bookmark-name-or-record prop)
362 "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
363 (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
365 (defun bookmark-prop-set (bookmark-name-or-record prop val)
366 "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
367 (let ((cell (assq
368 prop (bookmark-get-bookmark-record bookmark-name-or-record))))
369 (if cell
370 (setcdr cell val)
371 (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
372 (list (cons prop val))))))
374 (defun bookmark-get-annotation (bookmark-name-or-record)
375 "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
376 (bookmark-prop-get bookmark-name-or-record 'annotation))
378 (defun bookmark-set-annotation (bookmark-name-or-record ann)
379 "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
380 (bookmark-prop-set bookmark-name-or-record 'annotation ann))
383 (defun bookmark-get-filename (bookmark-name-or-record)
384 "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
385 (bookmark-prop-get bookmark-name-or-record 'filename))
388 (defun bookmark-set-filename (bookmark-name-or-record filename)
389 "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
390 (bookmark-prop-set bookmark-name-or-record 'filename filename))
393 (defun bookmark-get-position (bookmark-name-or-record)
394 "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
395 (bookmark-prop-get bookmark-name-or-record 'position))
398 (defun bookmark-set-position (bookmark-name-or-record position)
399 "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
400 (bookmark-prop-set bookmark-name-or-record 'position position))
403 (defun bookmark-get-front-context-string (bookmark-name-or-record)
404 "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
405 (bookmark-prop-get bookmark-name-or-record 'front-context-string))
408 (defun bookmark-set-front-context-string (bookmark-name-or-record string)
409 "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
410 (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
413 (defun bookmark-get-rear-context-string (bookmark-name-or-record)
414 "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
415 (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
418 (defun bookmark-set-rear-context-string (bookmark-name-or-record string)
419 "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
420 (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
423 (defun bookmark-get-handler (bookmark-name-or-record)
424 "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
425 (bookmark-prop-get bookmark-name-or-record 'handler))
427 (defvar bookmark-history nil
428 "The history list for bookmark functions.")
431 (defun bookmark-completing-read (prompt &optional default)
432 "Prompting with PROMPT, read a bookmark name in completion.
433 PROMPT will get a \": \" stuck on the end no matter what, so you
434 probably don't want to include one yourself.
435 Optional arg DEFAULT is a string to return if the user input is empty.
436 If DEFAULT is nil then return empty string for empty input."
437 (bookmark-maybe-load-default-file) ; paranoia
438 (if (listp last-nonmenu-event)
439 (bookmark-menu-popup-paned-menu t prompt
440 (if bookmark-sort-flag
441 (sort (bookmark-all-names)
442 'string-lessp)
443 (bookmark-all-names)))
444 (let* ((completion-ignore-case bookmark-completion-ignore-case)
445 (default (unless (equal "" default) default))
446 (prompt (concat prompt (if default
447 (format " (%s): " default)
448 ": "))))
449 (completing-read prompt
450 (lambda (string pred action)
451 (if (eq action 'metadata)
452 '(metadata (category . bookmark))
453 (complete-with-action
454 action bookmark-alist string pred)))
455 nil 0 nil 'bookmark-history default))))
458 (defmacro bookmark-maybe-historicize-string (string)
459 "Put STRING into the bookmark prompt history, if caller non-interactive.
460 We need this because sometimes bookmark functions are invoked from
461 menus, so `completing-read' never gets a chance to set `bookmark-history'."
462 `(or
463 (called-interactively-p 'interactive)
464 (setq bookmark-history (cons ,string bookmark-history))))
466 (defvar bookmark-make-record-function 'bookmark-make-record-default
467 "A function that should be called to create a bookmark record.
468 Modes may set this variable buffer-locally to enable bookmarking of
469 locations that should be treated specially, such as Info nodes,
470 news posts, images, pdf documents, etc.
472 The function will be called with no arguments.
473 It should signal a user error if it is unable to construct a record for
474 the current location.
476 The returned record should be a cons cell of the form (NAME . ALIST)
477 where ALIST is as described in `bookmark-alist' and may typically contain
478 a special cons (handler . HANDLER-FUNC) which specifies the handler function
479 that should be used instead of `bookmark-default-handler' to open this
480 bookmark. See the documentation for `bookmark-alist' for more.
482 NAME is a suggested name for the constructed bookmark. It can be nil
483 in which case a default heuristic will be used. The function can also
484 equivalently just return ALIST without NAME.")
486 (defun bookmark-make-record ()
487 "Return a new bookmark record (NAME . ALIST) for the current location."
488 (let ((record (funcall bookmark-make-record-function)))
489 ;; Set up default name if the function does not provide one.
490 (unless (stringp (car record))
491 (if (car record) (push nil record))
492 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name))))
493 ;; Set up defaults.
494 (bookmark-prop-set
495 record 'defaults
496 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
497 (list bookmark-current-bookmark
498 (car record)
499 (bookmark-buffer-name))))))
500 record))
502 (defun bookmark-store (name alist no-overwrite)
503 "Store the bookmark NAME with data ALIST.
504 If NO-OVERWRITE is non-nil and another bookmark of the same name already
505 exists in `bookmark-alist', record the new bookmark without throwing away the
506 old one."
507 (bookmark-maybe-load-default-file)
508 (let ((stripped-name (copy-sequence name)))
509 (or (featurep 'xemacs)
510 ;; XEmacs's `set-text-properties' doesn't work on
511 ;; free-standing strings, apparently.
512 (set-text-properties 0 (length stripped-name) nil stripped-name))
513 (if (and (not no-overwrite)
514 (bookmark-get-bookmark stripped-name 'noerror))
515 ;; already existing bookmark under that name and
516 ;; no prefix arg means just overwrite old bookmark
517 ;; Use the new (NAME . ALIST) format.
518 (setcdr (bookmark-get-bookmark stripped-name) alist)
520 ;; otherwise just cons it onto the front (either the bookmark
521 ;; doesn't exist already, or there is no prefix arg. In either
522 ;; case, we want the new bookmark consed onto the alist...)
524 (push (cons stripped-name alist) bookmark-alist))
526 ;; Added by db
527 (setq bookmark-current-bookmark stripped-name)
528 (setq bookmark-alist-modification-count
529 (1+ bookmark-alist-modification-count))
530 (if (bookmark-time-to-save-p)
531 (bookmark-save))
533 (setq bookmark-current-bookmark stripped-name)
534 (bookmark-bmenu-surreptitiously-rebuild-list)))
536 (defun bookmark-make-record-default (&optional no-file no-context posn)
537 "Return the record describing the location of a new bookmark.
538 Point should be at the buffer in which the bookmark is being set,
539 and normally should be at the position where the bookmark is desired,
540 but see the optional arguments for other possibilities.
542 If NO-FILE is non-nil, then only return the subset of the
543 record that pertains to the location within the buffer, leaving off
544 the part that records the filename.
546 If NO-CONTEXT is non-nil, do not include the front- and rear-context
547 strings in the record -- the position is enough.
549 If POSN is non-nil, record POSN as the point instead of `(point)'."
550 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
551 ,@(unless no-context `((front-context-string
552 . ,(if (>= (- (point-max) (point))
553 bookmark-search-size)
554 (buffer-substring-no-properties
555 (point)
556 (+ (point) bookmark-search-size))
557 nil))))
558 ,@(unless no-context `((rear-context-string
559 . ,(if (>= (- (point) (point-min))
560 bookmark-search-size)
561 (buffer-substring-no-properties
562 (point)
563 (- (point) bookmark-search-size))
564 nil))))
565 (position . ,(or posn (point)))))
568 ;;; File format stuff
570 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
571 ;; the bookmark file format -- please don't. The current format
572 ;; should be extensible enough. If you feel the need to change it,
573 ;; please discuss it with other Emacs developers first.
575 ;; The format of `bookmark-alist' has changed twice in its lifetime.
576 ;; This comment describes the three formats, FIRST, SECOND, and
577 ;; CURRENT.
579 ;; The FIRST format was used prior to Emacs 20:
581 ;; ((BOOKMARK-NAME (FILENAME
582 ;; STRING-IN-FRONT
583 ;; STRING-BEHIND
584 ;; POINT))
585 ;; ...)
587 ;; The SECOND format was introduced in Emacs 20:
589 ;; ((BOOKMARK-NAME ((filename . FILENAME)
590 ;; (position . POS)
591 ;; (front-context-string . STR-AFTER-POS)
592 ;; (rear-context-string . STR-BEFORE-POS)
593 ;; (annotation . ANNOTATION)
594 ;; (whatever . VALUE)
595 ;; ...
596 ;; ))
597 ;; ...)
599 ;; The CURRENT format was introduced in Emacs 22:
601 ;; ((BOOKMARK-NAME (filename . FILENAME)
602 ;; (position . POS)
603 ;; (front-context-string . STR-AFTER-POS)
604 ;; (rear-context-string . STR-BEFORE-POS)
605 ;; (annotation . ANNOTATION)
606 ;; (whatever . VALUE)
607 ;; ...
608 ;; )
609 ;; ...)
611 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
612 ;; bookmark record is a list of entry information. FIRST and SECOND
613 ;; differ in the form of the record information: FIRST uses a list of
614 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
615 ;; the list elements matters. In the SECOND format, the order of the
616 ;; alist elements is unimportant. The SECOND format facilitates the
617 ;; addition of new kinds of elements, to support new kinds of
618 ;; bookmarks or code evolution.
620 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
621 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
622 ;; no longer a cons. Why that change was made remains a mystery --
623 ;; just be aware of it. (Be aware too that this explanatory comment
624 ;; was incorrect in Emacs 22 and Emacs 23.1.)
626 ;; To deal with the change from FIRST format to SECOND, conversion
627 ;; code was added, and it is still in use. See
628 ;; `bookmark-maybe-upgrade-file-format'.
630 ;; No conversion from SECOND to CURRENT is done. Instead, the code
631 ;; handles both formats OK. It must continue to do so.
633 ;; See the doc string of `bookmark-alist' for information about the
634 ;; elements that define a bookmark (e.g. `filename').
637 (defconst bookmark-file-format-version 1
638 "The current version of the format used by bookmark files.
639 You should never need to change this.")
642 (defconst bookmark-end-of-version-stamp-marker
643 "-*- End Of Bookmark File Format Version Stamp -*-\n"
644 "This string marks the end of the version stamp in a bookmark file.")
647 (defun bookmark-alist-from-buffer ()
648 "Return a `bookmark-alist' (in any format) from the current buffer.
649 The buffer must of course contain bookmark format information.
650 Does not care from where in the buffer it is called, and does not
651 affect point."
652 (save-excursion
653 (goto-char (point-min))
654 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
655 (read (current-buffer))
656 ;; Else we're dealing with format version 0
657 (if (search-forward "(" nil t)
658 (progn
659 (forward-char -1)
660 (read (current-buffer)))
661 ;; Else no hope of getting information here.
662 (error "Not bookmark format")))))
665 (defun bookmark-upgrade-version-0-alist (old-list)
666 "Upgrade a version 0 alist OLD-LIST to the current version."
667 (mapcar
668 (lambda (bookmark)
669 (let* ((name (car bookmark))
670 (record (car (cdr bookmark)))
671 (filename (nth 0 record))
672 (front-str (nth 1 record))
673 (rear-str (nth 2 record))
674 (position (nth 3 record))
675 (ann (nth 4 record)))
676 (list
677 name
678 `((filename . ,filename)
679 (front-context-string . ,(or front-str ""))
680 (rear-context-string . ,(or rear-str ""))
681 (position . ,position)
682 (annotation . ,ann)))))
683 old-list))
686 (defun bookmark-upgrade-file-format-from-0 ()
687 "Upgrade a bookmark file of format 0 (the original format) to format 1.
688 This expects to be called from `point-min' in a bookmark file."
689 (message "Upgrading bookmark format from 0 to %d..."
690 bookmark-file-format-version)
691 (let* ((old-list (bookmark-alist-from-buffer))
692 (new-list (bookmark-upgrade-version-0-alist old-list)))
693 (delete-region (point-min) (point-max))
694 (bookmark-insert-file-format-version-stamp buffer-file-coding-system)
695 (pp new-list (current-buffer))
696 (save-buffer))
697 (goto-char (point-min))
698 (message "Upgrading bookmark format from 0 to %d...done"
699 bookmark-file-format-version)
703 (defun bookmark-grok-file-format-version ()
704 "Return an integer which is the file-format version of this bookmark file.
705 This expects to be called from `point-min' in a bookmark file."
706 (if (looking-at "^;;;;")
707 (save-excursion
708 (save-match-data
709 (re-search-forward "[0-9]")
710 (forward-char -1)
711 (read (current-buffer))))
712 ;; Else this is format version 0, the original one, which didn't
713 ;; even have version stamps.
717 (defun bookmark-maybe-upgrade-file-format ()
718 "Check the file-format version of this bookmark file.
719 If the version is not up-to-date, upgrade it automatically.
720 This expects to be called from `point-min' in a bookmark file."
721 (let ((version (bookmark-grok-file-format-version)))
722 (cond
723 ((= version bookmark-file-format-version)
724 ) ; home free -- version is current
725 ((= version 0)
726 (bookmark-upgrade-file-format-from-0))
728 (error "Bookmark file format version strangeness")))))
731 (defun bookmark-insert-file-format-version-stamp (coding)
732 "Insert text indicating current version of bookmark file format.
733 CODING is the symbol of the coding-system in which the file is encoded."
734 (if (memq (coding-system-base coding) '(undecided prefer-utf-8))
735 (setq coding 'utf-8-emacs))
736 (insert
737 (format ";;;; Emacs Bookmark Format Version %d ;;;; -*- coding: %S -*- \n"
738 bookmark-file-format-version (coding-system-base coding)))
739 (insert ";;; This format is meant to be slightly human-readable;\n"
740 ";;; nevertheless, you probably don't want to edit it.\n"
741 ";;; "
742 bookmark-end-of-version-stamp-marker))
745 ;;; end file-format stuff
748 ;;; Generic helpers.
750 (defun bookmark-maybe-message (fmt &rest args)
751 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
752 (if (>= baud-rate 9600)
753 (apply 'message fmt args)))
756 ;;; Core code:
758 (defvar bookmark-minibuffer-read-name-map
759 (let ((map (make-sparse-keymap)))
760 (set-keymap-parent map minibuffer-local-map)
761 (define-key map "\C-w" 'bookmark-yank-word)
762 map))
764 (defun bookmark-set-internal (prompt name overwrite-or-push)
765 "Interactively set a bookmark named NAME at the current location.
767 Begin the interactive prompt with PROMPT, followed by a space, a
768 generated default name in parentheses, a colon and a space.
770 If OVERWRITE-OR-PUSH is nil, then error if there is already a
771 bookmark named NAME; if `overwrite', then replace any existing
772 bookmark if there is one; if `push' then push the new bookmark
773 onto the bookmark alist. The `push' behavior means that among
774 bookmarks named NAME, this most recently set one becomes the one in
775 effect, but the others are still there, in order, if the topmost one
776 is ever deleted."
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 "%s (default: \"%s\"): " prompt default)
807 bookmark-minibuffer-read-name-map
808 nil nil defaults))))
809 (and (string-equal str "") (setq str default))
811 (cond
812 ((eq overwrite-or-push nil)
813 (if (bookmark-get-bookmark str t)
814 (error "A bookmark named \"%s\" already exists." str)
815 (bookmark-store str (cdr record) nil)))
816 ((eq overwrite-or-push 'overwrite)
817 (bookmark-store str (cdr record) nil))
818 ((eq overwrite-or-push 'push)
819 (bookmark-store str (cdr record) t))
821 (error "Unrecognized value for `overwrite-or-push': %S"
822 overwrite-or-push)))
824 ;; Ask for an annotation buffer for this bookmark
825 (when bookmark-use-annotations
826 (bookmark-edit-annotation str))))
827 (setq bookmark-yank-point nil)
828 (setq bookmark-current-buffer nil)))
831 ;;;###autoload
832 (defun bookmark-set (&optional name no-overwrite)
833 "Set a bookmark named NAME at the current location.
834 If NAME is nil, then prompt the user.
836 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
837 existing bookmark that has the same name as NAME, but instead push the
838 new bookmark onto the bookmark alist. The most recently set bookmark
839 with name NAME is thus the one in effect at any given time, but the
840 others are still there, should the user decide to delete the most
841 recent one.
843 To yank words from the text of the buffer and use them as part of the
844 bookmark name, type C-w while setting a bookmark. Successive C-w's
845 yank successive words.
847 Typing C-u inserts (at the bookmark name prompt) the name of the last
848 bookmark used in the document where the new bookmark is being set;
849 this helps you use a single bookmark name to track progress through a
850 large document. If there is no prior bookmark for this document, then
851 C-u inserts an appropriate name based on the buffer or file.
853 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
854 it removes only the first instance of a bookmark with that name from
855 the list of bookmarks.)"
856 (interactive (list nil current-prefix-arg))
857 (let ((prompt
858 (if no-overwrite "Set bookmark" "Set bookmark unconditionally")))
859 (bookmark-set-internal prompt name (if no-overwrite 'push 'overwrite))))
861 ;;;###autoload
862 (defun bookmark-set-no-overwrite (&optional name push-bookmark)
863 "Set a bookmark named NAME at the current location.
864 If NAME is nil, then prompt the user.
866 If a bookmark named NAME already exists and prefix argument
867 PUSH-BOOKMARK is non-nil, then push the new bookmark onto the
868 bookmark alist. Pushing it means that among bookmarks named
869 NAME, this one becomes the one in effect, but the others are
870 still there, in order, and become effective again if the user
871 ever deletes the most recent one.
873 Otherwise, if a bookmark named NAME already exists but PUSH-BOOKMARK
874 is nil, raise an error.
876 To yank words from the text of the buffer and use them as part of the
877 bookmark name, type C-w while setting a bookmark. Successive C-w's
878 yank successive words.
880 Typing C-u inserts (at the bookmark name prompt) the name of the last
881 bookmark used in the document where the new bookmark is being set;
882 this helps you use a single bookmark name to track progress through a
883 large document. If there is no prior bookmark for this document, then
884 C-u inserts an appropriate name based on the buffer or file.
886 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
887 it removes only the first instance of a bookmark with that name from
888 the list of bookmarks.)"
889 (interactive (list nil current-prefix-arg))
890 (bookmark-set-internal "Set bookmark" name (if push-bookmark 'push nil)))
893 (defun bookmark-kill-line (&optional newline-too)
894 "Kill from point to end of line.
895 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
896 Does not affect the kill ring."
897 (let ((eol (line-end-position)))
898 (delete-region (point) eol)
899 (if (and newline-too (looking-at "\n"))
900 (delete-char 1))))
903 ;; Defvars to avoid compilation warnings:
904 (defvar bookmark-annotation-name nil
905 "Variable holding the name of the bookmark.
906 This is used in `bookmark-edit-annotation' to record the bookmark
907 whose annotation is being edited.")
910 (defun bookmark-default-annotation-text (bookmark-name)
911 "Return default annotation text for BOOKMARK-NAME.
912 The default annotation text is simply some text explaining how to use
913 annotations."
914 (concat (format-message
915 "# Type the annotation for bookmark `%s' here.\n"
916 bookmark-name)
917 (format-message
918 "# All lines which start with a `#' will be deleted.\n")
919 "# Type C-c C-c when done.\n#\n"
920 "# Author: " (user-full-name) " <" (user-login-name) "@"
921 (system-name) ">\n"
922 "# Date: " (current-time-string) "\n"))
925 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
926 'bookmark-edit-annotation-text-func "23.1")
927 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
928 "Function to return default text to use for a bookmark annotation.
929 It takes one argument, the name of the bookmark, as a string.")
931 (defvar bookmark-edit-annotation-mode-map
932 (let ((map (make-sparse-keymap)))
933 (set-keymap-parent map text-mode-map)
934 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
935 map)
936 "Keymap for editing an annotation of a bookmark.")
938 (defun bookmark-insert-annotation (bookmark-name-or-record)
939 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
940 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
941 (if (and annotation (not (string-equal annotation "")))
942 (insert annotation))))
944 (define-derived-mode bookmark-edit-annotation-mode
945 text-mode "Edit Bookmark Annotation"
946 "Mode for editing the annotation of bookmarks.
947 When you have finished composing, type \\[bookmark-send-annotation].
949 \\{bookmark-edit-annotation-mode-map}")
952 (defun bookmark-send-edited-annotation ()
953 "Use buffer contents as annotation for a bookmark.
954 Lines beginning with `#' are ignored."
955 (interactive)
956 (if (not (derived-mode-p 'bookmark-edit-annotation-mode))
957 (error "Not in bookmark-edit-annotation-mode"))
958 (goto-char (point-min))
959 (while (< (point) (point-max))
960 (if (looking-at "^#")
961 (bookmark-kill-line t)
962 (forward-line 1)))
963 ;; Take no chances with text properties.
964 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
965 (bookmark-name bookmark-annotation-name))
966 (bookmark-set-annotation bookmark-name annotation)
967 (setq bookmark-alist-modification-count
968 (1+ bookmark-alist-modification-count))
969 (bookmark-bmenu-surreptitiously-rebuild-list))
970 (kill-buffer (current-buffer)))
973 (defun bookmark-edit-annotation (bookmark-name-or-record)
974 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
975 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
976 (bookmark-insert-annotation bookmark-name-or-record)
977 (bookmark-edit-annotation-mode)
978 (set (make-local-variable 'bookmark-annotation-name)
979 bookmark-name-or-record))
982 (defun bookmark-buffer-name ()
983 "Return the name of the current buffer in a form usable as a bookmark name.
984 If the buffer is associated with a file or directory, use that name."
985 (cond
986 ;; Or are we a file?
987 (buffer-file-name (file-name-nondirectory buffer-file-name))
988 ;; Or are we a directory?
989 ((and (boundp 'dired-directory) dired-directory)
990 (let* ((dirname (if (stringp dired-directory)
991 dired-directory
992 (car dired-directory)))
993 (idx (1- (length dirname))))
994 ;; Strip the trailing slash.
995 (if (= ?/ (aref dirname idx))
996 (file-name-nondirectory (substring dirname 0 idx))
997 ;; Else return the current-buffer
998 (buffer-name (current-buffer)))))
999 ;; If all else fails, use the buffer's name.
1001 (buffer-name (current-buffer)))))
1004 (defun bookmark-yank-word ()
1005 "Get the next word from buffer `bookmark-current-buffer' and append
1006 it to the name of the bookmark currently being set, advancing
1007 `bookmark-yank-point' by one word."
1008 (interactive)
1009 (let ((string (with-current-buffer bookmark-current-buffer
1010 (goto-char bookmark-yank-point)
1011 (buffer-substring-no-properties
1012 (point)
1013 (progn
1014 (forward-word 1)
1015 (setq bookmark-yank-point (point)))))))
1016 (insert string)))
1018 (defun bookmark-buffer-file-name ()
1019 "Return the current buffer's file in a way useful for bookmarks."
1020 ;; Abbreviate the path, both so it's shorter and so it's more
1021 ;; portable. E.g., the user's home dir might be a different
1022 ;; path on different machines, but "~/" will still reach it.
1023 (abbreviate-file-name
1024 (cond
1025 (buffer-file-name buffer-file-name)
1026 ((and (boundp 'dired-directory) dired-directory)
1027 (if (stringp dired-directory)
1028 dired-directory
1029 (car dired-directory)))
1030 (t (error "Buffer not visiting a file or directory")))))
1033 (defun bookmark-maybe-load-default-file ()
1034 "If bookmarks have not been loaded from the default place, load them."
1035 (and (not bookmarks-already-loaded)
1036 (null bookmark-alist)
1037 (prog2
1038 (and
1039 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1040 ;; to be renamed.
1041 (file-exists-p bookmark-old-default-file)
1042 (not (file-exists-p bookmark-default-file))
1043 (rename-file bookmark-old-default-file
1044 bookmark-default-file))
1045 ;; return t so the `and' will continue...
1048 (file-readable-p bookmark-default-file)
1049 (bookmark-load bookmark-default-file t t)
1050 (setq bookmarks-already-loaded t)))
1053 (defun bookmark-maybe-sort-alist ()
1054 "Return `bookmark-alist' for display.
1055 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
1056 (if bookmark-sort-flag
1057 (sort (copy-alist bookmark-alist)
1058 (function
1059 (lambda (x y) (string-lessp (car x) (car y)))))
1060 bookmark-alist))
1063 (defvar bookmark-after-jump-hook nil
1064 "Hook run after `bookmark-jump' jumps to a bookmark.
1065 Useful for example to unhide text in `outline-mode'.")
1067 (defun bookmark--jump-via (bookmark-name-or-record display-function)
1068 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
1069 current buffer as argument.
1071 After calling DISPLAY-FUNCTION, set window point to the point specified
1072 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1073 and then show any annotations for this bookmark."
1074 (bookmark-handle-bookmark bookmark-name-or-record)
1075 (save-current-buffer
1076 (funcall display-function (current-buffer)))
1077 (let ((win (get-buffer-window (current-buffer) 0)))
1078 (if win (set-window-point win (point))))
1079 ;; FIXME: we used to only run bookmark-after-jump-hook in
1080 ;; `bookmark-jump' itself, but in none of the other commands.
1081 (run-hooks 'bookmark-after-jump-hook)
1082 (if bookmark-automatically-show-annotations
1083 ;; if there is an annotation for this bookmark,
1084 ;; show it in a buffer.
1085 (bookmark-show-annotation bookmark-name-or-record)))
1088 ;;;###autoload
1089 (defun bookmark-jump (bookmark &optional display-func)
1090 "Jump to bookmark BOOKMARK (a point in some file).
1091 You may have a problem using this function if the value of variable
1092 `bookmark-alist' is nil. If that happens, you need to load in some
1093 bookmarks. See help on function `bookmark-load' for more about
1094 this.
1096 If the file pointed to by BOOKMARK no longer exists, you will be asked
1097 if you wish to give the bookmark a new location, and `bookmark-jump'
1098 will then jump to the new location, as well as recording it in place
1099 of the old one in the permanent bookmark record.
1101 BOOKMARK is usually a bookmark name (a string). It can also be a
1102 bookmark record, but this is usually only done by programmatic callers.
1104 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1105 bookmark. It defaults to `switch-to-buffer'. A typical value for
1106 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1107 (interactive
1108 (list (bookmark-completing-read "Jump to bookmark"
1109 bookmark-current-bookmark)))
1110 (unless bookmark
1111 (error "No bookmark specified"))
1112 (bookmark-maybe-historicize-string bookmark)
1113 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1116 ;;;###autoload
1117 (defun bookmark-jump-other-window (bookmark)
1118 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1119 (interactive
1120 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1121 bookmark-current-bookmark)))
1122 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1125 (defun bookmark-jump-noselect (bookmark)
1126 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1127 The return value has the form (BUFFER . POINT).
1129 Note: this function is deprecated and is present for Emacs 22
1130 compatibility only."
1131 (declare (obsolete bookmark-handle-bookmark "23.1"))
1132 (save-excursion
1133 (bookmark-handle-bookmark bookmark)
1134 (cons (current-buffer) (point))))
1136 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1137 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1138 if it has none. This changes current buffer and point and returns nil,
1139 or signals a `file-error'.
1141 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1142 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1143 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1144 (condition-case err
1145 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1146 'bookmark-default-handler)
1147 (bookmark-get-bookmark bookmark-name-or-record))
1148 (bookmark-error-no-filename ;file-error
1149 ;; We were unable to find the marked file, so ask if user wants to
1150 ;; relocate the bookmark, else remind them to consider deletion.
1151 (when (stringp bookmark-name-or-record)
1152 ;; `bookmark-name-or-record' can be either a bookmark name
1153 ;; (from `bookmark-alist') or a bookmark object. If it's an
1154 ;; object, we assume it's a bookmark used internally by some
1155 ;; other package.
1156 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1157 (when file ;Don't know how to relocate if there's no `file'.
1158 ;; If file is not a dir, directory-file-name just returns file.
1159 (let ((display-name (directory-file-name file)))
1160 (ding)
1161 ;; Dialog boxes can accept a file target, but usually don't
1162 ;; know how to accept a directory target (at least, this
1163 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1164 ;; true on Windows as well). So we suppress file dialogs
1165 ;; when relocating.
1166 (let ((use-dialog-box nil)
1167 (use-file-dialog nil))
1168 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1169 bookmark-name-or-record "\"? "))
1170 (progn
1171 (bookmark-relocate bookmark-name-or-record)
1172 ;; Try again.
1173 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1174 'bookmark-default-handler)
1175 (bookmark-get-bookmark bookmark-name-or-record)))
1176 (message
1177 "Bookmark not relocated; consider removing it (%s)."
1178 bookmark-name-or-record)
1179 (signal (car err) (cdr err))))))))))
1180 ;; Added by db.
1181 (when (stringp bookmark-name-or-record)
1182 (setq bookmark-current-bookmark bookmark-name-or-record))
1183 nil)
1185 (define-error 'bookmark-errors nil)
1186 (define-error 'bookmark-error-no-filename
1187 "Bookmark has no associated file (or directory)" 'bookmark-errors)
1189 (defun bookmark-default-handler (bmk-record)
1190 "Default handler to jump to a particular bookmark location.
1191 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1192 Changes current buffer and point and returns nil, or signals a `file-error'."
1193 (let ((file (bookmark-get-filename bmk-record))
1194 (buf (bookmark-prop-get bmk-record 'buffer))
1195 (forward-str (bookmark-get-front-context-string bmk-record))
1196 (behind-str (bookmark-get-rear-context-string bmk-record))
1197 (place (bookmark-get-position bmk-record)))
1198 (set-buffer
1199 (cond
1200 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1201 (find-file-noselect file))
1202 ;; No file found. See if buffer BUF have been created.
1203 ((and buf (get-buffer buf)))
1204 (t ;; If not, raise error.
1205 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1206 (if place (goto-char place))
1207 ;; Go searching forward first. Then, if forward-str exists and
1208 ;; was found in the file, we can search backward for behind-str.
1209 ;; Rationale is that if text was inserted between the two in the
1210 ;; file, it's better to be put before it so you can read it,
1211 ;; rather than after and remain perhaps unaware of the changes.
1212 (when (and forward-str (search-forward forward-str (point-max) t))
1213 (goto-char (match-beginning 0)))
1214 (when (and behind-str (search-backward behind-str (point-min) t))
1215 (goto-char (match-end 0)))
1216 nil))
1218 ;;;###autoload
1219 (defun bookmark-relocate (bookmark-name)
1220 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1222 This makes an already existing bookmark point to that file, instead of
1223 the one it used to point at. Useful when a file has been renamed
1224 after a bookmark was set in it."
1225 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1226 (bookmark-maybe-historicize-string bookmark-name)
1227 (bookmark-maybe-load-default-file)
1228 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1229 (newloc (abbreviate-file-name
1230 (expand-file-name
1231 (read-file-name
1232 (format "Relocate %s to: " bookmark-name)
1233 (file-name-directory bmrk-filename))))))
1234 (bookmark-set-filename bookmark-name newloc)
1235 (setq bookmark-alist-modification-count
1236 (1+ bookmark-alist-modification-count))
1237 (if (bookmark-time-to-save-p)
1238 (bookmark-save))
1239 (bookmark-bmenu-surreptitiously-rebuild-list)))
1242 ;;;###autoload
1243 (defun bookmark-insert-location (bookmark-name &optional no-history)
1244 "Insert the name of the file associated with BOOKMARK-NAME.
1246 Optional second arg NO-HISTORY means don't record this in the
1247 minibuffer history list `bookmark-history'."
1248 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1249 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1250 (insert (bookmark-location bookmark-name)))
1252 ;;;###autoload
1253 (defalias 'bookmark-locate 'bookmark-insert-location)
1255 (defun bookmark-location (bookmark-name-or-record)
1256 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1257 (bookmark-maybe-load-default-file)
1258 ;; We could call the `handler' and ask for it to construct a description
1259 ;; dynamically: it would open up several new possibilities, but it
1260 ;; would have the major disadvantage of forcing to load each and
1261 ;; every handler when the user calls bookmark-menu.
1262 (or (bookmark-prop-get bookmark-name-or-record 'location)
1263 (bookmark-get-filename bookmark-name-or-record)
1264 "-- Unknown location --"))
1267 ;;;###autoload
1268 (defun bookmark-rename (old-name &optional new-name)
1269 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1270 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1271 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1273 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1274 as an argument. If called with two strings, then no prompting is done.
1275 You must pass at least OLD-NAME when calling from Lisp.
1277 While you are entering the new name, consecutive C-w's insert
1278 consecutive words from the text of the buffer into the new bookmark
1279 name."
1280 (interactive (list (bookmark-completing-read "Old bookmark name")))
1281 (bookmark-maybe-historicize-string old-name)
1282 (bookmark-maybe-load-default-file)
1284 (setq bookmark-yank-point (point))
1285 (setq bookmark-current-buffer (current-buffer))
1286 (let ((final-new-name
1287 (or new-name ; use second arg, if non-nil
1288 (read-from-minibuffer
1289 "New name: "
1291 (let ((now-map (copy-keymap minibuffer-local-map)))
1292 (define-key now-map "\C-w" 'bookmark-yank-word)
1293 now-map)
1295 'bookmark-history))))
1296 (bookmark-set-name old-name final-new-name)
1297 (setq bookmark-current-bookmark final-new-name)
1298 (bookmark-bmenu-surreptitiously-rebuild-list)
1299 (setq bookmark-alist-modification-count
1300 (1+ bookmark-alist-modification-count))
1301 (if (bookmark-time-to-save-p)
1302 (bookmark-save))))
1305 ;;;###autoload
1306 (defun bookmark-insert (bookmark-name)
1307 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1308 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1310 You may have a problem using this function if the value of variable
1311 `bookmark-alist' is nil. If that happens, you need to load in some
1312 bookmarks. See help on function `bookmark-load' for more about
1313 this."
1314 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1315 (bookmark-maybe-historicize-string bookmark-name)
1316 (bookmark-maybe-load-default-file)
1317 (let ((orig-point (point))
1318 (str-to-insert
1319 (save-current-buffer
1320 (bookmark-handle-bookmark bookmark-name)
1321 (buffer-string))))
1322 (insert str-to-insert)
1323 (push-mark)
1324 (goto-char orig-point)))
1327 ;;;###autoload
1328 (defun bookmark-delete (bookmark-name &optional batch)
1329 "Delete BOOKMARK-NAME from the bookmark list.
1331 Removes only the first instance of a bookmark with that name. If
1332 there are one or more other bookmarks with the same name, they will
1333 not be deleted. Defaults to the \"current\" bookmark (that is, the
1334 one most recently used in this file, if any).
1335 Optional second arg BATCH means don't update the bookmark list buffer,
1336 probably because we were called from there."
1337 (interactive
1338 (list (bookmark-completing-read "Delete bookmark"
1339 bookmark-current-bookmark)))
1340 (bookmark-maybe-historicize-string bookmark-name)
1341 (bookmark-maybe-load-default-file)
1342 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1343 (setq bookmark-alist (delq will-go bookmark-alist))
1344 ;; Added by db, nil bookmark-current-bookmark if the last
1345 ;; occurrence has been deleted
1346 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1347 (setq bookmark-current-bookmark nil)))
1348 (unless batch
1349 (bookmark-bmenu-surreptitiously-rebuild-list))
1350 (setq bookmark-alist-modification-count
1351 (1+ bookmark-alist-modification-count))
1352 (when (bookmark-time-to-save-p)
1353 (bookmark-save)))
1356 (defun bookmark-time-to-save-p (&optional final-time)
1357 "Return t if it is time to save bookmarks to disk, nil otherwise.
1358 Optional argument FINAL-TIME means this is being called when Emacs
1359 is being killed, so save even if `bookmark-save-flag' is a number and
1360 is greater than `bookmark-alist-modification-count'."
1361 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1362 (cond (final-time
1363 (and (> bookmark-alist-modification-count 0)
1364 bookmark-save-flag))
1365 ((numberp bookmark-save-flag)
1366 (>= bookmark-alist-modification-count bookmark-save-flag))
1368 nil)))
1371 ;;;###autoload
1372 (defun bookmark-write ()
1373 "Write bookmarks to a file (reading the file name with the minibuffer)."
1374 (declare (interactive-only bookmark-save))
1375 (interactive)
1376 (bookmark-maybe-load-default-file)
1377 (bookmark-save t))
1380 ;;;###autoload
1381 (defun bookmark-save (&optional parg file)
1382 "Save currently defined bookmarks.
1383 Saves by default in the file defined by the variable
1384 `bookmark-default-file'. With a prefix arg, save it in file FILE
1385 \(second argument).
1387 If you are calling this from Lisp, the two arguments are PARG and
1388 FILE, and if you just want it to write to the default file, then
1389 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1390 instead. If you pass in one argument, and it is non-nil, then the
1391 user will be interactively queried for a file to save in.
1393 When you want to load in the bookmarks from a file, use
1394 `bookmark-load', \\[bookmark-load]. That function will prompt you
1395 for a file, defaulting to the file defined by variable
1396 `bookmark-default-file'."
1397 (interactive "P")
1398 (bookmark-maybe-load-default-file)
1399 (cond
1400 ((and (null parg) (null file))
1401 ;;whether interactive or not, write to default file
1402 (bookmark-write-file bookmark-default-file))
1403 ((and (null parg) file)
1404 ;;whether interactive or not, write to given file
1405 (bookmark-write-file file))
1406 ((and parg (not file))
1407 ;;have been called interactively w/ prefix arg
1408 (let ((file (read-file-name "File to save bookmarks in: ")))
1409 (bookmark-write-file file)))
1410 (t ; someone called us with prefix-arg *and* a file, so just write to file
1411 (bookmark-write-file file)))
1412 ;; signal that we have synced the bookmark file by setting this to
1413 ;; 0. If there was an error at any point before, it will not get
1414 ;; set, which is what we want.
1415 (setq bookmark-alist-modification-count 0))
1419 (defun bookmark-write-file (file)
1420 "Write `bookmark-alist' to FILE."
1421 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1422 (with-current-buffer (get-buffer-create " *Bookmarks*")
1423 (goto-char (point-min))
1424 (delete-region (point-min) (point-max))
1425 (let ((coding-system-for-write
1426 (or coding-system-for-write
1427 bookmark-file-coding-system 'utf-8-emacs))
1428 (print-length nil)
1429 (print-level nil)
1430 ;; See bug #12503 for why we bind `print-circle'. Users
1431 ;; can define their own bookmark types, which can result in
1432 ;; arbitrary Lisp objects being stored in bookmark records,
1433 ;; and some users create objects containing circularities.
1434 (print-circle t))
1435 (insert "(")
1436 ;; Rather than a single call to `pp' we make one per bookmark.
1437 ;; Apparently `pp' has a poor algorithmic complexity, so this
1438 ;; scales a lot better. bug#4485.
1439 (dolist (i bookmark-alist) (pp i (current-buffer)))
1440 (insert ")")
1441 ;; Make sure the specified encoding can safely encode the
1442 ;; bookmarks. If it cannot, suggest utf-8-emacs as default.
1443 (with-coding-priority '(utf-8-emacs)
1444 (setq coding-system-for-write
1445 (select-safe-coding-system (point-min) (point-max)
1446 (list t coding-system-for-write))))
1447 (goto-char (point-min))
1448 (bookmark-insert-file-format-version-stamp coding-system-for-write)
1449 (let ((version-control
1450 (cond
1451 ((null bookmark-version-control) nil)
1452 ((eq 'never bookmark-version-control) 'never)
1453 ((eq 'nospecial bookmark-version-control) version-control)
1454 (t t))))
1455 (condition-case nil
1456 (write-region (point-min) (point-max) file)
1457 (file-error (message "Can't write %s" file)))
1458 (setq bookmark-file-coding-system coding-system-for-write)
1459 (kill-buffer (current-buffer))
1460 (bookmark-maybe-message
1461 "Saving bookmarks to file %s...done" file)))))
1464 (defun bookmark-import-new-list (new-list)
1465 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1466 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1467 they conflict with existing bookmark names."
1468 (let ((names (bookmark-all-names)))
1469 (dolist (full-record new-list)
1470 (bookmark-maybe-rename full-record names)
1471 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1472 (push (bookmark-name-from-full-record full-record) names))))
1475 (defun bookmark-maybe-rename (full-record names)
1476 "Rename bookmark FULL-RECORD if its current name is already used.
1477 This is a helper for `bookmark-import-new-list'."
1478 (let ((found-name (bookmark-name-from-full-record full-record)))
1479 (if (member found-name names)
1480 ;; We've got a conflict, so generate a new name
1481 (let ((count 2)
1482 (new-name found-name))
1483 (while (member new-name names)
1484 (setq new-name (concat found-name (format "<%d>" count)))
1485 (setq count (1+ count)))
1486 (bookmark-set-name full-record new-name)))))
1489 ;;;###autoload
1490 (defun bookmark-load (file &optional overwrite no-msg)
1491 "Load bookmarks from FILE (which must be in bookmark format).
1492 Appends loaded bookmarks to the front of the list of bookmarks. If
1493 optional second argument OVERWRITE is non-nil, existing bookmarks are
1494 destroyed. Optional third arg NO-MSG means don't display any messages
1495 while loading.
1497 If you load a file that doesn't contain a proper bookmark alist, you
1498 will corrupt Emacs's bookmark list. Generally, you should only load
1499 in files that were created with the bookmark functions in the first
1500 place. Your own personal bookmark file, specified by the variable
1501 `bookmark-default-file', is maintained automatically by Emacs; you
1502 shouldn't need to load it explicitly.
1504 If you load a file containing bookmarks with the same names as
1505 bookmarks already present in your Emacs, the new bookmarks will get
1506 unique numeric suffixes \"<2>\", \"<3>\", etc."
1507 (interactive
1508 (list (read-file-name
1509 (format "Load bookmarks from: (%s) "
1510 bookmark-default-file)
1511 ;;Default might not be used often,
1512 ;;but there's no better default, and
1513 ;;I guess it's better than none at all.
1514 "~/" bookmark-default-file 'confirm)))
1515 (setq file (abbreviate-file-name (expand-file-name file)))
1516 (if (not (file-readable-p file))
1517 (error "Cannot read bookmark file %s" file)
1518 (if (null no-msg)
1519 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1520 (with-current-buffer (let ((enable-local-variables nil))
1521 (find-file-noselect file))
1522 (goto-char (point-min))
1523 (bookmark-maybe-upgrade-file-format)
1524 (let ((blist (bookmark-alist-from-buffer)))
1525 (if (listp blist)
1526 (progn
1527 (if overwrite
1528 (progn
1529 (setq bookmark-alist blist)
1530 (setq bookmark-alist-modification-count 0))
1531 ;; else
1532 (bookmark-import-new-list blist)
1533 (setq bookmark-alist-modification-count
1534 (1+ bookmark-alist-modification-count)))
1535 (if (string-equal
1536 (abbreviate-file-name
1537 (expand-file-name bookmark-default-file))
1538 file)
1539 (setq bookmarks-already-loaded t))
1540 (bookmark-bmenu-surreptitiously-rebuild-list)
1541 (setq bookmark-file-coding-system buffer-file-coding-system))
1542 (error "Invalid bookmark list in %s" file)))
1543 (kill-buffer (current-buffer)))
1544 (if (null no-msg)
1545 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1549 ;;; Code supporting the dired-like bookmark menu.
1550 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1553 (defvar bookmark-bmenu-hidden-bookmarks ())
1556 (defvar bookmark-bmenu-mode-map
1557 (let ((map (make-keymap)))
1558 (set-keymap-parent map special-mode-map)
1559 (define-key map "v" 'bookmark-bmenu-select)
1560 (define-key map "w" 'bookmark-bmenu-locate)
1561 (define-key map "2" 'bookmark-bmenu-2-window)
1562 (define-key map "1" 'bookmark-bmenu-1-window)
1563 (define-key map "j" 'bookmark-bmenu-this-window)
1564 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1565 (define-key map "f" 'bookmark-bmenu-this-window)
1566 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1567 (define-key map "o" 'bookmark-bmenu-other-window)
1568 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1569 (define-key map "s" 'bookmark-bmenu-save)
1570 (define-key map "k" 'bookmark-bmenu-delete)
1571 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1572 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1573 (define-key map "d" 'bookmark-bmenu-delete)
1574 (define-key map " " 'next-line)
1575 (define-key map "n" 'next-line)
1576 (define-key map "p" 'previous-line)
1577 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1578 (define-key map "u" 'bookmark-bmenu-unmark)
1579 (define-key map "m" 'bookmark-bmenu-mark)
1580 (define-key map "l" 'bookmark-bmenu-load)
1581 (define-key map "r" 'bookmark-bmenu-rename)
1582 (define-key map "R" 'bookmark-bmenu-relocate)
1583 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1584 (define-key map "a" 'bookmark-bmenu-show-annotation)
1585 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1586 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1587 (define-key map "/" 'bookmark-bmenu-search)
1588 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1589 map))
1591 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1592 ;; data.
1593 (put 'bookmark-bmenu-mode 'mode-class 'special)
1596 ;; todo: need to display whether or not bookmark exists as a buffer in
1597 ;; flag column.
1599 ;; Format:
1600 ;; FLAGS BOOKMARK [ LOCATION ]
1603 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1604 "Rebuild the Bookmark List if it exists.
1605 Don't affect the buffer ring order."
1606 (if (get-buffer "*Bookmark List*")
1607 (save-excursion
1608 (save-window-excursion
1609 (bookmark-bmenu-list)))))
1612 ;;;###autoload
1613 (defun bookmark-bmenu-list ()
1614 "Display a list of existing bookmarks.
1615 The list is displayed in a buffer named `*Bookmark List*'.
1616 The leftmost column displays a D if the bookmark is flagged for
1617 deletion, or > if it is flagged for displaying."
1618 (interactive)
1619 (bookmark-maybe-load-default-file)
1620 (let ((buf (get-buffer-create "*Bookmark List*")))
1621 (if (called-interactively-p 'interactive)
1622 (switch-to-buffer buf)
1623 (set-buffer buf)))
1624 (let ((inhibit-read-only t))
1625 (erase-buffer)
1626 (if (not bookmark-bmenu-use-header-line)
1627 (insert "% Bookmark\n- --------\n"))
1628 (add-text-properties (point-min) (point)
1629 '(font-lock-face bookmark-menu-heading))
1630 (dolist (full-record (bookmark-maybe-sort-alist))
1631 (let ((name (bookmark-name-from-full-record full-record))
1632 (annotation (bookmark-get-annotation full-record))
1633 (start (point))
1634 end)
1635 ;; if a bookmark has an annotation, prepend a "*"
1636 ;; in the list of bookmarks.
1637 (insert (if (and annotation (not (string-equal annotation "")))
1638 " *" " ")
1639 name)
1640 (setq end (point))
1641 (put-text-property
1642 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1643 (when (display-mouse-p)
1644 (add-text-properties
1645 (+ bookmark-bmenu-marks-width start) end
1646 '(font-lock-face bookmark-menu-bookmark
1647 mouse-face highlight
1648 follow-link t
1649 help-echo "mouse-2: go to this bookmark in other window")))
1650 (insert "\n")))
1651 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1652 (goto-char (point-min))
1653 (bookmark-bmenu-mode)
1654 (if bookmark-bmenu-use-header-line
1655 (bookmark-bmenu-set-header)
1656 (forward-line bookmark-bmenu-inline-header-height))
1657 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1658 (bookmark-bmenu-toggle-filenames t))))
1660 ;;;###autoload
1661 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1662 ;;;###autoload
1663 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1665 (defun bookmark-bmenu-set-header ()
1666 "Sets the immutable header line."
1667 (let ((header (concat "%% " "Bookmark")))
1668 (when bookmark-bmenu-toggle-filenames
1669 (setq header (concat header
1670 (make-string (- bookmark-bmenu-file-column
1671 (- (length header) 3)) ?\s)
1672 "File")))
1673 (let ((pos 0))
1674 (while (string-match "[ \t\n]+" header pos)
1675 (setq pos (match-end 0))
1676 (put-text-property (match-beginning 0) pos 'display
1677 (list 'space :align-to (- pos 1))
1678 header)))
1679 (put-text-property 0 2 'face 'fixed-pitch header)
1680 (setq header (concat (propertize " " 'display '(space :align-to 0))
1681 header))
1682 ;; Code derived from `buff-menu.el'.
1683 (setq header-line-format header)))
1685 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1686 "Major mode for editing a list of bookmarks.
1687 Each line describes one of the bookmarks in Emacs.
1688 Letters do not insert themselves; instead, they are commands.
1689 Bookmark names preceded by a \"*\" have annotations.
1690 \\<bookmark-bmenu-mode-map>
1691 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1692 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1693 Also show bookmarks marked using m in other windows.
1694 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1695 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1696 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1697 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1698 together with bookmark selected before this one in another window.
1699 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1700 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1701 so the bookmark menu bookmark remains visible in its window.
1702 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1703 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1704 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1705 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1706 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1707 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1708 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1709 With a prefix arg, prompts for a file to save in.
1710 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1711 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1712 With prefix argument, also move up one line.
1713 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1714 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1715 in another buffer.
1716 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1717 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1718 (setq truncate-lines t)
1719 (setq buffer-read-only t))
1722 (defun bookmark-bmenu-toggle-filenames (&optional show)
1723 "Toggle whether filenames are shown in the bookmark list.
1724 Optional argument SHOW means show them unconditionally."
1725 (interactive)
1726 (cond
1727 (show
1728 (setq bookmark-bmenu-toggle-filenames nil)
1729 (bookmark-bmenu-show-filenames)
1730 (setq bookmark-bmenu-toggle-filenames t))
1731 (bookmark-bmenu-toggle-filenames
1732 (bookmark-bmenu-hide-filenames)
1733 (setq bookmark-bmenu-toggle-filenames nil))
1735 (bookmark-bmenu-show-filenames)
1736 (setq bookmark-bmenu-toggle-filenames t)))
1737 (when bookmark-bmenu-use-header-line
1738 (bookmark-bmenu-set-header)))
1741 (defun bookmark-bmenu-show-filenames (&optional force)
1742 "In an interactive bookmark list, show filenames along with bookmarks.
1743 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1744 mainly for debugging, and should not be necessary in normal use."
1745 (if (and (not force) bookmark-bmenu-toggle-filenames)
1746 nil ;already shown, so do nothing
1747 (with-buffer-modified-unmodified
1748 (save-excursion
1749 (save-window-excursion
1750 (goto-char (point-min))
1751 (if (not bookmark-bmenu-use-header-line)
1752 (forward-line bookmark-bmenu-inline-header-height))
1753 (setq bookmark-bmenu-hidden-bookmarks ())
1754 (let ((inhibit-read-only t))
1755 (while (< (point) (point-max))
1756 (let ((bmrk (bookmark-bmenu-bookmark)))
1757 (push bmrk bookmark-bmenu-hidden-bookmarks)
1758 (let ((start (line-end-position)))
1759 (move-to-column bookmark-bmenu-file-column t)
1760 ;; Strip off `mouse-face' from the white spaces region.
1761 (if (display-mouse-p)
1762 (remove-text-properties start (point)
1763 '(mouse-face nil help-echo nil))))
1764 (delete-region (point) (progn (end-of-line) (point)))
1765 (insert " ")
1766 ;; Pass the NO-HISTORY arg:
1767 (bookmark-insert-location bmrk t)
1768 (forward-line 1)))))))))
1771 (defun bookmark-bmenu-hide-filenames (&optional force)
1772 "In an interactive bookmark list, hide the filenames of the bookmarks.
1773 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1774 mainly for debugging, and should not be necessary in normal use."
1775 (when (and (not force) bookmark-bmenu-toggle-filenames)
1776 ;; nothing to hide if above is nil
1777 (with-buffer-modified-unmodified
1778 (save-excursion
1779 (goto-char (point-min))
1780 (if (not bookmark-bmenu-use-header-line)
1781 (forward-line bookmark-bmenu-inline-header-height))
1782 (setq bookmark-bmenu-hidden-bookmarks
1783 (nreverse bookmark-bmenu-hidden-bookmarks))
1784 (let ((inhibit-read-only t))
1785 (while bookmark-bmenu-hidden-bookmarks
1786 (move-to-column bookmark-bmenu-marks-width t)
1787 (bookmark-kill-line)
1788 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1789 (start (point)))
1790 (insert name)
1791 (put-text-property start (point) 'bookmark-name-prop name)
1792 (if (display-mouse-p)
1793 (add-text-properties
1794 start (point)
1795 '(font-lock-face bookmark-menu-bookmark
1796 mouse-face highlight
1797 follow-link t help-echo
1798 "mouse-2: go to this bookmark in other window"))))
1799 (forward-line 1)))))))
1802 (defun bookmark-bmenu-ensure-position ()
1803 "If point is not on a bookmark line, move it to one.
1804 If before the first bookmark line, move to the first; if after the
1805 last full line, move to the last full line. The return value is undefined."
1806 (cond ((and (not bookmark-bmenu-use-header-line)
1807 (< (count-lines (point-min) (point))
1808 bookmark-bmenu-inline-header-height))
1809 (goto-char (point-min))
1810 (forward-line bookmark-bmenu-inline-header-height))
1811 ((and (bolp) (eobp))
1812 (beginning-of-line 0))))
1815 (defun bookmark-bmenu-bookmark ()
1816 "Return the bookmark for this line in an interactive bookmark list buffer."
1817 (bookmark-bmenu-ensure-position)
1818 (save-excursion
1819 (beginning-of-line)
1820 (forward-char bookmark-bmenu-marks-width)
1821 (get-text-property (point) 'bookmark-name-prop)))
1824 (defun bookmark-show-annotation (bookmark-name-or-record)
1825 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1826 if an annotation exists."
1827 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1828 (when (and annotation (not (string-equal annotation "")))
1829 (save-excursion
1830 (let ((old-buf (current-buffer)))
1831 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1832 (delete-region (point-min) (point-max))
1833 (insert annotation)
1834 (goto-char (point-min))
1835 (switch-to-buffer-other-window old-buf))))))
1838 (defun bookmark-show-all-annotations ()
1839 "Display the annotations for all bookmarks in a buffer."
1840 (save-selected-window
1841 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1842 (delete-region (point-min) (point-max))
1843 (dolist (full-record (bookmark-maybe-sort-alist))
1844 (let* ((name (bookmark-name-from-full-record full-record))
1845 (ann (bookmark-get-annotation full-record)))
1846 (insert (concat name ":\n"))
1847 (if (and ann (not (string-equal ann "")))
1848 ;; insert the annotation, indented by 4 spaces.
1849 (progn
1850 (save-excursion (insert ann) (unless (bolp)
1851 (insert "\n")))
1852 (while (< (point) (point-max))
1853 (beginning-of-line) ; paranoia
1854 (insert " ")
1855 (forward-line)
1856 (end-of-line))))))
1857 (goto-char (point-min))))
1860 (defun bookmark-bmenu-mark ()
1861 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1862 (interactive)
1863 (beginning-of-line)
1864 (bookmark-bmenu-ensure-position)
1865 (with-buffer-modified-unmodified
1866 (let ((inhibit-read-only t))
1867 (delete-char 1)
1868 (insert ?>)
1869 (forward-line 1)
1870 (bookmark-bmenu-ensure-position))))
1873 (defun bookmark-bmenu-select ()
1874 "Select this line's bookmark; also display bookmarks marked with `>'.
1875 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1876 (interactive)
1877 (let ((bmrk (bookmark-bmenu-bookmark))
1878 (menu (current-buffer))
1879 (others ())
1880 tem)
1881 (goto-char (point-min))
1882 (while (re-search-forward "^>" nil t)
1883 (setq tem (bookmark-bmenu-bookmark))
1884 (let ((inhibit-read-only t))
1885 (delete-char -1)
1886 (insert ?\s))
1887 (or (string-equal tem bmrk)
1888 (member tem others)
1889 (setq others (cons tem others))))
1890 (setq others (nreverse others)
1891 tem (/ (1- (frame-height)) (1+ (length others))))
1892 (delete-other-windows)
1893 (bookmark-jump bmrk)
1894 (bury-buffer menu)
1895 (if others
1896 (while others
1897 (split-window nil tem)
1898 (other-window 1)
1899 (bookmark-jump (car others))
1900 (setq others (cdr others)))
1901 (other-window 1))))
1904 (defun bookmark-bmenu-any-marks ()
1905 "Return non-nil if any bookmarks are marked in the marks column."
1906 (save-excursion
1907 (goto-char (point-min))
1908 (bookmark-bmenu-ensure-position)
1909 (catch 'found-mark
1910 (while (not (eobp))
1911 (beginning-of-line)
1912 (if (looking-at "^\\S-")
1913 (throw 'found-mark t)
1914 (forward-line 1)))
1915 nil)))
1918 (defun bookmark-bmenu-save (parg)
1919 "Save the current list into a bookmark file.
1920 With a prefix arg, prompts for a file to save them in."
1921 (interactive "P")
1922 (save-excursion
1923 (save-window-excursion
1924 (bookmark-save parg)
1925 (set-buffer-modified-p nil))))
1928 (defun bookmark-bmenu-load ()
1929 "Load the bookmark file and rebuild the bookmark menu-buffer."
1930 (interactive)
1931 (bookmark-bmenu-ensure-position)
1932 (save-excursion
1933 (save-window-excursion
1934 ;; This will call `bookmark-bmenu-list'
1935 (call-interactively 'bookmark-load))))
1938 (defun bookmark-bmenu-1-window ()
1939 "Select this line's bookmark, alone, in full frame."
1940 (interactive)
1941 (bookmark-jump (bookmark-bmenu-bookmark))
1942 (bury-buffer (other-buffer))
1943 (delete-other-windows))
1946 (defun bookmark-bmenu-2-window ()
1947 "Select this line's bookmark, with previous buffer in second window."
1948 (interactive)
1949 (let ((bmrk (bookmark-bmenu-bookmark))
1950 (menu (current-buffer))
1951 (pop-up-windows t))
1952 (delete-other-windows)
1953 (switch-to-buffer (other-buffer) nil t)
1954 (bookmark--jump-via bmrk 'pop-to-buffer)
1955 (bury-buffer menu)))
1958 (defun bookmark-bmenu-this-window ()
1959 "Select this line's bookmark in this window."
1960 (interactive)
1961 (bookmark-jump (bookmark-bmenu-bookmark)))
1964 (defun bookmark-bmenu-other-window ()
1965 "Select this line's bookmark in other window, leaving bookmark menu visible."
1966 (interactive)
1967 (let ((bookmark (bookmark-bmenu-bookmark)))
1968 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1971 (defun bookmark-bmenu-switch-other-window ()
1972 "Make the other window select this line's bookmark.
1973 The current window remains selected."
1974 (interactive)
1975 (let ((bookmark (bookmark-bmenu-bookmark))
1976 (fun (lambda (b) (display-buffer b t))))
1977 (bookmark--jump-via bookmark fun)))
1979 (defun bookmark-bmenu-other-window-with-mouse (event)
1980 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1981 (interactive "e")
1982 (with-current-buffer (window-buffer (posn-window (event-end event)))
1983 (save-excursion
1984 (goto-char (posn-point (event-end event)))
1985 (bookmark-bmenu-other-window))))
1988 (defun bookmark-bmenu-show-annotation ()
1989 "Show the annotation for the current bookmark in another window."
1990 (interactive)
1991 (let ((bookmark (bookmark-bmenu-bookmark)))
1992 (bookmark-show-annotation bookmark)))
1995 (defun bookmark-bmenu-show-all-annotations ()
1996 "Show the annotation for all bookmarks in another window."
1997 (interactive)
1998 (bookmark-show-all-annotations))
2001 (defun bookmark-bmenu-edit-annotation ()
2002 "Edit the annotation for the current bookmark in another window."
2003 (interactive)
2004 (let ((bookmark (bookmark-bmenu-bookmark)))
2005 (bookmark-edit-annotation bookmark)))
2008 (defun bookmark-bmenu-unmark (&optional backup)
2009 "Cancel all requested operations on bookmark on this line and move down.
2010 Optional BACKUP means move up."
2011 (interactive "P")
2012 (beginning-of-line)
2013 (bookmark-bmenu-ensure-position)
2014 (with-buffer-modified-unmodified
2015 (let ((inhibit-read-only t))
2016 (delete-char 1)
2017 ;; any flags to reset according to circumstances? How about a
2018 ;; flag indicating whether this bookmark is being visited?
2019 ;; well, we don't have this now, so maybe later.
2020 (insert " "))
2021 (forward-line (if backup -1 1))
2022 (bookmark-bmenu-ensure-position)))
2025 (defun bookmark-bmenu-backup-unmark ()
2026 "Move up and cancel all requested operations on bookmark on line above."
2027 (interactive)
2028 (forward-line -1)
2029 (bookmark-bmenu-ensure-position)
2030 (bookmark-bmenu-unmark)
2031 (forward-line -1)
2032 (bookmark-bmenu-ensure-position))
2035 (defun bookmark-bmenu-delete ()
2036 "Mark bookmark on this line to be deleted.
2037 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2038 (interactive)
2039 (beginning-of-line)
2040 (bookmark-bmenu-ensure-position)
2041 (with-buffer-modified-unmodified
2042 (let ((inhibit-read-only t))
2043 (delete-char 1)
2044 (insert ?D)
2045 (forward-line 1)
2046 (bookmark-bmenu-ensure-position))))
2049 (defun bookmark-bmenu-delete-backwards ()
2050 "Mark bookmark on this line to be deleted, then move up one line.
2051 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2052 (interactive)
2053 (bookmark-bmenu-delete)
2054 (forward-line -2)
2055 (bookmark-bmenu-ensure-position)
2056 (forward-line 1)
2057 (bookmark-bmenu-ensure-position))
2060 (defun bookmark-bmenu-execute-deletions ()
2061 "Delete bookmarks flagged `D'."
2062 (interactive)
2063 (message "Deleting bookmarks...")
2064 (let ((o-point (point))
2065 (o-str (save-excursion
2066 (beginning-of-line)
2067 (unless (looking-at "^D")
2068 (buffer-substring
2069 (point)
2070 (progn (end-of-line) (point))))))
2071 (o-col (current-column)))
2072 (goto-char (point-min))
2073 (unless bookmark-bmenu-use-header-line
2074 (forward-line 1))
2075 (while (re-search-forward "^D" (point-max) t)
2076 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2077 (bookmark-bmenu-list)
2078 (if o-str
2079 (progn
2080 (goto-char (point-min))
2081 (search-forward o-str)
2082 (beginning-of-line)
2083 (forward-char o-col))
2084 (goto-char o-point))
2085 (beginning-of-line)
2086 (message "Deleting bookmarks...done")
2090 (defun bookmark-bmenu-rename ()
2091 "Rename bookmark on current line. Prompts for a new name."
2092 (interactive)
2093 (let ((bmrk (bookmark-bmenu-bookmark))
2094 (thispoint (point)))
2095 (bookmark-rename bmrk)
2096 (goto-char thispoint)))
2099 (defun bookmark-bmenu-locate ()
2100 "Display location of this bookmark. Displays in the minibuffer."
2101 (interactive)
2102 (let ((bmrk (bookmark-bmenu-bookmark)))
2103 (message "%s" (bookmark-location bmrk))))
2105 (defun bookmark-bmenu-relocate ()
2106 "Change the file path of the bookmark on the current line,
2107 prompting with completion for the new path."
2108 (interactive)
2109 (let ((bmrk (bookmark-bmenu-bookmark))
2110 (thispoint (point)))
2111 (bookmark-relocate bmrk)
2112 (goto-char thispoint)))
2114 ;;; Bookmark-bmenu search
2116 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2117 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2118 (let ((bookmark-alist
2119 (cl-loop for i in bookmark-alist
2120 when (string-match regexp (car i)) collect i into new
2121 finally return new)))
2122 (bookmark-bmenu-list)))
2125 ;;;###autoload
2126 (defun bookmark-bmenu-search ()
2127 "Incremental search of bookmarks, hiding the non-matches as we go."
2128 (interactive)
2129 (let ((bmk (bookmark-bmenu-bookmark))
2130 (timer nil))
2131 (unwind-protect
2132 (minibuffer-with-setup-hook
2133 (lambda ()
2134 (setq timer (run-with-idle-timer
2135 bookmark-search-delay 'repeat
2136 #'(lambda (buf)
2137 (with-current-buffer buf
2138 (bookmark-bmenu-filter-alist-by-regexp
2139 (minibuffer-contents))))
2140 (current-buffer))))
2141 (read-string "Pattern: ")
2142 (when timer (cancel-timer timer) (setq timer nil)))
2143 (when timer ;; Signaled an error or a `quit'.
2144 (cancel-timer timer)
2145 (bookmark-bmenu-list)
2146 (bookmark-bmenu-goto-bookmark bmk)))))
2148 (defun bookmark-bmenu-goto-bookmark (name)
2149 "Move point to bookmark with name NAME."
2150 (goto-char (point-min))
2151 (while (not (or (equal name (bookmark-bmenu-bookmark))
2152 (eobp)))
2153 (forward-line 1))
2154 (forward-line 0))
2158 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2160 (defun bookmark-menu-popup-paned-menu (event name entries)
2161 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2162 That is, ENTRIES is a list of strings which appear as the choices
2163 in the menu.
2164 The number of panes depends on the number of entries.
2165 The visible entries are truncated to `bookmark-menu-length', but the
2166 strings returned are not."
2167 (let ((f-height (/ (frame-height) 2))
2168 (pane-list nil)
2169 (iter 0))
2170 (while entries
2171 (let (lst
2172 (count 0))
2173 (while (and (< count f-height) entries)
2174 (let ((str (car entries)))
2175 (push (cons
2176 (if (> (length str) bookmark-menu-length)
2177 (substring str 0 bookmark-menu-length)
2178 str)
2179 str)
2180 lst)
2181 (setq entries (cdr entries))
2182 (setq count (1+ count))))
2183 (setq iter (1+ iter))
2184 (push (cons
2185 (format "-*- %s (%d) -*-" name iter)
2186 (nreverse lst))
2187 pane-list)))
2189 ;; Popup the menu and return the string.
2190 (x-popup-menu event (cons (concat "-*- " name " -*-")
2191 (nreverse pane-list)))))
2194 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2195 ;; following works, and for explaining what to do to make it work.
2197 ;; We MUST autoload EACH form used to set up this variable's value, so
2198 ;; that the whole job is done in loaddefs.el.
2200 ;; Emacs menubar stuff.
2202 ;;;###autoload
2203 (defvar menu-bar-bookmark-map
2204 (let ((map (make-sparse-keymap "Bookmark functions")))
2205 (bindings--define-key map [load]
2206 '(menu-item "Load a Bookmark File..." bookmark-load
2207 :help "Load bookmarks from a bookmark file)"))
2208 (bindings--define-key map [write]
2209 '(menu-item "Save Bookmarks As..." bookmark-write
2210 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2211 (bindings--define-key map [save]
2212 '(menu-item "Save Bookmarks" bookmark-save
2213 :help "Save currently defined bookmarks"))
2214 (bindings--define-key map [edit]
2215 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2216 :help "Display a list of existing bookmarks"))
2217 (bindings--define-key map [delete]
2218 '(menu-item "Delete Bookmark..." bookmark-delete
2219 :help "Delete a bookmark from the bookmark list"))
2220 (bindings--define-key map [rename]
2221 '(menu-item "Rename Bookmark..." bookmark-rename
2222 :help "Change the name of a bookmark"))
2223 (bindings--define-key map [locate]
2224 '(menu-item "Insert Location..." bookmark-locate
2225 :help "Insert the name of the file associated with a bookmark"))
2226 (bindings--define-key map [insert]
2227 '(menu-item "Insert Contents..." bookmark-insert
2228 :help "Insert the text of the file pointed to by a bookmark"))
2229 (bindings--define-key map [set]
2230 '(menu-item "Set Bookmark..." bookmark-set
2231 :help "Set a bookmark named inside a file."))
2232 (bindings--define-key map [jump]
2233 '(menu-item "Jump to Bookmark..." bookmark-jump
2234 :help "Jump to a bookmark (a point in some file)"))
2235 map))
2237 ;;;###autoload
2238 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2240 ;; make bookmarks appear toward the right side of the menu.
2241 (if (boundp 'menu-bar-final-items)
2242 (if menu-bar-final-items
2243 (push 'bookmark menu-bar-final-items))
2244 (setq menu-bar-final-items '(bookmark)))
2246 ;;;; end bookmark menu stuff ;;;;
2249 ;; Load Hook
2250 (defvar bookmark-load-hook nil
2251 "Hook run at the end of loading library `bookmark.el'.")
2253 ;; Exit Hook, called from kill-emacs-hook
2254 (define-obsolete-variable-alias 'bookmark-exit-hooks
2255 'bookmark-exit-hook "22.1")
2256 (defvar bookmark-exit-hook nil
2257 "Hook run when Emacs exits.")
2259 (defun bookmark-exit-hook-internal ()
2260 "Save bookmark state, if necessary, at Emacs exit time.
2261 This also runs `bookmark-exit-hook'."
2262 (run-hooks 'bookmark-exit-hook)
2263 (and (bookmark-time-to-save-p t)
2264 (bookmark-save)))
2266 (unless noninteractive
2267 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2269 (defun bookmark-unload-function ()
2270 "Unload the Bookmark library."
2271 (when bookmark-save-flag (bookmark-save))
2272 ;; continue standard unloading
2273 nil)
2276 (run-hooks 'bookmark-load-hook)
2278 (provide 'bookmark)
2280 ;;; bookmark.el ends here