From ae5c474d5f3be945be41f5883143aff8f50ab3c8 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Wed, 1 Nov 2017 16:06:15 +0100 Subject: [PATCH] Eshell: Add support for auto-suggestions --- .emacs.d/lisp/init-eshell.el | 10 ++++- .emacs.d/lisp/package-eshell-autosuggest.el | 42 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .emacs.d/lisp/package-eshell-autosuggest.el diff --git a/.emacs.d/lisp/init-eshell.el b/.emacs.d/lisp/init-eshell.el index 67e163bf..a3e0121c 100644 --- a/.emacs.d/lisp/init-eshell.el +++ b/.emacs.d/lisp/init-eshell.el @@ -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) diff --git a/.emacs.d/lisp/package-eshell-autosuggest.el b/.emacs.d/lisp/package-eshell-autosuggest.el new file mode 100644 index 00000000..dfa89d87 --- /dev/null +++ b/.emacs.d/lisp/package-eshell-autosuggest.el @@ -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)