emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/detached 4e7cff1163 3/3: Update detach command


From: ELPA Syncer
Subject: [elpa] externals/detached 4e7cff1163 3/3: Update detach command
Date: Tue, 11 Oct 2022 12:57:30 -0400 (EDT)

branch: externals/detached
commit 4e7cff1163b1485295f549c0d1ae9cf49d126967
Author: Niklas Eklund <niklas.eklund@posteo.net>
Commit: Niklas Eklund <niklas.eklund@posteo.net>

    Update detach command
    
    Make a major-mode specific defmethod to detach from sessions in
    different modes. This becomes a simpler implementation and avoids the
    need of having to expose a detached-eshell function into detached.
---
 detached-compile.el |  5 +++++
 detached-eshell.el  |  8 +++++++
 detached-vterm.el   |  8 +++----
 detached.el         | 65 +++++++++++++++++++++++++++++------------------------
 4 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/detached-compile.el b/detached-compile.el
index f43f4d58b0..4d8df41ba9 100644
--- a/detached-compile.el
+++ b/detached-compile.el
@@ -148,6 +148,11 @@ Optionally EDIT-COMMAND."
       (when (re-search-forward (format "\n?%s\n" detached--dtach-eof-message) 
end t)
         (delete-region (match-beginning 0) (match-end 0))))))
 
+(cl-defmethod detached--detach-session ((_mode (derived-mode 
detached-compilation-mode)))
+  "Detach from session when MODE is `detached-compilation-mode'."
+  (detached--detach-from-comint-process)
+  (detached--quit-session-buffer))
+
 ;;;;; Major modes
 
 (defvar detached-compilation-mode-map
diff --git a/detached-eshell.el b/detached-eshell.el
index eb8b912389..a6cdd596c1 100644
--- a/detached-eshell.el
+++ b/detached-eshell.el
@@ -111,6 +111,14 @@ If prefix-argument directly DETACH from the session."
                        (seq-filter (lambda (it) (eq 'active 
(detached--determine-session-state it)))))))
     (detached-completing-read sessions)))
 
+(cl-defmethod detached--detach-session ((_mode (derived-mode eshell-mode)))
+  "Detach from session when MODE is `eshell-mode'."
+  (when-let ((active-session (eq 'active (detached--determine-session-state 
detached--buffer-session)))
+             (dtach-process (detached-eshell--get-dtach-process)))
+    (setq detached--buffer-session nil)
+    (process-send-string dtach-process
+                         detached--dtach-detach-character)))
+
 ;;;; Minor mode
 
 (defvar detached-eshell-mode-map
diff --git a/detached-vterm.el b/detached-vterm.el
index 99d2088d7b..5f343c32e1 100644
--- a/detached-vterm.el
+++ b/detached-vterm.el
@@ -32,6 +32,7 @@
 (declare-function vterm-send-C-e "vterm")
 (declare-function vterm-send-return "vterm")
 (declare-function vterm-end-of-line "vterm")
+(declare-function vterm-beginning-of-line "vterm")
 
 (defvar vterm--process)
 
@@ -81,9 +82,8 @@ Optionally DETACH from it."
     (process-send-string vterm--process (detached--shell-command session t))
     (vterm-send-return)))
 
