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