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