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

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

[nongnu] elpa/vcomplete 07f3483c0c 09/91: Improve usability of 'vcomplet


From: ELPA Syncer
Subject: [nongnu] elpa/vcomplete 07f3483c0c 09/91: Improve usability of 'vcomplete-kill-buffer'
Date: Tue, 24 May 2022 15:59:05 -0400 (EDT)

branch: elpa/vcomplete
commit 07f3483c0c866aa57a180494cc4b0464e955df5f
Author: Daniel Semyonov <cmstr@dsemy.com>
Commit: Daniel Semyonov <cmstr@dsemy.com>

    Improve usability of 'vcomplete-kill-buffer'
    
    Also, lay a more robust framework for future completion commands.
    
    * vcomplete.el (vcomplete-current-completion-string): Rename from
    'vcomplete-current-completion'.
    (vcomplete-current-completion-index): New variable which holds the
    index in the completion list of the current completion.
    (vcomplete--move-n-completions): Add step to update
    'vcomplete-current-completion-index'.  Change reference to
    'vcomplete-current-completion' to 'vcomplete-current-completion-string'.
    (vcomplete-kill-buffer-1): New function which performs the actual
    killing of buffers.
    (vcomplete-kill-buffer): Only allow killing buffers when invoked from
    the minibuffer.  Only prompt for confirmation when the buffer has been
    modified.  Move to the next completion in the list after killing the
    buffer.
    (vcomplete--update-in-minibuffer, vcomplete--update-in-region): Reset
    'vcomplete-current-completion-index' before updating the list.
    (vcomplete--reset-vars): New function which resets variables used by
    'vcomplete-mode' to their original values.
    (vcomplete--setup): Call 'vcomplete--reset-vars' when exiting
    'completion-in-region-mode' (instead of removing a hook manually).
    (vcomplete-mode): Hook 'vcomplete--reset-vars' to run when exiting the
    minibuffer.
---
 NEWS           |  5 +++++
 vcomplete.el   | 50 +++++++++++++++++++++++++++++++++++++++-----------
 vcomplete.texi | 10 ++++++++++
 3 files changed, 54 insertions(+), 11 deletions(-)

diff --git a/NEWS b/NEWS
index 957431c701..7d900ff65d 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,11 @@ See vcomplete-auto-update's documentation string for more 
information.
 ** Fix vcomplete--setup being removed from a wrong hook when
 vcomplete-mode is turned off.
 
+** Improve usability of vcomplete-kill-buffer.
+Killing a buffer using it will now cause the next completion in the
+list to be highlighted (or the previous one if it's the last
+completion in the list).
+
 * 0.1
 
 ** Initial version.
diff --git a/vcomplete.el b/vcomplete.el
index da3be65b65..75d71a674e 100644
--- a/vcomplete.el
+++ b/vcomplete.el
@@ -104,10 +104,11 @@ While evaluating body, BUFFER and WINDOW are locally 
bound to the
           "The ‘*Completions*’ buffer is set to an incorrect mode"))
        ,@body)))
 
