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

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

[elpa] externals/capf-autosuggest 96394ca 48/63: Simplify accept and sen


From: ELPA Syncer
Subject: [elpa] externals/capf-autosuggest 96394ca 48/63: Simplify accept and send menu-items
Date: Wed, 27 Oct 2021 14:58:02 -0400 (EDT)

branch: externals/capf-autosuggest
commit 96394ca05d2bbc11d08ffad4c471c4dd88671165
Author: jakanakaevangeli <jakanakaevangeli@chiru.no>
Commit: jakanakaevangeli <jakanakaevangeli@chiru.no>

    Simplify accept and send menu-items
---
 capf-autosuggest.el | 129 ++++++++++++++++++++++------------------------------
 1 file changed, 55 insertions(+), 74 deletions(-)

diff --git a/capf-autosuggest.el b/capf-autosuggest.el
index 4926119..4c3917e 100644
--- a/capf-autosuggest.el
+++ b/capf-autosuggest.el
@@ -344,30 +344,6 @@ in order to skip the history element already shown by the 
overlay."
   (eshell-previous-matching-input-from-input n)
   (setq this-command #'eshell-previous-matching-input-from-input))
 
-(defun capf-autosuggest-comint-send-input ()
-  "`capf-autosuggest-accept' and `comint-send-input'."
-  (interactive)
-  (capf-autosuggest-accept)
-  (call-interactively (or (command-remapping #'comint-send-input)
-                          #'comint-send-input))
-  (setq this-command #'comint-send-input))
-
-(defun capf-autosuggest-eshell-send-input ()
-  "`capf-autosuggest-accept' and `eshell-send-input'."
-  (interactive)
-  (capf-autosuggest-accept)
-  (call-interactively (or (command-remapping #'eshell-send-input)
-                          #'eshell-send-input))
-  (setq this-command #'eshell-send-input))
-
-(defun capf-autosuggest-minibuffer-send-input ()
-  "`capf-autosuggest-accept' and `exit-minibuffer'."
-  (interactive)
-  (capf-autosuggest-accept)
-  (call-interactively (or (command-remapping #'exit-minibuffer)
-                          #'exit-minibuffer))
-  (setq this-command #'exit-minibuffer))
-
 (defcustom capf-autosuggest-dwim-next-line t
   "Whether `next-line' can accept and send current suggestion.
 If t and point is on last line, `next-line' will accept the
@@ -395,6 +371,17 @@ If t and previous command wasn't a history matching command
 suggestion and send input."
   :type 'boolean)
 
+(defun capf-autosuggest--accept-and-remapping (cmd)
+  "Return a command that will accept input and run CMD."
+  ;; Avoid infinite recursion when searching for the command remapping
+  (let ((capf-autosuggest-active-mode nil))
+    (setq cmd (or (command-remapping cmd) cmd)))
+  (lambda ()
+    (interactive)
+    (capf-autosuggest-accept)
+    (setq this-command cmd)
+    (call-interactively cmd)))
+
 (defvar capf-autosuggest-active-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map [remap forward-word] #'capf-autosuggest-forward-word)
@@ -421,68 +408,62 @@ suggestion and send input."
       #'capf-autosuggest-comint-previous-matching-input-from-input)
 
     (define-key map [remap next-line]
-      (list 'menu-item "" #'next-line :filter
-            (lambda (cmd)
-              (if (and capf-autosuggest-dwim-next-line
-                       (looking-at-p "[^\n]*\n?\\'"))
-                  (cond
-                   ((derived-mode-p 'comint-mode) 
#'capf-autosuggest-comint-send-input)
-                   ((derived-mode-p 'eshell-mode) 
#'capf-autosuggest-eshell-send-input)
-                   ((minibufferp) #'capf-autosuggest-minibuffer-send-input)
-                   (t cmd))
-                cmd))))
+      (list 'menu-item "" nil :filter
+            (lambda (_cmd)
+              (and capf-autosuggest-dwim-next-line
+                   (looking-at-p "[^\n]*\n?\\'"))
+              (when-let*
+                  ((cmd (cond
+                         ((derived-mode-p 'comint-mode) #'comint-send-input)
+                         ((derived-mode-p 'eshell-mode) #'eshell-send-input)
+                         ((minibufferp) #'exit-minibuffer))))
+                (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap comint-next-prompt]
-      (list 'menu-item "" #'comint-next-prompt :filter
+      (list 'menu-item "" #'comint-send-input :filter
             (lambda (cmd)
-              (if (and capf-autosuggest-dwim-next-prompt
-                       (comint-after-pmark-p))
-                  #'capf-autosuggest-comint-send-input
-                cmd))))
+              (and capf-autosuggest-dwim-next-prompt
+                   (comint-after-pmark-p)
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap eshell-next-prompt]
-      (list 'menu-item "" #'eshell-next-prompt :filter
+      (list 'menu-item "" #'eshell-send-input :filter
             (lambda (cmd)
-              (if (and capf-autosuggest-dwim-next-prompt
-                       (>= (point) eshell-last-output-end))
-                  #'capf-autosuggest-eshell-send-input
-                cmd))))
+              (and capf-autosuggest-dwim-next-prompt
+                   (>= (point) eshell-last-output-end)
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap comint-next-input]
-      (list 'menu-item "" #'comint-next-input :filter
+      (list 'menu-item "" #'comint-send-input :filter
             (lambda (cmd)
-              (if (or (not capf-autosuggest-dwim-next-input)
-                      (memq last-command
-                            '(comint-next-matching-input-from-input
-                              comint-previous-matching-input-from-input
-                              comint-next-input comint-previous-input)))
-                  cmd
-                #'capf-autosuggest-comint-send-input))))
+              (and capf-autosuggest-dwim-next-input
+                   (not (memq last-command
+                              '(comint-next-matching-input-from-input
+                                comint-previous-matching-input-from-input
+                                comint-next-input comint-previous-input)))
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap eshell-next-input]
-      (list 'menu-item "" #'eshell-next-input :filter
+      (list 'menu-item "" #'eshell-send-input :filter
             (lambda (cmd)
-              (if (or (not capf-autosuggest-dwim-next-input)
-                      (memq last-command
-                            '(eshell-next-matching-input-from-input
-                              eshell-previous-matching-input-from-input
-                              eshell-next-input eshell-previous-input)))
-                  cmd
-                #'capf-autosuggest-eshell-send-input))))
+              (and capf-autosuggest-dwim-next-input
+                   (not (memq last-command
+                              '(eshell-next-matching-input-from-input
+                                eshell-previous-matching-input-from-input
+                                eshell-next-input eshell-previous-input)))
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap comint-next-matching-input-from-input]
-      (list 'menu-item "" #'comint-next-matching-input-from-input :filter
+      (list 'menu-item "" #'comint-send-input :filter
             (lambda (cmd)
-              (if (or (not 
capf-autosuggest-dwim-next-matching-input-from-input)
-                      (memq last-command
-                            '(comint-next-matching-input-from-input
-                              comint-previous-matching-input-from-input)))
-                  cmd
-                #'capf-autosuggest-comint-send-input))))
+              (and capf-autosuggest-dwim-next-matching-input-from-input
+                   (not (memq last-command
+                              '(comint-next-matching-input-from-input
+                                comint-previous-matching-input-from-input)))
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     (define-key map [remap eshell-next-matching-input-from-input]
-      (list 'menu-item "" #'eshell-next-matching-input-from-input :filter
+      (list 'menu-item "" #'eshell-send-input :filter
             (lambda (cmd)
-              (if (or (not 
capf-autosuggest-dwim-next-matching-input-from-input)
-                      (memq last-command
-                            '(eshell-previous-matching-input-from-input
-                              eshell-next-matching-input-from-input)))
-                  cmd
-                #'capf-autosuggest-eshell-send-input))))
+              (and capf-autosuggest-dwim-next-matching-input-from-input
+                   (not (memq last-command
+                              '(eshell-previous-matching-input-from-input
+                                eshell-next-matching-input-from-input)))
+                   (capf-autosuggest--accept-and-remapping cmd)))))
     map)
   "Keymap active when an auto-suggestion is shown.")
 



reply via email to

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