comparison lisp/packages/bookmark.el @ 0:376386a54a3c r19-14

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