blob: e134306015fc4a9f00e967cbc5850eee9506ab4b [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 Moolenaara1ba8112005-06-28 23:23:32 +000038 spell.c spell checking
39 syntax.c syntax and other highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 tag.c tags
41 term.c terminal handling, termcap codes
42 undo.c undo and redo
43 window.c handling split windows
44
45
Bram Moolenaar792f0e32018-02-27 17:27:13 +010046DEBUGGING
47
48If you have a reasonable recent version of gdb, you can use the :Termdebug
49command to debug Vim. See ":help :Termdebug".
50
51When something is time critical or stepping through code is a hassle, use the
52channel logging to create a time-stamped log file. Add lines to the code like
53this:
54 ch_log(NULL, "Value is now %02x", value);
55After compiling and starting Vim, do:
56 :call ch_logfile('debuglog', 'w')
57And edit "debuglog" to see what happens. The channel functions already have
58ch_log() calls, thus you always see that in the log.
59
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061IMPORTANT VARIABLES
62
63The current mode is stored in "State". The values it can have are NORMAL,
64INSERT, CMDLINE, and a few others.
65
66The current window is "curwin". The current buffer is "curbuf". These point
67to structures with the cursor position in the window, option values, the file
68name, etc. These are defined in structs.h.
69
70All the global variables are declared in globals.h.
71
72
73THE MAIN LOOP
74
75This is conveniently called main_loop(). It updates a few things and then
76calls normal_cmd() to process a command. This returns when the command is
77finished.
78
79The basic idea is that Vim waits for the user to type a character and
80processes it until another character is needed. Thus there are several places
81where Vim waits for a character to be typed. The vgetc() function is used for
82this. It also handles mapping.
83
84Updating the screen is mostly postponed until a command or a sequence of
85commands has finished. The work is done by update_screen(), which calls
86win_update() for every window, which calls win_line() for every line.
87See the start of screen.c for more explanations.
88
89
90COMMAND-LINE MODE
91
92When typing a ":", normal_cmd() will call getcmdline() to obtain a line with
93an Ex command. getcmdline() contains a loop that will handle each typed
94character. It returns when hitting <CR> or <Esc> or some other character that
95ends the command line mode.
96
97
98EX COMMANDS
99
100Ex commands are handled by the function do_cmdline(). It does the generic
101parsing of the ":" command line and calls do_one_cmd() for each separate
102command. It also takes care of while loops.
103
104do_one_cmd() parses the range and generic arguments and puts them in the
105exarg_t and passes it to the function that handles the command.
106
107The ":" commands are listed in ex_cmds.h. The third entry of each item is the
108name of the function that handles the command. The last entry are the flags
109that are used for the command.
110
111
112NORMAL MODE COMMANDS
113
114The Normal mode commands are handled by the normal_cmd() function. It also
115handles the optional count and an extra character for some commands. These
116are passed in a cmdarg_t to the function that handles the command.
117
118There is a table nv_cmds in normal.c which lists the first character of every
119command. The second entry of each item is the name of the function that
120handles the command.
121
122
123INSERT MODE COMMANDS
124
125When doing an "i" or "a" command, normal_cmd() will call the edit() function.
126It contains a loop that waits for the next character and handles it. It
127returns when leaving Insert mode.
128
129
130OPTIONS
131
132There is a list with all option names in option.c, called options[].
133
134
135THE GUI
136
137Most of the GUI code is implemented like it was a clever terminal. Typing a
138character, moving a scrollbar, clicking the mouse, etc. are all translated
139into events which are written in the input buffer. These are read by the
140main code, just like reading from a terminal. The code for this is scattered
141through gui.c. For example: gui_send_mouse_event() for a mouse click and
142gui_menu_cb() for a menu action. Key hits are handled by the system-specific
143GUI code, which calls add_to_input_buf() to send the key code.
144
145Updating the GUI window is done by writing codes in the output buffer, just
146like writing to a terminal. When the buffer gets full or is flushed,
147gui_write() will parse the codes and draw the appropriate items. Finally the
148system-specific GUI code will be called to do the work.
149
150
151DEBUGGING THE GUI
152
153Remember to prevent that gvim forks and the debugger thinks Vim has exited,
154add the "-f" argument. In gdb: "run -f -g".
155
Bram Moolenaar82038d72007-05-10 17:15:45 +0000156When stepping through display updating code, the focus event is triggered
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157when going from the debugger to Vim and back. To avoid this, recompile with
158some code in gui_focus_change() disabled.