Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 187 lines (169 sloc) 7.468 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")
34
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
35 (defvar helm-eshell-history-map
36 (let ((map (make-sparse-keymap)))
37 (set-keymap-parent map helm-map)
38 (define-key map (kbd "M-p") 'helm-next-line)
39 map)
40 "Keymap for `helm-eshell-history'.")
41
42 (defvar helm-c-source-esh
43 '((name . "Eshell completions")
44 (init . (lambda ()
45 (setq pcomplete-current-completions nil
46 pcomplete-last-completion-raw nil)
47 ;; Eshell-command add this hook in all minibuffers
48 ;; Remove it for the helm one. (Fixed in Emacs24)
49 (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
50 (candidates . helm-esh-get-candidates)
efc271e @thierryvolpiatto * helm-eshell.el (helm-c-source-esh): Abbreviate candidates by filtering...
thierryvolpiatto authored
51 (filtered-candidate-transformer
52 (lambda (candidates _sources)
53 (loop for i in candidates collect
54 (cons (abbreviate-file-name i) i))))
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
55 (action . helm-ec-insert))
56 "Helm source for Eshell completion.")
57
58 ;; Internal.
59 (defvar helm-ec-target "")
60 (defun helm-ec-insert (candidate)
61 "Replace text at point with CANDIDATE.
62 The function that call this should set `helm-ec-target' to thing at point."
63 (let ((pt (point)))
64 (when (and helm-ec-target
65 (search-backward helm-ec-target nil t)
66 (string= (buffer-substring (point) pt) helm-ec-target))
67 (delete-region (point) pt)))
efc271e @thierryvolpiatto * helm-eshell.el (helm-c-source-esh): Abbreviate candidates by filtering...
thierryvolpiatto authored
68 (if (string-match "\\`~/" helm-ec-target)
69 (insert (helm-quote-whitespace (abbreviate-file-name candidate)))
70 (insert (helm-quote-whitespace candidate))))
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
71
72 (defun helm-esh-get-candidates ()
73 "Get candidates for eshell completion using `pcomplete'."
74 (catch 'pcompleted
75 (let* ((pcomplete-stub)
76 pcomplete-seen pcomplete-norm-func
77 pcomplete-args pcomplete-last pcomplete-index
78 (pcomplete-autolist pcomplete-autolist)
79 (pcomplete-suffix-list pcomplete-suffix-list))
80 (with-helm-current-buffer
81 (loop with table = (pcomplete-completions)
82 with entry = (condition-case nil
83 ;; On Emacs24 `try-completion' return
84 ;; pattern when more than one result.
85 ;; Otherwise Emacs23 return nil, which
86 ;; is wrong, in this case use pattern
87 ;; to behave like Emacs24.
88 (or (try-completion helm-pattern
89 (pcomplete-entries))
90 helm-pattern)
91 ;; In Emacs23 `pcomplete-entries' may fail
92 ;; with error, so try this instead.
93 (error
94 nil
95 (let ((fc (car (last
96 (pcomplete-parse-arguments)))))
97 ;; Check if last arg require fname completion.
98 (and (file-name-directory fc) fc))))
99 for i in (all-completions pcomplete-stub table)
100 for file-cand = (and entry
101 (if (file-remote-p i) i
102 (expand-file-name
103 i (file-name-directory entry))))
104 if (and file-cand (or (file-remote-p file-cand)
105 (file-exists-p file-cand)))
106 collect file-cand into ls
107 else collect i into ls
108 finally return
109 (if (and entry (not (string= entry "")) (file-exists-p entry))
110 (append (list (expand-file-name entry default-directory)) ls)
111 ls))))))
112
113 ;;; Eshell history.
114 ;;
115 ;;
116 (defvar helm-c-source-eshell-history
117 `((name . "Eshell history")
118 (init . (lambda ()
119 (let (eshell-hist-ignoredups)
120 ;; Write the content's of ring to file.
121 (eshell-write-history eshell-history-file-name t)
122 (with-current-buffer (helm-candidate-buffer 'global)
123 (insert-file-contents eshell-history-file-name)))
124 ;; Same comment as in `helm-c-source-esh'
125 (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
126 (candidates-in-buffer)
127 (keymap . ,helm-eshell-history-map)
128 (filtered-candidate-transformer . (lambda (candidates sources)
129 (reverse candidates)))
130 (candidate-number-limit . 9999)
131 (action . (lambda (candidate)
132 (eshell-kill-input)
133 (insert candidate))))
134 "Helm source for Eshell history.")
135
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
136 ;;;###autoload
137 (defun helm-esh-pcomplete ()
138 "Preconfigured helm to provide helm completion in eshell."
139 (interactive)
140 (let* ((helm-quit-if-no-candidate t)
141 (helm-execute-action-at-once-if-one t)
142 (target (thing-at-point 'symbol))
143 (end (point))
144 (beg (or (and target (- end (length target)))
145 ;; Nothing at point.
146 (progn (insert " ") (point)))))
147 (setq helm-ec-target (or target " "))
148 (with-helm-show-completion beg end
149 (helm :sources 'helm-c-source-esh
150 :buffer "*helm pcomplete*"
162d583 @thierryvolpiatto * helm-eshell.el: Don't resume eshell completion sessions.
thierryvolpiatto authored
151 :resume 'noresume
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
152 :input (helm-ff-set-pattern ; Handle tramp filenames.
153 (car (last (ignore-errors ; Needed in lisp symbols completion.
154 (pcomplete-parse-arguments)))))))))
155
156 ;;;###autoload
157 (defun helm-eshell-history ()
158 "Preconfigured helm for eshell history."
159 (interactive)
160 (let* ((end (point))
161 (beg (save-excursion (eshell-bol) (point)))
162 (input (buffer-substring beg end))
163 flag-empty)
164 (when (eq beg end)
165 (insert " ")
166 (setq flag-empty t)
167 (setq end (point)))
168 (unwind-protect
169 (with-helm-show-completion beg end
170 (helm :sources 'helm-c-source-eshell-history
171 :buffer "*Eshell history*"
162d583 @thierryvolpiatto * helm-eshell.el: Don't resume eshell completion sessions.
thierryvolpiatto authored
172 :resume 'noresume
18fc764 @thierryvolpiatto * helm-config.el: Move helm-esh-pcomplete, helm-eshell-history.
thierryvolpiatto authored
173 :input input))
174 (when (and flag-empty
175 (looking-back " "))
176 (delete-char -1)))))
177
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
178 (provide 'helm-eshell)
179
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
180 ;; Local Variables:
181 ;; coding: utf-8
182 ;; indent-tabs-mode: nil
183 ;; byte-compile-dynamic: t
184 ;; End:
185
0018451 @thierryvolpiatto * helm-config.el: Move pcomplete and eshell hist to helm-eshell.el
thierryvolpiatto authored
186 ;;; helm-eshell ends here
Something went wrong with that request. Please try again.