Sublime Forum

Disable Menu Item when selection empty

#1

Hi,

I have made a menu entry for me as follows

{ "caption": "Cut and Paste", "command": "chain", "args": { "commands": [ ["cut"], ["new_file"], ["paste"] ] }, "context": [ { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, ] },

which allows me to cut the selected text and then pastes it into a new file. I now would like to disable the command if the selection is empty, i.e. has length 0. How is this possible? I trief it with the “context”, but it doesn’t seem to work.

0 Likes

#2

is chain a command defied by you?

0 Likes

#3

oh, sorry, I forgot to mention. chain command comes from the plugin ChainOfCommands. It simply executes one command after each other in the ‘args’ list.

0 Likes

#4

you can copy and rename the command https://github.com/jisaacks/ChainOfCommand/blob/master/chain.py and then use the is_enable methodd (a sibling of run) as described in the API http://www.sublimetext.com/docs/3/api_reference.html and to check for selection you can use undocumented method view.has_non_empty_selection_region()

0 Likes

#5

many thanks @tito, it works perfectly!

`class CutAndPasteCommand(sublime_plugin.WindowCommand):
def run(self):
window = self.window
view = window.active_view()
window.run_command(“cut”)
window.run_command(“new_file”)
window.run_command(“paste”)

def is_enabled(self):
    window = self.window
    view = window.active_view()
    return view.has_non_empty_selection_region()`

Just another small question: If i use

[ { "id": "edit", "children": [ { "caption": "Cut and Paste", "id": "cutandpaste", "command": "cut_and_paste" } ] } ]

to add my command to the “Edit” menu, the command appears at the very bottom. Is it possible to insert the command just after “Paste” ?

0 Likes