Skip to content

Instantly share code, notes, and snippets.

@orschiro
Forked from hluk/CopyQ - Snippets.ini
Created September 25, 2015 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orschiro/9af1e0d833e753775864 to your computer and use it in GitHub Desktop.
Save orschiro/9af1e0d833e753775864 to your computer and use it in GitHub Desktop.
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=Ctrl+Shift+I
Icon=\xf1fb
Name=Snippets
@iamdmason
Copy link

iamdmason commented Mar 17, 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