;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
-;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2003 Free Software Foundation
+;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
+;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
;; Author: Karl Fogel <kfogel@red-bean.com>
;; Maintainer: Karl Fogel <kfogel@red-bean.com>
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
;;; Commentary:
(defcustom bookmark-automatically-show-annotations t
- "*nil means don't show annotations when jumping to a bookmark."
+ "*Non-nil means show annotations when jumping to a bookmark."
:type 'boolean
:group 'bookmark)
:group 'bookmark)
-;;; No user-serviceable parts beyond this point.
+(defface bookmark-menu-heading
+ '((t (:inherit font-lock-type-face)))
+ "Face used to highlight the heading in bookmark menu buffers."
+ :group 'bookmark
+ :version "22.1")
-;; Is it XEmacs?
-(defconst bookmark-xemacsp
- (string-match "\\(Lucid\\|Xemacs\\)" emacs-version))
+;;; No user-serviceable parts beyond this point.
;; Added for lucid emacs compatibility, db
(or (fboundp 'defalias) (fset 'defalias 'fset))
;; suggested for lucid compatibility by david hughes:
(or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
-;; This variable is probably obsolete now...
-(or (boundp 'baud-rate)
- ;; some random value higher than 9600
- (setq baud-rate 19200))
-
-
\f
;;; Keymap stuff:
;; Read the help on all of these functions for details...
;;;###autoload (define-key bookmark-map "x" 'bookmark-set)
-;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark"
+;;;###autoload (define-key bookmark-map "m" 'bookmark-set) ;"m"ark
;;;###autoload (define-key bookmark-map "j" 'bookmark-jump)
-;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go"
+;;;###autoload (define-key bookmark-map "g" 'bookmark-jump) ;"g"o
+;;;###autoload (define-key bookmark-map "o" 'bookmark-jump-other-window)
;;;###autoload (define-key bookmark-map "i" 'bookmark-insert)
;;;###autoload (define-key bookmark-map "e" 'edit-bookmarks)
-;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ; "f" for "find"
+;;;###autoload (define-key bookmark-map "f" 'bookmark-insert-location) ;"f"ind
;;;###autoload (define-key bookmark-map "r" 'bookmark-rename)
;;;###autoload (define-key bookmark-map "d" 'bookmark-delete)
;;;###autoload (define-key bookmark-map "l" 'bookmark-load)
(defvar bookmark-yank-point 0)
(defvar bookmark-current-buffer nil)
-
+(defvar Info-current-node)
+(defvar Info-suffix-list)
\f
;; Helper functions.
(defun bookmark-get-bookmark (bookmark)
- "Return the full entry for BOOKMARK in bookmark-alist.
+ "Return the full entry for BOOKMARK in `bookmark-alist'.
If BOOKMARK is not a string, return nil."
(when (stringp bookmark)
(assoc-string bookmark bookmark-alist bookmark-completion-ignore-case)))
(defun bookmark-get-bookmark-record (bookmark)
- "Return the guts of the entry for BOOKMARK in bookmark-alist.
+ "Return the guts of the entry for BOOKMARK in `bookmark-alist'.
That is, all information but the name."
(car (cdr (bookmark-get-bookmark bookmark))))
(if cell
(setcdr cell filename)
(nconc (bookmark-get-bookmark-record bookmark)
- (list (cons 'filename filename))))))
+ (list (cons 'filename filename))))
+ (setq bookmark-alist-modification-count
+ (1+ bookmark-alist-modification-count))
+ (if (bookmark-time-to-save-p)
+ (bookmark-save))))
(defun bookmark-get-position (bookmark)
(list (cons 'info-node node)))))
(message "%S" (assq 'info-node (bookmark-get-bookmark-record bookmark)))
- (sit-for 4)
- )
+ (sit-for 4))
(defvar bookmark-history nil
INFO-NODE, so record this fact in the bookmark's entry."
(bookmark-maybe-load-default-file)
(let ((stripped-name (copy-sequence name)))
- (or bookmark-xemacsp
+ (or (featurep 'xemacs)
;; XEmacs's `set-text-properties' doesn't work on
;; free-standing strings, apparently.
(set-text-properties 0 (length stripped-name) nil stripped-name))
(defun bookmark-alist-from-buffer ()
- "Return a bookmark-alist (in any format) from the current buffer.
+ "Return a `bookmark-alist' (in any format) from the current buffer.
The buffer must of course contain bookmark format information.
Does not care from where in the buffer it is called, and does not
affect point."
(defun bookmark-upgrade-file-format-from-0 ()
"Upgrade a bookmark file of format 0 (the original format) to format 1.
-This expects to be called from point-min in a bookmark file."
+This expects to be called from `point-min' in a bookmark file."
(message "Upgrading bookmark format from 0 to %d..."
bookmark-file-format-version)
(let* ((old-list (bookmark-alist-from-buffer))
(defun bookmark-grok-file-format-version ()
"Return an integer which is the file-format version of this bookmark file.
-This expects to be called from point-min in a bookmark file."
+This expects to be called from `point-min' in a bookmark file."
(if (looking-at "^;;;;")
(save-excursion
(save-match-data
(defun bookmark-maybe-upgrade-file-format ()
"Check the file-format version of this bookmark file.
If the version is not up-to-date, upgrade it automatically.
-This expects to be called from point-min in a bookmark file."
+This expects to be called from `point-min' in a bookmark file."
(let ((version (bookmark-grok-file-format-version)))
(cond
((= version bookmark-file-format-version)
;;; end file-format stuff
+\f
+;;; Generic helpers.
+
+(defun bookmark-maybe-message (fmt &rest args)
+ "Apply `message' to FMT and ARGS, but only if the display is fast enough."
+ (if (>= baud-rate 9600)
+ (apply 'message fmt args)))
+
\f
;;; Core code:
(defun bookmark-kill-line (&optional newline-too)
"Kill from point to end of line.
If optional arg NEWLINE-TOO is non-nil, delete the newline too.
-Does not affect the kill-ring."
+Does not affect the kill ring."
(let ((eol (save-excursion (end-of-line) (point))))
(delete-region (point) eol)
(if (and newline-too (looking-at "\n"))
(defvar bookmark-read-annotation-text-func 'bookmark-default-annotation-text
"Function to return default text to use for a bookmark annotation.
-It takes the name of the bookmark, as a string, as an arg.")
+It takes one argument, the name of the bookmark, as a string.")
(defun bookmark-read-annotation-mode (buf point parg bookmark)
"Mode for composing annotations for a bookmark.
-Wants BUF POINT PARG and BOOKMARK.
+Wants BUF, POINT, PARG, and BOOKMARK.
When you have finished composing, type \\[bookmark-send-annotation] to send
the annotation.
(use-local-map bookmark-read-annotation-mode-map)
(setq major-mode 'bookmark-read-annotation-mode)
(insert (funcall bookmark-read-annotation-text-func bookmark))
- (run-hooks 'text-mode-hook))
+ (run-mode-hooks 'text-mode-hook))
(defun bookmark-read-annotation (parg bookmark)
(let ((annotation (bookmark-get-annotation bookmark)))
(if (and annotation (not (string-equal annotation "")))
(insert annotation)))
- (run-hooks 'text-mode-hook))
+ (run-mode-hooks 'text-mode-hook))
(defun bookmark-send-edited-annotation ()
(defun bookmark-insert-current-bookmark ()
- "Insert this buffer's value of bookmark-current-bookmark.
+ "Insert this buffer's value of `bookmark-current-bookmark'.
Default to file name if it's nil."
(interactive)
(let ((str
(insert string)))
+(defvar Info-current-file)
+
(defun bookmark-buffer-file-name ()
"Return the current buffer's file in a way useful for bookmarks.
For example, if this is a Info buffer, return the Info file's name."
- (if (eq major-mode 'Info-mode)
- Info-current-file
- (or
- buffer-file-name
- (if (and (boundp 'dired-directory) dired-directory)
- (if (stringp dired-directory)
- dired-directory
- (car dired-directory))))))
+ (cond
+ ((eq major-mode 'Info-mode)
+ Info-current-file)
+ (buffer-file-name
+ ;; Abbreviate the path, both so it's shorter and so it's more
+ ;; portable. E.g., the user's home dir might be a different
+ ;; path on different machines, but "~/" will still reach it.
+ (abbreviate-file-name buffer-file-name))
+ ((and (boundp 'dired-directory) dired-directory)
+ (if (stringp dired-directory)
+ dired-directory
+ (car dired-directory)))))
(defun bookmark-maybe-load-default-file ()
;;Return the bookmark-alist for display. If the bookmark-sort-flag
;;is non-nil, then return a sorted copy of the alist.
(if bookmark-sort-flag
- (setq bookmark-alist
- (sort (copy-alist bookmark-alist)
- (function
- (lambda (x y) (string-lessp (car x) (car y))))))))
+ (sort (copy-alist bookmark-alist)
+ (function
+ (lambda (x y) (string-lessp (car x) (car y)))))
+ bookmark-alist))
+
+(defvar bookmark-after-jump-hook nil
+ "Hook run after `bookmark-jump' jumps to a bookmark.
+Useful for example to unhide text in `outline-mode'.")
;;;###autoload
(defun bookmark-jump (bookmark)
this.
If the file pointed to by BOOKMARK no longer exists, you will be asked
-if you wish to give the bookmark a new location, and bookmark-jump
+if you wish to give the bookmark a new location, and `bookmark-jump'
will then jump to the new location, as well as recording it in place
of the old one in the permanent bookmark record."
(interactive
(list (bookmark-completing-read "Jump to bookmark"
bookmark-current-bookmark)))
+ (unless bookmark
+ (error "No bookmark specified"))
(bookmark-maybe-historicize-string bookmark)
(let ((cell (bookmark-jump-noselect bookmark)))
(and cell
(switch-to-buffer (car cell))
(goto-char (cdr cell))
+ (progn (run-hooks 'bookmark-after-jump-hook) t)
(if bookmark-automatically-show-annotations
;; if there is an annotation for this bookmark,
;; show it in a buffer.
(bookmark-show-annotation bookmark)))))
+;;;###autoload
+(defun bookmark-jump-other-window (bookmark)
+ "Jump to BOOKMARK (a point in some file) in another window.
+See `bookmark-jump'."
+ (interactive
+ (let ((bkm (bookmark-completing-read "Jump to bookmark (in another window)"
+ bookmark-current-bookmark)))
+ (if (> emacs-major-version 21)
+ (list bkm) bkm)))
+ (when bookmark
+ (bookmark-maybe-historicize-string bookmark)
+ (let ((cell (bookmark-jump-noselect bookmark)))
+ (and cell
+ (switch-to-buffer-other-window (car cell))
+ (goto-char (cdr cell))
+ (if bookmark-automatically-show-annotations
+ ;; if there is an annotation for this bookmark,
+ ;; show it in a buffer.
+ (bookmark-show-annotation bookmark))))))
+
+
(defun bookmark-file-or-variation-thereof (file)
"Return FILE (a string) if it exists, or return a reasonable
variation of FILE if that exists. Reasonable variations are checked
;; Info nodes must be visited with care.
(progn
(require 'info)
- (Info-find-node file info-node))
+ (with-no-warnings
+ (Info-find-node file info-node)))
;; Else no Info. Can do an ordinary find-file:
(set-buffer (find-file-noselect file))
(goto-char place))
(prog1
(insert (bookmark-location bookmark)) ; *Return this line*
(if (and (display-color-p) (display-mouse-p))
- (add-text-properties start
- (save-excursion (re-search-backward
- "[^ \t]")
+ (add-text-properties
+ start
+ (save-excursion (re-search-backward
+ "[^ \t]")
(1+ (point)))
- '(mouse-face highlight
- help-echo "mouse-2: go to this bookmark"))))))
+ '(mouse-face highlight
+ follow-link t
+ help-echo "mouse-2: go to this bookmark in other window"))))))
;;;###autoload
(defalias 'bookmark-locate 'bookmark-insert-location)
`bookmark-default-file'. With a prefix arg, save it in file FILE
\(second argument\).
-If you are calling this from Lisp, the two arguments are PREFIX-ARG
-and FILE, and if you just want it to write to the default file, then
+If you are calling this from Lisp, the two arguments are PARG and
+FILE, and if you just want it to write to the default file, then
pass no arguments. Or pass in nil and FILE, and it will save in FILE
instead. If you pass in one argument, and it is non-nil, then the
user will be interactively queried for a file to save in.
(defun bookmark-write-file (file)
(save-excursion
(save-window-excursion
- (if (>= baud-rate 9600)
- (message "Saving bookmarks to file %s..." file))
- (set-buffer (let ((enable-local-variables nil))
- (find-file-noselect file)))
+ (bookmark-maybe-message "Saving bookmarks to file %s..." file)
+ (set-buffer (get-buffer-create " *Bookmarks*"))
(goto-char (point-min))
+ (delete-region (point-min) (point-max))
(let ((print-length nil)
(print-level nil))
- (delete-region (point-min) (point-max))
(bookmark-insert-file-format-version-stamp)
(pp bookmark-alist (current-buffer))
(let ((version-control
((eq 'nospecial bookmark-version-control) version-control)
(t
t))))
- (write-file file)
+ (condition-case nil
+ (write-region (point-min) (point-max) file)
+ (file-error (message "Can't write %s" file)))
(kill-buffer (current-buffer))
- (if (>= baud-rate 9600)
- (message "Saving bookmarks to file %s...done" file)))))))
+ (bookmark-maybe-message
+ "Saving bookmarks to file %s...done" file))))))
(defun bookmark-import-new-list (new-list)
(if (file-readable-p file)
(save-excursion
(save-window-excursion
- (if (and (null no-msg) (>= baud-rate 9600))
- (message "Loading bookmarks from %s..." file))
+ (if (null no-msg)
+ (bookmark-maybe-message "Loading bookmarks from %s..." file))
(set-buffer (let ((enable-local-variables nil))
(find-file-noselect file)))
(goto-char (point-min))
(bookmark-bmenu-surreptitiously-rebuild-list))
(error "Invalid bookmark list in %s" file)))
(kill-buffer (current-buffer)))
- (if (and (null no-msg) (>= baud-rate 9600))
- (message "Loading bookmarks from %s...done" file)))
+ (if (null no-msg)
+ (bookmark-maybe-message "Loading bookmarks from %s...done" file)))
(error "Cannot read bookmark file %s" file)))
(define-key bookmark-bmenu-mode-map "m" 'bookmark-bmenu-mark)
(define-key bookmark-bmenu-mode-map "l" 'bookmark-bmenu-load)
(define-key bookmark-bmenu-mode-map "r" 'bookmark-bmenu-rename)
+ (define-key bookmark-bmenu-mode-map "R" 'bookmark-bmenu-relocate)
(define-key bookmark-bmenu-mode-map "t" 'bookmark-bmenu-toggle-filenames)
(define-key bookmark-bmenu-mode-map "a" 'bookmark-bmenu-show-annotation)
(define-key bookmark-bmenu-mode-map "A" 'bookmark-bmenu-show-all-annotations)
(let ((inhibit-read-only t))
(erase-buffer)
(insert "% Bookmark\n- --------\n")
- (bookmark-maybe-sort-alist)
- (mapcar
+ (add-text-properties (point-min) (point)
+ '(font-lock-face bookmark-menu-heading))
+ (mapc
(lambda (full-record)
;; if a bookmark has an annotation, prepend a "*"
;; in the list of bookmarks.
(let ((start (point)))
(insert (bookmark-name-from-full-record full-record))
(if (and (display-color-p) (display-mouse-p))
- (add-text-properties start
- (save-excursion (re-search-backward
- "[^ \t]")
- (1+ (point)))
- '(mouse-face highlight
- help-echo "mouse-2: go to this bookmark")))
+ (add-text-properties
+ start
+ (save-excursion (re-search-backward
+ "[^ \t]")
+ (1+ (point)))
+ '(mouse-face highlight
+ follow-link t
+ help-echo "mouse-2: go to this bookmark in other window")))
(insert "\n")
)))
- bookmark-alist))
+ (bookmark-maybe-sort-alist)))
(goto-char (point-min))
(forward-line 2)
(bookmark-bmenu-mode)
so the bookmark menu bookmark remains visible in its window.
\\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
\\[bookmark-bmenu-rename] -- rename this bookmark \(prompts for new name\).
+\\[bookmark-bmenu-relocate] -- relocate this bookmark's file \(prompts for new file\).
\\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
\\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
\\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
(setq buffer-read-only t)
(setq major-mode 'bookmark-bmenu-mode)
(setq mode-name "Bookmark Menu")
- (run-hooks 'bookmark-bmenu-mode-hook))
+ (run-mode-hooks 'bookmark-bmenu-mode-hook))
(defun bookmark-bmenu-toggle-filenames (&optional show)
(let ((start (point)))
(insert (car bookmark-bmenu-hidden-bookmarks))
(if (and (display-color-p) (display-mouse-p))
- (add-text-properties start
- (save-excursion (re-search-backward
- "[^ \t]")
- (1+ (point)))
- '(mouse-face highlight
- help-echo
- "mouse-2: go to this bookmark"))))
+ (add-text-properties
+ start
+ (save-excursion (re-search-backward
+ "[^ \t]")
+ (1+ (point)))
+ '(mouse-face highlight
+ follow-link t
+ help-echo
+ "mouse-2: go to this bookmark in other window"))))
(setq bookmark-bmenu-hidden-bookmarks
(cdr bookmark-bmenu-hidden-bookmarks))
(forward-line 1))))))))
(let ((old-buf (current-buffer)))
(pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
(delete-region (point-min) (point-max))
- (mapcar
+ (mapc
(lambda (full-record)
(let* ((name (bookmark-name-from-full-record full-record))
(ann (bookmark-get-annotation name)))
(if (and ann (not (string-equal ann "")))
;; insert the annotation, indented by 4 spaces.
(progn
- (save-excursion (insert ann))
+ (save-excursion (insert ann) (unless (bolp)
+ (insert "\n")))
(while (< (point) (point-max))
(beginning-of-line) ; paranoia
(insert " ")
(setq tem (bookmark-bmenu-bookmark))
(let ((inhibit-read-only t))
(delete-char -1)
- (insert ?\ ))
+ (insert ?\s))
(or (string-equal tem bmrk)
(member tem others)
(setq others (cons tem others))))
(interactive)
(if (bookmark-bmenu-check-position)
(let ((bmrk (bookmark-bmenu-bookmark)))
- (message (bookmark-location bmrk)))))
+ (message "%s" (bookmark-location bmrk)))))
+(defun bookmark-bmenu-relocate ()
+ "Change the file path of the bookmark on the current line,
+ prompting with completion for the new path."
+ (interactive)
+ (if (bookmark-bmenu-check-position)
+ (let ((bmrk (bookmark-bmenu-bookmark))
+ (thispoint (point)))
+ (bookmark-relocate bmrk)
+ (goto-char thispoint))))
\f
;;; Menu bar stuff. Prefix is "bookmark-menu".
(format "-*- %s (%d) -*-" name iter)
(nreverse lst))
pane-list)))
-
+
;; Popup the menu and return the string.
(x-popup-menu event (cons (concat "-*- " name " -*-")
(nreverse pane-list)))))
(define-key map [write] '("Save Bookmarks As..." . bookmark-write))
(define-key map [save] '("Save Bookmarks" . bookmark-save))
(define-key map [edit] '("Edit Bookmark List" . bookmark-bmenu-list))
- (define-key map [delete] '("Delete Bookmark" . bookmark-delete))
- (define-key map [rename] '("Rename Bookmark" . bookmark-rename))
- (define-key map [locate] '("Insert Location" . bookmark-locate))
- (define-key map [insert] '("Insert Contents" . bookmark-insert))
- (define-key map [set] '("Set Bookmark" . bookmark-set))
- (define-key map [jump] '("Jump to Bookmark" . bookmark-jump))
+ (define-key map [delete] '("Delete Bookmark..." . bookmark-delete))
+ (define-key map [rename] '("Rename Bookmark..." . bookmark-rename))
+ (define-key map [locate] '("Insert Location..." . bookmark-locate))
+ (define-key map [insert] '("Insert Contents..." . bookmark-insert))
+ (define-key map [set] '("Set Bookmark..." . bookmark-set))
+ (define-key map [jump] '("Jump to Bookmark..." . bookmark-jump))
map))
;;;###autoload
;;; Exit Hook, called from kill-emacs-hook
(defvar bookmark-exit-hook nil
- "Hook run when emacs exits.")
+ "Hook run when Emacs exits.")
+
+(define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
(defun bookmark-exit-hook-internal ()
"Save bookmark state, if necessary, at Emacs exit time.
-This also runs `bookmark-exit-hooks'."
- (run-hooks 'bookmark-exit-hooks)
+This also runs `bookmark-exit-hook'."
+ (run-hooks 'bookmark-exit-hook)
(and bookmark-alist
(bookmark-time-to-save-p t)
(bookmark-save)))