Skip to content

Instantly share code, notes, and snippets.

@hluk
Last active August 16, 2023 10:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hluk/af37cd2ca650c92ed0fa to your computer and use it in GitHub Desktop.
Save hluk/af37cd2ca650c92ed0fa 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=Meta+Alt+Q
Icon=\xf1fb
Name=Snippets
@pihentagy
Copy link

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
Copy link
Author

hluk commented Mar 24, 2017

@iamdmason
Copy link

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

(currently on MacOS)

@dbareis
Copy link

dbareis commented Jul 2, 2022

I just got a private message from someone having issues so this is my response:

I can't remember what I did but:

  • there must be a tab (Snippets),
  • the tab needs to contain at least one item
  • all the items should have notes attached (Shift+F2).

I have attached my slightly modified version with some error handling I added when getting this to work:

///////////////////////////////////////////////////////////////////////////////////////
// https://gist.github.com/hluk/af37cd2ca650c92ed0fa
//         The following code came from here, Dennis Bareis
//         added enough error handling code to work out the
//         problems he had getting it going...
// https://www.youtube.com/watch?v=xVUzM7TjV_g&t=158s
///////////////////////////////////////////////////////////////////////////////////////

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 (snippetName == "undefined")
         snippetName = "[NOTE MISSING ON 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)


//--- DB$: Exit if no Snippets exist ----------------------------------------
if  (snippets.length == 0)
{
    var Msg = 'You haven\'t defined any snippets in the "Snippets" tab yet!'
    serverLog(Msg)
    popup(Msg, "", 5000)
    abort()
}



var snippet = askForSnippet(snippetNames, snippets)

var textSnippet = getText(snippet)
if (textSnippet)
  pasteSnippet('text/plain', askToAssignPlaceholders(textSnippet))
else
  pasteSnippet('application/x-copyq-item', pack(snippet))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment