5

Suppose we have the following text:

(print "thIis-is-a-text")

and the cursor is in the word "this" which between char 'h' and 'i'.

In emacs, if I type C-s C-w, the text to search is 'is',

again C-w is 'is-is',

again C-w is 'is-is-a',

again C-w is 'is-is-a-text',

again C-w is 'is-is-a-text"'...

And there is a emacs plugin expand region: "Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want."

So I want to make C-s C-w to be more intelligent: to combine with expand region.

Cursor between char 'h' and 'i' in the word 'this' again, my goal is:

when I type C-s C-w, the word 'this' will be the text to search,

again will be 'this-is-a-text',

again will be '"this-is-a-text"',

again will be 'print "this-is-a-text"',

again will be '(print "this-is-a-text")',

....(behaves like expand region, maybe this is not a good example...)

As I find sometimes expand region is quite useful in searching texts, I hope someone can write some codes to achieve this for me since I am new to emacs and elisp. Thanks and please forgive my broken English! :)

Boris
  • 555
  • 7
  • 22

2 Answers2

3

There's a very simple package called thingopt that uses thingatpt to do something similar to expand-region. I've been using a modified version for quite a while now, which adds isearch support. This question reminded me to fork and put up a pull request. I have the following in my init.el:

(define-key isearch-mode-map (kbd "C-S-s") 'upward-isearch-thing)
(define-key isearch-mode-map (kbd "M-3") 'upward-isearch-thing)

(global-set-key (kbd "C-S-s") 'upward-mark-thing)
(global-set-key (kbd "M-3") 'upward-mark-thing)

(setq upward-mark-thing-list
      '(email
        url
        word
        symbol
        string
        (up-list . *)
        paragraph
        ))

I've tried expand-region, and I think I'd like it better, but it looks much more complicated, and I'd have to add isearch support before I'd start using it. Hopefully I'll (or someone else will) get around to it someday.

jpkotta
  • 9,237
  • 3
  • 29
  • 34
  • I have try your thingopt just now. It works fine.Yes, both expand region and thingopt are great but expand region is more designed for regions. Thingopt may be strong enough in search. Thanks, jpkotta. – Boris Jun 16 '12 at 01:17
1
(defadvice isearch-yank-word-or-char (before move-to-beginning-of-word)
  (unless (eq last-command this-command)
    (goto-char (car (bounds-of-thing-at-point 'word)))))
(ad-activate 'isearch-yank-word-or-char)
desudesudesu
  • 2,185
  • 1
  • 15
  • 20
  • Thanks for you answer. But it seems that only work for C-s C-w one time(Selecting the current word under cursor). When I keep typing C-w after a C-s, the text to search seems not to behave as the plugin 'expand-region'(Not trying to expand region). – Boris Jun 15 '12 at 18:00