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