]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
Merge pull request #457 from cpitclaudel/wip-simplify-electric
[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 (defun company-dabbrev--make-regexp (prefix)
95 (concat (if (equal prefix "")
96 (concat "\\(?:" 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 (let ((tmp-end (point)))
113 (company-dabrev--time-limit-while (not (bobp))
114 start limit
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'.
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-dabrev--time-limit-while (re-search-forward regexp nil t)
130 start limit
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 (with-current-buffer buffer
144 (when (if (eq other-buffer-modes 'all)
145 (not (string-match-p company-dabbrev-ignore-buffers
146 (buffer-name)))
147 (apply #'derived-mode-p other-buffer-modes))
148 (setq symbols
149 (company-dabbrev--search-buffer regexp nil symbols start
150 limit ignore-comments))))
151 (and limit
152 (> (float-time (time-since start)) limit)
153 (cl-return))))
154 symbols))
155
156 (defun company-dabbrev--prefix ()
157 ;; Not in the middle of a word.
158 (unless (looking-at company-dabbrev-char-regexp)
159 ;; Emacs can't do greedy backward-search.
160 (company-grab-line (format "\\(?:^\\| \\)[^ ]*?\\(\\(?:%s\\)*\\)"
161 company-dabbrev-char-regexp)
162 1)))
163
164 ;;;###autoload
165 (defun company-dabbrev (command &optional arg &rest ignored)
166 "dabbrev-like `company-mode' completion backend."
167 (interactive (list 'interactive))
168 (cl-case command
169 (interactive (company-begin-backend 'company-dabbrev))
170 (prefix (company-dabbrev--prefix))
171 (candidates
172 (let* ((case-fold-search company-dabbrev-ignore-case)
173 (words (company-dabbrev--search (company-dabbrev--make-regexp arg)
174 company-dabbrev-time-limit
175 (pcase company-dabbrev-other-buffers
176 (`t (list major-mode))
177 (`all `all))))
178 (downcase-p (if (eq company-dabbrev-downcase 'case-replace)
179 case-replace
180 company-dabbrev-downcase)))
181 (if downcase-p
182 (mapcar 'downcase words)
183 words)))
184 (ignore-case company-dabbrev-ignore-case)
185 (duplicates t)))
186
187 (provide 'company-dabbrev)
188 ;;; company-dabbrev.el ends here