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