]> code.delx.au - gnu-emacs-elpa/blobdiff - company.el
Added nxml-mode support.
[gnu-emacs-elpa] / company.el
index d109d3ca10b55e49ee4a737d06f1905484528ec2..d4fd307ed2b58634f8ebfbaf9fdf482d7c84bc5f 100644 (file)
@@ -1,5 +1,9 @@
 (eval-when-compile (require 'cl))
 
+(add-to-list 'debug-ignored-errors
+             "^Pseudo tooltip frontend cannot be used twice$")
+(add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
+
 (defgroup company nil
   ""
   :group 'abbrev
   "*"
   :group 'company)
 
+(defun company-frontends-set (variable value)
+  ;; uniquify
+  (let ((remainder value))
+    (setcdr remainder (delq (car remainder) (cdr remainder))))
+  (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
+       (memq 'company-pseudo-tooltip-frontend value)
+       (error "Pseudo tooltip frontend cannot be used twice"))
+  (and (memq 'company-preview-if-just-one-frontend value)
+       (memq 'company-preview-frontend value)
+       (error "Preview frontend cannot be used twice"))
+  ;; preview must come last
+  (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
+    (when (memq f value)
+      (setq value (append (delq f value) (list f)))))
+  (set variable value))
+
 (defcustom company-frontends '(company-echo-frontend
-                               company-pseudo-tooltip-frontend
-                               company-completion-frontend)
+                               company-pseudo-tooltip-unless-just-one-frontend
+                               company-preview-if-just-one-frontend)
   "*"
+  :set 'company-frontends-set
   :group 'company
-  :type '(repeat (function :tag "function" nil)))
-
-(defcustom company-backends '(company-elisp-completion)
+  :type '(repeat (choice (const :tag "echo" company-echo-frontend)
+                         (const :tag "pseudo tooltip"
+                                company-pseudo-tooltip-frontend)
+                         (const :tag "pseudo tooltip, multiple only"
+                                company-pseudo-tooltip-unless-just-one-frontend)
+                         (const :tag "preview" company-preview-frontend)
+                         (const :tag "preview, unique only"
+                                company-preview-if-just-one-frontend)
+                         (function :tag "custom function" nil))))
+
+(defcustom company-backends '(company-elisp company-nxml)
   "*"
   :group 'company
   :type '(repeat (function :tag "function" nil)))
   (let ((pos (syntax-ppss)))
     (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
 
-;;; elisp
-
-(defvar company-lisp-symbol-regexp
-  "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
-
-(defun company-grab-lisp-symbol ()
-  (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
-    (unless (and (company-in-string-or-comment (- (point) (length prefix)))
-                 (/= (char-before (- (point) (length prefix))) ?`))
-      prefix)))
-
-(defun company-elisp-completion (command &optional arg &rest ignored)
-  (case command
-    ('prefix (and (eq major-mode 'emacs-lisp-mode)
-                  (company-grab-lisp-symbol)))
-    ('candidates (let ((completion-ignore-case nil))
-                   (all-completions arg obarray
-                                    (lambda (symbol) (or (boundp symbol)
-                                                         (fboundp symbol))))))))
-
 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar company-backend nil)
 (defvar company-point nil)
 (make-variable-buffer-local 'company-point)
 
+(defvar company-disabled-backends nil)
+
 (defsubst company-strip-prefix (str)
   (substring str (length company-prefix)))
 
       (let ((completion-ignore-case nil) ;; TODO: make this optional
             prefix)
         (dolist (backend company-backends)
-          (when (setq prefix (funcall backend 'prefix))
-            (when (company-should-complete prefix)
-              (setq company-backend backend
-                    company-prefix prefix
-                    company-candidates
-                    (funcall company-backend 'candidates prefix)
-                    company-common (try-completion prefix company-candidates)
-                    company-selection 0
-                    company-point (point))
-              (company-call-frontends 'update))
-            (return prefix)))
+          (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-prefix prefix
+                        company-candidates
+                        (funcall company-backend 'candidates prefix)
+                        company-common
+                        (try-completion prefix company-candidates)
+                        company-selection 0
+                        company-point (point))
+                  (unless (funcall company-backend 'sorted)
+                    (setq company-candidates
+                          (sort company-candidates 'string<)))
+                  (company-call-frontends 'update))
+                (return prefix))
+            (unless (memq backend company-disabled-backends)
+              (push backend company-disabled-backends)
+              (message "Company back-end '%s' could not be initialized"
+                       backend))))
         (unless (and company-candidates
                      (not (eq t company-common)))
           (company-cancel)))))
           new
           (company-safe-substring old (+ offset (length new)))))
 
-(defun company-modified-substring (beg end lines column)
+(defun company-modified-substring (beg end lines column nl)
   (let ((old (company-buffer-lines beg end))
         new)
     ;; Inject into old lines.
     ;; Append whole new lines.
     (while lines
       (push (company-modify-line "" (pop lines) column) new))
-    (concat (mapconcat 'identity (nreverse new) "\n")
+    (concat (when nl "\n")
+            (mapconcat 'identity (nreverse new) "\n")
             "\n")))
 
 ;; show
 
 
     (move-to-column 0)
-    (move-to-window-line row)
-    (let ((beg (point))
+
+    (let ((nl (< (move-to-window-line row) row))
+          (beg (point))
           (end (save-excursion
                  (move-to-window-line (min (window-height)
                                            (+ row company-tooltip-limit)))
       (setq company-pseudo-tooltip-overlay (make-overlay beg end))
 
       (overlay-put company-pseudo-tooltip-overlay 'before-string
-                   (company-modified-substring beg end lines column))
+                   (company-modified-substring beg end lines column nl))
       (overlay-put company-pseudo-tooltip-overlay 'invisible t)
       (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
 
                     (- (point) (length company-prefix))))
     ('hide (company-pseudo-tooltip-hide))))
 
+(defun company-pseudo-tooltip-unless-just-one-frontend (command)
+  (unless (and (eq command 'post-command)
+               (not (cdr company-candidates)))
+    (company-pseudo-tooltip-frontend command)))
+
 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar company-preview-overlay nil)
     ('post-command (company-preview-show-at-point (point)))
     ('hide (company-preview-hide))))
 
+(defun company-preview-if-just-one-frontend (command)
+  (unless (and (eq command 'post-command)
+               (cdr company-candidates))
+    (company-preview-frontend command)))
+
 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar company-echo-last-msg nil)