From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: zsh-like zcomplete-mode based on icomplete-mode Date: Sat, 09 Apr 2022 21:45:14 +0300 Organization: LINKOV.NET Message-ID: <86sfqm14g6.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5226"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Apr 09 21:00:01 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ndGJV-0001DO-4o for ged-emacs-devel@m.gmane-mx.org; Sat, 09 Apr 2022 21:00:01 +0200 Original-Received: from localhost ([::1]:47866 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ndGJT-0007c8-Vp for ged-emacs-devel@m.gmane-mx.org; Sat, 09 Apr 2022 15:00:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49084) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndGIg-0006F8-U0 for emacs-devel@gnu.org; Sat, 09 Apr 2022 14:59:10 -0400 Original-Received: from relay3-d.mail.gandi.net ([217.70.183.195]:51825) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndGIe-00067I-FI for emacs-devel@gnu.org; Sat, 09 Apr 2022 14:59:10 -0400 Original-Received: (Authenticated sender: juri@linkov.net) by mail.gandi.net (Postfix) with ESMTPSA id E2B8860002 for ; Sat, 9 Apr 2022 18:59:03 +0000 (UTC) Received-SPF: pass client-ip=217.70.183.195; envelope-from=juri@linkov.net; helo=relay3-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:288044 Archived-At: --=-=-= Content-Type: text/plain Here is the first version of the mode with the behavior like in zsh, but that is based on icomplete-mode. So many customizable variables and commands were copied from icomplete-mode, but instead of displaying completions as an overlay in the minibuffer, in zcomplete-mode completions are displayed in the standard *Completions* window. All other icomplete features are preserved in zcomplete, such as typing in the minibuffer continuously updates a list of possible completions that match the typed string: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=zcomplete-mode.patch diff --git a/lisp/icomplete.el b/lisp/icomplete.el index 2986aa192c..1e6b11bbd1 100644 --- a/lisp/icomplete.el +++ b/lisp/icomplete.el @@ -1063,6 +1063,173 @@ icomplete-completions ;;;###autoload (make-obsolete 'iswitchb-mode ;;;###autoload "use `icomplete-mode' or `ido-mode' instead." "24.4")) +(defgroup zcomplete nil + "Show completions dynamically in *Completions* window from minibuffer." + :prefix "zcomplete-" + :link '(info-link "(emacs)Zcomplete") + :group 'minibuffer) + +(defcustom zcomplete-show-matches-on-no-input nil + "When non-nil, show completions when first prompting for input. +This means to show completions even when the current minibuffer contents +is the same as was the initial input after minibuffer activation. +This also means that if you traverse the list of completions with +commands and just hit RET without typing any characters, +the match under point will be chosen instead of the default." + :type 'boolean) + +(defcustom zcomplete-with-completion-tables t + "Specialized completion tables with which Zcomplete should operate. +If this is t, Zcomplete operates on all tables. +Otherwise this should be a list of the completion tables (e.g., +`internal-complete-buffer') on which Zcomplete should operate." + :type '(choice (const :tag "All" t) + (repeat function))) + +(defcustom zcomplete-compute-delay .15 + "Completions-computation stall, used only with large-number completions. +See `zcomplete-delay-completions-threshold'." + :type 'number) + +(defcustom zcomplete-delay-completions-threshold 400 + "Pending-completions number over which to apply `zcomplete-compute-delay'." + :type 'integer) + +(defcustom zcomplete-max-delay-chars 2 + "Maximum number of initial chars to apply `zcomplete-compute-delay'." + :type 'integer) + +(defcustom zcomplete-minibuffer-setup-hook nil + "Zcomplete-specific customization of minibuffer setup. +This hook is run during minibuffer setup if Zcomplete is active." + :type 'hook) + + +(defvar zcomplete--initial-input nil + "Initial input in the minibuffer when `zcomplete-mode' was activated. +Used to implement the option `zcomplete-show-matches-on-no-input'.") + +(defun zcomplete-post-command-hook (_ _ _) + (let ((non-essential t)) ;E.g. don't prompt for password! + (zcomplete-exhibit))) + +(defvar-keymap zcomplete-minibuffer-map + :doc "Keymap used by `zcomplete-mode' in the minibuffer." + " " #'zcomplete-ret + "" #'minibuffer-previous-completion + "" #'minibuffer-next-completion + "" #'zcomplete-previous-line-completion + "" #'zcomplete-next-line-completion) + +(defun zcomplete-ret () + "Exit minibuffer for zcomplete." + (interactive) + (remove-hook 'after-change-functions #'zcomplete-post-command-hook t) + (if (get-buffer-window "*Completions*" 0) + (minibuffer-choose-completion) + (minibuffer-complete-and-exit))) + +(defun zcomplete-previous-line-completion (&optional n) + "Run `previous-line' from the minibuffer in its completions window." + (interactive "p") + (with-minibuffer-completions-window + (when completions-highlight-face + (setq-local cursor-face-highlight-nonselected-window t)) + (previous-line n))) + +(defun zcomplete-next-line-completion (&optional n) + "Run `next-line' from the minibuffer in its completions window." + (interactive "p") + (with-minibuffer-completions-window + (when completions-highlight-face + (setq-local cursor-face-highlight-nonselected-window t)) + (next-line n))) + + +;;;###autoload +(define-minor-mode zcomplete-mode + "Toggle incremental minibuffer completion (Zcomplete mode). + +When this global minor mode is enabled, typing in the minibuffer +continuously displays a list of possible completions that match +the string you have typed. The list of completions is displayed +in the *Completions* window. + +For more information, see Info node `(emacs)Zcomplete'. +For options you can set, `\\[customize-group] zcomplete'. + +You can use the following key bindings to navigate and select +completions: + +\\{zcomplete-minibuffer-map}" + :global t :group 'zcomplete + (remove-hook 'minibuffer-setup-hook #'zcomplete-minibuffer-setup) + (when zcomplete-mode + (add-hook 'minibuffer-setup-hook #'zcomplete-minibuffer-setup))) + +(defun zcomplete--completion-table () + (if (window-minibuffer-p) minibuffer-completion-table + (or (nth 2 completion-in-region--data) + (message "In %S (w=%S): %S" + (current-buffer) (selected-window) (window-minibuffer-p))))) +(defun zcomplete--field-string () + (if (window-minibuffer-p) + (minibuffer-contents) + (buffer-substring-no-properties + (nth 0 completion-in-region--data) + (nth 1 completion-in-region--data)))) +(defun zcomplete--field-beg () + (if (window-minibuffer-p) (minibuffer-prompt-end) + (nth 0 completion-in-region--data))) +(defun zcomplete--field-end () + (if (window-minibuffer-p) (point-max) + (nth 1 completion-in-region--data))) + +(defun zcomplete-simple-completing-p () + "Non-nil if current window is a minibuffer that's doing simple completion." + (unless executing-kbd-macro + (let ((table (zcomplete--completion-table))) + (and table + (or (not (functionp table)) + (eq zcomplete-with-completion-tables t) + (member table zcomplete-with-completion-tables)))))) + +(defun zcomplete-minibuffer-setup () + "Run in minibuffer on activation to establish incremental completion. +Usually run by inclusion in `minibuffer-setup-hook'." + (when zcomplete-mode + (setq-local zcomplete--initial-input (zcomplete--field-string)) + (use-local-map (make-composed-keymap zcomplete-minibuffer-map + (current-local-map))) + (add-hook 'after-change-functions #'zcomplete-post-command-hook nil t) + (run-hooks 'zcomplete-minibuffer-setup-hook))) + +(defun zcomplete-exhibit () + "Update Zcomplete completions display. +Should be run via minibuffer `post-command-hook'. +See `zcomplete-mode' and `minibuffer-setup-hook'." + (when (and zcomplete-mode + (zcomplete-simple-completing-p)) ;Shouldn't be necessary. + (when (and (or zcomplete-show-matches-on-no-input + (not (equal (zcomplete--field-string) + zcomplete--initial-input))) + (or + ;; Don't bother with delay after certain number of chars: + (> (- (point) (zcomplete--field-beg)) + zcomplete-max-delay-chars) + ;; Don't delay if the completions are known. + completion-all-sorted-completions + ;; Don't delay if alternatives number is small enough: + (and (sequencep (zcomplete--completion-table)) + (< (length (zcomplete--completion-table)) + zcomplete-delay-completions-threshold)) + ;; Delay - give some grace time for next keystroke, before + ;; embarking on computing completions: + (sit-for zcomplete-compute-delay))) + (save-excursion + (minibuffer-completion-help) + (minibuffer-next-completion))))) + (provide 'icomplete) ;;;_* Local emacs vars. --=-=-=--