Last active
August 23, 2024 12:13
-
-
Save hluk/3041da1814b91f6daa9a951b4e1d49c6 to your computer and use it in GitHub Desktop.
CopyQ - Snippets Command (with custom dialog position)
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 x = 1850 | |
var y = 1000 | |
var snippetsTabName = 'Snippets' | |
// List snippets instead of search combo box? | |
var listSnippets = false | |
function newVarRe(content) { | |
return new RegExp('\\\\${' + content + '}', 'g') | |
} | |
function getText(item, format) { | |
return str(item[format] || '') | |
} | |
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) | |
if (prefix) | |
prefix += ': ' | |
for (var j = 0; j < size(); ++j) { | |
var snippet = getitem(j) | |
var snippetName = getText(snippet, mimeItemNotes) | |
|| getText(snippet, mimeText) | |
|| getText(snippet, mimeHtml) | |
snippetNames.push(prefix + snippetName) | |
snippets.push(snippet) | |
} | |
} | |
} | |
function askForSnippet(snippetNames, snippets) { | |
var list = listSnippets ? '.list:' : '' | |
var snippet = dialog( | |
'.title', 'Select Snippet', | |
'.x', x, | |
'.y', y, | |
list + 'Snippet', snippetNames | |
) || abort() | |
if (listSnippets) | |
return snippets[snippet] | |
var i = snippetNames.indexOf(snippet) | |
if (i != -1) | |
return snippets[i] | |
i = fuzzyIndexOf(snippetNames, snippet) | |
if (i != -1) | |
return snippets[i] | |
popup( | |
'Snippet Not Found', | |
'No matching snippet found for \"' + snippetName + '\"!' | |
) | |
abort() | |
} | |
function getPlaceholders(snippet) { | |
var placeholders = {} | |
var m | |
var reVar = newVarRe('([^:}]*):?(.*?)') | |
while ((m = reVar.exec(snippet)) !== null) { | |
if (!(m[1] in placeholders)) | |
placeholders[m[1]] = m[2].replace('\\\\n', '\\n') | |
} | |
return placeholders | |
} | |
function assignPlaceholders(text, values) { | |
if (!(values instanceof Object)) { | |
text = assignPlaceholder(text, '.*?', values) | |
} else { | |
for (var name in values) | |
text = assignPlaceholder(text, name, values[name]) | |
} | |
return text | |
} | |
function askToAssignPlaceholders(snippet, format, values) { | |
var text = getText(snippet, format) | |
var placeholders = getPlaceholders(text) | |
if (Object.keys(placeholders).length < 1) | |
return | |
if (values) { | |
snippet[format] = assignPlaceholders(text, values) | |
return values | |
} | |
var label = escapeHtml(text) | |
.replace(newVarRe('([^:}]*).*?'), '<b>$1</b>') | |
var dialogVars = [ | |
'.title', 'Set Snippet Values', | |
'.x', x, | |
'.y', y, | |
'.label', label | |
] | |
for (var name in placeholders) { | |
var values = placeholders[name].split(',') | |
dialogVars.push(name) | |
dialogVars.push((values.length == 1) ? values[0] : values) | |
} | |
var values = dialog.apply(this, dialogVars) || abort() | |
snippet[format] = assignPlaceholders(text, values) | |
return values | |
} | |
function pasteSnippet(mime, content) { | |
copy(mime, content) | |
copySelection(mime, content) | |
paste() | |
} | |
var snippetNames = [] | |
var snippets = [] | |
loadSnippets(snippetNames, snippets) | |
var snippet = askForSnippet(snippetNames, snippets) | |
values = askToAssignPlaceholders(snippet, mimeText) | |
askToAssignPlaceholders(snippet, mimeHtml, values) | |
pasteSnippet(mimeItems, pack(snippet))" | |
GlobalShortcut=meta+alt+q | |
Icon=\xf1fb | |
IsGlobalShortcut=true | |
Name=Snippets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment