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