branch: elpa/vcomplete commit dce24e895d13b1c141cb544f63d2526c88e2468b Author: Daniel Semyonov <cm...@dsemy.com> Commit: Daniel Semyonov <cm...@dsemy.com>
Replaced current completion variables with a function * vcomplete.el (vcomplete-current-completion-string) (vcomplete-current-completion-index): Replace with 'vcomplete-current-completion'. (vcomplete-current-completion): New function which gets the completion at point in the completion list buffer. (vcomplete--highlight-completion-at-point): Reimplement using 'vcomplete-current-completion'. (vcomplete--move-n-completions): Remove step which set 'vcomplete-current-completion-index', and remove old handling of incorrect highlighting occurring when moving to the end of the completion list buffer. (vcomplete--update-in-minibuffer, vcomplete--update-in-region): Remove step which set 'vcomplete-current-completion-index'. (vcomplete--reset-vars): Remove steps which reset 'vcomplete-current-completion-string' and 'vcomplete-current-completion-index'. --- vcomplete.el | 73 +++++++++++++++++++++++++--------------------------------- vcomplete.texi | 17 ++++++++------ 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/vcomplete.el b/vcomplete.el index 174c06b1e3..9078c5d55d 100644 --- a/vcomplete.el +++ b/vcomplete.el @@ -106,53 +106,48 @@ 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-string nil - "Currently selected completion string.") - -(defvar vcomplete-current-completion-index 0 - "Index (in the ‘*Completions*’ buffer) of the current completion.") +(defun vcomplete-current-completion (pos) + "Get the completion candidate at POS. +The completion candidate is returned as a list of the form: + (COMPLETION-STRING . (BEGINNING . END)) +If no completion is found, return nil." + (unless (derived-mode-p 'completion-list-mode) + (error "Not in a valid completion list buffer")) + ;; Modified from code in ‘choose-completion’. + (let (beg end noop) + (cond + ((and (not (eobp)) (get-text-property pos 'mouse-face)) + (setq end pos beg (1+ pos))) + ((and (not (bobp)) + (get-text-property (1- pos) 'mouse-face)) + (setq end (1- pos) beg pos)) + (t (setq noop t))) + (unless noop + (setq beg (previous-single-property-change beg 'mouse-face)) + (setq end (or (next-single-property-change end 'mouse-face) + (point-max))) + `(,(buffer-substring-no-properties beg end) . (,beg . ,end))))) (defvar vcomplete--last-completion-overlay nil "Last overlay created in the ‘*Completions*’ buffer.") (defun vcomplete--highlight-completion-at-point () - "Highlight the completion at point in the ‘*Completions*’ buffer. -The string of the current completion is saved in -‘vcomplete-current-completion-string’." + "Highlight the completion at point in the ‘*Completions*’ buffer." (while-no-input (redisplay) - (if (derived-mode-p 'completion-list-mode) - ;; Modified from code in ‘choose-completion’. - (let (beg end noop hl) - (cond - ((and (not (eobp)) (get-text-property (point) 'mouse-face)) - (setq end (point) beg (1+ (point)))) - ((and (not (bobp)) - (get-text-property (1- (point)) 'mouse-face)) - (setq end (1- (point)) beg (point))) - (t (setq noop t))) - (unless noop - (setq beg (previous-single-property-change beg 'mouse-face)) - (setq end (or (next-single-property-change end 'mouse-face) - (point-max))) - (setq vcomplete-current-completion-string - (buffer-substring-no-properties beg end)) - (overlay-put - (setq hl (make-overlay beg end)) 'face 'highlight)) - (when vcomplete--last-completion-overlay - (delete-overlay vcomplete--last-completion-overlay)) - (setq vcomplete--last-completion-overlay hl)) - (error "Not in a valid completion list buffer")))) + (let ((cur (vcomplete-current-completion (point)))) + (when vcomplete--last-completion-overlay + (delete-overlay vcomplete--last-completion-overlay)) + (when-let ((pos (cdr cur))) + (overlay-put + (setq vcomplete--last-completion-overlay + (make-overlay (car pos) (cdr pos))) + 'face 'highlight))))) (defun vcomplete--move-n-completions (n) - "Move N completions in the ‘*Completions*’ buffer. -The index of the current completion is saved in -‘vcomplete-current-completion-index’." + "Move N completions in the ‘*Completions*’ buffer." (vcomplete-with-completions-buffer (next-completion n) - (setq vcomplete-current-completion-index - (+ n vcomplete-current-completion-index)) - (when (= (point) (point-max)) (next-completion -1)) (set-window-point window (point)) (vcomplete--highlight-completion-at-point))) @@ -192,7 +187,6 @@ 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 () @@ -202,14 +196,11 @@ 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) + (setq 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)) diff --git a/vcomplete.texi b/vcomplete.texi index 103ed4da86..fe3d8eb6be 100644 --- a/vcomplete.texi +++ b/vcomplete.texi @@ -211,13 +211,16 @@ 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 +@defun vcomplete-current-completion pos +Get the completion candidate at POS. +The completion candidate is returned as a list of the form: +@table @asis +(COMPLETION-STRING . (BEGINNING . END)) +@end table +If no completion is found, this function returns nil. Note that this +function throws an error when the major mode of the buffer from which +it is called isn't derived from @code{completion-list-mode}. +@end defun @defmac vcomplete-with-completions-buffer body... Evaluate BODY with the @code{*Completions*} buffer temporarily