robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// mark.c: functions for setting marks and jumping to them 2 3#include <assert.h> 4#include <limits.h> 5#include <stdbool.h> 6#include <stdint.h> 7#include <stdio.h> 8#include <string.h> 9 10#include "nvim/api/private/helpers.h" 11#include "nvim/ascii_defs.h" 12#include "nvim/autocmd.h" 13#include "nvim/buffer.h" 14#include "nvim/buffer_defs.h" 15#include "nvim/charset.h" 16#include "nvim/cursor.h" 17#include "nvim/diff.h" 18#include "nvim/errors.h" 19#include "nvim/eval/typval.h" 20#include "nvim/eval/typval_defs.h" 21#include "nvim/ex_cmds_defs.h" 22#include "nvim/ex_docmd.h" 23#include "nvim/extmark.h" 24#include "nvim/extmark_defs.h" 25#include "nvim/fold.h" 26#include "nvim/gettext_defs.h" 27#include "nvim/globals.h" 28#include "nvim/highlight_defs.h" 29#include "nvim/insert.h" 30#include "nvim/lua/executor.h" 31#include "nvim/mark.h" 32#include "nvim/mbyte.h" 33#include "nvim/mcursor.h" 34#include "nvim/memline.h" 35#include "nvim/memory.h" 36#include "nvim/message.h" 37#include "nvim/move.h" 38#include "nvim/normal_defs.h" 39#include "nvim/option_vars.h" 40#include "nvim/os/fs.h" 41#include "nvim/os/input.h" 42#include "nvim/os/os.h" 43#include "nvim/os/os_defs.h" 44#include "nvim/os/time.h" 45#include "nvim/os/time_defs.h" 46#include "nvim/path.h" 47#include "nvim/plines.h" 48#include "nvim/pos_defs.h" 49#include "nvim/quickfix.h" 50#include "nvim/strings.h" 51#include "nvim/tag.h" 52#include "nvim/textobject.h" 53#include "nvim/types_defs.h" 54#include "nvim/undo.h" 55#include "nvim/vim_defs.h" 56 57// This file contains routines to maintain and manipulate marks. 58 59// If a named file mark's lnum is non-zero, it is valid. 60// If a named file mark's fnum is non-zero, it is for an existing buffer, 61// otherwise it is from .shada and namedfm[n].fname is the file name. 62// There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing 63// shada). 64 65#include "mark.c.generated.h" 66 67// Set named mark "c" at current cursor position. 68// Returns OK on success, FAIL if bad name given. 69int setmark (int c ) 70{ 71fmarkv_T view = mark_view_make (curwin ,curwin -> w_cursor ); 72return setmark_pos (c ,& curwin -> w_cursor ,curbuf -> b_fnum ,& view ); 73} 74 75/// Free fmark_T item 76void free_fmark (fmark_T fm ) 77{ 78xfree (fm .additional_data ); 79} 80 81/// Free xfmark_T item 82void free_xfmark (xfmark_T fm ) 83{ 84xfree (fm .fname ); 85free_fmark (fm .fmark ); 86} 87 88/// Free and clear fmark_T item. 89/// 90/// Does not trigger "MarkSet" event. 91void clear_fmark (fmark_T * const fm ,const Timestamp timestamp ) 92FUNC_ATTR_NONNULL_ALL 93{ 94free_fmark (* fm ); 95* fm = (fmark_T )INIT_FMARK ; 96fm -> timestamp = timestamp ; 97} 98 99/// Schedules "MarkSet" event. 100/// 101/// @param c The name of the mark, e.g., 'a'. 102/// @param pos Position of the mark in the buffer. 103/// @param buf The buffer of the mark. 104static void do_markset_autocmd (char c ,pos_T * pos ,buf_T * buf ) 105{ 106if (!has_event (EVENT_MARKSET )) { 107return ; 108 } 109 110MAXSIZE_TEMP_DICT (data ,3 ); 111char mark_str [2 ]= {c ,'\0' }; 112PUT_C (data ,"name" ,STRING_OBJ (((String ){ .data = mark_str , .size = 1 }))); 113PUT_C (data ,"line" ,INTEGER_OBJ (pos -> lnum )); 114PUT_C (data ,"col" ,INTEGER_OBJ (pos -> col )); 115aucmd_defer (EVENT_MARKSET ,mark_str ,NULL ,AUGROUP_ALL ,buf ,NULL ,& DICT_OBJ (data )); 116} 117 118// Set named mark "c" to position "pos". 119// When "c" is upper case use file "fnum". 120// Returns OK on success, FAIL if bad name given. 121int setmark_pos (int c ,pos_T * pos ,int fnum ,fmarkv_T * view_pt ) 122{ 123int i ; 124fmarkv_T view = view_pt != NULL ?* view_pt : (fmarkv_T )INIT_FMARKV ; 125 126// Check for a special key (may cause islower() to crash). 127if (c < 0 ) { 128return FAIL ; 129 } 130 131if (c == '\'' || c == '`' ) { 132if (pos == & curwin -> w_cursor ) { 133setpcmark (); 134// keep it even when the cursor doesn't move 135curwin -> w_prev_pcmark = curwin -> w_pcmark ; 136 }else { 137curwin -> w_pcmark = * pos ; 138 } 139return OK ; 140 } 141 142// Can't set a mark in a non-existent buffer. 143buf_T * buf = buflist_findnr (fnum ); 144if (buf == NULL ) { 145return FAIL ; 146 } 147 148if (c == '"' ) { 149RESET_FMARK (& buf -> b_last_cursor ,* pos ,buf -> b_fnum ,view ); 150do_markset_autocmd ((char )c ,pos ,buf ); 151return OK ; 152 } 153 154// Allow setting '[ and '] for an autocommand that simulates reading a 155// file. 156if (c == '[' ) { 157buf -> b_op_start = * pos ; 158do_markset_autocmd ((char )c ,pos ,buf ); 159return OK ; 160 } 161if (c == ']' ) { 162buf -> b_op_end = * pos ; 163do_markset_autocmd ((char )c ,pos ,buf ); 164return OK ; 165 } 166 167if (c == '<' || c == '>' ) { 168if (c == '<' ) { 169buf -> b_visual .vi_start = * pos ; 170 }else { 171buf -> b_visual .vi_end = * pos ; 172 } 173if (buf -> b_visual .vi_mode == NUL ) { 174// Visual_mode has not yet been set, use a sane default. 175buf -> b_visual .vi_mode = 'v' ; 176 } 177do_markset_autocmd ((char )c ,pos ,buf ); 178return OK ; 179 } 180 181if (c == ':' && bt_prompt (buf )) { 182RESET_FMARK (& buf -> b_prompt_start ,* pos ,buf -> b_fnum ,view ); 183return OK ; 184 } 185 186if (ASCII_ISLOWER (c )) { 187i = c - 'a' ; 188RESET_FMARK (buf -> b_namedm + i ,* pos ,fnum ,view ); 189// Moving a mark is not part of the pending change, so undo must not revert it. #5754 190u_update_named_mark (buf ,i ); 191do_markset_autocmd ((char )c ,pos ,buf ); 192return OK ; 193 } 194if (ASCII_ISUPPER (c )|| ascii_isdigit (c )) { 195if (ascii_isdigit (c )) { 196i = c - '0' + NMARKS ; 197 }else { 198i = c - 'A' ; 199 } 200RESET_XFMARK (namedfm + i ,* pos ,fnum ,view ,NULL ); 201do_markset_autocmd ((char )c ,pos ,buf ); 202return OK ; 203 } 204return FAIL ; 205} 206 207/// Remove every jump list entry referring to a given buffer. 208/// This function will also adjust the current jump list index. 209void mark_jumplist_forget_file (win_T * wp ,int fnum ) 210{ 211// Remove all jump list entries that match the deleted buffer. 212for (int i = wp -> w_jumplistlen - 1 ;i >=0 ;i -- ) { 213if (wp -> w_jumplist [i ].fmark .fnum == fnum ) { 214// Found an entry that we want to delete. 215free_xfmark (wp -> w_jumplist [i ]); 216 217// If the current jump list index is behind the entry we want to delete, 218// move it back by one. 219if (wp -> w_jumplistidx > i ) { 220wp -> w_jumplistidx -- ; 221 } 222 223// Actually remove the entry from the jump list. 224wp -> w_jumplistlen -- ; 225memmove (& wp -> w_jumplist [i ],& wp -> w_jumplist [i + 1 ], 226 (size_t )(wp -> w_jumplistlen - i )* sizeof (wp -> w_jumplist [i ])); 227 } 228 } 229} 230 231/// Delete every entry referring to file "fnum" from both the jumplist and the 232/// tag stack. 233void mark_forget_file (win_T * wp ,int fnum ) 234{ 235mark_jumplist_forget_file (wp ,fnum ); 236 237// Remove all tag stack entries that match the deleted buffer. 238for (int i = wp -> w_tagstacklen - 1 ;i >=0 ;i -- ) { 239if (wp -> w_tagstack [i ].fmark .fnum == fnum ) { 240// Found an entry that we want to delete. 241tagstack_clear_entry (& wp -> w_tagstack [i ]); 242 243// If the current tag stack index is behind the entry we want to delete, 244// move it back by one. 245if (wp -> w_tagstackidx > i ) { 246wp -> w_tagstackidx -- ; 247 } 248 249// Actually remove the entry from the tag stack. 250wp -> w_tagstacklen -- ; 251memmove (& wp -> w_tagstack [i ],& wp -> w_tagstack [i + 1 ], 252 (size_t )(wp -> w_tagstacklen - i )* sizeof (wp -> w_tagstack [i ])); 253 } 254 } 255} 256 257// Set the previous context mark to the current position and add it to the 258// jump list. 259void setpcmark (void ) 260{ 261xfmark_T * fm ; 262 263// For :global the mark is set only once. Not for multicursor replay (primary owns the jumplist). 264if (global_busy || listcmd_busy || (cmdmod .cmod_flags & CMOD_KEEPJUMPS )|| mc_replaying ()) { 265return ; 266 } 267 268curwin -> w_prev_pcmark = curwin -> w_pcmark ; 269curwin -> w_pcmark = curwin -> w_cursor ; 270 271if (curwin -> w_pcmark .lnum == 0 ) { 272curwin -> w_pcmark .lnum = 1 ; 273 } 274 275if (jop_flags & kOptJopFlagStack ) { 276// jumpoptions=stack: if we're somewhere in the middle of the jumplist 277// discard everything after the current index. 278if (curwin -> w_jumplistidx < curwin -> w_jumplistlen - 1 ) { 279// Discard the rest of the jumplist by cutting the length down to 280// contain nothing beyond the current index. 281curwin -> w_jumplistlen = curwin -> w_jumplistidx + 1 ; 282 } 283 } 284 285// If jumplist is full: remove oldest entry 286if (++ curwin -> w_jumplistlen > JUMPLISTSIZE ) { 287curwin -> w_jumplistlen = JUMPLISTSIZE ; 288free_xfmark (curwin -> w_jumplist [0 ]); 289memmove (& curwin -> w_jumplist [0 ],& curwin -> w_jumplist [1 ], 290 (JUMPLISTSIZE - 1 )* sizeof (curwin -> w_jumplist [0 ])); 291 } 292curwin -> w_jumplistidx = curwin -> w_jumplistlen ; 293fm = & curwin -> w_jumplist [curwin -> w_jumplistlen - 1 ]; 294 295fmarkv_T view = mark_view_make (curwin ,curwin -> w_pcmark ); 296SET_XFMARK (fm ,curwin -> w_pcmark ,curbuf -> b_fnum ,view ,NULL ); 297} 298 299// To change context, call setpcmark(), then move the current position to 300// where ever, then call checkpcmark(). This ensures that the previous 301// context will only be changed if the cursor moved to a different line. 302// If pcmark was deleted (with "dG") the previous mark is restored. 303void checkpcmark (void ) 304{ 305if (curwin -> w_prev_pcmark .lnum != 0 306&& (equalpos (curwin -> w_pcmark ,curwin -> w_cursor ) 307|| curwin -> w_pcmark .lnum == 0 )) { 308curwin -> w_pcmark = curwin -> w_prev_pcmark ; 309 } 310curwin -> w_prev_pcmark .lnum = 0 ;// it has been checked 311} 312 313/// Get mark in "count" position in the |jumplist| relative to the current index. 314/// 315/// If the mark is in a different buffer, it will be skipped unless the buffer exists. 316/// 317/// @note cleanup_jumplist() is run, which removes duplicate marks, and 318/// changes win->w_jumplistidx. 319/// @param[in] win window to get jumplist from. 320/// @param[in] count count to move may be negative. 321/// 322/// @return mark, NULL if out of jumplist bounds. 323fmark_T * get_jumplist (win_T * win ,int count ) 324{ 325xfmark_T * jmp = NULL ; 326 327cleanup_jumplist (win , true); 328 329if (win -> w_jumplistlen == 0 ) {// nothing to jump to 330return NULL ; 331 } 332 333while (true) { 334if (win -> w_jumplistidx + count < 0 335|| win -> w_jumplistidx + count >=win -> w_jumplistlen ) { 336return NULL ; 337 } 338 339// if first CTRL-O or CTRL-I command after a jump, add cursor position 340// to list. Careful: If there are duplicates (CTRL-O immediately after 341// starting Vim on a file), another entry may have been removed. 342if (win -> w_jumplistidx == win -> w_jumplistlen ) { 343setpcmark (); 344win -> w_jumplistidx -- ;// skip the new entry 345if (win -> w_jumplistidx + count < 0 ) { 346return NULL ; 347 } 348 } 349 350win -> w_jumplistidx += count ; 351 352jmp = win -> w_jumplist + win -> w_jumplistidx ; 353if (jmp -> fmark .fnum == 0 ) { 354// Resolve the fnum (buff number) in the mark before returning it (shada) 355fname2fnum (jmp ); 356 } 357if (jmp -> fmark .fnum != curbuf -> b_fnum ) { 358// Needs to switch buffer, if it can't find it skip the mark 359if (buflist_findnr (jmp -> fmark .fnum )== NULL ) { 360count += count < 0 ?-1 :1 ; 361continue ; 362 } 363 } 364break ; 365 } 366return & jmp -> fmark ; 367} 368 369/// Get mark in "count" position in the |changelist| relative to the current index. 370/// 371/// @note Changes the win->w_changelistidx. 372/// @param[in] win window to get jumplist from. 373/// @param[in] count count to move may be negative. 374/// 375/// @return mark, NULL if out of bounds. 376fmark_T * get_changelist (buf_T * buf ,win_T * win ,int count ) 377{ 378int n ; 379fmark_T * fm ; 380 381if (buf -> b_changelistlen == 0 ) {// nothing to jump to 382return NULL ; 383 } 384 385n = win -> w_changelistidx ; 386if (n + count < 0 ) { 387if (n == 0 ) { 388return NULL ; 389 } 390n = 0 ; 391 }else if (n + count >=buf -> b_changelistlen ) { 392if (n == buf -> b_changelistlen - 1 ) { 393return NULL ; 394 } 395n = buf -> b_changelistlen - 1 ; 396 }else { 397n += count ; 398 } 399win -> w_changelistidx = n ; 400fm = & (buf -> b_changelist [n ]); 401// Changelist marks are always buffer local, Shada does not set it when loading 402fm -> fnum = curbuf -> handle ; 403return & (buf -> b_changelist [n ]); 404} 405 406/// Get a named mark. 407/// 408/// All types of marks, even those that are not technically a mark will be returned as such. Use 409/// mark_move_to() to move to the mark. 410/// @note Some of the pointers are statically allocated, if in doubt make a copy. For more 411/// information read mark_get_local(). 412/// @param buf Buffer to get the mark from. 413/// @param win Window to get or calculate the mark from (motion type marks, context mark). 414/// @param fmp[out] Optional pointer to store the result in, as a workaround for the note above. 415/// @param flag MarkGet value 416/// @param name Name of the mark. 417/// 418/// @return Mark if found, otherwise NULL. For @c kMarkBufLocal, NULL is returned 419/// when no mark is found in @a buf. 420fmark_T * mark_get (buf_T * buf ,win_T * win ,fmark_T * fmp ,MarkGet flag ,int name ) 421{ 422fmark_T * fm = NULL ; 423if (ASCII_ISUPPER (name )|| ascii_isdigit (name )) { 424// Global marks 425xfmark_T * xfm = mark_get_global (flag != kMarkAllNoResolve ,name ); 426fm = & xfm -> fmark ; 427if (flag == kMarkBufLocal && xfm -> fmark .fnum != buf -> handle ) { 428// Only wanted marks belonging to the buffer 429return pos_to_mark (buf ,NULL , (pos_T ){ .lnum = 0 }); 430 } 431 }else if (name > 0 && name < NMARK_LOCAL_MAX ) { 432// Local Marks 433fm = mark_get_local (buf ,win ,name ); 434 } 435if (fmp != NULL && fm != NULL ) { 436* fmp = * fm ; 437return fmp ; 438 } 439return fm ; 440} 441 442/// Get a global mark {A-Z0-9}. 443/// 444/// @param name the name of the mark. 445/// @param resolve Whether to try resolving the mark fnum (i.e., load the buffer stored in 446/// the mark fname and update the xfmark_T (expensive)). 447/// 448/// @return Mark 449xfmark_T * mark_get_global (bool resolve ,int name ) 450{ 451xfmark_T * mark ; 452 453if (ascii_isdigit (name )) { 454name = name - '0' + NMARKS ; 455 }else if (ASCII_ISUPPER (name )) { 456name -= 'A' ; 457 }else { 458// Not a valid mark name 459assert (false); 460 } 461mark = & namedfm [name ]; 462 463if (resolve && mark -> fmark .fnum == 0 ) { 464// Resolve filename to fnum (SHADA marks) 465fname2fnum (mark ); 466 } 467return mark ; 468} 469 470/// Get a local mark (lowercase and symbols). 471/// 472/// Some marks are not actually marks, but positions that are never adjusted or motions presented as 473/// marks. Search first for marks and fallback to finding motion type marks. If it's known 474/// ahead of time that the mark is actually a motion use the mark_get_motion() directly. 475/// 476/// @note Lowercase, last_cursor '"', last insert '^', last change '.' are not statically 477/// allocated, everything else is. 478/// @param name the name of the mark. 479/// @param win window to retrieve marks that belong to it (motions and context mark). 480/// @param buf buf to retrieve marks that belong to it. 481/// 482/// @return Mark, NULL if not found. 483fmark_T * mark_get_local (buf_T * buf ,win_T * win ,int name ) 484{ 485fmark_T * mark = NULL ; 486if (ASCII_ISLOWER (name )) { 487// normal named mark 488mark = & buf -> b_namedm [name - 'a' ]; 489// to start of previous operator 490 }else if (name == '[' ) { 491mark = pos_to_mark (buf ,NULL ,buf -> b_op_start ); 492// to end of previous operator 493 }else if (name == ']' ) { 494mark = pos_to_mark (buf ,NULL ,buf -> b_op_end ); 495// visual marks 496 }else if (name == '<' || name == '>' ) { 497mark = mark_get_visual (buf ,name ); 498// previous context mark 499 }else if (name == '\'' || name == '`' ) { 500// TODO(muniter): w_pcmark should be stored as a mark, but causes a nasty bug. 501mark = pos_to_mark (curbuf ,NULL ,win -> w_pcmark ); 502// to position when leaving buffer 503 }else if (name == '"' ) { 504mark = & (buf -> b_last_cursor ); 505// to where last Insert mode stopped 506 }else if (name == '^' ) { 507mark = & (buf -> b_last_insert ); 508// to where last change was made 509 }else if (name == '.' ) { 510mark = & buf -> b_last_change ; 511// prompt start location 512 }else if (name == ':' && bt_prompt (buf )) { 513mark = & (buf -> b_prompt_start ); 514// Mark that are actually not marks but motions, e.g {, }, (, ), ... 515 }else { 516mark = mark_get_motion (buf ,win ,name ); 517 } 518 519if (mark ) { 520mark -> fnum = buf -> b_fnum ; 521 } 522 523return mark ; 524} 525 526/// Get marks that are actually motions but return them as marks 527/// 528/// Gets the following motions as marks: '{', '}', '(', ')' 529/// @param name name of the mark 530/// @param win window to retrieve the cursor to calculate the mark. 531/// @param buf buf to wrap motion marks with it's buffer number (fm->fnum). 532/// 533/// @return[static] Mark. 534fmark_T * mark_get_motion (buf_T * buf ,win_T * win ,int name ) 535{ 536fmark_T * mark = NULL ; 537const pos_T pos = curwin -> w_cursor ; 538const bool slcb = listcmd_busy ; 539listcmd_busy = true;// avoid that '' is changed 540if (name == '{' || name == '}' ) {// to previous/next paragraph 541oparg_T oa ; 542if (findpar (& oa .inclusive ,name == '}' ?FORWARD :BACKWARD ,1 ,NUL , false)) { 543mark = pos_to_mark (buf ,NULL ,win -> w_cursor ); 544 } 545 }else if (name == '(' || name == ')' ) {// to previous/next sentence 546if (findsent (name == ')' ?FORWARD :BACKWARD ,1 )) { 547mark = pos_to_mark (buf ,NULL ,win -> w_cursor ); 548 } 549 } 550curwin -> w_cursor = pos ; 551listcmd_busy = slcb ; 552return mark ; 553} 554 555/// Get visual marks '<', '>' 556/// 557/// This marks are different to normal marks: 558/// 1. Never adjusted. 559/// 2. Different behavior depending on editor state (visual mode). 560/// 3. Not saved in shada. 561/// 4. Re-ordered when defined in reverse. 562/// @param buf Buffer to get the mark from. 563/// @param name Mark name '<' or '>'. 564/// 565/// @return[static] Mark 566fmark_T * mark_get_visual (buf_T * buf ,int name ) 567{ 568fmark_T * mark = NULL ; 569if (name == '<' || name == '>' ) { 570// start/end of visual area 571pos_T startp = buf -> b_visual .vi_start ; 572pos_T endp = buf -> b_visual .vi_end ; 573if (((name == '<' )== lt (startp ,endp )|| endp .lnum == 0 ) 574&& startp .lnum != 0 ) { 575mark = pos_to_mark (buf ,NULL ,startp ); 576 }else { 577mark = pos_to_mark (buf ,NULL ,endp ); 578 } 579 580if (buf -> b_visual .vi_mode == 'V' ) { 581if (name == '<' ) { 582mark -> mark .col = 0 ; 583 }else { 584mark -> mark .col = MAXCOL ; 585 } 586mark -> mark .coladd = 0 ; 587 } 588 } 589return mark ; 590} 591 592/// Wrap a pos_T into an fmark_T, used to abstract marks handling. 593/// 594/// Pass an fmp if multiple c 595/// @note view fields are set to 0. 596/// @param buf for fmark->fnum. 597/// @param pos for fmark->mark. 598/// @param fmp pointer to save the mark. 599/// 600/// @return[static] Mark with the given information. 601fmark_T * pos_to_mark (buf_T * buf ,fmark_T * fmp ,pos_T pos ) 602FUNC_ATTR_NONNULL_RET 603{ 604static fmark_T fms = INIT_FMARK ; 605fmark_T * fm = fmp == NULL ?& fms :fmp ; 606fm -> fnum = buf -> handle ; 607fm -> mark = pos ; 608return fm ; 609} 610 611/// Attempt to switch to the buffer of the given global mark 612/// 613/// @param fm 614/// @param pcmark_on_switch leave a context mark when switching buffer. 615/// @return whether the buffer was switched or not. 616static MarkMoveRes switch_to_mark_buf (fmark_T * fm ,bool pcmark_on_switch ) 617{ 618if (fm -> fnum != curbuf -> b_fnum ) { 619// Switch to another file. 620int getfile_flag = pcmark_on_switch ?GETF_SETMARK :0 ; 621bool res = buflist_getfile (fm -> fnum ,fm -> mark .lnum ,getfile_flag , false)== OK ; 622return res == true ?kMarkSwitchedBuf :kMarkMoveFailed ; 623 } 624return 0 ; 625} 626 627/// Move to the given file mark, changing the buffer and cursor position. 628/// 629/// Validate the mark, switch to the buffer, and move the cursor. 630/// @param fm Mark, can be NULL will raise E78: Unknown mark 631/// @param flags MarkMove flags to configure the movement to the mark. 632/// 633/// @return MarkMovekRes flags representing the outcome 634MarkMoveRes mark_move_to (fmark_T * fm ,MarkMove flags ) 635{ 636static fmark_T fm_copy = INIT_FMARK ; 637MarkMoveRes res = kMarkMoveSuccess ; 638const char * errormsg = NULL ; 639if (!mark_check (fm ,& errormsg )) { 640if (errormsg != NULL ) { 641emsg (errormsg ); 642 } 643res = kMarkMoveFailed ; 644 gotoend ; 645 } 646 647if (fm -> fnum != curbuf -> handle ) { 648// Need to change buffer 649fm_copy = * fm ;// Copy, autocommand may change it 650fm = & fm_copy ; 651// Jump to the file with the mark 652res |=switch_to_mark_buf (fm , !(flags & kMarkJumpList )); 653// Failed switching buffer 654if (res & kMarkMoveFailed ) { 655 gotoend ; 656 } 657// Check line count now that the **destination buffer is loaded**. 658if (!mark_check_line_bounds (curbuf ,fm ,& errormsg )) { 659if (errormsg != NULL ) { 660emsg (errormsg ); 661 } 662res |=kMarkMoveFailed ; 663 gotoend ; 664 } 665 }else if (flags & kMarkContext ) { 666// Doing it in this condition avoids double context mark when switching buffer. 667setpcmark (); 668 } 669// Move the cursor while keeping track of what changed for the caller 670pos_T prev_pos = curwin -> w_cursor ; 671pos_T pos = fm -> mark ; 672// Set lnum again, autocommands my have changed it 673curwin -> w_cursor = fm -> mark ; 674if (flags & kMarkBeginLine ) { 675beginline (BL_WHITE |BL_FIX ); 676 } 677res = prev_pos .lnum != pos .lnum ?res |kMarkChangedLine |kMarkChangedCursor :res ; 678res = prev_pos .col != pos .col ?res |kMarkChangedCol |kMarkChangedCursor :res ; 679if (flags & kMarkSetView ) { 680mark_view_restore (fm ); 681 } 682 683if (res & kMarkSwitchedBuf || res & kMarkChangedCursor ) { 684check_cursor (curwin ); 685 } 686end : 687return res ; 688} 689 690/// Restore the mark view. 691/// By remembering the offset between topline and mark lnum at the time of 692/// definition, this function restores the "view". 693/// @note Assumes the mark has been checked, is valid. 694/// @param fm the named mark. 695void mark_view_restore (fmark_T * fm ) 696{ 697if (fm != NULL && fm -> view .topline_offset >=0 ) { 698linenr_T topline = fm -> mark .lnum - fm -> view .topline_offset ; 699// If the mark does not have a view, topline_offset is MAXLNUM, 700// and this check can prevent restoring mark view in that case. 701if (topline >=1 ) { 702set_topline (curwin ,topline ); 703curwin -> w_skipcol = (fm -> view .skipcol > 0 704&& !hasFolding (curwin ,topline ,NULL ,NULL ) 705&& fm -> view .skipcol < linetabsize_eol (curwin ,topline )) 706 ?fm -> view .skipcol :0 ; 707 } 708 } 709} 710 711fmarkv_T mark_view_make (const win_T * wp ,pos_T pos ) 712{ 713return (fmarkv_T ){pos .lnum - wp -> w_topline ,wp -> w_skipcol }; 714} 715 716/// Search for the next named mark in the current file from a start position. 717/// 718/// @param startpos where to start. 719/// @param dir direction for search. 720/// 721/// @return next mark or NULL if no mark is found. 722fmark_T * getnextmark (pos_T * startpos ,int dir ,int begin_line ) 723{ 724fmark_T * result = NULL ; 725pos_T pos = * startpos ; 726 727if (dir == BACKWARD && begin_line ) { 728pos .col = 0 ; 729 }else if (dir == FORWARD && begin_line ) { 730pos .col = MAXCOL ; 731 } 732 733for (int i = 0 ;i < NMARKS ;i ++ ) { 734if (curbuf -> b_namedm [i ].mark .lnum > 0 ) { 735if (dir == FORWARD ) { 736if ((result == NULL || lt (curbuf -> b_namedm [i ].mark ,result -> mark )) 737&& lt (pos ,curbuf -> b_namedm [i ].mark )) { 738result = & curbuf -> b_namedm [i ]; 739 } 740 }else { 741if ((result == NULL || lt (result -> mark ,curbuf -> b_namedm [i ].mark )) 742&& lt (curbuf -> b_namedm [i ].mark ,pos )) { 743result = & curbuf -> b_namedm [i ]; 744 } 745 } 746 } 747 } 748 749return result ; 750} 751 752// For an xtended filemark: set the fnum from the fname. 753// This is used for marks obtained from the .shada file. It's postponed 754// until the mark is used to avoid a long startup delay. 755static void fname2fnum (xfmark_T * fm ) 756{ 757if (fm -> fname == NULL ) { 758return ; 759 } 760 761// First expand "~/" in the file name to the home directory. 762// Don't expand the whole name, it may contain other '~' chars. 763if (fm -> fname [0 ]== '~' && vim_ispathsep_nocolon (fm -> fname [1 ])) { 764size_t len = expand_env ("~/" ,NameBuff ,MAXPATHL ); 765xstrlcpy (NameBuff + len ,fm -> fname + 2 ,MAXPATHL - len ); 766 }else { 767xstrlcpy (NameBuff ,fm -> fname ,MAXPATHL ); 768 } 769 770// Try to shorten the file name. 771os_dirname (IObuff ,IOSIZE ); 772char * p = path_shorten_fname (NameBuff ,IObuff ); 773 774// buflist_new() will call fmarks_check_names() 775 (void )buflist_new (NameBuff ,p ,1 ,0 ); 776} 777 778// Check all file marks for a name that matches the file name in buf. 779// May replace the name with an fnum. 780// Used for marks that come from the .shada file. 781void fmarks_check_names (buf_T * buf ) 782{ 783char * name = buf -> b_ffname ; 784 785if (buf -> b_ffname == NULL ) { 786return ; 787 } 788 789for (int i = 0 ;i < NGLOBALMARKS ;i ++ ) { 790fmarks_check_one (& namedfm [i ],name ,buf ); 791 } 792 793FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 794for (int i = 0 ;i < wp -> w_jumplistlen ;i ++ ) { 795fmarks_check_one (& wp -> w_jumplist [i ],name ,buf ); 796 } 797 } 798} 799 800static void fmarks_check_one (xfmark_T * fm ,char * name ,buf_T * buf ) 801{ 802if (fm -> fmark .fnum == 0 803&& fm -> fname != NULL 804&& path_equal (name ,fm -> fname ,kPathCmpLiteral )) { 805fm -> fmark .fnum = buf -> b_fnum ; 806XFREE_CLEAR (fm -> fname ); 807 } 808} 809 810/// Check the position in @a fm is valid. 811/// 812/// Checks for: 813/// - NULL raising unknown mark error. 814/// - Line number <= 0 raising mark not set. 815/// - Line number > buffer line count, raising invalid mark. 816/// 817/// @param fm[in] File mark to check. 818/// @param errormsg[out] Error message, if any. 819/// 820/// @return true if the mark passes all the above checks, else false. 821bool mark_check (fmark_T * fm ,const char ** errormsg ) 822{ 823if (fm == NULL ) { 824* errormsg = _ (e_umark ); 825return false; 826 }else if (fm -> mark .lnum <=0 ) { 827// In both cases it's an error but only raise when equals to 0 828if (fm -> mark .lnum == 0 ) { 829* errormsg = _ (e_marknotset ); 830 } 831return false; 832 } 833// Only check for valid line number if the buffer is loaded. 834if (fm -> fnum == curbuf -> handle && !mark_check_line_bounds (curbuf ,fm ,errormsg )) { 835return false; 836 } 837return true; 838} 839 840/// Check if a mark line number is greater than the buffer line count, and set e_markinval. 841/// 842/// @note Should be done after the buffer is loaded into memory. 843/// @param buf Buffer where the mark is set. 844/// @param fm Mark to check. 845/// @param errormsg[out] Error message, if any. 846/// @return true if below line count else false. 847bool mark_check_line_bounds (buf_T * buf ,fmark_T * fm ,const char ** errormsg ) 848{ 849if (buf != NULL && fm -> mark .lnum > buf -> b_ml .ml_line_count ) { 850* errormsg = _ (e_markinval ); 851return false; 852 } 853return true; 854} 855 856/// Clear all marks and change list in the given buffer 857/// 858/// Used mainly when trashing the entire buffer during ":e" type commands. 859/// 860/// Does not trigger "MarkSet" event. 861/// 862/// @param[out] buf Buffer to clear marks in. 863void clrallmarks (buf_T * const buf ,const Timestamp timestamp ) 864FUNC_ATTR_NONNULL_ALL 865{ 866for (size_t i = 0 ;i < NMARKS ;i ++ ) { 867clear_fmark (& buf -> b_namedm [i ],timestamp ); 868 } 869clear_fmark (& buf -> b_last_cursor ,timestamp ); 870buf -> b_last_cursor .mark .lnum = 1 ; 871clear_fmark (& buf -> b_last_insert ,timestamp ); 872clear_fmark (& buf -> b_last_change ,timestamp ); 873buf -> b_op_start .lnum = 0 ;// start/end op mark cleared 874buf -> b_op_end .lnum = 0 ; 875for (int i = 0 ;i < buf -> b_changelistlen ;i ++ ) { 876clear_fmark (& buf -> b_changelist [i ],timestamp ); 877 } 878buf -> b_changelistlen = 0 ; 879} 880 881// Get name of file from a filemark. 882// When it's in the current buffer, return the text at the mark. 883// Returns an allocated string. 884char * fm_getname (fmark_T * fmark ,int lead_len ) 885{ 886if (fmark -> fnum == curbuf -> b_fnum ) {// current buffer 887return mark_line (& (fmark -> mark ),lead_len ); 888 } 889return buflist_nr2name (fmark -> fnum , false, true); 890} 891 892/// Return the line at mark "mp". Truncate to fit in window. 893/// The returned string has been allocated. 894static char * mark_line (pos_T * mp ,int lead_len ) 895FUNC_ATTR_NONNULL_RET 896{ 897char * p ; 898 899if (mp -> lnum == 0 || mp -> lnum > curbuf -> b_ml .ml_line_count ) { 900return xstrdup ("-invalid-" ); 901 } 902assert (Columns >=0 ); 903// Allow for up to 5 bytes per character. 904char * s = xstrnsave (skipwhite (ml_get (mp -> lnum )), (size_t )Columns * 5 ); 905 906// Truncate the line to fit it in the window 907int len = 0 ; 908for (p = s ;* p != NUL ;MB_PTR_ADV (p )) { 909len += ptr2cells (p ); 910if (len >=Columns - lead_len ) { 911break ; 912 } 913 } 914* p = NUL ; 915return s ; 916} 917 918/// ":marks {arg}": list marks, or namespace-extmarks. 919void ex_marks (exarg_T * eap ) 920{ 921nlua_call_excmd ("vim._core.marks" ,"ex_marks" ,eap ,& cmdmod ,NULL ); 922} 923 924// ":delmarks[!] [marks]" 925void ex_delmarks (exarg_T * eap ) 926{ 927int from ,to ; 928int n ; 929pos_T pos = {0 ,0 ,0 }; 930 931if (* eap -> arg == NUL && eap -> forceit ) { 932// clear all marks 933for (size_t i = 0 ;i < NMARKS ;i ++ ) { 934if (curbuf -> b_namedm [i ].mark .lnum != 0 ) { 935do_markset_autocmd ((char )(i + 'a' ),& pos ,curbuf ); 936 } 937 } 938if (curbuf -> b_last_cursor .mark .lnum != 0 ) { 939do_markset_autocmd ('"' ,& pos ,curbuf ); 940 } 941if (curbuf -> b_last_insert .mark .lnum != 0 ) { 942do_markset_autocmd ('^' ,& pos ,curbuf ); 943 } 944if (curbuf -> b_last_change .mark .lnum != 0 ) { 945do_markset_autocmd ('.' ,& pos ,curbuf ); 946 } 947if (curbuf -> b_op_start .lnum != 0 ) { 948do_markset_autocmd ('[' ,& pos ,curbuf ); 949 } 950if (curbuf -> b_op_end .lnum != 0 ) { 951do_markset_autocmd (']' ,& pos ,curbuf ); 952 } 953clrallmarks (curbuf ,os_time ()); 954 }else if (eap -> forceit ) { 955emsg (_ (e_invarg )); 956 }else if (* eap -> arg == NUL ) { 957emsg (_ (e_argreq )); 958 }else { 959// clear specified marks only 960const Timestamp timestamp = os_time (); 961for (char * p = eap -> arg ;* p != NUL ;p ++ ) { 962bool lower = ASCII_ISLOWER (* p ); 963bool digit = ascii_isdigit (* p ); 964if (lower || digit || ASCII_ISUPPER (* p )) { 965if (p [1 ]== '-' ) { 966// clear range of marks 967from = (uint8_t )(* p ); 968to = (uint8_t )p [2 ]; 969if (!(lower ?ASCII_ISLOWER (p [2 ]) 970 : (digit ?ascii_isdigit (p [2 ]) 971 :ASCII_ISUPPER (p [2 ]))) 972|| to < from ) { 973semsg (_ (e_invarg2 ),p ); 974return ; 975 } 976p += 2 ; 977 }else { 978// clear one lower case mark 979from = to = (uint8_t )(* p ); 980 } 981 982for (int i = from ;i <=to ;i ++ ) { 983if (lower ) { 984if (curbuf -> b_namedm [i - 'a' ].mark .lnum != 0 ) { 985do_markset_autocmd ((char )i ,& pos ,curbuf ); 986 } 987curbuf -> b_namedm [i - 'a' ].mark .lnum = 0 ; 988curbuf -> b_namedm [i - 'a' ].timestamp = timestamp ; 989 }else { 990if (digit ) { 991n = i - '0' + NMARKS ; 992 }else { 993n = i - 'A' ; 994 } 995if (namedfm [n ].fmark .mark .lnum != 0 ) { 996buf_T * buf = buflist_findnr (namedfm [n ].fmark .fnum ); 997if (buf == NULL ) { 998buf = curbuf ; 999 } 1000do_markset_autocmd ((char )i ,& pos ,buf ); 1001 } 1002namedfm [n ].fmark .mark .lnum = 0 ; 1003namedfm [n ].fmark .fnum = 0 ; 1004namedfm [n ].fmark .timestamp = timestamp ; 1005XFREE_CLEAR (namedfm [n ].fname ); 1006 } 1007 } 1008 }else { 1009switch (* p ) { 1010case '"' : 1011if (curbuf -> b_last_cursor .mark .lnum != 0 ) { 1012do_markset_autocmd (* p ,& pos ,curbuf ); 1013 } 1014clear_fmark (& curbuf -> b_last_cursor ,timestamp ); 1015break ; 1016case '^' : 1017if (curbuf -> b_last_insert .mark .lnum != 0 ) { 1018do_markset_autocmd (* p ,& pos ,curbuf ); 1019 } 1020clear_fmark (& curbuf -> b_last_insert ,timestamp ); 1021break ; 1022case ':' : 1023// Readonly mark. No deletion allowed. 1024break ; 1025case '.' : 1026if (curbuf -> b_last_change .mark .lnum != 0 ) { 1027do_markset_autocmd (* p ,& pos ,curbuf ); 1028 } 1029clear_fmark (& curbuf -> b_last_change ,timestamp ); 1030break ; 1031case '[' : 1032if (curbuf -> b_op_start .lnum != 0 ) { 1033do_markset_autocmd (* p ,& pos ,curbuf ); 1034 } 1035curbuf -> b_op_start .lnum = 0 ; 1036break ; 1037case ']' : 1038if (curbuf -> b_op_end .lnum != 0 ) { 1039do_markset_autocmd (* p ,& pos ,curbuf ); 1040 } 1041curbuf -> b_op_end .lnum = 0 ; 1042break ; 1043case '<' : 1044if (curbuf -> b_visual .vi_start .lnum != 0 ) { 1045do_markset_autocmd (* p ,& pos ,curbuf ); 1046 } 1047curbuf -> b_visual .vi_start .lnum = 0 ; 1048break ; 1049case '>' : 1050if (curbuf -> b_visual .vi_end .lnum != 0 ) { 1051do_markset_autocmd (* p ,& pos ,curbuf ); 1052 } 1053curbuf -> b_visual .vi_end .lnum = 0 ; 1054break ; 1055case ' ' : 1056break ; 1057default : 1058semsg (_ (e_invarg2 ),p ); 1059return ; 1060 } 1061 } 1062 } 1063 } 1064} 1065 1066// print the jumplist 1067void ex_jumps (exarg_T * eap ) 1068{ 1069cleanup_jumplist (curwin , true); 1070// Highlight title 1071msg_ext_set_kind ("list_cmd" ); 1072msg_puts_title (_ ("\n jump line col file/text" )); 1073for (int i = 0 ;i < curwin -> w_jumplistlen && !got_int ;i ++ ) { 1074if (curwin -> w_jumplist [i ].fmark .mark .lnum != 0 ) { 1075char * name = fm_getname (& curwin -> w_jumplist [i ].fmark ,16 ); 1076 1077// Make sure to output the current indicator, even when on an wiped 1078// out buffer. ":filter" may still skip it. 1079if (name == NULL && i == curwin -> w_jumplistidx ) { 1080name = xstrdup ("-invalid-" ); 1081 } 1082// apply :filter /pat/ or file name not available 1083if (name == NULL || message_filtered (name )) { 1084xfree (name ); 1085continue ; 1086 } 1087 1088msg_putchar ('\n' ); 1089if (got_int ) { 1090xfree (name ); 1091break ; 1092 } 1093snprintf (IObuff ,IOSIZE ,"%c %2d %5" PRIdLINENR " %4d " , 1094i == curwin -> w_jumplistidx ?'>' :' ' , 1095i > curwin -> w_jumplistidx ?i - curwin -> w_jumplistidx :curwin -> w_jumplistidx - i , 1096curwin -> w_jumplist [i ].fmark .mark .lnum ,curwin -> w_jumplist [i ].fmark .mark .col ); 1097msg_outtrans (IObuff ,0 , false); 1098msg_outtrans (name ,curwin -> w_jumplist [i ].fmark .fnum == curbuf -> b_fnum ?HLF_D :0 , false); 1099xfree (name ); 1100os_breakcheck (); 1101 } 1102 } 1103if (curwin -> w_jumplistidx == curwin -> w_jumplistlen ) { 1104msg_puts ("\n>" ); 1105 } 1106} 1107 1108void ex_clearjumps (exarg_T * eap ) 1109{ 1110free_jumplist (curwin ); 1111curwin -> w_jumplistlen = 0 ; 1112curwin -> w_jumplistidx = 0 ; 1113} 1114 1115// print the changelist 1116void ex_changes (exarg_T * eap ) 1117{ 1118msg_ext_set_kind ("list_cmd" ); 1119// Highlight title 1120msg_puts_title (_ ("\nchange line col text" )); 1121 1122for (int i = 0 ;i < curbuf -> b_changelistlen && !got_int ;i ++ ) { 1123if (curbuf -> b_changelist [i ].mark .lnum != 0 ) { 1124msg_putchar ('\n' ); 1125if (got_int ) { 1126break ; 1127 } 1128snprintf (IObuff ,IOSIZE ,"%c %3d %5" PRIdLINENR " %4d " , 1129i == curwin -> w_changelistidx ?'>' :' ' , 1130i > 1131curwin -> w_changelistidx ?i - curwin -> w_changelistidx :curwin -> w_changelistidx - i , 1132curbuf -> b_changelist [i ].mark .lnum , 1133curbuf -> b_changelist [i ].mark .col ); 1134msg_outtrans (IObuff ,0 , false); 1135char * name = mark_line (& curbuf -> b_changelist [i ].mark ,17 ); 1136msg_outtrans (name ,HLF_D , false); 1137xfree (name ); 1138os_breakcheck (); 1139 } 1140 } 1141if (curwin -> w_changelistidx == curbuf -> b_changelistlen ) { 1142msg_puts ("\n>" ); 1143 } 1144} 1145 1146#define ONE_ADJUST (add ) \ 1147 { \ 1148 lp = add; \ 1149 if (*lp >= line1 && *lp <= line2) { \ 1150 if (amount == MAXLNUM) { \ 1151 *lp = 0; \ 1152 } else { \ 1153 *lp += amount; \ 1154 } \ 1155 } else if (amount_after && *lp > line2) { \ 1156 *lp += amount_after; \ 1157 } \ 1158 } 1159 1160// "NO DELete": don't delete the line, just put at first deleted line. 1161#define ONE_ADJUST_NODEL (add ) \ 1162 { \ 1163 lp = add; \ 1164 if (*lp >= line1 && *lp <= line2) { \ 1165 if (amount == MAXLNUM) { \ 1166 *lp = line1; \ 1167 } else { \ 1168 *lp += amount; \ 1169 } \ 1170 } else if (amount_after && *lp > line2) { \ 1171 *lp += amount_after; \ 1172 } \ 1173 } 1174 1175// Like ONE_ADJUST_NODEL(), but if the position is within the deleted range, 1176// move it to the start of the line before the range. 1177#define ONE_ADJUST_CURSOR (pp ) \ 1178 { \ 1179 pos_T *posp = pp; \ 1180 if (posp->lnum >= line1 && posp->lnum <= line2) { \ 1181 if (amount == MAXLNUM) {/* line with cursor is deleted */ \ 1182posp -> lnum = MAX (line1 - 1 ,1 ); \ 1183posp -> col = 0 ; \ 1184 }else {/* keep cursor on the same line */ \ 1185posp -> lnum += amount ; \ 1186 } \ 1187 }else if (amount_after && posp -> lnum > line2 ) { \ 1188posp -> lnum += amount_after ; \ 1189 } \ 1190 } 1191 1192// Adjust marks between "line1" and "line2" (inclusive) to move "amount" lines. 1193// Must be called before changed_*(), appended_lines() or deleted_lines(). 1194// May be called before or after changing the text. 1195// When deleting lines "line1" to "line2", use an "amount" of MAXLNUM: The 1196// marks within this range are made invalid. 1197// If "amount_after" is non-zero adjust marks after "line2". 1198// Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2); 1199// Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0); 1200// or: mark_adjust(56, 55, MAXLNUM, 2); 1201void mark_adjust (linenr_T line1 ,linenr_T line2 ,linenr_T amount ,linenr_T amount_after , 1202ExtmarkOp op ) 1203{ 1204mark_adjust_buf (curbuf ,line1 ,line2 ,amount ,amount_after , true,kMarkAdjustNormal ,op ); 1205} 1206 1207// mark_adjust_nofold() does the same as mark_adjust() but without adjusting 1208// folds in any way. Folds must be adjusted manually by the caller. 1209// This is only useful when folds need to be moved in a way different to 1210// calling foldMarkAdjust() with arguments line1, line2, amount, amount_after, 1211// for an example of why this may be necessary, see do_move(). 1212void mark_adjust_nofold (linenr_T line1 ,linenr_T line2 ,linenr_T amount ,linenr_T amount_after , 1213ExtmarkOp op ) 1214{ 1215mark_adjust_buf (curbuf ,line1 ,line2 ,amount ,amount_after , false,kMarkAdjustNormal ,op ); 1216} 1217 1218void mark_adjust_buf (buf_T * buf ,linenr_T line1 ,linenr_T line2 ,linenr_T amount , 1219linenr_T amount_after ,bool adjust_folds ,MarkAdjustMode mode ,ExtmarkOp op ) 1220{ 1221int fnum = buf -> b_fnum ; 1222linenr_T * lp ; 1223static pos_T initpos = {1 ,0 ,0 }; 1224 1225if (line2 < line1 && amount_after == 0 ) {// nothing to do 1226return ; 1227 } 1228 1229bool by_api = mode == kMarkAdjustApi ; 1230bool by_term = mode == kMarkAdjustTerm ; 1231 1232if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1233// named marks, lower case and upper case 1234for (int i = 0 ;i < NMARKS ;i ++ ) { 1235ONE_ADJUST (& (buf -> b_namedm [i ].mark .lnum )); 1236if (namedfm [i ].fmark .fnum == fnum ) { 1237ONE_ADJUST_NODEL (& (namedfm [i ].fmark .mark .lnum )); 1238 } 1239 } 1240for (int i = NMARKS ;i < NGLOBALMARKS ;i ++ ) { 1241if (namedfm [i ].fmark .fnum == fnum ) { 1242ONE_ADJUST_NODEL (& (namedfm [i ].fmark .mark .lnum )); 1243 } 1244 } 1245 1246// last Insert position 1247ONE_ADJUST (& (buf -> b_last_insert .mark .lnum )); 1248 1249// last change position 1250ONE_ADJUST (& (buf -> b_last_change .mark .lnum )); 1251 1252// last cursor position, if it was set 1253if (!equalpos (buf -> b_last_cursor .mark ,initpos ) 1254&& (!by_term || buf -> b_last_cursor .mark .lnum < buf -> b_ml .ml_line_count )) { 1255ONE_ADJUST (& (buf -> b_last_cursor .mark .lnum )); 1256 } 1257 1258// on prompt buffer adjust the last prompt start location mark 1259if (bt_prompt (buf )) { 1260ONE_ADJUST_NODEL (& (buf -> b_prompt_start .mark .lnum )); 1261 } 1262 1263// list of change positions 1264for (int i = 0 ;i < buf -> b_changelistlen ;i ++ ) { 1265ONE_ADJUST_NODEL (& (buf -> b_changelist [i ].mark .lnum )); 1266 } 1267 1268// Visual area 1269ONE_ADJUST_NODEL (& (buf -> b_visual .vi_start .lnum )); 1270ONE_ADJUST_NODEL (& (buf -> b_visual .vi_end .lnum )); 1271 1272// quickfix marks 1273if (!qf_mark_adjust (buf ,NULL ,line1 ,line2 ,amount ,amount_after )) { 1274buf -> b_has_qf_entry &= ~BUF_HAS_QF_ENTRY ; 1275 } 1276// location lists 1277bool found_one = false; 1278FOR_ALL_TAB_WINDOWS (tab ,win ) { 1279found_one |=qf_mark_adjust (buf ,win ,line1 ,line2 ,amount ,amount_after ); 1280 } 1281if (!found_one ) { 1282buf -> b_has_qf_entry &= ~BUF_HAS_LL_ENTRY ; 1283 } 1284 } 1285 1286if (op != kExtmarkNOOP ) { 1287extmark_adjust (buf ,line1 ,line2 ,amount ,amount_after ,op ); 1288 } 1289 1290if (curwin -> w_buffer == buf ) { 1291// previous context mark 1292ONE_ADJUST (& (curwin -> w_pcmark .lnum )); 1293 1294// previous pcpmark 1295ONE_ADJUST (& (curwin -> w_prev_pcmark .lnum )); 1296 1297// saved cursor for formatting 1298if (saved_cursor .lnum != 0 ) { 1299ONE_ADJUST_NODEL (& (saved_cursor .lnum )); 1300 } 1301 } 1302 1303// Adjust items in all windows related to the current buffer. 1304FOR_ALL_TAB_WINDOWS (tab ,win ) { 1305if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1306// Marks in the jumplist. When deleting lines, this may create 1307// duplicate marks in the jumplist, they will be removed later. 1308for (int i = 0 ;i < win -> w_jumplistlen ;i ++ ) { 1309if (win -> w_jumplist [i ].fmark .fnum == fnum ) { 1310ONE_ADJUST_NODEL (& (win -> w_jumplist [i ].fmark .mark .lnum )); 1311 } 1312 } 1313 } 1314 1315if (win -> w_buffer == buf ) { 1316if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1317// marks in the tag stack 1318for (int i = 0 ;i < win -> w_tagstacklen ;i ++ ) { 1319if (win -> w_tagstack [i ].fmark .fnum == fnum ) { 1320ONE_ADJUST_NODEL (& (win -> w_tagstack [i ].fmark .mark .lnum )); 1321 } 1322 } 1323 } 1324 1325// the displayed Visual area 1326if (win -> w_old_cursor_lnum != 0 ) { 1327ONE_ADJUST_NODEL (& (win -> w_old_cursor_lnum )); 1328ONE_ADJUST_NODEL (& (win -> w_old_visual_lnum )); 1329 } 1330 1331// topline and cursor position for windows with the same buffer 1332// other than the current window 1333if (by_api || (by_term ?win -> w_cursor .lnum < buf -> b_ml .ml_line_count :win != curwin )) { 1334if (win -> w_topline >=line1 && win -> w_topline <=line2 ) { 1335if (amount == MAXLNUM ) {// topline is deleted 1336if (by_api && amount_after > line1 - line2 - 1 ) { 1337// api: if the deleted region was replaced with new contents, topline will 1338// get adjusted later as an effect of the adjusted cursor in fix_cursor() 1339 }else { 1340win -> w_topline = MAX (line1 - 1 ,1 ); 1341 } 1342 }else if (win -> w_topline > line1 ) { 1343// keep topline on the same line, unless inserting just 1344// above it (we probably want to see that line then) 1345win -> w_topline += amount ; 1346 } 1347win -> w_topfill = 0 ; 1348 }else if (amount_after 1349// api: display new line if inserted right at topline 1350// TODO(bfredl): maybe always? 1351&& win -> w_topline > line2 + (by_api && line2 < line1 ?1 :0 )) { 1352win -> w_topline += amount_after ; 1353win -> w_topfill = 0 ; 1354 } 1355 } 1356if (!by_api && (by_term ?win -> w_cursor .lnum < buf -> b_ml .ml_line_count :win != curwin )) { 1357ONE_ADJUST_CURSOR (& (win -> w_cursor )); 1358 } 1359 1360if (adjust_folds ) { 1361foldMarkAdjust (win ,line1 ,line2 ,amount ,amount_after ); 1362 } 1363 } 1364 } 1365 1366// adjust diffs 1367diff_mark_adjust (buf ,line1 ,line2 ,amount ,amount_after ); 1368 1369// adjust per-window "last cursor" positions 1370for (size_t i = 0 ;i < kv_size (buf -> b_wininfo );i ++ ) { 1371WinInfo * wip = kv_A (buf -> b_wininfo ,i ); 1372if (!by_term || wip -> wi_mark .mark .lnum < buf -> b_ml .ml_line_count ) { 1373ONE_ADJUST_CURSOR (& (wip -> wi_mark .mark )); 1374 } 1375 } 1376} 1377 1378// This code is used often, needs to be fast. 1379#define COL_ADJUST (pp ) \ 1380 { \ 1381 posp = pp; \ 1382 if (posp->lnum == lnum && posp->col >= mincol) { \ 1383 posp->lnum += lnum_amount; \ 1384 assert(col_amount > INT_MIN && col_amount <= INT_MAX); \ 1385 if (col_amount < 0 && posp->col <= -col_amount) { \ 1386 posp->col = 0; \ 1387 } else if (posp->col < spaces_removed) { \ 1388 posp->col = col_amount + spaces_removed; \ 1389 } else { \ 1390 posp->col += col_amount; \ 1391 } \ 1392 } \ 1393 } 1394 1395// Adjust marks in line "lnum" at column "mincol" and further: add 1396// "lnum_amount" to the line number and add "col_amount" to the column 1397// position. 1398// "spaces_removed" is the number of spaces that were removed, matters when the 1399// cursor is inside them. 1400void mark_col_adjust (linenr_T lnum ,colnr_T mincol ,linenr_T lnum_amount ,colnr_T col_amount , 1401int spaces_removed ) 1402{ 1403int fnum = curbuf -> b_fnum ; 1404pos_T * posp ; 1405 1406if ((col_amount == 0 && lnum_amount == 0 )|| (cmdmod .cmod_flags & CMOD_LOCKMARKS )) { 1407return ;// nothing to do 1408 } 1409// named marks, lower case and upper case 1410for (int i = 0 ;i < NMARKS ;i ++ ) { 1411COL_ADJUST (& (curbuf -> b_namedm [i ].mark )); 1412if (namedfm [i ].fmark .fnum == fnum ) { 1413COL_ADJUST (& (namedfm [i ].fmark .mark )); 1414 } 1415 } 1416for (int i = NMARKS ;i < NGLOBALMARKS ;i ++ ) { 1417if (namedfm [i ].fmark .fnum == fnum ) { 1418COL_ADJUST (& (namedfm [i ].fmark .mark )); 1419 } 1420 } 1421 1422// last Insert position 1423COL_ADJUST (& (curbuf -> b_last_insert .mark )); 1424 1425// last change position 1426COL_ADJUST (& (curbuf -> b_last_change .mark )); 1427 1428if (bt_prompt (curbuf )) { 1429COL_ADJUST (& (curbuf -> b_prompt_start .mark )); 1430 } 1431 1432// list of change positions 1433for (int i = 0 ;i < curbuf -> b_changelistlen ;i ++ ) { 1434COL_ADJUST (& (curbuf -> b_changelist [i ].mark )); 1435 } 1436 1437// Visual area 1438COL_ADJUST (& (curbuf -> b_visual .vi_start )); 1439COL_ADJUST (& (curbuf -> b_visual .vi_end )); 1440 1441// previous context mark 1442COL_ADJUST (& (curwin -> w_pcmark )); 1443 1444// previous pcmark 1445COL_ADJUST (& (curwin -> w_prev_pcmark )); 1446 1447// saved cursor for formatting 1448COL_ADJUST (& saved_cursor ); 1449 1450// Adjust items in all windows related to the current buffer. 1451FOR_ALL_WINDOWS_IN_TAB (win ,curtab ) { 1452// marks in the jumplist 1453for (int i = 0 ;i < win -> w_jumplistlen ;i ++ ) { 1454if (win -> w_jumplist [i ].fmark .fnum == fnum ) { 1455COL_ADJUST (& (win -> w_jumplist [i ].fmark .mark )); 1456 } 1457 } 1458 1459if (win -> w_buffer == curbuf ) { 1460// marks in the tag stack 1461for (int i = 0 ;i < win -> w_tagstacklen ;i ++ ) { 1462if (win -> w_tagstack [i ].fmark .fnum == fnum ) { 1463COL_ADJUST (& (win -> w_tagstack [i ].fmark .mark )); 1464 } 1465 } 1466 1467// cursor position for other windows with the same buffer 1468if (win != curwin ) { 1469COL_ADJUST (& win -> w_cursor ); 1470 } 1471 } 1472 } 1473} 1474 1475// When deleting lines, this may create duplicate marks in the 1476// jumplist. They will be removed here for the specified window. 1477// When "loadfiles" is true first ensure entries have the "fnum" field set 1478// (this may be a bit slow). 1479void cleanup_jumplist (win_T * wp ,bool loadfiles ) 1480{ 1481int i ; 1482 1483if (loadfiles ) { 1484// If specified, load all the files from the jump list. This is 1485// needed to properly clean up duplicate entries, but will take some 1486// time. 1487for (i = 0 ;i < wp -> w_jumplistlen ;i ++ ) { 1488if ((wp -> w_jumplist [i ].fmark .fnum == 0 ) 1489&& (wp -> w_jumplist [i ].fmark .mark .lnum != 0 )) { 1490fname2fnum (& wp -> w_jumplist [i ]); 1491 } 1492 } 1493 } 1494 1495int to = 0 ; 1496for (int from = 0 ;from < wp -> w_jumplistlen ;from ++ ) { 1497if (wp -> w_jumplistidx == from ) { 1498wp -> w_jumplistidx = to ; 1499 } 1500for (i = from + 1 ;i < wp -> w_jumplistlen ;i ++ ) { 1501if (wp -> w_jumplist [i ].fmark .fnum 1502== wp -> w_jumplist [from ].fmark .fnum 1503&& wp -> w_jumplist [from ].fmark .fnum != 0 1504&& wp -> w_jumplist [i ].fmark .mark .lnum 1505== wp -> w_jumplist [from ].fmark .mark .lnum ) { 1506break ; 1507 } 1508 } 1509 1510bool mustfree ; 1511if (i >=wp -> w_jumplistlen ) {// not duplicate 1512mustfree = false; 1513 }else if (i > from + 1 ) {// non-adjacent duplicate 1514// jumpoptions=stack: remove duplicates only when adjacent. 1515mustfree = !(jop_flags & kOptJopFlagStack ); 1516 }else {// adjacent duplicate 1517mustfree = true; 1518 } 1519 1520if (mustfree ) { 1521xfree (wp -> w_jumplist [from ].fname ); 1522 }else { 1523if (to != from ) { 1524// Not using wp->w_jumplist[to++] = wp->w_jumplist[from] because 1525// this way valgrind complains about overlapping source and destination 1526// in memcpy() call. (clang-3.6.0, debug build with -DEXITFREE). 1527wp -> w_jumplist [to ]= wp -> w_jumplist [from ]; 1528 } 1529to ++ ; 1530 } 1531 } 1532if (wp -> w_jumplistidx == wp -> w_jumplistlen ) { 1533wp -> w_jumplistidx = to ; 1534 } 1535wp -> w_jumplistlen = to ; 1536 1537// When pointer is below last jump, remove the jump if it matches the current 1538// line. This avoids useless/phantom jumps. #9805 1539if (loadfiles // otherwise (i.e.: Shada), last entry should be kept 1540&& wp -> w_jumplistlen && wp -> w_jumplistidx == wp -> w_jumplistlen ) { 1541const xfmark_T * fm_last = & wp -> w_jumplist [wp -> w_jumplistlen - 1 ]; 1542if (fm_last -> fmark .fnum == curbuf -> b_fnum 1543&& fm_last -> fmark .mark .lnum == wp -> w_cursor .lnum ) { 1544xfree (fm_last -> fname ); 1545wp -> w_jumplistlen -- ; 1546wp -> w_jumplistidx -- ; 1547 } 1548 } 1549} 1550 1551// Copy the jumplist from window "from" to window "to". 1552void copy_jumplist (win_T * from ,win_T * to ) 1553{ 1554for (int i = 0 ;i < from -> w_jumplistlen ;i ++ ) { 1555to -> w_jumplist [i ]= from -> w_jumplist [i ]; 1556if (from -> w_jumplist [i ].fname != NULL ) { 1557to -> w_jumplist [i ].fname = xstrdup (from -> w_jumplist [i ].fname ); 1558 } 1559 } 1560to -> w_jumplistlen = from -> w_jumplistlen ; 1561to -> w_jumplistidx = from -> w_jumplistidx ; 1562} 1563 1564/// Iterate over jumplist items 1565/// 1566/// @warning No jumplist-editing functions must be called while iteration is in 1567/// progress. 1568/// 1569/// @param[in] iter Iterator. Pass NULL to start iteration. 1570/// @param[in] win Window for which jump list is processed. 1571/// @param[out] fm Item definition. 1572/// 1573/// @return Pointer that needs to be passed to next `mark_jumplist_iter` call or 1574/// NULL if iteration is over. 1575const void * mark_jumplist_iter (const void * const iter ,const win_T * const win ,xfmark_T * const fm ) 1576FUNC_ATTR_NONNULL_ARG (2 ,3 )FUNC_ATTR_WARN_UNUSED_RESULT 1577{ 1578if (iter == NULL && win -> w_jumplistlen == 0 ) { 1579* fm = (xfmark_T )INIT_XFMARK ; 1580return NULL ; 1581 } 1582const xfmark_T * const iter_mark = iter == NULL ?& (win -> w_jumplist [0 ]) 1583 : (const xfmark_T * const )iter ; 1584* fm = * iter_mark ; 1585if (iter_mark == & (win -> w_jumplist [win -> w_jumplistlen - 1 ])) { 1586return NULL ; 1587 } 1588return iter_mark + 1 ; 1589} 1590 1591/// Iterate over global marks 1592/// 1593/// @warning No mark-editing functions must be called while iteration is in 1594/// progress. 1595/// 1596/// @param[in] iter Iterator. Pass NULL to start iteration. 1597/// @param[out] name Mark name. 1598/// @param[out] fm Mark definition. 1599/// 1600/// @return Pointer that needs to be passed to next `mark_global_iter` call or 1601/// NULL if iteration is over. 1602const void * mark_global_iter (const void * const iter ,char * const name ,xfmark_T * const fm ) 1603FUNC_ATTR_NONNULL_ARG (2 ,3 )FUNC_ATTR_WARN_UNUSED_RESULT 1604{ 1605* name = NUL ; 1606const xfmark_T * iter_mark = (iter == NULL 1607 ?& (namedfm [0 ]) 1608 : (const xfmark_T * const )iter ); 1609while ((size_t )(iter_mark - & (namedfm [0 ]))< ARRAY_SIZE (namedfm ) 1610&& !iter_mark -> fmark .mark .lnum ) { 1611iter_mark ++ ; 1612 } 1613if ((size_t )(iter_mark - & (namedfm [0 ]))== ARRAY_SIZE (namedfm ) 1614|| !iter_mark -> fmark .mark .lnum ) { 1615return NULL ; 1616 } 1617size_t iter_off = (size_t )(iter_mark - & (namedfm [0 ])); 1618* name = (char )(iter_off < NMARKS 1619 ?'A' + (char )iter_off 1620 :'0' + (char )(iter_off - NMARKS )); 1621* fm = * iter_mark ; 1622while ((size_t )(++ iter_mark - & (namedfm [0 ]))< ARRAY_SIZE (namedfm )) { 1623if (iter_mark -> fmark .mark .lnum ) { 1624return (const void * )iter_mark ; 1625 } 1626 } 1627return NULL ; 1628} 1629 1630/// Get next mark and its name 1631/// 1632/// @param[in] buf Buffer for which next mark is taken. 1633/// @param[in,out] mark_name Pointer to the current mark name. Next mark name 1634/// will be saved at this address as well. 1635/// 1636/// Current mark name must either be NUL, '"', '^', 1637/// '.' or 'a' .. 'z'. If it is neither of these 1638/// behaviour is undefined. 1639/// 1640/// @return Pointer to the next mark or NULL. 1641static inline const fmark_T * next_buffer_mark (const buf_T * const buf ,char * const mark_name ) 1642FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 1643{ 1644switch (* mark_name ) { 1645case NUL : 1646* mark_name = '"' ; 1647return & (buf -> b_last_cursor ); 1648case '"' : 1649* mark_name = '^' ; 1650return & (buf -> b_last_insert ); 1651case '^' : 1652* mark_name = '.' ; 1653return & (buf -> b_last_change ); 1654case '.' : 1655* mark_name = 'a' ; 1656return & (buf -> b_namedm [0 ]); 1657case 'z' : 1658return NULL ; 1659default : 1660 (* mark_name )++ ; 1661return & (buf -> b_namedm [* mark_name - 'a' ]); 1662 } 1663} 1664 1665/// Iterate over buffer marks 1666/// 1667/// @warning No mark-editing functions must be called while iteration is in 1668/// progress. 1669/// 1670/// @param[in] iter Iterator. Pass NULL to start iteration. 1671/// @param[in] buf Buffer. 1672/// @param[out] name Mark name. 1673/// @param[out] fm Mark definition. 1674/// 1675/// @return Pointer that needs to be passed to next `mark_buffer_iter` call or 1676/// NULL if iteration is over. 1677const void * mark_buffer_iter (const void * const iter ,const buf_T * const buf ,char * const name , 1678fmark_T * const fm ) 1679FUNC_ATTR_NONNULL_ARG (2 ,3 ,4 )FUNC_ATTR_WARN_UNUSED_RESULT 1680{ 1681* name = NUL ; 1682char mark_name = (char )(iter == NULL 1683 ?NUL 1684 : (iter == & (buf -> b_last_cursor ) 1685 ?'"' 1686 : (iter == & (buf -> b_last_insert ) 1687 ?'^' 1688 : (iter == & (buf -> b_last_change ) 1689 ?'.' 1690 :'a' + (const fmark_T * )iter - & (buf -> b_namedm [0 ]))))); 1691const fmark_T * iter_mark = next_buffer_mark (buf ,& mark_name ); 1692while (iter_mark != NULL && iter_mark -> mark .lnum == 0 ) { 1693iter_mark = next_buffer_mark (buf ,& mark_name ); 1694 } 1695if (iter_mark == NULL ) { 1696return NULL ; 1697 } 1698size_t iter_off = (size_t )(iter_mark - & (buf -> b_namedm [0 ])); 1699if (mark_name ) { 1700* name = mark_name ; 1701 }else { 1702* name = (char )('a' + (char )iter_off ); 1703 } 1704* fm = * iter_mark ; 1705return (const void * )iter_mark ; 1706} 1707 1708/// Set global mark 1709/// 1710/// @param[in] name Mark name. 1711/// @param[in] fm Mark to be set. 1712/// @param[in] update If true then only set global mark if it was created 1713/// later then existing one. 1714/// 1715/// @return true on success, false on failure. 1716bool mark_set_global (const char name ,const xfmark_T fm ,const bool update ) 1717{ 1718const int idx = mark_global_index (name ); 1719if (idx == -1 ) { 1720return false; 1721 } 1722xfmark_T * const fm_tgt = & (namedfm [idx ]); 1723if (update && fm .fmark .timestamp <=fm_tgt -> fmark .timestamp ) { 1724return false; 1725 } 1726if (fm_tgt -> fmark .mark .lnum != 0 ) { 1727free_xfmark (* fm_tgt ); 1728 } 1729* fm_tgt = fm ; 1730return true; 1731} 1732 1733/// Set local mark 1734/// 1735/// @param[in] name Mark name. 1736/// @param[in] buf Pointer to the buffer to set mark in. 1737/// @param[in] fm Mark to be set. 1738/// @param[in] update If true then only set global mark if it was created 1739/// later then existing one. 1740/// 1741/// @return true on success, false on failure. 1742bool mark_set_local (const char name ,buf_T * const buf ,const fmark_T fm ,const bool update ) 1743FUNC_ATTR_NONNULL_ALL 1744{ 1745fmark_T * fm_tgt = NULL ; 1746if (ASCII_ISLOWER (name )) { 1747fm_tgt = & (buf -> b_namedm [name - 'a' ]); 1748 }else if (name == '"' ) { 1749fm_tgt = & (buf -> b_last_cursor ); 1750 }else if (name == '^' ) { 1751fm_tgt = & (buf -> b_last_insert ); 1752 }else if (name == ':' ) { 1753fm_tgt = & (buf -> b_prompt_start ); 1754 }else if (name == '.' ) { 1755fm_tgt = & (buf -> b_last_change ); 1756 }else { 1757return false; 1758 } 1759if (update && fm .timestamp <=fm_tgt -> timestamp ) { 1760return false; 1761 } 1762if (fm_tgt -> mark .lnum != 0 ) { 1763free_fmark (* fm_tgt ); 1764 } 1765* fm_tgt = fm ; 1766return true; 1767} 1768 1769// Free items in the jumplist of window "wp". 1770void free_jumplist (win_T * wp ) 1771{ 1772for (int i = 0 ;i < wp -> w_jumplistlen ;i ++ ) { 1773free_xfmark (wp -> w_jumplist [i ]); 1774 } 1775wp -> w_jumplistlen = 0 ; 1776} 1777 1778void set_last_cursor (win_T * win ) 1779{ 1780if (win -> w_buffer != NULL ) { 1781RESET_FMARK (& win -> w_buffer -> b_last_cursor ,win -> w_cursor ,0 , ((fmarkv_T )INIT_FMARKV )); 1782 } 1783} 1784 1785#ifdef EXITFREE 1786void free_all_marks (void ) 1787{ 1788int i ; 1789 1790for (i = 0 ;i < NGLOBALMARKS ;i ++ ) { 1791if (namedfm [i ].fmark .mark .lnum != 0 ) { 1792free_xfmark (namedfm [i ]); 1793 } 1794 } 1795CLEAR_FIELD (namedfm ); 1796} 1797#endif 1798 1799/// Adjust position to point to the first byte of a multi-byte character 1800/// 1801/// If it points to a tail byte it is move backwards to the head byte. 1802/// 1803/// @param[in] buf Buffer to adjust position in. 1804/// @param[out] lp Position to adjust. 1805void mark_mb_adjustpos (buf_T * buf ,pos_T * lp ) 1806FUNC_ATTR_NONNULL_ALL 1807{ 1808if (lp -> col > 0 || lp -> coladd > 1 ) { 1809const char * const p = ml_get_buf (buf ,lp -> lnum ); 1810if (* p == NUL || ml_get_buf_len (buf ,lp -> lnum )< lp -> col ) { 1811lp -> col = 0 ; 1812 }else { 1813lp -> col -= utf_head_off (p ,p + lp -> col ); 1814 } 1815// Reset "coladd" when the cursor would be on the right half of a 1816// double-wide character. 1817if (lp -> coladd == 1 1818&& p [lp -> col ]!= TAB 1819&& vim_isprintc (utf_ptr2char (p + lp -> col )) 1820&& ptr2cells (p + lp -> col )> 1 ) { 1821lp -> coladd = 0 ; 1822 } 1823 } 1824} 1825 1826// Add information about mark 'mname' to list 'l' 1827static int add_mark (list_T * l ,const char * mname ,size_t mnamelen ,const pos_T * pos ,int bufnr , 1828const char * fname ) 1829FUNC_ATTR_NONNULL_ARG (1 ,2 ,4 ) 1830{ 1831if (pos -> lnum <=0 ) { 1832return OK ; 1833 } 1834 1835dict_T * d = tv_dict_alloc (); 1836tv_list_append_dict (l ,d ); 1837 1838list_T * lpos = tv_list_alloc (kListLenMayKnow ); 1839 1840tv_list_append_number (lpos ,bufnr ); 1841tv_list_append_number (lpos ,pos -> lnum ); 1842tv_list_append_number (lpos ,pos -> col < MAXCOL ?pos -> col + 1 :MAXCOL ); 1843tv_list_append_number (lpos ,pos -> coladd ); 1844 1845if (tv_dict_add_str_len (d ,S_LEN ("mark" ),mname , (int )mnamelen )== FAIL 1846|| tv_dict_add_list (d ,S_LEN ("pos" ),lpos )== FAIL 1847|| (fname != NULL && tv_dict_add_str (d ,S_LEN ("file" ),fname )== FAIL )) { 1848return FAIL ; 1849 } 1850 1851return OK ; 1852} 1853 1854/// Get information about marks local to a buffer. 1855/// 1856/// @param[in] buf Buffer to get the marks from 1857/// @param[out] l List to store marks 1858void get_buf_local_marks (const buf_T * buf ,list_T * l ) 1859FUNC_ATTR_NONNULL_ALL 1860{ 1861char mname [3 ]= "' " ; 1862size_t mnamelen = STRLEN_LITERAL ("' " ); 1863 1864// Marks 'a' to 'z' 1865for (int i = 0 ;i < NMARKS ;i ++ ) { 1866mname [1 ]= (char )('a' + i ); 1867add_mark (l ,mname ,mnamelen ,& buf -> b_namedm [i ].mark ,buf -> b_fnum ,NULL ); 1868 } 1869 1870// Mark '' is a window local mark and not a buffer local mark 1871add_mark (l ,S_LEN ("''" ),& curwin -> w_pcmark ,curbuf -> b_fnum ,NULL ); 1872 1873add_mark (l ,S_LEN ("'\"" ),& buf -> b_last_cursor .mark ,buf -> b_fnum ,NULL ); 1874add_mark (l ,S_LEN ("'[" ),& buf -> b_op_start ,buf -> b_fnum ,NULL ); 1875add_mark (l ,S_LEN ("']" ),& buf -> b_op_end ,buf -> b_fnum ,NULL ); 1876add_mark (l ,S_LEN ("'^" ),& buf -> b_last_insert .mark ,buf -> b_fnum ,NULL ); 1877add_mark (l ,S_LEN ("'." ),& buf -> b_last_change .mark ,buf -> b_fnum ,NULL ); 1878if (bt_prompt ((buf_T * )buf )) { 1879add_mark (l ,S_LEN ("':" ),& buf -> b_prompt_start .mark ,buf -> b_fnum ,NULL ); 1880 } 1881add_mark (l ,S_LEN ("'<" ),& buf -> b_visual .vi_start ,buf -> b_fnum ,NULL ); 1882add_mark (l ,S_LEN ("'>" ),& buf -> b_visual .vi_end ,buf -> b_fnum ,NULL ); 1883} 1884 1885/// Get a global mark 1886/// 1887/// @note Mark might not have it's fnum resolved. 1888/// @param[in] Name of named mark 1889/// @param[out] Global/file mark 1890xfmark_T get_raw_global_mark (char name ) 1891{ 1892return namedfm [mark_global_index (name )]; 1893} 1894 1895/// Get information about global marks ('A' to 'Z' and '0' to '9') 1896/// 1897/// @param[out] l List to store global marks 1898void get_global_marks (list_T * l ) 1899FUNC_ATTR_NONNULL_ALL 1900{ 1901char mname [3 ]= "' " ; 1902size_t mnamelen = STRLEN_LITERAL ("' " ); 1903char * name ; 1904 1905// Marks 'A' to 'Z' and '0' to '9' 1906for (int i = 0 ;i < NMARKS + EXTRA_MARKS ;i ++ ) { 1907if (namedfm [i ].fmark .fnum != 0 ) { 1908name = buflist_nr2name (namedfm [i ].fmark .fnum , true, true); 1909 }else { 1910name = namedfm [i ].fname ; 1911 } 1912if (name != NULL ) { 1913mname [1 ]= i >=NMARKS ? (char )(i - NMARKS + '0' ) : (char )(i + 'A' ); 1914 1915add_mark (l ,mname ,mnamelen ,& namedfm [i ].fmark .mark ,namedfm [i ].fmark .fnum ,name ); 1916if (namedfm [i ].fmark .fnum != 0 ) { 1917xfree (name ); 1918 } 1919 } 1920 } 1921}