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