Company / completion-at-point backend to complete symbols in same mode buffers (like
ac-source-words-in-same-mode-buffers
)
with following features:
- Optional flex matching
- Good performance powered by radix-tree
- History mechanism to share candidates across sessions
load and initialize this package, and,
add to company-backends
.
(require 'company-same-mode-buffers)
(company-same-mode-buffers-initialize)
(push 'company-same-mode-buffers company-backends)
add to completion-at-point-functions
.
(require complete-same-mode-buffers)
(company-same-mode-buffers-initialize)
(push 'complete-same-mode-buffers (default-value 'completion-at-point-functions))
You may optionally save current candidates to a file, to share across sessions.
;; this line must be put before "company-same-mode-buffers-initialize"
(setq company-same-mode-buffers-history-file "~/.emacs.d/.company-same-mode-buffers-history")
An alist of the form alist[timestamp => alist[major-mode => list[symbol]]]
is saved to the history
file.
((TIMESTAMP (MAJOR_MODE SYMBOL ...) ...) ...)
For each history record (of the form cons[timestamp, alist[major-mode => symbol]]
), symbols are inserted into temporary buffer with appropreate major-mode.
;; sample history
((<timestamp>
(emacs-lisp-mode "save-excursion" "defun" "defvar")
(js-mode "function" "return"))
(<timestamp>
(emacs-lisp-mode "defconst")
(css-mode "margin")))
;; a temporary buffer with emacs-lisp-mode
save-excursion defun defvar defconst
;; a temporary buffer with js-mode
function return
;; a temporary buffer with css-mode
margin
Note that each history records' timestamp is compared to company-same-mode-buffers-history-store-limit
, and if it's older than the threshold, the record is skipped.
When killing Emacs, company-same-mode-buffers
visits all buffers (that derives prog-mode
, including temporary buffers created by company-same-mode-buffers
) and collect all symbols. If a symbol appears in more than two buffers, then the symbols is saved to the history file with new timestamp.
This way, we can store symbols ONLY IF it's useful across buffers, and save memory. We don't need file-specific symbols (like internal variable names) to be saved.
Downside: If a buffer is killed before killing Emacs, symbols in that buffer are NOT saved.
You may disable some of these matching algorithms, if you don't need them.
(setq company-same-mode-buffers-matchers
'(company-same-mode-buffers-matcher-basic
company-same-mode-buffers-matcher-partial
company-same-mode-buffers-matcher-exact-first-letter-flex-rest
;; company-same-mode-buffers-matcher-flex
))
-
company-same-mode-buffers-matcher-basic
compan
->compan*
->company-backends
-
company-same-mode-buffers-matcher-partial
co-ba
->co*-ba*
->company-backends
-
company-same-mode-buffers-matcher-exact-first-letter-flex-rest
cbns
->c*n*b*s*
->company-backends
-
company-same-mode-buffers-matcher-flex
pbns
->*p*n*b*s*
->company-backends