]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
Tag company-dabbrev-ignore-buffers with package-version
[gnu-emacs-elpa] / company-dabbrev.el
1 ;;; company-dabbrev.el --- dabbrev-like company-mode completion backend -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2009, 2011, 2014, 2015, 2016 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 (require 'cl-lib)
30
31 (defgroup company-dabbrev nil
32 "dabbrev-like completion backend."
33 :group 'company)
34
35 (defcustom company-dabbrev-other-buffers 'all
36 "Determines whether `company-dabbrev' should search other buffers.
37 If `all', search all other buffers, except the ignored ones. If t, search
38 buffers with the same major mode. See also `company-dabbrev-time-limit'."
39 :type '(choice (const :tag "Off" nil)
40 (const :tag "Same major mode" t)
41 (const :tag "All" all)))
42
43 (defcustom company-dabbrev-ignore-buffers "\\`[ *]"
44 "Regexp matching the names of buffers to ignore.
45 Or a function that returns non-nil for such buffers."
46 :type '(choice (regexp :tag "Regexp")
47 (function :tag "Predicate"))
48 :package-version '(company . "0.9.0"))
49
50 (defcustom company-dabbrev-time-limit .1
51 "Determines how many seconds `company-dabbrev' should look for matches."
52 :type '(choice (const :tag "Off" nil)
53 (number :tag "Seconds")))
54
55 (defcustom company-dabbrev-char-regexp "\\sw"
56 "A regular expression matching the characters `company-dabbrev' looks for."
57 :type 'regexp)
58
59 (defcustom company-dabbrev-ignore-case 'keep-prefix
60 "Non-nil to ignore case when collecting completion candidates.
61 When it's `keep-prefix', the text before point will remain unchanged after
62 candidate is inserted, even some of its characters have different case.")
63
64 (defcustom company-dabbrev-downcase 'case-replace
65 "Whether to downcase the returned candidates.
66
67 The value of nil means keep them as-is.
68 `case-replace' means use the value of `case-replace'.
69 Any other value means downcase.
70
71 If you set this value to nil, you may also want to set
72 `company-dabbrev-ignore-case' to any value other than `keep-prefix'.")
73
74 (defcustom company-dabbrev-minimum-length 4
75 "The minimum length for the completion candidate to be included.
76 This variable affects both `company-dabbrev' and `company-dabbrev-code'."
77 :type 'integer
78 :package-version '(company . "0.8.3"))
79
80 (defcustom company-dabbrev-ignore-invisible nil
81 "Non-nil to skip invisible text."
82 :type 'boolean
83 :package-version '(company . "0.9.0"))
84
85 (defmacro company-dabbrev--time-limit-while (test start limit freq &rest body)
86 (declare (indent 3) (debug t))
87 `(let ((company-time-limit-while-counter 0))
88 (catch 'done
89 (while ,test
90 ,@body
91 (and ,limit
92 (= (cl-incf company-time-limit-while-counter) ,freq)
93 (setq company-time-limit-while-counter 0)
94 (> (float-time (time-since ,start)) ,limit)
95 (throw 'done 'company-time-out))))))
96
97 (defun company-dabbrev--make-regexp ()
98 (concat "\\(?:" company-dabbrev-char-regexp "\\)+"))
99
100 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
101 ignore-comments)
102 (save-excursion
103 (cl-labels ((maybe-collect-match
104 ()
105 (let ((match (match-string-no-properties 0)))
106 (when (and (>= (length match) company-dabbrev-minimum-length)
107 (not (and company-dabbrev-ignore-invisible
108 (invisible-p (match-beginning 0)))))
109 (push match symbols)))))
110 (goto-char (if pos (1- pos) (point-min)))
111 ;; Search before pos.
112 (let ((tmp-end (point)))
113 (company-dabbrev--time-limit-while (> tmp-end (point-min))
114 start limit 1
115 (ignore-errors
116 (forward-char -10000))
117 (forward-line 0)
118 (save-excursion
119 ;; Before, we used backward search, but it matches non-greedily, and
120 ;; that forced us to use the "beginning/end of word" anchors in
121 ;; `company-dabbrev--make-regexp'. It's also about 2x slower.
122 (while (re-search-forward regexp tmp-end t)
123 (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
124 (re-search-forward "\\s>\\|\\s!\\|\\s\"" tmp-end t)
125 (maybe-collect-match))))
126 (setq tmp-end (point))))
127 (goto-char (or pos (point-min)))
128 ;; Search after pos.
129 (company-dabbrev--time-limit-while (re-search-forward regexp nil t)
130 start limit 25
131 (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
132 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
133 (maybe-collect-match)))
134 symbols)))
135
136 (defun company-dabbrev--search (regexp &optional limit other-buffer-modes
137 ignore-comments)
138 (let* ((start (current-time))
139 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
140 ignore-comments)))
141 (when other-buffer-modes
142 (cl-dolist (buffer (delq (current-buffer) (buffer-list)))
143 (unless (if (stringp company-dabbrev-ignore-buffers)
144 (string-match-p company-dabbrev-ignore-buffers
145 (buffer-name buffer))
146 (funcall company-dabbrev-ignore-buffers buffer))
147 (with-current-buffer buffer
148 (when (or (eq other-buffer-modes 'all)
149 (apply #'derived-mode-p other-buffer-modes))
150 (setq symbols
151 (company-dabbrev--search-buffer regexp nil symbols start
152 limit ignore-comments)))))
153 (and limit
154 (> (float-time (time-since start)) limit)
155 (cl-return))))
156 symbols))
157
158 (defun company-dabbrev--prefix ()
159 ;; Not in the middle of a word.
160 (unless (looking-at company-dabbrev-char-regexp)
161 ;; Emacs can't do greedy backward-search.
162 (company-grab-line (format "\\(?:^\\| \\)[^ ]*?\\(\\(?:%s\\)*\\)"
163 company-dabbrev-char-regexp)
164 1)))
165
166 (defun company-dabbrev--filter (prefix candidates)
167 (let ((completion-ignore-case company-dabbrev-ignore-case))
168 (all-completions prefix candidates)))
169
170 ;;;###autoload
171 (defun company-dabbrev (command &optional arg &rest ignored)
172 "dabbrev-like `company-mode' completion backend."
173 (interactive (list 'interactive))
174 (cl-case command
175 (interactive (company-begin-backend 'company-dabbrev))
176 (prefix (company-dabbrev--prefix))
177 (candidates
178 (let* ((case-fold-search company-dabbrev-ignore-case)
179 (words (company-dabbrev--search (company-dabbrev--make-regexp)
180 company-dabbrev-time-limit
181 (pcase company-dabbrev-other-buffers
182 (`t (list major-mode))
183 (`all `all))))
184 (downcase-p (if (eq company-dabbrev-downcase 'case-replace)
185 case-replace
186 company-dabbrev-downcase)))
187 (setq words (company-dabbrev--filter arg words))
188 (if downcase-p
189 (mapcar 'downcase words)
190 words)))
191 (ignore-case company-dabbrev-ignore-case)
192 (duplicates t)))
193
194 (provide 'company-dabbrev)
195 ;;; company-dabbrev.el ends here