]> code.delx.au - gnu-emacs-elpa/blob - company-dabbrev.el
Close #45
[gnu-emacs-elpa] / company-dabbrev.el
1 ;;; company-dabbrev.el --- dabbrev-like company-mode completion back-end
2
3 ;; Copyright (C) 2009, 2011 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 (eval-when-compile (require 'cl))
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 .5
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 (defmacro company-dabrev--time-limit-while (test start limit &rest body)
54 (declare (indent 3) (debug t))
55 `(let ((company-time-limit-while-counter 0))
56 (catch 'done
57 (while ,test
58 ,@body
59 (and ,limit
60 (eq (incf company-time-limit-while-counter) 25)
61 (setq company-time-limit-while-counter 0)
62 (> (float-time (time-since ,start)) ,limit)
63 (throw 'done 'company-time-out))))))
64
65 (defsubst company-dabbrev--make-regexp (prefix)
66 (concat "\\<" (if (equal prefix "")
67 company-dabbrev-char-regexp
68 (regexp-quote prefix))
69 "\\(" company-dabbrev-char-regexp "\\)*\\>"))
70
71 (defun company-dabbrev--search-buffer (regexp pos symbols start limit
72 ignore-comments)
73 (save-excursion
74 (let (match)
75 (goto-char (if pos (1- pos) (point-min)))
76 ;; search before pos
77 (company-dabrev--time-limit-while (re-search-backward regexp nil t)
78 start limit
79 (setq match (match-string-no-properties 0))
80 (if (and ignore-comments (company-in-string-or-comment))
81 (re-search-backward "\\s<\\|\\s!\\|\\s\"\\|\\s|" nil t)
82 (push match symbols)))
83 (goto-char (or pos (point-min)))
84 ;; search after pos
85 (company-dabrev--time-limit-while (re-search-forward regexp nil t)
86 start limit
87 (setq match (match-string-no-properties 0))
88 (if (and ignore-comments (company-in-string-or-comment))
89 (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
90 (push match symbols)))
91 symbols)))
92
93 (defun company-dabbrev--search (regexp &optional limit other-buffers
94 ignore-comments)
95 (let* ((start (current-time))
96 (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
97 ignore-comments)))
98 (when other-buffers
99 (dolist (buffer (delq (current-buffer) (buffer-list)))
100 (and (or (eq other-buffers 'all)
101 (eq (buffer-local-value 'major-mode buffer) major-mode))
102 (with-current-buffer buffer
103 (setq symbols
104 (company-dabbrev--search-buffer regexp nil symbols start
105 limit ignore-comments))))
106 (and limit
107 (> (float-time (time-since start)) limit)
108 (return))))
109 symbols))
110
111 ;;;###autoload
112 (defun company-dabbrev (command &optional arg &rest ignored)
113 "dabbrev-like `company-mode' completion back-end."
114 (interactive (list 'interactive))
115 (case command
116 (interactive (company-begin-backend 'company-dabbrev))
117 (prefix (company-grab-word))
118 (candidates
119 (mapcar 'downcase
120 (company-dabbrev--search (company-dabbrev--make-regexp arg)
121 company-dabbrev-time-limit
122 company-dabbrev-other-buffers)))
123 (ignore-case 'keep-prefix)
124 (duplicates t)))
125
126 (provide 'company-dabbrev)
127 ;;; company-dabbrev.el ends here