1 (eval-when-compile (require 'cl))
9 (defface company-tooltip
10 '((t :background "yellow"
15 (defface company-tooltip-selection
16 '((t :background "orange1"
21 (defface company-tooltip-common
22 '((t :inherit company-tooltip
27 (defface company-tooltip-common-selection
28 '((t :inherit company-tooltip-selection
33 (defcustom company-tooltip-limit 10
38 (defface company-preview
39 '((t :background "blue4"
44 (defface company-preview-common
45 '((t :inherit company-preview
50 (defcustom company-backends '(company-elisp-completion)
53 :type '(repeat (function :tag "function" nil)))
55 (defcustom company-minimum-prefix-length 3
58 :type '(integer :tag "prefix length"))
60 (defvar company-timer nil)
62 (defun company-timer-set (variable value)
64 (when company-timer (cancel-timer company-timer))
66 (setq company-timer (run-with-idle-timer value t 'company-idle-begin))))
68 (defcustom company-idle-delay .7
70 :set 'company-timer-set
72 :type '(choice (const :tag "never (nil)" nil)
73 (const :tag "immediate (t)" t)
74 (number :tag "seconds")))
76 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 (defvar company-mode-map
79 (let ((keymap (make-sparse-keymap)))
80 (define-key keymap (kbd "M-n") 'company-select-next)
81 (define-key keymap (kbd "M-p") 'company-select-previous)
82 (define-key keymap (kbd "M-<return>") 'company-complete-selection)
83 (define-key keymap "\t" 'company-complete-common)
87 (define-minor-mode company-mode
89 nil " comp" company-mode-map
92 (add-hook 'pre-command-hook 'company-pre-command nil t)
93 (add-hook 'post-command-hook 'company-post-command nil t)
94 (company-timer-set 'company-idle-delay
96 (remove-hook 'pre-command-hook 'company-pre-command t)
97 (remove-hook 'post-command-hook 'company-post-command t)
100 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 (defun company-grab (regexp &optional expression)
103 (when (looking-back regexp)
104 (or (match-string-no-properties (or expression 0)) "")))
106 (defun company-in-string-or-comment (&optional point)
107 (let ((pos (syntax-ppss)))
108 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
112 (defvar company-lisp-symbol-regexp
113 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
115 (defun company-grab-lisp-symbol ()
116 (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
117 (unless (and (company-in-string-or-comment (- (point) (length prefix)))
118 (/= (char-before (- (point) (length prefix))) ?`))
121 (defun company-elisp-completion (command &optional arg &rest ignored)
123 ('prefix (and (eq major-mode 'emacs-lisp-mode)
124 (company-grab-lisp-symbol)))
125 ('candidates (let ((completion-ignore-case nil))
126 (all-completions arg obarray
127 (lambda (symbol) (or (boundp symbol)
128 (fboundp symbol))))))))
130 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 (defvar company-backend nil)
133 (make-variable-buffer-local 'company-backend)
135 (defvar company-prefix nil)
136 (make-variable-buffer-local 'company-prefix)
138 (defvar company-candidates nil)
139 (make-variable-buffer-local 'company-candidates)
141 (defvar company-common nil)
142 (make-variable-buffer-local 'company-common)
144 (defvar company-selection 0)
145 (make-variable-buffer-local 'company-selection)
147 (defvar company-selection-changed nil)
148 (make-variable-buffer-local 'company-selection-changed)
150 (defvar company-point nil)
151 (make-variable-buffer-local 'company-point)
153 (defsubst company-strip-prefix (str)
154 (substring str (length company-prefix)))
156 (defsubst company-offset (display-limit)
157 (let ((offset (- company-selection display-limit -1)))
160 (defsubst company-should-complete (prefix)
161 (and (eq company-idle-delay t)
162 (>= (length prefix) company-minimum-prefix-length)))
164 (defun company-idle-begin ()
166 (not company-candidates)
167 (let ((company-idle-delay t))
169 (company-post-command))))
171 (defun company-manual-begin ()
173 (not company-candidates)
174 (let ((company-idle-delay t)
175 (company-minimum-prefix-length 0))
177 ;; Return non-nil if active.
180 (defun company-continue-or-cancel ()
181 (when company-candidates
182 (let ((old-point (- company-point (length company-prefix)))
183 (company-idle-delay t)
184 (company-minimum-prefix-length 0))
185 ;; TODO: Make more efficient.
186 (setq company-candidates nil)
188 (unless (and company-candidates
189 (equal old-point (- company-point (length company-prefix))))
191 company-candidates)))
193 (defun company-begin ()
194 (or (company-continue-or-cancel)
195 (let ((completion-ignore-case nil) ;; TODO: make this optional
197 (dolist (backend company-backends)
198 (when (setq prefix (funcall backend 'prefix))
199 (when (company-should-complete prefix)
200 (setq company-backend backend
201 company-prefix prefix
203 (funcall company-backend 'candidates prefix)
204 company-common (try-completion prefix company-candidates)
206 company-point (point)))
208 (unless (and company-candidates
209 (not (eq t company-common)))
212 (defun company-cancel ()
213 (setq company-backend nil
215 company-candidates nil
218 company-selection-changed nil
220 (company-pseudo-tooltip-hide))
222 (defun company-pre-command ()
223 (company-preview-hide)
224 (company-pseudo-tooltip-hide))
226 (defun company-post-command ()
227 (unless (equal (point) company-point)
229 (when company-candidates
230 (company-pseudo-tooltip-show-at-point (- (point) (length company-prefix))
233 (company-preview-show-at-point (point) company-candidates
236 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 (defun company-select-next ()
240 (when (company-manual-begin)
241 (setq company-selection (min (1- (length company-candidates))
242 (1+ company-selection))
243 company-selection-changed t)))
245 (defun company-select-previous ()
247 (when (company-manual-begin)
248 (setq company-selection (max 0 (1- company-selection))
249 company-selection-changed t)))
251 (defun company-complete-selection ()
253 (when (company-manual-begin)
254 (insert (company-strip-prefix (nth company-selection company-candidates)))))
256 (defun company-complete-common ()
258 (when (company-manual-begin)
259 (insert (company-strip-prefix company-common))))
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 (defconst company-space-strings-limit 100)
265 (defconst company-space-strings
267 (dotimes (i company-space-strings-limit)
268 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
269 (apply 'vector lst)))
271 (defsubst company-space-string (len)
272 (if (< len company-space-strings-limit)
273 (aref company-space-strings len)
274 (make-string len ?\ )))
276 (defsubst company-safe-substring (str from &optional to)
277 (let ((len (length str)))
280 (if (and to (> to len))
281 (concat (substring str from)
282 (company-space-string (- to len)))
283 (substring str from to)))))
285 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 (defvar company-pseudo-tooltip-overlay nil)
288 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
292 (defun company-fill-propertize (line width selected)
293 (setq line (company-safe-substring line 0 width))
294 (add-text-properties 0 width
295 (list 'face (if selected
296 'company-tooltip-selection
297 'company-tooltip)) line)
298 (add-text-properties 0 (length company-common)
299 (list 'face (if selected
300 'company-tooltip-common-selection
301 'company-tooltip-common)) line)
304 (defun company-fill-propertize-lines (column lines selection)
307 (len (min company-tooltip-limit (length lines)))
310 (setq width (max (length (pop lines-copy)) width)))
311 (setq width (min width (- (window-width) column)))
313 (push (company-fill-propertize (pop lines) width (equal i selection))
319 (defun company-buffer-lines (beg end)
321 (let ((row (cdr (posn-col-row (posn-at-point))))
323 (while (< (point) end)
324 (move-to-window-line (incf row))
325 (push (buffer-substring beg (min end (1- (point)))) lines)
329 (defun company-modify-line (old new offset)
330 (concat (company-safe-substring old 0 offset)
332 (company-safe-substring old (+ offset (length new)))))
334 (defun company-modified-substring (beg end lines column)
335 (let ((old (company-buffer-lines beg end))
337 ;; Inject into old lines.
339 (push (company-modify-line (pop old) (pop lines) column) new))
340 ;; Append whole new lines.
342 (push (company-modify-line "" (pop lines) column) new))
343 (concat (mapconcat 'identity (nreverse new) "\n")
348 (defun company-pseudo-tooltip-show (row column lines &optional selection)
349 (company-pseudo-tooltip-hide)
350 (unless lines (error "No text provided"))
354 (let ((offset (company-offset company-tooltip-limit)))
355 (setq lines (nthcdr offset lines))
356 (decf selection offset))
358 (setq lines (company-fill-propertize-lines column lines selection))
362 (move-to-window-line row)
365 (move-to-window-line (min (window-height)
366 (+ row company-tooltip-limit)))
370 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
372 (overlay-put company-pseudo-tooltip-overlay 'before-string
373 (company-modified-substring beg end lines column))
374 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
375 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
377 (defun company-pseudo-tooltip-show-at-point (pos text &optional selection)
378 (let ((col-row (posn-col-row (posn-at-point pos))))
379 (company-pseudo-tooltip-show (1+ (cdr col-row))
380 (car col-row) text selection)))
382 (defun company-pseudo-tooltip-hide ()
383 (when company-pseudo-tooltip-overlay
384 (delete-overlay company-pseudo-tooltip-overlay)
385 (setq company-pseudo-tooltip-overlay nil)))
387 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 (defvar company-preview-overlay nil)
390 (make-variable-buffer-local 'company-preview-overlay)
392 (defun company-preview-show-at-point (pos text &optional selection)
393 (company-preview-hide)
395 (setq company-preview-overlay (make-overlay pos pos))
397 (let ((completion (company-strip-prefix (nth company-selection
398 company-candidates))))
399 (and (equal pos (point))
400 (not (equal completion ""))
401 (add-text-properties 0 1 '(cursor t) completion))
403 (setq completion (propertize completion 'face 'company-preview))
404 (add-text-properties 0 (- (length company-common) (length company-prefix))
405 '(face company-preview-common) completion)
407 (overlay-put company-preview-overlay 'after-string completion)
408 (overlay-put company-preview-overlay 'window (selected-window))))
410 (defun company-preview-hide ()
411 (when company-preview-overlay
412 (delete-overlay company-preview-overlay)
413 (setq company-preview-overlay nil)))
416 ;;; company.el ends here