Skip to content

Commit

Permalink
corfu--completion-in-region: Fall back to default completion function…
Browse files Browse the repository at this point in the history
… on TUI (See #20)

* When using the text-mode UI, fall back to the default value of
  the `completion-in-region-function'
* Since there are mixed setups with daemons/text/gui, don't print
  an error message but try to do the right thing in any case
  • Loading branch information
minad committed May 6, 2021
1 parent e84e5e1 commit 34c45b9
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions corfu.el
Original file line number Diff line number Diff line change
Expand Up @@ -688,17 +688,21 @@ Set to nil in order to disable confirmation."

(defun corfu--completion-in-region (&rest args)
"Corfu completion in region function passing ARGS to `completion--in-region'."
;; Prevent restarting the completion. This can happen for example if C-M-/
;; (`dabbrev-completion') is pressed while the Corfu popup is already open.
(when (and completion-in-region-mode (not completion-cycling))
(user-error "Completion is already in progress"))
(let ((completion-show-inline-help)
(completion-auto-help)
;; XXX Disable original predicate check, keep completion alive when
;; popup is shown. Since the predicate is set always, it is ensured
;; that `completion-in-region-mode' is turned on.
(completion-in-region-mode-predicate (lambda () t)))
(apply #'completion--in-region args)))
(if (and (not emacs-basic-display) (display-graphic-p))
(let ((completion-show-inline-help)
(completion-auto-help)
;; XXX Disable original predicate check, keep completion alive when
;; popup is shown. Since the predicate is set always, it is ensured
;; that `completion-in-region-mode' is turned on.
(completion-in-region-mode-predicate (lambda () t)))
;; Prevent restarting the completion. This can happen for example if C-M-/
;; (`dabbrev-completion') is pressed while the Corfu popup is already open.
(when (and completion-in-region-mode (not completion-cycling))
(user-error "Completion is already in progress"))
(apply #'completion--in-region args))
;; XXX Warning this can result in an endless loop when `completion-in-region-function'
;; is set *globally* to `corfu--completion-in-region'. This should never happen.
(apply (default-value 'completion-in-region-function) args)))

;;;###autoload
(define-minor-mode corfu-mode
Expand All @@ -707,10 +711,8 @@ Set to nil in order to disable confirmation."
(remove-hook 'completion-in-region-mode-hook #'corfu--mode-hook 'local)
(kill-local-variable 'completion-in-region-function)
(when corfu-mode
(if (or emacs-basic-display (not (display-graphic-p)))
(message "Corfu mode is only compatible with the graphics display.")
(add-hook 'completion-in-region-mode-hook #'corfu--mode-hook nil 'local)
(setq-local completion-in-region-function #'corfu--completion-in-region))))
(add-hook 'completion-in-region-mode-hook #'corfu--mode-hook nil 'local)
(setq-local completion-in-region-function #'corfu--completion-in-region)))

;;;###autoload
(define-globalized-minor-mode corfu-global-mode corfu-mode corfu--on)
Expand Down

0 comments on commit 34c45b9

Please sign in to comment.