From 1c1272337c0ed44245e738fa814519177cb16284 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 1 Jun 2022 19:44:51 +0200 Subject: [PATCH] Adding styles and a basic prompt, tho local markers are not getting set --- mattermost.el | 120 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 101 insertions(+), 19 deletions(-) diff --git a/mattermost.el b/mattermost.el index fcbbaea..53d2c3b 100644 --- a/mattermost.el +++ b/mattermost.el @@ -147,6 +147,15 @@ if the user is not present" (let ((url (format "/channels/%s/posts" channel-id))) (mattermost-request "GET" url))) +(define-button-type 'mattermost-channel + 'face 'default + 'read-only t + 'rear-sticky t + 'front-sticky t + 'action #'(lambda (b) + (let* ((channel (button-get b 'channel))) + (mattermost-show-channel channel)))) + (defun mattermost-insert-channel (channel) "Inserts a channel button in the current buffer" (let* ((channel-display-name (plist-get channel :display_name)) @@ -155,13 +164,8 @@ if the user is not present" channel-name channel-display-name))) (insert-text-button (format "> %s\n" name) - 'action #'(lambda (b) - (let* ((channel (button-get b 'channel))) - (mattermost-show-channel channel))) - 'channel channel - 'read-only t - 'rear-sticky t - 'front-sticky t))) + :type 'mattermost-channel + 'channel channel))) ;; ToDo: when root buffer is closed, disconnect (defun mattermost-show-root () @@ -196,25 +200,96 @@ if the user is not present" "Mode to list the Mattermost teams and channels to allow the user to check their status and select between them") +(defvar mattermost-username-button-map + (let ((map (make-sparse-keymap))) + (set-keymap-parent map button-map) + (define-key map (kbd "n") #'next-line) + (define-key map (kbd "p") #'previous-line) + map)) + +(define-button-type 'mattermost-username + 'face 'bold + 'read-only t + 'rear-sticky t + 'front-sticky t + 'keymap mattermost-username-button-map + 'action #'(lambda (b) + (let* ((user (button-get b 'user))) + (message "action for user %s!" (plist-get user :username))))) + +(defvar mattermost-post-button-map + (let ((map (make-sparse-keymap))) + (set-keymap-parent map button-map) + (define-key map (kbd "n") #'next-line) + (define-key map (kbd "p") #'previous-line) + map)) + +(define-button-type 'mattermost-post + 'face 'default + 'read-only t + 'rear-sticky t + 'front-sticky t + 'keymap mattermost-post-button-map + 'action #'(lambda (b) + (let* ((post (button-get b 'post))) + (message "action for post %s!" (plist-get post :id))))) + (defun mattermost-insert-post (post) "Inserts a post message in the current buffer" (let* ((user-id (plist-get post :user_id)) (user (mattermost--get-user user-id)) (username (plist-get user :username)) (msg (plist-get post :message))) - (insert-text-button (format "[%s] %s\n" username msg) - 'action #'(lambda (b) - (message "button action! %s" (button-get b 'post))) - 'post post - 'read-only t - 'rear-sticky t - 'front-sticky t))) + (insert-text-button (format "[%s]" username) + :type 'mattermost-username + 'user user) + (insert-text-button (format " %s\n" msg) + :type 'mattermost-post + 'post post))) + +(defun mattermost-prompt () + "Shows the message prompt" + (setq mattermost-prompt-marker (make-marker) + mattermost-insert-marker (make-marker)) + (end-of-buffer) + (set-marker mattermost-insert-marker (point)) + (setq prompt (propertize ">> " + 'rear-nonsticky t + 'field t + 'front-sticky t + 'read-only t)) + (put-text-property 0 (1- (length prompt)) + 'font-lock-face 'bold + prompt) + (insert prompt) + (set-marker mattermost-prompt-marker (point))) + +(defun mattermost-channel-ret () + "Sends the post if on the input field or the button action + otherwise" + (interactive) + (if (> (point) mattermost-prompt-marker) + (let ((inhibit-read-only t) + (msg (field-string-no-properties (point)))) + (message "Sending: %s" msg) + (delete-field (point))))) + +(defvar mattermost-channel-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-m") 'mattermost-channel-ret) + map) + "The keymap for mattermost-channel-mode") + +(define-derived-mode mattermost-channel-mode fundamental-mode "Mattermost Channel" + "Mode use on a Mattermost channel buffer, that shows the + messages of the channel and allows the user to post theirs") ;; ToDo: should we receive just a channel id and fetch the channel ;; data? (defun mattermost-show-channel (channel) "Populates the channel buffer with posts and the prompt" - (let* ((id (plist-get channel :id)) + (let* ((inhibit-read-only t) + (id (plist-get channel :id)) (display-name (plist-get channel :display_name)) (name (plist-get channel :name)) (cname (if (string-empty-p display-name) name display-name)) @@ -225,15 +300,22 @@ user to check their status and select between them") (with-current-buffer chanb (erase-buffer) - (let* ((inhibit-read-only t) - (resp (mattermost-get-channel-messages id)) + + ;; ToDo: is this the place to define the variables? + (defvar-local mattermost-prompt-marker nil + "The marker that shows where the prompt starts") + (defvar-local mattermost-insert-marker nil + "The marker that shows where a new message should be inserted") + + (let* ((resp (mattermost-get-channel-messages id)) (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)))) - (mattermost-insert-post post))))) + (mattermost-insert-post post)))) + (mattermost-prompt) + (mattermost-channel-mode)) (switch-to-buffer chanb) - ;; ToDo: here enable mode (end-of-buffer))) (defun mattermost ()