dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

consult-icomplete.el (2174B)


      1 ;;; consult-icomplete.el --- Icomplete integration for Consult -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
      4 
      5 ;; This file is part of GNU Emacs.
      6 
      7 ;; This program is free software: you can redistribute it and/or modify
      8 ;; it under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation, either version 3 of the License, or
     10 ;; (at your option) any later version.
     11 
     12 ;; This program is distributed in the hope that it will be useful,
     13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 ;; GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     19 
     20 ;;; Commentary:
     21 
     22 ;; Integration code for the Icomplete completion system. This package
     23 ;; is automatically loaded by Consult.
     24 
     25 ;;; Code:
     26 
     27 (require 'consult)
     28 (require 'icomplete)
     29 
     30 (defun consult-icomplete--refresh (&optional reset)
     31   "Refresh icomplete view, keep current candidate unless RESET is non-nil."
     32   (when icomplete-mode
     33     (let ((top (car completion-all-sorted-completions)))
     34       (completion--flush-all-sorted-completions)
     35       ;; force flushing, otherwise narrowing is broken!
     36       (setq completion-all-sorted-completions nil)
     37       (when (and top (not reset))
     38         (let* ((completions (completion-all-sorted-completions))
     39                (last (last completions))
     40                (before)) ;; completions before top
     41           ;; warning: completions is an improper list
     42           (while (consp completions)
     43             (if (equal (car completions) top)
     44                 (progn
     45                   (setcdr last (append (nreverse before) (cdr last)))
     46                   (setq completion-all-sorted-completions completions
     47                         completions nil))
     48               (push (car completions) before)
     49               (setq completions (cdr completions)))))))
     50     (icomplete-exhibit)))
     51 
     52 (add-hook 'consult--completion-refresh-hook #'consult-icomplete--refresh)
     53 
     54 (provide 'consult-icomplete)
     55 ;;; consult-icomplete.el ends here