neovim/src/nvim
Justin M. Keyes a24eff0e27 extmark: fix spelling of "Extmark"
The spelling "Extmark" is used almost everywhere, so don't spell it
"ExtMark" inconsistently.
2019-11-11 22:12:59 -08:00
..
api extmark: rename ExtendedMark => Extmark 2019-11-11 22:12:59 -08:00
eval Remove excess <stdint.h> 2019-09-11 22:27:45 -04:00
event tui: flush ui buffer in tui_terminal_after_startup (#11083) 2019-09-24 08:34:00 +02:00
generators gen_eval.lua: use correct name in usage 2019-07-30 10:37:41 +02:00
lib nsmarks: initial commit 2019-11-11 19:43:15 +01:00
lua Lua: mark some functions as "private" 2019-11-11 00:43:02 -08:00
msgpack_rpc Remove excess <stdint.h> 2019-09-11 22:27:45 -04:00
os TUI/thread: guard env map from potential race with unibilium #11259 2019-10-20 15:47:08 -07:00
po startup: fail if --embed with -es/-Es #10753 2019-09-13 15:15:59 -07:00
testdir vim-patch:8.1.0622: adding quickfix items marks items as valid errors #11373 2019-11-11 21:24:25 -08:00
tui terminal: enable pass through indexed colors to TUI 2019-11-02 11:01:58 +01:00
viml/parser clang/"dead assignments" #10620 2019-07-29 00:52:24 +02:00
xdiff vim-patch:8.1.0360: using an external diff program is slow and inflexible 2018-12-09 19:45:56 +02:00
CMakeLists.txt vim-patch 8.1.0084: user name completion does not work on MS-Windows 2019-10-13 17:34:08 +09:00
README.md doc 2019-09-09 09:53:19 -07:00
arabic.c Refactor: Remove occurences of mb_char2bytes 2018-09-09 10:45:50 +03:00
arabic.h
ascii.h build/MSVC: fix "C4028: formal parameter different from declaration" 2018-03-18 14:11:39 +01:00
assert.h viml/profile: switch to uv_gettimeofday() #10356 2019-06-29 16:39:22 +02:00
aucmd.c rename: UIAttach/UIDetach => UIEnter/UILeave 2019-09-12 17:04:05 -07:00
aucmd.h UIAttach, UIDetach 2019-09-12 17:04:05 -07:00
auevents.lua autocmd: Fix event name casing #11332 2019-11-04 16:18:06 +09:00
buffer.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
buffer.h dictwatcheradd(): support b:changedtick #9693 2019-03-14 00:38:56 +01:00
buffer_defs.h extmark: fix spelling of "Extmark" 2019-11-11 22:12:59 -08:00
buffer_updates.c lua: support getting UTF-32 and UTF-16 sizes of replaced text 2019-08-06 20:24:36 +02:00
buffer_updates.h Use autogenerated declarations 2018-05-23 22:07:27 +02:00
bufhl_defs.h buffer: add support for virtual text annotations 2018-09-17 10:41:29 +02:00
change.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
change.h includes 2019-08-07 14:21:23 +02:00
channel.c UIAttach, UIDetach 2019-09-12 15:52:54 -07:00
channel.h channel: refactor events, prevent recursive invocation of events 2019-06-18 10:49:38 +02:00
charset.c getdigits: introduce `strict`, `def` parameters 2019-09-13 19:29:25 -07:00
charset.h charset: include option_defs.h for breakat_flags 2018-06-12 09:34:10 -04:00
context.c Context: rename "buflist" => "bufs" 2019-09-14 18:57:35 -07:00
context.h Context: rename "buflist" => "bufs" 2019-09-14 18:57:35 -07:00
cursor.c vim-patch:8.1.2140: "gk" and "gj" do not work correctly in number column #11208 2019-10-17 01:24:23 -07:00
cursor.h
cursor_shape.c lint 2019-09-13 19:29:25 -07:00
cursor_shape.h
diff.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
diff.h
digraph.c vim-patch:8.1.1588: in :let-heredoc line continuation is recognized 2019-10-07 14:14:13 +02:00
digraph.h
edit.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
edit.h vim-patch:8.1.1124: insert completion flags are mixed up 2019-08-21 01:16:31 -04:00
eval.c vim-patch:8.1.2262: unpack assignment in function not recognized 2019-11-09 09:58:32 -05:00
eval.h vim-patch:8.1.1913: not easy to compute the space on the command line (#10845) 2019-08-25 20:53:17 +02:00
eval.lua vim-patch:8.1.0460: assert_fails() message argument #11051 2019-09-21 14:03:46 -07:00
ex_cmds.c doc [ci skip] 2019-11-11 22:12:59 -08:00
ex_cmds.h
ex_cmds.lua vim-patch:8.1.1256: cannot navigate through errors relative to the cursor 2019-10-27 23:43:47 -04:00
ex_cmds2.c vim-patch:8.1.1625: script line numbers are not exactly right 2019-10-13 11:54:54 +02:00
ex_cmds2.h UIAttach, UIDetach 2019-09-12 17:04:05 -07:00
ex_cmds_defs.h vim-patch:8.1.1588: in :let-heredoc line continuation is recognized 2019-10-07 14:14:13 +02:00
ex_docmd.c vim-patch:8.1.0324: off-by-one error in cmdidx check 2019-11-09 09:58:33 -05:00
ex_docmd.h
ex_eval.c Remove excess <stdint.h> 2019-09-11 22:27:45 -04:00
ex_eval.h vim-patch:8.0.1729: no comma after last enum item 2019-09-02 12:19:10 -04:00
ex_getln.c Prevent prompts during inccommand previews 2019-10-31 19:22:22 +00:00
ex_getln.h vim-patch:8.1.2188: build error for missing define 2019-10-21 07:34:05 -04:00
file_search.c vim-patch:8.1.2120: some MB_ macros are more complicated than necessary 2019-10-06 21:11:38 -04:00
file_search.h
fileio.c vim-patch:8.1.1588: in :let-heredoc line continuation is recognized 2019-10-07 14:14:13 +02:00
fileio.h aucmd_prepbuf: also restore `prevwin` #9741 2019-03-17 02:09:06 +01:00
fold.c vim-patch:8.1.1890: ml_get error when deleting fold marker 2019-08-19 21:32:24 -04:00
fold.h
func_attr.h rename: FUNC_API_ASYNC => FUNC_API_FAST 2019-06-30 11:33:01 +02:00
garray.c vim-patch:8.1.1439: ga_grow(): 1.5x growth rate #10699 2019-08-05 23:42:41 +02:00
garray.h ui: implement ext_messages 2019-02-10 13:36:46 +01:00
getchar.c vim-patch:8.1.1588: in :let-heredoc line continuation is recognized 2019-10-07 14:14:13 +02:00
getchar.h refactor: allow us to process a child queue only while waiting on input 2019-09-08 15:24:14 +02:00
gettext.h mac: do not use libintl_setlocale() (#9789) 2019-03-26 11:42:25 +01:00
globals.h vim-patch:8.1.1228: not possible to process tags with a function 2019-10-22 20:13:02 +01:00
grid_defs.h screen: add some documentation of internals of msg_grid implementation 2019-09-01 18:53:55 +02:00
hardcopy.c getdigits: introduce `strict`, `def` parameters 2019-09-13 19:29:25 -07:00
hardcopy.h hardcopy: bold,italic,underline are TriState 2018-08-01 15:28:50 -04:00
hashtab.c vim-patch:8.0.0074 2017-12-15 15:50:58 -05:00
hashtab.h Perform HASHTAB_ITER bookkeeping before user-code 2019-10-22 19:55:55 +01:00
highlight.c highlight: correctly disable index attribute with combine/blend 2019-11-02 11:01:58 +01:00
highlight.h UI: implement 'pumblend' option for semi-transparent popupmenu 2019-02-07 16:43:51 +01:00
highlight_defs.h terminal: enable pass through indexed colors to TUI 2019-11-02 11:01:58 +01:00
iconv.h remove DYNAMIC_ICONV 2019-08-06 12:44:22 +09:00
if_cscope.c cleanup: remove mch_fopen in favor of os_fopen 2019-07-25 22:32:23 +02:00
if_cscope.h
if_cscope_defs.h
indent.c includes 2019-08-07 14:21:23 +02:00
indent.h
indent_c.c lint 2019-09-13 19:29:25 -07:00
indent_c.h
keymap.c vim-patch:8.1.0362: cannot get the script line number when executing a function 2019-09-04 13:40:04 +09:00
keymap.h paste: WIP #4448 2019-08-27 21:19:10 +02:00
log.c Remove excess <stdint.h> 2019-09-11 22:27:45 -04:00
log.h build: include auto/config.h explicitly 2019-01-20 00:18:39 +01:00
macros.h vim-patch:8.1.2120: some MB_ macros are more complicated than necessary 2019-10-06 21:11:38 -04:00
main.c lua: vim.rpcrequest, vim.rpcnotify, vim.NIL 2019-11-10 13:08:05 +01:00
main.h
map.c extmark: review changes 2019-11-11 20:18:15 +01:00
map.h extmark: review changes 2019-11-11 20:18:15 +01:00
map_defs.h
mark.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
mark.h
mark_defs.h
mark_extended.c extmark: fix spelling of "Extmark" 2019-11-11 22:12:59 -08:00
mark_extended.h extmark: fix spelling of "Extmark" 2019-11-11 22:12:59 -08:00
mark_extended_defs.h extmark: fix spelling of "Extmark" 2019-11-11 22:12:59 -08:00
math.c build: Fix -Wconversion warnings for fpclassify et al 2019-01-21 00:32:34 +01:00
math.h build: Fix -Wconversion warnings for fpclassify et al 2019-01-21 00:32:34 +01:00
mbyte.c vim-patch:8.0.1550: various small problems in source files 2019-09-07 10:35:09 -04:00
mbyte.h remove DYNAMIC_ICONV 2019-08-06 12:44:22 +09:00
memfile.c vim-patch:8.0.1496: VIM_CLEAR() 2019-05-25 10:07:05 +02:00
memfile.h
memfile_defs.h
memline.c vim-patch:8.1.0014: qf_init_ext() is too long 2019-10-23 01:33:15 -04:00
memline.h
memline_defs.h doc [ci skip] #9478 2019-01-26 20:38:14 +01:00
memory.c vim-patch:8.1.0488: using freed memory in quickfix code 2019-10-27 16:17:35 -04:00
memory.h refactor: introduce XFREE_CLEAR() 2019-05-25 10:01:17 +02:00
menu.c lint 2019-09-13 19:29:25 -07:00
menu.h
message.c ex_echo: fix check for got_int #11225 2019-10-19 14:45:27 -07:00
message.h screen: add some documentation of internals of msg_grid implementation 2019-09-01 18:53:55 +02:00
misc1.c nsmarks: initial commit 2019-11-11 19:43:15 +01:00
misc1.h
mouse.c screen: use dedicated message grid 2019-09-01 15:55:10 +02:00
mouse.h
move.c refactor: wrap common plines() usage in plines_win_full() #11141 2019-10-02 23:06:05 -07:00
move.h move: dir in onepage() is Direction 2018-09-10 23:16:16 -04:00
normal.c vim-patch:8.1.2231: introduce gM command #11321 2019-11-01 13:43:20 +09:00
normal.h
ops.c extmark: review changes 2019-11-11 20:18:15 +01:00
ops.h
option.c vim-patch:8.1.1228: not possible to process tags with a function 2019-10-22 20:13:02 +01:00
option.h
option_defs.h vim-patch:8.1.1228: not possible to process tags with a function 2019-10-22 20:13:02 +01:00
options.lua vim-patch:8.1.1228: not possible to process tags with a function 2019-10-22 20:13:02 +01:00
os_unix.c os_can_exe: remove char_u 2019-07-21 20:51:37 +02:00
os_unix.h
path.c vim-patch:8.1.2120: some MB_ macros are more complicated than necessary 2019-10-06 21:11:38 -04:00
path.h vim-patch:8.1.1371: cannot recover from a swap file #11081 2019-10-05 20:35:48 -07:00
popupmnu.c vim-patch:8.0.1309: cannot use 'balloonexpr' in a terminal #10983 2019-09-09 21:23:44 -07:00
popupmnu.h autocmd: add MenuPopupChanged autocmd 2019-03-15 04:24:41 +08:00
pos.h nsmarks: initial commit 2019-11-11 19:43:15 +01:00
profile.c reltimefloat(): allow negative result #10544 2019-07-20 10:46:09 +02:00
profile.h viml/profile: revert proftime_T to unsigned type #10521 2019-07-16 20:10:08 +02:00
quickfix.c vim-patch:8.1.0622: adding quickfix items marks items as valid errors #11373 2019-11-11 21:24:25 -08:00
quickfix.h
rbuffer.c channels: allow bytes sockets and stdio, and buffered bytes output 2017-11-24 14:54:15 +01:00
rbuffer.h
regexp.c lint 2019-09-13 19:29:25 -07:00
regexp.h vim-patch:8.1.0098: segfault when pattern with \z() is very slow (#9283) 2018-11-29 01:51:26 +01:00
regexp_defs.h vim-patch:8.1.1194: typos and small problems in source files 2019-04-26 07:01:18 -04:00
regexp_nfa.c vim-patch:8.1.2120: some MB_ macros are more complicated than necessary 2019-10-06 21:11:38 -04:00
screen.c vim-patch:8.1.2120: some MB_ macros are more complicated than necessary 2019-10-06 21:11:38 -04:00
screen.h window/ui: reorganize size variables, fix terminal window size with multigrid. 2019-01-27 12:07:06 +01:00
search.c vim-patch:8.1.2244: 'wrapscan' is not used for "gn" 2019-11-09 09:58:31 -05:00
search.h vim-patch:8.1.2173: searchit() has too many arguments 2019-10-27 03:35:19 +09:00
sha256.c
sha256.h
shada.c Remove excess <stdint.h> 2019-09-11 22:27:45 -04:00
shada.h API: Context 2019-07-27 16:36:57 +02:00
sign.c getdigits: introduce `strict`, `def` parameters 2019-09-13 19:29:25 -07:00
sign.h lint 2019-05-26 19:32:32 +02:00
sign_defs.h lint 2019-05-26 19:32:32 +02:00
spell.c vim-patch:8.1.2173: searchit() has too many arguments 2019-10-27 03:35:19 +09:00
spell.h
spell_defs.h vim-patch:8.0.1490: number of spell regions is spread out through the code 2018-08-10 12:35:38 -04:00
spellfile.c vim-patch:8.1.2268: spell file flag zero is not recognized 2019-11-09 09:58:32 -05:00
spellfile.h
state.c refactor: allow us to process a child queue only while waiting on input 2019-09-08 15:24:14 +02:00
state.h
strings.c vim-patch:8.1.2083: multi-byte chars do not work properly with "%.*S" in printf() (#11106) 2019-09-28 00:32:22 +02:00
strings.h buffer: PVS/V557: Refactor maketitle() 2018-04-09 10:29:28 +03:00
syntax.c syntax: zero-init local structs 2019-11-02 14:38:28 -04:00
syntax.h syntax.h: fix include #8742 2018-08-05 01:50:25 +02:00
syntax_defs.h syntax: disptick_T is uint16_t 2018-07-29 18:03:16 -04:00
tag.c vim-patch:8.1.2173: searchit() has too many arguments 2019-10-27 03:35:19 +09:00
tag.h vim-patch:8.1.1228: not possible to process tags with a function 2019-10-22 20:13:02 +01:00
terminal.c terminal: preserve support for g:terminal_color_X = "#1234ab" 2019-11-02 11:01:58 +01:00
terminal.h refactor/single-include: terminal.h 2017-10-19 11:14:54 +02:00
types.h paste: WIP #4448 2019-08-27 21:19:10 +02:00
ugrid.c PVS/V1028 ugrid.c:76 (#10495) 2019-07-14 15:46:07 +02:00
ugrid.h UGRID_FOREACH_CELL: avoid shadowed variables 2019-01-02 21:06:37 +01:00
ui.c rename: UIAttach/UIDetach => UIEnter/UILeave 2019-09-12 17:04:05 -07:00
ui.h Merge pull request #10475 from erw7/fix-ext-popupmenu 2019-09-08 20:42:32 +02:00
ui_bridge.c API/nvim_list_uis(): include "chan" field for TUI 2019-09-12 17:04:05 -07:00
ui_bridge.h
ui_compositor.c screen: don't crash on invalid grid cells being recomposed 2019-09-30 19:05:54 +02:00
ui_compositor.h doc [ci skip] 2019-03-26 19:55:33 +01:00
undo.c extmark: review changes 2019-11-11 20:18:15 +01:00
undo.h refactor/single-include: undo.h 2017-10-19 11:20:08 +02:00
undo_defs.h nsmarks: initial commit 2019-11-11 19:43:15 +01:00
version.c vim-patch:8.1.2218: "gN" is off by one in Visual mode 2019-10-27 03:35:19 +09:00
version.h
vim.h vim-patch:8.0.1768: SET_NO_HLSEARCH() used in a wrong way 2019-09-02 18:40:56 -04:00
window.c vim-patch:8.1.0434: copy_loclist() is too long 2019-10-23 01:33:16 -04:00
window.h

README.md

Nvim core

Module-specific details are documented at the top of each module (terminal.c, screen.c, …).

See :help dev for guidelines.

Filename conventions

The source files use extensions to hint about their purpose.

  • *.c, *.generated.c - full C files, with all includes, etc.
  • *.c.h - parametrized C files, contain all necessary includes, but require defining macros before actually using. Example: typval_encode.c.h
  • *.h - full headers, with all includes. Does not apply to *.generated.h.
  • *.h.generated.h - exported functions declarations.
  • *.c.generated.h - static functions declarations.

Logs

Low-level log messages sink to $NVIM_LOG_FILE.

Use LOG_CALLSTACK() (Linux only) to log the current stacktrace. To log to an alternate file (e.g. stderr) use LOG_CALLSTACK_TO_FILE(FILE*).

UI events are logged at DEBUG level (DEBUG_LOG_LEVEL).

rm -rf build/
make CMAKE_EXTRA_FLAGS="-DMIN_LOG_LEVEL=0"

Many log messages have a shared prefix, such as "UI" or "RPC". Use the shell to filter the log, e.g. at DEBUG level you might want to exclude UI messages:

tail -F ~/.local/share/nvim/log | cat -v | stdbuf -o0 grep -v UI | stdbuf -o0 tee -a log

Build with ASAN

Building Nvim with Clang sanitizers (Address Sanitizer: ASan, Undefined Behavior Sanitizer: UBSan, Memory Sanitizer: MSan, Thread Sanitizer: TSan) is a good way to catch undefined behavior, leaks and other errors as soon as they happen. It's significantly faster than Valgrind.

Requires clang 3.4 or later, and llvm-symbolizer must be in $PATH:

clang --version

Build Nvim with sanitizer instrumentation (choose one):

CC=clang make CMAKE_EXTRA_FLAGS="-DCLANG_ASAN_UBSAN=ON"
CC=clang make CMAKE_EXTRA_FLAGS="-DCLANG_MSAN=ON"
CC=clang make CMAKE_EXTRA_FLAGS="-DCLANG_TSAN=ON"

Create a directory to store logs:

mkdir -p "$HOME/logs"

Configure the sanitizer(s) via these environment variables:

# Change to detect_leaks=1 to detect memory leaks (slower).
export ASAN_OPTIONS="detect_leaks=0:log_path=$HOME/logs/asan"
# Show backtraces in the logs.
export UBSAN_OPTIONS=print_stacktrace=1
export MSAN_OPTIONS="log_path=${HOME}/logs/tsan"
export TSAN_OPTIONS="log_path=${HOME}/logs/tsan"

Logs will be written to ${HOME}/logs/*san.PID then.

For more information: https://github.com/google/sanitizers/wiki/SanitizerCommonFlags

TUI debugging

TUI troubleshoot

Nvim logs its internal terminfo state at 'verbose' level 3. This makes it possible to see exactly what terminfo values Nvim is using on any system.

nvim -V3log

TUI trace

The ancient script command is still the "state of the art" for tracing terminal behavior. The libvterm vterm-dump utility formats the result for human-readability.

Record a Nvim terminal session and format it with vterm-dump:

script foo
./build/bin/nvim -u NONE
# Exit the script session with CTRL-d

# Use `vterm-dump` utility to format the result.
./.deps/usr/bin/vterm-dump foo > bar

Then you can compare bar with another session, to debug TUI behavior.

TUI redraw

Set the 'writedelay' option to see where and when the UI is painted.

:set writedelay=1

Terminal reference

Nvim lifecycle

Following describes how Nvim processes input.

Consider a typical Vim-like editing session:

  1. Vim displays the welcome screen
  2. User types: :
  3. Vim enters command-line mode
  4. User types: edit README.txt<CR>
  5. Vim opens the file and returns to normal mode
  6. User types: G
  7. Vim navigates to the end of the file
  8. User types: 5
  9. Vim enters count-pending mode
  10. User types: d
  11. Vim enters operator-pending mode
  12. User types: w
  13. Vim deletes 5 words
  14. User types: g
  15. Vim enters the "g command mode"
  16. User types: g
  17. Vim goes to the beginning of the file
  18. User types: i
  19. Vim enters insert mode
  20. User types: word<ESC>
  21. Vim inserts "word" at the beginning and returns to normal mode

Note that we split user actions into sequences of inputs that change the state of the editor. While there's no documentation about a "g command mode" (step 16), internally it is implemented similarly to "operator-pending mode".

From this we can see that Vim has the behavior of an input-driven state machine (more specifically, a pushdown automaton since it requires a stack for transitioning back from states). Assuming each state has a callback responsible for handling keys, this pseudocode represents the main program loop:

def state_enter(state_callback, data):
  do
    key = readkey()                 # read a key from the user
  while state_callback(data, key)   # invoke the callback for the current state

That is, each state is entered by calling state_enter and passing a state-specific callback and data. Here is a high-level pseudocode for a program that implements something like the workflow described above:

def main()
  state_enter(normal_state, {}):

def normal_state(data, key):
  if key == ':':
    state_enter(command_line_state, {})
  elif key == 'i':
    state_enter(insert_state, {})
  elif key == 'd':
    state_enter(delete_operator_state, {})
  elif key == 'g':
    state_enter(g_command_state, {})
  elif is_number(key):
    state_enter(get_operator_count_state, {'count': key})
  elif key == 'G'
    jump_to_eof()
  return true

def command_line_state(data, key):
  if key == '<cr>':
    if data['input']:
      execute_ex_command(data['input'])
    return false
  elif key == '<esc>'
    return false

  if not data['input']:
    data['input'] = ''

  data['input'] += key
  return true

def delete_operator_state(data, key):
  count = data['count'] or 1
  if key == 'w':
    delete_word(count)
  elif key == '$':
    delete_to_eol(count)
  return false  # return to normal mode

def g_command_state(data, key):
  if key == 'g':
    go_top()
  elif key == 'v':
    reselect()
  return false  # return to normal mode

def get_operator_count_state(data, key):
  if is_number(key):
    data['count'] += key
    return true
  unshift_key(key)  # return key to the input buffer
  state_enter(delete_operator_state, data)
  return false

def insert_state(data, key):
  if key == '<esc>':
    return false  # exit insert mode
  self_insert(key)
  return true

The above gives an idea of how Nvim is organized internally. Some states like the g_command_state or get_operator_count_state do not have a dedicated state_enter callback, but are implicitly embedded into other states (this will change later as we continue the refactoring effort). To start reading the actual code, here's the recommended order:

  1. state_enter() function (state.c). This is the actual program loop, note that a VimState structure is used, which contains function pointers for the callback and state data.
  2. main() function (main.c). After all startup, normal_enter is called at the end of function to enter normal mode.
  3. normal_enter() function (normal.c) is a small wrapper for setting up the NormalState structure and calling state_enter.
  4. normal_check() function (normal.c) is called before each iteration of normal mode.
  5. normal_execute() function (normal.c) is called when a key is read in normal mode.

The basic structure described for normal mode in 3, 4 and 5 is used for other modes managed by the state_enter loop:

  • command-line mode: command_line_{enter,check,execute}()(ex_getln.c)
  • insert mode: insert_{enter,check,execute}()(edit.c)
  • terminal mode: terminal_{enter,execute}()(terminal.c)

Async event support

One of the features Nvim added is the support for handling arbitrary asynchronous events, which can include:

  • RPC requests
  • job control callbacks
  • timers

Nvim implements this functionality by entering another event loop while waiting for characters, so instead of:

def state_enter(state_callback, data):
  do
    key = readkey()                 # read a key from the user
  while state_callback(data, key)   # invoke the callback for the current state

Nvim program loop is more like:

def state_enter(state_callback, data):
  do
    event = read_next_event()       # read an event from the operating system
  while state_callback(data, event) # invoke the callback for the current state

where event is something the operating system delivers to us, including (but not limited to) user input. The read_next_event() part is internally implemented by libuv, the platform layer used by Nvim.

Since Nvim inherited its code from Vim, the states are not prepared to receive "arbitrary events", so we use a special key to represent those (When a state receives an "arbitrary event", it normally doesn't do anything other update the screen).

Main loop

The Loop structure (which describes main_loop) abstracts multiple queues into one loop:

uv_loop_t uv;
MultiQueue *events;
MultiQueue *thread_events;
MultiQueue *fast_events;

loop_poll_events checks Loop.uv and Loop.fast_events whenever Nvim is idle, and also at os_breakcheck intervals.

MultiQueue is cool because you can attach throw-away "child queues" trivially. For example do_os_system() does this (for every spawned process!) to automatically route events onto the main_loop:

Process *proc = &uvproc.process;
MultiQueue *events = multiqueue_new_child(main_loop.events);
proc->events = events;