Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CopyQ - Dialog for Pasting Snippets
[Command]
Command="
copyq:
var snippetsTabName = 'Snippets'
function newVarRe(content) {
return new RegExp('\\\\${' + content + '}', 'g')
}
function getText(item) {
var textData = item['text/plain']
return textData ? str(textData) : ''
}
function assignPlaceholder(snippet, placeholder, value) {
return snippet.replace(newVarRe(placeholder), value)
}
function fuzzyIndexOf(snippetNames, snippetName) {
var re = new RegExp(snippetName, 'i')
for (var i in snippetNames) {
if (snippetNames[i].match(re))
return i;
}
return -1
}
function loadSnippets(snippetNames, snippets)
{
var tabs = tab()
for (var i in tabs) {
var tabName = tabs[i];
if (tabName != snippetsTabName && tabName.indexOf(snippetsTabName + '/') != 0)
continue;
tab(tabName)
var prefix = tabName.substring(snippetsTabName.length + 1)
for (var j = 0; j < size(); ++j) {
var snippet = getitem(j)
var snippetName = str(snippet['application/x-copyq-item-notes']) || getText(snippet)
if (prefix.length != 0)
snippetName = prefix + ': ' + snippetName
snippetNames.push(snippetName)
snippets.push(snippet)
}
}
}
function askForSnippetName(snippetNames) {
return str(dialog(
'.title', 'Snippets',
'Snippet', [snippetNames[0]].concat(snippetNames)
) || abort())
}
function askForSnippet(snippetNames, snippets) {
var snippetName = askForSnippetName(snippetNames)
var i = snippetNames.indexOf(snippetName)
if (i == -1) {
i = fuzzyIndexOf(snippetNames, snippetName)
if (i == -1) {
popup(
'Snippet Not Found',
'No matching snippet found for \"' + snippetName + '\"!'
)
abort()
}
}
return snippets[i]
}
function getPlaceholders(snippet) {
var placeholders = []
var m
var reVar = newVarRe('(.*?)')
while ((m = reVar.exec(snippet)) !== null) {
if (placeholders.indexOf(m[1]) === -1)
placeholders.push(m[1])
}
return placeholders
}
function askToAssignPlaceholders(snippet) {
var placeholders = getPlaceholders(snippet)
if (placeholders.length > 0) {
var dialogVars = [
'.title', 'Snippet Values for \"' + snippet + '\"']
for (var i in placeholders) {
dialogVars.push(placeholders[i])
dialogVars.push(\"\")
}
var values = dialog.apply(this, dialogVars) || abort()
if (placeholders.length > 1) {
for (var i in placeholders)
snippet = assignPlaceholder(snippet, placeholders[i], values[placeholders[i]])
} else {
snippet = assignPlaceholder(snippet, placeholders[0], values)
}
}
return snippet
}
function pasteSnippet(mime, content) {
copy(mime, content)
copySelection(mime, content)
paste()
}
var snippetNames = []
var snippets = []
loadSnippets(snippetNames, snippets)
var snippet = askForSnippet(snippetNames, snippets)
var textSnippet = getText(snippet)
if (textSnippet)
pasteSnippet('text/plain', askToAssignPlaceholders(textSnippet))
else
pasteSnippet('application/x-copyq-item', pack(snippet))"
GlobalShortcut=Meta+Alt+Q
Icon=\xf1fb
Name=Snippets
@dbareis

This comment has been minimized.

Copy link

commented Feb 18, 2016

Again, looks good, but nothing to explain what it does and how to set it up. I guessed that I needed to create a "Snippets" tab and put something there but now the command just shows "Undefined". I will probably work it out but I agree with all the reviewers that this tool has a very steep learning curve.

CopyQ seems like an amazing product but is definately let down by its usability, I haven't even found a command repository yet but I'm still hopeful.

@pihentagy

This comment has been minimized.

Copy link

commented Nov 28, 2016

I think I've just figured out:

  • install the plugin
  • create tab called 'Snippets'
  • add items to it, the mnemonic is the item note
  • invoke it with Meta+Alt+Q, choose snippet and it will paste it.
@hluk

This comment has been minimized.

Copy link
Owner Author

commented Mar 24, 2017

@iamdmason

This comment has been minimized.

Copy link

commented Mar 24, 2017

Is there code to auto hide the main window initiating the Global Shortcut for this?

(currently on MacOS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.