Expose XWarpPointer to lua as rootwin:warp_pointer, for region_do_warp_alt
[notion.git] / contrib / scripts / schedule.lua
blobce557619ce4692d7531370d11291a4e836cdea11
1 -- Authors: Sadrul Habib Chowdhury <imadil@gmail.com>
2 -- License: Public domain
3 -- Last Changed: Unknown
4 --
5 -- schedule.lua: schedule notification script
6 --
7 -- You can schedule some messages to show up in the statusbar at
8 -- specified times.
9 --
10 -- Syntax:
11 -- y <year> <month> <date> <hour> <minute> <message>
12 -- or
13 -- m <month> <date> <hour> <minute> <message>
14 -- or
15 -- d <date> <hour> <minute> <message>
16 -- or
17 -- <hour> <minute> <message>
19 -- Current year/month/date will be used if you do not specify one. Messages
20 -- need not be within quotes. Some examples follow:
22 -- Example:
23 -- y 2005 4 1 2 0 "April fool!!" --> 1st April, 2005, 2 am
24 -- m 4 5 2 0 "Some message" --> 5th April of the current year, 2 am
25 -- d 5 2 0 "Other message" --> 5th of the current month and year, 2 am
26 -- 20 0 Last message --> 8 pm of today
28 -- Note:
29 -- The script now saves notification information in a file in the userdir
30 -- (usually ~/.ion3/schedule_save.lua). So the notifications stay after a
31 -- restart.
33 -- Author:
34 -- Sadrul Habib Chowdhury (Adil)
35 -- imadil at gmail dot com
37 if not mod_statusbar then return end
40 -- how often should the script check for scheduled messages?
43 if not schedule then
44 schedule = {
45 interval = 60 * 1000, -- check every minute
46 save_filename = "schedule_save.lua"
48 end
50 local timer = nil -- the timer
51 local list = nil -- doubly linked list of messages
54 -- extract the first token
56 function schedule.get_token(str)
57 local bg, fn
58 bg, fn = string.find(str, "%w+%s+")
59 return string.gsub(string.sub(str, bg, fn), "%s+", ""),
60 string.sub(str, fn+1)
61 end
64 -- show the notification when scheduled
66 function schedule.notify()
67 local l = list
68 local time = os.time()
69 if l and time > l.time then
70 io.stderr:write("schedule: " .. l.message .. "\n")
71 mod_statusbar.inform("schedule", l.message)
72 mod_statusbar.inform("schedule_hint", "important")
73 mod_statusbar.update()
74 list = l.next
75 if list then
76 list.prev = nil
77 end
78 l = nil
79 schedule.save_file()
80 end
81 timer:set(schedule.interval, schedule.notify)
82 end
85 -- very hackish
87 function schedule.insert_into_list(time, message)
88 local l = list
89 local t = nil
91 if os.time() > time then return false end
93 if l == nil then
94 l = {}
95 l.prev = nil
96 l.next = nil
97 l.time = time
98 l.message = message
99 list = l
100 return true
103 while l do
104 if l.time > time then
105 l = l.prev
106 break
109 if l.next then
110 l = l.next
111 else
112 break
116 t = {}
117 t.time = time
118 t.message = message
120 if l == nil then
121 list.prev = t
122 t.next = list
123 t.prev = nil
124 list = t
125 else
126 t.next = l.next
127 t.prev = l
128 l.next = t
130 return true
134 -- add messages in the queue
136 function schedule.add_message(mplex, str)
137 local token = nil
138 local tm = os.date("*t")
140 token, str = schedule.get_token(str)
142 if token == "y" then
143 tm.year, str = schedule.get_token(str)
144 tm.month, str = schedule.get_token(str)
145 tm.day, str = schedule.get_token(str)
146 tm.hour, str = schedule.get_token(str)
147 elseif token == "m" then
148 tm.month, str = schedule.get_token(str)
149 tm.day, str = schedule.get_token(str)
150 tm.hour, str = schedule.get_token(str)
151 elseif token == "d" then
152 tm.day, str = schedule.get_token(str)
153 tm.hour, str = schedule.get_token(str)
154 else
155 tm.hour = token
158 tm.min, str = schedule.get_token(str)
160 if schedule.insert_into_list(os.time(tm), str) then
161 mod_query.message(mplex, "Notification \"" .. str .. "\" scheduled at " ..
162 os.date("[%a %d %h %Y] %H:%M", os.time(tm)))
163 schedule.save_file()
164 else
165 mod_query.message(mplex, "dude, can't schedule notification for past!")
169 function schedule.ask_message(mplex)
170 mod_query.query(mplex, TR("Schedule notification:"), nil, schedule.add_message,
171 nil, "schedule")
175 -- show the scheduled notifications
177 function schedule.show_all(mplex)
178 local l = list
179 local output = "List of scheduled notifications:"
180 while l do
181 output = output .. "\n" .. os.date("[%a %d %h %Y] %H:%M", l.time) ..
182 " => " .. l.message
183 l = l.next
185 mod_query.message(mplex, output)
189 -- save in file
191 function schedule.save_file()
192 if not list then return end
193 local t = ioncore.get_paths() -- saving the file in userdir
194 local f = io.open(t.userdir .."/".. schedule.save_filename, "w")
195 if not f then return end
197 local l = list
198 while l do
199 f:write("schedule.insert_into_list("..l.time..", \""..l.message.."\")\n")
200 l = l.next
202 f:close()
206 -- the key bindings
208 defbindings("WMPlex", {
209 -- you can change the key bindings to your liking
210 kpress(MOD1.."F5", "schedule.ask_message(_)"),
211 kpress(MOD1.."Shift+F5", function () -- clear notification
212 mod_statusbar.inform("schedule", "")
213 mod_statusbar.inform("schedule_hint", "")
214 mod_statusbar.update()
215 end),
216 kpress(MOD1.."Control+F5", "schedule.show_all(_)"),
219 dopath(schedule.save_filename, true) -- read any saved notifications
220 timer = ioncore.create_timer()
221 timer:set(schedule.interval, schedule.notify)
222 mod_statusbar.inform("schedule", "")