robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
15.0 KiB520 linesraw
1#include <assert.h>
2#include <inttypes.h>
3#include <stdbool.h>
4#include <string.h>
5
6#include "nvim/ascii_defs.h"
7#include "nvim/assert_defs.h"
8#include "nvim/buffer_defs.h"
9#include "nvim/change.h"
10#include "nvim/cursor.h"
11#include "nvim/drawscreen.h"
12#include "nvim/fold.h"
13#include "nvim/globals.h"
14#include "nvim/macros_defs.h"
15#include "nvim/mark.h"
16#include "nvim/mbyte.h"
17#include "nvim/mbyte_defs.h"
18#include "nvim/memline.h"
19#include "nvim/memory.h"
20#include "nvim/move.h"
21#include "nvim/option.h"
22#include "nvim/option_vars.h"
23#include "nvim/plines.h"
24#include "nvim/pos_defs.h"
25#include "nvim/state.h"
26#include "nvim/state_defs.h"
27#include "nvim/types_defs.h"
28#include "nvim/vim_defs.h"
29
30#include "cursor.c.generated.h"
31
32/// @return  the screen position of the cursor.
33int getviscol(void)
34{
35  colnr_T x;
36
37  getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL, 0);
38  return (int)x;
39}
40
41/// @return the screen position of character col with a coladd in the cursor line.
42int getviscol2(colnr_T col, colnr_T coladd)
43{
44  colnr_T x;
45  pos_T pos;
46
47  pos.lnum = curwin->w_cursor.lnum;
48  pos.col = col;
49  pos.coladd = coladd;
50  getvvcol(curwin, &pos, &x, NULL, NULL, 0);
51  return (int)x;
52}
53
54/// Go to column "wcol", and add/insert white space as necessary to get the
55/// cursor in that column.
56/// The caller must have saved the cursor line for undo!
57int coladvance_force(colnr_T wcol)
58{
59  int rc = coladvance2(curwin, &curwin->w_cursor, true, false, wcol);
60
61  if (wcol == MAXCOL) {
62    curwin->w_valid &= ~VALID_VIRTCOL;
63  } else {
64    // Virtcol is valid
65    set_valid_virtcol(curwin, wcol);
66  }
67  return rc;
68}
69
70/// Try to advance the Cursor to the specified screen column.
71/// If virtual editing: fine tune the cursor position.
72/// Note that all virtual positions off the end of a line should share
73/// a curwin->w_cursor.col value (n.b. this is equal to strlen(line)),
74/// beginning at coladd 0.
75///
76/// @return  OK if desired column is reached, FAIL if not
77int coladvance(win_T *wp, colnr_T wcol)
78{
79  int rc = getvpos(wp, &wp->w_cursor, wcol);
80
81  if (wcol == MAXCOL || rc == FAIL) {
82    wp->w_valid &= ~VALID_VIRTCOL;
83  } else if (*(ml_get_buf(wp->w_buffer, wp->w_cursor.lnum) + wp->w_cursor.col) != TAB) {
84    // Virtcol is valid when not on a TAB
85    set_valid_virtcol(curwin, wcol);
86  }
87  return rc;
88}
89
90/// @param addspaces  change the text to achieve our goal? only for wp=curwin!
91/// @param finetune  change char offset for the exact column
92/// @param wcol_arg  column to move to (can be negative)
93static int coladvance2(win_T *wp, pos_T *pos, bool addspaces, bool finetune, colnr_T wcol_arg)
94{
95  assert(wp == curwin || !addspaces);
96  colnr_T wcol = wcol_arg;
97  int idx;
98  colnr_T col = 0;
99  int head = 0;
100
101  int one_more = (State & MODE_INSERT)
102                 || (State & MODE_TERMINAL)
103                 || restart_edit != NUL
104                 || (Visual.active && *p_sel != 'o')
105                 || ((get_ve_flags(wp) & kOptVeFlagOnemore) && wcol < MAXCOL);
106
107  char *line = ml_get_buf(wp->w_buffer, pos->lnum);
108  int linelen = ml_get_buf_len(wp->w_buffer, pos->lnum);
109
110  if (wcol >= MAXCOL) {
111    idx = linelen - 1 + one_more;
112    col = wcol;
113
114    if ((addspaces || finetune) && !Visual.active) {
115      wp->w_curswant = linetabsize(wp, pos->lnum) + one_more;
116      if (wp->w_curswant > 0) {
117        wp->w_curswant--;
118      }
119    }
120  } else {
121    int width = wp->w_view_width - win_col_off(wp);
122    int csize = 0;
123
124    if (finetune
125        && wp->w_p_wrap
126        && wp->w_view_width != 0
127        && wcol >= (colnr_T)width
128        && width > 0) {
129      csize = linetabsize_eol(wp, pos->lnum);
130      if (csize > 0) {
131        csize--;
132      }
133
134      if (wcol / width > (colnr_T)csize / width
135          && ((State & MODE_INSERT) == 0 || (int)wcol > csize + 1)) {
136        // In case of line wrapping don't move the cursor beyond the
137        // right screen edge.  In Insert mode allow going just beyond
138        // the last character (like what happens when typing and
139        // reaching the right window edge).
140        wcol = (csize / width + 1) * width - 1;
141      }
142    }
143
144    CharsizeArg csarg;
145    CSType cstype = init_charsize_arg(&csarg, wp, pos->lnum, line);
146    StrCharInfo ci = utf_ptr2StrCharInfo(line);
147    col = 0;
148    while (col <= wcol && *ci.ptr != NUL) {
149      ClusterInfo cli = utf_ClusterInfo(ci);
150      CharSize cs = win_charsize(cstype, col, ci.ptr, ci.chr.value, &csarg, cli.cells);
151      csize = cs.width;
152      head = cs.head;
153      col += cs.width;
154      ci = cli.next;
155    }
156    idx = (int)(ci.ptr - line);
157
158    // Handle all the special cases.  The virtual_active() check
159    // is needed to ensure that a virtual position off the end of
160    // a line has the correct indexing.  The one_more comparison
161    // replaces an explicit add of one_more later on.
162    if (col > wcol || (!virtual_active(wp) && one_more == 0)) {
163      idx -= 1;
164      // Don't count the chars from 'showbreak'.
165      csize -= head;
166      col -= csize;
167    }
168
169    if (virtual_active(wp)
170        && addspaces
171        && wcol >= 0
172        && ((col != wcol && col != wcol + 1) || csize > 1)) {
173      // 'virtualedit' is set: The difference between wcol and col is filled with spaces.
174
175      if (line[idx] == NUL) {
176        // Append spaces
177        int correct = wcol - col;
178        size_t newline_size;
179        STRICT_ADD(idx, correct, &newline_size, size_t);
180        char *newline = xmallocz(newline_size);
181        memcpy(newline, line, (size_t)idx);
182        memset(newline + idx, ' ', (size_t)correct);
183
184        ml_replace(pos->lnum, newline, false);
185        inserted_bytes(pos->lnum, (colnr_T)idx, 0, correct);
186        idx += correct;
187        col = wcol;
188      } else {
189        // Break a tab
190        int correct = wcol - col - csize + 1;             // negative!!
191        char *newline;
192
193        if (-correct > csize) {
194          return FAIL;
195        }
196
197        size_t n;
198        STRICT_ADD(linelen - 1, csize, &n, size_t);
199        newline = xmallocz(n);
200        // Copy first idx chars
201        memcpy(newline, line, (size_t)idx);
202        // Replace idx'th char with csize spaces
203        memset(newline + idx, ' ', (size_t)csize);
204        // Copy the rest of the line
205        STRICT_SUB(linelen, idx, &n, size_t);
206        STRICT_SUB(n, 1, &n, size_t);
207        memcpy(newline + idx + csize, line + idx + 1, n);
208
209        ml_replace(pos->lnum, newline, false);
210        inserted_bytes(pos->lnum, idx, 1, csize);
211        idx += (csize - 1 + correct);
212        col += correct;
213      }
214    }
215  }
216
217  pos->col = MAX(idx, 0);
218  pos->coladd = 0;
219
220  if (finetune) {
221    if (wcol == MAXCOL) {
222      // The width of the last character is used to set coladd.
223      if (!one_more) {
224        colnr_T scol, ecol;
225
226        getvcol(wp, pos, &scol, NULL, &ecol, 0);
227        pos->coladd = ecol - scol;
228      }
229    } else {
230      int b = (int)wcol - (int)col;
231
232      // The difference between wcol and col is used to set coladd.
233      if (b > 0 && b < (MAXCOL - 2 * wp->w_view_width)) {
234        pos->coladd = b;
235      }
236
237      col += b;
238    }
239  }
240
241  // Prevent from moving onto a trail byte.
242  mark_mb_adjustpos(wp->w_buffer, pos);
243
244  if (wcol < 0 || col < wcol) {
245    return FAIL;
246  }
247  return OK;
248}
249
250/// Return in "pos" the position of the cursor advanced to screen column "wcol".
251///
252/// @return  OK if desired column is reached, FAIL if not
253int getvpos(win_T *wp, pos_T *pos, colnr_T wcol)
254{
255  return coladvance2(wp, pos, false, virtual_active(wp), wcol);
256}
257
258/// Increment the cursor position.  See inc() for return values.
259int inc_cursor(void)
260{
261  return inc(&curwin->w_cursor);
262}
263
264/// Decrement the cursor position.  See dec() for return values.
265int dec_cursor(void)
266{
267  return dec(&curwin->w_cursor);
268}
269
270/// Get the line number relative to the current cursor position, i.e. the
271/// difference between line number and cursor position. Only look for lines that
272/// can be visible, folded lines don't count.
273///
274/// @param lnum line number to get the result for
275linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum)
276{
277  linenr_T cursor = wp->w_cursor.lnum;
278  if (lnum == cursor || !hasAnyFolding(wp)) {
279    return lnum - cursor;
280  }
281
282  linenr_T from_line = lnum < cursor ? lnum : cursor;
283  linenr_T to_line = lnum > cursor ? lnum : cursor;
284  linenr_T retval = 0;
285
286  // Loop until we reach to_line, skipping folds.
287  for (; from_line < to_line; from_line++, retval++) {
288    // If from_line is in a fold, set it to the last line of that fold.
289    hasFolding(wp, from_line, NULL, &from_line);
290  }
291
292  // If to_line is in a closed fold, the line count is off by +1. Correct it.
293  if (from_line > to_line) {
294    retval--;
295  }
296
297  return (lnum < cursor) ? -retval : retval;
298}
299
300/// Make sure "pos.lnum" and "pos.col" are valid in "buf".
301/// This allows for the col to be on the NUL byte.
302void check_pos(buf_T *buf, pos_T *pos)
303{
304  pos->lnum = MIN(pos->lnum, buf->b_ml.ml_line_count);
305  if (pos->col > 0) {
306    pos->col = MIN(pos->col, ml_get_buf_len(buf, pos->lnum));
307  }
308}
309
310/// Make sure win->w_cursor.lnum is valid.
311void check_cursor_lnum(win_T *win)
312{
313  buf_T *buf = win->w_buffer;
314  if (win->w_cursor.lnum > buf->b_ml.ml_line_count) {
315    // If there is a closed fold at the end of the file, put the cursor in
316    // its first line.  Otherwise in the last line.
317    if (!hasFolding(win, buf->b_ml.ml_line_count, &win->w_cursor.lnum, NULL)) {
318      win->w_cursor.lnum = buf->b_ml.ml_line_count;
319    }
320  }
321  if (win->w_cursor.lnum <= 0) {
322    win->w_cursor.lnum = 1;
323  }
324}
325
326/// Make sure win->w_cursor.col is valid. Special handling of insert-mode.
327/// @see mb_check_adjust_col
328void check_cursor_col(win_T *win)
329{
330  colnr_T oldcol = win->w_cursor.col;
331  colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
332  unsigned cur_ve_flags = get_ve_flags(win);
333
334  colnr_T len = ml_get_buf_len(win->w_buffer, win->w_cursor.lnum);
335  if (len == 0) {
336    win->w_cursor.col = 0;
337  } else if (win->w_cursor.col >= len) {
338    // Allow cursor past end-of-line when:
339    // - in Insert mode or restarting Insert mode
340    // - in Terminal mode
341    // - in Visual mode and 'selection' isn't "old"
342    // - 'virtualedit' is set
343    if ((State & MODE_INSERT) || restart_edit
344        || (State & MODE_TERMINAL)
345        || (Visual.active && *p_sel != 'o')
346        || (cur_ve_flags & kOptVeFlagOnemore)
347        || virtual_active(win)) {
348      win->w_cursor.col = len;
349    } else {
350      win->w_cursor.col = len - 1;
351      // Move the cursor to the head byte.
352      mark_mb_adjustpos(win->w_buffer, &win->w_cursor);
353    }
354  } else if (win->w_cursor.col < 0) {
355    win->w_cursor.col = 0;
356  }
357
358  // If virtual editing is on, we can leave the cursor on the old position,
359  // only we must set it to virtual.  But don't do it when at the end of the
360  // line.
361  if (oldcol == MAXCOL) {
362    win->w_cursor.coladd = 0;
363  } else if (cur_ve_flags == kOptVeFlagAll) {
364    if (oldcoladd > win->w_cursor.col) {
365      win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
366
367      // Make sure that coladd is not more than the char width.
368      // Not for the last character, coladd is then used when the cursor
369      // is actually after the last character.
370      if (win->w_cursor.col + 1 < len) {
371        assert(win->w_cursor.coladd > 0);
372        int cs, ce;
373
374        getvcol(win, &win->w_cursor, &cs, NULL, &ce, 0);
375        win->w_cursor.coladd = MIN(win->w_cursor.coladd, ce - cs);
376      }
377    } else {
378      // avoid weird number when there is a miscalculation or overflow
379      win->w_cursor.coladd = 0;
380    }
381  }
382}
383
384/// Make sure curwin->w_cursor in on a valid character
385void check_cursor(win_T *wp)
386{
387  check_cursor_lnum(wp);
388  check_cursor_col(wp);
389}
390
391/// Check if Visual.start position is valid, correct it if not.
392/// Can be called when in Visual mode and a change has been made.
393void check_visual_pos(void)
394{
395  if (Visual.start.lnum > curbuf->b_ml.ml_line_count) {
396    Visual.start.lnum = curbuf->b_ml.ml_line_count;
397    Visual.start.col = 0;
398    Visual.start.coladd = 0;
399  } else {
400    int len = ml_get_len(Visual.start.lnum);
401
402    if (Visual.start.col > len) {
403      Visual.start.col = len;
404      Visual.start.coladd = 0;
405    }
406  }
407}
408
409/// Make sure curwin->w_cursor is not on the NUL at the end of the line.
410/// Allow it when in Visual mode and 'selection' is not "old".
411void adjust_cursor_col(void)
412{
413  if (curwin->w_cursor.col > 0
414      && (!Visual.active || *p_sel == 'o')
415      && gchar_cursor() == NUL) {
416    curwin->w_cursor.col--;
417  }
418}
419
420/// Set "curwin->w_leftcol" to "leftcol".
421/// Adjust the cursor position if needed.
422///
423/// @return  true if the cursor was moved.
424bool set_leftcol(colnr_T leftcol)
425{
426  // Return quickly when there is no change.
427  if (curwin->w_leftcol == leftcol) {
428    return false;
429  }
430  curwin->w_leftcol = leftcol;
431
432  changed_cline_bef_curs(curwin);
433  // TODO(hinidu): I think it should be colnr_T or int, but p_siso is long.
434  // Perhaps we can change p_siso to int.
435  int64_t lastcol = curwin->w_leftcol + curwin->w_view_width - win_col_off(curwin) - 1;
436  validate_virtcol(curwin);
437
438  bool retval = false;
439  // If the cursor is right or left of the screen, move it to last or first
440  // visible character.
441  int64_t siso = get_sidescrolloff_value(curwin);
442  if (curwin->w_virtcol > (colnr_T)(lastcol - siso)) {
443    retval = true;
444    coladvance(curwin, (colnr_T)(lastcol - siso));
445  } else if (curwin->w_virtcol < curwin->w_leftcol + siso) {
446    retval = true;
447    coladvance(curwin, (colnr_T)(curwin->w_leftcol + siso));
448  }
449
450  // If the start of the character under the cursor is not on the screen,
451  // advance the cursor one more char.  If this fails (last char of the
452  // line) adjust the scrolling.
453  colnr_T s, e;
454  getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e, 0);
455  if (e > (colnr_T)lastcol) {
456    retval = true;
457    coladvance(curwin, s - 1);
458  } else if (s < curwin->w_leftcol) {
459    retval = true;
460    if (coladvance(curwin, e + 1) == FAIL) {    // there isn't another character
461      curwin->w_leftcol = s;            // adjust w_leftcol instead
462      changed_cline_bef_curs(curwin);
463    }
464  }
465
466  if (retval) {
467    curwin->w_set_curswant = true;
468  }
469  redraw_later(curwin, UPD_NOT_VALID);
470  return retval;
471}
472
473int gchar_cursor(void)
474{
475  return utf_ptr2char(get_cursor_pos_ptr());
476}
477
478/// Return the character immediately before the cursor.
479int char_before_cursor(void)
480{
481  if (curwin->w_cursor.col == 0) {
482    return -1;
483  }
484
485  char *line = get_cursor_line_ptr();
486  char *p = line + curwin->w_cursor.col;
487  int prev_len = utf_head_off(line, p - 1) + 1;
488  return utf_ptr2char(p - prev_len);
489}
490
491/// Write a character at the current cursor position.
492/// It is directly written into the block.
493void pchar_cursor(char c)
494{
495  *(ml_get_buf_mut(curbuf, curwin->w_cursor.lnum) + curwin->w_cursor.col) = c;
496}
497
498/// @return  pointer to cursor line.
499char *get_cursor_line_ptr(void)
500{
501  return ml_get_buf(curbuf, curwin->w_cursor.lnum);
502}
503
504/// @return  pointer to cursor position.
505char *get_cursor_pos_ptr(void)
506{
507  return ml_get_buf(curbuf, curwin->w_cursor.lnum) + curwin->w_cursor.col;
508}
509
510/// @return  length (excluding the NUL) of the cursor line.
511colnr_T get_cursor_line_len(void)
512{
513  return ml_get_buf_len(curbuf, curwin->w_cursor.lnum);
514}
515
516/// @return  length (excluding the NUL) of the cursor position.
517colnr_T get_cursor_pos_len(void)
518{
519  return ml_get_buf_len(curbuf, curwin->w_cursor.lnum) - curwin->w_cursor.col;
520}