reddit / emacs

overriding org-capture when the buffer is empty

yasser_kaddoura · 2022-04-15 01:27
What I need: Using `org-capture-finalize` in `org-capture-mode` while having some certain text, example ("* TODO \n") exits without doing anything (similar to the functionality of `org-capture-kill`) My progress: (defun my-finalize-if-no-todo () (interactive) (setq matches (count-matches "* TODO \n" 0)) (if (= matches 1) (org-capture-kill) ;; Should be changed to function properly nil )) (add-hook 'org-capture-prepare-finalize-hook #'my-finalize-if-no-todo) ;; Is this the right hook to use? Post notes: I tried to figure out how `org-capture-kill` does it, and it seems that it sets `org-note-abort` to `t`, but I am not sure how to do that in a hook. I tried to use `setq` and `let`, but I didn't manage to make it work. I am noob when it comes to Emacs and Emacs Lisp, so the answer might be simple. EDIT: I found a hack to get the behavior that I wanted. ;; Don't capture empty TODO tasks (defun my-finalize-if-no-todo () (interactive) (setq matches (count-matches "* TODO \n" 0)) (if (= matches 1) (setq org-note-abort t) nil )) (defun my-finalize-reset-abort () (interactive) (setq org-note-abort nil)) (add-hook 'org-capture-prepare-finalize-hook #'my-finalize-if-no-todo) (add-hook 'org-capture-after-finalize-hook #'my-finalize-reset-abort) The hook post finalization is needed, otherwise every capture afterwards won't work.
nv-elisp · 2022-04-15 04:58
It's a little unclear what you're asking for. You want to capture something, but immediately abort the capture? What's the actual context for that? There may be a better way to do what you're trying to do.
yasser_kaddoura · 2022-04-15 13:08
It's a very minor thing, but I was wondering how to do it. In a nutshell, when you press `C-c C-c` while capturing, act as `C-c C-k`. The context is that you don't want to capture an empty TODO entry by mistake.