Mercurial > emacs
diff lisp/bookmark.el @ 106166:858a083ed2f0
* bookmark.el (bookmark-search-delay, bookmark-search-prompt): New options.
(bookmark-search-pattern, bookmark-search-timer, bookmark-quit-flag): New vars.
(bookmark-read-search-input, bookmark-filtered-alist-by-regexp-only)
(bookmark-bmenu-filter-alist-by-regexp)
(bookmark-bmenu-goto-bookmark, bookmark-bmenu-cancel-search): New funs.
(bookmark-bmenu-search): New command.
(bookmark-bmenu-mode-map): Bind it.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 20 Nov 2009 15:47:26 +0000 |
parents | 0a019b48e945 |
children | f1323114a032 |
line wrap: on
line diff
--- a/lisp/bookmark.el Fri Nov 20 15:11:16 2009 +0000 +++ b/lisp/bookmark.el Fri Nov 20 15:47:26 2009 +0000 @@ -197,6 +197,18 @@ :group 'bookmark) +(defcustom bookmark-search-delay 0.2 + "*Display when searching bookmarks is updated all `bookmark-search-delay' seconds." + :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." @@ -320,6 +332,18 @@ 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") + + +(defvar bookmark-quit-flag nil + "Non nil make `bookmark-bmenu-search' quit immediately.") + + ;; Helper functions. @@ -1525,6 +1549,7 @@ (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) (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse) map)) @@ -2072,6 +2097,79 @@ (bookmark-relocate bmrk) (goto-char thispoint)))) +;;; Bookmark-bmenu search + +(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))) + (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)) + (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)) + + +(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))) + (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'" + (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)))))) + +(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)) + ;;; Menu bar stuff. Prefix is "bookmark-menu".