org-ql icon indicating copy to clipboard operation
org-ql copied to clipboard

org-ql-agenda: Custom formatter support

Open georgewsinger opened this issue 5 years ago β€’ 19 comments

Is there a way to force org-ql-agenda to show the name of the buffer from which each task is derived? I tried adding :auto-category t to :super-groups but it didn't work (i.e., it probably groups by category but still doesn't show the name of the category).

georgewsinger avatar Jun 03 '19 17:06 georgewsinger

You could modify the formatter to do so.

alphapapa avatar Jun 03 '19 17:06 alphapapa

By this do you mean modifying this function to show the buffer source?

georgewsinger avatar Jun 03 '19 19:06 georgewsinger

Yes. You can see a comment at the bottom that mentions adding the proper prefix, which refers to imitating org-agenda-prefix-format. Adding support for that would let you set the prefix according to your preferences.

Alternatively, you could use simple :after function advice to alter the string returned by the function.

alphapapa avatar Jun 03 '19 20:06 alphapapa

I probably won't work on this soon, but it's a good idea.

alphapapa avatar Aug 10 '19 11:08 alphapapa

https://github.com/alphapapa/org-ql/pull/44

alphapapa avatar Jan 03 '20 10:01 alphapapa

Isn't this already supported with the use of "select" in org-ql-query? There one can provide a custom sexp to format the output. Same with the action argument to org-ql-select, no?

Is there a reason for not using the same functionality in the org-ql views?

Whil- avatar May 03 '20 09:05 Whil-

@Whil- If you want to override the formatting function used in org-ql-view, you could use function advice. As I've said in other issues, I'm not very interested in adding little things that would be intended to be deprecated in the future.

alphapapa avatar May 03 '20 15:05 alphapapa

Got it, thanks. Though providing the select functionality also for the view function should hardly be considered minor. Thinking of org mode documents as a database of entities is very powerful and org-ql is very helpful with this. And allowing the output from the query on the database to be customized by for example adding attributes from the nodes etc. makes a lot of sense.

This ofc already is possible using org-ql-select and org-ql-query. So I'll just finish with a thumbs up for that! πŸ‘

Whil- avatar May 03 '20 15:05 Whil-

Though providing the select functionality also for the view function should hardly be considered minor.

What I mean is, since I plan to implement a new view library, I don't want to spend much effort on implementing formatting changes that would be obsoleted by the future plans.

In the meantime, you can use advice-add or cl-letf to override the formatting function in your code.

However, since this has taken a while, it might be worth adding an argument and/or a variable to let users more easily change the formatting function.

alphapapa avatar Oct 26 '20 09:10 alphapapa

As I have pointed out here on reddit, the URL from OP provided in link is not working anymore. So I don't know which format function he is referencing.

Also does that mean - asked from an Elisp newbie - I need to go into the source code and modify the format function? Or the returned string?

pragmat1c1 avatar Oct 26 '20 10:10 pragmat1c1

Let's pick one place or the other to have this conversation. I'll reply on Reddit again.

alphapapa avatar Oct 26 '20 11:10 alphapapa

For completeness I'll repost the solution via adding an advice here as well, if you don't mind?

(defun ugt/org-ql-view--format-element (orig-fun &rest args)
  "This function will intercept the original function and
   add the filename to the result.

   ARGS is `element' in `org-ql-view--format-element'"
  (if (not args)
      ""
    (let* ((element args)
           (properties (cadar element))
           (result (apply orig-fun element))
           (filename (buffer-name
                      (marker-buffer
                       (plist-get properties :org-marker)))))
      (concat filename " " result))))
(advice-add 'org-ql-view--format-element :around #'ugt/org-ql-view--format-element)

pragmat1c1 avatar Oct 26 '20 21:10 pragmat1c1

Minor tweak to @pragmat1c1 code. In case someone wants to add category instead of filename. updated properties as mentioned @ reddit comment

(defun ugt/org-ql-view--format-element (orig-fun &rest args)
  "This function will intercept the original function and
   add the category to the result.

   ARGS is `element' in `org-ql-view--format-element'"
  (if (not args)
      ""
    (let* ((element args)
           (properties (cadar element))
           (result (apply orig-fun element))
           (category (get-text-property
                      0
                      'org-category
                      (plist-get properties :todo-keyword))))
      (org-add-props (concat category ":" result) (text-properties-at 0 result)))))
(advice-add 'org-ql-view--format-element :around #'ugt/org-ql-view--format-element)

jun8git avatar Apr 24 '21 08:04 jun8git

Since it's been so long since the last 0.x release, I'm going to defer this to 0.7 in an effort to reduce the scope of 0.6 and release it sooner.

alphapapa avatar Jun 17 '21 09:06 alphapapa

I'm using this version of org-ql:

     Status: Installed in β€˜org-ql-20220301.841/’ (unsigned).
    Version: 20220301.841
     Commit: af18eac2b80b2f56c135f37fcbdcce19fbc34b65

and the function from the comment above returns an empty category.

Here's the fixed version:

  (defun zdo/org-ql-view--format-element (orig-fun &rest args)
    "This function will intercept the original function and
   add the category to the result.

   ARGS is `element' in `org-ql-view--format-element'"
    (if (not args)
        ""
      (let* ((element args)
             (properties (cadar element))
             (result (apply orig-fun element))
             (category (org-entry-get (plist-get properties :org-marker) "CATEGORY")))
        (org-add-props
            (format "   %-8s %s" (concat category ":") result)
            (text-properties-at 0 result)))))
  (advice-add 'org-ql-view--format-element :around #'zdo/org-ql-view--format-element)

with nice indentation πŸ™‚

image

dmitri-zganiaiko avatar Mar 09 '22 20:03 dmitri-zganiaiko

For future reference, this issue will generally be addressed by the branch mentioned here: https://github.com/alphapapa/org-ql/issues/331

alphapapa avatar Mar 31 '23 09:03 alphapapa

Thanks @dmitri-zganiaiko. I added some modifications to make sure the indentation will work too if the file or buffer name are too long.

(defun salih/org-ql-view--format-element (orig-fun &rest args)
  "This function will intercept the original function and
add the category to the result.

ARGS is `element' in `org-ql-view--format-element'"
  (if (not args)
      ""
    (let* ((element args)
           (properties (cadar element))
           (result (apply orig-fun element))
           (smt "")
           (category (org-entry-get (plist-get properties :org-marker) "CATEGORY")))
      (if (> (length category) 11)
          (setq category (substring category 0 10)))
      (if (< (length category) 11)
          (setq smt (make-string (- 11 (length category)) ?\s)))
      (org-add-props
       (format "   %-8s %s" (concat category ":" smt) result)
       (text-properties-at 0 result)))))

larrasket avatar Jul 26 '23 14:07 larrasket

Hi, I'm looking for a way to format a org-ql-block and this issue is related, but I think it's meant to be applied in a bigger scope than org-ql-block

How do I supply a formatting option to an individual org-ql-block? For instance, How can I turn the following code into org-ql-block?

      (tags-todo "inbox"
                 ((org-agenda-prefix-format "  %?-12t% s")
                  (org-agenda-overriding-header "\nInbox\n")))
                  

Thank you for the support

pcompassion avatar Sep 07 '23 09:09 pcompassion

@pcompassion For the header, please see the documentation: https://github.com/alphapapa/org-ql#function-org-ql-block The formatter is not yet customizeable, other than editing the function, which you're welcome to do in your configuration, but you'll be on your own for that.

alphapapa avatar Sep 08 '23 10:09 alphapapa