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