robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
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{ 35colnr_T x ; 36 37getvvcol (curwin ,& curwin -> w_cursor ,& x ,NULL ,NULL ,0 ); 38return (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{ 44colnr_T x ; 45pos_T pos ; 46 47pos .lnum = curwin -> w_cursor .lnum ; 48pos .col = col ; 49pos .coladd = coladd ; 50getvvcol (curwin ,& pos ,& x ,NULL ,NULL ,0 ); 51return (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{ 59int rc = coladvance2 (curwin ,& curwin -> w_cursor , true, false,wcol ); 60 61if (wcol == MAXCOL ) { 62curwin -> w_valid &= ~VALID_VIRTCOL ; 63 }else { 64// Virtcol is valid 65set_valid_virtcol (curwin ,wcol ); 66 } 67return 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{ 79int rc = getvpos (wp ,& wp -> w_cursor ,wcol ); 80 81if (wcol == MAXCOL || rc == FAIL ) { 82wp -> 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 85set_valid_virtcol (curwin ,wcol ); 86 } 87return 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{ 95assert (wp == curwin || !addspaces ); 96colnr_T wcol = wcol_arg ; 97int idx ; 98colnr_T col = 0 ; 99int head = 0 ; 100 101int 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 107char * line = ml_get_buf (wp -> w_buffer ,pos -> lnum ); 108int linelen = ml_get_buf_len (wp -> w_buffer ,pos -> lnum ); 109 110if (wcol >=MAXCOL ) { 111idx = linelen - 1 + one_more ; 112col = wcol ; 113 114if ((addspaces || finetune )&& !Visual .active ) { 115wp -> w_curswant = linetabsize (wp ,pos -> lnum )+ one_more ; 116if (wp -> w_curswant > 0 ) { 117wp -> w_curswant -- ; 118 } 119 } 120 }else { 121int width = wp -> w_view_width - win_col_off (wp ); 122int csize = 0 ; 123 124if (finetune 125&& wp -> w_p_wrap 126&& wp -> w_view_width != 0 127&& wcol >= (colnr_T )width 128&& width > 0 ) { 129csize = linetabsize_eol (wp ,pos -> lnum ); 130if (csize > 0 ) { 131csize -- ; 132 } 133 134if (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). 140wcol = (csize /width + 1 )* width - 1 ; 141 } 142 } 143 144CharsizeArg csarg ; 145CSType cstype = init_charsize_arg (& csarg ,wp ,pos -> lnum ,line ); 146StrCharInfo ci = utf_ptr2StrCharInfo (line ); 147col = 0 ; 148while (col <=wcol && * ci .ptr != NUL ) { 149ClusterInfo cli = utf_ClusterInfo (ci ); 150CharSize cs = win_charsize (cstype ,col ,ci .ptr ,ci .chr .value ,& csarg ,cli .cells ); 151csize = cs .width ; 152head = cs .head ; 153col += cs .width ; 154ci = cli .next ; 155 } 156idx = (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. 162if (col > wcol || (!virtual_active (wp )&& one_more == 0 )) { 163idx -= 1 ; 164// Don't count the chars from 'showbreak'. 165csize -= head ; 166col -= csize ; 167 } 168 169if (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 175if (line [idx ]== NUL ) { 176// Append spaces 177int correct = wcol - col ; 178size_t newline_size ; 179STRICT_ADD (idx ,correct ,& newline_size ,size_t ); 180char * newline = xmallocz (newline_size ); 181memcpy (newline ,line , (size_t )idx ); 182memset (newline + idx ,' ' , (size_t )correct ); 183 184ml_replace (pos -> lnum ,newline , false); 185inserted_bytes (pos -> lnum , (colnr_T )idx ,0 ,correct ); 186idx += correct ; 187col = wcol ; 188 }else { 189// Break a tab 190int correct = wcol - col - csize + 1 ;// negative!! 191char * newline ; 192 193if (- correct > csize ) { 194return FAIL ; 195 } 196 197size_t n ; 198STRICT_ADD (linelen - 1 ,csize ,& n ,size_t ); 199newline = xmallocz (n ); 200// Copy first idx chars 201memcpy (newline ,line , (size_t )idx ); 202// Replace idx'th char with csize spaces 203memset (newline + idx ,' ' , (size_t )csize ); 204// Copy the rest of the line 205STRICT_SUB (linelen ,idx ,& n ,size_t ); 206STRICT_SUB (n ,1 ,& n ,size_t ); 207memcpy (newline + idx + csize ,line + idx + 1 ,n ); 208 209ml_replace (pos -> lnum ,newline , false); 210inserted_bytes (pos -> lnum ,idx ,1 ,csize ); 211idx += (csize - 1 + correct ); 212col += correct ; 213 } 214 } 215 } 216 217pos -> col = MAX (idx ,0 ); 218pos -> coladd = 0 ; 219 220if (finetune ) { 221if (wcol == MAXCOL ) { 222// The width of the last character is used to set coladd. 223if (!one_more ) { 224colnr_T scol ,ecol ; 225 226getvcol (wp ,pos ,& scol ,NULL ,& ecol ,0 ); 227pos -> coladd = ecol - scol ; 228 } 229 }else { 230int b = (int )wcol - (int )col ; 231 232// The difference between wcol and col is used to set coladd. 233if (b > 0 && b < (MAXCOL - 2 * wp -> w_view_width )) { 234pos -> coladd = b ; 235 } 236 237col += b ; 238 } 239 } 240 241// Prevent from moving onto a trail byte. 242mark_mb_adjustpos (wp -> w_buffer ,pos ); 243 244if (wcol < 0 || col < wcol ) { 245return FAIL ; 246 } 247return 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{ 255return 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{ 261return inc (& curwin -> w_cursor ); 262} 263 264/// Decrement the cursor position. See dec() for return values. 265int dec_cursor (void ) 266{ 267return 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{ 277linenr_T cursor = wp -> w_cursor .lnum ; 278if (lnum == cursor || !hasAnyFolding (wp )) { 279return lnum - cursor ; 280 } 281 282linenr_T from_line = lnum < cursor ?lnum :cursor ; 283linenr_T to_line = lnum > cursor ?lnum :cursor ; 284linenr_T retval = 0 ; 285 286// Loop until we reach to_line, skipping folds. 287for (;from_line < to_line ;from_line ++ ,retval ++ ) { 288// If from_line is in a fold, set it to the last line of that fold. 289hasFolding (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. 293if (from_line > to_line ) { 294retval -- ; 295 } 296 297return (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{ 304pos -> lnum = MIN (pos -> lnum ,buf -> b_ml .ml_line_count ); 305if (pos -> col > 0 ) { 306pos -> 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{ 313buf_T * buf = win -> w_buffer ; 314if (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. 317if (!hasFolding (win ,buf -> b_ml .ml_line_count ,& win -> w_cursor .lnum ,NULL )) { 318win -> w_cursor .lnum = buf -> b_ml .ml_line_count ; 319 } 320 } 321if (win -> w_cursor .lnum <=0 ) { 322win -> 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{ 330colnr_T oldcol = win -> w_cursor .col ; 331colnr_T oldcoladd = win -> w_cursor .col + win -> w_cursor .coladd ; 332unsigned cur_ve_flags = get_ve_flags (win ); 333 334colnr_T len = ml_get_buf_len (win -> w_buffer ,win -> w_cursor .lnum ); 335if (len == 0 ) { 336win -> 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 343if ((State & MODE_INSERT )|| restart_edit 344|| (State & MODE_TERMINAL ) 345|| (Visual .active && * p_sel != 'o' ) 346|| (cur_ve_flags & kOptVeFlagOnemore ) 347|| virtual_active (win )) { 348win -> w_cursor .col = len ; 349 }else { 350win -> w_cursor .col = len - 1 ; 351// Move the cursor to the head byte. 352mark_mb_adjustpos (win -> w_buffer ,& win -> w_cursor ); 353 } 354 }else if (win -> w_cursor .col < 0 ) { 355win -> 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. 361if (oldcol == MAXCOL ) { 362win -> w_cursor .coladd = 0 ; 363 }else if (cur_ve_flags == kOptVeFlagAll ) { 364if (oldcoladd > win -> w_cursor .col ) { 365win -> 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. 370if (win -> w_cursor .col + 1 < len ) { 371assert (win -> w_cursor .coladd > 0 ); 372int cs ,ce ; 373 374getvcol (win ,& win -> w_cursor ,& cs ,NULL ,& ce ,0 ); 375win -> w_cursor .coladd = MIN (win -> w_cursor .coladd ,ce - cs ); 376 } 377 }else { 378// avoid weird number when there is a miscalculation or overflow 379win -> 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{ 387check_cursor_lnum (wp ); 388check_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{ 395if (Visual .start .lnum > curbuf -> b_ml .ml_line_count ) { 396Visual .start .lnum = curbuf -> b_ml .ml_line_count ; 397Visual .start .col = 0 ; 398Visual .start .coladd = 0 ; 399 }else { 400int len = ml_get_len (Visual .start .lnum ); 401 402if (Visual .start .col > len ) { 403Visual .start .col = len ; 404Visual .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{ 413if (curwin -> w_cursor .col > 0 414&& (!Visual .active || * p_sel == 'o' ) 415&& gchar_cursor ()== NUL ) { 416curwin -> 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. 427if (curwin -> w_leftcol == leftcol ) { 428return false; 429 } 430curwin -> w_leftcol = leftcol ; 431 432changed_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. 435int64_t lastcol = curwin -> w_leftcol + curwin -> w_view_width - win_col_off (curwin )- 1 ; 436validate_virtcol (curwin ); 437 438bool retval = false; 439// If the cursor is right or left of the screen, move it to last or first 440// visible character. 441int64_t siso = get_sidescrolloff_value (curwin ); 442if (curwin -> w_virtcol > (colnr_T )(lastcol - siso )) { 443retval = true; 444coladvance (curwin , (colnr_T )(lastcol - siso )); 445 }else if (curwin -> w_virtcol < curwin -> w_leftcol + siso ) { 446retval = true; 447coladvance (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. 453colnr_T s ,e ; 454getvvcol (curwin ,& curwin -> w_cursor ,& s ,NULL ,& e ,0 ); 455if (e > (colnr_T )lastcol ) { 456retval = true; 457coladvance (curwin ,s - 1 ); 458 }else if (s < curwin -> w_leftcol ) { 459retval = true; 460if (coladvance (curwin ,e + 1 )== FAIL ) {// there isn't another character 461curwin -> w_leftcol = s ;// adjust w_leftcol instead 462changed_cline_bef_curs (curwin ); 463 } 464 } 465 466if (retval ) { 467curwin -> w_set_curswant = true; 468 } 469redraw_later (curwin ,UPD_NOT_VALID ); 470return retval ; 471} 472 473int gchar_cursor (void ) 474{ 475return utf_ptr2char (get_cursor_pos_ptr ()); 476} 477 478/// Return the character immediately before the cursor. 479int char_before_cursor (void ) 480{ 481if (curwin -> w_cursor .col == 0 ) { 482return -1 ; 483 } 484 485char * line = get_cursor_line_ptr (); 486char * p = line + curwin -> w_cursor .col ; 487int prev_len = utf_head_off (line ,p - 1 )+ 1 ; 488return 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{ 501return ml_get_buf (curbuf ,curwin -> w_cursor .lnum ); 502} 503 504/// @return pointer to cursor position. 505char * get_cursor_pos_ptr (void ) 506{ 507return 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{ 513return 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{ 519return ml_get_buf_len (curbuf ,curwin -> w_cursor .lnum )- curwin -> w_cursor .col ; 520}