From 128e162fd3a16609fb618ff74d21d1470cdf1439 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Mon, 6 Mar 2023 15:45:25 +0100 Subject: [PATCH] Allow using helm-completion-styles-alist per commands in addition of modes. Modes are used from a buffer and completion-in-region and commands can now be specified instead for completing-read trigerred from a specific command. --- helm-lib.el | 54 ++++++++++++++++++++++++++-------------------------- helm-mode.el | 25 ++++++++++++++++-------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/helm-lib.el b/helm-lib.el index dd6341f63..685f4ccfc 100644 --- a/helm-lib.el +++ b/helm-lib.el @@ -1652,7 +1652,7 @@ I.e. when using `helm-next-line' and friends in BODY." :test 'equal))) (defvar helm-blacklist-completion-styles '(emacs21 emacs22)) -(defun helm--prepare-completion-styles (&optional nomode styles) +(defun helm--prepare-completion-styles (&optional com-or-mode styles) "Return a suitable list of styles for `completion-styles'. When `helm-completion-style' is not `emacs' the Emacs vanilla @@ -1661,38 +1661,38 @@ default `completion-styles' is used except for value for `helm-completion-style'. If styles are specified in `helm-completion-styles-alist' for a -particular mode, use these styles unless NOMODE is non nil. +particular mode, use these styles unless COM-OR-MODE is non nil. If STYLES is specified as a list of styles suitable for `completion-styles' these styles are used in the given order. Otherwise helm style is added to `completion-styles' always after flex or helm-flex completion style if present." ;; For `helm-completion-style' and `helm-completion-styles-alist'. (require 'helm-mode) - (if (memq helm-completion-style '(helm helm-fuzzy)) - ;; Keep default settings, but probably nil is fine as well. - '(basic partial-completion emacs22) - (or - styles - (pcase (and (null nomode) - (cdr (assq major-mode helm-completion-styles-alist))) - (`(,_l . ,ll) ll)) - ;; We need to have flex always behind helm, otherwise - ;; when matching against e.g. '(foo foobar foao frogo bar - ;; baz) with pattern "foo" helm style if before flex will - ;; return foo and foobar only defeating flex that would - ;; return foo foobar foao and frogo. - (let* ((wflex (car (or (assq 'flex completion-styles-alist) - (assq 'helm-flex completion-styles-alist)))) - (styles (append (and (memq wflex completion-styles) - (list wflex)) - (cl-loop for s in completion-styles - unless (or (memq s helm-blacklist-completion-styles) - (memq wflex completion-styles)) - collect s)))) - (helm-append-at-nth - styles '(helm) - (if (memq wflex completion-styles) - 1 0)))))) + (let ((from (if com-or-mode com-or-mode major-mode))) + (if (memq helm-completion-style '(helm helm-fuzzy)) + ;; Keep default settings, but probably nil is fine as well. + '(basic partial-completion emacs22) + (or + styles + (pcase (cdr (assq from helm-completion-styles-alist)) + (`(,_l . ,ll) ll)) + ;; We need to have flex always behind helm, otherwise + ;; when matching against e.g. '(foo foobar foao frogo bar + ;; baz) with pattern "foo" helm style if before flex will + ;; return foo and foobar only defeating flex that would + ;; return foo foobar foao and frogo. + (let* ((wflex (car (or (assq 'flex completion-styles-alist) + (assq 'helm-flex completion-styles-alist)))) + (styles (append (and (memq wflex completion-styles) + (list wflex)) + (cl-loop for s in completion-styles + unless (or (memq s helm-blacklist-completion-styles) + (memq wflex completion-styles)) + collect s)))) + (helm-append-at-nth + styles '(helm) + (if (memq wflex completion-styles) + 1 0))))))) (defun helm-dynamic-completion (collection predicate &optional point metadata nomode styles) "Build a completion function for `helm-pattern' in COLLECTION. diff --git a/helm-mode.el b/helm-mode.el index a6e9aa7ca..08ef14cb0 100644 --- a/helm-mode.el +++ b/helm-mode.el @@ -333,16 +333,18 @@ NOT `setq'." (defcustom helm-completion-styles-alist '((gud-mode . helm) ;; See https://github.com/djcb/mu/issues/2181. (mu4e-compose-mode . emacs)) - "Allow configuring `helm-completion-style' per mode. + "Allow configuring `helm-completion-style' per mode or command. + +NOTE: Use a mode for a completion that will be used in a buffer +i.e. completion-in-region, whereas you have to specify instead a command to +affect the completing-read trigerred by this command. Each entry is a cons cell like (mode . style) where style must be a suitable value for `helm-completion-style'. -When specifying emacs as style for a mode, `completion-styles' can be +When specifying emacs as style for a mode or a command, `completion-styles' can be specified by using a cons cell specifying completion-styles to use with helm emacs style, e.g. (foo-mode . (emacs helm flex)) will set -`completion-styles' to \\='(helm flex) for foo-mode. This affects only -completions happening in buffers and not minibuffer completions, -i.e. completing-read's." +`completion-styles' to \\='(helm flex) for foo-mode." :group 'helm-mode :type `(alist :key-type (symbol :tag "Major Mode") @@ -1007,8 +1009,8 @@ This handler uses dynamic matching which allows honouring `completion-styles'." (`(,l . ,_ll) l))) (completion-flex-nospace t) (minibuffer-completion-table collection) - (completion-styles - (helm--prepare-completion-styles 'nomode)) + ;; (completion-styles + ;; (helm--prepare-completion-styles 'nomode)) (metadata (or (completion-metadata (or input "") collection predicate) '(metadata))) (afun (or (plist-get completion-extra-properties :annotation-function) @@ -1261,10 +1263,17 @@ See documentation of `completing-read' and `all-completions' for details." ;; Disable hack that could be used before `completing-read'. ;; i.e (push ?\t unread-command-events). unread-command-events + ;; Let-bounding here helm-completion-style according to + ;; helm-completion-styles-alist allow using helm style per commands. + (helm-completion-style (helm-aif (cdr (assq current-command helm-completion-styles-alist)) + (if (cdr-safe it) (car it) it) + (default-value 'helm-completion-style))) + (completion-styles + (helm--prepare-completion-styles current-command)) (default-handler ;; If nothing is found in ;; helm-completing-read-handlers-alist use default - ;; handler. + ;; handler which will itself use `helm-completion-style'. #'helm-completing-read-default-handler)) (when (eq def-com 'ido) (setq def-com 'ido-completing-read)) (unless (or (not entry) def-com)