develop.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. *develop.txt* Nvim
  2. NVIM REFERENCE MANUAL
  3. Development of Nvim *development*
  4. This reference describes design constraints and guidelines, for developing
  5. Nvim applications or Nvim itself.
  6. Architecture and internal concepts are covered in src/nvim/README.md
  7. Nvim is free and open source. Everybody is encouraged to contribute.
  8. https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md
  9. Type |gO| to see the table of contents.
  10. ==============================================================================
  11. Design goals *design-goals*
  12. Most important things come first (roughly). Some items conflict; this is
  13. intentional. A balance must be found.
  14. NVIM IS... IMPROVED *design-improved*
  15. The Neo bits of Nvim should make it a better Vim, without becoming a
  16. completely different editor.
  17. - In matters of taste, prefer Vim/Unix tradition. If there is no relevant
  18. Vim/Unix tradition, consider the "common case".
  19. - A feature that people do not know about is a useless feature. Don't add
  20. obscure features, or at least add hints in documentation that they exist.
  21. - There is no limit to the features that can be added. Selecting new features
  22. is based on (1) what users ask for, (2) how much effort it takes to
  23. implement and (3) someone actually implementing it.
  24. - Backwards compatibility is a feature. The RPC API in particular should
  25. never break.
  26. NVIM IS... WELL DOCUMENTED *design-documented*
  27. - A feature that isn't documented is a useless feature. A patch for a new
  28. feature must include the documentation.
  29. - Documentation should be comprehensive and understandable. Use examples.
  30. - Don't make the text unnecessarily long. Less documentation means that an
  31. item is easier to find.
  32. NVIM IS... HIGH SPEED AND SMALL IN SIZE *design-speed-size*
  33. Keep Nvim small and fast.
  34. - Computers are becoming faster and bigger each year. Vim can grow too, but
  35. no faster than computers are growing. Keep Vim usable on older systems.
  36. - Many users start Vim from a shell very often. Startup time must be short.
  37. - Commands must work efficiently. The time they consume must be as small as
  38. possible. Useful commands may take longer.
  39. - Don't forget that some people use Vim over a slow connection. Minimize the
  40. communication overhead.
  41. - Vim is a component among other components. Don't turn it into a massive
  42. application, but have it work well together with other programs.
  43. NVIM IS... MAINTAINABLE *design-maintain*
  44. - The source code should not become a mess. It should be reliable code.
  45. - Use comments in a useful way! Quoting the function name and argument names
  46. is NOT useful. Do explain what they are for.
  47. - Porting to another platform should be made easy, without having to change
  48. too much platform-independent code.
  49. - Use the object-oriented spirit: Put data and code together. Minimize the
  50. knowledge spread to other parts of the code.
  51. NVIM IS... NOT *design-not*
  52. Nvim is not an operating system; instead it should be composed with other
  53. tools or hosted as a component. Marvim once said: "Unlike Emacs, Nvim does not
  54. include the kitchen sink... but it's good for plumbing."
  55. ==============================================================================
  56. Developer guidelines *dev*
  57. PROVIDERS *dev-provider*
  58. A goal of Nvim is to allow extension of the editor without special knowledge
  59. in the core. But some Vim components are too tightly coupled; in those cases
  60. a "provider" hook is exposed.
  61. Consider two examples of integration with external systems that are
  62. implemented in Vim and are now decoupled from Nvim core as providers:
  63. 1. In the Vim source code, clipboard logic accounts for more than 1k lines of
  64. C source code (ui.c), to perform two tasks that are now accomplished with
  65. shell commands such as xclip or pbcopy/pbpaste.
  66. 2. Python scripting support: Vim has three files dedicated to embedding the
  67. Python interpreter: if_python.c, if_python3.c and if_py_both.h. Together
  68. these files sum about 9.5k lines of C source code. In contrast, Nvim Python
  69. scripting is performed by an external host process implemented in ~2k lines
  70. of Python.
  71. Ideally we could implement Python and clipboard integration in pure vimscript
  72. and without touching the C code. But this is infeasible without compromising
  73. backwards compatibility with Vim; that's where providers help.
  74. The provider framework helps call vimscript from C. It is composed of two
  75. functions in eval.c:
  76. - eval_call_provider(name, method, arguments): calls provider#(name)#Call
  77. with the method and arguments.
  78. - eval_has_provider(name): Checks if a provider is implemented. Returns true
  79. if the provider#(name)#Call function is implemented. Called by |has()|
  80. (vimscript) to check if features are available.
  81. The provider#(name)#Call function implements integration with an external
  82. system, because shell commands and |RPC| clients are easier to work with in
  83. vimscript.
  84. For example, the Python provider is implemented by the
  85. autoload/provider/python.vim script; the provider#python#Call function is only
  86. defined if a valid external Python host is found. That works well with the
  87. `has('python')` expression (normally used by Python plugins) because if the
  88. Python host isn't installed then the plugin will "think" it is running in
  89. a Vim compiled without the |+python| feature.
  90. DOCUMENTATION *dev-doc*
  91. - Do not prefix help tags with "nvim-". Use |vim_diff.txt| to document
  92. differences from Vim; no other distinction is necessary.
  93. - If a Vim feature is removed, delete its help section and move its tag to
  94. |vim_diff.txt|.
  95. - Move deprecated features to |deprecated.txt|.
  96. - Use consistent language.
  97. - "terminal" in a help tag always means "the embedded terminal emulator", not
  98. "the user host terminal".
  99. - Use "tui-" to prefix help tags related to the host terminal, and "TUI"
  100. in prose if possible.
  101. - Docstrings: do not start parameter descriptions with "The" or "A" unless it
  102. is critical to avoid ambiguity.
  103. GOOD: >
  104. /// @param dirname Path fragment before `pend`
  105. < BAD: >
  106. /// @param dirname The path fragment before `pend`
  107. <
  108. API *dev-api*
  109. Use this template to name new API functions:
  110. nvim_{thing}_{action}_{arbitrary-qualifiers}
  111. If the function acts on an object then {thing} is the name of that object
  112. (e.g. "buf" or "win"). If the function operates in a "global" context then
  113. {thing} is usually omitted (but consider "namespacing" your global operations
  114. with a {thing} that groups functions under a common concept).
  115. Use existing common {action} names if possible:
  116. add Append to, or insert into, a collection
  117. get Get a thing (or subset of things by some query)
  118. set Set a thing
  119. del Delete a thing (or group of things)
  120. list Get all things
  121. Use consistent names for {thing} in all API functions. E.g. a buffer is called
  122. "buf" everywhere, not "buffer" in some places and "buf" in others.
  123. Example: `nvim_get_current_line` acts on the global editor state; the common
  124. {action} "get" is used but {thing} is omitted.
  125. Example: `nvim_buf_add_highlight` acts on a `Buffer` object (the first
  126. parameter) and uses the common {action} "add".
  127. Example: `nvim_list_bufs` operates in a global context (first parameter is
  128. _not_ a Buffer). The common {action} "list" indicates that it lists all
  129. bufs (plural) in the global context.
  130. Use this template to name new API events:
  131. nvim_{thing}_{event}_event
  132. Example: `nvim_buf_changedtick_event`.
  133. API-CLIENT *dev-api-client*
  134. Standard Features ~
  135. - Clients should call |nvim_set_client_info()| after connecting, so users and
  136. plugins can detect the client by handling the |ChanInfo| event. This
  137. avoids the need for special variables or other client hints.
  138. - Clients should handle |nvim_error_event| notifications, which will be sent
  139. if an async request to nvim was rejected or caused an error.
  140. Package Naming ~
  141. API client packages should NOT be named something ambiguous like "neovim" or
  142. "python-client". Use "nvim" as a prefix/suffix to some other identifier
  143. following ecosystem conventions.
  144. For example, Python packages tend to have "py" in the name, so "pynvim" is
  145. a good name: it's idiomatic and unambiguous. If the package is named "neovim",
  146. it confuses users, and complicates documentation and discussions.
  147. Examples of API-client package names:
  148. GOOD: nvim-racket
  149. GOOD: pynvim
  150. BAD: python-client
  151. BAD: neovim
  152. Implementation ~
  153. Consider using libmpack instead of the msgpack.org C/C++ library. libmpack is
  154. small (can be inlined into your C/C++ project) and efficient (no allocations).
  155. It also implements msgpack-RPC.
  156. https://github.com/libmpack/libmpack/
  157. EXTERNAL UI *dev-ui*
  158. External UIs should be aware of the |api-contract|. In particular, future
  159. versions of Nvim may add new items to existing events. The API is strongly
  160. backwards-compatible, but clients must not break if new (optional) fields are
  161. added to existing events.
  162. Standard Features ~
  163. External UIs are expected to implement these common features:
  164. - Call |nvim_set_client_info()| after connecting, so users and plugins can
  165. detect the UI by handling the |ChanInfo| event. This avoids the need for
  166. special variables and UI-specific config files (gvimrc, macvimrc, …).
  167. - Cursor style (shape, color) should conform to the 'guicursor' properties
  168. delivered with the mode_info_set UI event.
  169. - Send the ALT/META ("Option" on macOS) key as a |<M-| chord.
  170. - Send the "super" key (Windows key, Apple key) as a |<D-| chord.
  171. - Avoid mappings that conflict with the Nvim keymap-space; GUIs have many new
  172. chords (<C-,> <C-Enter> <C-S-x> <D-x>) and patterns ("shift shift") that do
  173. not potentially conflict with Nvim defaults, plugins, etc.
  174. - Consider the "option_set" |ui-global| event as a hint for other GUI
  175. behaviors. UI-related options ('guifont', 'ambiwidth', …) are published in
  176. this event.
  177. vim:tw=78:ts=8:noet:ft=help:norl: