]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev-code.el
Extracted company-dabbrev-code--make-regexp.
[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 Value t means complete in all modes."
34 :group 'company
35 :type '(choice (repeat (symbol :tag "Major mode"))
36 (const tag "All modes" t)))
37
38 (defcustom company-dabbrev-code-other-buffers .5
39 "*Determines whether `company-dabbrev-code' should search other buffers.
40 If t, search all buffers with the same major-mode. A numeric value means
41 search other buffers for that many seconds and then return."
42 :group 'company
43 :type '(choice (const :tag "Off" nil)
44 (const :tag "Same major mode" t)
45 (number :tag "Seconds")))
46
47 (defmacro company-dabrev-code--time-limit-while (test start limit &rest body)
48 (declare (indent 3) (debug t))
49 `(let ((company-time-limit-while-counter 0))
50 (catch 'done
51 (while ,test
52 ,@body
53 (and ,limit
54 (eq (incf company-time-limit-while-counter) 25)
55 (setq company-time-limit-while-counter 0)
56 (> (float-time (time-since ,start)) ,limit)
57 (throw 'done 'company-time-out))))))
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 (defun company-dabbrev-code--buffer-symbols (prefix pos &optional symbols
66 start limit)
67 (save-excursion
68 (goto-char (point-min))
69 (let ((regexp (company-dabbrev-code--make-regexp prefix))
70 match)
71 (company-dabrev-code--time-limit-while (re-search-forward regexp nil t)
72 start limit
73 (setq match (match-string-no-properties 0))
74 (if (company-in-string-or-comment)
75 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
76 (unless (eq (match-end 0) pos) ;; ignore match before point
77 (push match symbols))))
78 symbols)))
79
80 (defun company-dabbrev-code--symbols (prefix &optional limit)
81 (let ((start (current-time))
82 (symbols (company-dabbrev-code--buffer-symbols prefix (point))))
83 (dolist (buffer (delq (current-buffer) (buffer-list)))
84 (and (eq (buffer-local-value 'major-mode buffer) major-mode)
85 (with-current-buffer buffer
86 (setq symbols
87 (company-dabbrev-code--buffer-symbols prefix nil symbols
88 start limit))))
89 (and limit
90 (> (float-time (time-since start)) limit)
91 (return)))
92 symbols))
93
94 ;;;###autoload
95 (defun company-dabbrev-code (command &optional arg &rest ignored)
96 "A dabbrev-like `company-mode' back-end for code.
97 The back-end looks for all symbols in the current buffer that aren't in
98 comments or strings."
99 (interactive (list 'interactive))
100 (case command
101 ('interactive (company-begin-backend 'company-dabbrev-code))
102 ('prefix (and (or (eq t company-dabbrev-code-modes)
103 (apply 'derived-mode-p company-dabbrev-code-modes))
104 (not (company-in-string-or-comment))
105 (or (company-grab-symbol) 'stop)))
106 ('candidates (let ((case-fold-search nil))
107 (if company-dabbrev-code-other-buffers
108 (company-dabbrev-code--symbols
109 arg
110 (when (numberp company-dabbrev-code-other-buffers)
111 company-dabbrev-code-other-buffers))
112 (company-dabbrev-code--buffer-symbols arg (point)))))
113 ('duplicates t)))
114
115 (provide 'company-dabbrev-code)
116 ;;; company-dabbrev-code.el ends here