[Arcana] Isearch for symbols easily

Jim Blandy jimb at red-bean.com
Wed Feb 28 20:15:26 CST 2007


A while back, I implemented the \_< and \_> operators in Emacs, that
match the beginning and end of a symbol in a programming-language
mode.  So, for example, \_<foo\_> matches foo, but won't match
anything in foof, foo_bar or bar_foo.  It's really searching for that
identifier.

The thing is, those are a bear to type.  So here's a new key binding
for isearch mode that takes the current isearch (if it's a regexp
isearch) and wraps \_< and \_> around it.  Start a regexp isearch, get
the right identifier in there (perhaps by using C-w), and then hit M-e
to restrict the search to exactly that symbol.

;;;; Easily convert regexp isearches to whole-symbol searches.
(defun jimb-isearch-regexp-whole-symbol ()
  "Wrap the current regexp in \_< and \_> operators, to search for a
whole symbol."
  (interactive)
  (if (not isearch-regexp) (ding)
    (if (not (string-match "^\\\\_<.*\\\\_>" isearch-string))
        (setq isearch-string (concat "\\_<" isearch-string "\\_>")
              isearch-message (mapconcat 'isearch-text-char-description
                                         isearch-string "")
              ;; Don't move cursor in reverse search.
              isearch-yank-flag t)))
  (isearch-search-and-update))

(define-key isearch-mode-map "\M-e" 'jimb-isearch-regexp-whole-symbol)



More information about the Arcana mailing list