Eshell: Add support for auto-suggestions

master
Pierre Neidhardt 2017-11-01 16:06:15 +01:00
parent a9b618882f
commit ae5c474d5f
2 changed files with 51 additions and 1 deletions

View File

@ -189,6 +189,7 @@ See `eshell' for the numeric prefix ARG."
(switch-to-buffer (car last))
(eshell (or arg t))))))
;;; Completion
(when (require 'bash-completion nil t)
(defun eshell-bash-completion ()
(while (pcomplete-here
@ -199,6 +200,13 @@ See `eshell' for the numeric prefix ARG."
(when (and (executable-find "fish")
(require 'fish-completion nil t))
(fish-completion-eshell-global-toggle))
(fish-completion-eshell-toggle-globally))
;;; Auto-suggestion
(when (require 'package-eshell-autosuggest)
(add-hook 'eshell-mode-hook 'company-mode)
(when (require 'helm-config nil t)
(define-key company-active-map (kbd "M-p") 'helm-eshell-history))
(add-hook 'eshell-mode-hook 'eshell-setup-autosuggest))
(provide 'init-eshell)

View File

@ -0,0 +1,42 @@
(require 'company)
(defun company-eshell-autosuggest-candidates (prefix)
(let* ((history
(cl-remove-duplicates
(mapcar (lambda (str)
(string-trim (substring-no-properties str)))
(ring-elements eshell-history-ring))
:from-end t
:test #'string=))
(most-similar (cl-find-if
(lambda (str)
(string-prefix-p prefix str))
history)))
(when most-similar
`(,most-similar))))
(defun company-eshell-autosuggest--prefix ()
(let ((prefix
(string-trim-left
(buffer-substring-no-properties
(save-excursion
(eshell-bol))
(save-excursion (end-of-line) (point))))))
(if (not (string-empty-p prefix))
prefix
'stop)))
(defun company-eshell-autosuggest (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-eshell))
(prefix (and (eq major-mode 'eshell-mode)
(company-eshell-autosuggest--prefix)))
(candidates (company-eshell-autosuggest-candidates arg))))
(defun eshell-setup-autosuggest ()
(setq-local company-idle-delay 0.5)
(setq-local company-backends '(company-eshell-autosuggest))
(setq-local company-frontends '(company-preview-frontend)))
(provide 'package-eshell-autosuggest)