]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
Merged company-dabbrev and company-dabbrev-code.
[gnu-emacs-elpa] / 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.3.1.
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 (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))
42 (catch 'done
43 (while ,test
44 ,@body
45 (and ,limit
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))))))
50
51 (defsubst company-dabbrev--make-regexp (prefix)
52 (concat "\\<" (if (equal prefix "") "\\sw" (regexp-quote prefix)) "\\sw*\\>"))
53
54 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
55 ignore-comments)
56 (save-excursion
57 (let (match)
58 (goto-char (if pos (1- pos) (point-min)))
59 ;; search before pos
60 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
61 start limit
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)))
67 ;; search after pos
68 (company-dabrev--time-limit-while (re-search-forward 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-forward "\\s>\\|\\s!\\|\\s\"" nil t)
73 (push match symbols)))
74 symbols)))
75
76 (defun company-dabbrev--search (regexp &optional limit other-buffers
77 ignore-comments)
78 (let* ((start (current-time))
79 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
80 ignore-comments)))
81 (when other-buffers
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
86 (setq symbols
87 (company-dabbrev--search-buffer regexp nil symbols start
88 limit ignore-comments))))
89 (and limit
90 (> (float-time (time-since start)) limit)
91 (return))))
92 symbols))
93
94 ;;;###autoload
95 (defun company-dabbrev (command &optional arg &rest ignored)
96 "A dabbrev-like `company-mode' completion back-end."
97 (interactive (list 'interactive))
98 (case command
99 ('interactive (company-begin-backend 'company-dabbrev))
100 ('prefix (company-grab-word))
101 ('candidates
102 (mapcar 'downcase
103 (company-dabbrev--search (company-dabbrev--make-regexp arg)
104 company-dabbrev-time-limit
105 company-dabbrev-other-buffers)))
106 ('ignore-case t)
107 ('duplicates t)))
108
109 (provide 'company-dabbrev)
110 ;;; company-dabbrev.el ends here