org--agenda-prefix-format %? does not work

2019-04-02 01:35发布

问题:

Currently, I have my global TODO list shown as follows thanks to erikstokes:

(org-agenda-prefix-format " %i %?-12(concat \"[ \"(org-format-outline-path (list (nth 1 (org-get-outline-path)))) \" ]\") ")))

which outputs:

for org layout:

However, as you can see, for Task A, even though there is nothing in the project, it still shows up on the list.

describe-variable for org-agenda-prefix-format says :

If the first character after `%' is a question mark, the entire field
will only be included if the corresponding value applies to the current
entry.  This is useful for fields which should have fixed width when
present, but zero width when absent. 

So I feel like by using %?, [ ] shouldn't be there for Task A, yet it still shows up.

回答1:

The problem is that the field is never empty: it will always contain at least the left and right square brackets plus the white space to bring it to a width of 12.

The solution is to write a function that returns either an empty string or the bracketed project and use that in the format:

  (defun foo ()
     (let ((x (nth 1 (org-get-outline-path))))
       (if x
           (concat "[ " (org-format-outline-path (list x)) " ]")
         "")))

  (setq org-agenda-prefix-format " %i %?-12(foo) "