1*207f0093SBram Moolenaar*develop.txt* For Vim version 8.2. Last change: 2020 Aug 15 2071d4279SBram Moolenaar 3071d4279SBram Moolenaar 4071d4279SBram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar 5071d4279SBram Moolenaar 6071d4279SBram Moolenaar 7071d4279SBram MoolenaarDevelopment of Vim. *development* 8071d4279SBram Moolenaar 9071d4279SBram MoolenaarThis text is important for those who want to be involved in further developing 10071d4279SBram MoolenaarVim. 11071d4279SBram Moolenaar 12071d4279SBram Moolenaar1. Design goals |design-goals| 13071d4279SBram Moolenaar2. Coding style |coding-style| 14071d4279SBram Moolenaar3. Design decisions |design-decisions| 15071d4279SBram Moolenaar4. Assumptions |design-assumptions| 16071d4279SBram Moolenaar 17071d4279SBram MoolenaarSee the file README.txt in the "src" directory for an overview of the source 18071d4279SBram Moolenaarcode. 19071d4279SBram Moolenaar 20071d4279SBram MoolenaarVim is open source software. Everybody is encouraged to contribute to help 2185eee130SBram Moolenaarimproving Vim. For sending patches a unified diff "diff -u" is preferred. 2285eee130SBram MoolenaarYou can create a pull request on github, but it's not required. 23531da595SBram MoolenaarAlso see http://vim.wikia.com/wiki/How_to_make_and_submit_a_patch. 24071d4279SBram Moolenaar 25071d4279SBram Moolenaar============================================================================== 26071d4279SBram Moolenaar1. Design goals *design-goals* 27071d4279SBram Moolenaar 28071d4279SBram MoolenaarMost important things come first (roughly). 29071d4279SBram Moolenaar 30071d4279SBram MoolenaarNote that quite a few items are contradicting. This is intentional. A 31071d4279SBram Moolenaarbalance must be found between them. 32071d4279SBram Moolenaar 33071d4279SBram Moolenaar 34071d4279SBram MoolenaarVIM IS... VI COMPATIBLE *design-compatible* 35071d4279SBram Moolenaar 36071d4279SBram MoolenaarFirst of all, it should be possible to use Vim as a drop-in replacement for 37e7b1ea02SBram MoolenaarVi. When the user wants to, Vim can be used in compatible mode and hardly 38e7b1ea02SBram Moolenaarany differences with the original Vi will be noticed. 39071d4279SBram Moolenaar 40071d4279SBram MoolenaarExceptions: 41071d4279SBram Moolenaar- We don't reproduce obvious Vi bugs in Vim. 42071d4279SBram Moolenaar- There are different versions of Vi. I am using Version 3.7 (6/7/85) as a 43071d4279SBram Moolenaar reference. But support for other versions is also included when possible. 44071d4279SBram Moolenaar The Vi part of POSIX is not considered a definitive source. 45071d4279SBram Moolenaar- Vim adds new commands, you cannot rely on some command to fail because it 46071d4279SBram Moolenaar didn't exist in Vi. 47071d4279SBram Moolenaar- Vim will have a lot of features that Vi doesn't have. Going back from Vim 48071d4279SBram Moolenaar to Vi will be a problem, this cannot be avoided. 49071d4279SBram Moolenaar- Some things are hardly ever used (open mode, sending an e-mail when 50071d4279SBram Moolenaar crashing, etc.). Those will only be included when someone has a good reason 51071d4279SBram Moolenaar why it should be included and it's not too much work. 52071d4279SBram Moolenaar- For some items it is debatable whether Vi compatibility should be 53071d4279SBram Moolenaar maintained. There will be an option flag for these. 54071d4279SBram Moolenaar 55071d4279SBram Moolenaar 56071d4279SBram MoolenaarVIM IS... IMPROVED *design-improved* 57071d4279SBram Moolenaar 58071d4279SBram MoolenaarThe IMproved bits of Vim should make it a better Vi, without becoming a 59071d4279SBram Moolenaarcompletely different editor. Extensions are done with a "Vi spirit". 60071d4279SBram Moolenaar- Use the keyboard as much as feasible. The mouse requires a third hand, 61071d4279SBram Moolenaar which we don't have. Many terminals don't have a mouse. 62071d4279SBram Moolenaar- When the mouse is used anyway, avoid the need to switch back to the 63071d4279SBram Moolenaar keyboard. Avoid mixing mouse and keyboard handling. 64071d4279SBram Moolenaar- Add commands and options in a consistent way. Otherwise people will have a 65071d4279SBram Moolenaar hard time finding and remembering them. Keep in mind that more commands and 66071d4279SBram Moolenaar options will be added later. 67071d4279SBram Moolenaar- A feature that people do not know about is a useless feature. Don't add 688f3f58f2SBram Moolenaar obscure features, or at least add hints in documentation that they exist. 69071d4279SBram Moolenaar- Minimize using CTRL and other modifiers, they are more difficult to type. 70071d4279SBram Moolenaar- There are many first-time and inexperienced Vim users. Make it easy for 71071d4279SBram Moolenaar them to start using Vim and learn more over time. 72071d4279SBram Moolenaar- There is no limit to the features that can be added. Selecting new features 73071d4279SBram Moolenaar is one based on (1) what users ask for, (2) how much effort it takes to 74071d4279SBram Moolenaar implement and (3) someone actually implementing it. 75071d4279SBram Moolenaar 76071d4279SBram Moolenaar 77071d4279SBram MoolenaarVIM IS... MULTI PLATFORM *design-multi-platform* 78071d4279SBram Moolenaar 79071d4279SBram MoolenaarVim tries to help as many users on as many platforms as possible. 80071d4279SBram Moolenaar- Support many kinds of terminals. The minimal demands are cursor positioning 81071d4279SBram Moolenaar and clear-screen. Commands should only use key strokes that most keyboards 82071d4279SBram Moolenaar have. Support all the keys on the keyboard for mapping. 83071d4279SBram Moolenaar- Support many platforms. A condition is that there is someone willing to do 84071d4279SBram Moolenaar Vim development on that platform, and it doesn't mean messing up the code. 85071d4279SBram Moolenaar- Support many compilers and libraries. Not everybody is able or allowed to 86071d4279SBram Moolenaar install another compiler or GUI library. 87071d4279SBram Moolenaar- People switch from one platform to another, and from GUI to terminal 88071d4279SBram Moolenaar version. Features should be present in all versions, or at least in as many 89071d4279SBram Moolenaar as possible with a reasonable effort. Try to avoid that users must switch 90071d4279SBram Moolenaar between platforms to accomplish their work efficiently. 91071d4279SBram Moolenaar- That a feature is not possible on some platforms, or only possible on one 92071d4279SBram Moolenaar platform, does not mean it cannot be implemented. [This intentionally 93071d4279SBram Moolenaar contradicts the previous item, these two must be balanced.] 94071d4279SBram Moolenaar 95071d4279SBram Moolenaar 96071d4279SBram MoolenaarVIM IS... WELL DOCUMENTED *design-documented* 97071d4279SBram Moolenaar 98071d4279SBram Moolenaar- A feature that isn't documented is a useless feature. A patch for a new 99071d4279SBram Moolenaar feature must include the documentation. 100071d4279SBram Moolenaar- Documentation should be comprehensive and understandable. Using examples is 101071d4279SBram Moolenaar recommended. 102071d4279SBram Moolenaar- Don't make the text unnecessarily long. Less documentation means that an 103071d4279SBram Moolenaar item is easier to find. 104071d4279SBram Moolenaar 105071d4279SBram Moolenaar 106071d4279SBram MoolenaarVIM IS... HIGH SPEED AND SMALL IN SIZE *design-speed-size* 107071d4279SBram Moolenaar 108071d4279SBram MoolenaarUsing Vim must not be a big attack on system resources. Keep it small and 109071d4279SBram Moolenaarfast. 110071d4279SBram Moolenaar- Computers are becoming faster and bigger each year. Vim can grow too, but 111071d4279SBram Moolenaar no faster than computers are growing. Keep Vim usable on older systems. 112071d4279SBram Moolenaar- Many users start Vim from a shell very often. Startup time must be short. 113071d4279SBram Moolenaar- Commands must work efficiently. The time they consume must be as small as 114071d4279SBram Moolenaar possible. Useful commands may take longer. 115071d4279SBram Moolenaar- Don't forget that some people use Vim over a slow connection. Minimize the 116071d4279SBram Moolenaar communication overhead. 117071d4279SBram Moolenaar- Items that add considerably to the size and are not used by many people 118071d4279SBram Moolenaar should be a feature that can be disabled. 119071d4279SBram Moolenaar- Vim is a component among other components. Don't turn it into a massive 120071d4279SBram Moolenaar application, but have it work well together with other programs. 121071d4279SBram Moolenaar 122071d4279SBram Moolenaar 123071d4279SBram MoolenaarVIM IS... MAINTAINABLE *design-maintain* 124071d4279SBram Moolenaar 125071d4279SBram Moolenaar- The source code should not become a mess. It should be reliable code. 126071d4279SBram Moolenaar- Use the same layout in all files to make it easy to read |coding-style|. 127ae5bce1cSBram Moolenaar- Use comments in a useful way! Quoting the function name and argument names 128ae5bce1cSBram Moolenaar is NOT useful. Do explain what they are for. 129071d4279SBram Moolenaar- Porting to another platform should be made easy, without having to change 130071d4279SBram Moolenaar too much platform-independent code. 131071d4279SBram Moolenaar- Use the object-oriented spirit: Put data and code together. Minimize the 132071d4279SBram Moolenaar knowledge spread to other parts of the code. 133071d4279SBram Moolenaar 134071d4279SBram Moolenaar 135071d4279SBram MoolenaarVIM IS... FLEXIBLE *design-flexible* 136071d4279SBram Moolenaar 137071d4279SBram MoolenaarVim should make it easy for users to work in their preferred styles rather 138071d4279SBram Moolenaarthan coercing its users into particular patterns of work. This can be for 139071d4279SBram Moolenaaritems with a large impact (e.g., the 'compatible' option) or for details. The 140071d4279SBram Moolenaardefaults are carefully chosen such that most users will enjoy using Vim as it 141071d4279SBram Moolenaaris. Commands and options can be used to adjust Vim to the desire of the user 142071d4279SBram Moolenaarand its environment. 143071d4279SBram Moolenaar 144071d4279SBram Moolenaar 145071d4279SBram MoolenaarVIM IS... NOT *design-not* 146071d4279SBram Moolenaar 147f55e4c86SBram Moolenaar- Vim is not a shell or an Operating System. It does provide a terminal 148f55e4c86SBram Moolenaar window, in which you can run a shell or debugger. E.g. to be able to do 149f55e4c86SBram Moolenaar this over an ssh connection. But if you don't need a text editor with that 150f55e4c86SBram Moolenaar it is out of scope (use something like screen or tmux instead). 151071d4279SBram Moolenaar A satirical way to say this: "Unlike Emacs, Vim does not attempt to include 152071d4279SBram Moolenaar everything but the kitchen sink, but some people say that you can clean one 153071d4279SBram Moolenaar with it. ;-)" 1542c7f8c57SBram Moolenaar To use Vim with gdb see |terminal-debugger|. Other (older) tools can be 1552c7f8c57SBram Moolenaar found at http://www.agide.org and http://clewn.sf.net. 156071d4279SBram Moolenaar- Vim is not a fancy GUI editor that tries to look nice at the cost of 157071d4279SBram Moolenaar being less consistent over all platforms. But functional GUI features are 158071d4279SBram Moolenaar welcomed. 159071d4279SBram Moolenaar 160071d4279SBram Moolenaar============================================================================== 161071d4279SBram Moolenaar2. Coding style *coding-style* 162071d4279SBram Moolenaar 163071d4279SBram MoolenaarThese are the rules to use when making changes to the Vim source code. Please 164071d4279SBram Moolenaarstick to these rules, to keep the sources readable and maintainable. 165071d4279SBram Moolenaar 166071d4279SBram MoolenaarThis list is not complete. Look in the source code for more examples. 167071d4279SBram Moolenaar 168071d4279SBram Moolenaar 169071d4279SBram MoolenaarMAKING CHANGES *style-changes* 170071d4279SBram Moolenaar 171071d4279SBram MoolenaarThe basic steps to make changes to the code: 17213d5aeefSBram Moolenaar1. Get the code from github. That makes it easier to keep your changed 17313d5aeefSBram Moolenaar version in sync with the main code base (it may be a while before your 17413d5aeefSBram Moolenaar changes will be included). You do need to spend some time learning git, 17513d5aeefSBram Moolenaar it's not the most user friendly tool. 17613d5aeefSBram Moolenaar2. Adjust the documentation. Doing this first gives you an impression of how 177071d4279SBram Moolenaar your changes affect the user. 17813d5aeefSBram Moolenaar3. Make the source code changes. 17913d5aeefSBram Moolenaar4. Check ../doc/todo.txt if the change affects any listed item. 18013d5aeefSBram Moolenaar5. Make a patch with "git diff". You can also create a pull request on 18113d5aeefSBram Moolenaar github, but it's the diff that matters. 18213d5aeefSBram Moolenaar6. Make a note about what changed, preferably mentioning the problem and the 18368563937SBram Moolenaar solution. Send an email to the |vim-dev| maillist with an explanation and 18413d5aeefSBram Moolenaar include the diff. Or create a pull request on github. 18513d5aeefSBram Moolenaar 18613d5aeefSBram Moolenaar 18715142e27SBram MoolenaarC COMPILER *style-compiler* *ANSI-C* *C89* *C99* 18813d5aeefSBram Moolenaar 18913d5aeefSBram MoolenaarThe minimal C compiler version supported is C89, also known as ANSI C. 190561f8a5aSBram MoolenaarLater standards, such as C99, are not widely supported, or at least not 100% 191561f8a5aSBram Moolenaarsupported. Therefore we use only some of the C99 features and disallow some 192561f8a5aSBram Moolenaar(at least for now). 19313d5aeefSBram Moolenaar 194561f8a5aSBram MoolenaarPlease don't make changes everywhere to use the C99 features, it causes merge 195561f8a5aSBram Moolenaarproblems for existing patches. Only use them for new and changed code. 196561f8a5aSBram Moolenaar 197561f8a5aSBram MoolenaarComments ~ 198561f8a5aSBram Moolenaar 1990c0734d5SBram MoolenaarTraditionally Vim uses /* comments */. We intend to keep it that way 2000c0734d5SBram Moolenaarfor file and function headers and larger blocks of code, E.g.: 2010c0734d5SBram Moolenaar /* 2020c0734d5SBram Moolenaar * The "foo" argument does something useful. 2030c0734d5SBram Moolenaar * Return OK or FAIL. 2040c0734d5SBram Moolenaar */ 2050c0734d5SBram MoolenaarFor new code or lines of code that change, it is preferred to use // comments. 2060c0734d5SBram MoolenaarEspecially when it comes after code: 207561f8a5aSBram Moolenaar int some_var; // single line comment useful here 208561f8a5aSBram Moolenaar 209561f8a5aSBram MoolenaarEnums ~ 210561f8a5aSBram Moolenaar 211561f8a5aSBram MoolenaarThe last item in an enum may have a trailing comma. C89 didn't allow this. 212561f8a5aSBram Moolenaar 213561f8a5aSBram MoolenaarTypes ~ 214561f8a5aSBram Moolenaar 215561f8a5aSBram Moolenaar"long long" is allowed and can be expected to be 64 bits. Use %lld in printf 216561f8a5aSBram Moolenaarformats. Also "long long unsigned" with %llu. 217561f8a5aSBram Moolenaar 218561f8a5aSBram MoolenaarNot to be used ~ 219561f8a5aSBram Moolenaar 220561f8a5aSBram MoolenaarThese C99 features are not to be used, because not enough compilers support 221561f8a5aSBram Moolenaarthem: 222561f8a5aSBram Moolenaar- Declaration after Statements (MSVC 2012 does not support it). All 223561f8a5aSBram Moolenaar declarations need to be at the start of the block. 224561f8a5aSBram Moolenaar- Variable length arrays (even in C11 this is an optional feature). 225561f8a5aSBram Moolenaar- _Bool and _Complex types. 226561f8a5aSBram Moolenaar- "inline" (it's hardly ever needed, let the optimizer do its work) 227285e3358SBram Moolenaar- flexible array members: Not supported by HP-UX C compiler (John Marriott) 228071d4279SBram Moolenaar 229071d4279SBram Moolenaar 230071d4279SBram MoolenaarUSE OF COMMON FUNCTIONS *style-functions* 231071d4279SBram Moolenaar 232071d4279SBram MoolenaarSome functions that are common to use, have a special Vim version. Always 233071d4279SBram Moolenaarconsider using the Vim version, because they were introduced with a reason. 234071d4279SBram Moolenaar 235071d4279SBram MoolenaarNORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION 236071d4279SBram Moolenaarfree() vim_free() Checks for freeing NULL 237071d4279SBram Moolenaarmalloc() alloc() Checks for out of memory situation 238071d4279SBram Moolenaarmalloc() lalloc() Like alloc(), but has long argument 239071d4279SBram Moolenaarstrcpy() STRCPY() Includes cast to (char *), for char_u * args 240071d4279SBram Moolenaarstrchr() vim_strchr() Accepts special characters 241071d4279SBram Moolenaarstrrchr() vim_strrchr() Accepts special characters 242071d4279SBram Moolenaarisspace() vim_isspace() Can handle characters > 128 2439e368dbaSBram Moolenaariswhite() vim_iswhite() Only TRUE for tab and space 24436fc535cSBram Moolenaarmemcpy() mch_memmove() Handles overlapped copies 24536fc535cSBram Moolenaarbcopy() mch_memmove() Handles overlapped copies 246071d4279SBram Moolenaarmemset() vim_memset() Uniform for all systems 247071d4279SBram Moolenaar 248071d4279SBram Moolenaar 249071d4279SBram MoolenaarNAMES *style-names* 250071d4279SBram Moolenaar 251071d4279SBram MoolenaarFunction names can not be more than 31 characters long (because of VMS). 252071d4279SBram Moolenaar 25313d5aeefSBram MoolenaarDon't use "delete" or "this" as a variable name, C++ doesn't like it. 254071d4279SBram Moolenaar 255071d4279SBram MoolenaarBecause of the requirement that Vim runs on as many systems as possible, we 256071d4279SBram Moolenaarneed to avoid using names that are already defined by the system. This is a 257071d4279SBram Moolenaarlist of names that are known to cause trouble. The name is given as a regexp 258071d4279SBram Moolenaarpattern. 259071d4279SBram Moolenaar 260071d4279SBram Moolenaaris.*() POSIX, ctype.h 261071d4279SBram Moolenaarto.*() POSIX, ctype.h 262071d4279SBram Moolenaar 263071d4279SBram Moolenaard_.* POSIX, dirent.h 264071d4279SBram Moolenaarl_.* POSIX, fcntl.h 265071d4279SBram Moolenaargr_.* POSIX, grp.h 266071d4279SBram Moolenaarpw_.* POSIX, pwd.h 267071d4279SBram Moolenaarsa_.* POSIX, signal.h 268071d4279SBram Moolenaarmem.* POSIX, string.h 269071d4279SBram Moolenaarstr.* POSIX, string.h 270071d4279SBram Moolenaarwcs.* POSIX, string.h 271071d4279SBram Moolenaarst_.* POSIX, stat.h 272071d4279SBram Moolenaartms_.* POSIX, times.h 273071d4279SBram Moolenaartm_.* POSIX, time.h 274071d4279SBram Moolenaarc_.* POSIX, termios.h 275071d4279SBram MoolenaarMAX.* POSIX, limits.h 276071d4279SBram Moolenaar__.* POSIX, system 277071d4279SBram Moolenaar_[A-Z].* POSIX, system 278071d4279SBram MoolenaarE[A-Z0-9]* POSIX, errno.h 279071d4279SBram Moolenaar 2809964e468SBram Moolenaar.*_t POSIX, for typedefs. Use .*_T instead. 281071d4279SBram Moolenaar 282071d4279SBram Moolenaarwait don't use as argument to a function, conflicts with types.h 283071d4279SBram Moolenaarindex shadows global declaration 284071d4279SBram Moolenaartime shadows global declaration 285071d4279SBram Moolenaarnew C++ reserved keyword 286071d4279SBram Moolenaar 2876be7f873SBram Moolenaarclear Mac curses.h 2886be7f873SBram Moolenaarecho Mac curses.h 2896be7f873SBram Moolenaarinstr Mac curses.h 2906be7f873SBram Moolenaarmeta Mac curses.h 2916be7f873SBram Moolenaarnewwin Mac curses.h 2926be7f873SBram Moolenaarnl Mac curses.h 2936be7f873SBram Moolenaaroverwrite Mac curses.h 2946be7f873SBram Moolenaarrefresh Mac curses.h 2956be7f873SBram Moolenaarscroll Mac curses.h 2966be7f873SBram Moolenaartypeahead Mac curses.h 2976be7f873SBram Moolenaar 298071d4279SBram Moolenaarbasename() GNU string function 299071d4279SBram Moolenaardirname() GNU string function 300071d4279SBram Moolenaarget_env_value() Linux system function 301071d4279SBram Moolenaar 302071d4279SBram Moolenaar 303071d4279SBram MoolenaarVARIOUS *style-various* 304071d4279SBram Moolenaar 305e344beadSBram MoolenaarTypedef'ed names should end in "_T": > 306e344beadSBram Moolenaar typedef int some_T; 307071d4279SBram MoolenaarDefine'ed names should be uppercase: > 308071d4279SBram Moolenaar #define SOME_THING 309071d4279SBram MoolenaarFeatures always start with "FEAT_": > 310071d4279SBram Moolenaar #define FEAT_FOO 311071d4279SBram Moolenaar 312071d4279SBram MoolenaarDon't use '\"', some compilers can't handle it. '"' works fine. 313071d4279SBram Moolenaar 314071d4279SBram MoolenaarDon't use: 315071d4279SBram Moolenaar #if HAVE_SOME 316071d4279SBram MoolenaarSome compilers can't handle that and complain that "HAVE_SOME" is not defined. 317071d4279SBram MoolenaarUse 318071d4279SBram Moolenaar #ifdef HAVE_SOME 319071d4279SBram Moolenaaror 320071d4279SBram Moolenaar #if defined(HAVE_SOME) 321071d4279SBram Moolenaar 322071d4279SBram Moolenaar 323071d4279SBram MoolenaarSTYLE *style-examples* 324071d4279SBram Moolenaar 325071d4279SBram MoolenaarGeneral rule: One statement per line. 326071d4279SBram Moolenaar 327071d4279SBram MoolenaarWrong: if (cond) a = 1; 328071d4279SBram Moolenaar 329071d4279SBram MoolenaarOK: if (cond) 330071d4279SBram Moolenaar a = 1; 331071d4279SBram Moolenaar 332071d4279SBram MoolenaarWrong: while (cond); 333071d4279SBram Moolenaar 334071d4279SBram MoolenaarOK: while (cond) 335071d4279SBram Moolenaar ; 336071d4279SBram Moolenaar 337071d4279SBram MoolenaarWrong: do a = 1; while (cond); 338071d4279SBram Moolenaar 339071d4279SBram MoolenaarOK: do 340071d4279SBram Moolenaar a = 1; 341071d4279SBram Moolenaar while (cond); 342071d4279SBram Moolenaar 34313d5aeefSBram MoolenaarWrong: if (cond) { 34413d5aeefSBram Moolenaar cmd; 34513d5aeefSBram Moolenaar cmd; 34613d5aeefSBram Moolenaar } else { 34713d5aeefSBram Moolenaar cmd; 34813d5aeefSBram Moolenaar cmd; 34913d5aeefSBram Moolenaar } 35013d5aeefSBram Moolenaar 35113d5aeefSBram MoolenaarOK: if (cond) 35213d5aeefSBram Moolenaar { 35313d5aeefSBram Moolenaar cmd; 35413d5aeefSBram Moolenaar cmd; 35513d5aeefSBram Moolenaar } 35613d5aeefSBram Moolenaar else 35713d5aeefSBram Moolenaar { 35813d5aeefSBram Moolenaar cmd; 35913d5aeefSBram Moolenaar cmd; 36013d5aeefSBram Moolenaar } 361071d4279SBram Moolenaar 3624c92e75dSBram MoolenaarWhen a block has one line the braces can be left out. When an if/else has 3634c92e75dSBram Moolenaarbraces on one block, it usually looks better when the other block also has 3644c92e75dSBram Moolenaarbraces: 3654c92e75dSBram MoolenaarOK: if (cond) 3664c92e75dSBram Moolenaar cmd; 3674c92e75dSBram Moolenaar else 3684c92e75dSBram Moolenaar cmd; 3694c92e75dSBram Moolenaar 3704c92e75dSBram MoolenaarOK: if (cond) 3714c92e75dSBram Moolenaar { 3724c92e75dSBram Moolenaar cmd; 3734c92e75dSBram Moolenaar } 3744c92e75dSBram Moolenaar else 3754c92e75dSBram Moolenaar { 3764c92e75dSBram Moolenaar cmd; 3774c92e75dSBram Moolenaar cmd; 3784c92e75dSBram Moolenaar } 3794c92e75dSBram Moolenaar 3805e9b2fa9SBram MoolenaarUse ANSI (new style) function declarations with the return type on a separate 3815e9b2fa9SBram Moolenaarindented line. 382071d4279SBram Moolenaar 383071d4279SBram MoolenaarWrong: int function_name(int arg1, int arg2) 384071d4279SBram Moolenaar 385071d4279SBram MoolenaarOK: /* 386071d4279SBram Moolenaar * Explanation of what this function is used for. 387071d4279SBram Moolenaar * 388071d4279SBram Moolenaar * Return value explanation. 389071d4279SBram Moolenaar */ 390071d4279SBram Moolenaar int 39113d5aeefSBram Moolenaar function_name( 3924c92e75dSBram Moolenaar int arg1, // short comment about arg1 3934c92e75dSBram Moolenaar int arg2) // short comment about arg2 394071d4279SBram Moolenaar { 3954c92e75dSBram Moolenaar int local; // comment about local 396071d4279SBram Moolenaar 397071d4279SBram Moolenaar local = arg1 * arg2; 398071d4279SBram Moolenaar 399071d4279SBram Moolenaar 400071d4279SBram Moolenaar 401071d4279SBram MoolenaarSPACES AND PUNCTUATION *style-spaces* 402071d4279SBram Moolenaar 403071d4279SBram MoolenaarNo space between a function name and the bracket: 404071d4279SBram Moolenaar 405071d4279SBram MoolenaarWrong: func (arg); 406071d4279SBram MoolenaarOK: func(arg); 407071d4279SBram Moolenaar 408071d4279SBram MoolenaarDo use a space after if, while, switch, etc. 409071d4279SBram Moolenaar 410071d4279SBram MoolenaarWrong: if(arg) for(;;) 411071d4279SBram MoolenaarOK: if (arg) for (;;) 412071d4279SBram Moolenaar 413071d4279SBram MoolenaarUse a space after a comma and semicolon: 414071d4279SBram Moolenaar 415071d4279SBram MoolenaarWrong: func(arg1,arg2); for (i = 0;i < 2;++i) 416071d4279SBram MoolenaarOK: func(arg1, arg2); for (i = 0; i < 2; ++i) 417071d4279SBram Moolenaar 418071d4279SBram MoolenaarUse a space before and after '=', '+', '/', etc. 419071d4279SBram Moolenaar 420071d4279SBram MoolenaarWrong: var=a*5; 421071d4279SBram MoolenaarOK: var = a * 5; 422071d4279SBram Moolenaar 423071d4279SBram MoolenaarIn general: Use empty lines to group lines of code together. Put a comment 4248f3f58f2SBram Moolenaarjust above the group of lines. This makes it easier to quickly see what is 425071d4279SBram Moolenaarbeing done. 426071d4279SBram Moolenaar 427071d4279SBram MoolenaarOK: /* Prepare for building the table. */ 428071d4279SBram Moolenaar get_first_item(); 429071d4279SBram Moolenaar table_idx = 0; 430071d4279SBram Moolenaar 431071d4279SBram Moolenaar /* Build the table */ 432071d4279SBram Moolenaar while (has_item()) 433071d4279SBram Moolenaar table[table_idx++] = next_item(); 434071d4279SBram Moolenaar 435071d4279SBram Moolenaar /* Finish up. */ 436071d4279SBram Moolenaar cleanup_items(); 437071d4279SBram Moolenaar generate_hash(table); 438071d4279SBram Moolenaar 439071d4279SBram Moolenaar============================================================================== 440071d4279SBram Moolenaar3. Design decisions *design-decisions* 441071d4279SBram Moolenaar 442071d4279SBram MoolenaarFolding 443071d4279SBram Moolenaar 444071d4279SBram MoolenaarSeveral forms of folding should be possible for the same buffer. For example, 445071d4279SBram Moolenaarhave one window that shows the text with function bodies folded, another 446071d4279SBram Moolenaarwindow that shows a function body. 447071d4279SBram Moolenaar 448071d4279SBram MoolenaarFolding is a way to display the text. It should not change the text itself. 449071d4279SBram MoolenaarTherefore the folding has been implemented as a filter between the text stored 450071d4279SBram Moolenaarin a buffer (buffer lines) and the text displayed in a window (logical lines). 451071d4279SBram Moolenaar 452071d4279SBram Moolenaar 453071d4279SBram MoolenaarNaming the window 454071d4279SBram Moolenaar 455071d4279SBram MoolenaarThe word "window" is commonly used for several things: A window on the screen, 456071d4279SBram Moolenaarthe xterm window, a window inside Vim to view a buffer. 457071d4279SBram MoolenaarTo avoid confusion, other items that are sometimes called window have been 458071d4279SBram Moolenaargiven another name. Here is an overview of the related items: 459071d4279SBram Moolenaar 460071d4279SBram Moolenaarscreen The whole display. For the GUI it's something like 1024x768 461071d4279SBram Moolenaar pixels. The Vim shell can use the whole screen or part of it. 462071d4279SBram Moolenaarshell The Vim application. This can cover the whole screen (e.g., 463071d4279SBram Moolenaar when running in a console) or part of it (xterm or GUI). 464071d4279SBram Moolenaarwindow View on a buffer. There can be several windows in Vim, 465071d4279SBram Moolenaar together with the command line, menubar, toolbar, etc. they 466071d4279SBram Moolenaar fit in the shell. 467071d4279SBram Moolenaar 468071d4279SBram Moolenaar 469402d2feaSBram MoolenaarSpell checking *develop-spell* 470402d2feaSBram Moolenaar 471402d2feaSBram MoolenaarWhen spell checking was going to be added to Vim a survey was done over the 472402d2feaSBram Moolenaaravailable spell checking libraries and programs. Unfortunately, the result 473402d2feaSBram Moolenaarwas that none of them provided sufficient capabilities to be used as the spell 474402d2feaSBram Moolenaarchecking engine in Vim, for various reasons: 475402d2feaSBram Moolenaar 476*207f0093SBram Moolenaar- Missing support for multibyte encodings. At least UTF-8 must be supported, 477402d2feaSBram Moolenaar so that more than one language can be used in the same file. 4789ba0eb85SBram Moolenaar Doing on-the-fly conversion is not always possible (would require iconv 4799ba0eb85SBram Moolenaar support). 480402d2feaSBram Moolenaar- For the programs and libraries: Using them as-is would require installing 4819ba0eb85SBram Moolenaar them separately from Vim. That's mostly not impossible, but a drawback. 482402d2feaSBram Moolenaar- Performance: A few tests showed that it's possible to check spelling on the 483402d2feaSBram Moolenaar fly (while redrawing), just like syntax highlighting. But the mechanisms 4844770d09aSBram Moolenaar used by other code are much slower. Myspell uses a hashtable, for example. 4854770d09aSBram Moolenaar The affix compression that most spell checkers use makes it slower too. 48651485f06SBram Moolenaar- For using an external program like aspell a communication mechanism would 48751485f06SBram Moolenaar have to be setup. That's complicated to do in a portable way (Unix-only 48851485f06SBram Moolenaar would be relatively simple, but that's not good enough). And performance 48951485f06SBram Moolenaar will become a problem (lots of process switching involved). 490402d2feaSBram Moolenaar- Missing support for words with non-word characters, such as "Etten-Leur" and 491402d2feaSBram Moolenaar "et al.", would require marking the pieces of them OK, lowering the 492402d2feaSBram Moolenaar reliability. 493402d2feaSBram Moolenaar- Missing support for regions or dialects. Makes it difficult to accept 494402d2feaSBram Moolenaar all English words and highlight non-Canadian words differently. 495402d2feaSBram Moolenaar- Missing support for rare words. Many words are correct but hardly ever used 496402d2feaSBram Moolenaar and could be a misspelled often-used word. 4979ba0eb85SBram Moolenaar- For making suggestions the speed is less important and requiring to install 4989ba0eb85SBram Moolenaar another program or library would be acceptable. But the word lists probably 4999ba0eb85SBram Moolenaar differ, the suggestions may be wrong words. 500071d4279SBram Moolenaar 5014770d09aSBram Moolenaar 5024770d09aSBram MoolenaarSpelling suggestions *develop-spell-suggestions* 5034770d09aSBram Moolenaar 5044770d09aSBram MoolenaarFor making suggestions there are two basic mechanisms: 5054770d09aSBram Moolenaar1. Try changing the bad word a little bit and check for a match with a good 5064770d09aSBram Moolenaar word. Or go through the list of good words, change them a little bit and 5074770d09aSBram Moolenaar check for a match with the bad word. The changes are deleting a character, 5084770d09aSBram Moolenaar inserting a character, swapping two characters, etc. 5094770d09aSBram Moolenaar2. Perform soundfolding on both the bad word and the good words and then find 5104770d09aSBram Moolenaar matches, possibly with a few changes like with the first mechanism. 5114770d09aSBram Moolenaar 5124770d09aSBram MoolenaarThe first is good for finding typing mistakes. After experimenting with 5134770d09aSBram Moolenaarhashtables and looking at solutions from other spell checkers the conclusion 5144770d09aSBram Moolenaarwas that a trie (a kind of tree structure) is ideal for this. Both for 5154770d09aSBram Moolenaarreducing memory use and being able to try sensible changes. For example, when 5164770d09aSBram Moolenaarinserting a character only characters that lead to good words need to be 5174770d09aSBram Moolenaartried. Other mechanisms (with hashtables) need to try all possible letters at 5184770d09aSBram Moolenaarevery position in the word. Also, a hashtable has the requirement that word 5194770d09aSBram Moolenaarboundaries are identified separately, while a trie does not require this. 5204770d09aSBram MoolenaarThat makes the mechanism a lot simpler. 5214770d09aSBram Moolenaar 5224770d09aSBram MoolenaarSoundfolding is useful when someone knows how the words sounds but doesn't 5234770d09aSBram Moolenaarknow how it is spelled. For example, the word "dictionary" might be written 5244770d09aSBram Moolenaaras "daktonerie". The number of changes that the first method would need to 5254770d09aSBram Moolenaartry is very big, it's hard to find the good word that way. After soundfolding 5264770d09aSBram Moolenaarthe words become "tktnr" and "tkxnry", these differ by only two letters. 5274770d09aSBram Moolenaar 5284770d09aSBram MoolenaarTo find words by their soundfolded equivalent (soundalike word) we need a list 5294770d09aSBram Moolenaarof all soundfolded words. A few experiments have been done to find out what 5304770d09aSBram Moolenaarthe best method is. Alternatives: 5314770d09aSBram Moolenaar1. Do the sound folding on the fly when looking for suggestions. This means 5324770d09aSBram Moolenaar walking through the trie of good words, soundfolding each word and 5334770d09aSBram Moolenaar checking how different it is from the bad word. This is very efficient for 5344770d09aSBram Moolenaar memory use, but takes a long time. On a fast PC it takes a couple of 5354770d09aSBram Moolenaar seconds for English, which can be acceptable for interactive use. But for 5364770d09aSBram Moolenaar some languages it takes more than ten seconds (e.g., German, Catalan), 537088e8e34SBram Moolenaar which is unacceptably slow. For batch processing (automatic corrections) 53882038d73SBram Moolenaar it's too slow for all languages. 5394770d09aSBram Moolenaar2. Use a trie for the soundfolded words, so that searching can be done just 5404770d09aSBram Moolenaar like how it works without soundfolding. This requires remembering a list 5414770d09aSBram Moolenaar of good words for each soundfolded word. This makes finding matches very 5424770d09aSBram Moolenaar fast but requires quite a lot of memory, in the order of 1 to 10 Mbyte. 5434770d09aSBram Moolenaar For some languages more than the original word list. 5444770d09aSBram Moolenaar3. Like the second alternative, but reduce the amount of memory by using affix 5454770d09aSBram Moolenaar compression and store only the soundfolded basic word. This is what Aspell 5464770d09aSBram Moolenaar does. Disadvantage is that affixes need to be stripped from the bad word 5474770d09aSBram Moolenaar before soundfolding it, which means that mistakes at the start and/or end 5484770d09aSBram Moolenaar of the word will cause the mechanism to fail. Also, this becomes slow when 5494770d09aSBram Moolenaar the bad word is quite different from the good word. 5504770d09aSBram Moolenaar 5514770d09aSBram MoolenaarThe choice made is to use the second mechanism and use a separate file. This 5524770d09aSBram Moolenaarway a user with sufficient memory can get very good suggestions while a user 5534770d09aSBram Moolenaarwho is short of memory or just wants the spell checking and no suggestions 5544770d09aSBram Moolenaardoesn't use so much memory. 5554770d09aSBram Moolenaar 5564770d09aSBram Moolenaar 5574770d09aSBram MoolenaarWord frequency 5584770d09aSBram Moolenaar 5594770d09aSBram MoolenaarFor sorting suggestions it helps to know which words are common. In theory we 5604770d09aSBram Moolenaarcould store a word frequency with the word in the dictionary. However, this 5614770d09aSBram Moolenaarrequires storing a count per word. That degrades word tree compression a lot. 5624770d09aSBram MoolenaarAnd maintaining the word frequency for all languages will be a heavy task. 5634770d09aSBram MoolenaarAlso, it would be nice to prefer words that are already in the text. This way 5644770d09aSBram Moolenaarthe words that appear in the specific text are preferred for suggestions. 5654770d09aSBram Moolenaar 5664770d09aSBram MoolenaarWhat has been implemented is to count words that have been seen during 5674770d09aSBram Moolenaardisplaying. A hashtable is used to quickly find the word count. The count is 5684770d09aSBram Moolenaarinitialized from words listed in COMMON items in the affix file, so that it 5694770d09aSBram Moolenaaralso works when starting a new file. 5704770d09aSBram Moolenaar 5714770d09aSBram MoolenaarThis isn't ideal, because the longer Vim is running the higher the counts 57282038d73SBram Moolenaarbecome. But in practice it is a noticeable improvement over not using the word 5734770d09aSBram Moolenaarcount. 5744770d09aSBram Moolenaar 575071d4279SBram Moolenaar============================================================================== 576071d4279SBram Moolenaar4. Assumptions *design-assumptions* 577071d4279SBram Moolenaar 578071d4279SBram MoolenaarSize of variables: 579071d4279SBram Moolenaarchar 8 bit signed 580071d4279SBram Moolenaarchar_u 8 bit unsigned 5814770d09aSBram Moolenaarint 32 or 64 bit signed (16 might be possible with limited features) 5824770d09aSBram Moolenaarunsigned 32 or 64 bit unsigned (16 as with ints) 583071d4279SBram Moolenaarlong 32 or 64 bit signed, can hold a pointer 584071d4279SBram Moolenaar 585071d4279SBram MoolenaarNote that some compilers cannot handle long lines or strings. The C89 586071d4279SBram Moolenaarstandard specifies a limit of 509 characters. 587071d4279SBram Moolenaar 58891f84f6eSBram Moolenaar vim:tw=78:ts=8:noet:ft=help:norl: 589