-
Notifications
You must be signed in to change notification settings - Fork 79
/
org-pomodoro.el
656 lines (542 loc) · 23.1 KB
/
org-pomodoro.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
;;; org-pomodoro.el --- Pomodoro implementation for org-mode.
;; Author: Arthur Leonard Andersen <leoc.git@gmail.com>, Marcin Koziej <marcin at lolownia dot org>
;; URL: https://github.com/lolownia/org-pomodoro
;; Created: May 10, 2013
;; Version: 2.1.0
;; Package-Requires: ((alert "0.5.10") (cl-lib "0.5"))
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Org-pomodoro introduces an easy way to clock time in org-mode with
;; the pomodoro technique. You can clock into tasks with starting a
;; pomodoro time automatically. Each finished pomodoro is followed by
;; a break timer. If you completed 4 pomodoros in a row the break is
;; longer that the shorter break between each pomodoro.
;;
;; For a full explanation of the pomodoro technique, have a look at:
;; http://www.pomodorotechnique.com
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'timer)
(require 'org)
(require 'org-agenda)
(require 'org-clock)
(require 'org-timer)
(require 'alert)
;;; Custom Variables
(defgroup org-pomodoro nil
"Org pomodoro customization"
:tag "Org Pomodoro"
:group 'org-progress)
(defcustom org-pomodoro-long-break-frequency 4
"The maximum number of pomodoros until a long break is started."
:group 'org-pomodoro
:type 'integer)
(defcustom org-pomodoro-ask-upon-killing t
"Determines whether to ask upon killing a pomodoro or not."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-play-sounds t
"Determines whether sounds are played or not."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-manual-break nil
"Whether the user needs to exit manually from a running pomodoro to enter a break.
If non-nil, after the time is up for a pomodoro, an \"overtime\"
state is entered until ‘org-pomodoro’ is invoked, which then
finishes the pomodoro and enters the break period."
:group 'org-pomodoro
:type 'boolean)
;; Pomodoro Values
(defcustom org-pomodoro-length 25
"The length of a pomodoro in minutes."
:group 'org-pomodoro
:type 'integer)
(defcustom org-pomodoro-time-format "%.2m:%.2s"
"Defines the format of the time representation in the modeline."
:group 'org-pomodoro
:type 'string)
(defcustom org-pomodoro-format "Pomodoro~%s"
"The format of the mode line string during a pomodoro session."
:group 'org-pomodoro
:type 'string)
(defcustom org-pomodoro-audio-player (or (executable-find "aplay")
(executable-find "afplay"))
"Music player used to play sounds."
:group 'org-pomodoro
:type 'string)
;;; POMODORO START SOUND
(defcustom org-pomodoro-start-sound-p nil
"Determines whether to play a sound when a pomodoro started.
Use `org-pomodoro-start-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-start-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/bell.wav"))
"The path to a sound file that´s to be played when a pomodoro is started."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-start-sound-args nil
"Arguments used when playing the `org-pomodoro-start-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO FINISHED SOUND
(defcustom org-pomodoro-finished-sound-p t
"Determines whether to play a sound when a pomodoro finished.
Use `org-pomodoro-finished-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-finished-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/bell.wav"))
"The path to a sound file that´s to be played when a pomodoro was finished."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-finished-sound-args nil
"Arguments used when playing the `org-pomodoro-finished-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO OVERTIME SOUND
(defcustom org-pomodoro-overtime-sound-p t
"Determines whether to play a sound when a pomodoro starts to run overtime.
Use `org-pomodoro-overtime-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-overtime-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/bell.wav"))
"The path to a sound file that´s to be played when a pomodoro runs overtime."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-overtime-sound-args nil
"Arguments used when playing the `org-pomodoro-overtime-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO KILLED SOUND
(defcustom org-pomodoro-killed-sound-p nil
"Determines whether to play a sound when a pomodoro killed.
Use `org-pomodoro-killed-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-killed-sound nil
"The path to a sound file, that´s to be played when a pomodoro is killed."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-killed-sound-args nil
"Arguments used when playing the `org-pomodoro-killed-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO SHORT-BREAK SOUND
(defcustom org-pomodoro-short-break-sound-p t
"Determines whether to play a sound when a short-break finished.
Use `org-pomodoro-short-break-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-clock-break nil
"When t, also clocks time during breaks."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-short-break-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/bell.wav"))
"The path to a sound file that´s to be played when a break was finished."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-short-break-sound-args nil
"Arguments used when playing the `org-pomodoro-short-break-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO LONG-BREAK SOUND
(defcustom org-pomodoro-long-break-sound-p t
"Determines whether to play a sound when a long-break finished.
Use `org-pomodoro-long-break-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-long-break-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/bell_multiple.wav"))
"The path to a sound file that´s to be played when a long break is finished."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-long-break-sound-args nil
"Arguments used when playing the `org-pomodoro-long-break-sound'."
:group 'org-pomodoro
:type 'string)
;;; POMODORO TICKING SOUND
(defcustom org-pomodoro-ticking-sound-p nil
"Determines whether ticking sounds are played or not."
:group 'org-pomodoro
:type 'boolean)
(defcustom org-pomodoro-ticking-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/tick.wav"))
"The path to a sound file that´s to be played while a pomodoro is running."
:group 'org-pomodoro
:type 'file)
(defcustom org-pomodoro-ticking-sound-args nil
"Arguments used when playing the `org-pomodoro-ticking-sound'."
:group 'org-pomodoro
:type 'string)
(defcustom org-pomodoro-ticking-sound-states '(:pomodoro :short-break :long-break)
"The states in which to play ticking sounds."
:group 'org-pomodoro
:type 'list)
(defcustom org-pomodoro-ticking-frequency 1
"The frequency at which to playback the ticking sound."
:group 'org-pomodoro
:type 'list)
;;; OVERTIME VALUES
(defcustom org-pomodoro-overtime-format "+%s"
"The format of the mode line during a pomodoro running overtime."
:group 'org-pomodoro
:type 'string)
;;; BREAK VALUES
(defcustom org-pomodoro-short-break-length 5
"The length of a short break in minutes."
:group 'org-pomodoro
:type 'integer)
(defcustom org-pomodoro-short-break-format "Short Break~%s"
"The format of the mode line string during a short break."
:group 'org-pomodoro
:type 'string)
(defcustom org-pomodoro-long-break-length 20
"The length of a long break in minutes."
:group 'org-pomodoro
:type 'integer)
(defcustom org-pomodoro-long-break-format "Long Break~%s"
"The format of the mode line string during a long break."
:group 'org-pomodoro
:type 'string)
(defcustom org-pomodoro-expiry-time 120
"The time in minutes for which a pomodoro group is valid.
The size of a pomodoro group is defined by `org-pomodoro-long-break-frequency'.
If you do not clock in for this period of time you will be prompted
whether to reset the pomodoro count next time you call `org-pomodoro'."
:group 'org-pomodoro
:type 'integer)
(defcustom org-pomodoro-keep-killed-pomodoro-time nil
"Keeps the clocked time of killed pomodoros."
:group 'org-pomodoro
:type 'boolean)
;; Hooks
(defvar org-pomodoro-started-hook nil
"Hooks run when a pomodoro is started.")
(defvar org-pomodoro-finished-hook nil
"Hooks run when a pomodoro is finished.")
(defvar org-pomodoro-overtime-hook nil
"Hooks run when a pomodoro enters overtime.")
(defvar org-pomodoro-killed-hook nil
"Hooks run when a pomodoro is killed.")
(defvar org-pomodoro-break-finished-hook nil
"Hook run after any break has finished.
Run before a break's specific hook.")
(defvar org-pomodoro-long-break-finished-hook nil
"Hooks run when a long break is finished.")
(defvar org-pomodoro-short-break-finished-hook nil
"Hooks run when short break is finished.")
(defvar org-pomodoro-tick-hook nil
"Hooks run every second during a pomodoro.")
;; Faces
(defface org-pomodoro-mode-line
'((t (:foreground "tomato1")))
"Face of a pomodoro in the modeline."
:group 'faces)
(defface org-pomodoro-mode-line-overtime
'((t (:foreground "tomato3" :weight bold)))
"Face of a pomodoro running overtime in the modeline."
:group 'faces)
(defface org-pomodoro-mode-line-break
'((t (:foreground "#2aa198")))
"Face of a pomodoro break in the modeline ."
:group 'faces)
;; Temporary Variables
(defvar org-pomodoro-mode-line "")
(put 'org-pomodoro-mode-line 'risky-local-variable t)
(defvar org-pomodoro-timer nil
"The timer while a pomodoro or a break.")
(defvar org-pomodoro-end-time nil
"The end time of the current pomodoro phase.")
(defvar org-pomodoro-state :none
"The current state of `org-pomodoro`.
It changes to :pomodoro when starting a pomodoro and to :longbreak
or :break when starting a break.")
(defvar org-pomodoro-count 0
"The number of pomodoros since the last long break.")
(defvar org-pomodoro-last-clock-in nil
"The last time the pomodoro was set.")
;;; Internal
;; Helper Functions
(defun org-pomodoro-active-p ()
"Retrieve whether org-pomodoro is active or not."
(not (eq org-pomodoro-state :none)))
(defun org-pomodoro-expires-p ()
"Return true when the last clock-in was more than `org-pomodoro-expiry-time`."
(let ((delta-minutes (/ (float-time (time-subtract (current-time) org-pomodoro-last-clock-in)) 60)))
(> delta-minutes org-pomodoro-expiry-time)))
(defun org-pomodoro-sound-p (type)
"Return whether to play sound of given TYPE."
(cl-case type
(:start org-pomodoro-start-sound-p)
(:pomodoro org-pomodoro-finished-sound-p)
(:overtime org-pomodoro-overtime-sound-p)
(:killed org-pomodoro-killed-sound-p)
(:short-break org-pomodoro-short-break-sound-p)
(:long-break org-pomodoro-long-break-sound-p)
(:tick org-pomodoro-ticking-sound-p)
(t (error "Unknown org-pomodoro sound: %S" type))))
(defun org-pomodoro-sound (type)
"Return the sound file for given TYPE."
(cl-case type
(:start org-pomodoro-start-sound)
(:pomodoro org-pomodoro-finished-sound)
(:overtime org-pomodoro-overtime-sound)
(:killed org-pomodoro-killed-sound)
(:short-break org-pomodoro-short-break-sound)
(:long-break org-pomodoro-long-break-sound)
(:tick org-pomodoro-ticking-sound)
(t (error "Unknown org-pomodoro sound: %S" type))))
(defun org-pomodoro-sound-args (type)
"Return the playback arguments for given TYPE."
(cl-case type
(:start org-pomodoro-start-sound-args)
(:pomodoro org-pomodoro-finished-sound-args)
(:overtime org-pomodoro-overtime-sound-args)
(:killed org-pomodoro-killed-sound-args)
(:short-break org-pomodoro-short-break-sound-args)
(:long-break org-pomodoro-long-break-sound-args)
(:tick org-pomodoro-ticking-sound-args)
(t (error "Unknown org-pomodoro sound: %S" type))))
(defun org-pomodoro-play-sound (type)
"Play an audio file specified by TYPE (:pomodoro, :short-break, :long-break)."
(let ((sound (org-pomodoro-sound type))
(args (org-pomodoro-sound-args type)))
(cond ((and (fboundp 'sound-wav-play)
org-pomodoro-play-sounds
sound)
(sound-wav-play sound))
((and org-pomodoro-audio-player
org-pomodoro-play-sounds
sound)
(start-process-shell-command
"org-pomodoro-audio-player" nil
(mapconcat 'identity
`(,org-pomodoro-audio-player
,@(delq nil (list args (shell-quote-argument (expand-file-name sound)))))
" "))))))
(defun org-pomodoro-maybe-play-sound (type)
"Play an audio file specified by TYPE."
(when (org-pomodoro-sound-p type)
(org-pomodoro-play-sound type)))
(defun org-pomodoro-remaining-seconds ()
"Return the number of seconds remaining in the current phase as a float.
Negative if the current phase is over."
(float-time (time-subtract org-pomodoro-end-time (current-time))))
(defun org-pomodoro-format-seconds ()
"Format the time remaining in the current phase with the format specified in
org-pomodoro-time-format."
(format-seconds org-pomodoro-time-format
(if (eq org-pomodoro-state :overtime)
(- (org-pomodoro-remaining-seconds))
(org-pomodoro-remaining-seconds))))
(defun org-pomodoro-update-mode-line ()
"Set the modeline accordingly to the current state."
(let ((s (cl-case org-pomodoro-state
(:pomodoro
(propertize org-pomodoro-format 'face 'org-pomodoro-mode-line))
(:overtime
(propertize org-pomodoro-overtime-format
'face 'org-pomodoro-mode-line-overtime))
(:short-break
(propertize org-pomodoro-short-break-format
'face 'org-pomodoro-mode-line-break))
(:long-break
(propertize org-pomodoro-long-break-format
'face 'org-pomodoro-mode-line-break)))))
(setq org-pomodoro-mode-line
(when (and (org-pomodoro-active-p) (> (length s) 0))
(list "[" (format s (org-pomodoro-format-seconds)) "] "))))
(force-mode-line-update t))
(defun org-pomodoro-kill ()
"Kill the current timer, reset the phase and update the modeline."
(org-pomodoro-killed))
(defun org-pomodoro-tick ()
"A callback that is invoked by the running timer each second.
It checks whether we reached the duration of the current phase, when 't it
invokes the handlers for finishing."
(when (and (not (org-pomodoro-active-p)) org-pomodoro-timer)
(org-pomodoro-reset))
(when (org-pomodoro-active-p)
;; The first element of a time value is the high-order part of the seconds
;; value. This is less than 0 if org-pomodoro-end-time is in the past of
;; the current-time.
(when (< (org-pomodoro-remaining-seconds) 0)
(cl-case org-pomodoro-state
(:pomodoro (if org-pomodoro-manual-break
(org-pomodoro-overtime)
(org-pomodoro-finished)))
(:short-break (org-pomodoro-short-break-finished))
(:long-break (org-pomodoro-long-break-finished))))
(run-hooks 'org-pomodoro-tick-hook)
(org-pomodoro-update-mode-line)
(when (and (member org-pomodoro-state org-pomodoro-ticking-sound-states)
(equal (mod (truncate (org-pomodoro-remaining-seconds))
org-pomodoro-ticking-frequency)
0))
(org-pomodoro-maybe-play-sound :tick))))
(defun org-pomodoro-set (state)
"Set the org-pomodoro STATE."
(setq org-pomodoro-state state
org-pomodoro-end-time
(cl-case state
(:pomodoro (time-add (current-time) (* 60 org-pomodoro-length)))
(:overtime (current-time))
(:short-break (time-add (current-time) (* 60 org-pomodoro-short-break-length)))
(:long-break (time-add (current-time) (* 60 org-pomodoro-long-break-length))))
org-pomodoro-timer (run-with-timer t 1 'org-pomodoro-tick)))
(defun org-pomodoro-start (&optional state)
"Start the `org-pomodoro` timer.
The argument STATE is optional. The default state is `:pomodoro`."
(when org-pomodoro-timer (cancel-timer org-pomodoro-timer))
;; add the org-pomodoro-mode-line to the global-mode-string
(unless global-mode-string (setq global-mode-string '("")))
(unless (memq 'org-pomodoro-mode-line global-mode-string)
(setq global-mode-string (append global-mode-string
'(org-pomodoro-mode-line))))
(org-pomodoro-set (or state :pomodoro))
(when (eq org-pomodoro-state :pomodoro)
(org-pomodoro-maybe-play-sound :start)
(run-hooks 'org-pomodoro-started-hook))
(org-pomodoro-update-mode-line)
(org-agenda-maybe-redo))
(defun org-pomodoro-reset ()
"Reset the org-pomodoro state."
(when org-pomodoro-timer
(cancel-timer org-pomodoro-timer))
(setq org-pomodoro-state :none
org-pomodoro-end-time nil)
(org-pomodoro-update-mode-line)
(org-agenda-maybe-redo))
(defun org-pomodoro-notify (title message)
"Send a notification with TITLE and MESSAGE using `alert'."
(alert message :title title :category 'org-pomodoro))
;; Handlers for pomodoro events.
(defun org-pomodoro-overtime ()
"Is invoked when the time for a pomodoro runs out.
Notify the user that the pomodoro should be finished by calling ‘org-pomodoro’"
(org-pomodoro-maybe-play-sound :overtime)
(org-pomodoro-notify "Pomodoro completed. Now on overtime!" "Start break by calling ‘org-pomodoro’")
(org-pomodoro-start :overtime)
(org-pomodoro-update-mode-line)
(run-hooks 'org-pomodoro-overtime-hook))
(defun org-pomodoro-finished ()
"Is invoked when a pomodoro was finished successfully.
This may send a notification, play a sound and start a pomodoro break."
(unless org-pomodoro-clock-break
(org-clock-out nil t))
(org-pomodoro-maybe-play-sound :pomodoro)
(setq org-pomodoro-count (+ org-pomodoro-count 1))
(if (zerop (mod org-pomodoro-count org-pomodoro-long-break-frequency))
(org-pomodoro-start :long-break)
(org-pomodoro-start :short-break))
(org-pomodoro-notify "Pomodoro completed!" "Time for a break.")
(org-pomodoro-update-mode-line)
(org-agenda-maybe-redo)
(run-hooks 'org-pomodoro-finished-hook))
(defun org-pomodoro-killed ()
"Is invoked when a pomodoro was killed.
This may send a notification, play a sound and adds log."
(org-pomodoro-reset)
(org-pomodoro-notify "Pomodoro killed." "One does not simply kill a pomodoro!")
(org-pomodoro-maybe-play-sound :killed)
(when (org-clocking-p)
(if org-pomodoro-keep-killed-pomodoro-time
(org-clock-out nil t)
(org-clock-cancel)))
(run-hooks 'org-pomodoro-killed-hook))
(defun org-pomodoro-short-break-finished ()
"Is invoked when a short break is finished.
This may send a notification and play a sound."
(when org-pomodoro-clock-break
(org-clock-out nil t))
(org-pomodoro-reset)
(org-pomodoro-notify "Short break finished." "Ready for another pomodoro?")
(org-pomodoro-maybe-play-sound :short-break)
(run-hooks 'org-pomodoro-break-finished-hook 'org-pomodoro-short-break-finished-hook))
(defun org-pomodoro-long-break-finished ()
"Is invoked when a long break is finished.
This may send a notification and play a sound."
(when org-pomodoro-clock-break
(org-clock-out nil t))
(org-pomodoro-reset)
(org-pomodoro-notify "Long break finished." "Ready for another pomodoro?")
(org-pomodoro-maybe-play-sound :long-break)
(run-hooks 'org-pomodoro-break-finished-hook 'org-pomodoro-long-break-finished-hook))
(defun org-pomodoro-extend-last-clock ()
"Extends last clock to `current-time'."
(interactive)
(save-window-excursion
(org-clock-goto)
(when (re-search-forward ":LOGBOOK:" (save-excursion (outline-next-heading)) t)
(org-flag-drawer nil))
(let ((last-clock (car org-clock-history)))
(switch-to-buffer (marker-buffer last-clock))
(goto-char last-clock)
(let ((item-end (save-excursion (org-end-of-subtree t))))
(when (re-search-forward "CLOCK: \\(\\[.*?\\]\\)" item-end t)
(kill-line)
(org-clock-clock-out
(cons (copy-marker (match-end 1) t)
(org-time-string-to-time (match-string 1)))))))))
;;;###autoload
(defun org-pomodoro (&optional arg)
"Start a new pomodoro or stop the current one.
When no timer is running for `org-pomodoro` a new pomodoro is started and
the current task is clocked in. Otherwise EMACS will ask whether we´d like to
kill the current timer, this may be a break or a running pomodoro."
(interactive "P")
(when (and org-pomodoro-last-clock-in
org-pomodoro-expiry-time
(org-pomodoro-expires-p)
(y-or-n-p "Reset pomodoro count? "))
(setq org-pomodoro-count 0))
(setq org-pomodoro-last-clock-in (current-time))
(cond
;; possibly break from overtime
((and (org-pomodoro-active-p) (eq org-pomodoro-state :overtime))
(org-pomodoro-finished))
;; Maybe kill running pomodoro
((org-pomodoro-active-p)
(if (or (not org-pomodoro-ask-upon-killing)
(y-or-n-p "There is already a running timer. Would you like to stop it? "))
(org-pomodoro-kill)
(message "Alright, keep up the good work!")))
;; or start and clock in pomodoro
(t
(cond
((equal arg '(4))
(let ((current-prefix-arg '(4)))
(call-interactively 'org-clock-in)))
((equal arg '(16))
(call-interactively 'org-clock-in-last))
((memq major-mode (list 'org-mode 'org-journal-mode))
(call-interactively 'org-clock-in))
((eq major-mode 'org-agenda-mode)
(org-with-point-at (org-get-at-bol 'org-hd-marker)
(call-interactively 'org-clock-in)))
(t (let ((current-prefix-arg '(4)))
(call-interactively 'org-clock-in))))
(org-pomodoro-start :pomodoro))))
(provide 'org-pomodoro)
;;; org-pomodoro.el ends here