neovim/src/nvim
zeertzjq d6a1e71881
vim-patch:8.1.1038: Arabic support excludes Farsi (#19285)
Problem:    Arabic support excludes Farsi.
Solution:   Add Farsi support to the Arabic support. (Ali Gholami Rudi,
            Ameretat Reith)
dc4fa190e7

Omit Test_shape_final_to_medial(): removed in later patches.
2022-07-10 06:01:49 +08:00
..
api feat(api): add unsilent to command APIs 2022-07-03 04:34:28 +08:00
eval vim-patch:8.2.0614: get ml_get error when deleting a line in 'completefunc' (#19244) 2022-07-07 04:47:18 +08:00
event fix(jobs): deadlock in channel.c:exit_event #19082 2022-07-02 09:14:08 -07:00
generators vim-patch:8.2.0614: get ml_get error when deleting a line in 'completefunc' (#19244) 2022-07-07 04:47:18 +08:00
lib perf(ui): reduce allocation overhead when encoding "redraw" events 2022-06-20 12:44:56 +02:00
lua Merge pull request #19205 from bfredl/luaerr 2022-07-02 23:44:26 +02:00
msgpack_rpc fix(jobs): deadlock in channel.c:exit_event #19082 2022-07-02 09:14:08 -07:00
os refactor: remove unnecessary volatile #19210 2022-07-04 03:28:59 -07:00
po vim-patch:partial:63f32603789d (#18916) 2022-06-10 08:40:32 +02:00
testdir vim-patch:8.2.2917: spellfile functionality not fully tested 2022-07-09 20:33:00 +08:00
tui fix(tui): add fixups for hterm family #19078 2022-07-08 06:06:27 -07:00
viml/parser refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
arabic.c vim-patch:8.1.1038: Arabic support excludes Farsi (#19285) 2022-07-10 06:01:49 +08:00
arabic.h vim-patch:8.1.1038: Arabic support excludes Farsi (#19285) 2022-07-10 06:01:49 +08:00
ascii.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
assert.h ci: lint with uncrustify #18563 2022-05-20 20:41:57 -07:00
auevents.lua feat(lsp): add LspAttach and LspDetach autocommands 2022-05-18 11:21:00 -06:00
autocmd.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
autocmd.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
buffer_defs.h refactor: replace char_u 2022-07-02 16:01:27 +02:00
buffer_updates.c feat: add preview functionality to user commands 2022-05-31 20:55:05 +06:00
buffer_updates.h refactor(build): remove unused includes #17078 2022-04-26 21:38:12 -07:00
buffer.c vim-patch:8.2.5043: can open a cmdline window from a substitute expression 2022-07-07 07:00:11 +08:00
buffer.h refactor(build): remove unused includes #17078 2022-04-26 21:38:12 -07:00
change.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
change.h vim-patch:8.2.5008: when 'formatoptions' contains "/" wrongly wrapping comment (#18717) 2022-05-23 21:27:57 +08:00
channel.c fix(terminal): coverity USE_AFTER_FREE #18978 2022-06-18 09:53:12 -07:00
channel.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
charset.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
charset.h
CMakeLists.txt build: add github action annotations for clint 2022-07-04 14:20:10 +01:00
context.c refactor: replace char_u variables and functions with char 2022-05-11 23:19:57 +02:00
context.h
cursor_shape.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
cursor_shape.h
cursor.c vim-patch:8.2.4969: changing text in Visual mode may cause invalid memory access 2022-05-17 08:16:37 +08:00
cursor.h
debugger.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
debugger.h
decoration_provider.c refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
decoration_provider.h
decoration.c fix(decorations): nvim_buf_set_extmark breaks conceal #19010 2022-06-19 10:33:54 -07:00
decoration.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
diff.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
diff.h
digraph.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
digraph.h refactor(uncrustify): format all c code under /src/nvim/ 2022-04-29 13:52:43 +02:00
edit.c vim-patch:8.2.0614: get ml_get error when deleting a line in 'completefunc' (#19244) 2022-07-07 04:47:18 +08:00
edit.h
eval.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
eval.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
eval.lua feat(lua): allow some viml functions to run in fast 2022-05-17 10:29:33 +01:00
ex_cmds2.c vim-patch:8.2.0151: detecting a script was already sourced is unreliable 2022-07-08 14:17:22 +08:00
ex_cmds2.h vim-patch:8.2.0151: detecting a script was already sourced is unreliable 2022-07-08 14:17:22 +08:00
ex_cmds_defs.h feat(api): make nvim_parse_cmd and nvim_cmd support :filter 2022-06-28 17:31:04 +06:00
ex_cmds.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
ex_cmds.h refactor(uncrustify): change rules to better align with the style guide 2022-04-29 14:13:06 +02:00
ex_cmds.lua vim-patch:8.1.0487: no menus specifically for the terminal window 2022-07-01 10:17:39 +08:00
ex_docmd.c vim-patch:8.2.0274: hang with combination of feedkeys(), Ex mode and :global 2022-07-08 23:11:12 +08:00
ex_docmd.h refactor(api): move command related API to separate file 2022-06-12 19:45:39 +02:00
ex_eval.c refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
ex_eval.h refactor: replace char_u variables and functions with char 2022-05-04 15:25:54 +02:00
ex_getln.c vim-patch:8.1.1038: Arabic support excludes Farsi (#19285) 2022-07-10 06:01:49 +08:00
ex_getln.h refactor: replace char_u variables and functions with char 2022-05-11 23:19:57 +02:00
ex_session.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
ex_session.h refactor(uncrustify): change rules to better align with the style guide 2022-04-29 14:13:06 +02:00
extmark_defs.h
extmark.c fix(decorations): nvim_buf_set_extmark breaks conceal #19010 2022-06-19 10:33:54 -07:00
extmark.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
file_search.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
file_search.h
fileio.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
fileio.h [RFC] vim-patch:8.1.1378: delete() can not handle a file name that looks li… (#16268) 2022-04-03 20:27:46 +08:00
fold.c refactor(fold): style #19175 2022-07-02 04:29:27 -07:00
fold.h vim-patch:8.1.0535: increment/decrement might get interrupted by updating folds 2022-05-22 06:41:31 +08:00
func_attr.h refactor(uncrustify): change config to better align with style guide (#18803) 2022-06-30 20:27:52 +08:00
garray.c refactor: convert macros to all-caps (#17895) 2022-04-24 20:18:43 -06:00
garray.h
getchar.c vim-patch:8.2.0274: hang with combination of feedkeys(), Ex mode and :global 2022-07-08 23:11:12 +08:00
getchar.h refactor: move some mapping-related code to a separate file (#19061) 2022-06-23 21:17:11 +08:00
gettext.h
globals.h vim-patch:8.2.0274: hang with combination of feedkeys(), Ex mode and :global 2022-07-08 23:11:12 +08:00
grid_defs.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
grid.c vim-patch:8.1.1038: Arabic support excludes Farsi (#19285) 2022-07-10 06:01:49 +08:00
grid.h refactor: replace char_u variables and functions with char 2022-05-09 10:03:29 +02:00
hardcopy.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
hardcopy.h refactor(highlight)!: rename attributes to match Vim (#19159) 2022-06-30 16:57:44 +08:00
hashtab.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
hashtab.h
highlight_defs.h refactor(highlight)!: rename attributes to match Vim (#19159) 2022-06-30 16:57:44 +08:00
highlight_group.c refactor(highlight)!: rename attributes to match Vim (#19159) 2022-06-30 16:57:44 +08:00
highlight_group.h refactor(uncrustify): format all c code under /src/nvim/ 2022-04-29 13:52:43 +02:00
highlight.c refactor(highlight)!: rename attributes to match Vim (#19159) 2022-06-30 16:57:44 +08:00
highlight.h
iconv.h
if_cscope_defs.h
if_cscope.c refactor: replace char_u #18429 2022-06-28 02:31:54 -07:00
if_cscope.h
indent_c.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
indent_c.h
indent.c vim-patch:8.2.5122: lisp indenting my run over the end of the line 2022-07-05 15:10:46 +08:00
indent.h
input.c vim-patch:8.2.4911: the mode #defines are not clearly named (#18499) 2022-05-10 07:58:58 +08:00
input.h
keycodes.c perf(map): avoid extraneous heap allocations when setting mappings 2022-06-24 13:53:42 +02:00
keycodes.h refactor: replace char_u variables and functions with char 2022-05-16 13:27:06 +02:00
log.c fix(logging): try harder to resolve Nvim "name" #19016 2022-06-18 09:30:54 -07:00
log.h fix(logging): skip recursion, fix crash #18764 2022-05-30 13:07:33 -07:00
macros.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
main.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
main.h refactor: replace char_u variables and functions with char 2022-05-04 15:25:54 +02:00
map_defs.h
map.c feat(tui): recognize keypad keys when using kitty keyboard protocol 2022-07-04 21:09:33 +08:00
map.h feat(tui): recognize keypad keys when using kitty keyboard protocol 2022-07-04 21:09:33 +08:00
mapping.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
mapping.h refactor: move some mapping-related code to a separate file (#19061) 2022-06-23 21:17:11 +08:00
mark_defs.h fix(mark): mark without a view restores at topline #19224 2022-07-04 13:28:14 -07:00
mark.c fix(mark): fix unexpected cursor movements (#19253) 2022-07-06 19:01:44 +08:00
mark.h feat(marks): restore viewport on jump #15831 2022-06-30 05:59:52 -07:00
marktree.c refactor: missing parenthesis may cause unexpected problems (#17443) 2022-05-26 10:49:25 +08:00
marktree.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
match.c vim-patch:8.2.0614: get ml_get error when deleting a line in 'completefunc' (#19244) 2022-07-07 04:47:18 +08:00
match.h vim-patch:8.2.1078: highlight and match functionality together in one file (#17805) 2022-03-23 06:31:50 +08:00
math.c refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
math.h
mbyte.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
mbyte.h
memfile_defs.h
memfile.c refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
memfile.h
memline_defs.h
memline.c vim-patch:8.2.4399: crash after ml_get error 2022-07-07 08:10:53 +08:00
memline.h
memory.c vim-patch:9.0.0015: with EXITFREE defined terminal menus are not cleared (#19187) 2022-07-01 21:02:59 +08:00
memory.h perf(ui): reduce allocation overhead when encoding "redraw" events 2022-06-20 12:44:56 +02:00
menu.c vim-patch:9.0.0015: with EXITFREE defined terminal menus are not cleared (#19187) 2022-07-01 21:02:59 +08:00
menu.h vim-patch:8.1.0487: no menus specifically for the terminal window 2022-07-01 10:17:39 +08:00
message.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
message.h fix(messages): add color when showing nvim_echo in :messages history 2022-06-11 22:29:51 +02:00
mouse.c feat: add 'mousescroll' option (#12355) 2022-07-06 19:34:24 +08:00
mouse.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
move.c refactor: replace char_u #18429 2022-06-28 02:31:54 -07:00
move.h
normal.c vim-patch:8.2.5023: substitute overwrites allocated buffer 2022-07-07 07:00:11 +08:00
normal.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
ops.c vim-patch:7.4.212 2022-07-06 19:58:44 +08:00
ops.h refactor: replace char_u variables and functions with char 2022-05-09 10:03:29 +02:00
option_defs.h feat: add 'mousescroll' option (#12355) 2022-07-06 19:34:24 +08:00
option.c feat: add 'mousescroll' option (#12355) 2022-07-06 19:34:24 +08:00
option.h
options.lua feat: add 'mousescroll' option (#12355) 2022-07-06 19:34:24 +08:00
os_unix.c refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
os_unix.h
path.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
path.h
plines.c refactor: replace char_u #18429 2022-06-28 02:31:54 -07:00
plines.h
popupmnu.c feat(ui): make right-click menu work properly with ext_multigrid 2022-07-01 10:28:07 +08:00
popupmnu.h
pos.h refactor: change type of linenr_T from long to int32_t 2022-06-10 16:16:41 +02:00
profile.c
profile.h
quickfix.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
quickfix.h
rbuffer.c refactor(api): use a unpacker based on libmpack instead of msgpack-c 2022-06-02 16:05:24 +02:00
rbuffer.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
README.md fix(docs): correct obsolete note about 'writedelay' in dev docs 2022-05-31 00:20:10 +02:00
regexp_bt.c feat(marks): restore viewport on jump #15831 2022-06-30 05:59:52 -07:00
regexp_defs.h vim-patch:8.2.5046: vim_regsub() can overwrite the destination (#18812) 2022-05-31 19:55:04 +08:00
regexp_nfa.c feat(marks): restore viewport on jump #15831 2022-06-30 05:59:52 -07:00
regexp.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
regexp.h refactor(uncrustify): format all c code under /src/nvim/ 2022-04-29 13:52:43 +02:00
runtime.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
runtime.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
screen.c vim-patch:8.1.1038: Arabic support excludes Farsi (#19285) 2022-07-10 06:01:49 +08:00
screen.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
search.c vim-patch:7.4.212 2022-07-06 19:58:44 +08:00
search.h
sha256.c refactor(uncrustify): change rules to better align with the style guide 2022-04-29 14:13:06 +02:00
sha256.h
shada.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
shada.h
sign_defs.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
sign.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
sign.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
spell_defs.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
spell.c vim-patch:9.0.0035: spell dump may go beyond end of an array (#19228) 2022-07-05 06:47:58 +08:00
spell.h
spellfile.c vim-patch:8.2.1522: not enough test coverage for the spell file handling 2022-07-04 09:15:03 +08:00
spellfile.h
state.c docs: fix typos (#18269) 2022-06-04 11:56:36 +08:00
state.h
strings.c refactor: missing parenthesis may cause unexpected problems (#17443) 2022-05-26 10:49:25 +08:00
strings.h ci: lint with uncrustify #18563 2022-05-20 20:41:57 -07:00
syntax_defs.h vim-patch:8.2.4639: not sufficient parenthesis in preprocessor macros 2022-04-03 15:57:07 -07:00
syntax.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
syntax.h vim-patch:8.1.1693: syntax coloring and highlighting is in one big file (#17721) 2022-03-18 12:47:08 +08:00
tag.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
tag.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
terminal.c refactor: replace char_u 2022-07-02 16:01:27 +02:00
terminal.h feat(server): set $NVIM, unset $NVIM_LISTEN_ADDRESS #11009 2022-05-03 06:08:35 -07:00
testing.c refactor: replace char_u variables and functions with char 2022-05-07 14:54:01 +02:00
testing.h refactor(uncrustify): format all c code under /src/nvim/ 2022-04-29 13:52:43 +02:00
types.h
ugrid.c refactor(uncrustify): change rules to better align with the style guide 2022-04-29 14:13:06 +02:00
ugrid.h
ui_bridge.c refactor(uncrustify): change rules to better align with the style guide 2022-04-29 14:13:06 +02:00
ui_bridge.h refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) 2022-05-25 12:31:14 -06:00
ui_client.c refactor(api): use hashy hash for looking up api method and event names 2022-05-30 14:11:01 +02:00
ui_client.h refactor(api): use hashy hash for looking up api method and event names 2022-05-30 14:11:01 +02:00
ui_compositor.c fix(float): make screen*() functions respect floating windows 2022-06-23 10:34:02 +03:00
ui_compositor.h
ui.c fix(input): use correct grid when restoring cursor for <expr> mapping (#19047) 2022-06-23 06:04:03 +08:00
ui.h perf(ui): reduce allocation overhead when encoding "redraw" events 2022-06-20 12:44:56 +02:00
undo_defs.h
undo.c vim-patch:8.2.5023: substitute overwrites allocated buffer 2022-07-07 07:00:11 +08:00
undo.h
version.c version.c: update [skip ci] #19179 2022-07-01 03:26:42 -07:00
version.h
vim.h feat: add preview functionality to user commands 2022-05-31 20:55:05 +06:00
window.c vim-patch:8.2.5043: can open a cmdline window from a substitute expression 2022-07-07 07:00:11 +08:00
window.h

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.

UI events are logged at DEBUG level (LOGLVL_DBG).

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

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*). Requires -no-pie (ref):

rm -rf build/
make CMAKE_EXTRA_FLAGS="-DMIN_LOG_LEVEL=0 -DCMAKE_C_FLAGS=-no-pie"

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/state/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/msan"
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

Debug: Performance

Profiling (easy)

For debugging performance bottlenecks in any code, there is a simple (and very effective) approach:

  1. Run the slow code in a loop.
  2. Break execution ~5 times and save the stacktrace.
  3. The cause of the bottleneck will (almost always) appear in most of the stacktraces.

Profiling (fancy)

For more advanced profiling, consider perf + flamegraph.

USDT profiling (powerful)

Or you can use USDT probes via NVIM_PROBE (#12036).

USDT is basically a way to define stable probe points in userland binaries. The benefit of bcc is the ability to define logic to go along with the probe points.

Tools:

  • bpftrace provides an awk-like language to the kernel bytecode, BPF.
  • BCC provides a subset of C. Provides more complex logic than bpftrace, but takes a bit more effort.

Example using bpftrace to track slow vim functions, and print out any files that were opened during the trace. At the end, it prints a histogram of function timing:

#!/usr/bin/env bpftrace

BEGIN {
  @depth = -1;
}

tracepoint:sched:sched_process_fork /@pidmap[args->parent_pid]/ {
  @pidmap[args->child_pid] = 1;
}

tracepoint:sched:sched_process_exit /@pidmap[args->pid]/ {
  delete(@pidmap[args->pid]);
}

usdt:build/bin/nvim:neovim:eval__call_func__entry {
    @pidmap[pid] = 1;
    @depth++;
    @funcentry[@depth] = nsecs;
}

usdt:build/bin/nvim:neovim:eval__call_func__return {
    $func = str(arg0);
    $msecs = (nsecs - @funcentry[@depth]) / 1000000;

    @time_histo = hist($msecs);

    if ($msecs >= 1000) {
      printf("%u ms for %s\n", $msecs, $func);
      print(@files);
    }

    clear(@files);
    delete(@funcentry[@depth]);
    @depth--;
}

tracepoint:syscalls:sys_enter_open,
tracepoint:syscalls:sys_enter_openat {
  if (@pidmap[pid] == 1 && @depth >= 0) {
    @files[str(args->filename)] = count();
  }
}

END {
  clear(@depth);
}

$ sudo bpftrace funcslower.bt
1527 ms for Slower
@files[/usr/lib/libstdc++.so.6]: 2
@files[/etc/fish/config.fish]: 2
<snip>

^C
@time_histo:
[0]                71430 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[1]                  346 |                                                    |
[2, 4)               208 |                                                    |
[4, 8)                91 |                                                    |
[8, 16)               22 |                                                    |
[16, 32)              85 |                                                    |
[32, 64)               7 |                                                    |
[64, 128)              0 |                                                    |
[128, 256)             0 |                                                    |
[256, 512)             6 |                                                    |
[512, 1K)              1 |                                                    |
[1K, 2K)               5 |                                                    |

Debug: TUI

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' and 'redrawdebug' options to see where and when the UI is painted.

:set writedelay=50 rdb=compositor

Note: neovim uses an internal screenbuffer to only send minimal updates even if a large region is repainted internally. To also highlight excess internal redraws, use

:set writedelay=50 rdb=compositor,nodelta

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;