]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Show numbers in tooltip irrespective of scrolling
[gnu-emacs-elpa] / company.el
index 15a6eeed6b3e07a02007d95007dd1692272ffc4c..fcb100e5ca7dad4267b8d299cc6352b792528944 100644 (file)
@@ -5,7 +5,7 @@
 ;; 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"))
 
@@ -551,6 +551,7 @@ happens.  The value of nil means no idle completion."
 
 (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
@@ -848,7 +849,7 @@ means that `company-mode' is always turned on except in `message-mode' buffers."
         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))
@@ -1028,7 +1029,7 @@ can retrieve meta-data for them."
 
 (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)))))
@@ -1279,7 +1280,8 @@ from the rest of the back-ends in the group, if any, will be left at the end."
        (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
@@ -1504,7 +1506,7 @@ from the rest of the back-ends in the group, if any, will be left at the end."
 
 (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)
@@ -1518,8 +1520,15 @@ from the rest of the back-ends in the group, if any, will be left at the end."
   (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.
@@ -1575,14 +1584,12 @@ from the rest of the back-ends in the group, if any, will be left at the end."
 (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 ()
@@ -1609,13 +1616,9 @@ from the rest of the back-ends in the group, if any, will be left at the end."
       (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.
@@ -1854,18 +1857,26 @@ inserted."
       (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))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -2088,8 +2099,11 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
 (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))))
@@ -2148,6 +2162,25 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
                              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)
@@ -2183,6 +2216,26 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
       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)
 
@@ -2207,7 +2260,8 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
     (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)
@@ -2229,7 +2283,6 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
 
 (defun company--create-lines (selection limit)
   (let ((len company-candidates-length)
-        (numbered 99999)
         (window-width (company--window-width))
         lines
         width
@@ -2275,7 +2328,7 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
           ;; `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)))
@@ -2283,16 +2336,13 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
 
     (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))
 
@@ -2342,26 +2392,6 @@ If SHOW-VERSION is non-nil, show the version in the echo area."
 
 ;; 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."