]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Fix #20
[gnu-emacs-elpa] / company.el
index e7e054032a3feb81ff177eb4de8820f7c5fddde0..2734ae75414306534509739d4ae6053cb0c03379 100644 (file)
@@ -1,10 +1,10 @@
-;;; company.el --- Modular in-buffer completion framework
+;;; company.el --- Modular in-buffer completion framework  -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2009-2013  Free Software Foundation, Inc.
 
 ;; Author: Nikolaj Schumacher
 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
 
 ;; Copyright (C) 2009-2013  Free Software Foundation, Inc.
 
 ;; Author: Nikolaj Schumacher
 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
-;; Version: 0.6.2
+;; Version: 0.6.8
 ;; Keywords: abbrev, convenience, matching
 ;; URL: http://company-mode.github.com/
 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
 ;; Keywords: abbrev, convenience, matching
 ;; URL: http://company-mode.github.com/
 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
   :group 'matching)
 
 (defface company-tooltip
   :group 'matching)
 
 (defface company-tooltip
-  '((t :background "yellow"
-       :foreground "black"))
-  "Face used for the tool tip."
-  :group 'company)
+  '((default :foreground "black")
+    (((class color) (min-colors 88) (background light))
+     (:background "cornsilk"))
+    (((class color) (min-colors 88) (background dark))
+     (:background "yellow")))
+  "Face used for the tooltip.")
 
 (defface company-tooltip-selection
   '((default :inherit company-tooltip)
 
 (defface company-tooltip-selection
   '((default :inherit company-tooltip)
-    (((class color) (min-colors 88)) (:background "orange1"))
+    (((class color) (min-colors 88) (background light))
+     (:background "light blue"))
+    (((class color) (min-colors 88) (background dark))
+     (:background "orange1"))
     (t (:background "green")))
     (t (:background "green")))
-  "Face used for the selection in the tool tip."
-  :group 'company)
+  "Face used for the selection in the tooltip.")
 
 (defface company-tooltip-mouse
   '((default :inherit highlight))
 
 (defface company-tooltip-mouse
   '((default :inherit highlight))
-  "Face used for the tool tip item under the mouse."
-  :group 'company)
+  "Face used for the tooltip item under the mouse.")
 
 (defface company-tooltip-common
 
 (defface company-tooltip-common
-  '((t :inherit company-tooltip
-       :foreground "red"))
-  "Face used for the common completion in the tool tip."
-  :group 'company)
+  '((default :inherit company-tooltip)
+    (((background light))
+     :foreground "darkred")
+    (((background dark))
+     :foreground "red"))
+  "Face used for the common completion in the tooltip.")
 
 (defface company-tooltip-common-selection
 
 (defface company-tooltip-common-selection
-  '((t :inherit company-tooltip-selection
-       :foreground "red"))
-  "Face used for the selected common completion in the tool tip."
-  :group 'company)
+  '((default :inherit company-tooltip-selection)
+    (((background light))
+     :foreground "darkred")
+    (((background dark))
+     :foreground "red"))
+  "Face used for the selected common completion in the tooltip.")
 
 (defface company-preview
   '((t :background "blue4"
        :foreground "wheat"))
 
 (defface company-preview
   '((t :background "blue4"
        :foreground "wheat"))
-  "Face used for the completion preview."
-  :group 'company)
+  "Face used for the completion preview.")
 
 (defface company-preview-common
   '((t :inherit company-preview
        :foreground "red"))
 
 (defface company-preview-common
   '((t :inherit company-preview
        :foreground "red"))
-  "Face used for the common part of the completion preview."
-  :group 'company)
+  "Face used for the common part of the completion preview.")
 
 (defface company-preview-search
   '((t :inherit company-preview
        :background "blue1"))
 
 (defface company-preview-search
   '((t :inherit company-preview
        :background "blue1"))
