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.3.1.
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 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
40 (declare (indent 3) (debug t))
41 `(let ((company-time-limit-while-counter 0))
46 (eq (incf company-time-limit-while-counter) 25)
47 (setq company-time-limit-while-counter 0)
48 (> (float-time (time-since ,start)) ,limit)
49 (throw 'done 'company-time-out))))))
51 (defsubst company-dabbrev--make-regexp (prefix)
52 (concat "\\<" (if (equal prefix "") "\\sw" (regexp-quote prefix)) "\\sw*\\>"))
54 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
58 (goto-char (if pos (1- pos) (point-min)))
60 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
62 (setq match (match-string-no-properties 0))
63 (if (and ignore-comments (company-in-string-or-comment))
64 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
65 (push match symbols)))
66 (goto-char (or pos (point-min)))
68 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
70 (setq match (match-string-no-properties 0))
71 (if (and ignore-comments (company-in-string-or-comment))
72 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
73 (push match symbols)))
76 (defun company-dabbrev--search (regexp &optional limit other-buffers
78 (let* ((start (current-time))
79 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
82 (dolist (buffer (delq (current-buffer) (buffer-list)))
83 (and (or (eq other-buffers 'all)
84 (eq (buffer-local-value 'major-mode buffer) major-mode))
85 (with-current-buffer buffer
87 (company-dabbrev--search-buffer regexp nil symbols start
88 limit ignore-comments))))
90 (> (float-time (time-since start)) limit)
95 (defun company-dabbrev (command &optional arg &rest ignored)
96 "A dabbrev-like `company-mode' completion back-end."
97 (interactive (list 'interactive))
99 ('interactive (company-begin-backend 'company-dabbrev))
100 ('prefix (company-grab-word))
103 (company-dabbrev--search (company-dabbrev--make-regexp arg)
104 company-dabbrev-time-limit
105 company-dabbrev-other-buffers)))
109 (provide 'company-dabbrev)
110 ;;; company-dabbrev.el ends here