SilverBullet has no repeating tasks, and typing dates by hand is slow. This adds both: tasks that come back after you finish them, and a /schedule command that takes dates in plain words.
The repeating part is adapted from killeik's script on the SilverBullet community forum (in the thread about moving from Obsidian Tasks).
It works best together with Task Status Markers, because a finished task comes back as [TODO].
CONFIG page, or any page you keep code on.space-lua code block (three backticks followed by space-lua) and paste the first piece of code below inside it. It handles repeating tasks.space-lua block and paste the second piece of code inside it. It adds /schedule, and it needs the first piece to work, so keep both.System: Reload.REPEAT_WEEKDAYS = {sun = 0, sunday = 0, mon = 1, monday = 1, tue = 2, tues = 2, tuesday = 2,
wed = 3, weds = 3, wednesday = 3, thu = 4, thur = 4, thurs = 4, thursday = 4,
fri = 5, friday = 5, sat = 6, saturday = 6}
REPEAT_ORDINALS = {first = 1, second = 2, third = 3, fourth = 4, fifth = 5,
["1st"] = 1, ["2nd"] = 2, ["3rd"] = 3, ["4th"] = 4, ["5th"] = 5, last = -1}
function repeatDaysIn(y, m)
local days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if m == 2 and ((y % 4 == 0 and y % 100 ~= 0) or y % 400 == 0) then return 29 end
return days[m]
end
-- 0 = Sunday ... 6 = Saturday
function repeatWeekday(y, m, d)
local t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
if m < 3 then y = y - 1 end
return (y + math.floor(y / 4) - math.floor(y / 100) + math.floor(y / 400) + t[m] + d) % 7
end
-- Day of month of the nth weekday w (n = -1 for the last), or nil
function repeatNthWeekday(y, m, n, w)
if n == -1 then
local last = repeatDaysIn(y, m)
return last - (repeatWeekday(y, m, last) - w) % 7
end
local d = 1 + (w - repeatWeekday(y, m, 1)) % 7 + 7 * (n - 1)
if d > repeatDaysIn(y, m) then return nil end
return d
end
function repeatDayKey(word)
if REPEAT_WEEKDAYS[word] then return word end
local single = string.gsub(word, "s$", "")
if REPEAT_WEEKDAYS[single] then return single end
return nil
end
function repeatNextDate(spec, fromDate)
spec = string.lower(tostring(spec))
local afterDone = string.find(spec, "after done", 1, true) ~= nil
spec = string.gsub(spec, "after done", "")
spec = string.gsub(spec, "^%s*every%s+", "")
spec = string.gsub(spec, "^%s+", "")
spec = string.gsub(spec, "%s+$", "")
local aliases = {daily = "1 day", weekly = "1 week", monthly = "1 month",
quarterly = "3 months", yearly = "1 year", annually = "1 year"}
spec = aliases[spec] or spec
if spec == "weekdays" then spec = "mon tue wed thu fri" end
local y, m, d
local by, bm, bd
if fromDate and not afterDone then
by, bm, bd = string.match(tostring(fromDate), "(%d+)%-(%d+)%-(%d+)")
end
if by then
y, m, d = tonumber(by), tonumber(bm), tonumber(bd)
else
y = tonumber(os.date("%Y"))
m = tonumber(os.date("%m"))
d = tonumber(os.date("%d"))
end
local function fmt(yy, mm, dd)
return string.format("%04d-%02d-%02d", yy, mm, dd)
end
-- Nth weekday of the month: "1st saturday", "second tuesday", "last friday"
local ordWord, dayWord = string.match(spec, "^(%w+)%s+(%a+)")
if ordWord and REPEAT_ORDINALS[ordWord] and dayWord and repeatDayKey(dayWord) then
local n = REPEAT_ORDINALS[ordWord]
local w = REPEAT_WEEKDAYS[repeatDayKey(dayWord)]
local yy, mm = y, m
for i = 1, 14 do
local dd = repeatNthWeekday(yy, mm, n, w)
if dd and (yy ~= y or mm ~= m or dd > d) then return fmt(yy, mm, dd) end
mm = mm + 1
if mm > 12 then
mm = 1
yy = yy + 1
end
end
return nil
end
-- Days of the week: "mon, wed, fri", "tuesday and thursday"
local wanted, anyDay, onlyDays = {}, false, true
for word in string.gmatch(spec, "%a+") do
local key = repeatDayKey(word)
if key then
wanted[REPEAT_WEEKDAYS[key]] = true
anyDay = true
elseif word ~= "and" and word ~= "on" then
onlyDays = false
end
end
if anyDay and onlyDays and not string.find(spec, "%d") then
local yy, mm, dd = y, m, d
for i = 1, 7 do
dd = dd + 1
if dd > repeatDaysIn(yy, mm) then
dd = 1
mm = mm + 1
if mm > 12 then
mm = 1
yy = yy + 1
end
end
if wanted[repeatWeekday(yy, mm, dd)] then return fmt(yy, mm, dd) end
end
return nil
end
local n, unit = string.match(spec, "^(%d+)%s*(%a*)")
if not n then return nil end
n = tonumber(n)
local u = string.sub(unit, 1, 1)
if u == "" or u == "d" then d = d + n
elseif u == "w" then d = d + 7 * n
elseif u == "m" then m = m + n
elseif u == "y" then y = y + n
else return nil end
return os.date("%Y-%m-%d", os.time({year = y, month = m, day = d, hour = 12}))
end
function repeatResetLine(line)
local spec = string.match(line, "%[repeat:%s*([^%]]+)%]")
if not spec then return nil end
local oldDate = string.match(line, "%[scheduled:%s*\"?([%d%-]+)")
or string.match(line, "%[due:%s*\"?([%d%-]+)")
local nextDate = repeatNextDate(spec, oldDate)
if not nextDate then
editor.flashNotification("Couldn't read repeat value: " .. spec, "error")
return nil
end
local newLine = string.gsub(line, "^(%s*[%*%-]%s+)%[^%](<^%>)*%]", "%1[TODO]", 1)
local count
newLine, count = string.gsub(newLine, "%[scheduled:%s*[^%]]*%]", "[scheduled: " .. nextDate .. "]", 1)
if count == 0 then
newLine, count = string.gsub(newLine, "%[due:%s*[^%]]*%]", "[due: " .. nextDate .. "]", 1)
end
if count == 0 then newLine = newLine .. " [scheduled: " .. nextDate .. "]" end
return newLine, nextDate
end
-- When a reset repeating task has a priority, ask whether to clear it.
function repeatAskPriority(oldLine, newLine)
local prio = string.match(newLine, "%[priority:%s*([^%]]-)%s*%]")
if not prio then return newLine end
local name = string.gsub(oldLine, "^%s*[%*%-]%s+%[^%](<^%>)*%]%s*", "")
name = string.gsub(name, "%s*%[[%w_%-]+:[^%]]*%]", "")
name = string.gsub(name, "%s*%[%[^%](<^%>)*%]%]", "")
name = string.gsub(name, "^%s*(.-)%s*$", "%1")
if string.len(name) > 60 then name = string.sub(name, 1, 57) .. "..." end
if editor.confirm("\"" .. name .. "\" is done and will repeat. Clear its priority (" .. prio .. ")?") then
newLine = string.gsub(newLine, "%s*%[priority:[^%]]*%]", "", 1)
end
return newLine
end
-- First date for an open repeating task that has no date yet: the next
-- matching day on or after today for weekday patterns, otherwise today.
function repeatFirstDate(spec)
local clean = string.lower(string.gsub(spec, "after done", ""))
local isPattern = string.find(clean, "weekdays", 1, true) ~= nil
for word in string.gmatch(clean, "%a+") do
if repeatDayKey(word) then isPattern = true end
end
if isPattern then
return repeatNextDate(clean, os.date("%Y-%m-%d", os.time() - 86400))
end
if repeatNextDate(clean, nil) then return os.date("%Y-%m-%d") end
return nil
end
-- Resets every finished repeating task on the open page, and gives a date to
-- open repeating tasks that have none. Runs on task state changes and on page
-- edits, because the state menu doesn't always send a state change.
repeatBusy = false
function repeatResetPage()
if repeatBusy then return end
local text = editor.getText()
if not string.find(text, "[repeat:", 1, true) then return end
-- busy from here, so a second event can't ask the priority question twice
repeatBusy = true
local found = {}
local pos = 0
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
local state = string.match(line, "^%s*[%*%-]%s+%[([^%]]*)%]")
if state and (state == "DONE" or state == "x" or state == "X") then
local newLine, nextDate = repeatResetLine(line)
if newLine then
newLine = repeatAskPriority(line, newLine)
table.insert(found, {from = pos, to = pos + string.len(line), text = newLine, date = nextDate})
end
elseif state and not string.find(string.lower(state), "cancel", 1, true)
and not string.find(line, "%[scheduled:") and not string.find(line, "%[due:") then
local spec = string.match(line, "%[repeat:%s*([^%]]+)%]")
local first = spec and repeatFirstDate(spec)
if first then
table.insert(found, {from = pos, to = pos + string.len(line), text = line .. " [scheduled: " .. first .. "]", date = first})
end
end
pos = pos + string.len(line) + 1
end
if #found == 0 then
repeatBusy = false
return
end
for i = #found, 1, -1 do
editor.replaceRange(found[i].from, found[i].to, found[i].text)
end
repeatBusy = false
editor.flashNotification("Repeats on " .. found[#found].date)
end
-- A task ticked in a query is written to its own page in the background, so
-- repeatResetPage never sees it. Reset finished repeating tasks on every
-- other page that holds a repeating task.
function repeatResetOtherPages()
local current = editor.getCurrentPage()
local pages, seen = {}, {}
for _, t in ipairs(query[from t = index.tasks()](<from t = index.tasks()>)) do
if t["repeat"] and t.page and t.page ~= current and not seen[t.page] then
seen[t.page] = true
table.insert(pages, t.page)
end
end
local lastDate = nil
for _, name in ipairs(pages) do
local text = space.readPage(name)
if text and string.find(text, "[repeat:", 1, true) then
local out, changed = {}, false
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
local state = string.match(line, "^%s*[%*%-]%s+%[([^%]]*)%]")
if state == "DONE" or state == "x" or state == "X" then
local newLine, nextDate = repeatResetLine(line)
if newLine then
newLine = repeatAskPriority(line, newLine)
line = newLine
changed = true
lastDate = nextDate
end
end
table.insert(out, line)
end
if changed then space.writePage(name, table.concat(out, "\n")) end
end
end
if lastDate then
editor.flashNotification("Repeats on " .. lastDate)
-- the index picks up the rewritten page a moment later, so redraw the
-- queries on this page then (twice, in case indexing is slow)
js.window.setTimeout(function() codeWidget.refreshAll() end, 1000)
js.window.setTimeout(function() codeWidget.refreshAll() end, 3000)
end
end
event.listen {
name = "task:stateChange",
run = function(e)
repeatResetPage()
local s = e and e.data and e.data.newState
if s == "DONE" or s == "x" or s == "X" then repeatResetOtherPages() end
end
}
event.listen {
name = "editor:pageModified",
run = function(e) repeatResetPage() end
}
SCHEDULE_MONTHS = {jan = 1, feb = 2, mar = 3, apr = 4, may = 5, jun = 6,
jul = 7, aug = 8, sep = 9, oct = 10, nov = 11, dec = 12}
-- "feb 1", "february 1st", "february 1st, 2027", "1 feb 2027"; nil if not a month date
function scheduleMonthDate(input)
local s = string.gsub(string.lower(input), ",", " ")
local month, day, year
for w in string.gmatch(s, "%S+") do
local letters = string.match(w, "^(%a+)%.?$")
if letters then
if letters ~= "of" and letters ~= "the" then
local m = SCHEDULE_MONTHS[string.sub(letters, 1, 3)]
if month or not m then return nil end
month = m
end
else
local n = string.match(w, "^(%d+)%a*$")
if not n then return nil end
n = tonumber(n)
if not day and n >= 1 and n <= 31 then
day = n
elseif not year and n >= 1000 then
year = n
else
return nil
end
end
end
if not month or not day then return nil end
local today = os.date("%Y-%m-%d")
local y = year or tonumber(os.date("%Y"))
if day > repeatDaysIn(y, month) then return nil end
local date = string.format("%04d-%02d-%02d", y, month, day)
if not year and date < today then
y = y + 1
if day > repeatDaysIn(y, month) then return nil end
date = string.format("%04d-%02d-%02d", y, month, day)
end
return date
end
SCHEDULE_UNITS = {d = true, day = true, days = true, w = true, wk = true, wks = true,
week = true, weeks = true, mo = true, mos = true, month = true, months = true,
y = true, yr = true, yrs = true, year = true, years = true}
-- "in 3 days", "2 weeks", "in a month", "next week", "next month", "end of month",
-- "15th", "the 15th", "2/1", "2/1/2027"; nil if none of these
function scheduleExtraDate(input)
local s = string.lower(input)
local today = os.date("%Y-%m-%d")
local ty = tonumber(os.date("%Y"))
local tm = tonumber(os.date("%m"))
local td = tonumber(os.date("%d"))
if s == "next week" then return repeatNextDate("mon after done", nil) end
if s == "next month" then
if tm == 12 then return string.format("%04d-01-01", ty + 1) end
return string.format("%04d-%02d-01", ty, tm + 1)
end
if s == "end of month" or s == "eom" or s == "end of the month" then
return string.format("%04d-%02d-%02d", ty, tm, repeatDaysIn(ty, tm))
end
-- Gaps: "in 3 days", "2 weeks", "a month"
local g = string.gsub(s, "^in%s+", "")
g = string.gsub(g, "^an?%s+", "1 ")
local n, unit = string.match(g, "^(%d+)%s*(%a+)$")
if n and SCHEDULE_UNITS[unit] then
return repeatNextDate(n .. " " .. unit .. " after done", nil)
end
-- Month/day, optional year
local mm, dd, yy = string.match(s, "^(%d%d?)/(%d%d?)/?(%d*)$")
if mm then
mm, dd = tonumber(mm), tonumber(dd)
local y = tonumber(yy)
if y and y < 100 then y = 2000 + y end
if mm < 1 or mm > 12 then return nil end
local useY = y or ty
if dd < 1 or dd > repeatDaysIn(useY, mm) then return nil end
local date = string.format("%04d-%02d-%02d", useY, mm, dd)
if not y and date < today then
useY = useY + 1
if dd > repeatDaysIn(useY, mm) then return nil end
date = string.format("%04d-%02d-%02d", useY, mm, dd)
end
return date
end
-- Day of the month: "15th", "the 15th" (the next one to come)
local day = string.match(s, "^t?h?e?%s*(%d%d?)[snrt][tdh]$")
if day then
day = tonumber(day)
local y, m = ty, tm
for i = 1, 3 do
if day >= 1 and day <= repeatDaysIn(y, m) and (i > 1 or day >= td) then
return string.format("%04d-%02d-%02d", y, m, day)
end
m = m + 1
if m > 12 then
m = 1
y = y + 1
end
end
end
return nil
end
slashCommand.define {
name = "schedule",
description = "Set a task's scheduled date and repeat",
run = function()
local line = editor.getCurrentLine()
local text = line.text
local current = string.match(text, "%[scheduled:%s*\"?([%d%-]+)") or ""
local input = editor.prompt("Scheduled date (YYYY-MM-DD, feb 1, today, tomorrow, +3 days, friday, 1st saturday; blank keeps it, none removes it):", current)
if input == nil then return end
input = string.lower(string.gsub(input, "^%s*(.-)%s*$", "%1"))
local date = nil
local pattern = nil
if input == "none" then
text = string.gsub(text, "%s*%[scheduled:[^%]]*%]", "")
elseif input ~= "" and input ~= current then
if input == "today" then
date = os.date("%Y-%m-%d")
elseif input == "tomorrow" then
date = repeatNextDate("1 day", nil)
elseif string.match(input, "^%d%d%d%d%-%d%d%-%d%d$") then
date = input
elseif string.match(input, "^%+") then
date = repeatNextDate(string.sub(input, 2) .. " after done", nil)
elseif scheduleMonthDate(input) then
date = scheduleMonthDate(input)
elseif scheduleExtraDate(input) then
date = scheduleExtraDate(input)
else
-- "friday", "next friday", "1st saturday", "last friday"
local words = string.gsub(input, "^next%s+", "")
date = repeatNextDate(words .. " after done", nil)
pattern = words
end
if not date then
editor.flashNotification("Couldn't read that date: " .. input, "error")
return
end
local count
text, count = string.gsub(text, "%[scheduled:[^%]]*%]", "[scheduled: " .. date .. "]", 1)
if count == 0 then text = text .. " [scheduled: " .. date .. "]" end
end
local currentRepeat = string.match(text, "%[repeat:%s*([^%]]-)%s*%]") or ""
currentRepeat = string.gsub(currentRepeat, "%s*after done$", "")
local suggested = pattern or currentRepeat
local typed = editor.prompt("Repeat (e.g. daily, weekly, 2 weeks, monthly, 6 months, yearly, weekdays, mon wed fri, 3rd saturday, last friday; blank keeps it, none removes it):", suggested)
if typed ~= nil then
local spec = string.lower(string.gsub(typed, "^%s*(.-)%s*$", "%1"))
if spec == "" or spec == currentRepeat then spec = "keep" end
if spec == "none" then
text = string.gsub(text, "%s*%[repeat:[^%]]*%]", "")
elseif spec ~= "keep" and not repeatNextDate(spec, nil) then
editor.flashNotification("Couldn't read that repeat: " .. spec, "error")
return
elseif spec ~= "keep" then
local from = editor.filterBox("Count the next date from:", {
{name = "the task's date", value = ""},
{name = "the day it's done", value = " after done"}
})
if from then
local value = spec .. from.value
local count
text, count = string.gsub(text, "%[repeat:[^%]]*%]", "[repeat: " .. value .. "]", 1)
if count == 0 then text = text .. " [repeat: " .. value .. "]" end
end
end
end
editor.replaceRange(line.from, line.to, text)
end
}
/schedule.2027-02-01, feb 1, 1 Feb 2027, 2/1 (month first), 15th, today, tomorrow, +3 days, in 2 weeks, next week (next Monday), next month (the 1st), end of month, friday, 1st saturday, last friday. Leave it blank to keep the date, or type none to remove it.daily, weekly, monthly, quarterly, yearly, 3 days, 2 weeks, 6 months, weekdays, mon wed fri, 2nd tuesday, last friday. Type none to remove a repeat.The command writes [scheduled: ...] and [repeat: ...] on the line. You can also type them by hand.
[x]. The line goes back to [TODO] and its date moves forward.[scheduled: ...], or [due: ...] if there is no scheduled date. With neither, a scheduled date is added.after done to count from the day you finish it, for example [repeat: 6 months after done].[priority: ...], a dialog asks whether to clear it. OK clears it, Cancel keeps it.