blob: d97d230e9f36d606d24eb58824612f1683e710d0 [file] [log] [blame]
Christian Brabandtde6a3132024-12-25 18:00:38 +01001*develop.txt* For Vim version 9.1. Last change: 2024 Dec 25
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Development of Vim. *development*
8
9This text is important for those who want to be involved in further developing
10Vim.
11
121. Design goals |design-goals|
132. Coding style |coding-style|
143. Design decisions |design-decisions|
154. Assumptions |design-assumptions|
16
17See the file README.txt in the "src" directory for an overview of the source
18code.
19
20Vim is open source software. Everybody is encouraged to contribute to help
Bram Moolenaar85eee132018-05-06 17:57:30 +020021improving Vim. For sending patches a unified diff "diff -u" is preferred.
22You can create a pull request on github, but it's not required.
Bram Moolenaar531da592013-05-06 05:58:55 +020023Also see http://vim.wikia.com/wiki/How_to_make_and_submit_a_patch.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25==============================================================================
261. Design goals *design-goals*
27
28Most important things come first (roughly).
29
30Note that quite a few items are contradicting. This is intentional. A
31balance must be found between them.
32
33
34VIM IS... VI COMPATIBLE *design-compatible*
35
36First of all, it should be possible to use Vim as a drop-in replacement for
Bram Moolenaare7b1ea02020-08-07 19:54:59 +020037Vi. When the user wants to, Vim can be used in compatible mode and hardly
38any differences with the original Vi will be noticed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40Exceptions:
41- We don't reproduce obvious Vi bugs in Vim.
42- There are different versions of Vi. I am using Version 3.7 (6/7/85) as a
43 reference. But support for other versions is also included when possible.
44 The Vi part of POSIX is not considered a definitive source.
45- Vim adds new commands, you cannot rely on some command to fail because it
46 didn't exist in Vi.
47- Vim will have a lot of features that Vi doesn't have. Going back from Vim
48 to Vi will be a problem, this cannot be avoided.
49- Some things are hardly ever used (open mode, sending an e-mail when
50 crashing, etc.). Those will only be included when someone has a good reason
51 why it should be included and it's not too much work.
52- For some items it is debatable whether Vi compatibility should be
53 maintained. There will be an option flag for these.
54
55
56VIM IS... IMPROVED *design-improved*
57
58The IMproved bits of Vim should make it a better Vi, without becoming a
59completely different editor. Extensions are done with a "Vi spirit".
60- Use the keyboard as much as feasible. The mouse requires a third hand,
61 which we don't have. Many terminals don't have a mouse.
62- When the mouse is used anyway, avoid the need to switch back to the
63 keyboard. Avoid mixing mouse and keyboard handling.
64- Add commands and options in a consistent way. Otherwise people will have a
65 hard time finding and remembering them. Keep in mind that more commands and
66 options will be added later.
67- A feature that people do not know about is a useless feature. Don't add
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +010068 obscure features, or at least add hints in documentation that they exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +000069- Minimize using CTRL and other modifiers, they are more difficult to type.
70- There are many first-time and inexperienced Vim users. Make it easy for
71 them to start using Vim and learn more over time.
72- There is no limit to the features that can be added. Selecting new features
73 is one based on (1) what users ask for, (2) how much effort it takes to
74 implement and (3) someone actually implementing it.
75
76
77VIM IS... MULTI PLATFORM *design-multi-platform*
78
79Vim tries to help as many users on as many platforms as possible.
80- Support many kinds of terminals. The minimal demands are cursor positioning
81 and clear-screen. Commands should only use key strokes that most keyboards
82 have. Support all the keys on the keyboard for mapping.
83- Support many platforms. A condition is that there is someone willing to do
84 Vim development on that platform, and it doesn't mean messing up the code.
85- Support many compilers and libraries. Not everybody is able or allowed to
86 install another compiler or GUI library.
87- People switch from one platform to another, and from GUI to terminal
88 version. Features should be present in all versions, or at least in as many
89 as possible with a reasonable effort. Try to avoid that users must switch
90 between platforms to accomplish their work efficiently.
91- That a feature is not possible on some platforms, or only possible on one
92 platform, does not mean it cannot be implemented. [This intentionally
93 contradicts the previous item, these two must be balanced.]
94
95
96VIM IS... WELL DOCUMENTED *design-documented*
97
98- A feature that isn't documented is a useless feature. A patch for a new
99 feature must include the documentation.
100- Documentation should be comprehensive and understandable. Using examples is
101 recommended.
102- Don't make the text unnecessarily long. Less documentation means that an
103 item is easier to find.
104
105
106VIM IS... HIGH SPEED AND SMALL IN SIZE *design-speed-size*
107
108Using Vim must not be a big attack on system resources. Keep it small and
109fast.
110- Computers are becoming faster and bigger each year. Vim can grow too, but
111 no faster than computers are growing. Keep Vim usable on older systems.
112- Many users start Vim from a shell very often. Startup time must be short.
113- Commands must work efficiently. The time they consume must be as small as
114 possible. Useful commands may take longer.
115- Don't forget that some people use Vim over a slow connection. Minimize the
116 communication overhead.
117- Items that add considerably to the size and are not used by many people
118 should be a feature that can be disabled.
119- Vim is a component among other components. Don't turn it into a massive
120 application, but have it work well together with other programs.
121
122
123VIM IS... MAINTAINABLE *design-maintain*
124
125- The source code should not become a mess. It should be reliable code.
126- Use the same layout in all files to make it easy to read |coding-style|.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000127- Use comments in a useful way! Quoting the function name and argument names
128 is NOT useful. Do explain what they are for.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129- Porting to another platform should be made easy, without having to change
130 too much platform-independent code.
131- Use the object-oriented spirit: Put data and code together. Minimize the
132 knowledge spread to other parts of the code.
133
134
135VIM IS... FLEXIBLE *design-flexible*
136
137Vim should make it easy for users to work in their preferred styles rather
138than coercing its users into particular patterns of work. This can be for
139items with a large impact (e.g., the 'compatible' option) or for details. The
140defaults are carefully chosen such that most users will enjoy using Vim as it
141is. Commands and options can be used to adjust Vim to the desire of the user
142and its environment.
143
144
145VIM IS... NOT *design-not*
146
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200147- Vim is not a shell or an Operating System. It does provide a terminal
148 window, in which you can run a shell or debugger. E.g. to be able to do
149 this over an ssh connection. But if you don't need a text editor with that
150 it is out of scope (use something like screen or tmux instead).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 A satirical way to say this: "Unlike Emacs, Vim does not attempt to include
152 everything but the kitchen sink, but some people say that you can clean one
153 with it. ;-)"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200154 To use Vim with gdb see |terminal-debugger|. Other (older) tools can be
Christian Brabandt1c5728e2024-05-11 11:12:40 +0200155 found at http://www.agide.org (link seems dead) and http://clewn.sf.net.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156- Vim is not a fancy GUI editor that tries to look nice at the cost of
157 being less consistent over all platforms. But functional GUI features are
158 welcomed.
159
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161==============================================================================
1622. Coding style *coding-style*
163
164These are the rules to use when making changes to the Vim source code. Please
165stick to these rules, to keep the sources readable and maintainable.
166
167This list is not complete. Look in the source code for more examples.
168
Luca Saccarolafeea1b42024-11-11 21:33:50 +0100169The code repository contains an editorconfig file, that can be used together
170with the distributed editorconfig plugin |editorconfig-install| to ensure the
171recommended style is followed.
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174MAKING CHANGES *style-changes*
175
176The basic steps to make changes to the code:
Bram Moolenaar13d5aee2016-01-21 23:36:05 +01001771. Get the code from github. That makes it easier to keep your changed
178 version in sync with the main code base (it may be a while before your
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100179 changes will be included).
Bram Moolenaar13d5aee2016-01-21 23:36:05 +01001802. Adjust the documentation. Doing this first gives you an impression of how
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 your changes affect the user.
Bram Moolenaar13d5aee2016-01-21 23:36:05 +01001823. Make the source code changes.
1834. Check ../doc/todo.txt if the change affects any listed item.
Luca Saccarola55adc5b2024-10-31 10:28:40 +01001845. Add a test to src/testdir to verify the new behaviour and ensure it won't
185 regress in the future.
1866. Make a patch with "git diff".
1877. Make a note about what changed, preferably mentioning the problem and the
Bram Moolenaar68563932017-01-10 13:31:15 +0100188 solution. Send an email to the |vim-dev| maillist with an explanation and
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100189 include the diff.
190
191For any non-trivial change, please always create a pull request on github,
192since this triggers the test suite.
Bram Moolenaar13d5aee2016-01-21 23:36:05 +0100193
Christian Brabandtde6a3132024-12-25 18:00:38 +0100194 *style-clang-format*
195sound.c and sign.c can be (semi-) automatically formated using the
196`clang-format` formatter according to the distributed .clang-format file.
197Other source files do not yet correspond to the .clang-format file. This may
198change in the future and they may be reformatted as well.
199
Bram Moolenaar13d5aee2016-01-21 23:36:05 +0100200
Bram Moolenaar15142e22018-04-30 22:19:58 +0200201C COMPILER *style-compiler* *ANSI-C* *C89* *C99*
Bram Moolenaar13d5aee2016-01-21 23:36:05 +0100202
203The minimal C compiler version supported is C89, also known as ANSI C.
Bram Moolenaar561f8a52018-04-17 22:02:45 +0200204Later standards, such as C99, are not widely supported, or at least not 100%
Bram Moolenaar4cbdcbd2022-09-20 21:23:12 +0100205supported. Therefore we use only some of the C99 features and explicitly
206disallow some (this will gradually be adjusted over time).
Bram Moolenaar13d5aee2016-01-21 23:36:05 +0100207
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100208Features not to be used ~
Bram Moolenaar561f8a52018-04-17 22:02:45 +0200209
210These C99 features are not to be used, because not enough compilers support
211them:
Bram Moolenaar561f8a52018-04-17 22:02:45 +0200212- Variable length arrays (even in C11 this is an optional feature).
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100213- C99 _Bool and _Complex types.
Bram Moolenaar561f8a52018-04-17 22:02:45 +0200214- "inline" (it's hardly ever needed, let the optimizer do its work)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200215- flexible array members: Not supported by HP-UX C compiler (John Marriott)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
217
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100218COMMENTS *style-comments*
219
220Try to avoid putting multiline comments inside a function body: if the
221function is so complex that you need to separately comment parts of it, you
222should probably rethink the structure of the function.
223
224For file headers and function descriptions use: >
225 /*
226 * Description
227 */
228<
229For everything else use: >
230 // comment
231<
232
233
234INDENTATION *style-indentation*
235
236We use 4 space to indent the code. If you are using Vim to edit the source,
237you don't need to do anything due to the |modeline|.
238
239For other editors an `.editorconfig` is provided at the root of the repo.
240
241
242DECLARATIONS *style-declarations*
243
244Declare, when possible, `for` loop variables in the guard:
245OK: >
246 for (int i = 0; i < len; ++i)
247<
248Wrong: >
249 int i;
250 for (i = 0; i < len; ++i)
251<
252Always declare a variable with a default value:
253OK: >
254 int n = 0;
255 int *ptr = NULL;
256<
257Wrong: >
258 int n;
259 int *ptr;
260<
261
262
263BRACES *style-braces*
264
265All curly braces must be returned onto a new line:
266OK: >
267 if (cond)
268 {
269 cmd;
270 cmd;
271 }
272 else
273 {
274 cmd;
275 cmd;
276 }
277<
278Wrong: >
279 if (cond) {
280 cmd;
281 cmd;
282 } else {
283 cmd;
284 cmd;
285 }
286<
287OK: >
288 while (cond)
289 {
290 cmd;
291 }
292<
293Wrong: >
294 while (cond) {
295 cmd;
296 }
297<
298When a block has one line, including comments, the braces can be left out.
299OK: >
300 if (cond)
301 cmd;
302 else
303 cmd;
304<
305Wrong: >
306 if (cond)
307 /*
308 * comment
309 */
310 cmd;
311 else
312 cmd;
313<
314When an `if`/`else` has braces on one block, the other should have it too.
315OK: >
316 if (cond)
317 {
318 cmd;
319 }
320 else
321 {
322 cmd;
323 cmd;
324 }
325<
326Wrong: >
327 if (cond)
328 cmd;
329 else
330 {
331 cmd;
332 cmd;
333 }
334
335 if (cond)
336 {
337 cmd;
338 cmd;
339 }
340 else
341 cmd;
342<
343OK: >
344 while (cond)
345 cmd;
346<
347Wrong:
348>
349 while (cond)
350 if (cond)
351 cmd;
352<
353
354
355TYPES *style-types*
356
357Use descriptive types. You can find a list of them in the src/structs.h file
358and probably in a typedef in the file you are working on.
359
360Note that all custom types are postfixed with "_T"
361
362OK: >
363 int is_valid_line_number(linenr_T lnum);
364<
365Wrong: >
366 int is_valid_line_number(unsigned long lnum);
367<
368
369
370SPACES AND PUNCTUATION *style-spaces*
371
372No space between a function name and the bracket:
373
374OK: func(arg);
375Wrong: func (arg);
376
377Do use a space after `if`, `while`, `switch`, etc.
378
379OK: if (arg) for (;;)
380Wrong: if(arg) for(;;)
381
382Use a space after a comma or semicolon:
383
384OK: func(arg1, arg2); for (i = 0; i < 2; ++i)
385Wrong: func(arg1,arg2); for (i = 0;i < 2;++i)
386
387Use a space before and after '=', '+', '/', etc.
388
389Wrong: var=a*5;
390OK: var = a * 5;
391
392Use empty lines to group similar actions together.
393
394OK: >
395 msg_puts_title(_("\n--- Signs ---"));
396 msg_putchar('\n');
397
398 if (rbuf == NULL)
399 buf = firstbuf;
400 else
401 buf = rbuf;
402
403 while (buf != NULL && !got_int)
404<
405Wrong: >
406 msg_puts_title(_("\n--- Signs ---"));
407 msg_putchar('\n');
408 if (rbuf == NULL)
409 buf = firstbuf;
410 else
411 buf = rbuf;
412 while (buf != NULL && !got_int)
413<
414
415
416FUNCTIONS *style-functions*
417
418Use function declarations with the return type on a separate indented line.
419
420OK: >
421 int
422 function_name(int arg1, int arg2)
423 {
424 }
425<
426Wrong: >
427 int function_name(int arg1, int arg2)
428 {
429 }
430<
431
432Give meaningful names to function parameters.
433
434
435USE OF COMMON FUNCTIONS *style-common-functions*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437Some functions that are common to use, have a special Vim version. Always
438consider using the Vim version, because they were introduced with a reason.
439
440NORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION
441free() vim_free() Checks for freeing NULL
442malloc() alloc() Checks for out of memory situation
443malloc() lalloc() Like alloc(), but has long argument
444strcpy() STRCPY() Includes cast to (char *), for char_u * args
445strchr() vim_strchr() Accepts special characters
446strrchr() vim_strrchr() Accepts special characters
447isspace() vim_isspace() Can handle characters > 128
Bram Moolenaar9e368db2007-05-12 13:25:01 +0000448iswhite() vim_iswhite() Only TRUE for tab and space
Bram Moolenaar36fc5352006-03-04 21:49:37 +0000449memcpy() mch_memmove() Handles overlapped copies
450bcopy() mch_memmove() Handles overlapped copies
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451memset() vim_memset() Uniform for all systems
452
453
454NAMES *style-names*
455
456Function names can not be more than 31 characters long (because of VMS).
457
Bram Moolenaar13d5aee2016-01-21 23:36:05 +0100458Don't use "delete" or "this" as a variable name, C++ doesn't like it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
460Because of the requirement that Vim runs on as many systems as possible, we
461need to avoid using names that are already defined by the system. This is a
462list of names that are known to cause trouble. The name is given as a regexp
463pattern.
464
465is.*() POSIX, ctype.h
466to.*() POSIX, ctype.h
467
468d_.* POSIX, dirent.h
469l_.* POSIX, fcntl.h
470gr_.* POSIX, grp.h
471pw_.* POSIX, pwd.h
472sa_.* POSIX, signal.h
473mem.* POSIX, string.h
474str.* POSIX, string.h
475wcs.* POSIX, string.h
476st_.* POSIX, stat.h
477tms_.* POSIX, times.h
478tm_.* POSIX, time.h
479c_.* POSIX, termios.h
480MAX.* POSIX, limits.h
481__.* POSIX, system
482_[A-Z].* POSIX, system
483E[A-Z0-9]* POSIX, errno.h
484
Bram Moolenaar9964e462007-05-05 17:54:07 +0000485.*_t POSIX, for typedefs. Use .*_T instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487wait don't use as argument to a function, conflicts with types.h
488index shadows global declaration
489time shadows global declaration
490new C++ reserved keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100492clear Mac curses.h
493echo Mac curses.h
494instr Mac curses.h
495meta Mac curses.h
496newwin Mac curses.h
497nl Mac curses.h
498overwrite Mac curses.h
499refresh Mac curses.h
500scroll Mac curses.h
501typeahead Mac curses.h
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503basename() GNU string function
504dirname() GNU string function
505get_env_value() Linux system function
506
507
508VARIOUS *style-various*
509
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100510Define'd names should be uppercase: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 #define SOME_THING
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100512<
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514Features always start with "FEAT_": >
515 #define FEAT_FOO
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100516<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518Don't use '\"', some compilers can't handle it. '"' works fine.
519
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100520Don't use: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 #if HAVE_SOME
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100522<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523Some compilers can't handle that and complain that "HAVE_SOME" is not defined.
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100524Use >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 #ifdef HAVE_SOME
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100526<
527or >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 #if defined(HAVE_SOME)
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100529<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
531STYLE *style-examples*
532
Luca Saccarola55adc5b2024-10-31 10:28:40 +0100533One statement per line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535Wrong: if (cond) a = 1;
536
537OK: if (cond)
538 a = 1;
539
540Wrong: while (cond);
541
542OK: while (cond)
543 ;
544
545Wrong: do a = 1; while (cond);
546
547OK: do
548 a = 1;
549 while (cond);
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552==============================================================================
5533. Design decisions *design-decisions*
554
555Folding
556
557Several forms of folding should be possible for the same buffer. For example,
558have one window that shows the text with function bodies folded, another
559window that shows a function body.
560
561Folding is a way to display the text. It should not change the text itself.
562Therefore the folding has been implemented as a filter between the text stored
563in a buffer (buffer lines) and the text displayed in a window (logical lines).
564
565
566Naming the window
567
568The word "window" is commonly used for several things: A window on the screen,
569the xterm window, a window inside Vim to view a buffer.
570To avoid confusion, other items that are sometimes called window have been
571given another name. Here is an overview of the related items:
572
573screen The whole display. For the GUI it's something like 1024x768
574 pixels. The Vim shell can use the whole screen or part of it.
575shell The Vim application. This can cover the whole screen (e.g.,
576 when running in a console) or part of it (xterm or GUI).
577window View on a buffer. There can be several windows in Vim,
578 together with the command line, menubar, toolbar, etc. they
579 fit in the shell.
580
581
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000582Spell checking *develop-spell*
583
584When spell checking was going to be added to Vim a survey was done over the
585available spell checking libraries and programs. Unfortunately, the result
586was that none of them provided sufficient capabilities to be used as the spell
587checking engine in Vim, for various reasons:
588
Bram Moolenaar207f0092020-08-30 17:20:20 +0200589- Missing support for multibyte encodings. At least UTF-8 must be supported,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000590 so that more than one language can be used in the same file.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591 Doing on-the-fly conversion is not always possible (would require iconv
592 support).
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000593- For the programs and libraries: Using them as-is would require installing
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000594 them separately from Vim. That's mostly not impossible, but a drawback.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000595- Performance: A few tests showed that it's possible to check spelling on the
596 fly (while redrawing), just like syntax highlighting. But the mechanisms
Bram Moolenaar4770d092006-01-12 23:22:24 +0000597 used by other code are much slower. Myspell uses a hashtable, for example.
598 The affix compression that most spell checkers use makes it slower too.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000599- For using an external program like aspell a communication mechanism would
600 have to be setup. That's complicated to do in a portable way (Unix-only
601 would be relatively simple, but that's not good enough). And performance
602 will become a problem (lots of process switching involved).
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000603- Missing support for words with non-word characters, such as "Etten-Leur" and
604 "et al.", would require marking the pieces of them OK, lowering the
605 reliability.
606- Missing support for regions or dialects. Makes it difficult to accept
607 all English words and highlight non-Canadian words differently.
608- Missing support for rare words. Many words are correct but hardly ever used
609 and could be a misspelled often-used word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610- For making suggestions the speed is less important and requiring to install
611 another program or library would be acceptable. But the word lists probably
612 differ, the suggestions may be wrong words.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614
615Spelling suggestions *develop-spell-suggestions*
616
617For making suggestions there are two basic mechanisms:
6181. Try changing the bad word a little bit and check for a match with a good
619 word. Or go through the list of good words, change them a little bit and
620 check for a match with the bad word. The changes are deleting a character,
621 inserting a character, swapping two characters, etc.
6222. Perform soundfolding on both the bad word and the good words and then find
623 matches, possibly with a few changes like with the first mechanism.
624
625The first is good for finding typing mistakes. After experimenting with
626hashtables and looking at solutions from other spell checkers the conclusion
627was that a trie (a kind of tree structure) is ideal for this. Both for
628reducing memory use and being able to try sensible changes. For example, when
629inserting a character only characters that lead to good words need to be
630tried. Other mechanisms (with hashtables) need to try all possible letters at
631every position in the word. Also, a hashtable has the requirement that word
632boundaries are identified separately, while a trie does not require this.
633That makes the mechanism a lot simpler.
634
635Soundfolding is useful when someone knows how the words sounds but doesn't
636know how it is spelled. For example, the word "dictionary" might be written
637as "daktonerie". The number of changes that the first method would need to
638try is very big, it's hard to find the good word that way. After soundfolding
639the words become "tktnr" and "tkxnry", these differ by only two letters.
640
641To find words by their soundfolded equivalent (soundalike word) we need a list
642of all soundfolded words. A few experiments have been done to find out what
643the best method is. Alternatives:
6441. Do the sound folding on the fly when looking for suggestions. This means
645 walking through the trie of good words, soundfolding each word and
646 checking how different it is from the bad word. This is very efficient for
647 memory use, but takes a long time. On a fast PC it takes a couple of
648 seconds for English, which can be acceptable for interactive use. But for
649 some languages it takes more than ten seconds (e.g., German, Catalan),
Bram Moolenaar088e8e32019-08-08 22:15:18 +0200650 which is unacceptably slow. For batch processing (automatic corrections)
Bram Moolenaar82038d72007-05-10 17:15:45 +0000651 it's too slow for all languages.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006522. Use a trie for the soundfolded words, so that searching can be done just
653 like how it works without soundfolding. This requires remembering a list
654 of good words for each soundfolded word. This makes finding matches very
655 fast but requires quite a lot of memory, in the order of 1 to 10 Mbyte.
656 For some languages more than the original word list.
6573. Like the second alternative, but reduce the amount of memory by using affix
658 compression and store only the soundfolded basic word. This is what Aspell
659 does. Disadvantage is that affixes need to be stripped from the bad word
660 before soundfolding it, which means that mistakes at the start and/or end
661 of the word will cause the mechanism to fail. Also, this becomes slow when
662 the bad word is quite different from the good word.
663
664The choice made is to use the second mechanism and use a separate file. This
665way a user with sufficient memory can get very good suggestions while a user
666who is short of memory or just wants the spell checking and no suggestions
667doesn't use so much memory.
668
669
670Word frequency
671
672For sorting suggestions it helps to know which words are common. In theory we
673could store a word frequency with the word in the dictionary. However, this
674requires storing a count per word. That degrades word tree compression a lot.
675And maintaining the word frequency for all languages will be a heavy task.
676Also, it would be nice to prefer words that are already in the text. This way
677the words that appear in the specific text are preferred for suggestions.
678
679What has been implemented is to count words that have been seen during
680displaying. A hashtable is used to quickly find the word count. The count is
681initialized from words listed in COMMON items in the affix file, so that it
682also works when starting a new file.
683
684This isn't ideal, because the longer Vim is running the higher the counts
Bram Moolenaar82038d72007-05-10 17:15:45 +0000685become. But in practice it is a noticeable improvement over not using the word
Bram Moolenaar4770d092006-01-12 23:22:24 +0000686count.
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688==============================================================================
6894. Assumptions *design-assumptions*
690
691Size of variables:
692char 8 bit signed
693char_u 8 bit unsigned
Bram Moolenaar4770d092006-01-12 23:22:24 +0000694int 32 or 64 bit signed (16 might be possible with limited features)
695unsigned 32 or 64 bit unsigned (16 as with ints)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696long 32 or 64 bit signed, can hold a pointer
697
698Note that some compilers cannot handle long lines or strings. The C89
699standard specifies a limit of 509 characters.
700
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200701 vim:tw=78:ts=8:noet:ft=help:norl: