]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
company-search-candidates: Mention company-search-regexp-function
[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 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 :type 'regexp)
46
47 (defcustom company-dabbrev-time-limit .1
48 "Determines how many seconds `company-dabbrev' should look for matches."
49 :type '(choice (const :tag "Off" nil)
50 (number :tag "Seconds")))
51
52 (defcustom company-dabbrev-char-regexp "\\sw"
53 "A regular expression matching the characters `company-dabbrev' looks for."
54 :type 'regexp)
55
56 (defcustom company-dabbrev-ignore-case 'keep-prefix
57 "Non-nil to ignore case when collecting completion candidates.
58 When it's `keep-prefix', the text before point will remain unchanged after
59 candidate is inserted, even some of its characters have different case.")
60
61 (defcustom company-dabbrev-downcase 'case-replace
62 "Whether to downcase the returned candidates.
63
64 The value of nil means keep them as-is.
65 `case-replace' means use the value of `case-replace'.
66 Any other value means downcase.
67
68 If you set this value to nil, you may also want to set
69 `company-dabbrev-ignore-case' to any value other than `keep-prefix'.")
70
71 (defcustom company-dabbrev-minimum-length 4
72 "The minimum length for the completion candidate to be included.
73 This variable affects both `company-dabbrev' and `company-dabbrev-code'."
74 :type 'integer
75 :package-version '(company . "0.8.3"))
76
77 (defcustom company-dabbrev-ignore-invisible nil
78 "Non-nil to skip invisible text."
79 :type 'boolean
80 :package-version '(company . "0.9.0"))
81
82 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
83 (declare (indent 3) (debug t))
84 `(let ((company-time-limit-while-counter 0))
85 (catch 'done
86 (while ,test
87 ,@body
88 (and ,limit
89 (eq (cl-incf company-time-limit-while-counter) 25)
90 (setq company-time-limit-while-counter 0)
91 (> (float-time (time-since ,start)) ,limit)
92 (throw 'done 'company-time-out))))))
93
94 (defsubst company-dabbrev--make-regexp (prefix)
95 (concat "\\<" (if (equal prefix "")
96 company-dabbrev-char-regexp
97 (regexp-quote prefix))
98 "\\(" 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 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
113 start limit
114 (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
115 (goto-char (nth 8 (syntax-ppss)))
116 (maybe-collect-match)))
117 (goto-char (or pos (point-min)))
118 ;; search after pos
119 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
120 start limit
121 (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
122 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
123 (maybe-collect-match)))
124 symbols)))
125
126 (defun company-dabbrev--search (regexp &optional limit other-buffer-modes
127 ignore-comments)
128 (let* ((start (current-time))
129 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
130 ignore-comments)))
131 (when other-buffer-modes
132 (cl-dolist (buffer (delq (current-buffer) (buffer-list)))
133 (with-current-buffer buffer
134 (when (if (eq other-buffer-modes 'all)
135 (not (string-match-p company-dabbrev-ignore-buffers
136 (buffer-name)))
137 (apply #'derived-mode-p other-buffer-modes))
138 (setq symbols
139 (company-dabbrev--search-buffer regexp nil symbols start
140 limit ignore-comments))))
141 (and limit
142 (> (float-time (time-since start)) limit)
143 (cl-return))))
144 symbols))
145
146 ;;;###autoload
147 (defun company-dabbrev (command &optional arg &rest ignored)
148 "dabbrev-like `company-mode' completion backend."
149 (interactive (list 'interactive))
150 (cl-case command
151 (interactive (company-begin-backend 'company-dabbrev))
152 (prefix (company-grab-word))
153 (candidates
154 (let* ((case-fold-search company-dabbrev-ignore-case)
155 (words (company-dabbrev--search (company-dabbrev--make-regexp arg)
156 company-dabbrev-time-limit
157 (pcase company-dabbrev-other-buffers
158 (`t (list major-mode))
159 (`all `all))))
160 (downcase-p (if (eq company-dabbrev-downcase 'case-replace)
161 case-replace
162 company-dabbrev-downcase)))
163 (if downcase-p
164 (mapcar 'downcase words)
165 words)))
166 (ignore-case company-dabbrev-ignore-case)
167 (duplicates t)))
168
169 (provide 'company-dabbrev)
170 ;;; company-dabbrev.el ends here