-
Notifications
You must be signed in to change notification settings - Fork 10
/
esh-autosuggest.el
163 lines (138 loc) · 5.53 KB
/
esh-autosuggest.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; esh-autosuggest.el --- History autosuggestions for eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Diego A. Mundo
;; Author: Diego A. Mundo <[email protected]>
;; URL: http://github.com/dieggsy/esh-autosuggest
;; Git-Repository: git://github.com/dieggsy/esh-autosuggest.git
;; Created: 2017-10-28
;; Version: 2.0.1
;; Keywords: completion company matching convenience abbrev
;; Package-Requires: ((emacs "24.4") (company "0.9.4"))
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Provides a company backend that implements functionality similar to fish
;; shell history autosuggestions.
;;; Code:
(require 'company)
(require 'cl-lib)
(eval-when-compile (require 'eshell))
(require 'em-prompt)
(defgroup esh-autosuggest nil
"Fish-like autosuggestions for eshell."
:group 'company)
(defcustom esh-autosuggest-delay 0
"Delay for history autosuggestion."
:group 'esh-autosuggest
:type 'number)
(defcustom esh-autosuggest-use-company-map nil
"Instead of overriding `company-active-map', use as-is.
This is disabled by default, as bindings in `company-active-map'
to RET and TAB may interfere with command input and completion
respectively."
:group 'esh-autosuggest
:type 'boolean)
(defvar esh-autosuggest-active-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "<right>") 'company-complete-selection)
(define-key keymap (kbd "C-f") 'company-complete-selection)
(define-key keymap (kbd "M-<right>") 'esh-autosuggest-complete-word)
(define-key keymap (kbd "M-f") 'esh-autosuggest-complete-word)
keymap)
"Keymap that is enabled during an active history
autosuggestion.")
(defun esh-autosuggest-candidates (prefix)
"Select the first eshell history candidate that starts with PREFIX."
(let* ((history
(delete-dups
(mapcar (lambda (str)
(string-trim (substring-no-properties str)))
(ring-elements eshell-history-ring))))
(most-similar (cl-find-if
(lambda (str)
(string-prefix-p prefix str))
history)))
(when most-similar
`(,most-similar))))
(defun esh-autosuggest-complete-word ()
(interactive)
(save-excursion
(let ((pos (point)))
(company-complete-selection)
(goto-char pos)
(forward-word)
(unless (or (eobp) (eolp))
(kill-line))))
(end-of-line)
(ignore-errors
(let ((inhibit-message t))
(company-begin-backend 'esh-autosuggest))))
(defun esh-autosuggest--prefix ()
"Get current eshell input."
(let* ((input-start (save-excursion
(eshell-previous-prompt 1)
(eshell-next-prompt 1)
(point)))
(prefix
(string-trim-left
(buffer-substring-no-properties
input-start
(line-end-position)))))
(if (not (string-empty-p prefix))
prefix
'stop)))
;;;###autoload
(defun esh-autosuggest (command &optional arg &rest _ignored)
"`company-mode' backend to provide eshell history suggestion."
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'esh-autosuggest))
(prefix (and (eq major-mode 'eshell-mode)
(esh-autosuggest--prefix)))
(candidates (esh-autosuggest-candidates arg))
(require-match 'never)))
;;;###autoload
(define-minor-mode esh-autosuggest-mode
"Enable fish-like autosuggestions in eshell.
You can use <right> to select the suggestion. This is
customizable through `esh-autosuggest-active-map'. If
you prefer to use the default value of `company-active-map', you
may set the variable
`esh-autosuggest-use-company-map', though this isn't
recommended as RET and TAB may not work as expected (send input,
trigger completions, respectively) when there is an active
suggestion.
The delay defaults to 0 seconds to emulate fish shell's
instantaneous suggestions, but is customizable with
`esh-autosuggest-delay'.
Note: This assumes you want to use something other than company
for shell completion, e.g. `eshell-pcomplete',
`completion-at-point', or helm-esh-pcomplete, since
`company-active-map', `company-backends', and `company-frontends'
will be locally overriden and company will be used solely for
history autosuggestions."
:init-value nil
:group 'esh-autosuggest
(if esh-autosuggest-mode
(progn
(company-mode 1)
(unless esh-autosuggest-use-company-map
(setq-local company-active-map esh-autosuggest-active-map))
(setq-local company-idle-delay esh-autosuggest-delay)
(setq-local company-backends '(esh-autosuggest))
(setq-local company-frontends '(company-preview-frontend)))
(company-mode -1)
(kill-local-variable 'company-active-map)
(kill-local-variable 'company-idle-delay)
(kill-local-variable 'company-backends)
(kill-local-variable 'company-frontends)))
(provide 'esh-autosuggest)
;;; esh-autosuggest.el ends here