]> code.delx.au - gnu-emacs-elpa/commitdiff
Added hooks.
authorNikolaj Schumacher <git@nschum.de>
Tue, 7 Apr 2009 21:02:31 +0000 (23:02 +0200)
committerNikolaj Schumacher <git@nschum.de>
Sun, 12 Apr 2009 22:23:45 +0000 (00:23 +0200)
company.el

index 827eb3e8ab792f41fb49c4147cde21784f2742de..464ac68f5ef36b6070bc4357e0e0e1942bb57b3b 100644 (file)
@@ -65,6 +65,7 @@
 ;;
 ;;; Change Log:
 ;;
+;;    Added hooks.
 ;;    Added `company-require-match' option.
 ;;
 ;; 2009-04-05 (0.2.1)
@@ -269,6 +270,28 @@ does not know about."
   :group 'company
   :type '(repeat (function :tag "function" nil)))
 
+(defvar start-count 0)
+
+(defcustom company-completion-started-hook nil
+  "*Hook run when company starts completing.
+The hook is called with one argument that is non-nil if the completion was
+started manually."
+  :group 'company
+  :type 'hook)
+
+(defcustom company-completion-cancelled-hook nil
+  "*Hook run when company cancels completing.
+The hook is called with one argument that is non-nil if the completion was
+aborted manually."
+  :group 'company
+  :type 'hook)
+
+(defcustom company-completion-finished-hook nil
+  "*Hook run when company successfully completes.
+The hook is called with the selected candidate as an argument."
+  :group 'company
+  :type 'hook)
+
 (defcustom company-minimum-prefix-length 3
   "*The minimum prefix length for automatic completion."
   :group 'company
@@ -550,7 +573,10 @@ keymap during active completions (`company-active-map'):
                                          company-candidates-predicate)))
         (unless (funcall company-backend 'sorted)
           (setq candidates (sort candidates 'string<)))
-        candidates)))
+        (when (or (cdr candidates)
+                  (not (equal (car candidates) prefix)))
+          ;; Don't start when already completed and unique.
+          candidates))))
 
 (defun company-idle-begin (buf win tick pos)
   (and company-mode
@@ -625,9 +651,13 @@ keymap during active completions (`company-active-map'):
                      (setq prefix (funcall backend 'prefix)))
             (setq company-backend backend)
             (when (company-should-complete prefix)
-              (setq company-prefix prefix)
-              (company-update-candidates (company-calculate-candidates prefix))
-              (company-call-frontends 'show))
+              (let ((c (company-calculate-candidates prefix)))
+                (when 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))))))
   (if company-candidates
       (progn
@@ -639,7 +669,7 @@ keymap during active completions (`company-active-map'):
         (company-call-frontends 'update))
     (company-cancel)))
 
-(defun company-cancel ()
+(defun company-cancel (&optional result)
   (and company-added-newline
        (> (point-max) (point-min))
        (let ((tick (buffer-chars-modified-tick)))
@@ -647,6 +677,10 @@ keymap during active completions (`company-active-map'):
          (equal tick company-added-newline))
        ;; Only set unmodified when tick remained the same since insert.
        (set-buffer-modified-p nil))
+  (when company-prefix
+    (if (stringp result)
+        (run-hook-with-args 'company-completion-finished-hook result)
+      (run-hook-with-args 'company-completion-cancelled-hook result)))
   (setq company-added-newline nil
         company-backend nil
         company-prefix nil
@@ -666,7 +700,13 @@ keymap during active completions (`company-active-map'):
   (company-enable-overriding-keymap nil))
 
 (defun company-abort ()
-  (company-cancel)
+  (company-cancel t)
+  ;; Don't start again, unless started manually.
+  (setq company-point (point)))
+
+(defun company-finish (result)
+  (insert (company-strip-prefix result))
+  (company-cancel result)
   ;; Don't start again, unless started manually.
   (setq company-point (point)))
 
@@ -925,14 +965,16 @@ followed by `company-search-kill-others' after each input."
   "Complete the selected candidate."
   (interactive)
   (when (company-manual-begin)
-    (insert (company-strip-prefix (nth company-selection company-candidates)))
-    (company-abort)))
+    (company-finish (nth company-selection company-candidates))))
 
 (defun company-complete-common ()
   "Complete the common part of all candidates."
   (interactive)
   (when (company-manual-begin)
-    (insert (company-strip-prefix company-common))))
+    (if (equal company-common (car company-candidates))
+        ;; for success message
+        (company-complete-selection)
+      (insert (company-strip-prefix company-common)))))
 
 (defun company-complete ()
   "Complete the common part of all candidates or the current selection.
@@ -952,8 +994,7 @@ when the selection has been changed, the selected candidate is completed."
     (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)))
+    (company-finish (nth n company-candidates))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;