]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blobdiff - company.el
Bumped version to 0.1.5.
[sojka/company-mode.git] / company.el
index 662e51dbd0c92c387acecd70eaa7b53c9f6e9e7e..9c62ed191f21703ee3105855083ee119c1ac5f88 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.
 ;;
 ;;
 ;;; Change Log:
 ;;
+;; 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:
@@ -68,6 +75,7 @@
 (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"
@@ -171,6 +179,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"
@@ -230,6 +242,12 @@ 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)))
+
 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar company-mode-map (make-sparse-keymap)
@@ -245,6 +263,10 @@ immediately when a prefix of `company-minimum-prefix-length' is reached."
     (define-key keymap "\t" 'company-complete-common)
     (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
     (define-key keymap "\C-s" 'company-search-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.")
 
@@ -252,11 +274,12 @@ 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'.
 
@@ -273,7 +296,13 @@ keymap during active completions:
   (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)
@@ -363,8 +392,6 @@ keymap during active completions:
 
 (defvar company-timer nil)
 
-(defvar company-disabled-backends nil)
-
 (defsubst company-strip-prefix (str)
   (substring str (length company-prefix)))
 
@@ -459,6 +486,7 @@ 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)
@@ -488,18 +516,12 @@ 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
         (setq company-point (point))
@@ -769,6 +791,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)
@@ -932,6 +963,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
@@ -959,14 +991,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
@@ -1137,25 +1183,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."
@@ -1164,6 +1246,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