]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Bumped version to 0.3.1.
[gnu-emacs-elpa] / company.el
index d73503bfa237f488578fa7980b2ef4ef0c5ec60c..3be107ef0e7d856eacea585a9adddf035ec417e1 100644 (file)
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2009 Nikolaj Schumacher
 ;;
 ;; Author: Nikolaj Schumacher <bugs * nschum de>
-;; Version: 0.2.1
+;; Version: 0.3.1
 ;; Keywords: abbrev, convenience, matchis
 ;; URL: http://nschum.de/src/emacs/company/
 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
 ;; Enable company-mode with M-x company-mode.  For further information look at
 ;; the documentation for `company-mode' (C-h f company-mode RET)
 ;;
+;; If you want to start a specific back-end, call it interactively or use
+;; `company-begin-backend'.  For example:
+;; M-x company-abbrev will prompt for and insert an abbrev.
+;;
 ;; To write your own back-end, look at the documentation for `company-backends'.
 ;; Here is a simple example completing "foo":
 ;;
 ;;
 ;;; Change Log:
 ;;
+;; 2009-04-15 (0.3.1)
+;;    Added 'stop prefix to prevent dabbrev from completing inside of symbols.
+;;    Fixed issues with tabbar-mode and line-spacing.
+;;    Performance enhancements.
+;;
+;; 2009-04-12 (0.3)
+;;    Added `company-begin-commands' option.
+;;    Added abbrev, tempo and Xcode back-ends.
 ;;    Back-ends are now interactive.  You can start them with M-x backend-name.
 ;;    Added `company-begin-with' for starting company from elisp-code.
 ;;    Added hooks.
@@ -229,8 +241,9 @@ 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-etags
-                              company-oddmuse company-files company-dabbrev)
+                              company-semantic company-xcode 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
@@ -238,7 +251,8 @@ of the following:
 
 'prefix: The back-end should return the text to be completed.  It must be
 text immediately before `point'.  Returning nil passes control to the next
-back-end.
+back-end.  The function should return 'stop if it should complete but cannot
+(e.g. if it is in the middle of a string).
 
 'candidates: The second argument is the prefix to be completed.  The
 return value should be a list of candidates that start with the prefix.
@@ -314,19 +328,29 @@ This can be overridden by the back-end, if it returns t or 'never to
                         'company-explicit-action-p)
                  (const :tag "On" t)))
 
