Home Download Buy Blog Forum Support

HOWTO: Keybind, insert_snippet + clipboard question.

HOWTO: Keybind, insert_snippet + clipboard question.

Postby cosmixtew on Thu Dec 27, 2012 12:04 am

Hi All,

I'm getting familiar with writing custom key bindings.
I have the following snippet:

Code: Select all
...
{ "keys": ["ctrl+shift+d"],
  "command": "insert_snippet",
  "args": {
    "contents": "console.log('=== $SELECTION $TM_FILENAME [$TM_LINE_NUMBER] ===', $SELECTION);${0}"
  },
  "context": [{
    "key": "selector",
    "operator": "equal",
    "operand": "source.js",
    "match_all": true
  }]
}
...


It is a simple snippet insertion with a purpose to aid JS debugging. It allows me to select text (variable name, e.g. testvar) and using ctrl+shift+d converts it into:

Code: Select all
console.log('=== testvar some_file.js [477] ===', testvar); // where 477 is a line number and some_file.js is the current js file I'm working in.


That works all fine and dandy.
What I'm trying to accomplish is to be able to pass clipboard content as an argument to insert_snippet. Is it possible to pass commands as arguments (paste in this case), or is there a different approach to accomplish this task? It would be nice to be able to use clipboard content instead of selection (or both if needed) somehow.

Any help is appreciated!
cosmixtew
 
Posts: 1
Joined: Wed Dec 26, 2012 11:48 pm

Re: HOWTO: Keybind, insert_snippet + clipboard question.

Postby agibsonsw on Thu Dec 27, 2012 9:01 pm

TextMate uses `pbpaste` for the clipboard content but AFAIK this is not available to ST (it is an interpolated shell command). I don't believe that you can achieve this with a simple key-binding, soz.. I won't be upset, though, if someone proves me wrong ;).

It is possible via a plug-in, using get_clipboard(), but this won't behave the same as a snippet.
"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: HOWTO: Keybind, insert_snippet + clipboard question.

Postby agibsonsw on Thu Dec 27, 2012 9:16 pm

My Dynamic Snippet: patent pending! 8-)

Code: Select all
import sublime, sublime_plugin

MYSNIPPET = \
"""
This guy ${1:%(name1)s} pasted this ${2:%(name2)s}.$0
"""
class DynamicSnip(sublime_plugin.TextCommand):
    def run(self, edit):
        someName = self.view.substr(self.view.sel()[0])
        pasted = sublime.get_clipboard()
        self.view.run_command("insert_snippet", { "contents" : (MYSNIPPET % \
            { "name1": someName, "name2": pasted }) })

Simpler, of course, is to use a tab-placeholder and for the user to Paste (Ctrl-V) the clipboard content..?
"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: HOWTO: Keybind, insert_snippet + clipboard question.

Postby agibsonsw on Thu Dec 27, 2012 9:28 pm

Restrict to JS file:

Code: Select all
import sublime, sublime_plugin

MYSNIPPET = \
"""
This guy ${1:%(name1)s} pasted this ${2:%(name2)s}.$0
"""
class DynamicSnip(sublime_plugin.TextCommand):
    def run(self, edit):
        if not self.view.match_selector(0, 'source.js -string -comment -constant'):
            return
        someName = self.view.substr(self.view.sel()[0])
        pasted = sublime.get_clipboard()
        self.view.run_command("insert_snippet", { "contents" : (MYSNIPPET % \
            { "name1": someName, "name2": pasted }) })
"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: HOWTO: Keybind, insert_snippet + clipboard question.

Postby agibsonsw on Thu Dec 27, 2012 9:50 pm

My dynamic snippet has great potential, which I don't think has been spotted. It could be a package where we can set and read package-settings, or variables created for a particular view, and dynamically insert these within snippets. Changing a setting would mean that the next time we insert a snippet it would reflect the current setting.

An example would be for docstrings, where the snippet could read the previous-lines' argument list, and insert other settings, such as the project-name, user or a datestamp, etc.. I don't know if there is a package that already does something similar for docstrings(?).
"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 General Discussion

Who is online

Users browsing this forum: Google [Bot], jonathanh and 12 guests