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