Home Download Buy Blog Forum Support

Closing input panel early?

Closing input panel early?

Postby adamkangas on Tue Jan 24, 2012 4:34 am

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!

Code: Select all
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)
adamkangas
 
Posts: 1
Joined: Tue Jan 24, 2012 4:20 am

Re: Closing input panel early?

Postby sublimator on Tue Jan 24, 2012 6:05 am

You can create a binding `['alt+x', '<character>']` and it should be available as `character` inside the TextCommand. eg `def run(self, edit, character)`
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests