robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
6.0 KiB142 linesraw
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 {
15  pos_T pos;         ///< Current cursor position (cache, see `mark`).
16  uint32_t mark;     ///< Extmark id tracking `pos` across buffer edits.
17  colnr_T curswant;  ///< Preferred column ("curswant"); -1 if unset.
18
19  int visual_mode_eval;  ///< `b_visual_mode_eval` (visualmode()).
20  visualinfo_T visual;   ///< (Cache) Previous Visual area (`'<`, `'>`, "gv").
21  pos_T op_start;        ///< (Cache) '[ (b_op_start).
22  pos_T op_end;          ///< (Cache) '] (b_op_end).
23  fmark_T last_change;   ///< (Cache) '. (b_last_change).
24  fmark_T last_insert;   ///< (Cache) '^ (b_last_insert).
25  uint32_t pos_marks[6];  ///< Extmarks tracking the (Cache) positions above (in that order),
26                          ///< across buffer edits. 0: none.
27  handle_T buf;      ///< Current buffer handle.
28  String regs;       ///< Registers (shada msgpack string).
29  String jumps;      ///< Jumplist (shada msgpack string).
30  String bufs;       ///< Buffer list (shada msgpack string).
31  String gvars;      ///< Global variables (shada msgpack string).
32  Array 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 {
56  kCtxRegs = 1,       ///< Registers
57  kCtxJumps = 2,      ///< Jumplist
58  kCtxBufs = 4,       ///< Buffer list
59  kCtxGVars = 8,      ///< Global variables
60  kCtxSFuncs = 16,    ///< Script functions
61  kCtxFuncs = 32,     ///< Functions
62  kCtxAll = kCtxRegs | kCtxJumps | kCtxBufs | kCtxGVars | kCtxSFuncs | kCtxFuncs,
63
64  // In-memory only (TODO(justinmk): support this in dict/kCtxAll too):
65  kCtxCursor = 64,    ///< Cursor position and curswant.
66  kCtxMarks = 128,    ///< Change marks ('[ '] '.), last insert ('^).
67  kCtxVisual = 256,   ///< Visual area ('< '>, visualmode()).
68} CtxStateFlags;
69
70/// "How" to load, orthogonal to "what" (CtxStateFlags).
71typedef enum {
72  kCtxMergeReg = 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 {
79  win_T *cw_win;   ///< The window, or NULL if not yet allocated.
80  bool 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.
89  kCtxKeepCwd = 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.
93  kCtxKeepDirs = 2,
94  /// Don't affect the display (no redraw; limits access to another tabpage).
95  kCtxNoDisplay = 4,
96  /// Block autocommands until ctx_restore().
97  kCtxNoEvents = 8,
98  /// Validate cursor/Visual around the switch; update display (statusline) if the target window's
99  /// cursor moved.
100  kCtxValidate = 16,
101} CtxSwitchFlags;
102
103/// What ctx_switch() switched (set internally).
104enum {
105  kCtxSwitchNone = 0,  ///< Zero-initialized: ctx_restore() is a no-op.
106  kCtxSwitchWin,       ///< Window target.
107  kCtxSwitchBuf,       ///< Buffer target.
108  kCtxSwitchDirs,      ///< 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 {
113  CtxSwitchFlags cs_flags;        ///< kCtx* flags of the switch
114  int cs_mode;                    ///< kCtxSwitch* (what was switched)
115  // Saved location:
116  handle_T cs_curwin;             ///< saved curwin
117  handle_T cs_prevwin;            ///< saved prevwin (ctx_switch())
118  tabpage_T *cs_curtab;           ///< saved curtab (NULL: tabpage unchanged)
119  bool cs_same_win;               ///< Visual.active was not reset
120  bool cs_visual_active;          ///< saved Visual.active
121  int cs_prompt_insert;           ///< saved b_prompt_insert
122  // Temporary location (ctx_switch()):
123  handle_T cs_new_curwin;         ///< ID of new curwin
124  bufref_T cs_new_curbuf;         ///< new curbuf
125  int cs_ctxwin_idx;              ///< "autocmd" window in the ctx_win pool, or -1.
126  // Target tracking (kCtxValidate):
127  handle_T cs_target_win;         ///< the window switched to
128  pos_T cs_target_old_pos;        ///< its cursor before the switch
129  // State kept across the switch:
130  bool cs_did_chdir;              ///< saved `ctx_did_chdir` of the enclosing context
131  handle_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.
136  char *cs_w_localdir;            ///< Saved w_localdir of the target window
137  char *cs_b_localdir;            ///< Saved b_localdir of the target buffer
138  char *cs_tp_localdir;           ///< Saved tp_localdir
139  char *cs_globaldir;             ///< Saved globaldir
140  char *cs_cwd;                   ///< Saved CWD (kCtxKeepCwd/kCtxKeepDirs).
141  bool cs_apply_acd;              ///< Re-apply 'autochdir' on ctx_restore().
142} CtxSwitch;