;; Author: Nikolaj Schumacher
;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
;; URL: http://company-mode.github.io/
-;; Version: 0.8.5
+;; Version: 0.8.7
;; Keywords: abbrev, convenience, matching
;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
(defcustom company-begin-commands '(self-insert-command
org-self-insert-command
+ orgtbl-self-insert-command
c-scope-operator
c-electric-colon
c-electric-lt-gt
res))))
(defun company-call-backend-raw (&rest args)
- (condition-case err
+ (condition-case-unless-debug err
(if (functionp company-backend)
(apply company-backend args)
(apply #'company--multi-backend-adapter company-backend args))
(defun company-call-frontends (command)
(dolist (frontend company-frontends)
- (condition-case err
+ (condition-case-unless-debug err
(funcall frontend command)
(error (error "Company: Front-end %s error \"%s\" on command %s"
frontend (error-message-string err) command)))))
(eq pos (point))
(when (company-auto-begin)
(company-input-noop)
- (company-post-command))))
+ (let ((this-command 'company-idle-begin))
+ (company-post-command)))))
(defun company-auto-begin ()
(and company-mode
(defun company-pre-command ()
(unless (company-keep this-command)
- (condition-case err
+ (condition-case-unless-debug err
(when company-candidates
(company-call-frontends 'pre-command)
(unless (company--should-continue)
(company-uninstall-map))
(defun company-post-command ()
+ (when (null this-command)
+ ;; Happens when the user presses `C-g' while inside
+ ;; `flyspell-post-command-hook', for example.
+ ;; Or any other `post-command-hook' function that can call `sit-for',
+ ;; or any quittable timer function.
+ (company-abort)
+ (setq this-command 'company-abort))
(unless (company-keep this-command)
- (condition-case err
+ (condition-case-unless-debug err
(progn
(unless (equal (point) company-point)
(let (company-idle-delay) ; Against misbehavior while debugging.
(defun company-search-printing-char ()
(interactive)
(company-search-assert-enabled)
- (setq company-search-string
- (concat (or company-search-string "") (string last-command-event))
- company-search-lighter (concat " Search: \"" company-search-string
- "\""))
- (let ((pos (company-search company-search-string
- (nthcdr company-selection company-candidates))))
+ (let* ((ss (concat company-search-string (string last-command-event)))
+ (pos (company-search ss (nthcdr company-selection company-candidates))))
(if (null pos)
(ding)
+ (setq company-search-string ss
+ company-search-lighter (concat " Search: \"" ss "\""))
(company-set-selection (+ company-selection pos) t))))
(defun company-search-repeat-forward ()
(company-set-selection (- company-selection pos 1) t))))
(defun company-create-match-predicate ()
- (setq company-candidates-predicate
- `(lambda (candidate)
- ,(if company-candidates-predicate
- `(and (string-match ,company-search-string candidate)
- (funcall ,company-candidates-predicate
- candidate))
- `(string-match ,company-search-string candidate))))
+ (let ((ss company-search-string))
+ (setq company-candidates-predicate
+ (when ss (lambda (candidate) (string-match ss candidate)))))
(company-update-candidates
(company-apply-predicate company-candidates company-candidates-predicate))
;; Invalidate cache.
(setq this-command 'company-complete-common))))
(defun company-complete-number (n)
- "Insert the Nth candidate.
+ "Insert the Nth candidate visible in the tooltip.
To show the number next to the candidates in some back-ends, enable
`company-show-numbers'. When called interactively, uses the last typed
character, stripping the modifiers. That character must be a digit."
(interactive
- (list (let ((n (- (event-basic-type last-command-event) ?0)))
+ (list (let* ((type (event-basic-type last-command-event))
+ (char (if (characterp type)
+ ;; Number on the main row.
+ type
+ ;; Keypad number, if bound directly.
+ (car (last (string-to-list (symbol-name type))))))
+ (n (- char ?0)))
(if (zerop n) 10 n))))
(when (company-manual-begin)
- (and (or (< n 1) (> n company-candidates-length))
+ (and (or (< n 1) (> n (- company-candidates-length
+ company-tooltip-offset)))
(error "No candidate number %d" n))
(cl-decf n)
- (company-finish (nth n company-candidates))))
+ (company-finish (nth (+ n company-tooltip-offset)
+ company-candidates))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun company-fill-propertize (value annotation width selected left right)
(let* ((margin (length left))
(common (or (company-call-backend 'match value)
- (length company-common)))
+ (if company-common
+ (string-width company-common)
+ 0)))
(ann-ralign company-tooltip-align-annotations)
+ (value (company--clean-string value))
(ann-truncate (< width
(+ (length value) (length annotation)
(if ann-ralign 1 0))))
line)))
line))
+(defun company--clean-string (str)
+ (replace-regexp-in-string
+ "\\([^[:graph:] ]\\)\\|\\(\ufeff\\)\\|[[:multibyte:]]"
+ (lambda (match)
+ (cond
+ ((match-beginning 1)
+ ;; FIXME: Better char for 'non-printable'?
+ ;; We shouldn't get any of these, but sometimes we might.
+ "\u2017")
+ ((match-beginning 2)
+ ;; Zero-width non-breakable space.
+ "")
+ ((> (string-width match) 1)
+ (concat
+ (make-string (1- (string-width match)) ?\ufeff)
+ match))
+ (t match)))
+ str))
+
;;; replace
(defun company-buffer-lines (beg end)
limit
(length lst)))
+(defsubst company--window-height ()
+ (if (fboundp 'window-screen-lines)
+ (floor (window-screen-lines))
+ (window-body-height)))
+
+(defsubst company--window-width ()
+ (let ((ww (window-body-width)))
+ ;; Account for the line continuation column.
+ (when (zerop (cadr (window-fringes)))
+ (cl-decf ww))
+ (unless (or (display-graphic-p)
+ (version< "24.3.1" emacs-version))
+ ;; Emacs 24.3 and earlier included margins
+ ;; in window-width when in TTY.
+ (cl-decf ww
+ (let ((margins (window-margins)))
+ (+ (or (car margins) 0)
+ (or (cdr margins) 0)))))
+ ww))
+
(defun company--replacement-string (lines old column nl &optional align-top)
(cl-decf column company-tooltip-margin)
(while old
(push (company-modify-line (pop old)
(company--offset-line (pop lines) offset)
- column) new))
+ column)
+ new))
;; Append whole new lines.
(while lines
(push (concat (company-space-string column)
(defun company--create-lines (selection limit)
(let ((len company-candidates-length)
- (numbered 99999)
(window-width (company--window-width))
lines
width
;; `lisp-completion-at-point' adds a space.
(setq annotation (comment-string-strip annotation t nil)))
(push (cons value annotation) items)
- (setq width (max (+ (length value)
+ (setq width (max (+ (string-width value)
(if (and annotation company-tooltip-align-annotations)
(1+ (length annotation))
(length annotation)))
(setq width (min window-width
(max company-tooltip-minimum-width
- (if (and company-show-numbers
- (< company-tooltip-offset 10))
+ (if company-show-numbers
(+ 2 width)
width))))
- ;; number can make tooltip too long
- (when company-show-numbers
- (setq numbered company-tooltip-offset))
-
- (let ((items (nreverse items)) new)
+ (let ((items (nreverse items))
+ (numbered (if company-show-numbers 0 99999))
+ new)
(when previous
(push (company--scrollpos-line previous width) new))
;; show
-(defsubst company--window-height ()
- (if (fboundp 'window-screen-lines)
- (floor (window-screen-lines))
- (window-body-height)))
-
-(defsubst company--window-width ()
- (let ((ww (window-body-width)))
- ;; Account for the line continuation column.
- (when (zerop (cadr (window-fringes)))
- (cl-decf ww))
- (unless (or (display-graphic-p)
- (version< "24.3.1" emacs-version))
- ;; Emacs 24.3 and earlier included margins
- ;; in window-width when in TTY.
- (cl-decf ww
- (let ((margins (window-margins)))
- (+ (or (car margins) 0)
- (or (cdr margins) 0)))))
- ww))
-
(defun company--pseudo-tooltip-height ()
"Calculate the appropriate tooltip height.
Returns a negative number if the tooltip should be displayed above point."