Less latency in org-timer-item.

* org-timer.el (org-timer-item): Refactoring. Compute timer string
  before inserting it in the buffer
* org-timer.el (org-timer): added an optional argument to return timer
  string instead of inserting it.
This commit is contained in:
Nicolas Goaziou 2010-07-16 11:20:05 +02:00
parent 8eece59f9e
commit 53c4b53e8f
1 changed files with 15 additions and 12 deletions

View File

@ -145,18 +145,23 @@ With prefix arg STOP, stop it entirely."
(org-timer-set-mode-line 'off)) (org-timer-set-mode-line 'off))
;;;###autoload ;;;###autoload
(defun org-timer (&optional restart) (defun org-timer (&optional restart no-insert-p)
"Insert a H:MM:SS string from the timer into the buffer. "Insert a H:MM:SS string from the timer into the buffer.
The first time this command is used, the timer is started. When used with The first time this command is used, the timer is started. When used with
a \\[universal-argument] prefix, force restarting the timer. a \\[universal-argument] prefix, force restarting the timer.
When used with a double prefix argument \ When used with a double prefix argument \
\\[universal-argument] \\universal-argument], change all the timer string \\[universal-argument] \\universal-argument], change all the timer string
in the region by a fixed amount. This can be used to recalibrate a timer in the region by a fixed amount. This can be used to recalibrate a timer
that was not started at the correct moment." that was not started at the correct moment.
If NO-INSERT-P is non-nil, return the string instead of inserting
it in the buffer."
(interactive "P") (interactive "P")
(if (equal restart '(4)) (org-timer-start)) (when (or (equal restart '(4)) (not org-timer-start-time))
(or org-timer-start-time (org-timer-start)) (org-timer-start))
(insert (org-timer-value-string))) (if no-insert-p
(org-timer-value-string)
(insert (org-timer-value-string))))
(defun org-timer-value-string () (defun org-timer-value-string ()
(format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds))))) (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
@ -196,23 +201,21 @@ that was not started at the correct moment."
"Insert a description-type item with the current timer value." "Insert a description-type item with the current timer value."
(interactive "P") (interactive "P")
(cond (cond
;; If we are in a timer list, insert item like `org-insert-item'. ;; In a timer list, insert with `org-insert-item-internal'.
((and (org-in-item-p) ((and (org-in-item-p)
(save-excursion (save-excursion
(org-beginning-of-item) (org-beginning-of-item)
(looking-at "[ \t]*[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+ ::"))) (looking-at "[ \t]*[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+ ::")))
(org-insert-item-internal (point)) (org-insert-item-internal (point) nil (concat (org-timer (when arg '(4)) t) ":: ")))
(org-timer (if arg '(4))) ;; In a list of another type, don't break anything: throw an error.
(insert ":: "))
;; We are still are in a list, of a wrong type: throw an error.
((org-in-item-p) ((org-in-item-p)
(error "This is not a timer list")) (error "This is not a timer list"))
;; Else, go to beginning of line, and insert the timer ;; Else, insert the timer correctly indented at bol.
(t (t
(beginning-of-line) (beginning-of-line)
(org-indent-line-function) (org-indent-line-function)
(insert "- ") (insert "- ")
(org-timer (if arg '(4))) (org-timer (when arg '(4)))
(insert ":: ")))) (insert ":: "))))
(defun org-timer-fix-incomplete (hms) (defun org-timer-fix-incomplete (hms)