]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-dabbrev.el
2480f798219cfe6380967df5a194e40d562ef552
[gnu-emacs-elpa] / packages / company-0.5 / company-dabbrev.el
1 ;;; company-dabbrev.el --- a dabbrev-like company-mode completion back-end
2
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Code:
23
24 (require 'company)
25 (eval-when-compile (require 'cl))
26
27 (defcustom company-dabbrev-other-buffers 'all
28 "*Determines whether `company-dabbrev' should search other buffers.
29 If 'all, search all other buffers. If t, search buffers with the same
30 major-mode.
31 See also `company-dabbrev-time-limit'."
32 :group 'company
33 :type '(choice (const :tag "Off" nil)
34 (const :tag "Same major mode" t)
35 (const :tag "All" all)))
36
37 (defcustom company-dabbrev-time-limit .5
38 "*Determines how many seconds `company-dabbrev' should look for matches."
39 :group 'company
40 :type '(choice (const :tag "Off" nil)
41 (number :tag "Seconds")))
42
43 (defcustom company-dabbrev-char-regexp "\\sw"
44 "*A regular expression matching the characters `company-dabbrev' looks for."
45 :group 'company
46 :type 'regexp)
47
48 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
49 (declare (indent 3) (debug t))
50 `(let ((company-time-limit-while-counter 0))
51 (catch 'done
52 (while ,test
53 ,@body
54 (and ,limit
55 (eq (incf company-time-limit-while-counter) 25)
56 (setq company-time-limit-while-counter 0)
57 (> (float-time (time-since ,start)) ,limit)
58 (throw 'done 'company-time-out))))))
59
60 (defsubst company-dabbrev--make-regexp (prefix)
61 (concat "\\<" (if (equal prefix "")
62 company-dabbrev-char-regexp
63 (regexp-quote prefix))
64 "\\(" company-dabbrev-char-regexp "\\)*\\>"))
65
66 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
67 ignore-comments)
68 (save-excursion
69 (let (match)
70 (goto-char (if pos (1- pos) (point-min)))
71 ;; search before pos
72 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
73 start limit
74 (setq match (match-string-no-properties 0))
75 (if (and ignore-comments (company-in-string-or-comment))
76 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
77 (push match symbols)))
78 (goto-char (or pos (point-min)))
79 ;; search after pos
80 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
81 start limit
82 (setq match (match-string-no-properties 0))
83 (if (and ignore-comments (company-in-string-or-comment))
84 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
85 (push match symbols)))
86 symbols)))
87
88 (defun company-dabbrev--search (regexp &optional limit other-buffers
89 ignore-comments)
90 (let* ((start (current-time))
91 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
92 ignore-comments)))
93 (when other-buffers
94 (dolist (buffer (delq (current-buffer) (buffer-list)))
95 (and (or (eq other-buffers 'all)
96 (eq (buffer-local-value 'major-mode buffer) major-mode))
97 (with-current-buffer buffer
98 (setq symbols
99 (company-dabbrev--search-buffer regexp nil symbols start
100 limit ignore-comments))))
101 (and limit
102 (> (float-time (time-since start)) limit)
103 (return))))
104 symbols))
105
106 ;;;###autoload
107 (defun company-dabbrev (command &optional arg &rest ignored)
108 "A dabbrev-like `company-mode' completion back-end."
109 (interactive (list 'interactive))
110 (case command
111 ('interactive (company-begin-backend 'company-dabbrev))
112 ('prefix (company-grab-word))
113 ('candidates
114 (mapcar 'downcase
115 (company-dabbrev--search (company-dabbrev--make-regexp arg)
116 company-dabbrev-time-limit
117 company-dabbrev-other-buffers)))
118 ('ignore-case t)
119 ('duplicates t)))
120
121 (provide 'company-dabbrev)
122 ;;; company-dabbrev.el ends here