-(defun detached-vterm-detach ()
-  "Detach from a `detached' session."
-  (interactive)
+(cl-defmethod detached--detach-session ((_mode (derived-mode vterm-mode)))
+  "Detach from session when MODE is `vterm-mode'."
   (process-send-string
    vterm--process
    detached--dtach-detach-character))
@@ -94,7 +94,7 @@ Optionally DETACH from it."
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "<S-return>") #'detached-vterm-send-input)
     (define-key map (kbd "<C-return>") #'detached-vterm-attach)
-    (define-key map (kbd detached-detach-key) #'detached-vterm-detach)
+    (define-key map (kbd detached-detach-key) #'detached-detach-session)
     map)
   "Keymap for `detached-vterm-mode'.")
 
diff --git a/detached.el b/detached.el
index d52a2a6d6a..c67a00c970 100644
--- a/detached.el
+++ b/detached.el
@@ -53,8 +53,6 @@
 (require 'subr-x)
 (require 'tramp)
 
-(declare-function detached-eshell--get-dtach-process "detached-eshell")
-
 ;;;; Variables
 
 ;;;;; Customizable
@@ -603,33 +601,8 @@ This command is only activated if 
`detached--buffer-session' is an
 active session.  For sessions created with `detached-compile' or
 `detached-shell-command', the command will also kill the window."
   (interactive)
-  (if (detached-session-p detached--buffer-session)
-      (if-let ((command-or-compile
-                (cond ((string-match detached--shell-command-buffer 
(buffer-name)) t)
-                      ((string-match "\*detached-compilation" (buffer-name)) t)
-                      ((eq major-mode 'detached-log-mode) t)
-                      (t nil))))
-          ;; `detached-shell-command' or `detached-compile'
-          (let ((kill-buffer-query-functions nil))
-            (when-let ((process (get-buffer-process (current-buffer))))
-              (comint-simple-send process detached--dtach-detach-character)
-              (message "[detached]"))
-            (setq detached--buffer-session nil)
-            (if (= (length (window-list)) 1)
-                (kill-buffer)
-              (kill-buffer-and-window)))
-        (if (eq 'active (detached--determine-session-state 
detached--buffer-session))
-            ;; `detached-eshell'
-            (if-let ((process (and (eq major-mode 'eshell-mode)
-                                   (detached-eshell--get-dtach-process))))
-                (progn
-                  (setq detached--buffer-session nil)
-                  (process-send-string process 
detached--dtach-detach-character))
-              ;; `detached-shell'
-              (let ((process (get-buffer-process (current-buffer))))
-                (comint-simple-send process detached--dtach-detach-character)
-                (setq detached--buffer-session nil)))
-          (message "No active detached-session found in buffer.")))
+  (if-let ((has-session (detached-session-p detached--buffer-session)))
+      (detached--detach-session major-mode)
     (message "No detached-session found in buffer.")))
 
 ;;;###autoload
@@ -1217,6 +1190,40 @@ Optionally make the path LOCAL to host."
       detached-session-directory
     (concat (file-remote-p default-directory) detached-session-directory)))
 
+(cl-defgeneric detached--detach-session (_mode)
+  "Default implementation."
+  (message "`detached' doesn't know how to detach from a session in this 
mode"))
+
+(cl-defmethod detached--detach-session ((_mode (derived-mode 
detached-log-mode)))
+  "Detach from session when MODE is `detached-log-mode'."
+  (detached--quit-session-buffer))
+
+(cl-defmethod detached--detach-session ((_mode (derived-mode shell-mode)))
+  "Detach from session when MODE is `shell-mode'."
+  (detached--detach-from-comint-process)
+  (when (string-match detached--shell-command-buffer (buffer-name))
+    (detached--quit-session-buffer)))
+
+(cl-defmethod detached--detach-session ((_mode (derived-mode comint-mode)))
+  "Detach from session when MODE is `comint-mode'."
+  (detached--detach-from-comint-process)
+  (detached--quit-session-buffer))
+
+(defun detached--detach-from-comint-process ()
+  "Detach from the underlying `comint' process."
+  (when-let ((active-session (eq 'active (detached--determine-session-state 
detached--buffer-session)))
+             (dtach-process (get-buffer-process (current-buffer))))
+    (setq detached--buffer-session nil)
+    (comint-simple-send dtach-process detached--dtach-detach-character)))
+
+(defun detached--quit-session-buffer ()
+  "Quit session buffer."
+  (message "[detached]")
+  (let ((kill-buffer-query-functions nil))
+    (if (= (length (window-list)) 1)
+        (kill-buffer)
+      (kill-buffer-and-window))))
+
 ;;;;; Database
 
 (defun detached--db-initialize ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]