alphapapa / org-ql

A searching tool for Org-mode, including custom query languages, commands, saved searches and agenda-like views, etc.
GNU General Public License v3.0
1.39k stars 110 forks source link

Dynamic block property column should default to property name #286

Open yibie opened 2 years ago

yibie commented 2 years ago

For example, I want to create a dynamic block by property, but I don't how to do with my org file.

This is my file:

 DONE 岩田先生:任天堂传奇社长如是说
CLOSED: [2022-07-04 Mon 16:57]
:PROPERTIES:
:Autor: HOBO 日刊 ITOI 新闻
:Category: 企业家
:Added: <2022-07-04 Mon>
:Star: ⭐️⭐️⭐️⭐️⭐️
:Notelink: [[id:E23B33EF-C3A7-45DC-9A7A-43E5CBBA2C47][岩田先生:企业家传奇社长如是说]]
:END:

And I use org-ql in dynamic block, get result:

#+BEGIN: org-ql :query "todo: nil" :columns (heading (property "Author") (property "Category") (property "Added") (property "Star") (property "Notelink")) :take 7
| Heading | Author | Category | Added | Star | Notelink |
|---------+--------+----------+-------+------+----------|
#+END:
tpeacock19 commented 2 years ago

There are a couple things here:

The property colums needs to have labels, and the todo query needs to have a value that will match your heading, in this case DONE. The below worked or me:

#+BEGIN: org-ql :query "todo:DONE" :columns (heading ((property "Author") "A") ( (property "Category") "C") ( (property "Added") "A") ( (property "Star") "S") ( (property "Notelink") "N")) :take 7
| Heading                   | A                  | C     | A          | S           | N                         |
|---------------------------+------------------- +-------+----------- +------------ +---------------------------|
| [[岩田先生:任天堂传奇社长如是说][岩田先生:任天堂传奇社长如是说]] | HOBO 日刊 ITOI 新闻 | 企业家 | <2022-07-04 Mon> | ⭐️⭐️⭐️⭐️⭐️ | [[id:E23B33EF-C3A7-45DC-9A7A-43E5CBBA2C47][岩田先生:企业家传奇社长如是说]] |
#+END:

I suggest having a look at the README especially General Predicates and Dynamic Blocks. It goes into much detail and would address most, if not all, your questions.

Although, @alphapapa it does seem like the property column parameter here should default to the property name if there is no label passed. That doesnt seem to be the case when I've tried it.

#+BEGIN: org-ql :query "todo:DONE" :columns (heading ((property "Author") nil) ( (property "Category") "") )
| Heading                   |                    |       |
|---------------------------+------------------- +-------|
| [[岩田先生:任天堂传奇社长如是说][岩田先生:任天堂传奇社长如是说]] | HOBO 日刊 ITOI 新闻 | 企业家 |
#+END:

I believe it can be fixed with this diff. Though feel free to change whatever you need. I couldn't figure out if pcase allowed for optional elements in a pattern.

diff --git a/org-ql-search.el b/org-ql-search.el
index aa75656..1daacb1 100644
--- a/org-ql-search.el
+++ b/org-ql-search.el
@@ -309,7 +309,7 @@ For example, an org-ql dynamic block header could look like:
                  (cons 'closed (lambda (element)
                                  (--when-let (org-element-property :closed element)
                                    (ts-format ts-format (ts-parse-org-element it)))))
-                 (cons 'property (lambda (element property)
+                 (cons 'property (lambda (element &optional property)
                                    (org-element-property (intern (concat ":" (upcase property))) element)))))
           (elements (org-ql-query :from (current-buffer)
                                   :where query
@@ -320,20 +320,23 @@ For example, an org-ql dynamic block header could look like:
                        ((and integer (satisfies cl-minusp)) (-take-last (abs take) elements))
                        (integer (-take take elements)))))
     (cl-labels ((format-element
-                 (element) (string-join (cl-loop for column in columns
-                                                 collect (or (pcase-exhaustive column
-                                                               ((pred symbolp)
-                                                                (funcall (alist-get column format-fns) element))
-                                                               (`((,column . ,args) ,_header)
-                                                                (apply (alist-get column format-fns) element args))
-                                                               (`(,column ,_header)
-                                                                (funcall (alist-get column format-fns) element)))
-                                                             ""))
-                                        " | ")))
+                  (element) (string-join (cl-loop for column in columns
+                                                  collect (or (pcase-exhaustive column
+                                                                ((pred symbolp)
+                                                                 (funcall (alist-get column format-fns) element))
+                                                                (`((,column . ,args) ,_header)
+                                                                 (apply (alist-get column format-fns) element args))
+                                                                (`((,column . ,args))
+                                                                 (apply (alist-get column format-fns) element args))
+                                                                (`(,column ,_header)
+                                                                 (funcall (alist-get column format-fns) element)))
+                                                              ""))
+                                         " | ")))
       ;; Table header
       (insert "| " (string-join (--map (pcase it
                                          ((pred symbolp) (capitalize (symbol-name it)))
-                                         (`(,_ ,name) name))
+                                         (`(,_ ,name) name)
+                                         (`((,_ ,name)) name))
                                        columns)
                                 " | ")
               " |" "\n")
yibie commented 2 years ago

thx, helps a lot.

alphapapa commented 2 years ago

@tpeacock19 Thanks.