1

I have been using:

(defun org-todo-list-current-file (&optional arg)
 ;;  "Like `org-todo-list', but using only the current buffer's file."
  (interactive "P")
  (let ((org-agenda-files (list (buffer-file-name (current-buffer)))))
    (if (null (car org-agenda-files))
        (error "%s is not visiting a file" (buffer-name (current-buffer)))
      (org-todo-list arg))))

(global-set-key (kbd "<f7>") 'org-todo-list-current-file)

to create a Global list of TODO items, as a shortcut key to C-c a < t .

I then used to be able to do

Press ‘N r’ (e.g. ‘0 r’)

To filter by a particular TODO type.

However, for some reason, this stopped working recently. Maybe due to an upgrade of emacs. Also pressing r to refresh a list has stopped working.

However, if I use C-c a < t to create the same Global list of TODO items, then N - r and r do work.

I have been trying to figure out a solution to this and have not come up with anything.

Any help much appreciated.

7
  • I'm guessing you got that from this question. I posted a comment on the relevant answer indicating its shortcoming in this respect. You are better off either imitating what Org mode is doing when setting a restriction (which is somewhat complicated IIRC). I would just use a keyboard macro for C-c a < t instead and let Org mode do the heavy lifting.
    – NickD
    Apr 26, 2021 at 19:00
  • ... except that does not seem to work.
    – NickD
    Apr 26, 2021 at 19:50
  • Hi NickD thanks once again for your time and help. I just figured out that if I add the file to the list using C-c [, then filtering by TODO type or using r to do a refresh does work.
    – ironfish
    Apr 27, 2021 at 19:31
  • PS I did get that code from the question you refer to.
    – ironfish
    Apr 27, 2021 at 19:32
  • Now I would like to see if I can add some code to my code above to add the current file to the list....if you have any thoughts on that, it would be appreciated....
    – ironfish
    Apr 27, 2021 at 19:33

1 Answer 1

2

Try the following:

    (defun ndk/org-todo-list-current-file ()
      (interactive)
      (when (derived-mode-p 'org-mode))
        (org-agenda nil "t" 'buffer))


    (define-key org-mode-map (kbd "<f7>zt") #'ndk/org-todo-list-current-file)

Since we assume that the current file is an Org mode file (it does not make any sense to ask for the TODO list of arbitrary files), the function checks that the mode of the current buffer is org-mode before calling org-agenda in a special way: it calls it with a keys argument of the string "t" (IOW asking for org-agenda to dispatch to the function org-todo-list) and with a restriction argument of buffer, restricting the agenda command to just the current buffer. This is the lisp equivalent of C-c a < t.

We also define the key to bind this command to in org-mode-map only, not globally, since it is not useful to define it globally: we cannot invoke the command unless the buffer is an Org mode buffer. Given this, we could dispense with the checking of the mode in the function, but a little paranoia never hurt (and actually, it is necessary, just in case you decide to call the function directly with M-x ndk/org-todo-list-current-file and not through the keybinding).

You can use any key combo that is not used. In my particular case, both <f7> and <f7>z are prefix keys, and <f7>zt was unused, so that's the keybinding I used, but YMMV: choose something appropriate to your setup.

If I visit an Org mode file (whether it's an agenda file or not) and call the command either with the keybinding I created, <f7>zt, or with M-x ndk/org-todo-list-current-file I get the TODO list for that file only and any subsequent N r invocations remain restricted to that file only.

2
  • That works perfectly :) This comment box says to avoid "Thanks" but I am very grateful for your help!
    – ironfish
    Apr 28, 2021 at 12:52
  • Thanks also for taking the time to describe how the code works - I learned something :)
    – ironfish
    Apr 28, 2021 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.