;;
;; Known Issues:
;; When point is at the very end of the buffer, the pseudo-tooltip appears very
-;; wrong.
+;; wrong, unless company is allowed to temporarily insert a fake newline.
+;; This behavior is enabled by `company-end-of-buffer-workaround'.
;;
;;; Change Log:
;;
+;; Added etags back-end.
+;; Added work-around for end-of-buffer bug.
;; Added `company-filter-candidates'.
;; More local Lisp variables are now included in the candidates.
;;
"*Face used for the common part of the completion preview."
:group 'company)
+(defface company-preview-search
+ '((t :inherit company-preview
+ :background "blue1"))
+ "*Face used for the search string in the completion preview."
+ :group 'company)
+
(defface company-echo nil
"*Face used for completions in the echo area."
:group 'company)
(set variable value))
(defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
- company-preview-if-just-one-frontend
+ company-preview-frontend
company-echo-metadata-frontend)
"*The list of active front-ends (visualizations).
Each front-end is a function that takes one argument. It is called with
(function :tag "custom function" nil))))
(defcustom company-backends '(company-elisp company-nxml company-css
- company-semantic company-gtags company-oddmuse
- company-files company-dabbrev)
+ company-semantic company-gtags company-etags
+ company-oddmuse company-files company-dabbrev)
"*The list of active back-ends (completion engines).
Each back-end is a function that takes a variable number of arguments.
The first argument is the command requested from the back-end. It is one
:type '(choice (const :tag "off" nil)
(const :tag "on" t)))
+(defvar company-end-of-buffer-workaround t
+ "*Work around a visualization bug when completing at the end of the buffer.
+The work-around consists of adding a newline.")
+
;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar company-mode-map (make-sparse-keymap)
called before `company-idle-delay', completion will also start.
Completions can be searched with `company-search-candidates' or
-`company-filter-candidates'.
+`company-filter-candidates'. These can be used while completion is
+inactive, as well.
The completion data is retrieved using `company-backends' and displayed using
`company-frontends'.
-regular keymap:
+regular keymap (`company-mode-map'):
\\{company-mode-map}
-keymap during active completions:
+keymap during active completions (`company-active-map'):
\\{company-active-map}"
nil " comp" company-mode-map
(defvar company-timer nil)
+(defvar company-added-newline nil)
+(make-variable-buffer-local 'company-added-newline)
+
(defsubst company-strip-prefix (str)
(substring str (length company-prefix)))
(unless (funcall company-backend 'sorted)
(setq candidates (sort candidates 'string<)))
candidates)))
+ (unless company-candidates-cache
+ (company-call-frontends 'show))
(unless (assoc prefix company-candidates-cache)
(push (cons prefix company-candidates) company-candidates-cache))
company-candidates)
(return prefix)))))
(if company-candidates
(progn
+ (when (and company-end-of-buffer-workaround (eobp))
+ (save-excursion (insert "\n"))
+ (setq company-added-newline (buffer-chars-modified-tick)))
(setq company-point (point))
(company-enable-overriding-keymap company-active-map)
(company-call-frontends 'update))
(company-cancel)))
(defun company-cancel ()
- (setq company-backend nil
+ (and company-added-newline
+ (> (point-max) (point-min))
+ (let ((tick (buffer-chars-modified-tick)))
+ (delete-region (1- (point-max)) (point-max))
+ (equal tick company-added-newline))
+ ;; Only set unmodified when tick remained the same since insert.
+ (set-buffer-modified-p nil))
+ (setq company-added-newline nil
+ company-backend nil
company-prefix nil
company-candidates nil
company-candidates-length nil
;; Don't start again, unless started manually.
(setq company-point (point)))
+(defsubst company-keep (command)
+ (and (symbolp command) (get command 'company-keep)))
+
(defun company-pre-command ()
- (unless (eq this-command 'company-show-doc-buffer)
+ (unless (company-keep this-command)
(condition-case err
(when company-candidates
(company-call-frontends 'pre-command))
(company-uninstall-map))
(defun company-post-command ()
- (unless (eq this-command 'company-show-doc-buffer)
+ (unless (company-keep this-command)
(condition-case err
(progn
(unless (equal (point) company-point)
(let ((l (generic-character-list))
(table (nth 1 keymap)))
(while l
- (set-char-table-default table (car l) 'isearch-printing-char)
+ (set-char-table-default table (car l) 'company-search-printing-char)
(setq l (cdr l))))))
(define-key keymap [t] 'company-search-other-char)
(while (< i ?\s)
(erase-buffer)
(current-buffer)))
+(defmacro company-electric (&rest body)
+ (declare (indent 0) (debug t))
+ `(if company-mode
+ (when (company-manual-begin)
+ (save-window-excursion
+ (let ((height (window-height))
+ (row (cdr (posn-col-row (posn-at-point)))))
+ ,@body
+ (and (< (window-height) height)
+ (< (- (window-height) row 2) company-tooltip-limit)
+ (recenter (- (window-height) row 2)))
+ (while (eq 'scroll-other-window
+ (key-binding (vector (list (read-event)))))
+ (call-interactively 'scroll-other-window))
+ (when last-input-event
+ (clear-this-command-keys t)
+ (setq unread-command-events (list last-input-event))))))
+ (error "Company not enabled")))
+
(defun company-show-doc-buffer ()
"Temporarily show a buffer with the complete documentation for the selection."
(interactive)
- (unless company-mode (error "Company not enabled"))
- (when (company-manual-begin)
- (save-window-excursion
- (let* ((height (window-height))
- (row (cdr (posn-col-row (posn-at-point))))
- (selected (nth company-selection company-candidates))
- (buffer (funcall company-backend 'doc-buffer selected)))
- (if (not buffer)
- (error "No documentation available.")
- (display-buffer buffer)
- (and (< (window-height) height)
- (< (- (window-height) row 2) company-tooltip-limit)
- (recenter (- (window-height) row 2)))
- (while (eq 'scroll-other-window
- (key-binding (vector (list (read-event)))))
- (scroll-other-window))
- (when last-input-event
- (clear-this-command-keys t)
- (setq unread-command-events (list last-input-event))))))))
+ (company-electric
+ (let ((selected (nth company-selection company-candidates)))
+ (display-buffer (or (funcall company-backend 'doc-buffer selected)
+ (error "No documentation available")) t))))
+(put 'company-show-doc-buffer 'company-keep t)
;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq company-preview-overlay (make-overlay pos pos))
- (let ((completion (company-strip-prefix (nth company-selection
- company-candidates))))
+ (let ((completion(nth company-selection company-candidates)))
+ (setq completion (propertize completion 'face 'company-preview))
+ (add-text-properties 0 (length company-common)
+ '(face company-preview-common) completion)
+
+ ;; Add search string
+ (and company-search-string
+ (string-match (regexp-quote company-search-string) completion)
+ (add-text-properties (match-beginning 0)
+ (match-end 0)
+ '(face company-preview-search)
+ completion))
+
+ (setq completion (company-strip-prefix completion))
+
(and (equal pos (point))
(not (equal completion ""))
(add-text-properties 0 1 '(cursor t) completion))
- (setq completion (propertize completion 'face 'company-preview))
- (add-text-properties 0 (- (length company-common) (length company-prefix))
- '(face company-preview-common) completion)
-
(overlay-put company-preview-overlay 'after-string completion)
(overlay-put company-preview-overlay 'window (selected-window))))