| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 1 |  |
| 2 | |
| 3 | # Vim source code # |
| 4 | |
| 5 | Here are a few hints for finding your way around the source code. This |
| 6 | doesn't make it less complex than it is, but it gets you started. |
| 7 | |
| 8 | You might also want to read |
| 9 | [`:help development`](http://vimdoc.sourceforge.net/htmldoc/develop.html#development). |
| 10 | |
| 11 | |
| 12 | ## Jumping around ## |
| 13 | |
| 14 | First of all, use `:make tags` to generate a tags file, so that you can jump |
| 15 | around in the source code. |
| 16 | |
| 17 | To jump to a function or variable definition, move the cursor on the name and |
| 18 | use the `CTRL-]` command. Use `CTRL-T` or `CTRL-O` to jump back. |
| 19 | |
| 20 | To jump to a file, move the cursor on its name and use the `gf` command. |
| 21 | |
| 22 | Most code can be found in a file with an obvious name (incomplete list): |
| 23 | |
| Bram Moolenaar | ecaa70e | 2019-07-14 14:55:39 +0200 | [diff] [blame] | 24 | File name | Description |
| 25 | --------------- | ----------- |
| Yegappan Lakshmanan | cbae580 | 2021-08-06 21:51:55 +0200 | [diff] [blame] | 26 | alloc.c | memory management |
| Bram Moolenaar | 4ad6215 | 2019-08-17 14:38:55 +0200 | [diff] [blame] | 27 | arglist.c | handling argument list |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 28 | autocmd.c | autocommands |
| Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 29 | blob.c | blob data type |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 30 | buffer.c | manipulating buffers (loaded files) |
| Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 31 | bufwrite.c | writing a buffer to file |
| Bram Moolenaar | ec28d15 | 2019-05-11 18:36:34 +0200 | [diff] [blame] | 32 | change.c | handling changes to text |
| Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 33 | cindent.c | C and Lisp indentation |
| Bram Moolenaar | f87a040 | 2020-04-05 20:21:03 +0200 | [diff] [blame] | 34 | clientserver.c | client server functionality |
| Bram Moolenaar | 45fffdf | 2020-03-24 21:42:01 +0100 | [diff] [blame] | 35 | clipboard.c | handling the clipboard |
| Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 36 | cmdexpand.c | command-line completion |
| Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 37 | cmdhist.c | command-line history |
| Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [diff] [blame] | 38 | debugger.c | Vim script debugger |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 39 | diff.c | diff mode (vimdiff) |
| Bram Moolenaar | 7528d1f | 2019-09-19 23:06:20 +0200 | [diff] [blame] | 40 | drawline.c | drawing a window line |
| 41 | drawscreen.c | drawing the windows |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 42 | eval.c | expression evaluation |
| Bram Moolenaar | 261f346 | 2019-09-07 15:45:32 +0200 | [diff] [blame] | 43 | evalbuffer.c | buffer related built-in functions |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 44 | evalfunc.c | built-in functions |
| Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 45 | evalvars.c | vim variables |
| Bram Moolenaar | 261f346 | 2019-09-07 15:45:32 +0200 | [diff] [blame] | 46 | evalwindow.c | window related built-in functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 47 | fileio.c | reading and writing files |
| Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 48 | filepath.c | dealing with file names and paths |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 49 | findfile.c | search for files in 'path' |
| 50 | fold.c | folding |
| 51 | getchar.c | getting characters and key mapping |
| Yegappan Lakshmanan | 25536f4 | 2024-05-22 16:45:04 +0200 | [diff] [blame] | 52 | gc.c | garbage collection |
| Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 53 | help.c | vim help related functions |
| Bram Moolenaar | f9cc9f2 | 2019-07-14 21:29:22 +0200 | [diff] [blame] | 54 | highlight.c | syntax highlighting |
| Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 55 | indent.c | text indentation |
| Bram Moolenaar | 95946f1 | 2019-03-31 15:31:59 +0200 | [diff] [blame] | 56 | insexpand.c | Insert mode completion |
| Bram Moolenaar | 054f14b | 2020-07-22 19:11:19 +0200 | [diff] [blame] | 57 | locale.c | locale/language handling |
| Bram Moolenaar | b66bab3 | 2019-08-01 14:28:24 +0200 | [diff] [blame] | 58 | map.c | mapping and abbreviations |
| Bram Moolenaar | 06cf97e | 2020-06-28 13:17:26 +0200 | [diff] [blame] | 59 | mark.c | marks |
| 60 | match.c | highlight matching |
| Yegappan Lakshmanan | 01c798c | 2021-06-02 17:07:18 +0200 | [diff] [blame] | 61 | float.c | floating point functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 62 | mbyte.c | multi-byte character handling |
| 63 | memfile.c | storing lines for buffers in a swapfile |
| 64 | memline.c | storing lines for buffers in memory |
| 65 | menu.c | menus |
| 66 | message.c | (error) messages |
| Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 67 | mouse.c | handling the mouse |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 68 | ops.c | handling operators ("d", "y", "p") |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 69 | option.c | options |
| Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 70 | optionstr.c | handling string options |
| Bram Moolenaar | 30e8e73 | 2019-09-27 13:08:36 +0200 | [diff] [blame] | 71 | popupmenu.c | popup menu |
| Bram Moolenaar | ecaa70e | 2019-07-14 14:55:39 +0200 | [diff] [blame] | 72 | popupwin.c | popup window |
| Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [diff] [blame] | 73 | profiler.c | Vim script profiler |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 74 | quickfix.c | quickfix commands (":make", ":cn") |
| 75 | regexp.c | pattern matching |
| Bram Moolenaar | 4aea03e | 2019-09-25 22:37:17 +0200 | [diff] [blame] | 76 | register.c | handling registers |
| Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 77 | scriptfile.c | runtime directory handling and sourcing scripts |
| Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 78 | screen.c | lower level screen functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 79 | search.c | pattern searching |
| Bram Moolenaar | 8453807 | 2019-07-28 14:15:42 +0200 | [diff] [blame] | 80 | session.c | sessions and views |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 81 | sign.c | signs |
| Bram Moolenaar | 46a426c | 2019-09-27 12:41:56 +0200 | [diff] [blame] | 82 | spell.c | spell checking core |
| 83 | spellfile.c | spell file handling |
| 84 | spellsuggest.c | spell correction suggestions |
| Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 85 | strings.c | string manipulation functions |
| Bram Moolenaar | fa55cfc | 2019-07-13 22:59:32 +0200 | [diff] [blame] | 86 | syntax.c | syntax and other highlighting |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 87 | tag.c | tags |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 88 | term.c | terminal handling, termcap codes |
| Bram Moolenaar | ecaa70e | 2019-07-14 14:55:39 +0200 | [diff] [blame] | 89 | testing.c | testing: assert and test functions |
| Bram Moolenaar | 11abd09 | 2020-05-01 14:26:37 +0200 | [diff] [blame] | 90 | textformat.c | text formatting |
| Bram Moolenaar | ed8ce05 | 2020-04-29 21:04:15 +0200 | [diff] [blame] | 91 | textobject.c | text objects |
| Bram Moolenaar | ecaa70e | 2019-07-14 14:55:39 +0200 | [diff] [blame] | 92 | textprop.c | text properties |
| Bram Moolenaar | 0a8fed6 | 2020-02-14 13:22:17 +0100 | [diff] [blame] | 93 | time.c | time and timer functions |
| Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [diff] [blame] | 94 | typval.c | Vim script type/value functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 95 | undo.c | undo and redo |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 96 | usercmd.c | user defined commands |
| 97 | userfunc.c | user defined functions |
| Bram Moolenaar | 1e78e69 | 2019-07-22 20:18:27 +0200 | [diff] [blame] | 98 | viminfo.c | viminfo handling |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 99 | window.c | handling split windows |
| 100 | |
| 101 | |
| 102 | ## Debugging ## |
| 103 | |
| 104 | If you have a reasonable recent version of gdb, you can use the `:Termdebug` |
| 105 | command to debug Vim. See `:help :Termdebug`. |
| 106 | |
| 107 | When something is time critical or stepping through code is a hassle, use the |
| 108 | channel logging to create a time-stamped log file. Add lines to the code like |
| 109 | this: |
| 110 | |
| 111 | ch_log(NULL, "Value is now %02x", value); |
| 112 | |
| 113 | After compiling and starting Vim, do: |
| 114 | |
| 115 | :call ch_logfile('debuglog', 'w') |
| 116 | |
| 117 | And edit `debuglog` to see what happens. The channel functions already have |
| 118 | `ch_log()` calls, thus you always see that in the log. |
| 119 | |
| 120 | |
| 121 | ## Important Variables ## |
| 122 | |
| 123 | The current mode is stored in `State`. The values it can have are `NORMAL`, |
| 124 | `INSERT`, `CMDLINE`, and a few others. |
| 125 | |
| 126 | The current window is `curwin`. The current buffer is `curbuf`. These point |
| 127 | to structures with the cursor position in the window, option values, the file |
| 128 | name, etc. These are defined in |
| Bram Moolenaar | c8cc0ad | 2019-04-26 21:31:38 +0200 | [diff] [blame] | 129 | [`structs.h`](https://github.com/vim/vim/blob/master/src/structs.h). |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 130 | |
| 131 | All the global variables are declared in |
| Bram Moolenaar | c8cc0ad | 2019-04-26 21:31:38 +0200 | [diff] [blame] | 132 | [`globals.h`](https://github.com/vim/vim/blob/master/src/globals.h). |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 133 | |
| 134 | |
| 135 | ## The main loop ## |
| 136 | |
| 137 | This is conveniently called `main_loop()`. It updates a few things and then |
| 138 | calls `normal_cmd()` to process a command. This returns when the command is |
| 139 | finished. |
| 140 | |
| 141 | The basic idea is that Vim waits for the user to type a character and |
| 142 | processes it until another character is needed. Thus there are several places |
| 143 | where Vim waits for a character to be typed. The `vgetc()` function is used |
| 144 | for this. It also handles mapping. |
| 145 | |
| 146 | Updating the screen is mostly postponed until a command or a sequence of |
| 147 | commands has finished. The work is done by `update_screen()`, which calls |
| 148 | `win_update()` for every window, which calls `win_line()` for every line. |
| 149 | See the start of |
| 150 | [`screen.c`](https://github.com/vim/vim/blob/master/src/screen.c) |
| 151 | for more explanations. |
| 152 | |
| 153 | |
| 154 | ## Command-line mode ## |
| 155 | |
| 156 | When typing a `:`, `normal_cmd()` will call `getcmdline()` to obtain a line |
| 157 | with an Ex command. `getcmdline()` contains a loop that will handle each typed |
| 158 | character. It returns when hitting `CR` or `Esc` or some other character that |
| 159 | ends the command line mode. |
| 160 | |
| 161 | |
| 162 | ## Ex commands ## |
| 163 | |
| 164 | Ex commands are handled by the function `do_cmdline()`. It does the generic |
| 165 | parsing of the `:` command line and calls `do_one_cmd()` for each separate |
| 166 | command. It also takes care of while loops. |
| 167 | |
| 168 | `do_one_cmd()` parses the range and generic arguments and puts them in the |
| 169 | `exarg_t` and passes it to the function that handles the command. |
| 170 | |
| 171 | The `:` commands are listed in `ex_cmds.h`. The third entry of each item is |
| 172 | the name of the function that handles the command. The last entry are the |
| 173 | flags that are used for the command. |
| 174 | |
| 175 | |
| 176 | ## Normal mode commands ## |
| 177 | |
| 178 | The Normal mode commands are handled by the `normal_cmd()` function. It also |
| 179 | handles the optional count and an extra character for some commands. These |
| 180 | are passed in a `cmdarg_t` to the function that handles the command. |
| 181 | |
| 182 | There is a table `nv_cmds` in |
| 183 | [`normal.c`](https://github.com/vim/vim/blob/master/src/normal.c) |
| 184 | which lists the first character of every command. The second entry of each |
| 185 | item is the name of the function that handles the command. |
| 186 | |
| 187 | |
| 188 | ## Insert mode commands ## |
| 189 | |
| 190 | When doing an `i` or `a` command, `normal_cmd()` will call the `edit()` |
| 191 | function. It contains a loop that waits for the next character and handles it. |
| 192 | It returns when leaving Insert mode. |
| 193 | |
| 194 | |
| 195 | ## Options ## |
| 196 | |
| 197 | There is a list with all option names in |
| 198 | [`option.c`](https://github.com/vim/vim/blob/master/src/option.c), |
| 199 | called `options[]`. |
| 200 | |
| 201 | |
| 202 | ## The GUI ## |
| 203 | |
| 204 | Most of the GUI code is implemented like it was a clever terminal. Typing a |
| 205 | character, moving a scrollbar, clicking the mouse, etc. are all translated |
| 206 | into events which are written in the input buffer. These are read by the |
| 207 | main code, just like reading from a terminal. The code for this is scattered |
| 208 | through [`gui.c`](https://github.com/vim/vim/blob/master/src/gui.c). |
| 209 | For example, `gui_send_mouse_event()` for a mouse click and `gui_menu_cb()` for |
| 210 | a menu action. Key hits are handled by the system-specific GUI code, which |
| 211 | calls `add_to_input_buf()` to send the key code. |
| 212 | |
| 213 | Updating the GUI window is done by writing codes in the output buffer, just |
| 214 | like writing to a terminal. When the buffer gets full or is flushed, |
| 215 | `gui_write()` will parse the codes and draw the appropriate items. Finally the |
| 216 | system-specific GUI code will be called to do the work. |
| 217 | |
| 218 | |
| 219 | ## Debugging the GUI ## |
| 220 | |
| 221 | Remember to prevent that gvim forks and the debugger thinks Vim has exited, |
| 222 | add the `-f` argument. In gdb: `run -f -g`. |
| 223 | |
| 224 | When stepping through display updating code, the focus event is triggered |
| 225 | when going from the debugger to Vim and back. To avoid this, recompile with |
| 226 | some code in `gui_focus_change()` disabled. |
| 227 | |
| 228 | |
| 229 | ## Contributing ## |
| 230 | |
| 231 | If you would like to help making Vim better, see the |
| 232 | [`CONTRIBUTING.md`](https://github.com/vim/vim/blob/master/CONTRIBUTING.md) |
| 233 | file. |
| 234 | |
| 235 | |
| Christian Brabandt | b4ddc6c | 2024-01-02 16:51:11 +0100 | [diff] [blame] | 236 | This is `README.md` for version 9.1 of the Vim source code. |