From 635e95107d7140fbdffc20942465fb7bfeeda304 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 3 Jun 2022 19:22:36 +0200 Subject: [PATCH] Maintain a list of buffers --- mattermost.el | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/mattermost.el b/mattermost.el index 612bee7..da3dc9a 100644 --- a/mattermost.el +++ b/mattermost.el @@ -40,6 +40,9 @@ (defvar mattermost-websocket nil "The websocket connected to the server") +(defvar mattermost-buffers '() + "Plist with all the open channel buffers") + (defvar-local mattermost-prompt-marker nil "The marker that shows where the prompt starts") @@ -49,6 +52,9 @@ (defvar-local mattermost-channel-id nil "The buffer channel ID") +(defconst mattermost-root-buffer-name "*Mattermost Root*" + "The name of the root buffer") + ;; ToDo: probably not the best way to get a keyword from a string (defun mattermost-string->keyword (str) "Returns a keyword from a string" @@ -132,7 +138,7 @@ conform to a post plist" (websocket-send-text ws (mattermost--get-auth-challenge))) :on-message 'mattermost--process-ws-frame :on-close (lambda (ws) - (lwarn 'mattermost :error "websocket connection closed")))) + (lwarn 'mattermost :debug "websocket connection closed")))) ;; ToDo: once mattermost-request parses headers, use it to fetch both ;; the user (set the id to a local var) and the headers (set the @@ -218,7 +224,7 @@ conform to a post plist" (defun mattermost-show-root () "Populates the Mattermost Root buffer and changes to it" (interactive) - (let ((rootb (get-buffer-create "*Mattermost Root*"))) + (let ((rootb (get-buffer-create mattermost-root-buffer-name))) (with-current-buffer rootb (let ((inhibit-read-only t) (teams (mattermost-get-teams))) @@ -352,9 +358,11 @@ user to check their status and select between them") (order (seq-reverse (plist-get resp :order))) (posts (plist-get resp :posts))) (dolist (post-id order) - (let ((post (plist-get posts (mattermost-string->keyword post-id)))) + (let* ((post (plist-get posts (mattermost-string->keyword post-id)))) (mattermost-insert-post post)))) (mattermost-channel-mode)) + (setq mattermost-buffers + (plist-put mattermost-buffers (mattermost-string->keyword id) chanb)) (switch-to-buffer chanb) (setq mattermost-channel-id id) (setq mattermost-prompt-marker (make-marker)) @@ -376,9 +384,21 @@ user to check their status and select between them") (mattermost-login mattermost-login-id password)) (mattermost-show-root)) +(defun mattermost--kill-all-buffers () + "Kills all Mattermost related buffers" + (while (car mattermost-buffers) + (let ((buf (car (cdr mattermost-buffers))) + (tail (cddr mattermost-buffers))) + (setq mattermost-buffers tail) + (kill-buffer buf))) + (setq mattermost-buffers '()) + (unless (null (get-buffer mattermost-root-buffer-name)) + (kill-buffer mattermost-root-buffer-name))) + (defun mattermost-disconnect () "Closes the connection with the Mattermost server" (interactive) + (mattermost--kill-all-buffers) (unless (null mattermost-websocket) (websocket-close mattermost-websocket)) (setq mattermost-token nil