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 ;; Thanks go to Andrew V. Klein <avk@rtsg.mot.com> for the code that
35 ;; sorts the alist before presenting it to the user (in list-bookmarks
38 ;; And much thanks to David Hughes <djh@harston.cv.com> for many small
39 ;; suggestions and the code to implement them (like
40 ;; Bookmark-menu-check-position, and some of the Lucid compatibility
43 ;; Kudos (whatever they are) go to Jim Blandy <jimb@cs.oberlin.edu>
44 ;; for his eminently sensible suggestion to separate bookmark-jump
45 ;; into bookmark-jump and bookmark-jump-noselect, which made many
46 ;; other things cleaner as well.
48 ;; Thanks to Roland McGrath for encouragement and help with defining
49 ;; autoloads on the menu-bar.
51 ;; Jonathan Stigelman <stig@key.amdahl.com> gave patches for default
52 ;; values in bookmark-jump and bookmark-set. Everybody please keep
53 ;; all the keystrokes they save thereby and send them to him at the
54 ;; end of each year :-) (No, seriously, thanks Jonathan!)
56 ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
60 ;; bookmark|Karl Fogel|kfogel@cs.oberlin.edu|
61 ;; Setting bookmarks in files or directories, jumping to them later.|
62 ;; 16-July-93|Version: 2.5|~/misc/bookmark.el.Z|
64 ;; Enough with the credits already, get on to the good stuff:
66 ;; FAVORITE CHINESE RESTAURANT:
67 ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
68 ;; Choice (both in Chicago's Chinatown). Well, both. How about you?
70 ;;; Commentary on code:
72 ;; bookmark alist format:
74 ;; (bookmark-name (filename
80 ;; bookmark-name is the string the user gives the bookmark and
81 ;; accesses it by from then on. filename is the location of the file
82 ;; in which the bookmark is set. string-in-front is a string of
83 ;; `bookmark-search-size' chars of context in front of the point the
84 ;; bookmark is set at, string-behind is the same thing after the
85 ;; point. bookmark-jump will search for string-behind and
86 ;; string-in-front in case the file has changed since the bookmark was
87 ;; set. It will attempt to place the user before the changes, if
90 ;; The bookmark list is sorted lexically by default, but you can turn
91 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
92 ;; the list will be presented in the order it is recorded
93 ;; (chronologically), which is actually fairly useful as well.
97 ;; Added for lucid emacs compatibility, db
98 (or (fboundp 'defalias
) (fset 'defalias
'fset
))
100 ;; suggested for lucid compatibility by david hughes:
101 (or (fboundp 'frame-height
) (fset 'frame-height
'screen-height
))
103 ;; some people have C-x r set to rmail or whatever. We don't want to
104 ;; assume that C-x r is a prefix map just because it's distributed
106 ;; These are the distribution keybindings suggested by RMS, everything
107 ;; else will be done with M-x or the menubar:
109 (if (or (symbolp (key-binding "\C-xr"))
110 (fboundp 'bookmark-set
))
112 (progn (define-key ctl-x-map
"rb" 'bookmark-jump
)
113 (define-key ctl-x-map
"rm" 'bookmark-set
)
114 (define-key ctl-x-map
"rl" 'list-bookmarks
)))
116 ;; define the map, so it can be bound by those who desire to do so:
119 (defvar bookmark-map nil
120 "Keymap containing bindings to bookmark functions.
121 It is not bound to any key by default: to bind it
122 so that you have a bookmark prefix, just use `global-set-key' and bind a
123 key of your choice to `bookmark-map'. All interactive bookmark
124 functions have a binding in this keymap.")
127 (define-prefix-command 'bookmark-map
)
129 ;; Read the help on all of these functions for details...
131 (define-key bookmark-map
"x" 'bookmark-set
)
133 (define-key bookmark-map
"m" 'bookmark-set
) ; "m" for "mark"
135 (define-key bookmark-map
"j" 'bookmark-jump
)
137 (define-key bookmark-map
"g" 'bookmark-jump
) ; "g" for "go"
139 (define-key bookmark-map
"i" 'bookmark-insert
)
141 (define-key bookmark-map
"e" 'edit-bookmarks
)
143 (define-key bookmark-map
"f" 'bookmark-locate
) ; "f" for "find"
145 (define-key bookmark-map
"r" 'bookmark-rename
)
147 (define-key bookmark-map
"d" 'bookmark-delete
)
149 (define-key bookmark-map
"l" 'bookmark-load
)
151 (define-key bookmark-map
"w" 'bookmark-write
)
153 (define-key bookmark-map
"s" 'bookmark-save
)
155 (defvar bookmark-alist
()
156 "Association list of bookmarks.
157 You probably don't want to change the value of this alist yourself;
158 instead, let the various bookmark functions do it for you.")
160 (defvar bookmarks-already-loaded nil
)
162 ;; just add the hook to make sure that people don't lose bookmarks
163 ;; when they kill Emacs, unless they don't want to save them.
164 (add-hook 'kill-emacs-hook
166 (lambda () (and (featurep 'bookmark
)
168 (bookmark-time-to-save-p t
)
171 ;; more stuff added by db.
173 (defvar bookmark-current-bookmark nil
174 "Name of bookmark most recently used in the current file.
175 It is buffer local, used to make moving a bookmark forward
176 through a file easier.")
178 (make-variable-buffer-local 'bookmark-current-bookmark
)
180 (defvar bookmark-save-flag t
181 "*Controls when Emacs saves bookmarks to a file.
182 --> Nil means never save bookmarks, except when `bookmark-save' is
183 explicitly called \(\\[bookmark-save]\).
184 --> t means save bookmarks when Emacs is killed.
185 --> Otherise, it should be a number that is the frequency with which
186 the bookmark list is saved \(i.e.: the number of times which
187 Emacs' bookmark list may be modified before it is automatically
188 saved.\). If it is a number, Emacs will also automatically save
189 bookmarks when it is killed.
191 Therefore, the way to get it to save every time you make or delete a
192 bookmark is to set this variable to 1 \(or 0, which produces the same
195 To specify the file in which to save them, modify the variable
196 bookmark-file, which is `~/.emacs-bkmrks' by default.")
198 (defvar bookmark-alist-modification-count
0
199 "Number of modifications to bookmark list since it was last saved.")
201 (defvar bookmark-file
"~/.emacs-bkmrks"
202 "*File in which to save bookmarks by default.")
204 (defvar bookmark-version-control
'nospecial
205 "This variable controls whether or not to make numbered backups of
206 the master bookmark file. It can have four values: t, nil, never, and
207 nospecial. The first three have the same meaning that they do for the
208 variable version-control, and the final value nospecial means just use
209 the value of version-control.")
211 (defvar bookmark-completion-ignore-case t
212 "*Non-nil means bookmark functions ignore case in completion.")
214 (defvar bookmark-sort-flag t
215 "*Non-nil means that bookmarks will be displayed sorted by bookmark
216 name. Otherwise they will be displayed in LIFO order (that is, most
217 recently set ones come first, oldest ones come last).")
219 (defvar bookmark-search-size
500
220 "Length of the context strings recorded on either side of a bookmark.")
222 (defvar bookmark-current-point
0)
223 (defvar bookmark-yank-point
0)
224 (defvar bookmark-current-buffer nil
)
227 (defun bookmark-set (&optional parg
)
228 "Set a bookmark named NAME inside a file.
229 With prefix arg, will not overwrite a bookmark that has the same name
230 as NAME if such a bookmark already exists, but instead will \"push\"
231 the new bookmark onto the bookmark alist. Thus the most recently set
232 bookmark with name NAME would be the one in effect at any given time,
233 but the others are still there, should you decide to delete the most
236 To yank words from the text of the buffer and use them as part of the
237 bookmark name, type C-w while setting a bookmark. Successive C-w's
238 yank successive words.
240 Typing C-v inserts the name of the current file being visited. Typing
241 C-u inserts the name of the last bookmark used in the buffer \(as an
242 aid in using a single bookmark name to track your progress through a
243 large file\). If no bookmark was used, then C-u behaves like C-v and
244 inserts the name of the file being visited.
246 Use \\[bookmark-delete] to remove bookmarks \(you give it a name,
247 and it removes only the first instance of a bookmark with that name from
248 the list of bookmarks.\)"
250 (if (not (bookmark-buffer-file-name))
251 (error "Buffer not visiting a file or directory."))
252 (bookmark-try-default-file)
253 (setq bookmark-current-point
(point))
254 (setq bookmark-yank-point
(point))
255 (setq bookmark-current-buffer
(current-buffer))
256 (let* ((default (or bookmark-current-bookmark
257 (buffer-name (current-buffer))))
259 (read-from-minibuffer
260 (format "Set bookmark (%s): " default
)
262 (let ((now-map (copy-keymap minibuffer-local-map
)))
263 (progn (define-key now-map
"\C-w"
265 (define-key now-map
"\C-v"
266 'bookmark-insert-current-file-name
)
267 (define-key now-map
"\C-u"
268 'bookmark-insert-current-bookmark
))
270 (and (string-equal str
"") (setq str default
))
272 (bookmark-make parg str
)
273 (setq bookmark-current-bookmark str
)
274 (if (get-buffer "*Bookmark List*") ;rebuild the bookmark list
276 (save-window-excursion
278 (goto-char bookmark-current-point
))))
280 (defun bookmark-insert-current-bookmark ()
281 ;; insert this buffer's value of bookmark-current-bookmark, default
282 ;; to file name if it's nil.
286 (set-buffer bookmark-current-buffer
)
287 bookmark-current-bookmark
)))
288 (if str
(insert str
) (bookmark-insert-current-file-name))))
290 (defun bookmark-insert-current-file-name ()
291 ;; insert the name (sans path) of the current file into the bookmark
292 ;; name that is being set.
294 (let ((str (save-excursion
295 (set-buffer bookmark-current-buffer
)
296 (bookmark-buffer-file-name))))
303 (defun bookmark-yank-word ()
305 ;; get the next word from the buffer and append it to the name of
306 ;; the bookmark currently being set.
307 (let ((string (save-excursion
308 (set-buffer bookmark-current-buffer
)
309 (goto-char bookmark-yank-point
)
314 (setq bookmark-yank-point
(point)))))))
317 (defun bookmark-make (parg str
)
318 (if (and (assoc str bookmark-alist
) (not parg
))
319 ;; already existing boookmark under that name and
320 ;; no prefix arg means just overwrite old bookmark
321 (setcdr (assoc str bookmark-alist
)
322 (list (bookmark-make-cell)))
324 ;; otherwise just cons it onto the front (either the bookmark
325 ;; doesn't exist already, or there is no prefix arg. In either
326 ;; case, we want the new bookmark consed onto the alist...)
331 (bookmark-make-cell))
334 (setq bookmark-current-bookmark str
)
335 (setq bookmark-alist-modification-count
336 (1+ bookmark-alist-modification-count
))
337 (if (bookmark-time-to-save-p)
340 (defun bookmark-make-cell ()
341 ;; make the cell that is the cdr of a bookmark alist element. It
343 ;; (filename search-forward-str search-back-str point)
345 (bookmark-buffer-file-name)
346 (if (>= (- (point-max) (point)) bookmark-search-size
)
349 (+ (point) bookmark-search-size
))
351 (if (>= (- (point) (point-min)) bookmark-search-size
)
354 (- (point) bookmark-search-size
))
358 (defun bookmark-buffer-file-name ()
361 (if (and (boundp 'dired-directory
) dired-directory
)
362 (if (stringp dired-directory
)
364 (car dired-directory
)))))
366 (defun bookmark-try-default-file ()
367 (and (not bookmarks-already-loaded
)
368 (null bookmark-alist
)
369 (file-readable-p (expand-file-name bookmark-file
))
371 (bookmark-load bookmark-file t t
)
372 (setq bookmarks-already-loaded t
))))
374 (defun bookmark-maybe-sort-alist ()
375 ;;Return the bookmark-alist for display. If the bookmark-sort-flag
376 ;;is non-nil, then return a sorted copy of the alist.
377 (if bookmark-sort-flag
379 (sort (copy-alist bookmark-alist
)
381 (lambda (x y
) (string-lessp (car x
) (car y
))))))))
384 (defun bookmark-jump (str)
385 "Jump to bookmark BOOKMARK (a point in some file).
386 You may have a problem using this function if the value of variable
387 `bookmark-alist' is nil. If that happens, you need to load in some
388 bookmarks. See help on function `bookmark-load' for more about
391 If the file pointed to by BOOKMARK no longer exists, you will be asked
392 if you wish to give the bookmark a new location, and bookmark-jump
393 will then jump to the new location, as well as recording it in place
394 of the old one in the permanent bookmark record."
395 (interactive (progn (bookmark-try-default-file)
396 (let* ((completion-ignore-case
397 bookmark-completion-ignore-case
)
400 (assoc bookmark-current-bookmark
402 bookmark-current-bookmark
)
403 (and (assoc (buffer-name (current-buffer))
405 (buffer-name (current-buffer)))))
409 (format "Jump to bookmark (%s): "
411 "Jump to bookmark: ")
415 (and (string-equal "" str
)
418 (let ((cell (bookmark-jump-noselect str
)))
420 (switch-to-buffer (car cell
))
421 (goto-char (cdr cell
)))))
423 (defun bookmark-jump-noselect (str)
424 ;; a leetle helper for bookmark-jump :-)
425 ;; returns (BUFFER . POINT)
426 (let ((whereto-list (car (cdr (assoc str bookmark-alist
)))))
427 (let* ((file (expand-file-name (car whereto-list
)))
429 (forward-str (car (cdr whereto-list
)))
430 (behind-str (car (cdr (cdr whereto-list
))))
431 (place (car (cdr (cdr (cdr whereto-list
))))))
434 ;; else try some common compression extensions
435 ;; and Emacs better handle it right!
438 (let ((altname (concat file
".Z")))
439 (and (file-exists-p altname
)
441 (let ((altname (concat file
".gz")))
442 (and (file-exists-p altname
)
444 (let ((altname (concat file
".z")))
445 (and (file-exists-p altname
)
448 (set-buffer (find-file-noselect file
))
450 ;; Go searching forward first. Then, if forward-str exists and
451 ;; was found in the file, we can search backward for behind-str.
452 ;; Rationale is that if text was inserted between the two in the
453 ;; file, it's better to be put before it so you can read it,
454 ;; rather than after and remain perhaps unaware of the changes.
456 (if (search-forward forward-str
(point-max) t
)
457 (backward-char bookmark-search-size
)))
459 (if (search-backward behind-str
(point-min) t
)
460 (forward-char bookmark-search-size
)))
462 (setq bookmark-current-bookmark str
)
463 (cons (current-buffer) (point)))
466 (if (y-or-n-p (concat (file-name-nondirectory orig-file
)
467 " nonexistent. Relocate \""
471 (bookmark-relocate str
)
472 ;; gasp! It's a recursive function call in Emacs Lisp!
473 (bookmark-jump-noselect str
))
475 "Bookmark not relocated, but deleting it would be a good idea.")
479 (defun bookmark-relocate (str)
480 "Relocate BOOKMARK -- prompts for a filename, and makes an already
481 existing bookmark point to that file, instead of the one it used to
482 point at. Useful when a file has been renamed after a bookmark was
484 (interactive (let ((completion-ignore-case
485 bookmark-completion-ignore-case
))
486 (progn (bookmark-try-default-file)
487 (list (completing-read
488 "Bookmark to relocate: "
492 (let* ((bmrk (assoc str bookmark-alist
))
493 (bmrk-filename (car (car (cdr bmrk
))))
494 (newloc (expand-file-name
496 (format "Relocate %s to: " str
)
497 (file-name-directory bmrk-filename
)))))
498 (setcar (car (cdr bmrk
)) newloc
)))
501 (defun bookmark-locate (str &optional no-insertion
)
502 "Insert the name of the file associated with BOOKMARK.
503 Optional second arg NO-INSERTION means merely return the filename as a
505 (interactive (let ((completion-ignore-case
506 bookmark-completion-ignore-case
))
507 (progn (bookmark-try-default-file)
508 (list (completing-read
509 "Insert bookmark location: "
513 (let ((where (car (car (cdr (assoc str bookmark-alist
))))))
519 (defun bookmark-rename (old &optional new
)
520 "Change the name of OLD-BOOKMARK to NEWNAME.
521 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
522 If called from menubar, OLD-BOOKMARK is selected from a menu, and
524 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
525 passed as an argument. If called with two strings, then no prompting
526 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
528 While you are entering the new name, consecutive C-w's insert
529 consectutive words from the text of the buffer into the new bookmark
530 name, and C-v inserts the name of the file."
531 (interactive (let ((completion-ignore-case
532 bookmark-completion-ignore-case
))
533 (progn (bookmark-try-default-file)
534 (list (completing-read "Old bookmark name: "
539 (setq bookmark-current-point
(point))
540 (setq bookmark-yank-point
(point))
541 (setq bookmark-current-buffer
(current-buffer))
542 (let ((cell (assoc old bookmark-alist
))
544 (or new
; use second arg, if non-nil
545 (read-from-minibuffer
548 (let ((now-map (copy-keymap minibuffer-local-map
)))
549 (progn (define-key now-map
"\C-w"
551 (define-key now-map
"\C-v"
552 'bookmark-insert-current-file-name
))
556 (setq bookmark-current-bookmark str
)
557 (if (get-buffer "*Bookmark List*")
558 (save-excursion (save-window-excursion (list-bookmarks))))
559 (setq bookmark-alist-modification-count
560 (1+ bookmark-alist-modification-count
))
561 (if (bookmark-time-to-save-p)
565 (defun bookmark-insert (str)
566 "Insert the text of the file pointed to by bookmark BOOKMARK.
567 You may have a problem using this function if the value of variable
568 `bookmark-alist' is nil. If that happens, you need to load in some
569 bookmarks. See help on function `bookmark-load' for more about
571 (interactive (let ((completion-ignore-case
572 bookmark-completion-ignore-case
))
573 (progn (bookmark-try-default-file)
574 (list (completing-read
575 "Insert bookmark contents: "
579 (let ((orig-point (point))
582 (set-buffer (car (bookmark-jump-noselect str
)))
583 (buffer-substring (point-min) (point-max)))))
584 (insert str-to-insert
)
586 (goto-char orig-point
)))
589 (defun bookmark-delete (str)
590 "Delete the bookmark named NAME from the bookmark list.
591 Removes only the first instance of a bookmark with that name. If
592 there are one or more other bookmarks with the same name, they will
593 not be deleted. Defaults to the \"current\" bookmark \(that is, the
594 one most recently used in this file, if any\)."
595 (interactive (let ((completion-ignore-case
596 bookmark-completion-ignore-case
))
597 (progn (bookmark-try-default-file)
604 bookmark-current-bookmark
)))))
605 (let ((will-go (assoc str bookmark-alist
)))
606 (setq bookmark-alist
(delq will-go bookmark-alist
))
607 ;; Added by db, nil bookmark-current-bookmark if the last
608 ;; occurence has been deleted
609 (or (assoc bookmark-current-bookmark bookmark-alist
)
610 (setq bookmark-current-bookmark nil
)))
611 (if (get-buffer "*Bookmark List*")
612 (save-excursion (save-window-excursion (list-bookmarks))))
613 (setq bookmark-alist-modification-count
614 (1+ bookmark-alist-modification-count
))
615 (if (bookmark-time-to-save-p)
618 (defun bookmark-time-to-save-p (&optional last-time
)
619 ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu>
620 ;; finds out whether it's time to save bookmarks to a file, by
621 ;; examining the value of variable bookmark-save-flag, and maybe
622 ;; bookmark-alist-modification-count. Returns t if they should be
623 ;; saved, nil otherwise. if last-time is non-nil, then this is
624 ;; being called when emacs is killed.
626 (and (> bookmark-alist-modification-count
0)
628 ((numberp bookmark-save-flag
)
629 (>= bookmark-alist-modification-count bookmark-save-flag
))
634 (defun bookmark-write ()
635 "Write bookmarks to a file \(for which the user will be prompted
636 interactively\). Don't use this in Lisp programs; use bookmark-save
639 (bookmark-try-default-file)
643 (defun bookmark-save (&optional parg file
)
644 "Save currently defined bookmarks.
645 Saves by default in the file defined by the variable
646 `bookmark-file'. With a prefix arg, save it in file FILE.
648 If you are calling this from Lisp, the two arguments are PREFIX-ARG
649 and FILE, and if you just want it to write to the default file, then
650 pass no arguments. Or pass in nil and FILE, and it will save in FILE
651 instead. If you pass in one argument, and it is non-nil, then the
652 user will be interactively queried for a file to save in.
654 When you want to load in the bookmarks from a file, use
655 \`bookmark-load\', \\[bookmark-load]. That function will prompt you
656 for a file, defaulting to the file defined by variable
659 (bookmark-try-default-file)
661 ((and (null parg
) (null file
))
662 ;;whether interactive or not, write to default file
663 (bookmark-write-file bookmark-file
))
664 ((and (null parg
) file
)
665 ;;whether interactive or not, write to given file
666 (bookmark-write-file file
))
667 ((and parg
(not file
))
668 ;;have been called interactively w/ prefix arg
669 (let ((file (read-file-name "File to save bookmarks in: ")))
670 (bookmark-write-file file
)))
671 (t ; someone called us with prefix-arg *and* a file, so just write to file
672 (bookmark-write-file file
)))
673 ;; signal that we have synced the bookmark file by setting this to
674 ;; 0. If there was an error at any point before, it will not get
675 ;; set, which is what we want.
676 (setq bookmark-alist-modification-count
0))
678 (defun bookmark-write-file (file)
680 (save-window-excursion
681 (if (>= baud-rate
9600)
682 (message (format "Saving bookmarks to file %s." file
)))
683 (set-buffer (let ((enable-local-variables nil
))
684 (find-file-noselect file
)))
685 (goto-char (point-min))
686 (delete-region (point-min) (point-max))
687 (print bookmark-alist
(current-buffer))
688 (let ((version-control
690 ((null bookmark-version-control
) nil
)
691 ((eq 'never bookmark-version-control
) 'never
)
692 ((eq 'nospecial bookmark-version-control
) version-control
)
696 (kill-buffer (current-buffer))))))
699 (defun bookmark-load (file &optional revert no-msg
)
700 "Load bookmarks from FILE (which must be in bookmark format).
701 Appends loaded bookmarks to the front of the list of bookmarks. If
702 optional second argument REVERT is non-nil, existing bookmarks are
703 destroyed. Optional third arg NO-MSG means don't display any messages
706 If you load a file that doesn't contain a proper bookmark alist, you
707 will corrupt Emacs's bookmark list. Generally, you should only load
708 in files that were created with the bookmark functions in the first
709 place. Your own personal bookmark file, `~/.emacs-bkmrks', is
710 maintained automatically by Emacs; you shouldn't need to load it
713 (progn (bookmark-try-default-file)
714 (list (read-file-name
715 (format "Load bookmarks from: (%s) "
717 ;;Default might not be used often,
718 ;;but there's no better default, and
719 ;;I guess it's better than none at all.
720 "~/" bookmark-file
'confirm
))))
721 (setq file
(expand-file-name file
))
722 (if (file-readable-p file
)
724 (save-window-excursion
725 (if (and (null no-msg
) (>= baud-rate
9600))
726 (message (format "Loading bookmarks from %s..." file
)))
727 (set-buffer (let ((enable-local-variables nil
))
728 (find-file-noselect file
)))
729 (goto-char (point-min))
730 (let ((blist (car (read-from-string
731 (buffer-substring (point-min) (point-max))))))
735 (setq bookmark-alist-modification-count
736 (1+ bookmark-alist-modification-count
))
737 (setq bookmark-alist-modification-count
0))
739 (append blist
(if (not revert
) bookmark-alist
)))
740 (if (get-buffer "*Bookmark List*")
741 (save-excursion (list-bookmarks))))
742 (error (format "Invalid bookmark list in %s." file
))))
743 (kill-buffer (current-buffer)))
744 (if (and (null no-msg
) (>= baud-rate
9600))
745 (message (format "Loading bookmarks from %s... done" file
))))
746 (error (format "Cannot read bookmark file %s." file
))))
748 ;;;; bookmark-menu-mode stuff ;;;;
750 (defvar Bookmark-menu-bookmark-column nil
)
752 (defvar Bookmark-menu-hidden-bookmarks
())
754 (defvar Bookmark-menu-file-column
30
755 "*Column at which to display filenames in a buffer listing bookmarks.
756 You can toggle whether files are shown with \\<Bookmark-menu-mode-map>\\[Bookmark-menu-toggle-filenames].")
758 (defvar Bookmark-menu-toggle-filenames t
759 "*Non-nil means show filenames when listing bookmarks.
760 This may result in truncated bookmark names. To disable this, put the
761 following in your .emacs:
763 \(setq Bookmark-menu-toggle-filenames nil\)")
765 (defvar Bookmark-menu-mode-map nil
)
767 (if Bookmark-menu-mode-map
769 (setq Bookmark-menu-mode-map
(make-keymap))
770 (suppress-keymap Bookmark-menu-mode-map t
)
771 (define-key Bookmark-menu-mode-map
"q" 'Bookmark-menu-quit
)
772 (define-key Bookmark-menu-mode-map
"v" 'Bookmark-menu-select
)
773 (define-key Bookmark-menu-mode-map
"w" 'Bookmark-menu-locate
)
774 (define-key Bookmark-menu-mode-map
"2" 'Bookmark-menu-2-window
)
775 (define-key Bookmark-menu-mode-map
"1" 'Bookmark-menu-1-window
)
776 (define-key Bookmark-menu-mode-map
"j" 'Bookmark-menu-this-window
)
777 (define-key Bookmark-menu-mode-map
"f" 'Bookmark-menu-this-window
)
778 (define-key Bookmark-menu-mode-map
"o" 'Bookmark-menu-other-window
)
779 (define-key Bookmark-menu-mode-map
"\C-o" 'Bookmark-menu-switch-other-window
)
780 (define-key Bookmark-menu-mode-map
"s" 'Bookmark-menu-save
)
781 (define-key Bookmark-menu-mode-map
"k" 'Bookmark-menu-delete
)
782 (define-key Bookmark-menu-mode-map
"\C-d" 'Bookmark-menu-delete-backwards
)
783 (define-key Bookmark-menu-mode-map
"x" 'Bookmark-menu-execute
)
784 (define-key Bookmark-menu-mode-map
"\C-k" 'Bookmark-menu-delete
)
785 (define-key Bookmark-menu-mode-map
"d" 'Bookmark-menu-delete
)
786 (define-key Bookmark-menu-mode-map
" " 'next-line
)
787 (define-key Bookmark-menu-mode-map
"n" 'next-line
)
788 (define-key Bookmark-menu-mode-map
"p" 'previous-line
)
789 (define-key Bookmark-menu-mode-map
"\177" 'Bookmark-menu-backup-unmark
)
790 (define-key Bookmark-menu-mode-map
"?" 'describe-mode
)
791 (define-key Bookmark-menu-mode-map
"u" 'Bookmark-menu-unmark
)
792 (define-key Bookmark-menu-mode-map
"m" 'Bookmark-menu-mark
)
793 (define-key Bookmark-menu-mode-map
"l" 'Bookmark-menu-load
)
794 (define-key Bookmark-menu-mode-map
"r" 'Bookmark-menu-rename
)
795 (define-key Bookmark-menu-mode-map
"t" 'Bookmark-menu-toggle-filenames
))
797 ;; Bookmark Menu mode is suitable only for specially formatted data.
798 (put 'Bookmark-menu-mode
'mode-class
'special
)
800 ;; need to display whether or not bookmark exists as a buffer in flag
804 ;; FLAGS BOOKMARK (/FILE/NAME/HERE/WHAT/REGEXP/TO/USE?)
805 ;; goto bookmark-column and then search till "(/[^)]*)$" or "(/.*)$" ?
808 (defalias 'edit-bookmarks
'list-bookmarks
)
811 (defun list-bookmarks ()
812 "Display a list of existing bookmarks.
813 The list is displayed in a buffer named `*Bookmark List*'.
814 The leftmost column displays a D if the bookmark is flagged for
815 deletion, or > if it is flagged for displaying."
817 (bookmark-try-default-file)
818 (switch-to-buffer (get-buffer-create "*Bookmark List*"))
819 (let ((buffer-read-only nil
))
820 (delete-region (point-max) (point-min))
821 (goto-char (point-min)) ;sure are playing it safe...
822 (insert "% Bookmark\n- --------\n")
823 (bookmark-maybe-sort-alist)
824 (let ((lst bookmark-alist
))
827 (concat " " (car (car lst
)) "\n"))
828 (setq lst
(cdr lst
)))))
829 (goto-char (point-min))
832 (if Bookmark-menu-toggle-filenames
833 (Bookmark-menu-toggle-filenames t
)))
835 (defun bookmark-menu-mode ()
836 "Major mode for editing a list of bookmarks.
837 Each line describes one of the bookmarks in Emacs.
838 Letters do not insert themselves; instead, they are commands.
839 \\<Bookmark-menu-mode-map>
840 \\[Bookmark-menu-mark] -- mark bookmark to be displayed.
841 \\[Bookmark-menu-select] -- select bookmark of line point is on.
842 Also show bookmarks marked using m in other windows.
843 \\[Bookmark-menu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
844 \\[Bookmark-menu-locate] -- display (in minibuffer) location of this bookmark.
845 \\[Bookmark-menu-1-window] -- select this bookmark in full-frame window.
846 \\[Bookmark-menu-2-window] -- select this bookmark in one window,
847 together with bookmark selected before this one in another window.
848 \\[Bookmark-menu-this-window] -- select this bookmark in place of the bookmark menu buffer.
849 \\[Bookmark-menu-other-window] -- select this bookmark in another window,
850 so the bookmark menu bookmark remains visible in its window.
851 \\[Bookmark-menu-switch-other-window] -- switch the other window to this bookmark.
852 \\[Bookmark-menu-rename] -- rename this bookmark \(prompts for new name\).
853 \\[Bookmark-menu-delete] -- mark this bookmark to be deleted, and move down.
854 \\[Bookmark-menu-delete-backwards] -- mark this bookmark to be deleted, and move up.
855 \\[Bookmark-menu-execute] -- delete marked bookmarks.
856 \\[Bookmark-menu-save] -- save the current bookmark list in the default file.
857 With a prefix arg, prompts for a file to save in.
858 \\[Bookmark-menu-load] -- load in a file of bookmarks (prompts for file.)
859 \\[Bookmark-menu-unmark] -- remove all kinds of marks from current line.
860 With prefix argument, also move up one line.
861 \\[Bookmark-menu-backup-unmark] -- back up a line and remove marks."
862 (kill-all-local-variables)
863 (use-local-map Bookmark-menu-mode-map
)
864 (setq truncate-lines t
)
865 (setq buffer-read-only t
)
866 (setq major-mode
'bookmark-menu-mode
)
867 (setq mode-name
"Bookmark Menu")
868 (run-hooks 'bookmark-menu-mode-hook
))
870 (defun Bookmark-menu-toggle-filenames (&optional parg
)
871 "Toggle whether filenames are shown in the bookmark list.
872 Optional argument SHOW means show them unconditionally."
876 (setq Bookmark-menu-toggle-filenames nil
)
877 (Bookmark-menu-show-filenames)
878 (setq Bookmark-menu-toggle-filenames t
))
879 (Bookmark-menu-toggle-filenames
880 (Bookmark-menu-hide-filenames)
881 (setq Bookmark-menu-toggle-filenames nil
))
883 (Bookmark-menu-show-filenames)
884 (setq Bookmark-menu-toggle-filenames t
))))
886 (defun Bookmark-menu-show-filenames (&optional force
)
887 (if (and (not force
) Bookmark-menu-toggle-filenames
)
888 nil
;already shown, so do nothing
890 (save-window-excursion
891 (goto-char (point-min))
893 (setq Bookmark-menu-hidden-bookmarks
())
894 (let ((buffer-read-only nil
))
895 (while (< (point) (point-max))
896 (let ((bmrk (Bookmark-menu-bookmark)))
897 (setq Bookmark-menu-hidden-bookmarks
898 (cons bmrk Bookmark-menu-hidden-bookmarks
))
899 (move-to-column Bookmark-menu-file-column t
)
900 (delete-region (point) (progn (end-of-line) (point)))
902 (bookmark-locate bmrk
)
903 (forward-line 1))))))))
905 (defun Bookmark-menu-hide-filenames (&optional force
)
906 (if (and (not force
) Bookmark-menu-toggle-filenames
)
907 ;; nothing to hide if above is nil
909 (save-window-excursion
910 (goto-char (point-min))
912 (setq Bookmark-menu-hidden-bookmarks
913 (nreverse Bookmark-menu-hidden-bookmarks
))
915 (goto-char (point-min))
916 (search-forward "Bookmark")
918 (setq Bookmark-menu-bookmark-column
(current-column)))
920 (let ((buffer-read-only nil
))
921 (while Bookmark-menu-hidden-bookmarks
922 (move-to-column Bookmark-menu-bookmark-column t
)
924 (insert (car Bookmark-menu-hidden-bookmarks
))
925 (setq Bookmark-menu-hidden-bookmarks
926 (cdr Bookmark-menu-hidden-bookmarks
))
927 (forward-line 1))))))))
929 ;; if you look at this next function from far away, it resembles a
930 ;; gun. But only with this comment above...
931 (defun Bookmark-menu-check-position ()
932 ;; Returns t if on a line with a bookmark.
933 ;; Otherwise, repositions and returns t.
934 ;; written by David Hughes <djh@harston.cv.com>
935 ;; Mucho thanks, David! -karl
936 (cond ((< (count-lines (point-min) (point)) 2)
937 (goto-char (point-min))
941 (beginning-of-line 0)
946 (defun Bookmark-menu-bookmark ()
947 ;; return a string which is bookmark of this line.
948 (if (Bookmark-menu-check-position)
950 (save-window-excursion
951 (goto-char (point-min))
952 (search-forward "Bookmark")
954 (setq Bookmark-menu-bookmark-column
(current-column)))))
955 (if Bookmark-menu-toggle-filenames
956 (Bookmark-menu-hide-filenames))
958 (save-window-excursion
960 (forward-char Bookmark-menu-bookmark-column
)
962 (buffer-substring (point)
966 ;; well, this is certainly crystal-clear:
967 (if Bookmark-menu-toggle-filenames
968 (Bookmark-menu-toggle-filenames t
))))))
970 (defun Bookmark-menu-mark ()
971 "Mark bookmark on this line to be displayed by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-select] command."
974 (if (Bookmark-menu-check-position)
975 (let ((buffer-read-only nil
))
980 (defun Bookmark-menu-select ()
981 "Select this line's bookmark; also display bookmarks marked with `>'.
982 You can mark bookmarks with the \\<Bookmark-menu-mode-map>\\[Bookmark-menu-mark] command."
984 (if (Bookmark-menu-check-position)
985 (let ((bmrk (Bookmark-menu-bookmark))
986 (menu (current-buffer))
989 (goto-char (point-min))
990 (while (re-search-forward "^>" nil t
)
991 (setq tem
(Bookmark-menu-bookmark))
992 (let ((buffer-read-only nil
))
995 (or (string-equal tem bmrk
)
997 (setq others
(cons tem others
))))
998 (setq others
(nreverse others
)
999 tem
(/ (1- (frame-height)) (1+ (length others
))))
1000 (delete-other-windows)
1001 (bookmark-jump bmrk
)
1003 (if (equal (length others
) 0)
1006 (split-window nil tem
)
1008 (bookmark-jump (car others
))
1009 (setq others
(cdr others
)))
1010 (other-window 1)))))
1012 (defun Bookmark-menu-save (parg)
1013 "Save the current list into a bookmark file.
1014 With a prefix arg, prompts for a file to save them in."
1017 (save-window-excursion
1018 (bookmark-save parg
))))
1020 (defun Bookmark-menu-load ()
1021 "Load a bookmark file and rebuild list."
1023 (if (Bookmark-menu-check-position)
1025 (save-window-excursion
1026 (call-interactively 'bookmark-load
)))))
1028 (defun Bookmark-menu-1-window ()
1029 "Select this line's bookmark, alone, in full frame."
1031 (if (Bookmark-menu-check-position)
1033 (bookmark-jump (Bookmark-menu-bookmark))
1034 (bury-buffer (other-buffer))
1035 (delete-other-windows))))
1037 (defun Bookmark-menu-2-window ()
1038 "Select this line's bookmark, with previous buffer in second window."
1040 (if (Bookmark-menu-check-position)
1041 (let ((bmrk (Bookmark-menu-bookmark))
1042 (menu (current-buffer))
1044 (delete-other-windows)
1045 (switch-to-buffer (other-buffer))
1046 (let ((buff (car (bookmark-jump-noselect bmrk
))))
1047 (pop-to-buffer buff
))
1048 (bury-buffer menu
))))
1050 (defun Bookmark-menu-this-window ()
1051 "Select this line's bookmark in this window."
1053 (if (Bookmark-menu-check-position)
1054 (bookmark-jump (Bookmark-menu-bookmark))))
1056 (defun Bookmark-menu-other-window ()
1057 "Select this line's bookmark in other window, leaving bookmark menu visible."
1059 (if (Bookmark-menu-check-position)
1060 (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark)))))
1061 (switch-to-buffer-other-window buff
))))
1063 (defun Bookmark-menu-switch-other-window ()
1064 "Make the other window select this line's bookmark.
1065 The current window remains selected."
1067 (if (Bookmark-menu-check-position)
1068 (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark)))))
1069 (display-buffer buff
))))
1071 (defun Bookmark-menu-quit ()
1072 "Quit the bookmark menu."
1074 (let ((buffer (current-buffer)))
1075 (switch-to-buffer (other-buffer))
1076 (bury-buffer buffer
)))
1078 (defun Bookmark-menu-unmark (&optional backup
)
1079 "Cancel all requested operations on bookmark on this line and move down.
1080 Optional ARG means move up."
1083 (if (Bookmark-menu-check-position)
1085 (let ((buffer-read-only nil
))
1087 ;; any flags to reset according to circumstances? How about a
1088 ;; flag indicating whether this bookmark is being visited?
1089 ;; well, we don't have this now, so maybe later.
1091 (forward-line (if backup -
1 1)))))
1093 (defun Bookmark-menu-backup-unmark ()
1094 "Move up and cancel all requested operations on bookmark on line above."
1097 (if (Bookmark-menu-check-position)
1099 (Bookmark-menu-unmark)
1100 (forward-line -
1))))
1102 (defun Bookmark-menu-delete ()
1103 "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command."
1106 (if (Bookmark-menu-check-position)
1107 (let ((buffer-read-only nil
))
1112 (defun Bookmark-menu-delete-backwards ()
1113 "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command
1114 and then move up one line"
1116 (Bookmark-menu-delete)
1118 (if (Bookmark-menu-check-position)
1121 (defun Bookmark-menu-execute ()
1122 "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
1124 (let ((hide-em Bookmark-menu-toggle-filenames
))
1125 (if hide-em
(Bookmark-menu-hide-filenames))
1126 (setq Bookmark-menu-toggle-filenames nil
)
1127 (goto-char (point-min))
1130 (while (re-search-forward "^D" (point-max) t
)
1131 (setq deaders
(cons (Bookmark-menu-bookmark) deaders
)))
1132 (mapcar (lambda (str)
1133 (setq bookmark-alist
1134 (delq (assoc str bookmark-alist
) bookmark-alist
)))
1137 (goto-char (point-min))
1139 (setq Bookmark-menu-toggle-filenames hide-em
)
1140 (if Bookmark-menu-toggle-filenames
1141 (Bookmark-menu-toggle-filenames t
))))
1143 (defun Bookmark-menu-rename ()
1144 "Rename bookmark on current line. Prompts for a new name."
1146 (if (Bookmark-menu-check-position)
1147 (let ((bmrk (Bookmark-menu-bookmark))
1148 (thispoint (point)))
1149 (bookmark-rename bmrk
)
1151 (goto-char thispoint
))))
1153 (defun Bookmark-menu-locate ()
1154 "Display location of this bookmark. Displays in the minibuffer."
1156 (if (Bookmark-menu-check-position)
1157 (let ((bmrk (Bookmark-menu-bookmark)))
1158 (message (bookmark-locate bmrk t
)))))
1160 ;;;; bookmark menu bar stuff ;;;;
1162 (defvar bookmark-menu-bar-length
70
1163 "*Maximum length of a bookmark name displayed on a popup menu.")
1165 (defun bookmark-make-menu-bar-alist ()
1166 (bookmark-try-default-file)
1167 (bookmark-maybe-sort-alist)
1169 (mapcar (lambda (cell)
1170 (let ((str (car cell
)))
1172 (if (> (length str
) bookmark-menu-bar-length
)
1173 (substring str
0 bookmark-menu-bar-length
)
1177 (error "No bookmarks currently set.")))
1179 (defun bookmark-make-menu-bar-with-function (func-sym
1182 ;; help function for making menus that need to apply a bookmark
1183 ;; function to a string.
1184 (let* ((menu (bookmark-make-menu-bar-alist))
1185 (str (x-popup-menu event
1187 (cons menu-str menu
)))))
1188 (if str
(apply func-sym
(list str
)))))
1191 (defun bookmark-menu-bar-insert (event)
1192 "Insert the text of the file pointed to by bookmark BOOKMARK.
1193 You may have a problem using this function if the value of variable
1194 `bookmark-alist' is nil. If that happens, you need to load in some
1195 bookmarks. See help on function `bookmark-load' for more about
1198 (bookmark-make-menu-bar-with-function 'bookmark-insert
1199 "Bookmark Insert Menu"
1200 "--- Insert Contents ---"
1204 (defun bookmark-menu-bar-jump (event)
1205 "Jump to bookmark BOOKMARK (a point in some file).
1206 You may have a problem using this function if the value of variable
1207 `bookmark-alist' is nil. If that happens, you need to load in some
1208 bookmarks. See help on function `bookmark-load' for more about
1211 (bookmark-make-menu-bar-with-function 'bookmark-jump
1212 "Bookmark Jump Menu"
1213 "--- Jump to Bookmark ---"
1217 (defun bookmark-menu-bar-locate (event)
1218 "Insert the name of the file associated with BOOKMARK.
1219 \(This is not the same as the contents of that file\)."
1221 (bookmark-make-menu-bar-with-function 'bookmark-locate
1222 "Bookmark Locate Menu"
1223 "--- Insert Location ---"
1227 (defun bookmark-menu-bar-rename (event)
1228 "Change the name of OLD-BOOKMARK to NEWNAME.
1229 If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME.
1230 If called from menubar, OLD-BOOKMARK is selected from a menu, and
1231 prompts for NEWNAME.
1232 If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was
1233 passed as an argument. If called with two strings, then no prompting
1234 is done. You must pass at least OLD-BOOKMARK when calling from Lisp.
1236 While you are entering the new name, consecutive C-w's insert
1237 consectutive words from the text of the buffer into the new bookmark
1238 name, and C-v inserts the name of the file."
1240 (bookmark-make-menu-bar-with-function 'bookmark-rename
1241 "Bookmark Rename Menu"
1242 "--- Rename Bookmark ---"
1246 (defun bookmark-menu-bar-delete (event)
1247 "Delete the bookmark named NAME from the bookmark list.
1248 Removes only the first instance of a bookmark with that name. If
1249 there are one or more other bookmarks with the same name, they will
1250 not be deleted. Defaults to the \"current\" bookmark \(that is, the
1251 one most recently used in this file, if any\)."
1253 (bookmark-make-menu-bar-with-function 'bookmark-delete
1254 "Bookmark Delete Menu"
1255 "--- Delete Bookmark ---"
1258 ;; Thanks to Roland McGrath for fixing menubar.el so that the
1259 ;; following works, and for explaining what to do to make it work.
1262 (defvar menu-bar-bookmark-map
(make-sparse-keymap "Bookmark functions."))
1265 (fset 'menu-bar-bookmark-map
(symbol-value 'menu-bar-bookmark-map
))
1268 (define-key menu-bar-bookmark-map
[load]
1269 '("Load a bookmark file" . bookmark-load))
1272 (define-key menu-bar-bookmark-map [write]
1273 '("Write \(to another file\)" . bookmark-write))
1276 (define-key menu-bar-bookmark-map [save]
1277 '("Save \(in default file\)" . bookmark-save))
1280 (define-key menu-bar-bookmark-map [edit]
1281 '("Edit Bookmark List" . list-bookmarks))
1284 (define-key menu-bar-bookmark-map [delete]
1285 '("Delete bookmark" . bookmark-menu-bar-delete))
1288 (define-key menu-bar-bookmark-map [rename]
1289 '("Rename bookmark" . bookmark-menu-bar-rename))
1292 (define-key menu-bar-bookmark-map [locate]
1293 '("Insert location" . bookmark-menu-bar-locate))
1296 (define-key menu-bar-bookmark-map [insert]
1297 '("Insert contents" . bookmark-menu-bar-insert))
1300 (define-key menu-bar-bookmark-map [set]
1301 '("Set bookmark" . bookmark-set))
1304 (define-key menu-bar-bookmark-map [jump]
1305 '("Jump to bookmark" . bookmark-menu-bar-jump))
1307 (fset 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map))
1309 ;;;; end bookmark menu-bar stuff ;;;;
1313 ;;; bookmark.el ends here