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