EmacsWiki: Auto Complete (original) (raw)

Auto completion with popup menu.

Downloads and full documentation are found at the following page:

https://github.com/auto-complete/auto-complete

Features

Screenshot

AutoCompleteScreenshot

Extensions

Lisp:auto-complete-extension.el is some enhancement by AndyStewart.

Clang source for Auto Complete

Another Clang source for auto Complete

Another clang autocompletion source

Clang based package for C & C++ completion, on-the-fly syntax checking and more

Readline completion for shell-mode

Auto Complete on nXML-mode

Auto Complete for Perl

Auto Complete for Typescript

Auto Complete for VBA

Auto Complete for org-mode

ac-tex-ref.el – for LaTeX \ref and \cite names

Other extensions are found in AutoCompleteSources.

Development Version Notes

If you are using the current development version of AutoComplete (0.20 at the time of this edit) be aware that ac-source-semantic is now included in the package. Furthermore, if you are using the semantic source, make sure that you have required semantic-ia in your init file. If you do not then it falls back to a method of gathering the tags that appears to not check the semantic cache slowing the whole thing up rather tremendously (especially with omnicompletion).

[I’m pretty sure this is horribly outdated! (current version 1.4)] Speaking of omnicompletion, the way it functions was changed recently. Since it is not documented yet here is an example of how it works:

