blob: 2cb39a5e1f52c5d9a127c5fec9f2adb65d088628 [file] [log] [blame]
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * sign.c: functions for managing signs
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_SIGNS) || defined(PROTO)
17
18/*
19 * Struct to hold the sign properties.
20 */
21typedef struct sign sign_T;
22
23struct sign
24{
25 sign_T *sn_next; /* next sign in list */
26 int sn_typenr; /* type number of sign */
27 char_u *sn_name; /* name of sign */
28 char_u *sn_icon; /* name of pixmap */
29# ifdef FEAT_SIGN_ICONS
30 void *sn_image; /* icon image */
31# endif
32 char_u *sn_text; /* text used instead of pixmap */
33 int sn_line_hl; /* highlight ID for line */
34 int sn_text_hl; /* highlight ID for text */
35};
36
37static sign_T *first_sign = NULL;
38static int next_sign_typenr = 1;
39
40static void sign_list_defined(sign_T *sp);
41static void sign_undefine(sign_T *sp, sign_T *sp_prev);
42
43static char *cmds[] = {
44 "define",
45# define SIGNCMD_DEFINE 0
46 "undefine",
47# define SIGNCMD_UNDEFINE 1
48 "list",
49# define SIGNCMD_LIST 2
50 "place",
51# define SIGNCMD_PLACE 3
52 "unplace",
53# define SIGNCMD_UNPLACE 4
54 "jump",
55# define SIGNCMD_JUMP 5
56 NULL
57# define SIGNCMD_LAST 6
58};
59
60static hashtab_T sg_table; // sign group (signgroup_T) hashtable
61static int next_sign_id = 1; // next sign id in the global group
62
63/*
64 * Initialize data needed for managing signs
65 */
66 void
67init_signs(void)
68{
69 hash_init(&sg_table); // sign group hash table
70}
71
72/*
73 * A new sign in group 'groupname' is added. If the group is not present,
74 * create it. Otherwise reference the group.
75 */
76 static signgroup_T *
77sign_group_ref(char_u *groupname)
78{
79 hash_T hash;
80 hashitem_T *hi;
81 signgroup_T *group;
82
83 hash = hash_hash(groupname);
84 hi = hash_lookup(&sg_table, groupname, hash);
85 if (HASHITEM_EMPTY(hi))
86 {
87 // new group
88 group = (signgroup_T *)alloc(
89 (unsigned)(sizeof(signgroup_T) + STRLEN(groupname)));
90 if (group == NULL)
91 return NULL;
92 STRCPY(group->sg_name, groupname);
93 group->refcount = 1;
94 group->next_sign_id = 1;
95 hash_add_item(&sg_table, hi, group->sg_name, hash);
96 }
97 else
98 {
99 // existing group
100 group = HI2SG(hi);
101 group->refcount++;
102 }
103
104 return group;
105}
106
107/*
108 * A sign in group 'groupname' is removed. If all the signs in this group are
109 * removed, then remove the group.
110 */
111 static void
112sign_group_unref(char_u *groupname)
113{
114 hashitem_T *hi;
115 signgroup_T *group;
116
117 hi = hash_find(&sg_table, groupname);
118 if (!HASHITEM_EMPTY(hi))
119 {
120 group = HI2SG(hi);
121 group->refcount--;
122 if (group->refcount == 0)
123 {
124 // All the signs in this group are removed
125 hash_remove(&sg_table, hi);
126 vim_free(group);
127 }
128 }
129}
130
131/*
132 * Returns TRUE if 'sign' is in 'group'.
133 * A sign can either be in the global group (sign->group == NULL)
134 * or in a named group. If 'group' is '*', then the sign is part of the group.
135 */
136 static int
137sign_in_group(signlist_T *sign, char_u *group)
138{
139 return ((group != NULL && STRCMP(group, "*") == 0)
140 || (group == NULL && sign->group == NULL)
141 || (group != NULL && sign->group != NULL
142 && STRCMP(group, sign->group->sg_name) == 0));
143}
144
145/*
146 * Get the next free sign identifier in the specified group
147 */
148 static int
149sign_group_get_next_signid(buf_T *buf, char_u *groupname)
150{
151 int id = 1;
152 signgroup_T *group = NULL;
153 signlist_T *sign;
154 hashitem_T *hi;
155 int found = FALSE;
156
157 if (groupname != NULL)
158 {
159 hi = hash_find(&sg_table, groupname);
160 if (HASHITEM_EMPTY(hi))
161 return id;
162 group = HI2SG(hi);
163 }
164
165 // Search for the next usuable sign identifier
166 while (!found)
167 {
168 if (group == NULL)
169 id = next_sign_id++; // global group
170 else
171 id = group->next_sign_id++;
172
173 // Check whether this sign is already placed in the buffer
174 found = TRUE;
175 FOR_ALL_SIGNS_IN_BUF(buf, sign)
176 {
177 if (id == sign->id && sign_in_group(sign, groupname))
178 {
179 found = FALSE; // sign identifier is in use
180 break;
181 }
182 }
183 }
184
185 return id;
186}
187
188/*
189 * Insert a new sign into the signlist for buffer 'buf' between the 'prev' and
190 * 'next' signs.
191 */
192 static void
193insert_sign(
194 buf_T *buf, // buffer to store sign in
195 signlist_T *prev, // previous sign entry
196 signlist_T *next, // next sign entry
197 int id, // sign ID
198 char_u *group, // sign group; NULL for global group
199 int prio, // sign priority
200 linenr_T lnum, // line number which gets the mark
201 int typenr) // typenr of sign we are adding
202{
203 signlist_T *newsign;
204
205 newsign = (signlist_T *)lalloc_id((long_u)sizeof(signlist_T), FALSE,
206 aid_insert_sign);
207 if (newsign != NULL)
208 {
209 newsign->id = id;
210 newsign->lnum = lnum;
211 newsign->typenr = typenr;
212 if (group != NULL)
213 {
214 newsign->group = sign_group_ref(group);
215 if (newsign->group == NULL)
216 {
217 vim_free(newsign);
218 return;
219 }
220 }
221 else
222 newsign->group = NULL;
223 newsign->priority = prio;
224 newsign->next = next;
225 newsign->prev = prev;
226 if (next != NULL)
227 next->prev = newsign;
228
229 if (prev == NULL)
230 {
231 // When adding first sign need to redraw the windows to create the
232 // column for signs.
233 if (buf->b_signlist == NULL)
234 {
235 redraw_buf_later(buf, NOT_VALID);
236 changed_cline_bef_curs();
237 }
238
239 // first sign in signlist
240 buf->b_signlist = newsign;
241#ifdef FEAT_NETBEANS_INTG
242 if (netbeans_active())
243 buf->b_has_sign_column = TRUE;
244#endif
245 }
246 else
247 prev->next = newsign;
248 }
249}
250
251/*
252 * Insert a new sign sorted by line number and sign priority.
253 */
254 static void
255insert_sign_by_lnum_prio(
256 buf_T *buf, // buffer to store sign in
257 signlist_T *prev, // previous sign entry
258 int id, // sign ID
259 char_u *group, // sign group; NULL for global group
260 int prio, // sign priority
261 linenr_T lnum, // line number which gets the mark
262 int typenr) // typenr of sign we are adding
263{
264 signlist_T *sign;
265
266 // keep signs sorted by lnum and by priority: insert new sign at
267 // the proper position in the list for this lnum.
268 while (prev != NULL && prev->lnum == lnum && prev->priority <= prio)
269 prev = prev->prev;
270 if (prev == NULL)
271 sign = buf->b_signlist;
272 else
273 sign = prev->next;
274
275 insert_sign(buf, prev, sign, id, group, prio, lnum, typenr);
276}
277
278/*
279 * Get the name of a sign by its typenr.
280 */
281 static char_u *
282sign_typenr2name(int typenr)
283{
284 sign_T *sp;
285
286 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
287 if (sp->sn_typenr == typenr)
288 return sp->sn_name;
289 return (char_u *)_("[Deleted]");
290}
291
292/*
293 * Return information about a sign in a Dict
294 */
295 static dict_T *
296sign_get_info(signlist_T *sign)
297{
298 dict_T *d;
299
300 if ((d = dict_alloc_id(aid_sign_getinfo)) == NULL)
301 return NULL;
302 dict_add_number(d, "id", sign->id);
303 dict_add_string(d, "group", (sign->group == NULL) ?
304 (char_u *)"" : sign->group->sg_name);
305 dict_add_number(d, "lnum", sign->lnum);
306 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
307 dict_add_number(d, "priority", sign->priority);
308
309 return d;
310}
311
312/*
313 * Add the sign into the signlist. Find the right spot to do it though.
314 */
315 static void
316buf_addsign(
317 buf_T *buf, // buffer to store sign in
318 int id, // sign ID
319 char_u *groupname, // sign group
320 int prio, // sign priority
321 linenr_T lnum, // line number which gets the mark
322 int typenr) // typenr of sign we are adding
323{
324 signlist_T *sign; // a sign in the signlist
325 signlist_T *prev; // the previous sign
326
327 prev = NULL;
328 FOR_ALL_SIGNS_IN_BUF(buf, sign)
329 {
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100330 if (lnum == sign->lnum && id == sign->id
331 && sign_in_group(sign, groupname))
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100332 {
333 // Update an existing sign
334 sign->typenr = typenr;
335 return;
336 }
337 else if (lnum < sign->lnum)
338 {
339 insert_sign_by_lnum_prio(buf, prev, id, groupname, prio,
340 lnum, typenr);
341 return;
342 }
343 prev = sign;
344 }
345
346 insert_sign_by_lnum_prio(buf, prev, id, groupname, prio, lnum, typenr);
347 return;
348}
349
350/*
351 * For an existing, placed sign "markId" change the type to "typenr".
352 * Returns the line number of the sign, or zero if the sign is not found.
353 */
354 static linenr_T
355buf_change_sign_type(
356 buf_T *buf, // buffer to store sign in
357 int markId, // sign ID
358 char_u *group, // sign group
359 int typenr) // typenr of sign we are adding
360{
361 signlist_T *sign; // a sign in the signlist
362
363 FOR_ALL_SIGNS_IN_BUF(buf, sign)
364 {
365 if (sign->id == markId && sign_in_group(sign, group))
366 {
367 sign->typenr = typenr;
368 return sign->lnum;
369 }
370 }
371
372 return (linenr_T)0;
373}
374
375/*
376 * Return the type number of the sign at line number 'lnum' in buffer 'buf'
377 * which has the attribute specifed by 'type'. Returns 0 if a sign is not found
378 * at the line number or it doesn't have the specified attribute.
379 */
380 int
381buf_getsigntype(
382 buf_T *buf,
383 linenr_T lnum,
384 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
385{
386 signlist_T *sign; /* a sign in a b_signlist */
387
388 FOR_ALL_SIGNS_IN_BUF(buf, sign)
389 if (sign->lnum == lnum
390 && (type == SIGN_ANY
391# ifdef FEAT_SIGN_ICONS
392 || (type == SIGN_ICON
393 && sign_get_image(sign->typenr) != NULL)
394# endif
395 || (type == SIGN_TEXT
396 && sign_get_text(sign->typenr) != NULL)
397 || (type == SIGN_LINEHL
398 && sign_get_attr(sign->typenr, TRUE) != 0)))
399 return sign->typenr;
400 return 0;
401}
402
403/*
404 * Delete sign 'id' in group 'group' from buffer 'buf'.
405 * If 'id' is zero, then delete all the signs in group 'group'. Otherwise
406 * delete only the specified sign.
407 * If 'group' is '*', then delete the sign in all the groups. If 'group' is
408 * NULL, then delete the sign in the global group. Otherwise delete the sign in
409 * the specified group.
410 * Returns the line number of the deleted sign. If multiple signs are deleted,
411 * then returns the line number of the last sign deleted.
412 */
413 linenr_T
414buf_delsign(
415 buf_T *buf, // buffer sign is stored in
416 linenr_T atlnum, // sign at this line, 0 - at any line
417 int id, // sign id
418 char_u *group) // sign group
419{
420 signlist_T **lastp; // pointer to pointer to current sign
421 signlist_T *sign; // a sign in a b_signlist
422 signlist_T *next; // the next sign in a b_signlist
423 linenr_T lnum; // line number whose sign was deleted
424
425 lastp = &buf->b_signlist;
426 lnum = 0;
427 for (sign = buf->b_signlist; sign != NULL; sign = next)
428 {
429 next = sign->next;
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100430 if ((id == 0 || sign->id == id)
431 && (atlnum == 0 || sign->lnum == atlnum)
432 && sign_in_group(sign, group))
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100433
434 {
435 *lastp = next;
436 if (next != NULL)
437 next->prev = sign->prev;
438 lnum = sign->lnum;
439 if (sign->group != NULL)
440 sign_group_unref(sign->group->sg_name);
441 vim_free(sign);
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100442 redraw_buf_line_later(buf, lnum);
443
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100444 // Check whether only one sign needs to be deleted
445 // If deleting a sign with a specific identifer in a particular
446 // group or deleting any sign at a particular line number, delete
447 // only one sign.
448 if (group == NULL
449 || (*group != '*' && id != 0)
450 || (*group == '*' && atlnum != 0))
451 break;
452 }
453 else
454 lastp = &sign->next;
455 }
456
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100457 // When deleting the last sign the cursor position may change, because the
458 // sign columns no longer shows.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100459 if (buf->b_signlist == NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100460 changed_cline_bef_curs();
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100461
462 return lnum;
463}
464
465
466/*
467 * Find the line number of the sign with the requested id in group 'group'. If
468 * the sign does not exist, return 0 as the line number. This will still let
469 * the correct file get loaded.
470 */
471 int
472buf_findsign(
473 buf_T *buf, // buffer to store sign in
474 int id, // sign ID
475 char_u *group) // sign group
476{
477 signlist_T *sign; // a sign in the signlist
478
479 FOR_ALL_SIGNS_IN_BUF(buf, sign)
480 if (sign->id == id && sign_in_group(sign, group))
481 return sign->lnum;
482
483 return 0;
484}
485
486/*
487 * Return the sign at line 'lnum' in buffer 'buf'. Returns NULL if a sign is
488 * not found at the line. If 'groupname' is NULL, searches in the global group.
489 */
490 static signlist_T *
491buf_getsign_at_line(
492 buf_T *buf, // buffer whose sign we are searching for
493 linenr_T lnum, // line number of sign
494 char_u *groupname) // sign group name
495{
496 signlist_T *sign; // a sign in the signlist
497
498 FOR_ALL_SIGNS_IN_BUF(buf, sign)
499 if (sign->lnum == lnum && sign_in_group(sign, groupname))
500 return sign;
501
502 return NULL;
503}
504
505/*
506 * Return the identifier of the sign at line number 'lnum' in buffer 'buf'.
507 */
508 int
509buf_findsign_id(
510 buf_T *buf, // buffer whose sign we are searching for
511 linenr_T lnum, // line number of sign
512 char_u *groupname) // sign group name
513{
514 signlist_T *sign; // a sign in the signlist
515
516 sign = buf_getsign_at_line(buf, lnum, groupname);
517 if (sign != NULL)
518 return sign->id;
519
520 return 0;
521}
522
523# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
524/*
525 * See if a given type of sign exists on a specific line.
526 */
527 int
528buf_findsigntype_id(
529 buf_T *buf, /* buffer whose sign we are searching for */
530 linenr_T lnum, /* line number of sign */
531 int typenr) /* sign type number */
532{
533 signlist_T *sign; /* a sign in the signlist */
534
535 FOR_ALL_SIGNS_IN_BUF(buf, sign)
536 if (sign->lnum == lnum && sign->typenr == typenr)
537 return sign->id;
538
539 return 0;
540}
541
542
543# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
544/*
545 * Return the number of icons on the given line.
546 */
547 int
548buf_signcount(buf_T *buf, linenr_T lnum)
549{
550 signlist_T *sign; // a sign in the signlist
551 int count = 0;
552
553 FOR_ALL_SIGNS_IN_BUF(buf, sign)
554 if (sign->lnum == lnum)
555 if (sign_get_image(sign->typenr) != NULL)
556 count++;
557
558 return count;
559}
560# endif /* FEAT_SIGN_ICONS */
561# endif /* FEAT_NETBEANS_INTG */
562
563/*
564 * Delete signs in group 'group' in buffer "buf". If 'group' is '*', then
565 * delete all the signs.
566 */
567 void
568buf_delete_signs(buf_T *buf, char_u *group)
569{
570 signlist_T *sign;
571 signlist_T **lastp; // pointer to pointer to current sign
572 signlist_T *next;
573
574 // When deleting the last sign need to redraw the windows to remove the
575 // sign column. Not when curwin is NULL (this means we're exiting).
576 if (buf->b_signlist != NULL && curwin != NULL)
577 {
578 redraw_buf_later(buf, NOT_VALID);
579 changed_cline_bef_curs();
580 }
581
582 lastp = &buf->b_signlist;
583 for (sign = buf->b_signlist; sign != NULL; sign = next)
584 {
585 next = sign->next;
586 if (sign_in_group(sign, group))
587 {
588 *lastp = next;
589 if (next != NULL)
590 next->prev = sign->prev;
591 if (sign->group != NULL)
592 sign_group_unref(sign->group->sg_name);
593 vim_free(sign);
594 }
595 else
596 lastp = &sign->next;
597 }
598}
599
600/*
601 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
602 */
603 static void
604sign_list_placed(buf_T *rbuf, char_u *sign_group)
605{
606 buf_T *buf;
607 signlist_T *sign;
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100608 char lbuf[MSG_BUF_LEN];
609 char group[MSG_BUF_LEN];
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100610
611 MSG_PUTS_TITLE(_("\n--- Signs ---"));
612 msg_putchar('\n');
613 if (rbuf == NULL)
614 buf = firstbuf;
615 else
616 buf = rbuf;
617 while (buf != NULL && !got_int)
618 {
619 if (buf->b_signlist != NULL)
620 {
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100621 vim_snprintf(lbuf, MSG_BUF_LEN, _("Signs for %s:"), buf->b_fname);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100622 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
623 msg_putchar('\n');
624 }
625 FOR_ALL_SIGNS_IN_BUF(buf, sign)
626 {
627 if (got_int)
628 break;
629 if (!sign_in_group(sign, sign_group))
630 continue;
631 if (sign->group != NULL)
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100632 vim_snprintf(group, MSG_BUF_LEN, _(" group=%s"),
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100633 sign->group->sg_name);
634 else
635 group[0] = '\0';
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100636 vim_snprintf(lbuf, MSG_BUF_LEN,
637 _(" line=%ld id=%d%s name=%s priority=%d"),
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100638 (long)sign->lnum, sign->id, group,
639 sign_typenr2name(sign->typenr), sign->priority);
640 MSG_PUTS(lbuf);
641 msg_putchar('\n');
642 }
643 if (rbuf != NULL)
644 break;
645 buf = buf->b_next;
646 }
647}
648
649/*
650 * Adjust a placed sign for inserted/deleted lines.
651 */
652 void
653sign_mark_adjust(
654 linenr_T line1,
655 linenr_T line2,
656 long amount,
657 long amount_after)
658{
659 signlist_T *sign; /* a sign in a b_signlist */
660
661 FOR_ALL_SIGNS_IN_BUF(curbuf, sign)
662 {
663 if (sign->lnum >= line1 && sign->lnum <= line2)
664 {
665 if (amount == MAXLNUM)
666 sign->lnum = line1;
667 else
668 sign->lnum += amount;
669 }
670 else if (sign->lnum > line2)
671 sign->lnum += amount_after;
672 }
673}
674
675/*
676 * Find index of a ":sign" subcmd from its name.
677 * "*end_cmd" must be writable.
678 */
679 static int
680sign_cmd_idx(
681 char_u *begin_cmd, /* begin of sign subcmd */
682 char_u *end_cmd) /* just after sign subcmd */
683{
684 int idx;
685 char save = *end_cmd;
686
687 *end_cmd = NUL;
688 for (idx = 0; ; ++idx)
689 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
690 break;
691 *end_cmd = save;
692 return idx;
693}
694
695/*
696 * Find a sign by name. Also returns pointer to the previous sign.
697 */
698 static sign_T *
699sign_find(char_u *name, sign_T **sp_prev)
700{
701 sign_T *sp;
702
703 if (sp_prev != NULL)
704 *sp_prev = NULL;
705 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
706 {
707 if (STRCMP(sp->sn_name, name) == 0)
708 break;
709 if (sp_prev != NULL)
710 *sp_prev = sp;
711 }
712
713 return sp;
714}
715
716/*
717 * Define a new sign or update an existing sign
718 */
719 int
720sign_define_by_name(
721 char_u *name,
722 char_u *icon,
723 char_u *linehl,
724 char_u *text,
725 char_u *texthl)
726{
727 sign_T *sp_prev;
728 sign_T *sp;
729
730 sp = sign_find(name, &sp_prev);
731 if (sp == NULL)
732 {
733 sign_T *lp;
734 int start = next_sign_typenr;
735
736 // Allocate a new sign.
737 sp = (sign_T *)alloc_clear_id((unsigned)sizeof(sign_T),
738 aid_sign_define_by_name);
739 if (sp == NULL)
740 return FAIL;
741
742 // Check that next_sign_typenr is not already being used.
743 // This only happens after wrapping around. Hopefully
744 // another one got deleted and we can use its number.
745 for (lp = first_sign; lp != NULL; )
746 {
747 if (lp->sn_typenr == next_sign_typenr)
748 {
749 ++next_sign_typenr;
750 if (next_sign_typenr == MAX_TYPENR)
751 next_sign_typenr = 1;
752 if (next_sign_typenr == start)
753 {
754 vim_free(sp);
755 EMSG(_("E612: Too many signs defined"));
756 return FAIL;
757 }
758 lp = first_sign; // start all over
759 continue;
760 }
761 lp = lp->sn_next;
762 }
763
764 sp->sn_typenr = next_sign_typenr;
765 if (++next_sign_typenr == MAX_TYPENR)
766 next_sign_typenr = 1; // wrap around
767
768 sp->sn_name = vim_strsave(name);
769 if (sp->sn_name == NULL) // out of memory
770 {
771 vim_free(sp);
772 return FAIL;
773 }
774
775 // add the new sign to the list of signs
776 if (sp_prev == NULL)
777 first_sign = sp;
778 else
779 sp_prev->sn_next = sp;
780 }
781
782 // set values for a defined sign.
783 if (icon != NULL)
784 {
785 vim_free(sp->sn_icon);
786 sp->sn_icon = vim_strsave(icon);
787 backslash_halve(sp->sn_icon);
788# ifdef FEAT_SIGN_ICONS
789 if (gui.in_use)
790 {
791 out_flush();
792 if (sp->sn_image != NULL)
793 gui_mch_destroy_sign(sp->sn_image);
794 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
795 }
796# endif
797 }
798
799 if (text != NULL)
800 {
801 char_u *s;
802 char_u *endp;
803 int cells;
804 int len;
805
806 endp = text + (int)STRLEN(text);
807 for (s = text; s + 1 < endp; ++s)
808 if (*s == '\\')
809 {
810 // Remove a backslash, so that it is possible
811 // to use a space.
812 STRMOVE(s, s + 1);
813 --endp;
814 }
815# ifdef FEAT_MBYTE
816 // Count cells and check for non-printable chars
817 if (has_mbyte)
818 {
819 cells = 0;
820 for (s = text; s < endp; s += (*mb_ptr2len)(s))
821 {
822 if (!vim_isprintc((*mb_ptr2char)(s)))
823 break;
824 cells += (*mb_ptr2cells)(s);
825 }
826 }
827 else
828# endif
829 {
830 for (s = text; s < endp; ++s)
831 if (!vim_isprintc(*s))
832 break;
833 cells = (int)(s - text);
834 }
835 // Currently must be one or two display cells
836 if (s != endp || cells < 1 || cells > 2)
837 {
838 EMSG2(_("E239: Invalid sign text: %s"), text);
839 return FAIL;
840 }
841
842 vim_free(sp->sn_text);
843 // Allocate one byte more if we need to pad up
844 // with a space.
845 len = (int)(endp - text + ((cells == 1) ? 1 : 0));
846 sp->sn_text = vim_strnsave(text, len);
847
848 if (sp->sn_text != NULL && cells == 1)
849 STRCPY(sp->sn_text + len - 1, " ");
850 }
851
852 if (linehl != NULL)
853 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl));
854
855 if (texthl != NULL)
856 sp->sn_text_hl = syn_check_group(texthl, (int)STRLEN(texthl));
857
858 return OK;
859}
860
861/*
862 * Free the sign specified by 'name'.
863 */
864 int
865sign_undefine_by_name(char_u *name)
866{
867 sign_T *sp_prev;
868 sign_T *sp;
869
870 sp = sign_find(name, &sp_prev);
871 if (sp == NULL)
872 {
873 EMSG2(_("E155: Unknown sign: %s"), name);
874 return FAIL;
875 }
876 sign_undefine(sp, sp_prev);
877
878 return OK;
879}
880
881/*
882 * List the signs matching 'name'
883 */
884 static void
885sign_list_by_name(char_u *name)
886{
887 sign_T *sp;
888
889 sp = sign_find(name, NULL);
890 if (sp != NULL)
891 sign_list_defined(sp);
892 else
893 EMSG2(_("E155: Unknown sign: %s"), name);
894}
895
896/*
897 * Place a sign at the specifed file location or update a sign.
898 */
899 int
900sign_place(
901 int *sign_id,
902 char_u *sign_group,
903 char_u *sign_name,
904 buf_T *buf,
905 linenr_T lnum,
906 int prio)
907{
908 sign_T *sp;
909
910 // Check for reserved character '*' in group name
911 if (sign_group != NULL && (*sign_group == '*' || *sign_group == '\0'))
912 return FAIL;
913
914 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
915 if (STRCMP(sp->sn_name, sign_name) == 0)
916 break;
917 if (sp == NULL)
918 {
919 EMSG2(_("E155: Unknown sign: %s"), sign_name);
920 return FAIL;
921 }
922 if (*sign_id == 0)
923 *sign_id = sign_group_get_next_signid(buf, sign_group);
924
925 if (lnum > 0)
926 // ":sign place {id} line={lnum} name={name} file={fname}":
927 // place a sign
928 buf_addsign(buf, *sign_id, sign_group, prio, lnum, sp->sn_typenr);
929 else
930 // ":sign place {id} file={fname}": change sign type
931 lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr);
932 if (lnum > 0)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100933 redraw_buf_line_later(buf, lnum);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100934 else
935 {
936 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
937 return FAIL;
938 }
939
940 return OK;
941}
942
943/*
944 * Unplace the specified sign
945 */
946 int
947sign_unplace(int sign_id, char_u *sign_group, buf_T *buf, linenr_T atlnum)
948{
949 if (buf->b_signlist == NULL) // No signs in the buffer
950 return OK;
951
952 if (sign_id == 0)
953 {
954 // Delete all the signs in the specified buffer
955 redraw_buf_later(buf, NOT_VALID);
956 buf_delete_signs(buf, sign_group);
957 }
958 else
959 {
960 linenr_T lnum;
961
962 // Delete only the specified signs
963 lnum = buf_delsign(buf, atlnum, sign_id, sign_group);
964 if (lnum == 0)
965 return FAIL;
966 }
967
968 return OK;
969}
970
971/*
972 * Unplace the sign at the current cursor line.
973 */
974 static void
975sign_unplace_at_cursor(char_u *groupname)
976{
977 int id = -1;
978
979 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum, groupname);
980 if (id > 0)
981 sign_unplace(id, groupname, curwin->w_buffer, curwin->w_cursor.lnum);
982 else
983 EMSG(_("E159: Missing sign number"));
984}
985
986/*
987 * sign define command
988 * ":sign define {name} ..."
989 */
990 static void
991sign_define_cmd(char_u *sign_name, char_u *cmdline)
992{
993 char_u *arg;
994 char_u *p = cmdline;
995 char_u *icon = NULL;
996 char_u *text = NULL;
997 char_u *linehl = NULL;
998 char_u *texthl = NULL;
999 int failed = FALSE;
1000
1001 // set values for a defined sign.
1002 for (;;)
1003 {
1004 arg = skipwhite(p);
1005 if (*arg == NUL)
1006 break;
1007 p = skiptowhite_esc(arg);
1008 if (STRNCMP(arg, "icon=", 5) == 0)
1009 {
1010 arg += 5;
1011 icon = vim_strnsave(arg, (int)(p - arg));
1012 }
1013 else if (STRNCMP(arg, "text=", 5) == 0)
1014 {
1015 arg += 5;
1016 text = vim_strnsave(arg, (int)(p - arg));
1017 }
1018 else if (STRNCMP(arg, "linehl=", 7) == 0)
1019 {
1020 arg += 7;
1021 linehl = vim_strnsave(arg, (int)(p - arg));
1022 }
1023 else if (STRNCMP(arg, "texthl=", 7) == 0)
1024 {
1025 arg += 7;
1026 texthl = vim_strnsave(arg, (int)(p - arg));
1027 }
1028 else
1029 {
1030 EMSG2(_(e_invarg2), arg);
1031 failed = TRUE;
1032 break;
1033 }
1034 }
1035
1036 if (!failed)
1037 sign_define_by_name(sign_name, icon, linehl, text, texthl);
1038
1039 vim_free(icon);
1040 vim_free(text);
1041 vim_free(linehl);
1042 vim_free(texthl);
1043}
1044
1045/*
1046 * :sign place command
1047 */
1048 static void
1049sign_place_cmd(
1050 buf_T *buf,
1051 linenr_T lnum,
1052 char_u *sign_name,
1053 int id,
1054 char_u *group,
1055 int prio)
1056{
1057 if (id <= 0)
1058 {
1059 // List signs placed in a file/buffer
1060 // :sign place file={fname}
1061 // :sign place group={group} file={fname}
1062 // :sign place group=* file={fname}
1063 // :sign place buffer={nr}
1064 // :sign place group={group} buffer={nr}
1065 // :sign place group=* buffer={nr}
1066 // :sign place
1067 // :sign place group={group}
1068 // :sign place group=*
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001069 if (lnum >= 0 || sign_name != NULL
1070 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001071 EMSG(_(e_invarg));
1072 else
1073 sign_list_placed(buf, group);
1074 }
1075 else
1076 {
1077 // Place a new sign
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001078 if (sign_name == NULL || buf == NULL
1079 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001080 {
1081 EMSG(_(e_invarg));
1082 return;
1083 }
1084
1085 sign_place(&id, group, sign_name, buf, lnum, prio);
1086 }
1087}
1088
1089/*
1090 * :sign unplace command
1091 */
1092 static void
1093sign_unplace_cmd(
1094 buf_T *buf,
1095 linenr_T lnum,
1096 char_u *sign_name,
1097 int id,
1098 char_u *group)
1099{
1100 if (lnum >= 0 || sign_name != NULL || (group != NULL && *group == '\0'))
1101 {
1102 EMSG(_(e_invarg));
1103 return;
1104 }
1105
1106 if (id == -2)
1107 {
1108 if (buf != NULL)
1109 // :sign unplace * file={fname}
1110 // :sign unplace * group={group} file={fname}
1111 // :sign unplace * group=* file={fname}
1112 // :sign unplace * buffer={nr}
1113 // :sign unplace * group={group} buffer={nr}
1114 // :sign unplace * group=* buffer={nr}
1115 sign_unplace(0, group, buf, 0);
1116 else
1117 // :sign unplace *
1118 // :sign unplace * group={group}
1119 // :sign unplace * group=*
1120 FOR_ALL_BUFFERS(buf)
1121 if (buf->b_signlist != NULL)
1122 buf_delete_signs(buf, group);
1123 }
1124 else
1125 {
1126 if (buf != NULL)
1127 // :sign unplace {id} file={fname}
1128 // :sign unplace {id} group={group} file={fname}
1129 // :sign unplace {id} group=* file={fname}
1130 // :sign unplace {id} buffer={nr}
1131 // :sign unplace {id} group={group} buffer={nr}
1132 // :sign unplace {id} group=* buffer={nr}
1133 sign_unplace(id, group, buf, 0);
1134 else
1135 {
1136 if (id == -1)
1137 {
1138 // :sign unplace group={group}
1139 // :sign unplace group=*
1140 sign_unplace_at_cursor(group);
1141 }
1142 else
1143 {
1144 // :sign unplace {id}
1145 // :sign unplace {id} group={group}
1146 // :sign unplace {id} group=*
1147 FOR_ALL_BUFFERS(buf)
1148 sign_unplace(id, group, buf, 0);
1149 }
1150 }
1151 }
1152}
1153
1154/*
1155 * Jump to a placed sign
1156 * :sign jump {id} file={fname}
1157 * :sign jump {id} buffer={nr}
1158 * :sign jump {id} group={group} file={fname}
1159 * :sign jump {id} group={group} buffer={nr}
1160 */
1161 static void
1162sign_jump_cmd(
1163 buf_T *buf,
1164 linenr_T lnum,
1165 char_u *sign_name,
1166 int id,
1167 char_u *group)
1168{
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001169 if (sign_name == NULL && group == NULL && id == -1)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001170 {
1171 EMSG(_(e_argreq));
1172 return;
1173 }
1174
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001175 if (buf == NULL || (group != NULL && *group == '\0')
1176 || lnum >= 0 || sign_name != NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001177 {
1178 // File or buffer is not specified or an empty group is used
1179 // or a line number or a sign name is specified.
1180 EMSG(_(e_invarg));
1181 return;
1182 }
1183
1184 if ((lnum = buf_findsign(buf, id, group)) <= 0)
1185 {
1186 EMSGN(_("E157: Invalid sign ID: %ld"), id);
1187 return;
1188 }
1189
1190 // goto a sign ...
1191 if (buf_jump_open_win(buf) != NULL)
1192 { // ... in a current window
1193 curwin->w_cursor.lnum = lnum;
1194 check_cursor_lnum();
1195 beginline(BL_WHITE);
1196 }
1197 else
1198 { // ... not currently in a window
1199 char_u *cmd;
1200
1201 if (buf->b_fname == NULL)
1202 {
1203 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
1204 return;
1205 }
1206 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
1207 if (cmd == NULL)
1208 return;
1209 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
1210 do_cmdline_cmd(cmd);
1211 vim_free(cmd);
1212 }
1213# ifdef FEAT_FOLDING
1214 foldOpenCursor();
1215# endif
1216}
1217
1218/*
1219 * Parse the command line arguments for the ":sign place", ":sign unplace" and
1220 * ":sign jump" commands.
1221 * The supported arguments are: line={lnum} name={name} group={group}
1222 * priority={prio} and file={fname} or buffer={nr}.
1223 */
1224 static int
1225parse_sign_cmd_args(
1226 int cmd,
1227 char_u *arg,
1228 char_u **sign_name,
1229 int *signid,
1230 char_u **group,
1231 int *prio,
1232 buf_T **buf,
1233 linenr_T *lnum)
1234{
1235 char_u *arg1;
1236 char_u *name;
1237 char_u *filename = NULL;
Bram Moolenaarb589f952019-01-07 22:10:00 +01001238 int lnum_arg = FALSE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001239
1240 // first arg could be placed sign id
1241 arg1 = arg;
1242 if (VIM_ISDIGIT(*arg))
1243 {
1244 *signid = getdigits(&arg);
1245 if (!VIM_ISWHITE(*arg) && *arg != NUL)
1246 {
1247 *signid = -1;
1248 arg = arg1;
1249 }
1250 else
1251 arg = skipwhite(arg);
1252 }
1253
1254 while (*arg != NUL)
1255 {
1256 if (STRNCMP(arg, "line=", 5) == 0)
1257 {
1258 arg += 5;
1259 *lnum = atoi((char *)arg);
1260 arg = skiptowhite(arg);
Bram Moolenaarb589f952019-01-07 22:10:00 +01001261 lnum_arg = TRUE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001262 }
1263 else if (STRNCMP(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE)
1264 {
1265 if (*signid != -1)
1266 {
1267 EMSG(_(e_invarg));
1268 return FAIL;
1269 }
1270 *signid = -2;
1271 arg = skiptowhite(arg + 1);
1272 }
1273 else if (STRNCMP(arg, "name=", 5) == 0)
1274 {
1275 arg += 5;
1276 name = arg;
1277 arg = skiptowhite(arg);
1278 if (*arg != NUL)
1279 *arg++ = NUL;
1280 while (name[0] == '0' && name[1] != NUL)
1281 ++name;
1282 *sign_name = name;
1283 }
1284 else if (STRNCMP(arg, "group=", 6) == 0)
1285 {
1286 arg += 6;
1287 *group = arg;
1288 arg = skiptowhite(arg);
1289 if (*arg != NUL)
1290 *arg++ = NUL;
1291 }
1292 else if (STRNCMP(arg, "priority=", 9) == 0)
1293 {
1294 arg += 9;
1295 *prio = atoi((char *)arg);
1296 arg = skiptowhite(arg);
1297 }
1298 else if (STRNCMP(arg, "file=", 5) == 0)
1299 {
1300 arg += 5;
1301 filename = arg;
1302 *buf = buflist_findname_exp(arg);
1303 break;
1304 }
1305 else if (STRNCMP(arg, "buffer=", 7) == 0)
1306 {
1307 arg += 7;
1308 filename = arg;
1309 *buf = buflist_findnr((int)getdigits(&arg));
1310 if (*skipwhite(arg) != NUL)
1311 EMSG(_(e_trailing));
1312 break;
1313 }
1314 else
1315 {
1316 EMSG(_(e_invarg));
1317 return FAIL;
1318 }
1319 arg = skipwhite(arg);
1320 }
1321
1322 if (filename != NULL && *buf == NULL)
1323 {
1324 EMSG2(_("E158: Invalid buffer name: %s"), filename);
1325 return FAIL;
1326 }
1327
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001328 // If the filename is not supplied for the sign place or the sign jump
1329 // command, then use the current buffer.
Bram Moolenaarb589f952019-01-07 22:10:00 +01001330 if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg)
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001331 || cmd == SIGNCMD_JUMP))
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001332 *buf = curwin->w_buffer;
1333
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001334 return OK;
1335}
1336
1337/*
1338 * ":sign" command
1339 */
1340 void
1341ex_sign(exarg_T *eap)
1342{
1343 char_u *arg = eap->arg;
1344 char_u *p;
1345 int idx;
1346 sign_T *sp;
1347 buf_T *buf = NULL;
1348
1349 // Parse the subcommand.
1350 p = skiptowhite(arg);
1351 idx = sign_cmd_idx(arg, p);
1352 if (idx == SIGNCMD_LAST)
1353 {
1354 EMSG2(_("E160: Unknown sign command: %s"), arg);
1355 return;
1356 }
1357 arg = skipwhite(p);
1358
1359 if (idx <= SIGNCMD_LIST)
1360 {
1361 // Define, undefine or list signs.
1362 if (idx == SIGNCMD_LIST && *arg == NUL)
1363 {
1364 // ":sign list": list all defined signs
1365 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
1366 sign_list_defined(sp);
1367 }
1368 else if (*arg == NUL)
1369 EMSG(_("E156: Missing sign name"));
1370 else
1371 {
1372 char_u *name;
1373
1374 // Isolate the sign name. If it's a number skip leading zeroes,
1375 // so that "099" and "99" are the same sign. But keep "0".
1376 p = skiptowhite(arg);
1377 if (*p != NUL)
1378 *p++ = NUL;
1379 while (arg[0] == '0' && arg[1] != NUL)
1380 ++arg;
1381 name = vim_strsave(arg);
1382
1383 if (idx == SIGNCMD_DEFINE)
1384 sign_define_cmd(name, p);
1385 else if (idx == SIGNCMD_LIST)
1386 // ":sign list {name}"
1387 sign_list_by_name(name);
1388 else
1389 // ":sign undefine {name}"
1390 sign_undefine_by_name(name);
1391
1392 vim_free(name);
1393 return;
1394 }
1395 }
1396 else
1397 {
1398 int id = -1;
1399 linenr_T lnum = -1;
1400 char_u *sign_name = NULL;
1401 char_u *group = NULL;
1402 int prio = SIGN_DEF_PRIO;
1403
1404 // Parse command line arguments
1405 if (parse_sign_cmd_args(idx, arg, &sign_name, &id, &group, &prio,
1406 &buf, &lnum) == FAIL)
1407 return;
1408
1409 if (idx == SIGNCMD_PLACE)
1410 sign_place_cmd(buf, lnum, sign_name, id, group, prio);
1411 else if (idx == SIGNCMD_UNPLACE)
1412 sign_unplace_cmd(buf, lnum, sign_name, id, group);
1413 else if (idx == SIGNCMD_JUMP)
1414 sign_jump_cmd(buf, lnum, sign_name, id, group);
1415 }
1416}
1417
1418/*
1419 * Return information about a specified sign
1420 */
1421 static void
1422sign_getinfo(sign_T *sp, dict_T *retdict)
1423{
1424 char_u *p;
1425
1426 dict_add_string(retdict, "name", (char_u *)sp->sn_name);
1427 if (sp->sn_icon != NULL)
1428 dict_add_string(retdict, "icon", (char_u *)sp->sn_icon);
1429 if (sp->sn_text != NULL)
1430 dict_add_string(retdict, "text", (char_u *)sp->sn_text);
1431 if (sp->sn_line_hl > 0)
1432 {
1433 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1434 if (p == NULL)
1435 p = (char_u *)"NONE";
1436 dict_add_string(retdict, "linehl", (char_u *)p);
1437 }
1438 if (sp->sn_text_hl > 0)
1439 {
1440 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1441 if (p == NULL)
1442 p = (char_u *)"NONE";
1443 dict_add_string(retdict, "texthl", (char_u *)p);
1444 }
1445}
1446
1447/*
1448 * If 'name' is NULL, return a list of all the defined signs.
1449 * Otherwise, return information about the specified sign.
1450 */
1451 void
1452sign_getlist(char_u *name, list_T *retlist)
1453{
1454 sign_T *sp = first_sign;
1455 dict_T *dict;
1456
1457 if (name != NULL)
1458 {
1459 sp = sign_find(name, NULL);
1460 if (sp == NULL)
1461 return;
1462 }
1463
1464 for (; sp != NULL && !got_int; sp = sp->sn_next)
1465 {
1466 if ((dict = dict_alloc_id(aid_sign_getlist)) == NULL)
1467 return;
1468 if (list_append_dict(retlist, dict) == FAIL)
1469 return;
1470 sign_getinfo(sp, dict);
1471
1472 if (name != NULL) // handle only the specified sign
1473 break;
1474 }
1475}
1476
1477/*
1478 * Returns information about signs placed in a buffer as list of dicts.
1479 */
1480 void
1481get_buffer_signs(buf_T *buf, list_T *l)
1482{
1483 signlist_T *sign;
1484 dict_T *d;
1485
1486 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1487 {
1488 if ((d = sign_get_info(sign)) != NULL)
1489 list_append_dict(l, d);
1490 }
1491}
1492
1493/*
1494 * Return information about all the signs placed in a buffer
1495 */
1496 static void
1497sign_get_placed_in_buf(
1498 buf_T *buf,
1499 linenr_T lnum,
1500 int sign_id,
1501 char_u *sign_group,
1502 list_T *retlist)
1503{
1504 dict_T *d;
1505 list_T *l;
1506 signlist_T *sign;
1507 dict_T *sdict;
1508
1509 if ((d = dict_alloc_id(aid_sign_getplaced_dict)) == NULL)
1510 return;
1511 list_append_dict(retlist, d);
1512
1513 dict_add_number(d, "bufnr", (long)buf->b_fnum);
1514
1515 if ((l = list_alloc_id(aid_sign_getplaced_list)) == NULL)
1516 return;
1517 dict_add_list(d, "signs", l);
1518
1519 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1520 {
1521 if (!sign_in_group(sign, sign_group))
1522 continue;
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001523 if ((lnum == 0 && sign_id == 0)
1524 || (sign_id == 0 && lnum == sign->lnum)
1525 || (lnum == 0 && sign_id == sign->id)
1526 || (lnum == sign->lnum && sign_id == sign->id))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001527 {
1528 if ((sdict = sign_get_info(sign)) != NULL)
1529 list_append_dict(l, sdict);
1530 }
1531 }
1532}
1533
1534/*
1535 * Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
1536 * sign placed at the line number. If 'lnum' is zero, return all the signs
1537 * placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
1538 */
1539 void
1540sign_get_placed(
1541 buf_T *buf,
1542 linenr_T lnum,
1543 int sign_id,
1544 char_u *sign_group,
1545 list_T *retlist)
1546{
1547 if (buf != NULL)
1548 sign_get_placed_in_buf(buf, lnum, sign_id, sign_group, retlist);
1549 else
1550 {
1551 FOR_ALL_BUFFERS(buf)
1552 {
1553 if (buf->b_signlist != NULL)
1554 sign_get_placed_in_buf(buf, 0, sign_id, sign_group, retlist);
1555 }
1556 }
1557}
1558
1559# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1560/*
1561 * Allocate the icons. Called when the GUI has started. Allows defining
1562 * signs before it starts.
1563 */
1564 void
1565sign_gui_started(void)
1566{
1567 sign_T *sp;
1568
1569 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1570 if (sp->sn_icon != NULL)
1571 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
1572}
1573# endif
1574
1575/*
1576 * List one sign.
1577 */
1578 static void
1579sign_list_defined(sign_T *sp)
1580{
1581 char_u *p;
1582
1583 smsg((char_u *)"sign %s", sp->sn_name);
1584 if (sp->sn_icon != NULL)
1585 {
1586 MSG_PUTS(" icon=");
1587 msg_outtrans(sp->sn_icon);
1588# ifdef FEAT_SIGN_ICONS
1589 if (sp->sn_image == NULL)
1590 MSG_PUTS(_(" (NOT FOUND)"));
1591# else
1592 MSG_PUTS(_(" (not supported)"));
1593# endif
1594 }
1595 if (sp->sn_text != NULL)
1596 {
1597 MSG_PUTS(" text=");
1598 msg_outtrans(sp->sn_text);
1599 }
1600 if (sp->sn_line_hl > 0)
1601 {
1602 MSG_PUTS(" linehl=");
1603 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1604 if (p == NULL)
1605 MSG_PUTS("NONE");
1606 else
1607 msg_puts(p);
1608 }
1609 if (sp->sn_text_hl > 0)
1610 {
1611 MSG_PUTS(" texthl=");
1612 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1613 if (p == NULL)
1614 MSG_PUTS("NONE");
1615 else
1616 msg_puts(p);
1617 }
1618}
1619
1620/*
1621 * Undefine a sign and free its memory.
1622 */
1623 static void
1624sign_undefine(sign_T *sp, sign_T *sp_prev)
1625{
1626 vim_free(sp->sn_name);
1627 vim_free(sp->sn_icon);
1628# ifdef FEAT_SIGN_ICONS
1629 if (sp->sn_image != NULL)
1630 {
1631 out_flush();
1632 gui_mch_destroy_sign(sp->sn_image);
1633 }
1634# endif
1635 vim_free(sp->sn_text);
1636 if (sp_prev == NULL)
1637 first_sign = sp->sn_next;
1638 else
1639 sp_prev->sn_next = sp->sn_next;
1640 vim_free(sp);
1641}
1642
1643/*
1644 * Get highlighting attribute for sign "typenr".
1645 * If "line" is TRUE: line highl, if FALSE: text highl.
1646 */
1647 int
1648sign_get_attr(int typenr, int line)
1649{
1650 sign_T *sp;
1651
1652 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1653 if (sp->sn_typenr == typenr)
1654 {
1655 if (line)
1656 {
1657 if (sp->sn_line_hl > 0)
1658 return syn_id2attr(sp->sn_line_hl);
1659 }
1660 else
1661 {
1662 if (sp->sn_text_hl > 0)
1663 return syn_id2attr(sp->sn_text_hl);
1664 }
1665 break;
1666 }
1667 return 0;
1668}
1669
1670/*
1671 * Get text mark for sign "typenr".
1672 * Returns NULL if there isn't one.
1673 */
1674 char_u *
1675sign_get_text(int typenr)
1676{
1677 sign_T *sp;
1678
1679 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1680 if (sp->sn_typenr == typenr)
1681 return sp->sn_text;
1682 return NULL;
1683}
1684
1685# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1686 void *
1687sign_get_image(
1688 int typenr) /* the attribute which may have a sign */
1689{
1690 sign_T *sp;
1691
1692 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1693 if (sp->sn_typenr == typenr)
1694 return sp->sn_image;
1695 return NULL;
1696}
1697# endif
1698
1699/*
1700 * Undefine/free all signs.
1701 */
1702 void
1703free_signs(void)
1704{
1705 while (first_sign != NULL)
1706 sign_undefine(first_sign, NULL);
1707}
1708
1709# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1710static enum
1711{
1712 EXP_SUBCMD, /* expand :sign sub-commands */
1713 EXP_DEFINE, /* expand :sign define {name} args */
1714 EXP_PLACE, /* expand :sign place {id} args */
1715 EXP_UNPLACE, /* expand :sign unplace" */
1716 EXP_SIGN_NAMES /* expand with name of placed signs */
1717} expand_what;
1718
1719/*
1720 * Function given to ExpandGeneric() to obtain the sign command
1721 * expansion.
1722 */
1723 char_u *
1724get_sign_name(expand_T *xp UNUSED, int idx)
1725{
1726 sign_T *sp;
1727 int current_idx;
1728
1729 switch (expand_what)
1730 {
1731 case EXP_SUBCMD:
1732 return (char_u *)cmds[idx];
1733 case EXP_DEFINE:
1734 {
1735 char *define_arg[] =
1736 {
1737 "icon=", "linehl=", "text=", "texthl=", NULL
1738 };
1739 return (char_u *)define_arg[idx];
1740 }
1741 case EXP_PLACE:
1742 {
1743 char *place_arg[] =
1744 {
1745 "line=", "name=", "group=", "priority=", "file=",
1746 "buffer=", NULL
1747 };
1748 return (char_u *)place_arg[idx];
1749 }
1750 case EXP_UNPLACE:
1751 {
1752 char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1753 return (char_u *)unplace_arg[idx];
1754 }
1755 case EXP_SIGN_NAMES:
1756 /* Complete with name of signs already defined */
1757 current_idx = 0;
1758 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1759 if (current_idx++ == idx)
1760 return sp->sn_name;
1761 return NULL;
1762 default:
1763 return NULL;
1764 }
1765}
1766
1767/*
1768 * Handle command line completion for :sign command.
1769 */
1770 void
1771set_context_in_sign_cmd(expand_T *xp, char_u *arg)
1772{
1773 char_u *p;
1774 char_u *end_subcmd;
1775 char_u *last;
1776 int cmd_idx;
1777 char_u *begin_subcmd_args;
1778
1779 /* Default: expand subcommands. */
1780 xp->xp_context = EXPAND_SIGN;
1781 expand_what = EXP_SUBCMD;
1782 xp->xp_pattern = arg;
1783
1784 end_subcmd = skiptowhite(arg);
1785 if (*end_subcmd == NUL)
1786 /* expand subcmd name
1787 * :sign {subcmd}<CTRL-D>*/
1788 return;
1789
1790 cmd_idx = sign_cmd_idx(arg, end_subcmd);
1791
1792 /* :sign {subcmd} {subcmd_args}
1793 * |
1794 * begin_subcmd_args */
1795 begin_subcmd_args = skipwhite(end_subcmd);
1796 p = skiptowhite(begin_subcmd_args);
1797 if (*p == NUL)
1798 {
1799 /*
1800 * Expand first argument of subcmd when possible.
1801 * For ":jump {id}" and ":unplace {id}", we could
1802 * possibly expand the ids of all signs already placed.
1803 */
1804 xp->xp_pattern = begin_subcmd_args;
1805 switch (cmd_idx)
1806 {
1807 case SIGNCMD_LIST:
1808 case SIGNCMD_UNDEFINE:
1809 /* :sign list <CTRL-D>
1810 * :sign undefine <CTRL-D> */
1811 expand_what = EXP_SIGN_NAMES;
1812 break;
1813 default:
1814 xp->xp_context = EXPAND_NOTHING;
1815 }
1816 return;
1817 }
1818
1819 /* expand last argument of subcmd */
1820
1821 /* :sign define {name} {args}...
1822 * |
1823 * p */
1824
1825 /* Loop until reaching last argument. */
1826 do
1827 {
1828 p = skipwhite(p);
1829 last = p;
1830 p = skiptowhite(p);
1831 } while (*p != NUL);
1832
1833 p = vim_strchr(last, '=');
1834
1835 /* :sign define {name} {args}... {last}=
1836 * | |
1837 * last p */
1838 if (p == NULL)
1839 {
1840 /* Expand last argument name (before equal sign). */
1841 xp->xp_pattern = last;
1842 switch (cmd_idx)
1843 {
1844 case SIGNCMD_DEFINE:
1845 expand_what = EXP_DEFINE;
1846 break;
1847 case SIGNCMD_PLACE:
1848 expand_what = EXP_PLACE;
1849 break;
1850 case SIGNCMD_JUMP:
1851 case SIGNCMD_UNPLACE:
1852 expand_what = EXP_UNPLACE;
1853 break;
1854 default:
1855 xp->xp_context = EXPAND_NOTHING;
1856 }
1857 }
1858 else
1859 {
1860 /* Expand last argument value (after equal sign). */
1861 xp->xp_pattern = p + 1;
1862 switch (cmd_idx)
1863 {
1864 case SIGNCMD_DEFINE:
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001865 if (STRNCMP(last, "texthl", p - last) == 0
1866 || STRNCMP(last, "linehl", p - last) == 0)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001867 xp->xp_context = EXPAND_HIGHLIGHT;
1868 else if (STRNCMP(last, "icon", p - last) == 0)
1869 xp->xp_context = EXPAND_FILES;
1870 else
1871 xp->xp_context = EXPAND_NOTHING;
1872 break;
1873 case SIGNCMD_PLACE:
1874 if (STRNCMP(last, "name", p - last) == 0)
1875 expand_what = EXP_SIGN_NAMES;
1876 else
1877 xp->xp_context = EXPAND_NOTHING;
1878 break;
1879 default:
1880 xp->xp_context = EXPAND_NOTHING;
1881 }
1882 }
1883}
1884# endif
1885
1886#endif /* FEAT_SIGNS */