Sublime Forum

It is possible to copy the command palette input text without using window.run_command("copy")?

#1

On the recent development build 3156 of Sublime Text, there was this fix:

Reinvoking the Command Palette while it is open, but without focus, now works properly

However, that bug was the only way I have to close the command palette without erasing my initial text: Core$1814. On build 3155, If I called the command palette command again, when it was without focus, then it does not deleted my inputted text.

Now, I wrote a plugin (fixed_command_palette_input_history.py) which runs as series of commands like:

...
self.window.run_command( "show_overlay", {"overlay": "command_palette"} )
self.window.run_command( "select_all" )
self.window.run_command( "copy" )
current_clipboard_text = sublime.get_clipboard()
...
sublime.set_clipboard( clipboard_text )
self.window.run_command( "paste" )
self.window.run_command( "select_all" )

Then it was able to fix the issue Core$1814, however now I loose the clipboard text I had before opening the command palette, as it is replaced by the text I copied from the command palette input widget.

I am thinking about saving and restoring it before running these commands, however is there another way I can copy the contents of the Command Palette other than:

self.window.run_command( "select_all" )
self.window.run_command( "copy" )
0 Likes

Beyond `sublime.project_file_name()` can I get something like `sublime.workspace_file_name()`?
#2

After search on the forum I found this post by jps in 2011:

Then I did what he said and my TextCommand worked on the widget. This 19b2534 is the commit where I removed my older code using the clipboard command, and added the new command using view.sel():

Here it is how I open the command palette now using the WindowCommand:

class FixedCommandPaletteLastInputHistoryCommand(sublime_plugin.WindowCommand):

    def run(self):
        global is_command_palette_open
        global is_command_palette_just_closed

        if is_command_palette_open:
            self.window.run_command( "fixed_hide_overlay_which_is_correctly_logged" )

        else:
            self.window.run_command( "show_overlay", {"overlay": "command_palette"} )
            self.window.run_command( "select_all" )

            self.window.run_command( "fixed_command_palette_last_input_history_helper")
            self.window.run_command( "select_all" )

            is_command_palette_open = True

And this is the recent/just created fixed_command_palette_last_input_history_helper which extends the TextCommand class:

class FixedCommandPaletteLastInputHistoryHelperCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        selections = self.view.sel()

        global widget_text
        current_widget_text = self.view.substr( selections[0] )

        if len( current_widget_text ) \
                and current_widget_text != "\n":

            widget_text = current_widget_text
            self.view.erase( edit, selections[0] )

        self.view.run_command( "append", {"characters": widget_text} )

In order for everything work and it remember the command palette input after closing it with the escape key, still there are other commands and keybinds required, you can find remaining code at:

  1. https://github.com/evandrocoan/ClearCursorsCarets
0 Likes

#3

Updates

Now, I have a dedicated plugin to remember the command palette input:

0 Likes