Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 1 | *develop.txt* For Vim version 9.1. Last change: 2025 May 05 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Development of Vim. *development* |
| 8 | |
| 9 | This text is important for those who want to be involved in further developing |
| 10 | Vim. |
| 11 | |
| 12 | 1. Design goals |design-goals| |
| 13 | 2. Coding style |coding-style| |
| 14 | 3. Design decisions |design-decisions| |
| 15 | 4. Assumptions |design-assumptions| |
| 16 | |
| 17 | See the file README.txt in the "src" directory for an overview of the source |
| 18 | code. |
| 19 | |
| 20 | Vim is open source software. Everybody is encouraged to contribute to help |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 21 | improving Vim. For sending patches a unified diff "diff -u" is preferred. |
| 22 | You can create a pull request on github, but it's not required. |
Bram Moolenaar | 531da59 | 2013-05-06 05:58:55 +0200 | [diff] [blame] | 23 | Also see http://vim.wikia.com/wiki/How_to_make_and_submit_a_patch. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | |
| 25 | ============================================================================== |
| 26 | 1. Design goals *design-goals* |
| 27 | |
| 28 | Most important things come first (roughly). |
| 29 | |
| 30 | Note that quite a few items are contradicting. This is intentional. A |
| 31 | balance must be found between them. |
| 32 | |
| 33 | |
| 34 | VIM IS... VI COMPATIBLE *design-compatible* |
| 35 | |
| 36 | First of all, it should be possible to use Vim as a drop-in replacement for |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 37 | Vi. When the user wants to, Vim can be used in compatible mode and hardly |
| 38 | any differences with the original Vi will be noticed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | |
| 40 | Exceptions: |
| 41 | - We don't reproduce obvious Vi bugs in Vim. |
| 42 | - There are different versions of Vi. I am using Version 3.7 (6/7/85) as a |
| 43 | reference. But support for other versions is also included when possible. |
| 44 | The Vi part of POSIX is not considered a definitive source. |
| 45 | - Vim adds new commands, you cannot rely on some command to fail because it |
| 46 | didn't exist in Vi. |
| 47 | - Vim will have a lot of features that Vi doesn't have. Going back from Vim |
| 48 | to Vi will be a problem, this cannot be avoided. |
| 49 | - Some things are hardly ever used (open mode, sending an e-mail when |
| 50 | crashing, etc.). Those will only be included when someone has a good reason |
| 51 | why it should be included and it's not too much work. |
| 52 | - For some items it is debatable whether Vi compatibility should be |
| 53 | maintained. There will be an option flag for these. |
| 54 | |
| 55 | |
| 56 | VIM IS... IMPROVED *design-improved* |
| 57 | |
| 58 | The IMproved bits of Vim should make it a better Vi, without becoming a |
| 59 | completely different editor. Extensions are done with a "Vi spirit". |
| 60 | - Use the keyboard as much as feasible. The mouse requires a third hand, |
| 61 | which we don't have. Many terminals don't have a mouse. |
| 62 | - When the mouse is used anyway, avoid the need to switch back to the |
| 63 | keyboard. Avoid mixing mouse and keyboard handling. |
| 64 | - Add commands and options in a consistent way. Otherwise people will have a |
| 65 | hard time finding and remembering them. Keep in mind that more commands and |
| 66 | options will be added later. |
| 67 | - A feature that people do not know about is a useless feature. Don't add |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 68 | obscure features, or at least add hints in documentation that they exist. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | - Minimize using CTRL and other modifiers, they are more difficult to type. |
| 70 | - There are many first-time and inexperienced Vim users. Make it easy for |
| 71 | them to start using Vim and learn more over time. |
| 72 | - There is no limit to the features that can be added. Selecting new features |
| 73 | is one based on (1) what users ask for, (2) how much effort it takes to |
| 74 | implement and (3) someone actually implementing it. |
| 75 | |
| 76 | |
| 77 | VIM IS... MULTI PLATFORM *design-multi-platform* |
| 78 | |
| 79 | Vim tries to help as many users on as many platforms as possible. |
| 80 | - Support many kinds of terminals. The minimal demands are cursor positioning |
| 81 | and clear-screen. Commands should only use key strokes that most keyboards |
| 82 | have. Support all the keys on the keyboard for mapping. |
| 83 | - Support many platforms. A condition is that there is someone willing to do |
| 84 | Vim development on that platform, and it doesn't mean messing up the code. |
| 85 | - Support many compilers and libraries. Not everybody is able or allowed to |
| 86 | install another compiler or GUI library. |
| 87 | - People switch from one platform to another, and from GUI to terminal |
| 88 | version. Features should be present in all versions, or at least in as many |
| 89 | as possible with a reasonable effort. Try to avoid that users must switch |
| 90 | between platforms to accomplish their work efficiently. |
| 91 | - That a feature is not possible on some platforms, or only possible on one |
| 92 | platform, does not mean it cannot be implemented. [This intentionally |
| 93 | contradicts the previous item, these two must be balanced.] |
| 94 | |
| 95 | |
| 96 | VIM IS... WELL DOCUMENTED *design-documented* |
| 97 | |
| 98 | - A feature that isn't documented is a useless feature. A patch for a new |
| 99 | feature must include the documentation. |
| 100 | - Documentation should be comprehensive and understandable. Using examples is |
| 101 | recommended. |
| 102 | - Don't make the text unnecessarily long. Less documentation means that an |
| 103 | item is easier to find. |
| 104 | |
| 105 | |
| 106 | VIM IS... HIGH SPEED AND SMALL IN SIZE *design-speed-size* |
| 107 | |
| 108 | Using Vim must not be a big attack on system resources. Keep it small and |
| 109 | fast. |
| 110 | - Computers are becoming faster and bigger each year. Vim can grow too, but |
| 111 | no faster than computers are growing. Keep Vim usable on older systems. |
| 112 | - Many users start Vim from a shell very often. Startup time must be short. |
| 113 | - Commands must work efficiently. The time they consume must be as small as |
| 114 | possible. Useful commands may take longer. |
| 115 | - Don't forget that some people use Vim over a slow connection. Minimize the |
| 116 | communication overhead. |
| 117 | - Items that add considerably to the size and are not used by many people |
| 118 | should be a feature that can be disabled. |
| 119 | - Vim is a component among other components. Don't turn it into a massive |
| 120 | application, but have it work well together with other programs. |
| 121 | |
| 122 | |
| 123 | VIM IS... MAINTAINABLE *design-maintain* |
| 124 | |
| 125 | - The source code should not become a mess. It should be reliable code. |
| 126 | - Use the same layout in all files to make it easy to read |coding-style|. |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 127 | - Use comments in a useful way! Quoting the function name and argument names |
| 128 | is NOT useful. Do explain what they are for. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | - Porting to another platform should be made easy, without having to change |
| 130 | too much platform-independent code. |
| 131 | - Use the object-oriented spirit: Put data and code together. Minimize the |
| 132 | knowledge spread to other parts of the code. |
| 133 | |
| 134 | |
| 135 | VIM IS... FLEXIBLE *design-flexible* |
| 136 | |
| 137 | Vim should make it easy for users to work in their preferred styles rather |
| 138 | than coercing its users into particular patterns of work. This can be for |
| 139 | items with a large impact (e.g., the 'compatible' option) or for details. The |
| 140 | defaults are carefully chosen such that most users will enjoy using Vim as it |
| 141 | is. Commands and options can be used to adjust Vim to the desire of the user |
| 142 | and its environment. |
| 143 | |
| 144 | |
| 145 | VIM IS... NOT *design-not* |
| 146 | |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 147 | - Vim is not a shell or an Operating System. It does provide a terminal |
| 148 | window, in which you can run a shell or debugger. E.g. to be able to do |
| 149 | this over an ssh connection. But if you don't need a text editor with that |
| 150 | it is out of scope (use something like screen or tmux instead). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | A satirical way to say this: "Unlike Emacs, Vim does not attempt to include |
| 152 | everything but the kitchen sink, but some people say that you can clean one |
| 153 | with it. ;-)" |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 154 | To use Vim with gdb see |terminal-debugger|. Other (older) tools can be |
Christian Brabandt | 1c5728e | 2024-05-11 11:12:40 +0200 | [diff] [blame] | 155 | found at http://www.agide.org (link seems dead) and http://clewn.sf.net. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | - Vim is not a fancy GUI editor that tries to look nice at the cost of |
| 157 | being less consistent over all platforms. But functional GUI features are |
| 158 | welcomed. |
| 159 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | ============================================================================== |
| 162 | 2. Coding style *coding-style* |
| 163 | |
| 164 | These are the rules to use when making changes to the Vim source code. Please |
| 165 | stick to these rules, to keep the sources readable and maintainable. |
| 166 | |
| 167 | This list is not complete. Look in the source code for more examples. |
| 168 | |
Luca Saccarola | feea1b4 | 2024-11-11 21:33:50 +0100 | [diff] [blame] | 169 | The code repository contains an editorconfig file, that can be used together |
| 170 | with the distributed editorconfig plugin |editorconfig-install| to ensure the |
| 171 | recommended style is followed. |
| 172 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | |
| 174 | MAKING CHANGES *style-changes* |
| 175 | |
| 176 | The basic steps to make changes to the code: |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 177 | 1. Get the code from github. That makes it easier to keep your changed |
| 178 | version in sync with the main code base (it may be a while before your |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 179 | changes will be included). |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 180 | 2. Adjust the documentation. Doing this first gives you an impression of how |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 181 | your changes affect the user. |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 182 | 3. Make the source code changes. |
| 183 | 4. Check ../doc/todo.txt if the change affects any listed item. |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 184 | 5. Add a test to src/testdir to verify the new behaviour and ensure it won't |
| 185 | regress in the future. |
| 186 | 6. Make a patch with "git diff". |
| 187 | 7. Make a note about what changed, preferably mentioning the problem and the |
Bram Moolenaar | 6856393 | 2017-01-10 13:31:15 +0100 | [diff] [blame] | 188 | solution. Send an email to the |vim-dev| maillist with an explanation and |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 189 | include the diff. |
| 190 | |
| 191 | For any non-trivial change, please always create a pull request on github, |
| 192 | since this triggers the test suite. |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 193 | |
Christian Brabandt | de6a313 | 2024-12-25 18:00:38 +0100 | [diff] [blame] | 194 | *style-clang-format* |
zeertzjq | 9880097 | 2025-04-18 10:57:33 +0200 | [diff] [blame] | 195 | sound.c and sign.c can be (semi-) automatically formatted using the |
Christian Brabandt | de6a313 | 2024-12-25 18:00:38 +0100 | [diff] [blame] | 196 | `clang-format` formatter according to the distributed .clang-format file. |
| 197 | Other source files do not yet correspond to the .clang-format file. This may |
| 198 | change in the future and they may be reformatted as well. |
| 199 | |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 200 | |
Bram Moolenaar | 15142e2 | 2018-04-30 22:19:58 +0200 | [diff] [blame] | 201 | C COMPILER *style-compiler* *ANSI-C* *C89* *C99* |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 202 | |
| 203 | The minimal C compiler version supported is C89, also known as ANSI C. |
Bram Moolenaar | 561f8a5 | 2018-04-17 22:02:45 +0200 | [diff] [blame] | 204 | Later standards, such as C99, are not widely supported, or at least not 100% |
Bram Moolenaar | 4cbdcbd | 2022-09-20 21:23:12 +0100 | [diff] [blame] | 205 | supported. Therefore we use only some of the C99 features and explicitly |
| 206 | disallow some (this will gradually be adjusted over time). |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 207 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 208 | Features not to be used ~ |
Bram Moolenaar | 561f8a5 | 2018-04-17 22:02:45 +0200 | [diff] [blame] | 209 | |
| 210 | These C99 features are not to be used, because not enough compilers support |
| 211 | them: |
Bram Moolenaar | 561f8a5 | 2018-04-17 22:02:45 +0200 | [diff] [blame] | 212 | - Variable length arrays (even in C11 this is an optional feature). |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 213 | - C99 _Bool and _Complex types. |
Bram Moolenaar | 561f8a5 | 2018-04-17 22:02:45 +0200 | [diff] [blame] | 214 | - "inline" (it's hardly ever needed, let the optimizer do its work) |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 215 | - flexible array members: Not supported by HP-UX C compiler (John Marriott) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | |
| 217 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 218 | COMMENTS *style-comments* |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 219 | |
| 220 | Try to avoid putting multiline comments inside a function body: if the |
| 221 | function is so complex that you need to separately comment parts of it, you |
| 222 | should probably rethink the structure of the function. |
| 223 | |
| 224 | For file headers and function descriptions use: > |
| 225 | /* |
| 226 | * Description |
| 227 | */ |
| 228 | < |
| 229 | For everything else use: > |
| 230 | // comment |
| 231 | < |
| 232 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 233 | INDENTATION *style-indentation* |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 234 | |
| 235 | We use 4 space to indent the code. If you are using Vim to edit the source, |
| 236 | you don't need to do anything due to the |modeline|. |
| 237 | |
| 238 | For other editors an `.editorconfig` is provided at the root of the repo. |
| 239 | |
| 240 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 241 | DECLARATIONS *style-declarations* |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 242 | |
| 243 | Declare, when possible, `for` loop variables in the guard: |
| 244 | OK: > |
| 245 | for (int i = 0; i < len; ++i) |
| 246 | < |
| 247 | Wrong: > |
| 248 | int i; |
| 249 | for (i = 0; i < len; ++i) |
| 250 | < |
| 251 | Always declare a variable with a default value: |
| 252 | OK: > |
| 253 | int n = 0; |
| 254 | int *ptr = NULL; |
| 255 | < |
| 256 | Wrong: > |
| 257 | int n; |
| 258 | int *ptr; |
| 259 | < |
| 260 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 261 | BRACES *style-braces* |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 262 | |
| 263 | All curly braces must be returned onto a new line: |
| 264 | OK: > |
| 265 | if (cond) |
| 266 | { |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 267 | cmd; |
| 268 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 269 | } |
| 270 | else |
| 271 | { |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 272 | cmd; |
| 273 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 274 | } |
| 275 | < |
| 276 | Wrong: > |
| 277 | if (cond) { |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 278 | cmd; |
| 279 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 280 | } else { |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 281 | cmd; |
| 282 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 283 | } |
| 284 | < |
| 285 | OK: > |
| 286 | while (cond) |
| 287 | { |
| 288 | cmd; |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 289 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 290 | } |
| 291 | < |
| 292 | Wrong: > |
| 293 | while (cond) { |
| 294 | cmd; |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 295 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 296 | } |
| 297 | < |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 298 | OK: > |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 299 | do |
| 300 | { |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 301 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 302 | cmd; |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 303 | } while (cond); |
| 304 | < |
| 305 | or > |
| 306 | do |
| 307 | { |
| 308 | cmd; |
| 309 | cmd; |
| 310 | } |
| 311 | while (cond); |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 312 | < |
| 313 | Wrong: > |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 314 | do { |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 315 | cmd; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 316 | cmd; |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 317 | } while (cond); |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 318 | < |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 319 | |
| 320 | TYPES *style-types* |
| 321 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 322 | Use descriptive types. These are defined in src/vim.h, src/structs.h etc. |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 323 | Note that all custom types are postfixed with "_T" |
| 324 | |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 325 | Example: > |
| 326 | linenr_T |
| 327 | buf_T |
| 328 | pos_T |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 329 | < |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 330 | |
| 331 | SPACES AND PUNCTUATION *style-spaces* |
| 332 | |
| 333 | No space between a function name and the bracket: |
| 334 | |
| 335 | OK: func(arg); |
| 336 | Wrong: func (arg); |
| 337 | |
| 338 | Do use a space after `if`, `while`, `switch`, etc. |
| 339 | |
| 340 | OK: if (arg) for (;;) |
| 341 | Wrong: if(arg) for(;;) |
| 342 | |
| 343 | Use a space after a comma or semicolon: |
| 344 | |
| 345 | OK: func(arg1, arg2); for (i = 0; i < 2; ++i) |
| 346 | Wrong: func(arg1,arg2); for (i = 0;i < 2;++i) |
| 347 | |
| 348 | Use a space before and after '=', '+', '/', etc. |
| 349 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 350 | OK: var = a * 5; |
Hirohito Higashi | 55f9e2b | 2025-05-05 20:19:09 +0200 | [diff] [blame] | 351 | Wrong: var=a*5; |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 352 | |
| 353 | Use empty lines to group similar actions together. |
| 354 | |
| 355 | OK: > |
| 356 | msg_puts_title(_("\n--- Signs ---")); |
| 357 | msg_putchar('\n'); |
| 358 | |
| 359 | if (rbuf == NULL) |
| 360 | buf = firstbuf; |
| 361 | else |
| 362 | buf = rbuf; |
| 363 | |
| 364 | while (buf != NULL && !got_int) |
| 365 | < |
| 366 | Wrong: > |
| 367 | msg_puts_title(_("\n--- Signs ---")); |
| 368 | msg_putchar('\n'); |
| 369 | if (rbuf == NULL) |
| 370 | buf = firstbuf; |
| 371 | else |
| 372 | buf = rbuf; |
| 373 | while (buf != NULL && !got_int) |
| 374 | < |
| 375 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 376 | FUNCTIONS *style-functions* |
| 377 | |
| 378 | Use function declarations with the return type on a separate indented line. |
| 379 | |
| 380 | OK: > |
| 381 | int |
| 382 | function_name(int arg1, int arg2) |
| 383 | { |
| 384 | } |
| 385 | < |
| 386 | Wrong: > |
| 387 | int function_name(int arg1, int arg2) |
| 388 | { |
| 389 | } |
| 390 | < |
| 391 | |
| 392 | Give meaningful names to function parameters. |
| 393 | |
| 394 | |
| 395 | USE OF COMMON FUNCTIONS *style-common-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 396 | |
| 397 | Some functions that are common to use, have a special Vim version. Always |
| 398 | consider using the Vim version, because they were introduced with a reason. |
| 399 | |
| 400 | NORMAL NAME VIM NAME DIFFERENCE OF VIM VERSION |
| 401 | free() vim_free() Checks for freeing NULL |
| 402 | malloc() alloc() Checks for out of memory situation |
| 403 | malloc() lalloc() Like alloc(), but has long argument |
| 404 | strcpy() STRCPY() Includes cast to (char *), for char_u * args |
| 405 | strchr() vim_strchr() Accepts special characters |
| 406 | strrchr() vim_strrchr() Accepts special characters |
| 407 | isspace() vim_isspace() Can handle characters > 128 |
Bram Moolenaar | 9e368db | 2007-05-12 13:25:01 +0000 | [diff] [blame] | 408 | iswhite() vim_iswhite() Only TRUE for tab and space |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 409 | memcpy() mch_memmove() Handles overlapped copies |
| 410 | bcopy() mch_memmove() Handles overlapped copies |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 411 | memset() vim_memset() Uniform for all systems |
| 412 | |
| 413 | |
| 414 | NAMES *style-names* |
| 415 | |
| 416 | Function names can not be more than 31 characters long (because of VMS). |
| 417 | |
Bram Moolenaar | 13d5aee | 2016-01-21 23:36:05 +0100 | [diff] [blame] | 418 | Don't use "delete" or "this" as a variable name, C++ doesn't like it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 419 | |
| 420 | Because of the requirement that Vim runs on as many systems as possible, we |
| 421 | need to avoid using names that are already defined by the system. This is a |
| 422 | list of names that are known to cause trouble. The name is given as a regexp |
| 423 | pattern. |
| 424 | |
| 425 | is.*() POSIX, ctype.h |
| 426 | to.*() POSIX, ctype.h |
| 427 | |
| 428 | d_.* POSIX, dirent.h |
| 429 | l_.* POSIX, fcntl.h |
| 430 | gr_.* POSIX, grp.h |
| 431 | pw_.* POSIX, pwd.h |
| 432 | sa_.* POSIX, signal.h |
| 433 | mem.* POSIX, string.h |
| 434 | str.* POSIX, string.h |
| 435 | wcs.* POSIX, string.h |
| 436 | st_.* POSIX, stat.h |
| 437 | tms_.* POSIX, times.h |
| 438 | tm_.* POSIX, time.h |
| 439 | c_.* POSIX, termios.h |
| 440 | MAX.* POSIX, limits.h |
| 441 | __.* POSIX, system |
| 442 | _[A-Z].* POSIX, system |
| 443 | E[A-Z0-9]* POSIX, errno.h |
| 444 | |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 445 | .*_t POSIX, for typedefs. Use .*_T instead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | |
| 447 | wait don't use as argument to a function, conflicts with types.h |
| 448 | index shadows global declaration |
| 449 | time shadows global declaration |
| 450 | new C++ reserved keyword |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | |
Bram Moolenaar | 6be7f87 | 2012-01-20 21:08:56 +0100 | [diff] [blame] | 452 | clear Mac curses.h |
| 453 | echo Mac curses.h |
| 454 | instr Mac curses.h |
| 455 | meta Mac curses.h |
| 456 | newwin Mac curses.h |
| 457 | nl Mac curses.h |
| 458 | overwrite Mac curses.h |
| 459 | refresh Mac curses.h |
| 460 | scroll Mac curses.h |
| 461 | typeahead Mac curses.h |
| 462 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 463 | basename() GNU string function |
| 464 | dirname() GNU string function |
| 465 | get_env_value() Linux system function |
| 466 | |
| 467 | |
| 468 | VARIOUS *style-various* |
| 469 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 470 | Define'd names should be uppercase: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 471 | #define SOME_THING |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 472 | < |
| 473 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 474 | Features always start with "FEAT_": > |
| 475 | #define FEAT_FOO |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 476 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | |
| 478 | Don't use '\"', some compilers can't handle it. '"' works fine. |
| 479 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 480 | Don't use: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | #if HAVE_SOME |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 482 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | Some compilers can't handle that and complain that "HAVE_SOME" is not defined. |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 484 | Use > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 485 | #ifdef HAVE_SOME |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 486 | < |
| 487 | or > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | #if defined(HAVE_SOME) |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 489 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | |
| 491 | STYLE *style-examples* |
| 492 | |
Luca Saccarola | 55adc5b | 2024-10-31 10:28:40 +0100 | [diff] [blame] | 493 | One statement per line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | |
| 495 | Wrong: if (cond) a = 1; |
| 496 | |
| 497 | OK: if (cond) |
| 498 | a = 1; |
| 499 | |
| 500 | Wrong: while (cond); |
| 501 | |
| 502 | OK: while (cond) |
| 503 | ; |
| 504 | |
| 505 | Wrong: do a = 1; while (cond); |
| 506 | |
| 507 | OK: do |
| 508 | a = 1; |
| 509 | while (cond); |
| 510 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | |
| 512 | ============================================================================== |
| 513 | 3. Design decisions *design-decisions* |
| 514 | |
| 515 | Folding |
| 516 | |
| 517 | Several forms of folding should be possible for the same buffer. For example, |
| 518 | have one window that shows the text with function bodies folded, another |
| 519 | window that shows a function body. |
| 520 | |
| 521 | Folding is a way to display the text. It should not change the text itself. |
| 522 | Therefore the folding has been implemented as a filter between the text stored |
| 523 | in a buffer (buffer lines) and the text displayed in a window (logical lines). |
| 524 | |
| 525 | |
| 526 | Naming the window |
| 527 | |
| 528 | The word "window" is commonly used for several things: A window on the screen, |
| 529 | the xterm window, a window inside Vim to view a buffer. |
| 530 | To avoid confusion, other items that are sometimes called window have been |
| 531 | given another name. Here is an overview of the related items: |
| 532 | |
| 533 | screen The whole display. For the GUI it's something like 1024x768 |
| 534 | pixels. The Vim shell can use the whole screen or part of it. |
| 535 | shell The Vim application. This can cover the whole screen (e.g., |
| 536 | when running in a console) or part of it (xterm or GUI). |
| 537 | window View on a buffer. There can be several windows in Vim, |
| 538 | together with the command line, menubar, toolbar, etc. they |
| 539 | fit in the shell. |
| 540 | |
| 541 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 542 | Spell checking *develop-spell* |
| 543 | |
| 544 | When spell checking was going to be added to Vim a survey was done over the |
| 545 | available spell checking libraries and programs. Unfortunately, the result |
| 546 | was that none of them provided sufficient capabilities to be used as the spell |
| 547 | checking engine in Vim, for various reasons: |
| 548 | |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 549 | - Missing support for multibyte encodings. At least UTF-8 must be supported, |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 550 | so that more than one language can be used in the same file. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 551 | Doing on-the-fly conversion is not always possible (would require iconv |
| 552 | support). |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 553 | - For the programs and libraries: Using them as-is would require installing |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 554 | them separately from Vim. That's mostly not impossible, but a drawback. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 555 | - Performance: A few tests showed that it's possible to check spelling on the |
| 556 | fly (while redrawing), just like syntax highlighting. But the mechanisms |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 557 | used by other code are much slower. Myspell uses a hashtable, for example. |
| 558 | The affix compression that most spell checkers use makes it slower too. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 559 | - For using an external program like aspell a communication mechanism would |
| 560 | have to be setup. That's complicated to do in a portable way (Unix-only |
| 561 | would be relatively simple, but that's not good enough). And performance |
| 562 | will become a problem (lots of process switching involved). |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 563 | - Missing support for words with non-word characters, such as "Etten-Leur" and |
| 564 | "et al.", would require marking the pieces of them OK, lowering the |
| 565 | reliability. |
| 566 | - Missing support for regions or dialects. Makes it difficult to accept |
| 567 | all English words and highlight non-Canadian words differently. |
| 568 | - Missing support for rare words. Many words are correct but hardly ever used |
| 569 | and could be a misspelled often-used word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 570 | - For making suggestions the speed is less important and requiring to install |
| 571 | another program or library would be acceptable. But the word lists probably |
| 572 | differ, the suggestions may be wrong words. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 573 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 574 | |
| 575 | Spelling suggestions *develop-spell-suggestions* |
| 576 | |
| 577 | For making suggestions there are two basic mechanisms: |
| 578 | 1. Try changing the bad word a little bit and check for a match with a good |
| 579 | word. Or go through the list of good words, change them a little bit and |
| 580 | check for a match with the bad word. The changes are deleting a character, |
| 581 | inserting a character, swapping two characters, etc. |
| 582 | 2. Perform soundfolding on both the bad word and the good words and then find |
| 583 | matches, possibly with a few changes like with the first mechanism. |
| 584 | |
| 585 | The first is good for finding typing mistakes. After experimenting with |
| 586 | hashtables and looking at solutions from other spell checkers the conclusion |
| 587 | was that a trie (a kind of tree structure) is ideal for this. Both for |
| 588 | reducing memory use and being able to try sensible changes. For example, when |
| 589 | inserting a character only characters that lead to good words need to be |
| 590 | tried. Other mechanisms (with hashtables) need to try all possible letters at |
| 591 | every position in the word. Also, a hashtable has the requirement that word |
| 592 | boundaries are identified separately, while a trie does not require this. |
| 593 | That makes the mechanism a lot simpler. |
| 594 | |
| 595 | Soundfolding is useful when someone knows how the words sounds but doesn't |
| 596 | know how it is spelled. For example, the word "dictionary" might be written |
| 597 | as "daktonerie". The number of changes that the first method would need to |
| 598 | try is very big, it's hard to find the good word that way. After soundfolding |
| 599 | the words become "tktnr" and "tkxnry", these differ by only two letters. |
| 600 | |
| 601 | To find words by their soundfolded equivalent (soundalike word) we need a list |
| 602 | of all soundfolded words. A few experiments have been done to find out what |
| 603 | the best method is. Alternatives: |
| 604 | 1. Do the sound folding on the fly when looking for suggestions. This means |
| 605 | walking through the trie of good words, soundfolding each word and |
| 606 | checking how different it is from the bad word. This is very efficient for |
| 607 | memory use, but takes a long time. On a fast PC it takes a couple of |
| 608 | seconds for English, which can be acceptable for interactive use. But for |
| 609 | some languages it takes more than ten seconds (e.g., German, Catalan), |
Bram Moolenaar | 088e8e3 | 2019-08-08 22:15:18 +0200 | [diff] [blame] | 610 | which is unacceptably slow. For batch processing (automatic corrections) |
Bram Moolenaar | 82038d7 | 2007-05-10 17:15:45 +0000 | [diff] [blame] | 611 | it's too slow for all languages. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 612 | 2. Use a trie for the soundfolded words, so that searching can be done just |
| 613 | like how it works without soundfolding. This requires remembering a list |
| 614 | of good words for each soundfolded word. This makes finding matches very |
| 615 | fast but requires quite a lot of memory, in the order of 1 to 10 Mbyte. |
| 616 | For some languages more than the original word list. |
| 617 | 3. Like the second alternative, but reduce the amount of memory by using affix |
| 618 | compression and store only the soundfolded basic word. This is what Aspell |
| 619 | does. Disadvantage is that affixes need to be stripped from the bad word |
| 620 | before soundfolding it, which means that mistakes at the start and/or end |
| 621 | of the word will cause the mechanism to fail. Also, this becomes slow when |
| 622 | the bad word is quite different from the good word. |
| 623 | |
| 624 | The choice made is to use the second mechanism and use a separate file. This |
| 625 | way a user with sufficient memory can get very good suggestions while a user |
| 626 | who is short of memory or just wants the spell checking and no suggestions |
| 627 | doesn't use so much memory. |
| 628 | |
| 629 | |
| 630 | Word frequency |
| 631 | |
| 632 | For sorting suggestions it helps to know which words are common. In theory we |
| 633 | could store a word frequency with the word in the dictionary. However, this |
| 634 | requires storing a count per word. That degrades word tree compression a lot. |
| 635 | And maintaining the word frequency for all languages will be a heavy task. |
| 636 | Also, it would be nice to prefer words that are already in the text. This way |
| 637 | the words that appear in the specific text are preferred for suggestions. |
| 638 | |
| 639 | What has been implemented is to count words that have been seen during |
| 640 | displaying. A hashtable is used to quickly find the word count. The count is |
| 641 | initialized from words listed in COMMON items in the affix file, so that it |
| 642 | also works when starting a new file. |
| 643 | |
| 644 | This isn't ideal, because the longer Vim is running the higher the counts |
Bram Moolenaar | 82038d7 | 2007-05-10 17:15:45 +0000 | [diff] [blame] | 645 | become. But in practice it is a noticeable improvement over not using the word |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 646 | count. |
| 647 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 648 | ============================================================================== |
| 649 | 4. Assumptions *design-assumptions* |
| 650 | |
| 651 | Size of variables: |
| 652 | char 8 bit signed |
| 653 | char_u 8 bit unsigned |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 654 | int 32 or 64 bit signed (16 might be possible with limited features) |
| 655 | unsigned 32 or 64 bit unsigned (16 as with ints) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 656 | long 32 or 64 bit signed, can hold a pointer |
| 657 | |
| 658 | Note that some compilers cannot handle long lines or strings. The C89 |
| 659 | standard specifies a limit of 509 characters. |
| 660 | |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 661 | vim:tw=78:ts=8:noet:ft=help:norl: |