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 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 (defvar company-mode-map
58 (let ((keymap (make-sparse-keymap)))
59 (define-key keymap (kbd "M-n") 'company-select-next)
60 (define-key keymap (kbd "M-p") 'company-select-previous)
61 (define-key keymap (kbd "M-<return>") 'company-complete-selection)
62 (define-key keymap "\t" 'company-complete-common)
66 (define-minor-mode company-mode
68 nil " comp" company-mode-map
71 (add-hook 'pre-command-hook 'company-pre-command nil t)
72 (add-hook 'post-command-hook 'company-post-command nil t))
73 (remove-hook 'pre-command-hook 'company-pre-command t)
74 (remove-hook 'post-command-hook 'company-post-command t)
77 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 (defun company-grab (regexp &optional expression)
80 (when (looking-back regexp)
81 (or (match-string-no-properties (or expression 0)) "")))
83 (defun company-in-string-or-comment (&optional point)
84 (let ((pos (syntax-ppss)))
85 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
89 (defvar company-lisp-symbol-regexp
90 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
92 (defun company-grab-lisp-symbol ()
93 (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
94 (unless (and (company-in-string-or-comment (- (point) (length prefix)))
95 (/= (char-before (- (point) (length prefix))) ?`))
98 (defun company-elisp-completion (command &optional arg &rest ignored)
100 ('prefix (and (eq major-mode 'emacs-lisp-mode)
101 (company-grab-lisp-symbol)))
102 ('candidates (let ((completion-ignore-case nil))
103 (all-completions arg obarray
104 (lambda (symbol) (or (boundp symbol)
105 (fboundp symbol))))))))
107 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109 (defvar company-backend nil)
110 (make-variable-buffer-local 'company-backend)
112 (defvar company-prefix nil)
113 (make-variable-buffer-local 'company-prefix)
115 (defvar company-candidates nil)
116 (make-variable-buffer-local 'company-candidates)
118 (defvar company-common nil)
119 (make-variable-buffer-local 'company-common)
121 (defvar company-selection 0)
122 (make-variable-buffer-local 'company-selection)
124 (defvar company-selection-changed nil)
125 (make-variable-buffer-local 'company-selection-changed)
127 (defvar company-point nil)
128 (make-variable-buffer-local 'company-point)
130 (defsubst company-strip-prefix (str)
131 (substring str (length company-prefix)))
133 (defsubst company-offset (display-limit)
134 (let ((offset (- company-selection display-limit -1)))
137 (defun company-begin ()
138 (let ((completion-ignore-case nil) ;; TODO: make this optional
140 (dolist (backend company-backends)
141 (when (setq prefix (funcall backend 'prefix))
142 (setq company-backend backend
143 company-prefix prefix
145 (funcall company-backend 'candidates prefix)
146 company-common (try-completion prefix company-candidates)
148 company-point (point))
150 (unless (and company-candidates
151 (not (eq t company-common)))
154 (defun company-cancel ()
155 (setq company-backend nil
157 company-candidates nil
160 company-selection-changed nil
162 (company-pseudo-tooltip-hide))
164 (defun company-pre-command ()
165 (company-preview-hide)
166 (company-pseudo-tooltip-hide))
168 (defun company-post-command ()
169 (unless (equal (point) company-point)
171 (when company-candidates
172 (company-pseudo-tooltip-show-at-point (- (point) (length company-prefix))
175 (company-preview-show-at-point (point) company-candidates
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 (defun company-select-next ()
182 (setq company-selection (min (1- (length company-candidates))
183 (1+ company-selection))
184 company-selection-changed t))
186 (defun company-select-previous ()
188 (setq company-selection (max 0 (1- company-selection))
189 company-selection-changed t))
191 (defun company-complete-selection ()
193 (insert (company-strip-prefix (nth company-selection company-candidates))))
195 (defun company-complete-common ()
197 (insert (company-strip-prefix company-common)))
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 (defconst company-space-strings-limit 100)
203 (defconst company-space-strings
205 (dotimes (i company-space-strings-limit)
206 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
207 (apply 'vector lst)))
209 (defsubst company-space-string (len)
210 (if (< len company-space-strings-limit)
211 (aref company-space-strings len)
212 (make-string len ?\ )))
214 (defsubst company-safe-substring (str from &optional to)
215 (let ((len (length str)))
218 (if (and to (> to len))
219 (concat (substring str from)
220 (company-space-string (- to len)))
221 (substring str from to)))))
223 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 (defvar company-pseudo-tooltip-overlay nil)
226 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
230 (defun company-fill-propertize (line width selected)
231 (setq line (company-safe-substring line 0 width))
232 (add-text-properties 0 width
233 (list 'face (if selected
234 'company-tooltip-selection
235 'company-tooltip)) line)
236 (add-text-properties 0 (length company-common)
237 (list 'face (if selected
238 'company-tooltip-common-selection
239 'company-tooltip-common)) line)
242 (defun company-fill-propertize-lines (column lines selection)
245 (len (min company-tooltip-limit (length lines)))
248 (setq width (max (length (pop lines-copy)) width)))
249 (setq width (min width (- (window-width) column)))
251 (push (company-fill-propertize (pop lines) width (equal i selection))
257 (defun company-buffer-lines (beg end)
259 (let ((row (cdr (posn-col-row (posn-at-point))))
261 (while (< (point) end)
262 (move-to-window-line (incf row))
263 (push (buffer-substring beg (min end (1- (point)))) lines)
267 (defun company-modify-line (old new offset)
268 (concat (company-safe-substring old 0 offset)
270 (company-safe-substring old (+ offset (length new)))))
272 (defun company-modified-substring (beg end lines column)
273 (let ((old (company-buffer-lines beg end))
275 ;; Inject into old lines.
277 (push (company-modify-line (pop old) (pop lines) column) new))
278 ;; Append whole new lines.
280 (push (company-modify-line "" (pop lines) column) new))
281 (concat (mapconcat 'identity (nreverse new) "\n")
286 (defun company-pseudo-tooltip-show (row column lines &optional selection)
287 (company-pseudo-tooltip-hide)
288 (unless lines (error "No text provided"))
292 (let ((offset (company-offset company-tooltip-limit)))
293 (setq lines (nthcdr offset lines))
294 (decf selection offset))
296 (setq lines (company-fill-propertize-lines column lines selection))
300 (move-to-window-line row)
303 (move-to-window-line (min (window-height)
304 (+ row company-tooltip-limit)))
308 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
310 (overlay-put company-pseudo-tooltip-overlay 'before-string
311 (company-modified-substring beg end lines column))
312 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
313 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
315 (defun company-pseudo-tooltip-show-at-point (pos text &optional selection)
316 (let ((col-row (posn-col-row (posn-at-point pos))))
317 (company-pseudo-tooltip-show (1+ (cdr col-row))
318 (car col-row) text selection)))
320 (defun company-pseudo-tooltip-hide ()
321 (when company-pseudo-tooltip-overlay
322 (delete-overlay company-pseudo-tooltip-overlay)
323 (setq company-pseudo-tooltip-overlay nil)))
325 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 (defvar company-preview-overlay nil)
328 (make-variable-buffer-local 'company-preview-overlay)
330 (defun company-preview-show-at-point (pos text &optional selection)
331 (company-preview-hide)
333 (setq company-preview-overlay (make-overlay pos pos))
335 (let ((completion (company-strip-prefix (nth company-selection
336 company-candidates))))
337 (and (equal pos (point))
338 (not (equal completion ""))
339 (add-text-properties 0 1 '(cursor t) completion))
341 (setq completion (propertize completion 'face 'company-preview))
342 (add-text-properties 0 (- (length company-common) (length company-prefix))
343 '(face company-preview-common) completion)
345 (overlay-put company-preview-overlay 'after-string completion)
346 (overlay-put company-preview-overlay 'window (selected-window))))
348 (defun company-preview-hide ()
349 (when company-preview-overlay
350 (delete-overlay company-preview-overlay)
351 (setq company-preview-overlay nil)))
354 ;;; company.el ends here