;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
-;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
;; Author: Karl Fogel <kfogel@red-bean.com>
;; Maintainer: Karl Fogel <kfogel@red-bean.com>
;; can navigate your way to that location by providing the string.
;; See the "User Variables" section for customizations.
-;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and
-;; then implementing the bookmark-current-bookmark idea. He even
-;; sent *patches*, bless his soul...
-
-;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for
-;; fixing and improving bookmark-time-to-save-p.
-
-;; Thanks go to Andrew V. Klein <avk@cig.mot.com> for the code that
-;; sorts the alist before presenting it to the user (in bookmark-bmenu-list
-;; and the menu-bar).
-
-;; And much thanks to David Hughes <djh@harston.cv.com> for many small
-;; suggestions and the code to implement them (like
-;; bookmark-bmenu-check-position, and some of the Lucid compatibility
-;; stuff).
-
-;; Kudos (whatever they are) go to Jim Blandy <jimb@red-bean.com>
-;; for his eminently sensible suggestion to separate bookmark-jump
-;; into bookmark-jump and bookmark-jump-noselect, which made many
-;; other things cleaner as well.
-
-;; Thanks to Roland McGrath for encouragement and help with defining
-;; autoloads on the menu-bar.
-
-;; Jonathan Stigelman <stig@hackvan.com> gave patches for default
-;; values in bookmark-jump and bookmark-set. Everybody please keep
-;; all the keystrokes they save thereby and send them to him at the
-;; end of each year :-) (No, seriously, thanks Jonathan!)
-
-;; Buckets of gratitude to John Grabowski <johng@media.mit.edu> for
-;; thinking up the annotations feature and implementing it so well.
-
-;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad
-;; <olstad@msc.edu>.
-
-;; Thanks to Mikio Nakajima <PBC01764@niftyserve.or.jp> for many bugs
-;; reported and fixed.
-
-;; Thank you, Michael Kifer, for contributing the XEmacs support.
-
-;; Enough with the credits already, get on to the good stuff:
-
-;; FAVORITE CHINESE RESTAURANT:
-;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's
-;; Choice (both in Chicago's Chinatown). Well, both. How about you?
\f
;;; Code:
(defcustom bookmark-save-flag t
"Controls when Emacs saves bookmarks to a file.
--> nil means never save bookmarks, except when `bookmark-save' is
- explicitly called \(\\[bookmark-save]\).
+ explicitly called (\\[bookmark-save]).
--> t means save bookmarks when Emacs is killed.
--> Otherwise, it should be a number that is the frequency with which
- the bookmark list is saved \(i.e.: the number of times which
+ the bookmark list is saved (i.e.: the number of times which
Emacs' bookmark list may be modified before it is automatically
- saved.\). If it is a number, Emacs will also automatically save
+ saved.). If it is a number, Emacs will also automatically save
bookmarks when it is killed.
Therefore, the way to get it to save every time you make or delete a
-bookmark is to set this variable to 1 \(or 0, which produces the same
-behavior.\)
+bookmark is to set this variable to 1 (or 0, which produces the same
+behavior.)
To specify the file in which to save them, modify the variable
`bookmark-default-file', which is `~/.emacs.bmk' by default."
:group 'bookmark)
+(defconst bookmark-bmenu-header-height 2
+ "Number of lines used for the *Bookmark List* header.")
+
+(defconst bookmark-bmenu-marks-width 2
+ "Number of columns (chars) used for the *Bookmark List* marks column,
+including the annotations column.")
+
(defcustom bookmark-bmenu-file-column 30
"Column at which to display filenames in a buffer listing bookmarks.
You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
This may result in truncated bookmark names. To disable this, put the
following in your `.emacs' file:
-\(setq bookmark-bmenu-toggle-filenames nil\)"
+\(setq bookmark-bmenu-toggle-filenames nil)"
:type 'boolean
:group 'bookmark)
:type 'integer
:group 'bookmark)
-
+;; FIXME: Is it really worth a customization option?
(defcustom bookmark-search-delay 0.2
- "*Display when searching bookmarks is updated all `bookmark-search-delay' seconds."
+ "Time before `bookmark-bmenu-search' updates the display."
:group 'bookmark
:type 'integer)
-
-(defcustom bookmark-search-prompt "Pattern: "
- "*Prompt used for `bookmark-bmenu-search'."
- :group 'bookmark
- :type 'string)
-
-
(defface bookmark-menu-heading
'((t (:inherit font-lock-type-face)))
"Face used to highlight the heading in bookmark menu buffers."
for instance. HANDLER must accept a bookmark as argument.")
(defvar bookmarks-already-loaded nil
- "Non-nil iff bookmarks have been loaded from `bookmark-default-file'.")
+ "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
;; more stuff added by db.
(defvar bookmark-yank-point 0
"The next point from which to pull source text for `bookmark-yank-word'.
-This point is in `bookmark-curent-buffer'.")
-
-
-(defvar bookmark-search-pattern ""
- "Store keyboard input for incremental search.")
-
-
-(defvar bookmark-search-timer nil
- "Timer used for searching")
+This point is in `bookmark-current-buffer'.")
(defvar bookmark-quit-flag nil
"Non nil make `bookmark-bmenu-search' quit immediately.")
-
-
\f
-;; Helper functions.
-
-;; Only functions on this page and the next one (file formats) need to
-;; know anything about the format of bookmark-alist entries.
+;; Helper functions and macros.
+
+(defmacro with-buffer-modified-unmodified (&rest body)
+ "Run BODY while preserving the buffer's `buffer-modified-p' state."
+ (let ((was-modified (make-symbol "was-modified")))
+ `(let ((,was-modified (buffer-modified-p)))
+ (unwind-protect
+ (progn ,@body)
+ (set-buffer-modified-p ,was-modified)))))
+
+;; Only functions below, in this page and the next one (file formats),
+;; need to know anything about the format of bookmark-alist entries.
;; Everyone else should go through them.
-
(defun bookmark-name-from-full-record (full-record)
- "Return name of FULL-RECORD \(an alist element instead of a string\)."
+ "Return name of FULL-RECORD (an alist element instead of a string)."
(car full-record))
(defun bookmark-get-position (bookmark)
- "Return the position \(i.e.: point\) of BOOKMARK, or nil if none.
+ "Return the position (i.e.: point) of BOOKMARK, or nil if none.
BOOKMARK may be a bookmark name (a string) or a bookmark record."
(bookmark-prop-get bookmark 'position))
(defun bookmark-set-position (bookmark position)
- "Set the position \(i.e.: point\) of BOOKMARK to POSITION.
+ "Set the position (i.e.: point) of BOOKMARK to POSITION.
BOOKMARK may be a bookmark name (a string) or a bookmark record."
(bookmark-prop-set bookmark 'position position))
large document. If there is no prior bookmark for this document, then
C-u inserts an appropriate name based on the buffer or file.
-Use \\[bookmark-delete] to remove bookmarks \(you give it a name and
+Use \\[bookmark-delete] to remove bookmarks (you give it a name and
it removes only the first instance of a bookmark with that name from
-the list of bookmarks.\)"
+the list of bookmarks.)"
(interactive (list nil current-prefix-arg))
(let* ((record (bookmark-make-record))
(default (car record)))
(let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
(bookmark bookmark-annotation-name))
(bookmark-set-annotation bookmark annotation)
+ (setq bookmark-alist-modification-count
+ (1+ bookmark-alist-modification-count))
(bookmark-bmenu-surreptitiously-rebuild-list))
(kill-buffer (current-buffer)))
(defun bookmark-yank-word ()
"Get the next word from buffer `bookmark-current-buffer' and append
it to the name of the bookmark currently being set, advancing
-`bookmark-yank-point' by one word."
+`bookmark-yank-point' by one word."
(interactive)
(let ((string (with-current-buffer bookmark-current-buffer
(goto-char bookmark-yank-point)
(and
;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
;; to be renamed.
- (file-exists-p (expand-file-name bookmark-old-default-file))
- (not (file-exists-p (expand-file-name bookmark-default-file)))
- (rename-file (expand-file-name bookmark-old-default-file)
- (expand-file-name bookmark-default-file)))
+ (file-exists-p bookmark-old-default-file)
+ (not (file-exists-p bookmark-default-file))
+ (rename-file bookmark-old-default-file
+ bookmark-default-file))
;; return t so the `and' will continue...
t)
- (file-readable-p (expand-file-name bookmark-default-file))
+ (file-readable-p bookmark-default-file)
(bookmark-load bookmark-default-file t t)
(setq bookmarks-already-loaded t)))
(funcall (or (bookmark-get-handler bookmark)
'bookmark-default-handler)
(bookmark-get-bookmark bookmark))
- (file-error
+ (bookmark-error-no-filename ;file-error
;; We were unable to find the marked file, so ask if user wants to
;; relocate the bookmark, else remind them to consider deletion.
(when (stringp bookmark)
'bookmark-default-handler)
(bookmark-get-bookmark bookmark)))
(message
- "Bookmark not relocated; consider removing it \(%s\)."
+ "Bookmark not relocated; consider removing it (%s)."
bookmark)
(signal (car err) (cdr err))))))))))
;; Added by db.
BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
Changes current buffer and point and returns nil, or signals a `file-error'."
(let ((file (bookmark-get-filename bmk-record))
+ (buf (bookmark-prop-get bmk-record 'buffer))
(forward-str (bookmark-get-front-context-string bmk-record))
(behind-str (bookmark-get-rear-context-string bmk-record))
(place (bookmark-get-position bmk-record)))
- (if (not file)
- (signal 'bookmark-error-no-filename (list 'stringp file))
- (set-buffer (find-file-noselect file))
- (if place (goto-char place))
- ;; Go searching forward first. Then, if forward-str exists and
- ;; was found in the file, we can search backward for behind-str.
- ;; Rationale is that if text was inserted between the two in the
- ;; file, it's better to be put before it so you can read it,
- ;; rather than after and remain perhaps unaware of the changes.
- (if forward-str
- (if (search-forward forward-str (point-max) t)
- (goto-char (match-beginning 0))))
- (if behind-str
- (if (search-backward behind-str (point-min) t)
- (goto-char (match-end 0)))))
+ (set-buffer
+ (cond
+ ((and file (file-readable-p file) (not (buffer-live-p buf)))
+ (find-file-noselect file))
+ ;; No file found. See if buffer BUF have been created.
+ ((and buf (get-buffer buf)))
+ (t ;; If not, raise error.
+ (signal 'bookmark-error-no-filename (list 'stringp file)))))
+ (if place (goto-char place))
+ ;; Go searching forward first. Then, if forward-str exists and
+ ;; was found in the file, we can search backward for behind-str.
+ ;; Rationale is that if text was inserted between the two in the
+ ;; file, it's better to be put before it so you can read it,
+ ;; rather than after and remain perhaps unaware of the changes.
+ (when (and forward-str (search-forward forward-str (point-max) t))
+ (goto-char (match-beginning 0)))
+ (when (and behind-str (search-backward behind-str (point-min) t))
+ (goto-char (match-end 0)))
nil))
;;;###autoload
(bookmark-maybe-historicize-string bookmark)
(bookmark-maybe-load-default-file)
(let* ((bmrk-filename (bookmark-get-filename bookmark))
- (newloc (expand-file-name
- (read-file-name
- (format "Relocate %s to: " bookmark)
- (file-name-directory bmrk-filename)))))
+ (newloc (abbreviate-file-name
+ (expand-file-name
+ (read-file-name
+ (format "Relocate %s to: " bookmark)
+ (file-name-directory bmrk-filename))))))
(bookmark-set-filename bookmark newloc)
(setq bookmark-alist-modification-count
(1+ bookmark-alist-modification-count))
"New name: "
nil
(let ((now-map (copy-keymap minibuffer-local-map)))
- (define-key now-map "\C-w" 'bookmark-yank-word)
+ (define-key now-map "\C-w" 'bookmark-yank-word)
now-map)
nil
'bookmark-history))))
Removes only the first instance of a bookmark with that name. If
there are one or more other bookmarks with the same name, they will
-not be deleted. Defaults to the \"current\" bookmark \(that is, the
-one most recently used in this file, if any\).
+not be deleted. Defaults to the \"current\" bookmark (that is, the
+one most recently used in this file, if any).
Optional second arg BATCH means don't update the bookmark list buffer,
probably because we were called from there."
(interactive
"Save currently defined bookmarks.
Saves by default in the file defined by the variable
`bookmark-default-file'. With a prefix arg, save it in file FILE
-\(second argument\).
+\(second argument).
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
user will be interactively queried for a file to save in.
When you want to load in the bookmarks from a file, use
-\`bookmark-load\', \\[bookmark-load]. That function will prompt you
+`bookmark-load', \\[bookmark-load]. That function will prompt you
for a file, defaulting to the file defined by variable
`bookmark-default-file'."
(interactive "P")
;; Rather than a single call to `pp' we make one per bookmark.
;; Apparently `pp' has a poor algorithmic complexity, so this
;; scales a lot better. bug#4485.
- (dolist (i bookmark-alist) (pp i (current-buffer)))
+ (dolist (i bookmark-alist) (pp i (current-buffer)))
(insert ")")
(let ((version-control
(cond
;;but there's no better default, and
;;I guess it's better than none at all.
"~/" bookmark-default-file 'confirm)))
- (setq file (expand-file-name file))
+ (setq file (abbreviate-file-name (expand-file-name file)))
(if (not (file-readable-p file))
(error "Cannot read bookmark file %s" file)
(if (null no-msg)
(setq bookmark-alist-modification-count
(1+ bookmark-alist-modification-count)))
(if (string-equal
- (expand-file-name bookmark-default-file)
+ (abbreviate-file-name
+ (expand-file-name bookmark-default-file))
file)
(setq bookmarks-already-loaded t))
(bookmark-bmenu-surreptitiously-rebuild-list))
;; Prefix is "bookmark-bmenu" for "buffer-menu":
-(defvar bookmark-bmenu-bookmark-column nil)
-
-
(defvar bookmark-bmenu-hidden-bookmarks ())
(define-key map "a" 'bookmark-bmenu-show-annotation)
(define-key map "A" 'bookmark-bmenu-show-all-annotations)
(define-key map "e" 'bookmark-bmenu-edit-annotation)
- (define-key map "\M-g" 'bookmark-bmenu-search)
+ ;; The original binding of M-g hides the M-g prefix map.
+ ;; If someone has a better idea than M-g s, I'm open to suggestions.
+ (define-key map [?\M-g ?s] 'bookmark-bmenu-search)
(define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
map))
(add-text-properties (point-min) (point)
'(font-lock-face bookmark-menu-heading))
(dolist (full-record (bookmark-maybe-sort-alist))
- ;; if a bookmark has an annotation, prepend a "*"
- ;; in the list of bookmarks.
- (let ((annotation (bookmark-get-annotation full-record)))
+ (let ((name (bookmark-name-from-full-record full-record))
+ (annotation (bookmark-get-annotation full-record))
+ (start (point))
+ end)
+ ;; if a bookmark has an annotation, prepend a "*"
+ ;; in the list of bookmarks.
(insert (if (and annotation (not (string-equal annotation "")))
- " *" " "))
- (let ((start (point)))
- (insert (bookmark-name-from-full-record full-record))
- (if (display-mouse-p)
- (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")))))
-
- (goto-char (point-min))
- (forward-line 2)
- (bookmark-bmenu-mode)
- (if bookmark-bmenu-toggle-filenames
- (bookmark-bmenu-toggle-filenames t)))
+ " *" " ")
+ name)
+ (setq end (point))
+ (put-text-property
+ (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
+ (when (display-mouse-p)
+ (add-text-properties
+ (+ bookmark-bmenu-marks-width start) end
+ '(mouse-face highlight
+ follow-link t
+ help-echo "mouse-2: go to this bookmark in other window")))
+ (insert "\n")))
+ (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
+ (goto-char (point-min))
+ (forward-line 2)
+ (bookmark-bmenu-mode)
+ (if bookmark-bmenu-toggle-filenames
+ (bookmark-bmenu-toggle-filenames t))))
;;;###autoload
(defalias 'list-bookmarks 'bookmark-bmenu-list)
\\[bookmark-bmenu-other-window] -- select this bookmark in another window,
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-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]'.
mainly for debugging, and should not be necessary in normal use."
(if (and (not force) bookmark-bmenu-toggle-filenames)
nil ;already shown, so do nothing
- (save-excursion
- (save-window-excursion
- (goto-char (point-min))
- (forward-line 2)
- (setq bookmark-bmenu-hidden-bookmarks ())
- (let ((inhibit-read-only t))
- (while (< (point) (point-max))
- (let ((bmrk (bookmark-bmenu-bookmark)))
- (setq bookmark-bmenu-hidden-bookmarks
- (cons bmrk bookmark-bmenu-hidden-bookmarks))
- (let ((start (save-excursion (end-of-line) (point))))
- (move-to-column bookmark-bmenu-file-column t)
- ;; Strip off `mouse-face' from the white spaces region.
- (if (display-mouse-p)
- (remove-text-properties start (point)
- '(mouse-face nil help-echo nil))))
- (delete-region (point) (progn (end-of-line) (point)))
- (insert " ")
- ;; Pass the NO-HISTORY arg:
- (bookmark-insert-location bmrk t)
- (forward-line 1))))))))
+ (with-buffer-modified-unmodified
+ (save-excursion
+ (save-window-excursion
+ (goto-char (point-min))
+ (forward-line 2)
+ (setq bookmark-bmenu-hidden-bookmarks ())
+ (let ((inhibit-read-only t))
+ (while (< (point) (point-max))
+ (let ((bmrk (bookmark-bmenu-bookmark)))
+ (push bmrk bookmark-bmenu-hidden-bookmarks)
+ (let ((start (save-excursion (end-of-line) (point))))
+ (move-to-column bookmark-bmenu-file-column t)
+ ;; Strip off `mouse-face' from the white spaces region.
+ (if (display-mouse-p)
+ (remove-text-properties start (point)
+ '(mouse-face nil help-echo nil))))
+ (delete-region (point) (progn (end-of-line) (point)))
+ (insert " ")
+ ;; Pass the NO-HISTORY arg:
+ (bookmark-insert-location bmrk t)
+ (forward-line 1)))))))))
(defun bookmark-bmenu-hide-filenames (&optional force)
"In an interactive bookmark list, hide the filenames of the bookmarks.
Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
mainly for debugging, and should not be necessary in normal use."
- (if (and (not force) bookmark-bmenu-toggle-filenames)
- ;; nothing to hide if above is nil
- (save-excursion
- (save-window-excursion
- (goto-char (point-min))
- (forward-line 2)
- (setq bookmark-bmenu-hidden-bookmarks
- (nreverse bookmark-bmenu-hidden-bookmarks))
- (save-excursion
- (goto-char (point-min))
- (search-forward "Bookmark")
- (backward-word 1)
- (setq bookmark-bmenu-bookmark-column (current-column)))
- (save-excursion
- (let ((inhibit-read-only t))
- (while bookmark-bmenu-hidden-bookmarks
- (move-to-column bookmark-bmenu-bookmark-column t)
- (bookmark-kill-line)
- (let ((start (point)))
- (insert (car bookmark-bmenu-hidden-bookmarks))
- (if (display-mouse-p)
- (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))))))))
-
-
-(defun bookmark-bmenu-check-position ()
+ (when (and (not force) bookmark-bmenu-toggle-filenames)
+ ;; nothing to hide if above is nil
+ (with-buffer-modified-unmodified
+ (save-excursion
+ (goto-char (point-min))
+ (forward-line 2)
+ (setq bookmark-bmenu-hidden-bookmarks
+ (nreverse bookmark-bmenu-hidden-bookmarks))
+ (let ((inhibit-read-only t))
+ (while bookmark-bmenu-hidden-bookmarks
+ (move-to-column bookmark-bmenu-marks-width t)
+ (bookmark-kill-line)
+ (let ((name (pop bookmark-bmenu-hidden-bookmarks))
+ (start (point)))
+ (insert name)
+ (put-text-property start (point) 'bookmark-name-prop name)
+ (if (display-mouse-p)
+ (add-text-properties
+ start (point)
+ '(mouse-face
+ highlight follow-link t help-echo
+ "mouse-2: go to this bookmark in other window"))))
+ (forward-line 1)))))))
+
+
+(defun bookmark-bmenu-ensure-position ()
"If point is not on a bookmark line, move it to one.
-If before the first bookmark line, move it to the first.
-If after the last, move it to the last.
-Return `bookmark-alist'"
- ;; FIXME: The doc string originally implied that this returns nil if
- ;; not on a bookmark, which is false. Is there any real reason to
- ;; return `bookmark-alist'? This seems to be called in a few places
- ;; as a check of whether point is on a bookmark line. Those
- ;; "checks" are in fact no-ops, since this never returns nil.
- ;; -dadams, 2009-10-10
- (cond ((< (count-lines (point-min) (point)) 2)
+If before the first bookmark line, move to the first; if after the
+last full line, move to the last full line. The return value is undefined."
+ (cond ((< (count-lines (point-min) (point)) bookmark-bmenu-header-height)
(goto-char (point-min))
- (forward-line 2)
- bookmark-alist)
+ (forward-line bookmark-bmenu-header-height))
((and (bolp) (eobp))
- (beginning-of-line 0)
- bookmark-alist)
- (t
- bookmark-alist)))
+ (beginning-of-line 0))))
(defun bookmark-bmenu-bookmark ()
"Return the bookmark for this line in an interactive bookmark list buffer."
- (if (bookmark-bmenu-check-position)
- (save-excursion
- (save-window-excursion
- (goto-char (point-min))
- (search-forward "Bookmark")
- (backward-word 1)
- (setq bookmark-bmenu-bookmark-column (current-column)))))
- (if bookmark-bmenu-toggle-filenames
- (bookmark-bmenu-hide-filenames))
+ (bookmark-bmenu-ensure-position)
(save-excursion
- (save-window-excursion
- (beginning-of-line)
- (forward-char bookmark-bmenu-bookmark-column)
- (prog1
- (buffer-substring-no-properties (point)
- (progn
- (end-of-line)
- (point)))
- ;; well, this is certainly crystal-clear:
- (if bookmark-bmenu-toggle-filenames
- (bookmark-bmenu-toggle-filenames t))))))
+ (beginning-of-line)
+ (forward-char bookmark-bmenu-marks-width)
+ (get-text-property (point) 'bookmark-name-prop)))
(defun bookmark-show-annotation (bookmark)
"Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
(interactive)
(beginning-of-line)
- (if (bookmark-bmenu-check-position)
- (let ((inhibit-read-only t))
- (delete-char 1)
- (insert ?>)
- (forward-line 1)
- (bookmark-bmenu-check-position))))
+ (bookmark-bmenu-ensure-position)
+ (with-buffer-modified-unmodified
+ (let ((inhibit-read-only t))
+ (delete-char 1)
+ (insert ?>)
+ (forward-line 1)
+ (bookmark-bmenu-ensure-position))))
(defun bookmark-bmenu-select ()
"Select this line's bookmark; also display bookmarks marked with `>'.
You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
(interactive)
- (if (bookmark-bmenu-check-position)
- (let ((bmrk (bookmark-bmenu-bookmark))
- (menu (current-buffer))
- (others ())
- tem)
- (goto-char (point-min))
- (while (re-search-forward "^>" nil t)
- (setq tem (bookmark-bmenu-bookmark))
- (let ((inhibit-read-only t))
- (delete-char -1)
- (insert ?\s))
- (or (string-equal tem bmrk)
- (member tem others)
- (setq others (cons tem others))))
- (setq others (nreverse others)
- tem (/ (1- (frame-height)) (1+ (length others))))
- (delete-other-windows)
- (bookmark-jump bmrk)
- (bury-buffer menu)
- (if others
- (while others
- (split-window nil tem)
- (other-window 1)
- (bookmark-jump (car others))
- (setq others (cdr others)))
- (other-window 1)))))
+ (let ((bmrk (bookmark-bmenu-bookmark))
+ (menu (current-buffer))
+ (others ())
+ tem)
+ (goto-char (point-min))
+ (while (re-search-forward "^>" nil t)
+ (setq tem (bookmark-bmenu-bookmark))
+ (let ((inhibit-read-only t))
+ (delete-char -1)
+ (insert ?\s))
+ (or (string-equal tem bmrk)
+ (member tem others)
+ (setq others (cons tem others))))
+ (setq others (nreverse others)
+ tem (/ (1- (frame-height)) (1+ (length others))))
+ (delete-other-windows)
+ (bookmark-jump bmrk)
+ (bury-buffer menu)
+ (if others
+ (while others
+ (split-window nil tem)
+ (other-window 1)
+ (bookmark-jump (car others))
+ (setq others (cdr others)))
+ (other-window 1))))
+
+
+(defun bookmark-bmenu-any-marks ()
+ "Return non-nil if any bookmarks are marked in the marks column."
+ (save-excursion
+ (goto-char (point-min))
+ (bookmark-bmenu-ensure-position)
+ (catch 'found-mark
+ (while (not (eobp))
+ (beginning-of-line)
+ (if (looking-at "^\\S-")
+ (throw 'found-mark t)
+ (forward-line 1)))
+ nil)))
(defun bookmark-bmenu-save (parg)
(interactive "P")
(save-excursion
(save-window-excursion
- (bookmark-save parg))))
+ (bookmark-save parg)
+ (set-buffer-modified-p nil))))
(defun bookmark-bmenu-load ()
"Load the bookmark file and rebuild the bookmark menu-buffer."
(interactive)
- (if (bookmark-bmenu-check-position)
- (save-excursion
- (save-window-excursion
- ;; This will call `bookmark-bmenu-list'
- (call-interactively 'bookmark-load)))))
+ (bookmark-bmenu-ensure-position)
+ (save-excursion
+ (save-window-excursion
+ ;; This will call `bookmark-bmenu-list'
+ (call-interactively 'bookmark-load))))
(defun bookmark-bmenu-1-window ()
"Select this line's bookmark, alone, in full frame."
(interactive)
- (if (bookmark-bmenu-check-position)
- (progn
- (bookmark-jump (bookmark-bmenu-bookmark))
- (bury-buffer (other-buffer))
- (delete-other-windows))))
+ (bookmark-jump (bookmark-bmenu-bookmark))
+ (bury-buffer (other-buffer))
+ (delete-other-windows))
(defun bookmark-bmenu-2-window ()
"Select this line's bookmark, with previous buffer in second window."
(interactive)
- (if (bookmark-bmenu-check-position)
- (let ((bmrk (bookmark-bmenu-bookmark))
- (menu (current-buffer))
- (pop-up-windows t))
- (delete-other-windows)
- (switch-to-buffer (other-buffer))
- (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
- (bookmark--jump-via bmrk 'pop-to-buffer))
- (bury-buffer menu))))
+ (let ((bmrk (bookmark-bmenu-bookmark))
+ (menu (current-buffer))
+ (pop-up-windows t))
+ (delete-other-windows)
+ (switch-to-buffer (other-buffer))
+ (let ((bookmark-automatically-show-annotations nil)) ;FIXME: needed?
+ (bookmark--jump-via bmrk 'pop-to-buffer))
+ (bury-buffer menu)))
(defun bookmark-bmenu-this-window ()
"Select this line's bookmark in this window."
(interactive)
- (if (bookmark-bmenu-check-position)
- (bookmark-jump (bookmark-bmenu-bookmark))))
+ (bookmark-jump (bookmark-bmenu-bookmark)))
(defun bookmark-bmenu-other-window ()
"Select this line's bookmark in other window, leaving bookmark menu visible."
(interactive)
(let ((bookmark (bookmark-bmenu-bookmark)))
- (if (bookmark-bmenu-check-position)
- (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
- (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))))
+ (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
+ (bookmark--jump-via bookmark 'switch-to-buffer-other-window))))
(defun bookmark-bmenu-switch-other-window ()
(pop-up-windows t)
same-window-buffer-names
same-window-regexps)
- (if (bookmark-bmenu-check-position)
- (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
- (bookmark--jump-via bookmark 'display-buffer)))))
+ (let ((bookmark-automatically-show-annotations t)) ;FIXME: needed?
+ (bookmark--jump-via bookmark 'display-buffer))))
(defun bookmark-bmenu-other-window-with-mouse (event)
"Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
"Show the annotation for the current bookmark in another window."
(interactive)
(let ((bookmark (bookmark-bmenu-bookmark)))
- (if (bookmark-bmenu-check-position)
- (bookmark-show-annotation bookmark))))
+ (bookmark-show-annotation bookmark)))
(defun bookmark-bmenu-show-all-annotations ()
"Edit the annotation for the current bookmark in another window."
(interactive)
(let ((bookmark (bookmark-bmenu-bookmark)))
- (if (bookmark-bmenu-check-position)
- (bookmark-edit-annotation bookmark))))
+ (bookmark-edit-annotation bookmark)))
(defun bookmark-bmenu-unmark (&optional backup)
Optional BACKUP means move up."
(interactive "P")
(beginning-of-line)
- (if (bookmark-bmenu-check-position)
- (progn
- (let ((inhibit-read-only t))
- (delete-char 1)
- ;; any flags to reset according to circumstances? How about a
- ;; flag indicating whether this bookmark is being visited?
- ;; well, we don't have this now, so maybe later.
- (insert " "))
- (forward-line (if backup -1 1))
- (bookmark-bmenu-check-position))))
+ (bookmark-bmenu-ensure-position)
+ (with-buffer-modified-unmodified
+ (let ((inhibit-read-only t))
+ (delete-char 1)
+ ;; any flags to reset according to circumstances? How about a
+ ;; flag indicating whether this bookmark is being visited?
+ ;; well, we don't have this now, so maybe later.
+ (insert " "))
+ (forward-line (if backup -1 1))
+ (bookmark-bmenu-ensure-position)))
(defun bookmark-bmenu-backup-unmark ()
"Move up and cancel all requested operations on bookmark on line above."
(interactive)
(forward-line -1)
- (if (bookmark-bmenu-check-position)
- (progn
- (bookmark-bmenu-unmark)
- (forward-line -1)
- (bookmark-bmenu-check-position))))
+ (bookmark-bmenu-ensure-position)
+ (bookmark-bmenu-unmark)
+ (forward-line -1)
+ (bookmark-bmenu-ensure-position))
(defun bookmark-bmenu-delete ()
To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
(interactive)
(beginning-of-line)
- (if (bookmark-bmenu-check-position)
- (let ((inhibit-read-only t))
- (delete-char 1)
- (insert ?D)
- (forward-line 1)
- (bookmark-bmenu-check-position))))
+ (bookmark-bmenu-ensure-position)
+ (with-buffer-modified-unmodified
+ (let ((inhibit-read-only t))
+ (delete-char 1)
+ (insert ?D)
+ (forward-line 1)
+ (bookmark-bmenu-ensure-position))))
(defun bookmark-bmenu-delete-backwards ()
(interactive)
(bookmark-bmenu-delete)
(forward-line -2)
- (if (bookmark-bmenu-check-position)
- (forward-line 1))
- (bookmark-bmenu-check-position))
+ (bookmark-bmenu-ensure-position)
+ (forward-line 1)
+ (bookmark-bmenu-ensure-position))
(defun bookmark-bmenu-execute-deletions ()
- "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
+ "Delete bookmarks flagged `D'."
(interactive)
(message "Deleting bookmarks...")
- (let ((hide-em bookmark-bmenu-toggle-filenames)
- (o-point (point))
+ (let ((o-point (point))
(o-str (save-excursion
(beginning-of-line)
- (if (looking-at "^D")
- nil
+ (unless (looking-at "^D")
(buffer-substring
(point)
(progn (end-of-line) (point))))))
(o-col (current-column)))
- (if hide-em (bookmark-bmenu-hide-filenames))
- (setq bookmark-bmenu-toggle-filenames nil)
(goto-char (point-min))
(forward-line 1)
(while (re-search-forward "^D" (point-max) t)
(bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
(bookmark-bmenu-list)
- (setq bookmark-bmenu-toggle-filenames hide-em)
- (if bookmark-bmenu-toggle-filenames
- (bookmark-bmenu-toggle-filenames t))
(if o-str
(progn
(goto-char (point-min))
(defun bookmark-bmenu-rename ()
"Rename bookmark on current line. Prompts for a new name."
(interactive)
- (if (bookmark-bmenu-check-position)
- (let ((bmrk (bookmark-bmenu-bookmark))
- (thispoint (point)))
- (bookmark-rename bmrk)
- (goto-char thispoint))))
+ (let ((bmrk (bookmark-bmenu-bookmark))
+ (thispoint (point)))
+ (bookmark-rename bmrk)
+ (goto-char thispoint)))
(defun bookmark-bmenu-locate ()
"Display location of this bookmark. Displays in the minibuffer."
(interactive)
- (if (bookmark-bmenu-check-position)
- (let ((bmrk (bookmark-bmenu-bookmark)))
- (message "%s" (bookmark-location bmrk)))))
+ (let ((bmrk (bookmark-bmenu-bookmark)))
+ (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))))
+ (let ((bmrk (bookmark-bmenu-bookmark))
+ (thispoint (point)))
+ (bookmark-relocate bmrk)
+ (goto-char thispoint)))
;;; Bookmark-bmenu search
+;; Store keyboard input for incremental search.
+(defvar bookmark-search-pattern)
+
(defun bookmark-read-search-input ()
"Read each keyboard input and add it to `bookmark-search-pattern'."
- (setq bookmark-search-pattern "") ; Always reset pattern to empty string
- (let ((prompt (propertize bookmark-search-prompt 'face '((:foreground "cyan"))))
- (inhibit-quit t)
- (tmp-list ())
- char)
- (catch 'break
- (while 1
- (catch 'continue
- (condition-case nil
- (setq char (read-char (concat prompt bookmark-search-pattern)))
- (error (throw 'break nil)))
+ (let ((prompt (propertize "Pattern: " 'face 'minibuffer-prompt))
+ ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
+ (tmp-list ()))
+ (while
+ (let ((char (read-key (concat prompt bookmark-search-pattern))))
(case char
- ((or ?\e ?\r) (throw 'break nil)) ; RET or ESC break search loop and lead to [1].
- (?\d (pop tmp-list) ; Delete last char of `bookmark-search-pattern' with DEL
- (setq bookmark-search-pattern (mapconcat 'identity (reverse tmp-list) ""))
- (throw 'continue nil))
- (?\C-g (setq bookmark-quit-flag t) (throw 'break nil))
+ ((?\e ?\r) nil) ; RET or ESC break the search loop.
+ (?\C-g (setq bookmark-quit-flag t) nil)
+ (?\d (pop tmp-list) t) ; Delete last char of pattern with DEL
(t
- (push (text-char-description char) tmp-list)
- (setq bookmark-search-pattern (mapconcat 'identity (reverse tmp-list) ""))
- (throw 'continue nil))))))))
-
-
-(defun bookmark-filtered-alist-by-regexp-only (regexp)
- "Return a filtered `bookmark-alist' with bookmarks matching REGEXP."
- (loop for i in bookmark-alist
- when (string-match regexp (car i)) collect i into new
- finally return new))
+ (if (characterp char)
+ (push char tmp-list)
+ (setq unread-command-events
+ (nconc (mapcar 'identity
+ (this-single-command-raw-keys))
+ unread-command-events))
+ nil))))
+ (setq bookmark-search-pattern
+ (apply 'string (reverse tmp-list))))))
(defun bookmark-bmenu-filter-alist-by-regexp (regexp)
"Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
- (let ((bookmark-alist (bookmark-filtered-alist-by-regexp-only regexp)))
+ (let ((bookmark-alist
+ (loop for i in bookmark-alist
+ when (string-match regexp (car i)) collect i into new
+ finally return new)))
(bookmark-bmenu-list)))
+
;;;###autoload
(defun bookmark-bmenu-search ()
- "Incremental search of bookmarks matching `bookmark-search-pattern'.
-`bookmark-search-pattern' is build incrementally with `bookmark-read-search-input'"
+ "Incremental search of bookmarks, hiding the non-matches as we go."
(interactive)
- (when (string= (buffer-name (current-buffer)) "*Bookmark List*")
- (let ((bmk (bookmark-bmenu-bookmark)))
- (unwind-protect
- (progn
- (setq bookmark-search-timer
- (run-with-idle-timer bookmark-search-delay 'repeat
- #'(lambda ()
- (bookmark-bmenu-filter-alist-by-regexp bookmark-search-pattern))))
- (bookmark-read-search-input))
- (progn ; [1] Stop timer.
- (bookmark-bmenu-cancel-search)
- (when bookmark-quit-flag ; C-g hit restore menu list.
- (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
- (setq bookmark-quit-flag nil))))))
-
+ (let ((bmk (bookmark-bmenu-bookmark))
+ (bookmark-search-pattern "")
+ (timer (run-with-idle-timer
+ bookmark-search-delay 'repeat
+ #'(lambda ()
+ (bookmark-bmenu-filter-alist-by-regexp
+ bookmark-search-pattern)))))
+ (unwind-protect
+ (bookmark-read-search-input)
+ (cancel-timer timer)
+ (message nil)
+ (when bookmark-quit-flag ; C-g hit restore menu list.
+ (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
+ (setq bookmark-quit-flag nil))))
+
(defun bookmark-bmenu-goto-bookmark (name)
"Move point to bookmark with name NAME."
(goto-char (point-min))
- (bookmark-bmenu-check-position)
(while (not (equal name (bookmark-bmenu-bookmark)))
(forward-line 1))
(forward-line 0))
-
-(defun bookmark-bmenu-cancel-search ()
- "Cancel timer used for searching in bookmarks."
- (cancel-timer bookmark-search-timer)
- (setq bookmark-search-timer nil))
\f
;;; Menu bar stuff. Prefix is "bookmark-menu".