1 ;;; bookmark.el --- set bookmarks, jump to them later.
3 ;; Copyright (C) 1993 Free Software Foundation
5 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
6 ;; Maintainer: Karl Fogel <kfogel@cs.oberlin.edu>
9 ;; Keywords: bookmarks, placeholders
11 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
28 ;; then implementing the bookmark-current-bookmark idea. He even
29 ;; sent *patches*, bless his soul...
31 ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
32 ;; fixing and improving bookmark-time-to-save-p.
34 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
35 ;; suggestions and the code to implement them (like
36 ;; Bookmark-menu-check-position, and some of the Lucid compatibility
39 ;; Thanks to Roland McGrath for encouragement and help with defining
40 ;; autoloads on the menu-bar.
42 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
46 ;; bookmark|Karl Fogel|kfogel@cs.oberlin.edu|
47 ;; Setting bookmarks in files or directories, jumping to them later.|
48 ;; 16-July-93|Version: 2.4|~/misc/bookmark.el.Z|
50 ;; Enough with the credits already, get on to the good stuff:
52 ;; FAVORITE CHINESE RESTAURANT:
53 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
54 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
56 ;;; Commentary on code:
58 ;; bookmark alist format:
60 ;; (bookmark-name (filename
66 ;; bookmark-name is the string the user gives the bookmark and
67 ;; accesses it by from then on. filename is the location of the file
68 ;; in which the bookmark is set. string-in-front is a string of
69 ;; `bookmark-search-size' chars of context in front of the point the
70 ;; bookmark is set at, string-behind is the same thing after the
71 ;; point. bookmark-jump will search for string-behind and
72 ;; string-in-front in case the file has changed since the bookmark was
73 ;; set. It will attempt to place the user before the changes, if
76 ;; The bookmark list is sorted lexically by default, but you can turn
77 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
78 ;; the list will be presented in the order it is recorded
79 ;; (chronologically), which is actually fairly useful as well.
83 ;; Added for lucid emacs compatibility, db
84 (or (fboundp 'defalias
) (fset 'defalias
'fset
))
86 ;; suggested for lucid compatibility by david hughes:
87 (or (fboundp 'frame-height
) (fset 'frame-height
'screen-height
))
89 ;; some people have C-x r set to rmail or whatever. We don't want to
90 ;; assume that C-x r is a prefix map just because it's distributed
92 ;; These are the distribution keybindings suggested by RMS, everything
93 ;; else will be done with M-x or the menubar:
95 (if (symbolp (key-binding "\C-xr"))
97 (progn (define-key ctl-x-map
"rb" 'bookmark-jump
)
98 (define-key ctl-x-map
"rm" 'bookmark-set
)
99 (define-key ctl-x-map
"rl" 'list-bookmarks
)))
101 ;; define the map, so it can be bound by those who desire to do so:
104 (defvar bookmark-map nil
105 "Keymap containing bindings to bookmark functions.
106 It is not bound to any key by default: to bind it
107 so that you have a bookmark prefix, just use `global-set-key' and bind a
108 key of your choice to `bookmark-map'. All interactive bookmark
109 functions have a binding in this keymap.")
112 (define-prefix-command 'bookmark-map
)
114 ;; Read the help on all of these functions for details...
116 (define-key bookmark-map
"x" 'bookmark-set
)
118 (define-key bookmark-map
"m" 'bookmark-set
) ; "m" for "mark"
120 (define-key bookmark-map
"j" 'bookmark-jump
)
122 (define-key bookmark-map
"g" 'bookmark-jump
) ; "g" for "go"
124 (define-key bookmark-map
"i" 'bookmark-insert
)
126 (define-key bookmark-map
"e" 'edit-bookmarks
)
128 (define-key bookmark-map
"f" 'bookmark-locate
) ; "f" for "find"
130 (define-key bookmark-map
"r" 'bookmark-rename
)
132 (define-key bookmark-map
"d" 'bookmark-delete
)
134 (define-key bookmark-map
"l" 'bookmark-load
)
136 (define-key bookmark-map
"w" 'bookmark-write
)
138 (define-key bookmark-map
"s" 'bookmark-save
)
140 (defvar bookmark-alist
()
141 "Association list of bookmarks.
142 You probably don't want to change the value of this alist yourself;
143 instead, let the various bookmark functions do it for you.")
145 (defvar bookmarks-already-loaded nil
)
147 ;; just add the hook to make sure that people don't lose bookmarks
148 ;; when they kill Emacs, unless they don't want to save them.
150 (add-hook 'kill-emacs-hook
152 (lambda () (and (featurep 'bookmark
)
154 (bookmark-time-to-save-p t
)
157 ;; more stuff added by db.
159 (defvar bookmark-current-bookmark nil
160 "Name of bookmark most recently used in the current file.
161 It is buffer local, used to make moving a bookmark forward
162 through a file easier.")
164 (make-variable-buffer-local 'bookmark-current-bookmark
)
166 (defvar bookmark-save-flag t
167 "*Controls when Emacs saves bookmarks to a file.
168 --> Nil means never save bookmarks, except when `bookmark-save' is
169 explicitly called \(\\[bookmark-save]\).
170 --> t means save bookmarks when Emacs is killed.
171 --> Otherise, it should be a number that is the frequency with which
172 the bookmark list is saved \(i.e.: the number of times which
173 Emacs' bookmark list may be modified before it is automatically
174 saved.\). If it is a number, Emacs will also automatically save
175 bookmarks when it is killed.
177 Therefore, the way to get it to save every time you make or delete a
178 bookmark is to set this variable to 1 \(or 0, which produces the same
181 To specify the file in which to save them, modify the variable
182 bookmark-file, which is `~/.emacs-bkmrks' by default.")
184 (defvar bookmark-alist-modification-count
0
185 "Number of modifications to bookmark list since it was last saved.")
187 (defvar bookmark-file
"~/.emacs-bkmrks"
188 "*File in which to save bookmarks by default.")
190 (defvar bookmark-version-control
'nospecial
191 "This variable controls whether or not to make numbered backups of
192 the master bookmark file. It can have four values: t, nil, never, and
193 nospecial. The first three have the same meaning that they do for the
194 variable version-control, and the final value nospecial means just use
195 the value of version-control.")
197 (defvar bookmark-completion-ignore-case t
198 "*Non-nil means bookmark functions ignore case in completion.")
200 (defvar bookmark-sort-flag t
201 "*Non-nil means that bookmarks will be displayed sorted by bookmark
202 name. Otherwise they will be displayed in LIFO order (that is, most
203 recently set ones come first, oldest ones come last).")
205 (defvar bookmark-search-size
500
206 "Length of the context strings recorded on either side of a bookmark.")
208 (defvar bookmark-current-point
0)
209 (defvar bookmark-yank-point
0)
210 (defvar bookmark-current-buffer nil
)
213 (defun bookmark-set (&optional parg
)
214 "Set a bookmark named NAME inside a file.
215 With prefix arg, will not overwrite a bookmark that has the same name
216 as NAME if such a bookmark already exists, but instead will \"push\"
217 the new bookmark onto the bookmark alist. Thus the most recently set
218 bookmark with name NAME would be the one in effect at any given time,
219 but the others are still there, should you decide to delete the most
222 To yank words from the text of the buffer and use them as part of the
223 bookmark name, type C-w while setting a bookmark. Successive C-w's
224 yank successive words.
226 Typing C-v inserts the name of the current file being visited. Typing
227 C-u inserts the name of the last bookmark used in the buffer \(as an
228 aid in using a single bookmark name to track your progress through a
229 large file\). If no bookmark was used, then C-u behaves like C-v and
230 inserts the name of the file being visited.
232 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
233 and it removes only the first instance of a bookmark with that name from
234 the list of bookmarks.\)"
236 (if (not (bookmark-buffer-file-name))
237 (error "Buffer not visiting a file or directory."))
238 (bookmark-try-default-file)
239 (setq bookmark-current-point
(point))
240 (setq bookmark-yank-point
(point))
241 (setq bookmark-current-buffer
(current-buffer))
242 (let* ((default (or bookmark-current-bookmark
243 (buffer-name (current-buffer))))
245 (read-from-minibuffer
246 (format "Set bookmark (%s): " default
)
248 (let ((now-map (copy-keymap minibuffer-local-map
)))
249 (progn (define-key now-map
"\C-w"
251 (define-key now-map
"\C-v"
252 'bookmark-insert-current-file-name
)
253 (define-key now-map
"\C-u"
254 'bookmark-insert-current-bookmark
))
256 (and (string-equal str
"") (setq str default
))
258 (bookmark-make parg str
)
259 (setq bookmark-current-bookmark str
)
260 (if (get-buffer "*Bookmark List*") ;rebuild the bookmark list
262 (save-window-excursion
264 (goto-char bookmark-current-point
))))
266 (defun bookmark-insert-current-bookmark ()
267 ;; insert this buffer's value of bookmark-current-bookmark, default
268 ;; to file name if it's nil.
272 (set-buffer bookmark-current-buffer
)
273 bookmark-current-bookmark
)))
274 (if str
(insert str
) (bookmark-insert-current-file-name))))
276 (defun bookmark-insert-current-file-name ()
277 ;; insert the name (sans path) of the current file into the bookmark
278 ;; name that is being set.
280 (let ((str (save-excursion
281 (set-buffer bookmark-current-buffer
)
282 (bookmark-buffer-file-name))))
289 (defun bookmark-yank-word ()
291 ;; get the next word from the buffer and append it to the name of
292 ;; the bookmark currently being set.
293 (let ((string (save-excursion
294 (set-buffer bookmark-current-buffer
)
295 (goto-char bookmark-yank-point
)
300 (setq bookmark-yank-point
(point)))))))
303 (defun bookmark-make (parg str
)
304 (if (and (assoc str bookmark-alist
) (not parg
))
305 ;; already existing boookmark under that name and
306 ;; no prefix arg means just overwrite old bookmark
307 (setcdr (assoc str bookmark-alist
)
308 (list (bookmark-make-cell)))
310 ;; otherwise just cons it onto the front (either the bookmark
311 ;; doesn't exist already, or there is no prefix arg. In either
312 ;; case, we want the new bookmark consed onto the alist...)
317 (bookmark-make-cell))
320 (setq bookmark-current-bookmark str
)
321 (setq bookmark-alist-modification-count
322 (1+ bookmark-alist-modification-count
))
323 (if (bookmark-time-to-save-p)
326 (defun bookmark-make-cell ()
327 ;; make the cell that is the cdr of a bookmark alist element. It
329 ;; (filename search-forward-str search-back-str point)
331 (bookmark-buffer-file-name)
332 (if (>= (- (point-max) (point)) bookmark-search-size
)
335 (+ (point) bookmark-search-size
))
337 (if (>= (- (point) (point-min)) bookmark-search-size
)
340 (- (point) bookmark-search-size
))
344 (defun bookmark-buffer-file-name ()
347 (if (and (boundp 'dired-directory
) dired-directory
)
348 (if (stringp dired-directory
)
350 (car dired-directory
)))))
352 (defun bookmark-try-default-file ()
353 (and (not bookmarks-already-loaded
)
354 (null bookmark-alist
)
355 (file-readable-p (expand-file-name bookmark-file
))
357 (bookmark-load bookmark-file t t
)
358 (setq bookmarks-already-loaded t
))))
360 (defun bookmark-maybe-sort-alist ()
361 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
362 ;;is non-nil, then return a sorted copy of the alist.
363 (if bookmark-sort-flag
365 (sort (copy-alist bookmark-alist
)
367 (lambda (x y
) (string-lessp (car x
) (car y
))))))))
370 (defun bookmark-jump (str)
371 "Jump to bookmark BOOKMARK (a point in some file).
372 You may have a problem using this function if the value of variable
373 `bookmark-alist' is nil. If that happens, you need to load in some
374 bookmarks. See help on function `bookmark-load' for more about
377 If the file pointed to by BOOKMARK no longer exists, you will be asked
378 if you wish to give the bookmark a new location, and bookmark-jump
379 will then jump to the new location, as well as recording it in place
380 of the old one in the permanent bookmark record."
381 (interactive (progn (bookmark-try-default-file)
382 (let* ((completion-ignore-case
383 bookmark-completion-ignore-case
)
386 (assoc bookmark-current-bookmark
388 bookmark-current-bookmark
)
389 (and (assoc (buffer-name (current-buffer))
391 (buffer-name (current-buffer)))))
395 (format "Jump to bookmark (%s): "
397 "Jump to bookmark: ")
401 (and (string-equal "" str
)
404 (let ((cell (bookmark-jump-noselect str
)))
406 (switch-to-buffer (car cell
))
407 (goto-char (cdr cell
)))))
409 (defun bookmark-jump-noselect (str)
410 ;; a leetle helper for bookmark-jump :-)
411 ;; returns (BUFFER . POINT)
412 (let ((whereto-list (car (cdr (assoc str bookmark-alist
)))))
413 (let* ((file (expand-file-name (car whereto-list
)))
415 (forward-str (car (cdr whereto-list
)))
416 (behind-str (car (cdr (cdr whereto-list
))))
417 (place (car (cdr (cdr (cdr whereto-list
))))))
420 ;; else try some common compression extensions
421 ;; and Emacs better handle it right!
424 (let ((altname (concat file
".Z")))
425 (and (file-exists-p altname
)
427 (let ((altname (concat file
".gz")))
428 (and (file-exists-p altname
)
430 (let ((altname (concat file
".z")))
431 (and (file-exists-p altname
)
434 (set-buffer (find-file-noselect file
))
436 ;; Go searching forward first. Then, if forward-str exists and
437 ;; was found in the file, we can search backward for behind-str.
438 ;; Rationale is that if text was inserted between the two in the
439 ;; file, it's better to be put before it so you can read it,
440 ;; rather than after and remain perhaps unaware of the changes.
442 (if (search-forward forward-str
(point-max) t
)
443 (backward-char bookmark-search-size
)))
445 (if (search-backward behind-str
(point-min) t
)
446 (forward-char bookmark-search-size
)))
448 (setq bookmark-current-bookmark str
)
449 (cons (current-buffer) (point)))
452 (if (y-or-n-p (concat (file-name-nondirectory orig-file
)
453 " nonexistent. Relocate \""
457 (bookmark-relocate str
)
458 ;; gasp! It's a recursive function call in Emacs Lisp!
459 (bookmark-jump-noselect str
))
461 "Bookmark not relocated, but deleting it would be a good idea.")
465 (defun bookmark-relocate (str)
466 "Relocate BOOKMARK -- prompts for a filename, and makes an already
467 existing bookmark point to that file, instead of the one it used to
468 point at. Useful when a file has been renamed after a bookmark was
470 (interactive (let ((completion-ignore-case
471 bookmark-completion-ignore-case
))
472 (progn (bookmark-try-default-file)
473 (list (completing-read
474 "Bookmark to relocate: "
478 (let* ((bmrk (assoc str bookmark-alist
))
479 (bmrk-filename (car (car (cdr bmrk
))))
480 (newloc (expand-file-name
482 (format "Relocate %s to: " str
)
483 (file-name-directory bmrk-filename
)))))
484 (setcar (car (cdr bmrk
)) newloc
)))
487 (defun bookmark-locate (str &optional no-insertion
)
488 "Insert the name of the file associated with BOOKMARK.
489 Optional second arg NO-INSERTION means merely return the filename as a
491 (interactive (let ((completion-ignore-case
492 bookmark-completion-ignore-case
))
493 (progn (bookmark-try-default-file)
494 (list (completing-read
495 "Insert bookmark location: "
499 (let ((where (car (car (cdr (assoc str bookmark-alist
))))))
505 (defun bookmark-rename (old &optional new
)
506 "Change the name of OLD-BOOKMARK to NEWNAME.
507 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
508 If called from menubar, OLD-BOOKMARK is selected from a menu, and
510 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
511 passed as an argument. If called with two strings, then no prompting
512 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
514 While you are entering the new name, consecutive C-w's insert
515 consectutive words from the text of the buffer into the new bookmark
516 name, and C-v inserts the name of the file."
517 (interactive (let ((completion-ignore-case
518 bookmark-completion-ignore-case
))
519 (progn (bookmark-try-default-file)
520 (list (completing-read "Old bookmark name: "
525 (setq bookmark-current-point
(point))
526 (setq bookmark-yank-point
(point))
527 (setq bookmark-current-buffer
(current-buffer))
528 (let ((cell (assoc old bookmark-alist
))
530 (or new
; use second arg, if non-nil
531 (read-from-minibuffer
534 (let ((now-map (copy-keymap minibuffer-local-map
)))
535 (progn (define-key now-map
"\C-w"
537 (define-key now-map
"\C-v"
538 'bookmark-insert-current-file-name
))
542 (setq bookmark-current-bookmark str
)
543 (if (get-buffer "*Bookmark List*")
544 (save-excursion (save-window-excursion (list-bookmarks))))
545 (setq bookmark-alist-modification-count
546 (1+ bookmark-alist-modification-count
))
547 (if (bookmark-time-to-save-p)
551 (defun bookmark-insert (str)
552 "Insert the text of the file pointed to by bookmark BOOKMARK.
553 You may have a problem using this function if the value of variable
554 `bookmark-alist' is nil. If that happens, you need to load in some
555 bookmarks. See help on function `bookmark-load' for more about
557 (interactive (let ((completion-ignore-case
558 bookmark-completion-ignore-case
))
559 (progn (bookmark-try-default-file)
560 (list (completing-read
561 "Insert bookmark contents: "
565 (let ((orig-point (point))
568 (set-buffer (car (bookmark-jump-noselect str
)))
569 (buffer-substring (point-min) (point-max)))))
570 (insert str-to-insert
)
572 (goto-char orig-point
)))
575 (defun bookmark-delete (str)
576 "Delete the bookmark named NAME from the bookmark list.
577 Removes only the first instance of a bookmark with that name. If
578 there are one or more other bookmarks with the same name, they will
579 not be deleted. Defaults to the \"current\" bookmark \(that is, the
580 one most recently used in this file, if any\)."
581 (interactive (let ((completion-ignore-case
582 bookmark-completion-ignore-case
))
583 (progn (bookmark-try-default-file)
590 bookmark-current-bookmark
)))))
591 (let ((will-go (assoc str bookmark-alist
)))
592 (setq bookmark-alist
(delq will-go bookmark-alist
))
593 ;; Added by db, nil bookmark-current-bookmark if the last
594 ;; occurence has been deleted
595 (or (assoc bookmark-current-bookmark bookmark-alist
)
596 (setq bookmark-current-bookmark nil
)))
597 (if (get-buffer "*Bookmark List*")
598 (save-excursion (save-window-excursion (list-bookmarks))))
599 (setq bookmark-alist-modification-count
600 (1+ bookmark-alist-modification-count
))
601 (if (bookmark-time-to-save-p)
604 (defun bookmark-time-to-save-p (&optional last-time
)
605 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
606 ;; finds out whether it's time to save bookmarks to a file, by
607 ;; examining the value of variable bookmark-save-flag, and maybe
608 ;; bookmark-alist-modification-count. Returns t if they should be
609 ;; saved, nil otherwise. if last-time is non-nil, then this is
610 ;; being called when emacs is killed.
612 (and (> bookmark-alist-modification-count
0)
614 ((numberp bookmark-save-flag
)
615 (>= bookmark-alist-modification-count bookmark-save-flag
))
620 (defun bookmark-write ()
621 "Write bookmarks to a file \(for which the user will be prompted
622 interactively\). Don't use this in Lisp programs; use bookmark-save
625 (bookmark-try-default-file)
629 (defun bookmark-save (&optional parg file
)
630 "Save currently defined bookmarks.
631 Saves by default in the file defined by the variable
632 `bookmark-file'. With a prefix arg, save it in file FILE.
634 If you are calling this from Lisp, the two arguments are PREFIX-ARG
635 and FILE, and if you just want it to write to the default file, then
636 pass no arguments. Or pass in nil and FILE, and it will save in FILE
637 instead. If you pass in one argument, and it is non-nil, then the
638 user will be interactively queried for a file to save in.
640 When you want to load in the bookmarks from a file, use
641 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
642 for a file, defaulting to the file defined by variable
645 (bookmark-try-default-file)
647 ((and (null parg
) (null file
))
648 ;;whether interactive or not, write to default file
649 (bookmark-write-file bookmark-file
))
650 ((and (null parg
) file
)
651 ;;whether interactive or not, write to given file
652 (bookmark-write-file file
))
653 ((and parg
(not file
))
654 ;;have been called interactively w/ prefix arg
655 (let ((file (read-file-name "File to save bookmarks in: ")))
656 (bookmark-write-file file
)))
657 (t ; someone called us with prefix-arg *and* a file, so just write to file
658 (bookmark-write-file file
)))
659 ;; signal that we have synced the bookmark file by setting this to
660 ;; 0. If there was an error at any point before, it will not get
661 ;; set, which is what we want.
662 (setq bookmark-alist-modification-count
0))
664 (defun bookmark-write-file (file)
666 (save-window-excursion
667 (if (>= baud-rate
9600)
668 (message (format "Saving bookmarks to file %s." file
)))
669 (set-buffer (find-file-noselect file
))
670 (goto-char (point-min))
671 (delete-region (point-min) (point-max))
672 (print bookmark-alist
(current-buffer))
673 (let ((version-control
675 ((null bookmark-version-control
) nil
)
676 ((eq 'never bookmark-version-control
) 'never
)
677 ((eq 'nospecial bookmark-version-control
) version-control
)
681 (kill-buffer (current-buffer))))))
684 (defun bookmark-load (file &optional revert no-msg
)
685 "Load bookmarks from FILE (which must be in bookmark format).
686 Appends loaded bookmarks to the front of the list of bookmarks. If
687 optional second argument REVERT is non-nil, existing bookmarks are
688 destroyed. Optional third arg NO-MSG means don't display any messages
691 If you load a file that doesn't contain a proper bookmark alist, you
692 will corrupt Emacs's bookmark list. Generally, you should only load
693 in files that were created with the bookmark functions in the first
694 place. Your own personal bookmark file, `~/.emacs-bkmrks', is
695 maintained automatically by Emacs; you shouldn't need to load it
698 (progn (bookmark-try-default-file)
699 (list (read-file-name
700 (format "Load bookmarks from: (%s) "
702 ;;Default might not be used often,
703 ;;but there's no better default, and
704 ;;I guess it's better than none at all.
705 "~/" bookmark-file
'confirm
))))
706 (setq file
(expand-file-name file
))
707 (if (file-readable-p file
)
709 (save-window-excursion
710 (if (and (null no-msg
) (>= baud-rate
9600))
711 (message (format "Loading bookmarks from %s..." file
)))
712 (set-buffer (find-file-noselect file
))
713 (goto-char (point-min))
714 (let ((blist (car (read-from-string
715 (buffer-substring (point-min) (point-max))))))
719 (setq bookmark-alist-modification-count
720 (1+ bookmark-alist-modification-count
))
721 (setq bookmark-alist-modification-count
0))
723 (append blist
(if (not revert
) bookmark-alist
)))
724 (if (get-buffer "*Bookmark List*")
725 (save-excursion (list-bookmarks))))
726 (error (format "Invalid bookmark list in %s." file
))))
727 (kill-buffer (current-buffer)))
728 (if (and (null no-msg
) (>= baud-rate
9600))
729 (message (format "Loading bookmarks from %s... done" file
))))
730 (error (format "Cannot read bookmark file %s." file
))))
732 ;;;; bookmark-menu-mode stuff ;;;;
734 (defvar Bookmark-menu-bookmark-column nil
)
736 (defvar Bookmark-menu-hidden-bookmarks
())
738 (defvar Bookmark-menu-file-column
30
739 "*Column at which to display filenames in a buffer listing bookmarks.
740 You can toggle whether files are shown with \\<Bookmark-menu-mode-map>\\[Bookmark-menu-toggle-filenames].")
742 (defvar Bookmark-menu-toggle-filenames t
743 "*Non-nil means show filenames when listing bookmarks.
744 This may result in truncated bookmark names. To disable this, put the
745 following in your .emacs:
747 \(setq Bookmark-menu-toggle-filenames nil\)")
749 (defvar Bookmark-menu-mode-map nil
)
751 (if Bookmark-menu-mode-map
753 (setq Bookmark-menu-mode-map
(make-keymap))
754 (suppress-keymap Bookmark-menu-mode-map t
)
755 (define-key Bookmark-menu-mode-map
"q" 'Bookmark-menu-quit
)
756 (define-key Bookmark-menu-mode-map
"v" 'Bookmark-menu-select
)
757 (define-key Bookmark-menu-mode-map
"w" 'Bookmark-menu-locate
)
758 (define-key Bookmark-menu-mode-map
"2" 'Bookmark-menu-2-window
)
759 (define-key Bookmark-menu-mode-map
"1" 'Bookmark-menu-1-window
)
760 (define-key Bookmark-menu-mode-map
"j" 'Bookmark-menu-this-window
)
761 (define-key Bookmark-menu-mode-map
"f" 'Bookmark-menu-this-window
)
762 (define-key Bookmark-menu-mode-map
"o" 'Bookmark-menu-other-window
)
763 (define-key Bookmark-menu-mode-map
"\C-o" 'Bookmark-menu-switch-other-window
)
764 (define-key Bookmark-menu-mode-map
"s" 'Bookmark-menu-save
)
765 (define-key Bookmark-menu-mode-map
"k" 'Bookmark-menu-delete
)
766 (define-key Bookmark-menu-mode-map
"\C-d" 'Bookmark-menu-delete-backwards
)
767 (define-key Bookmark-menu-mode-map
"x" 'Bookmark-menu-execute
)
768 (define-key Bookmark-menu-mode-map
"\C-k" 'Bookmark-menu-delete
)
769 (define-key Bookmark-menu-mode-map
"d" 'Bookmark-menu-delete
)
770 (define-key Bookmark-menu-mode-map
" " 'next-line
)
771 (define-key Bookmark-menu-mode-map
"n" 'next-line
)
772 (define-key Bookmark-menu-mode-map
"p" 'previous-line
)
773 (define-key Bookmark-menu-mode-map
"\177" 'Bookmark-menu-backup-unmark
)
774 (define-key Bookmark-menu-mode-map
"?" 'describe-mode
)
775 (define-key Bookmark-menu-mode-map
"u" 'Bookmark-menu-unmark
)
776 (define-key Bookmark-menu-mode-map
"m" 'Bookmark-menu-mark
)
777 (define-key Bookmark-menu-mode-map
"l" 'Bookmark-menu-load
)
778 (define-key Bookmark-menu-mode-map
"r" 'Bookmark-menu-rename
)
779 (define-key Bookmark-menu-mode-map
"t" 'Bookmark-menu-toggle-filenames
))
781 ;; Bookmark Menu mode is suitable only for specially formatted data.
782 (put 'Bookmark-menu-mode
'mode-class
'special
)
784 ;; need to display whether or not bookmark exists as a buffer in flag
788 ;; FLAGS BOOKMARK (/FILE/NAME/HERE/WHAT/REGEXP/TO/USE?)
789 ;; goto bookmark-column and then search till "(/[^)]*)$" or "(/.*)$" ?
792 (defalias 'edit-bookmarks
'list-bookmarks
)
795 (defun list-bookmarks ()
796 "Display a list of existing bookmarks.
797 The list is displayed in a buffer named `*Bookmark List*'.
798 The leftmost column displays a D if the bookmark is flagged for
799 deletion, or > if it is flagged for displaying."
801 (bookmark-try-default-file)
802 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
803 (let ((buffer-read-only nil
))
804 (delete-region (point-max) (point-min))
805 (goto-char (point-min)) ;sure are playing it safe...
806 (insert "% Bookmark\n- --------\n")
807 (bookmark-maybe-sort-alist)
808 (let ((lst bookmark-alist
))
811 (concat " " (car (car lst
)) "\n"))
812 (setq lst
(cdr lst
)))))
813 (goto-char (point-min))
816 (if Bookmark-menu-toggle-filenames
817 (Bookmark-menu-toggle-filenames t
)))
819 (defun bookmark-menu-mode ()
820 "Major mode for editing a list of bookmarks.
821 Each line describes one of the bookmarks in Emacs.
822 Letters do not insert themselves; instead, they are commands.
823 \\<Bookmark-menu-mode-map>
824 \\[Bookmark-menu-mark] -- mark bookmark to be displayed.
825 \\[Bookmark-menu-select] -- select bookmark of line point is on.
826 Also show bookmarks marked using m in other windows.
827 \\[Bookmark-menu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
828 \\[Bookmark-menu-locate] -- display (in minibuffer) location of this bookmark.
829 \\[Bookmark-menu-1-window] -- select this bookmark in full-frame window.
830 \\[Bookmark-menu-2-window] -- select this bookmark in one window,
831 together with bookmark selected before this one in another window.
832 \\[Bookmark-menu-this-window] -- select this bookmark in place of the bookmark menu buffer.
833 \\[Bookmark-menu-other-window] -- select this bookmark in another window,
834 so the bookmark menu bookmark remains visible in its window.
835 \\[Bookmark-menu-switch-other-window] -- switch the other window to this bookmark.
836 \\[Bookmark-menu-rename] -- rename this bookmark \(prompts for new name\).
837 \\[Bookmark-menu-delete] -- mark this bookmark to be deleted, and move down.
838 \\[Bookmark-menu-delete-backwards] -- mark this bookmark to be deleted, and move up.
839 \\[Bookmark-menu-execute] -- delete marked bookmarks.
840 \\[Bookmark-menu-save] -- save the current bookmark list in the default file.
841 With a prefix arg, prompts for a file to save in.
842 \\[Bookmark-menu-load] -- load in a file of bookmarks (prompts for file.)
843 \\[Bookmark-menu-unmark] -- remove all kinds of marks from current line.
844 With prefix argument, also move up one line.
845 \\[Bookmark-menu-backup-unmark] -- back up a line and remove marks."
846 (kill-all-local-variables)
847 (use-local-map Bookmark-menu-mode-map
)
848 (setq truncate-lines t
)
849 (setq buffer-read-only t
)
850 (setq major-mode
'bookmark-menu-mode
)
851 (setq mode-name
"Bookmark Menu")
852 (run-hooks 'bookmark-menu-mode-hook
))
854 (defun Bookmark-menu-toggle-filenames (&optional parg
)
855 "Toggle whether filenames are shown in the bookmark list.
856 Optional argument SHOW means show them unconditionally."
860 (setq Bookmark-menu-toggle-filenames nil
)
861 (Bookmark-menu-show-filenames)
862 (setq Bookmark-menu-toggle-filenames t
))
863 (Bookmark-menu-toggle-filenames
864 (Bookmark-menu-hide-filenames)
865 (setq Bookmark-menu-toggle-filenames nil
))
867 (Bookmark-menu-show-filenames)
868 (setq Bookmark-menu-toggle-filenames t
))))
870 (defun Bookmark-menu-show-filenames (&optional force
)
871 (if (and (not force
) Bookmark-menu-toggle-filenames
)
872 nil
;already shown, so do nothing
874 (save-window-excursion
875 (goto-char (point-min))
877 (setq Bookmark-menu-hidden-bookmarks
())
878 (let ((buffer-read-only nil
))
879 (while (< (point) (point-max))
880 (let ((bmrk (Bookmark-menu-bookmark)))
881 (setq Bookmark-menu-hidden-bookmarks
882 (cons bmrk Bookmark-menu-hidden-bookmarks
))
883 (move-to-column Bookmark-menu-file-column t
)
884 (delete-region (point) (progn (end-of-line) (point)))
886 (bookmark-locate bmrk
)
887 (forward-line 1))))))))
889 (defun Bookmark-menu-hide-filenames (&optional force
)
890 (if (and (not force
) Bookmark-menu-toggle-filenames
)
891 ;; nothing to hide if above is nil
893 (save-window-excursion
894 (goto-char (point-min))
896 (setq Bookmark-menu-hidden-bookmarks
897 (nreverse Bookmark-menu-hidden-bookmarks
))
899 (goto-char (point-min))
900 (search-forward "Bookmark")
902 (setq Bookmark-menu-bookmark-column
(current-column)))
904 (let ((buffer-read-only nil
))
905 (while Bookmark-menu-hidden-bookmarks
906 (move-to-column Bookmark-menu-bookmark-column t
)
908 (insert (car Bookmark-menu-hidden-bookmarks
))
909 (setq Bookmark-menu-hidden-bookmarks
910 (cdr Bookmark-menu-hidden-bookmarks
))
911 (forward-line 1))))))))
913 ;; if you look at this next function from far away, it resembles a
914 ;; gun. But only with this comment above...
915 (defun Bookmark-menu-check-position ()
916 ;; Returns t if on a line with a bookmark.
917 ;; Otherwise, repositions and returns t.
918 ;; written by David Hughes <djh@harston.cv.com>
919 ;; Mucho thanks, David! -karl
920 (cond ((< (count-lines (point-min) (point)) 2)
921 (goto-char (point-min))
925 (beginning-of-line 0)
930 (defun Bookmark-menu-bookmark ()
931 ;; return a string which is bookmark of this line.
932 (if (Bookmark-menu-check-position)
934 (save-window-excursion
935 (goto-char (point-min))
936 (search-forward "Bookmark")
938 (setq Bookmark-menu-bookmark-column
(current-column)))))
939 (if Bookmark-menu-toggle-filenames
940 (Bookmark-menu-hide-filenames))
942 (save-window-excursion
944 (forward-char Bookmark-menu-bookmark-column
)
946 (buffer-substring (point)
950 ;; well, this is certainly crystal-clear:
951 (if Bookmark-menu-toggle-filenames
952 (Bookmark-menu-toggle-filenames t
))))))
954 (defun Bookmark-menu-mark ()
955 "Mark bookmark on this line to be displayed by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-select] command."
958 (if (Bookmark-menu-check-position)
959 (let ((buffer-read-only nil
))
964 (defun Bookmark-menu-select ()
965 "Select this line's bookmark; also display bookmarks marked with `>'.
966 You can mark bookmarks with the \\<Bookmark-menu-mode-map>\\[Bookmark-menu-mark] command."
968 (if (Bookmark-menu-check-position)
969 (let ((bmrk (Bookmark-menu-bookmark))
970 (menu (current-buffer))
973 (goto-char (point-min))
974 (while (re-search-forward "^>" nil t
)
975 (setq tem
(Bookmark-menu-bookmark))
976 (let ((buffer-read-only nil
))
979 (or (string-equal tem bmrk
)
981 (setq others
(cons tem others
))))
982 (setq others
(nreverse others
)
983 tem
(/ (1- (frame-height)) (1+ (length others
))))
984 (delete-other-windows)
987 (if (equal (length others
) 0)
990 (split-window nil tem
)
992 (bookmark-jump (car others
))
993 (setq others
(cdr others
)))
996 (defun Bookmark-menu-save (parg)
997 "Save the current list into a bookmark file.
998 With a prefix arg, prompts for a file to save them in."
1001 (save-window-excursion
1002 (bookmark-save parg
))))
1004 (defun Bookmark-menu-load ()
1005 "Load a bookmark file and rebuild list."
1007 (if (Bookmark-menu-check-position)
1009 (save-window-excursion
1010 (call-interactively 'bookmark-load
)))))
1012 (defun Bookmark-menu-1-window ()
1013 "Select this line's bookmark, alone, in full frame."
1015 (if (Bookmark-menu-check-position)
1017 (bookmark-jump (Bookmark-menu-bookmark))
1018 (bury-buffer (other-buffer))
1019 (delete-other-windows))))
1021 (defun Bookmark-menu-2-window ()
1022 "Select this line's bookmark, with previous buffer in second window."
1024 (if (Bookmark-menu-check-position)
1025 (let ((bmrk (Bookmark-menu-bookmark))
1026 (menu (current-buffer))
1028 (delete-other-windows)
1029 (switch-to-buffer (other-buffer))
1030 (let ((buff (car (bookmark-jump-noselect bmrk
))))
1031 (pop-to-buffer buff
))
1032 (bury-buffer menu
))))
1034 (defun Bookmark-menu-this-window ()
1035 "Select this line's bookmark in this window."
1037 (if (Bookmark-menu-check-position)
1038 (bookmark-jump (Bookmark-menu-bookmark))))
1040 (defun Bookmark-menu-other-window ()
1041 "Select this line's bookmark in other window, leaving bookmark menu visible."
1043 (if (Bookmark-menu-check-position)
1044 (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark)))))
1045 (switch-to-buffer-other-window buff
))))
1047 (defun Bookmark-menu-switch-other-window ()
1048 "Make the other window select this line's bookmark.
1049 The current window remains selected."
1051 (if (Bookmark-menu-check-position)
1052 (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark)))))
1053 (display-buffer buff
))))
1055 (defun Bookmark-menu-quit ()
1056 "Quit the bookmark menu."
1058 (let ((buffer (current-buffer)))
1059 (switch-to-buffer (other-buffer))
1060 (bury-buffer buffer
)))
1062 (defun Bookmark-menu-unmark (&optional backup
)
1063 "Cancel all requested operations on bookmark on this line and move down.
1064 Optional ARG means move up."
1067 (if (Bookmark-menu-check-position)
1069 (let ((buffer-read-only nil
))
1071 ;; any flags to reset according to circumstances? How about a
1072 ;; flag indicating whether this bookmark is being visited?
1073 ;; well, we don't have this now, so maybe later.
1075 (forward-line (if backup -
1 1)))))
1077 (defun Bookmark-menu-backup-unmark ()
1078 "Move up and cancel all requested operations on bookmark on line above."
1081 (if (Bookmark-menu-check-position)
1083 (Bookmark-menu-unmark)
1084 (forward-line -
1))))
1086 (defun Bookmark-menu-delete ()
1087 "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command."
1090 (if (Bookmark-menu-check-position)
1091 (let ((buffer-read-only nil
))
1096 (defun Bookmark-menu-delete-backwards ()
1097 "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command
1098 and then move up one line"
1100 (Bookmark-menu-delete)
1102 (if (Bookmark-menu-check-position)
1105 (defun Bookmark-menu-execute ()
1106 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1108 (let ((hide-em Bookmark-menu-toggle-filenames
))
1109 (if hide-em
(Bookmark-menu-hide-filenames))
1110 (setq Bookmark-menu-toggle-filenames nil
)
1111 (goto-char (point-min))
1114 (while (re-search-forward "^D" (point-max) t
)
1115 (setq deaders
(cons (Bookmark-menu-bookmark) deaders
)))
1116 (mapcar (lambda (str)
1117 (setq bookmark-alist
1118 (delq (assoc str bookmark-alist
) bookmark-alist
)))
1121 (goto-char (point-min))
1123 (setq Bookmark-menu-toggle-filenames hide-em
)
1124 (if Bookmark-menu-toggle-filenames
1125 (Bookmark-menu-toggle-filenames t
))))
1127 (defun Bookmark-menu-rename ()
1128 "Rename bookmark on current line. Prompts for a new name."
1130 (if (Bookmark-menu-check-position)
1131 (let ((bmrk (Bookmark-menu-bookmark))
1132 (thispoint (point)))
1133 (bookmark-rename bmrk
)
1135 (goto-char thispoint
))))
1137 (defun Bookmark-menu-locate ()
1138 "Display location of this bookmark. Displays in the minibuffer."
1140 (if (Bookmark-menu-check-position)
1141 (let ((bmrk (Bookmark-menu-bookmark)))
1142 (message (bookmark-locate bmrk t
)))))
1144 ;;;; bookmark menu bar stuff ;;;;
1146 (defvar bookmark-menu-bar-length
70
1147 "*Maximum length of a bookmark name displayed on a popup menu.")
1149 (defun bookmark-make-menu-bar-alist ()
1150 (bookmark-try-default-file)
1151 (bookmark-maybe-sort-alist)
1153 (mapcar (lambda (cell)
1154 (let ((str (car cell
)))
1156 (if (> (length str
) bookmark-menu-bar-length
)
1157 (substring str
0 bookmark-menu-bar-length
)
1161 (error "No bookmarks currently set.")))
1163 (defun bookmark-make-menu-bar-with-function (func-sym
1166 ;; help function for making menus that need to apply a bookmark
1167 ;; function to a string.
1168 (let* ((menu (bookmark-make-menu-bar-alist))
1169 (str (x-popup-menu event
1171 (cons menu-str menu
)))))
1172 (if str
(apply func-sym
(list str
)))))
1174 (defun bookmark-menu-bar-insert (event)
1175 "Insert the text of the file pointed to by bookmark BOOKMARK.
1176 You may have a problem using this function if the value of variable
1177 `bookmark-alist' is nil. If that happens, you need to load in some
1178 bookmarks. See help on function `bookmark-load' for more about
1181 (bookmark-make-menu-bar-with-function 'bookmark-insert
1182 "Bookmark Insert Menu"
1183 "--- Insert Contents ---"
1186 (defun bookmark-menu-bar-jump (event)
1187 "Jump to bookmark BOOKMARK (a point in some file).
1188 You may have a problem using this function if the value of variable
1189 `bookmark-alist' is nil. If that happens, you need to load in some
1190 bookmarks. See help on function `bookmark-load' for more about
1193 (bookmark-make-menu-bar-with-function 'bookmark-jump
1194 "Bookmark Jump Menu"
1195 "--- Jump to Bookmark ---"
1198 (defun bookmark-menu-bar-locate (event)
1199 "Insert the name of the file associated with BOOKMARK.
1200 \(This is not the same as the contents of that file\)."
1202 (bookmark-make-menu-bar-with-function 'bookmark-locate
1203 "Bookmark Locate Menu"
1204 "--- Insert Location ---"
1207 (defun bookmark-menu-bar-rename (event)
1208 "Change the name of OLD-BOOKMARK to NEWNAME.
1209 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
1210 If called from menubar, OLD-BOOKMARK is selected from a menu, and
1211 prompts for NEWNAME.
1212 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
1213 passed as an argument. If called with two strings, then no prompting
1214 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
1216 While you are entering the new name, consecutive C-w's insert
1217 consectutive words from the text of the buffer into the new bookmark
1218 name, and C-v inserts the name of the file."
1220 (bookmark-make-menu-bar-with-function 'bookmark-rename
1221 "Bookmark Rename Menu"
1222 "--- Rename Bookmark ---"
1225 (defun bookmark-menu-bar-delete (event)
1226 "Delete the bookmark named NAME from the bookmark list.
1227 Removes only the first instance of a bookmark with that name. If
1228 there are one or more other bookmarks with the same name, they will
1229 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1230 one most recently used in this file, if any\)."
1232 (bookmark-make-menu-bar-with-function 'bookmark-delete
1233 "Bookmark Delete Menu"
1234 "--- Delete Bookmark ---"
1237 ;; Thanks to Roland McGrath for fixing menubar.el so that the
1238 ;; following works, and for explaining what to do to make it work.
1241 (define-key global-map
[menu-bar bookmark
]
1242 '("Bookmarks" . menu-bar-bookmark-map
))
1244 (defvar menu-bar-bookmark-map
(make-sparse-keymap "Bookmark functions."))
1246 ;; make bookmarks appear toward the right side of the menu.
1247 (if (boundp 'menu-bar-final-items
)
1248 (if menu-bar-final-items
1249 (setq menu-bar-final-items
1250 (cons 'bookmark menu-bar-final-items
)))
1251 (setq menu-bar-final-items
'(bookmark)))
1253 (define-key menu-bar-bookmark-map
[load]
1254 '("Load a bookmark file" . bookmark-load))
1256 (define-key menu-bar-bookmark-map [write]
1257 '("Write \(to another file\)" . bookmark-write))
1259 (define-key menu-bar-bookmark-map [save]
1260 '("Save \(in default file\)" . bookmark-save))
1262 (define-key menu-bar-bookmark-map [edit]
1263 '("Edit Bookmark List" . list-bookmarks))
1265 (define-key menu-bar-bookmark-map [delete]
1266 '("Delete bookmark" . bookmark-menu-bar-delete))
1268 (define-key menu-bar-bookmark-map [rename]
1269 '("Rename bookmark" . bookmark-menu-bar-rename))
1271 (define-key menu-bar-bookmark-map [locate]
1272 '("Insert location" . bookmark-menu-bar-locate))
1274 (define-key menu-bar-bookmark-map [insert]
1275 '("Insert contents" . bookmark-menu-bar-insert))
1277 (define-key menu-bar-bookmark-map [set]
1278 '("Set bookmark" . bookmark-set))
1280 (define-key menu-bar-bookmark-map [jump]
1281 '("Jump to bookmark" . bookmark-menu-bar-jump))
1283 ;;;###autoload (autoload 'menu-bar-bookmark-map "bookmark" nil t 'keymap)
1285 (fset 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
1287 ;;;; end bookmark menu-bar stuff ;;;;
1291 ;;; bookmark.el ends here