Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame^] | 1 | README for the Vim source code |
| 2 | |
| 3 | Here are a few hints for finding your way around the source code. This |
| 4 | doesn't make it less complex than it is, but it gets you started. |
| 5 | |
| 6 | You might also want to read ":help development". |
| 7 | |
| 8 | |
| 9 | JUMPING AROUND |
| 10 | |
| 11 | First of all, use ":make tags" to generate a tags file, so that you can use |
| 12 | the ":tag" command to jump around the source code. |
| 13 | |
| 14 | To jump to a function or variable definition, move the cursor on the name and |
| 15 | use the CTRL-] command. Use CTRL-T or CTRL-O to jump back. |
| 16 | |
| 17 | To jump to a file, move the cursor on its name and use the "gf" command. |
| 18 | |
| 19 | Most 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 |
| 27 | memfile.c storing lines for buffers in a swapfile |
| 28 | memline.c storing lines for buffers in memory |
| 29 | menu.c menus |
| 30 | message.c (error) messages |
| 31 | mbyte.c multy-byte character handling |
| 32 | ops.c handling operators ("d", "y", "p") |
| 33 | option.c options |
| 34 | quickfix.c quickfix commands (":make", ":cn") |
| 35 | screen.c updating the windows |
| 36 | search.c pattern searching |
| 37 | tag.c tags |
| 38 | term.c terminal handling, termcap codes |
| 39 | undo.c undo and redo |
| 40 | window.c handling split windows |
| 41 | |
| 42 | |
| 43 | IMPORTANT VARIABLES |
| 44 | |
| 45 | The current mode is stored in "State". The values it can have are NORMAL, |
| 46 | INSERT, CMDLINE, and a few others. |
| 47 | |
| 48 | The current window is "curwin". The current buffer is "curbuf". These point |
| 49 | to structures with the cursor position in the window, option values, the file |
| 50 | name, etc. These are defined in structs.h. |
| 51 | |
| 52 | All the global variables are declared in globals.h. |
| 53 | |
| 54 | |
| 55 | THE MAIN LOOP |
| 56 | |
| 57 | This is conveniently called main_loop(). It updates a few things and then |
| 58 | calls normal_cmd() to process a command. This returns when the command is |
| 59 | finished. |
| 60 | |
| 61 | The basic idea is that Vim waits for the user to type a character and |
| 62 | processes it until another character is needed. Thus there are several places |
| 63 | where Vim waits for a character to be typed. The vgetc() function is used for |
| 64 | this. It also handles mapping. |
| 65 | |
| 66 | Updating the screen is mostly postponed until a command or a sequence of |
| 67 | commands has finished. The work is done by update_screen(), which calls |
| 68 | win_update() for every window, which calls win_line() for every line. |
| 69 | See the start of screen.c for more explanations. |
| 70 | |
| 71 | |
| 72 | COMMAND-LINE MODE |
| 73 | |
| 74 | When typing a ":", normal_cmd() will call getcmdline() to obtain a line with |
| 75 | an Ex command. getcmdline() contains a loop that will handle each typed |
| 76 | character. It returns when hitting <CR> or <Esc> or some other character that |
| 77 | ends the command line mode. |
| 78 | |
| 79 | |
| 80 | EX COMMANDS |
| 81 | |
| 82 | Ex commands are handled by the function do_cmdline(). It does the generic |
| 83 | parsing of the ":" command line and calls do_one_cmd() for each separate |
| 84 | command. It also takes care of while loops. |
| 85 | |
| 86 | do_one_cmd() parses the range and generic arguments and puts them in the |
| 87 | exarg_t and passes it to the function that handles the command. |
| 88 | |
| 89 | The ":" commands are listed in ex_cmds.h. The third entry of each item is the |
| 90 | name of the function that handles the command. The last entry are the flags |
| 91 | that are used for the command. |
| 92 | |
| 93 | |
| 94 | NORMAL MODE COMMANDS |
| 95 | |
| 96 | The Normal mode commands are handled by the normal_cmd() function. It also |
| 97 | handles the optional count and an extra character for some commands. These |
| 98 | are passed in a cmdarg_t to the function that handles the command. |
| 99 | |
| 100 | There is a table nv_cmds in normal.c which lists the first character of every |
| 101 | command. The second entry of each item is the name of the function that |
| 102 | handles the command. |
| 103 | |
| 104 | |
| 105 | INSERT MODE COMMANDS |
| 106 | |
| 107 | When doing an "i" or "a" command, normal_cmd() will call the edit() function. |
| 108 | It contains a loop that waits for the next character and handles it. It |
| 109 | returns when leaving Insert mode. |
| 110 | |
| 111 | |
| 112 | OPTIONS |
| 113 | |
| 114 | There is a list with all option names in option.c, called options[]. |
| 115 | |
| 116 | |
| 117 | THE GUI |
| 118 | |
| 119 | Most of the GUI code is implemented like it was a clever terminal. Typing a |
| 120 | character, moving a scrollbar, clicking the mouse, etc. are all translated |
| 121 | into events which are written in the input buffer. These are read by the |
| 122 | main code, just like reading from a terminal. The code for this is scattered |
| 123 | through gui.c. For example: gui_send_mouse_event() for a mouse click and |
| 124 | gui_menu_cb() for a menu action. Key hits are handled by the system-specific |
| 125 | GUI code, which calls add_to_input_buf() to send the key code. |
| 126 | |
| 127 | Updating the GUI window is done by writing codes in the output buffer, just |
| 128 | like writing to a terminal. When the buffer gets full or is flushed, |
| 129 | gui_write() will parse the codes and draw the appropriate items. Finally the |
| 130 | system-specific GUI code will be called to do the work. |
| 131 | |
| 132 | |
| 133 | DEBUGGING THE GUI |
| 134 | |
| 135 | Remember to prevent that gvim forks and the debugger thinks Vim has exited, |
| 136 | add the "-f" argument. In gdb: "run -f -g". |
| 137 | |
| 138 | When stepping through display updating code, the focus event is triggerred |
| 139 | when going from the debugger to Vim and back. To avoid this, recompile with |
| 140 | some code in gui_focus_change() disabled. |