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