Skip to content

[emacs] inferior isearch compatible with "multiple-cursors"

Notifications You must be signed in to change notification settings

magnars/phi-search

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phi-search.el

multiple-cursors.el対応のインクリメンタルサーチ

another incremental search command, compatible with “multiple-cursors”

THE VERSION 1.1.0 BROKE BACKWARD COMPATIBILITY

“phi-search-keybindings” is now removed and, “phi-search-mode-map” is available instead. To change keybindings, DO NOT use “phi-search-keybindings” but USE “phi-search-mode-map”.

Screencast

img/search.gif

Usage

Require this script

(require 'phi-search)

then command “phi-search” and “phi-search-backward” are available. You may bind a key to the command.

(global-set-key (kbd "C-s") 'phi-search)
(global-set-key (kbd "C-r") 'phi-search-backward)

When the command is called, a small window appears at the bottom of the current window. Input query there in (emacs) regular expression to start searching. In the window, following commands are available.

  • [C-s] phi-search-again-or-next

    Move to the next matching item. If query is blank, use the last query.

  • [C-r] phi-search-again-or-previous

    Similar to phi-search-again-or-next, but move to the previous item.

  • [RET] phi-search-complete

    Finish searching.

  • [C-g] phi-search-abort

    Finish searching, and move back to the original position.

Working with Regions

(in following examples, assume “[” as mark, and “|” as point)

When phi-search is called with an active region, then the substring is used as the default query.

[region| another region   |region another region   region another |region
_______________________   ______________________   ______________________
                          query: region            query: region

Or, if phi-search is called with an active mark but no region there, mark stays active until phi-search ends. So you may use this command to expand region.

[|mark me !   [|mark me !   [mark |me !
___________   ___________   ___________
              query:        query: me

Working with multiple-cursors.el

phi-search is available even when multiple-cursors-mode is active. Here are some examples.

|foo bar baz   |foo bar baz   foo bar |baz
|foo bar baz   |foo bar baz   foo bar |baz
____________   ____________   ____________
               query:         query: baz
[|mark us !                 [|mark us !                 [mark |us !
[|mark us with a region !   [|mark us with a region !   [mark |us with a region !
_________________________   _________________________   _________________________
                            query:                      query: us
[foo| ABC foo       |foo ABC foo       foo ABC |foo
[bar| EFGHIJK bar   |bar EFGHIJK bar   bar EFGHIJK |bar
_________________   ________________   ________________
                    query: foo         query: foo

How This Works with multiple-cursors ?

“multiple-cursors” will repeat commands with fake cursors, in “post-command-hook”. The command “phi-search” will open a new buffer named phi-search to read a query. So “post-command-hook” for the command runs in phi-search buffer. Currently, “multiple-cursors” works buffer locally. Therefore, even when multiple-cursrors-mode is enabled, phi-search buffer is not affected.

When “phi-search-complete” is called, phi-search will generate a command that is repeatable for fake cursors, and set the command to “this-original-command” and “mc–this-command”. Then go back to the buffer where phi-search is called from. “post-command-hook” for the command runs here, and the generated command will be repeated with fake cursors instead. Because seen from multiple-cursors, this command is “the” command.

Customization

You may change keybindings by changing “phi-search-mode-map”, and limit of search by “phi-search-limit”.

(define-key phi-search-mode-map (kbd "<") 'phi-search-again-or-previous)
(define-key phi-search-mode-map (kbd ">") 'phi-search-again-or-next)
(setq phi-search-limit 500)

Matching items are propertized with “phi-search-match-face”, and the selected item is propertized with “phi-search-selection-face”. If they look not pretty in your colorscheme, you may modify them.

(set-face-attribute 'phi-search-selection-face nil
                    :background "orange")

phi-replace.el

“phi-replace” and “phi-replace-query” in “phi-replace.el” are interactive replace commands, that use the same interface as “phi-search”.

They have advantages in checking candidates before actually execute replace.

img/replace.gif

To use these commands, require

(requre 'phi-replace)

and bind a key.

(global-set-key (kbd "M-%") 'phi-replace-query)

In the phi-replace buffer, following commands are available.

  • [C-v] phi-replace-scroll-down

    Scroll the target window down, to check candidates.

  • [M-v] phi-replace-scroll-up

    Scroll the target window up.

  • [RET] phi-replace-complete

    Execute replace. If query is blank, use the last query.

  • [C-g] phi-search-abort

    Quit phi-replace without executing.

Known Bugs and Limitations

  • popwin.el compatibility

    Because “phi-search” command creates a new window, it is not available in the singleton pop-up window (which “popwin.el” creates). Therefore, if the selected window is pop-up window, “phi-search” command will call “isearch-forward” instead.

Notes

This command uses “multiple-cursors” variables and behavior that are not documented. Therefore, after you update “multiple-cursors”, it is good idea to test if this command works still correctly, before you use this command actually.

About

[emacs] inferior isearch compatible with "multiple-cursors"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Emacs Lisp 100.0%