]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev-code.el
Tag company-dabbrev-ignore-buffers with package-version
[gnu-emacs-elpa] / company-dabbrev-code.el
1 ;;; company-dabbrev-code.el --- dabbrev-like company-mode backend for code -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2009, 2011, 2014 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 backend for code."
34 :group 'company)
35
36 (defcustom company-dabbrev-code-modes
37 '(prog-mode
38 batch-file-mode csharp-mode css-mode erlang-mode haskell-mode jde-mode
39 lua-mode python-mode)
40 "Modes that use `company-dabbrev-code'.
41 In all these modes (and their derivatives) `company-dabbrev-code' will
42 complete only symbols, not text in comments or strings. In other modes
43 `company-dabbrev-code' will pass control to other backends
44 \(e.g. `company-dabbrev'\). Value t means complete in all modes."
45 :type '(choice (repeat :tag "Some modes" (symbol :tag "Major mode"))
46 (const :tag "All modes" t)))
47
48 (defcustom company-dabbrev-code-other-buffers t
49 "Determines whether `company-dabbrev-code' should search other buffers.
50 If `all', search all other buffers, except the ignored ones. If t, search
51 buffers with the same major mode. If `code', search all buffers with major
52 modes in `company-dabbrev-code-modes', or derived from one of them. See
53 also `company-dabbrev-code-time-limit'."
54 :type '(choice (const :tag "Off" nil)
55 (const :tag "Same major mode" t)
56 (const :tag "Code major modes" code)
57 (const :tag "All" all)))
58
59 (defcustom company-dabbrev-code-time-limit .1
60 "Determines how long `company-dabbrev-code' should look for matches."
61 :type '(choice (const :tag "Off" nil)
62 (number :tag "Seconds")))
63
64 (defcustom company-dabbrev-code-everywhere nil
65 "Non-nil to offer completions in comments and strings."
66 :type 'boolean)
67
68 (defcustom company-dabbrev-code-ignore-case nil
69 "Non-nil to ignore case when collecting completion candidates."
70 :type 'boolean)
71
72 (defun company-dabbrev-code--make-regexp (prefix)
73 (concat "\\_<" (if (equal prefix "")
74 "\\([a-zA-Z]\\|\\s_\\)"
75 (regexp-quote prefix))
76 "\\(\\sw\\|\\s_\\)*\\_>"))
77
78 ;;;###autoload
79 (defun company-dabbrev-code (command &optional arg &rest ignored)
80 "dabbrev-like `company-mode' backend for code.
81 The backend looks for all symbols in the current buffer that aren't in
82 comments or strings."
83 (interactive (list 'interactive))
84 (cl-case command
85 (interactive (company-begin-backend 'company-dabbrev-code))
86 (prefix (and (or (eq t company-dabbrev-code-modes)
87 (apply #'derived-mode-p company-dabbrev-code-modes))
88 (or company-dabbrev-code-everywhere
89 (not (company-in-string-or-comment)))
90 (or (company-grab-symbol) 'stop)))
91 (candidates (let ((case-fold-search company-dabbrev-code-ignore-case))
92 (company-dabbrev--search
93 (company-dabbrev-code--make-regexp arg)
94 company-dabbrev-code-time-limit
95 (pcase company-dabbrev-code-other-buffers
96 (`t (list major-mode))
97 (`code company-dabbrev-code-modes)
98 (`all `all))
99 t)))
100 (ignore-case company-dabbrev-code-ignore-case)
101 (duplicates t)))
102
103 (provide 'company-dabbrev-code)
104 ;;; company-dabbrev-code.el ends here