nobiot / org-transclusion

Emacs package to enable transclusion with Org Mode
https://nobiot.github.io/org-transclusion/
GNU General Public License v3.0
926 stars 47 forks source link

Some quirks of kill-emacs-hook when set as buffer-local #246

Closed akashpal-21 closed 4 months ago

akashpal-21 commented 4 months ago

consider the simple case

(defun debug/local-kill-hook ()
  (with-temp-buffer
    (write-file "~/Desktop/test2/local-confirmation")))

(add-hook 'kill-emacs-hook #'debug/local-kill-hook nil t)

(defun debug/global-kill-hook ()
  (with-temp-buffer
    (write-file "~/Desktop/test2/global-confirmation")))

(add-hook 'kill-emacs-hook #'debug/global-kill-hook)

When emacs is running in daemon mode - the local hook only runs successfully iff

when emacs receives the restart signal using systemctl --user restart emacs the buffer is currently active and visible

If user switches to another buffer or closes client the local-hook never runs.

This hook in buffer-local mode is used by org-transclusion when running the org-transclusion-before-kill and suffers from the same limitation.

akashpal-21 commented 4 months ago

Sketch of a proof-of-concept global kill-emacs-hook

 (defvar org-transclusion-buffers nil)

  (defun org-transclusion-add-to-buffer-list (&rest args)
    (add-to-list 'org-transclusion-buffers (current-buffer) t 'equal))

  (defun org-transclusion-remove-from-buffer-list (&rest args)
    (setq org-transclusion-buffers (delete (current-buffer) org-transclusion-buffers)))

  (defun org-transclusion-before-kill-emacs (&rest args)
    (when org-transclusion-buffers
      (dolist (buffer org-transclusion-buffers)
    (with-current-buffer buffer
      (funcall #'org-transclusion-before-kill)))))

  (advice-add 'org-transclusion-add :after #'org-transclusion-add-to-buffer-list)
  (advice-add 'org-transclusion-remove-all :after #'org-transclusion-remove-from-buffer-list)
  (add-hook 'kill-emacs-hook #'org-transclusion-before-kill-emacs)
akashpal-21 commented 4 months ago

Closing -- not org-transclusion related. But should be kept in mind as a known limitation,