1*develop.txt* For Vim version 7.0aa. Last change: 2006 Jan 12 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 context diff "diff -c" is preferred. 22Also see http://www.vim.org/tips/tip.php?tip_id=618. 23 24============================================================================== 251. Design goals *design-goals* 26 27Most important things come first (roughly). 28 29Note that quite a few items are contradicting. This is intentional. A 30balance must be found between them. 31 32 33VIM IS... VI COMPATIBLE *design-compatible* 34 35First of all, it should be possible to use Vim as a drop-in replacement for 36Vi. When the user wants to, he can use Vim in compatible mode and hardly 37notice any difference with the original Vi. 38 39Exceptions: 40- We don't reproduce obvious Vi bugs in Vim. 41- There are different versions of Vi. I am using Version 3.7 (6/7/85) as a 42 reference. But support for other versions is also included when possible. 43 The Vi part of POSIX is not considered a definitive source. 44- Vim adds new commands, you cannot rely on some command to fail because it 45 didn't exist in Vi. 46- Vim will have a lot of features that Vi doesn't have. Going back from Vim 47 to Vi will be a problem, this cannot be avoided. 48- Some things are hardly ever used (open mode, sending an e-mail when 49 crashing, etc.). Those will only be included when someone has a good reason 50 why it should be included and it's not too much work. 51- For some items it is debatable whether Vi compatibility should be 52 maintained. There will be an option flag for these. 53 54 55VIM IS... IMPROVED *design-improved* 56 57The IMproved bits of Vim should make it a better Vi, without becoming a 58completely different editor. Extensions are done with a "Vi spirit". 59- Use the keyboard as much as feasible. The mouse requires a third hand, 60 which we don't have. Many terminals don't have a mouse. 61- When the mouse is used anyway, avoid the need to switch back to the 62 keyboard. Avoid mixing mouse and keyboard handling. 63- Add commands and options in a consistent way. Otherwise people will have a 64 hard time finding and remembering them. Keep in mind that more commands and 65 options will be added later. 66- A feature that people do not know about is a useless feature. Don't add 67 obscure features, or at least add hints in documentation that they exists. 68- Minimize using CTRL and other modifiers, they are more difficult to type. 69- There are many first-time and inexperienced Vim users. Make it easy for 70 them to start using Vim and learn more over time. 71- There is no limit to the features that can be added. Selecting new features 72 is one based on (1) what users ask for, (2) how much effort it takes to 73 implement and (3) someone actually implementing it. 74 75 76VIM IS... MULTI PLATFORM *design-multi-platform* 77 78Vim tries to help as many users on as many platforms as possible. 79- Support many kinds of terminals. The minimal demands are cursor positioning 80 and clear-screen. Commands should only use key strokes that most keyboards 81 have. Support all the keys on the keyboard for mapping. 82- Support many platforms. A condition is that there is someone willing to do 83 Vim development on that platform, and it doesn't mean messing up the code. 84- Support many compilers and libraries. Not everybody is able or allowed to 85 install another compiler or GUI library. 86- People switch from one platform to another, and from GUI to terminal 87 version. Features should be present in all versions, or at least in as many 88 as possible with a reasonable effort. Try to avoid that users must switch 89 between platforms to accomplish their work efficiently. 90- That a feature is not possible on some platforms, or only possible on one 91 platform, does not mean it cannot be implemented. [This intentionally 92 contradicts the previous item, these two must be balanced.] 93 94 95VIM IS... WELL DOCUMENTED *design-documented* 96 97- A feature that isn't documented is a useless feature. A patch for a new 98 feature must include the documentation. 99- Documentation should be comprehensive and understandable. Using examples is 100 recommended. 101- Don't make the text unnecessarily long. Less documentation means that an 102 item is easier to find. 103 104 105VIM IS... HIGH SPEED AND SMALL IN SIZE *design-speed-size* 106 107Using Vim must not be a big attack on system resources. Keep it small and 108fast. 109- Computers are becoming faster and bigger each year. Vim can grow too, but 110 no faster than computers are growing. Keep Vim usable on older systems. 111- Many users start Vim from a shell very often. Startup time must be short. 112- Commands must work efficiently. The time they consume must be as small as 113 possible. Useful commands may take longer. 114- Don't forget that some people use Vim over a slow connection. Minimize the 115 communication overhead. 116- Items that add considerably to the size and are not used by many people 117 should be a feature that can be disabled. 118- Vim is a component among other components. Don't turn it into a massive 119 application, but have it work well together with other programs. 120 121 122VIM IS... MAINTAINABLE *design-maintain* 123 124- The source code should not become a mess. It should be reliable code. 125- Use the same layout in all files to make it easy to read |coding-style|. 126- Use comments in a useful way! Quoting the function name and argument names 127 is NOT useful. Do explain what they are for. 128- Porting to another platform should be made easy, without having to change 129 too much platform-independent code. 130- Use the object-oriented spirit: Put data and code together. Minimize the 131 knowledge spread to other parts of the code. 132 133 134VIM IS... FLEXIBLE *design-flexible* 135 136Vim should make it easy for users to work in their preferred styles rather 137than coercing its users into particular patterns of work. This can be for 138items with a large impact (e.g., the 'compatible' option) or for details. The 139defaults are carefully chosen such that most users will enjoy using Vim as it 140is. Commands and options can be used to adjust Vim to the desire of the user 141and its environment. 142 143 144VIM IS... NOT *design-not* 145 146- Vim is not a shell or an Operating System. You will not be able to run a 147 shell inside Vim or use it to control a debugger. This should work the 148 other way around: Use Vim as a component from a shell or in an IDE. 149 A satirical way to say this: "Unlike Emacs, Vim does not attempt to include 150 everything but the kitchen sink, but some people say that you can clean one 151 with it. ;-)" 152- Vim is not a fancy GUI editor that tries to look nice at the cost of 153 being less consistent over all platforms. But functional GUI features are 154 welcomed. 155 156============================================================================== 1572. Coding style *coding-style* 158 159These are the rules to use when making changes to the Vim source code. Please 160stick to these rules, to keep the sources readable and maintainable. 161 162This list is not complete. Look in the source code for more examples. 163 164 165MAKING CHANGES *style-changes* 166 167The basic steps to make changes to the code: 1681. Adjust the documentation. Doing this first gives you an impression of how 169 your changes affect the user. 1702. Make the source code changes. 1713. Check ../doc/todo.txt if the change affects any listed item. 1724. Make a patch with "diff -c" against the unmodified code and docs. 1735. Make a note about what changed and include it with the patch. 174 175 176USE OF COMMON FUNCTIONS *style-functions* 177 178Some functions that are common to use, have a special Vim version. Always 179consider using the Vim version, because they were introduced with a reason. 180 181NORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION 182free() vim_free() Checks for freeing NULL 183malloc() alloc() Checks for out of memory situation 184malloc() lalloc() Like alloc(), but has long argument 185strcpy() STRCPY() Includes cast to (char *), for char_u * args 186strchr() vim_strchr() Accepts special characters 187strrchr() vim_strrchr() Accepts special characters 188isspace() vim_isspace() Can handle characters > 128 189iswhite() vim_iswhite() Only TRUE for Tab and space 190memcpy() vim_memmove() Handles overlapped copies 191bcopy() vim_memmove() Handles overlapped copies 192memset() vim_memset() Uniform for all systems 193 194 195NAMES *style-names* 196 197Function names can not be more than 31 characters long (because of VMS). 198 199Don't use "delete" as a variable name, C++ doesn't like it. 200 201Because of the requirement that Vim runs on as many systems as possible, we 202need to avoid using names that are already defined by the system. This is a 203list of names that are known to cause trouble. The name is given as a regexp 204pattern. 205 206is.*() POSIX, ctype.h 207to.*() POSIX, ctype.h 208 209d_.* POSIX, dirent.h 210l_.* POSIX, fcntl.h 211gr_.* POSIX, grp.h 212pw_.* POSIX, pwd.h 213sa_.* POSIX, signal.h 214mem.* POSIX, string.h 215str.* POSIX, string.h 216wcs.* POSIX, string.h 217st_.* POSIX, stat.h 218tms_.* POSIX, times.h 219tm_.* POSIX, time.h 220c_.* POSIX, termios.h 221MAX.* POSIX, limits.h 222__.* POSIX, system 223_[A-Z].* POSIX, system 224E[A-Z0-9]* POSIX, errno.h 225 226*_t POSIX, for typedefs. Use *_T instead. 227 228wait don't use as argument to a function, conflicts with types.h 229index shadows global declaration 230time shadows global declaration 231new C++ reserved keyword 232try Borland C++ doesn't like it to be used as a variable. 233 234basename() GNU string function 235dirname() GNU string function 236get_env_value() Linux system function 237 238 239VARIOUS *style-various* 240 241Typedef'ed names should end in "_T": > 242 typedef int some_T; 243Define'ed names should be uppercase: > 244 #define SOME_THING 245Features always start with "FEAT_": > 246 #define FEAT_FOO 247 248Don't use '\"', some compilers can't handle it. '"' works fine. 249 250Don't use: 251 #if HAVE_SOME 252Some compilers can't handle that and complain that "HAVE_SOME" is not defined. 253Use 254 #ifdef HAVE_SOME 255or 256 #if defined(HAVE_SOME) 257 258 259STYLE *style-examples* 260 261General rule: One statement per line. 262 263Wrong: if (cond) a = 1; 264 265OK: if (cond) 266 a = 1; 267 268Wrong: while (cond); 269 270OK: while (cond) 271 ; 272 273Wrong: do a = 1; while (cond); 274 275OK: do 276 a = 1; 277 while (cond); 278 279 280Functions start with: 281 282Wrong: int function_name(int arg1, int arg2) 283 284OK: /* 285 * Explanation of what this function is used for. 286 * 287 * Return value explanation. 288 */ 289 int 290 function_name(arg1, arg2) 291 int arg1; /* short comment about arg1 */ 292 int arg2; /* short comment about arg2 */ 293 { 294 int local; /* comment about local */ 295 296 local = arg1 * arg2; 297 298NOTE: Don't use ANSI style function declarations. A few people still have to 299use a compiler that doesn't support it. 300 301 302SPACES AND PUNCTUATION *style-spaces* 303 304No space between a function name and the bracket: 305 306Wrong: func (arg); 307OK: func(arg); 308 309Do use a space after if, while, switch, etc. 310 311Wrong: if(arg) for(;;) 312OK: if (arg) for (;;) 313 314Use a space after a comma and semicolon: 315 316Wrong: func(arg1,arg2); for (i = 0;i < 2;++i) 317OK: func(arg1, arg2); for (i = 0; i < 2; ++i) 318 319Use a space before and after '=', '+', '/', etc. 320 321Wrong: var=a*5; 322OK: var = a * 5; 323 324In general: Use empty lines to group lines of code together. Put a comment 325just above the group of lines. This makes it more easy to quickly see what is 326being done. 327 328OK: /* Prepare for building the table. */ 329 get_first_item(); 330 table_idx = 0; 331 332 /* Build the table */ 333 while (has_item()) 334 table[table_idx++] = next_item(); 335 336 /* Finish up. */ 337 cleanup_items(); 338 generate_hash(table); 339 340============================================================================== 3413. Design decisions *design-decisions* 342 343Folding 344 345Several forms of folding should be possible for the same buffer. For example, 346have one window that shows the text with function bodies folded, another 347window that shows a function body. 348 349Folding is a way to display the text. It should not change the text itself. 350Therefore the folding has been implemented as a filter between the text stored 351in a buffer (buffer lines) and the text displayed in a window (logical lines). 352 353 354Naming the window 355 356The word "window" is commonly used for several things: A window on the screen, 357the xterm window, a window inside Vim to view a buffer. 358To avoid confusion, other items that are sometimes called window have been 359given another name. Here is an overview of the related items: 360 361screen The whole display. For the GUI it's something like 1024x768 362 pixels. The Vim shell can use the whole screen or part of it. 363shell The Vim application. This can cover the whole screen (e.g., 364 when running in a console) or part of it (xterm or GUI). 365window View on a buffer. There can be several windows in Vim, 366 together with the command line, menubar, toolbar, etc. they 367 fit in the shell. 368 369 370Spell checking *develop-spell* 371 372When spell checking was going to be added to Vim a survey was done over the 373available spell checking libraries and programs. Unfortunately, the result 374was that none of them provided sufficient capabilities to be used as the spell 375checking engine in Vim, for various reasons: 376 377- Missing support for multi-byte encodings. At least UTF-8 must be supported, 378 so that more than one language can be used in the same file. 379 Doing on-the-fly conversion is not always possible (would require iconv 380 support). 381- For the programs and libraries: Using them as-is would require installing 382 them separately from Vim. That's mostly not impossible, but a drawback. 383- Performance: A few tests showed that it's possible to check spelling on the 384 fly (while redrawing), just like syntax highlighting. But the mechanisms 385 used by other code are much slower. Myspell uses a hashtable, for example. 386 The affix compression that most spell checkers use makes it slower too. 387- For using an external program like aspell a communication mechanism would 388 have to be setup. That's complicated to do in a portable way (Unix-only 389 would be relatively simple, but that's not good enough). And performance 390 will become a problem (lots of process switching involved). 391- Missing support for words with non-word characters, such as "Etten-Leur" and 392 "et al.", would require marking the pieces of them OK, lowering the 393 reliability. 394- Missing support for regions or dialects. Makes it difficult to accept 395 all English words and highlight non-Canadian words differently. 396- Missing support for rare words. Many words are correct but hardly ever used 397 and could be a misspelled often-used word. 398- For making suggestions the speed is less important and requiring to install 399 another program or library would be acceptable. But the word lists probably 400 differ, the suggestions may be wrong words. 401 402 403Spelling suggestions *develop-spell-suggestions* 404 405For making suggestions there are two basic mechanisms: 4061. Try changing the bad word a little bit and check for a match with a good 407 word. Or go through the list of good words, change them a little bit and 408 check for a match with the bad word. The changes are deleting a character, 409 inserting a character, swapping two characters, etc. 4102. Perform soundfolding on both the bad word and the good words and then find 411 matches, possibly with a few changes like with the first mechanism. 412 413The first is good for finding typing mistakes. After experimenting with 414hashtables and looking at solutions from other spell checkers the conclusion 415was that a trie (a kind of tree structure) is ideal for this. Both for 416reducing memory use and being able to try sensible changes. For example, when 417inserting a character only characters that lead to good words need to be 418tried. Other mechanisms (with hashtables) need to try all possible letters at 419every position in the word. Also, a hashtable has the requirement that word 420boundaries are identified separately, while a trie does not require this. 421That makes the mechanism a lot simpler. 422 423Soundfolding is useful when someone knows how the words sounds but doesn't 424know how it is spelled. For example, the word "dictionary" might be written 425as "daktonerie". The number of changes that the first method would need to 426try is very big, it's hard to find the good word that way. After soundfolding 427the words become "tktnr" and "tkxnry", these differ by only two letters. 428 429To find words by their soundfolded equivalent (soundalike word) we need a list 430of all soundfolded words. A few experiments have been done to find out what 431the best method is. Alternatives: 4321. Do the sound folding on the fly when looking for suggestions. This means 433 walking through the trie of good words, soundfolding each word and 434 checking how different it is from the bad word. This is very efficient for 435 memory use, but takes a long time. On a fast PC it takes a couple of 436 seconds for English, which can be acceptable for interactive use. But for 437 some languages it takes more than ten seconds (e.g., German, Catalan), 438 which is unacceptable slow. For batch processing (automatic corrections) 439 it's to slow for all languages. 4402. Use a trie for the soundfolded words, so that searching can be done just 441 like how it works without soundfolding. This requires remembering a list 442 of good words for each soundfolded word. This makes finding matches very 443 fast but requires quite a lot of memory, in the order of 1 to 10 Mbyte. 444 For some languages more than the original word list. 4453. Like the second alternative, but reduce the amount of memory by using affix 446 compression and store only the soundfolded basic word. This is what Aspell 447 does. Disadvantage is that affixes need to be stripped from the bad word 448 before soundfolding it, which means that mistakes at the start and/or end 449 of the word will cause the mechanism to fail. Also, this becomes slow when 450 the bad word is quite different from the good word. 451 452The choice made is to use the second mechanism and use a separate file. This 453way a user with sufficient memory can get very good suggestions while a user 454who is short of memory or just wants the spell checking and no suggestions 455doesn't use so much memory. 456 457 458Word frequency 459 460For sorting suggestions it helps to know which words are common. In theory we 461could store a word frequency with the word in the dictionary. However, this 462requires storing a count per word. That degrades word tree compression a lot. 463And maintaining the word frequency for all languages will be a heavy task. 464Also, it would be nice to prefer words that are already in the text. This way 465the words that appear in the specific text are preferred for suggestions. 466 467What has been implemented is to count words that have been seen during 468displaying. A hashtable is used to quickly find the word count. The count is 469initialized from words listed in COMMON items in the affix file, so that it 470also works when starting a new file. 471 472This isn't ideal, because the longer Vim is running the higher the counts 473become. But in practice it is a noticable improvement over not using the word 474count. 475 476============================================================================== 4774. Assumptions *design-assumptions* 478 479Size of variables: 480char 8 bit signed 481char_u 8 bit unsigned 482int 32 or 64 bit signed (16 might be possible with limited features) 483unsigned 32 or 64 bit unsigned (16 as with ints) 484long 32 or 64 bit signed, can hold a pointer 485 486Note that some compilers cannot handle long lines or strings. The C89 487standard specifies a limit of 509 characters. 488 489 vim:tw=78:ts=8:ft=help:norl: 490