]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Extracted company-show-electric-buffer.
[gnu-emacs-elpa] / company.el
index e30075d48b85927d4f0b8953269bddef089a424d..f12b38ba2a2d4a0b5b5dfa73b397ef254df802dd 100644 (file)
@@ -3,10 +3,10 @@
 ;; Copyright (C) 2009 Nikolaj Schumacher
 ;;
 ;; Author: Nikolaj Schumacher <bugs * nschum de>
-;; Version: 
+;; Version: 0.1.5
 ;; Keywords: abbrev, convenience, matchis
 ;; URL: http://nschum.de/src/emacs/company/
-;; Compatibility: GNU Emacs 23.x
+;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
 ;;
 ;; This file is NOT part of GNU Emacs.
 ;;
 ;;     ('candidates (list "foobar" "foobaz" "foobarbaz"))
 ;;     ('meta (format "This value is named %s" arg))))
 ;;
+;; Known Issues:
+;; When point is at the very end of the buffer, the pseudo-tooltip appears very
+;; 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.
+;;
+;; 2009-03-21 (0.1.5)
+;;    Fixed elisp documentation buffer always showing the same doc.
+;;    Added `company-echo-strip-common-frontend'.
+;;    Added `company-show-numbers' option and M-0 ... M-9 default bindings.
+;;    Don't hide the echo message if it isn't shown.
+;;
+;; 2009-03-20 (0.1)
 ;;    Initial release.
 ;;
 ;;; Code:
@@ -62,6 +79,9 @@
 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
 (add-to-list 'debug-ignored-errors "^No documentation available$")
+(add-to-list 'debug-ignored-errors "^Company not enabled$")
+(add-to-list 'debug-ignored-errors "^Company not in search mode$")
+(add-to-list 'debug-ignored-errors "^No candidate number ")
 
 (defgroup company nil
   "Extensible inline text completion mechanism"
@@ -76,8 +96,9 @@
   :group 'company)
 
 (defface company-tooltip-selection
-  '((t :background "orange1"
-       :foreground "black"))
+  '((default :inherit company-tooltip)
+    (((class color) (min-colors 88)) (:background "orange1"))
+    (t (:background "green")))
   "*Face used for the selection in the tool tip."
   :group 'company)
 
   "*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
@@ -164,6 +191,10 @@ The visualized data is stored in `company-prefix', `company-candidates',
   :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)
+                         (const :tag "show echo meta-data in echo"
+                                company-echo-metadata-frontend)
                          (const :tag "pseudo tooltip"
                                 company-pseudo-tooltip-frontend)
                          (const :tag "pseudo tooltip, multiple only"
@@ -174,8 +205,8 @@ The visualized data is stored in `company-prefix', `company-candidates',
                          (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
@@ -223,6 +254,16 @@ immediately when a prefix of `company-minimum-prefix-length' is reached."
                  (const :tag "immediate (t)" t)
                  (number :tag "seconds")))
 
+(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)))
+
+(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)
@@ -232,10 +273,17 @@ immediately when a prefix of `company-minimum-prefix-length' is reached."
   (let ((keymap (make-sparse-keymap)))
     (define-key keymap (kbd "M-n") 'company-select-next)
     (define-key keymap (kbd "M-p") 'company-select-previous)
+    (define-key keymap (kbd "<down>") 'company-select-next)
+    (define-key keymap (kbd "<up>") 'company-select-previous)
     (define-key keymap "\C-m" 'company-complete-selection)
     (define-key keymap "\t" 'company-complete-common)
     (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
     (define-key keymap "\C-s" 'company-search-candidates)
+    (define-key keymap "\C-\M-s" 'company-filter-candidates)
+    (dotimes (i 10)
+      (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
+        `(lambda () (interactive) (company-complete-number ,i))))
+
     keymap)
   "Keymap that is enabled during an active completion.")
 
@@ -243,28 +291,37 @@ immediately when a prefix of `company-minimum-prefix-length' is reached."
 (define-minor-mode company-mode
   "\"complete anything\"; in in-buffer completion framework.
 Completion starts automatically, depending on the values
-`company-idle-delay' and `company-minimum-prefix-length'
+`company-idle-delay' and `company-minimum-prefix-length'.
 
 Completion can be controlled with the commands:
 `company-complete-common', `company-complete-selection', `company-complete',
-`company-select-next', `company-select-previous'.
+`company-select-next', `company-select-previous'.  If these commands are
+called before `company-idle-delay', completion will also start.
 
-Completions can be searched with `company-search-candidates'.
+Completions can be searched with `company-search-candidates' or
+`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
   (if company-mode
       (progn
         (add-hook 'pre-command-hook 'company-pre-command nil t)
-        (add-hook 'post-command-hook 'company-post-command nil t))
+        (add-hook 'post-command-hook 'company-post-command nil t)
+        (dolist (backend company-backends)
+          (unless (fboundp backend)
+            (ignore-errors (require backend nil t)))
+          (unless (fboundp backend)
+            (message "Company back-end '%s' could not be initialized"
+                     backend))))
     (remove-hook 'pre-command-hook 'company-pre-command t)
     (remove-hook 'post-command-hook 'company-post-command t)
     (company-cancel)
@@ -354,7 +411,8 @@ keymap during active completions:
 
 (defvar company-timer nil)
 
-(defvar company-disabled-backends nil)
+(defvar company-added-newline nil)
+(make-variable-buffer-local 'company-added-newline)
 
 (defsubst company-strip-prefix (str)
   (substring str (length company-prefix)))
@@ -431,6 +489,8 @@ keymap during active completions:
          (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)
@@ -450,6 +510,8 @@ keymap during active completions:
            (company-post-command)))))
 
 (defun company-manual-begin ()
+  (interactive)
+  (unless company-mode (error "Company not enabled"))
   (and company-mode
        (not company-candidates)
        (let ((company-idle-delay t)
@@ -478,27 +540,32 @@ keymap during active completions:
     (unless company-candidates
       (let (prefix)
         (dolist (backend company-backends)
-          (unless (fboundp backend)
-            (ignore-errors (require backend nil t)))
-          (if (fboundp backend)
-              (when (setq prefix (funcall backend 'prefix))
-                (when (company-should-complete prefix)
-                  (setq company-backend backend)
-                  (company-calculate-candidates prefix))
-                (return prefix))
-            (unless (memq backend company-disabled-backends)
-              (push backend company-disabled-backends)
-              (message "Company back-end '%s' could not be initialized"
-                       backend)))))))
+          (and (fboundp backend)
+               (setq prefix (funcall backend 'prefix))
+               (company-should-complete prefix)
+               (setq company-backend backend)
+               (company-calculate-candidates prefix))
+          (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
@@ -519,8 +586,11 @@ keymap during active completions:
   ;; 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))
@@ -532,7 +602,7 @@ keymap during active completions:
   (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)
@@ -573,6 +643,8 @@ keymap during active completions:
 
 (defun company-search-printing-char ()
   (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
   (setq company-search-string
         (concat (or company-search-string "") (string last-command-event))
         company-search-lighter (concat " Search: \"" company-search-string
@@ -586,6 +658,8 @@ keymap during active completions:
 (defun company-search-repeat-forward ()
   "Repeat the incremental search in completion candidates forward."
   (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
   (let ((pos (company-search company-search-string
                               (cdr (nthcdr company-selection
                                            company-candidates)))))
@@ -596,6 +670,8 @@ keymap during active completions:
 (defun company-search-repeat-backward ()
   "Repeat the incremental search in completion candidates backwards."
   (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
   (let ((pos (company-search company-search-string
                               (nthcdr (- company-candidates-length
                                          company-selection)
@@ -604,31 +680,46 @@ keymap during active completions:
         (ding)
       (company-set-selection (- company-selection pos 1) t))))
 
-(defsubst company-create-match-predicate (search-string)
-  `(lambda (candidate)
-     ,(if company-candidates-predicate
-          `(and (string-match ,search-string candidate)
-                (funcall ,company-candidates-predicate candidate))
-        `(string-match ,company-search-string candidate))))
+(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))))
+  (company-update-candidates
+   (company-apply-predicate company-candidates company-candidates-predicate)))
+
+(defun company-filter-printing-char ()
+  (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
+  (company-search-printing-char)
+  (company-create-match-predicate)
+  (company-call-frontends 'update))
 
 (defun company-search-kill-others ()
   "Limit the completion candidates to the ones matching the search string."
   (interactive)
-  (let ((predicate (company-create-match-predicate company-search-string)))
-    (setq company-candidates-predicate predicate)
-    (company-update-candidates (company-apply-predicate company-candidates
-                                                        predicate))
-    (company-search-mode 0)
-    (company-call-frontends 'update)))
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
+  (company-create-match-predicate)
+  (company-search-mode 0)
+  (company-call-frontends 'update))
 
 (defun company-search-abort ()
   "Abort searching the completion candidates."
   (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
   (company-set-selection company-search-old-selection t)
   (company-search-mode 0))
 
 (defun company-search-other-char ()
   (interactive)
+  (unless company-mode (error "Company not enabled"))
+  (unless company-search-mode (error "Company not in search mode"))
   (company-search-mode 0)
   (when last-input-event
     (clear-this-command-keys t)
@@ -645,7 +736,7 @@ keymap during active completions:
         (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)
@@ -669,23 +760,14 @@ keymap during active completions:
   "Keymap used for incrementally searching the completion candidates.")
 
 (define-minor-mode company-search-mode
-  "Start searching the completion candidates incrementally.
-
-\\<company-search-map>Search can be controlled with the commands:
-- `company-search-repeat-forward' (\\[company-search-repeat-forward])
-- `company-search-repeat-backward' (\\[company-search-repeat-backward])
-- `company-search-abort' (\\[company-search-abort])
-
-Regular characters are appended to the search string.
-
-The command `company-search-kill-others' (\\[company-search-kill-others]) uses
- the search string to limit the completion candidates."
+  "Search mode for completion candidates.
+Don't start this directly, use `company-search-candidates' or
+`company-filter-candidates'."
   nil company-search-lighter nil
   (if company-search-mode
       (if (company-manual-begin)
           (progn
             (setq company-search-old-selection company-selection)
-            (company-enable-overriding-keymap company-search-map)
             (company-call-frontends 'update))
         (setq company-search-mode nil))
     (kill-local-variable 'company-search-string)
@@ -706,7 +788,24 @@ Regular characters are appended to the search string.
 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
  the search string to limit the completion candidates."
   (interactive)
-  (company-search-mode 1))
+  (company-search-mode 1)
+  (company-enable-overriding-keymap company-search-map))
+
+(defvar company-filter-map
+  (let ((keymap (make-keymap)))
+    (define-key keymap [remap company-search-printing-char]
+      'company-filter-printing-char)
+    (set-keymap-parent keymap company-search-map)
+    keymap)
+  "Keymap used for incrementally searching the completion candidates.")
+
+(defun company-filter-candidates ()
+  "Start filtering the completion candidates incrementally.
+This works the same way as `company-search-candidates' immediately
+followed by `company-search-kill-others' after each input."
+  (interactive)
+  (company-search-mode 1)
+  (company-enable-overriding-keymap company-filter-map))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -747,6 +846,15 @@ when the selection has been changed, the selected candidate is completed."
       (call-interactively 'company-complete-common)
       (setq this-command 'company-complete-common))))
 
+(defun company-complete-number (n)
+  "Complete the Nth candidate."
+  (when (company-manual-begin)
+    (and (< n 1) (> n company-candidates-length)
+         (error "No candidate number %d" n))
+    (decf n)
+    (insert (company-strip-prefix (nth n company-candidates)))
+    (company-abort)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defconst company-space-strings-limit 100)
@@ -788,27 +896,33 @@ when the selection has been changed, the selected candidate is completed."
     (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)
-  (when company-candidates
-    (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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -909,6 +1023,7 @@ when the selection has been changed, the selected candidate is completed."
 (defun company-create-lines (column selection limit)
 
   (let ((len company-candidates-length)
+        (numbered 99999)
         lines
         width
         lines-copy
@@ -936,14 +1051,28 @@ when the selection has been changed, the selected candidate is completed."
       (setq width (max (length (pop lines-copy)) width)))
     (setq width (min width (- (window-width) column)))
 
+    (setq lines-copy lines)
+
+    ;; number can make tooltip too long
+    (and company-show-numbers
+         (< (setq numbered company-tooltip-offset) 10)
+         (incf width 2))
+
     (when previous
       (push (propertize (company-safe-substring previous 0 width)
                         'face 'company-tooltip)
             new))
 
     (dotimes (i len)
-      (push (company-fill-propertize (company-reformat (pop lines))
-                                     width (equal i selection))
+      (push (company-fill-propertize
+             (if (>= numbered 10)
+                 (company-reformat (pop lines))
+               (incf numbered)
+               (format "%s %d"
+                       (company-safe-substring (company-reformat (pop lines))
+                                               0 (- width 2))
+                       (mod numbered 10)))
+             width (equal i selection))
             new))
 
     (when remainder
@@ -1016,7 +1145,8 @@ when the selection has been changed, the selected candidate is completed."
   (when company-pseudo-tooltip-overlay
     (overlay-put company-pseudo-tooltip-overlay 'invisible t)
     (overlay-put company-pseudo-tooltip-overlay 'before-string
-                 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
+                 (overlay-get company-pseudo-tooltip-overlay 'company-before))
+    (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
 
 (defun company-pseudo-tooltip-frontend (command)
   "A `company-mode' front-end similar to a tool-tip but based on overlays."
@@ -1053,16 +1183,25 @@ when the selection has been changed, the selected candidate is completed."
 
   (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))))
 
@@ -1113,25 +1252,61 @@ when the selection has been changed, the selected candidate is completed."
         (len -1)
         ;; Roll to selection.
         (candidates (nthcdr company-selection company-candidates))
+        (i (if company-show-numbers company-selection 99999))
         comp msg)
 
     (while candidates
       (setq comp (company-reformat (pop candidates))
             len (+ len 1 (length comp)))
-      (if (>= len limit)
-          (setq candidates nil)
+      (if (< i 10)
+          ;; Add number.
+          (progn
+            (setq comp (propertize (format "%d: %s" i comp)
+                                   'face 'company-echo))
+            (incf len 3)
+            (incf i)
+            (add-text-properties 3 (+ 3 (length company-common))
+                                 '(face company-echo-common) comp))
         (setq comp (propertize comp 'face 'company-echo))
         (add-text-properties 0 (length company-common)
-                             '(face company-echo-common) comp)
+                             '(face company-echo-common) comp))
+      (if (>= len limit)
+          (setq candidates nil)
         (push comp msg)))
 
     (mapconcat 'identity (nreverse msg) " ")))
 
+(defun company-echo-strip-common-format ()
+
+  (let ((limit (window-width (minibuffer-window)))
+        (len (+ (length company-prefix) 2))
+        ;; Roll to selection.
+        (candidates (nthcdr company-selection company-candidates))
+        (i (if company-show-numbers company-selection 99999))
+        msg comp)
+
+    (while candidates
+      (setq comp (company-strip-prefix (pop candidates))
+            len (+ len 2 (length comp)))
+      (when (< i 10)
+        ;; Add number.
+        (setq comp (format "%s (%d)" comp i))
+        (incf len 4)
+        (incf i))
+      (if (>= len limit)
+          (setq candidates nil)
+        (push (propertize comp 'face 'company-echo) msg)))
+
+    (concat (propertize company-prefix 'face 'company-echo-common) "{"
+            (mapconcat 'identity (nreverse msg) ", ")
+            "}")))
+
 (defun company-echo-hide ()
   (when company-echo-timer
     (cancel-timer company-echo-timer))
-  (setq company-echo-last-msg "")
-  (company-echo-show))
+  (unless (equal company-echo-last-msg "")
+    (setq company-echo-last-msg "")
+    (company-echo-show)))
 
 (defun company-echo-frontend (command)
   "A `company-mode' front-end showing the candidates in the echo area."
@@ -1140,6 +1315,13 @@ when the selection has been changed, the selected candidate is completed."
     ('post-command (company-echo-show-soon 'company-echo-format))
     ('hide (company-echo-hide))))
 
+(defun company-echo-strip-common-frontend (command)
+  "A `company-mode' front-end showing the candidates in the echo area."
+  (case command
+    ('pre-command (company-echo-show-soon))
+    ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
+    ('hide (company-echo-hide))))
+
 (defun company-echo-metadata-frontend (command)
   "A `company-mode' front-end showing the documentation in the echo area."
   (case command