1*develop.txt* For Vim version 8.2. Last change: 2019 Nov 22 2 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 21improving Vim. For sending patches a unified diff "diff -u" is preferred. 22You can create a pull request on github, but it's not required. 23Also see http://vim.wikia.com/wiki/How_to_make_and_submit_a_patch. 24 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 37Vi. When the user wants to, he can use Vim in compatible mode and hardly 38notice any difference with the original Vi. 39 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 68 obscure features, or at least add hints in documentation that they exist. 69- 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|. 127- Use comments in a useful way! Quoting the function name and argument names 128 is NOT useful. Do explain what they are for. 129- 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 147- 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). 151 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. ;-)" 154 To use Vim with gdb see: http://www.agide.org and http://clewn.sf.net. 155- Vim is not a fancy GUI editor that tries to look nice at the cost of 156 being less consistent over all platforms. But functional GUI features are 157 welcomed. 158 159============================================================================== 1602. Coding style *coding-style* 161 162These are the rules to use when making changes to the Vim source code. Please 163stick to these rules, to keep the sources readable and maintainable. 164 165This list is not complete. Look in the source code for more examples. 166 167 168MAKING CHANGES *style-changes* 169 170The basic steps to make changes to the code: 1711. Get the code from github. That makes it easier to keep your changed 172 version in sync with the main code base (it may be a while before your 173 changes will be included). You do need to spend some time learning git, 174 it's not the most user friendly tool. 1752. Adjust the documentation. Doing this first gives you an impression of how 176 your changes affect the user. 1773. Make the source code changes. 1784. Check ../doc/todo.txt if the change affects any listed item. 1795. Make a patch with "git diff". You can also create a pull request on 180 github, but it's the diff that matters. 1816. Make a note about what changed, preferably mentioning the problem and the 182 solution. Send an email to the |vim-dev| maillist with an explanation and 183 include the diff. Or create a pull request on github. 184 185 186C COMPILER *style-compiler* *ANSI-C* *C89* *C99* 187 188The minimal C compiler version supported is C89, also known as ANSI C. 189Later standards, such as C99, are not widely supported, or at least not 100% 190supported. Therefore we use only some of the C99 features and disallow some 191(at least for now). 192 193Please don't make changes everywhere to use the C99 features, it causes merge 194problems for existing patches. Only use them for new and changed code. 195 196Comments ~ 197 198Traditionally Vim uses /* comments */. We intend to keep it that way 199for file and function headers and larger blocks of code, E.g.: 200 /* 201 * The "foo" argument does something useful. 202 * Return OK or FAIL. 203 */ 204For new code or lines of code that change, it is preferred to use // comments. 205Especially when it comes after code: 206 int some_var; // single line comment useful here 207 208Enums ~ 209 210The last item in an enum may have a trailing comma. C89 didn't allow this. 211 212Types ~ 213 214"long long" is allowed and can be expected to be 64 bits. Use %lld in printf 215formats. Also "long long unsigned" with %llu. 216 217Not to be used ~ 218 219These C99 features are not to be used, because not enough compilers support 220them: 221- Declaration after Statements (MSVC 2012 does not support it). All 222 declarations need to be at the start of the block. 223- Variable length arrays (even in C11 this is an optional feature). 224- _Bool and _Complex types. 225- "inline" (it's hardly ever needed, let the optimizer do its work) 226- flexible array members: Not supported by HP-UX C compiler (John Marriott) 227 228 229USE OF COMMON FUNCTIONS *style-functions* 230 231Some functions that are common to use, have a special Vim version. Always 232consider using the Vim version, because they were introduced with a reason. 233 234NORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION 235free() vim_free() Checks for freeing NULL 236malloc() alloc() Checks for out of memory situation 237malloc() lalloc() Like alloc(), but has long argument 238strcpy() STRCPY() Includes cast to (char *), for char_u * args 239strchr() vim_strchr() Accepts special characters 240strrchr() vim_strrchr() Accepts special characters 241isspace() vim_isspace() Can handle characters > 128 242iswhite() vim_iswhite() Only TRUE for tab and space 243memcpy() mch_memmove() Handles overlapped copies 244bcopy() mch_memmove() Handles overlapped copies 245memset() vim_memset() Uniform for all systems 246 247 248NAMES *style-names* 249 250Function names can not be more than 31 characters long (because of VMS). 251 252Don't use "delete" or "this" as a variable name, C++ doesn't like it. 253 254Because of the requirement that Vim runs on as many systems as possible, we 255need to avoid using names that are already defined by the system. This is a 256list of names that are known to cause trouble. The name is given as a regexp 257pattern. 258 259is.*() POSIX, ctype.h 260to.*() POSIX, ctype.h 261 262d_.* POSIX, dirent.h 263l_.* POSIX, fcntl.h 264gr_.* POSIX, grp.h 265pw_.* POSIX, pwd.h 266sa_.* POSIX, signal.h 267mem.* POSIX, string.h 268str.* POSIX, string.h 269wcs.* POSIX, string.h 270st_.* POSIX, stat.h 271tms_.* POSIX, times.h 272tm_.* POSIX, time.h 273c_.* POSIX, termios.h 274MAX.* POSIX, limits.h 275__.* POSIX, system 276_[A-Z].* POSIX, system 277E[A-Z0-9]* POSIX, errno.h 278 279.*_t POSIX, for typedefs. Use .*_T instead. 280 281wait don't use as argument to a function, conflicts with types.h 282index shadows global declaration 283time shadows global declaration 284new C++ reserved keyword 285 286clear Mac curses.h 287echo Mac curses.h 288instr Mac curses.h 289meta Mac curses.h 290newwin Mac curses.h 291nl Mac curses.h 292overwrite Mac curses.h 293refresh Mac curses.h 294scroll Mac curses.h 295typeahead Mac curses.h 296 297basename() GNU string function 298dirname() GNU string function 299get_env_value() Linux system function 300 301 302VARIOUS *style-various* 303 304Typedef'ed names should end in "_T": > 305 typedef int some_T; 306Define'ed names should be uppercase: > 307 #define SOME_THING 308Features always start with "FEAT_": > 309 #define FEAT_FOO 310 311Don't use '\"', some compilers can't handle it. '"' works fine. 312 313Don't use: 314 #if HAVE_SOME 315Some compilers can't handle that and complain that "HAVE_SOME" is not defined. 316Use 317 #ifdef HAVE_SOME 318or 319 #if defined(HAVE_SOME) 320 321 322STYLE *style-examples* 323 324General rule: One statement per line. 325 326Wrong: if (cond) a = 1; 327 328OK: if (cond) 329 a = 1; 330 331Wrong: while (cond); 332 333OK: while (cond) 334 ; 335 336Wrong: do a = 1; while (cond); 337 338OK: do 339 a = 1; 340 while (cond); 341 342Wrong: if (cond) { 343 cmd; 344 cmd; 345 } else { 346 cmd; 347 cmd; 348 } 349 350OK: if (cond) 351 { 352 cmd; 353 cmd; 354 } 355 else 356 { 357 cmd; 358 cmd; 359 } 360 361When a block has one line the braces can be left out. When an if/else has 362braces on one block, it usually looks better when the other block also has 363braces: 364OK: if (cond) 365 cmd; 366 else 367 cmd; 368 369OK: if (cond) 370 { 371 cmd; 372 } 373 else 374 { 375 cmd; 376 cmd; 377 } 378 379Use ANSI (new style) function declarations with the return type on a separate 380indented line. 381 382Wrong: int function_name(int arg1, int arg2) 383 384OK: /* 385 * Explanation of what this function is used for. 386 * 387 * Return value explanation. 388 */ 389 int 390 function_name( 391 int arg1, // short comment about arg1 392 int arg2) // short comment about arg2 393 { 394 int local; // comment about local 395 396 local = arg1 * arg2; 397 398 399 400SPACES AND PUNCTUATION *style-spaces* 401 402No space between a function name and the bracket: 403 404Wrong: func (arg); 405OK: func(arg); 406 407Do use a space after if, while, switch, etc. 408 409Wrong: if(arg) for(;;) 410OK: if (arg) for (;;) 411 412Use a space after a comma and semicolon: 413 414Wrong: func(arg1,arg2); for (i = 0;i < 2;++i) 415OK: func(arg1, arg2); for (i = 0; i < 2; ++i) 416 417Use a space before and after '=', '+', '/', etc. 418 419Wrong: var=a*5; 420OK: var = a * 5; 421 422In general: Use empty lines to group lines of code together. Put a comment 423just above the group of lines. This makes it easier to quickly see what is 424being done. 425 426OK: /* Prepare for building the table. */ 427 get_first_item(); 428 table_idx = 0; 429 430 /* Build the table */ 431 while (has_item()) 432 table[table_idx++] = next_item(); 433 434 /* Finish up. */ 435 cleanup_items(); 436 generate_hash(table); 437 438============================================================================== 4393. Design decisions *design-decisions* 440 441Folding 442 443Several forms of folding should be possible for the same buffer. For example, 444have one window that shows the text with function bodies folded, another 445window that shows a function body. 446 447Folding is a way to display the text. It should not change the text itself. 448Therefore the folding has been implemented as a filter between the text stored 449in a buffer (buffer lines) and the text displayed in a window (logical lines). 450 451 452Naming the window 453 454The word "window" is commonly used for several things: A window on the screen, 455the xterm window, a window inside Vim to view a buffer. 456To avoid confusion, other items that are sometimes called window have been 457given another name. Here is an overview of the related items: 458 459screen The whole display. For the GUI it's something like 1024x768 460 pixels. The Vim shell can use the whole screen or part of it. 461shell The Vim application. This can cover the whole screen (e.g., 462 when running in a console) or part of it (xterm or GUI). 463window View on a buffer. There can be several windows in Vim, 464 together with the command line, menubar, toolbar, etc. they 465 fit in the shell. 466 467 468Spell checking *develop-spell* 469 470When spell checking was going to be added to Vim a survey was done over the 471available spell checking libraries and programs. Unfortunately, the result 472was that none of them provided sufficient capabilities to be used as the spell 473checking engine in Vim, for various reasons: 474 475- Missing support for multi-byte encodings. At least UTF-8 must be supported, 476 so that more than one language can be used in the same file. 477 Doing on-the-fly conversion is not always possible (would require iconv 478 support). 479- For the programs and libraries: Using them as-is would require installing 480 them separately from Vim. That's mostly not impossible, but a drawback. 481- Performance: A few tests showed that it's possible to check spelling on the 482 fly (while redrawing), just like syntax highlighting. But the mechanisms 483 used by other code are much slower. Myspell uses a hashtable, for example. 484 The affix compression that most spell checkers use makes it slower too. 485- For using an external program like aspell a communication mechanism would 486 have to be setup. That's complicated to do in a portable way (Unix-only 487 would be relatively simple, but that's not good enough). And performance 488 will become a problem (lots of process switching involved). 489- Missing support for words with non-word characters, such as "Etten-Leur" and 490 "et al.", would require marking the pieces of them OK, lowering the 491 reliability. 492- Missing support for regions or dialects. Makes it difficult to accept 493 all English words and highlight non-Canadian words differently. 494- Missing support for rare words. Many words are correct but hardly ever used 495 and could be a misspelled often-used word. 496- For making suggestions the speed is less important and requiring to install 497 another program or library would be acceptable. But the word lists probably 498 differ, the suggestions may be wrong words. 499 500 501Spelling suggestions *develop-spell-suggestions* 502 503For making suggestions there are two basic mechanisms: 5041. Try changing the bad word a little bit and check for a match with a good 505 word. Or go through the list of good words, change them a little bit and 506 check for a match with the bad word. The changes are deleting a character, 507 inserting a character, swapping two characters, etc. 5082. Perform soundfolding on both the bad word and the good words and then find 509 matches, possibly with a few changes like with the first mechanism. 510 511The first is good for finding typing mistakes. After experimenting with 512hashtables and looking at solutions from other spell checkers the conclusion 513was that a trie (a kind of tree structure) is ideal for this. Both for 514reducing memory use and being able to try sensible changes. For example, when 515inserting a character only characters that lead to good words need to be 516tried. Other mechanisms (with hashtables) need to try all possible letters at 517every position in the word. Also, a hashtable has the requirement that word 518boundaries are identified separately, while a trie does not require this. 519That makes the mechanism a lot simpler. 520 521Soundfolding is useful when someone knows how the words sounds but doesn't 522know how it is spelled. For example, the word "dictionary" might be written 523as "daktonerie". The number of changes that the first method would need to 524try is very big, it's hard to find the good word that way. After soundfolding 525the words become "tktnr" and "tkxnry", these differ by only two letters. 526 527To find words by their soundfolded equivalent (soundalike word) we need a list 528of all soundfolded words. A few experiments have been done to find out what 529the best method is. Alternatives: 5301. Do the sound folding on the fly when looking for suggestions. This means 531 walking through the trie of good words, soundfolding each word and 532 checking how different it is from the bad word. This is very efficient for 533 memory use, but takes a long time. On a fast PC it takes a couple of 534 seconds for English, which can be acceptable for interactive use. But for 535 some languages it takes more than ten seconds (e.g., German, Catalan), 536 which is unacceptably slow. For batch processing (automatic corrections) 537 it's too slow for all languages. 5382. Use a trie for the soundfolded words, so that searching can be done just 539 like how it works without soundfolding. This requires remembering a list 540 of good words for each soundfolded word. This makes finding matches very 541 fast but requires quite a lot of memory, in the order of 1 to 10 Mbyte. 542 For some languages more than the original word list. 5433. Like the second alternative, but reduce the amount of memory by using affix 544 compression and store only the soundfolded basic word. This is what Aspell 545 does. Disadvantage is that affixes need to be stripped from the bad word 546 before soundfolding it, which means that mistakes at the start and/or end 547 of the word will cause the mechanism to fail. Also, this becomes slow when 548 the bad word is quite different from the good word. 549 550The choice made is to use the second mechanism and use a separate file. This 551way a user with sufficient memory can get very good suggestions while a user 552who is short of memory or just wants the spell checking and no suggestions 553doesn't use so much memory. 554 555 556Word frequency 557 558For sorting suggestions it helps to know which words are common. In theory we 559could store a word frequency with the word in the dictionary. However, this 560requires storing a count per word. That degrades word tree compression a lot. 561And maintaining the word frequency for all languages will be a heavy task. 562Also, it would be nice to prefer words that are already in the text. This way 563the words that appear in the specific text are preferred for suggestions. 564 565What has been implemented is to count words that have been seen during 566displaying. A hashtable is used to quickly find the word count. The count is 567initialized from words listed in COMMON items in the affix file, so that it 568also works when starting a new file. 569 570This isn't ideal, because the longer Vim is running the higher the counts 571become. But in practice it is a noticeable improvement over not using the word 572count. 573 574============================================================================== 5754. Assumptions *design-assumptions* 576 577Size of variables: 578char 8 bit signed 579char_u 8 bit unsigned 580int 32 or 64 bit signed (16 might be possible with limited features) 581unsigned 32 or 64 bit unsigned (16 as with ints) 582long 32 or 64 bit signed, can hold a pointer 583 584Note that some compilers cannot handle long lines or strings. The C89 585standard specifies a limit of 509 characters. 586 587 vim:tw=78:ts=8:noet:ft=help:norl: 588