X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/8b956d6a100f9a9db2c0bd45c79c28830dcfba7c..3300b2d2b1a199d666bf261a1882aaaaa0d5af5b:/company-elisp.el diff --git a/company-elisp.el b/company-elisp.el index 4f8df83ad..a501efef8 100644 --- a/company-elisp.el +++ b/company-elisp.el @@ -2,7 +2,7 @@ ;; ;; Copyright (C) 2009 Nikolaj Schumacher ;; -;; This file is part of company 0.2. +;; This file is part of company 0.3. ;; ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License @@ -19,6 +19,7 @@ (require 'company) (eval-when-compile (require 'cl)) +(require 'help-mode) (defcustom company-elisp-detect-function-context t "*If enabled, offer lisp functions only in appropriate contexts. @@ -27,12 +28,9 @@ Functions are offered for completion only after ' and \(." :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))) + (let ((prefix (company-grab-symbol))) + (unless (and (company-in-string-or-comment) (/= (char-before (- (point) (length prefix))) ?`)) prefix))) @@ -46,27 +44,41 @@ Functions are offered for completion only after ' and \(." (defvar company-elisp-binding-regexp (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst" "lambda" "lexical-let")) - "\\*?")) + "\\*?") + "Regular expression matching sexps containing variable bindings.") + +(defvar company-elisp-binding-regexp-1 + (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes"))) + "Regular expression matching sexps containing one variable binding.") (defun company-elisp-parse-local (prefix vars) - (let ((regexp (concat "[ \t\n]*\\(" (regexp-quote prefix) - "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))) + (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix) + "\\(?:\\sw\\|\\s_\\)*\\_>\\)")) + (pos (point))) (ignore-errors (save-excursion (dotimes (i company-elisp-parse-depth) (up-list -1) (save-excursion - (when (looking-at company-elisp-binding-regexp) + (cond + ((looking-at company-elisp-binding-regexp) (down-list 2) (ignore-errors (dotimes (i company-elisp-parse-limit) (save-excursion (when (looking-at "[ \t\n]*(") (down-list 1)) - (if (looking-at regexp) - (add-to-list 'vars (match-string-no-properties 1)) - (error))) - (forward-sexp)))))))) + (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) + (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) @@ -90,11 +102,14 @@ 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 + ('interactive (company-begin-backend 'company-elisp)) ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode) - (company-grab-lisp-symbol))) + (or (company-grab-lisp-symbol) 'stop))) ('candidates (company-elisp-candidates arg)) ('meta (company-elisp-doc arg)) ('doc-buffer (let ((symbol (intern arg)))