Patrick Nicodemus <gadget...@gmail.com> writes:

> Any task which is a habit is listed in a special portion of the agenda for
> consistency tracking. This is useful and convenient. However, this
> apparently conflicts with scheduling the habit for a certain time. For
> example, if I want to go for a run on Tuesday at 5pm, I would like to have
> the option to schedule my running habit for 5pm on Tuesday and have it
> appear in the agenda at that position.
>
> Ideally I would like to toggle the features of the habit module on and off,
> so that in the agenda view, habits are treated either as normal tasks at a
> particular time, or they appear in the consistency tracker window rather
> than the schedule. Is there a way to accomplish this?
>
> Note that I don't want to filter (remove) the habits from the agenda. I am
> familiar with this functionality.

This is no built-in way to toggle this, but I can give you pointers how
to achieve what you want.

The reason why habits are grouped together is the default value of
`org-agenda-sorting-strategy':

org-agenda-sorting-strategy is a variable defined in ‘org-agenda-sort.el’.

Its value is
((agenda habit-down time-up urgency-down category-keep)
 (todo urgency-down category-keep) (tags urgency-down category-keep)
 (search category-keep))

As you can see, for "agenda" blocks, the first sorting key is
"habit-down" causing all habits go after non-habits.

To get the habits sorted by time (I assume that you are looking for your
habits to appear in the time grid), you need to remove "habit-down" from
the sorting strategy.

You can write a small helper command to do this:

(defvar my/org-agenda-sorting-strategy-pre nil
  "Previous value of `org-agenda-sorting-strategy'.")
(defun my/org-agenda-toggle-habit-grouping ()
  "Toggle grouping of habits in agendas.
Does not work when custom agenda command defines local sorting strategy."
  (interactive)
  (org-agenda-check-type t 'agenda)
  (if my/org-agenda-sorting-strategy-pre
      (progn
        (setq org-agenda-sorting-strategy my/org-agenda-sorting-strategy-pre)
        (setq my/org-agenda-sorting-strategy-pre nil)
        (org-agenda-redo)
        (message "Habit grouping ON"))
    (let ((old-strategy (alist-get 'agenda org-agenda-sorting-strategy)))
      (if (not (or (memq 'habit-up old-strategy) (memq 'habit-down 
old-strategy)))
          (user-error "Habits are not grouped in agendas: %S" old-strategy)
        (setq my/org-agenda-sorting-strategy-pre (copy-alist 
org-agenda-sorting-strategy))
        (setf (alist-get 'agenda org-agenda-sorting-strategy) (delete 'habit-up 
(delete 'habit-down old-strategy)))
        (org-agenda-redo)
        (message "Habit grouping OFF")))))

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to