Maintain a list of buffers

This commit is contained in:
Miguel de la Cruz 2022-06-03 19:22:36 +02:00
parent afc79685b8
commit 635e95107d

View file

@ -40,6 +40,9 @@
(defvar mattermost-websocket nil (defvar mattermost-websocket nil
"The websocket connected to the server") "The websocket connected to the server")
(defvar mattermost-buffers '()
"Plist with all the open channel buffers")
(defvar-local mattermost-prompt-marker nil (defvar-local mattermost-prompt-marker nil
"The marker that shows where the prompt starts") "The marker that shows where the prompt starts")
@ -49,6 +52,9 @@
(defvar-local mattermost-channel-id nil (defvar-local mattermost-channel-id nil
"The buffer channel ID") "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 ;; ToDo: probably not the best way to get a keyword from a string
(defun mattermost-string->keyword (str) (defun mattermost-string->keyword (str)
"Returns a keyword from a string" "Returns a keyword from a string"
@ -132,7 +138,7 @@ conform to a post plist"
(websocket-send-text ws (mattermost--get-auth-challenge))) (websocket-send-text ws (mattermost--get-auth-challenge)))
:on-message 'mattermost--process-ws-frame :on-message 'mattermost--process-ws-frame
:on-close (lambda (ws) :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 ;; 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 ;; 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 () (defun mattermost-show-root ()
"Populates the Mattermost Root buffer and changes to it" "Populates the Mattermost Root buffer and changes to it"
(interactive) (interactive)
(let ((rootb (get-buffer-create "*Mattermost Root*"))) (let ((rootb (get-buffer-create mattermost-root-buffer-name)))
(with-current-buffer rootb (with-current-buffer rootb
(let ((inhibit-read-only t) (let ((inhibit-read-only t)
(teams (mattermost-get-teams))) (teams (mattermost-get-teams)))
@ -352,9 +358,11 @@ user to check their status and select between them")
(order (seq-reverse (plist-get resp :order))) (order (seq-reverse (plist-get resp :order)))
(posts (plist-get resp :posts))) (posts (plist-get resp :posts)))
(dolist (post-id order) (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-insert-post post))))
(mattermost-channel-mode)) (mattermost-channel-mode))
(setq mattermost-buffers
(plist-put mattermost-buffers (mattermost-string->keyword id) chanb))
(switch-to-buffer chanb) (switch-to-buffer chanb)
(setq mattermost-channel-id id) (setq mattermost-channel-id id)
(setq mattermost-prompt-marker (make-marker)) (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-login mattermost-login-id password))
(mattermost-show-root)) (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 () (defun mattermost-disconnect ()
"Closes the connection with the Mattermost server" "Closes the connection with the Mattermost server"
(interactive) (interactive)
(mattermost--kill-all-buffers)
(unless (null mattermost-websocket) (unless (null mattermost-websocket)
(websocket-close mattermost-websocket)) (websocket-close mattermost-websocket))
(setq mattermost-token nil (setq mattermost-token nil