alphapapa / org-ql

A searching tool for Org-mode, including custom query languages, commands, saved searches and agenda-like views, etc.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to run usual org functions like refile or schedule on results from org-ql or org-ql-search

AloisJanicek opened this issue · comments

I've just noticed that you can't use functions like org-agenda-refile or org-agenda-schedule on result items when using org-ql or org-ql-search. I even tried putting org-ql-block into org-agenda-custom-commands as shown in README, and I got same result.
For example running org-agenda-schedule returns org-agenda-check-type: No Org agenda currently displayed.
I kind of understand that org-ql buffers probably aren't usual org-agenda buffers and that org-ql is still in development, so at this point I am just asking if this is an expected behavior or some kind of issue on my side and if former, is there currently any way to use org-ql and be able to manipulate org items directly from results' buffer?

This works as a workaround for me:

(defun my-set-org-agenda-type (&rest args)
  (when (and (not org-agenda-type)
             (eq major-mode 'org-agenda-mode))
    (set (make-local-variable 'org-agenda-type) 'agenda)))
(advice-add 'org-agenda-schedule :before 'my-set-org-agenda-type)
(advice-add 'org-agenda-deadline :before 'my-set-org-agenda-type)

Strangely, simply setting org-agenda-type in the agenda buffer doesn't work, its value remains nil.

Thank you, works perfectly.
I also noticed that today org-agenda-refile works for me just fine without advising, strange.

@AloisJanicek Yes, as noted in the readme:

Note: The view buffer is currently put in org-agenda-mode, which means that some Org Agenda commands work, such as jumping to entries and changing item priorities (without necessarily updating the view). This feature is experimental and not guaranteed to work correctly with all commands. (It works to the extent it does because the appropriate text properties are placed on each item, imitating an Agenda buffer.)

@mz-pdm Yes, after some quick testing, I also found the org-agenda-type variable to be essentially unsettable, even buffer-locally with setq-local, which is strange.

I don't know what the best solution is. I don't think we should be advising functions to work around this in org-ql. Ideally we need to set org-agenda-type, then those commands should work. That's the problem we should try to solve first.

The following piece of code, when placed to org-agenda-finalize-hook, makes org-agenda-do-date-later and org-agenda-do-date-earlier (S-right, S-left) commands working:

    (goto-char (point-min))
    (while (not (eobp))
      (let* ((scheduled (org-get-at-bol 'scheduled))
             (pos (and scheduled (plist-get (cadr scheduled) :begin)))
             (marker (org-get-at-bol 'org-marker)))
        (when (and pos marker)
          (move-marker marker pos (marker-buffer marker))))
      (forward-line))

Advising the commands with org-agenda-type as outlined above is additionally needed.

@mz-pdm I don't understand what that code is doing, but we definitely can't be doing anything like that in this package. We can't advise any org-agenda functions here.

What the code does is moving org-marker, present in the entry properties, from the beginning of a scheduled entry to its SCHEDULED: date. This is what the org-agenda-do-date-* functions require. It's a workaround I use to make the functions working, which makes my org-ql-block agenda functionality fairly complete.
Of course, we shouldn't advise Org functions, it's just a workaround until a proper solution, setting org-agenda-type, is found (sorry, I don't have time to help with that at the moment).

@mz-pdm Yes, after some quick testing, I also found the org-agenda-type variable to be essentially unsettable, even buffer-locally with setq-local, which is strange.

I am not sure why this is designed in such way, I am not a emacs lisp expert, but I think there are two bits that making this unsetteble outside.

Frist one is in org-agenda-finalize function which setting org-agenda-type based on text properties from agenda buffer:

(defun org-agenda-finalize ()
  [...]
	(setq org-agenda-type (org-get-at-bol 'org-agenda-type))

The second one is org-agenda-update-agenda-type which is basically do the same but in post-command-hook:

(defun org-agenda-mode ()
  [...]
  (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)

[...]

(defun org-agenda-update-agenda-type ()
  "Update the agenda type after each command."
  (setq org-agenda-type
	(or (get-text-property (point) 'org-agenda-type)
	    (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))

Also reading org-ql-agenda-buffer reports that this property is set to nil. I checked practically every symbol in the buffer, but sorry! No luck.

Fortunetly hacking org-ql package was very easy. First function in "Faces/properties" section is the function that is applying properties to the text that will appear in the buffer. Here is the patch:

--- org-ql-agenda-a.el  2019-09-12 20:16:05.143186082 +0200
+++ org-ql-agenda-b.el  2019-09-12 20:15:58.336519490 +0200
@@ -574,6 +574,7 @@
            (org-add-props it properties
              'todo-state todo-keyword
              'tags tag-list
+             'org-agenda-type 'agenda
              'org-habit-p habit-property)))))

Although, shifting entires with "org-agenda-do-date-earlier" and similiar still not working, it reports that timestamp was not found. Probably is should be text properties too?

Anyway, for me org-schedule is enough and I set org-agenda-type to 'search. I believe it suits org-ql buffers better.

Hi Mikhail,

Thank you very much for doing that research, and for sending the PR. I will see about merging it soon.

This should be fixed now (at least, for some commands, like scheduling and deadlines). Thanks to all for your help here.