Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better search in emacs with expand region

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! :)

like image 885
Boris Avatar asked Oct 08 '22 12:10

Boris


1 Answers

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.

like image 191
jpkotta Avatar answered Oct 13 '22 11:10

jpkotta