(add-hook 'c-mode-common-hook '(lambda ()

      (add-to-list 'ac-omni-completion-sources
                   (cons "\\." '(ac-source-semantic)))
      (add-to-list 'ac-omni-completion-sources
                   (cons "->" '(ac-source-semantic)))
      

      (setq ac-sources '(ac-source-semantic ac-source-yasnippet))

))

Not terribly difficult but it took me a bit to puzzle out what needed to be done so I thought I would share my results. AndyStewart has included the omnicompletion bits into his init file.

LesHarris

auto-complete bug tracker on GitHub

Integrating Rope and Yasnippet with Auto Complete.

Integrating for Haskell (experimentally, for the Haskell compiler GHC 6.10.1)

Use company backend for Auto-Complete

Questions and Feedback


I’d like ‘return’ to trigger the completion. I’ve been trying this: (ac-set-trigger-key “RET”)

However, this stops ‘return’ from creating newlines.

Non lisp-hacker here. How can I add my own completions, and make auto-complete-mode start automatically for any opened file? Thanks.

For the second part of the question here is what i currently use to bypass ac-modes list:

(define-globalized-minor-mode real-global-auto-complete-mode auto-complete-mode (lambda () (if (not (minibufferp (current-buffer))) (auto-complete-mode 1)) )) (real-global-auto-complete-mode t)


CompletionUI use tooltip as completion UI, AutoComplete and CompanyMode implement by Overlay.

Tooltip just can use in X, and overlay can use both X and console.

Tooltip is slower than overlay when finger is very fast. 😊

But overlay have align render bug with multi-byte language (such as CJK), and tooltip haven’t this problem.

About different between AutoComplete and CompanyMode, in fact they’re very similar, but CompanyMode have more bug. – AndyStewart

CompanyMode is more feature-rich, actually. For example, as long as the source supports that, it can show a documentation or a definition buffer without quitting the completion interface.

Try hitting ‘f1’ or ‘C-w’ when completing something in an EmacsLisp buffer.

And it has a separate notion of metadata, which it shows in the echo area, that can be used to quickly show the function’s argument list, for example.

AutoComplete can display a candidate’s documentation, but it uses third-party libraries for that. They can make it look cooler, but they impose certain limitations, like you can’t add highlighting to the documentation text. – DmitryGutov

Thanks for the explanation! CompletionUI includes many other user-interfaces besides the tooltip, implements an auto-completion-mode, and includes many other features besides. My aim was to make a swiss-army knife of completion user-interface code, so no one else would have to go through the same tedium! Would you be interested in adding your overlay display code to CompletionUI? --TobyCubitt


AutoComplete stops working as soon as I enable Flymake. I’m using the development version. --TiagoCamargo

After loading auto-complete,

(ac-flyspell-workaround)

--jpkotta


If I’m editing OrgMode files, I can start AutoComplete (using \M-/ and not auto start), but if I don’t want to make any of the completions, ESC-ESC-ESC doesn’t kill the menu. I don’t see this problem in other emacs files. Any debugging hints?


What about completion with candidates from other buffers? I’ve tried to add ac-source-words-in-all-buffer to ac-sources, but that didn’t give any effect 😟

In c++-mode, ac-source-filename can not work, because

(defun ac-sources-prefix-default () "Default implementation for `ac-sources-prefix-function'." (require 'thingatpt) (car-safe (bounds-of-thing-at-point 'symbol)))

and thing-at-point is ahe when point after /home/ahe. I think auto-complete should have multiple ac-prefix, and then complete those all. – ahei

Can anyone explain how the ac-omni-completion-sources replacement works? I am not understanding how to use this ac-prefix stuff. I want to start completion on ‘.’ and ‘->’ for C programming, so foo→ would pop up completions for the struct Foo. This was working in 0.20 and I’m just not seeing how to set this up again. LesHarris


fix for yasnippet >=0.7.0

(defun ac-yasnippet-candidate-1 (table) (let ((hashtab (yas/snippet-table-hash table)) (parent (if (fboundp 'yas/snippet-table-parent) (car (yas/snippet-table-parent table)))) candidates) (maphash (lambda (key value) (push key candidates)) hashtab) (setq candidates (all-completions ac-prefix (nreverse candidates))) (if parent (setq candidates (append candidates (ac-yasnippet-candidate-1 parent)))) candidates))


I’m getting the following error message:

Symbol’s function definition is void: window-full-width-p

I googled this message, and found the following (at [1]):

The manual states that GNU Emacs 22 or later is required, and the version I am using is GNU Emacs 22.1.1. Is my old version of emacs the source of the error, and if it is, should the manual be corrected to list a newer required version of emacs (at least 22.2)?

I replaced “(window-full-width-p window)” with “t”, then popup.el seems to work fine. – Anonymous Coward


The gray-shaded inline expansion gets in the way when deleting characters in the midst of a line, as it overlays following existing text. This is a bit inconvenient. Is it possible to suppress completion while actively deleting?


Is it possible to use auto-complete to translate words with a lookup at dict.cc leo.org?

Yes, of course. auto-complete is a frame work for completion. But you have to give it the sources to complete from and take care of the output from completion. (You could of course use Emacs default completion frame work too. Or Icicles etc.)


infinite loop if type "border-width:" in css-mode maybe you need to modify auto-complete-config.el file and byte-compile.

--- auto-complete-config.el.orig 2010-05-29 23:51:11.000000000 +0900 +++ auto-complete-config.el 2010-08-25 20:49:15.000000000 +0900 @@ -344,10 +344,11 @@ (defconst ac-css-value-classes '((absolute-size "xx-small" "x-small" "small" "medium" "large" "x-large" "xx-large") (border-style "none" "hidden" "dotted" "dashed" "solid" "double" "groove" "ridge" "inset" "outset") + (border-width "thin" "medium" "thick" "inherit") (color "aqua" "black" "blue" "fuchsia" "gray" "green" "lime" "maroon" "navy" "olive" "orange" "purple" "red" "silver" "teal" "white" "yellow" "rgb") (counter "counter") (family-name "Courier" "Helvetica" "Times") @@ -475,6 +476,6 @@ (add-hook 'css-mode-hook 'ac-css-mode-setup) (add-hook 'auto-complete-mode-hook 'ac-common-setup) (global-auto-complete-mode t)) (provide 'auto-complete-config)


You can monkey patch using this somewhere else if you don’t want to change autocomplete.el

(add-to-list 'ac-css-value-classes '(border-width "thin" "medium" "thick" "inherit"))


I changed the yasnippet source to support the lastest SVN version of Yasnippet. In addition, I added a documentation function that gives the name of the expansion and the actual template.

(defface ac-yasnippet-candidate-face '((t (:background "sandybrown" :foreground "black"))) "Face for yasnippet candidate." :group 'auto-complete)

(defface ac-yasnippet-selection-face '((t (:background "coral3" :foreground "white"))) "Face for the yasnippet selected candidate." :group 'auto-complete)

(defun ac-yasnippet-table-hash (table) (cond ((fboundp 'yas/snippet-table-hash) (yas/snippet-table-hash table)) ((fboundp 'yas/table-hash) (yas/table-hash table))))

(defun ac-yasnippet-table-parent (table) (cond ((fboundp 'yas/snippet-table-parent) (yas/snippet-table-parent table)) ((fboundp 'yas/table-parent) (car (yas/table-parent table))) ((fboundp 'yas/table-parents) (car (yas/table-parents table))) ) )

(defun ac-yasnippet-candidate-1 (table) (with-no-warnings (let ((hashtab (ac-yasnippet-table-hash table)) (parent (ac-yasnippet-table-parent table)) (ac-prefix (ac-yasnippet-prefix)) candidates) (maphash (lambda (key value) (push key candidates)) hashtab) (when ac-prefix (setq candidates (all-completions ac-prefix (nreverse candidates))) ) (when parent (setq candidates (append candidates (ac-yasnippet-candidate-1 parent))) ) candidates)))

(defun ac-yasnippet-candidates () (with-no-warnings (if (fboundp 'yas/get-snippet-tables) ;; >0.6.0 (apply 'append (mapcar 'ac-yasnippet-candidate-1 (yas/get-snippet-tables major-mode))) (let ((table (if (fboundp 'yas/snippet-table) ;; <0.6.0 (yas/snippet-table major-mode) ;; 0.6.0 (yas/current-snippet-table)))) (if table (ac-yasnippet-candidate-1 table))))))

(defun ac-yasnippet-document (complete) "* Completes Documentation for Yasnippet" (let (templates only-one (ret "")) (setq templates (mapcar #'cdr (mapcan #'(lambda (table) (yas/fetch table complete)) (yas/get-snippet-tables)))) (mapc #'(lambda (template) (setq ret (format "%s%s\n%s\n" ret (yas/template-name template) (yas/template-content template)))) templates) (when (string-match "\n\'" ret) (setq ret (replace-match "" nil nil ret))) ret))

(defun ac-yasnippet-prefix () "* Gets Yasnippet Prefix." (if (looking-back "[^ ]*") (match-string 0) nil ) )

(ac-define-source yasnippet '((depends yasnippet) (candidates . ac-yasnippet-candidates) (action . yas/expand) (candidate-face . ac-yasnippet-candidate-face) (selection-face . ac-yasnippet-selection-face) (document . ac-yasnippet-document) (requires . 3) (prefix . "\(\<[^ ]*\)") (symbol . "a")))

--MatthewFidler

The following made yasnippet 0.7.0 and autocomplete 1.4 (both retrieved using el-get) work nicely in Emacs 24.0.92.

(require 'yasnippet)

(defvar yas-candidates nil)

(defun init-yas-candidates () (let ((table (yas/get-snippet-tables major-mode))) (if table (let (candidates (list)) (mapcar (lambda (mode)
(maphash (lambda (key value)
(push key candidates))
(yas/table-hash mode))) table) (setq yas-candidates candidates)))))

(defvar ac-new-yas-source '( (init . init-yas-candidates) (candidates . yas-candidates) (action . yas/expand) (symbol . "a")))

(provide 'my-yas-funs)

--David Dreisigmeyer

In windows the pos-tip doesn’t work. To get it to work change

(eq window-system 'x)

to

(memq window-system '(x w32))

I emailed maintainer, but I haven’t heard anything.

--MatthewFidler

BUG: Do not display completion when there is only one completion and the word is already complete! That’s just plain nonsensical!

Could you please write more documentation on how to use/reuse popup menus from popup.el. It would be nice to have an easy to use popup menus (implemented as overlays) in different contexts, for example in CompletionUI. --SewerynKokot

In css-mode, as soon as I hit ‘;’ to complete a declaration, the auto-complete menu appears and auto-selects a completion. Naturally, I’ll hit RET to begin a new line, but instead the first-selected auto-completion is inserted! I have to use C-j to get new lines. How can I prevent this without disabling auto-complete in every other mode? --IanL

Hey, if you want to stop having an autocomplete prompt when you hit RET, edit your auto-complete-config.el file and add the semicolon to the prefix regex. You should have something like that:

(defun ac-css-prefix () (when (save-excursion (re-search-backward "\_<\(.+?\)\_>\s *:.*\=\ nil t)) (setq ac-css-property (match-string 1)) (or (ac-prefix-symbol) (point))))

Happy hacking! – Etenil

I got tired of removing a character from a function name in order to see the docstring for it popup. The code below will display the docstring for a function in a popup when the point is on the function name or over a space in the function call list. Scott Jaderholm

(require 'pos-tip) (defun jsj-ac-show-help () "show docs for symbol at point or at beginning of list if not on a symbol" (interactive) (let ((s (save-excursion (or (symbol-at-point) (progn (backward-up-list) (forward-char) (symbol-at-point)))))) (pos-tip-show (if (equal major-mode 'emacs-lisp-mode) (ac-symbol-documentation s) (ac-slime-documentation (symbol-name s))) 'popup-tip-face

              (point)
              nil
              -1)))

(define-key lisp-mode-shared-map (kbd "C-c C-h") 'jsj-ac-show-help)

Hi, guys! I wrote an auto-complete-source to use Scite’s API files, called AutoCompleteSciteAPI. (For those don’t familiar with Scite, this page has some description on its API completion. Scite itself lacks such detail info)

Blog article for it: [2] (Sorry that it was written in Chinese. I’ll wrote some English description some time later. Or you can read the comment in source 😊)

Code: [3]

For me, it works fine on GNU Emacs 23.2/24.0.50 and auto-complete 1.2/1.3. I’m a emacs newbie (<1 year). If someone would point out any error in my elisp code, I’d be very happy. --BaManzi

Hi. I use autocomplete + eclim. I wrote an autocomplete source which uses eclim to retrieve completion candidates and then builds a yasnippet to expand on candidate selection. Hope someone finds it useful.

(defvar ac-eclim-yas-mark 'nil)

(defun ac-eclim-yas-candidates () (let (lista) (with-no-warnings (when eclim-auto-save (save-buffer)) (loop for c in (eclim/java-complete) do (if (not (equal (nth 0 c) "f")) (push (nth 1 c) lista) (let ((texto (nth 2 c)) aux) (push (concat (replace-regexp-in-string "^\([^(](\)." "\1" texto) (replace-regexp-in-string "[^ <]+\(?:<[^>]+>\)? \([^ ,]+\)\(,?\)" "${\1}\2" (replace-regexp-in-string "^[^(]*(\([^)]\)).$" "\1" texto)) ")") lista))))) lista))

(defun yas/expand-eclim-snippet () (let ((snip (buffer-substring-no-properties ac-eclim-yas-mark (point)))) (delete-region ac-eclim-yas-mark (point)) (yas/expand-snippet snip)))

(defun eclim-yas-init () (setq ac-eclim-yas-mark (point)))

(ac-define-source eclim-yas '((candidates . ac-eclim-yas-candidates) (init . eclim-yas-init) (requires . 0) (prefix . c-dot) (action . yas/expand-eclim-snippet) (symbol . "f")))

--Lucas Pandolfo

Is it possible to add some additional information in the popup menus that doesn’t get inserted when the text is completed? I would like to have the additional type information semantic provides for C/C++ code in the popup menu、like

| l : long[] | | x : int |.

--AlessandroPiras

I couldn’t find the place to post a bug report, so I’ll leave it here. As of Emacs 24.3.50.1 and auto-complete 1.4 the variable ‘ac-auto-start’ is mysteriously set to ‘nil’ regardless of any configuration value. It is set right after calling ‘auto-complete-mode’ and cannot be reset afterwards. Most likely some timer resets it to nil. However, it is not ‘ac-clear-variables-every-minute’, that happens much later.

--wvxvw

…Since I installed auto-complete I just can’t exit emacs as it freezes with ‘Wrote ~/.emacs.d/ac-comphist.d’. Any ideas? I have this plugins installed: evil, evil-nerd-commenter, undo-tree, smooth-scrolling, color-theme

Try… (setq ac-use-comphist nil). This option makes emacs to exit slowly… – Kiwon

AutoComplete and PredictiveMode. I asked the author of Predictive mode. I was informed that although Predictive mode has a module called ‘auto-complete-mode’ it is not a reference to this AutoComplete. They are separate from one another.


CategoryCompletion

[FAQ]

How does it work ? What does it get assigned to any keys ? Which function should I assign to get auto-completion ?


If you have a black background use auto-complete, the cursor turns black. This obscures the text and can be fixed by adding (set-cursor-color “white”) to your .emacs file after loading the auto-complete package.