Sublime Forum

I want to rewrite incremental find - is that possible?

#1

I have spent a weekend hacking Sublime to give me basic emacs functionality. I know other packages already exist but they didn’t really do things quite right, and in many ways Emacs got many of the basics just absolutely right. It was a successful weekend of hacking and I am still learning.

I like the incremental find that comes with Sublime, but it is missing a couple of features. The main ones for me are:

  • if I type ctrl+s immediately after the first one, it should use the same search string that I ended with last time
  • if I type ctrl+w it should append the text in front of the cursor to the current search string
  • if I search for a MixedCase item, the search automatically becomes case sensitive, otherwise case insensitive
  • when it’s done I should be able to get a notification so I can set the mark at the original location
  • I’d love a way to say “find the rest in this direction without wrapping” so I can then go all multi-cursor on the ones I’ve selected

I realize these are all personal taste so I am happy to implement it myself, but I either need to start from scratch (is that possible?) or I need a way to get hooks into what is happening in the incremental_find panel. I am unclear on how to do either.

Here are a few observations and comments about what I think I have learned so far:

  • I think it’s a regular view inside the incremental_find panel. My text command event listener seems to be invoked when I run commands there. That is excellent! At first I thought the panel was a complete special case.
  • If I put a “context” in my key binding such that it’s only active if the incremental_find panel is showing and has focus, I don’t seem to be getting my function called. Should that work?
  • If that does work, would the view argument be the view for the incremental find or the active view of the active window?
  • Is there a way to capture all key strokes? I want to be able to terminate my incremental search if I type some other command, like Control-A.
0 Likes

#2

Looks very possible.

[quote]* If I put a “context” in my key binding such that it’s only active if the incremental_find panel is showing and has focus, I don’t seem to be getting my function called. Should that work?
[/quote]

Looks like… should work, if “operand” accepts the name of your panel.

[quote] { “keys”: “alt+enter”], “command”: “find_all”, “args”: {“close_panel”: true},
“context”: {“key”: “panel”, “operand”: “incremental_find”}, {“key”: “panel_has_focus”}]
},[/quote]

[quote]* If that does work, would the view argument be the view for the incremental find or the active view of the active window?
[/quote]

show_input_panel(name) returns a view, you can keep a reference to the view, and also keep track if the view is shown or not, There is also an on_change listener.

[quote]* Is there a way to capture all key strokes? I want to be able to terminate my incremental search if I type some other command, like Control-A.
[/quote]

Probably with keybindings.

you can log inputs and commands to a console, See sublimetext.com/docs/3/api_reference.html

You need to hack hard and is complicated without testing, because some things that are supposed to work, in some very special conditions, does not work. So I can’t tell with confidence.

0 Likes

#3

Ho ho! It worked! My problem with the key bindings was - oh my - specifying “content” instead of “context” in the key binding.

I have managed to get emacs-style incremental search working. What this means is:

  • As you type characters the search string grows and all the matches are highlighted.

  • If you type the search key (Control-s) again, you move to the next match.

  • If you type backspace, you go back exactly one state. That might be going back to the previous match OR going back to a search string with one less character in it.

  • If you type Control-W characters from buffer are appended to the search string.

  • If you find yourself searching for something that doesn’t exist, you can type Control-G to go back to the last state your search was succeeding.

  • If you type Control-G and your search is working, you will abort the search and go back to where you started, panel hidden.

  • If you type Control-S to start a search and then type it again immediately, it uses the last search string you used.

  • If you click the mouse in the window, the search is automatically stopped.

  • And because this is part of a larger project of mine, when you are done with your search the mark is set to where you started.

I’ve made the following sublime text enhancements:

  • Like ST when you start a search one item is highlighted with a solid background and other matches are outlined.

  • If you type Control-S again it moves that highlighted item to the next item. (This is the same.)

  • If you press CMD-D while searching, however, it adds the current item to the set of cursors, and moves to the next item and highlights it. By “move to the next item” I mean moves forward or backward depending on the direction you were going in when you pressed CMD-D.

  • This means you can add to your resulting cursors with CMD-D or you can SKIP the current item by just searching again with Control-S. This allows you to have non-contiguous
    matches in your multi-cursor when you’re done.

  • If you accidentally go too far and add something you don’t want, you can press backspace, because backspaces restores you to your previous state, including your selected cursors.

  • When you’re done you press Return and your cursors are waiting for you!

0 Likes

#4

I’m very interested in what you did. Could you please share it ?

0 Likes

#5

Sorry I didn’t notice this for so long.

It’s part of the sublemacspro plugin.

0 Likes

#6

Is this possible at this moment? Because it sounds awesome!

0 Likes

#7

It’s all part of the sublemacspro plugin. It’s not possible to separate it out.

1 Like