Sublime Forum

Menu question

#1

I have been trying to make a menu for my plug in and am finding there is a lack of documentation on the menu structure.

For instance the word wrap menu entry is

What does toggle do, and how can my plugin find out about the state of the check?
There is a lot of other commands that are kind of cryptic.
toggleApp

this is intresting also

how does the repeat work?

For instance can i make a menu option that pops up a single line edit window and allow the user to enter a name? Then have that menu option be “Server: xxxxx” and change when the user enters a new name?

0 Likes

#2

(for other readers: this is relevant to Sublime Text 1 only)

The ‘toggle’ command toggles the value of an option. You can get the current value of an option via:

view.options().get('wordWrap')

‘repeat’ is just shorthand for repeating the item entry in the XML that many times, each time replacing %i with an incrementing number, e.g.,

<item caption="&amp;%I %s" command="openRecentProject 0"/>
<item caption="&amp;%I %s" command="openRecentProject 1"/>
...

With regards to adding your own menu entries, the general way is to make a plugin that does want you want, and then create a menu item with the corresponding command. It’s not possible to for Python commands to customize the text shown for their menu item, however.

0 Likes

#3

Added this to the Main.sublime-menu file

The item appears in my menu and is checkable, and uncheckable.

Using the console all I get returned is None

print view.options().get(“runClient”)
None

Do i have to set the name into a config file or something to make it work?

0 Likes

#4

The option won’t exist until you toggle it the first time:

>>> print view.options().get('foo')
None
>>> view.runCommand('toggle foo')
>>> view.options().get('foo')
True
0 Likes

#5

The example you give works, but

view.runCommand(“toggle runClient”)
print view.options().get(“runClient”)
False

I check the menu at this point and it is checked.

view.runCommand(“toggle runClient”)
print view.options().get(“runClient”)
True

Again checked the menu and it is still checked, many repeats don’t change the setting.

Also changing the menu so it is checked or unchecked still produces the same result as the last run command. I am adding this to Main.sublime-menu if that makes a difference. There appears to be a non obvious disconnect between the view.options() and the main menu. Below is the menu XML I am using and the console commands that I am using to try and poll the options.

	<menu caption="&amp;CCP">
		<item caption="&amp;Launch" command="slipwayCmd"/>
		<menu caption="&amp;Client">
			<item caption="&amp;Run Client" command="toggle runClient"/>
			<item caption="&amp;Debug" command="toggle debugClient"/>
			<item caption="&amp;Jessica" command="toggle jessicaClient"/>
			<item caption="&amp;x64" command="toggle x64Client"/>
			<item caption="Debugger" command="toggle debuggerClient"/>
		</menu>
	</menu>

view.runCommand(“toggle runClient”)
print view.options().get(“runClient”)

0 Likes

#6

also here is my command

[code]import sublime, sublimeplugin

class slipwayCmd(sublimeplugin.ApplicationCommand):
# -----------------------------------------------------------------------------------
def run(self, args):
o = sublime.options()
a = “runClient”, “debugClient”, “jessicaClient”, “x64Client”, “debuggerClient”,]
for each in a:
print each, o.get(each)
print “-------------------------------------”[/code]

0 Likes

#7

Your command is asking for the global options, but the toggle command works on the view options. Use view.options().

0 Likes

#8

yes but as i said in the first post, the menu doesn’t change the value even when i use the view.options().

The root problem is still if i toggle a option in the menu, none of the ways presented in this thread return the current state of the check on the menu.

0 Likes

#9

Ah, this ones a bit tricky. view.options() will return the options for the current file being edited. The menu, on the other hand, will display the value of the option for the widget that currently has input focus. This may well mean that it’s displaying the value of the option from within the console view, rather than the view associated with the file you were last editing.

To ensure that the menu is always displaying the the option from the currently edited file, check that the file itself has input focus before showing the menu, and not the console panel.

0 Likes

#10

yep that did it.
using the following to get the options and they mirror the menu exactly.

sublime.activeWindow().activeView().options()

Thanks for the help.

0 Likes

#11

Another question. Is possible to get the location of the project? and or pull options from the project?

0 Likes

#12

There is the undocumented window.project() function, which will give you access to the project filename.

0 Likes

#13

Problem is this seems not to be ported to Sublime 2 yet. Am I wrong?

0 Likes