Skip to content

Commit

Permalink
Allow using helm-completion-styles-alist per commands in addition of
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thierryvolpiatto committed Mar 6, 2023
1 parent 86a6143 commit 128e162
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
54 changes: 27 additions & 27 deletions helm-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
25 changes: 17 additions & 8 deletions helm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 128e162

Please sign in to comment.