]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Added company-dabbrev-code.
[gnu-emacs-elpa] / company.el
index c4bd200e641c963e452cdfbc19df1939ab5cb5cf..6f8f3f6d1aec027a01af63c6d2bfedf9882e1265 100644 (file)
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2009 Nikolaj Schumacher
 ;;
 ;; Author: Nikolaj Schumacher <bugs * nschum de>
-;; Version: 0.3
+;; Version: 0.3.1
 ;; Keywords: abbrev, convenience, matchis
 ;; URL: http://nschum.de/src/emacs/company/
 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
 ;;
 ;;; Change Log:
 ;;
+;;    Added back-end `company-dabbrev-code' for generic code.
+;;    Fixed `company-begin-with'.
+;;
+;; 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.
@@ -242,7 +246,7 @@ The visualized data is stored in `company-prefix', `company-candidates',
 (defcustom company-backends '(company-elisp company-nxml company-css
                               company-semantic company-xcode company-gtags
                               company-etags company-oddmuse company-files
-                              company-dabbrev)
+                              company-dabbrev-code 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
@@ -251,7 +255,7 @@ 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.  The function should return 'stop if it should complete but cannot
-(e.g. if it is in the middle of a string).
+\(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.
@@ -261,6 +265,9 @@ Optional commands:
 'sorted: The back-end may return t here to indicate that the candidates
 are sorted and will not need to be sorted again.
 
+'duplicates: If non-nil, company will take care of removing duplicates
+from the list.
+
 'no-cache: Usually company doesn't ask for candidates again as completion
 progresses, unless the back-end returns t for this command.  The second
 argument is the latest prefix.
@@ -670,6 +677,12 @@ keymap during active completions (`company-active-map'):
                           c company-candidates-predicate)))
                (unless (funcall company-backend 'sorted)
                  (setq c (sort c 'string<)))
+               (when (company-call-backend 'duplicates)
+                 ;; strip duplicates
+                 (let ((c2 c))
+                   (while c2
+                     (setcdr c2 (progn (while (equal (pop c2) (car c2)))
+                                       c2)))))
                c))))
     (if (or (cdr candidates)
             (not (equal (car candidates) prefix)))
@@ -769,9 +782,8 @@ keymap during active completions (`company-active-map'):
       company-candidates)))
 
 (defun company-begin ()
-  (when (if company-candidates
-            (not (company-continue))
-          (company--should-complete))
+  (when (and (not (and company-candidates (company-continue)))
+             (company--should-complete))
     (let (prefix)
       (dolist (backend (if company-backend
                            ;; prefer manual override
@@ -1220,9 +1232,14 @@ To show the number next to the candidates in some back-ends, enable
 (defvar company-callback nil)
 (make-variable-buffer-local 'company-callback)
 
+(defvar company-begin-with-marker nil)
+(make-variable-buffer-local 'company-begin-with-marker)
+
 (defun company-remove-callback (&optional ignored)
   (remove-hook 'company-completion-finished-hook company-callback t)
-  (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t))
+  (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
+  (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
+  (set-marker company-begin-with-marker nil))
 
 (defun company-begin-backend (backend &optional callback)
   "Start a completion at point using BACKEND."
@@ -1231,13 +1248,10 @@ To show the number next to the candidates in some back-ends, enable
                                            'functionp nil "company-")))
                  (when val
                    (list (intern val)))))
-  (when callback
-    (setq company-callback
-          `(lambda (completion)
-             (funcall ',callback completion)
-             (company-remove-callback)))
-    (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
+  (when (setq company-callback callback)
     (add-hook 'company-completion-finished-hook company-callback nil t))
+  (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
+  (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
   (setq company-backend backend)
   ;; Return non-nil if active.
   (or (company-manual-begin)
@@ -1256,10 +1270,11 @@ Example:
 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
   (company-begin-backend
    (let ((start (- (point) (or prefix-length 0))))
+     (setq company-begin-with-marker (copy-marker (point) t))
      `(lambda (command &optional arg &rest ignored)
-        (case command-history
-          ('prefix (message "prefix %s" (buffer-substring ,start (point)))
-                   (when (>= (point) ,start)
+        (case command
+          ('prefix (when (equal (point)
+                                (marker-position company-begin-with-marker))
                      (buffer-substring ,start (point))))
           ('candidates (all-completions arg ',candidates))
           ('require-match ,require-match))))