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