blob: ffce497a5aa2baf5656e8ef657b5f330006f1fca [file] [log] [blame] [view]
Bram Moolenaar8ac8a772019-03-29 13:10:08 +01001![Vim Logo](https://github.com/vim/vim/blob/master/runtime/vimlogo.gif)
2
3# Vim source code #
4
5Here are a few hints for finding your way around the source code. This
6doesn't make it less complex than it is, but it gets you started.
7
8You might also want to read
9[`:help development`](http://vimdoc.sourceforge.net/htmldoc/develop.html#development).
10
11
12## Jumping around ##
13
14First of all, use `:make tags` to generate a tags file, so that you can jump
15around in the source code.
16
17To jump to a function or variable definition, move the cursor on the name and
18use the `CTRL-]` command. Use `CTRL-T` or `CTRL-O` to jump back.
19
20To jump to a file, move the cursor on its name and use the `gf` command.
21
22Most code can be found in a file with an obvious name (incomplete list):
23
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020024File name | Description
25--------------- | -----------
Bram Moolenaar4ad62152019-08-17 14:38:55 +020026arglist.c | handling argument list
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010027autocmd.c | autocommands
Bram Moolenaard7663c22019-08-06 21:59:57 +020028blob.c | blob data type
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010029buffer.c | manipulating buffers (loaded files)
Bram Moolenaarec28d152019-05-11 18:36:34 +020030change.c | handling changes to text
Bram Moolenaard7663c22019-08-06 21:59:57 +020031cmdhist.c | command-line history
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020032debugger.c | vim script debugger
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010033diff.c | diff mode (vimdiff)
34eval.c | expression evaluation
Bram Moolenaarac9fb182019-04-27 13:04:13 +020035evalfunc.c | built-in functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010036fileio.c | reading and writing files
37findfile.c | search for files in 'path'
38fold.c | folding
39getchar.c | getting characters and key mapping
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020040highlight.c | syntax highlighting
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010041indent.c | C and Lisp indentation
Bram Moolenaar95946f12019-03-31 15:31:59 +020042insexpand.c | Insert mode completion
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010043mark.c | marks
Bram Moolenaarb66bab32019-08-01 14:28:24 +020044map.c | mapping and abbreviations
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010045mbyte.c | multi-byte character handling
46memfile.c | storing lines for buffers in a swapfile
47memline.c | storing lines for buffers in memory
48menu.c | menus
49message.c | (error) messages
Bram Moolenaarac9fb182019-04-27 13:04:13 +020050ops.c | handling operators ("d", "y", "p")
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010051option.c | options
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020052popupmnu.c | popup menu
53popupwin.c | popup window
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020054profiler.c | vim script profiler
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010055quickfix.c | quickfix commands (":make", ":cn")
56regexp.c | pattern matching
57screen.c | updating the windows
58search.c | pattern searching
Bram Moolenaar84538072019-07-28 14:15:42 +020059session.c | sessions and views
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010060sign.c | signs
61spell.c | spell checking
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020062syntax.c | syntax and other highlighting
Bram Moolenaarac9fb182019-04-27 13:04:13 +020063tag.c | tags
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010064term.c | terminal handling, termcap codes
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020065testing.c | testing: assert and test functions
66textprop.c | text properties
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010067undo.c | undo and redo
Bram Moolenaarac9fb182019-04-27 13:04:13 +020068usercmd.c | user defined commands
69userfunc.c | user defined functions
Bram Moolenaar1e78e692019-07-22 20:18:27 +020070viminfo.c | viminfo handling
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010071window.c | handling split windows
72
73
74## Debugging ##
75
76If you have a reasonable recent version of gdb, you can use the `:Termdebug`
77command to debug Vim. See `:help :Termdebug`.
78
79When something is time critical or stepping through code is a hassle, use the
80channel logging to create a time-stamped log file. Add lines to the code like
81this:
82
83 ch_log(NULL, "Value is now %02x", value);
84
85After compiling and starting Vim, do:
86
87 :call ch_logfile('debuglog', 'w')
88
89And edit `debuglog` to see what happens. The channel functions already have
90`ch_log()` calls, thus you always see that in the log.
91
92
93## Important Variables ##
94
95The current mode is stored in `State`. The values it can have are `NORMAL`,
96`INSERT`, `CMDLINE`, and a few others.
97
98The current window is `curwin`. The current buffer is `curbuf`. These point
99to structures with the cursor position in the window, option values, the file
100name, etc. These are defined in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200101[`structs.h`](https://github.com/vim/vim/blob/master/src/structs.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100102
103All the global variables are declared in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200104[`globals.h`](https://github.com/vim/vim/blob/master/src/globals.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100105
106
107## The main loop ##
108
109This is conveniently called `main_loop()`. It updates a few things and then
110calls `normal_cmd()` to process a command. This returns when the command is
111finished.
112
113The basic idea is that Vim waits for the user to type a character and
114processes it until another character is needed. Thus there are several places
115where Vim waits for a character to be typed. The `vgetc()` function is used
116for this. It also handles mapping.
117
118Updating the screen is mostly postponed until a command or a sequence of
119commands has finished. The work is done by `update_screen()`, which calls
120`win_update()` for every window, which calls `win_line()` for every line.
121See the start of
122[`screen.c`](https://github.com/vim/vim/blob/master/src/screen.c)
123for more explanations.
124
125
126## Command-line mode ##
127
128When typing a `:`, `normal_cmd()` will call `getcmdline()` to obtain a line
129with an Ex command. `getcmdline()` contains a loop that will handle each typed
130character. It returns when hitting `CR` or `Esc` or some other character that
131ends the command line mode.
132
133
134## Ex commands ##
135
136Ex commands are handled by the function `do_cmdline()`. It does the generic
137parsing of the `:` command line and calls `do_one_cmd()` for each separate
138command. It also takes care of while loops.
139
140`do_one_cmd()` parses the range and generic arguments and puts them in the
141`exarg_t` and passes it to the function that handles the command.
142
143The `:` commands are listed in `ex_cmds.h`. The third entry of each item is
144the name of the function that handles the command. The last entry are the
145flags that are used for the command.
146
147
148## Normal mode commands ##
149
150The Normal mode commands are handled by the `normal_cmd()` function. It also
151handles the optional count and an extra character for some commands. These
152are passed in a `cmdarg_t` to the function that handles the command.
153
154There is a table `nv_cmds` in
155[`normal.c`](https://github.com/vim/vim/blob/master/src/normal.c)
156which lists the first character of every command. The second entry of each
157item is the name of the function that handles the command.
158
159
160## Insert mode commands ##
161
162When doing an `i` or `a` command, `normal_cmd()` will call the `edit()`
163function. It contains a loop that waits for the next character and handles it.
164It returns when leaving Insert mode.
165
166
167## Options ##
168
169There is a list with all option names in
170[`option.c`](https://github.com/vim/vim/blob/master/src/option.c),
171called `options[]`.
172
173
174## The GUI ##
175
176Most of the GUI code is implemented like it was a clever terminal. Typing a
177character, moving a scrollbar, clicking the mouse, etc. are all translated
178into events which are written in the input buffer. These are read by the
179main code, just like reading from a terminal. The code for this is scattered
180through [`gui.c`](https://github.com/vim/vim/blob/master/src/gui.c).
181For example, `gui_send_mouse_event()` for a mouse click and `gui_menu_cb()` for
182a menu action. Key hits are handled by the system-specific GUI code, which
183calls `add_to_input_buf()` to send the key code.
184
185Updating the GUI window is done by writing codes in the output buffer, just
186like writing to a terminal. When the buffer gets full or is flushed,
187`gui_write()` will parse the codes and draw the appropriate items. Finally the
188system-specific GUI code will be called to do the work.
189
190
191## Debugging the GUI ##
192
193Remember to prevent that gvim forks and the debugger thinks Vim has exited,
194add the `-f` argument. In gdb: `run -f -g`.
195
196When stepping through display updating code, the focus event is triggered
197when going from the debugger to Vim and back. To avoid this, recompile with
198some code in `gui_focus_change()` disabled.
199
200
201## Contributing ##
202
203If you would like to help making Vim better, see the
204[`CONTRIBUTING.md`](https://github.com/vim/vim/blob/master/CONTRIBUTING.md)
205file.
206
207
208This is `README.md` for version 8.1 of the Vim source code.