]> code.delx.au - gnu-emacs-elpa/blobdiff - company-elisp.el
Remove the As
[gnu-emacs-elpa] / company-elisp.el
index 53b48855cc2d84604ba78026e4c3dfc31fd0c357..2046f6a4e6da82bdf4773a82b983dad7138cb912 100644 (file)
@@ -1,4 +1,4 @@
-;;; company-elisp.el --- company-mode completion back-end for emacs-lisp-mode -*- lexical-binding: t -*-
+;;; company-elisp.el --- company-mode completion back-end for emacs-lisp-mode -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2009, 2011-2013  Free Software Foundation, Inc.
 
@@ -47,8 +47,9 @@ first in the candidates list."
 (defun company-elisp--prefix ()
   (let ((prefix (company-grab-symbol)))
     (if prefix
-        (unless (and (company-in-string-or-comment)
-                     (/= (char-before (- (point) (length prefix))) ?`))
+        (when (if (company-in-string-or-comment)
+                  (= (char-before (- (point) (length prefix))) ?`)
+                (company-elisp--should-complete))
           prefix)
       'stop)))
 
@@ -58,23 +59,49 @@ first in the candidates list."
       (facep symbol)
       (featurep symbol)))
 
+(defun company-elisp--fns-regexp (&rest names)
+  (concat "\\_<\\(?:cl-\\)?" (regexp-opt names) "\\*?\\_>"))
+
 (defvar company-elisp-parse-limit 30)
 (defvar company-elisp-parse-depth 100)
 
+(defvar company-elisp-defun-names '("defun" "defmacro" "defsubst"))
+
 (defvar company-elisp-var-binding-regexp
-  (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("let" "defun" "defmacro" "defsubst"
-                                           "lambda" "lexical-let"))
-          "\\*?\\_>")
+  (apply #'company-elisp--fns-regexp "let" "lambda" "lexical-let"
+         company-elisp-defun-names)
   "Regular expression matching head of a multiple variable bindings form.")
 
 (defvar company-elisp-var-binding-regexp-1
-  (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("dolist" "dotimes")) "\\_>")
+  (company-elisp--fns-regexp "dolist" "dotimes")
   "Regular expression matching head of a form with one variable binding.")
 
 (defvar company-elisp-fun-binding-regexp
-  (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("flet" "labels")) "\\*?\\_>")
+  (company-elisp--fns-regexp "flet" "labels")
   "Regular expression matching head of a function bindings form.")
 
+(defvar company-elisp-defuns-regexp
+  (concat "([ \t\n]*"
+          (apply #'company-elisp--fns-regexp company-elisp-defun-names)))
+
+(defun company-elisp--should-complete ()
+  (let ((start (point))
+        (depth (car (syntax-ppss))))
+    (not
+     (when (> depth 0)
+       (save-excursion
+         (up-list (- depth))
+         (when (looking-at company-elisp-defuns-regexp)
+           (forward-char)
+           (forward-sexp 1)
+           (unless (= (point) start)
+             (condition-case nil
+                 (let ((args-end (scan-sexps (point) 2)))
+                   (or (null args-end)
+                       (> args-end start)))
+               (scan-error
+                t)))))))))
+
 (defun company-elisp--locals (prefix functions-p)
   (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
                         "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
@@ -130,24 +157,29 @@ first in the candidates list."
 
 (defun company-elisp--candidates-predicate (prefix)
   (let* ((completion-ignore-case nil)
-         (before (char-before (- (point) (length prefix)))))
+         (beg (- (point) (length prefix)))
+         (before (char-before beg)))
     (if (and company-elisp-detect-function-context
-             (not (eq before ?')))
+             (not (memq before '(?' ?`))))
         (if (and (eq before ?\()
                  (not
                   (save-excursion
                     (ignore-errors
-                      (up-list -2)
-                      (and (save-excursion
-                             (forward-char 1)
-                             (looking-at "[ \t\n]*("))
-                           (prog1 (search-backward "(")
-                             (forward-char 1))
-                           (looking-at company-elisp-var-binding-regexp))))))
+                      (goto-char (1- beg))
+                      (or (company-elisp--before-binding-varlist-p)
+                          (progn
+                            (up-list -1)
+                            (company-elisp--before-binding-varlist-p)))))))
             'fboundp
           'boundp)
       'company-elisp--predicate)))
 
+(defun company-elisp--before-binding-varlist-p ()
+  (save-excursion
+    (and (prog1 (search-backward "(")
+           (forward-char 1))
+         (looking-at company-elisp-var-binding-regexp))))
+
 (defun company-elisp--doc (symbol)
   (let* ((symbol (intern symbol))
          (doc (if (fboundp symbol)
@@ -159,7 +191,7 @@ first in the candidates list."
 
 ;;;###autoload
 (defun company-elisp (command &optional arg &rest ignored)
-  "`company-mode' completion back-end for `emacs-lisp-mode'."
+  "`company-mode' completion back-end for `emacs-lisp-mode'."
   (interactive (list 'interactive))
   (case command
     (interactive (company-begin-backend 'company-elisp))