0

org-src-mode-hook is run upon editing a source block using org-edit-src-mode. Is there a way to set behaviour upon exiting editing a source block?

In particular I would like to run blacken-buffer upon exiting editing a block of Python code.

  • 1
    the docstring of org-src-mode-hook says it is run "after entering or leaving org-src-mode". – JeanPierre Oct 6 '18 at 13:01
  • @JeanPierre That's right. However, exiting editing a source block doesn't trigger leaving org-src-mode, the buffer simply gets killed, the org-src-mode buffer-local minor mode won't be disabled. – xuchunyang Oct 7 '18 at 5:55
  • @xuchunyang Oh indeed, very good point! – JeanPierre Oct 7 '18 at 7:21
1

You can try to advice the org-edit-src-exit command, for example,

(define-advice org-edit-src-exit (:before (&rest _args) format-python)
  "Run `blacken-buffer' on Python code."
  (when (eq major-mode 'python-mode)
    (message "Formatting python code...")
    (blacken-buffer)))

I don't have the blacken-buffer command available, thus I don't know if the above actually works.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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