Home Download Buy Blog Forum Support

Keyboard shortcuts manipulating text selection / carets

Keyboard shortcuts manipulating text selection / carets

Postby hems on Wed Feb 22, 2012 3:14 pm

Hello, am quite new to sublime text / plugins so am sorry if am repeating some already know question.

I have created a post on Technical support, but perhaps it might be in the wrong place: viewtopic.php?f=3&t=5421

Basically i wish to do Supercollider live-coding using Sublime Text, but for that i'll need to implement some keyboard shortcuts with text manipulation capabilities. I.E. executing command line calls passing selected text as parameter and/or moving the caret around after/before that.

Any advice/reference ( has someone implemented something similar ? ) is appreciated.

Thanks a lot
hems
 
Posts: 3
Joined: Mon Feb 20, 2012 12:21 pm

Re: Keyboard shortcuts manipulating text selection / carets

Postby geoffroy on Thu Feb 23, 2012 10:09 pm

I'm trying the same thing tonight :)
I'm reading the How to create a Sublime Text 2 tutorial here :
http://net.tutsplus.com/tutorials/pytho ... -2-plugin/

Good luck!

Geoffroy
geoffroy
 
Posts: 6
Joined: Sat Nov 05, 2011 9:28 am

Re: Keyboard shortcuts manipulating text selection / carets

Postby JMM on Fri Feb 24, 2012 2:09 pm

I don't know exactly what you want to do, but you can write custom code in Python and then invoke the code from key bindings. In the custom code you can access the selected text, and move the cursor around / manipulate the selections.

Here's a real quick example:

Code: Select all
import sublime, sublime_plugin


class SomeExampleCommand( sublime_plugin.TextCommand ):

  # The 'cfg' arg will be populated with the members
  # of the 'args' object in the key binding below

  def run( self, edit, **cfg ):

    view = self.view

    # Array of current selections

    sel = view.sel()   


    # First / only selection or cursor position

    point = sel[0]   


    # sublime.Region object

    line = view.line( point )


    # Content of line

    view.substr( line )


    # Clear current selections and move the cursor

    view.sel().clear()

    target_point = sublime.Region( start_char_offset, end_char_offset )

    view.sel().add( target_point )

    view.show( target_point )   



Sample key binding:

Code: Select all
[

  {

    "keys" : [ "ctrl+shift+b", "up" ],

    "command" : "some_example",

    "args" : {

      "v_dir" : -1,

      "depth_offset" : 0

    }

  }

]



The example is extracted from my Block Nav plugin, so you can take a look at that if you want. You'd also want to see the Sublime API docs.
JMM
 
Posts: 9
Joined: Fri Feb 03, 2012 1:23 pm
Location: Portsmouth NH

Re: Keyboard shortcuts manipulating text selection / carets

Postby geoffroy on Mon Feb 27, 2012 5:25 pm

Thanks for your reply!

I'm new both to Python and to Sublime plugin development.

SuperCollider consists in sclang, a command line executable which waits for commands on STDIN and displays the result in STDOUT.
It's a blocking.

The idea is to start a new thread with sclang running, and to send commands to sclang, and display what's on STDOUT in a console inside Sublime Text.

I started with this code :

Code: Select all
import sublime, sublime_plugin
import subprocess

class SupercolliderCommand(sublime_plugin.WindowCommand):
  def run(self):
    # create output panel
    panel_name = 'supercollider'
    if not hasattr(self, 'output_view'):
        self.output_view = self.window.get_output_panel(panel_name)
    v = self.output_view

    # call supercollider
    if not hasattr(self, 'sclang_instance'):
        sc_dir = 'C:\\Program Files\\SuperCollider-3.5-rc2\\'
        sc_exe = 'sclang'
        self.sclang_instance = subprocess.Popen(sc_exe, cwd=sc_dir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

    edit = v.begin_edit()
    stdoutdata, stderrdata = self.sclang_instance.communicate()
    v.insert(edit, v.size(), stdoutdata + '\n')
    v.end_edit(edit)
    v.show(v.size()) # scroll down
       
    self.window.run_command("show_panel", {"panel": "output." + panel_name})


It launches sclang but
1. there's a new instance of sclang launched every time I run the command (bad!)
2. it blocks sublime text

If I kill the sclang process, I can see that my panel was created and sclang displayed something, so I'm on the right way.

-> I have to find how to launch a separate thread with sclang, and how to communicate with that process.

Cheers!

geoffroy
geoffroy
 
Posts: 6
Joined: Sat Nov 05, 2011 9:28 am

Re: Keyboard shortcuts manipulating text selection / carets

Postby geoffroy on Tue Feb 28, 2012 5:21 pm

Hi

Managed to get a first alpha version working.
Still have some issues with multiple lines.
https://github.com/geoffroymontel/super ... blime-text

Cheers

Geoffroy
geoffroy
 
Posts: 6
Joined: Sat Nov 05, 2011 9:28 am

Re: Keyboard shortcuts manipulating text selection / carets

Postby hems on Fri Mar 02, 2012 5:38 pm

Cool geoffroymontel am not alone in this mission anymore!
:ugeek:

I was planning to send the commands to supercollider using https://github.com/rfwatson/sc3ctrl ( allows control to supercollider via command line ) or this "PIPE" method used by the SCMATE bundle: https://github.com/sbl/scmate.tmbundle which theoretically has better communication ( messages with more than 300 chars ).

I'll send you private msg with my contacts, and check what you have done.

peace
hems
 
Posts: 3
Joined: Mon Feb 20, 2012 12:21 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests