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