1*develop.txt* For Vim version 7.0aa. Last change: 2005 Jun 13 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! 127- Porting to another platform should be made easy, without having to change 128 too much platform-independent code. 129- Use the object-oriented spirit: Put data and code together. Minimize the 130 knowledge spread to other parts of the code. 131 132 133VIM IS... FLEXIBLE *design-flexible* 134 135Vim should make it easy for users to work in their preferred styles rather 136than coercing its users into particular patterns of work. This can be for 137items with a large impact (e.g., the 'compatible' option) or for details. The 138defaults are carefully chosen such that most users will enjoy using Vim as it 139is. Commands and options can be used to adjust Vim to the desire of the user 140and its environment. 141 142 143VIM IS... NOT *design-not* 144 145- Vim is not a shell or an Operating System. You will not be able to run a 146 shell inside Vim or use it to control a debugger. This should work the 147 other way around: Use Vim as a component from a shell or in an IDE. 148 A satirical way to say this: "Unlike Emacs, Vim does not attempt to include 149 everything but the kitchen sink, but some people say that you can clean one 150 with it. ;-)" 151- Vim is not a fancy GUI editor that tries to look nice at the cost of 152 being less consistent over all platforms. But functional GUI features are 153 welcomed. 154 155============================================================================== 1562. Coding style *coding-style* 157 158These are the rules to use when making changes to the Vim source code. Please 159stick to these rules, to keep the sources readable and maintainable. 160 161This list is not complete. Look in the source code for more examples. 162 163 164MAKING CHANGES *style-changes* 165 166The basic steps to make changes to the code: 1671. Adjust the documentation. Doing this first gives you an impression of how 168 your changes affect the user. 1692. Make the source code changes. 1703. Check ../doc/todo.txt if the change affects any listed item. 1714. Make a patch with "diff -c" against the unmodified code and docs. 1725. Make a note about what changed and include it with the patch. 173 174 175USE OF COMMON FUNCTIONS *style-functions* 176 177Some functions that are common to use, have a special Vim version. Always 178consider using the Vim version, because they were introduced with a reason. 179 180NORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION 181free() vim_free() Checks for freeing NULL 182malloc() alloc() Checks for out of memory situation 183malloc() lalloc() Like alloc(), but has long argument 184strcpy() STRCPY() Includes cast to (char *), for char_u * args 185strchr() vim_strchr() Accepts special characters 186strrchr() vim_strrchr() Accepts special characters 187isspace() vim_isspace() Can handle characters > 128 188iswhite() vim_iswhite() Only TRUE for Tab and space 189memcpy() vim_memmove() Handles overlapped copies 190bcopy() vim_memmove() Handles overlapped copies 191memset() vim_memset() Uniform for all systems 192 193 194NAMES *style-names* 195 196Function names can not be more than 31 characters long (because of VMS). 197 198Don't use "delete" as a variable name, C++ doesn't like it. 199 200Because of the requirement that Vim runs on as many systems as possible, we 201need to avoid using names that are already defined by the system. This is a 202list of names that are known to cause trouble. The name is given as a regexp 203pattern. 204 205is.*() POSIX, ctype.h 206to.*() POSIX, ctype.h 207 208d_.* POSIX, dirent.h 209l_.* POSIX, fcntl.h 210gr_.* POSIX, grp.h 211pw_.* POSIX, pwd.h 212sa_.* POSIX, signal.h 213mem.* POSIX, string.h 214str.* POSIX, string.h 215wcs.* POSIX, string.h 216st_.* POSIX, stat.h 217tms_.* POSIX, times.h 218tm_.* POSIX, time.h 219c_.* POSIX, termios.h 220MAX.* POSIX, limits.h 221__.* POSIX, system 222_[A-Z].* POSIX, system 223E[A-Z0-9]* POSIX, errno.h 224 225*_t POSIX, for typedefs. Use *_T instead. 226 227wait don't use as argument to a function, conflicts with types.h 228index shadows global declaration 229time shadows global declaration 230new C++ reserved keyword 231try Borland C++ doesn't like it to be used as a variable. 232 233basename() GNU string function 234dirname() GNU string function 235get_env_value() Linux system function 236 237 238VARIOUS *style-various* 239 240Typedef'ed names should end in "_t": > 241 typedef int some_t; 242Define'ed names should be uppercase: > 243 #define SOME_THING 244Features always start with "FEAT_": > 245 #define FEAT_FOO 246 247Don't use '\"', some compilers can't handle it. '"' works fine. 248 249Don't use: 250 #if HAVE_SOME 251Some compilers can't handle that and complain that "HAVE_SOME" is not defined. 252Use 253 #ifdef HAVE_SOME 254or 255 #if defined(HAVE_SOME) 256 257 258STYLE *style-examples* 259 260General rule: One statement per line. 261 262Wrong: if (cond) a = 1; 263 264OK: if (cond) 265 a = 1; 266 267Wrong: while (cond); 268 269OK: while (cond) 270 ; 271 272Wrong: do a = 1; while (cond); 273 274OK: do 275 a = 1; 276 while (cond); 277 278 279Functions start with: 280 281Wrong: int function_name(int arg1, int arg2) 282 283OK: /* 284 * Explanation of what this function is used for. 285 * 286 * Return value explanation. 287 */ 288 int 289 function_name(arg1, arg2) 290 int arg1; /* short comment about arg1 */ 291 int arg2; /* short comment about arg2 */ 292 { 293 int local; /* comment about local */ 294 295 local = arg1 * arg2; 296 297NOTE: Don't use ANSI style function declarations. A few people still have to 298use a compiler that doesn't support it. 299 300 301SPACES AND PUNCTUATION *style-spaces* 302 303No space between a function name and the bracket: 304 305Wrong: func (arg); 306OK: func(arg); 307 308Do use a space after if, while, switch, etc. 309 310Wrong: if(arg) for(;;) 311OK: if (arg) for (;;) 312 313Use a space after a comma and semicolon: 314 315Wrong: func(arg1,arg2); for (i = 0;i < 2;++i) 316OK: func(arg1, arg2); for (i = 0; i < 2; ++i) 317 318Use a space before and after '=', '+', '/', etc. 319 320Wrong: var=a*5; 321OK: var = a * 5; 322 323In general: Use empty lines to group lines of code together. Put a comment 324just above the group of lines. This makes it more easy to quickly see what is 325being done. 326 327OK: /* Prepare for building the table. */ 328 get_first_item(); 329 table_idx = 0; 330 331 /* Build the table */ 332 while (has_item()) 333 table[table_idx++] = next_item(); 334 335 /* Finish up. */ 336 cleanup_items(); 337 generate_hash(table); 338 339============================================================================== 3403. Design decisions *design-decisions* 341 342Folding 343 344Several forms of folding should be possible for the same buffer. For example, 345have one window that shows the text with function bodies folded, another 346window that shows a function body. 347 348Folding is a way to display the text. It should not change the text itself. 349Therefore the folding has been implemented as a filter between the text stored 350in a buffer (buffer lines) and the text displayed in a window (logical lines). 351 352 353Naming the window 354 355The word "window" is commonly used for several things: A window on the screen, 356the xterm window, a window inside Vim to view a buffer. 357To avoid confusion, other items that are sometimes called window have been 358given another name. Here is an overview of the related items: 359 360screen The whole display. For the GUI it's something like 1024x768 361 pixels. The Vim shell can use the whole screen or part of it. 362shell The Vim application. This can cover the whole screen (e.g., 363 when running in a console) or part of it (xterm or GUI). 364window View on a buffer. There can be several windows in Vim, 365 together with the command line, menubar, toolbar, etc. they 366 fit in the shell. 367 368 369Spell checking *develop-spell* 370 371When spell checking was going to be added to Vim a survey was done over the 372available spell checking libraries and programs. Unfortunately, the result 373was that none of them provided sufficient capabilities to be used as the spell 374checking engine in Vim, for various reasons: 375 376- Missing support for multi-byte encodings. At least UTF-8 must be supported, 377 so that more than one language can be used in the same file. 378 Doing on-the-fly conversion is not always possible (would require iconv 379 support). 380- For the programs and libraries: Using them as-is would require installing 381 them separately from Vim. That's mostly not impossible, but a drawback. 382- Performance: A few tests showed that it's possible to check spelling on the 383 fly (while redrawing), just like syntax highlighting. But the mechanisms 384 used by other code are much slower. Myspell uses a simplistic hashtable, 385 for example. 386- For using an external program like aspell a communication mechanism would 387 have to be setup. That's complicated to do in a portable way (Unix-only 388 would be relatively simple, but that's not good enough). And performance 389 will become a problem (lots of process switching involved). 390- Missing support for words with non-word characters, such as "Etten-Leur" and 391 "et al.", would require marking the pieces of them OK, lowering the 392 reliability. 393- Missing support for regions or dialects. Makes it difficult to accept 394 all English words and highlight non-Canadian words differently. 395- Missing support for rare words. Many words are correct but hardly ever used 396 and could be a misspelled often-used word. 397- For making suggestions the speed is less important and requiring to install 398 another program or library would be acceptable. But the word lists probably 399 differ, the suggestions may be wrong words. 400 401============================================================================== 4024. Assumptions *design-assumptions* 403 404Size of variables: 405char 8 bit signed 406char_u 8 bit unsigned 407int 16, 32 or 64 bit signed 408unsigned 16, 32 or 64 bit unsigned 409long 32 or 64 bit signed, can hold a pointer 410 411Note that some compilers cannot handle long lines or strings. The C89 412standard specifies a limit of 509 characters. 413 414 vim:tw=78:ts=8:ft=help:norl: 415