]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-dabbrev-code.el
0a531b2d897c3f965a81a11cb6a630860c1c61cf
[gnu-emacs-elpa] / packages / company-0.5 / company-dabbrev-code.el
1 ;;; company-dabbrev-code.el --- a dabbrev-like company-mode back-end for code
2
3 ;; Copyright (C) 2009 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 ;;; Code:
23
24 (require 'company)
25 (require 'company-dabbrev)
26 (eval-when-compile (require 'cl))
27
28 (defcustom company-dabbrev-code-modes
29 '(asm-mode batch-file-mode c++-mode c-mode cperl-mode csharp-mode css-mode
30 emacs-lisp-mode erlang-mode espresso-mode f90-mode fortran-mode
31 haskell-mode java-mode javascript-mode jde-mode js2-mode lisp-mode
32 lua-mode objc-mode perl-mode php-mode python-mode ruby-mode scheme-mode
33 shell-script-mode)
34 "*Modes that use `company-dabbrev-code'.
35 In all these modes `company-dabbrev-code' will complete only symbols, not text
36 in comments or strings. In other modes `company-dabbrev-code' will pass control
37 to other back-ends \(e.g. `company-dabbrev'\).
38 Value t means complete in all modes."
39 :group 'company
40 :type '(choice (repeat (symbol :tag "Major mode"))
41 (const tag "All modes" t)))
42
43 (defcustom company-dabbrev-code-other-buffers t
44 "*Determines whether `company-dabbrev-code' should search other buffers.
45 If 'all, search all other buffers. If t, search buffers with the same
46 major-mode.
47 See also `company-dabbrev-code-time-limit'."
48 :group 'company
49 :type '(choice (const :tag "Off" nil)
50 (const :tag "Same major mode" t)
51 (const :tag "All" all)))
52
53 (defcustom company-dabbrev-code-time-limit .5
54 "*Determines how long `company-dabbrev-code' should look for matches."
55 :group 'company
56 :type '(choice (const :tag "Off" nil)
57 (number :tag "Seconds")))
58
59 (defsubst company-dabbrev-code--make-regexp (prefix)
60 (concat "\\_<" (if (equal prefix "")
61 "\\([a-zA-Z]\\|\\s_\\)"
62 (regexp-quote prefix))
63 "\\(\\sw\\|\\s_\\)*\\_>"))
64
65 ;;;###autoload
66 (defun company-dabbrev-code (command &optional arg &rest ignored)
67 "A dabbrev-like `company-mode' back-end for code.
68 The back-end looks for all symbols in the current buffer that aren't in
69 comments or strings."
70 (interactive (list 'interactive))
71 (case command
72 ('interactive (company-begin-backend 'company-dabbrev-code))
73 ('prefix (and (or (eq t company-dabbrev-code-modes)
74 (apply 'derived-mode-p company-dabbrev-code-modes))
75 (not (company-in-string-or-comment))
76 (or (company-grab-symbol) 'stop)))
77 ('candidates (let ((completion-ignore-case nil))
78 (company-dabbrev--search
79 (company-dabbrev-code--make-regexp arg)
80 company-dabbrev-code-time-limit
81 company-dabbrev-code-other-buffers t)))
82 ('duplicates t)))
83
84 (provide 'company-dabbrev-code)
85 ;;; company-dabbrev-code.el ends here