-(defvar vcomplete-current-completion nil
-  "Currently selected completion.
-Note that this variable is never cleared, so it can be used to get the
-last selected completion after completion has ended.")
+(defvar vcomplete-current-completion-string nil
+  "Currently selected completion string.")
+
+(defvar vcomplete-current-completion-index 0
+  "Index (in the ‘*Completions*’ buffer) of the current completion.")
 
 (defvar vcomplete--last-completion-overlay nil
   "Last overlay created in the ‘*Completions*’ buffer.")
@@ -117,6 +118,8 @@ last selected completion after completion has ended.")
 The completion selected is marked with an overlay."
   (vcomplete-with-completions-buffer
     (next-completion n)
+    (setq vcomplete-current-completion-index
+          (+ n vcomplete-current-completion-index))
     (when (= (point) (point-max)) (next-completion -1))
     (let ((beg (set-window-point window (point))) end)
       (unless (or (= beg (point-min))
@@ -126,7 +129,7 @@ The completion selected is marked with an overlay."
             (?\t (setq end (- (point) 2)))
             (?\n (setq end (- (point) 1)))
             (_ (setq end (point)))))
-        (setq vcomplete-current-completion
+        (setq vcomplete-current-completion-string
               (buffer-substring-no-properties beg end))
         (when vcomplete--last-completion-overlay
           (delete-overlay vcomplete--last-completion-overlay))
@@ -159,11 +162,24 @@ With prefix argument N, move N items (negative N means 
move forward)."
 (defun vcomplete-kill-buffer ()
   "Kill the buffer associated with the current completion (if it exists)."
   (interactive)
-  (if-let ((buf (get-buffer vcomplete-current-completion)))
-      (when (kill-buffer-ask buf)
-        (minibuffer-completion-help))
-    (user-error "‘%s’ is not a valid buffer"
-                vcomplete-current-completion)))
+  (unless (minibufferp)
+    (user-error "‘vcomplete-kill-buffer’ only works in the minibuffer"))
+  (if-let ((buf (get-buffer vcomplete-current-completion-string))
+           (index vcomplete-current-completion-index)
+           (enable-recursive-minibuffers t))
+      (if (buffer-modified-p buf)
+          (when (yes-or-no-p
+                 (format "Buffer %s modified; kill anyway? " buf))
+            (kill-buffer buf)
+            (setq vcomplete-current-completion-index 0)
+            (minibuffer-completion-help)
+            (vcomplete--move-n-completions index))
+        (kill-buffer buf)
+        (setq vcomplete-current-completion-index 0)
+        (minibuffer-completion-help)
+        (vcomplete--move-n-completions index))
+    (user-error "‘%s’ is not a valid buffer" buf))
+  (setq this-command 'vcomplete--no-update))
 
 (defvar vcomplete-command-map
   (let ((map (make-sparse-keymap)))
@@ -181,6 +197,7 @@ With prefix argument N, move N items (negative N means move 
forward)."
   (while-no-input
     (redisplay)
     (unless (eq this-command 'vcomplete--no-update)
+      (setq vcomplete-current-completion-index 0)
       (minibuffer-completion-help))))
 
 (defun vcomplete--update-in-region ()
@@ -190,8 +207,17 @@ With prefix argument N, move N items (negative N means 
move forward)."
     (unless (or (eq this-command 'vcomplete--no-update)
                 (eq this-command 'completion-at-point)
                 (null completion-in-region-mode))
+      (setq vcomplete-current-completion-index 0)
       (completion-help-at-point))))
 
+(defun vcomplete--reset-vars ()
+  "Reset variables used by Vcomplete to their default values."
+  (setq vcomplete-current-completion-string nil
+        vcomplete-current-completion-index 0
+        vcomplete--last-completion-overlay nil)
+  (remove-hook 'post-command-hook #'vcomplete--update-in-region t)
+  (remove-hook 'post-command-hook #'vcomplete--update-in-minibuffer t))
+
 (defun vcomplete--setup ()
   "Setup ‘vcomplete-mode’."
   (if (minibufferp)
@@ -208,7 +234,7 @@ With prefix argument N, move N items (negative N means move 
forward)."
             (add-hook 'post-command-hook
                       #'vcomplete--update-in-region))
           (setcdr map vcomplete-command-map))
-      (remove-hook 'post-command-hook #'vcomplete--update-in-region t))))
+      (vcomplete--reset-vars))))
 
 ;;;###autoload
 (define-minor-mode vcomplete-mode
@@ -223,8 +249,10 @@ completion:
   (if vcomplete-mode
       (progn
         (add-hook 'minibuffer-setup-hook #'vcomplete--setup)
+        (add-hook 'minibuffer-exit-hook #'vcomplete--reset-vars)
         (add-hook 'completion-in-region-mode-hook #'vcomplete--setup))
     (remove-hook 'minibuffer-setup-hook #'vcomplete--setup)
+    (remove-hook 'minibuffer-exit-hook #'vcomplete--reset-vars)
     (remove-hook 'completion-in-region-mode-hook #'vcomplete--setup)))
 
 (provide 'vcomplete)
diff --git a/vcomplete.texi b/vcomplete.texi
index cb24cd1200..b155200868 100644
--- a/vcomplete.texi
+++ b/vcomplete.texi
@@ -213,12 +213,22 @@ functions) which are bound to a key sequence in
 and @code{vcomplete-kill-buffer} are implemented as completion
 commands. Completion commands can inhibit updating the completion list
 buffer by setting @code{this-command} to @code{vcomplete--no-update}.
+Note that the completion list is updated after every command (through
+@code{post-command-hook}).
 
 @defvar vcomplete-command-map
 Key map which holds key bindings to completion commands. This key map
 should be available whenever completion is initiated.
 @end defvar
 
+@defvar vcomplete-current-completion-string
+String corresponding to the currently highlighted completion.
+@end defvar
+
+@defvar vcomplete-current-completion-index
+Index corresponding to the currently highlighted completion.
+@end defvar
+
 @defmac vcomplete-with-completions-buffer body...
 Evaluate BODY with the @code{*Completions*} buffer temporarily
 current.  While evaluating body, BUFFER and WINDOW are locally bound



reply via email to

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