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