]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
Extract regexp for buffers to ignore into a new var
[gnu-emacs-elpa] / company-dabbrev.el
1 ;;; company-dabbrev.el --- dabbrev-like company-mode completion back-end -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2009, 2011, 2014 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 back-end."
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 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
78 (declare (indent 3) (debug t))
79 `(let ((company-time-limit-while-counter 0))
80 (catch 'done
81 (while ,test
82 ,@body
83 (and ,limit
84 (eq (cl-incf company-time-limit-while-counter) 25)
85 (setq company-time-limit-while-counter 0)
86 (> (float-time (time-since ,start)) ,limit)
87 (throw 'done 'company-time-out))))))
88
89 (defsubst company-dabbrev--make-regexp (prefix)
90 (concat "\\<" (if (equal prefix "")
91 company-dabbrev-char-regexp
92 (regexp-quote prefix))
93 "\\(" company-dabbrev-char-regexp "\\)*\\>"))
94
95 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
96 ignore-comments)
97 (save-excursion
98 (let (match)
99 (goto-char (if pos (1- pos) (point-min)))
100 ;; search before pos
101 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
102 start limit
103 (setq match (match-string-no-properties 0))
104 (if (and ignore-comments (company-in-string-or-comment))
105 (goto-char (nth 8 (syntax-ppss)))
106 (when (>= (length match) company-dabbrev-minimum-length)
107 (push match symbols))))
108 (goto-char (or pos (point-min)))
109 ;; search after pos
110 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
111 start limit
112 (setq match (match-string-no-properties 0))
113 (if (and ignore-comments (company-in-string-or-comment))
114 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
115 (when (>= (length match) company-dabbrev-minimum-length)
116 (push match symbols))))
117 symbols)))
118
119 (defun company-dabbrev--search (regexp &optional limit other-buffer-modes
120 ignore-comments)
121 (let* ((start (current-time))
122 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
123 ignore-comments)))
124 (when other-buffer-modes
125 (cl-dolist (buffer (delq (current-buffer) (buffer-list)))
126 (with-current-buffer buffer
127 (when (if (eq other-buffer-modes 'all)
128 (not (string-match-p company-dabbrev-ignore-buffers
129 (buffer-name)))
130 (apply #'derived-mode-p other-buffer-modes))
131 (setq symbols
132 (company-dabbrev--search-buffer regexp nil symbols start
133 limit ignore-comments))))
134 (and limit
135 (> (float-time (time-since start)) limit)
136 (cl-return))))
137 symbols))
138
139 ;;;###autoload
140 (defun company-dabbrev (command &optional arg &rest ignored)
141 "dabbrev-like `company-mode' completion back-end."
142 (interactive (list 'interactive))
143 (cl-case command
144 (interactive (company-begin-backend 'company-dabbrev))
145 (prefix (company-grab-word))
146 (candidates
147 (let* ((case-fold-search company-dabbrev-ignore-case)
148 (words (company-dabbrev--search (company-dabbrev--make-regexp arg)
149 company-dabbrev-time-limit
150 (pcase company-dabbrev-other-buffers
151 (`t (list major-mode))
152 (`all `all))))
153 (downcase-p (if (eq company-dabbrev-downcase 'case-replace)
154 case-replace
155 company-dabbrev-downcase)))
156 (if downcase-p
157 (mapcar 'downcase words)
158 words)))
159 (ignore-case company-dabbrev-ignore-case)
160 (duplicates t)))
161
162 (provide 'company-dabbrev)
163 ;;; company-dabbrev.el ends here