robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// sign.c: functions for managing with signs 2 3#include <assert.h> 4#include <inttypes.h> 5#include <stdbool.h> 6#include <stdio.h> 7#include <stdlib.h> 8#include <string.h> 9 10#include "klib/kvec.h" 11#include "nvim/api/extmark.h" 12#include "nvim/api/private/defs.h" 13#include "nvim/api/private/helpers.h" 14#include "nvim/ascii_defs.h" 15#include "nvim/buffer.h" 16#include "nvim/buffer_defs.h" 17#include "nvim/charset.h" 18#include "nvim/cmdexpand_defs.h" 19#include "nvim/cursor.h" 20#include "nvim/decoration.h" 21#include "nvim/decoration_defs.h" 22#include "nvim/drawscreen.h" 23#include "nvim/errors.h" 24#include "nvim/eval/funcs.h" 25#include "nvim/eval/typval.h" 26#include "nvim/eval/typval_defs.h" 27#include "nvim/ex_cmds.h" 28#include "nvim/ex_cmds_defs.h" 29#include "nvim/extmark.h" 30#include "nvim/fold.h" 31#include "nvim/gettext_defs.h" 32#include "nvim/globals.h" 33#include "nvim/grid.h" 34#include "nvim/highlight_defs.h" 35#include "nvim/highlight_group.h" 36#include "nvim/insert.h" 37#include "nvim/macros_defs.h" 38#include "nvim/map_defs.h" 39#include "nvim/mark.h" 40#include "nvim/marktree.h" 41#include "nvim/marktree_defs.h" 42#include "nvim/mbyte.h" 43#include "nvim/memory.h" 44#include "nvim/message.h" 45#include "nvim/pos_defs.h" 46#include "nvim/sign.h" 47#include "nvim/sign_defs.h" 48#include "nvim/strings.h" 49#include "nvim/types_defs.h" 50#include "nvim/vim_defs.h" 51#include "nvim/window.h" 52 53#include "sign.c.generated.h" 54 55static PMap (cstr_t )sign_map = MAP_INIT ; 56static kvec_t (Integer )sign_ns = KV_INITIAL_VALUE ; 57 58static char * cmds []= { 59"define" , 60#define SIGNCMD_DEFINE 0 61"undefine" , 62#define SIGNCMD_UNDEFINE 1 63"list" , 64#define SIGNCMD_LIST 2 65"place" , 66#define SIGNCMD_PLACE 3 67"unplace" , 68#define SIGNCMD_UNPLACE 4 69"jump" , 70#define SIGNCMD_JUMP 5 71NULL 72#define SIGNCMD_LAST 6 73}; 74 75// Convert the supplied "group" to a namespace filter 76static int64_t group_get_ns (const char * group ) 77{ 78if (group == NULL ) { 79return 0 ;// Global namespace 80 }else if (strcmp (group ,"*" )== 0 ) { 81return UINT32_MAX ;// All namespaces 82 } 83// Specific or non-existing namespace 84int ns = map_get (String ,int )(& namespace_ids ,cstr_as_string (group )); 85return ns ?ns :-1 ; 86} 87 88static const char * sign_get_name (DecorSignHighlight * sh ) 89{ 90char * name = sh -> sign_name ; 91return !name ?"" :map_has (cstr_t ,& sign_map ,name ) ?name :"[Deleted]" ; 92} 93 94/// Create or update a sign extmark. 95/// 96/// @param buf buffer to store sign in 97/// @param id sign ID 98/// @param group sign group 99/// @param prio sign priority 100/// @param lnum line number which gets the mark 101/// @param sp sign properties 102static void buf_set_sign (buf_T * buf ,uint32_t * id ,char * group ,int prio ,linenr_T lnum ,sign_T * sp ) 103{ 104if (group && !map_get (String ,int )(& namespace_ids ,cstr_as_string (group ))) { 105kv_push (sign_ns ,nvim_create_namespace (cstr_as_string (group ))); 106 } 107 108uint32_t ns = group ? (uint32_t )nvim_create_namespace (cstr_as_string (group )) :0 ; 109DecorSignHighlight sign = DECOR_SIGN_HIGHLIGHT_INIT ; 110 111sign .flags |=kSHIsSign ; 112memcpy (sign .text ,sp -> sn_text ,SIGN_WIDTH * sizeof (schar_T )); 113sign .sign_name = xstrdup (sp -> sn_name ); 114sign .hl_id = sp -> sn_text_hl ; 115sign .line_hl_id = sp -> sn_line_hl ; 116sign .number_hl_id = sp -> sn_num_hl ; 117sign .cursorline_hl_id = sp -> sn_cul_hl ; 118sign .priority = (DecorPriority )prio ; 119 120bool has_hl = (sp -> sn_line_hl || sp -> sn_num_hl || sp -> sn_cul_hl ); 121uint16_t decor_flags = (sp -> sn_text [0 ] ?MT_FLAG_DECOR_SIGNTEXT :0 ) 122 | (has_hl ?MT_FLAG_DECOR_SIGNHL :0 ); 123 124DecorInline decor = { .ext = true, .data .ext = { .vt = NULL , .sh_idx = decor_put_sh (sign ) } }; 125extmark_set (buf ,ns ,id ,MIN (buf -> b_ml .ml_line_count ,lnum )- 1 ,0 ,-1 ,-1 , 126decor ,decor_flags , true, false, true, true,NULL ); 127} 128 129/// For an existing, placed sign with "id", modify the sign, group or priority. 130/// Returns the line number of the sign, or zero if the sign is not found. 131/// 132/// @param buf buffer to store sign in 133/// @param id sign ID 134/// @param group sign group 135/// @param prio sign priority 136/// @param sp sign pointer 137static linenr_T buf_mod_sign (buf_T * buf ,uint32_t * id ,char * group ,int prio ,sign_T * sp ) 138{ 139int64_t ns = group_get_ns (group ); 140if (ns < 0 || (group && ns == 0 )) { 141return 0 ; 142 } 143 144MTKey mark = marktree_lookup_ns (buf -> b_marktree , (uint32_t )ns ,* id , false,NULL ); 145if (mark .pos .row >=0 ) { 146buf_set_sign (buf ,id ,group ,prio ,mark .pos .row + 1 ,sp ); 147 } 148return mark .pos .row + 1 ; 149} 150 151/// Find the line number of the sign with the requested id in group 'group'. If 152/// the sign does not exist, return 0 as the line number. This will still let 153/// the correct file get loaded. 154/// 155/// @param buf buffer to store sign in 156/// @param id sign ID 157/// @param group sign group 158static int buf_findsign (buf_T * buf ,int id ,char * group ) 159{ 160int64_t ns = group_get_ns (group ); 161if (ns < 0 || (group && ns == 0 )) { 162return 0 ; 163 } 164return marktree_lookup_ns (buf -> b_marktree , (uint32_t )ns , (uint32_t )id , false,NULL ).pos .row + 1 ; 165} 166 167/// qsort() function to sort signs by line number, priority, id and recency. 168static int sign_row_cmp (const void * p1 ,const void * p2 ) 169{ 170const MTKey * s1 = (MTKey * )p1 ; 171const MTKey * s2 = (MTKey * )p2 ; 172 173if (s1 -> pos .row != s2 -> pos .row ) { 174return s1 -> pos .row > s2 -> pos .row ?1 :-1 ; 175 } 176 177DecorSignHighlight * sh1 = decor_find_sign (mt_decor (* s1 )); 178DecorSignHighlight * sh2 = decor_find_sign (mt_decor (* s2 )); 179assert (sh1 && sh2 ); 180SignItem si1 = {sh1 ,s1 -> id }; 181SignItem si2 = {sh2 ,s2 -> id }; 182 183return sign_item_cmp (& si1 ,& si2 ); 184} 185 186/// Delete the specified sign(s) 187/// 188/// @param buf buffer sign is stored in or NULL for all buffers 189/// @param group sign group 190/// @param id sign id 191/// @param atlnum single sign at this line, specified signs at any line when -1 192static int buf_delete_signs (buf_T * buf ,char * group ,int id ,linenr_T atlnum ) 193{ 194int64_t ns = group_get_ns (group ); 195if (ns < 0 ) { 196return FAIL ; 197 } 198 199MarkTreeIter itr [1 ]; 200int row = atlnum > 0 ?atlnum - 1 :0 ; 201kvec_t (MTKey )signs = KV_INITIAL_VALUE ; 202// Store signs at a specific line number to remove one later. 203if (atlnum > 0 ) { 204if (!marktree_itr_get_overlap (buf -> b_marktree ,row ,0 ,itr )) { 205return FAIL ; 206 } 207 208MTPair pair ; 209while (marktree_itr_step_overlap (buf -> b_marktree ,itr ,& pair )) { 210if ((ns == UINT32_MAX || ns == pair .start .ns )&& mt_decor_sign (pair .start )) { 211kv_push (signs ,pair .start ); 212 } 213 } 214 }else { 215marktree_itr_get (buf -> b_marktree ,0 ,0 ,itr ); 216 } 217 218while (itr -> x ) { 219MTKey mark = marktree_itr_current (itr ); 220if (row && mark .pos .row > row ) { 221break ; 222 } 223if (!mt_end (mark )&& mt_decor_sign (mark ) 224&& (id == 0 || (int )mark .id == id ) 225&& (ns == UINT32_MAX || ns == mark .ns )) { 226if (atlnum > 0 ) { 227kv_push (signs ,mark ); 228marktree_itr_next (buf -> b_marktree ,itr ); 229 }else { 230extmark_del (buf ,itr ,mark , true); 231 } 232 }else { 233marktree_itr_next (buf -> b_marktree ,itr ); 234 } 235 } 236 237// Sort to remove the highest priority sign at a specific line number. 238if (kv_size (signs )) { 239qsort ((void * )& kv_A (signs ,0 ),kv_size (signs ),sizeof (MTKey ),sign_row_cmp ); 240extmark_del_id (buf ,kv_A (signs ,0 ).ns ,kv_A (signs ,0 ).id ); 241kv_destroy (signs ); 242 }else if (atlnum > 0 ) { 243return FAIL ; 244 } 245 246return OK ; 247} 248 249bool buf_has_signs (const buf_T * buf ) 250{ 251return (buf_meta_total (buf ,kMTMetaSignHL )+ buf_meta_total (buf ,kMTMetaSignText )); 252} 253 254/// List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. 255static void sign_list_placed (buf_T * rbuf ,char * group ) 256{ 257char lbuf [MSG_BUF_LEN ]; 258char namebuf [MSG_BUF_LEN ]; 259char groupbuf [MSG_BUF_LEN ]; 260buf_T * buf = rbuf ?rbuf :firstbuf ; 261int64_t ns = group_get_ns (group ); 262 263msg_puts_title (_ ("\n--- Signs ---" )); 264 265while (buf != NULL && !got_int ) { 266if (buf_has_signs (buf )) { 267msg_putchar ('\n' ); 268const char * fname = buf -> b_fname ?buf -> b_fname :_ ("Untitled" ); 269snprintf (lbuf ,MSG_BUF_LEN ,_ ("Signs for %s:" ),fname ); 270msg_puts_hl (lbuf ,HLF_D , false); 271 } 272 273if (ns >=0 ) { 274MarkTreeIter itr [1 ]; 275kvec_t (MTKey )signs = KV_INITIAL_VALUE ; 276marktree_itr_get (buf -> b_marktree ,0 ,0 ,itr ); 277 278while (itr -> x ) { 279MTKey mark = marktree_itr_current (itr ); 280if (!mt_end (mark )&& mt_decor_sign (mark ) 281&& (ns == UINT32_MAX || ns == mark .ns )) { 282kv_push (signs ,mark ); 283 } 284marktree_itr_next (buf -> b_marktree ,itr ); 285 } 286 287if (kv_size (signs )) { 288qsort ((void * )& kv_A (signs ,0 ),kv_size (signs ),sizeof (MTKey ),sign_row_cmp ); 289msg_putchar ('\n' ); 290 291for (size_t i = 0 ;i < kv_size (signs );i ++ ) { 292namebuf [0 ]= NUL ; 293groupbuf [0 ]= NUL ; 294MTKey mark = kv_A (signs ,i ); 295 296DecorSignHighlight * sh = decor_find_sign (mt_decor (mark )); 297if (sh -> sign_name != NULL ) { 298vim_snprintf (namebuf ,MSG_BUF_LEN ,_ (" name=%s" ),sign_get_name (sh )); 299 } 300if (mark .ns != 0 ) { 301vim_snprintf (groupbuf ,MSG_BUF_LEN ,_ (" group=%s" ),describe_ns ((int )mark .ns ,"" )); 302 } 303vim_snprintf (lbuf ,MSG_BUF_LEN ,_ (" line=%" PRIdLINENR " id=%u%s%s priority=%d" ), 304mark .pos .row + 1 ,mark .id ,groupbuf ,namebuf ,sh -> priority ); 305msg_puts (lbuf ); 306if (i < kv_size (signs )- 1 ) { 307msg_putchar ('\n' ); 308 } 309 } 310kv_destroy (signs ); 311 } 312 } 313 314if (rbuf != NULL ) { 315return ; 316 } 317buf = buf -> b_next ; 318 } 319} 320 321/// Find index of a ":sign" subcmd from its name. 322/// "*end_cmd" must be writable. 323/// 324/// @param begin_cmd begin of sign subcmd 325/// @param end_cmd just after sign subcmd 326static int sign_cmd_idx (char * begin_cmd ,char * end_cmd ) 327{ 328int idx ; 329char save = * end_cmd ; 330 331* end_cmd = NUL ; 332for (idx = 0 ;;idx ++ ) { 333if (cmds [idx ]== NULL || strcmp (begin_cmd ,cmds [idx ])== 0 ) { 334break ; 335 } 336 } 337* end_cmd = save ; 338return idx ; 339} 340 341/// buf must be SIGN_WIDTH * MAX_SCHAR_SIZE (no extra +1 needed) 342size_t describe_sign_text (char * buf ,schar_T * sign_text ) 343{ 344size_t p = 0 ; 345for (int i = 0 ;i < SIGN_WIDTH ;i ++ ) { 346schar_get (buf + p ,sign_text [i ]); 347size_t len = strlen (buf + p ); 348if (len == 0 ) { 349break ; 350 } 351p += len ; 352 } 353return p ; 354} 355 356/// Initialize the "text" for a new sign and store in "sign_text". 357/// "sp" is NULL for signs added through nvim_buf_set_extmark(). 358int init_sign_text (sign_T * sp ,schar_T * sign_text ,char * text ) 359{ 360char * s ; 361char * endp = text + (int )strlen (text ); 362 363for (s = sp ?text :endp ;s + 1 < endp ;s ++ ) { 364if (* s == '\\' ) { 365// Remove a backslash, so that it is possible to use a space. 366STRMOVE (s ,s + 1 ); 367endp -- ; 368 } 369 } 370// Count cells and check for non-printable chars 371int cells = 0 ; 372for (s = text ;s < endp ;s += utfc_ptr2len (s )) { 373int c ; 374sign_text [cells ]= utfc_ptr2schar (s ,& c ); 375if (!vim_isprintc (c )) { 376break ; 377 } 378int width = utf_ptr2cells (s ); 379if (width == 2 ) { 380sign_text [cells + 1 ]= 0 ; 381 } 382cells += width ; 383 } 384// Currently must be empty, one or two display cells 385if (s != endp || cells > SIGN_WIDTH ) { 386if (sp != NULL ) { 387semsg (_ ("E239: Invalid sign text: %s" ),text ); 388 } 389return FAIL ; 390 } 391 392if (cells < 1 ) { 393sign_text [0 ]= 0 ; 394 }else if (cells == 1 ) { 395sign_text [1 ]= schar_from_ascii (' ' ); 396 } 397 398return OK ; 399} 400 401/// Define a new sign or update an existing sign 402static int sign_define_by_name (char * name ,char * icon ,char * text ,char * linehl ,char * texthl , 403char * culhl ,char * numhl ,int prio ) 404{ 405cstr_t * key ; 406bool new_sign = false; 407sign_T ** sp = (sign_T ** )pmap_put_ref (cstr_t )(& sign_map ,name ,& key ,& new_sign ); 408 409if (new_sign ) { 410* key = xstrdup (name ); 411* sp = xcalloc (1 ,sizeof (sign_T )); 412 (* sp )-> sn_name = (char * )(* key ); 413 } 414 415// Set values for a defined sign. 416if (icon != NULL ) { 417/// Initialize the icon information for a new sign 418xfree ((* sp )-> sn_icon ); 419 (* sp )-> sn_icon = xstrdup (icon ); 420backslash_halve ((* sp )-> sn_icon ); 421 } 422 423if (text != NULL && (init_sign_text (* sp , (* sp )-> sn_text ,text )== FAIL )) { 424return FAIL ; 425 } 426 427 (* sp )-> sn_priority = prio ; 428 429char * arg []= {linehl ,texthl ,culhl ,numhl }; 430int * hl []= {& (* sp )-> sn_line_hl ,& (* sp )-> sn_text_hl ,& (* sp )-> sn_cul_hl ,& (* sp )-> sn_num_hl }; 431for (int i = 0 ;i < 4 ;i ++ ) { 432if (arg [i ]!= NULL ) { 433* hl [i ]= * arg [i ] ?syn_check_group (arg [i ],strlen (arg [i ])) :0 ; 434 } 435 } 436 437// Update already placed signs and redraw if necessary when modifying a sign. 438if (!new_sign ) { 439bool did_redraw = false; 440for (size_t i = 0 ;i < kv_size (decor_items );i ++ ) { 441DecorSignHighlight * sh = & kv_A (decor_items ,i ); 442if (sh -> sign_name && strcmp (sh -> sign_name ,name )== 0 ) { 443memcpy (sh -> text , (* sp )-> sn_text ,SIGN_WIDTH * sizeof (schar_T )); 444sh -> hl_id = (* sp )-> sn_text_hl ; 445sh -> line_hl_id = (* sp )-> sn_line_hl ; 446sh -> number_hl_id = (* sp )-> sn_num_hl ; 447sh -> cursorline_hl_id = (* sp )-> sn_cul_hl ; 448if (!did_redraw ) { 449FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 450if (buf_has_signs (wp -> w_buffer )) { 451redraw_buf_later (wp -> w_buffer ,UPD_NOT_VALID ); 452 } 453 } 454did_redraw = true; 455 } 456 } 457 } 458 } 459return OK ; 460} 461 462/// Free the sign specified by 'name'. 463static int sign_undefine_by_name (const char * name ) 464{ 465sign_T * sp = pmap_del (cstr_t )(& sign_map ,name ,NULL ); 466if (sp == NULL ) { 467semsg (_ ("E155: Unknown sign: %s" ),name ); 468return FAIL ; 469 } 470 471xfree (sp -> sn_name ); 472xfree (sp -> sn_icon ); 473xfree (sp ); 474return OK ; 475} 476 477/// List one sign. 478static void sign_list_defined (sign_T * sp ) 479{ 480smsg (0 ,"sign %s" ,sp -> sn_name ); 481if (sp -> sn_icon != NULL ) { 482msg_puts (" icon=" ); 483msg_outtrans (sp -> sn_icon ,0 , false); 484msg_puts (_ (" (not supported)" )); 485 } 486if (sp -> sn_text [0 ]) { 487msg_puts (" text=" ); 488char buf [SIGN_WIDTH * MAX_SCHAR_SIZE ]; 489describe_sign_text (buf ,sp -> sn_text ); 490msg_outtrans (buf ,0 , false); 491 } 492if (sp -> sn_priority > 0 ) { 493char lbuf [MSG_BUF_LEN ]; 494vim_snprintf (lbuf ,MSG_BUF_LEN ," priority=%d" ,sp -> sn_priority ); 495msg_puts (lbuf ); 496 } 497static char * arg []= {" linehl=" ," texthl=" ," culhl=" ," numhl=" }; 498int hl []= {sp -> sn_line_hl ,sp -> sn_text_hl ,sp -> sn_cul_hl ,sp -> sn_num_hl }; 499for (int i = 0 ;i < 4 ;i ++ ) { 500if (hl [i ]> 0 ) { 501msg_puts (arg [i ]); 502const char * p = get_highlight_name_ext (NULL ,hl [i ]- 1 , false); 503msg_puts (p ?p :"NONE" ); 504 } 505 } 506} 507 508/// List the signs matching 'name' 509static void sign_list_by_name (char * name ) 510{ 511sign_T * sp = pmap_get (cstr_t )(& sign_map ,name ); 512if (sp != NULL ) { 513sign_list_defined (sp ); 514 }else { 515semsg (_ ("E155: Unknown sign: %s" ),name ); 516 } 517} 518 519/// Place a sign at the specified file location or update a sign. 520static int sign_place (uint32_t * id ,char * group ,char * name ,buf_T * buf ,linenr_T lnum ,int prio ) 521{ 522// Check for reserved character '*' in group name 523if (group != NULL && (* group == '*' || * group == NUL )) { 524return FAIL ; 525 } 526 527sign_T * sp = pmap_get (cstr_t )(& sign_map ,name ); 528if (sp == NULL ) { 529semsg (_ ("E155: Unknown sign: %s" ),name ); 530return FAIL ; 531 } 532 533// Use the default priority value for this sign. 534if (prio == -1 ) { 535prio = (sp -> sn_priority != -1 ) ?sp -> sn_priority :SIGN_DEF_PRIO ; 536 } 537 538if (lnum > 0 ) { 539// ":sign place {id} line={lnum} name={name} file={fname}": place a sign 540buf_set_sign (buf ,id ,group ,prio ,lnum ,sp ); 541 }else { 542// ":sign place {id} file={fname}": change sign type and/or priority 543lnum = buf_mod_sign (buf ,id ,group ,prio ,sp ); 544 } 545if (lnum <=0 ) { 546semsg (_ ("E885: Not possible to change sign %s" ),name ); 547return FAIL ; 548 } 549 550return OK ; 551} 552 553static int sign_unplace_inner (buf_T * buf ,int id ,char * group ,linenr_T atlnum ) 554{ 555if (!buf_has_signs (buf )) {// No signs in the buffer 556return FAIL ; 557 } 558 559if (id == 0 || atlnum > 0 || (group != NULL && * group == '*' )) { 560// Delete multiple specified signs 561if (!buf_delete_signs (buf ,group ,id ,atlnum )) { 562return FAIL ; 563 } 564 }else { 565// Delete only a single sign 566int64_t ns = group_get_ns (group ); 567if (ns < 0 || !extmark_del_id (buf , (uint32_t )ns , (uint32_t )id )) { 568return FAIL ; 569 } 570 } 571 572return OK ; 573} 574 575/// Unplace the specified sign for a single or all buffers 576static int sign_unplace (buf_T * buf ,int id ,char * group ,linenr_T atlnum ) 577{ 578if (buf != NULL ) { 579return sign_unplace_inner (buf ,id ,group ,atlnum ); 580 }else { 581int retval = OK ; 582FOR_ALL_BUFFERS (cbuf ) { 583if (!sign_unplace_inner (cbuf ,id ,group ,atlnum )) { 584retval = FAIL ; 585 } 586 } 587return retval ; 588 } 589} 590 591/// Jump to a sign. 592static linenr_T sign_jump (int id ,char * group ,buf_T * buf ) 593{ 594linenr_T lnum = buf_findsign (buf ,id ,group ); 595 596if (lnum <=0 ) { 597semsg (_ ("E157: Invalid sign ID: %d" ),id ); 598return -1 ; 599 } 600 601// goto a sign ... 602if (buf_jump_open_win (buf )!= NULL ) {// ... in a current window 603curwin -> w_cursor .lnum = lnum ; 604check_cursor_lnum (curwin ); 605beginline (BL_WHITE ); 606 }else {// ... not currently in a window 607if (buf -> b_fname == NULL ) { 608emsg (_ ("E934: Cannot jump to a buffer that does not have a name" )); 609return -1 ; 610 } 611setpcmark (); 612if (do_ecmd (buf -> b_fnum ,NULL ,NULL ,NULL ,lnum , 613buf_hide (curbuf ) ?ECMD_HIDE :0 ,curwin )== FAIL ) { 614return -1 ; 615 } 616 } 617 618foldOpenCursor (); 619 620return lnum ; 621} 622 623/// ":sign define {name} ..." command 624static void sign_define_cmd (char * name ,char * cmdline ) 625{ 626char * icon = NULL ; 627char * text = NULL ; 628char * linehl = NULL ; 629char * texthl = NULL ; 630char * culhl = NULL ; 631char * numhl = NULL ; 632int prio = -1 ; 633 634// set values for a defined sign. 635while (true) { 636char * arg = skipwhite (cmdline ); 637if (* arg == NUL ) { 638break ; 639 } 640cmdline = skiptowhite_esc (arg ); 641if (strncmp (arg ,"icon=" ,5 )== 0 ) { 642icon = arg + 5 ; 643 }else if (strncmp (arg ,"text=" ,5 )== 0 ) { 644text = arg + 5 ; 645 }else if (strncmp (arg ,"linehl=" ,7 )== 0 ) { 646linehl = arg + 7 ; 647 }else if (strncmp (arg ,"texthl=" ,7 )== 0 ) { 648texthl = arg + 7 ; 649 }else if (strncmp (arg ,"culhl=" ,6 )== 0 ) { 650culhl = arg + 6 ; 651 }else if (strncmp (arg ,"numhl=" ,6 )== 0 ) { 652numhl = arg + 6 ; 653 }else if (strncmp (arg ,"priority=" ,9 )== 0 ) { 654prio = atoi (arg + 9 ); 655 }else { 656semsg (_ (e_invarg2 ),arg ); 657return ; 658 } 659if (* cmdline == NUL ) { 660break ; 661 } 662* cmdline ++ = NUL ; 663 } 664 665sign_define_by_name (name ,icon ,text ,linehl ,texthl ,culhl ,numhl ,prio ); 666} 667 668/// ":sign place" command 669static void sign_place_cmd (buf_T * buf ,linenr_T lnum ,char * name ,int id ,char * group ,int prio ) 670{ 671if (id <=0 ) { 672// List signs placed in a file/buffer 673// :sign place file={fname} 674// :sign place group={group} file={fname} 675// :sign place group=* file={fname} 676// :sign place buffer={nr} 677// :sign place group={group} buffer={nr} 678// :sign place group=* buffer={nr} 679// :sign place 680// :sign place group={group} 681// :sign place group=* 682if (lnum >=0 || name != NULL || (group != NULL && * group == NUL )) { 683emsg (_ (e_invarg )); 684 }else { 685sign_list_placed (buf ,group ); 686 } 687 }else { 688// Place a new sign 689if (name == NULL || buf == NULL || (group != NULL && * group == NUL )) { 690emsg (_ (e_invarg )); 691return ; 692 } 693uint32_t uid = (uint32_t )id ; 694sign_place (& uid ,group ,name ,buf ,lnum ,prio ); 695 } 696} 697 698/// ":sign unplace" command 699static void sign_unplace_cmd (buf_T * buf ,linenr_T lnum ,const char * name ,int id ,char * group ) 700{ 701if (lnum >=0 || name != NULL || (group != NULL && * group == NUL )) { 702emsg (_ (e_invarg )); 703return ; 704 } 705 706if (id == -1 ) { 707lnum = curwin -> w_cursor .lnum ; 708buf = curwin -> w_buffer ; 709 } 710 711if (!sign_unplace (buf ,MAX (0 ,id ),group ,lnum )&& lnum > 0 ) { 712emsg (_ ("E159: Missing sign number" )); 713 } 714} 715 716/// Jump to a placed sign commands: 717/// :sign jump {id} file={fname} 718/// :sign jump {id} buffer={nr} 719/// :sign jump {id} group={group} file={fname} 720/// :sign jump {id} group={group} buffer={nr} 721static void sign_jump_cmd (buf_T * buf ,linenr_T lnum ,const char * name ,int id ,char * group ) 722{ 723if (name == NULL && group == NULL && id == -1 ) { 724emsg (_ (e_argreq )); 725return ; 726 } 727 728if (buf == NULL || (group != NULL && * group == NUL )|| lnum >=0 || name != NULL ) { 729// File or buffer is not specified or an empty group is used 730// or a line number or a sign name is specified. 731emsg (_ (e_invarg )); 732return ; 733 } 734 735sign_jump (id ,group ,buf ); 736} 737 738/// Parse the command line arguments for the ":sign place", ":sign unplace" and 739/// ":sign jump" commands. 740/// The supported arguments are: line={lnum} name={name} group={group} 741/// priority={prio} and file={fname} or buffer={nr}. 742static int parse_sign_cmd_args (int cmd ,char * arg ,char ** name ,int * id ,char ** group ,int * prio , 743buf_T ** buf ,linenr_T * lnum ) 744{ 745char * arg1 = arg ; 746char * filename = NULL ; 747bool lnum_arg = false; 748 749// first arg could be placed sign id 750if (ascii_isdigit (* arg )) { 751* id = getdigits_int (& arg , true,0 ); 752if (!ascii_iswhite (* arg )&& * arg != NUL ) { 753* id = -1 ; 754arg = arg1 ; 755 }else { 756arg = skipwhite (arg ); 757 } 758 } 759 760while (* arg != NUL ) { 761if (strncmp (arg ,"line=" ,5 )== 0 ) { 762arg += 5 ; 763* lnum = atoi (arg ); 764arg = skiptowhite (arg ); 765lnum_arg = true; 766 }else if (strncmp (arg ,"*" ,1 )== 0 && cmd == SIGNCMD_UNPLACE ) { 767if (* id != -1 ) { 768emsg (_ (e_invarg )); 769return FAIL ; 770 } 771* id = -2 ; 772arg = skiptowhite (arg + 1 ); 773 }else if (strncmp (arg ,"name=" ,5 )== 0 ) { 774arg += 5 ; 775char * namep = arg ; 776arg = skiptowhite (arg ); 777if (* arg != NUL ) { 778* arg ++ = NUL ; 779 } 780while (namep [0 ]== '0' && namep [1 ]!= NUL ) { 781namep ++ ; 782 } 783* name = namep ; 784 }else if (strncmp (arg ,"group=" ,6 )== 0 ) { 785arg += 6 ; 786* group = arg ; 787arg = skiptowhite (arg ); 788if (* arg != NUL ) { 789* arg ++ = NUL ; 790 } 791 }else if (strncmp (arg ,"priority=" ,9 )== 0 ) { 792arg += 9 ; 793* prio = atoi (arg ); 794arg = skiptowhite (arg ); 795 }else if (strncmp (arg ,"file=" ,5 )== 0 ) { 796arg += 5 ; 797filename = arg ; 798* buf = buflist_findname_exp (arg ); 799break ; 800 }else if (strncmp (arg ,"buffer=" ,7 )== 0 ) { 801arg += 7 ; 802filename = arg ; 803* buf = buflist_findnr (getdigits_int (& arg , true,0 )); 804if (* skipwhite (arg )!= NUL ) { 805semsg (_ (e_trailing_arg ),arg ); 806 } 807break ; 808 }else { 809emsg (_ (e_invarg )); 810return FAIL ; 811 } 812arg = skipwhite (arg ); 813 } 814 815if (filename != NULL && * buf == NULL ) { 816semsg (_ (e_invalid_buffer_name_str ),filename ); 817return FAIL ; 818 } 819 820// If the filename is not supplied for the sign place or the sign jump 821// command, then use the current buffer. 822if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg )|| cmd == SIGNCMD_JUMP )) { 823* buf = curwin -> w_buffer ; 824 } 825return OK ; 826} 827 828/// ":sign" command 829void ex_sign (exarg_T * eap ) 830{ 831char * arg = eap -> arg ; 832 833// Parse the subcommand. 834char * p = skiptowhite (arg ); 835int idx = sign_cmd_idx (arg ,p ); 836if (idx == SIGNCMD_LAST ) { 837semsg (_ ("E160: Unknown sign command: %s" ),arg ); 838return ; 839 } 840arg = skipwhite (p ); 841 842if (idx <=SIGNCMD_LIST ) { 843// Define, undefine or list signs. 844if (idx == SIGNCMD_LIST && * arg == NUL ) { 845// ":sign list": list all defined signs 846sign_T * sp ; 847map_foreach_value (& sign_map ,sp , { 848sign_list_defined (sp ); 849 }); 850 }else if (* arg == NUL ) { 851emsg (_ ("E156: Missing sign name" )); 852 }else { 853// Isolate the sign name. If it's a number skip leading zeroes, 854// so that "099" and "99" are the same sign. But keep "0". 855p = skiptowhite (arg ); 856if (* p != NUL ) { 857* p ++ = NUL ; 858 } 859while (arg [0 ]== '0' && arg [1 ]!= NUL ) { 860arg ++ ; 861 } 862 863if (idx == SIGNCMD_DEFINE ) { 864sign_define_cmd (arg ,p ); 865 }else if (idx == SIGNCMD_LIST ) { 866// ":sign list {name}" 867sign_list_by_name (arg ); 868 }else { 869// ":sign undefine {name}" 870sign_undefine_by_name (arg ); 871 } 872 873return ; 874 } 875 }else { 876int id = -1 ; 877linenr_T lnum = -1 ; 878char * name = NULL ; 879char * group = NULL ; 880int prio = -1 ; 881buf_T * buf = NULL ; 882 883// Parse command line arguments 884if (parse_sign_cmd_args (idx ,arg ,& name ,& id ,& group ,& prio ,& buf ,& lnum )== FAIL ) { 885return ; 886 } 887 888if (idx == SIGNCMD_PLACE ) { 889sign_place_cmd (buf ,lnum ,name ,id ,group ,prio ); 890 }else if (idx == SIGNCMD_UNPLACE ) { 891sign_unplace_cmd (buf ,lnum ,name ,id ,group ); 892 }else if (idx == SIGNCMD_JUMP ) { 893sign_jump_cmd (buf ,lnum ,name ,id ,group ); 894 } 895 } 896} 897 898/// Get dictionary of information for a defined sign "sp" 899static dict_T * sign_get_info_dict (sign_T * sp ) 900{ 901dict_T * d = tv_dict_alloc (); 902 903tv_dict_add_str (d ,S_LEN ("name" ),sp -> sn_name ); 904 905if (sp -> sn_icon != NULL ) { 906tv_dict_add_str (d ,S_LEN ("icon" ),sp -> sn_icon ); 907 } 908if (sp -> sn_text [0 ]) { 909char buf [SIGN_WIDTH * MAX_SCHAR_SIZE ]; 910describe_sign_text (buf ,sp -> sn_text ); 911tv_dict_add_str (d ,S_LEN ("text" ),buf ); 912 } 913if (sp -> sn_priority > 0 ) { 914tv_dict_add_nr (d ,S_LEN ("priority" ),sp -> sn_priority ); 915 } 916static char * arg []= {"linehl" ,"texthl" ,"culhl" ,"numhl" }; 917int hl []= {sp -> sn_line_hl ,sp -> sn_text_hl ,sp -> sn_cul_hl ,sp -> sn_num_hl }; 918for (int i = 0 ;i < 4 ;i ++ ) { 919if (hl [i ]> 0 ) { 920const char * p = get_highlight_name_ext (NULL ,hl [i ]- 1 , false); 921if (p == NULL ) { 922tv_dict_add_str_len (d ,arg [i ],strlen (arg [i ]),S_LEN ("NONE" )); 923 }else { 924tv_dict_add_str (d ,arg [i ],strlen (arg [i ]),p ); 925 } 926 } 927 } 928return d ; 929} 930 931/// Get dictionary of information for placed sign "mark" 932static dict_T * sign_get_placed_info_dict (MTKey mark ) 933{ 934dict_T * d = tv_dict_alloc (); 935 936DecorSignHighlight * sh = decor_find_sign (mt_decor (mark )); 937 938tv_dict_add_str (d ,S_LEN ("name" ),sign_get_name (sh )); 939tv_dict_add_nr (d ,S_LEN ("id" ), (int )mark .id ); 940tv_dict_add_str (d ,S_LEN ("group" ),describe_ns ((int )mark .ns ,"" )); 941tv_dict_add_nr (d ,S_LEN ("lnum" ),mark .pos .row + 1 ); 942tv_dict_add_nr (d ,S_LEN ("priority" ),sh -> priority ); 943return d ; 944} 945 946/// Returns information about signs placed in a buffer as list of dicts. 947list_T * get_buffer_signs (buf_T * buf ) 948FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 949{ 950list_T * const l = tv_list_alloc (kListLenMayKnow ); 951MarkTreeIter itr [1 ]; 952marktree_itr_get (buf -> b_marktree ,0 ,0 ,itr ); 953 954while (itr -> x ) { 955MTKey mark = marktree_itr_current (itr ); 956if (!mt_end (mark )&& mt_decor_sign (mark )) { 957tv_list_append_dict (l ,sign_get_placed_info_dict (mark )); 958 } 959marktree_itr_next (buf -> b_marktree ,itr ); 960 } 961 962return l ; 963} 964 965/// @return information about all the signs placed in a buffer 966static void sign_get_placed_in_buf (buf_T * buf ,linenr_T lnum ,int sign_id ,const char * group , 967list_T * retlist ) 968{ 969dict_T * d = tv_dict_alloc (); 970tv_list_append_dict (retlist ,d ); 971 972tv_dict_add_nr (d ,S_LEN ("bufnr" ),buf -> b_fnum ); 973 974list_T * l = tv_list_alloc (kListLenMayKnow ); 975tv_dict_add_list (d ,S_LEN ("signs" ),l ); 976 977int64_t ns = group_get_ns (group ); 978if (!buf_has_signs (buf )|| ns < 0 ) { 979return ; 980 } 981 982MarkTreeIter itr [1 ]; 983kvec_t (MTKey )signs = KV_INITIAL_VALUE ; 984marktree_itr_get (buf -> b_marktree ,lnum ?lnum - 1 :0 ,0 ,itr ); 985 986while (itr -> x ) { 987MTKey mark = marktree_itr_current (itr ); 988if (lnum && mark .pos .row >=lnum ) { 989break ; 990 } 991if (!mt_end (mark ) 992&& (ns == UINT32_MAX || ns == mark .ns ) 993&& ((lnum == 0 && sign_id == 0 ) 994|| (sign_id == 0 && lnum == mark .pos .row + 1 ) 995|| (lnum == 0 && sign_id == (int )mark .id ) 996|| (lnum == mark .pos .row + 1 && sign_id == (int )mark .id ))) { 997if (mt_decor_sign (mark )) { 998kv_push (signs ,mark ); 999 } 1000 } 1001marktree_itr_next (buf -> b_marktree ,itr ); 1002 } 1003 1004if (kv_size (signs )) { 1005qsort ((void * )& kv_A (signs ,0 ),kv_size (signs ),sizeof (MTKey ),sign_row_cmp ); 1006for (size_t i = 0 ;i < kv_size (signs );i ++ ) { 1007tv_list_append_dict (l ,sign_get_placed_info_dict (kv_A (signs ,i ))); 1008 } 1009kv_destroy (signs ); 1010 } 1011} 1012 1013/// Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the 1014/// sign placed at the line number. If 'lnum' is zero, return all the signs 1015/// placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers. 1016static void sign_get_placed (buf_T * buf ,linenr_T lnum ,int id ,const char * group ,list_T * retlist ) 1017{ 1018if (buf != NULL ) { 1019sign_get_placed_in_buf (buf ,lnum ,id ,group ,retlist ); 1020 }else { 1021FOR_ALL_BUFFERS (cbuf ) { 1022if (buf_has_signs (cbuf )) { 1023sign_get_placed_in_buf (cbuf ,0 ,id ,group ,retlist ); 1024 } 1025 } 1026 } 1027} 1028 1029void free_signs (void ) 1030{ 1031cstr_t name ; 1032kvec_t (cstr_t )names = KV_INITIAL_VALUE ; 1033map_foreach_key (& sign_map ,name , { 1034kv_push (names ,name ); 1035 }); 1036for (size_t i = 0 ;i < kv_size (names );i ++ ) { 1037sign_undefine_by_name (kv_A (names ,i )); 1038 } 1039kv_destroy (names ); 1040} 1041 1042static enum { 1043EXP_SUBCMD ,// expand :sign sub-commands 1044EXP_DEFINE ,// expand :sign define {name} args 1045EXP_PLACE ,// expand :sign place {id} args 1046EXP_LIST ,// expand :sign place args 1047EXP_UNPLACE ,// expand :sign unplace" 1048EXP_SIGN_NAMES ,// expand with name of placed signs 1049EXP_SIGN_GROUPS ,// expand with name of placed sign groups 1050}expand_what ; 1051 1052/// @return the n'th sign name (used for command line completion) 1053static char * get_nth_sign_name (int idx ) 1054FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 1055{ 1056// Complete with name of signs already defined 1057cstr_t name ; 1058int current_idx = 0 ; 1059map_foreach_key (& sign_map ,name , { 1060if (current_idx ++ == idx ) { 1061return (char * )name ; 1062 } 1063 }); 1064return NULL ; 1065} 1066 1067/// @return the n'th sign group name (used for command line completion) 1068static char * get_nth_sign_group_name (int idx ) 1069{ 1070// Complete with name of sign groups already defined 1071if (idx < (int )kv_size (sign_ns )) { 1072return (char * )describe_ns ((NS )kv_A (sign_ns ,idx ),"" ); 1073 } 1074return NULL ; 1075} 1076 1077/// Function given to ExpandGeneric() to obtain the sign command expansion. 1078char * get_sign_name (expand_T * xp ,int idx ) 1079{ 1080switch (expand_what ) { 1081case EXP_SUBCMD : 1082return cmds [idx ]; 1083case EXP_DEFINE : { 1084char * define_arg []= {"culhl=" ,"icon=" ,"linehl=" ,"numhl=" ,"text=" ,"texthl=" , 1085"priority=" ,NULL }; 1086return define_arg [idx ]; 1087 } 1088case EXP_PLACE : { 1089char * place_arg []= {"line=" ,"name=" ,"group=" ,"priority=" ,"file=" ,"buffer=" ,NULL }; 1090return place_arg [idx ]; 1091 } 1092case EXP_LIST : { 1093char * list_arg []= {"group=" ,"file=" ,"buffer=" ,NULL }; 1094return list_arg [idx ]; 1095 } 1096case EXP_UNPLACE : { 1097char * unplace_arg []= {"group=" ,"file=" ,"buffer=" ,NULL }; 1098return unplace_arg [idx ]; 1099 } 1100case EXP_SIGN_NAMES : 1101return get_nth_sign_name (idx ); 1102case EXP_SIGN_GROUPS : 1103return get_nth_sign_group_name (idx ); 1104default : 1105return NULL ; 1106 } 1107} 1108 1109/// Handle command line completion for :sign command. 1110void set_context_in_sign_cmd (expand_T * xp ,char * arg ) 1111{ 1112// Default: expand subcommands. 1113xp -> xp_context = EXPAND_SIGN ; 1114expand_what = EXP_SUBCMD ; 1115xp -> xp_pattern = arg ; 1116 1117char * end_subcmd = skiptowhite (arg ); 1118if (* end_subcmd == NUL ) { 1119// expand subcmd name 1120// :sign {subcmd}<CTRL-D> 1121return ; 1122 } 1123 1124int cmd_idx = sign_cmd_idx (arg ,end_subcmd ); 1125 1126// :sign {subcmd} {subcmd_args} 1127// | 1128// begin_subcmd_args 1129char * begin_subcmd_args = skipwhite (end_subcmd ); 1130 1131// Expand last argument of subcmd. 1132// 1133// :sign define {name} {args}... 1134// | 1135// p 1136 1137// Loop until reaching last argument. 1138char * last ; 1139char * p = begin_subcmd_args ; 1140do { 1141p = skipwhite (p ); 1142last = p ; 1143p = skiptowhite (p ); 1144 }while (* p != NUL ); 1145 1146p = vim_strchr (last ,'=' ); 1147 1148// :sign define {name} {args}... {last}= 1149// | | 1150// last p 1151if (p == NULL ) { 1152// Expand last argument name (before equal sign). 1153xp -> xp_pattern = last ; 1154switch (cmd_idx ) { 1155case SIGNCMD_DEFINE : 1156expand_what = EXP_DEFINE ; 1157break ; 1158case SIGNCMD_PLACE : 1159// List placed signs 1160if (ascii_isdigit (* begin_subcmd_args )) { 1161// :sign place {id} {args}... 1162expand_what = EXP_PLACE ; 1163 }else { 1164// :sign place {args}... 1165expand_what = EXP_LIST ; 1166 } 1167break ; 1168case SIGNCMD_LIST : 1169case SIGNCMD_UNDEFINE : 1170// :sign list <CTRL-D> 1171// :sign undefine <CTRL-D> 1172expand_what = EXP_SIGN_NAMES ; 1173break ; 1174case SIGNCMD_JUMP : 1175case SIGNCMD_UNPLACE : 1176expand_what = EXP_UNPLACE ; 1177break ; 1178default : 1179xp -> xp_context = EXPAND_NOTHING ; 1180 } 1181 }else { 1182// Expand last argument value (after equal sign). 1183xp -> xp_pattern = p + 1 ; 1184switch (cmd_idx ) { 1185case SIGNCMD_DEFINE : 1186if (strncmp (last ,"texthl" ,6 )== 0 1187|| strncmp (last ,"linehl" ,6 )== 0 1188|| strncmp (last ,"culhl" ,5 )== 0 1189|| strncmp (last ,"numhl" ,5 )== 0 ) { 1190xp -> xp_context = EXPAND_HIGHLIGHT ; 1191 }else if (strncmp (last ,"icon" ,4 )== 0 ) { 1192xp -> xp_context = EXPAND_FILES ; 1193 }else { 1194xp -> xp_context = EXPAND_NOTHING ; 1195 } 1196break ; 1197case SIGNCMD_PLACE : 1198if (strncmp (last ,"name" ,4 )== 0 ) { 1199expand_what = EXP_SIGN_NAMES ; 1200 }else if (strncmp (last ,"group" ,5 )== 0 ) { 1201expand_what = EXP_SIGN_GROUPS ; 1202 }else if (strncmp (last ,"file" ,4 )== 0 ) { 1203xp -> xp_context = EXPAND_BUFFERS ; 1204 }else { 1205xp -> xp_context = EXPAND_NOTHING ; 1206 } 1207break ; 1208case SIGNCMD_UNPLACE : 1209case SIGNCMD_JUMP : 1210if (strncmp (last ,"group" ,5 )== 0 ) { 1211expand_what = EXP_SIGN_GROUPS ; 1212 }else if (strncmp (last ,"file" ,4 )== 0 ) { 1213xp -> xp_context = EXPAND_BUFFERS ; 1214 }else { 1215xp -> xp_context = EXPAND_NOTHING ; 1216 } 1217break ; 1218default : 1219xp -> xp_context = EXPAND_NOTHING ; 1220 } 1221 } 1222} 1223 1224/// Define a sign using the attributes in 'dict'. Returns 0 on success and -1 on 1225/// failure. 1226static int sign_define_from_dict (char * name ,dict_T * dict ) 1227{ 1228if (name == NULL ) { 1229name = tv_dict_get_string (dict ,"name" , false); 1230if (name == NULL || name [0 ]== NUL ) { 1231return -1 ; 1232 } 1233 } 1234 1235char * icon = NULL ; 1236char * linehl = NULL ; 1237char * text = NULL ; 1238char * texthl = NULL ; 1239char * culhl = NULL ; 1240char * numhl = NULL ; 1241int prio = -1 ; 1242 1243if (dict != NULL ) { 1244icon = tv_dict_get_string (dict ,"icon" , false); 1245linehl = tv_dict_get_string (dict ,"linehl" , false); 1246text = tv_dict_get_string (dict ,"text" , false); 1247texthl = tv_dict_get_string (dict ,"texthl" , false); 1248culhl = tv_dict_get_string (dict ,"culhl" , false); 1249numhl = tv_dict_get_string (dict ,"numhl" , false); 1250prio = (int )tv_dict_get_number_def (dict ,"priority" ,-1 ); 1251 } 1252 1253return sign_define_by_name (name ,icon ,text ,linehl ,texthl ,culhl ,numhl ,prio )- 1 ; 1254} 1255 1256/// Define multiple signs using attributes from list 'l' and store the return 1257/// values in 'retlist'. 1258static void sign_define_multiple (list_T * l ,list_T * retlist ) 1259{ 1260TV_LIST_ITER_CONST (l ,li , { 1261int retval = -1 ; 1262if (TV_LIST_ITEM_TV (li )-> v_type == VAR_DICT ) { 1263retval = sign_define_from_dict (NULL ,TV_LIST_ITEM_TV (li )-> vval .v_dict ); 1264 }else { 1265emsg (_ (e_dictreq )); 1266 } 1267tv_list_append_number (retlist ,retval ); 1268 }); 1269} 1270 1271/// "sign_define()" function 1272void f_sign_define (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1273{ 1274if (argvars [0 ].v_type == VAR_LIST && argvars [1 ].v_type == VAR_UNKNOWN ) { 1275// Define multiple signs 1276tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1277 1278sign_define_multiple (argvars [0 ].vval .v_list ,rettv -> vval .v_list ); 1279return ; 1280 } 1281 1282// Define a single sign 1283rettv -> vval .v_number = -1 ; 1284 1285char * name = (char * )tv_get_string_chk (& argvars [0 ]); 1286if (name == NULL ) { 1287return ; 1288 } 1289 1290if (tv_check_for_opt_dict_arg (argvars ,1 )== FAIL ) { 1291return ; 1292 } 1293 1294dict_T * d = argvars [1 ].v_type == VAR_DICT ?argvars [1 ].vval .v_dict :NULL ; 1295rettv -> vval .v_number = sign_define_from_dict (name ,d ); 1296} 1297 1298/// "sign_getdefined()" function 1299void f_sign_getdefined (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1300{ 1301tv_list_alloc_ret (rettv ,0 ); 1302 1303if (argvars [0 ].v_type == VAR_UNKNOWN ) { 1304sign_T * sp ; 1305map_foreach_value (& sign_map ,sp , { 1306tv_list_append_dict (rettv -> vval .v_list ,sign_get_info_dict (sp )); 1307 }); 1308 }else { 1309sign_T * sp = pmap_get (cstr_t )(& sign_map ,tv_get_string (& argvars [0 ])); 1310if (sp != NULL ) { 1311tv_list_append_dict (rettv -> vval .v_list ,sign_get_info_dict (sp )); 1312 } 1313 } 1314} 1315 1316/// "sign_getplaced()" function 1317void f_sign_getplaced (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1318{ 1319buf_T * buf = NULL ; 1320linenr_T lnum = 0 ; 1321int sign_id = 0 ; 1322const char * group = NULL ; 1323bool notanum = false; 1324 1325tv_list_alloc_ret (rettv ,0 ); 1326 1327if (argvars [0 ].v_type != VAR_UNKNOWN ) { 1328// get signs placed in the specified buffer 1329buf = get_buf_arg (& argvars [0 ]); 1330if (buf == NULL ) { 1331return ; 1332 } 1333 1334if (argvars [1 ].v_type != VAR_UNKNOWN ) { 1335if (tv_check_for_nonnull_dict_arg (argvars ,1 )== FAIL ) { 1336return ; 1337 } 1338dictitem_T * di ; 1339dict_T * dict = argvars [1 ].vval .v_dict ; 1340if ((di = tv_dict_find (dict ,"lnum" ,-1 ))!= NULL ) { 1341// get signs placed at this line 1342lnum = tv_get_lnum (& di -> di_tv ); 1343if (lnum <=0 ) { 1344return ; 1345 } 1346 } 1347if ((di = tv_dict_find (dict ,"id" ,-1 ))!= NULL ) { 1348// get sign placed with this identifier 1349sign_id = (int )tv_get_number_chk (& di -> di_tv ,& notanum ); 1350if (notanum ) { 1351return ; 1352 } 1353 } 1354if ((di = tv_dict_find (dict ,"group" ,-1 ))!= NULL ) { 1355group = tv_get_string_chk (& di -> di_tv ); 1356if (group == NULL ) { 1357return ; 1358 } 1359if (* group == NUL ) {// empty string means global group 1360group = NULL ; 1361 } 1362 } 1363 } 1364 } 1365 1366sign_get_placed (buf ,lnum ,sign_id ,group ,rettv -> vval .v_list ); 1367} 1368 1369/// "sign_jump()" function 1370void f_sign_jump (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1371{ 1372rettv -> vval .v_number = -1 ; 1373 1374// Sign identifier 1375bool notanum = false; 1376int id = (int )tv_get_number_chk (& argvars [0 ],& notanum ); 1377if (notanum ) { 1378return ; 1379 } 1380if (id <=0 ) { 1381emsg (_ (e_invarg )); 1382return ; 1383 } 1384 1385// Sign group 1386char * group = (char * )tv_get_string_chk (& argvars [1 ]); 1387if (group == NULL ) { 1388return ; 1389 } 1390if (group [0 ]== NUL ) { 1391group = NULL ; 1392 } 1393 1394// Buffer to place the sign 1395buf_T * buf = get_buf_arg (& argvars [2 ]); 1396if (buf == NULL ) { 1397return ; 1398 } 1399 1400rettv -> vval .v_number = sign_jump (id ,group ,buf ); 1401} 1402 1403/// Place a new sign using the values specified in dict 'dict'. Returns the sign 1404/// identifier if successfully placed, otherwise returns -1. 1405static int sign_place_from_dict (typval_T * id_tv ,typval_T * group_tv ,typval_T * name_tv , 1406typval_T * buf_tv ,dict_T * dict ) 1407{ 1408dictitem_T * di ; 1409 1410int id = 0 ; 1411bool notanum = false; 1412if (id_tv == NULL ) { 1413di = tv_dict_find (dict ,"id" ,-1 ); 1414if (di != NULL ) { 1415id_tv = & di -> di_tv ; 1416 } 1417 } 1418if (id_tv != NULL ) { 1419id = (int )tv_get_number_chk (id_tv ,& notanum ); 1420if (notanum ) { 1421return -1 ; 1422 } 1423if (id < 0 ) { 1424emsg (_ (e_invarg )); 1425return -1 ; 1426 } 1427 } 1428 1429char * group = NULL ; 1430if (group_tv == NULL ) { 1431di = tv_dict_find (dict ,"group" ,-1 ); 1432if (di != NULL ) { 1433group_tv = & di -> di_tv ; 1434 } 1435 } 1436if (group_tv != NULL ) { 1437group = (char * )tv_get_string_chk (group_tv ); 1438if (group == NULL ) { 1439return -1 ; 1440 } 1441if (group [0 ]== NUL ) { 1442group = NULL ; 1443 } 1444 } 1445 1446char * name = NULL ; 1447if (name_tv == NULL ) { 1448di = tv_dict_find (dict ,"name" ,-1 ); 1449if (di != NULL ) { 1450name_tv = & di -> di_tv ; 1451 } 1452 } 1453if (name_tv == NULL ) { 1454return -1 ; 1455 } 1456name = (char * )tv_get_string_chk (name_tv ); 1457if (name == NULL ) { 1458return -1 ; 1459 } 1460 1461if (buf_tv == NULL ) { 1462di = tv_dict_find (dict ,"buffer" ,-1 ); 1463if (di != NULL ) { 1464buf_tv = & di -> di_tv ; 1465 } 1466 } 1467if (buf_tv == NULL ) { 1468return -1 ; 1469 } 1470buf_T * buf = get_buf_arg (buf_tv ); 1471if (buf == NULL ) { 1472return -1 ; 1473 } 1474 1475linenr_T lnum = 0 ; 1476di = tv_dict_find (dict ,"lnum" ,-1 ); 1477if (di != NULL ) { 1478lnum = tv_get_lnum (& di -> di_tv ); 1479if (lnum <=0 ) { 1480emsg (_ (e_invarg )); 1481return -1 ; 1482 } 1483 } 1484 1485int prio = -1 ; 1486di = tv_dict_find (dict ,"priority" ,-1 ); 1487if (di != NULL ) { 1488prio = (int )tv_get_number_chk (& di -> di_tv ,& notanum ); 1489if (notanum ) { 1490return -1 ; 1491 } 1492 } 1493 1494uint32_t uid = (uint32_t )id ; 1495if (sign_place (& uid ,group ,name ,buf ,lnum ,prio )== OK ) { 1496return (int )uid ; 1497 } 1498 1499return -1 ; 1500} 1501 1502/// "sign_place()" function 1503void f_sign_place (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1504{ 1505dict_T * dict = NULL ; 1506 1507rettv -> vval .v_number = -1 ; 1508 1509if (argvars [4 ].v_type != VAR_UNKNOWN ) { 1510if (tv_check_for_nonnull_dict_arg (argvars ,4 )== FAIL ) { 1511return ; 1512 } 1513dict = argvars [4 ].vval .v_dict ; 1514 } 1515 1516rettv -> vval .v_number = sign_place_from_dict (& argvars [0 ],& argvars [1 ], 1517& argvars [2 ],& argvars [3 ],dict ); 1518} 1519 1520/// "sign_placelist()" function. Place multiple signs. 1521void f_sign_placelist (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1522{ 1523tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1524 1525if (argvars [0 ].v_type != VAR_LIST ) { 1526emsg (_ (e_listreq )); 1527return ; 1528 } 1529 1530// Process the List of sign attributes 1531TV_LIST_ITER_CONST (argvars [0 ].vval .v_list ,li , { 1532int sign_id = -1 ; 1533if (TV_LIST_ITEM_TV (li )-> v_type == VAR_DICT ) { 1534sign_id = sign_place_from_dict (NULL ,NULL ,NULL ,NULL ,TV_LIST_ITEM_TV (li )-> vval .v_dict ); 1535 }else { 1536emsg (_ (e_dictreq )); 1537 } 1538tv_list_append_number (rettv -> vval .v_list ,sign_id ); 1539 }); 1540} 1541 1542/// Undefine multiple signs 1543static void sign_undefine_multiple (list_T * l ,list_T * retlist ) 1544{ 1545TV_LIST_ITER_CONST (l ,li , { 1546int retval = -1 ; 1547char * name = (char * )tv_get_string_chk (TV_LIST_ITEM_TV (li )); 1548if (name != NULL && (sign_undefine_by_name (name )== OK )) { 1549retval = 0 ; 1550 } 1551tv_list_append_number (retlist ,retval ); 1552 }); 1553} 1554 1555/// "sign_undefine()" function 1556void f_sign_undefine (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1557{ 1558if (argvars [0 ].v_type == VAR_LIST && argvars [1 ].v_type == VAR_UNKNOWN ) { 1559// Undefine multiple signs 1560tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1561 1562sign_undefine_multiple (argvars [0 ].vval .v_list ,rettv -> vval .v_list ); 1563return ; 1564 } 1565 1566rettv -> vval .v_number = -1 ; 1567 1568if (argvars [0 ].v_type == VAR_UNKNOWN ) { 1569// Free all the signs 1570free_signs (); 1571rettv -> vval .v_number = 0 ; 1572 }else { 1573// Free only the specified sign 1574const char * name = tv_get_string_chk (& argvars [0 ]); 1575if (name == NULL ) { 1576return ; 1577 } 1578 1579if (sign_undefine_by_name (name )== OK ) { 1580rettv -> vval .v_number = 0 ; 1581 } 1582 } 1583} 1584 1585/// Unplace the sign with attributes specified in 'dict'. Returns 0 on success 1586/// and -1 on failure. 1587static int sign_unplace_from_dict (typval_T * group_tv ,dict_T * dict ) 1588{ 1589dictitem_T * di ; 1590int id = 0 ; 1591buf_T * buf = NULL ; 1592char * group = (group_tv != NULL ) ? (char * )tv_get_string (group_tv ) 1593 :tv_dict_get_string (dict ,"group" , false); 1594if (group != NULL && group [0 ]== NUL ) { 1595group = NULL ; 1596 } 1597 1598if (dict != NULL ) { 1599if ((di = tv_dict_find (dict ,"buffer" ,-1 ))!= NULL ) { 1600buf = get_buf_arg (& di -> di_tv ); 1601if (buf == NULL ) { 1602return -1 ; 1603 } 1604 } 1605if (tv_dict_find (dict ,"id" ,-1 )!= NULL ) { 1606id = (int )tv_dict_get_number (dict ,"id" ); 1607if (id <=0 ) { 1608emsg (_ (e_invarg )); 1609return -1 ; 1610 } 1611 } 1612 } 1613 1614return sign_unplace (buf ,id ,group ,0 )- 1 ; 1615} 1616 1617/// "sign_unplace()" function 1618void f_sign_unplace (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1619{ 1620dict_T * dict = NULL ; 1621 1622rettv -> vval .v_number = -1 ; 1623 1624if (tv_check_for_string_arg (argvars ,0 )== FAIL 1625|| tv_check_for_opt_dict_arg (argvars ,1 )== FAIL ) { 1626return ; 1627 } 1628 1629if (argvars [1 ].v_type != VAR_UNKNOWN ) { 1630dict = argvars [1 ].vval .v_dict ; 1631 } 1632 1633rettv -> vval .v_number = sign_unplace_from_dict (& argvars [0 ],dict ); 1634} 1635 1636/// "sign_unplacelist()" function 1637void f_sign_unplacelist (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1638{ 1639tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1640 1641if (argvars [0 ].v_type != VAR_LIST ) { 1642emsg (_ (e_listreq )); 1643return ; 1644 } 1645 1646TV_LIST_ITER_CONST (argvars [0 ].vval .v_list ,li , { 1647int retval = -1 ; 1648if (TV_LIST_ITEM_TV (li )-> v_type == VAR_DICT ) { 1649retval = sign_unplace_from_dict (NULL ,TV_LIST_ITEM_TV (li )-> vval .v_dict ); 1650 }else { 1651emsg (_ (e_dictreq )); 1652 } 1653tv_list_append_number (rettv -> vval .v_list ,retval ); 1654 }); 1655}