org-timer.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.13a
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains the relative timer code for Org-mode
  24. (defvar org-timer-start-time nil
  25. "t=0 for the running timer.")
  26. (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
  27. "Regular expression used to match timer stamps.")
  28. (defcustom org-timer-format "%s "
  29. "The format to insert the time of the timer.
  30. This format must contain one instance of \"%s\" which will be replaced by
  31. the value of the relative timer."
  32. :group 'org-time
  33. :type 'string)
  34. ;;;###autoload
  35. (defun org-timer-start (&optional offset)
  36. "Set the starting time for the relative timer to now.
  37. When called with prefix argument OFFSET, prompt the user for an offset time,
  38. with the default taken from a timer stamp at point, if any.
  39. If OFFSET is a string or an integer, it is directly taken to be the offset
  40. without user interaction.
  41. When called with a double prefix arg, all timer strings in the active
  42. region will be shifted by a specific amount. You will be prompted for
  43. the amount, with the default to make the first timer string in
  44. the region 0:00:00."
  45. (interactive "P")
  46. (if (equal offset '(16))
  47. (call-interactively 'org-timer-change-times-in-region)
  48. (let (delta des s)
  49. (if (not offset)
  50. (setq org-timer-start-time (current-time))
  51. (cond
  52. ((integerp offset) (setq delta offset))
  53. ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
  54. (t
  55. (setq def (if (org-in-regexp org-timer-re)
  56. (match-string 0)
  57. "0:00:00")
  58. s (read-string
  59. (format "Restart timer with offset [%s]: " def)))
  60. (unless (string-match "\\S-" s) (setq s def))
  61. (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
  62. (setq org-timer-start-time
  63. (seconds-to-time
  64. (-
  65. (time-to-seconds (current-time))
  66. (org-timer-hms-to-secs s)))))
  67. (message "Timer start time set to %s, current value is %s"
  68. (format-time-string "%T" org-timer-start-time)
  69. (org-timer-secs-to-hms (or delta 0))))))
  70. ;;;###autoload
  71. (defun org-timer (&optional restart)
  72. "Insert a H:MM:SS string from the timer into the buffer.
  73. The first time this command is used, the timer is started. When used with
  74. a `C-u' prefix, force restarting the timer.
  75. When used with a double prefix arg `C-u C-u', change all the timer string
  76. in the region by a fixed amount. This can be used to recalibrate a timer
  77. that was not started at the correct moment."
  78. (interactive "P")
  79. (if (equal restart '(4)) (org-timer-start))
  80. (or org-timer-start-time (org-timer-start))
  81. (insert (format
  82. org-timer-format
  83. (org-timer-secs-to-hms
  84. (floor
  85. (- (time-to-seconds (current-time))
  86. (time-to-seconds org-timer-start-time)))))))
  87. ;;;###autoload
  88. (defun org-timer-change-times-in-region (beg end delta)
  89. "Change all h:mm:ss time in region by a DELTA."
  90. (interactive
  91. "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
  92. (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
  93. (unless (string-match "\\S-" delta)
  94. (save-excursion
  95. (goto-char beg)
  96. (when (re-search-forward re end t)
  97. (setq delta (match-string 0))
  98. (if (equal (string-to-char delta) ?-)
  99. (setq delta (substring delta 1))
  100. (setq delta (concat "-" delta))))))
  101. (setq delta (my-hms-to-secs (org-timer-fix-incomplete delta)))
  102. (when (= delta 0) (error "No change"))
  103. (save-excursion
  104. (goto-char end)
  105. (while (re-search-backward re beg t)
  106. (setq p (point))
  107. (replace-match
  108. (save-match-data
  109. (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
  110. t t)
  111. (goto-char p)))))
  112. ;;;###autoload
  113. (defun org-timer-item (&optional arg)
  114. "Insert a description-type item with the curren timer value."
  115. (interactive "P")
  116. (let ((ind 0))
  117. (save-excursion
  118. (skip-chars-backward " \n\t")
  119. (condition-case nil
  120. (progn
  121. (org-beginning-of-item)
  122. (setq ind (org-get-indentation)))
  123. (error nil)))
  124. (or (bolp) (newline))
  125. (org-indent-line-to ind)
  126. (insert "- ")
  127. (org-timer (if arg '(4)))
  128. (insert ":: ")))
  129. (defun org-timer-fix-incomplete (hms)
  130. "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
  131. (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
  132. (replace-match
  133. (format "%d:%02d:%02d"
  134. (if (match-end 1) (string-to-int (match-string 1 hms)) 0)
  135. (if (match-end 2) (string-to-int (match-string 2 hms)) 0)
  136. (string-to-int (match-string 3 hms)))
  137. t t hms)
  138. (error "Canot parse HMS string \"%s\"" hms)))
  139. (defun org-timer-hms-to-secs (hms)
  140. "Convert h:mm:ss string to an integer time.
  141. If the string starts with a minus sign, the integer will be negative."
  142. (if (not (string-match
  143. "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
  144. hms))
  145. 0
  146. (let* ((h (string-to-int (match-string 1 hms)))
  147. (m (string-to-int (match-string 2 hms)))
  148. (s (string-to-int (match-string 3 hms)))
  149. (sign (equal (substring (match-string 1 hms) 0 1) "-")))
  150. (setq h (abs h))
  151. (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
  152. (defun org-timer-secs-to-hms (s)
  153. "Convert integer S into h:mm:ss.
  154. If the integer is negative, the strig will start with \"-\"."
  155. (let (sign m h)
  156. (setq sign (if (< s 0) "-" "")
  157. s (abs s)
  158. m (/ s 60) s (- s (* 60 m))
  159. h (/ m 60) m (- m (* 60 h)))
  160. (format "%s%d:%02d:%02d" sign h m s)))
  161. ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
  162. ;;; org-timer.el ends here