Home Download Buy Blog Forum Support

I could use a plugin skeleton with example of clipboard mgmt

I could use a plugin skeleton with example of clipboard mgmt

Postby Joche on Tue Mar 11, 2014 8:44 am

I'm in the need of a feature where I have something like this in my editor:

Code: Select all
db.kittens.aggregate([
    {$unwind : "kitty.name"},
    {$sort : {"kitty.age" : -1}}
])

This is just example code for showing formatting

I want to be able to mark the sentence, click on a keyboard shortcut and have the marked sentence filtered without newlines and tabs and double-spaces and added to the clipboard. I don't want the sentence in the editor to be changed.

I read throw the "how to create a plugin" document but it's a bit overwhelming at the moment, so if someone has an example of something similar to the request above it would really kick start me.

Thanks alot in advance,

Joche
Joche
 
Posts: 2
Joined: Tue Mar 11, 2014 8:37 am

Re: I could use a plugin skeleton with example of clipboard mgmt

Postby iamntz on Tue Mar 11, 2014 8:52 am

https://www.sublimetext.com/docs/2/api_reference.html

There is a set_clipboard method available, so i guess you could hook into it.

Later edit: i re-read your request, so here is a starting point:

Code: Select all
import sublime, sublime_plugin, re;

class CopyAndProcessCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    view = self.view
    selection = view.sel() # get selection coords
    selection = view.substr( selection[0] ) # get selected text

    processedSelection = re.sub( "(\r|\n|\t)", "", selection ) # removes all new lines and tabs
    sublime.set_clipboard( processedSelection ) # set clipboard
iamntz
 
Posts: 915
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

Re: I could use a plugin skeleton with example of clipboard mgmt

Postby Joche on Wed Mar 12, 2014 9:07 am

Thank you a lot - this really got me started. Actually, your code worked exactly as I was intending. Thanks again! For other readers I can also tip that in "Preferences -> Key bindings - user" you can add a key binding if you don't prefer to run the plugin from console.
Code: Select all
[
{ "keys": ["ctrl+shift+c"], "command": "copy_and_process" }
]
Joche
 
Posts: 2
Joined: Tue Mar 11, 2014 8:37 am

Re: I could use a plugin skeleton with example of clipboard mgmt

Postby FichteFoll on Thu Mar 13, 2014 3:29 pm

Joche wrote:I want to be able to mark the sentence, click on a keyboard shortcut and have the marked sentence filtered without newlines and tabs and double-spaces and added to the clipboard. I don't want the sentence in the editor to be changed.


The proposed code by iamntz does not remove double (or multiple spaces), this will:

Code: Select all
    processedSelection = ' '.join(selection.split()) # replaces all whitspaces (also multiple) with a single space


Not sure about performance, but it should be faster as well.

If you were to mimimc the default copy behaviour with multiple selections, try this:
Code: Select all
import sublime
import sublime_plugin


class CopyAndProcessCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        lines = []
        for r in self.view.sel():
            text = self.view.substr(r)

            # replaces all whitspaces (also multiple) with a single space
            lines.append(' '.join(text.split()))

        sublime.set_clipboard('\n'.join(lines))


Or, if you're fancy:
Code: Select all
import sublime
import sublime_plugin


class CopyAndProcessCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # replaces all whitspaces (also multiple) with a single space
        sublime.set_clipboard('\n'.join(' '.join(self.view.substr(r).split())
                              for r in self.view.sel()))
FichteFoll
 
Posts: 406
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 10 guests