robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1/// change.c: functions related to changing text 2 3#include <assert.h> 4#include <stdbool.h> 5#include <stdint.h> 6#include <string.h> 7 8#include "nvim/ascii_defs.h" 9#include "nvim/assert_defs.h" 10#include "nvim/autocmd.h" 11#include "nvim/autocmd_defs.h" 12#include "nvim/buffer.h" 13#include "nvim/buffer_defs.h" 14#include "nvim/buffer_updates.h" 15#include "nvim/change.h" 16#include "nvim/charset.h" 17#include "nvim/cursor.h" 18#include "nvim/diff.h" 19#include "nvim/drawscreen.h" 20#include "nvim/eval/vars.h" 21#include "nvim/ex_cmds_defs.h" 22#include "nvim/extmark.h" 23#include "nvim/extmark_defs.h" 24#include "nvim/fold.h" 25#include "nvim/gettext_defs.h" 26#include "nvim/globals.h" 27#include "nvim/highlight_defs.h" 28#include "nvim/indent.h" 29#include "nvim/indent_c.h" 30#include "nvim/insert.h" 31#include "nvim/insexpand.h" 32#include "nvim/macros_defs.h" 33#include "nvim/mark.h" 34#include "nvim/mark_defs.h" 35#include "nvim/marktree_defs.h" 36#include "nvim/mbyte.h" 37#include "nvim/mbyte_defs.h" 38#include "nvim/mcursor.h" 39#include "nvim/memline.h" 40#include "nvim/memline_defs.h" 41#include "nvim/memory.h" 42#include "nvim/message.h" 43#include "nvim/move.h" 44#include "nvim/option.h" 45#include "nvim/option_vars.h" 46#include "nvim/os/time.h" 47#include "nvim/plines.h" 48#include "nvim/pos_defs.h" 49#include "nvim/search.h" 50#include "nvim/spell.h" 51#include "nvim/state.h" 52#include "nvim/state_defs.h" 53#include "nvim/strings.h" 54#include "nvim/textformat.h" 55#include "nvim/types_defs.h" 56#include "nvim/ui.h" 57#include "nvim/undo.h" 58#include "nvim/vim_defs.h" 59 60#include "change.c.generated.h" 61 62/// If the file is readonly, give a warning message with the first change. 63/// Don't do this for autocommands. 64/// Doesn't use emsg(), because it flushes the macro buffer. 65/// If we have undone all changes b_changed will be false, but "b_did_warn" 66/// will be true. 67/// "col" is the column for the message; non-zero when in insert mode and 68/// 'showmode' is on. 69/// Careful: may trigger autocommands that reload the buffer. 70void change_warning (buf_T * buf ,int col ) 71{ 72static const char * w_readonly = N_ ("W10: Warning: Changing a readonly file" ); 73 74if (buf -> b_did_warn == false 75&& curbufIsChanged ()== 0 76&& !autocmd_busy 77&& buf -> b_p_ro ) { 78buf -> b_ro_locked ++ ; 79apply_autocmds (EVENT_FILECHANGEDRO ,NULL ,NULL , false,buf ); 80buf -> b_ro_locked -- ; 81if (!buf -> b_p_ro ) { 82return ; 83 } 84// Do what msg() does, but with a column offset if the warning should 85// be after the mode message. 86msg_start (); 87if (msg_row == Rows - 1 ) { 88msg_col = col ; 89 } 90msg_source (HLF_W ); 91msg_ext_set_kind ("wmsg" ); 92msg_puts_hl (_ (w_readonly ),HLF_W , true); 93set_vim_var_string (VV_WARNINGMSG ,_ (w_readonly ),-1 ); 94msg_clr_eos (); 95msg_end (); 96if (msg_silent == 0 && !silent_mode && ui_active ()) { 97msg_delay (1002 , true);// give the user time to think about it 98 } 99buf -> b_did_warn = true; 100redraw_cmdline = false;// don't redraw and erase the message 101if (msg_row < Rows - 1 ) { 102showmode (); 103 } 104 } 105} 106 107/// Call this function when something in a buffer is changed. 108/// 109/// Most often called through changed_bytes() and changed_lines(), which also 110/// mark the area of the display to be redrawn. 111/// 112/// Careful: may trigger autocommands that reload the buffer. 113void changed (buf_T * buf ) 114{ 115if (!buf -> b_changed ) { 116int save_msg_scroll = msg_scroll ; 117 118// Give a warning about changing a read-only file. This may also 119// check-out the file, thus change "curbuf"! 120change_warning (buf ,0 ); 121 122// Create a swap file if that is wanted. 123// Don't do this for "nofile" and "nowrite" buffer types. 124if (buf -> b_may_swap && !bt_dontwrite (buf )) { 125bool save_need_wait_return = need_wait_return ; 126 127need_wait_return = false; 128ml_open_file (buf ); 129 130// The ml_open_file() can cause an ATTENTION message. 131// Wait two seconds, to make sure the user reads this unexpected 132// message. Since we could be anywhere, call wait_return() now, 133// and don't let the emsg() set msg_scroll. 134if (need_wait_return && emsg_silent == 0 && !in_assert_fails && !ui_has (kUIMessages )) { 135msg_delay (2002 , true); 136wait_return (true); 137msg_scroll = save_msg_scroll ; 138 }else { 139need_wait_return = save_need_wait_return ; 140 } 141 } 142changed_internal (buf ); 143 } 144buf_inc_changedtick (buf ); 145 146// A match highlight can only be invalidated by changes to the current buffer. 147if (buf == curbuf ) { 148Search .hl_match = false; 149 } 150} 151 152/// Internal part of changed(), no user interaction. 153/// Also used for recovery. 154void changed_internal (buf_T * buf ) 155{ 156bool was_changed = buf -> b_changed ; 157buf -> b_changed = true; 158ml_setflags (buf ); 159redraw_buf_status_later (buf ); 160redraw_tabline = true; 161need_maketitle = true;// set window title later 162if (!was_changed ) { 163aucmd_defer_modified (buf , true); 164 } 165} 166 167/// Invalidate a window's w_valid flags and w_lines[] entries after changing lines. 168static void changed_lines_invalidate_win (win_T * wp ,linenr_T lnum ,colnr_T col ,linenr_T lnume , 169linenr_T xtra ) 170{ 171// If the changed line is in a range of previously folded lines, 172// compare with the first line in that range. 173if (wp -> w_cursor .lnum <=lnum ) { 174int i = find_wl_entry (wp ,lnum ); 175if (i >=0 && wp -> w_cursor .lnum > wp -> w_lines [i ].wl_lnum ) { 176changed_line_abv_curs_win (wp ); 177 } 178 } 179 180if (wp -> w_cursor .lnum > lnum ) { 181changed_line_abv_curs_win (wp ); 182 }else if (wp -> w_cursor .lnum == lnum && wp -> w_cursor .col >=col ) { 183changed_cline_bef_curs (wp ); 184 } 185if (wp -> w_botline >=lnum ) { 186if (xtra < 0 ) { 187invalidate_botline_win (wp ); 188 }else { 189// Assume that botline doesn't change (inserted lines make 190// other lines scroll down below botline). 191approximate_botline_win (wp ); 192 } 193 } 194 195// If lines have been inserted/deleted and the buffer has virt_lines, or 196// inline virt_text with 'wrap' enabled, invalidate the line after the changed 197// lines. virt_lines may now be drawn above that line, and inline virt_text 198// may cause that line to wrap. 199if ((xtra < 0 && wp -> w_p_wrap && buf_meta_total (wp -> w_buffer ,kMTMetaInline )) 200|| (xtra != 0 && buf_meta_total (wp -> w_buffer ,kMTMetaLines ))) { 201lnume ++ ; 202 } 203// Check if any w_lines[] entries have become invalid. 204// For entries below the change: Correct the lnums for inserted/deleted lines. 205// Makes it possible to stop displaying after the change. 206for (int i = 0 ;i < wp -> w_lines_valid ;i ++ ) { 207if (wp -> w_lines [i ].wl_valid ) { 208if (wp -> w_lines [i ].wl_lnum >=lnum ) { 209// Do not change wl_lnum at index zero, it is used to compare with w_topline. 210// Invalidate it instead. 211if (i == 0 || wp -> w_lines [i ].wl_lnum < lnume ) { 212// line included in change 213wp -> w_lines [i ].wl_valid = false; 214 }else if (xtra != 0 ) { 215// line below change 216wp -> w_lines [i ].wl_lnum += xtra ; 217wp -> w_lines [i ].wl_foldend += xtra ; 218wp -> w_lines [i ].wl_lastlnum += xtra ; 219 } 220 }else if (wp -> w_lines [i ].wl_lastlnum >=lnum ) { 221// change somewhere inside this range of folded or concealed lines, 222// may need to be redrawn 223wp -> w_lines [i ].wl_valid = false; 224 } 225 } 226 } 227} 228 229/// Line changed_lines_invalidate_win(), but for all windows displaying a buffer. 230void changed_lines_invalidate_buf (buf_T * buf ,linenr_T lnum ,colnr_T col ,linenr_T lnume , 231linenr_T xtra ) 232{ 233FOR_ALL_TAB_WINDOWS (tp ,wp ) { 234if (wp -> w_buffer == buf ) { 235changed_lines_invalidate_win (wp ,lnum ,col ,lnume ,xtra ); 236 } 237 } 238} 239 240/// Common code for when a change was made. 241/// See changed_lines() for the arguments. 242/// Careful: may trigger autocommands that reload the buffer. 243static void changed_common (buf_T * buf ,linenr_T lnum ,colnr_T col ,linenr_T lnume ,linenr_T xtra ) 244{ 245// mark the buffer as modified 246changed (buf ); 247 248FOR_ALL_WINDOWS_IN_TAB (win ,curtab ) { 249if (win -> w_buffer == buf ) { 250if (win -> w_p_diff && diff_internal ()) { 251curtab -> tp_diff_update = true; 252diff_update_line (lnum ); 253 } 254 255// Redraw status when line count changes to prevent 'statusline' or 256// 'rulerformat' items becoming invalid. 257if (xtra != 0 ) { 258win -> w_redr_status = true; 259 } 260 } 261 } 262 263// Set the '. mark. 264if ((cmdmod .cmod_flags & CMOD_KEEPJUMPS )== 0 ) { 265fmarkv_T view = INIT_FMARKV ; 266// Set the markview only if lnum is visible, as changes might be done 267// outside of the current window view. 268 269if (curwin -> w_buffer == buf ) { 270if (lnum >=curwin -> w_topline && lnum <=curwin -> w_botline ) { 271view = mark_view_make (curwin ,curwin -> w_cursor ); 272 } 273 } 274RESET_FMARK (& buf -> b_last_change , ((pos_T ) {lnum ,col ,0 }),buf -> handle ,view ); 275 } 276 277// Update the changelist (g;). Its last entry follows the '. mark. 278if ((cmdmod .cmod_flags & CMOD_KEEPJUMPS )== 0 && !mc_replaying ()) { 279// Create a new entry if a new undo-able change was started or we 280// don't have an entry yet. 281if (buf -> b_new_change || buf -> b_changelistlen == 0 ) { 282bool add ; 283if (buf -> b_changelistlen == 0 ) { 284add = true; 285 }else { 286// Don't create a new entry when the line number is the same 287// as the last one and the column is not too far away. Avoids 288// creating many entries for typing "xxxxx". 289pos_T * p = & buf -> b_changelist [buf -> b_changelistlen - 1 ].mark ; 290if (p -> lnum != lnum ) { 291add = true; 292 }else { 293int cols = comp_textwidth (false); 294if (cols == 0 ) { 295cols = 79 ; 296 } 297add = (p -> col + cols < col || col + cols < p -> col ); 298 } 299 } 300if (add ) { 301// This is the first of a new sequence of undo-able changes 302// and it's at some distance of the last change. Use a new 303// position in the changelist. 304buf -> b_new_change = false; 305 306if (buf -> b_changelistlen == JUMPLISTSIZE ) { 307// changelist is full: remove oldest entry 308buf -> b_changelistlen = JUMPLISTSIZE - 1 ; 309memmove (buf -> b_changelist ,buf -> b_changelist + 1 , 310sizeof (buf -> b_changelist [0 ])* (JUMPLISTSIZE - 1 )); 311FOR_ALL_TAB_WINDOWS (tp ,wp ) { 312// Correct position in changelist for other windows on 313// this buffer. 314if (wp -> w_buffer == buf && wp -> w_changelistidx > 0 ) { 315wp -> w_changelistidx -- ; 316 } 317 } 318 } 319FOR_ALL_TAB_WINDOWS (tp ,wp ) { 320// For other windows, if the position in the changelist is 321// at the end it stays at the end. 322if (wp -> w_buffer == buf 323&& wp -> w_changelistidx == buf -> b_changelistlen ) { 324wp -> w_changelistidx ++ ; 325 } 326 } 327buf -> b_changelistlen ++ ; 328 } 329 } 330buf -> b_changelist [buf -> b_changelistlen - 1 ]= 331buf -> b_last_change ; 332// The current window is always after the last change, so that "g," 333// takes you back to it. 334if (curwin -> w_buffer == buf ) { 335curwin -> w_changelistidx = buf -> b_changelistlen ; 336 } 337 } 338 339if (curwin -> w_buffer == buf && Visual .active ) { 340check_visual_pos (); 341 } 342 343FOR_ALL_TAB_WINDOWS (tp ,wp ) { 344if (wp -> w_buffer == buf ) { 345// Mark this window to be redrawn later. 346if (!redraw_not_allowed && wp -> w_redr_type < UPD_VALID ) { 347wp -> w_redr_type = UPD_VALID ; 348 } 349 350// When inserting/deleting lines and the window has specific lines 351// to be redrawn, w_redraw_top and w_redraw_bot may now be invalid, 352// so just redraw everything. 353if (xtra != 0 && wp -> w_redraw_top != 0 ) { 354redraw_later (wp ,UPD_NOT_VALID ); 355 } 356 357linenr_T last = lnume + xtra - 1 ;// last line after the change 358 359// Reset "w_skipcol" if the topline length has become smaller to 360// such a degree that nothing will be visible anymore, accounting 361// for 'smoothscroll' <<< or 'listchars' "precedes" marker. 362if (wp -> w_skipcol > 0 363&& (last < wp -> w_topline 364|| (wp -> w_topline >=lnum 365&& wp -> w_topline < lnume 366&& (linetabsize_eol (wp ,wp -> w_topline ) 367 <=wp -> w_skipcol + sms_marker_overlap (wp ,-1 ))))) { 368wp -> w_skipcol = 0 ; 369 } 370 371// Check if a change in the buffer has invalidated the cached 372// values for the cursor. 373// Update the folds for this window. Can't postpone this, because 374// a following operator might work on the whole fold: ">>dd". 375foldUpdate (wp ,lnum ,last ); 376 377// The change may cause lines above or below the change to become 378// included in a fold. Set lnum/lnume to the first/last line that 379// might be displayed differently. 380// Set w_cline_folded here as an efficient way to update it when 381// inserting lines just above a closed fold. 382bool folded = hasFoldingWin (wp ,lnum ,& lnum ,NULL , false,NULL ); 383if (wp -> w_cursor .lnum == lnum ) { 384wp -> w_cline_folded = folded ; 385 } 386folded = hasFoldingWin (wp ,last ,NULL ,& last , false,NULL ); 387if (wp -> w_cursor .lnum == last ) { 388wp -> w_cline_folded = folded ; 389 } 390 391changed_lines_invalidate_win (wp ,lnum ,col ,lnume ,xtra ); 392 393// Take care of side effects for setting w_topline when folds have 394// changed. Esp. when the buffer was changed in another window. 395if (hasAnyFolding (wp )) { 396set_topline (wp ,wp -> w_topline ); 397 } 398 399// If lines have been added or removed, relative numbering always 400// requires an update even if cursor didn't move. 401if (wp -> w_p_rnu && xtra != 0 ) { 402wp -> w_last_cursor_lnum_rnu = 0 ; 403 } 404 405if (wp -> w_p_cul && wp -> w_last_cursorline >=lnum ) { 406if (wp -> w_last_cursorline < lnume ) { 407// If 'cursorline' was inside the change, it has already 408// been invalidated in w_lines[] by the loop above. 409wp -> w_last_cursorline = 0 ; 410 }else { 411// If 'cursorline' was below the change, adjust its lnum. 412wp -> w_last_cursorline += xtra ; 413 } 414 } 415 } 416 417if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >=lnum ) { 418search_hl_has_cursor_lnum += xtra ; 419 } 420 } 421 422// Call update_screen() later, which checks out what needs to be redrawn, 423// since it notices b_mod_set and then uses b_mod_*. 424set_must_redraw (UPD_VALID ); 425 426// when the cursor line is changed always trigger CursorMoved 427if (last_cursormoved_win == curwin && curwin -> w_buffer == buf 428&& lnum <=curwin -> w_cursor .lnum 429&& lnume + (xtra < 0 ?- xtra :xtra )> curwin -> w_cursor .lnum ) { 430last_cursormoved .lnum = 0 ; 431 } 432} 433 434/// Changed bytes within a single line for the current buffer. 435/// - marks the windows on this buffer to be redisplayed 436/// - marks the buffer changed by calling changed() 437/// - invalidates cached values 438/// Careful: may trigger autocommands that reload the buffer. 439void changed_bytes (linenr_T lnum ,colnr_T col ) 440{ 441changed_lines_redraw_buf (curbuf ,lnum ,lnum + 1 ,0 ); 442changed_common (curbuf ,lnum ,col ,lnum + 1 ,0 ); 443// When text has been changed at the end of the line, possibly the start of 444// the next line may have SpellCap that should be removed or it needs to be 445// displayed. Schedule the next line for redrawing just in case. 446// Don't do this when displaying '$' at the end of changed text. 447if (spell_check_window (curwin ) 448&& lnum < curbuf -> b_ml .ml_line_count 449&& vim_strchr (p_cpo ,kCpoDollar )== NULL ) { 450redrawWinline (curwin ,lnum + 1 ); 451 } 452// notify any channels that are watching 453buf_updates_send_changes (curbuf ,lnum ,1 ,1 ); 454 455// Diff highlighting in other diff windows may need to be updated too. 456if (curwin -> w_p_diff ) { 457FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 458if (wp -> w_p_diff && wp != curwin ) { 459redraw_later (wp ,UPD_VALID ); 460linenr_T wlnum = diff_lnum_win (lnum ,wp ); 461if (wlnum > 0 ) { 462changed_lines_redraw_buf (wp -> w_buffer ,wlnum ,wlnum + 1 ,0 ); 463 } 464 } 465 } 466 } 467} 468 469/// insert/delete bytes at column 470/// 471/// Like changed_bytes() but also adjust extmark for "new" bytes. 472void inserted_bytes (linenr_T lnum ,colnr_T start_col ,int old_col ,int new_col ) 473{ 474if (curbuf_splice_pending == 0 ) { 475extmark_splice_cols (curbuf , (int )lnum - 1 ,start_col ,old_col ,new_col ,kExtmarkUndo ); 476 } 477 478changed_bytes (lnum ,start_col ); 479} 480 481/// Appended "count" lines below line "lnum" in the given buffer. 482/// Must be called AFTER the change and after mark_adjust(). 483/// Takes care of marking the buffer to be redrawn and sets the changed flag. 484void appended_lines_buf (buf_T * buf ,linenr_T lnum ,linenr_T count ) 485{ 486changed_lines (buf ,lnum + 1 ,0 ,lnum + 1 ,count , true); 487} 488 489/// Appended "count" lines below line "lnum" in the current buffer. 490/// Must be called AFTER the change and after mark_adjust(). 491/// Takes care of marking the buffer to be redrawn and sets the changed flag. 492void appended_lines (linenr_T lnum ,linenr_T count ) 493{ 494appended_lines_buf (curbuf ,lnum ,count ); 495} 496 497/// Like appended_lines(), but adjust marks first. 498void appended_lines_mark (linenr_T lnum ,int count ) 499{ 500mark_adjust (lnum + 1 , (linenr_T )MAXLNUM , (linenr_T )count ,0 ,kExtmarkUndo ); 501changed_lines (curbuf ,lnum + 1 ,0 ,lnum + 1 , (linenr_T )count , true); 502} 503 504/// Deleted "count" lines at line "lnum" in the given buffer. 505/// Must be called AFTER the change and after mark_adjust(). 506/// Takes care of marking the buffer to be redrawn and sets the changed flag. 507void deleted_lines_buf (buf_T * buf ,linenr_T lnum ,linenr_T count ) 508{ 509changed_lines (buf ,lnum ,0 ,lnum + count ,- count , true); 510} 511 512/// Deleted "count" lines at line "lnum" in the current buffer. 513/// Must be called AFTER the change and after mark_adjust(). 514/// Takes care of marking the buffer to be redrawn and sets the changed flag. 515void deleted_lines (linenr_T lnum ,linenr_T count ) 516{ 517deleted_lines_buf (curbuf ,lnum ,count ); 518} 519 520/// Like deleted_lines(), but adjust marks first. 521/// Make sure the cursor is on a valid line before calling, a GUI callback may 522/// be triggered to display the cursor. 523void deleted_lines_mark (linenr_T lnum ,int count ) 524{ 525bool made_empty = (count > 0 )&& curbuf -> b_ml .ml_flags & ML_EMPTY ; 526 527mark_adjust (lnum , (linenr_T )(lnum + count - 1 ),MAXLNUM ,- (linenr_T )count ,kExtmarkNOOP ); 528// if we deleted the entire buffer, we need to implicitly add a new empty line 529extmark_adjust (curbuf ,lnum , (linenr_T )(lnum + count - 1 ),MAXLNUM , 530- (linenr_T )count + (made_empty ?1 :0 ),kExtmarkUndo ); 531changed_lines (curbuf ,lnum ,0 ,lnum + (linenr_T )count , (linenr_T )(- count ), true); 532} 533 534/// Marks the area to be redrawn after a change. 535/// Consider also calling changed_lines_invalidate_buf(). 536/// 537/// @param buf the buffer where lines were changed 538/// @param lnum first line with change 539/// @param lnume line below last changed line 540/// @param xtra number of extra lines (negative when deleting) 541void changed_lines_redraw_buf (buf_T * buf ,linenr_T lnum ,linenr_T lnume ,linenr_T xtra ) 542{ 543// If lines have been deleted and there may be decorations in the buffer, ensure 544// win_update() calculates the height of, and redraws the line to which or whence 545// from its mark may have moved. When lines are deleted, a virt_line mark may 546// have moved be drawn two lines below so increase by one more. 547if (xtra != 0 && buf -> b_marktree -> n_keys > 0 ) { 548lnume += 1 + (xtra < 0 && buf_meta_total (buf ,kMTMetaLines )); 549 } 550 551if (buf -> b_mod_set ) { 552// find the maximum area that must be redisplayed 553buf -> b_mod_top = MIN (buf -> b_mod_top ,lnum ); 554if (lnum < buf -> b_mod_bot ) { 555// adjust old bot position for xtra lines 556buf -> b_mod_bot += xtra ; 557buf -> b_mod_bot = MAX (buf -> b_mod_bot ,lnum ); 558 } 559buf -> b_mod_bot = MAX (buf -> b_mod_bot ,lnume + xtra ); 560buf -> b_mod_xlines += xtra ; 561 }else { 562// set the area that must be redisplayed 563buf -> b_mod_set = true; 564buf -> b_mod_top = lnum ; 565buf -> b_mod_bot = lnume + xtra ; 566buf -> b_mod_xlines = xtra ; 567 } 568} 569 570/// Changed lines for a buffer. 571/// Must be called AFTER the change and after mark_adjust(). 572/// - mark the buffer changed by calling changed() 573/// - mark the windows on this buffer to be redisplayed 574/// - invalidate cached values 575/// "lnum" is the first line that needs displaying, "lnume" the first line 576/// below the changed lines (BEFORE the change). 577/// When only inserting lines, "lnum" and "lnume" are equal. 578/// Takes care of calling changed() and updating b_mod_*. 579/// Careful: may trigger autocommands that reload the buffer. 580/// 581/// @param lnum first line with change 582/// @param col column in first line with change 583/// @param lnume line below last changed line 584/// @param xtra number of extra lines (negative when deleting) 585/// @param do_buf_event some callers like undo/redo call changed_lines() and 586/// then increment changedtick *again*. This flag allows these callers to send 587/// the nvim_buf_lines_event events after they're done modifying changedtick. 588void changed_lines (buf_T * buf ,linenr_T lnum ,colnr_T col ,linenr_T lnume ,linenr_T xtra , 589bool do_buf_event ) 590{ 591changed_lines_redraw_buf (buf ,lnum ,lnume ,xtra ); 592 593if (xtra == 0 && curwin -> w_p_diff && curwin -> w_buffer == buf && !diff_internal ()) { 594// When the number of lines doesn't change then mark_adjust() isn't 595// called and other diff buffers still need to be marked for 596// displaying. 597linenr_T wlnum ; 598 599FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 600if (wp -> w_p_diff && wp != curwin ) { 601redraw_later (wp ,UPD_VALID ); 602wlnum = diff_lnum_win (lnum ,wp ); 603if (wlnum > 0 ) { 604changed_lines_redraw_buf (wp -> w_buffer ,wlnum ,lnume - lnum + wlnum ,0 ); 605 } 606 } 607 } 608 } 609 610changed_common (buf ,lnum ,col ,lnume ,xtra ); 611 612if (do_buf_event ) { 613int64_t num_added = (int64_t )(lnume + xtra - lnum ); 614int64_t num_removed = lnume - lnum ; 615buf_updates_send_changes (buf ,lnum ,num_added ,num_removed ); 616 } 617} 618 619/// Called when the changed flag must be reset for buffer `buf`. 620/// When `ff` is true also reset 'fileformat'. 621/// When `always_inc_changedtick` is true b:changedtick is incremented even 622/// when the changed flag was off. 623void unchanged (buf_T * buf ,bool ff ,bool always_inc_changedtick ) 624{ 625if (buf -> b_changed || (ff && file_ff_differs (buf , false))) { 626bool was_changed = buf -> b_changed ; 627buf -> b_changed = false; 628ml_setflags (buf ); 629if (ff ) { 630save_file_ff (buf ); 631 } 632redraw_buf_status_later (buf ); 633redraw_tabline = true; 634need_maketitle = true;// set window title later 635buf_inc_changedtick (buf ); 636if (was_changed ) { 637aucmd_defer_modified (buf , false); 638 } 639 }else if (always_inc_changedtick ) { 640buf_inc_changedtick (buf ); 641 } 642} 643 644/// Save the current values of 'fileformat' and 'fileencoding', so that we know 645/// the file must be considered changed when the value is different. 646void save_file_ff (buf_T * buf ) 647{ 648buf -> b_start_ffc = (unsigned char )(* buf -> b_p_ff ); 649buf -> b_start_eof = buf -> b_p_eof ; 650buf -> b_start_eol = buf -> b_p_eol ; 651buf -> b_start_bomb = buf -> b_p_bomb ; 652 653// Only use free/alloc when necessary, they take time. 654if (buf -> b_start_fenc == NULL 655|| strcmp (buf -> b_start_fenc ,buf -> b_p_fenc )!= 0 ) { 656xfree (buf -> b_start_fenc ); 657buf -> b_start_fenc = xstrdup (buf -> b_p_fenc ); 658 } 659} 660 661/// Return true if 'fileformat' and/or 'fileencoding' has a different value 662/// from when editing started (save_file_ff() called). 663/// Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was 664/// changed and 'binary' is not set. 665/// Also when 'endofline' was changed and 'fixeol' is not set. 666/// When "ignore_empty" is true don't consider a new, empty buffer to be 667/// changed. 668bool file_ff_differs (buf_T * buf ,bool ignore_empty ) 669FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 670{ 671// In a buffer that was never loaded the options are not valid. 672if (buf -> b_flags & BF_NEVERLOADED ) { 673return false; 674 } 675if (ignore_empty 676&& (buf -> b_flags & BF_NEW ) 677&& buf -> b_ml .ml_line_count == 1 678&& * ml_get_buf (buf ,1 )== NUL ) { 679return false; 680 } 681if (buf -> b_start_ffc != * buf -> b_p_ff ) { 682return true; 683 } 684if ((buf -> b_p_bin || !buf -> b_p_fixeol ) 685&& (buf -> b_start_eof != buf -> b_p_eof || buf -> b_start_eol != buf -> b_p_eol )) { 686return true; 687 } 688if (!buf -> b_p_bin && buf -> b_start_bomb != buf -> b_p_bomb ) { 689return true; 690 } 691if (buf -> b_start_fenc == NULL ) { 692return * buf -> b_p_fenc != NUL ; 693 } 694return strcmp (buf -> b_start_fenc ,buf -> b_p_fenc )!= 0 ; 695} 696 697/// Insert string "p" at the cursor position. Stops at a NUL byte. 698/// Handles Replace mode and multi-byte characters. 699void ins_bytes (char * p ) 700{ 701ins_bytes_len (p ,strlen (p )); 702} 703 704/// Insert string "p" with length "len" at the cursor position. 705/// Handles Replace mode and multi-byte characters. 706void ins_bytes_len (char * p ,size_t len ) 707{ 708size_t n ; 709for (size_t i = 0 ;i < len ;i += n ) { 710// avoid reading past p[len] 711n = (size_t )utfc_ptr2len_len (p + i , (int )(len - i )); 712ins_char_bytes (p + i ,n ); 713 } 714} 715 716/// Insert or replace a single character at the cursor position. 717/// When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character. 718/// Caller must have prepared for undo. 719/// For multi-byte characters we get the whole character, the caller must 720/// convert bytes to a character. 721void ins_char (int c ) 722{ 723char buf [MB_MAXCHAR + 1 ]; 724size_t n = (size_t )utf_char2bytes (c ,buf ); 725 726// When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. 727// Happens for CTRL-Vu9900. 728if (buf [0 ]== 0 ) { 729buf [0 ]= '\n' ; 730 } 731ins_char_bytes (buf ,n ); 732} 733 734void ins_char_bytes (char * buf ,size_t charlen ) 735{ 736// Break tabs if needed. 737if (virtual_active (curwin )&& curwin -> w_cursor .coladd > 0 ) { 738coladvance_force (getviscol ()); 739 } 740 741size_t col = (size_t )curwin -> w_cursor .col ; 742linenr_T lnum = curwin -> w_cursor .lnum ; 743char * oldp = ml_get (lnum ); 744size_t linelen = (size_t )ml_get_len (lnum )+ 1 ;// length of old line including NUL 745 746// The lengths default to the values for when not replacing. 747size_t oldlen = 0 ;// nr of bytes inserted 748size_t newlen = charlen ;// nr of bytes deleted (0 when not replacing) 749 750if (State & REPLACE_FLAG ) { 751if (State & VREPLACE_FLAG ) { 752// Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. 753// Returns the old value of list, so when finished, 754// curwin->w_p_list should be set back to this. 755int old_list = curwin -> w_p_list ; 756if (old_list && vim_strchr (p_cpo ,kCpoListwm )== NULL ) { 757curwin -> w_p_list = false; 758 } 759// In virtual replace mode each character may replace one or more 760// characters (zero if it's a TAB). Count the number of bytes to 761// be deleted to make room for the new character, counting screen 762// cells. May result in adding spaces to fill a gap. 763colnr_T vcol ; 764getvcol (curwin ,& curwin -> w_cursor ,NULL ,& vcol ,NULL ,0 ); 765colnr_T new_vcol = vcol + win_chartabsize (curwin ,buf ,vcol ); 766while (oldp [col + oldlen ]!= NUL && vcol < new_vcol ) { 767vcol += win_chartabsize (curwin ,oldp + col + oldlen ,vcol ); 768// Don't need to remove a TAB that takes us to the right 769// position. 770if (vcol > new_vcol && oldp [col + oldlen ]== TAB ) { 771break ; 772 } 773oldlen += (size_t )utfc_ptr2len (oldp + col + oldlen ); 774// Deleted a bit too much, insert spaces. 775if (vcol > new_vcol ) { 776newlen += (size_t )(vcol - new_vcol ); 777 } 778 } 779curwin -> w_p_list = old_list ; 780 }else if (oldp [col ]!= NUL ) { 781// normal replace 782oldlen = (size_t )utfc_ptr2len (oldp + col ); 783 } 784 785// Push the replaced bytes onto the replace stack, so that they can be 786// put back when BS is used. The bytes of a multi-byte character are 787// done the other way around, so that the first byte is popped off 788// first (it tells the byte length of the character). 789replace_push_nul (); 790replace_push (oldp + col ,oldlen ); 791 } 792 793char * newp = xmalloc (linelen + newlen - oldlen ); 794 795// Copy bytes before the cursor. 796if (col > 0 ) { 797memmove (newp ,oldp ,col ); 798 } 799 800// Copy bytes after the changed character(s). 801char * p = newp + col ; 802if (linelen > col + oldlen ) { 803memmove (p + newlen ,oldp + col + oldlen ,linelen - col - oldlen ); 804 } 805 806// Insert or overwrite the new character. 807memmove (p ,buf ,charlen ); 808 809// Fill with spaces when necessary. 810for (size_t i = charlen ;i < newlen ;i ++ ) { 811p [i ]= ' ' ; 812 } 813 814// Replace the line in the buffer. 815ml_replace (lnum ,newp , false); 816 817// mark the buffer as changed and prepare for displaying 818inserted_bytes (lnum , (colnr_T )col , (int )oldlen , (int )newlen ); 819 820// If we're in Insert or Replace mode and 'showmatch' is set, then briefly 821// show the match for right parens and braces. 822if (p_sm && (State & MODE_INSERT ) 823&& msg_silent == 0 824&& !ins_compl_active ()) { 825showmatch (utf_ptr2char (buf )); 826 } 827 828if (!p_ri || (State & REPLACE_FLAG )) { 829// Normal insert: move cursor right 830curwin -> w_cursor .col += (colnr_T )charlen ; 831 } 832// TODO(Bram): should try to update w_row here, to avoid recomputing it later. 833} 834 835/// Insert a string at the cursor position. 836/// Note: Does NOT handle Replace mode. 837/// Caller must have prepared for undo. 838void ins_str (char * s ,size_t slen ) 839{ 840linenr_T lnum = curwin -> w_cursor .lnum ; 841 842if (virtual_active (curwin )&& curwin -> w_cursor .coladd > 0 ) { 843coladvance_force (getviscol ()); 844 } 845 846colnr_T col = curwin -> w_cursor .col ; 847char * oldp = ml_get (lnum ); 848int oldlen = ml_get_len (lnum ); 849 850char * newp = xmalloc ((size_t )oldlen + slen + 1 ); 851if (col > 0 ) { 852memmove (newp ,oldp , (size_t )col ); 853 } 854memmove (newp + col ,s ,slen ); 855int bytes = oldlen - col + 1 ; 856assert (bytes >=0 ); 857memmove (newp + col + slen ,oldp + col , (size_t )bytes ); 858ml_replace (lnum ,newp , false); 859inserted_bytes (lnum ,col ,0 , (int )slen ); 860curwin -> w_cursor .col += (colnr_T )slen ; 861} 862 863// Delete one character under the cursor. 864// If "fixpos" is true, don't leave the cursor on the NUL after the line. 865// Caller must have prepared for undo. 866// 867// return FAIL for failure, OK otherwise 868int del_char (bool fixpos ) 869{ 870// Make sure the cursor is at the start of a character. 871mb_adjust_cursor (); 872if (* get_cursor_pos_ptr ()== NUL ) { 873return FAIL ; 874 } 875return del_chars (1 ,fixpos ); 876} 877 878/// Like del_bytes(), but delete characters instead of bytes. 879int del_chars (int count ,int fixpos ) 880{ 881int bytes = 0 ; 882char * p = get_cursor_pos_ptr (); 883for (int i = 0 ;i < count && * p != NUL ;i ++ ) { 884int l = utfc_ptr2len (p ); 885bytes += l ; 886p += l ; 887 } 888return del_bytes (bytes ,fixpos , true); 889} 890 891/// Delete "count" bytes under the cursor. 892/// If "fixpos" is true, don't leave the cursor on the NUL after the line. 893/// Caller must have prepared for undo. 894/// 895/// @param count number of bytes to be deleted 896/// @param fixpos_arg leave the cursor on the NUL after the line 897/// @param use_delcombine 'delcombine' option applies 898/// 899/// @return FAIL for failure, OK otherwise 900int del_bytes (colnr_T count ,bool fixpos_arg ,bool use_delcombine ) 901{ 902linenr_T lnum = curwin -> w_cursor .lnum ; 903colnr_T col = curwin -> w_cursor .col ; 904bool fixpos = fixpos_arg ; 905char * oldp = ml_get (lnum ); 906colnr_T oldlen = ml_get_len (lnum ); 907 908// Can't do anything when the cursor is on the NUL after the line. 909if (col >=oldlen ) { 910return FAIL ; 911 } 912// If "count" is zero there is nothing to do. 913if (count == 0 ) { 914return OK ; 915 } 916// If "count" is negative the caller must be doing something wrong. 917if (count < 1 ) { 918siemsg ("E292: Invalid count for del_bytes(): %ld" , (int64_t )count ); 919return FAIL ; 920 } 921 922// If 'delcombine' is set and deleting (less than) one character, only 923// delete the last combining character. 924if (p_deco && use_delcombine && utfc_ptr2len (oldp + col ) >=count ) { 925char * p0 = oldp + col ; 926GraphemeState state = GRAPHEME_STATE_INIT ; 927if (utf_composinglike (p0 ,p0 + utf_ptr2len (p0 ),& state )) { 928// Find the last composing char, there can be several. 929int n = col ; 930do { 931col = n ; 932count = utf_ptr2len (oldp + n ); 933n += count ; 934 }while (utf_composinglike (oldp + col ,oldp + n ,& state )); 935fixpos = false; 936 } 937 } 938 939// When count is too big, reduce it. 940int movelen = oldlen - col - count + 1 ;// includes trailing NUL 941if (movelen <=1 ) { 942// If we just took off the last character of a non-blank line, and 943// fixpos is true, we don't want to end up positioned at the NUL, 944// unless "restart_edit" is set or 'virtualedit' contains "onemore". 945if (col > 0 && fixpos && restart_edit == 0 946&& (get_ve_flags (curwin )& kOptVeFlagOnemore )== 0 ) { 947curwin -> w_cursor .col -- ; 948curwin -> w_cursor .coladd = 0 ; 949curwin -> w_cursor .col -= utf_head_off (oldp ,oldp + curwin -> w_cursor .col ); 950 } 951count = oldlen - col ; 952movelen = 1 ; 953 } 954colnr_T newlen = oldlen - count ; 955 956// If the old line has been allocated the deletion can be done in the 957// existing line. Otherwise a new line has to be allocated. 958bool alloc_newp = !ml_line_alloced ();// check if oldp was allocated 959char * newp ; 960if (!alloc_newp ) { 961ml_add_deleted_len (curbuf -> b_ml .ml_line_ptr ,oldlen ); 962newp = oldp ;// use same allocated memory 963 }else {// need to allocate a new line 964newp = xmallocz ((size_t )newlen ); 965memmove (newp ,oldp , (size_t )col ); 966 } 967memmove (newp + col ,oldp + col + count , (size_t )movelen ); 968if (alloc_newp ) { 969ml_replace (lnum ,newp , false); 970 }else { 971curbuf -> b_ml .ml_line_textlen = newlen + 1 ; 972 } 973 974// mark the buffer as changed and prepare for displaying 975inserted_bytes (lnum ,col ,count ,0 ); 976 977return OK ; 978} 979 980/// open_line: Add a new line below or above the current line. 981/// 982/// For MODE_VREPLACE state, we only add a new line when we get to the end of 983/// the file, otherwise we just start replacing the next line. 984/// 985/// Caller must take care of undo. Since MODE_VREPLACE may affect any number of 986/// lines however, it may call u_save_cursor() again when starting to change a 987/// new line. 988/// "flags": OPENLINE_DELSPACES delete spaces after cursor 989/// OPENLINE_DO_COM format comments 990/// OPENLINE_KEEPTRAIL keep trailing spaces 991/// OPENLINE_MARKFIX adjust mark positions after the line break 992/// OPENLINE_COM_LIST format comments with list or 2nd line indent 993/// OPENLINE_FORCE_INDENT set indent from second_line_indent, ignore 'autoindent' 994/// 995/// "second_line_indent": indent for after ^^D in Insert mode or if flag 996/// OPENLINE_COM_LIST 997/// "did_do_comment" is set to true when intentionally putting the comment 998/// leader in front of the new line. 999/// 1000/// @param dir FORWARD or BACKWARD 1001/// 1002/// @return true on success, false on failure 1003bool open_line (int dir ,int flags ,int second_line_indent ,bool * did_do_comment ) 1004{ 1005char * next_line = NULL ;// copy of the next line 1006char * p_extra = NULL ;// what goes to next line 1007colnr_T less_cols = 0 ;// less columns for mark in new line 1008colnr_T less_cols_off = 0 ;// columns to skip for mark and 1009// extmark adjustment 1010pos_T old_cursor ;// old cursor position 1011colnr_T newcol = 0 ;// new cursor column 1012int newindent = 0 ;// auto-indent of the new line 1013bool trunc_line = false;// truncate current line afterwards 1014bool retval = false;// return value 1015int extra_len = 0 ;// length of p_extra string 1016int lead_len ;// length of comment leader 1017int comment_start = 0 ;// start index of the comment leader 1018char * lead_flags ;// position in 'comments' for comment leader 1019char * leader = NULL ;// copy of comment leader 1020char * allocated = NULL ;// allocated memory 1021char * p ; 1022char saved_char = NUL ;// init for GCC 1023pos_T * pos ; 1024bool do_si = may_do_si (); 1025bool no_si = false;// reset Ins.did_si afterwards 1026int first_char = NUL ;// init for GCC 1027int vreplace_mode ; 1028bool did_append ;// appended a new line 1029int saved_pi = curbuf -> b_p_pi ;// copy of preserveindent setting 1030 1031linenr_T lnum = curwin -> w_cursor .lnum ; 1032colnr_T mincol = curwin -> w_cursor .col + 1 ; 1033 1034// make a copy of the current line so we can mess with it 1035char * saved_line = xstrnsave (get_cursor_line_ptr (), (size_t )get_cursor_line_len ()); 1036 1037if (State & VREPLACE_FLAG ) { 1038// With MODE_VREPLACE we make a copy of the next line, which we will be 1039// starting to replace. First make the new line empty and let vim play 1040// with the indenting and comment leader to its heart's content. Then 1041// we grab what it ended up putting on the new line, put back the 1042// original line, and call ins_char() to put each new character onto 1043// the line, replacing what was there before and pushing the right 1044// stuff onto the replace stack. -- webb. 1045if (curwin -> w_cursor .lnum < orig_line_count ) { 1046next_line = xstrnsave (ml_get (curwin -> w_cursor .lnum + 1 ), 1047 (size_t )ml_get_len (curwin -> w_cursor .lnum + 1 )); 1048 }else { 1049next_line = xstrdup ("" ); 1050 } 1051 1052// In MODE_VREPLACE state, a NL replaces the rest of the line, and 1053// starts replacing the next line, so push all of the characters left 1054// on the line onto the replace stack. We'll push any other characters 1055// that might be replaced at the start of the next line (due to 1056// autoindent etc) a bit later. 1057replace_push_nul ();// Call twice because BS over NL expects it 1058replace_push_nul (); 1059p = saved_line + curwin -> w_cursor .col ; 1060replace_push (p ,strlen (p )); 1061saved_line [curwin -> w_cursor .col ]= NUL ; 1062 } 1063 1064if ((State & MODE_INSERT )&& (State & VREPLACE_FLAG )== 0 ) { 1065p_extra = saved_line + curwin -> w_cursor .col ; 1066if (do_si ) {// need first char after new line break 1067p = skipwhite (p_extra ); 1068first_char = (unsigned char )(* p ); 1069 } 1070extra_len = (int )strlen (p_extra ); 1071saved_char = * p_extra ; 1072* p_extra = NUL ; 1073 } 1074 1075u_clearline (curbuf );// cannot do "U" command when adding lines 1076Ins .did_si = false; 1077Ins .ai_col = 0 ; 1078 1079// If we just did an auto-indent, then we didn't type anything on 1080// the prior line, and it should be truncated. Do this even if 'ai' is not 1081// set because automatically inserting a comment leader also sets Ins.did_ai. 1082if (dir == FORWARD && Ins .did_ai ) { 1083trunc_line = true; 1084 } 1085 1086if ((flags & OPENLINE_FORCE_INDENT )) { 1087newindent = second_line_indent ; 1088 }else if (curbuf -> b_p_ai || do_si ) { 1089// If 'autoindent' and/or 'smartindent' is set, try to figure out what 1090// indent to use for the new line. 1091// count white space on current line 1092newindent = indent_size_ts (saved_line ,curbuf -> b_p_ts ,curbuf -> b_p_vts_array ); 1093if (newindent == 0 && !(flags & OPENLINE_COM_LIST )) { 1094newindent = second_line_indent ;// for ^^D command in insert mode 1095 } 1096 1097// Do smart indenting. 1098// In insert/replace mode (only when dir == FORWARD) 1099// we may move some text to the next line. If it starts with '{' 1100// don't add an indent. Fixes inserting a NL before '{' in line 1101// "if (condition) {" 1102if (!trunc_line && do_si && * saved_line != NUL 1103&& (p_extra == NULL || first_char != '{' )) { 1104old_cursor = curwin -> w_cursor ; 1105char * ptr = saved_line ; 1106if (flags & OPENLINE_DO_COM ) { 1107lead_len = get_leader_len (ptr ,NULL , false, true); 1108 }else { 1109lead_len = 0 ; 1110 } 1111if (dir == FORWARD ) { 1112// Skip preprocessor directives, unless they are recognised as comments. 1113if (lead_len == 0 && ptr [0 ]== '#' ) { 1114while (ptr [0 ]== '#' && curwin -> w_cursor .lnum > 1 ) { 1115ptr = ml_get (-- curwin -> w_cursor .lnum ); 1116 } 1117newindent = get_indent (); 1118 } 1119if (flags & OPENLINE_DO_COM ) { 1120lead_len = get_leader_len (ptr ,NULL , false, true); 1121 }else { 1122lead_len = 0 ; 1123 } 1124if (lead_len > 0 ) { 1125// This case gets the following right: 1126// \* 1127// * A comment (read '\' as '/'). 1128// */ 1129// #define IN_THE_WAY 1130// This should line up here; 1131p = skipwhite (ptr ); 1132if (p [0 ]== '/' && p [1 ]== '*' ) { 1133p ++ ; 1134 } 1135if (p [0 ]== '*' ) { 1136for (p ++ ;* p ;p ++ ) { 1137if (p [0 ]== '/' && p [-1 ]== '*' ) { 1138// End of C comment, indent should line up 1139// with the line containing the start of 1140// the comment. 1141curwin -> w_cursor .col = (colnr_T )(p - ptr ); 1142if ((pos = findmatch (NULL ,NUL ))!= NULL ) { 1143curwin -> w_cursor .lnum = pos -> lnum ; 1144newindent = get_indent (); 1145break ; 1146 } 1147// this may make "ptr" invalid, get it again 1148ptr = ml_get (curwin -> w_cursor .lnum ); 1149p = ptr + curwin -> w_cursor .col ; 1150 } 1151 } 1152 } 1153 }else {// Not a comment line 1154// Find last non-blank in line 1155p = ptr + strlen (ptr )- 1 ; 1156while (p > ptr && ascii_iswhite (* p )) { 1157p -- ; 1158 } 1159char last_char = * p ; 1160 1161// find the character just before the '{' or ';' 1162if (last_char == '{' || last_char == ';' ) { 1163if (p > ptr ) { 1164p -- ; 1165 } 1166while (p > ptr && ascii_iswhite (* p )) { 1167p -- ; 1168 } 1169 } 1170// Try to catch lines that are split over multiple 1171// lines. eg: 1172// if (condition && 1173// condition) { 1174// Should line up here! 1175// } 1176if (* p == ')' ) { 1177curwin -> w_cursor .col = (colnr_T )(p - ptr ); 1178if ((pos = findmatch (NULL ,'(' ))!= NULL ) { 1179curwin -> w_cursor .lnum = pos -> lnum ; 1180newindent = get_indent (); 1181ptr = get_cursor_line_ptr (); 1182 } 1183 } 1184// If last character is '{' do indent, without 1185// checking for "if" and the like. 1186if (last_char == '{' ) { 1187Ins .did_si = true;// do indent 1188no_si = true;// don't delete it when '{' typed 1189// Look for "if" and the like, use 'cinwords'. 1190// Don't do this if the previous line ended in ';' or 1191// '}'. 1192 }else if (last_char != ';' && last_char != '}' 1193&& cin_is_cinword (ptr )) { 1194Ins .did_si = true; 1195 } 1196 } 1197 }else {// dir == BACKWARD 1198// Skip preprocessor directives, unless they are 1199// recognised as comments. 1200if (lead_len == 0 && ptr [0 ]== '#' ) { 1201bool was_backslashed = false; 1202 1203while ((ptr [0 ]== '#' || was_backslashed ) 1204&& curwin -> w_cursor .lnum < curbuf -> b_ml .ml_line_count ) { 1205if (* ptr && ptr [strlen (ptr )- 1 ]== '\\' ) { 1206was_backslashed = true; 1207 }else { 1208was_backslashed = false; 1209 } 1210ptr = ml_get (++ curwin -> w_cursor .lnum ); 1211 } 1212if (was_backslashed ) { 1213newindent = 0 ;// Got to end of file 1214 }else { 1215newindent = get_indent (); 1216 } 1217 } 1218p = skipwhite (ptr ); 1219if (* p == '}' ) {// if line starts with '}': do indent 1220Ins .did_si = true; 1221 }else {// can delete indent when '{' typed 1222Ins .can_si_back = true; 1223 } 1224 } 1225curwin -> w_cursor = old_cursor ; 1226 } 1227if (do_si ) { 1228Ins .can_si = true; 1229 } 1230 1231Ins .did_ai = true; 1232 } 1233 1234// May do indenting after opening a new line. 1235bool do_cindent = !p_paste && (curbuf -> b_p_cin || curbuf -> b_p_inde .type != kCallbackNone ) 1236&& in_cinkeys (dir == FORWARD ?KEY_OPEN_FORW :KEY_OPEN_BACK , 1237' ' ,linewhite (curwin -> w_cursor .lnum )) 1238&& !(flags & OPENLINE_FORCE_INDENT ); 1239 1240// Find out if the current line starts with a comment leader. 1241// This may then be inserted in front of the new line. 1242Ins .end_comment_pending = NUL ; 1243if (flags & OPENLINE_DO_COM ) { 1244lead_len = get_leader_len (saved_line ,& lead_flags ,dir == BACKWARD , true); 1245if (lead_len == 0 && curbuf -> b_p_cin && do_cindent && dir == FORWARD 1246&& (!has_format_option (kFoNoOpenComs )|| (flags & OPENLINE_FORMAT ))) { 1247// Check for a line comment after code. 1248comment_start = check_linecomment (saved_line ); 1249if (comment_start != MAXCOL ) { 1250lead_len = get_leader_len (saved_line + comment_start ,& lead_flags , false, true); 1251if (lead_len != 0 ) { 1252lead_len += comment_start ; 1253if (did_do_comment != NULL ) { 1254* did_do_comment = true; 1255 } 1256 } 1257 } 1258 } 1259 }else { 1260lead_len = 0 ; 1261 } 1262if (lead_len > 0 ) { 1263char * lead_repl = NULL ;// replaces comment leader 1264int lead_repl_len = 0 ;// length of *lead_repl 1265char lead_middle [COM_MAX_LEN ];// middle-comment string 1266int lead_middle_len ;// length of the lead_middle 1267char lead_end [COM_MAX_LEN ];// end-comment string 1268char * comment_end = NULL ;// where lead_end has been found 1269int extra_space = false;// append extra space 1270bool require_blank = false;// requires blank after middle 1271char * p2 ; 1272 1273// If the comment leader has the start, middle or end flag, it may not 1274// be used or may be replaced with the middle leader. 1275for (p = lead_flags ;* p && * p != ':' ;p ++ ) { 1276if (* p == COM_BLANK ) { 1277require_blank = true; 1278continue ; 1279 } 1280if (* p == COM_START || * p == COM_MIDDLE ) { 1281int current_flag = (unsigned char )(* p ); 1282if (* p == COM_START ) { 1283// Doing "O" on a start of comment does not insert leader. 1284if (dir == BACKWARD ) { 1285lead_len = 0 ; 1286break ; 1287 } 1288 1289// find start of middle part 1290copy_option_part (& p ,lead_middle ,COM_MAX_LEN ,"," ); 1291require_blank = false; 1292 } 1293 1294// Isolate the strings of the middle and end leader. 1295while (* p && p [-1 ]!= ':' ) {// find end of middle flags 1296if (* p == COM_BLANK ) { 1297require_blank = true; 1298 } 1299p ++ ; 1300 } 1301lead_middle_len = (int )copy_option_part (& p ,lead_middle ,COM_MAX_LEN ,"," ); 1302 1303while (* p && p [-1 ]!= ':' ) {// find end of end flags 1304// Check whether we allow automatic ending of comments 1305if (* p == COM_AUTO_END ) { 1306Ins .end_comment_pending = -1 ;// means we want to set it 1307 } 1308p ++ ; 1309 } 1310size_t n = copy_option_part (& p ,lead_end ,COM_MAX_LEN ,"," ); 1311 1312if (Ins .end_comment_pending == -1 ) {// we can set it now 1313Ins .end_comment_pending = (unsigned char )lead_end [n - 1 ]; 1314 } 1315 1316// If the end of the comment is in the same line, don't use 1317// the comment leader. 1318if (dir == FORWARD ) { 1319for (p = saved_line + lead_len ;* p ;p ++ ) { 1320if (strncmp (p ,lead_end ,n )== 0 ) { 1321comment_end = p ; 1322lead_len = 0 ; 1323break ; 1324 } 1325 } 1326 } 1327 1328// Doing "o" on a start of comment inserts the middle leader. 1329if (lead_len > 0 ) { 1330if (current_flag == COM_START ) { 1331lead_repl = lead_middle ; 1332lead_repl_len = lead_middle_len ; 1333 } 1334 1335// If we have hit RETURN immediately after the start 1336// comment leader, then put a space after the middle 1337// comment leader on the next line. 1338if (!ascii_iswhite (saved_line [lead_len - 1 ]) 1339&& ((p_extra != NULL 1340&& (int )curwin -> w_cursor .col == lead_len ) 1341|| (p_extra == NULL 1342&& saved_line [lead_len ]== NUL ) 1343|| require_blank )) { 1344extra_space = true; 1345 } 1346 } 1347break ; 1348 } 1349if (* p == COM_END ) { 1350// Doing "o" on the end of a comment does not insert leader. 1351// Remember where the end is, might want to use it to find the 1352// start (for C-comments). 1353if (dir == FORWARD ) { 1354comment_end = skipwhite (saved_line ); 1355lead_len = 0 ; 1356break ; 1357 } 1358 1359// Doing "O" on the end of a comment inserts the middle leader. 1360// Find the string for the middle leader, searching backwards. 1361while (p > curbuf -> b_p_com && * p != ',' ) { 1362p -- ; 1363 } 1364for (lead_repl = p ;lead_repl > curbuf -> b_p_com 1365&& lead_repl [-1 ]!= ':' ;lead_repl -- ) {} 1366lead_repl_len = (int )(p - lead_repl ); 1367 1368// We can probably always add an extra space when doing "O" on 1369// the comment-end 1370extra_space = true; 1371 1372// Check whether we allow automatic ending of comments 1373for (p2 = p ;* p2 && * p2 != ':' ;p2 ++ ) { 1374if (* p2 == COM_AUTO_END ) { 1375Ins .end_comment_pending = -1 ;// means we want to set it 1376 } 1377 } 1378if (Ins .end_comment_pending == -1 ) { 1379// Find last character in end-comment string 1380while (* p2 && * p2 != ',' ) { 1381p2 ++ ; 1382 } 1383Ins .end_comment_pending = (unsigned char )p2 [-1 ]; 1384 } 1385break ; 1386 } 1387if (* p == COM_FIRST ) { 1388// Comment leader for first line only: Don't repeat leader 1389// when using "O", blank out leader when using "o". 1390if (dir == BACKWARD ) { 1391lead_len = 0 ; 1392 }else { 1393lead_repl = "" ; 1394lead_repl_len = 0 ; 1395 } 1396break ; 1397 } 1398 } 1399if (lead_len > 0 ) { 1400// allocate buffer (may concatenate p_extra later) 1401int bytes = lead_len 1402+ lead_repl_len 1403+ extra_space 1404+ extra_len 1405+ (second_line_indent > 0 ?second_line_indent :0 ) 1406+ 1 ; 1407assert (bytes >=0 ); 1408leader = xmalloc ((size_t )bytes ); 1409allocated = leader ;// remember to free it later 1410 1411xmemcpyz (leader ,saved_line , (size_t )lead_len ); 1412 1413// TODO(vim): handle multi-byte and double width chars 1414for (int li = 0 ;li < comment_start ;li ++ ) { 1415if (!ascii_iswhite (leader [li ])) { 1416leader [li ]= ' ' ; 1417 } 1418 } 1419 1420// Replace leader with lead_repl, right or left adjusted 1421if (lead_repl != NULL ) { 1422int c = 0 ; 1423int off = 0 ; 1424 1425for (p = lead_flags ;* p != NUL && * p != ':' ;) { 1426if (* p == COM_RIGHT || * p == COM_LEFT ) { 1427c = (unsigned char )(* p ++ ); 1428 }else if (ascii_isdigit (* p )|| * p == '-' ) { 1429off = getdigits_int (& p , true,0 ); 1430 }else { 1431p ++ ; 1432 } 1433 } 1434if (c == COM_RIGHT ) {// right adjusted leader 1435// find last non-white in the leader to line up with 1436for (p = leader + lead_len - 1 ;p > leader 1437&& ascii_iswhite (* p );p -- ) {} 1438p ++ ; 1439 1440// Compute the length of the replaced characters in 1441// screen characters, not bytes. 1442 { 1443int repl_size = vim_strnsize (lead_repl ,lead_repl_len ); 1444int old_size = 0 ; 1445char * endp = p ; 1446 1447while (old_size < repl_size && p > leader ) { 1448MB_PTR_BACK (leader ,p ); 1449old_size += ptr2cells (p ); 1450 } 1451int l = lead_repl_len - (int )(endp - p ); 1452if (l != 0 ) { 1453memmove (endp + l ,endp , 1454 (size_t )((leader + lead_len )- endp )); 1455 } 1456lead_len += l ; 1457 } 1458memmove (p ,lead_repl , (size_t )lead_repl_len ); 1459if (p + lead_repl_len > leader + lead_len ) { 1460p [lead_repl_len ]= NUL ; 1461 } 1462 1463// blank-out any other chars from the old leader. 1464while (-- p >=leader ) { 1465int l = utf_head_off (leader ,p ); 1466 1467if (l > 1 ) { 1468p -= l ; 1469if (ptr2cells (p )> 1 ) { 1470p [1 ]= ' ' ; 1471l -- ; 1472 } 1473memmove (p + 1 ,p + l + 1 , 1474 (size_t )((leader + lead_len )- (p + l + 1 ))); 1475lead_len -= l ; 1476* p = ' ' ; 1477 }else if (!ascii_iswhite (* p )) { 1478* p = ' ' ; 1479 } 1480 } 1481 }else {// left adjusted leader 1482p = skipwhite (leader ); 1483// Compute the length of the replaced characters in 1484// screen characters, not bytes. Move the part that is 1485// not to be overwritten. 1486 { 1487int repl_size = vim_strnsize (lead_repl ,lead_repl_len ); 1488int i ; 1489int l ; 1490 1491for (i = 0 ;i < lead_len && p [i ]!= NUL ;i += l ) { 1492l = utfc_ptr2len (p + i ); 1493if (vim_strnsize (p ,i + l )> repl_size ) { 1494break ; 1495 } 1496 } 1497if (i != lead_repl_len ) { 1498memmove (p + lead_repl_len ,p + i , 1499 (size_t )(lead_len - i - (p - leader ))); 1500lead_len += lead_repl_len - i ; 1501 } 1502 } 1503memmove (p ,lead_repl , (size_t )lead_repl_len ); 1504 1505// Replace any remaining non-white chars in the old 1506// leader by spaces. Keep Tabs, the indent must 1507// remain the same. 1508for (p += lead_repl_len ;p < leader + lead_len ;p ++ ) { 1509if (!ascii_iswhite (* p )) { 1510// Don't put a space before a TAB. 1511if (p + 1 < leader + lead_len && p [1 ]== TAB ) { 1512lead_len -- ; 1513memmove (p ,p + 1 , (size_t )(leader + lead_len - p )); 1514 }else { 1515int l = utfc_ptr2len (p ); 1516 1517if (l > 1 ) { 1518if (ptr2cells (p )> 1 ) { 1519// Replace a double-wide char with 1520// two spaces 1521l -- ; 1522* p ++ = ' ' ; 1523 } 1524memmove (p + 1 ,p + l , (size_t )(leader + lead_len - p )); 1525lead_len -= l - 1 ; 1526 } 1527* p = ' ' ; 1528 } 1529 } 1530 } 1531* p = NUL ; 1532 } 1533 1534// Recompute the indent, it may have changed. 1535if (curbuf -> b_p_ai || do_si ) { 1536newindent = indent_size_ts (leader ,curbuf -> b_p_ts ,curbuf -> b_p_vts_array ); 1537 } 1538 1539// Add the indent offset 1540if (newindent + off < 0 ) { 1541off = - newindent ; 1542newindent = 0 ; 1543 }else { 1544newindent += off ; 1545 } 1546 1547// Correct trailing spaces for the shift, so that 1548// alignment remains equal. 1549while (off > 0 && lead_len > 0 1550&& leader [lead_len - 1 ]== ' ' ) { 1551// Don't do it when there is a tab before the space 1552if (vim_strchr (skipwhite (leader ),'\t' )!= NULL ) { 1553break ; 1554 } 1555lead_len -- ; 1556off -- ; 1557 } 1558 1559// If the leader ends in white space, don't add an 1560// extra space 1561if (lead_len > 0 && ascii_iswhite (leader [lead_len - 1 ])) { 1562extra_space = false; 1563 } 1564leader [lead_len ]= NUL ; 1565 } 1566 1567if (extra_space ) { 1568leader [lead_len ++ ]= ' ' ; 1569leader [lead_len ]= NUL ; 1570 } 1571 1572newcol = lead_len ; 1573 1574// if a new indent will be set below, remove the indent that 1575// is in the comment leader 1576if (newindent || Ins .did_si ) { 1577while (lead_len && ascii_iswhite (* leader )) { 1578lead_len -- ; 1579newcol -- ; 1580leader ++ ; 1581 } 1582 } 1583Ins .did_si = Ins .can_si = false; 1584 }else if (comment_end != NULL ) { 1585// We have finished a comment, so we don't use the leader. 1586// If this was a C-comment and 'ai' or 'si' is set do a normal 1587// indent to align with the line containing the start of the 1588// comment. 1589if (comment_end [0 ]== '*' && comment_end [1 ]== '/' 1590&& (curbuf -> b_p_ai || do_si )) { 1591old_cursor = curwin -> w_cursor ; 1592curwin -> w_cursor .col = (colnr_T )(comment_end - saved_line ); 1593if ((pos = findmatch (NULL ,NUL ))!= NULL ) { 1594curwin -> w_cursor .lnum = pos -> lnum ; 1595newindent = get_indent (); 1596 } 1597curwin -> w_cursor = old_cursor ; 1598 } 1599 } 1600 } 1601 1602// (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD 1603if (p_extra != NULL ) { 1604* p_extra = saved_char ;// restore char that NUL replaced 1605 1606// When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first 1607// non-blank. 1608// 1609// When in MODE_REPLACE state, put the deleted blanks on the replace 1610// stack, preceded by a NUL, so they can be put back when a BS is 1611// entered. 1612if (REPLACE_NORMAL (State )) { 1613replace_push_nul ();// end of extra blanks 1614 } 1615if (curbuf -> b_p_ai || (flags & OPENLINE_DELSPACES )) { 1616while ((* p_extra == ' ' || * p_extra == '\t' ) 1617&& !utf_iscomposing_first (utf_ptr2char (p_extra + 1 ))) { 1618if (REPLACE_NORMAL (State )) { 1619replace_push (p_extra ,1 );// always ascii, len = 1 1620 } 1621p_extra ++ ; 1622less_cols_off ++ ; 1623 } 1624 } 1625 1626// columns for marks adjusted for removed columns 1627less_cols = (int )(p_extra - saved_line ); 1628 } 1629 1630if (p_extra == NULL ) { 1631p_extra = "" ;// append empty line 1632 } 1633 1634// concatenate leader and p_extra, if there is a leader 1635if (lead_len > 0 ) { 1636if (flags & OPENLINE_COM_LIST && second_line_indent > 0 ) { 1637int padding = second_line_indent 1638- (newindent + (int )strlen (leader )); 1639 1640// Here whitespace is inserted after the comment char. 1641// Below, set_indent(newindent, SIN_INSERT) will insert the 1642// whitespace needed before the comment char. 1643for (int i = 0 ;i < padding ;i ++ ) { 1644strcat (leader ," " ); 1645less_cols -- ; 1646newcol ++ ; 1647 } 1648 } 1649strcat (leader ,p_extra ); 1650p_extra = leader ; 1651Ins .did_ai = true;// So truncating blanks works with comments 1652less_cols -= lead_len ; 1653 }else { 1654Ins .end_comment_pending = NUL ;// turns out there was no leader 1655 } 1656 1657curbuf_splice_pending ++ ; 1658old_cursor = curwin -> w_cursor ; 1659int old_cmod_flags = cmdmod .cmod_flags ; 1660char * prompt_moved = NULL ; 1661if (dir == BACKWARD ) { 1662// In case of prompt buffer, when we are applying 'normal O' operation on line of prompt, 1663// we can't add a new line before the prompt. In this case, we move the prompt text one 1664// line below and create a new prompt line as current line. 1665if (bt_prompt (curbuf )&& curwin -> w_cursor .lnum == curbuf -> b_prompt_start .mark .lnum ) { 1666char * prompt_line = ml_get (curwin -> w_cursor .lnum ); 1667char * prompt = prompt_text (); 1668size_t prompt_len = strlen (prompt ); 1669 1670if (strncmp (prompt_line ,prompt ,prompt_len )== 0 ) { 1671STRMOVE (prompt_line ,prompt_line + prompt_len ); 1672// We are moving the lines but the b_prompt_start mark needs to stay in 1673// place so freezing marks before making the move. 1674cmdmod .cmod_flags = cmdmod .cmod_flags |CMOD_LOCKMARKS ; 1675ml_replace (curwin -> w_cursor .lnum ,prompt_line , true); 1676prompt_moved = concat_str (prompt ,p_extra ); 1677p_extra = prompt_moved ; 1678 } 1679 } 1680curwin -> w_cursor .lnum -- ; 1681 } 1682if ((State & VREPLACE_FLAG )== 0 || old_cursor .lnum >=orig_line_count ) { 1683if (ml_append (curwin -> w_cursor .lnum ,p_extra ,0 , false)== FAIL ) { 1684 gototheend ; 1685 } 1686// Postpone calling changed_lines(), because it would mess up folding 1687// with markers. 1688mark_adjust (curwin -> w_cursor .lnum + 1 , (linenr_T )MAXLNUM ,1 ,0 ,kExtmarkNOOP ); 1689did_append = true; 1690 }else { 1691// In MODE_VREPLACE state we are starting to replace the next line. 1692curwin -> w_cursor .lnum ++ ; 1693if (curwin -> w_cursor .lnum >=Ins .start .lnum + vr_lines_changed ) { 1694// In case we NL to a new line, BS to the previous one, and NL 1695// again, we don't want to save the new line for undo twice. 1696u_save_cursor ();// errors are ignored! 1697vr_lines_changed ++ ; 1698 } 1699ml_replace (curwin -> w_cursor .lnum ,p_extra , true); 1700changed_bytes (curwin -> w_cursor .lnum ,0 ); 1701// TODO(vigoux): extmark_splice_cols here?? 1702curwin -> w_cursor .lnum -- ; 1703did_append = false; 1704 } 1705 1706inhibit_delete_count ++ ; 1707if (newindent || Ins .did_si ) { 1708curwin -> w_cursor .lnum ++ ; 1709if (Ins .did_si ) { 1710int sw = get_sw_value (curbuf ); 1711 1712if (p_sr ) { 1713newindent -= newindent %sw ; 1714 } 1715newindent += sw ; 1716 } 1717// Copy the indent 1718if (curbuf -> b_p_ci ) { 1719copy_indent (newindent ,saved_line ); 1720 1721// Set the 'preserveindent' option so that any further screwing 1722// with the line doesn't entirely destroy our efforts to preserve 1723// it. It gets restored at the function end. 1724curbuf -> b_p_pi = true; 1725 }else { 1726set_indent (newindent ,SIN_INSERT |SIN_NOMARK ); 1727 } 1728less_cols -= curwin -> w_cursor .col ; 1729 1730Ins .ai_col = curwin -> w_cursor .col ; 1731 1732// In MODE_REPLACE state, for each character in the new indent, there 1733// must be a NUL on the replace stack, for when it is deleted with BS 1734if (REPLACE_NORMAL (State )) { 1735for (colnr_T n = 0 ;n < curwin -> w_cursor .col ;n ++ ) { 1736replace_push_nul (); 1737 } 1738 } 1739newcol += curwin -> w_cursor .col ; 1740if (no_si ) { 1741Ins .did_si = false; 1742 } 1743 } 1744inhibit_delete_count -- ; 1745 1746// In MODE_REPLACE state, for each character in the extra leader, there 1747// must be a NUL on the replace stack, for when it is deleted with BS. 1748if (REPLACE_NORMAL (State )) { 1749while (lead_len -- > 0 ) { 1750replace_push_nul (); 1751 } 1752 } 1753 1754curwin -> w_cursor = old_cursor ; 1755 1756if (dir == FORWARD ) { 1757if (trunc_line || (State & MODE_INSERT )) { 1758// truncate current line at cursor 1759saved_line [curwin -> w_cursor .col ]= NUL ; 1760// Remove trailing white space, unless OPENLINE_KEEPTRAIL used. 1761if (trunc_line && !(flags & OPENLINE_KEEPTRAIL )) { 1762truncate_spaces (saved_line , (size_t )curwin -> w_cursor .col ); 1763 } 1764ml_replace (curwin -> w_cursor .lnum ,saved_line , false); 1765 1766int new_len = (int )strlen (saved_line ); 1767 1768// TODO(vigoux): maybe there is issues there with expandtabs ? 1769int cols_spliced = 0 ; 1770if (new_len < curwin -> w_cursor .col ) { 1771extmark_splice_cols (curbuf , (int )curwin -> w_cursor .lnum - 1 , 1772new_len ,curwin -> w_cursor .col - new_len ,0 ,kExtmarkUndo ); 1773cols_spliced = curwin -> w_cursor .col - new_len ; 1774 } 1775 1776saved_line = NULL ; 1777if (did_append ) { 1778// Always move extmarks - Here we move only the line where the cursor is, 1779// the previous mark_adjust() took care of the lines after. 1780int cols_added = mincol - 1 + less_cols_off - less_cols ; 1781extmark_splice (curbuf , (int )lnum - 1 ,mincol - 1 - cols_spliced , 17820 ,less_cols_off ,less_cols_off , 17831 ,cols_added ,1 + cols_added ,kExtmarkUndo ); 1784 1785changed_lines (curbuf ,curwin -> w_cursor .lnum ,curwin -> w_cursor .col , 1786curwin -> w_cursor .lnum + 1 ,1 , true); 1787did_append = false; 1788 1789// Move marks after the line break to the new line. 1790if (flags & OPENLINE_MARKFIX ) { 1791mark_col_adjust (curwin -> w_cursor .lnum , 1792curwin -> w_cursor .col + less_cols_off , 17931 ,- less_cols ,0 ); 1794 } 1795 }else { 1796changed_bytes (curwin -> w_cursor .lnum ,curwin -> w_cursor .col ); 1797 } 1798 } 1799 1800// Put the cursor on the new line. Careful: the scrollup() above may 1801// have moved w_cursor, we must use old_cursor. 1802curwin -> w_cursor .lnum = old_cursor .lnum + 1 ; 1803 } 1804if (did_append ) { 1805// bail out and just get the final length of the line we just manipulated 1806bcount_t extra = ml_get_len (curwin -> w_cursor .lnum ); 1807extmark_splice (curbuf , (int )curwin -> w_cursor .lnum - 1 ,0 , 18080 ,0 ,0 ,1 ,0 ,1 + extra ,kExtmarkUndo ); 1809changed_lines (curbuf ,curwin -> w_cursor .lnum ,0 ,curwin -> w_cursor .lnum ,1 , true); 1810 } 1811curbuf_splice_pending -- ; 1812 1813curwin -> w_cursor .col = newcol ; 1814curwin -> w_cursor .coladd = 0 ; 1815 1816// In MODE_VREPLACE state, we are handling the replace stack ourselves, so 1817// stop fixthisline() from doing it (via change_indent()) by telling it 1818// we're in normal MODE_INSERT state. 1819if (State & VREPLACE_FLAG ) { 1820vreplace_mode = State ;// So we know to put things right later 1821State = MODE_INSERT ; 1822 }else { 1823vreplace_mode = 0 ; 1824 } 1825 1826if (!p_paste ) { 1827if (leader == NULL 1828&& !use_indentexpr_for_lisp () 1829&& curbuf -> b_p_lisp 1830&& curbuf -> b_p_ai ) { 1831// do lisp indenting 1832fixthisline (get_lisp_indent ); 1833Ins .ai_col = (colnr_T )getwhitecols_curline (); 1834 }else if (do_cindent || (curbuf -> b_p_ai && use_indentexpr_for_lisp ())) { 1835// do 'cindent' or 'indentexpr' indenting 1836do_c_expr_indent (); 1837Ins .ai_col = (colnr_T )getwhitecols_curline (); 1838 } 1839 } 1840 1841if (vreplace_mode != 0 ) { 1842State = vreplace_mode ; 1843 } 1844 1845// Finally, MODE_VREPLACE gets the stuff on the new line, then puts back 1846// the original line, and inserts the new stuff char by char, pushing old 1847// stuff onto the replace stack (via ins_char()). 1848if (State & VREPLACE_FLAG ) { 1849// Put new line in p_extra 1850p_extra = xstrnsave (get_cursor_line_ptr (), (size_t )get_cursor_line_len ()); 1851 1852// Put back original line 1853ml_replace (curwin -> w_cursor .lnum ,next_line , false); 1854 1855// Insert new stuff into line again 1856curwin -> w_cursor .col = 0 ; 1857curwin -> w_cursor .coladd = 0 ; 1858ins_bytes (p_extra );// will call changed_bytes() 1859xfree (p_extra ); 1860next_line = NULL ; 1861 } 1862 1863retval = true;// success! 1864theend : 1865curbuf -> b_p_pi = saved_pi ; 1866xfree (saved_line ); 1867xfree (next_line ); 1868xfree (allocated ); 1869xfree (prompt_moved ); 1870cmdmod .cmod_flags = old_cmod_flags ; 1871return retval ; 1872} 1873 1874/// Delete from cursor to end of line. 1875/// Caller must have prepared for undo. 1876/// If "fixpos" is true fix the cursor position when done. 1877void truncate_line (int fixpos ) 1878{ 1879linenr_T lnum = curwin -> w_cursor .lnum ; 1880colnr_T col = curwin -> w_cursor .col ; 1881char * old_line = ml_get (lnum ); 1882char * newp = col == 0 ?xstrdup ("" ) :xstrnsave (old_line , (size_t )col ); 1883int deleted = ml_get_len (lnum )- col ; 1884 1885ml_replace (lnum ,newp , false); 1886 1887// mark the buffer as changed and prepare for displaying 1888inserted_bytes (lnum ,curwin -> w_cursor .col ,deleted ,0 ); 1889 1890// If "fixpos" is true we don't want to end up positioned at the NUL. 1891if (fixpos && curwin -> w_cursor .col > 0 ) { 1892curwin -> w_cursor .col -- ; 1893 } 1894} 1895 1896/// Delete "nlines" lines at the cursor. 1897/// Saves the lines for undo first if "undo" is true. 1898void del_lines (linenr_T nlines ,bool undo ) 1899{ 1900int n ; 1901linenr_T first = curwin -> w_cursor .lnum ; 1902 1903if (nlines <=0 ) { 1904return ; 1905 } 1906 1907// save the deleted lines for undo 1908if (undo && u_savedel (first ,nlines )== FAIL ) { 1909return ; 1910 } 1911 1912for (n = 0 ;n < nlines ;) { 1913if (curbuf -> b_ml .ml_flags & ML_EMPTY ) {// nothing to delete 1914break ; 1915 } 1916 1917ml_delete_flags (first ,ML_DEL_MESSAGE ); 1918n ++ ; 1919 1920// If we delete the last line in the file, stop 1921if (first > curbuf -> b_ml .ml_line_count ) { 1922break ; 1923 } 1924 } 1925 1926// Correct the cursor position before calling deleted_lines_mark(), it may 1927// trigger a callback to display the cursor. 1928curwin -> w_cursor .col = 0 ; 1929check_cursor_lnum (curwin ); 1930 1931// adjust marks, mark the buffer as changed and prepare for displaying 1932deleted_lines_mark (first ,n ); 1933} 1934 1935/// Returns the length in bytes of the prefix of the given string which introduces a comment. 1936/// 1937/// If this string is not a comment then 0 is returned. 1938/// When "flags" is not NULL, it is set to point to the flags of the recognized comment leader. 1939/// "backward" must be true for the "O" command. 1940/// If "include_space" is set, include trailing whitespace while calculating the length. 1941int get_leader_len (char * line ,char ** flags ,bool backward ,bool include_space ) 1942{ 1943int j ; 1944bool got_com = false; 1945char part_buf [COM_MAX_LEN ];// buffer for one option part 1946char * string ;// pointer to comment string 1947int middle_match_len = 0 ; 1948char * saved_flags = NULL ; 1949 1950int result = 0 ; 1951int i = 0 ; 1952while (ascii_iswhite (line [i ])) {// leading white space is ignored 1953i ++ ; 1954 } 1955 1956// Repeat to match several nested comment strings. 1957while (line [i ]!= NUL ) { 1958// scan through the 'comments' option for a match 1959bool found_one = false; 1960for (char * list = curbuf -> b_p_com ;* list ;) { 1961// Get one option part into part_buf[]. Advance "list" to next 1962// one. Put "string" at start of string. 1963if (!got_com && flags != NULL ) { 1964* flags = list ;// remember where flags started 1965 } 1966char * prev_list = list ; 1967copy_option_part (& list ,part_buf ,COM_MAX_LEN ,"," ); 1968string = vim_strchr (part_buf ,':' ); 1969if (string == NULL ) {// missing ':', ignore this part 1970continue ; 1971 } 1972* string ++ = NUL ;// isolate flags from string 1973 1974// If we found a middle match previously, use that match when this 1975// is not a middle or end. 1976if (middle_match_len != 0 1977&& vim_strchr (part_buf ,COM_MIDDLE )== NULL 1978&& vim_strchr (part_buf ,COM_END )== NULL ) { 1979break ; 1980 } 1981 1982// When we already found a nested comment, only accept further 1983// nested comments. 1984if (got_com && vim_strchr (part_buf ,COM_NEST )== NULL ) { 1985continue ; 1986 } 1987 1988// When 'O' flag present and using "O" command skip this one. 1989if (backward && vim_strchr (part_buf ,COM_NOBACK )!= NULL ) { 1990continue ; 1991 } 1992 1993// Line contents and string must match. 1994// When string starts with white space, must have some white space 1995// (but the amount does not need to match, there might be a mix of 1996// TABs and spaces). 1997if (ascii_iswhite (string [0 ])) { 1998if (i == 0 || !ascii_iswhite (line [i - 1 ])) { 1999continue ;// missing white space 2000 } 2001while (ascii_iswhite (string [0 ])) { 2002string ++ ; 2003 } 2004 } 2005for (j = 0 ;string [j ]!= NUL && string [j ]== line [i + j ];j ++ ) {} 2006if (string [j ]!= NUL ) { 2007continue ;// string doesn't match 2008 } 2009// When 'b' flag used, there must be white space or an 2010// end-of-line after the string in the line. 2011if (vim_strchr (part_buf ,COM_BLANK )!= NULL 2012&& !ascii_iswhite (line [i + j ])&& line [i + j ]!= NUL ) { 2013continue ; 2014 } 2015 2016// We have found a match, stop searching unless this is a middle 2017// comment. The middle comment can be a substring of the end 2018// comment in which case it's better to return the length of the 2019// end comment and its flags. Thus we keep searching with middle 2020// and end matches and use an end match if it matches better. 2021if (vim_strchr (part_buf ,COM_MIDDLE )!= NULL ) { 2022if (middle_match_len == 0 ) { 2023middle_match_len = j ; 2024saved_flags = prev_list ; 2025 } 2026continue ; 2027 } 2028if (middle_match_len != 0 && j > middle_match_len ) { 2029// Use this match instead of the middle match, since it's a 2030// longer thus better match. 2031middle_match_len = 0 ; 2032 } 2033 2034if (middle_match_len == 0 ) { 2035i += j ; 2036 } 2037found_one = true; 2038break ; 2039 } 2040 2041if (middle_match_len != 0 ) { 2042// Use the previously found middle match after failing to find a 2043// match with an end. 2044if (!got_com && flags != NULL ) { 2045* flags = saved_flags ; 2046 } 2047i += middle_match_len ; 2048found_one = true; 2049 } 2050 2051// No match found, stop scanning. 2052if (!found_one ) { 2053break ; 2054 } 2055 2056result = i ; 2057 2058// Include any trailing white space. 2059while (ascii_iswhite (line [i ])) { 2060i ++ ; 2061 } 2062 2063if (include_space ) { 2064result = i ; 2065 } 2066 2067// If this comment doesn't nest, stop here. 2068got_com = true; 2069if (vim_strchr (part_buf ,COM_NEST )== NULL ) { 2070break ; 2071 } 2072 } 2073return result ; 2074} 2075 2076/// Return the offset at which the last comment in line starts. If there is no 2077/// comment in the whole line, -1 is returned. 2078/// 2079/// When "flags" is not null, it is set to point to the flags describing the 2080/// recognized comment leader. 2081int get_last_leader_offset (char * line ,char ** flags ) 2082{ 2083int result = -1 ; 2084int j ; 2085int lower_check_bound = 0 ; 2086char * com_leader ; 2087char * com_flags ; 2088char part_buf [COM_MAX_LEN ];// buffer for one option part 2089 2090// Repeat to match several nested comment strings. 2091int i = (int )strlen (line ); 2092while (-- i >=lower_check_bound ) { 2093// scan through the 'comments' option for a match 2094bool found_one = false; 2095for (char * list = curbuf -> b_p_com ;* list ;) { 2096char * flags_save = list ; 2097 2098// Get one option part into part_buf[]. Advance list to next one. 2099// put string at start of string. 2100copy_option_part (& list ,part_buf ,COM_MAX_LEN ,"," ); 2101char * string = vim_strchr (part_buf ,':' ); 2102if (string == NULL ) {// If everything is fine, this cannot actually 2103// happen. 2104continue ; 2105 } 2106* string ++ = NUL ;// Isolate flags from string. 2107com_leader = string ; 2108 2109// Line contents and string must match. 2110// When string starts with white space, must have some white space 2111// (but the amount does not need to match, there might be a mix of 2112// TABs and spaces). 2113if (ascii_iswhite (string [0 ])) { 2114if (i == 0 || !ascii_iswhite (line [i - 1 ])) { 2115continue ; 2116 } 2117while (ascii_iswhite (* string )) { 2118string ++ ; 2119 } 2120 } 2121for (j = 0 ;string [j ]!= NUL && string [j ]== line [i + j ];j ++ ) { 2122// do nothing 2123 } 2124if (string [j ]!= NUL ) { 2125continue ; 2126 } 2127 2128// When 'b' flag used, there must be white space or an 2129// end-of-line after the string in the line. 2130if (vim_strchr (part_buf ,COM_BLANK )!= NULL 2131&& !ascii_iswhite (line [i + j ])&& line [i + j ]!= NUL ) { 2132continue ; 2133 } 2134 2135if (vim_strchr (part_buf ,COM_MIDDLE )!= NULL ) { 2136// For a middlepart comment, only consider it to match if 2137// everything before the current position in the line is 2138// whitespace. Otherwise we would think we are inside a 2139// comment if the middle part appears somewhere in the middle 2140// of the line. E.g. for C the "*" appears often. 2141for (j = 0 ;j <=i && ascii_iswhite (line [j ]);j ++ ) {} 2142if (j < i ) { 2143continue ; 2144 } 2145 } 2146 2147// We have found a match, stop searching. 2148found_one = true; 2149 2150if (flags ) { 2151* flags = flags_save ; 2152 } 2153com_flags = flags_save ; 2154 2155break ; 2156 } 2157 2158if (found_one ) { 2159char part_buf2 [COM_MAX_LEN ];// buffer for one option part 2160 2161result = i ; 2162// If this comment nests, continue searching. 2163if (vim_strchr (part_buf ,COM_NEST )!= NULL ) { 2164continue ; 2165 } 2166 2167lower_check_bound = i ; 2168 2169// Let's verify whether the comment leader found is a substring 2170// of other comment leaders. If it is, let's adjust the 2171// lower_check_bound so that we make sure that we have determined 2172// the comment leader correctly. 2173 2174while (ascii_iswhite (* com_leader )) { 2175com_leader ++ ; 2176 } 2177int len1 = (int )strlen (com_leader ); 2178 2179for (char * list = curbuf -> b_p_com ;* list ;) { 2180char * flags_save = list ; 2181 2182copy_option_part (& list ,part_buf2 ,COM_MAX_LEN ,"," ); 2183if (flags_save == com_flags ) { 2184continue ; 2185 } 2186char * string = vim_strchr (part_buf2 ,':' ); 2187string ++ ; 2188while (ascii_iswhite (* string )) { 2189string ++ ; 2190 } 2191int len2 = (int )strlen (string ); 2192if (len2 == 0 ) { 2193continue ; 2194 } 2195 2196// Now we have to verify whether string ends with a substring 2197// beginning the com_leader. 2198for (int off = (len2 > i ?i :len2 );off > 0 && off + len1 > len2 ;) { 2199off -- ; 2200if (strncmp (string + off ,com_leader , (size_t )(len2 - off ))== 0 ) { 2201lower_check_bound = MIN (lower_check_bound ,i - off ); 2202 } 2203 } 2204 } 2205 } 2206 } 2207return result ; 2208}