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