1 ;;; company-dabbrev.el --- dabbrev-like company-mode completion back-end
3 ;; Copyright (C) 2009, 2011 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
29 (eval-when-compile (require 'cl))
31 (defgroup company-dabbrev nil
32 "dabbrev-like completion back-end."
35 (defcustom company-dabbrev-other-buffers 'all
36 "Determines whether `company-dabbrev' should search other buffers.
37 If `all', search all other buffers. If t, search buffers with the same
39 See also `company-dabbrev-time-limit'."
40 :type '(choice (const :tag "Off" nil)
41 (const :tag "Same major mode" t)
42 (const :tag "All" all)))
44 (defcustom company-dabbrev-time-limit .1
45 "Determines how many seconds `company-dabbrev' should look for matches."
46 :type '(choice (const :tag "Off" nil)
47 (number :tag "Seconds")))
49 (defcustom company-dabbrev-char-regexp "\\sw"
50 "A regular expression matching the characters `company-dabbrev' looks for."
53 (defcustom company-dabbrev-ignore-case 'keep-prefix
54 "The value of `ignore-case' returned by `company-dabbrev'.")
56 (defcustom company-dabbrev-minimum-length (1+ company-minimum-prefix-length)
57 "The minimum length for the string to be included.")
59 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
60 (declare (indent 3) (debug t))
61 `(let ((company-time-limit-while-counter 0))
66 (eq (incf company-time-limit-while-counter) 25)
67 (setq company-time-limit-while-counter 0)
68 (> (float-time (time-since ,start)) ,limit)
69 (throw 'done 'company-time-out))))))
71 (defsubst company-dabbrev--make-regexp (prefix)
72 (concat "\\<" (if (equal prefix "")
73 company-dabbrev-char-regexp
74 (regexp-quote prefix))
75 "\\(" company-dabbrev-char-regexp "\\)*\\>"))
77 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
81 (goto-char (if pos (1- pos) (point-min)))
83 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
85 (setq match (match-string-no-properties 0))
86 (if (and ignore-comments (company-in-string-or-comment))
87 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
88 (when (>= (length match) company-dabbrev-minimum-length)
89 (push match symbols))))
90 (goto-char (or pos (point-min)))
92 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
94 (setq match (match-string-no-properties 0))
95 (if (and ignore-comments (company-in-string-or-comment))
96 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
97 (when (>= (length match) company-dabbrev-minimum-length)
98 (push match symbols))))
101 (defun company-dabbrev--search (regexp &optional limit other-buffers
103 (let* ((start (current-time))
104 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
107 (dolist (buffer (delq (current-buffer) (buffer-list)))
108 (and (or (eq other-buffers 'all)
109 (eq (buffer-local-value 'major-mode buffer) major-mode))
110 (with-current-buffer buffer
112 (company-dabbrev--search-buffer regexp nil symbols start
113 limit ignore-comments))))
115 (> (float-time (time-since start)) limit)
120 (defun company-dabbrev (command &optional arg &rest ignored)
121 "dabbrev-like `company-mode' completion back-end."
122 (interactive (list 'interactive))
124 (interactive (company-begin-backend 'company-dabbrev))
125 (prefix (company-grab-word))
128 (company-dabbrev--search (company-dabbrev--make-regexp arg)
129 company-dabbrev-time-limit
130 company-dabbrev-other-buffers)))
131 (ignore-case company-dabbrev-ignore-case)
134 (provide 'company-dabbrev)
135 ;;; company-dabbrev.el ends here