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