Register Slash Command

I’m not sure what I’m missing. It seems there should be a registerSlashCommand() function available but when I try to call it like I would the registerCommand or registerFunction functions I get an error.

For example:

silverbullet.registerSlashCommand({ name: `this${day}` }, () => computeDay(dayNumber));

I get the error: silverbullet.registerSlashCommand is not a function

But I can see it right there alongside all the other available ‘register’ functions when I look through the code on github.

Well that’s embarrassing. Guess that registerSlashCommand function was shiny and new and I hadn’t updated yet.

I’m still missing something, I get a completion popup, but when I hit enter no text is inserted.

The registerSlashCommand was built with Space Lua in mind (and documented there), but perhaps you can use it from Space Script too. I’m surprised you even found it :laughing: You may be able to get it to work, but returning a string won’t do anything. What it should do (not tested in the context of space script) is simply run the code in your callback. It won’t actually use the return value at all, so if you want to insert text into your document you should use a syscall like editor.insertAtCursor() or similar.

Here’s what I was trying to do, got it working finally. Just wanted to be able to add slash commands for nextMonday etc without having to repeat myself.

```space-lua
daysMapping = {
  ["Monday"]=0;
  ["Tuesday"]=1;
  ["Wednesday"]=2;
  ["Thursday"]=3;
  ["Friday"]=4;
  ["Saturday"]=5;
  ["Sunday"]=6;
}
one_day = 24*60*60

function computeDay(dayNumber, offset)
  today = os.time()
  currentDayOfWeek = math.fmod(tonumber(os.date("%w", today)) - 1, 7)
  daysUntil = (dayNumber - currentDayOfWeek + offset) * one_day
  return os.date("%Y-%m-%d",today + daysUntil)
end

for day, number in pairs(daysMapping) do
  slashcommand.define {
    name = "last" + day,
    run = function()
      editor.insertAtCursor(computeDay(number, -7))
    end
  }
  slashcommand.define {
    name = "this" + day,
    run = function()
      editor.insertAtCursor(computeDay(number, 0))
    end
  }
  slashcommand.define {
    name = "next" + day,
    run = function()
      editor.insertAtCursor(computeDay(number, 7))
    end
  }
end
2 Likes

Nice!