]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev-code.el
4a17e5d5d32ffbb407557c741510293b4558a795
[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 t
39 "*Determines whether `company-dabbrev-code' should search other buffers.
40 If 'all, search all other buffers. If t, search buffers with the same
41 major-mode.
42 See also `company-dabbrev-code-time-limit'."
43 :group 'company
44 :type '(choice (const :tag "Off" nil)
45 (const :tag "Same major mode" t)
46 (const :tag "All" all)))
47
48 (defcustom company-dabbrev-code-time-limit .5
49 "*Determines how long `company-dabbrev-code' should look for matches."
50 :group 'company
51 :type '(choice (const :tag "Off" nil)
52 (number :tag "Seconds")))
53
54 (defmacro company-dabrev-code--time-limit-while (test start limit &rest body)
55 (declare (indent 3) (debug t))
56 `(let ((company-time-limit-while-counter 0))
57 (catch 'done
58 (while ,test
59 ,@body
60 (and ,limit
61 (eq (incf company-time-limit-while-counter) 25)
62 (setq company-time-limit-while-counter 0)
63 (> (float-time (time-since ,start)) ,limit)
64 (throw 'done 'company-time-out))))))
65
66 (defsubst company-dabbrev-code--make-regexp (prefix)
67 (concat "\\_<" (if (equal prefix "")
68 "\\([a-zA-Z]\\|\\s_\\)"
69 (regexp-quote prefix))
70 "\\(\\sw\\|\\s_\\)*\\_>"))
71
72 (defun company-dabbrev-code--buffer-symbols (regexp pos &optional symbols
73 start limit)
74 (save-excursion
75 (let (match)
76 (goto-char (if pos (1- pos) (point-min)))
77 ;; search before pos
78 (company-dabrev-code--time-limit-while (re-search-backward regexp nil t)
79 start limit
80 (setq match (match-string-no-properties 0))
81 (if (company-in-string-or-comment)
82 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
83 (push match symbols)))
84 (goto-char (or pos (point-min)))
85 ;; search after pos
86 (company-dabrev-code--time-limit-while (re-search-forward regexp nil t)
87 start limit
88 (setq match (match-string-no-properties 0))
89 (if (company-in-string-or-comment)
90 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
91 (push match symbols)))
92 symbols)))
93
94 (defun company-dabbrev-code--symbols (regexp)
95 (let* ((start (current-time))
96 (limit company-dabbrev-code-time-limit)
97 (symbols (company-dabbrev-code--buffer-symbols regexp (point) nil
98 start limit)))
99 (when company-dabbrev-code-other-buffers
100 (dolist (buffer (delq (current-buffer) (buffer-list)))
101 (and (or (eq company-dabbrev-code-other-buffers 'all)
102 (eq (buffer-local-value 'major-mode buffer) major-mode))
103 (with-current-buffer buffer
104 (setq symbols
105 (company-dabbrev-code--buffer-symbols regexp nil symbols
106 start limit))))
107 (and limit
108 (> (float-time (time-since start)) limit)
109 (return))))
110 symbols))
111
112 ;;;###autoload
113 (defun company-dabbrev-code (command &optional arg &rest ignored)
114 "A dabbrev-like `company-mode' back-end for code.
115 The back-end looks for all symbols in the current buffer that aren't in
116 comments or strings."
117 (interactive (list 'interactive))
118 (case command
119 ('interactive (company-begin-backend 'company-dabbrev-code))
120 ('prefix (and (or (eq t company-dabbrev-code-modes)
121 (apply 'derived-mode-p company-dabbrev-code-modes))
122 (not (company-in-string-or-comment))
123 (or (company-grab-symbol) 'stop)))
124 ('candidates (let ((case-fold-search nil))
125 (company-dabbrev-code--symbols
126 (company-dabbrev-code--make-regexp arg))))
127 ('duplicates t)))
128
129 (provide 'company-dabbrev-code)
130 ;;; company-dabbrev-code.el ends here