]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-dabbrev.el
Fix popup when prefix is split by line continuation
[sojka/company-mode.git] / 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   "The value of `ignore-case' returned by `company-dabbrev'.")
55
56 (defcustom company-dabbrev-downcase 'case-replace
57   "Whether to downcase the returned candidates.
58
59 The value of nil means keep them as-is.
60 `case-replace' means use the value of `case-replace'.
61 Any other value means downcase.
62
63 If you set this value to nil, you may also want to set
64 `company-dabbrev-ignore-case' to any value other than `keep-prefix'.")
65
66 (defcustom company-dabbrev-minimum-length (1+ company-minimum-prefix-length)
67   "The minimum length for the string to be included."
68   :type 'integer)
69
70 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
71   (declare (indent 3) (debug t))
72   `(let ((company-time-limit-while-counter 0))
73      (catch 'done
74        (while ,test
75          ,@body
76          (and ,limit
77               (eq (cl-incf company-time-limit-while-counter) 25)
78               (setq company-time-limit-while-counter 0)
79               (> (float-time (time-since ,start)) ,limit)
80               (throw 'done 'company-time-out))))))
81
82 (defsubst company-dabbrev--make-regexp (prefix)
83   (concat "\\<" (if (equal prefix "")
84               company-dabbrev-char-regexp
85             (regexp-quote prefix))
86           "\\(" company-dabbrev-char-regexp "\\)*\\>"))
87
88 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
89                                        ignore-comments)
90   (save-excursion
91     (let (match)
92       (goto-char (if pos (1- pos) (point-min)))
93       ;; search before pos
94       (company-dabrev--time-limit-while (re-search-backward regexp nil t)
95           start limit
96         (setq match (match-string-no-properties 0))
97         (if (and ignore-comments (company-in-string-or-comment))
98             (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
99           (when (>= (length match) company-dabbrev-minimum-length)
100             (push match symbols))))
101       (goto-char (or pos (point-min)))
102       ;; search after pos
103       (company-dabrev--time-limit-while (re-search-forward regexp nil t)
104           start limit
105         (setq match (match-string-no-properties 0))
106         (if (and ignore-comments (company-in-string-or-comment))
107             (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
108           (when (>= (length match) company-dabbrev-minimum-length)
109             (push match symbols))))
110       symbols)))
111
112 (defun company-dabbrev--search (regexp &optional limit other-buffer-modes
113                                 ignore-comments)
114   (let* ((start (current-time))
115          (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
116                                                   ignore-comments)))
117     (when other-buffer-modes
118       (cl-dolist (buffer (delq (current-buffer) (buffer-list)))
119         (with-current-buffer buffer
120           (when (or (eq other-buffer-modes 'all)
121                     (apply #'derived-mode-p other-buffer-modes))
122             (setq symbols
123                   (company-dabbrev--search-buffer regexp nil symbols start
124                                                   limit ignore-comments))))
125         (and limit
126              (> (float-time (time-since start)) limit)
127              (cl-return))))
128     symbols))
129
130 ;;;###autoload
131 (defun company-dabbrev (command &optional arg &rest ignored)
132   "dabbrev-like `company-mode' completion back-end."
133   (interactive (list 'interactive))
134   (cl-case command
135     (interactive (company-begin-backend 'company-dabbrev))
136     (prefix (company-grab-word))
137     (candidates
138      (let ((words (company-dabbrev--search (company-dabbrev--make-regexp arg)
139                                            company-dabbrev-time-limit
140                                            (pcase company-dabbrev-other-buffers
141                                              (`t (list major-mode))
142                                              (`all `all))))
143            (downcase-p (if (eq company-dabbrev-downcase 'case-replace)
144                            case-replace
145                          company-dabbrev-downcase)))
146        (if downcase-p
147            (mapcar 'downcase words)
148          words)))
149     (ignore-case company-dabbrev-ignore-case)
150     (duplicates t)))
151
152 (provide 'company-dabbrev)
153 ;;; company-dabbrev.el ends here