* bookmark.el
[bpt/emacs.git] / lisp / bookmark.el
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
2
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Karl Fogel <kfogel@red-bean.com>
7 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
8 ;; Created: July, 1993
9 ;; Keywords: bookmarks, placeholders, annotations
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This package is for setting "bookmarks" in files. A bookmark
29 ;; associates a string with a location in a certain file. Thus, you
30 ;; can navigate your way to that location by providing the string.
31 ;; See the "User Variables" section for customizations.
32
33 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
34 ;; then implementing the bookmark-current-bookmark idea. He even
35 ;; sent *patches*, bless his soul...
36
37 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
38 ;; fixing and improving bookmark-time-to-save-p.
39
40 ;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
41 ;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
42 ;; and the menu-bar).
43
44 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
45 ;; suggestions and the code to implement them (like
46 ;; bookmark-bmenu-check-position, and some of the Lucid compatibility
47 ;; stuff).
48
49 ;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
50 ;; for his eminently sensible suggestion to separate bookmark-jump
51 ;; into bookmark-jump and bookmark-jump-noselect, which made many
52 ;; other things cleaner as well.
53
54 ;; Thanks to Roland McGrath for encouragement and help with defining
55 ;; autoloads on the menu-bar.
56
57 ;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
58 ;; values in bookmark-jump and bookmark-set. Everybody please keep
59 ;; all the keystrokes they save thereby and send them to him at the
60 ;; end of each year :-) (No, seriously, thanks Jonathan!)
61
62 ;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
63 ;; thinking up the annotations feature and implementing it so well.
64
65 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
66 ;; <olstad@msc.edu>.
67
68 ;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
69 ;; reported and fixed.
70
71 ;; Thank you, Michael Kifer, for contributing the XEmacs support.
72
73 ;; Enough with the credits already, get on to the good stuff:
74
75 ;; FAVORITE CHINESE RESTAURANT:
76 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
77 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
78 \f
79 ;;; Code:
80
81 (require 'pp)
82 (eval-when-compile (require 'cl))
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 "Non-nil means 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 (defface bookmark-menu-heading
201 '((t (:inherit font-lock-type-face)))
202 "Face used to highlight the heading in bookmark menu buffers."
203 :group 'bookmark
204 :version "22.1")
205
206
207 ;;; No user-serviceable parts beyond this point.
208
209 ;; Added for lucid emacs compatibility, db
210 (or (fboundp 'defalias) (fset 'defalias 'fset))
211
212 ;; suggested for lucid compatibility by david hughes:
213 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
214
215 \f
216 ;;; Keymap stuff:
217
218 ;; Set up these bindings dumping time *only*;
219 ;; if the user alters them, don't override the user when loading bookmark.el.
220
221 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
222 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
223 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
224
225 ;;;###autoload
226 (defvar bookmark-map
227 (let ((map (make-sparse-keymap)))
228 ;; Read the help on all of these functions for details...
229 (define-key map "x" 'bookmark-set)
230 (define-key map "m" 'bookmark-set) ;"m"ark
231 (define-key map "j" 'bookmark-jump)
232 (define-key map "g" 'bookmark-jump) ;"g"o
233 (define-key map "o" 'bookmark-jump-other-window)
234 (define-key map "i" 'bookmark-insert)
235 (define-key map "e" 'edit-bookmarks)
236 (define-key map "f" 'bookmark-insert-location) ;"f"ind
237 (define-key map "r" 'bookmark-rename)
238 (define-key map "d" 'bookmark-delete)
239 (define-key map "l" 'bookmark-load)
240 (define-key map "w" 'bookmark-write)
241 (define-key map "s" 'bookmark-save)
242 map)
243 "Keymap containing bindings to bookmark functions.
244 It is not bound to any key by default: to bind it
245 so that you have a bookmark prefix, just use `global-set-key' and bind a
246 key of your choice to `bookmark-map'. All interactive bookmark
247 functions have a binding in this keymap.")
248
249 ;;;###autoload (fset 'bookmark-map bookmark-map)
250
251 \f
252 ;;; Core variables and data structures:
253 (defvar bookmark-alist ()
254 "Association list of bookmarks and their records.
255 You probably don't want to change the value of this alist yourself;
256 instead, let the various bookmark functions do it for you.
257
258 The format of the alist is
259
260 \(BOOKMARK1 BOOKMARK2 ...\)
261
262 where each BOOKMARK is of the form
263
264 (NAME PARAM-ALIST) or (NAME . PARAM-ALIST)
265
266 where the first form is the old deprecated one and the second is
267 the new favored one. PARAM-ALIST is typically of the form:
268
269 ((filename . FILE)
270 (front-context-string . FRONT-STR)
271 (rear-context-string . REAR-STR)
272 (position . POS)
273 (handler . HANDLER-FUNC)
274 (annotation . ANNOTATION))
275
276 If the element `(handler . HANDLER-FUNC)' is present, HANDLER-FUNC
277 will be used to open this bookmark instead of `bookmark-default-handler',
278 whose calling discipline HANDLER-FUNC should of course match.")
279
280
281 (defvar bookmarks-already-loaded nil)
282
283
284 ;; more stuff added by db.
285
286 (defvar bookmark-current-bookmark nil
287 "Name of bookmark most recently used in the current file.
288 It is buffer local, used to make moving a bookmark forward
289 through a file easier.")
290
291 (make-variable-buffer-local 'bookmark-current-bookmark)
292
293
294 (defvar bookmark-alist-modification-count 0
295 "Number of modifications to bookmark list since it was last saved.")
296
297
298 (defvar bookmark-search-size 16
299 "Length of the context strings recorded on either side of a bookmark.")
300
301
302 (defvar bookmark-current-point 0)
303 (defvar bookmark-yank-point 0)
304 (defvar bookmark-current-buffer nil)
305
306 (defvar Info-suffix-list)
307 \f
308 ;; Helper functions.
309
310 ;; Only functions on this page and the next one (file formats) need to
311 ;; know anything about the format of bookmark-alist entries.
312 ;; Everyone else should go through them.
313
314
315 (defun bookmark-name-from-full-record (full-record)
316 "Return name of FULL-RECORD \(an alist element instead of a string\)."
317 (car full-record))
318
319
320 (defun bookmark-all-names ()
321 "Return a list of all current bookmark names."
322 (bookmark-maybe-load-default-file)
323 (mapcar 'bookmark-name-from-full-record bookmark-alist))
324
325
326 (defun bookmark-get-bookmark (bookmark &optional noerror)
327 "Return the bookmark record corresponding to BOOKMARK.
328 If BOOKMARK is a string, look for the corresponding bookmark record in
329 `bookmark-alist'; return it if found, otherwise error. Else if
330 BOOKMARK is already a bookmark record, just return it."
331 (cond
332 ((consp bookmark) bookmark)
333 ((stringp bookmark)
334 (or (assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)
335 (unless noerror (error "Invalid bookmark %s" bookmark))))))
336
337
338 (defun bookmark-get-bookmark-record (bookmark)
339 "Return the record portion of the entry for BOOKMARK in
340 `bookmark-alist' (that is, all information but the name).
341 BOOKMARK may be a bookmark name (a string) or a bookmark record."
342 (let ((alist (cdr (bookmark-get-bookmark bookmark))))
343 ;; The bookmark objects can either look like (NAME ALIST) or
344 ;; (NAME . ALIST), so we have to distinguish the two here.
345 (if (and (null (cdr alist)) (consp (caar alist)))
346 (car alist) alist)))
347
348
349 (defun bookmark-set-name (bookmark newname)
350 "Set BOOKMARK's name to NEWNAME.
351 BOOKMARK may be a bookmark name (a string) or a bookmark record."
352 (setcar
353 (if (stringp bookmark) (bookmark-get-bookmark bookmark) bookmark)
354 newname))
355
356 (defun bookmark-prop-get (bookmark prop)
357 "Return the property PROP of BOOKMARK, or nil if none.
358 BOOKMARK may be a bookmark name (a string) or a bookmark record."
359 (cdr (assq prop (bookmark-get-bookmark-record bookmark))))
360
361 (defun bookmark-prop-set (bookmark prop val)
362 "Set the property PROP of BOOKMARK to VAL.
363 BOOKMARK may be a bookmark name (a string) or a bookmark record."
364 (let ((cell (assq prop (bookmark-get-bookmark-record bookmark))))
365 (if cell
366 (setcdr cell val)
367 (nconc (bookmark-get-bookmark-record bookmark)
368 (list (cons prop val))))))
369
370 (defun bookmark-get-annotation (bookmark)
371 "Return the annotation of BOOKMARK, or nil if none.
372 BOOKMARK may be a bookmark name (a string) or a bookmark record."
373 (bookmark-prop-get bookmark 'annotation))
374
375 (defun bookmark-set-annotation (bookmark ann)
376 "Set the annotation of BOOKMARK to ANN.
377 BOOKMARK may be a bookmark name (a string) or a bookmark record."
378 (bookmark-prop-set bookmark 'annotation ann))
379
380
381 (defun bookmark-get-filename (bookmark)
382 "Return the full filename of BOOKMARK, or nil if none.
383 BOOKMARK may be a bookmark name (a string) or a bookmark record."
384 (bookmark-prop-get bookmark 'filename))
385
386
387 (defun bookmark-set-filename (bookmark filename)
388 "Set the full filename of BOOKMARK to FILENAME.
389 BOOKMARK may be a bookmark name (a string) or a bookmark record."
390 (bookmark-prop-set bookmark 'filename filename))
391
392
393 (defun bookmark-get-position (bookmark)
394 "Return the position \(i.e.: point\) of BOOKMARK, or nil if none.
395 BOOKMARK may be a bookmark name (a string) or a bookmark record."
396 (bookmark-prop-get bookmark 'position))
397
398
399 (defun bookmark-set-position (bookmark position)
400 "Set the position \(i.e.: point\) of BOOKMARK to POSITION.
401 BOOKMARK may be a bookmark name (a string) or a bookmark record."
402 (bookmark-prop-set bookmark 'position position))
403
404
405 (defun bookmark-get-front-context-string (bookmark)
406 "Return the front-context-string of BOOKMARK, or nil if none.
407 BOOKMARK may be a bookmark name (a string) or a bookmark record."
408 (bookmark-prop-get bookmark 'front-context-string))
409
410
411 (defun bookmark-set-front-context-string (bookmark string)
412 "Set the front-context-string of BOOKMARK to STRING.
413 BOOKMARK may be a bookmark name (a string) or a bookmark record."
414 (bookmark-prop-set bookmark 'front-context-string string))
415
416
417 (defun bookmark-get-rear-context-string (bookmark)
418 "Return the rear-context-string of BOOKMARK, or nil if none.
419 BOOKMARK may be a bookmark name (a string) or a bookmark record."
420 (bookmark-prop-get bookmark 'rear-context-string))
421
422
423 (defun bookmark-set-rear-context-string (bookmark string)
424 "Set the rear-context-string of BOOKMARK to STRING.
425 BOOKMARK may be a bookmark name (a string) or a bookmark record."
426 (bookmark-prop-set bookmark 'rear-context-string string))
427
428
429 (defun bookmark-get-handler (bookmark)
430 "Return the handler function for BOOKMARK, or nil if none.
431 BOOKMARK may be a bookmark name (a string) or a bookmark record."
432 (bookmark-prop-get bookmark 'handler))
433
434 (defvar bookmark-history nil
435 "The history list for bookmark functions.")
436
437
438 (defun bookmark-completing-read (prompt &optional default)
439 "Prompting with PROMPT, read a bookmark name in completion.
440 PROMPT will get a \": \" stuck on the end no matter what, so you
441 probably don't want to include one yourself.
442 Optional second arg DEFAULT is a string to return if the user enters
443 the empty string."
444 (bookmark-maybe-load-default-file) ; paranoia
445 (if (listp last-nonmenu-event)
446 (bookmark-menu-popup-paned-menu t prompt (bookmark-all-names))
447 (let* ((completion-ignore-case bookmark-completion-ignore-case)
448 (default default)
449 (prompt (if default
450 (concat prompt (format " (%s): " default))
451 (concat prompt ": ")))
452 (str
453 (completing-read prompt
454 bookmark-alist
455 nil
456 0
457 nil
458 'bookmark-history)))
459 (if (string-equal "" str) default str))))
460
461
462 (defmacro bookmark-maybe-historicize-string (string)
463 "Put STRING into the bookmark prompt history, if caller non-interactive.
464 We need this because sometimes bookmark functions are invoked from
465 menus, so `completing-read' never gets a chance to set `bookmark-history'."
466 `(or
467 (called-interactively-p 'interactive)
468 (setq bookmark-history (cons ,string bookmark-history))))
469
470 (defvar bookmark-make-record-function 'bookmark-make-record-default
471 "A function that should be called to create a bookmark record.
472 Modes may set this variable buffer-locally to enable bookmarking of
473 locations that should be treated specially, such as Info nodes,
474 news posts, images, pdf documents, etc.
475
476 The function will be called with no arguments.
477 It should signal a user error if it is unable to construct a record for
478 the current location.
479
480 The returned record should be a cons cell of the form (NAME . ALIST)
481 where ALIST is as described in `bookmark-alist' and may typically contain
482 a special cons (handler . HANDLER-FUNC) which specifies the handler function
483 that should be used instead of `bookmark-default-handler' to open this
484 bookmark. See the documentation for `bookmark-alist' for more.
485
486 NAME is a suggested name for the constructed bookmark. It can be nil
487 in which case a default heuristic will be used. The function can also
488 equivalently just return ALIST without NAME.")
489
490 (defun bookmark-make-record ()
491 "Return a new bookmark record (NAME . ALIST) for the current location."
492 (let ((record (funcall bookmark-make-record-function)))
493 ;; Set up default name.
494 (if (stringp (car record))
495 ;; The function already provided a default name.
496 record
497 (if (car record) (push nil record))
498 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
499 record)))
500
501 (defun bookmark-store (name alist no-overwrite)
502 "Store the bookmark NAME with data ALIST.
503 If NO-OVERWRITE is non-nil and another bookmark of the same name already
504 exists in `bookmark-alist', record the new bookmark without throwing away the
505 old one."
506 (bookmark-maybe-load-default-file)
507 (let ((stripped-name (copy-sequence name)))
508 (or (featurep 'xemacs)
509 ;; XEmacs's `set-text-properties' doesn't work on
510 ;; free-standing strings, apparently.
511 (set-text-properties 0 (length stripped-name) nil stripped-name))
512 (if (and (not no-overwrite)
513 (bookmark-get-bookmark stripped-name 'noerror))
514 ;; already existing bookmark under that name and
515 ;; no prefix arg means just overwrite old bookmark
516 ;; Use the new (NAME . ALIST) format.
517 (setcdr (bookmark-get-bookmark stripped-name) alist)
518
519 ;; otherwise just cons it onto the front (either the bookmark
520 ;; doesn't exist already, or there is no prefix arg. In either
521 ;; case, we want the new bookmark consed onto the alist...)
522
523 (push (cons stripped-name alist) bookmark-alist))
524
525 ;; Added by db
526 (setq bookmark-current-bookmark stripped-name)
527 (setq bookmark-alist-modification-count
528 (1+ bookmark-alist-modification-count))
529 (if (bookmark-time-to-save-p)
530 (bookmark-save))
531
532 (setq bookmark-current-bookmark stripped-name)
533 (bookmark-bmenu-surreptitiously-rebuild-list)))
534
535 (defun bookmark-make-record-default (&optional point-only)
536 "Return the record describing the location of a new bookmark.
537 Must be at the correct position in the buffer in which the bookmark is
538 being set.
539 If POINT-ONLY is non-nil, then only return the subset of the
540 record that pertains to the location within the buffer."
541 `(,@(unless point-only `((filename . ,(bookmark-buffer-file-name))))
542 (front-context-string
543 . ,(if (>= (- (point-max) (point)) bookmark-search-size)
544 (buffer-substring-no-properties
545 (point)
546 (+ (point) bookmark-search-size))
547 nil))
548 (rear-context-string
549 . ,(if (>= (- (point) (point-min)) bookmark-search-size)
550 (buffer-substring-no-properties
551 (point)
552 (- (point) bookmark-search-size))
553 nil))
554 (position . ,(point))))
555
556 \f
557 ;;; File format stuff
558
559 ;; The OLD format of the bookmark-alist was:
560 ;;
561 ;; ((BOOKMARK-NAME . (FILENAME
562 ;; STRING-IN-FRONT
563 ;; STRING-BEHIND
564 ;; POINT))
565 ;; ...)
566 ;;
567 ;; The NEW format of the bookmark-alist is:
568 ;;
569 ;; ((BOOKMARK-NAME (filename . FILENAME)
570 ;; (front-context-string . STRING-IN-FRONT)
571 ;; (rear-context-string . STRING-BEHIND)
572 ;; (position . POINT)
573 ;; (annotation . ANNOTATION)
574 ;; (whatever . VALUE)
575 ;; ...
576 ;; ))
577 ;; ...)
578 ;;
579 ;;
580 ;; I switched to using an internal as well as external alist because I
581 ;; felt that would be a more flexible framework in which to add
582 ;; features. It means that the order in which values appear doesn't
583 ;; matter, and it means that arbitrary values can be added without
584 ;; risk of interfering with existing ones.
585 ;;
586 ;; BOOKMARK-NAME is the string the user gives the bookmark and
587 ;; accesses it by from then on.
588 ;;
589 ;; FILENAME is the location of the file in which the bookmark is set.
590 ;;
591 ;; STRING-IN-FRONT is a string of `bookmark-search-size' chars of
592 ;; context in front of the point at which the bookmark is set.
593 ;;
594 ;; STRING-BEHIND is the same thing, but after the point.
595 ;;
596 ;; The context strings exist so that modifications to a file don't
597 ;; necessarily cause a bookmark's position to be invalidated.
598 ;; bookmark-jump will search for STRING-BEHIND and STRING-IN-FRONT in
599 ;; case the file has changed since the bookmark was set. It will
600 ;; attempt to place the user before the changes, if there were any.
601 ;; ANNOTATION is the annotation for the bookmark; it may not exist
602 ;; (for backward compatibility), be nil (no annotation), or be a
603 ;; string.
604
605
606 (defconst bookmark-file-format-version 1
607 "The current version of the format used by bookmark files.
608 You should never need to change this.")
609
610
611 (defconst bookmark-end-of-version-stamp-marker
612 "-*- End Of Bookmark File Format Version Stamp -*-\n"
613 "This string marks the end of the version stamp in a bookmark file.")
614
615
616 (defun bookmark-alist-from-buffer ()
617 "Return a `bookmark-alist' (in any format) from the current buffer.
618 The buffer must of course contain bookmark format information.
619 Does not care from where in the buffer it is called, and does not
620 affect point."
621 (save-excursion
622 (goto-char (point-min))
623 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
624 (read (current-buffer))
625 ;; Else we're dealing with format version 0
626 (if (search-forward "(" nil t)
627 (progn
628 (forward-char -1)
629 (read (current-buffer)))
630 ;; Else no hope of getting information here.
631 (error "Not bookmark format")))))
632
633
634 (defun bookmark-upgrade-version-0-alist (old-list)
635 "Upgrade a version 0 alist OLD-LIST to the current version."
636 (mapcar
637 (lambda (bookmark)
638 (let* ((name (car bookmark))
639 (record (car (cdr bookmark)))
640 (filename (nth 0 record))
641 (front-str (nth 1 record))
642 (rear-str (nth 2 record))
643 (position (nth 3 record))
644 (ann (nth 4 record)))
645 (list
646 name
647 `((filename . ,filename)
648 (front-context-string . ,(or front-str ""))
649 (rear-context-string . ,(or rear-str ""))
650 (position . ,position)
651 (annotation . ,ann)))))
652 old-list))
653
654
655 (defun bookmark-upgrade-file-format-from-0 ()
656 "Upgrade a bookmark file of format 0 (the original format) to format 1.
657 This expects to be called from `point-min' in a bookmark file."
658 (message "Upgrading bookmark format from 0 to %d..."
659 bookmark-file-format-version)
660 (let* ((old-list (bookmark-alist-from-buffer))
661 (new-list (bookmark-upgrade-version-0-alist old-list)))
662 (delete-region (point-min) (point-max))
663 (bookmark-insert-file-format-version-stamp)
664 (pp new-list (current-buffer))
665 (save-buffer))
666 (goto-char (point-min))
667 (message "Upgrading bookmark format from 0 to %d...done"
668 bookmark-file-format-version)
669 )
670
671
672 (defun bookmark-grok-file-format-version ()
673 "Return an integer which is the file-format version of this bookmark file.
674 This expects to be called from `point-min' in a bookmark file."
675 (if (looking-at "^;;;;")
676 (save-excursion
677 (save-match-data
678 (re-search-forward "[0-9]")
679 (forward-char -1)
680 (read (current-buffer))))
681 ;; Else this is format version 0, the original one, which didn't
682 ;; even have version stamps.
683 0))
684
685
686 (defun bookmark-maybe-upgrade-file-format ()
687 "Check the file-format version of this bookmark file.
688 If the version is not up-to-date, upgrade it automatically.
689 This expects to be called from `point-min' in a bookmark file."
690 (let ((version (bookmark-grok-file-format-version)))
691 (cond
692 ((= version bookmark-file-format-version)
693 ) ; home free -- version is current
694 ((= version 0)
695 (bookmark-upgrade-file-format-from-0))
696 (t
697 (error "Bookmark file format version strangeness")))))
698
699
700 (defun bookmark-insert-file-format-version-stamp ()
701 "Insert text indicating current version of bookmark file format."
702 (insert
703 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
704 bookmark-file-format-version))
705 (insert ";;; This format is meant to be slightly human-readable;\n"
706 ";;; nevertheless, you probably don't want to edit it.\n"
707 ";;; "
708 bookmark-end-of-version-stamp-marker))
709
710
711 ;;; end file-format stuff
712
713 \f
714 ;;; Generic helpers.
715
716 (defun bookmark-maybe-message (fmt &rest args)
717 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
718 (if (>= baud-rate 9600)
719 (apply 'message fmt args)))
720
721 \f
722 ;;; Core code:
723
724 (defvar bookmark-minibuffer-read-name-map
725 (let ((map (make-sparse-keymap)))
726 (set-keymap-parent map minibuffer-local-map)
727 (define-key map "\C-w" 'bookmark-yank-word)
728 ;; This C-u binding might not be very useful any more now that we
729 ;; provide access to the default via the standard M-n binding.
730 ;; Maybe we should just remove it? --Stef-08
731 (define-key map "\C-u" 'bookmark-insert-current-bookmark)
732 map))
733
734 ;;;###autoload
735 (defun bookmark-set (&optional name no-overwrite)
736 "Set a bookmark named NAME inside a file.
737 If name is nil, then prompt the user.
738
739 With prefix arg (NO-OVERWRITE), do not overwrite a bookmark that
740 has the same name as NAME if such a bookmark already exists, but
741 instead \"push\" the new bookmark onto the bookmark alist. Thus
742 the most recently set bookmark with name NAME would be the one in
743 effect at any given time, but the others are still there, should
744 the user decide to delete the most recent one.
745
746 To yank words from the text of the buffer and use them as part of the
747 bookmark name, type C-w while setting a bookmark. Successive C-w's
748 yank successive words.
749
750 Typing C-u inserts the name of the last bookmark used in the buffer
751 \(as an aid in using a single bookmark name to track progress through
752 a large file\). If no bookmark was used, then C-u inserts the name of
753 the file being visited.
754
755 Use \\[bookmark-delete] to remove bookmarks \(give it a name and it
756 removes only the first instance of a bookmark with that name from
757 the list of bookmarks.\)"
758 (interactive (list nil current-prefix-arg))
759 (let* ((record (bookmark-make-record))
760 (default (car record)))
761
762 (bookmark-maybe-load-default-file)
763
764 (setq bookmark-current-point (point))
765 (setq bookmark-yank-point (point))
766 (setq bookmark-current-buffer (current-buffer))
767
768 (let ((str
769 (or name
770 (read-from-minibuffer
771 (format "Set bookmark (%s): " default)
772 nil
773 bookmark-minibuffer-read-name-map
774 nil nil default))))
775 (and (string-equal str "") (setq str default))
776 (bookmark-store str (cdr record) no-overwrite)
777
778 ;; Ask for an annotation buffer for this bookmark
779 (if bookmark-use-annotations
780 (bookmark-edit-annotation str)
781 (goto-char bookmark-current-point)))))
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-name nil
795 "Variable holding the name of the bookmark.
796 This is used in `bookmark-edit-annotation' to record the bookmark
797 whose annotation is being edited.")
798
799
800 (defun bookmark-default-annotation-text (bookmark)
801 "Return default annotation text for BOOKMARK (a string, not a record).
802 The default annotation text is simply some text explaining how to use
803 annotations."
804 (concat "# Type the annotation for bookmark '" bookmark "' here.\n"
805 "# All lines which start with a '#' will be deleted.\n"
806 "# Type C-c C-c when done.\n#\n"
807 "# Author: " (user-full-name) " <" (user-login-name) "@"
808 (system-name) ">\n"
809 "# Date: " (current-time-string) "\n"))
810
811
812 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
813 "Function to return default text to use for a bookmark annotation.
814 It takes one argument, the name of the bookmark, as a string.")
815 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
816 'bookmark-edit-annotation-text-func "23.1")
817
818 (defvar bookmark-edit-annotation-mode-map
819 (let ((map (make-sparse-keymap)))
820 (set-keymap-parent map text-mode-map)
821 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
822 map)
823 "Keymap for editing an annotation of a bookmark.")
824
825
826 (defun bookmark-edit-annotation-mode (bookmark)
827 "Mode for editing the annotation of bookmark BOOKMARK.
828 When you have finished composing, type \\[bookmark-send-annotation].
829
830 BOOKMARK is a bookmark name (a string) or a bookmark record.
831
832 \\{bookmark-edit-annotation-mode-map}"
833 (interactive)
834 (kill-all-local-variables)
835 (make-local-variable 'bookmark-annotation-name)
836 (setq bookmark-annotation-name bookmark)
837 (use-local-map bookmark-edit-annotation-mode-map)
838 (setq major-mode 'bookmark-edit-annotation-mode
839 mode-name "Edit Bookmark Annotation")
840 (insert (funcall bookmark-edit-annotation-text-func bookmark))
841 (let ((annotation (bookmark-get-annotation bookmark)))
842 (if (and annotation (not (string-equal annotation "")))
843 (insert annotation)))
844 (run-mode-hooks 'text-mode-hook))
845
846
847 (defun bookmark-send-edited-annotation ()
848 "Use buffer contents as annotation for a bookmark.
849 Lines beginning with `#' are ignored."
850 (interactive)
851 (if (not (eq major-mode 'bookmark-edit-annotation-mode))
852 (error "Not in bookmark-edit-annotation-mode"))
853 (goto-char (point-min))
854 (while (< (point) (point-max))
855 (if (looking-at "^#")
856 (bookmark-kill-line t)
857 (forward-line 1)))
858 ;; Take no chances with text properties.
859 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
860 (bookmark bookmark-annotation-name))
861 (bookmark-set-annotation bookmark annotation)
862 (bookmark-bmenu-surreptitiously-rebuild-list)
863 (goto-char bookmark-current-point))
864 (kill-buffer (current-buffer)))
865
866
867 (defun bookmark-edit-annotation (bookmark)
868 "Pop up a buffer for editing bookmark BOOKMARK's annotation.
869 BOOKMARK is a bookmark name (a string) or a bookmark record."
870 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
871 (bookmark-edit-annotation-mode bookmark))
872
873
874 (defun bookmark-insert-current-bookmark ()
875 "Insert this buffer's value of `bookmark-current-bookmark'.
876 Default to file name if it's nil."
877 (interactive)
878 (let ((str
879 (with-current-buffer bookmark-current-buffer
880 (or bookmark-current-bookmark
881 (bookmark-buffer-name)))))
882 (insert str)))
883
884
885 (defun bookmark-buffer-name ()
886 "Return the name of the current buffer's file, non-directory."
887 (cond
888 ;; Or are we a file?
889 (buffer-file-name (file-name-nondirectory buffer-file-name))
890 ;; Or are we a directory?
891 ((and (boundp 'dired-directory) dired-directory)
892 (let* ((dirname (if (stringp dired-directory)
893 dired-directory
894 (car dired-directory)))
895 (idx (1- (length dirname))))
896 ;; Strip the trailing slash.
897 (if (= ?/ (aref dirname idx))
898 (file-name-nondirectory (substring dirname 0 idx))
899 ;; Else return the current-buffer
900 (buffer-name (current-buffer)))))
901 ;; If all else fails, use the buffer's name.
902 (t
903 (buffer-name (current-buffer)))))
904
905
906 (defun bookmark-yank-word ()
907 "Get the next word from the buffer and append it to the name of the
908 bookmark currently being set, advancing point by one word."
909 (interactive)
910 (let ((string (with-current-buffer bookmark-current-buffer
911 (goto-char bookmark-yank-point)
912 (buffer-substring-no-properties
913 (point)
914 (progn
915 (forward-word 1)
916 (setq bookmark-yank-point (point)))))))
917 (insert string)))
918
919 (defun bookmark-buffer-file-name ()
920 "Return the current buffer's file in a way useful for bookmarks."
921 ;; Abbreviate the path, both so it's shorter and so it's more
922 ;; portable. E.g., the user's home dir might be a different
923 ;; path on different machines, but "~/" will still reach it.
924 (abbreviate-file-name
925 (cond
926 (buffer-file-name buffer-file-name)
927 ((and (boundp 'dired-directory) dired-directory)
928 (if (stringp dired-directory)
929 dired-directory
930 (car dired-directory)))
931 (t (error "Buffer not visiting a file or directory")))))
932
933
934 (defun bookmark-maybe-load-default-file ()
935 "If bookmarks have not been loaded from the default place, load them."
936 (and (not bookmarks-already-loaded)
937 (null bookmark-alist)
938 (prog2
939 (and
940 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
941 ;; to be renamed.
942 (file-exists-p (expand-file-name bookmark-old-default-file))
943 (not (file-exists-p (expand-file-name bookmark-default-file)))
944 (rename-file (expand-file-name bookmark-old-default-file)
945 (expand-file-name bookmark-default-file)))
946 ;; return t so the `and' will continue...
947 t)
948
949 (file-readable-p (expand-file-name bookmark-default-file))
950 (bookmark-load bookmark-default-file t t)
951 (setq bookmarks-already-loaded t)))
952
953
954 (defun bookmark-maybe-sort-alist ()
955 "Return `bookmark-alist' for display.
956 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
957 (if bookmark-sort-flag
958 (sort (copy-alist bookmark-alist)
959 (function
960 (lambda (x y) (string-lessp (car x) (car y)))))
961 bookmark-alist))
962
963
964 (defvar bookmark-after-jump-hook nil
965 "Hook run after `bookmark-jump' jumps to a bookmark.
966 Useful for example to unhide text in `outline-mode'.")
967
968 (defun bookmark--jump-via (bookmark display-function)
969 "Handle BOOKMARK, then call DISPLAY-FUNCTION with current buffer as argument.
970 Bookmark may be a bookmark name (a string) or a bookmark record.
971
972 After calling DISPLAY-FUNCTION, set window point to the point specified
973 by BOOKMARK, if necessary, run `bookmark-after-jump-hook', and then show
974 any annotations for this bookmark."
975 (bookmark-handle-bookmark bookmark)
976 (save-current-buffer
977 (funcall display-function (current-buffer)))
978 (let ((win (get-buffer-window (current-buffer) 0)))
979 (if win (set-window-point win (point))))
980 ;; FIXME: we used to only run bookmark-after-jump-hook in
981 ;; `bookmark-jump' itself, but in none of the other commands.
982 (run-hooks 'bookmark-after-jump-hook)
983 (if bookmark-automatically-show-annotations
984 ;; if there is an annotation for this bookmark,
985 ;; show it in a buffer.
986 (bookmark-show-annotation bookmark)))
987
988
989 ;;;###autoload
990 (defun bookmark-jump (bookmark)
991 "Jump to bookmark BOOKMARK (a point in some file).
992 You may have a problem using this function if the value of variable
993 `bookmark-alist' is nil. If that happens, you need to load in some
994 bookmarks. See help on function `bookmark-load' for more about
995 this.
996
997 If the file pointed to by BOOKMARK no longer exists, you will be asked
998 if you wish to give the bookmark a new location, and `bookmark-jump'
999 will then jump to the new location, as well as recording it in place
1000 of the old one in the permanent bookmark record.
1001
1002 BOOKMARK may be a bookmark name (a string) or a bookmark record, but
1003 the latter is usually only used by programmatic callers."
1004 (interactive
1005 (list (bookmark-completing-read "Jump to bookmark"
1006 bookmark-current-bookmark)))
1007 (unless bookmark
1008 (error "No bookmark specified"))
1009 (bookmark-maybe-historicize-string bookmark)
1010 (bookmark--jump-via bookmark 'switch-to-buffer))
1011
1012
1013 ;;;###autoload
1014 (defun bookmark-jump-other-window (bookmark)
1015 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1016 (interactive
1017 (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
1018 bookmark-current-bookmark)))
1019 (if (> emacs-major-version 21)
1020 (list bkm) bkm)))
1021 (when bookmark
1022 (bookmark-maybe-historicize-string bookmark)
1023 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1024
1025
1026 (defun bookmark-file-or-variation-thereof (file)
1027 "Return FILE (a string) if it exists, or return a reasonable
1028 variation of FILE if that exists. Reasonable variations are checked
1029 by appending suffixes defined in `Info-suffix-list'. If cannot find FILE
1030 nor a reasonable variation thereof, then still return FILE if it can
1031 be retrieved from a VC backend, else return nil."
1032 (if (file-exists-p file)
1033 file
1034 (or
1035 (progn (require 'info) ; ensure Info-suffix-list is bound
1036 (catch 'found
1037 (mapc (lambda (elt)
1038 (let ((suffixed-file (concat file (car elt))))
1039 (if (file-exists-p suffixed-file)
1040 (throw 'found suffixed-file))))
1041 Info-suffix-list)
1042 nil))
1043 ;; Last possibility: try VC
1044 (if (vc-backend file) file))))
1045
1046 (defun bookmark-jump-noselect (bookmark)
1047 "Return the location pointed to by the bookmark BOOKMARK.
1048 The return value has the form (BUFFER . POINT).
1049
1050 BOOKMARK may be a bookmark name (a string) or a bookmark record.
1051
1052 Note: this function is deprecated and is present for Emacs 22
1053 compatibility only."
1054 (save-excursion
1055 (bookmark-handle-bookmark bookmark)
1056 (cons (current-buffer) (point))))
1057
1058 (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
1059
1060 (defun bookmark-handle-bookmark (bookmark)
1061 "Call BOOKMARK's handler or `bookmark-default-handler' if it has none.
1062 Changes current buffer and point and returns nil, or signals a `file-error'.
1063
1064 BOOKMARK may be a bookmark name (a string) or a bookmark record."
1065 (condition-case err
1066 (funcall (or (bookmark-get-handler bookmark)
1067 'bookmark-default-handler)
1068 (bookmark-get-bookmark bookmark))
1069 (file-error
1070 ;; We were unable to find the marked file, so ask if user wants to
1071 ;; relocate the bookmark, else remind them to consider deletion.
1072 (when (stringp bookmark)
1073 ;; `bookmark' can either be a bookmark name (found in
1074 ;; `bookmark-alist') or a bookmark object. If it's an object, we
1075 ;; assume it's a bookmark used internally by some other package.
1076 (let ((file (bookmark-get-filename bookmark)))
1077 (when file ;Don't know how to relocate if there's no `file'.
1078 (setq file (expand-file-name file))
1079 (ding)
1080 (if (y-or-n-p (concat (file-name-nondirectory file)
1081 " nonexistent. Relocate \""
1082 bookmark
1083 "\"? "))
1084 (progn
1085 (bookmark-relocate bookmark)
1086 ;; Try again.
1087 (funcall (or (bookmark-get-handler bookmark)
1088 'bookmark-default-handler)
1089 (bookmark-get-bookmark bookmark)))
1090 (message
1091 "Bookmark not relocated; consider removing it \(%s\)." bookmark)
1092 (signal (car err) (cdr err))))))))
1093 ;; Added by db.
1094 (when (stringp bookmark)
1095 (setq bookmark-current-bookmark bookmark))
1096 nil)
1097
1098 (defun bookmark-default-handler (bmk-record)
1099 "Default handler to jump to a particular bookmark location.
1100 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1101 Changes current buffer and point and returns nil, or signals a `file-error'."
1102 (let* ((file (bookmark-get-filename bmk-record))
1103 (buf (bookmark-prop-get bmk-record 'buffer))
1104 (forward-str (bookmark-get-front-context-string bmk-record))
1105 (behind-str (bookmark-get-rear-context-string bmk-record))
1106 (place (bookmark-get-position bmk-record)))
1107 ;; FIXME: bookmark-file-or-variation-thereof was needed for Info files,
1108 ;; but now that Info bookmarks are handled elsewhere it seems that we
1109 ;; should be able to get rid of it. --Stef
1110 (if (not (if buf (buffer-live-p buf)
1111 (setq file (bookmark-file-or-variation-thereof file))))
1112 (signal 'file-error
1113 `("Jumping to bookmark" "No such file or directory"
1114 (bookmark-get-filename bmk-record)))
1115 (set-buffer (or buf (find-file-noselect file)))
1116 (if place (goto-char place))
1117
1118 ;; Go searching forward first. Then, if forward-str exists and
1119 ;; was found in the file, we can search backward for behind-str.
1120 ;; Rationale is that if text was inserted between the two in the
1121 ;; file, it's better to be put before it so you can read it,
1122 ;; rather than after and remain perhaps unaware of the changes.
1123 (if forward-str
1124 (if (search-forward forward-str (point-max) t)
1125 (goto-char (match-beginning 0))))
1126 (if behind-str
1127 (if (search-backward behind-str (point-min) t)
1128 (goto-char (match-end 0)))))
1129 nil))
1130
1131 ;;;###autoload
1132 (defun bookmark-relocate (bookmark)
1133 "Relocate BOOKMARK to another file (reading file name with minibuffer).
1134 BOOKMARK is a bookmark name (a string), not a bookmark record.
1135
1136 This makes an already existing bookmark point to that file, instead of
1137 the one it used to point at. Useful when a file has been renamed
1138 after a bookmark was set in it."
1139 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1140 (bookmark-maybe-historicize-string bookmark)
1141 (bookmark-maybe-load-default-file)
1142 (let* ((bmrk-filename (bookmark-get-filename bookmark))
1143 (newloc (expand-file-name
1144 (read-file-name
1145 (format "Relocate %s to: " bookmark)
1146 (file-name-directory bmrk-filename)))))
1147 (bookmark-set-filename bookmark newloc)
1148 (setq bookmark-alist-modification-count
1149 (1+ bookmark-alist-modification-count))
1150 (if (bookmark-time-to-save-p)
1151 (bookmark-save))
1152 (bookmark-bmenu-surreptitiously-rebuild-list)))
1153
1154
1155 ;;;###autoload
1156 (defun bookmark-insert-location (bookmark &optional no-history)
1157 "Insert the name of the file associated with BOOKMARK.
1158 BOOKMARK is a bookmark name (a string), not a bookmark record.
1159
1160 Optional second arg NO-HISTORY means don't record this in the
1161 minibuffer history list `bookmark-history'."
1162 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1163 (or no-history (bookmark-maybe-historicize-string bookmark))
1164 (let ((start (point)))
1165 (prog1
1166 (insert (bookmark-location bookmark)) ; *Return this line*
1167 (if (and (display-color-p) (display-mouse-p))
1168 (add-text-properties
1169 start
1170 (save-excursion (re-search-backward
1171 "[^ \t]")
1172 (1+ (point)))
1173 '(mouse-face highlight
1174 follow-link t
1175 help-echo "mouse-2: go to this bookmark in other window"))))))
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 may be a bookmark name (a string) or a bookmark record."
1183 (bookmark-maybe-load-default-file)
1184 (bookmark-get-filename bookmark))
1185
1186
1187 ;;;###autoload
1188 (defun bookmark-rename (old &optional new)
1189 "Change the name of OLD bookmark to NEW name.
1190 If called from keyboard, prompt for OLD and NEW. If called from
1191 menubar, select OLD from a menu and prompt for NEW.
1192
1193 Both OLD and NEW are bookmark names (strings), never bookmark records.
1194
1195 If called from Lisp, prompt for NEW if only OLD was passed as an
1196 argument. If called with two strings, then no prompting is done. You
1197 must pass at least OLD when calling from Lisp.
1198
1199 While you are entering the new name, consecutive C-w's insert
1200 consecutive words from the text of the buffer into the new bookmark
1201 name."
1202 (interactive (list (bookmark-completing-read "Old bookmark name")))
1203 (bookmark-maybe-historicize-string old)
1204 (bookmark-maybe-load-default-file)
1205
1206 (setq bookmark-current-point (point))
1207 (setq bookmark-yank-point (point))
1208 (setq bookmark-current-buffer (current-buffer))
1209 (let ((newname
1210 (or new ; use second arg, if non-nil
1211 (read-from-minibuffer
1212 "New name: "
1213 nil
1214 (let ((now-map (copy-keymap minibuffer-local-map)))
1215 (define-key now-map "\C-w" 'bookmark-yank-word)
1216 now-map)
1217 nil
1218 'bookmark-history))))
1219 (bookmark-set-name old newname)
1220 (setq bookmark-current-bookmark newname)
1221 (bookmark-bmenu-surreptitiously-rebuild-list)
1222 (setq bookmark-alist-modification-count
1223 (1+ bookmark-alist-modification-count))
1224 (if (bookmark-time-to-save-p)
1225 (bookmark-save))))
1226
1227
1228 ;;;###autoload
1229 (defun bookmark-insert (bookmark)
1230 "Insert the text of the file pointed to by bookmark BOOKMARK.
1231 BOOKMARK is a bookmark name (a string), not a bookmark record.
1232
1233 You may have a problem using this function if the value of variable
1234 `bookmark-alist' is nil. If that happens, you need to load in some
1235 bookmarks. See help on function `bookmark-load' for more about
1236 this."
1237 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1238 (bookmark-maybe-historicize-string bookmark)
1239 (bookmark-maybe-load-default-file)
1240 (let ((orig-point (point))
1241 (str-to-insert
1242 (save-current-buffer
1243 (bookmark-handle-bookmark bookmark)
1244 (buffer-string))))
1245 (insert str-to-insert)
1246 (push-mark)
1247 (goto-char orig-point)))
1248
1249
1250 ;;;###autoload
1251 (defun bookmark-delete (bookmark &optional batch)
1252 "Delete BOOKMARK from the bookmark list.
1253 BOOKMARK is a bookmark name (a string), not a bookmark record.
1254
1255 Removes only the first instance of a bookmark with that name. If
1256 there are one or more other bookmarks with the same name, they will
1257 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1258 one most recently used in this file, if any\).
1259 Optional second arg BATCH means don't update the bookmark list buffer,
1260 probably because we were called from there."
1261 (interactive
1262 (list (bookmark-completing-read "Delete bookmark"
1263 bookmark-current-bookmark)))
1264 (bookmark-maybe-historicize-string bookmark)
1265 (bookmark-maybe-load-default-file)
1266 (let ((will-go (bookmark-get-bookmark bookmark 'noerror)))
1267 (setq bookmark-alist (delq will-go bookmark-alist))
1268 ;; Added by db, nil bookmark-current-bookmark if the last
1269 ;; occurrence has been deleted
1270 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1271 (setq bookmark-current-bookmark nil)))
1272 ;; Don't rebuild the list
1273 (if batch
1274 nil
1275 (bookmark-bmenu-surreptitiously-rebuild-list)
1276 (setq bookmark-alist-modification-count
1277 (1+ bookmark-alist-modification-count))
1278 (if (bookmark-time-to-save-p)
1279 (bookmark-save))))
1280
1281
1282 (defun bookmark-time-to-save-p (&optional final-time)
1283 "Return t if it is time to save bookmarks to disk, nil otherwise.
1284 Optional argument FINAL-TIME means this is being called when Emacs
1285 is being killed, so save even if `bookmark-save-flag' is a number and
1286 is greater than `bookmark-alist-modification-count'."
1287 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1288 (cond (final-time
1289 (and (> bookmark-alist-modification-count 0)
1290 bookmark-save-flag))
1291 ((numberp bookmark-save-flag)
1292 (>= bookmark-alist-modification-count bookmark-save-flag))
1293 (t
1294 nil)))
1295
1296
1297 ;;;###autoload
1298 (defun bookmark-write ()
1299 "Write bookmarks to a file (reading the file name with the minibuffer).
1300 Don't use this in Lisp programs; use `bookmark-save' instead."
1301 (interactive)
1302 (bookmark-maybe-load-default-file)
1303 (bookmark-save t))
1304
1305
1306 ;;;###autoload
1307 (defun bookmark-save (&optional parg file)
1308 "Save currently defined bookmarks.
1309 Saves by default in the file defined by the variable
1310 `bookmark-default-file'. With a prefix arg, save it in file FILE
1311 \(second argument\).
1312
1313 If you are calling this from Lisp, the two arguments are PARG and
1314 FILE, and if you just want it to write to the default file, then
1315 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1316 instead. If you pass in one argument, and it is non-nil, then the
1317 user will be interactively queried for a file to save in.
1318
1319 When you want to load in the bookmarks from a file, use
1320 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
1321 for a file, defaulting to the file defined by variable
1322 `bookmark-default-file'."
1323 (interactive "P")
1324 (bookmark-maybe-load-default-file)
1325 (cond
1326 ((and (null parg) (null file))
1327 ;;whether interactive or not, write to default file
1328 (bookmark-write-file bookmark-default-file))
1329 ((and (null parg) file)
1330 ;;whether interactive or not, write to given file
1331 (bookmark-write-file file))
1332 ((and parg (not file))
1333 ;;have been called interactively w/ prefix arg
1334 (let ((file (read-file-name "File to save bookmarks in: ")))
1335 (bookmark-write-file file)))
1336 (t ; someone called us with prefix-arg *and* a file, so just write to file
1337 (bookmark-write-file file)))
1338 ;; signal that we have synced the bookmark file by setting this to
1339 ;; 0. If there was an error at any point before, it will not get
1340 ;; set, which is what we want.
1341 (setq bookmark-alist-modification-count 0))
1342
1343
1344 \f
1345 (defun bookmark-write-file (file)
1346 "Write `bookmark-alist' to FILE."
1347 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1348 (with-current-buffer (get-buffer-create " *Bookmarks*")
1349 (goto-char (point-min))
1350 (delete-region (point-min) (point-max))
1351 (let ((print-length nil)
1352 (print-level nil))
1353 (bookmark-insert-file-format-version-stamp)
1354 (insert "(")
1355 ;; Rather than a single call to `pp' we make one per bookmark.
1356 ;; Apparently `pp' has a poor algorithmic complexity, so this
1357 ;; scales a lot better. bug#4485.
1358 (dolist (i bookmark-alist) (pp i (current-buffer)))
1359 (insert ")")
1360 (let ((version-control
1361 (cond
1362 ((null bookmark-version-control) nil)
1363 ((eq 'never bookmark-version-control) 'never)
1364 ((eq 'nospecial bookmark-version-control) version-control)
1365 (t t))))
1366 (condition-case nil
1367 (write-region (point-min) (point-max) file)
1368 (file-error (message "Can't write %s" file)))
1369 (kill-buffer (current-buffer))
1370 (bookmark-maybe-message
1371 "Saving bookmarks to file %s...done" file)))))
1372
1373
1374 (defun bookmark-import-new-list (new-list)
1375 "Add NEW-LIST of bookmarks to `bookmark-alist', rename new bookmarks
1376 with \"<N>\" extensions where they collide with existing bookmark names."
1377 (let ((lst new-list)
1378 (names (bookmark-all-names)))
1379 (while lst
1380 (let* ((full-record (car lst)))
1381 (bookmark-maybe-rename full-record names)
1382 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1383 (setq names (cons (bookmark-name-from-full-record full-record) names))
1384 (setq lst (cdr lst))))))
1385
1386
1387 (defun bookmark-maybe-rename (full-record names)
1388 "If bookmark record FULL-RECORD collides with anything in NAMES, give
1389 FULL-RECORD a new name. This is a helper for `bookmark-import-new-list'."
1390 (let ((found-name (bookmark-name-from-full-record full-record)))
1391 (if (member found-name names)
1392 ;; We've got a conflict, so generate a new name
1393 (let ((count 2)
1394 (new-name found-name))
1395 (while (member new-name names)
1396 (setq new-name (concat found-name (format "<%d>" count)))
1397 (setq count (1+ count)))
1398 (bookmark-set-name full-record new-name)))))
1399
1400
1401 ;;;###autoload
1402 (defun bookmark-load (file &optional overwrite no-msg)
1403 "Load bookmarks from FILE (which must be in bookmark format).
1404 Appends loaded bookmarks to the front of the list of bookmarks. If
1405 optional second argument OVERWRITE is non-nil, existing bookmarks are
1406 destroyed. Optional third arg NO-MSG means don't display any messages
1407 while loading.
1408
1409 If you load a file that doesn't contain a proper bookmark alist, you
1410 will corrupt Emacs's bookmark list. Generally, you should only load
1411 in files that were created with the bookmark functions in the first
1412 place. Your own personal bookmark file, `~/.emacs.bmk', is
1413 maintained automatically by Emacs; you shouldn't need to load it
1414 explicitly.
1415
1416 If you load a file containing bookmarks with the same names as
1417 bookmarks already present in your Emacs, the new bookmarks will get
1418 unique numeric suffixes \"<2>\", \"<3>\", ... following the same
1419 method buffers use to resolve name collisions."
1420 (interactive
1421 (list (read-file-name
1422 (format "Load bookmarks from: (%s) "
1423 bookmark-default-file)
1424 ;;Default might not be used often,
1425 ;;but there's no better default, and
1426 ;;I guess it's better than none at all.
1427 "~/" bookmark-default-file 'confirm)))
1428 (setq file (expand-file-name file))
1429 (if (not (file-readable-p file))
1430 (error "Cannot read bookmark file %s" file)
1431 (if (null no-msg)
1432 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1433 (with-current-buffer (let ((enable-local-variables nil))
1434 (find-file-noselect file))
1435 (goto-char (point-min))
1436 (bookmark-maybe-upgrade-file-format)
1437 (let ((blist (bookmark-alist-from-buffer)))
1438 (if (listp blist)
1439 (progn
1440 (if overwrite
1441 (progn
1442 (setq bookmark-alist blist)
1443 (setq bookmark-alist-modification-count 0))
1444 ;; else
1445 (bookmark-import-new-list blist)
1446 (setq bookmark-alist-modification-count
1447 (1+ bookmark-alist-modification-count)))
1448 (if (string-equal
1449 (expand-file-name bookmark-default-file)
1450 file)
1451 (setq bookmarks-already-loaded t))
1452 (bookmark-bmenu-surreptitiously-rebuild-list))
1453 (error "Invalid bookmark list in %s" file)))
1454 (kill-buffer (current-buffer)))
1455 (if (null no-msg)
1456 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1457
1458
1459 \f
1460 ;;; Code supporting the dired-like bookmark menu.
1461 ;; Prefix is "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 (called-interactively-p 'interactive)
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 (add-text-properties (point-min) (point)
1549 '(font-lock-face bookmark-menu-heading))
1550 (mapc
1551 (lambda (full-record)
1552 ;; if a bookmark has an annotation, prepend a "*"
1553 ;; in the list of bookmarks.
1554 (let ((annotation (bookmark-get-annotation
1555 (bookmark-name-from-full-record full-record))))
1556 (if (and annotation (not (string-equal annotation "")))
1557 (insert " *")
1558 (insert " "))
1559 (let ((start (point)))
1560 (insert (bookmark-name-from-full-record full-record))
1561 (if (and (display-color-p) (display-mouse-p))
1562 (add-text-properties
1563 start
1564 (save-excursion (re-search-backward
1565 "[^ \t]")
1566 (1+ (point)))
1567 '(mouse-face highlight
1568 follow-link t
1569 help-echo "mouse-2: go to this bookmark in other window")))
1570 (insert "\n")
1571 )))
1572 (bookmark-maybe-sort-alist)))
1573 (goto-char (point-min))
1574 (forward-line 2)
1575 (bookmark-bmenu-mode)
1576 (if bookmark-bmenu-toggle-filenames
1577 (bookmark-bmenu-toggle-filenames t)))
1578
1579 ;;;###autoload
1580 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1581 ;;;###autoload
1582 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1583
1584
1585
1586 (defun bookmark-bmenu-mode ()
1587 "Major mode for editing a list of bookmarks.
1588 Each line describes one of the bookmarks in Emacs.
1589 Letters do not insert themselves; instead, they are commands.
1590 Bookmark names preceded by a \"*\" have annotations.
1591 \\<bookmark-bmenu-mode-map>
1592 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1593 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1594 Also show bookmarks marked using m in other windows.
1595 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1596 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1597 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1598 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1599 together with bookmark selected before this one in another window.
1600 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1601 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1602 so the bookmark menu bookmark remains visible in its window.
1603 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1604 \\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
1605 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
1606 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1607 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1608 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1609 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1610 With a prefix arg, prompts for a file to save in.
1611 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1612 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1613 With prefix argument, also move up one line.
1614 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1615 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1616 in another buffer.
1617 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1618 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1619 (kill-all-local-variables)
1620 (use-local-map bookmark-bmenu-mode-map)
1621 (setq truncate-lines t)
1622 (setq buffer-read-only t)
1623 (setq major-mode 'bookmark-bmenu-mode)
1624 (setq mode-name "Bookmark Menu")
1625 (run-mode-hooks 'bookmark-bmenu-mode-hook))
1626
1627
1628 (defun bookmark-bmenu-toggle-filenames (&optional show)
1629 "Toggle whether filenames are shown in the bookmark list.
1630 Optional argument SHOW means show them unconditionally."
1631 (interactive)
1632 (cond
1633 (show
1634 (setq bookmark-bmenu-toggle-filenames nil)
1635 (bookmark-bmenu-show-filenames)
1636 (setq bookmark-bmenu-toggle-filenames t))
1637 (bookmark-bmenu-toggle-filenames
1638 (bookmark-bmenu-hide-filenames)
1639 (setq bookmark-bmenu-toggle-filenames nil))
1640 (t
1641 (bookmark-bmenu-show-filenames)
1642 (setq bookmark-bmenu-toggle-filenames t))))
1643
1644
1645 (defun bookmark-bmenu-show-filenames (&optional force)
1646 "In an interactive bookmark list, show filenames along with bookmarks.
1647
1648 If FORCE is non-nil, force a redisplay showing the filenames; this is
1649 used mainly for debugging, and should not be necessary in normal usage."
1650 (if (and (not force) bookmark-bmenu-toggle-filenames)
1651 nil ;already shown, so do nothing
1652 (save-excursion
1653 (save-window-excursion
1654 (goto-char (point-min))
1655 (forward-line 2)
1656 (setq bookmark-bmenu-hidden-bookmarks ())
1657 (let ((inhibit-read-only t))
1658 (while (< (point) (point-max))
1659 (let ((bmrk (bookmark-bmenu-bookmark)))
1660 (setq bookmark-bmenu-hidden-bookmarks
1661 (cons bmrk bookmark-bmenu-hidden-bookmarks))
1662 (let ((start (save-excursion (end-of-line) (point))))
1663 (move-to-column bookmark-bmenu-file-column t)
1664 ;; Strip off `mouse-face' from the white spaces region.
1665 (if (and (display-color-p) (display-mouse-p))
1666 (remove-text-properties start (point)
1667 '(mouse-face nil help-echo nil))))
1668 (delete-region (point) (progn (end-of-line) (point)))
1669 (insert " ")
1670 ;; Pass the NO-HISTORY arg:
1671 (bookmark-insert-location bmrk t)
1672 (forward-line 1))))))))
1673
1674
1675 (defun bookmark-bmenu-hide-filenames (&optional force)
1676 "In an interactive bookmark list, hide the filenames of the bookmarks.
1677
1678 If FORCE is non-nil, force a redisplay hiding the filenames; this is
1679 used mainly for debugging, and should not be necessary in normal usage."
1680 (if (and (not force) bookmark-bmenu-toggle-filenames)
1681 ;; nothing to hide if above is nil
1682 (save-excursion
1683 (save-window-excursion
1684 (goto-char (point-min))
1685 (forward-line 2)
1686 (setq bookmark-bmenu-hidden-bookmarks
1687 (nreverse bookmark-bmenu-hidden-bookmarks))
1688 (save-excursion
1689 (goto-char (point-min))
1690 (search-forward "Bookmark")
1691 (backward-word 1)
1692 (setq bookmark-bmenu-bookmark-column (current-column)))
1693 (save-excursion
1694 (let ((inhibit-read-only t))
1695 (while bookmark-bmenu-hidden-bookmarks
1696 (move-to-column bookmark-bmenu-bookmark-column t)
1697 (bookmark-kill-line)
1698 (let ((start (point)))
1699 (insert (car bookmark-bmenu-hidden-bookmarks))
1700 (if (and (display-color-p) (display-mouse-p))
1701 (add-text-properties
1702 start
1703 (save-excursion (re-search-backward
1704 "[^ \t]")
1705 (1+ (point)))
1706 '(mouse-face highlight
1707 follow-link t
1708 help-echo
1709 "mouse-2: go to this bookmark in other window"))))
1710 (setq bookmark-bmenu-hidden-bookmarks
1711 (cdr bookmark-bmenu-hidden-bookmarks))
1712 (forward-line 1))))))))
1713
1714
1715 (defun bookmark-bmenu-check-position ()
1716 "Return non-nil if on a line with a bookmark (the actual value
1717 returned is `bookmark-alist'). Else reposition and try again; else if
1718 still no bookmark, return nil."
1719 ;; FIXME: I don't believe this doc string. As far as I can tell,
1720 ;; this function always just returns bookmark-alist. So what is
1721 ;; it for, really? -kfogel, 2009-10-04
1722 (cond ((< (count-lines (point-min) (point)) 2)
1723 (goto-char (point-min))
1724 (forward-line 2)
1725 bookmark-alist)
1726 ((and (bolp) (eobp))
1727 (beginning-of-line 0)
1728 bookmark-alist)
1729 (t
1730 bookmark-alist)))
1731
1732
1733 (defun bookmark-bmenu-bookmark ()
1734 "Return the bookmark for this line in an interactive bookmark list buffer."
1735 ;; return a string which is bookmark of this line.
1736 (if (bookmark-bmenu-check-position)
1737 (save-excursion
1738 (save-window-excursion
1739 (goto-char (point-min))
1740 (search-forward "Bookmark")
1741 (backward-word 1)
1742 (setq bookmark-bmenu-bookmark-column (current-column)))))
1743 (if bookmark-bmenu-toggle-filenames
1744 (bookmark-bmenu-hide-filenames))
1745 (save-excursion
1746 (save-window-excursion
1747 (beginning-of-line)
1748 (forward-char bookmark-bmenu-bookmark-column)
1749 (prog1
1750 (buffer-substring-no-properties (point)
1751 (progn
1752 (end-of-line)
1753 (point)))
1754 ;; well, this is certainly crystal-clear:
1755 (if bookmark-bmenu-toggle-filenames
1756 (bookmark-bmenu-toggle-filenames t))))))
1757
1758
1759 (defun bookmark-show-annotation (bookmark)
1760 "Display the annotation for bookmark named BOOKMARK in a buffer,
1761 if an annotation exists."
1762 (let ((annotation (bookmark-get-annotation bookmark)))
1763 (if (and annotation (not (string-equal annotation "")))
1764 (save-excursion
1765 (let ((old-buf (current-buffer)))
1766 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1767 (delete-region (point-min) (point-max))
1768 ;; (insert (concat "Annotation for bookmark '" bookmark "':\n\n"))
1769 (insert annotation)
1770 (goto-char (point-min))
1771 (pop-to-buffer old-buf))))))
1772
1773
1774 (defun bookmark-show-all-annotations ()
1775 "Display the annotations for all bookmarks in a buffer."
1776 (let ((old-buf (current-buffer)))
1777 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1778 (delete-region (point-min) (point-max))
1779 (mapc
1780 (lambda (full-record)
1781 (let* ((name (bookmark-name-from-full-record full-record))
1782 (ann (bookmark-get-annotation name)))
1783 (insert (concat name ":\n"))
1784 (if (and ann (not (string-equal ann "")))
1785 ;; insert the annotation, indented by 4 spaces.
1786 (progn
1787 (save-excursion (insert ann) (unless (bolp)
1788 (insert "\n")))
1789 (while (< (point) (point-max))
1790 (beginning-of-line) ; paranoia
1791 (insert " ")
1792 (forward-line)
1793 (end-of-line))))))
1794 bookmark-alist)
1795 (goto-char (point-min))
1796 (pop-to-buffer old-buf)))
1797
1798
1799 (defun bookmark-bmenu-mark ()
1800 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1801 (interactive)
1802 (beginning-of-line)
1803 (if (bookmark-bmenu-check-position)
1804 (let ((inhibit-read-only t))
1805 (delete-char 1)
1806 (insert ?>)
1807 (forward-line 1)
1808 (bookmark-bmenu-check-position))))
1809
1810
1811 (defun bookmark-bmenu-select ()
1812 "Select this line's bookmark; also display bookmarks marked with `>'.
1813 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1814 (interactive)
1815 (if (bookmark-bmenu-check-position)
1816 (let ((bmrk (bookmark-bmenu-bookmark))
1817 (menu (current-buffer))
1818 (others ())
1819 tem)
1820 (goto-char (point-min))
1821 (while (re-search-forward "^>" nil t)
1822 (setq tem (bookmark-bmenu-bookmark))
1823 (let ((inhibit-read-only t))
1824 (delete-char -1)
1825 (insert ?\s))
1826 (or (string-equal tem bmrk)
1827 (member tem others)
1828 (setq others (cons tem others))))
1829 (setq others (nreverse others)
1830 tem (/ (1- (frame-height)) (1+ (length others))))
1831 (delete-other-windows)
1832 (bookmark-jump bmrk)
1833 (bury-buffer menu)
1834 (if others
1835 (while others
1836 (split-window nil tem)
1837 (other-window 1)
1838 (bookmark-jump (car others))
1839 (setq others (cdr others)))
1840 (other-window 1)))))
1841
1842
1843 (defun bookmark-bmenu-save (parg)
1844 "Save the current list into a bookmark file.
1845 With a prefix arg, prompts for a file to save them in."
1846 (interactive "P")
1847 (save-excursion
1848 (save-window-excursion
1849 (bookmark-save parg))))
1850
1851
1852 (defun bookmark-bmenu-load ()
1853 "Load the bookmark file and rebuild the bookmark menu-buffer."
1854 (interactive)
1855 (if (bookmark-bmenu-check-position)
1856 (save-excursion
1857 (save-window-excursion
1858 ;; This will call `bookmark-bmenu-list'
1859 (call-interactively 'bookmark-load)))))
1860
1861
1862 (defun bookmark-bmenu-1-window ()
1863 "Select this line's bookmark, alone, in full frame."
1864 (interactive)
1865 (if (bookmark-bmenu-check-position)
1866 (progn
1867 (bookmark-jump (bookmark-bmenu-bookmark))
1868 (bury-buffer (other-buffer))
1869 (delete-other-windows))))
1870
1871
1872 (defun bookmark-bmenu-2-window ()
1873 "Select this line's bookmark, with previous buffer in second window."
1874 (interactive)
1875 (if (bookmark-bmenu-check-position)
1876 (let ((bmrk (bookmark-bmenu-bookmark))
1877 (menu (current-buffer))
1878 (pop-up-windows t))
1879 (delete-other-windows)
1880 (switch-to-buffer (other-buffer))
1881 (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
1882 (bookmark--jump-via bmrk 'pop-to-buffer))
1883 (bury-buffer menu))))
1884
1885
1886 (defun bookmark-bmenu-this-window ()
1887 "Select this line's bookmark in this window."
1888 (interactive)
1889 (if (bookmark-bmenu-check-position)
1890 (bookmark-jump (bookmark-bmenu-bookmark))))
1891
1892
1893 (defun bookmark-bmenu-other-window ()
1894 "Select this line's bookmark in other window, leaving bookmark menu visible."
1895 (interactive)
1896 (let ((bookmark (bookmark-bmenu-bookmark)))
1897 (if (bookmark-bmenu-check-position)
1898 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1899 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
1900
1901
1902 (defun bookmark-bmenu-switch-other-window ()
1903 "Make the other window select this line's bookmark.
1904 The current window remains selected."
1905 (interactive)
1906 (let ((bookmark (bookmark-bmenu-bookmark))
1907 (pop-up-windows t)
1908 same-window-buffer-names
1909 same-window-regexps)
1910 (if (bookmark-bmenu-check-position)
1911 (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
1912 (bookmark--jump-via bookmark 'display-buffer)))))
1913
1914 (defun bookmark-bmenu-other-window-with-mouse (event)
1915 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1916 (interactive "e")
1917 (with-current-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 "%s" (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 (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
2147
2148 (defun bookmark-exit-hook-internal ()
2149 "Save bookmark state, if necessary, at Emacs exit time.
2150 This also runs `bookmark-exit-hook'."
2151 (run-hooks 'bookmark-exit-hook)
2152 (and bookmark-alist
2153 (bookmark-time-to-save-p t)
2154 (bookmark-save)))
2155
2156 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal)
2157
2158 (defun bookmark-unload-function ()
2159 "Unload the Bookmark library."
2160 (when bookmark-save-flag (bookmark-save))
2161 ;; continue standard unloading
2162 nil)
2163
2164
2165 (run-hooks 'bookmark-load-hook)
2166
2167 (provide 'bookmark)
2168
2169 ;; arch-tag: 139f519a-dd0c-4b8d-8b5d-f9fcf53ca8f6
2170 ;;; bookmark.el ends here