Sublime Forum

Adding/removing groups (splits)

#1

Hi,

I’m trying to develop a plugin to automate creation of split views within the editor. In Sublime Text this is exposed through the concepts of ‘groups’. I can use

sublime.Window.active_group()
sublime.Window.num_groups()
sublime.Window.focus_group(group)
sublime.Window.set_view_index(view, group, index)

To move views between existing ‘groups’, but can’t seem to figure out how to create new ones. Here is my attempt at switching to the next active group in my plugin (which works):

class NextGroupCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		st_window = self.view.window()
		# get new group to move to
		new_grp = st_window.active_group() + 1
		if new_grp >= st_window.num_groups():
			new_grp = 0
		# move to new group
		st_window.focus_group(new_grp)

I want to also be able to create a new group and then create a new view into the current file in that group. Here is my (non-working) attempt:

class SplitLeftRightCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		st_window = self.view.window()
		print st_window.active_group()
		print st_window.num_groups()
		if st_window.num_groups() > 1:
			st_window.run_command('layoutSingle')
		st_window.run_command('layoutDoubleHoriz')
		print st_window.num_groups()
		st_window.run_command('newViewIntoFile')
		st_window.set_view_index(st_window.active_view(), 1, 0)
		st_window.focus_group(1)

The following lines don’t work, probably as I am calling them in the wrong way:

st_window.run_command('layoutSingle')
st_window.run_command('layoutDoubleHoriz')

Does anyone have any idea how to create new splits (or ‘groups’) in the editor?

Thanks for your help!

Best,

Ken

0 Likes

#2

I’m curious where you located these commands:

st_window.run_command('layoutSingle') st_window.run_command('layoutDoubleHoriz')

Is there a list of these somewhere?

If views follow the way the ST interface works then you cannot create a new group. You would need to use run_command to replicate the following key-binding behaviour:

"keys": "alt+shift+1"], "command": "set_layout", "args": { "cols": [0.0, 1.0], "rows": [0.0, 1.0], "cells": [0, 0, 1, 1]] } },

But someone with experience of this may correct me :smile:

0 Likes

#3

Hi agibsonsw,

Thanks so much for this. I got the ‘layoutSingle’ and ‘layoutDoubleHoriz’ commands from the following page:

sublimetext.com/docs/commands

Although I don’t think the page is related to the API. Given the lack of API documentation available at present, and having just started dabbling in this today, I was happy to take what I could find though :wink:

I tried using run_command with the set_layout command as you suggested, and it looks like it works fine. Here is the updated, fixed and now working version of the ‘split_left_right’ command posted before:

class SplitLeftRightCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		st_window = self.view.window()
		print st_window.active_group()
		print st_window.num_groups()
		st_window.run_command('set_layout',{
			'cols': [0.0, 0.5, 1.0],
			'rows': [0.0, 1.0],
			'cells': [0, 0, 1, 1], [1, 0, 2, 1]]
			})
		print st_window.num_groups()
		st_window.run_command('newViewIntoFile')
		st_window.set_view_index(st_window.active_view(), 1, 0)
		st_window.focus_group(1)

It’s useful to know that run_command takes the same format of commands as those used in the keybindings files – am sure this will come in useful for my continued ventures into plugin development :stuck_out_tongue:

If anyone knows of any better way of doing this, then I’d be very interested :smile: However, this approach seems to work, so am very happy with using it for now.

By the way, I’m throwing together a plugin to emulate ‘emacs style’ window splitting using the standard emacs keybindings. Will try and make a release when I’m done and it all goes well.

Thanks again!

Ken

0 Likes

#4

You could utilise the macro recorder as well. These will create a sequence of command calls. Then you need to work out what can be achieved via the API and what is simpler to achieve with run_command().

You might also consider (if you haven’t already) the difference between WindowCommand and TextCommand. As I understand, WindowCommand is generally applicable within the window, whereas a TextCommand is specific to a particular view. There is an ApplicationCommand as well but I’ve not used it myself.

0 Likes

#5

Thanks again agibsonsw!

Was trying to use the macro recorder too, but for some reason the ‘View->Layout->2 Columns’ etc. commands don’t seem to be recorded :frowning: Am sure it’s a good tip for when it does work though!

By the way, this may be being a little ambitious, but does anyone have any idea how to do the opposite and get the current layout of groups on the screen? Or perhaps more generally, are there any commands at all that return values from ‘run_command’? Would be nice if there was a ‘get_layout’ command to mirror ‘set_layout’, but it seems like there isn’t :stuck_out_tongue:

Thanks for your time!

Ken

0 Likes

#6

ST-API reference

There is another, community, version of this but I don’t have the address for it.

0 Likes