branch: elpa/jabber
commit 1bfd1754a6d2ce6e884c1f6a6f45934a07b9320e
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    input: Separate shared buffer state
---
 lisp/jabber-chatbuffer.el       | 29 +-------------------------
 lisp/jabber-console.el          | 14 +------------
 lisp/jabber-input.el            | 45 +++++++++++++++++++++++++++++++++++++++++
 tests/jabber-test-chatbuffer.el | 34 +++++++++++++++++++++++++++++++
 4 files changed, 81 insertions(+), 41 deletions(-)

diff --git a/lisp/jabber-chatbuffer.el b/lisp/jabber-chatbuffer.el
index 90f9a519f8..7b4a0f9caa 100644
--- a/lisp/jabber-chatbuffer.el
+++ b/lisp/jabber-chatbuffer.el
@@ -27,14 +27,12 @@
 ;;; Code:
 
 (require 'jabber-util)
+(require 'jabber-input)
 (require 'jabber-core)
 (require 'jabber-db)
 (require 'jabber-muc-protocol)
 (require 'help-at-pt)
 
-(defvar jabber-point-insert nil
-  "Position where the message being composed starts.")
-
 (defcustom jabber-scrolltobottom-all nil
   "Non-nil means explicit input recentering affects all chat windows.
 When `jabber-chat-buffer-recenter-input' is called, recenter all
@@ -81,9 +79,6 @@ windows."
   "Return the last of VALUES without scroll side effects."
   (car (last values)))
 
-(defvar jabber-send-function nil
-  "Function for sending a message from a chat buffer.")
-
 (defvar jabber-chat-mode-hook nil
   "Hook called at the end of `jabber-chat-mode'.
 Note that functions in this hook have no way of knowing
@@ -106,9 +101,6 @@ previous sequence detect the mismatch and stop.")
 
 (declare-function jabber-muc-nick-completion-at-point 
"jabber-muc-nick-completion.el" ())
 
-(defvar-local jabber-buffer-connection nil
-  "The connection used by this buffer.")
-
 (defvar jabber-chatting-with)              ; jabber-chat.el
 (defvar jabber-chat-header-line-format)   ; jabber-chat.el
 (defvar jabber-chat-earliest-backlog)     ; jabber-chat.el
@@ -373,25 +365,6 @@ EWOC-PP is the pretty-printer function for the message 
EWOC."
                   (buffer entries callback &optional generation))
 (declare-function jabber-chat-display-buffer-images "jabber-chat" ())
 
-(defun jabber-chat-buffer-send ()
-  "Send the message composed below the prompt in the current chat buffer."
-  (interactive)
-  ;; If user accidentally hits RET without writing anything, just
-  ;; ignore it.
-  (when (cl-plusp (- (point-max) jabber-point-insert))
-    ;; If connection was lost...
-    (unless (memq jabber-buffer-connection jabber-connections)
-      ;; ...maybe there is a new connection to the same account.
-      (let ((new-jc (jabber-find-active-connection jabber-buffer-connection)))
-       (if new-jc
-           ;; If so, just use it.
-           (setq jabber-buffer-connection new-jc)
-         ;; Otherwise, ask for a new account.
-         (setq jabber-buffer-connection (jabber-read-account t)))))
-
-    (let ((body (delete-and-extract-region jabber-point-insert (point-max))))
-      (funcall jabber-send-function jabber-buffer-connection body))))
-
 (defun jabber-chat-buffer-switch ()
   "Switch to a specified jabber chat buffer."
   (interactive)
diff --git a/lisp/jabber-console.el b/lisp/jabber-console.el
index 5a1b0ca2e8..2d3806a618 100644
--- a/lisp/jabber-console.el
+++ b/lisp/jabber-console.el
@@ -26,6 +26,7 @@
 ;;; Code:
 
 (require 'jabber-keymap)
+(require 'jabber-input)
 (require 'jabber-util)
 (require 'jabber-truncate)
 (require 'xml)
@@ -51,12 +52,6 @@ Not truncate if set to 0."
   :type 'integer
   :group 'jabber-debug)
 
-(defvar jabber-point-insert nil
-  "Position where the message being composed starts.")
-
-(defvar jabber-send-function nil
-  "Function for sending a message from a chat buffer.")
-
 (defvar jabber-console-mode-hook nil
   "Hook called at the end of `jabber-console-mode'.
 Note that functions in this hook have no way of knowing
@@ -69,13 +64,6 @@ what kind of chat buffer is being created.")
   :parent jabber-common-keymap
   "RET" #'jabber-chat-buffer-send)
 
-;; Global reference declarations
-
-(declare-function jabber-chat-buffer-send "jabber-chatbuffer.el"  ())
-(defvar jabber-buffer-connection)       ; jabber-chatbuffer.el
-
-;;
-
 (defun jabber-console-create-buffer (jc)
   "Get or create the XMPP console buffer for connection JC."
   (with-current-buffer
diff --git a/lisp/jabber-input.el b/lisp/jabber-input.el
new file mode 100644
index 0000000000..c2ac8e2c21
--- /dev/null
+++ b/lisp/jabber-input.el
@@ -0,0 +1,45 @@
+;;; jabber-input.el --- Shared Jabber input buffer support  -*- 
lexical-binding: t; -*-
+
+;; Copyright (C) 2026  Thanos Apollo
+
+;; Maintainer: Thanos Apollo <[email protected]>
+
+;; This file is a part of jabber.el.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;;; Commentary:
+
+;; State and sending behavior shared by chat and XML console input buffers.
+
+;;; Code:
+
+(require 'cl-lib)
+(require 'jabber-util)
+
+(defvar jabber-point-insert nil
+  "Position where the message being composed starts.")
+
+(defvar jabber-send-function nil
+  "Function for sending a message from a Jabber input buffer.")
+
+(defvar-local jabber-buffer-connection nil
+  "Jabber connection associated with the current buffer.")
+
+(defun jabber-chat-buffer-send ()
+  "Send the input composed below the prompt in the current buffer."
+  (interactive)
+  (when (cl-plusp (- (point-max) jabber-point-insert))
+    (unless (memq jabber-buffer-connection jabber-connections)
+      (setq jabber-buffer-connection
+            (or (jabber-find-active-connection jabber-buffer-connection)
+                (jabber-read-account t))))
+    (let ((body (delete-and-extract-region jabber-point-insert (point-max))))
+      (funcall jabber-send-function jabber-buffer-connection body))))
+
+(provide 'jabber-input)
+
+;;; jabber-input.el ends here
diff --git a/tests/jabber-test-chatbuffer.el b/tests/jabber-test-chatbuffer.el
index 53c0efed4f..6d60851173 100644
--- a/tests/jabber-test-chatbuffer.el
+++ b/tests/jabber-test-chatbuffer.el
@@ -1043,6 +1043,40 @@ forced to the bottom (the only path that overwrites 
point)."
       (jabber-chat-mode)
       (should-not (local-variable-p 'help-at-pt-display-when-idle)))))
 
+;;; Group 14: shared input sending
+
+(ert-deftest jabber-test-input-send-extracts-body ()
+  "Send and remove input below the prompt marker."
+  (with-temp-buffer
+    (insert "Prompt: hello")
+    (let ((jabber-connections '(connection))
+          (sent nil))
+      (setq-local jabber-buffer-connection 'connection)
+      (setq-local jabber-point-insert (copy-marker 9))
+      (setq-local jabber-send-function
+                  (lambda (jc body) (setq sent (cons jc body))))
+      (jabber-chat-buffer-send)
+      (should (equal sent '(connection . "hello")))
+      (should (string= (buffer-string) "Prompt: ")))))
+
+(ert-deftest jabber-test-input-send-reuses-active-connection ()
+  "Replace a stale connection before sending input."
+  (with-temp-buffer
+    (insert "hello")
+    (let ((jabber-connections '(new))
+          (sent nil))
+      (setq-local jabber-buffer-connection 'old)
+      (setq-local jabber-point-insert (copy-marker (point-min)))
+      (setq-local jabber-send-function
+                  (lambda (jc body) (setq sent (cons jc body))))
+      (cl-letf (((symbol-function 'jabber-find-active-connection)
+                 (lambda (_jc) 'new))
+                ((symbol-function 'jabber-read-account)
+                 (lambda (&rest _) (ert-fail "Prompted for an account"))))
+        (jabber-chat-buffer-send))
+      (should (equal sent '(new . "hello")))
+      (should (eq jabber-buffer-connection 'new)))))
+
 (provide 'jabber-test-chatbuffer)
 
 ;;; jabber-test-chatbuffer.el ends here

Reply via email to