fix(ui): draft-local keymap and correct initial cursor placement

- Toggle overriding-local-map via post-command-hook: while point is in
  the draft, a global-map-parented keymap shadows mode hotkeys (g, h,
  q, SPC, DEL) so any text can be typed; RET/C-c C-c send
- Treat missing window as "at bottom" during first render and force
  point to the prompt after switch-to-buffer
This commit is contained in:
Ivan I. Ovchinnikov
2026-11-21 15:04:17 +03:00
parent d33edb4e44
commit ca973ac530
+168 -68
View File
@@ -2,20 +2,40 @@
(require 'mattermost-custom)
(require 'mattermost-protocol)
(require 'mattermost-files)
(defvar mattermost--draft-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map global-map)
(define-key map (kbd "RET") #'mattermost-channel-send-message)
(define-key map (kbd "C-c C-c") #'mattermost-channel-send-message)
(define-key map (kbd "C-c C-s") #'mattermost-channel-send-message)
map)
"Keymap active while point is inside the draft.
Parent is `global-map', so printable characters self-insert and motion
keys work; mode-specific single-key bindings (g, h, q, ...) are shadowed.")
(defvar mattermost-channel-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'mattermost-channel-ret)
(define-key map (kbd "C-c C-c") #'mattermost-channel-send-message)
(define-key map (kbd "C-c C-s") #'mattermost-channel-send-message)
(define-key map (kbd "g") #'mattermost-channel-refresh)
(define-key map (kbd "RET") #'mattermost-channel-open-link)
map)
"Keymap for `mattermost-channel-mode`.")
"Keymap for `mattermost-channel-mode' (history area).")
(define-derived-mode mattermost-channel-mode special-mode "Mattermost-Channel"
"Major mode for viewing a Mattermost channel.
"Major mode for viewing and writing in a Mattermost channel.
History above the \"> \" prompt is protected by a local before-change
hook; the draft after the prompt is editable and pinned to the bottom.
While point is in the draft, `mattermost--draft-map' overrides mode
bindings so any text can be typed.
\\{mattermost-channel-mode-map}"
(setq buffer-read-only t)
(setq truncate-lines nil))
(setq buffer-read-only nil)
(setq truncate-lines nil)
(add-hook 'before-change-functions #'mattermost--protect-history nil t)
(add-hook 'window-scroll-functions #'mattermost--clamp-window nil t)
(add-hook 'post-command-hook #'mattermost--update-draft-keymap nil t))
(defvar-local mattermost--channel-id nil
"Channel ID for the current buffer.")
@@ -26,6 +46,48 @@
(defvar-local mattermost--channel-name nil
"Display name of the current channel.")
(defvar-local mattermost--posts nil
"List of post plists currently displayed, oldest first.")
(defvar-local mattermost--input-text ""
"Draft message text kept across re-renders.")
(defvar-local mattermost--input-start nil
"Buffer position where the editable draft begins.")
(defvar-local mattermost--rendering nil
"Non-nil while the buffer is re-rendered programmatically.")
(defvar-local mattermost--sending nil
"Non-nil while a send_post request is in flight.")
(defun mattermost--protect-history (beg _end)
"Forbid modifications starting in the history area (before the draft)."
(unless mattermost--rendering
(when (and mattermost--input-start (< beg mattermost--input-start))
(user-error "History is read-only"))))
(defun mattermost--clamp-window (win _start)
"Prevent scrolling below the draft prompt line."
(when (and mattermost--input-start (not mattermost--rendering))
(let* ((height (window-body-height win))
(max-start (save-excursion
(goto-char mattermost--input-start)
(when (> (forward-line (- 1 height)) 0)
(goto-char (point-min)))
(point))))
(when (> (window-start win) max-start)
(set-window-start win max-start t)))))
(defun mattermost--in-draft-p ()
"Return non-nil when point is inside the editable draft."
(and mattermost--input-start (>= (point) mattermost--input-start)))
(defun mattermost--update-draft-keymap ()
"Enable the draft keymap while point is in the draft, disable otherwise."
(setq overriding-local-map
(if (mattermost--in-draft-p) mattermost--draft-map nil)))
(defun mattermost-channel-open (server-id channel-id channel-name)
"Open a buffer for CHANNEL-ID on SERVER-ID with CHANNEL-NAME."
(let ((buf-name (format "*MM: %s*" channel-name)))
@@ -35,14 +97,24 @@
(setq-local mattermost--server-id server-id)
(setq-local mattermost--channel-id channel-id)
(setq-local mattermost--channel-name channel-name)
(setq-local mattermost--posts nil)
(setq-local mattermost--input-text "")
(setq-local mattermost--input-start nil)
(mattermost-channel-load-history))
(switch-to-buffer buf))))
(switch-to-buffer buf)
(with-current-buffer buf
(goto-char (point-max))
(when-let ((win (get-buffer-window buf)))
(set-window-point win (point-max))
(with-selected-window win
(recenter -1))))
buf)))
(defun mattermost-channel-load-history ()
"Load and display message history for the current channel."
"Load message history for the current channel into `mattermost--posts'."
(let ((server-id mattermost--server-id)
(channel-id mattermost--channel-id)
(channel-name mattermost--channel-name))
(buf (current-buffer)))
(message "[MM-DEBUG] Loading history for channel %s..." channel-id)
(mattermost--send-request
"get_history"
@@ -50,26 +122,23 @@
:channel_id channel-id
:limit 50)
(lambda (response)
(if (plist-get response :ok)
(mattermost--render-channel-history
channel-name
(plist-get (plist-get response :result) :posts))
(message "[MM] Failed to load history: %S" (plist-get response :error)))))))
(defun mattermost--format-file-size (bytes)
"Format BYTES as human-readable size."
(cond
((< bytes 1024) (format "%d B" bytes))
((< bytes (* 1024 1024)) (format "%.1f KB" (/ bytes 1024.0)))
((< bytes (* 1024 1024 1024)) (format "%.1f MB" (/ bytes (* 1024.0 1024.0))))
(t (format "%.1f GB" (/ bytes (* 1024.0 1024.0 1024.0))))))
(when (buffer-live-p buf)
(with-current-buffer buf
(if (plist-get response :ok)
(progn
(setq mattermost--posts
(plist-get (plist-get response :result) :posts))
(mattermost--render-channel-buffer))
(message "[MM] Failed to load history: %S"
(plist-get response :error)))))))))
(defun mattermost--format-post (post)
"Format a POST plist for display with multi-line support, bold sender, and files."
"Format a POST plist: timestamp, bold sender, message, file block."
(let* ((msg-text (or (plist-get post :message) ""))
(create-at (plist-get post :create_at))
(time-str (if create-at
(format-time-string "%Y-%m-%d %H:%M" (seconds-to-time (/ create-at 1000)))
(format-time-string "%Y-%m-%d %H:%M"
(seconds-to-time (/ create-at 1000)))
"????-??-?? ??:??"))
(sender (or (plist-get post :user_display_name)
(plist-get post :user_id)
@@ -83,66 +152,102 @@
(propertize sender 'face 'mattermost-sender-face)
(format ":**\n%s"
(if (string-empty-p msg-text) "" (concat msg-text "\n")))
(when files
(mapconcat
(lambda (f)
(let* ((name (or (plist-get f :name) "unknown"))
(size (or (plist-get f :size) 0))
(id (plist-get f :id)))
(propertize
(format " 📎 %s (%s)\n" name (mattermost--format-file-size size))
'mm-file-id id
'mm-file-name name
'mouse-face 'highlight
'help-echo "RET to download and open")))
files
""))
(when files (mattermost--format-file-block files))
"\n")))
(defun mattermost--render-channel-history (channel-name posts)
"Render POSTS in the current channel buffer with CHANNEL-NAME."
(let ((inhibit-read-only t))
(defun mattermost--render-channel-buffer (&optional new-draft)
"Re-render history, prompt and draft; pin prompt to the window bottom.
When NEW-DRAFT is non-nil, replace the draft with it instead of
recapturing from the buffer. If the user was reading history, the
window view is left undisturbed."
(let* ((mattermost--rendering t)
(win (get-buffer-window (current-buffer)))
(old-point (point))
(old-window-start (when win (window-start win)))
(was-in-draft (and mattermost--input-start
(>= old-point mattermost--input-start)))
(was-at-bottom (or (null win)
(pos-visible-in-window-p (point-max) win)))
(draft-offset nil))
(when new-draft
(setq mattermost--input-text new-draft))
(when (and (null new-draft)
mattermost--input-start
(>= (point-max) mattermost--input-start))
(setq mattermost--input-text
(buffer-substring-no-properties mattermost--input-start (point-max)))
(when was-in-draft
(setq draft-offset (- old-point mattermost--input-start))))
(erase-buffer)
(insert (format "Channel: %s\n" channel-name))
(insert (format "Channel: %s\n" mattermost--channel-name))
(insert "========================================\n\n")
(dolist (post posts)
(dolist (post mattermost--posts)
(insert (mattermost--format-post post))
(insert "\n"))
(goto-char (point-max))))
(insert (propertize "> " 'face 'mattermost-sender-face))
(setq mattermost--input-start (point))
(insert mattermost--input-text)
(cond
((or was-in-draft was-at-bottom)
(goto-char (if draft-offset
(min (+ mattermost--input-start draft-offset) (point-max))
(point-max)))
(when win
(set-window-point win (point))
(with-selected-window win
(recenter -1))))
(t
(goto-char (min old-point (max 1 (1- mattermost--input-start))))
(when win
(set-window-start win old-window-start t)
(set-window-point win (point)))))))
(defun mattermost-channel-refresh ()
"Reload message history for the current channel."
"Reload message history, keeping the current draft."
(interactive)
(message "[MM-DEBUG] Refreshing channel...")
(mattermost-channel-load-history))
(defun mattermost-channel-send-message ()
"Send a message to the current channel.
The message will appear automatically via WebSocket fan-out; no refresh needed."
(defun mattermost-channel-ret ()
"In the draft send the message; in history open the file link."
(interactive)
(if (mattermost--in-draft-p)
(mattermost-channel-send-message)
(mattermost-channel-open-link)))
(defun mattermost-channel-send-message ()
"Send the draft text to the current channel and clear the draft."
(interactive)
(when mattermost--sending
(user-error "Message send already in progress"))
(let* ((server-id mattermost--server-id)
(channel-id mattermost--channel-id)
(msg (read-string "Message: ")))
(when (string-empty-p msg)
(error "Message cannot be empty"))
(message "[MM-DEBUG] Sending message to channel %s..." channel-id)
(buf (current-buffer))
(msg (if (and mattermost--input-start
(>= (point-max) mattermost--input-start))
(buffer-substring-no-properties
mattermost--input-start (point-max))
mattermost--input-text)))
(when (string-empty-p (string-trim msg))
(user-error "Message is empty"))
(setq mattermost--sending t)
(mattermost--render-channel-buffer "")
(mattermost--send-request
"send_post"
(list :server_id server-id
:channel_id channel-id
:message msg)
(lambda (response)
(if (plist-get response :ok)
(message "[MM] Message sent. Waiting for WS echo...")
(message "[MM] Failed to send message: %S" (plist-get response :error)))))))
(when (buffer-live-p buf)
(with-current-buffer buf
(setq mattermost--sending nil)
(unless (plist-get response :ok)
(mattermost--render-channel-buffer msg)
(message "[MM] Failed to send message: %S"
(plist-get response :error)))))))))
(defun mattermost-channel-open-link ()
"Open the link under point.
If point is on a file attachment, download it and open with xdg-open.
Otherwise report that there is no openable link."
"Download the file attachment under point and open it with xdg-open."
(interactive)
(let ((file-id (get-text-property (point) 'mm-file-id))
(file-name (get-text-property (point) 'mm-file-name)))
@@ -164,7 +269,6 @@ Otherwise report that there is no openable link."
(let* ((result (plist-get response :result))
(local-path (plist-get result :local_path)))
(message "[MM] File saved to: %s" local-path)
;; Открываем через xdg-open (системное приложение по умолчанию)
(start-process "xdg-open" nil "xdg-open" local-path))
(message "[MM] Failed to download file: %S"
(plist-get response :error)))))))
@@ -172,18 +276,14 @@ Otherwise report that there is no openable link."
(message "[MM] No downloadable link under point.")))))
(defun mattermost-channel-on-posted (params)
"Handle a 'posted' event. PARAMS should contain the 'post' plist."
"Handle a 'posted' event: append the post and re-render, keeping the draft."
(let* ((post (plist-get params :post))
(channel-id (plist-get post :channel_id)))
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (and (eq major-mode 'mattermost-channel-mode)
(string= mattermost--channel-id channel-id))
(let ((inhibit-read-only t))
(goto-char (point-max))
(insert (mattermost--format-post post))
(insert "\n")
(when (= (point) (point-max))
(goto-char (point-max)))))))))
(setq mattermost--posts (append mattermost--posts (list post)))
(mattermost--render-channel-buffer))))))
(provide 'mattermost-channel)