robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
43.8 KiB1655 linesraw
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
71  NULL
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{
78  if (group == NULL) {
79    return 0;           // Global namespace
80  } else if (strcmp(group, "*") == 0) {
81    return UINT32_MAX;  // All namespaces
82  }
83  // Specific or non-existing namespace
84  int ns = map_get(String, int)(&namespace_ids, cstr_as_string(group));
85  return ns ? ns : -1;
86}
87
88static const char *sign_get_name(DecorSignHighlight *sh)
89{
90  char *name = sh->sign_name;
91  return !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{
104  if (group && !map_get(String, int)(&namespace_ids, cstr_as_string(group))) {
105    kv_push(sign_ns, nvim_create_namespace(cstr_as_string(group)));
106  }
107
108  uint32_t ns = group ? (uint32_t)nvim_create_namespace(cstr_as_string(group)) : 0;
109  DecorSignHighlight sign = DECOR_SIGN_HIGHLIGHT_INIT;
110
111  sign.flags |= kSHIsSign;
112  memcpy(sign.text, sp->sn_text, SIGN_WIDTH * sizeof(schar_T));
113  sign.sign_name = xstrdup(sp->sn_name);
114  sign.hl_id = sp->sn_text_hl;
115  sign.line_hl_id = sp->sn_line_hl;
116  sign.number_hl_id = sp->sn_num_hl;
117  sign.cursorline_hl_id = sp->sn_cul_hl;
118  sign.priority = (DecorPriority)prio;
119
120  bool has_hl = (sp->sn_line_hl || sp->sn_num_hl || sp->sn_cul_hl);
121  uint16_t decor_flags = (sp->sn_text[0] ? MT_FLAG_DECOR_SIGNTEXT : 0)
122                         | (has_hl ? MT_FLAG_DECOR_SIGNHL : 0);
123
124  DecorInline decor = { .ext = true, .data.ext = { .vt = NULL, .sh_idx = decor_put_sh(sign) } };
125  extmark_set(buf, ns, id, MIN(buf->b_ml.ml_line_count, lnum) - 1, 0, -1, -1,
126              decor, 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{
139  int64_t ns = group_get_ns(group);
140  if (ns < 0 || (group && ns == 0)) {
141    return 0;
142  }
143
144  MTKey mark = marktree_lookup_ns(buf->b_marktree, (uint32_t)ns, *id, false, NULL);
145  if (mark.pos.row >= 0) {
146    buf_set_sign(buf, id, group, prio, mark.pos.row + 1, sp);
147  }
148  return 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{
160  int64_t ns = group_get_ns(group);
161  if (ns < 0 || (group && ns == 0)) {
162    return 0;
163  }
164  return 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{
170  const MTKey *s1 = (MTKey *)p1;
171  const MTKey *s2 = (MTKey *)p2;
172
173  if (s1->pos.row != s2->pos.row) {
174    return s1->pos.row > s2->pos.row ? 1 : -1;
175  }
176
177  DecorSignHighlight *sh1 = decor_find_sign(mt_decor(*s1));
178  DecorSignHighlight *sh2 = decor_find_sign(mt_decor(*s2));
179  assert(sh1 && sh2);
180  SignItem si1 = { sh1, s1->id };
181  SignItem si2 = { sh2, s2->id };
182
183  return 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{
194  int64_t ns = group_get_ns(group);
195  if (ns < 0) {
196    return FAIL;
197  }
198
199  MarkTreeIter itr[1];
200  int row = atlnum > 0 ? atlnum - 1 : 0;
201  kvec_t(MTKey) signs = KV_INITIAL_VALUE;
202  // Store signs at a specific line number to remove one later.
203  if (atlnum > 0) {
204    if (!marktree_itr_get_overlap(buf->b_marktree, row, 0, itr)) {
205      return FAIL;
206    }
207
208    MTPair pair;
209    while (marktree_itr_step_overlap(buf->b_marktree, itr, &pair)) {
210      if ((ns == UINT32_MAX || ns == pair.start.ns) && mt_decor_sign(pair.start)) {
211        kv_push(signs, pair.start);
212      }
213    }
214  } else {
215    marktree_itr_get(buf->b_marktree, 0, 0, itr);
216  }
217
218  while (itr->x) {
219    MTKey mark = marktree_itr_current(itr);
220    if (row && mark.pos.row > row) {
221      break;
222    }
223    if (!mt_end(mark) && mt_decor_sign(mark)
224        && (id == 0 || (int)mark.id == id)
225        && (ns == UINT32_MAX || ns == mark.ns)) {
226      if (atlnum > 0) {
227        kv_push(signs, mark);
228        marktree_itr_next(buf->b_marktree, itr);
229      } else {
230        extmark_del(buf, itr, mark, true);
231      }
232    } else {
233      marktree_itr_next(buf->b_marktree, itr);
234    }
235  }
236
237  // Sort to remove the highest priority sign at a specific line number.
238  if (kv_size(signs)) {
239    qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
240    extmark_del_id(buf, kv_A(signs, 0).ns, kv_A(signs, 0).id);
241    kv_destroy(signs);
242  } else if (atlnum > 0) {
243    return FAIL;
244  }
245
246  return OK;
247}
248
249bool buf_has_signs(const buf_T *buf)
250{
251  return (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{
257  char lbuf[MSG_BUF_LEN];
258  char namebuf[MSG_BUF_LEN];
259  char groupbuf[MSG_BUF_LEN];
260  buf_T *buf = rbuf ? rbuf : firstbuf;
261  int64_t ns = group_get_ns(group);
262
263  msg_puts_title(_("\n--- Signs ---"));
264
265  while (buf != NULL && !got_int) {
266    if (buf_has_signs(buf)) {
267      msg_putchar('\n');
268      const char *fname = buf->b_fname ? buf->b_fname : _("Untitled");
269      snprintf(lbuf, MSG_BUF_LEN, _("Signs for %s:"), fname);
270      msg_puts_hl(lbuf, HLF_D, false);
271    }
272
273    if (ns >= 0) {
274      MarkTreeIter itr[1];
275      kvec_t(MTKey) signs = KV_INITIAL_VALUE;
276      marktree_itr_get(buf->b_marktree, 0, 0, itr);
277
278      while (itr->x) {
279        MTKey mark = marktree_itr_current(itr);
280        if (!mt_end(mark) && mt_decor_sign(mark)
281            && (ns == UINT32_MAX || ns == mark.ns)) {
282          kv_push(signs, mark);
283        }
284        marktree_itr_next(buf->b_marktree, itr);
285      }
286
287      if (kv_size(signs)) {
288        qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
289        msg_putchar('\n');
290
291        for (size_t i = 0; i < kv_size(signs); i++) {
292          namebuf[0] = NUL;
293          groupbuf[0] = NUL;
294          MTKey mark = kv_A(signs, i);
295
296          DecorSignHighlight *sh = decor_find_sign(mt_decor(mark));
297          if (sh->sign_name != NULL) {
298            vim_snprintf(namebuf, MSG_BUF_LEN, _("  name=%s"), sign_get_name(sh));
299          }
300          if (mark.ns != 0) {
301            vim_snprintf(groupbuf, MSG_BUF_LEN, _("  group=%s"), describe_ns((int)mark.ns, ""));
302          }
303          vim_snprintf(lbuf, MSG_BUF_LEN, _("    line=%" PRIdLINENR "  id=%u%s%s  priority=%d"),
304                       mark.pos.row + 1, mark.id, groupbuf, namebuf, sh->priority);
305          msg_puts(lbuf);
306          if (i < kv_size(signs) - 1) {
307            msg_putchar('\n');
308          }
309        }
310        kv_destroy(signs);
311      }
312    }
313
314    if (rbuf != NULL) {
315      return;
316    }
317    buf = 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{
328  int idx;
329  char save = *end_cmd;
330
331  *end_cmd = NUL;
332  for (idx = 0;; idx++) {
333    if (cmds[idx] == NULL || strcmp(begin_cmd, cmds[idx]) == 0) {
334      break;
335    }
336  }
337  *end_cmd = save;
338  return 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{
344  size_t p = 0;
345  for (int i = 0; i < SIGN_WIDTH; i++) {
346    schar_get(buf + p, sign_text[i]);
347    size_t len = strlen(buf + p);
348    if (len == 0) {
349      break;
350    }
351    p += len;
352  }
353  return 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{
360  char *s;
361  char *endp = text + (int)strlen(text);
362
363  for (s = sp ? text : endp; s + 1 < endp; s++) {
364    if (*s == '\\') {
365      // Remove a backslash, so that it is possible to use a space.
366      STRMOVE(s, s + 1);
367      endp--;
368    }
369  }
370  // Count cells and check for non-printable chars
371  int cells = 0;
372  for (s = text; s < endp; s += utfc_ptr2len(s)) {
373    int c;
374    sign_text[cells] = utfc_ptr2schar(s, &c);
375    if (!vim_isprintc(c)) {
376      break;
377    }
378    int width = utf_ptr2cells(s);
379    if (width == 2) {
380      sign_text[cells + 1] = 0;
381    }
382    cells += width;
383  }
384  // Currently must be empty, one or two display cells
385  if (s != endp || cells > SIGN_WIDTH) {
386    if (sp != NULL) {
387      semsg(_("E239: Invalid sign text: %s"), text);
388    }
389    return FAIL;
390  }
391
392  if (cells < 1) {
393    sign_text[0] = 0;
394  } else if (cells == 1) {
395    sign_text[1] = schar_from_ascii(' ');
396  }
397
398  return 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,
403                               char *culhl, char *numhl, int prio)
404{
405  cstr_t *key;
406  bool new_sign = false;
407  sign_T **sp = (sign_T **)pmap_put_ref(cstr_t)(&sign_map, name, &key, &new_sign);
408
409  if (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.
416  if (icon != NULL) {
417    /// Initialize the icon information for a new sign
418    xfree((*sp)->sn_icon);
419    (*sp)->sn_icon = xstrdup(icon);
420    backslash_halve((*sp)->sn_icon);
421  }
422
423  if (text != NULL && (init_sign_text(*sp, (*sp)->sn_text, text) == FAIL)) {
424    return FAIL;
425  }
426
427  (*sp)->sn_priority = prio;
428
429  char *arg[] = { linehl, texthl, culhl, numhl };
430  int *hl[] = { &(*sp)->sn_line_hl, &(*sp)->sn_text_hl, &(*sp)->sn_cul_hl, &(*sp)->sn_num_hl };
431  for (int i = 0; i < 4; i++) {
432    if (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.
438  if (!new_sign) {
439    bool did_redraw = false;
440    for (size_t i = 0; i < kv_size(decor_items); i++) {
441      DecorSignHighlight *sh = &kv_A(decor_items, i);
442      if (sh->sign_name && strcmp(sh->sign_name, name) == 0) {
443        memcpy(sh->text, (*sp)->sn_text, SIGN_WIDTH * sizeof(schar_T));
444        sh->hl_id = (*sp)->sn_text_hl;
445        sh->line_hl_id = (*sp)->sn_line_hl;
446        sh->number_hl_id = (*sp)->sn_num_hl;
447        sh->cursorline_hl_id = (*sp)->sn_cul_hl;
448        if (!did_redraw) {
449          FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
450            if (buf_has_signs(wp->w_buffer)) {
451              redraw_buf_later(wp->w_buffer, UPD_NOT_VALID);
452            }
453          }
454          did_redraw = true;
455        }
456      }
457    }
458  }
459  return OK;
460}
461
462/// Free the sign specified by 'name'.
463static int sign_undefine_by_name(const char *name)
464{
465  sign_T *sp = pmap_del(cstr_t)(&sign_map, name, NULL);
466  if (sp == NULL) {
467    semsg(_("E155: Unknown sign: %s"), name);
468    return FAIL;
469  }
470
471  xfree(sp->sn_name);
472  xfree(sp->sn_icon);
473  xfree(sp);
474  return OK;
475}
476
477/// List one sign.
478static void sign_list_defined(sign_T *sp)
479{
480  smsg(0, "sign %s", sp->sn_name);
481  if (sp->sn_icon != NULL) {
482    msg_puts(" icon=");
483    msg_outtrans(sp->sn_icon, 0, false);
484    msg_puts(_(" (not supported)"));
485  }
486  if (sp->sn_text[0]) {
487    msg_puts(" text=");
488    char buf[SIGN_WIDTH * MAX_SCHAR_SIZE];
489    describe_sign_text(buf, sp->sn_text);
490    msg_outtrans(buf, 0, false);
491  }
492  if (sp->sn_priority > 0) {
493    char lbuf[MSG_BUF_LEN];
494    vim_snprintf(lbuf, MSG_BUF_LEN, " priority=%d", sp->sn_priority);
495    msg_puts(lbuf);
496  }
497  static char *arg[] = { " linehl=", " texthl=", " culhl=", " numhl=" };
498  int hl[] = { sp->sn_line_hl, sp->sn_text_hl, sp->sn_cul_hl, sp->sn_num_hl };
499  for (int i = 0; i < 4; i++) {
500    if (hl[i] > 0) {
501      msg_puts(arg[i]);
502      const char *p = get_highlight_name_ext(NULL, hl[i] - 1, false);
503      msg_puts(p ? p : "NONE");
504    }
505  }
506}
507
508/// List the signs matching 'name'
509static void sign_list_by_name(char *name)
510{
511  sign_T *sp = pmap_get(cstr_t)(&sign_map, name);
512  if (sp != NULL) {
513    sign_list_defined(sp);
514  } else {
515    semsg(_("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
523  if (group != NULL && (*group == '*' || *group == NUL)) {
524    return FAIL;
525  }
526
527  sign_T *sp = pmap_get(cstr_t)(&sign_map, name);
528  if (sp == NULL) {
529    semsg(_("E155: Unknown sign: %s"), name);
530    return FAIL;
531  }
532
533  // Use the default priority value for this sign.
534  if (prio == -1) {
535    prio = (sp->sn_priority != -1) ? sp->sn_priority : SIGN_DEF_PRIO;
536  }
537
538  if (lnum > 0) {
539    // ":sign place {id} line={lnum} name={name} file={fname}": place a sign
540    buf_set_sign(buf, id, group, prio, lnum, sp);
541  } else {
542    // ":sign place {id} file={fname}": change sign type and/or priority
543    lnum = buf_mod_sign(buf, id, group, prio, sp);
544  }
545  if (lnum <= 0) {
546    semsg(_("E885: Not possible to change sign %s"), name);
547    return FAIL;
548  }
549
550  return OK;
551}
552
553static int sign_unplace_inner(buf_T *buf, int id, char *group, linenr_T atlnum)
554{
555  if (!buf_has_signs(buf)) {  // No signs in the buffer
556    return FAIL;
557  }
558
559  if (id == 0 || atlnum > 0 || (group != NULL && *group == '*')) {
560    // Delete multiple specified signs
561    if (!buf_delete_signs(buf, group, id, atlnum)) {
562      return FAIL;
563    }
564  } else {
565    // Delete only a single sign
566    int64_t ns = group_get_ns(group);
567    if (ns < 0 || !extmark_del_id(buf, (uint32_t)ns, (uint32_t)id)) {
568      return FAIL;
569    }
570  }
571
572  return 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{
578  if (buf != NULL) {
579    return sign_unplace_inner(buf, id, group, atlnum);
580  } else {
581    int retval = OK;
582    FOR_ALL_BUFFERS(cbuf) {
583      if (!sign_unplace_inner(cbuf, id, group, atlnum)) {
584        retval = FAIL;
585      }
586    }
587    return retval;
588  }
589}
590
591/// Jump to a sign.
592static linenr_T sign_jump(int id, char *group, buf_T *buf)
593{
594  linenr_T lnum = buf_findsign(buf, id, group);
595
596  if (lnum <= 0) {
597    semsg(_("E157: Invalid sign ID: %d"), id);
598    return -1;
599  }
600
601  // goto a sign ...
602  if (buf_jump_open_win(buf) != NULL) {     // ... in a current window
603    curwin->w_cursor.lnum = lnum;
604    check_cursor_lnum(curwin);
605    beginline(BL_WHITE);
606  } else {      // ... not currently in a window
607    if (buf->b_fname == NULL) {
608      emsg(_("E934: Cannot jump to a buffer that does not have a name"));
609      return -1;
610    }
611    setpcmark();
612    if (do_ecmd(buf->b_fnum, NULL, NULL, NULL, lnum,
613                buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == FAIL) {
614      return -1;
615    }
616  }
617
618  foldOpenCursor();
619
620  return lnum;
621}
622
623/// ":sign define {name} ..." command
624static void sign_define_cmd(char *name, char *cmdline)
625{
626  char *icon = NULL;
627  char *text = NULL;
628  char *linehl = NULL;
629  char *texthl = NULL;
630  char *culhl = NULL;
631  char *numhl = NULL;
632  int prio = -1;
633
634  // set values for a defined sign.
635  while (true) {
636    char *arg = skipwhite(cmdline);
637    if (*arg == NUL) {
638      break;
639    }
640    cmdline = skiptowhite_esc(arg);
641    if (strncmp(arg, "icon=", 5) == 0) {
642      icon = arg + 5;
643    } else if (strncmp(arg, "text=", 5) == 0) {
644      text = arg + 5;
645    } else if (strncmp(arg, "linehl=", 7) == 0) {
646      linehl = arg + 7;
647    } else if (strncmp(arg, "texthl=", 7) == 0) {
648      texthl = arg + 7;
649    } else if (strncmp(arg, "culhl=", 6) == 0) {
650      culhl = arg + 6;
651    } else if (strncmp(arg, "numhl=", 6) == 0) {
652      numhl = arg + 6;
653    } else if (strncmp(arg, "priority=", 9) == 0) {
654      prio = atoi(arg + 9);
655    } else {
656      semsg(_(e_invarg2), arg);
657      return;
658    }
659    if (*cmdline == NUL) {
660      break;
661    }
662    *cmdline++ = NUL;
663  }
664
665  sign_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{
671  if (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=*
682    if (lnum >= 0 || name != NULL || (group != NULL && *group == NUL)) {
683      emsg(_(e_invarg));
684    } else {
685      sign_list_placed(buf, group);
686    }
687  } else {
688    // Place a new sign
689    if (name == NULL || buf == NULL || (group != NULL && *group == NUL)) {
690      emsg(_(e_invarg));
691      return;
692    }
693    uint32_t uid = (uint32_t)id;
694    sign_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{
701  if (lnum >= 0 || name != NULL || (group != NULL && *group == NUL)) {
702    emsg(_(e_invarg));
703    return;
704  }
705
706  if (id == -1) {
707    lnum = curwin->w_cursor.lnum;
708    buf = curwin->w_buffer;
709  }
710
711  if (!sign_unplace(buf, MAX(0, id), group, lnum) && lnum > 0) {
712    emsg(_("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{
723  if (name == NULL && group == NULL && id == -1) {
724    emsg(_(e_argreq));
725    return;
726  }
727
728  if (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.
731    emsg(_(e_invarg));
732    return;
733  }
734
735  sign_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,
743                               buf_T **buf, linenr_T *lnum)
744{
745  char *arg1 = arg;
746  char *filename = NULL;
747  bool lnum_arg = false;
748
749  // first arg could be placed sign id
750  if (ascii_isdigit(*arg)) {
751    *id = getdigits_int(&arg, true, 0);
752    if (!ascii_iswhite(*arg) && *arg != NUL) {
753      *id = -1;
754      arg = arg1;
755    } else {
756      arg = skipwhite(arg);
757    }
758  }
759
760  while (*arg != NUL) {
761    if (strncmp(arg, "line=", 5) == 0) {
762      arg += 5;
763      *lnum = atoi(arg);
764      arg = skiptowhite(arg);
765      lnum_arg = true;
766    } else if (strncmp(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE) {
767      if (*id != -1) {
768        emsg(_(e_invarg));
769        return FAIL;
770      }
771      *id = -2;
772      arg = skiptowhite(arg + 1);
773    } else if (strncmp(arg, "name=", 5) == 0) {
774      arg += 5;
775      char *namep = arg;
776      arg = skiptowhite(arg);
777      if (*arg != NUL) {
778        *arg++ = NUL;
779      }
780      while (namep[0] == '0' && namep[1] != NUL) {
781        namep++;
782      }
783      *name = namep;
784    } else if (strncmp(arg, "group=", 6) == 0) {
785      arg += 6;
786      *group = arg;
787      arg = skiptowhite(arg);
788      if (*arg != NUL) {
789        *arg++ = NUL;
790      }
791    } else if (strncmp(arg, "priority=", 9) == 0) {
792      arg += 9;
793      *prio = atoi(arg);
794      arg = skiptowhite(arg);
795    } else if (strncmp(arg, "file=", 5) == 0) {
796      arg += 5;
797      filename = arg;
798      *buf = buflist_findname_exp(arg);
799      break;
800    } else if (strncmp(arg, "buffer=", 7) == 0) {
801      arg += 7;
802      filename = arg;
803      *buf = buflist_findnr(getdigits_int(&arg, true, 0));
804      if (*skipwhite(arg) != NUL) {
805        semsg(_(e_trailing_arg), arg);
806      }
807      break;
808    } else {
809      emsg(_(e_invarg));
810      return FAIL;
811    }
812    arg = skipwhite(arg);
813  }
814
815  if (filename != NULL && *buf == NULL) {
816    semsg(_(e_invalid_buffer_name_str), filename);
817    return 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.
822  if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg) || cmd == SIGNCMD_JUMP)) {
823    *buf = curwin->w_buffer;
824  }
825  return OK;
826}
827
828/// ":sign" command
829void ex_sign(exarg_T *eap)
830{
831  char *arg = eap->arg;
832
833  // Parse the subcommand.
834  char *p = skiptowhite(arg);
835  int idx = sign_cmd_idx(arg, p);
836  if (idx == SIGNCMD_LAST) {
837    semsg(_("E160: Unknown sign command: %s"), arg);
838    return;
839  }
840  arg = skipwhite(p);
841
842  if (idx <= SIGNCMD_LIST) {
843    // Define, undefine or list signs.
844    if (idx == SIGNCMD_LIST && *arg == NUL) {
845      // ":sign list": list all defined signs
846      sign_T *sp;
847      map_foreach_value(&sign_map, sp, {
848        sign_list_defined(sp);
849      });
850    } else if (*arg == NUL) {
851      emsg(_("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".
855      p = skiptowhite(arg);
856      if (*p != NUL) {
857        *p++ = NUL;
858      }
859      while (arg[0] == '0' && arg[1] != NUL) {
860        arg++;
861      }
862
863      if (idx == SIGNCMD_DEFINE) {
864        sign_define_cmd(arg, p);
865      } else if (idx == SIGNCMD_LIST) {
866        // ":sign list {name}"
867        sign_list_by_name(arg);
868      } else {
869        // ":sign undefine {name}"
870        sign_undefine_by_name(arg);
871      }
872
873      return;
874    }
875  } else {
876    int id = -1;
877    linenr_T lnum = -1;
878    char *name = NULL;
879    char *group = NULL;
880    int prio = -1;
881    buf_T *buf = NULL;
882
883    // Parse command line arguments
884    if (parse_sign_cmd_args(idx, arg, &name, &id, &group, &prio, &buf, &lnum) == FAIL) {
885      return;
886    }
887
888    if (idx == SIGNCMD_PLACE) {
889      sign_place_cmd(buf, lnum, name, id, group, prio);
890    } else if (idx == SIGNCMD_UNPLACE) {
891      sign_unplace_cmd(buf, lnum, name, id, group);
892    } else if (idx == SIGNCMD_JUMP) {
893      sign_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{
901  dict_T *d = tv_dict_alloc();
902
903  tv_dict_add_str(d, S_LEN("name"), sp->sn_name);
904
905  if (sp->sn_icon != NULL) {
906    tv_dict_add_str(d, S_LEN("icon"), sp->sn_icon);
907  }
908  if (sp->sn_text[0]) {
909    char buf[SIGN_WIDTH * MAX_SCHAR_SIZE];
910    describe_sign_text(buf, sp->sn_text);
911    tv_dict_add_str(d, S_LEN("text"), buf);
912  }
913  if (sp->sn_priority > 0) {
914    tv_dict_add_nr(d, S_LEN("priority"), sp->sn_priority);
915  }
916  static char *arg[] = { "linehl", "texthl", "culhl", "numhl" };
917  int hl[] = { sp->sn_line_hl, sp->sn_text_hl, sp->sn_cul_hl, sp->sn_num_hl };
918  for (int i = 0; i < 4; i++) {
919    if (hl[i] > 0) {
920      const char *p = get_highlight_name_ext(NULL, hl[i] - 1, false);
921      if (p == NULL) {
922        tv_dict_add_str_len(d, arg[i], strlen(arg[i]), S_LEN("NONE"));
923      } else {
924        tv_dict_add_str(d, arg[i], strlen(arg[i]), p);
925      }
926    }
927  }
928  return d;
929}
930
931/// Get dictionary of information for placed sign "mark"
932static dict_T *sign_get_placed_info_dict(MTKey mark)
933{
934  dict_T *d = tv_dict_alloc();
935
936  DecorSignHighlight *sh = decor_find_sign(mt_decor(mark));
937
938  tv_dict_add_str(d, S_LEN("name"), sign_get_name(sh));
939  tv_dict_add_nr(d,  S_LEN("id"), (int)mark.id);
940  tv_dict_add_str(d, S_LEN("group"), describe_ns((int)mark.ns, ""));
941  tv_dict_add_nr(d,  S_LEN("lnum"), mark.pos.row + 1);
942  tv_dict_add_nr(d,  S_LEN("priority"), sh->priority);
943  return d;
944}
945
946/// Returns information about signs placed in a buffer as list of dicts.
947list_T *get_buffer_signs(buf_T *buf)
948  FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
949{
950  list_T *const l = tv_list_alloc(kListLenMayKnow);
951  MarkTreeIter itr[1];
952  marktree_itr_get(buf->b_marktree, 0, 0, itr);
953
954  while (itr->x) {
955    MTKey mark = marktree_itr_current(itr);
956    if (!mt_end(mark) && mt_decor_sign(mark)) {
957      tv_list_append_dict(l, sign_get_placed_info_dict(mark));
958    }
959    marktree_itr_next(buf->b_marktree, itr);
960  }
961
962  return 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,
967                                   list_T *retlist)
968{
969  dict_T *d = tv_dict_alloc();
970  tv_list_append_dict(retlist, d);
971
972  tv_dict_add_nr(d, S_LEN("bufnr"), buf->b_fnum);
973
974  list_T *l = tv_list_alloc(kListLenMayKnow);
975  tv_dict_add_list(d, S_LEN("signs"), l);
976
977  int64_t ns = group_get_ns(group);
978  if (!buf_has_signs(buf) || ns < 0) {
979    return;
980  }
981
982  MarkTreeIter itr[1];
983  kvec_t(MTKey) signs = KV_INITIAL_VALUE;
984  marktree_itr_get(buf->b_marktree, lnum ? lnum - 1 : 0, 0, itr);
985
986  while (itr->x) {
987    MTKey mark = marktree_itr_current(itr);
988    if (lnum && mark.pos.row >= lnum) {
989      break;
990    }
991    if (!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))) {
997      if (mt_decor_sign(mark)) {
998        kv_push(signs, mark);
999      }
1000    }
1001    marktree_itr_next(buf->b_marktree, itr);
1002  }
1003
1004  if (kv_size(signs)) {
1005    qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
1006    for (size_t i = 0; i < kv_size(signs); i++) {
1007      tv_list_append_dict(l, sign_get_placed_info_dict(kv_A(signs, i)));
1008    }
1009    kv_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{
1018  if (buf != NULL) {
1019    sign_get_placed_in_buf(buf, lnum, id, group, retlist);
1020  } else {
1021    FOR_ALL_BUFFERS(cbuf) {
1022      if (buf_has_signs(cbuf)) {
1023        sign_get_placed_in_buf(cbuf, 0, id, group, retlist);
1024      }
1025    }
1026  }
1027}
1028
1029void free_signs(void)
1030{
1031  cstr_t name;
1032  kvec_t(cstr_t) names = KV_INITIAL_VALUE;
1033  map_foreach_key(&sign_map, name, {
1034    kv_push(names, name);
1035  });
1036  for (size_t i = 0; i < kv_size(names); i++) {
1037    sign_undefine_by_name(kv_A(names, i));
1038  }
1039  kv_destroy(names);
1040}
1041
1042static enum {
1043  EXP_SUBCMD,   // expand :sign sub-commands
1044  EXP_DEFINE,   // expand :sign define {name} args
1045  EXP_PLACE,    // expand :sign place {id} args
1046  EXP_LIST,     // expand :sign place args
1047  EXP_UNPLACE,  // expand :sign unplace"
1048  EXP_SIGN_NAMES,   // expand with name of placed signs
1049  EXP_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)
1054  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
1055{
1056  // Complete with name of signs already defined
1057  cstr_t name;
1058  int current_idx = 0;
1059  map_foreach_key(&sign_map, name, {
1060    if (current_idx++ == idx) {
1061      return (char *)name;
1062    }
1063  });
1064  return 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
1071  if (idx < (int)kv_size(sign_ns)) {
1072    return (char *)describe_ns((NS)kv_A(sign_ns, idx), "");
1073  }
1074  return NULL;
1075}
1076
1077/// Function given to ExpandGeneric() to obtain the sign command expansion.
1078char *get_sign_name(expand_T *xp, int idx)
1079{
1080  switch (expand_what) {
1081  case EXP_SUBCMD:
1082    return cmds[idx];
1083  case EXP_DEFINE: {
1084    char *define_arg[] = { "culhl=", "icon=", "linehl=", "numhl=", "text=", "texthl=",
1085                           "priority=", NULL };
1086    return define_arg[idx];
1087  }
1088  case EXP_PLACE: {
1089    char *place_arg[] = { "line=", "name=", "group=", "priority=", "file=", "buffer=", NULL };
1090    return place_arg[idx];
1091  }
1092  case EXP_LIST: {
1093    char *list_arg[] = { "group=", "file=", "buffer=", NULL };
1094    return list_arg[idx];
1095  }
1096  case EXP_UNPLACE: {
1097    char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1098    return unplace_arg[idx];
1099  }
1100  case EXP_SIGN_NAMES:
1101    return get_nth_sign_name(idx);
1102  case EXP_SIGN_GROUPS:
1103    return get_nth_sign_group_name(idx);
1104  default:
1105    return 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.
1113  xp->xp_context = EXPAND_SIGN;
1114  expand_what = EXP_SUBCMD;
1115  xp->xp_pattern = arg;
1116
1117  char *end_subcmd = skiptowhite(arg);
1118  if (*end_subcmd == NUL) {
1119    // expand subcmd name
1120    // :sign {subcmd}<CTRL-D>
1121    return;
1122  }
1123
1124  int cmd_idx = sign_cmd_idx(arg, end_subcmd);
1125
1126  // :sign {subcmd} {subcmd_args}
1127  //                |
1128  //                begin_subcmd_args
1129  char *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.
1138  char *last;
1139  char *p = begin_subcmd_args;
1140  do {
1141    p = skipwhite(p);
1142    last = p;
1143    p = skiptowhite(p);
1144  } while (*p != NUL);
1145
1146  p = vim_strchr(last, '=');
1147
1148  // :sign define {name} {args}... {last}=
1149  //                               |     |
1150  //                            last     p
1151  if (p == NULL) {
1152    // Expand last argument name (before equal sign).
1153    xp->xp_pattern = last;
1154    switch (cmd_idx) {
1155    case SIGNCMD_DEFINE:
1156      expand_what = EXP_DEFINE;
1157      break;
1158    case SIGNCMD_PLACE:
1159      // List placed signs
1160      if (ascii_isdigit(*begin_subcmd_args)) {
1161        //   :sign place {id} {args}...
1162        expand_what = EXP_PLACE;
1163      } else {
1164        //   :sign place {args}...
1165        expand_what = EXP_LIST;
1166      }
1167      break;
1168    case SIGNCMD_LIST:
1169    case SIGNCMD_UNDEFINE:
1170      // :sign list <CTRL-D>
1171      // :sign undefine <CTRL-D>
1172      expand_what = EXP_SIGN_NAMES;
1173      break;
1174    case SIGNCMD_JUMP:
1175    case SIGNCMD_UNPLACE:
1176      expand_what = EXP_UNPLACE;
1177      break;
1178    default:
1179      xp->xp_context = EXPAND_NOTHING;
1180    }
1181  } else {
1182    // Expand last argument value (after equal sign).
1183    xp->xp_pattern = p + 1;
1184    switch (cmd_idx) {
1185    case SIGNCMD_DEFINE:
1186      if (strncmp(last, "texthl", 6) == 0
1187          || strncmp(last, "linehl", 6) == 0
1188          || strncmp(last, "culhl", 5) == 0
1189          || strncmp(last, "numhl", 5) == 0) {
1190        xp->xp_context = EXPAND_HIGHLIGHT;
1191      } else if (strncmp(last, "icon", 4) == 0) {
1192        xp->xp_context = EXPAND_FILES;
1193      } else {
1194        xp->xp_context = EXPAND_NOTHING;
1195      }
1196      break;
1197    case SIGNCMD_PLACE:
1198      if (strncmp(last, "name", 4) == 0) {
1199        expand_what = EXP_SIGN_NAMES;
1200      } else if (strncmp(last, "group", 5) == 0) {
1201        expand_what = EXP_SIGN_GROUPS;
1202      } else if (strncmp(last, "file", 4) == 0) {
1203        xp->xp_context = EXPAND_BUFFERS;
1204      } else {
1205        xp->xp_context = EXPAND_NOTHING;
1206      }
1207      break;
1208    case SIGNCMD_UNPLACE:
1209    case SIGNCMD_JUMP:
1210      if (strncmp(last, "group", 5) == 0) {
1211        expand_what = EXP_SIGN_GROUPS;
1212      } else if (strncmp(last, "file", 4) == 0) {
1213        xp->xp_context = EXPAND_BUFFERS;
1214      } else {
1215        xp->xp_context = EXPAND_NOTHING;
1216      }
1217      break;
1218    default:
1219      xp->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{
1228  if (name == NULL) {
1229    name = tv_dict_get_string(dict, "name", false);
1230    if (name == NULL || name[0] == NUL) {
1231      return -1;
1232    }
1233  }
1234
1235  char *icon = NULL;
1236  char *linehl = NULL;
1237  char *text = NULL;
1238  char *texthl = NULL;
1239  char *culhl = NULL;
1240  char *numhl = NULL;
1241  int prio = -1;
1242
1243  if (dict != NULL) {
1244    icon = tv_dict_get_string(dict, "icon", false);
1245    linehl = tv_dict_get_string(dict, "linehl", false);
1246    text = tv_dict_get_string(dict, "text", false);
1247    texthl = tv_dict_get_string(dict, "texthl", false);
1248    culhl = tv_dict_get_string(dict, "culhl", false);
1249    numhl = tv_dict_get_string(dict, "numhl", false);
1250    prio = (int)tv_dict_get_number_def(dict, "priority", -1);
1251  }
1252
1253  return 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{
1260  TV_LIST_ITER_CONST(l, li, {
1261    int retval = -1;
1262    if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
1263      retval = sign_define_from_dict(NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
1264    } else {
1265      emsg(_(e_dictreq));
1266    }
1267    tv_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{
1274  if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_UNKNOWN) {
1275    // Define multiple signs
1276    tv_list_alloc_ret(rettv, kListLenMayKnow);
1277
1278    sign_define_multiple(argvars[0].vval.v_list, rettv->vval.v_list);
1279    return;
1280  }
1281
1282  // Define a single sign
1283  rettv->vval.v_number = -1;
1284
1285  char *name = (char *)tv_get_string_chk(&argvars[0]);
1286  if (name == NULL) {
1287    return;
1288  }
1289
1290  if (tv_check_for_opt_dict_arg(argvars, 1) == FAIL) {
1291    return;
1292  }
1293
1294  dict_T *d = argvars[1].v_type == VAR_DICT ? argvars[1].vval.v_dict : NULL;
1295  rettv->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{
1301  tv_list_alloc_ret(rettv, 0);
1302
1303  if (argvars[0].v_type == VAR_UNKNOWN) {
1304    sign_T *sp;
1305    map_foreach_value(&sign_map, sp, {
1306      tv_list_append_dict(rettv->vval.v_list, sign_get_info_dict(sp));
1307    });
1308  } else {
1309    sign_T *sp = pmap_get(cstr_t)(&sign_map, tv_get_string(&argvars[0]));
1310    if (sp != NULL) {
1311      tv_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{
1319  buf_T *buf = NULL;
1320  linenr_T lnum = 0;
1321  int sign_id = 0;
1322  const char *group = NULL;
1323  bool notanum = false;
1324
1325  tv_list_alloc_ret(rettv, 0);
1326
1327  if (argvars[0].v_type != VAR_UNKNOWN) {
1328    // get signs placed in the specified buffer
1329    buf = get_buf_arg(&argvars[0]);
1330    if (buf == NULL) {
1331      return;
1332    }
1333
1334    if (argvars[1].v_type != VAR_UNKNOWN) {
1335      if (tv_check_for_nonnull_dict_arg(argvars, 1) == FAIL) {
1336        return;
1337      }
1338      dictitem_T *di;
1339      dict_T *dict = argvars[1].vval.v_dict;
1340      if ((di = tv_dict_find(dict, "lnum", -1)) != NULL) {
1341        // get signs placed at this line
1342        lnum = tv_get_lnum(&di->di_tv);
1343        if (lnum <= 0) {
1344          return;
1345        }
1346      }
1347      if ((di = tv_dict_find(dict, "id", -1)) != NULL) {
1348        // get sign placed with this identifier
1349        sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
1350        if (notanum) {
1351          return;
1352        }
1353      }
1354      if ((di = tv_dict_find(dict, "group", -1)) != NULL) {
1355        group = tv_get_string_chk(&di->di_tv);
1356        if (group == NULL) {
1357          return;
1358        }
1359        if (*group == NUL) {  // empty string means global group
1360          group = NULL;
1361        }
1362      }
1363    }
1364  }
1365
1366  sign_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{
1372  rettv->vval.v_number = -1;
1373
1374  // Sign identifier
1375  bool notanum = false;
1376  int id = (int)tv_get_number_chk(&argvars[0], &notanum);
1377  if (notanum) {
1378    return;
1379  }
1380  if (id <= 0) {
1381    emsg(_(e_invarg));
1382    return;
1383  }
1384
1385  // Sign group
1386  char *group = (char *)tv_get_string_chk(&argvars[1]);
1387  if (group == NULL) {
1388    return;
1389  }
1390  if (group[0] == NUL) {
1391    group = NULL;
1392  }
1393
1394  // Buffer to place the sign
1395  buf_T *buf = get_buf_arg(&argvars[2]);
1396  if (buf == NULL) {
1397    return;
1398  }
1399
1400  rettv->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,
1406                                typval_T *buf_tv, dict_T *dict)
1407{
1408  dictitem_T *di;
1409
1410  int id = 0;
1411  bool notanum = false;
1412  if (id_tv == NULL) {
1413    di = tv_dict_find(dict, "id", -1);
1414    if (di != NULL) {
1415      id_tv = &di->di_tv;
1416    }
1417  }
1418  if (id_tv != NULL) {
1419    id = (int)tv_get_number_chk(id_tv, &notanum);
1420    if (notanum) {
1421      return -1;
1422    }
1423    if (id < 0) {
1424      emsg(_(e_invarg));
1425      return -1;
1426    }
1427  }
1428
1429  char *group = NULL;
1430  if (group_tv == NULL) {
1431    di = tv_dict_find(dict, "group", -1);
1432    if (di != NULL) {
1433      group_tv = &di->di_tv;
1434    }
1435  }
1436  if (group_tv != NULL) {
1437    group = (char *)tv_get_string_chk(group_tv);
1438    if (group == NULL) {
1439      return -1;
1440    }
1441    if (group[0] == NUL) {
1442      group = NULL;
1443    }
1444  }
1445
1446  char *name = NULL;
1447  if (name_tv == NULL) {
1448    di = tv_dict_find(dict, "name", -1);
1449    if (di != NULL) {
1450      name_tv = &di->di_tv;
1451    }
1452  }
1453  if (name_tv == NULL) {
1454    return -1;
1455  }
1456  name = (char *)tv_get_string_chk(name_tv);
1457  if (name == NULL) {
1458    return -1;
1459  }
1460
1461  if (buf_tv == NULL) {
1462    di = tv_dict_find(dict, "buffer", -1);
1463    if (di != NULL) {
1464      buf_tv = &di->di_tv;
1465    }
1466  }
1467  if (buf_tv == NULL) {
1468    return -1;
1469  }
1470  buf_T *buf = get_buf_arg(buf_tv);
1471  if (buf == NULL) {
1472    return -1;
1473  }
1474
1475  linenr_T lnum = 0;
1476  di = tv_dict_find(dict, "lnum", -1);
1477  if (di != NULL) {
1478    lnum = tv_get_lnum(&di->di_tv);
1479    if (lnum <= 0) {
1480      emsg(_(e_invarg));
1481      return -1;
1482    }
1483  }
1484
1485  int prio = -1;
1486  di = tv_dict_find(dict, "priority", -1);
1487  if (di != NULL) {
1488    prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
1489    if (notanum) {
1490      return -1;
1491    }
1492  }
1493
1494  uint32_t uid = (uint32_t)id;
1495  if (sign_place(&uid, group, name, buf, lnum, prio) == OK) {
1496    return (int)uid;
1497  }
1498
1499  return -1;
1500}
1501
1502/// "sign_place()" function
1503void f_sign_place(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
1504{
1505  dict_T *dict = NULL;
1506
1507  rettv->vval.v_number = -1;
1508
1509  if (argvars[4].v_type != VAR_UNKNOWN) {
1510    if (tv_check_for_nonnull_dict_arg(argvars, 4) == FAIL) {
1511      return;
1512    }
1513    dict = argvars[4].vval.v_dict;
1514  }
1515
1516  rettv->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{
1523  tv_list_alloc_ret(rettv, kListLenMayKnow);
1524
1525  if (argvars[0].v_type != VAR_LIST) {
1526    emsg(_(e_listreq));
1527    return;
1528  }
1529
1530  // Process the List of sign attributes
1531  TV_LIST_ITER_CONST(argvars[0].vval.v_list, li, {
1532    int sign_id = -1;
1533    if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
1534      sign_id = sign_place_from_dict(NULL, NULL, NULL, NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
1535    } else {
1536      emsg(_(e_dictreq));
1537    }
1538    tv_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{
1545  TV_LIST_ITER_CONST(l, li, {
1546    int retval = -1;
1547    char *name = (char *)tv_get_string_chk(TV_LIST_ITEM_TV(li));
1548    if (name != NULL && (sign_undefine_by_name(name) == OK)) {
1549      retval = 0;
1550    }
1551    tv_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{
1558  if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_UNKNOWN) {
1559    // Undefine multiple signs
1560    tv_list_alloc_ret(rettv, kListLenMayKnow);
1561
1562    sign_undefine_multiple(argvars[0].vval.v_list, rettv->vval.v_list);
1563    return;
1564  }
1565
1566  rettv->vval.v_number = -1;
1567
1568  if (argvars[0].v_type == VAR_UNKNOWN) {
1569    // Free all the signs
1570    free_signs();
1571    rettv->vval.v_number = 0;
1572  } else {
1573    // Free only the specified sign
1574    const char *name = tv_get_string_chk(&argvars[0]);
1575    if (name == NULL) {
1576      return;
1577    }
1578
1579    if (sign_undefine_by_name(name) == OK) {
1580      rettv->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{
1589  dictitem_T *di;
1590  int id = 0;
1591  buf_T *buf = NULL;
1592  char *group = (group_tv != NULL) ? (char *)tv_get_string(group_tv)
1593                                   : tv_dict_get_string(dict, "group", false);
1594  if (group != NULL && group[0] == NUL) {
1595    group = NULL;
1596  }
1597
1598  if (dict != NULL) {
1599    if ((di = tv_dict_find(dict, "buffer", -1)) != NULL) {
1600      buf = get_buf_arg(&di->di_tv);
1601      if (buf == NULL) {
1602        return -1;
1603      }
1604    }
1605    if (tv_dict_find(dict, "id", -1) != NULL) {
1606      id = (int)tv_dict_get_number(dict, "id");
1607      if (id <= 0) {
1608        emsg(_(e_invarg));
1609        return -1;
1610      }
1611    }
1612  }
1613
1614  return 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{
1620  dict_T *dict = NULL;
1621
1622  rettv->vval.v_number = -1;
1623
1624  if (tv_check_for_string_arg(argvars, 0) == FAIL
1625      || tv_check_for_opt_dict_arg(argvars, 1) == FAIL) {
1626    return;
1627  }
1628
1629  if (argvars[1].v_type != VAR_UNKNOWN) {
1630    dict = argvars[1].vval.v_dict;
1631  }
1632
1633  rettv->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{
1639  tv_list_alloc_ret(rettv, kListLenMayKnow);
1640
1641  if (argvars[0].v_type != VAR_LIST) {
1642    emsg(_(e_listreq));
1643    return;
1644  }
1645
1646  TV_LIST_ITER_CONST(argvars[0].vval.v_list, li, {
1647    int retval = -1;
1648    if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
1649      retval = sign_unplace_from_dict(NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
1650    } else {
1651      emsg(_(e_dictreq));
1652    }
1653    tv_list_append_number(rettv->vval.v_list, retval);
1654  });
1655}