-(defcustom company-auto-complete '(?\  ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
+(defcustom company-auto-complete 'company-explicit-action-p
+  "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)))
+
+(defcustom company-auto-complete-chars '(?\  ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
   "Determines which characters trigger an automatic completion.
-If this is a function, it is called with the new input and should return non-nil
-if company should auto-complete.
+See `company-auto-complete'.  If this is a string, each string character causes
+completion.  If it is a list of syntax description characters (see
+`modify-syntax-entry'), all characters with that syntax auto-complete.
 
-If this is a string, all characters in that string will complete automatically.
+This can also be a function, which is called with the new input and should
+return non-nil if company should auto-complete.
 
-A list of characters represent the syntax (see `modify-syntax-entry') of
-characters that complete automatically."
+A character that is part of a valid candidate never starts auto-completion."
   :group 'company
-  :type '(choice (const :tag "Off" nil)
-                 (function :tag "Predicate function")
-                 (string :tag "Characters")
+  :type '(choice (string :tag "Characters")
                  (set :tag "Syntax"
                       (const :tag "Whitespace" ?\ )
                       (const :tag "Symbol" ?_)
@@ -341,7 +365,8 @@ characters that complete automatically."
                       (const :tag "Comment ender." ?>)
                       (const :tag "Character-quote." ?/)
                       (const :tag "Generic string fence." ?|)
-                      (const :tag "Generic comment fence." ?!))))
+                      (const :tag "Generic comment fence." ?!))
+                 (function :tag "Predicate function")))
 
 (defcustom company-idle-delay .7
   "*The idle delay in seconds until automatic completions starts.
@@ -352,6 +377,17 @@ immediately when a prefix of `company-minimum-prefix-length' is reached."
                  (const :tag "immediate (t)" t)
                  (number :tag "seconds")))
 
+(defcustom company-begin-commands t
+  "*A list of commands following which company will start completing.
+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."
+  :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."
   :group 'company
@@ -482,13 +518,30 @@ keymap during active completions (`company-active-map'):
 
 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-(defun company-grab (regexp &optional expression)
-  (when (looking-back regexp)
+(defun company-grab (regexp &optional expression limit)
+  (when (looking-back regexp limit)
     (or (match-string-no-properties (or expression 0)) "")))
 
-(defun company-in-string-or-comment (&optional point)
-  (let ((pos (syntax-ppss)))
-    (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
+(defun company-grab-line (regexp &optional expression)
+  (company-grab regexp expression (point-at-bol)))
+
+(defun company-grab-symbol ()
+  (if (looking-at "\\_>")
+      (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
+                                                (point)))
+    (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
+      "")))
+
+(defun company-grab-word ()
+  (if (looking-at "\\>")
+      (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
+                                                (point)))
+    (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
+      "")))
+
+(defun company-in-string-or-comment ()
+  (let ((ppss (syntax-ppss)))
+    (or (nth 3 ppss) (nth 4 ppss) (nth 7 ppss))))
 
 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -523,6 +576,8 @@ keymap during active completions (`company-active-map'):
   "Non-nil, if explicit completion took place.")
 (make-variable-buffer-local 'company--explicit-action)
 
+(defvar company--this-command nil)
+
 (defvar company-point nil)
 (make-variable-buffer-local 'company-point)
 
@@ -544,10 +599,14 @@ keymap during active completions (`company-active-map'):
   ;; It's mory efficient to fix it only when they are displayed.
   (concat company-prefix (substring candidate (length company-prefix))))
 
-(defsubst company-should-complete (prefix)
-  (and (eq company-idle-delay t)
-       (not (and transient-mark-mode mark-active))
-       (>= (length prefix) company-minimum-prefix-length)))
+(defun company--should-complete ()
+  (and (not (or buffer-read-only overriding-terminal-local-map
+                overriding-local-map))
+       (eq company-idle-delay t)
+       (or (eq t company-begin-commands)
+           (memq company--this-command company-begin-commands)
+           (and (symbolp this-command) (get this-command 'company-begin)))
+       (not (and transient-mark-mode mark-active))))
 
 (defsubst company-call-frontends (command)
   (dolist (frontend company-frontends)
@@ -640,7 +699,8 @@ keymap during active completions (`company-active-map'):
   (and company-mode
        (not company-candidates)
        (let ((company-idle-delay t)
-             (company-minimum-prefix-length 0))
+             (company-minimum-prefix-length 0)
+             (company-begin-commands t))
          (setq company--explicit-action t)
          (company-begin)))
   ;; Return non-nil if active.
@@ -666,71 +726,72 @@ keymap during active completions (`company-active-map'):
   "Return non-nil, if input starts with punctuation or parentheses."
   (and (> end beg)
        (if (functionp company-auto-complete)
-           (funcall company-auto-complete (buffer-substring beg end))
-         (if (consp company-auto-complete)
-             (memq (char-syntax (char-after beg)) company-auto-complete)
+           (funcall company-auto-complete)
+         company-auto-complete)
+       (if (functionp company-auto-complete-chars)
+           (funcall company-auto-complete-chars (buffer-substring beg end))
+         (if (consp company-auto-complete-chars)
+             (memq (char-syntax (char-after beg)) company-auto-complete-chars)
            (string-match (buffer-substring beg (1+ beg))
-                         company-auto-complete)))))
+                         company-auto-complete-chars)))))
 
 (defun company-continue ()
-  (when company-candidates
-    (when (funcall company-backend 'no-cache company-prefix)
-      ;; Don't complete existing candidates, fetch new ones.
-      (setq company-candidates-cache nil))
-    (let ((new-prefix (funcall company-backend 'prefix)))
-      (unless (and (= (- (point) (length new-prefix))
-                      (- company-point (length company-prefix)))
-                   (or (equal company-prefix new-prefix)
-                       (let ((c (company-calculate-candidates new-prefix)))
-                         ;; t means complete/unique.
-                         (if (eq c t)
-                             (progn (company-cancel new-prefix) t)
-                           (when (consp c)
-                             (setq company-prefix new-prefix)
-                             (company-update-candidates c)
-                             t)))))
-        (if (company-auto-complete-p company-point (point))
-            (save-excursion
-              (goto-char company-point)
-              (company-complete-selection)
+  (when (funcall company-backend 'no-cache company-prefix)
+    ;; Don't complete existing candidates, fetch new ones.
+    (setq company-candidates-cache nil))
+  (let ((new-prefix (funcall company-backend 'prefix)))
+    (unless (and (= (- (point) (length new-prefix))
+                    (- company-point (length company-prefix)))
+                 (or (equal company-prefix new-prefix)
+                     (let ((c (company-calculate-candidates new-prefix)))
+                       ;; t means complete/unique.
+                       (if (eq c t)
+                           (progn (company-cancel new-prefix) t)
+                         (when (consp c)
+                           (setq company-prefix new-prefix)
+                           (company-update-candidates c)
+                           t)))))
+      (if (company-auto-complete-p company-point (point))
+          (save-excursion
+            (goto-char company-point)
+            (company-complete-selection)
+            (setq company-candidates nil))
+        (if (not (and (company-incremental-p company-prefix new-prefix)
+                      (company-require-match-p)))
+            (progn
+              (when (equal company-prefix (car company-candidates))
+                ;; cancel, but last input was actually success
+                (company-cancel company-prefix))
               (setq company-candidates nil))
-          (if (not (and (company-incremental-p company-prefix new-prefix)
-                        (company-require-match-p)))
-              (progn
-                (when (equal company-prefix (car company-candidates))
-                  ;; cancel, but last input was actually success
-                  (company-cancel company-prefix))
-                (setq company-candidates nil))
-            (backward-delete-char (length new-prefix))
-            (insert company-prefix)
-            (ding)
-            (message "Matching input is required")))
-        company-candidates))))
+          (backward-delete-char (length new-prefix))
+          (insert company-prefix)
+          (ding)
+          (message "Matching input is required")))
+      company-candidates)))
 
 (defun company-begin ()
-  (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
-      ;; Don't complete in these cases.
-      (setq company-candidates nil)
-    (company-continue)
-    (unless company-candidates
-      (let (prefix)
-        (dolist (backend (if company-backend
-                             ;; prefer manual override
-                             (list company-backend)
-                           (cons company-backend company-backends)))
-          (when (and (functionp backend)
-                     (setq prefix (funcall backend 'prefix)))
-            (setq company-backend backend)
-            (when (company-should-complete prefix)
-              (let ((c (company-calculate-candidates prefix)))
-                ;; t means complete/unique.  We don't start, so no hooks.
-                (when (consp c)
-                  (setq company-prefix prefix)
-                  (company-update-candidates c)
-                  (run-hook-with-args 'company-completion-started-hook
-                                      (company-explicit-action-p))
-                  (company-call-frontends 'show))))
-            (return prefix))))))
+  (when (if company-candidates
+            (not (company-continue))
+          (company--should-complete))
+    (let (prefix)
+      (dolist (backend (if company-backend
+                           ;; prefer manual override
+                           (list company-backend)
+                         company-backends))
+        (when (and (functionp backend)
+                   (setq prefix (funcall backend 'prefix)))
+          (when (and (stringp prefix)
+                     (>= (length prefix) company-minimum-prefix-length))
+            (setq company-backend backend
+                  company-prefix prefix)
+            (let ((c (company-calculate-candidates prefix)))
+              ;; t means complete/unique.  We don't start, so no hooks.
+              (when (consp c)
+                (company-update-candidates c)
+                (run-hook-with-args 'company-completion-started-hook
+                                    (company-explicit-action-p))
+                (company-call-frontends 'show))))
+          (return prefix)))))
   (if company-candidates
       (progn
         (when (and company-end-of-buffer-workaround (eobp))
@@ -802,6 +863,7 @@ keymap during active completions (`company-active-map'):
   (unless (company-keep this-command)
     (condition-case err
         (progn
+          (setq company--this-command this-command)
           (unless (equal (point) company-point)
             (company-begin))
           (when company-candidates
@@ -1023,8 +1085,8 @@ followed by `company-search-kill-others' after each input."
   "Select the candidate picked by the mouse."
   (interactive "e")
   (when (nth 4 (event-start event))
-    (company-set-selection (- (cdr (posn-col-row (event-start event)))
-                              (cdr (posn-col-row (posn-at-point)))
+    (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
+                              (cdr (posn-actual-col-row (posn-at-point)))
                               1))
     t))
 
@@ -1044,8 +1106,8 @@ followed by `company-search-kill-others' after each input."
   "Complete the common part of all candidates."
   (interactive)
   (when (company-manual-begin)
-    (if (equal company-common (car company-candidates))
-        ;; for success message
+    (if (and (not (cdr company-candidates))
+             (equal company-common (car company-candidates)))
         (company-complete-selection)
       (insert (company-strip-prefix company-common)))))
 
@@ -1062,7 +1124,9 @@ when the selection has been changed, the selected candidate is completed."
       (setq this-command 'company-complete-common))))
 
 (defun company-complete-number (n)
-  "Complete the Nth candidate."
+  "Complete the Nth candidate.
+To show the number next to the candidates in some back-ends, enable
+`company-show-numbers'."
   (when (company-manual-begin)
     (and (< n 1) (> n company-candidates-length)
          (error "No candidate number %d" n))
@@ -1115,7 +1179,7 @@ when the selection has been changed, the selected candidate is completed."
   `(when (company-manual-begin)
      (save-window-excursion
        (let ((height (window-height))
-             (row (cdr (posn-col-row (posn-at-point)))))
+             (row (cdr (posn-actual-col-row (posn-at-point)))))
          ,@body
          (and (< (window-height) height)
               (< (- (window-height) row 2) company-tooltip-limit)
@@ -1279,7 +1343,7 @@ Example:
 
 (defun company-buffer-lines (beg end)
   (goto-char beg)
-  (let ((row (cdr (posn-col-row (posn-at-point))))
+  (let ((row (cdr (posn-actual-col-row (posn-at-point))))
         lines)
     (while (and (equal (move-to-window-line (incf row)) row)
                 (<= (point) end))
@@ -1340,9 +1404,8 @@ Example:
     (setq lines-copy lines)
 
     ;; number can make tooltip too long
-    (and company-show-numbers
-         (< (setq numbered company-tooltip-offset) 10)
-         (incf width 2))
+    (when company-show-numbers
+      (setq numbered company-tooltip-offset))
 
     (when previous
       (push (propertize (company-safe-substring previous 0 width)
@@ -1405,8 +1468,10 @@ Example:
       (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
 
 (defun company-pseudo-tooltip-show-at-point (pos)
-  (let ((col-row (posn-col-row (posn-at-point pos))))
-    (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
+  (let ((col-row (posn-actual-col-row (posn-at-point pos))))
+    (when col-row
+      (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
+                                   company-selection))))
 
 (defun company-pseudo-tooltip-edit (lines selection)
   (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))