emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved
@ 2021-05-11 10:07 mail
  2021-05-11 11:12 ` Nick Savage
  0 siblings, 1 reply; 4+ messages in thread
From: mail @ 2021-05-11 10:07 UTC (permalink / raw)
  To: emacs-orgmode

* Reproduction

1. emacs -Q
2. C-x C-f /tmp/new.org RET
3. Type anything
4. Type C-u C-c # (C-u org-update-statistics-cookies) to try to update all statistics cookies in the buffer
5. Notice the "Non-existent agenda file /tmp/new.org." error.

This doesn't happen if the file already exists.

I first saw this reported in this reddit thread: https://www.reddit.com/r/orgmode/comments/n9g1yf/is_this_a_bug_with_orgupdatestatisticcookies/ (u/flyin1501 is me)

* Expectation

All statistics cookies in the buffer are updated no matter if the buffer's associated file is already created or not.

* Why this is happening

- `org-update-statistics-cookies` (C-c #) uses `org-map-entries` to update all cookies.
- `org-map-entries` uses `org-agenda-prepare-buffers` to prepare the file when SCOPE is nil. Excerpt:

        (if (not scope)
            (progn
              (org-agenda-prepare-buffers
               (and buffer-file-name (list buffer-file-name)))
              (setq res
                    (org-scan-tags
                     func matcher org--matcher-tags-todo-only start-level)))

- This function complains if the file does not exist, causing the error.

* Solution (IMO)

`org-map-entries` is quite complex because of its matching and scoping features; `org-update-statistics-cookies` only updates the current buffer and should use `org-map-region` instead. IMO it should go from

    (if all
        (progn
          (org-update-checkbox-count 'all)
          (org-map-entries 'org-update-parent-todo-statistics))

to:

    (if all
        (progn
          (org-update-checkbox-count 'all)
          (org-map-region 'org-update-parent-todo-statistics (point-min) (point-max)))


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

* Re: [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved
  2021-05-11 10:07 [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved mail
@ 2021-05-11 11:12 ` Nick Savage
  2021-05-16 12:23   ` Bastien
  2021-05-16 19:49   ` Bastien
  0 siblings, 2 replies; 4+ messages in thread
From: Nick Savage @ 2021-05-11 11:12 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2239 bytes --]

Thanks for the bug report, I can confirm this is happening.

I encountered a similar bug where org-agenda-prepare-buffers was causing 
the same issue when the file hadn't been saved to disk, and I agree that 
the solution looks good. I've formatted this into a patch and attached 
it. Might be worth considering too if this is happening anywhere else 
too, since this is the second time it has come up.

On 5/11/21 6:07 AM, mail@kisaragi-hiu.com wrote:
> * Reproduction
>
> 1. emacs -Q
> 2. C-x C-f /tmp/new.org RET
> 3. Type anything
> 4. Type C-u C-c # (C-u org-update-statistics-cookies) to try to update all statistics cookies in the buffer
> 5. Notice the "Non-existent agenda file /tmp/new.org." error.
>
> This doesn't happen if the file already exists.
>
> I first saw this reported in this reddit thread: https://www.reddit.com/r/orgmode/comments/n9g1yf/is_this_a_bug_with_orgupdatestatisticcookies/ (u/flyin1501 is me)
>
> * Expectation
>
> All statistics cookies in the buffer are updated no matter if the buffer's associated file is already created or not.
>
> * Why this is happening
>
> - `org-update-statistics-cookies` (C-c #) uses `org-map-entries` to update all cookies.
> - `org-map-entries` uses `org-agenda-prepare-buffers` to prepare the file when SCOPE is nil. Excerpt:
>
>          (if (not scope)
>              (progn
>                (org-agenda-prepare-buffers
>                 (and buffer-file-name (list buffer-file-name)))
>                (setq res
>                      (org-scan-tags
>                       func matcher org--matcher-tags-todo-only start-level)))
>
> - This function complains if the file does not exist, causing the error.
>
> * Solution (IMO)
>
> `org-map-entries` is quite complex because of its matching and scoping features; `org-update-statistics-cookies` only updates the current buffer and should use `org-map-region` instead. IMO it should go from
>
>      (if all
>          (progn
>            (org-update-checkbox-count 'all)
>            (org-map-entries 'org-update-parent-todo-statistics))
>
> to:
>
>      (if all
>          (progn
>            (org-update-checkbox-count 'all)
>            (org-map-region 'org-update-parent-todo-statistics (point-min) (point-max)))
>

[-- Attachment #2: 0001-lisp-org.el-Fix-org-update-statistics-cookies-error.patch --]
[-- Type: text/x-patch, Size: 1062 bytes --]

From 2081a1d2bd628dd8e5a1ad9fc4ffd58a4f951972 Mon Sep 17 00:00:00 2001
From: Nicholas Savage <nick@nicksavage.ca>
Date: Tue, 11 May 2021 06:59:14 -0400
Subject: [PATCH] lisp/org.el: Fix org-update-statistics-cookies error

* lisp/org.el (org-update-statistics-cookies): Replace
usage of `org-map-entries' with `org-map-region' to prevent bug where
an error was thrown if the current buffer had not yet been saved to disk.

Formatted from a patch proposal by mail@kisaragi-hiu.com
Reported-by: mail@kisaragi-hiu.com
---
 lisp/org.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index 6dbf33abc..fc5197642 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9989,7 +9989,7 @@ all statistics cookies in the buffer."
   (if all
       (progn
 	(org-update-checkbox-count 'all)
-	(org-map-entries 'org-update-parent-todo-statistics))
+	(org-map-region 'org-update-parent-todo-statistics (point-min) (point-max)))
     (if (not (org-at-heading-p))
 	(org-update-checkbox-count)
       (let ((pos (point-marker))
-- 
2.20.1


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

* Re: [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved
  2021-05-11 11:12 ` Nick Savage
@ 2021-05-16 12:23   ` Bastien
  2021-05-16 19:49   ` Bastien
  1 sibling, 0 replies; 4+ messages in thread
From: Bastien @ 2021-05-16 12:23 UTC (permalink / raw)
  To: Nick Savage; +Cc: emacs-orgmode

Hi Nick,

Nick Savage <nick@nicksavage.ca> writes:

> Thanks for the bug report, I can confirm this is happening.

I'm now adding the X-Woof-Bug: confirmed header to track this.

Thanks,

-- 
 Bastien


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

* Re: [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved
  2021-05-11 11:12 ` Nick Savage
  2021-05-16 12:23   ` Bastien
@ 2021-05-16 19:49   ` Bastien
  1 sibling, 0 replies; 4+ messages in thread
From: Bastien @ 2021-05-16 19:49 UTC (permalink / raw)
  To: Nick Savage; +Cc: emacs-orgmode

Nick Savage <nick@nicksavage.ca> writes:

> Thanks for the bug report, I can confirm this is happening.

I read too hastily, I've now applied your patch against maint.

Thanks!

-- 
 Bastien


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

end of thread, other threads:[~2021-05-16 19:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-11 10:07 [bug] C-u org-update-statitics-cookies errors out with "Non-existent agenda file" if current file isn't saved mail
2021-05-11 11:12 ` Nick Savage
2021-05-16 12:23   ` Bastien
2021-05-16 19:49   ` Bastien

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).