]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-dabbrev.el
Bumped version to 0.5.
[sojka/company-mode.git] / company-dabbrev.el
1 ;;; company-dabbrev.el --- a dabbrev-like company-mode completion back-end
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.5.
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-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
26 major-mode.
27 See also `company-dabbrev-time-limit'."
28   :group 'company
29   :type '(choice (const :tag "Off" nil)
30                  (const :tag "Same major mode" t)
31                  (const :tag "All" all)))
32
33 (defcustom company-dabbrev-time-limit .5
34   "*Determines how many seconds `company-dabbrev' should look for matches."
35   :group 'company
36   :type '(choice (const :tag "Off" nil)
37                  (number :tag "Seconds")))
38
39 (defcustom company-dabbrev-char-regexp "\\sw"
40   "*A regular expression matching the characters `company-dabbrev' looks for."
41   :group 'company
42   :type 'regexp)
43
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))
47      (catch 'done
48        (while ,test
49          ,@body
50          (and ,limit
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))))))
55
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 "\\)*\\>"))
61
62 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
63                                        ignore-comments)
64   (save-excursion
65     (let (match)
66       (goto-char (if pos (1- pos) (point-min)))
67       ;; search before pos
68       (company-dabrev--time-limit-while (re-search-backward regexp nil t)
69           start limit
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)))
75       ;; search after pos
76       (company-dabrev--time-limit-while (re-search-forward regexp nil t)
77           start limit
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)))
82       symbols)))
83
84 (defun company-dabbrev--search (regexp &optional limit other-buffers
85                                 ignore-comments)
86   (let* ((start (current-time))
87          (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
88                                                   ignore-comments)))
89     (when other-buffers
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
94                (setq symbols
95                      (company-dabbrev--search-buffer regexp nil symbols start
96                                                      limit ignore-comments))))
97         (and limit
98              (> (float-time (time-since start)) limit)
99              (return))))
100     symbols))
101
102 ;;;###autoload
103 (defun company-dabbrev (command &optional arg &rest ignored)
104   "A dabbrev-like `company-mode' completion back-end."
105   (interactive (list 'interactive))
106   (case command
107     ('interactive (company-begin-backend 'company-dabbrev))
108     ('prefix (company-grab-word))
109     ('candidates
110      (mapcar 'downcase
111              (company-dabbrev--search (company-dabbrev--make-regexp arg)
112                                       company-dabbrev-time-limit
113                                       company-dabbrev-other-buffers)))
114     ('ignore-case t)
115     ('duplicates t)))
116
117 (provide 'company-dabbrev)
118 ;;; company-dabbrev.el ends here