1 ;;; company-dabbrev.el --- a dabbrev-like company-mode completion back-end
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; This file is part of company 0.4.3.
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.
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.
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/>.
21 (eval-when-compile (require 'cl))
23 (defcustom company-dabbrev-other-buffers 'all
24 "*Determines whether `company-dabbrev' should search other buffers.
25 If 'all, search all other buffers. If t, search buffers with the same
27 See also `company-dabbrev-time-limit'."
29 :type '(choice (const :tag "Off" nil)
30 (const :tag "Same major mode" t)
31 (const :tag "All" all)))
33 (defcustom company-dabbrev-time-limit .5
34 "*Determines how many seconds `company-dabbrev' should look for matches."
36 :type '(choice (const :tag "Off" nil)
37 (number :tag "Seconds")))
39 (defcustom company-dabbrev-char-regexp "\\sw"
40 "*A regular expression matching the characters `company-dabbrev' looks for."
44 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
45 (declare (indent 3) (debug t))
46 `(let ((company-time-limit-while-counter 0))
51 (eq (incf company-time-limit-while-counter) 25)
52 (setq company-time-limit-while-counter 0)
53 (> (float-time (time-since ,start)) ,limit)
54 (throw 'done 'company-time-out))))))
56 (defsubst company-dabbrev--make-regexp (prefix)
57 (concat "\\<" (if (equal prefix "")
58 company-dabbrev-char-regexp
59 (regexp-quote prefix))
60 "\\(" company-dabbrev-char-regexp "\\)*\\>"))
62 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
66 (goto-char (if pos (1- pos) (point-min)))
68 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
70 (setq match (match-string-no-properties 0))
71 (if (and ignore-comments (company-in-string-or-comment))
72 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
73 (push match symbols)))
74 (goto-char (or pos (point-min)))
76 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
78 (setq match (match-string-no-properties 0))
79 (if (and ignore-comments (company-in-string-or-comment))
80 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
81 (push match symbols)))
84 (defun company-dabbrev--search (regexp &optional limit other-buffers
86 (let* ((start (current-time))
87 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
90 (dolist (buffer (delq (current-buffer) (buffer-list)))
91 (and (or (eq other-buffers 'all)
92 (eq (buffer-local-value 'major-mode buffer) major-mode))
93 (with-current-buffer buffer
95 (company-dabbrev--search-buffer regexp nil symbols start
96 limit ignore-comments))))
98 (> (float-time (time-since start)) limit)
103 (defun company-dabbrev (command &optional arg &rest ignored)
104 "A dabbrev-like `company-mode' completion back-end."
105 (interactive (list 'interactive))
107 ('interactive (company-begin-backend 'company-dabbrev))
108 ('prefix (company-grab-word))
111 (company-dabbrev--search (company-dabbrev--make-regexp arg)
112 company-dabbrev-time-limit
113 company-dabbrev-other-buffers)))
117 (provide 'company-dabbrev)
118 ;;; company-dabbrev.el ends here