2

I put together the function below but it doesn't work. The problem is get-text-property returns nil for every task.

Any idea how to get the CREATED property and fix the entire thing?

(defun jarfar/org-agenda-cmp-user-defined-created-date (a b)
  "Org Agenda user function to sort tasks based on CREATED property."
  (let* (
          (time-a (get-text-property 0 'CREATED a))
          (time-b (get-text-property 0 'CREATED b))
          (time-a (if time-a (org-time-string-to-time time-a) nil))
          (time-b (if time-b (org-time-string-to-time time-b) nil)))

    (if (and time-a time-b)
      (if (org-time< time-a time-b)
        -1
        (if (org-time> time-a time-b)
          1
          nil))
      (if time-a time-a time-b)
      )))

EDIT: Here is an example data file:

* Tasks
** TODO Task 1
:PROPERTIES:
:CREATED: [2020-07-23 Thu]
:END:

** TODO Task 2
:PROPERTIES:
:CREATED: [2020-06-15 Tue]
:END:

** TODO Task 3
:PROPERTIES:
:CREATED: [2020-05-22 Sat]
:END:

** TODO Task 4
:PROPERTIES:
:CREATED: [2020-06-01 Tue]
:END:

EDIT: Update to the function:

(defun jarfar/org-agenda-cmp-user-defined-created-date (a b)
  "Org Agenda user function to sort tasks based on CREATED property."
  (let* (
          (marker-a (get-text-property 0 'org-marker a))
          (marker-b (get-text-property 0 'org-marker b))
          (time-a (if marker-a (org-entry-get marker-a "CREATED") nil))
          (time-b (if marker-b (org-entry-get marker-b "CREATED") nil))
          (time-a (if time-a (org-time-string-to-time time-a) nil))
          (time-b (if time-b (org-time-string-to-time time-b) nil)))

    (if (and time-a time-b)
      (if (org-time< time-a time-b)
        -1
        (if (org-time> time-a time-b)
          1
          nil))
      (if time-a time-a time-b)
      )))
11
  • What is org-journal-create-re? The name implies that it's a regex, not a text property. If you do C-u C-x = on the entry, does it report some sort of created text property?
    – NickD
    Commented Sep 4, 2020 at 16:45
  • I found some examples online which use regex in place of text property but I have not idea if these work. I tried different things, together with regex. I run this shortcut while pointing on TODO and it opened help buffer with charset/encoding info, but face was org-todo. When I did the same on CREATED prop the face is org-special-keyword. It didn't change anything when I put org-special-keyword in place of org-journal-created-re. The value returned by get-text-property is still nil. org-special-keyword shows up as a face for anything inside heading.
    – Navidot
    Commented Sep 4, 2020 at 21:01
  • AFAIK, text properties are symbols. Go to your buffer, put the cursor somewhere and say M-x describe-text-properties to get the text properties at point.
    – NickD
    Commented Sep 5, 2020 at 1:27
  • Ok, I did that but it doesn't show anything specific to CREATED.
    – Navidot
    Commented Sep 5, 2020 at 6:02
  • So that tells you there is no CREATED text property at that point. Should there be? How do you know? If I were you, the next question I would ask myself is: I was expecting this text property to have been created here - who should be creating that text property at this point in the buffer and why is it NOT creating it? Unfortunately, debugging is hard (but remote debugging is almost impossible).
    – NickD
    Commented Sep 6, 2020 at 0:14

1 Answer 1

2

The compare function is run on the agenda buffer, not the original org buffer. Various data from the original is inserted as text properties, but this doesn't include general org properties (as in things in the :PROPERTIES: drawer).

To retrieve generic data from an org entry, first get the position of the original entry (stored as the org-marker text property). Then use org-entry-get on that position:

(let ((a-pos (get-text-property 0 'org-marker a))
      (a-time (org-entry-get a-pos "CREATED"))
      ))
5
  • It works, thanks! For some files I get sort: Wrong type argument: number-or-marker-p, (23498 25072). Any idea why and how to handle this?
    – Navidot
    Commented Sep 10, 2020 at 13:37
  • 1
    Sounds like somebody is expecting a marker but not getting one. Try M-x toggle-debug-on-error to get a backtrace and see if you can find out where the bad data is coming from. Looking at those numbers it looks like a timestamp. decode-time says it points to 2018-10-19. Maybe you're accidentally passing a time instead of a position?
    – erikstokes
    Commented Sep 10, 2020 at 15:50
  • thx, I was able to catch and fix some errors in data. I updated the function in original post to reflect current state. Now I get error when there is empty properties list - but it's declared. Any idea to improve that func to do some check and prevent the error if the property is not there?
    – Navidot
    Commented Sep 11, 2020 at 15:45
  • Might be better to ask a new question so you can put in more details about the error.
    – erikstokes
    Commented Sep 11, 2020 at 15:52
  • Related question emacs.stackexchange.com/questions/60613/…
    – Navidot
    Commented Sep 11, 2020 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.