From b98abca2591f5dbed43dbe72b3b353b376e3d01d Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Tue, 7 Jun 2022 20:52:57 +0200 Subject: [PATCH] Add hooks so buffers close correctly --- mattermost.el | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mattermost.el b/mattermost.el index 214fb95..8097d99 100644 --- a/mattermost.el +++ b/mattermost.el @@ -259,6 +259,11 @@ conform to a post plist" "Mode to list the Mattermost teams and channels to allow the user to check their status and select between them") +(defun mattermost--kill-root-function () + "Function to call when the Mattermost root buffer is killed" + (when (eq major-mode 'mattermost-root-mode) + (mattermost-disconnect))) + (defvar mattermost-username-button-map (let ((map (make-sparse-keymap))) (set-keymap-parent map button-map) @@ -348,6 +353,12 @@ user to check their status and select between them") "Mode use on a Mattermost channel buffer, that shows the messages of the channel and allows the user to post theirs") +(defun mattermost--kill-buffer-function () + "Function to call when a Mattermost buffer is killed" + (when (eq major-mode 'mattermost-channel-mode) + (setq mattermost-buffers + (plist-put mattermost-buffers (mattermost-string->keyword mattermost-channel-id) nil)))) + ;; ToDo: should we receive just a channel id and fetch the channel ;; data? (defun mattermost-show-channel (channel) @@ -390,10 +401,14 @@ user to check their status and select between them") (mattermost-disconnect) (let ((password (mattermost-read-password))) (mattermost-login mattermost-login-id password)) + (add-hook 'kill-buffer-hook #'mattermost--kill-root-function) + (add-hook 'kill-buffer-hook #'mattermost--kill-buffer-function) (mattermost-show-root)) (defun mattermost--kill-all-buffers () "Kills all Mattermost related buffers" + (remove-hook 'kill-buffer-hook #'mattermost--kill-root-function) + (remove-hook 'kill-buffer-hook #'mattermost--kill-buffer-function) (while (car mattermost-buffers) (let ((buf (car (cdr mattermost-buffers))) (tail (cddr mattermost-buffers)))