;; Author: Nikolaj Schumacher
;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
-;; Version: 0.6
+;; Version: 0.6.2
;; Keywords: abbrev, convenience, matching
;; URL: http://company-mode.github.com/
;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
number where the completion candidate was defined.
`require-match': If this value is t, the user is not allowed to enter anything
-not offering as a candidate. Use with care! The default value nil gives the
+not offered as a candidate. Use with care! The default value nil gives the
user that choice with `company-require-match'. Return value 'never overrides
that option the other way around.
+`init': Called once for each buffer, the back-end can check for external
+programs and files and load any required libraries. Raising an error here will
+show up in message log once, and the backend will not be used for completion.
+
+`post-completion': Called after a completion candidate has been inserted into
+the buffer. The second argument is the candidate. Can be used to modify it,
+e.g. to expand a snippet.
+
The back-end should return nil for all commands it does not support or
does not know about. It should also be callable interactively and use
`company-begin-backend' to start itself in that case."
(defcustom company-completion-finished-hook nil
"Hook run when company successfully completes.
-The hook is called with the selected candidate as an argument."
+The hook is called with the selected candidate as an argument.
+
+If you indend to use it to post-process candidates from a specific back-end,
+consider using the `post-completion' command instead."
:group 'company
:type 'hook)
This can also be a function, which is called with the new input and should
return non-nil if company should auto-complete.
-A character that is part of a valid candidate never starts auto-completion."
+A character that is part of a valid candidate never triggers auto-completion."
:group 'company
:type '(choice (string :tag "Characters")
(set :tag "Syntax"
(company-cancel)
(kill-local-variable 'company-point)))
-(define-globalized-minor-mode global-company-mode company-mode
- (lambda () (unless (or noninteractive (eq (aref (buffer-name) 0) ?\s))
- (company-mode 1))))
+;;;###autoload
+(define-globalized-minor-mode global-company-mode company-mode company-mode-on)
+
+(defun company-mode-on ()
+ (unless (or noninteractive (eq (aref (buffer-name) 0) ?\s))
+ (company-mode 1)))
(defsubst company-assert-enabled ()
(unless company-mode
(apply 'company--multi-backend-adapter company-backend args)))
(defun company--multi-backend-adapter (backends command &rest args)
- (let ((backends (remove-if (lambda (b) (eq 'failed (get b 'company-init)))
- backends)))
+ (let ((backends (loop for b in backends
+ when (not (and (symbolp b)
+ (eq 'failed (get b 'company-init))))
+ collect b)))
(case command
(candidates
(loop for backend in backends
when (equal (funcall backend 'prefix)
(car args))
- nconc (apply backend 'candidates args)))
+ append (apply backend 'candidates args)))
(sorted nil)
(duplicates t)
(otherwise
"Non-nil, if explicit completion took place.")
(make-variable-buffer-local 'company--explicit-action)
+(defvar company--auto-completion nil
+ "Non-nil when current candidate is being completed automatically.
+Controlled by `company-auto-complete'.")
+
(defvar company--point-max nil)
(make-variable-buffer-local 'company--point-max)
(push (cons company-prefix company-candidates) company-candidates-cache)
;; Calculate common.
(let ((completion-ignore-case (company-call-backend 'ignore-case)))
- (setq company-common (try-completion company-prefix company-candidates)))
+ (setq company-common (company--safe-candidate
+ (try-completion company-prefix company-candidates))))
(when (eq company-common t)
(setq company-candidates nil)))
+(defun company--safe-candidate (str)
+ (or (company-call-backend 'crop str)
+ str))
+
(defun company-calculate-candidates (prefix)
(let ((candidates (cdr (assoc prefix company-candidates-cache)))
(ignore-case (company-call-backend 'ignore-case)))
(while c2
(setcdr c2 (progn (while (equal (pop c2) (car c2)))
c2)))))))
- (if (and candidates
- (or (cdr candidates)
- (not (eq t (compare-strings (car candidates) nil nil
- prefix nil nil ignore-case)))))
- ;; Don't start when already completed and unique.
- candidates
- ;; Not the right place? maybe when setting?
- (and company-candidates t))))
+ (when candidates
+ (if (or (cdr candidates)
+ (not (eq t (compare-strings (car candidates) nil nil
+ prefix nil nil ignore-case))))
+ candidates
+ ;; Already completed and unique; don't start.
+ t))))
(defun company-idle-begin (buf win tick pos)
(and company-mode
;; auto-complete
(save-excursion
(goto-char company-point)
- (company-complete-selection)
+ (let ((company--auto-completion t))
+ (company-complete-selection))
nil))
((and (company--string-incremental-p company-prefix new-prefix)
(company-require-match-p))
(defun company--good-prefix-p (prefix)
(and (or (company-explicit-action-p)
- (>= (or (cdr-safe prefix) (length prefix))
- company-minimum-prefix-length))
+ (unless (eq prefix 'stop)
+ (>= (or (cdr-safe prefix) (length prefix))
+ company-minimum-prefix-length)))
(stringp (or (car-safe prefix) prefix))))
(defun company--continue ()
"Complete the selected candidate."
(interactive)
(when (company-manual-begin)
- (company-finish (nth company-selection company-candidates))))
+ (let ((result (nth company-selection company-candidates)))
+ (when company--auto-completion
+ (setq result (company--safe-candidate result)))
+ (company-finish result))))
(defun company-complete-common ()
"Complete the common part of all candidates."
(setq company-backend backend)
;; Return non-nil if active.
(or (company-manual-begin)
- (error "Cannot complete at point")))
+ (progn
+ (setq company-backend nil)
+ (error "Cannot complete at point"))))
(defun company-begin-with (candidates
&optional prefix-length require-match callback)
(overlay-get company-pseudo-tooltip-overlay 'company-before))
(overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
+(defun company-pseudo-tooltip-guard ()
+ (buffer-substring-no-properties
+ (point) (overlay-start company-pseudo-tooltip-overlay)))
+
(defun company-pseudo-tooltip-frontend (command)
"A `company-mode' front-end similar to a tool-tip but based on overlays."
(case command
0))
(new-height (company--pseudo-tooltip-height)))
(unless (and (>= (* old-height new-height) 0)
- (>= (abs old-height) (abs new-height)))
+ (>= (abs old-height) (abs new-height))
+ (equal (company-pseudo-tooltip-guard)
+ (overlay-get company-pseudo-tooltip-overlay
+ 'company-guard)))
;; Redraw needed.
(company-pseudo-tooltip-show-at-point (- (point)
- (length company-prefix)))))
+ (length company-prefix)))
+ (overlay-put company-pseudo-tooltip-overlay
+ 'company-guard (company-pseudo-tooltip-guard))))
(company-pseudo-tooltip-unhide))
(hide (company-pseudo-tooltip-hide)
(setq company-tooltip-offset 0))
(post-command (company-echo-show-when-idle 'company-fetch-metadata))
(hide (company-echo-hide))))
-;; templates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-(autoload 'company-template-declare-template "company-template")
-
(provide 'company)
;;; company.el ends here