3

I wanted Emacs to automatically save all buffers after I refiled something, so I followed the bottom-most comment from this question and used

(advice-add 'org-refile :after 'org-save-all-org-buffers)

It works great when the only thing I need to do is refile.

However, if I try to org-copy something (which does an org-refile under the hood), or use a custom function which uses org-refile in some way, I get the following error:

Wrong number of arguments: #[nil "ÀÁ!\210ÂÃÄ\"\210ÅÆ!\203^R^@Ç \210ÀÈ!\207" 
[message "Saving all Org-mode buffers..." save-some-buffers t 
#[nil "ÀÁ!\207" [derived-mode-p org-mode] 2] featurep org-id org-id-locations-save 
"Saving all Org-mode buffers... done"] 
3 ("c:/Users/wiuah/.emacs.d/elpa/org-plus-contrib-20160801/org.elc" . 620558) nil], 4

Anyone know what's going on?

wiuah
  • 409
  • 2
  • 11
  • 1
    How about creating a new custom function that calls `org-refile` and then calls `org-save-all-org-buffers` instead of placing an `advice` on a commonly used function? Sure, you can still play around with `advice` and put in exceptions and additional arguments, but why go through the trouble? – lawlist Sep 07 '16 at 22:03
  • @lawlist If you can provide a working function that takes the same parameters as `org-refile` but also saves the buffers after copying/moving, I'll probably accept it. – wiuah Sep 07 '16 at 22:19
  • 1
    `(defun my-org-refile (&optional goto default-buffer rfloc msg) (interactive "P") "Doc-string." (org-refile goto default-buffer rfloc msg) (org-save-all-org-buffers))` If you really want to learn how to do this with `advice` and create exceptions so that `org-refile` can be used by other functions without throwing an error, then by all means leave the question open for now. Almost everyone except me loves `advice` -- I use it very sparingly. – lawlist Sep 07 '16 at 22:27

2 Answers2

6

I had the exact same issue. The refile advice used to work but stopped at some point. A similar error reported here suggested a solution that works for me:

(advice-add 'org-refile :after
        (lambda (&rest _)
        (org-save-all-org-buffers)))
djangoliv
  • 3,169
  • 16
  • 31
0

Similar advice is described here. It only saves the buffer in question:

(defun save-after-capture-refile ()
  (with-current-buffer (marker-buffer org-capture-last-stored-marker)
    (save-buffer)))
(advice-add 'org-capture-refile :after 'save-after-capture-refile)