]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev-code.el
Define groups for back-ends; remove explicit :group declarations
[gnu-emacs-elpa] / company-dabbrev-code.el
1 ;;; company-dabbrev-code.el --- dabbrev-like company-mode back-end for code
2
3 ;; Copyright (C) 2009, 2011 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'company-dabbrev)
30 (eval-when-compile (require 'cl))
31
32 (defgroup company-dabbrev-code nil
33 "dabbrev-like completion back-end for code."
34 :group 'company)
35
36 (defcustom company-dabbrev-code-modes
37 '(asm-mode batch-file-mode c++-mode c-mode cperl-mode csharp-mode css-mode
38 emacs-lisp-mode erlang-mode espresso-mode f90-mode fortran-mode
39 haskell-mode java-mode javascript-mode jde-mode js2-mode lisp-mode
40 lua-mode objc-mode perl-mode php-mode python-mode ruby-mode scheme-mode
41 shell-script-mode)
42 "Modes that use `company-dabbrev-code'.
43 In all these modes `company-dabbrev-code' will complete only symbols, not text
44 in comments or strings. In other modes `company-dabbrev-code' will pass control
45 to other back-ends \(e.g. `company-dabbrev'\).
46 Value t means complete in all modes."
47 :type '(choice (repeat (symbol :tag "Major mode"))
48 (const tag "All modes" t)))
49
50 (defcustom company-dabbrev-code-other-buffers t
51 "Determines whether `company-dabbrev-code' should search other buffers.
52 If `all', search all other buffers. If t, search buffers with the same
53 major mode.
54 See also `company-dabbrev-code-time-limit'."
55 :type '(choice (const :tag "Off" nil)
56 (const :tag "Same major mode" t)
57 (const :tag "All" all)))
58
59 (defcustom company-dabbrev-code-time-limit .5
60 "Determines how long `company-dabbrev-code' should look for matches."
61 :type '(choice (const :tag "Off" nil)
62 (number :tag "Seconds")))
63
64 (defsubst company-dabbrev-code--make-regexp (prefix)
65 (concat "\\_<" (if (equal prefix "")
66 "\\([a-zA-Z]\\|\\s_\\)"
67 (regexp-quote prefix))
68 "\\(\\sw\\|\\s_\\)*\\_>"))
69
70 ;;;###autoload
71 (defun company-dabbrev-code (command &optional arg &rest ignored)
72 "dabbrev-like `company-mode' back-end for code.
73 The back-end looks for all symbols in the current buffer that aren't in
74 comments or strings."
75 (interactive (list 'interactive))
76 (case command
77 (interactive (company-begin-backend 'company-dabbrev-code))
78 (prefix (and (or (eq t company-dabbrev-code-modes)
79 (apply 'derived-mode-p company-dabbrev-code-modes))
80 (not (company-in-string-or-comment))
81 (or (company-grab-symbol) 'stop)))
82 (candidates (let ((case-fold-search nil))
83 (company-dabbrev--search
84 (company-dabbrev-code--make-regexp arg)
85 company-dabbrev-code-time-limit
86 company-dabbrev-code-other-buffers t)))
87 (duplicates t)))
88
89 (provide 'company-dabbrev-code)
90 ;;; company-dabbrev-code.el ends here