emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* feature request for empty blocks in customized agenda
@ 2015-06-13 20:11 Arun Persaud
  2015-06-13 23:00 ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Arun Persaud @ 2015-06-13 20:11 UTC (permalink / raw)
  To: emacs-orgmode

Hi

I just started using a customized agenda with many blocks and some of my
blocks are often empty. However, org-mode still insert the header and
the separator between blocks. Is there a way to skip empty blocks
completely?

I had a quick look at org-agenda.el and it seems that it directly
inserts text, e.g. the header, into the buffer, before it even knows how
many items it added. I guess one could remember point at the beginning,
count the items inserted and if that's 0, delete backwards to the saved
point?
My elisp knowledge is probably not good enough to add this though and it
also seems one would have to add it at different location
(org-agenda-list, org-agenda-todo, etc).

I also checked org-agenda-compact-blocks, but that removes the separator
from all blocks, which is not what I want.

Any chance to get something like this added?

Thanks

Arun

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: feature request for empty blocks in customized agenda
  2015-06-13 20:11 feature request for empty blocks in customized agenda Arun Persaud
@ 2015-06-13 23:00 ` Kyle Meyer
  2015-06-13 23:57   ` Arun Persaud
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2015-06-13 23:00 UTC (permalink / raw)
  To: Arun Persaud; +Cc: emacs-orgmode

Hi,

Arun Persaud <apersaud@lbl.gov> wrote:
> Hi
>
> I just started using a customized agenda with many blocks and some of my
> blocks are often empty. However, org-mode still insert the header and
> the separator between blocks. Is there a way to skip empty blocks
> completely?

Not that I'm aware of.

> I had a quick look at org-agenda.el and it seems that it directly
> inserts text, e.g. the header, into the buffer, before it even knows how
> many items it added. I guess one could remember point at the beginning,
> count the items inserted and if that's 0, delete backwards to the saved
> point?
> My elisp knowledge is probably not good enough to add this though and it
> also seems one would have to add it at different location
> (org-agenda-list, org-agenda-todo, etc).
>
> I also checked org-agenda-compact-blocks, but that removes the separator
> from all blocks, which is not what I want.
>
> Any chance to get something like this added?

I'm not sure if someone will find this feature useful enough to
incorporate.  However, even if it doesn't get added, I think you can get
the behavior you want using org-agenda-finalize-hook.  Hopefully the
snippet below can be a useful starting point.

#+begin_src elisp
  (defun org-agenda-delete-empty-blocks ()
    "Remove empty agenda blocks.
  A block is identified as empty if there are fewer than 2
  non-empty lines in the block (excluding the line with
  `org-agenda-block-separator' characters)."
    (when org-agenda-compact-blocks
      (user-error "Cannot delete empty compact blocks"))
    (setq buffer-read-only nil)
    (save-excursion
      (goto-char (point-min))
      (let* ((blank-line-re "^\\s-*$")
             (content-line-count (if (looking-at-p blank-line-re) 0 1))
             (start-pos (point))
             (block-re (format "%c\\{10,\\}" org-agenda-block-separator)))
        (while (and (not (eobp)) (forward-line))
          (cond
           ((looking-at-p block-re)
            (when (< content-line-count 2)
              (delete-region start-pos (1+ (point-at-bol))))
            (setq start-pos (point))
            (forward-line)
            (setq content-line-count (if (looking-at-p blank-line-re) 0 1)))
           ((not (looking-at-p blank-line-re))
            (setq content-line-count (1+ content-line-count)))))
        (when (< content-line-count 2)
          (delete-region start-pos (point-max)))
        (goto-char (point-min))
        ;; The above strategy can leave a separator line at the beginning
        ;; of the buffer.
        (when (looking-at-p block-re)
          (delete-region (point) (1+ (point-at-eol))))))
    (setq buffer-read-only t))

  (add-hook 'org-agenda-finalize-hook #'org-agenda-delete-empty-blocks)
#+end_src

--
Kyle

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: feature request for empty blocks in customized agenda
  2015-06-13 23:00 ` Kyle Meyer
@ 2015-06-13 23:57   ` Arun Persaud
  0 siblings, 0 replies; 3+ messages in thread
From: Arun Persaud @ 2015-06-13 23:57 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: emacs-orgmode

Hi

> I'm not sure if someone will find this feature useful enough to
> incorporate.  However, even if it doesn't get added, I think you can get
> the behavior you want using org-agenda-finalize-hook.  Hopefully the
> snippet below can be a useful starting point.
> 
> #+begin_src elisp
>   (defun org-agenda-delete-empty-blocks ()
>     "Remove empty agenda blocks.
>   A block is identified as empty if there are fewer than 2
>   non-empty lines in the block (excluding the line with
>   `org-agenda-block-separator' characters)."
>     (when org-agenda-compact-blocks
>       (user-error "Cannot delete empty compact blocks"))
>     (setq buffer-read-only nil)
>     (save-excursion
>       (goto-char (point-min))
>       (let* ((blank-line-re "^\\s-*$")
>              (content-line-count (if (looking-at-p blank-line-re) 0 1))
>              (start-pos (point))
>              (block-re (format "%c\\{10,\\}" org-agenda-block-separator)))
>         (while (and (not (eobp)) (forward-line))
>           (cond
>            ((looking-at-p block-re)
>             (when (< content-line-count 2)
>               (delete-region start-pos (1+ (point-at-bol))))
>             (setq start-pos (point))
>             (forward-line)
>             (setq content-line-count (if (looking-at-p blank-line-re) 0 1)))
>            ((not (looking-at-p blank-line-re))
>             (setq content-line-count (1+ content-line-count)))))
>         (when (< content-line-count 2)
>           (delete-region start-pos (point-max)))
>         (goto-char (point-min))
>         ;; The above strategy can leave a separator line at the beginning
>         ;; of the buffer.
>         (when (looking-at-p block-re)
>           (delete-region (point) (1+ (point-at-eol))))))
>     (setq buffer-read-only t))
> 
>   (add-hook 'org-agenda-finalize-hook #'org-agenda-delete-empty-blocks)
> #+end_src

a starting point seems to be an understatement ;) this seems to work
exactly as intended. Thanks!

Completely blanked out the use of any hooks, although it seems the
obvious thing to do ;)

Thanks again

Arun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-13 23:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-13 20:11 feature request for empty blocks in customized agenda Arun Persaud
2015-06-13 23:00 ` Kyle Meyer
2015-06-13 23:57   ` Arun Persaud

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).