]> code.delx.au - gnu-emacs-elpa/blobdiff - company-elisp.el
Silence some byte-compilation warnings
[gnu-emacs-elpa] / company-elisp.el
index c26e16ee0731f51dc5f8240e02b5e40c8e3ddeb8..986cd3d4b73b254f62c2f309096022ac2e0b4220 100644 (file)
@@ -1,51 +1,62 @@
-;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
-;;
-;; Copyright (C) 2009 Nikolaj Schumacher
-;;
-;; This file is part of company 0.2.
-;;
-;; This program is free software; you can redistribute it and/or
-;; modify it under the terms of the GNU General Public License
-;; as published by the Free Software Foundation; either version 2
-;; of the License, or (at your option) any later version.
-;;
-;; This program is distributed in the hope that it will be useful,
+;;; company-elisp.el --- A company-mode completion back-end for emacs-lisp-mode
+
+;; Copyright (C) 2009, 2011-2012  Free Software Foundation, Inc.
+
+;; Author: Nikolaj Schumacher
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
-;;
+
 ;; You should have received a copy of the GNU General Public License
-;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+
+;;; Commentary:
+;;
+
+;;; Code:
 
 (require 'company)
 (eval-when-compile (require 'cl))
+(require 'help-mode)
+(require 'find-func)
 
 (defcustom company-elisp-detect-function-context t
-  "*If enabled, offer lisp functions only in appropriate contexts.
+  "If enabled, offer Lisp functions only in appropriate contexts.
 Functions are offered for completion only after ' and \(."
   :group 'company
   :type '(choice (const :tag "Off" nil)
                  (const :tag "On" t)))
 
-(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)))
+  (let ((prefix (company-grab-symbol)))
+    (if prefix
+        (unless (and (company-in-string-or-comment)
+                     (/= (char-before (- (point) (length prefix))) ?`))
+          prefix)
+      'stop)))
 
 (defun company-elisp-predicate (symbol)
   (or (boundp symbol)
-      (fboundp symbol)))
+      (fboundp symbol)
+      (facep symbol)
+      (featurep symbol)))
 
 (defvar company-elisp-parse-limit 30)
 (defvar company-elisp-parse-depth 100)
 
 (defvar company-elisp-binding-regexp
   (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
-                                        "lambda" "lexical-let"))
+                                        "lambda" "lexical-let" "flet" "labels"))
           "\\*?")
   "Regular expression matching sexps containing variable bindings.")
 
@@ -55,7 +66,8 @@ Functions are offered for completion only after ' and \(."
 
 (defun company-elisp-parse-local (prefix vars)
   (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
-                        "\\(?:\\sw\\|\\s_\\)*\\_>\\)")))
+                        "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
+        (pos (point)))
     (ignore-errors
       (save-excursion
         (dotimes (i company-elisp-parse-depth)
@@ -69,13 +81,17 @@ Functions are offered for completion only after ' and \(."
                   (save-excursion
                     (when (looking-at "[ \t\n]*(")
                       (down-list 1))
-                    (when (looking-at regexp)
-                      (add-to-list 'vars (match-string-no-properties 1))))
+                    (and (looking-at regexp)
+                         ;; Don't add incomplete text as candidate.
+                         (not (eq (match-end 0) pos))
+                         (add-to-list 'vars (match-string-no-properties 1))))
                   (forward-sexp))))
              ((looking-at company-elisp-binding-regexp-1)
               (down-list 2)
-              (when (looking-at regexp)
-                (add-to-list 'vars (match-string-no-properties 1)))))))))
+              (and (looking-at regexp)
+                   ;; Don't add incomplete text as candidate.
+                   (not (eq (match-end 0) pos))
+                   (add-to-list 'vars (match-string-no-properties 1)))))))))
     vars))
 
 (defun company-elisp-candidates (prefix)
@@ -99,22 +115,34 @@ Functions are offered for completion only after ' and \(."
          (string-match ".*$" doc)
          (match-string 0 doc))))
 
+;;;###autoload
 (defun company-elisp (command &optional arg &rest ignored)
   "A `company-mode' completion back-end for `emacs-lisp-mode'."
+  (interactive (list 'interactive))
   (case command
-    ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
-                  (company-grab-lisp-symbol)))
-    ('candidates (company-elisp-candidates arg))
-    ('meta (company-elisp-doc arg))
-    ('doc-buffer (let ((symbol (intern arg)))
-                   (save-window-excursion
-                     (when (or (ignore-errors (describe-function symbol))
-                               (ignore-errors (describe-variable symbol)))
-                       (help-buffer)))))
-    ('location (let ((sym (intern arg)))
-                 (or (ignore-errors (find-definition-noselect sym nil))
-                     (ignore-errors (find-definition-noselect sym 'defvar))
-                     (ignore-errors (find-definition-noselect sym t)))))))
+    (interactive (company-begin-backend 'company-elisp))
+    (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
+                 (company-grab-lisp-symbol)))
+    (candidates (company-elisp-candidates arg))
+    (meta (company-elisp-doc arg))
+    (doc-buffer (let ((symbol (intern arg)))
+                  (save-window-excursion
+                    (ignore-errors
+                      (cond
+                       ((fboundp symbol) (describe-function symbol))
+                       ((boundp symbol) (describe-variable symbol))
+                       ((featurep symbol) (describe-package symbol))
+                       ((facep symbol) (describe-face symbol))
+                       (t (signal 'user-error nil)))
+                      (help-buffer)))))
+    (location (let ((sym (intern arg)))
+                (cond
+                 ((fboundp sym) (find-definition-noselect sym nil))
+                 ((boundp sym) (find-definition-noselect sym 'defvar))
+                 ((featurep sym) (cons (find-file-noselect (find-library-name
+                                                            (symbol-name sym)))
+                                       0))
+                 ((facep sym) (find-definition-noselect sym 'defface)))))))
 
 (provide 'company-elisp)
 ;;; company-elisp.el ends here