robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#pragma once 2 3#include <stdbool.h> 4 5#include "klib/kvec.h" 6#include "nvim/api/private/defs.h" 7#include "nvim/buffer_defs.h" 8#include "nvim/mark_defs.h" 9#include "nvim/normal_defs.h" 10#include "nvim/os/os_defs.h" 11#include "nvim/pos_defs.h" 12#include "nvim/types_defs.h" 13 14typedef struct { 15pos_T pos ;///< Current cursor position (cache, see `mark`). 16uint32_t mark ;///< Extmark id tracking `pos` across buffer edits. 17colnr_T curswant ;///< Preferred column ("curswant"); -1 if unset. 18 19int visual_mode_eval ;///< `b_visual_mode_eval` (visualmode()). 20visualinfo_T visual ;///< (Cache) Previous Visual area (`'<`, `'>`, "gv"). 21pos_T op_start ;///< (Cache) '[ (b_op_start). 22pos_T op_end ;///< (Cache) '] (b_op_end). 23fmark_T last_change ;///< (Cache) '. (b_last_change). 24fmark_T last_insert ;///< (Cache) '^ (b_last_insert). 25uint32_t pos_marks [6 ];///< Extmarks tracking the (Cache) positions above (in that order), 26///< across buffer edits. 0: none. 27handle_T buf ;///< Current buffer handle. 28String regs ;///< Registers (shada msgpack string). 29String jumps ;///< Jumplist (shada msgpack string). 30String bufs ;///< Buffer list (shada msgpack string). 31String gvars ;///< Global variables (shada msgpack string). 32Array funcs ;///< Functions. 33}Context ; 34typedef kvec_t (Context )ContextVec ; 35 36#define CONTEXT_INIT { \ 37 .pos = { 0 }, \ 38 .mark = 0, \ 39 .curswant = -1, \ 40 .visual = { .vi_start = { 0 } }, \ 41 .visual_mode_eval = 0, \ 42 .op_start = { 0 }, \ 43 .op_end = { 0 }, \ 44 .last_change = { .mark = { 0 } }, \ 45 .last_insert = { .mark = { 0 } }, \ 46 .pos_marks = { 0 }, \ 47 .buf = 0, \ 48 .regs = STRING_INIT, \ 49 .jumps = STRING_INIT, \ 50 .bufs = STRING_INIT, \ 51 .gvars = STRING_INIT, \ 52 .funcs = ARRAY_DICT_INIT, \ 53} 54 55typedef enum { 56kCtxRegs = 1 ,///< Registers 57kCtxJumps = 2 ,///< Jumplist 58kCtxBufs = 4 ,///< Buffer list 59kCtxGVars = 8 ,///< Global variables 60kCtxSFuncs = 16 ,///< Script functions 61kCtxFuncs = 32 ,///< Functions 62kCtxAll = kCtxRegs |kCtxJumps |kCtxBufs |kCtxGVars |kCtxSFuncs |kCtxFuncs , 63 64// In-memory only (TODO(justinmk): support this in dict/kCtxAll too): 65kCtxCursor = 64 ,///< Cursor position and curswant. 66kCtxMarks = 128 ,///< Change marks ('[ '] '.), last insert ('^). 67kCtxVisual = 256 ,///< Visual area ('< '>, visualmode()). 68}CtxStateFlags ; 69 70/// "How" to load, orthogonal to "what" (CtxStateFlags). 71typedef enum { 72kCtxMergeReg = 1 ,///< Merge incoming registers with existing. 73}CtxLoadFlags ; 74 75/// Temporary, hidden window (fka "autocmd window"): a pooled window created to temporarily show 76/// a buffer that has no window (ctx_switch() on a buffer target), to handle the side effects. When 77/// switches nest we may need more than one. 78typedef struct { 79win_T * cw_win ;///< The window, or NULL if not yet allocated. 80bool cw_used ;///< Not currently in use. 81}CtxWin ; 82 83/// Flags for ctx_switch(). 84typedef enum { 85/// Restore process CWD: undo incidental chdir ('autochdir', "leaked" win/tab-local CWD). 86/// 87/// Note: this flag only exists for performance. Semantically every ctx-switch wants this, but the 88/// getcwd() bookkeeping is costly for internal switches that don't run user code. 89kCtxKeepCwd = 1 , 90/// Restore the target's full CWD state: undo all "chdir" operations on ctx_restore(), including 91/// explicit :cd/:tcd/:bcd (which otherwise persist). 92/// - Note: :lcd targeting a hidden buffer (temp window) is always discarded. 93kCtxKeepDirs = 2 , 94/// Don't affect the display (no redraw; limits access to another tabpage). 95kCtxNoDisplay = 4 , 96/// Block autocommands until ctx_restore(). 97kCtxNoEvents = 8 , 98/// Validate cursor/Visual around the switch; update display (statusline) if the target window's 99/// cursor moved. 100kCtxValidate = 16 , 101}CtxSwitchFlags ; 102 103/// What ctx_switch() switched (set internally). 104enum { 105kCtxSwitchNone = 0 ,///< Zero-initialized: ctx_restore() is a no-op. 106kCtxSwitchWin ,///< Window target. 107kCtxSwitchBuf ,///< Buffer target. 108kCtxSwitchDirs ,///< No target: only CWD state is saved. 109}; 110 111/// Context before a temporary switch of current window/buffer. Undone by ctx_restore(). 112typedef struct { 113CtxSwitchFlags cs_flags ;///< kCtx* flags of the switch 114int cs_mode ;///< kCtxSwitch* (what was switched) 115// Saved location: 116handle_T cs_curwin ;///< saved curwin 117handle_T cs_prevwin ;///< saved prevwin (ctx_switch()) 118tabpage_T * cs_curtab ;///< saved curtab (NULL: tabpage unchanged) 119bool cs_same_win ;///< Visual.active was not reset 120bool cs_visual_active ;///< saved Visual.active 121int cs_prompt_insert ;///< saved b_prompt_insert 122// Temporary location (ctx_switch()): 123handle_T cs_new_curwin ;///< ID of new curwin 124bufref_T cs_new_curbuf ;///< new curbuf 125int cs_ctxwin_idx ;///< "autocmd" window in the ctx_win pool, or -1. 126// Target tracking (kCtxValidate): 127handle_T cs_target_win ;///< the window switched to 128pos_T cs_target_old_pos ;///< its cursor before the switch 129// State kept across the switch: 130bool cs_did_chdir ;///< saved `ctx_did_chdir` of the enclosing context 131handle_T cs_dirs_tab ;///< kCtxKeepDirs: tabpage that owns cs_tp_localdir. 132// Saved dir state. Three users: 133// 1. hidden-buffer target always saves b/tp/globaldir (so the temp context starts dir-neutral) 134// 2. kCtxKeepCwd saves globaldir. 135// 3. kCtxKeepDirs also saves w/b/tp-localdir. 136char * cs_w_localdir ;///< Saved w_localdir of the target window 137char * cs_b_localdir ;///< Saved b_localdir of the target buffer 138char * cs_tp_localdir ;///< Saved tp_localdir 139char * cs_globaldir ;///< Saved globaldir 140char * cs_cwd ;///< Saved CWD (kCtxKeepCwd/kCtxKeepDirs). 141bool cs_apply_acd ;///< Re-apply 'autochdir' on ctx_restore(). 142}CtxSwitch ;