develop.txt 23 KB

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