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