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