| 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 | |
| 24 | File name | Description |
| 25 | --------- | ----------- |
| 26 | autocmd.c | autocommands |
| 27 | buffer.c | manipulating buffers (loaded files) |
| Bram Moolenaar | ec28d15 | 2019-05-11 18:36:34 +0200 | [diff] [blame] | 28 | change.c | handling changes to text |
| Bram Moolenaar | 31fc39e | 2019-04-23 18:39:49 +0200 | [diff] [blame] | 29 | debugger.c | vim script debugger |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 30 | diff.c | diff mode (vimdiff) |
| 31 | eval.c | expression evaluation |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 32 | evalfunc.c | built-in functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 33 | fileio.c | reading and writing files |
| 34 | findfile.c | search for files in 'path' |
| 35 | fold.c | folding |
| 36 | getchar.c | getting characters and key mapping |
| 37 | indent.c | C and Lisp indentation |
| Bram Moolenaar | 95946f1 | 2019-03-31 15:31:59 +0200 | [diff] [blame] | 38 | insexpand.c | Insert mode completion |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 39 | mark.c | marks |
| 40 | mbyte.c | multi-byte character handling |
| 41 | memfile.c | storing lines for buffers in a swapfile |
| 42 | memline.c | storing lines for buffers in memory |
| 43 | menu.c | menus |
| 44 | message.c | (error) messages |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 45 | ops.c | handling operators ("d", "y", "p") |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 46 | option.c | options |
| Bram Moolenaar | fa55cfc | 2019-07-13 22:59:32 +0200 | [diff] [blame^] | 47 | profiler.c | vim script profiler |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 48 | quickfix.c | quickfix commands (":make", ":cn") |
| 49 | regexp.c | pattern matching |
| 50 | screen.c | updating the windows |
| 51 | search.c | pattern searching |
| 52 | sign.c | signs |
| 53 | spell.c | spell checking |
| Bram Moolenaar | fa55cfc | 2019-07-13 22:59:32 +0200 | [diff] [blame^] | 54 | syntax.c | syntax and other highlighting |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 55 | tag.c | tags |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 56 | term.c | terminal handling, termcap codes |
| 57 | undo.c | undo and redo |
| Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 58 | usercmd.c | user defined commands |
| 59 | userfunc.c | user defined functions |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 60 | window.c | handling split windows |
| 61 | |
| 62 | |
| 63 | ## Debugging ## |
| 64 | |
| 65 | If you have a reasonable recent version of gdb, you can use the `:Termdebug` |
| 66 | command to debug Vim. See `:help :Termdebug`. |
| 67 | |
| 68 | When something is time critical or stepping through code is a hassle, use the |
| 69 | channel logging to create a time-stamped log file. Add lines to the code like |
| 70 | this: |
| 71 | |
| 72 | ch_log(NULL, "Value is now %02x", value); |
| 73 | |
| 74 | After compiling and starting Vim, do: |
| 75 | |
| 76 | :call ch_logfile('debuglog', 'w') |
| 77 | |
| 78 | And edit `debuglog` to see what happens. The channel functions already have |
| 79 | `ch_log()` calls, thus you always see that in the log. |
| 80 | |
| 81 | |
| 82 | ## Important Variables ## |
| 83 | |
| 84 | The current mode is stored in `State`. The values it can have are `NORMAL`, |
| 85 | `INSERT`, `CMDLINE`, and a few others. |
| 86 | |
| 87 | The current window is `curwin`. The current buffer is `curbuf`. These point |
| 88 | to structures with the cursor position in the window, option values, the file |
| 89 | name, etc. These are defined in |
| Bram Moolenaar | c8cc0ad | 2019-04-26 21:31:38 +0200 | [diff] [blame] | 90 | [`structs.h`](https://github.com/vim/vim/blob/master/src/structs.h). |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 91 | |
| 92 | All the global variables are declared in |
| Bram Moolenaar | c8cc0ad | 2019-04-26 21:31:38 +0200 | [diff] [blame] | 93 | [`globals.h`](https://github.com/vim/vim/blob/master/src/globals.h). |
| Bram Moolenaar | 8ac8a77 | 2019-03-29 13:10:08 +0100 | [diff] [blame] | 94 | |
| 95 | |
| 96 | ## The main loop ## |
| 97 | |
| 98 | This is conveniently called `main_loop()`. It updates a few things and then |
| 99 | calls `normal_cmd()` to process a command. This returns when the command is |
| 100 | finished. |
| 101 | |
| 102 | The basic idea is that Vim waits for the user to type a character and |
| 103 | processes it until another character is needed. Thus there are several places |
| 104 | where Vim waits for a character to be typed. The `vgetc()` function is used |
| 105 | for this. It also handles mapping. |
| 106 | |
| 107 | Updating the screen is mostly postponed until a command or a sequence of |
| 108 | commands has finished. The work is done by `update_screen()`, which calls |
| 109 | `win_update()` for every window, which calls `win_line()` for every line. |
| 110 | See the start of |
| 111 | [`screen.c`](https://github.com/vim/vim/blob/master/src/screen.c) |
| 112 | for more explanations. |
| 113 | |
| 114 | |
| 115 | ## Command-line mode ## |
| 116 | |
| 117 | When typing a `:`, `normal_cmd()` will call `getcmdline()` to obtain a line |
| 118 | with an Ex command. `getcmdline()` contains a loop that will handle each typed |
| 119 | character. It returns when hitting `CR` or `Esc` or some other character that |
| 120 | ends the command line mode. |
| 121 | |
| 122 | |
| 123 | ## Ex commands ## |
| 124 | |
| 125 | Ex commands are handled by the function `do_cmdline()`. It does the generic |
| 126 | parsing of the `:` command line and calls `do_one_cmd()` for each separate |
| 127 | command. It also takes care of while loops. |
| 128 | |
| 129 | `do_one_cmd()` parses the range and generic arguments and puts them in the |
| 130 | `exarg_t` and passes it to the function that handles the command. |
| 131 | |
| 132 | The `:` commands are listed in `ex_cmds.h`. The third entry of each item is |
| 133 | the name of the function that handles the command. The last entry are the |
| 134 | flags that are used for the command. |
| 135 | |
| 136 | |
| 137 | ## Normal mode commands ## |
| 138 | |
| 139 | The Normal mode commands are handled by the `normal_cmd()` function. It also |
| 140 | handles the optional count and an extra character for some commands. These |
| 141 | are passed in a `cmdarg_t` to the function that handles the command. |
| 142 | |
| 143 | There is a table `nv_cmds` in |
| 144 | [`normal.c`](https://github.com/vim/vim/blob/master/src/normal.c) |
| 145 | which lists the first character of every command. The second entry of each |
| 146 | item is the name of the function that handles the command. |
| 147 | |
| 148 | |
| 149 | ## Insert mode commands ## |
| 150 | |
| 151 | When doing an `i` or `a` command, `normal_cmd()` will call the `edit()` |
| 152 | function. It contains a loop that waits for the next character and handles it. |
| 153 | It returns when leaving Insert mode. |
| 154 | |
| 155 | |
| 156 | ## Options ## |
| 157 | |
| 158 | There is a list with all option names in |
| 159 | [`option.c`](https://github.com/vim/vim/blob/master/src/option.c), |
| 160 | called `options[]`. |
| 161 | |
| 162 | |
| 163 | ## The GUI ## |
| 164 | |
| 165 | Most of the GUI code is implemented like it was a clever terminal. Typing a |
| 166 | character, moving a scrollbar, clicking the mouse, etc. are all translated |
| 167 | into events which are written in the input buffer. These are read by the |
| 168 | main code, just like reading from a terminal. The code for this is scattered |
| 169 | through [`gui.c`](https://github.com/vim/vim/blob/master/src/gui.c). |
| 170 | For example, `gui_send_mouse_event()` for a mouse click and `gui_menu_cb()` for |
| 171 | a menu action. Key hits are handled by the system-specific GUI code, which |
| 172 | calls `add_to_input_buf()` to send the key code. |
| 173 | |
| 174 | Updating the GUI window is done by writing codes in the output buffer, just |
| 175 | like writing to a terminal. When the buffer gets full or is flushed, |
| 176 | `gui_write()` will parse the codes and draw the appropriate items. Finally the |
| 177 | system-specific GUI code will be called to do the work. |
| 178 | |
| 179 | |
| 180 | ## Debugging the GUI ## |
| 181 | |
| 182 | Remember to prevent that gvim forks and the debugger thinks Vim has exited, |
| 183 | add the `-f` argument. In gdb: `run -f -g`. |
| 184 | |
| 185 | When stepping through display updating code, the focus event is triggered |
| 186 | when going from the debugger to Vim and back. To avoid this, recompile with |
| 187 | some code in `gui_focus_change()` disabled. |
| 188 | |
| 189 | |
| 190 | ## Contributing ## |
| 191 | |
| 192 | If you would like to help making Vim better, see the |
| 193 | [`CONTRIBUTING.md`](https://github.com/vim/vim/blob/master/CONTRIBUTING.md) |
| 194 | file. |
| 195 | |
| 196 | |
| 197 | This is `README.md` for version 8.1 of the Vim source code. |