blob: 30d57310f912c2e15fbb911be0c7d535075e9216 [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--------------- | -----------
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +020026alloc.c | memory management
Bram Moolenaar4ad62152019-08-17 14:38:55 +020027arglist.c | handling argument list
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010028autocmd.c | autocommands
Bram Moolenaard7663c22019-08-06 21:59:57 +020029blob.c | blob data type
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010030buffer.c | manipulating buffers (loaded files)
Bram Moolenaar473952e2019-09-28 16:30:04 +020031bufwrite.c | writing a buffer to file
Bram Moolenaarec28d152019-05-11 18:36:34 +020032change.c | handling changes to text
Bram Moolenaar14c01f82019-10-09 22:53:08 +020033cindent.c | C and Lisp indentation
Bram Moolenaarf87a0402020-04-05 20:21:03 +020034clientserver.c | client server functionality
Bram Moolenaar45fffdf2020-03-24 21:42:01 +010035clipboard.c | handling the clipboard
Bram Moolenaar66b51422019-08-18 21:44:12 +020036cmdexpand.c | command-line completion
Bram Moolenaard7663c22019-08-06 21:59:57 +020037cmdhist.c | command-line history
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020038debugger.c | vim script debugger
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010039diff.c | diff mode (vimdiff)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020040drawline.c | drawing a window line
41drawscreen.c | drawing the windows
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010042eval.c | expression evaluation
Bram Moolenaar261f3462019-09-07 15:45:32 +020043evalbuffer.c | buffer related built-in functions
Bram Moolenaarac9fb182019-04-27 13:04:13 +020044evalfunc.c | built-in functions
Bram Moolenaar0522ba02019-08-27 22:48:30 +020045evalvars.c | vim variables
Bram Moolenaar261f3462019-09-07 15:45:32 +020046evalwindow.c | window related built-in functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010047fileio.c | reading and writing files
Bram Moolenaarb005cd82019-09-04 15:54:55 +020048filepath.c | dealing with file names and paths
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010049findfile.c | search for files in 'path'
50fold.c | folding
51getchar.c | getting characters and key mapping
Bram Moolenaarf868ba82020-07-21 21:07:20 +020052help.c | vim help related functions
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020053highlight.c | syntax highlighting
Bram Moolenaar14c01f82019-10-09 22:53:08 +020054indent.c | text indentation
Bram Moolenaar95946f12019-03-31 15:31:59 +020055insexpand.c | Insert mode completion
Bram Moolenaar054f14b2020-07-22 19:11:19 +020056locale.c | locale/language handling
Bram Moolenaarb66bab32019-08-01 14:28:24 +020057map.c | mapping and abbreviations
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020058mark.c | marks
59match.c | highlight matching
Yegappan Lakshmanan01c798c2021-06-02 17:07:18 +020060float.c | floating point functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010061mbyte.c | multi-byte character handling
62memfile.c | storing lines for buffers in a swapfile
63memline.c | storing lines for buffers in memory
64menu.c | menus
65message.c | (error) messages
Bram Moolenaarb20b9e12019-09-21 20:48:04 +020066mouse.c | handling the mouse
Bram Moolenaarac9fb182019-04-27 13:04:13 +020067ops.c | handling operators ("d", "y", "p")
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010068option.c | options
Bram Moolenaardac13472019-09-16 21:06:21 +020069optionstr.c | handling string options
Bram Moolenaar30e8e732019-09-27 13:08:36 +020070popupmenu.c | popup menu
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020071popupwin.c | popup window
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020072profiler.c | vim script profiler
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010073quickfix.c | quickfix commands (":make", ":cn")
74regexp.c | pattern matching
Bram Moolenaar4aea03e2019-09-25 22:37:17 +020075register.c | handling registers
Bram Moolenaar307c5a52019-08-25 15:41:00 +020076scriptfile.c | runtime directory handling and sourcing scripts
Bram Moolenaarb20b9e12019-09-21 20:48:04 +020077screen.c | lower level screen functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010078search.c | pattern searching
Bram Moolenaar84538072019-07-28 14:15:42 +020079session.c | sessions and views
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010080sign.c | signs
Bram Moolenaar46a426c2019-09-27 12:41:56 +020081spell.c | spell checking core
82spellfile.c | spell file handling
83spellsuggest.c | spell correction suggestions
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020084strings.c | string manipulation functions
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020085syntax.c | syntax and other highlighting
Bram Moolenaarac9fb182019-04-27 13:04:13 +020086tag.c | tags
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010087term.c | terminal handling, termcap codes
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020088testing.c | testing: assert and test functions
Bram Moolenaar11abd092020-05-01 14:26:37 +020089textformat.c | text formatting
Bram Moolenaared8ce052020-04-29 21:04:15 +020090textobject.c | text objects
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020091textprop.c | text properties
Bram Moolenaar0a8fed62020-02-14 13:22:17 +010092time.c | time and timer functions
Bram Moolenaar367d59e2020-05-30 17:06:14 +020093typval.c | vim script type/value functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010094undo.c | undo and redo
Bram Moolenaarac9fb182019-04-27 13:04:13 +020095usercmd.c | user defined commands
96userfunc.c | user defined functions
Bram Moolenaar1e78e692019-07-22 20:18:27 +020097viminfo.c | viminfo handling
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010098window.c | handling split windows
99
100
101## Debugging ##
102
103If you have a reasonable recent version of gdb, you can use the `:Termdebug`
104command to debug Vim. See `:help :Termdebug`.
105
106When something is time critical or stepping through code is a hassle, use the
107channel logging to create a time-stamped log file. Add lines to the code like
108this:
109
110 ch_log(NULL, "Value is now %02x", value);
111
112After compiling and starting Vim, do:
113
114 :call ch_logfile('debuglog', 'w')
115
116And edit `debuglog` to see what happens. The channel functions already have
117`ch_log()` calls, thus you always see that in the log.
118
119
120## Important Variables ##
121
122The current mode is stored in `State`. The values it can have are `NORMAL`,
123`INSERT`, `CMDLINE`, and a few others.
124
125The current window is `curwin`. The current buffer is `curbuf`. These point
126to structures with the cursor position in the window, option values, the file
127name, etc. These are defined in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200128[`structs.h`](https://github.com/vim/vim/blob/master/src/structs.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100129
130All the global variables are declared in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200131[`globals.h`](https://github.com/vim/vim/blob/master/src/globals.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100132
133
134## The main loop ##
135
136This is conveniently called `main_loop()`. It updates a few things and then
137calls `normal_cmd()` to process a command. This returns when the command is
138finished.
139
140The basic idea is that Vim waits for the user to type a character and
141processes it until another character is needed. Thus there are several places
142where Vim waits for a character to be typed. The `vgetc()` function is used
143for this. It also handles mapping.
144
145Updating the screen is mostly postponed until a command or a sequence of
146commands has finished. The work is done by `update_screen()`, which calls
147`win_update()` for every window, which calls `win_line()` for every line.
148See the start of
149[`screen.c`](https://github.com/vim/vim/blob/master/src/screen.c)
150for more explanations.
151
152
153## Command-line mode ##
154
155When typing a `:`, `normal_cmd()` will call `getcmdline()` to obtain a line
156with an Ex command. `getcmdline()` contains a loop that will handle each typed
157character. It returns when hitting `CR` or `Esc` or some other character that
158ends the command line mode.
159
160
161## Ex commands ##
162
163Ex commands are handled by the function `do_cmdline()`. It does the generic
164parsing of the `:` command line and calls `do_one_cmd()` for each separate
165command. It also takes care of while loops.
166
167`do_one_cmd()` parses the range and generic arguments and puts them in the
168`exarg_t` and passes it to the function that handles the command.
169
170The `:` commands are listed in `ex_cmds.h`. The third entry of each item is
171the name of the function that handles the command. The last entry are the
172flags that are used for the command.
173
174
175## Normal mode commands ##
176
177The Normal mode commands are handled by the `normal_cmd()` function. It also
178handles the optional count and an extra character for some commands. These
179are passed in a `cmdarg_t` to the function that handles the command.
180
181There is a table `nv_cmds` in
182[`normal.c`](https://github.com/vim/vim/blob/master/src/normal.c)
183which lists the first character of every command. The second entry of each
184item is the name of the function that handles the command.
185
186
187## Insert mode commands ##
188
189When doing an `i` or `a` command, `normal_cmd()` will call the `edit()`
190function. It contains a loop that waits for the next character and handles it.
191It returns when leaving Insert mode.
192
193
194## Options ##
195
196There is a list with all option names in
197[`option.c`](https://github.com/vim/vim/blob/master/src/option.c),
198called `options[]`.
199
200
201## The GUI ##
202
203Most of the GUI code is implemented like it was a clever terminal. Typing a
204character, moving a scrollbar, clicking the mouse, etc. are all translated
205into events which are written in the input buffer. These are read by the
206main code, just like reading from a terminal. The code for this is scattered
207through [`gui.c`](https://github.com/vim/vim/blob/master/src/gui.c).
208For example, `gui_send_mouse_event()` for a mouse click and `gui_menu_cb()` for
209a menu action. Key hits are handled by the system-specific GUI code, which
210calls `add_to_input_buf()` to send the key code.
211
212Updating the GUI window is done by writing codes in the output buffer, just
213like writing to a terminal. When the buffer gets full or is flushed,
214`gui_write()` will parse the codes and draw the appropriate items. Finally the
215system-specific GUI code will be called to do the work.
216
217
218## Debugging the GUI ##
219
220Remember to prevent that gvim forks and the debugger thinks Vim has exited,
221add the `-f` argument. In gdb: `run -f -g`.
222
223When stepping through display updating code, the focus event is triggered
224when going from the debugger to Vim and back. To avoid this, recompile with
225some code in `gui_focus_change()` disabled.
226
227
228## Contributing ##
229
230If you would like to help making Vim better, see the
231[`CONTRIBUTING.md`](https://github.com/vim/vim/blob/master/CONTRIBUTING.md)
232file.
233
234
Bram Moolenaareb490412022-06-28 13:44:46 +0100235This is `README.md` for version 9.0 of the Vim source code.