-  "Face used for the search string in the completion preview."
-  :group 'company)
+  "Face used for the search string in the completion preview.")
 
 (defface company-echo nil
 
 (defface company-echo nil
-  "Face used for completions in the echo area."
-  :group 'company)
+  "Face used for completions in the echo area.")
 
 (defface company-echo-common
   '((((background dark)) (:foreground "firebrick1"))
     (((background light)) (:background "firebrick4")))
 
 (defface company-echo-common
   '((((background dark)) (:foreground "firebrick1"))
     (((background light)) (:background "firebrick4")))
-  "Face used for the common part of completions in the echo area."
-  :group 'company)
+  "Face used for the common part of completions in the echo area.")
 
 (defun company-frontends-set (variable value)
   ;; uniquify
 
 (defun company-frontends-set (variable value)
   ;; uniquify
@@ -185,7 +187,6 @@ The visualized data is stored in `company-prefix', `company-candidates',
 `company-common', `company-selection', `company-point' and
 `company-search-string'."
   :set 'company-frontends-set
 `company-common', `company-selection', `company-point' and
 `company-search-string'."
   :set 'company-frontends-set
-  :group 'company
   :type '(repeat (choice (const :tag "echo" company-echo-frontend)
                          (const :tag "echo, strip common"
                                 company-echo-strip-common-frontend)
   :type '(repeat (choice (const :tag "echo" company-echo-frontend)
                          (const :tag "echo, strip common"
                                 company-echo-strip-common-frontend)
@@ -201,34 +202,32 @@ The visualized data is stored in `company-prefix', `company-candidates',
                          (function :tag "custom function" nil))))
 
 (defcustom company-tooltip-limit 10
                          (function :tag "custom function" nil))))
 
 (defcustom company-tooltip-limit 10
-  "The maximum number of candidates in the tool tip"
-  :group 'company
+  "The maximum number of candidates in the tooltip"
   :type 'integer)
 
 (defcustom company-tooltip-minimum 6
   :type 'integer)
 
 (defcustom company-tooltip-minimum 6
-  "The minimum height of the tool tip.
+  "The minimum height of the tooltip.
 If this many lines are not available, prefer to display the tooltip above."
 If this many lines are not available, prefer to display the tooltip above."
-  :group 'company
   :type 'integer)
 
 (defvar company-safe-backends
   '((company-abbrev . "Abbrev")
   :type 'integer)
 
 (defvar company-safe-backends
   '((company-abbrev . "Abbrev")
-    (company-clang . "clang")
+    (company-clang . "Clang")
     (company-css . "CSS")
     (company-dabbrev . "dabbrev for plain text")
     (company-dabbrev-code . "dabbrev for code")
     (company-css . "CSS")
     (company-dabbrev . "dabbrev for plain text")
     (company-dabbrev-code . "dabbrev for code")
-    (company-eclim . "eclim (an Eclipse interace)")
+    (company-eclim . "Eclim (an Eclipse interface)")
     (company-elisp . "Emacs Lisp")
     (company-etags . "etags")
     (company-files . "Files")
     (company-gtags . "GNU Global")
     (company-elisp . "Emacs Lisp")
     (company-etags . "etags")
     (company-files . "Files")
     (company-gtags . "GNU Global")
-    (company-ispell . "ispell")
+    (company-ispell . "Ispell")
     (company-keywords . "Programming language keywords")
     (company-nxml . "nxml")
     (company-oddmuse . "Oddmuse")
     (company-pysmell . "PySmell")
     (company-ropemacs . "ropemacs")
     (company-keywords . "Programming language keywords")
     (company-nxml . "nxml")
     (company-oddmuse . "Oddmuse")
     (company-pysmell . "PySmell")
     (company-ropemacs . "ropemacs")
-    (company-semantic . "CEDET Semantic")
+    (company-semantic . "Semantic")
     (company-tempo . "Tempo templates")
     (company-xcode . "Xcode")))
 (put 'company-safe-backends 'risky-local-variable t)
     (company-tempo . "Tempo templates")
     (company-xcode . "Xcode")))
 (put 'company-safe-backends 'risky-local-variable t)
@@ -264,7 +263,7 @@ Requires Emacs 24.1 or newer."
                           (plist-get (nthcdr 4 res) :predicate)))))))
 
 (defcustom company-backends '(company-elisp company-nxml company-css
                           (plist-get (nthcdr 4 res) :predicate)))))))
 
 (defcustom company-backends '(company-elisp company-nxml company-css
-                              company-clang company-semantic company-eclim
+                              company-semantic company-clang company-eclim
                               company-xcode company-ropemacs
                               (company-gtags company-etags company-dabbrev-code
                                company-keywords)
                               company-xcode company-ropemacs
                               (company-gtags company-etags company-dabbrev-code
                                company-keywords)
@@ -316,7 +315,7 @@ return the cons of buffer and buffer location, or of file and line
 number where the completion candidate was defined.
 
 `require-match': If this value is t, the user is not allowed to enter anything
 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.
 
 user that choice with `company-require-match'.  Return value 'never overrides
 that option the other way around.
 
@@ -331,7 +330,6 @@ 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."
 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."
-  :group 'company
   :type `(repeat
           (choice
            :tag "Back-end"
   :type `(repeat
           (choice
            :tag "Back-end"
@@ -351,25 +349,24 @@ does not know about.  It should also be callable interactively and use
   "Hook run when company starts completing.
 The hook is called with one argument that is non-nil if the completion was
 started manually."
   "Hook run when company starts completing.
 The hook is called with one argument that is non-nil if the completion was
 started manually."
-  :group 'company
   :type 'hook)
 
 (defcustom company-completion-cancelled-hook nil
   "Hook run when company cancels completing.
 The hook is called with one argument that is non-nil if the completion was
 aborted manually."
   :type 'hook)
 
 (defcustom company-completion-cancelled-hook nil
   "Hook run when company cancels completing.
 The hook is called with one argument that is non-nil if the completion was
 aborted manually."
-  :group 'company
   :type 'hook)
 
 (defcustom company-completion-finished-hook nil
   "Hook run when company successfully completes.
   :type 'hook)
 
 (defcustom company-completion-finished-hook nil
   "Hook run when company successfully completes.
-The hook is called with the selected candidate as an argument."
-  :group 'company
+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."
   :type 'hook)
 
 (defcustom company-minimum-prefix-length 3
   "The minimum prefix length for automatic completion."
   :type 'hook)
 
 (defcustom company-minimum-prefix-length 3
   "The minimum prefix length for automatic completion."
-  :group 'company
   :type '(integer :tag "prefix length"))
 
 (defcustom company-require-match 'company-explicit-action-p
   :type '(integer :tag "prefix length"))
 
 (defcustom company-require-match 'company-explicit-action-p
@@ -378,25 +375,23 @@ This can be a function do determine if a match is required.
 
 This can be overridden by the back-end, if it returns t or 'never to
 'require-match.  `company-auto-complete' also takes precedence over this."
 
 This can be overridden by the back-end, if it returns t or 'never to
 'require-match.  `company-auto-complete' also takes precedence over this."
-  :group 'company
   :type '(choice (const :tag "Off" nil)
                  (function :tag "Predicate function")
                  (const :tag "On, if user interaction took place"
                         'company-explicit-action-p)
                  (const :tag "On" t)))
 
   :type '(choice (const :tag "Off" nil)
                  (function :tag "Predicate function")
                  (const :tag "On, if user interaction took place"
                         'company-explicit-action-p)
                  (const :tag "On" t)))
 
-(defcustom company-auto-complete 'company-explicit-action-p
+(defcustom company-auto-complete nil
   "Determines when to auto-complete.
 If this is enabled, all characters from `company-auto-complete-chars' complete
 the selected completion.  This can also be a function."
   "Determines when to auto-complete.
 If this is enabled, all characters from `company-auto-complete-chars' complete
 the selected completion.  This can also be a function."
-  :group 'company
   :type '(choice (const :tag "Off" nil)
                  (function :tag "Predicate function")
                  (const :tag "On, if user interaction took place"
                         'company-explicit-action-p)
                  (const :tag "On" t)))
 
   :type '(choice (const :tag "Off" nil)
                  (function :tag "Predicate function")
                  (const :tag "On, if user interaction took place"
                         'company-explicit-action-p)
                  (const :tag "On" t)))
 
-(defcustom company-auto-complete-chars '(?\  ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
+(defcustom company-auto-complete-chars '(?\  ?\) ?.)
   "Determines which characters trigger an automatic completion.
 See `company-auto-complete'.  If this is a string, each string character causes
 completion.  If it is a list of syntax description characters (see
   "Determines which characters trigger an automatic completion.
 See `company-auto-complete'.  If this is a string, each string character causes
 completion.  If it is a list of syntax description characters (see
@@ -405,8 +400,7 @@ completion.  If it is a list of syntax description characters (see
 This can also be a function, which is called with the new input and should
 return non-nil if company should auto-complete.
 
 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."
-  :group 'company
+A character that is part of a valid candidate never triggers auto-completion."
   :type '(choice (string :tag "Characters")
                  (set :tag "Syntax"
                       (const :tag "Whitespace" ?\ )
   :type '(choice (string :tag "Characters")
                  (set :tag "Syntax"
                       (const :tag "Whitespace" ?\ )
@@ -429,7 +423,6 @@ A character that is part of a valid candidate never starts auto-completion."
   "The idle delay in seconds until automatic completions starts.
 A value of nil means never complete automatically, t means complete
 immediately when a prefix of `company-minimum-prefix-length' is reached."
   "The idle delay in seconds until automatic completions starts.
 A value of nil means never complete automatically, t means complete
 immediately when a prefix of `company-minimum-prefix-length' is reached."
-  :group 'company
   :type '(choice (const :tag "never (nil)" nil)
                  (const :tag "immediate (t)" t)
                  (number :tag "seconds")))
   :type '(choice (const :tag "never (nil)" nil)
                  (const :tag "immediate (t)" t)
                  (number :tag "seconds")))
@@ -440,14 +433,12 @@ If this is t, it will complete after any command.  See `company-idle-delay'.
 
 Alternatively any command with a non-nil 'company-begin property is treated as
 if it was on this list."
 
 Alternatively any command with a non-nil 'company-begin property is treated as
 if it was on this list."
-  :group 'company
   :type '(choice (const :tag "Any command" t)
                  (const :tag "Self insert command" '(self-insert-command))
                  (repeat :tag "Commands" function)))
 
 (defcustom company-show-numbers nil
   "If enabled, show quick-access numbers for the first ten candidates."
   :type '(choice (const :tag "Any command" t)
                  (const :tag "Self insert command" '(self-insert-command))
                  (repeat :tag "Commands" function)))
 
 (defcustom company-show-numbers nil
   "If enabled, show quick-access numbers for the first ten candidates."
-  :group 'company
   :type '(choice (const :tag "off" nil)
                  (const :tag "on" t)))
 
   :type '(choice (const :tag "off" nil)
                  (const :tag "on" t)))
 
@@ -475,7 +466,9 @@ The work-around consists of adding a newline.")
     (define-key keymap [up-mouse-1] 'ignore)
     (define-key keymap [up-mouse-3] 'ignore)
     (define-key keymap [return] 'company-complete-selection)
     (define-key keymap [up-mouse-1] 'ignore)
     (define-key keymap [up-mouse-3] 'ignore)
     (define-key keymap [return] 'company-complete-selection)
+    (define-key keymap (kbd "RET") 'company-complete-selection)
     (define-key keymap [tab] 'company-complete-common)
     (define-key keymap [tab] 'company-complete-common)
+    (define-key keymap (kbd "TAB") 'company-complete-common)
     (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
     (define-key keymap "\C-w" 'company-show-location)
     (define-key keymap "\C-s" 'company-search-candidates)
     (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
     (define-key keymap "\C-w" 'company-show-location)
     (define-key keymap "\C-s" 'company-search-candidates)
@@ -841,14 +834,13 @@ can retrieve meta-data for them."
               (while c2
                 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
                                   c2)))))))
               (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)))))
-        candidates
-      ;; Already completed and unique; don't start.
-      ;; FIXME: 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
 
 (defun company-idle-begin (buf win tick pos)
   (and company-mode
@@ -879,7 +871,10 @@ can retrieve meta-data for them."
 (defun company-manual-begin ()
   (interactive)
   (setq company--explicit-action t)
 (defun company-manual-begin ()
   (interactive)
   (setq company--explicit-action t)
-  (company-auto-begin))
+  (unwind-protect
+      (company-auto-begin)
+    (unless company-candidates
+      (setq company--explicit-action nil))))
 
 (defun company-other-backend (&optional backward)
   (interactive (list current-prefix-arg))
 
 (defun company-other-backend (&optional backward)
   (interactive (list current-prefix-arg))
@@ -906,10 +901,6 @@ can retrieve meta-data for them."
                (eq company-require-match t))
              (not (eq backend-value 'never))))))
 
                (eq company-require-match t))
              (not (eq backend-value 'never))))))
 
-(defun company-punctuation-p (input)
-  "Return non-nil, if input starts with punctuation or parentheses."
-  (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
-
 (defun company-auto-complete-p (input)
   "Return non-nil, if input starts with punctuation or parentheses."
   (and (if (functionp company-auto-complete)
 (defun company-auto-complete-p (input)
   "Return non-nil, if input starts with punctuation or parentheses."
   (and (if (functionp company-auto-complete)
@@ -1337,14 +1328,35 @@ and invoke the normal binding."
     (company-abort)
     (company--unread-last-input)))
 
     (company-abort)
     (company--unread-last-input)))
 
+(defun company--inside-tooltip-p (event-col-row ovl-row)
+  (when company-pseudo-tooltip-overlay
+    (let* ((ovl company-pseudo-tooltip-overlay)
+           (column (overlay-get ovl 'company-column))
+           (width (overlay-get ovl 'company-width))
+           (height (overlay-get ovl 'company-height))
+           (evt-col (car event-col-row))
+           (evt-row (cdr event-col-row)))
+      (and (>= evt-col column)
+           (< evt-col (+ column width))
+           (> evt-row ovl-row)
+           (<= evt-row (+ ovl-row height) )))))
+
 (defun company-select-mouse (event)
   "Select the candidate picked by the mouse."
   (interactive "e")
 (defun company-select-mouse (event)
   "Select the candidate picked by the mouse."
   (interactive "e")
-  (when (nth 4 (event-start event))
-    (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
-                              (company--row)
-                              1))
-    t))
+  (let ((event-col-row (posn-actual-col-row (event-start event)))
+        (ovl-row (company--row)))
+    (if (company--inside-tooltip-p event-col-row ovl-row)
+        (progn
+          (company-set-selection (- (cdr event-col-row)
+                                    (if (zerop company-tooltip-offset)
+                                        1
+                                      (- 2 company-tooltip-offset))
+                                    ovl-row))
+          t)
+      (company-abort)
+      (company--unread-last-input)
+      nil)))
 
 (defun company-complete-mouse (event)
   "Complete the candidate picked by the mouse."
 
 (defun company-complete-mouse (event)
   "Complete the candidate picked by the mouse."
@@ -1631,12 +1643,13 @@ Example:
 (defun company-buffer-lines (beg end)
   (goto-char beg)
   (let (lines)
 (defun company-buffer-lines (beg end)
   (goto-char beg)
   (let (lines)
-    (while (and (= 1 (vertical-motion 1))
-                (<= (point) end))
-      (push (buffer-substring beg (min end (1- (point)))) lines)
-      (setq beg (point)))
-    (unless (eq beg end)
-      (push (buffer-substring beg end) lines))
+    (while (< (point) end)
+      (let ((bol (point)))
+        ;; A visual line can contain several physical lines (e.g. with outline's
+        ;; folding overlay).  Take only the first one.
+        (re-search-forward "$")
+        (push (buffer-substring bol (min end (point))) lines))
+      (vertical-motion 1))
     (nreverse lines)))
 
 (defsubst company-modify-line (old new offset)
     (nreverse lines)))
 
 (defsubst company-modify-line (old new offset)
@@ -1775,13 +1788,14 @@ Returns a negative number if the tooltip should be displayed above point."
 
         (setq company-pseudo-tooltip-overlay ov)
         (overlay-put ov 'company-replacement-args args)
 
         (setq company-pseudo-tooltip-overlay ov)
         (overlay-put ov 'company-replacement-args args)
-        (overlay-put ov 'company-before
-                     (apply 'company--replacement-string
-                            (company--create-lines selection (abs height))
-                            args))
+
+        (let ((lines (company--create-lines selection (abs height))))
+          (overlay-put ov 'company-before
+                       (apply 'company--replacement-string lines args))
+          (overlay-put ov 'company-width (string-width (car lines))))
 
         (overlay-put ov 'company-column column)
 
         (overlay-put ov 'company-column column)
-        (overlay-put ov 'company-height (abs height))))))
+        (overlay-put ov 'company-height height)))))
 
 (defun company-pseudo-tooltip-show-at-point (pos)
   (let ((col-row (company--col-row pos)))
 
 (defun company-pseudo-tooltip-show-at-point (pos)
   (let ((col-row (company--col-row pos)))
@@ -1790,11 +1804,10 @@ Returns a negative number if the tooltip should be displayed above point."
                                    company-selection))))
 
 (defun company-pseudo-tooltip-edit (lines selection)
                                    company-selection))))
 
 (defun company-pseudo-tooltip-edit (lines selection)
-  (let ((column (overlay-get company-pseudo-tooltip-overlay 'company-column))
-        (height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
+  (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
     (overlay-put company-pseudo-tooltip-overlay 'company-before
                  (apply 'company--replacement-string
     (overlay-put company-pseudo-tooltip-overlay 'company-before
                  (apply 'company--replacement-string
-                        (company--create-lines selection height)
+                        (company--create-lines selection (abs height))
                         (overlay-get company-pseudo-tooltip-overlay
                                      'company-replacement-args)))))
 
                         (overlay-get company-pseudo-tooltip-overlay
                                      'company-replacement-args)))))
 
@@ -1811,12 +1824,18 @@ Returns a negative number if the tooltip should be displayed above point."
 (defun company-pseudo-tooltip-unhide ()
   (when company-pseudo-tooltip-overlay
     (overlay-put company-pseudo-tooltip-overlay 'invisible t)
 (defun company-pseudo-tooltip-unhide ()
   (when company-pseudo-tooltip-overlay
     (overlay-put company-pseudo-tooltip-overlay 'invisible t)
+    ;; Beat outline's folding overlays, at least.
+    (overlay-put company-pseudo-tooltip-overlay 'priority 1)
     (overlay-put company-pseudo-tooltip-overlay 'before-string
                  (overlay-get company-pseudo-tooltip-overlay 'company-before))
     (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
 
     (overlay-put company-pseudo-tooltip-overlay 'before-string
                  (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)
 (defun company-pseudo-tooltip-frontend (command)
-  "A `company-mode' front-end similar to a tool-tip but based on overlays."
+  "`company-mode' front-end similar to a tooltip but based on overlays."
   (case command
     (pre-command (company-pseudo-tooltip-hide-temporarily))
     (post-command
   (case command
     (pre-command (company-pseudo-tooltip-hide-temporarily))
     (post-command
@@ -1826,10 +1845,15 @@ Returns a negative number if the tooltip should be displayed above point."
                          0))
            (new-height (company--pseudo-tooltip-height)))
        (unless (and (>= (* old-height new-height) 0)
                          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)
          ;; 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))
      (company-pseudo-tooltip-unhide))
     (hide (company-pseudo-tooltip-hide)
           (setq company-tooltip-offset 0))
@@ -1881,7 +1905,7 @@ Returns a negative number if the tooltip should be displayed above point."
     (setq company-preview-overlay nil)))
 
 (defun company-preview-frontend (command)
     (setq company-preview-overlay nil)))
 
 (defun company-preview-frontend (command)
-  "`company-mode' front-end showing the selection as if it had been inserted."
+  "`company-mode' front-end showing the selection as if it had been inserted."
   (case command
     (pre-command (company-preview-hide))
     (post-command (company-preview-show-at-point (point)))
   (case command
     (pre-command (company-preview-hide))
     (post-command (company-preview-show-at-point (point)))
@@ -1985,19 +2009,19 @@ Returns a negative number if the tooltip should be displayed above point."
     (company-echo-show)))
 
 (defun company-echo-frontend (command)
     (company-echo-show)))
 
 (defun company-echo-frontend (command)
-  "`company-mode' front-end showing the candidates in the echo area."
+  "`company-mode' front-end showing the candidates in the echo area."
   (case command
     (post-command (company-echo-show-soon 'company-echo-format))
     (hide (company-echo-hide))))
 
 (defun company-echo-strip-common-frontend (command)
   (case command
     (post-command (company-echo-show-soon 'company-echo-format))
     (hide (company-echo-hide))))
 
 (defun company-echo-strip-common-frontend (command)
-  "`company-mode' front-end showing the candidates in the echo area."
+  "`company-mode' front-end showing the candidates in the echo area."
   (case command
     (post-command (company-echo-show-soon 'company-echo-strip-common-format))
     (hide (company-echo-hide))))
 
 (defun company-echo-metadata-frontend (command)
   (case command
     (post-command (company-echo-show-soon 'company-echo-strip-common-format))
     (hide (company-echo-hide))))
 
 (defun company-echo-metadata-frontend (command)
-  "`company-mode' front-end showing the documentation in the echo area."
+  "`company-mode' front-end showing the documentation in the echo area."
   (case command
     (post-command (company-echo-show-when-idle 'company-fetch-metadata))
     (hide (company-echo-hide))))
   (case command
     (post-command (company-echo-show-when-idle 'company-fetch-metadata))
     (hide (company-echo-hide))))