comparison lisp/bookmark.el @ 106179:0cc25cd9870d

(bookmark-search-prompt, bookmark-search-timer): Remove. (bookmark-search-pattern): Move and leave unbound. (bookmark-bmenu-mode-map): Change binding. (bookmark-read-search-input): Simplify. Don't use text-char-description. Don't error on non-char events. (bookmark-filtered-alist-by-regexp-only): Remove by folding into the only caller (i.e. bookmark-bmenu-filter-alist-by-regexp). (bookmark-bmenu-search): Don't check we're in a bookmark-list buffer. Use a local var for the timer. (bookmark-bmenu-cancel-search): Remove by folding into the only caller (i.e. bookmark-bmenu-search).
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sat, 21 Nov 2009 04:43:10 +0000
parents f1323114a032
children 36947fb87ecd
comparison
equal deleted inserted replaced
106178:3f65383da746 106179:0cc25cd9870d
194 (defcustom bookmark-menu-length 70 194 (defcustom bookmark-menu-length 70
195 "Maximum length of a bookmark name displayed on a popup menu." 195 "Maximum length of a bookmark name displayed on a popup menu."
196 :type 'integer 196 :type 'integer
197 :group 'bookmark) 197 :group 'bookmark)
198 198
199 199 ;; FIXME: Is it really worth a customization option?
200 (defcustom bookmark-search-delay 0.2 200 (defcustom bookmark-search-delay 0.2
201 "*When searching bookmarks, redisplay every `bookmark-search-delay' seconds." 201 "Time before `bookmark-bmenu-search' updates the display."
202 :group 'bookmark 202 :group 'bookmark
203 :type 'integer) 203 :type 'integer)
204
205
206 (defcustom bookmark-search-prompt "Pattern: "
207 "*Prompt used for `bookmark-bmenu-search'."
208 :group 'bookmark
209 :type 'string)
210
211 204
212 (defface bookmark-menu-heading 205 (defface bookmark-menu-heading
213 '((t (:inherit font-lock-type-face))) 206 '((t (:inherit font-lock-type-face)))
214 "Face used to highlight the heading in bookmark menu buffers." 207 "Face used to highlight the heading in bookmark menu buffers."
215 :group 'bookmark 208 :group 'bookmark
330 (defvar bookmark-yank-point 0 323 (defvar bookmark-yank-point 0
331 "The next point from which to pull source text for `bookmark-yank-word'. 324 "The next point from which to pull source text for `bookmark-yank-word'.
332 This point is in `bookmark-curent-buffer'.") 325 This point is in `bookmark-curent-buffer'.")
333 326
334 327
335 (defvar bookmark-search-pattern ""
336 "Store keyboard input for incremental search.")
337
338
339 (defvar bookmark-search-timer nil
340 "Timer used for searching")
341
342
343 (defvar bookmark-quit-flag nil 328 (defvar bookmark-quit-flag nil
344 "Non nil make `bookmark-bmenu-search' quit immediately.") 329 "Non nil make `bookmark-bmenu-search' quit immediately.")
345
346
347 330
348 ;; Helper functions. 331 ;; Helper functions.
349 332
350 ;; Only functions on this page and the next one (file formats) need to 333 ;; Only functions on this page and the next one (file formats) need to
351 ;; know anything about the format of bookmark-alist entries. 334 ;; know anything about the format of bookmark-alist entries.
1547 (define-key map "R" 'bookmark-bmenu-relocate) 1530 (define-key map "R" 'bookmark-bmenu-relocate)
1548 (define-key map "t" 'bookmark-bmenu-toggle-filenames) 1531 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1549 (define-key map "a" 'bookmark-bmenu-show-annotation) 1532 (define-key map "a" 'bookmark-bmenu-show-annotation)
1550 (define-key map "A" 'bookmark-bmenu-show-all-annotations) 1533 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1551 (define-key map "e" 'bookmark-bmenu-edit-annotation) 1534 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1552 (define-key map "\M-g" 'bookmark-bmenu-search) 1535 ;; The original binding of M-g hides the M-g prefix map.
1536 ;; If someone has a better idea than M-g s, I'm open to suggestions.
1537 (define-key map [?\M-g ?s] 'bookmark-bmenu-search)
1553 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse) 1538 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1554 map)) 1539 map))
1555 1540
1556 ;; Bookmark Buffer Menu mode is suitable only for specially formatted 1541 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1557 ;; data. 1542 ;; data.
2097 (bookmark-relocate bmrk) 2082 (bookmark-relocate bmrk)
2098 (goto-char thispoint)))) 2083 (goto-char thispoint))))
2099 2084
2100 ;;; Bookmark-bmenu search 2085 ;;; Bookmark-bmenu search
2101 2086
2087 ;; Store keyboard input for incremental search.
2088 (defvar bookmark-search-pattern)
2089
2102 (defun bookmark-read-search-input () 2090 (defun bookmark-read-search-input ()
2103 "Read each keyboard input and add it to `bookmark-search-pattern'." 2091 "Read each keyboard input and add it to `bookmark-search-pattern'."
2104 (setq bookmark-search-pattern "") ; Always reset pattern to empty string 2092 (let ((prompt (propertize "Pattern: " 'face 'minibuffer-prompt))
2105 (let ((prompt (propertize bookmark-search-prompt 2093 ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
2106 'face '((:foreground "cyan")))) 2094 (tmp-list ()))
2107 (inhibit-quit t) 2095 (while
2108 (tmp-list ()) 2096 (let ((char (read-key (concat prompt bookmark-search-pattern))))
2109 char)
2110 (catch 'break
2111 (while 1
2112 (catch 'continue
2113 (condition-case nil
2114 (setq char (read-char (concat prompt bookmark-search-pattern)))
2115 (error (throw 'break nil)))
2116 (case char 2097 (case char
2117 ((or ?\e ?\r) ; RET or ESC break search loop and lead to [1]. 2098 ((?\e ?\r) nil) ; RET or ESC break the search loop.
2118 (throw 'break nil)) 2099 (?\C-g (setq bookmark-quit-flag t) nil)
2119 (?\d (pop tmp-list) ; Delete last char of `bookmark-search-pattern' 2100 (?\d (pop tmp-list) t) ; Delete last char of pattern with DEL
2120 (setq bookmark-search-pattern
2121 (mapconcat 'identity (reverse tmp-list) ""))
2122 (throw 'continue nil))
2123 (?\C-g (setq bookmark-quit-flag t) (throw 'break nil))
2124 (t 2101 (t
2125 (push (text-char-description char) tmp-list) 2102 (if (characterp char)
2126 (setq bookmark-search-pattern 2103 (push char tmp-list)
2127 (mapconcat 'identity (reverse tmp-list) "")) 2104 (setq unread-command-events
2128 (throw 'continue nil)))))))) 2105 (nconc (mapcar 'identity
2129 2106 (this-single-command-raw-keys))
2130 2107 unread-command-events))
2131 (defun bookmark-filtered-alist-by-regexp-only (regexp) 2108 nil))))
2132 "Return a filtered `bookmark-alist' with bookmarks matching REGEXP." 2109 (setq bookmark-search-pattern
2133 (loop for i in bookmark-alist 2110 (apply 'string (reverse tmp-list))))))
2134 when (string-match regexp (car i)) collect i into new
2135 finally return new))
2136 2111
2137 2112
2138 (defun bookmark-bmenu-filter-alist-by-regexp (regexp) 2113 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2139 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list." 2114 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2140 (let ((bookmark-alist (bookmark-filtered-alist-by-regexp-only regexp))) 2115 (let ((bookmark-alist
2116 (loop for i in bookmark-alist
2117 when (string-match regexp (car i)) collect i into new
2118 finally return new)))
2141 (bookmark-bmenu-list))) 2119 (bookmark-bmenu-list)))
2120
2142 2121
2143 ;;;###autoload 2122 ;;;###autoload
2144 (defun bookmark-bmenu-search () 2123 (defun bookmark-bmenu-search ()
2145 "Incrementally search bookmarks matching `bookmark-search-pattern'. 2124 "Incremental search of bookmarks, hiding the non-matches as we go."
2146 `bookmark-search-pattern' is built incrementally with 2125 (interactive)
2147 `bookmark-read-search-input'." 2126 (let ((bmk (bookmark-bmenu-bookmark))
2148 (interactive) 2127 (bookmark-search-pattern "")
2149 (when (string= (buffer-name (current-buffer)) "*Bookmark List*") 2128 (timer (run-with-idle-timer
2150 (let ((bmk (bookmark-bmenu-bookmark))) 2129 bookmark-search-delay 'repeat
2151 (unwind-protect 2130 #'(lambda ()
2152 (progn 2131 (bookmark-bmenu-filter-alist-by-regexp
2153 (setq bookmark-search-timer 2132 bookmark-search-pattern)))))
2154 (run-with-idle-timer 2133 (unwind-protect
2155 bookmark-search-delay 'repeat 2134 (bookmark-read-search-input)
2156 #'(lambda () 2135 (cancel-timer timer)
2157 (bookmark-bmenu-filter-alist-by-regexp 2136 (when bookmark-quit-flag ; C-g hit restore menu list.
2158 bookmark-search-pattern)))) 2137 (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
2159 (bookmark-read-search-input)) 2138 (setq bookmark-quit-flag nil))))
2160 (progn ; [1] Stop timer.
2161 (bookmark-bmenu-cancel-search)
2162 (when bookmark-quit-flag ; C-g hit restore menu list.
2163 (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
2164 (setq bookmark-quit-flag nil))))))
2165 2139
2166 (defun bookmark-bmenu-goto-bookmark (name) 2140 (defun bookmark-bmenu-goto-bookmark (name)
2167 "Move point to bookmark with name NAME." 2141 "Move point to bookmark with name NAME."
2168 (goto-char (point-min)) 2142 (goto-char (point-min))
2169 (bookmark-bmenu-check-position) 2143 (bookmark-bmenu-check-position)
2170 (while (not (equal name (bookmark-bmenu-bookmark))) 2144 (while (not (equal name (bookmark-bmenu-bookmark)))
2171 (forward-line 1)) 2145 (forward-line 1))
2172 (forward-line 0)) 2146 (forward-line 0))
2173 2147
2174
2175 (defun bookmark-bmenu-cancel-search ()
2176 "Cancel timer used for searching in bookmarks."
2177 (cancel-timer bookmark-search-timer)
2178 (setq bookmark-search-timer nil))
2179 2148
2180 2149
2181 ;;; Menu bar stuff. Prefix is "bookmark-menu". 2150 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2182 2151
2183 (defun bookmark-menu-popup-paned-menu (event name entries) 2152 (defun bookmark-menu-popup-paned-menu (event name entries)