Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (189 sloc) 8.353 kB
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
1 ;;; helm-eshell.el --- pcomplete and eshell completion for helm.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;; Enable like this in .emacs:
19 ;;
20 ;; (add-hook 'eshell-mode-hook
21 ;; #'(lambda ()
22 ;; (define-key eshell-mode-map [remap pcomplete] 'helm-esh-pcomplete)))
23 ;;
24
7d8197a @thierryvolpiatto Fix warnings at compilation.
thierryvolpiatto authored
25 ;;; Code:
26 (eval-when-compile (require 'cl))
27 (require 'helm)
28 (require 'helm-elisp)
29 (require 'helm-regexp)
30
31 (declare-function eshell-read-aliases-list "em-alias")
32 (declare-function eshell-send-input "esh-mode" (&optional use-region queue-p no-newline))
33 (declare-function eshell-bol "esh-mode")
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
34 (declare-function eshell-parse-arguments "esh-arg" (beg end))
7d8197a @thierryvolpiatto Fix warnings at compilation.
thierryvolpiatto authored
35
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
36 (defvar helm-eshell-history-map
37 (let ((map (make-sparse-keymap)))
38 (set-keymap-parent map helm-map)
39 (define-key map (kbd "M-p") 'helm-next-line)
40 map)
41 "Keymap for `helm-eshell-history'.")
42
43 (defvar helm-c-source-esh
44 '((name . "Eshell completions")
45 (init . (lambda ()
46 (setq pcomplete-current-completions nil
47 pcomplete-last-completion-raw nil)
48 ;; Eshell-command add this hook in all minibuffers
49 ;; Remove it for the helm one. (Fixed in Emacs24)
50 (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
51 (candidates . helm-esh-get-candidates)
efc271e @thierryvolpiatto * helm-eshell.el (helm-c-source-esh): Abbreviate candidates by filter…
thierryvolpiatto authored
52 (filtered-candidate-transformer
53 (lambda (candidates _sources)
54 (loop for i in candidates collect
55 (cons (abbreviate-file-name i) i))))
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
56 (action . helm-ec-insert))
57 "Helm source for Eshell completion.")
58
59 ;; Internal.
60 (defvar helm-ec-target "")
61 (defun helm-ec-insert (candidate)
62 "Replace text at point with CANDIDATE.
63 The function that call this should set `helm-ec-target' to thing at point."
64 (let ((pt (point)))
65 (when (and helm-ec-target
66 (search-backward helm-ec-target nil t)
67 (string= (buffer-substring (point) pt) helm-ec-target))
68 (delete-region (point) pt)))
efc271e @thierryvolpiatto * helm-eshell.el (helm-c-source-esh): Abbreviate candidates by filter…
thierryvolpiatto authored
69 (if (string-match "\\`~/" helm-ec-target)
70 (insert (helm-quote-whitespace (abbreviate-file-name candidate)))
71 (insert (helm-quote-whitespace candidate))))
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
72
73 (defun helm-esh-get-candidates ()
74 "Get candidates for eshell completion using `pcomplete'."
75 (catch 'pcompleted
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
76 (with-helm-current-buffer
77 (let* ((pcomplete-stub)
78 pcomplete-seen pcomplete-norm-func
79 pcomplete-args pcomplete-last pcomplete-index
80 (pcomplete-autolist pcomplete-autolist)
81 (pcomplete-suffix-list pcomplete-suffix-list)
82 (table (pcomplete-completions))
83 (entry (condition-case nil
84 ;; On Emacs24 `try-completion' return
85 ;; pattern when more than one result.
86 ;; Otherwise Emacs23 return nil, which
87 ;; is wrong, in this case use pattern
88 ;; to behave like Emacs24.
89 (or (try-completion helm-pattern
90 (pcomplete-entries))
91 helm-pattern)
92 ;; In Emacs23 `pcomplete-entries' may fail
93 ;; with error, so try this instead.
94 (error
95 nil
96 (let ((fc (car (last
97 (pcomplete-parse-arguments)))))
98 ;; Check if last arg require fname completion.
99 (and (file-name-directory fc) fc))))))
100 (loop for i in (all-completions pcomplete-stub table)
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
101 for file-cand = (and entry
102 (if (file-remote-p i) i
103 (expand-file-name
104 i (file-name-directory entry))))
105 if (and file-cand (or (file-remote-p file-cand)
106 (file-exists-p file-cand)))
107 collect file-cand into ls
108 else collect i into ls
109 finally return
110 (if (and entry (not (string= entry "")) (file-exists-p entry))
111 (append (list (expand-file-name entry default-directory)) ls)
112 ls))))))
113
114 ;;; Eshell history.
115 ;;
116 ;;
117 (defvar helm-c-source-eshell-history
118 `((name . "Eshell history")
119 (init . (lambda ()
120 (let (eshell-hist-ignoredups)
121 ;; Write the content's of ring to file.
f9ed9c2 @thierryvolpiatto * helm-eshell.el (helm-c-source-eshell-history): Remove APPEND arg of…
thierryvolpiatto authored
122 (eshell-write-history eshell-history-file-name)
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
123 (with-current-buffer (helm-candidate-buffer 'global)
124 (insert-file-contents eshell-history-file-name)))
125 ;; Same comment as in `helm-c-source-esh'
126 (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
127 (candidates-in-buffer)
128 (keymap . ,helm-eshell-history-map)
129 (filtered-candidate-transformer . (lambda (candidates sources)
130 (reverse candidates)))
131 (candidate-number-limit . 9999)
132 (action . (lambda (candidate)
133 (eshell-kill-input)
134 (insert candidate))))
135 "Helm source for Eshell history.")
136
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
137 ;;;###autoload
138 (defun helm-esh-pcomplete ()
139 "Preconfigured helm to provide helm completion in eshell."
140 (interactive)
141 (let* ((helm-quit-if-no-candidate t)
142 (helm-execute-action-at-once-if-one t)
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
143 (end (point-marker))
144 (beg (save-excursion (eshell-bol) (point)))
145 (args (catch 'eshell-incomplete
146 (eshell-parse-arguments beg end)))
147 ;; Use thing-at-point instead of last args value
148 ;; to exclude possible delimiters e.g "(".
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
149 (target (thing-at-point 'symbol))
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
150 (first (car args)) ; Maybe lisp delimiter "(".
151 last) ; Will be the last but parsed by pcomplete.
152 (setq helm-ec-target (or target " ")
153 end (point)
154 ;; Reset beg for `with-helm-show-completion'.
155 beg (or (and target (- end (length target)))
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
156 ;; Nothing at point.
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
157 (progn (insert " ") (point))))
158 (cond ((eq first ?\()
9e4d14a @thierryvolpiatto * helm-elisp.el: Fix error when trying completion on nothing.
thierryvolpiatto authored
159 (helm-lisp-completion-or-file-name-at-point))
40080c6 @thierryvolpiatto * helm-eshell.el (helm-esh-get-candidates): Call pcomplete-completion…
thierryvolpiatto authored
160 ;; In eshell `pcomplete-parse-arguments' is called
161 ;; with `pcomplete-parse-arguments-function'
162 ;; locally bound to `eshell-complete-parse-arguments'
163 ;; which is calling `lisp-complete-symbol',
164 ;; calling it before would popup the
165 ;; *completions* buffer.
166 (t (setq last (car (last (ignore-errors
167 (pcomplete-parse-arguments)))))
168 (with-helm-show-completion beg end
169 (helm :sources 'helm-c-source-esh
170 :buffer "*helm pcomplete*"
171 :resume 'noresume
172 :input (and (stringp last)
173 (helm-ff-set-pattern last))))))))
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
174
175 ;;;###autoload
176 (defun helm-eshell-history ()
177 "Preconfigured helm for eshell history."
178 (interactive)
179 (let* ((end (point))
180 (beg (save-excursion (eshell-bol) (point)))
181 (input (buffer-substring beg end))
182 flag-empty)
183 (when (eq beg end)
184 (insert " ")
185 (setq flag-empty t)
186 (setq end (point)))
187 (unwind-protect
188 (with-helm-show-completion beg end
189 (helm :sources 'helm-c-source-eshell-history
190 :buffer "*Eshell history*"
162d583 @thierryvolpiatto * helm-eshell.el: Don't resume eshell completion sessions.
thierryvolpiatto authored
191 :resume 'noresume
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
192 :input input))
193 (when (and flag-empty
194 (looking-back " "))
195 (delete-char -1)))))
196
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
197 (provide 'helm-eshell)
198
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
199 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
200 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
201 ;; coding: utf-8
202 ;; indent-tabs-mode: nil
203 ;; byte-compile-dynamic: t
204 ;; End:
205
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
206 ;;; helm-eshell ends here
Something went wrong with that request. Please try again.