From 6632ce537e1ec575de3a09c23d8a0ca40dee1139 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sat, 4 Feb 2017 21:45:35 +0100 Subject: [PATCH] Change `org-get-repeater' signature * lisp/org.el (org-get-repeater): Change optional argument meaning. * lisp/org-habit.el (org-habit-parse-todo): Apply signature change. * testing/test-org.el (test-org/get-repeater): Add tests. --- etc/ORG-NEWS | 4 ++++ lisp/org-habit.el | 2 +- lisp/org.el | 31 ++++++++++++++++++++----------- testing/lisp/test-org.el | 10 +++++++++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 77ad2bcbe..ad70cc1a0 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -62,6 +62,10 @@ to the following ~:istart~, ~:icount~, ~:iend~ and ~:isep~ now expect the type of the list as their first argument. +*** Change signature for ~org-get-repeater~ +The optional argument is now a string to extract the repeater from. +See docstring for details. + ** New features *** ~org-edit-special~ can edit LaTeX environments diff --git a/lisp/org-habit.el b/lisp/org-habit.el index 1f6156571..52a0628df 100644 --- a/lisp/org-habit.el +++ b/lisp/org-habit.el @@ -170,7 +170,7 @@ This list represents a \"habit\" for the rest of this module." (if pom (goto-char pom)) (cl-assert (org-is-habit-p (point))) (let* ((scheduled (org-get-scheduled-time (point))) - (scheduled-repeat (org-get-repeat org-scheduled-string)) + (scheduled-repeat (org-get-repeat (org-entry-get (point) "SCHEDULED"))) (end (org-entry-end-position)) (habit-entry (org-no-properties (nth 4 (org-heading-components)))) closed-dates deadline dr-days sr-days sr-type) diff --git a/lisp/org.el b/lisp/org.el index 619df32bb..19ad0a345 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -13194,18 +13194,27 @@ on INACTIVE-OK." (throw 'exit t))) nil))) -(defun org-get-repeat (&optional tagline) - "Check if there is a deadline/schedule with repeater in this entry." +(defun org-get-repeat (&optional timestamp) + "Check if there is a time-stamp with repeater in this entry. + +Return the repeater, as a string, or nil. Also return nil when +this function is called before first heading. + +When optional argument TIMESTAMP is a string, extract the +repeater from there instead." (save-match-data - (save-excursion - (org-back-to-heading t) - (let ((end (org-entry-end-position)) - (regexp (if tagline (concat tagline "\\s-*" org-repeat-re) - org-repeat-re))) - (catch :repeat - (while (re-search-forward regexp end t) - (when (save-match-data (org-at-timestamp-p)) - (throw :repeat (match-string-no-properties 1))))))))) + (cond (timestamp + (and (string-match org-repeat-re timestamp) + (match-string-no-properties 1 timestamp))) + ((org-before-first-heading-p) nil) + (t + (save-excursion + (org-back-to-heading t) + (let ((end (org-entry-end-position))) + (catch :repeat + (while (re-search-forward org-repeat-re end t) + (when (save-match-data (org-at-timestamp-p)) + (throw :repeat (match-string-no-properties 1))))))))))) (defvar org-last-changed-timestamp) (defvar org-last-inserted-timestamp) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 816b0e1c3..8f5210f70 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -5615,7 +5615,15 @@ Paragraph" (should-not (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE" - (org-get-repeat)))) + (org-get-repeat))) + ;; Return nil when called before first heading. + (should-not + (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>" + (org-get-repeat))) + ;; When called with an optional argument, extract repeater from that + ;; string instead. + (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>"))) + (should-not (org-get-repeat "<2012-03-29 Thu 16:40>"))) (ert-deftest test-org/timestamp-format () "Test `org-timestamp-format' specifications."