]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev-code.el
Added company-dabbrev-code.
[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.3.1.
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 (eval-when-compile (require 'cl))
22
23 (defcustom company-dabbrev-code-modes
24 '(asm-mode batch-file-mode c++-mode c-mode cperl-mode csharp-mode css-mode
25 emacs-lisp-mode erlang-mode espresso-mode f90-mode fortran-mode
26 haskell-mode java-mode javascript-mode jde-mode js2-mode lisp-mode
27 lua-mode objc-mode perl-mode php-mode python-mode ruby-mode scheme-mode
28 shell-script-mode)
29 "*Modes that use `company-dabbrev-code'.
30 In all these modes `company-dabbrev-code' will complete only symbols, not text
31 in comments or strings. In other modes `company-dabbrev-code' will pass control
32 to other back-ends \(e.g. `company-dabbrev'\)"
33 :group 'company
34 :type '(repeat (symbol :tag "Major mode")))
35
36 (defun company-dabbrev-code--buffer-symbols (prefix)
37 (save-excursion
38 (goto-char (point-min))
39 (let ((regexp (concat "\\_<" (if (equal prefix "")
40 "\\([a-zA-Z]\\|\\s_\\)"
41 (regexp-quote prefix))
42 "\\(\\sw\\|\\s_\\)*\\_>"))
43 match symbols)
44 (while (re-search-forward regexp nil t)
45 (setq match (match-string-no-properties 0))
46 (unless (company-in-string-or-comment)
47 (add-to-list 'symbols match)))
48 symbols)))
49
50 ;;;###autoload
51 (defun company-dabbrev-code (command &optional arg &rest ignored)
52 "A dabbrev-like `company-mode' back-end for code.
53 The back-end looks for all symbols in the current buffer that aren't in
54 comments or strings."
55 (interactive (list 'interactive))
56 (case command
57 ('interactive (company-begin-backend 'company-dabbrev-code))
58 ('prefix (and (apply 'derived-mode-p company-dabbrev-code-modes)
59 (not (company-in-string-or-comment))
60 (or (company-grab-symbol) 'stop)))
61 ('candidates (let ((case-fold-search nil))
62 (company-dabbrev-code--buffer-symbols arg)))))
63
64 (provide 'company-dabbrev-code)
65 ;;; company-dabbrev-code.el ends here