Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/auto-complete-1.3/doc/manual.txt
990 views
1
Title: Auto Complete Mode - User Manual
2
Numbering: yes
3
CSS: style.css
4
5
Auto Complete Mode User Manual
6
==============================
7
8
[Index](index.txt)
9
10
\[[Japanese](manual.ja.txt)]
11
12
* * * *
13
14
{toc}
15
16
* * * *
17
18
Introduction
19
------------
20
21
Auto Complete Mode (aka `auto-complete.el`, `auto-complete-mode`) is a extension that automates and advances completion-system of [GNU Emacs][]. This is superior than old system:
22
23
* Visual interface
24
* Reduce overhead of completion by using statistic method
25
* Extensibility
26
27
This user manual covers from how to install and how to use to how to extend. Please contact me if you have question.
28
29
Auto Complete Mode is licensed under the term of [GPLv3][]. And this document is licensed under the term of [GFDL][].
30
31
Downloads
32
---------
33
34
You can download from [Auto Complete Mode top page](index.txt).
35
36
Installation
37
------------
38
39
### Requirements ###
40
41
* 800MHz or higher CPU
42
* 256MB or higher RAM
43
* GNU Emacs 22 or later
44
45
### Installation Script ###
46
47
It is easy to install by using a installation script called `etc/install.el` that is located in the package directory.
48
49
Type `M-x load-file RET` in the running Emacs or newly launched Emacs. Note that if you want to upgrade `auto-complete-mode`, you have to install in **a newly launched Emacs with `-q` option**. Then input a file name to load which is a path string with adding `/etc/install.el` to the package directory. For example, if the package directory is `~/tmp/auto-complete-1.2`, the file name will be `~/tmp/auto-complete-1.2/etc/install.el`.
50
51
Then input a directory where Auto Complete will be installed. You need to add a directory to `load-path` later if `load-path` doesn't include the directory. The directory is to be `~/.emacs.d` by default.
52
53
Finally type `RET` to start installation. After installation, you may see the following buffer and follow instructions to edit `.emacs`.
54
55
You can also install from terminal like:
56
57
$ make install
58
$ # or with directory specified
59
$ make install DIR=$HOME/.emacs.d/
60
61
If you don't have GNU Make, run emacs like:
62
63
$ emacs -batch -l etc/install.el
64
65
**Example message after installation (\*Installation Result* Buffer)**
66
67
Successfully installed!
68
69
Add the following code to your .emacs:
70
71
(add-to-list 'load-path "~/.emacs.d") ; This may not be appeared if you have already added.
72
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
73
(require 'auto-complete-config)
74
(ac-config-default)
75
76
### Manual Installation ###
77
78
It is also possible to install manually if you follow a directory configuration. First, do byte-compile all `.el` files in the package directory. You may use `Makefile` in UNIX OS.
79
80
$ make byte-compile
81
82
If you can't use `Makefile`, open the directory from Emacs by `C-x d` and type `* . el RET B RET` to do byte-compile.
83
84
Then copy all `.el` files and `.elc` files to a directory which is added to `load-path`. You may do such the following command if the directory is `~/.emacs.d`
85
86
$ cp *.el *.elc ~/.emacs.d
87
88
And then install dictionary files. They are optional to run Auto Complete Mode, but you should install if you don't have any reason. Dictionary files are located in called `dict` directory, it is needed that they are installed to a directory which is `auto-complete.el` has been installed. If you installed `auto-complete.el` to a directory called `~/.emacs.d`, you also have to install dictionary files to `~/.emacs.d`. Please be careful not to overwrite existed files. It may be rare case, but the installation script above avoids overwrite by renaming `dict` directory to `ac-dict` directory.
89
90
$ cp -r dict ~/.emacs.d
91
92
Finally add the following code to `.emacs`.
93
94
(add-to-list 'ac-dictionary-directories "~/.emacs.d/dict")
95
(require 'auto-complete-config)
96
(ac-config-default)
97
98
If you haven't added the directory to `load-path`, you need to add the following code too.
99
100
(add-to-list 'load-path "~/.emacs.d")
101
102
### Check ###
103
104
Type some characters in \*scratch\* buffer in a restarted Emacs or newly launched Emacs. It is successful if you see completion menu. If you have error or no completion is started, it is failure maybe. Please contact me in such case with confirmation following:
105
106
* Using correct `load-path`?
107
108
A directory which `auto-complete.el` is installed to is in `load-path`.
109
110
* Characters `AC` in mode-line?
111
112
If you don't see characters `AC` in mode-line (a gray line of bottom of buffer), `auto-complete-mode` is not enabled. Type `M-x auto-complete-mode` to enable and try again.
113
114
* Error occurred
115
116
If you have \*Backtrace* with errors or errors in minibuffer (bottom of frame), please contact me with the errors.
117
118
Basic Usage
119
-----------
120
121
First, in a meaning, `auto-complete-mode` has no "usage". Because `auto-complete-mode` is designed to fade into Emacs editing system. Users will be received a highly-developed completion-system automatically without any difficulty. Ultimately, a goal of `auto-complete-mode` is to provide a system that does what users want without any command, but it is impossible to accomplish 100% accuracy actually. So there is "usage" to cover that points.
122
123
### Input Characters ###
124
125
Inputting characters is basic. Any completion will never be shown without any character. So when completion will be started, in other others, what character causes completion to be started? It is good question but it is difficult to answer here. In simple words, completion will be started when just character is inserted. See [`ac-trigger-commands`](#) for more details.
126
127
![Inputting Characters](ac.png)
128
129
### Completion by TAB ####
130
131
After completion is started, completion by TAB will be enabled temporarily. Completion by TAB is the most important and most frequent used command. TAB has several meanings.
132
133
* Case that only one candidate remains
134
135
If only on candidate remains, the candidate will be used to complete.
136
137
* Case that there is common part among candidates
138
139
For example, if all candidates start with "set", it means they have common part "set". So TAB completes "set" at first.
140
141
* Otherwise
142
143
Otherwise, select candidates in cycle by typing TAB.
144
145
It may be different a little according to settings, but basically completion by TAB works as we wrote above. A reason why TAB has several meanings is that we want users to do anything with TAB.
146
147
### Completion by RET ###
148
149
Like completion by TAB but some points are different:
150
151
* Complete a selected candidate immediately
152
* Execute an action if a selected candidate has the action
153
154
It is necessary to type TAB a few times for completion by TAB. Completion by RET instead complete a selected candidate immediately, so when you see a candidate you want, just type RET. If the candidate has an action, the action will be executed. Take a example of builtin abbrev completion. In completion by TAB, an abbrev which expands "www" to "World Wide Web" will be completed to "www", but in completion by RET, the abbrev will be expanded to "World Wide Web" as completion.
155
156
### Candidate Selection ###
157
158
Following `auto-complete-mode` philosophy, it is not recommended to select candidates. Because, it means it has been failed to guess completion, and also it requires for users to do candidate selection which is a high cost operation. We think there is so many cases that requires to do candidate selection, because completion by TAB will help candidate selection somehow and in recent version, a statistic method contributes to make a candidate suggestion more accurate. However, actually, this is such cases. So we also think it is not bad idea to remember how to select candidates.
159
160
Selecting candidates is not a complex operation. You can select candidates forward or backward by cursor key or `M-p` and `M-n`. According to setting, a behavior of completion by TAB will be changed as a behavior of completion by RET. See [`ac-dwim`](#) for more details.
161
162
There is other ways to select candidates. `M-1` to select candidate 1, `M-2` to select candidate 2, and so on.
163
164
### Help ###
165
166
`auto-complete-mode` has two type of help functionalities called *Quick Help* and *Buffer Help*. They are different in a point of displaying. Quick help will appear at the side of completion menu, so you can easily see that, but there is a problem if there is no space to displaying the help. Quick help will be shown automatically. To use quick help, you need to set [`ac-use-quick-help`](#) to `t`. Delay time show quick help is given by [`ac-quick-help-delay`](#).
167
168
On other side, buffer help will not be shown without any instructions from users. Buffer help literally display a help in a buffer of other window. It costs much to see than quick help, but it has more readability. To show buffer help, press `C-?` or `f1`. By pressing `C-M-v` or `C-M-S-v` after showing buffer help, you can scroll forward or backward for help buffer. Other commands will be fallbacked and buffer help will be closed.
169
170
### Summary ###
171
172
Completion will be started by inserting characters. After completion is started, operations in the following table will be enabled temporarily. After completion is finished, these operations will be disable.
173
174
| Key | Command | Description |
175
|-----------|-------------|---------------------------|
176
| TAB, C-i | ac-expand | Completion by TAB |
177
| RET, C-m | ac-complete | Completion by RET |
178
| down, M-n | ac-next | Select next candidate |
179
| up, M-p | ac-previous | Select previous candidate |
180
| C-?, f1 | ac-help | Show buffer help |
181
182
To stop completion, use `C-g` simply.
183
184
Advanced Usage
185
--------------
186
187
### `auto-complete` command ###
188
189
Basically there is assumption that `auto-complete-mode` will be started automatically, but there is also exception. For example, that is a case that an user wants to complete without inserting any character or a case not to start `auto-complete-mode` automatically by settings. A command called `auto-complete` is useful in such cases, which is used with key binding in general. The following code changes a default completion command to more advanced feature that `auto-complete-mode` provides.
190
191
(define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
192
193
So, as of `auto-complete` command, it is a little different from an original automatic completion.
194
195
* Case that only one candidate remains
196
197
Complete immediately without showing completion menu.
198
199
* Case that no candidates remains
200
201
Attempt to complete with fuzzy matching. See [Completion by Fuzzy Matching](#) for more details.
202
203
* Otherwise
204
205
Otherwise start completion with/without expanding a whole common part and showing completion menu. See also [`ac-show-menu-immediately-on-auto-complete`](#) and [`ac-expand-on-auto-complete`](#).
206
207
### Completion by Fuzzy Matching ###
208
209
In a case that there is no candidates by `auto-complete` command or a case that `ac-fuzzy-complete` command is executed, `auto-complete-mode` attempts to complete with fuzzy matching instead of usual exact matching. Parameters of fuzzy matching has already been optimized for use, so users don't need to change them. However if you want to know the internals, see `fuzzy.el`. Using completion by fuzzy matching, typo will be fixed as a series of completion. For instance, input "messaeg" in a buffer, and then do `M-x auto-complete` or `M-x ac-fuzzy-complete`. The cursor color will be changed to red if completion has been successful, and then you can continue to complete with regarding of "messaeg" as "message". It is not bad idea to bind `auto-complete` command to some key in a meaning of handling such cases.
210
211
![Fuzzy matching](ac-fuzzy.png)
212
213
### Filtering Completion Candidates ###
214
215
You can start filtering by `C-s`. The cursor color will be changed to blue. Then input characters to filter. It is possible to do completion by TAB or select candidates, which changes the cursor color to original so that telling filtering completion candidates has done. The filtering string will be restored when `C-s` again. To delete the filter string, press `DEL` or `C-h`. Other general operations is not allowed there.
216
217
![Filtering](ac-isearch.png)
218
219
### Trigger Key ###
220
221
It is difficult what key `auto-complete` command is bound to. It should be bound to a key which is easy to press as much as possible because completion operation is often happened. However, it is a major problem that there is no empty key to press easily. `auto-complete-mode` provides a feature called *Trigger Key* that handles such the problem. Using trigger key, you can use an arbitrary key temporarily if necessary. The following code uses `TAB` as trigger key.
222
223
(ac-set-trigger-key "TAB")
224
225
Trigger key will be enabled after inserting characters. Otherwise it is dealt as an usual command (TAB will be indent). Generally, trigger key is used with `auto-auto-start` being `nil`.
226
227
(setq ac-auto-start nil)
228
229
As of `ac-auto-start`, see [Not to complete automatically](#) or [`ac-auto-start`](#) for more details.
230
231
### Candidate Suggestion ###
232
233
`auto-complete-mode` analyzes completion operations one by one and reduces overheads of completion as much as possible. For example, having a candidate "foobar" been completed few times, `auto-complete-mode` arranges it to top of the candidates next time and make a situation that allows users to complete the word with one time TAB or few times TAB. It is called `comphist` internally, and you can use it by setting `ac-use-comphist` to `t`. It is enabled by default. Collection operations data will be stored in `user-emacs-directory` or `~/.emacs.d/` with a name `ac-comphist.dat`.
234
235
`auto-complete-mode` collects two types of data to accomplish accurate candidate suggestion.
236
237
* Count of completion
238
* Position of completion
239
240
Simply saying, it collects not only a completion count but also a position of completion. A completion candidate will be scored with the count and the point. If you complete `find-file` with a word f few times, in next time `find-file` will be arranged to top of candidates. However it is too simple. Actually `find-file` with `find-` will not have the same score, because a distance between `f` and `find-` will reduce a weight of scoring. It means that if you often complete `find-library` after `find-`, `find-library` will get high score than `find-file` at that position. So `auto-complete-mode` can guess `find-file` will be top after `f` and `find-library` will be top after `find-` as it seems to learn from users' operations.
241
242
### Completion by Dictionary ###
243
244
Dictionary is a simple list of string. There is three types of dictionary: user defined dictionary, major mode dictionary, and extension dictionary. You need to add `ac-source-dictionary` to `ac-sources` (default). See [source](#Source) for more details.
245
246
#### User Defined Dictionary ####
247
248
User defined dictionary is composed of a list of string specified `ac-user-dictionary` and dictionary files specified by `ac-user-dictionary-files`. Dictionary file is a word list separated with newline. User defined dictionary is shared with all buffers. Here is example adding your mail address to dictionary.
249
250
(add-to-list 'ac-user-dictionary "[email protected]")
251
252
Setting will be applied immediately. Try to input "foo" in a buffer. You may see `[email protected]` as a completion candidate. This setting will be cleared if Emacs will quit. You need to write the following code to keep setting in next Emacs launching.
253
254
(setq ac-user-dictionary '("[email protected]" "[email protected]"))
255
256
There is more easy way to add word to dictionary. Files specified by `ac-user-dictionary-files` will be treated as dictionary files. By default, `~/.dict` will be a dictionary file, so edit `~/.dict` like:
257
258
[email protected]
259
[email protected]
260
261
As we said, words are separated with newline. They are not applied immediately, because `auto-complete-mode` uses cache not to load every time from a dictionary file. It may be high cost. To clear cache, do `M-x ac-clear-dictionary-cache`. After that, dictionary files will be load absolutely.
262
263
No need to say perhaps, you can use other files as dictionary file by adding to `ac-user-dictionary-files`.
264
265
#### Major Mode Dictionary and Extension Dictionary ####
266
267
You can use other dictionaries for every major-modes and extensions. A dictionary will loaded from a directory specified with `ac-dictionary-directories`. `ac-dictionary-directories` may be the following setting if you followed [installation](#Installation) instructions.
268
269
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
270
271
A dictionary named `c++-mode` for specific major-mode and a dictionary named `txt` for specific extension will be stored in the directory. For instance, you complete in a buffer named `a.cpp` with dictionary completion, following the setting above, `~/.emacs.d/ac-dict/c++-mode` and `~/.emacs.d/ac-dict/cpp` will be loaded as dictionary file. You can edit the dictionary files and make a new one. In addition, you can add a new dictionary file to a directory that has same configuration.
272
273
As same as user defined dictionary, after editing and adding dictionary, you should do `M-x ac-clear-dictionary-cache` to apply changes.
274
275
Source
276
------
277
278
*Source* is a concept that insures a extensibility of `auto-complete-mode`. Simply saying, source is a description about:
279
280
* How to generate completion candidates
281
* How to complete
282
* How to show
283
284
Anybody who know about Emacs Lisp a little can define a source easily. See [extend](#Extend) for how to define a source. Here we can explain how to use builtin sources.
285
286
Usually a name of source starts with `ac-source-`. So you can list up sources with `apropos` (`M-x apropos RET ^ac-source-`). You may see `ac-source-filename` and `ac-source-dictionary` which are entities of sources.
287
288
### Using Source ###
289
290
If you wrote `(ac-config-default)` in your `.emacs`, it is rare to change a source setting because it is already optimized to use. Here is a short explanation about source however. Sources will be used by setting `ac-sources` to a list of sources. You can see the setting by evaluating `ac-sources` in \*scratch\* buffer:
291
292
;; Formatted
293
(ac-source-filename
294
ac-source-functions
295
ac-source-yasnippet
296
ac-source-variables
297
ac-source-symbols
298
ac-source-features
299
ac-source-abbrev
300
ac-source-words-in-same-mode-buffers
301
ac-source-dictionary)
302
303
As you see, `ac-sources` in \*scratch\* buffer has six sources. We explain each source for detail, you can guess meanings of sources. It is worth to remember that `ac-sources` is a buffer local variable, which means each `ac-sources` for buffers will be different.
304
305
Here is an example. Think you are at \*scratch\* buffer. As we said, this buffer has many sources. Some people think it is too many. So try to change `ac-sources` to reduce functionalities. It is easy to change. Just evaluate the following code in \*scratch\* buffer or with `M-:`:
306
307
(setq ac-sources '(ac-source-symbols ac-source-words-in-same-mode-buffers))
308
309
This example changes `ac-source` setting and enable only symbol completion and word completion among same major modes. Then, how can we enable this setting in next Emacs launching? We can change settings by adding a hook which is called when \*scratch\* buffer is created.
310
311
(defun my-ac-emacs-lisp-mode ()
312
(setq ac-sources '(ac-source-symbols ac-source-words-in-same-mode-buffers)))
313
314
(add-hook 'emacs-lisp-mode-hook 'my-ac-emacs-lisp-mode)
315
316
If a code `(ac-config-default)` is written in `.emacs`, the code above may not be worked correctly. It is because of `(ac-config-default)` will overwrite the setting. In such case, you can redefine a function which is used in `(ac-config-default)`. The function name is `ac-emacs-lisp-mode-setup` in `emacs-lisp-mode`. See `auto-complete-config.el` for more details.
317
318
(defun ac-emacs-lisp-mode-setup ()
319
(setq ac-sources '(ac-source-symbols ac-source-words-in-same-mode-buffers)))
320
321
So, now you know how to change sources in a specific major mode. Summary is:
322
323
1. Define a function changing `ac-sources`
324
2. Register the function to proper mode hooks (`c++-mode-hook`, `ruby-mode-hook`, and `python-mode-hook`, etc)
325
326
By the way, how can we change a setting for all buffers? We use `setq-default` to change `ac-sources` instead of `setq` in such case. Then the default value of `ac-sources` will be changed to the value you specified.
327
328
(setq-default ac-sources '(ac-source-words-in-all-buffer))
329
330
There is other ways to do that. `(ac-config-default)` changes the default value of `ac-sources` by registering a hook for `auto-complete-mode`. The registered function is `ac-common-setup` that adds `ac-source-filename` to the first of `ac-sources` by default. So all `auto-complete-mode` enabled buffer will have `ac-source-filename` at the first of `ac-sources`. A reason why adding to the first is relating to [omni completion](#Omni_Completion). Anyway you don't care about it here. So if you want to change `ac-sources` of all buffer, you can redefine `ac-common-setup` function to do that.
331
332
;; Add ac-source-dictionary to ac-sources of all buffer
333
(defun ac-common-setup ()
334
(setq ac-sources (append ac-sources '(ac-source-dictionary))))
335
336
### Builtin Sources ###
337
338
Here are defined sources in `auto-complete.el` and `auto-complete-config.el`.
339
340
#### `ac-source-abbrev` ####
341
342
A source for Emacs abbreviation function. See `info emacs Abbrevs` about abbreviation function.
343
344
#### `ac-source-css-property` ####
345
346
A source for CSS property.
347
348
#### `ac-source-dictionary` ####
349
350
A source for dictionary. See [completion by dictionary](#Completion_by_Dictionary) about dictionary.
351
352
#### `ac-source-eclim` ####
353
354
A source for [Emacs-eclim](http://github.com/senny/emacs-eclim).
355
356
#### `ac-source-features` ####
357
358
A source for completing features which are available with `(require '`.
359
360
#### `ac-source-filename` ####
361
362
A source for completing file name. Completion will be started after inserting `/`.
363
364
#### `ac-source-files-in-current-dir` ####
365
366
A source for completing files in a current directory. It may be useful with `eshell`.
367
368
#### `ac-source-functions` ####
369
370
A source for completing Emacs Lisp functions. It is available only after `(`.
371
372
#### `ac-source-gtags` ####
373
374
A source for completing tags of [Global](http://www.tamacom.com/global.html).
375
376
#### `ac-source-imenu` ####
377
378
A source for completing `imenu` nodes. See `info emacs imenu` for details.
379
380
#### `ac-source-semantic` ####
381
382
A source for [Semantic](http://cedet.sourceforge.net/semantic.shtml). It can be used for completing member name for C/C++.
383
384
#### `ac-source-semantic-raw` ####
385
386
Unlike `ac-source-semantic`, this source is for completing symbols in a raw namespace.
387
388
#### `ac-source-symbols` ####
389
390
A source for completing Emacs Lisp symbols.
391
392
#### `ac-source-variables` ####
393
394
A source for completing Emacs Lisp symbols.
395
396
#### `ac-source-words-in-all-buffer` ####
397
398
A source for completing words in all buffer. Unlikely [`ac-source-words-in-same-mode-buffers`](#), it doesn't regard major-mode.
399
400
#### `ac-source-words-in-buffer` ####
401
402
A source for completing words in a current buffer.
403
404
#### `ac-source-words-in-same-mode-buffers` ####
405
406
A source for completing words which are collected over buffers whom major-mode is same to of a current buffer. For example, words will shared among `a.cpp` and `b.cpp`, but not shared among `a.pl` and `b.cpp` because they are different major-mode buffers. Usually this source is more useful than [`ac-source-words-in-all-buffer`](#).
407
408
#### `ac-source-yasnippet` ####
409
410
A source for [Yasnippet](http://code.google.com/p/yasnippet/) to complete and expand snippets.
411
412
Tips
413
----
414
415
### Not to complete automatically ###
416
417
If you are being annoyed with displaying completion menu, you can disable automatic starting completion by setting `ac-auto-start` to `nil`.
418
419
(setq ac-auto-start nil)
420
421
You need to bind some key to `auto-complete` command (because you need to complete anyway). For example, bind to `ac-mode-map`, which is a key map for `auto-complete-mode` enabled buffer:
422
423
(define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
424
425
Or bind to global key map.
426
427
(global-set-key "\M-/" 'auto-complete)
428
429
In addition, if you allow to start completion automatically but also want to be silent as much as possible, you can do it by setting `ac-auto-start` to an prefix length integer. For example, if you want to start completion automatically when you has inserted 4 or more characters, just set `ac-auto-start` to 4:
430
431
(setq ac-auto-start 4)
432
433
Setting `ac-auto-start` to large number will result in good for performance. Lesser `ac-auto-start`, more high cost to produce completion candidates, because there will be so many candidates necessarily. If you feel `auto-complete-mode` is stalling, change `ac-auto-start` to a larger number or `nil`.
434
435
See [`ac-auto-start`](#) for more details.
436
437
And consider to use [trigger key](#Trigger_Key).
438
439
### Not to show completion menu automatically ###
440
441
There is another approach to solve the annoying problem is that not to show completion menu automatically. Not to show completion menu automatically, set [`ac-auto-show-menu`](#) to `nil`.
442
443
(setq ac-auto-show-menu nil)
444
445
When you select or filter candidates, completion menu will be shown.
446
447
In other way, you can delay showing completion menu by setting `ac-auto-show-menu` to seconds in real number.
448
449
;; Show 0.8 second later
450
(setq ac-auto-show-menu 0.8)
451
452
This interface has both good points of completely automatic completion and completely non-automatic completion. This may be default in the future.
453
454
### Stop completion ###
455
456
You can stop completion by pressing `C-g`. However you won't press `C-g` while defining a macro. In such case, it is a good idea to bind some key to `ac-completing-map`.
457
458
(define-key ac-completing-map "\M-/" 'ac-stop)
459
460
Now you can stop completion by pressing `M-/`.
461
462
### Finish completion by TAB ###
463
464
As we described above, there is many behaviors in TAB. You need to use TAB and RET properly, but there is a simple interface that bind RET to original and TAB to finish completion:
465
466
(define-key ac-completing-map "\t" 'ac-complete)
467
(define-key ac-completing-map "\r" nil)
468
469
### Select candidates with C-n/C-p only when completion menu is displayed ###
470
471
By evaluating the following code, you can select candidates with C-n/C-p, but it might be annoying sometimes.
472
473
;; Bad config
474
(define-key ac-completing-map "\C-n" 'ac-next)
475
(define-key ac-completing-map "\C-p" 'ac-previous)
476
477
In this case, it is better that selecting candidates is enabled only when completion menu is displayed so that the key input will not be taken as much as possible. `ac-menu-map` is a keymap for completion on completion menu which is enabled when `ac-use-menu-map` is `t`.
478
479
(setq ac-use-menu-map t)
480
;; Default settings
481
(define-key ac-menu-map "\C-n" 'ac-next)
482
(define-key ac-menu-map "\C-p" 'ac-previous)
483
484
See [`ac-use-menu-map`](#) and [`ac-menu-map`](#) for more details.
485
486
### Not to use quick help ###
487
488
A tooltip help that is shown when completing is called quick help. You can disable it if you don't want to use it:
489
490
(setq ac-use-quick-help nil)
491
492
### Change a height of completion menu ###
493
494
Set `ac-menu-height` to number of lines.
495
496
;; 20 lines
497
(setq ac-menu-height 20)
498
499
### Enable `auto-complete-mode` automatically for specific modes ###
500
501
`auto-complete-mode` won't be enabled automatically for modes that are not in `ac-modes`. So you need to set if necessary:
502
503
(add-to-list 'ac-modes 'brandnew-mode)
504
505
### Ignore case ###
506
507
There is three ways to distinguish upper case and lower case.
508
509
;; Just ignore case
510
(setq ac-ignore-case t)
511
;; Ignore case if completion target string doesn't include upper characters
512
(setq ac-ignore-case 'smart)
513
;; Distinguish case
514
(setq ac-ignore-case nil)
515
516
Default is `smart`.
517
518
### Stop completion automatically after inserting specific words ###
519
520
Set `ac-ignores` to words that stops completion automatically. In ruby, some people want to stop completion automatically after inserting "end":
521
522
(add-hook 'ruby-mode-hook
523
(lambda ()
524
(make-local-variable 'ac-ignores)
525
(add-to-list 'ac-ignores "end")))
526
527
Note that `ac-ignores` is not a buffer local variable, so you need to make it buffer local with `make-local-variable` if it is buffer specific setting.
528
529
### Change colors ###
530
531
Colors settings are following:
532
533
| Face | Description |
534
|----------------------|---------------------------------------|
535
| `ac-completion-face` | Foreground color of inline completion |
536
| `ac-candidate-face` | Color of completion menu |
537
| `ac-selection-face` | Selection color of completion menu |
538
539
To change face background color, use `set-face-background`. To change face foreground color, use `set-face-foreground`. To set underline, use `set-face-underline`.
540
541
;; Examples
542
(set-face-background 'ac-candidate-face "lightgray")
543
(set-face-underline 'ac-candidate-face "darkgray")
544
(set-face-background 'ac-selection-face "steelblue")
545
546
### Change default sources ###
547
548
Read [source](#Source) first if you don't familiar with sources. To change default of sources, use `setq-default`:
549
550
(setq-default ac-sources '(ac-source-words-in-all-buffer))
551
552
### Change sources for specific major modes ###
553
554
For example, you may want to use specific sources for C++ buffers. To do that, register a hook by `add-hook` and change `ac-sources` properly:
555
556
(add-hook 'c++-mode (lambda () (add-to-list 'ac-sources 'ac-source-semantic)))
557
558
### Completion with specific source ###
559
560
You can start completion with specific source. For example, if you want to complete file name, do `M-x ac-complete-filename` at point. Or if you want to complete C/C++ member name, do `M-x ac-complete-semantic` at point. Usually, you may bind them to some key like:
561
562
;; Complete member name by C-c . for C++ mode.
563
(add-hook 'c++-mode-hook
564
(lambda ()
565
(local-set-key (kbd "C-c .") 'ac-complete-semantic)))
566
;; Complete file name by C-c /
567
(global-set-key (kbd "C-c /") 'ac-complete-filename)
568
569
Generally, such commands will be automatically available when sources are defined. Assume that a source named `ac-source-foobar` is being defined for example, a command called `ac-complete-foobar` will be also defined automatically. See also [builtin sources](#Builtin_Sources) for available commands.
570
571
If you want to use multiple sources for a command, you need to define a command for it like:
572
573
(defun semantic-and-gtags-complete ()
574
(interactive)
575
(auto-complete '(ac-source-semantic ac-source-gtags)))
576
577
`auto-complete` function can take an alternative of `ac-sources`.
578
579
### Show help persistently ###
580
581
Use `ac-persist-help` instead of `ac-help`, which is bound to `M-<f1>` and `C-M-?`.
582
583
### Show a lastly completed candidate help ###
584
585
`ac-last-help` command shows a lastly completed candidate help in a `ac-help` (buffer help) form. If you give an argument by `C-u` or just call `ac-last-persist-help`, its help buffer will not disappear automatically.
586
587
`ac-last-quick-help` command show a lastly completed candidate help in a `ac-quick-help` (quick help) form. It is useful if you want to see a function documentation, for example.
588
589
You may bind keys to these command like:
590
591
(define-key ac-mode-map (kbd "C-c h") 'ac-last-quick-help)
592
(define-key ac-mode-map (kbd "C-c H") 'ac-last-help)
593
594
### Show help beautifully ###
595
596
If [pos-tip.el](http://www.emacswiki.org/emacs-en/PosTip) is installed, `auto-complete-mode` uses its native rendering engine for displaying quick help instead of legacy one.
597
598
Configuration
599
-------------
600
601
Any configuration item will be set in `.emacs` or with `M-x customize-group RET auto-complete RET`.
602
603
### `ac-delay` ###
604
605
Delay time to start completion in real number seconds. It is a trade off of responsibility and performance.
606
607
### `ac-auto-show-menu` ###
608
609
Show completion menu automatically if `t` specified. `t` means always automatically showing completion menu. `nil` means never showing completion menu. Real number means delay time in seconds.
610
611
### `ac-show-menu-immediately-on-auto-complete` ###
612
613
Whether or not to show completion menu immediately on `auto-complete` command. If inline completion has already been showed, this configuration will be ignored.
614
615
### `ac-expand-on-auto-complete` ###
616
617
Whether or not to expand a common part of whole candidates.
618
619
### `ac-disable-faces` ###
620
621
Specify a list of face symbols for disabling auto completion. Auto completion will not be started if a face text property at a point is included in the list.
622
623
### `ac-stop-flymake-on-completing` ###
624
625
Whether or not to stop Flymake on completion.
626
627
### `ac-use-fuzzy` ###
628
629
Whether or not to use [fuzzy matching](#Completion_by_Fuzzy_Matching).
630
631
### `ac-fuzzy-cursor-color` ###
632
633
Change cursor color to specified color when [fuzzy matching](#Completion_by_Fuzzy_Matching) is started. `nil` means never changed. Available colors can be seen with `M-x list-colors-display`.
634
635
### `ac-use-comphist` ###
636
637
Whether or not to use [candidate suggestion](#Candidate_Suggestion). `nil` means never using it and get performance better maybe.
638
639
### `ac-comphist-threshold` ###
640
641
Specify a percentage of limiting lower scored candidates. 100% for whole scores.
642
643
### `ac-comphist-file` ###
644
645
Specify a file stores data of [candidate suggestion](#Candidate_Suggestion).
646
647
### `ac-use-quick-help` ###
648
649
Whether or not to use quick help.
650
651
### `ac-quick-help-delay` ###
652
653
Delay time to show quick help in real number seconds.
654
655
### `ac-menu-height` ###
656
657
Specify an integer of lines of completion menu.
658
659
### `ac-quick-help-height` ###
660
661
Specify an integer of lines of quick help.
662
663
### `ac-candidate-limit` ###
664
665
Limit a number of candidates. Specifying an integer, the value will be a limit of candidates. `nil` means no limit.
666
667
### `ac-modes` ###
668
669
Specify major modes as a list of symbols that will be enabled automatically if `global-auto-complete-mode` is enabled.
670
671
### `ac-compatible-packages-regexp` ###
672
673
Specify a regexp that identifies starting completion or not for that package.
674
675
### `ac-trigger-commands` ###
676
677
Specify commands as a list of symbols that starts completion automatically. `self-insert-command` is one of default.
678
679
### `ac-trigger-commands-on-completing` ###
680
681
Same as [`ac-trigger-commands`](#) expect this will be used on completing.
682
683
### `ac-trigger-key` ###
684
685
Specify a [trigger key](#Trigger_Key).
686
687
### `ac-auto-start` ###
688
689
Specify how completion will be started. `t` means always starting completion automatically. `nil` means never started automatically. An integer means completion will not be started until the value is more than a length of the completion target string.
690
691
### `ac-ignores` ###
692
693
Specify a list of strings that stops completion.
694
695
### `ac-ignore-case` ###
696
697
Specify how distinguish case. `t` means always ignoring case. `nil` means never ignoring case. `smart` in symbol means ignoring case only when the completion target string doesn't include upper characters.
698
699
### `ac-dwim` ###
700
701
"Do What I Mean" function. `t` means:
702
703
* After selecting candidates, TAB will behave as RET
704
* TAB will behave as RET only on candidate remains
705
706
### `ac-use-menu-map` ###
707
708
Specify a special keymap (`ac-menu-map`) should be enabled when completion menu is displayed. `ac-menu-map` will be enabled when it is `t` and satisfy one of the following conditions:
709
710
* `ac-auto-start` and `ac-auto-show-menu` are not `nil`, and completion menu is displayed after starting completion
711
* Completion menu is displayed by `auto-complete` command
712
* Completion menu is displayed by `ac-isearch` command
713
714
### `ac-use-overriding-local-map` ###
715
716
Use only when operations is not affected. Internally it uses `overriding-local-map`, which is too powerful to use with keeping orthogonality. So don't use as much as possible.
717
718
### `ac-completion-face` ###
719
720
Face of inline completion.
721
722
### `ac-candidate-face` ###
723
724
Face of completion menu background.
725
726
### `ac-selection-face` ###
727
728
Face of completion menu selection.
729
730
### `global-auto-complete-mode` ###
731
732
Whether or not to use `auto-complete-mode` globally. It is `t` in general.
733
734
### `ac-user-dictionary` ###
735
736
Specify a dictionary as a list of string for [completion by dictionary](#Completion_by_Dictionary).
737
738
### `ac-user-dictionary-files` ###
739
740
Specify a dictionary files as a list of string for [completion by dictionary](#Completion_by_Dictionary).
741
742
### `ac-dictionary-directories` ###
743
744
Specify a dictionary directories as a list of string for [completion by dictionary](#Completion_by_Dictionary).
745
746
### `ac-sources` ###
747
748
Specify sources as a list of [source](#Source). This is a buffer local variable.
749
750
### `ac-completing-map` ###
751
752
Keymap for completion.
753
754
### `ac-menu-map` ###
755
756
Keymap for completion on completion menu. See also [`ac-use-menu-map`](#).
757
758
### `ac-mode-map` ###
759
760
Keymap for `auto-complete-mode` enabled buffers.
761
762
Extend
763
------
764
765
A meaning to extend `auto-complete-mode` is just defining a [source](#Source). This section describe how to define a source.
766
767
### Prototype ###
768
769
Source basically takes a form of the following:
770
771
(defvar ac-source-mysource1
772
'((prop . value)
773
...))
774
775
As you see, source is just an associate list. You can define a source by combining pairs of defined property and its value.
776
777
### Example ###
778
779
The most important property for source is [`candidates`](#) property. This property describes how to generate completion candidates by giving a function, an expression, or a variable. A result of evaluation should be a list of strings. Here is an example to generate candidates "Foo", "Bar", and "Baz":
780
781
(defvar ac-source-mysource1
782
'((candidates . (list "Foo" "Bar" "Baz"))))
783
784
Then add this source to `ac-sources` and use:
785
786
(setq ac-sources '(ac-source-mysource1))
787
788
It is successful if you have "Bar" and "Baz" by inserting "B". The example above has an expression `(list ...)` in `candidates` property. The expression specified there will not be byte-compiled, so you should not use an expression unless it is too simple, because it has a bad affection on performance. You should use a function instead maybe:
789
790
(defun mysource1-candidates ()
791
'("Foo" "Bar" "Baz"))
792
793
(defvar ac-source-mysource1
794
'((candidates . mysource1-candidates)))
795
796
The function specified in `candidates` property will be called without any arguments on every time candidates updated. There is another way: a variable.
797
798
### Initialization ###
799
800
You may want to initialize a source at first time to complete. Use `init` property in these cases. As same as `candidates` property, specify a function without any parameters or an expression. Here is an example:
801
802
(defvar mysource2-cache nil)
803
804
(defun mysource2-init ()
805
(setq mysource2-cache '("Huge" "Processing" "Is" "Done" "Here")))
806
807
(defvar ac-source-mysource2
808
'((init . mysource2-init)
809
(candidates . mysource2-cache)))
810
811
In this example, `mysource2-init` function does huge processing, and stores the result into `mysource2-cache` variable. Then specifying the variable in `candidates` property, this source prevents huge processing on every time update completions. There are possible usage:
812
813
* Do `require`
814
* Open buffers first of all
815
816
### Cache ###
817
818
Caching strategy is important for `auto-complete-mode`. There are two major ways: `init` property and `cache` property that is described in this section. Specifying `cache` property in source definition, a result of evaluation of `candidates` property will be cached and reused the result as the result of evaluation of `candidates` property next time.
819
820
Rewrite the example in previous section by using `cache` property.
821
822
(defun mysource2-candidates ()
823
'("Huge" "Processing" "Is" "Done" "Here"))
824
825
(defvar ac-source-mysource2
826
'((candidates . mysource2-candidates)
827
(cache)))
828
829
There is no performance problem because this source has `cache` property even if `candidates` property will do huge processing.
830
831
#### Cache Expiration ####
832
833
It is possible to keep among more wider scope than `init` property and `cache` property. It may be useful for remembering all function names which is rarely changed. In these cases, how can we clear cache property not at the expense of performance? This is true time use that functionality.
834
835
Use `ac-clear-variable-after-save` to clear cache every time a buffer saved. Here is an example:
836
837
(defvar mysource3-cache nil)
838
839
(ac-clear-variable-after-save 'mysource3-cache)
840
841
(defun mysource3-candidates ()
842
(or mysource3-cache
843
(setq mysource3-cache (list (format "Time %s" (current-time-string))))))
844
845
(defvar ac-source-mysource3
846
'((candidates . mysource3-candidates)))
847
848
Add this source to `ac-sources` and complete with "Time". You may see a time when completion has been started. After that, you also see the same time, because `mysource3-candidates` returns the cache as much as possible. Then, save the buffer once and complete with "Time" again. In this time, you may find a new time. An essence of this source is to use `ac-clear-variable-after-save` to manage a variable for cache.
849
850
It is also possible to clear cache periodically. Use `ac-clear-variable-every-minute` to do that. A way to use is same to `ac-clear-variable-after-save` except its cache will be cleared every minutes. A builtin source `ac-source-functions` uses this functionality.
851
852
### Action ###
853
854
[Complete by RET](#) will evaluate a function or an expression specified in `action` property. A builtin sources `ac-source-abbrev` and `ac-source-yasnippet` use this property.
855
856
### Omni Completion ###
857
858
*Omni Completion* is a type of completion which regards of a context of editing. A file name completion which completes with slashed detected and a member name completion in C/C++ with dots detected are omni completions. To make a source support for omni completion, use `prefix` property. A result of evaluation of `prefix` property must be a beginning point of completion target string. Retuning `nil` means the source is disabled within the context.
859
860
Consider a source that completes mail addresses only after "To: ". First of all, define a mail address completion source as same as above.
861
862
(defvar ac-source-to-mailaddr
863
'((candidates . (list "[email protected]"
864
"[email protected]"
865
"[email protected]"))))
866
867
(setq ac-sources '(ac-source-to-mailaddr))
868
869
Then enable completions only after "To: " by using `prefix` property. `prefix` property must be one of:
870
871
* Regexp
872
* Function
873
* Expression
874
875
Specifying a regexp, `auto-complete-mode` thinks of a point of start of group 1 or group 0 as a beginning point of completion target string by doing `re-search-backward`[^1] with the regexp. If you want to do more complicated, use a function or an expression instead. The beginning point that is evaluated here will be stored into [`ac-point`](#). In above example, regexp is enough.
876
877
^To: \(.*\)
878
879
A reason why capturing group 1 is skipping "To: ". By adding this into the source definition, the source looks like:
880
881
(defvar ac-source-to-mailaddr
882
'((candidates . (list "[email protected]"
883
"[email protected]"
884
"[email protected]"))
885
(prefix . "^To: \\(.*\\)")))
886
887
Add this source to `ac-sources` and then type "To: ". You will be able to complete mail addresses.
888
889
[^1]: Strictly `re-search-backward` with the added adding `\=` at the end
890
891
### `ac-define-source` ###
892
893
You may use an utility macro called `ac-define-source` which defines a source and a command.
894
895
(ac-define-source mysource3
896
'((candidates . (list "Foo" "Bar" "Baz"))))
897
898
This expression will be expanded like:
899
900
(defvar ac-source-mysource3
901
'((candidates . (list "Foo" "Bar" "Baz"))))
902
903
(defun ac-complete-mysource3 ()
904
(interactive)
905
(auto-complete '(ac-source-mysource3)))
906
907
A source will be defined as usual and in addition a command that completes with the source will be defined. Calling `auto-complete` without arguments will use `ac-sources` as default sources and with arguments will use the arguments as default sources. Considering compatibility, it is difficult to answer which you should use `defvar` and `ac-define-source`. Builtin sources are defined with `ac-define-sources`, so you can use them alone by binding some key to these commands such like `ac-complete-filename`. See also [this tips](#Completion_with_specific_source].
908
909
### Source Properties ###
910
911
#### `init` ####
912
913
Specify a function or an expression that is evaluated only once when completion is started.
914
915
#### `candidates` ####
916
917
Specify a function, an expression, or a variable to calculate candidates. Candidates should be a list of string. If [`cache`](#) property is enabled, this property will be ignored twice or later.
918
919
#### `prefix` ####
920
921
Specify a regexp, a function, or an expression to find a point of completion target string for [omni completion](#Omni_Completion). This source will be ignored when `nil` returned. If a regexp is specified, a start point of group 1 or group 2 will be used as a value.
922
923
#### `requires` ####
924
925
Specify a required number of characters of completion target string. If nothing is specified, `auto-complete-mode` uses [`ac-auto-start`](#) instead.
926
927
#### `action` ####
928
929
Specify a function or an expression that is executed on [completion by RET](#Completion_by_RET).
930
931
#### `limit` ####
932
933
Specify a limit of candidates. It overrides `ac-candidate-limit` partially.
934
935
#### `symbol` ####
936
937
Specify a symbol of candidate meaning in one character string. The symbol will be any character, but you should follow the rule:
938
939
| Symbol | Meaning |
940
|--------|------------------|
941
| s | Symbol |
942
| f | Function, Method |
943
| v | Variable |
944
| c | Constant |
945
| a | Abbreviation |
946
| d | Dictionary |
947
948
#### `summary` ####
949
950
Specify a summary of candidate in string. It should be used for summarizing the candidate in short string.
951
952
#### `cache` ####
953
954
Use [cache](#).
955
956
#### `require` ####
957
958
Specify an integer or `nil`. This source will be ignored when the integer value is lager than a length of completion target string. `nil` means nothing ignored.
959
960
#### `candidate-face` ####
961
962
Specify a face of candidate. It overrides [`ac-candidate-face`](#) partially.
963
964
#### `selection-face` ####
965
966
Specify a face of selection. It overrides [`ac-selection-face`](#) partially.
967
968
#### `depends` ####
969
970
Specify a list of features (which are `require`d) that the source is depending.
971
972
#### `available` ####
973
974
Specify a function or an expression that describe the source is available or not.
975
976
### Variables ###
977
978
Here is a list of often used variables.
979
980
#### `ac-buffer` ####
981
982
A buffer where completion started.
983
984
#### `ac-point` ####
985
986
A start point of completion target string.
987
988
#### `ac-prefix` ####
989
990
A string of completion target.
991
992
#### `ac-limit` ####
993
994
A limit of candidates. Its value may be one of [`ac-candidate-limit`](#) and [`limit`](#) property.
995
996
#### `ac-candidates` ####
997
998
A list of candidates.
999
1000
Trouble Shooting
1001
----------------
1002
1003
### Response Latency ###
1004
1005
To keep much responsibility is very important for `auto-complete-mode`. However it is well known fact that a performance is a trade off of functionalities. List up options related to the performance.
1006
1007
#### `ac-auto-start` #### {#trouble_ac-auto-start}
1008
1009
For a larger number, it reduces a cost of generating completion candidates. Or you can remove the cost by setting `nil` and you can use when you truly need. See [not to complete automatically](#Not_to_complete_automatically) for more details.
1010
1011
#### `ac-delay` #### {#trouble_ac-delay}
1012
1013
For a larger number, it reduces a cost of starting completion.
1014
1015
#### `ac-auto-show-menu` #### {#trouble_ac-auto-show-menu}
1016
1017
For a larger number, it reduces a displaying cost of completion menu.
1018
1019
#### `ac-use-comphist` #### {#trouble_ac-use-comphist}
1020
1021
Setting [`ac-use-comphist`](#) to `nil` to disable [candidate suggestion](#Candidate_Suggestion), it reduces a cost of suggestion.
1022
1023
#### `ac-candidate-limit` #### {#trouble_ac-candidate-limit}
1024
1025
For a property number, it reduces much computation of generating candidates.
1026
1027
### Completion menu is disrupted ###
1028
1029
There is two major cases.
1030
1031
#### Column Computation Case ####
1032
1033
`auto-complete-mode` tries to reduce a cost of computation of columns to show completion menu correctly by using a optimized function at the expense of accuracy. However, it probably causes a menu to be disrupted. Not to use the optimized function, evaluate the following code:
1034
1035
(setq popup-use-optimized-column-computation nil)
1036
1037
#### Font Case ####
1038
1039
There is a problem when render [IPA font](http://ossipedia.ipa.go.jp/ipafont/) with Xft in Ubuntu 9.10. Use [VL gothic](http://dicey.org/vlgothic/), which renders more suitably. Or disable Xft, then it can render correctly.
1040
1041
We don't good answers now, but you may shot the troubles by changing font size with `set-face-font`. For instance, completion menu may be disrupted when displaying the menu including Japanese in NTEmacs. In such case, it is worth to try to evaluate the following code to fix it:
1042
1043
(set-face-font 'ac-candidate-face "MS Gothic 11")
1044
(set-face-font 'ac-selection-face "MS Gothic 11")
1045
1046
Known Bugs
1047
----------
1048
1049
### Auto completion will not be started in a buffer `flyspell-mode` enabled ###
1050
1051
A way of delaying processes of `flyspell-mode` disables auto completion. You can avoid this problem by `M-x ac-flyspell-workaround`. You can write the following code into your `~/.emacs`.
1052
1053
(ac-flyspell-workaround)
1054
1055
Reporting Bugs
1056
--------------
1057
1058
Visit [Auto Complete Mode Bug Tracking System](http://cx4a.org/redmine/projects/auto-complete-mode) and create a new ticket.
1059
1060
[GNU Emacs]: http://www.gnu.org/software/emacs/
1061
[GPLv3]: http://gplv3.fsf.org/
1062
[GFDL]: http://www.gnu.org/copyleft/fdl.html
1063
1064