+ (cl-labels ((maybe-collect-match
+ ()
+ (let ((match (match-string-no-properties 0)))
+ (when (and (>= (length match) company-dabbrev-minimum-length)
+ (not (and company-dabbrev-ignore-invisible
+ (invisible-p (match-beginning 0)))))
+ (push match symbols)))))
+ (goto-char (if pos (1- pos) (point-min)))
+ ;; Search before pos.
+ (let ((tmp-end (point)))
+ (company-dabrev--time-limit-while (not (bobp))
+ start limit
+ (ignore-errors
+ (forward-char -10000))
+ (forward-line 0)
+ (save-excursion
+ ;; Before, we used backward search, but it matches non-greedily, and
+ ;; that forced us to use the "beginning/end of word" anchors in
+ ;; `company-dabbrev--make-regexp'.
+ (while (re-search-forward regexp tmp-end t)
+ (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
+ (re-search-forward "\\s>\\|\\s!\\|\\s\"" tmp-end t)
+ (maybe-collect-match))))
+ (setq tmp-end (point))))
+ (goto-char (or pos (point-min)))
+ ;; Search after pos.
+ (company-dabrev--time-limit-while (re-search-forward regexp nil t)
+ start limit
+ (if (and ignore-comments (save-match-data (company-in-string-or-comment)))
+ (re-search-forward "\\s>\\|\\s!\\|\\s\"" nil t)
+ (maybe-collect-match)))
+ symbols)))
+
+(defun company-dabbrev--search (regexp &optional limit other-buffer-modes
+ ignore-comments)
+ (let* ((start (current-time))
+ (symbols (company-dabbrev--search-buffer regexp (point) nil start limit
+ ignore-comments)))
+ (when other-buffer-modes
+ (cl-dolist (buffer (delq (current-buffer) (buffer-list)))
+ (with-current-buffer buffer
+ (when (if (eq other-buffer-modes 'all)
+ (not (string-match-p company-dabbrev-ignore-buffers
+ (buffer-name)))
+ (apply #'derived-mode-p other-buffer-modes))
+ (setq symbols
+ (company-dabbrev--search-buffer regexp nil symbols start
+ limit ignore-comments))))
+ (and limit
+ (> (float-time (time-since start)) limit)
+ (cl-return))))
+ symbols))
+
+(defun company-dabbrev--prefix ()
+ ;; Not in the middle of a word.
+ (unless (looking-at company-dabbrev-char-regexp)
+ ;; Emacs can't do greedy backward-search.
+ (company-grab-line (format "\\(?:^\\| \\)[^ ]*?\\(\\(?:%s\\)*\\)"
+ company-dabbrev-char-regexp)
+ 1)))