Sublime Forum

Closing input panel early?

#1

I’m considering making the leap to SublimeText 2 from emacs and as an introduction I’ve been trying to port some of my usual emacs commands into Sublime Text.

I’m trying to create a version of my “M-x zap-up-to-char” which takes a single character of input from the user and erases everything between the current selection and the first occurrence of that character. I have it working if I open the input panel, type a character, and press enter, so that on_done() fires. However, I’d like to eliminate the need to press enter, by having the process complete via the on_change() callback.

I’m not sure how to close the input panel early programmatically. What would I add into on_change() to make this possible?

Thanks!

import sublime, sublime_plugin


class PromptZapUpToCharCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel("Zap Up To Char:", "", self.on_done, self.on_change, None)
        pass

    def on_done(self, text):
        try:
            char = text[0]
            if self.window.active_view():
                self.window.active_view().run_command("zap_up_to_char", {"char": char})
        except ValueError:
            pass

    def on_change(self, text):
        try:
            if text != "":
                pass  # possibly close window early here
        except ValueError:
            pass


class ZapUpToCharCommand(sublime_plugin.TextCommand):

    def run(self, edit, char):
        edit = self.view.begin_edit()
        for s in self.view.sel():
            if s.empty():
                matching_region = self.view.find(char, s.b)
                if matching_region:
                    self.view.erase(edit, sublime.Region(s.b, matching_region.a))
        self.view.end_edit(edit)
0 Likes

#2

You can create a binding 'alt+x', '<character>'] and it should be available as character inside the TextCommand. eg def run(self, edit, [size=150] **character**[/size])

0 Likes