Sublime Forum

Keyboard shortcuts manipulating text selection / carets

#1

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: Command using selected text as parameter

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

0 Likes

#2

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

Good luck!

Geoffroy

0 Likes

#3

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:

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:



  {

    "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.

0 Likes

#4

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 :

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

0 Likes

#5

Hi

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

Cheers

Geoffroy

0 Likes

#6

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

I was planning to send the commands to supercollider using 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

0 Likes