blob: a01f695d93e5ce0f17cdb29d606d5943d694f0cd [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
Hirohito Higashifbe4a8f2025-04-27 15:28:30 +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
Yegappan Lakshmanan25536f42024-05-22 16:45:04 +020052gc.c | garbage collection
Bram Moolenaarf868ba82020-07-21 21:07:20 +020053help.c | vim help related functions
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020054highlight.c | syntax highlighting
Bram Moolenaar14c01f82019-10-09 22:53:08 +020055indent.c | text indentation
Bram Moolenaar95946f12019-03-31 15:31:59 +020056insexpand.c | Insert mode completion
Bram Moolenaar054f14b2020-07-22 19:11:19 +020057locale.c | locale/language handling
Bram Moolenaarb66bab32019-08-01 14:28:24 +020058map.c | mapping and abbreviations
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020059mark.c | marks
60match.c | highlight matching
Yegappan Lakshmanan01c798c2021-06-02 17:07:18 +020061float.c | floating point functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010062mbyte.c | multi-byte character handling
63memfile.c | storing lines for buffers in a swapfile
64memline.c | storing lines for buffers in memory
65menu.c | menus
66message.c | (error) messages
Bram Moolenaarb20b9e12019-09-21 20:48:04 +020067mouse.c | handling the mouse
Bram Moolenaarac9fb182019-04-27 13:04:13 +020068ops.c | handling operators ("d", "y", "p")
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010069option.c | options
Bram Moolenaardac13472019-09-16 21:06:21 +020070optionstr.c | handling string options
Bram Moolenaar30e8e732019-09-27 13:08:36 +020071popupmenu.c | popup menu
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020072popupwin.c | popup window
Hirohito Higashifbe4a8f2025-04-27 15:28:30 +020073profiler.c | Vim script profiler
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010074quickfix.c | quickfix commands (":make", ":cn")
75regexp.c | pattern matching
Bram Moolenaar4aea03e2019-09-25 22:37:17 +020076register.c | handling registers
Bram Moolenaar307c5a52019-08-25 15:41:00 +020077scriptfile.c | runtime directory handling and sourcing scripts
Bram Moolenaarb20b9e12019-09-21 20:48:04 +020078screen.c | lower level screen functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010079search.c | pattern searching
Bram Moolenaar84538072019-07-28 14:15:42 +020080session.c | sessions and views
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010081sign.c | signs
Bram Moolenaar46a426c2019-09-27 12:41:56 +020082spell.c | spell checking core
83spellfile.c | spell file handling
84spellsuggest.c | spell correction suggestions
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020085strings.c | string manipulation functions
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +020086syntax.c | syntax and other highlighting
Bram Moolenaarac9fb182019-04-27 13:04:13 +020087tag.c | tags
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010088term.c | terminal handling, termcap codes
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020089testing.c | testing: assert and test functions
Bram Moolenaar11abd092020-05-01 14:26:37 +020090textformat.c | text formatting
Bram Moolenaared8ce052020-04-29 21:04:15 +020091textobject.c | text objects
Bram Moolenaarecaa70e2019-07-14 14:55:39 +020092textprop.c | text properties
Bram Moolenaar0a8fed62020-02-14 13:22:17 +010093time.c | time and timer functions
Hirohito Higashifbe4a8f2025-04-27 15:28:30 +020094typval.c | Vim script type/value functions
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010095undo.c | undo and redo
Bram Moolenaarac9fb182019-04-27 13:04:13 +020096usercmd.c | user defined commands
97userfunc.c | user defined functions
Bram Moolenaar1e78e692019-07-22 20:18:27 +020098viminfo.c | viminfo handling
Bram Moolenaar8ac8a772019-03-29 13:10:08 +010099window.c | handling split windows
100
101
102## Debugging ##
103
104If you have a reasonable recent version of gdb, you can use the `:Termdebug`
105command to debug Vim. See `:help :Termdebug`.
106
107When something is time critical or stepping through code is a hassle, use the
108channel logging to create a time-stamped log file. Add lines to the code like
109this:
110
111 ch_log(NULL, "Value is now %02x", value);
112
113After compiling and starting Vim, do:
114
115 :call ch_logfile('debuglog', 'w')
116
117And 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
123The current mode is stored in `State`. The values it can have are `NORMAL`,
124`INSERT`, `CMDLINE`, and a few others.
125
126The current window is `curwin`. The current buffer is `curbuf`. These point
127to structures with the cursor position in the window, option values, the file
128name, etc. These are defined in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200129[`structs.h`](https://github.com/vim/vim/blob/master/src/structs.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100130
131All the global variables are declared in
Bram Moolenaarc8cc0ad2019-04-26 21:31:38 +0200132[`globals.h`](https://github.com/vim/vim/blob/master/src/globals.h).
Bram Moolenaar8ac8a772019-03-29 13:10:08 +0100133
134
135## The main loop ##
136
137This is conveniently called `main_loop()`. It updates a few things and then
138calls `normal_cmd()` to process a command. This returns when the command is
139finished.
140
141The basic idea is that Vim waits for the user to type a character and
142processes it until another character is needed. Thus there are several places
143where Vim waits for a character to be typed. The `vgetc()` function is used
144for this. It also handles mapping.
145
146Updating the screen is mostly postponed until a command or a sequence of
147commands has finished. The work is done by `update_screen()`, which calls
148`win_update()` for every window, which calls `win_line()` for every line.
149See the start of
150[`screen.c`](https://github.com/vim/vim/blob/master/src/screen.c)
151for more explanations.
152
153
154## Command-line mode ##
155
156When typing a `:`, `normal_cmd()` will call `getcmdline()` to obtain a line
157with an Ex command. `getcmdline()` contains a loop that will handle each typed
158character. It returns when hitting `CR` or `Esc` or some other character that
159ends the command line mode.
160
161
162## Ex commands ##
163
164Ex commands are handled by the function `do_cmdline()`. It does the generic
165parsing of the `:` command line and calls `do_one_cmd()` for each separate
166command. 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
171The `:` commands are listed in `ex_cmds.h`. The third entry of each item is
172the name of the function that handles the command. The last entry are the
173flags that are used for the command.
174
175
176## Normal mode commands ##
177
178The Normal mode commands are handled by the `normal_cmd()` function. It also
179handles the optional count and an extra character for some commands. These
180are passed in a `cmdarg_t` to the function that handles the command.
181
182There is a table `nv_cmds` in
183[`normal.c`](https://github.com/vim/vim/blob/master/src/normal.c)
184which lists the first character of every command. The second entry of each
185item is the name of the function that handles the command.
186
187
188## Insert mode commands ##
189
190When doing an `i` or `a` command, `normal_cmd()` will call the `edit()`
191function. It contains a loop that waits for the next character and handles it.
192It returns when leaving Insert mode.
193
194
195## Options ##
196
197There is a list with all option names in
198[`option.c`](https://github.com/vim/vim/blob/master/src/option.c),
199called `options[]`.
200
201
202## The GUI ##
203
204Most of the GUI code is implemented like it was a clever terminal. Typing a
205character, moving a scrollbar, clicking the mouse, etc. are all translated
206into events which are written in the input buffer. These are read by the
207main code, just like reading from a terminal. The code for this is scattered
208through [`gui.c`](https://github.com/vim/vim/blob/master/src/gui.c).
209For example, `gui_send_mouse_event()` for a mouse click and `gui_menu_cb()` for
210a menu action. Key hits are handled by the system-specific GUI code, which
211calls `add_to_input_buf()` to send the key code.
212
213Updating the GUI window is done by writing codes in the output buffer, just
214like 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
216system-specific GUI code will be called to do the work.
217
218
219## Debugging the GUI ##
220
221Remember to prevent that gvim forks and the debugger thinks Vim has exited,
222add the `-f` argument. In gdb: `run -f -g`.
223
224When stepping through display updating code, the focus event is triggered
225when going from the debugger to Vim and back. To avoid this, recompile with
226some code in `gui_focus_change()` disabled.
227
228
229## Contributing ##
230
231If you would like to help making Vim better, see the
232[`CONTRIBUTING.md`](https://github.com/vim/vim/blob/master/CONTRIBUTING.md)
233file.
234
235
Christian Brabandtb4ddc6c2024-01-02 16:51:11 +0100236This is `README.md` for version 9.1 of the Vim source code.