Last active
November 7, 2024 19:53
-
-
Save hluk/af37cd2ca650c92ed0fa to your computer and use it in GitHub Desktop.
CopyQ - Dialog for Pasting Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
Is it possible to have also have computed fields / that dynamically generate based on what was input for another field?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just got a private message from someone having issues so this is my response:
I can't remember what I did but:
I have attached my slightly modified version with some error handling I added when getting this to work: