blob: 6e7094cec0909dfb44dc7a9a1ec3d69b942a3d5f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001README for the Vim source code
2
3Here are a few hints for finding your way around the source code. This
4doesn't make it less complex than it is, but it gets you started.
5
6You might also want to read ":help development".
7
8
9JUMPING AROUND
10
Bram Moolenaar792f0e32018-02-27 17:27:13 +010011First of all, use ":make tags" to generate a tags file, so that you can jump
12around in the source code.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14To jump to a function or variable definition, move the cursor on the name and
15use the CTRL-] command. Use CTRL-T or CTRL-O to jump back.
16
17To jump to a file, move the cursor on its name and use the "gf" command.
18
19Most code can be found in a file with an obvious name (incomplete list):
20 buffer.c manipulating buffers (loaded files)
21 diff.c diff mode (vimdiff)
22 eval.c expression evaluation
23 fileio.c reading and writing files
24 fold.c folding
25 getchar.c getting characters and key mapping
26 mark.c marks
Bram Moolenaarfff2bee2010-05-15 13:56:02 +020027 mbyte.c multi-byte character handling
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 memfile.c storing lines for buffers in a swapfile
29 memline.c storing lines for buffers in memory
30 menu.c menus
31 message.c (error) messages
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 ops.c handling operators ("d", "y", "p")
33 option.c options
34 quickfix.c quickfix commands (":make", ":cn")
Bram Moolenaara1ba8112005-06-28 23:23:32 +000035 regexp.c pattern matching
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 screen.c updating the windows
37 search.c pattern searching
Bram Moolenaarbbea4702019-01-01 13:20:31 +010038 sign.c signs
Bram Moolenaara1ba8112005-06-28 23:23:32 +000039 spell.c spell checking
40 syntax.c syntax and other highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 tag.c tags
42 term.c terminal handling, termcap codes
43 undo.c undo and redo
44 window.c handling split windows
45
46
Bram Moolenaar792f0e32018-02-27 17:27:13 +010047DEBUGGING
48
49If you have a reasonable recent version of gdb, you can use the :Termdebug
50command to debug Vim. See ":help :Termdebug".
51
52When something is time critical or stepping through code is a hassle, use the
53channel logging to create a time-stamped log file. Add lines to the code like
54this:
55 ch_log(NULL, "Value is now %02x", value);
56After compiling and starting Vim, do:
57 :call ch_logfile('debuglog', 'w')
58And edit "debuglog" to see what happens. The channel functions already have
59ch_log() calls, thus you always see that in the log.
60
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062IMPORTANT VARIABLES
63
64The current mode is stored in "State". The values it can have are NORMAL,
65INSERT, CMDLINE, and a few others.
66
67The current window is "curwin". The current buffer is "curbuf". These point
68to structures with the cursor position in the window, option values, the file
69name, etc. These are defined in structs.h.
70
71All the global variables are declared in globals.h.
72
73
74THE MAIN LOOP
75
76This is conveniently called main_loop(). It updates a few things and then
77calls normal_cmd() to process a command. This returns when the command is
78finished.
79
80The basic idea is that Vim waits for the user to type a character and
81processes it until another character is needed. Thus there are several places
82where Vim waits for a character to be typed. The vgetc() function is used for
83this. It also handles mapping.
84
85Updating the screen is mostly postponed until a command or a sequence of
86commands has finished. The work is done by update_screen(), which calls
87win_update() for every window, which calls win_line() for every line.
88See the start of screen.c for more explanations.
89
90
91COMMAND-LINE MODE
92
93When typing a ":", normal_cmd() will call getcmdline() to obtain a line with
94an Ex command. getcmdline() contains a loop that will handle each typed
95character. It returns when hitting <CR> or <Esc> or some other character that
96ends the command line mode.
97
98
99EX COMMANDS
100
101Ex commands are handled by the function do_cmdline(). It does the generic
102parsing of the ":" command line and calls do_one_cmd() for each separate
103command. It also takes care of while loops.
104
105do_one_cmd() parses the range and generic arguments and puts them in the
106exarg_t and passes it to the function that handles the command.
107
108The ":" commands are listed in ex_cmds.h. The third entry of each item is the
109name of the function that handles the command. The last entry are the flags
110that are used for the command.
111
112
113NORMAL MODE COMMANDS
114
115The Normal mode commands are handled by the normal_cmd() function. It also
116handles the optional count and an extra character for some commands. These
117are passed in a cmdarg_t to the function that handles the command.
118
119There is a table nv_cmds in normal.c which lists the first character of every
120command. The second entry of each item is the name of the function that
121handles the command.
122
123
124INSERT MODE COMMANDS
125
126When doing an "i" or "a" command, normal_cmd() will call the edit() function.
127It contains a loop that waits for the next character and handles it. It
128returns when leaving Insert mode.
129
130
131OPTIONS
132
133There is a list with all option names in option.c, called options[].
134
135
136THE GUI
137
138Most of the GUI code is implemented like it was a clever terminal. Typing a
139character, moving a scrollbar, clicking the mouse, etc. are all translated
140into events which are written in the input buffer. These are read by the
141main code, just like reading from a terminal. The code for this is scattered
142through gui.c. For example: gui_send_mouse_event() for a mouse click and
143gui_menu_cb() for a menu action. Key hits are handled by the system-specific
144GUI code, which calls add_to_input_buf() to send the key code.
145
146Updating the GUI window is done by writing codes in the output buffer, just
147like writing to a terminal. When the buffer gets full or is flushed,
148gui_write() will parse the codes and draw the appropriate items. Finally the
149system-specific GUI code will be called to do the work.
150
151
152DEBUGGING THE GUI
153
154Remember to prevent that gvim forks and the debugger thinks Vim has exited,
155add the "-f" argument. In gdb: "run -f -g".
156
Bram Moolenaar82038d72007-05-10 17:15:45 +0000157When stepping through display updating code, the focus event is triggered
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158when going from the debugger to Vim and back. To avoid this, recompile with
159some code in gui_focus_change() disabled.