Home Download Buy Blog Forum Support

Adding/removing groups (splits)

Adding/removing groups (splits)

Postby kencoken on Tue Oct 09, 2012 7:14 pm

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

Code: Select all
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):

Code: Select all
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:

Code: Select all
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:

Code: Select all
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
kencoken
 
Posts: 3
Joined: Tue Oct 09, 2012 7:06 pm

Re: Adding/removing groups (splits)

Postby agibsonsw on Tue Oct 09, 2012 8:45 pm

I'm curious where you located these commands:

Code: Select all
    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:

Code: Select all
      "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 :)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Adding/removing groups (splits)

Postby kencoken on Tue Oct 09, 2012 9:07 pm

Hi agibsonsw,

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

http://www.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 ;)

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:

Code: Select all
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 :P

If anyone knows of any better way of doing this, then I'd be very interested :) 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
kencoken
 
Posts: 3
Joined: Tue Oct 09, 2012 7:06 pm

Re: Adding/removing groups (splits)

Postby agibsonsw on Tue Oct 09, 2012 9:18 pm

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.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Adding/removing groups (splits)

Postby kencoken on Tue Oct 09, 2012 9:22 pm

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 :( 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 :P

Thanks for your time!

Ken
kencoken
 
Posts: 3
Joined: Tue Oct 09, 2012 7:06 pm

Re: Adding/removing groups (splits)

Postby agibsonsw on Tue Oct 09, 2012 9:47 pm

ST-API reference

There is another, community, version of this but I don't have the address for it.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests

cron