-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhelm-company.el
183 lines (153 loc) · 6.12 KB
/
helm-company.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;;; helm-company.el --- Helm interface for company-mode
;; Copyright (C) 2013 Yasuyuki Oka <[email protected]>
;; Author: Yasuyuki Oka <[email protected]>
;; Version: 0.1.2
;; URL: https://github.com/yasuyk/helm-company
;; Package-Requires: ((helm-core "1.9.3") (company "0.9.0"))
;; 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:
;; Add the following to your Emacs init file:
;;
;; (autoload 'helm-company "helm-company") ;; Not necessary if using ELPA package
;; (eval-after-load 'company
;; '(progn
;; (define-key company-mode-map (kbd "C-:") 'helm-company)
;; (define-key company-active-map (kbd "C-:") 'helm-company)))
;;; Code:
(require 'helm)
(require 'helm-files)
(require 'helm-elisp) ;; For with-helm-show-completion
(require 'company)
(defgroup helm-company nil
"Helm interface for company-mode."
:prefix "helm-company-"
:group 'helm)
(defcustom helm-company-candidate-number-limit 300
"Limit candidate number of `helm-company'.
Set it to nil if you don't want this limit."
:group 'helm-company
:type '(choice (const :tag "Disabled" nil) integer))
(defvar helm-company-help-window nil)
(defvar helm-company-backend nil)
(defun helm-company-init ()
"Prepare helm for company."
(helm-attrset 'company-candidates company-candidates)
(helm-attrset 'company-common company-common)
(setq helm-company-help-window nil)
(if (<= (length company-candidates) 1)
(helm-exit-minibuffer)
(setq helm-company-backend company-backend))
(company-abort))
(defun helm-company-action-insert (candidate)
"Insert CANDIDATE."
(delete-char (- (length (helm-attr 'company-common))))
(insert candidate)
;; for GC
(helm-attrset 'company-candidates nil))
(defun helm-company-action-show-document (candidate)
"Show the documentation of the CANDIDATE."
(interactive)
(let ((buffer (funcall helm-company-backend 'doc-buffer candidate)))
(when buffer
(display-buffer buffer))))
(defun helm-company-show-doc-buffer (candidate)
"Temporarily show the documentation buffer for the CANDIDATE."
(interactive)
(let ((buffer (funcall helm-company-backend 'doc-buffer candidate)))
(when buffer
(if (and helm-company-help-window
(window-live-p helm-company-help-window))
(with-selected-window helm-company-help-window
(helm-company-display-document-buffer buffer))
(setq helm-company-help-window
(helm-company-display-document-buffer buffer))))))
(defun helm-company-find-location (candidate)
"Find location of CANDIDATE."
(interactive)
(let* ((location (save-excursion (funcall helm-company-backend 'location candidate)))
(pos (or (cdr location) (error "No location available")))
(buffer (or (and (bufferp (car location)) (car location))
(find-file-noselect (car location) t))))
(with-selected-window (display-buffer buffer t)
(save-restriction
(widen)
(if (bufferp (car location))
(goto-char pos)
(goto-char (point-min))
(forward-line (1- pos))))
(set-window-start nil (point)))))
(defun helm-company-display-document-buffer (buffer)
"Temporarily show the documentation BUFFER."
(with-current-buffer buffer
(goto-char (point-min)))
(display-buffer buffer
'((display-buffer-same-window . t)
(display-buffer-reuse-window . t))))
(defmacro helm-company-run-action (&rest body)
`(with-helm-window
(save-selected-window
(with-helm-display-same-window
,@body))))
(defun helm-company-run-show-doc-buffer ()
"Run showing documentation action from `helm-company'."
(interactive)
(helm-company-run-action
(helm-company-show-doc-buffer (helm-get-selection))))
(defun helm-company-run-show-location ()
"Run showing location action from `helm-company'."
(interactive)
(helm-company-run-action
(helm-company-find-location (helm-get-selection))))
(defvar helm-company-map
(let ((keymap (make-sparse-keymap)))
(set-keymap-parent keymap helm-map)
(define-key keymap (kbd "M-s") 'helm-company-run-show-location)
(define-key keymap (kbd "C-s") 'helm-company-run-show-doc-buffer)
(delq nil keymap))
"Keymap used in Company sources.")
(defvar helm-company-actions
'(("Insert" . helm-company-action-insert)
("Show documentation (If available)" . helm-company-action-show-document)
("Find location (If available)" . helm-company-find-location))
"Actions for `helm-company'.")
(defcustom helm-company-fuzzy-match t
"Enable fuzzy matching for Helm Company."
:type 'boolean)
(defvar helm-source-company
(helm-build-in-buffer-source "Company"
:data (lambda ()
(helm-company-init)
(helm-attr 'company-candidates))
:fuzzy-match helm-company-fuzzy-match
:keymap helm-company-map
:persistent-action 'helm-company-show-doc-buffer
:persistent-help "Show document (If available)"
:action helm-company-actions)
"Helm source definition for recent files in current project.")
;;;###autoload
(defun helm-company ()
"Select `company-complete' candidates by `helm'.
It is useful to narrow candidates."
(interactive)
(unless company-candidates
(company-complete))
(when company-point
(helm :sources 'helm-source-company
:buffer "*helm company*"
:candidate-number-limit helm-company-candidate-number-limit)))
(provide 'helm-company)
;; Local Variables:
;; coding: utf-8
;; eval: (setq byte-compile-not-obsolete-vars '(display-buffer-function))
;; eval: (checkdoc-minor-mode 1)
;; End:
;;; helm-company.el ends here