blob: 734dd257e04f8a6ffd0e001a0730fb7821b96b8d [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{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010025 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
Bram Moolenaarbbea4702019-01-01 13:20:31 +010029# ifdef FEAT_SIGN_ICONS
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010030 void *sn_image; // icon image
Bram Moolenaarbbea4702019-01-01 13:20:31 +010031# endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010032 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
Bram Moolenaarbbea4702019-01-01 13:20:31 +010035};
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'
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100377 * which has the attribute specified by 'type'. Returns 0 if a sign is not
378 * found at the line number or it doesn't have the specified attribute.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100379 */
380 int
381buf_getsigntype(
382 buf_T *buf,
383 linenr_T lnum,
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100384 int type) // SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100385{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100386 signlist_T *sign; // a sign in a b_signlist
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100387
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
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100445 // If deleting a sign with a specific identifier in a particular
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100446 // 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
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100458 // sign columns no longer shows. And the 'signcolumn' may be hidden.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100459 if (buf->b_signlist == NULL)
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100460 {
461 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100462 changed_cline_bef_curs();
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100463 }
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100464
465 return lnum;
466}
467
468
469/*
470 * Find the line number of the sign with the requested id in group 'group'. If
471 * the sign does not exist, return 0 as the line number. This will still let
472 * the correct file get loaded.
473 */
474 int
475buf_findsign(
476 buf_T *buf, // buffer to store sign in
477 int id, // sign ID
478 char_u *group) // sign group
479{
480 signlist_T *sign; // a sign in the signlist
481
482 FOR_ALL_SIGNS_IN_BUF(buf, sign)
483 if (sign->id == id && sign_in_group(sign, group))
484 return sign->lnum;
485
486 return 0;
487}
488
489/*
490 * Return the sign at line 'lnum' in buffer 'buf'. Returns NULL if a sign is
491 * not found at the line. If 'groupname' is NULL, searches in the global group.
492 */
493 static signlist_T *
494buf_getsign_at_line(
495 buf_T *buf, // buffer whose sign we are searching for
496 linenr_T lnum, // line number of sign
497 char_u *groupname) // sign group name
498{
499 signlist_T *sign; // a sign in the signlist
500
501 FOR_ALL_SIGNS_IN_BUF(buf, sign)
502 if (sign->lnum == lnum && sign_in_group(sign, groupname))
503 return sign;
504
505 return NULL;
506}
507
508/*
509 * Return the identifier of the sign at line number 'lnum' in buffer 'buf'.
510 */
511 int
512buf_findsign_id(
513 buf_T *buf, // buffer whose sign we are searching for
514 linenr_T lnum, // line number of sign
515 char_u *groupname) // sign group name
516{
517 signlist_T *sign; // a sign in the signlist
518
519 sign = buf_getsign_at_line(buf, lnum, groupname);
520 if (sign != NULL)
521 return sign->id;
522
523 return 0;
524}
525
526# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
527/*
528 * See if a given type of sign exists on a specific line.
529 */
530 int
531buf_findsigntype_id(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100532 buf_T *buf, // buffer whose sign we are searching for
533 linenr_T lnum, // line number of sign
534 int typenr) // sign type number
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100535{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100536 signlist_T *sign; // a sign in the signlist
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100537
538 FOR_ALL_SIGNS_IN_BUF(buf, sign)
539 if (sign->lnum == lnum && sign->typenr == typenr)
540 return sign->id;
541
542 return 0;
543}
544
545
546# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
547/*
548 * Return the number of icons on the given line.
549 */
550 int
551buf_signcount(buf_T *buf, linenr_T lnum)
552{
553 signlist_T *sign; // a sign in the signlist
554 int count = 0;
555
556 FOR_ALL_SIGNS_IN_BUF(buf, sign)
557 if (sign->lnum == lnum)
558 if (sign_get_image(sign->typenr) != NULL)
559 count++;
560
561 return count;
562}
563# endif /* FEAT_SIGN_ICONS */
564# endif /* FEAT_NETBEANS_INTG */
565
566/*
567 * Delete signs in group 'group' in buffer "buf". If 'group' is '*', then
568 * delete all the signs.
569 */
570 void
571buf_delete_signs(buf_T *buf, char_u *group)
572{
573 signlist_T *sign;
574 signlist_T **lastp; // pointer to pointer to current sign
575 signlist_T *next;
576
577 // When deleting the last sign need to redraw the windows to remove the
578 // sign column. Not when curwin is NULL (this means we're exiting).
579 if (buf->b_signlist != NULL && curwin != NULL)
580 {
581 redraw_buf_later(buf, NOT_VALID);
582 changed_cline_bef_curs();
583 }
584
585 lastp = &buf->b_signlist;
586 for (sign = buf->b_signlist; sign != NULL; sign = next)
587 {
588 next = sign->next;
589 if (sign_in_group(sign, group))
590 {
591 *lastp = next;
592 if (next != NULL)
593 next->prev = sign->prev;
594 if (sign->group != NULL)
595 sign_group_unref(sign->group->sg_name);
596 vim_free(sign);
597 }
598 else
599 lastp = &sign->next;
600 }
601}
602
603/*
604 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
605 */
606 static void
607sign_list_placed(buf_T *rbuf, char_u *sign_group)
608{
609 buf_T *buf;
610 signlist_T *sign;
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100611 char lbuf[MSG_BUF_LEN];
612 char group[MSG_BUF_LEN];
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100613
614 MSG_PUTS_TITLE(_("\n--- Signs ---"));
615 msg_putchar('\n');
616 if (rbuf == NULL)
617 buf = firstbuf;
618 else
619 buf = rbuf;
620 while (buf != NULL && !got_int)
621 {
622 if (buf->b_signlist != NULL)
623 {
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100624 vim_snprintf(lbuf, MSG_BUF_LEN, _("Signs for %s:"), buf->b_fname);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100625 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
626 msg_putchar('\n');
627 }
628 FOR_ALL_SIGNS_IN_BUF(buf, sign)
629 {
630 if (got_int)
631 break;
632 if (!sign_in_group(sign, sign_group))
633 continue;
634 if (sign->group != NULL)
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100635 vim_snprintf(group, MSG_BUF_LEN, _(" group=%s"),
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100636 sign->group->sg_name);
637 else
638 group[0] = '\0';
Bram Moolenaard730c8e2019-01-07 21:16:53 +0100639 vim_snprintf(lbuf, MSG_BUF_LEN,
640 _(" line=%ld id=%d%s name=%s priority=%d"),
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100641 (long)sign->lnum, sign->id, group,
642 sign_typenr2name(sign->typenr), sign->priority);
643 MSG_PUTS(lbuf);
644 msg_putchar('\n');
645 }
646 if (rbuf != NULL)
647 break;
648 buf = buf->b_next;
649 }
650}
651
652/*
653 * Adjust a placed sign for inserted/deleted lines.
654 */
655 void
656sign_mark_adjust(
657 linenr_T line1,
658 linenr_T line2,
659 long amount,
660 long amount_after)
661{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100662 signlist_T *sign; // a sign in a b_signlist
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100663
664 FOR_ALL_SIGNS_IN_BUF(curbuf, sign)
665 {
666 if (sign->lnum >= line1 && sign->lnum <= line2)
667 {
668 if (amount == MAXLNUM)
669 sign->lnum = line1;
670 else
671 sign->lnum += amount;
672 }
673 else if (sign->lnum > line2)
674 sign->lnum += amount_after;
675 }
676}
677
678/*
679 * Find index of a ":sign" subcmd from its name.
680 * "*end_cmd" must be writable.
681 */
682 static int
683sign_cmd_idx(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100684 char_u *begin_cmd, // begin of sign subcmd
685 char_u *end_cmd) // just after sign subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100686{
687 int idx;
688 char save = *end_cmd;
689
690 *end_cmd = NUL;
691 for (idx = 0; ; ++idx)
692 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
693 break;
694 *end_cmd = save;
695 return idx;
696}
697
698/*
699 * Find a sign by name. Also returns pointer to the previous sign.
700 */
701 static sign_T *
702sign_find(char_u *name, sign_T **sp_prev)
703{
704 sign_T *sp;
705
706 if (sp_prev != NULL)
707 *sp_prev = NULL;
708 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
709 {
710 if (STRCMP(sp->sn_name, name) == 0)
711 break;
712 if (sp_prev != NULL)
713 *sp_prev = sp;
714 }
715
716 return sp;
717}
718
719/*
720 * Define a new sign or update an existing sign
721 */
722 int
723sign_define_by_name(
724 char_u *name,
725 char_u *icon,
726 char_u *linehl,
727 char_u *text,
728 char_u *texthl)
729{
730 sign_T *sp_prev;
731 sign_T *sp;
732
733 sp = sign_find(name, &sp_prev);
734 if (sp == NULL)
735 {
736 sign_T *lp;
737 int start = next_sign_typenr;
738
739 // Allocate a new sign.
740 sp = (sign_T *)alloc_clear_id((unsigned)sizeof(sign_T),
741 aid_sign_define_by_name);
742 if (sp == NULL)
743 return FAIL;
744
745 // Check that next_sign_typenr is not already being used.
746 // This only happens after wrapping around. Hopefully
747 // another one got deleted and we can use its number.
748 for (lp = first_sign; lp != NULL; )
749 {
750 if (lp->sn_typenr == next_sign_typenr)
751 {
752 ++next_sign_typenr;
753 if (next_sign_typenr == MAX_TYPENR)
754 next_sign_typenr = 1;
755 if (next_sign_typenr == start)
756 {
757 vim_free(sp);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758 emsg(_("E612: Too many signs defined"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100759 return FAIL;
760 }
761 lp = first_sign; // start all over
762 continue;
763 }
764 lp = lp->sn_next;
765 }
766
767 sp->sn_typenr = next_sign_typenr;
768 if (++next_sign_typenr == MAX_TYPENR)
769 next_sign_typenr = 1; // wrap around
770
771 sp->sn_name = vim_strsave(name);
772 if (sp->sn_name == NULL) // out of memory
773 {
774 vim_free(sp);
775 return FAIL;
776 }
777
778 // add the new sign to the list of signs
779 if (sp_prev == NULL)
780 first_sign = sp;
781 else
782 sp_prev->sn_next = sp;
783 }
784
785 // set values for a defined sign.
786 if (icon != NULL)
787 {
788 vim_free(sp->sn_icon);
789 sp->sn_icon = vim_strsave(icon);
790 backslash_halve(sp->sn_icon);
791# ifdef FEAT_SIGN_ICONS
792 if (gui.in_use)
793 {
794 out_flush();
795 if (sp->sn_image != NULL)
796 gui_mch_destroy_sign(sp->sn_image);
797 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
798 }
799# endif
800 }
801
802 if (text != NULL)
803 {
804 char_u *s;
805 char_u *endp;
806 int cells;
807 int len;
808
809 endp = text + (int)STRLEN(text);
810 for (s = text; s + 1 < endp; ++s)
811 if (*s == '\\')
812 {
813 // Remove a backslash, so that it is possible
814 // to use a space.
815 STRMOVE(s, s + 1);
816 --endp;
817 }
818# ifdef FEAT_MBYTE
819 // Count cells and check for non-printable chars
820 if (has_mbyte)
821 {
822 cells = 0;
823 for (s = text; s < endp; s += (*mb_ptr2len)(s))
824 {
825 if (!vim_isprintc((*mb_ptr2char)(s)))
826 break;
827 cells += (*mb_ptr2cells)(s);
828 }
829 }
830 else
831# endif
832 {
833 for (s = text; s < endp; ++s)
834 if (!vim_isprintc(*s))
835 break;
836 cells = (int)(s - text);
837 }
838 // Currently must be one or two display cells
839 if (s != endp || cells < 1 || cells > 2)
840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100841 semsg(_("E239: Invalid sign text: %s"), text);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100842 return FAIL;
843 }
844
845 vim_free(sp->sn_text);
846 // Allocate one byte more if we need to pad up
847 // with a space.
848 len = (int)(endp - text + ((cells == 1) ? 1 : 0));
849 sp->sn_text = vim_strnsave(text, len);
850
851 if (sp->sn_text != NULL && cells == 1)
852 STRCPY(sp->sn_text + len - 1, " ");
853 }
854
855 if (linehl != NULL)
856 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl));
857
858 if (texthl != NULL)
859 sp->sn_text_hl = syn_check_group(texthl, (int)STRLEN(texthl));
860
861 return OK;
862}
863
864/*
865 * Free the sign specified by 'name'.
866 */
867 int
868sign_undefine_by_name(char_u *name)
869{
870 sign_T *sp_prev;
871 sign_T *sp;
872
873 sp = sign_find(name, &sp_prev);
874 if (sp == NULL)
875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100877 return FAIL;
878 }
879 sign_undefine(sp, sp_prev);
880
881 return OK;
882}
883
884/*
885 * List the signs matching 'name'
886 */
887 static void
888sign_list_by_name(char_u *name)
889{
890 sign_T *sp;
891
892 sp = sign_find(name, NULL);
893 if (sp != NULL)
894 sign_list_defined(sp);
895 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100896 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100897}
898
899/*
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100900 * Place a sign at the specified file location or update a sign.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100901 */
902 int
903sign_place(
904 int *sign_id,
905 char_u *sign_group,
906 char_u *sign_name,
907 buf_T *buf,
908 linenr_T lnum,
909 int prio)
910{
911 sign_T *sp;
912
913 // Check for reserved character '*' in group name
914 if (sign_group != NULL && (*sign_group == '*' || *sign_group == '\0'))
915 return FAIL;
916
917 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
918 if (STRCMP(sp->sn_name, sign_name) == 0)
919 break;
920 if (sp == NULL)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 semsg(_("E155: Unknown sign: %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100923 return FAIL;
924 }
925 if (*sign_id == 0)
926 *sign_id = sign_group_get_next_signid(buf, sign_group);
927
928 if (lnum > 0)
929 // ":sign place {id} line={lnum} name={name} file={fname}":
930 // place a sign
931 buf_addsign(buf, *sign_id, sign_group, prio, lnum, sp->sn_typenr);
932 else
933 // ":sign place {id} file={fname}": change sign type
934 lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr);
935 if (lnum > 0)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100936 redraw_buf_line_later(buf, lnum);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100937 else
938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 semsg(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100940 return FAIL;
941 }
942
943 return OK;
944}
945
946/*
947 * Unplace the specified sign
948 */
949 int
950sign_unplace(int sign_id, char_u *sign_group, buf_T *buf, linenr_T atlnum)
951{
952 if (buf->b_signlist == NULL) // No signs in the buffer
953 return OK;
954
955 if (sign_id == 0)
956 {
957 // Delete all the signs in the specified buffer
958 redraw_buf_later(buf, NOT_VALID);
959 buf_delete_signs(buf, sign_group);
960 }
961 else
962 {
963 linenr_T lnum;
964
965 // Delete only the specified signs
966 lnum = buf_delsign(buf, atlnum, sign_id, sign_group);
967 if (lnum == 0)
968 return FAIL;
969 }
970
971 return OK;
972}
973
974/*
975 * Unplace the sign at the current cursor line.
976 */
977 static void
978sign_unplace_at_cursor(char_u *groupname)
979{
980 int id = -1;
981
982 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum, groupname);
983 if (id > 0)
984 sign_unplace(id, groupname, curwin->w_buffer, curwin->w_cursor.lnum);
985 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 emsg(_("E159: Missing sign number"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100987}
988
989/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100990 * Jump to a sign.
991 */
992 linenr_T
993sign_jump(int sign_id, char_u *sign_group, buf_T *buf)
994{
995 linenr_T lnum;
996
997 if ((lnum = buf_findsign(buf, sign_id, sign_group)) <= 0)
998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100999 semsg(_("E157: Invalid sign ID: %ld"), sign_id);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001000 return -1;
1001 }
1002
1003 // goto a sign ...
1004 if (buf_jump_open_win(buf) != NULL)
1005 { // ... in a current window
1006 curwin->w_cursor.lnum = lnum;
1007 check_cursor_lnum();
1008 beginline(BL_WHITE);
1009 }
1010 else
1011 { // ... not currently in a window
1012 char_u *cmd;
1013
1014 if (buf->b_fname == NULL)
1015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 emsg(_("E934: Cannot jump to a buffer that does not have a name"));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001017 return -1;
1018 }
1019 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
1020 if (cmd == NULL)
1021 return -1;
1022 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
1023 do_cmdline_cmd(cmd);
1024 vim_free(cmd);
1025 }
1026# ifdef FEAT_FOLDING
1027 foldOpenCursor();
1028# endif
1029
1030 return lnum;
1031}
1032
1033/*
1034 * ":sign define {name} ..." command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001035 */
1036 static void
1037sign_define_cmd(char_u *sign_name, char_u *cmdline)
1038{
1039 char_u *arg;
1040 char_u *p = cmdline;
1041 char_u *icon = NULL;
1042 char_u *text = NULL;
1043 char_u *linehl = NULL;
1044 char_u *texthl = NULL;
1045 int failed = FALSE;
1046
1047 // set values for a defined sign.
1048 for (;;)
1049 {
1050 arg = skipwhite(p);
1051 if (*arg == NUL)
1052 break;
1053 p = skiptowhite_esc(arg);
1054 if (STRNCMP(arg, "icon=", 5) == 0)
1055 {
1056 arg += 5;
1057 icon = vim_strnsave(arg, (int)(p - arg));
1058 }
1059 else if (STRNCMP(arg, "text=", 5) == 0)
1060 {
1061 arg += 5;
1062 text = vim_strnsave(arg, (int)(p - arg));
1063 }
1064 else if (STRNCMP(arg, "linehl=", 7) == 0)
1065 {
1066 arg += 7;
1067 linehl = vim_strnsave(arg, (int)(p - arg));
1068 }
1069 else if (STRNCMP(arg, "texthl=", 7) == 0)
1070 {
1071 arg += 7;
1072 texthl = vim_strnsave(arg, (int)(p - arg));
1073 }
1074 else
1075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001076 semsg(_(e_invarg2), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001077 failed = TRUE;
1078 break;
1079 }
1080 }
1081
1082 if (!failed)
1083 sign_define_by_name(sign_name, icon, linehl, text, texthl);
1084
1085 vim_free(icon);
1086 vim_free(text);
1087 vim_free(linehl);
1088 vim_free(texthl);
1089}
1090
1091/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001092 * ":sign place" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001093 */
1094 static void
1095sign_place_cmd(
1096 buf_T *buf,
1097 linenr_T lnum,
1098 char_u *sign_name,
1099 int id,
1100 char_u *group,
1101 int prio)
1102{
1103 if (id <= 0)
1104 {
1105 // List signs placed in a file/buffer
1106 // :sign place file={fname}
1107 // :sign place group={group} file={fname}
1108 // :sign place group=* file={fname}
1109 // :sign place buffer={nr}
1110 // :sign place group={group} buffer={nr}
1111 // :sign place group=* buffer={nr}
1112 // :sign place
1113 // :sign place group={group}
1114 // :sign place group=*
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001115 if (lnum >= 0 || sign_name != NULL
1116 || (group != NULL && *group == '\0'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001118 else
1119 sign_list_placed(buf, group);
1120 }
1121 else
1122 {
1123 // Place a new sign
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001124 if (sign_name == NULL || buf == NULL
1125 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001128 return;
1129 }
1130
1131 sign_place(&id, group, sign_name, buf, lnum, prio);
1132 }
1133}
1134
1135/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001136 * ":sign unplace" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001137 */
1138 static void
1139sign_unplace_cmd(
1140 buf_T *buf,
1141 linenr_T lnum,
1142 char_u *sign_name,
1143 int id,
1144 char_u *group)
1145{
1146 if (lnum >= 0 || sign_name != NULL || (group != NULL && *group == '\0'))
1147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001149 return;
1150 }
1151
1152 if (id == -2)
1153 {
1154 if (buf != NULL)
1155 // :sign unplace * file={fname}
1156 // :sign unplace * group={group} file={fname}
1157 // :sign unplace * group=* file={fname}
1158 // :sign unplace * buffer={nr}
1159 // :sign unplace * group={group} buffer={nr}
1160 // :sign unplace * group=* buffer={nr}
1161 sign_unplace(0, group, buf, 0);
1162 else
1163 // :sign unplace *
1164 // :sign unplace * group={group}
1165 // :sign unplace * group=*
1166 FOR_ALL_BUFFERS(buf)
1167 if (buf->b_signlist != NULL)
1168 buf_delete_signs(buf, group);
1169 }
1170 else
1171 {
1172 if (buf != NULL)
1173 // :sign unplace {id} file={fname}
1174 // :sign unplace {id} group={group} file={fname}
1175 // :sign unplace {id} group=* file={fname}
1176 // :sign unplace {id} buffer={nr}
1177 // :sign unplace {id} group={group} buffer={nr}
1178 // :sign unplace {id} group=* buffer={nr}
1179 sign_unplace(id, group, buf, 0);
1180 else
1181 {
1182 if (id == -1)
1183 {
1184 // :sign unplace group={group}
1185 // :sign unplace group=*
1186 sign_unplace_at_cursor(group);
1187 }
1188 else
1189 {
1190 // :sign unplace {id}
1191 // :sign unplace {id} group={group}
1192 // :sign unplace {id} group=*
1193 FOR_ALL_BUFFERS(buf)
1194 sign_unplace(id, group, buf, 0);
1195 }
1196 }
1197 }
1198}
1199
1200/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001201 * Jump to a placed sign commands:
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001202 * :sign jump {id} file={fname}
1203 * :sign jump {id} buffer={nr}
1204 * :sign jump {id} group={group} file={fname}
1205 * :sign jump {id} group={group} buffer={nr}
1206 */
1207 static void
1208sign_jump_cmd(
1209 buf_T *buf,
1210 linenr_T lnum,
1211 char_u *sign_name,
1212 int id,
1213 char_u *group)
1214{
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001215 if (sign_name == NULL && group == NULL && id == -1)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_(e_argreq));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001218 return;
1219 }
1220
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001221 if (buf == NULL || (group != NULL && *group == '\0')
1222 || lnum >= 0 || sign_name != NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001223 {
1224 // File or buffer is not specified or an empty group is used
1225 // or a line number or a sign name is specified.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001227 return;
1228 }
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001229 (void)sign_jump(id, group, buf);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001230}
1231
1232/*
1233 * Parse the command line arguments for the ":sign place", ":sign unplace" and
1234 * ":sign jump" commands.
1235 * The supported arguments are: line={lnum} name={name} group={group}
1236 * priority={prio} and file={fname} or buffer={nr}.
1237 */
1238 static int
1239parse_sign_cmd_args(
1240 int cmd,
1241 char_u *arg,
1242 char_u **sign_name,
1243 int *signid,
1244 char_u **group,
1245 int *prio,
1246 buf_T **buf,
1247 linenr_T *lnum)
1248{
1249 char_u *arg1;
1250 char_u *name;
1251 char_u *filename = NULL;
Bram Moolenaarb589f952019-01-07 22:10:00 +01001252 int lnum_arg = FALSE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001253
1254 // first arg could be placed sign id
1255 arg1 = arg;
1256 if (VIM_ISDIGIT(*arg))
1257 {
1258 *signid = getdigits(&arg);
1259 if (!VIM_ISWHITE(*arg) && *arg != NUL)
1260 {
1261 *signid = -1;
1262 arg = arg1;
1263 }
1264 else
1265 arg = skipwhite(arg);
1266 }
1267
1268 while (*arg != NUL)
1269 {
1270 if (STRNCMP(arg, "line=", 5) == 0)
1271 {
1272 arg += 5;
1273 *lnum = atoi((char *)arg);
1274 arg = skiptowhite(arg);
Bram Moolenaarb589f952019-01-07 22:10:00 +01001275 lnum_arg = TRUE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001276 }
1277 else if (STRNCMP(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE)
1278 {
1279 if (*signid != -1)
1280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001281 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001282 return FAIL;
1283 }
1284 *signid = -2;
1285 arg = skiptowhite(arg + 1);
1286 }
1287 else if (STRNCMP(arg, "name=", 5) == 0)
1288 {
1289 arg += 5;
1290 name = arg;
1291 arg = skiptowhite(arg);
1292 if (*arg != NUL)
1293 *arg++ = NUL;
1294 while (name[0] == '0' && name[1] != NUL)
1295 ++name;
1296 *sign_name = name;
1297 }
1298 else if (STRNCMP(arg, "group=", 6) == 0)
1299 {
1300 arg += 6;
1301 *group = arg;
1302 arg = skiptowhite(arg);
1303 if (*arg != NUL)
1304 *arg++ = NUL;
1305 }
1306 else if (STRNCMP(arg, "priority=", 9) == 0)
1307 {
1308 arg += 9;
1309 *prio = atoi((char *)arg);
1310 arg = skiptowhite(arg);
1311 }
1312 else if (STRNCMP(arg, "file=", 5) == 0)
1313 {
1314 arg += 5;
1315 filename = arg;
1316 *buf = buflist_findname_exp(arg);
1317 break;
1318 }
1319 else if (STRNCMP(arg, "buffer=", 7) == 0)
1320 {
1321 arg += 7;
1322 filename = arg;
1323 *buf = buflist_findnr((int)getdigits(&arg));
1324 if (*skipwhite(arg) != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001325 emsg(_(e_trailing));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001326 break;
1327 }
1328 else
1329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001330 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001331 return FAIL;
1332 }
1333 arg = skipwhite(arg);
1334 }
1335
1336 if (filename != NULL && *buf == NULL)
1337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001338 semsg(_("E158: Invalid buffer name: %s"), filename);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001339 return FAIL;
1340 }
1341
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001342 // If the filename is not supplied for the sign place or the sign jump
1343 // command, then use the current buffer.
Bram Moolenaarb589f952019-01-07 22:10:00 +01001344 if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg)
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001345 || cmd == SIGNCMD_JUMP))
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001346 *buf = curwin->w_buffer;
1347
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001348 return OK;
1349}
1350
1351/*
1352 * ":sign" command
1353 */
1354 void
1355ex_sign(exarg_T *eap)
1356{
1357 char_u *arg = eap->arg;
1358 char_u *p;
1359 int idx;
1360 sign_T *sp;
1361 buf_T *buf = NULL;
1362
1363 // Parse the subcommand.
1364 p = skiptowhite(arg);
1365 idx = sign_cmd_idx(arg, p);
1366 if (idx == SIGNCMD_LAST)
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 semsg(_("E160: Unknown sign command: %s"), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001369 return;
1370 }
1371 arg = skipwhite(p);
1372
1373 if (idx <= SIGNCMD_LIST)
1374 {
1375 // Define, undefine or list signs.
1376 if (idx == SIGNCMD_LIST && *arg == NUL)
1377 {
1378 // ":sign list": list all defined signs
1379 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
1380 sign_list_defined(sp);
1381 }
1382 else if (*arg == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001383 emsg(_("E156: Missing sign name"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001384 else
1385 {
1386 char_u *name;
1387
1388 // Isolate the sign name. If it's a number skip leading zeroes,
1389 // so that "099" and "99" are the same sign. But keep "0".
1390 p = skiptowhite(arg);
1391 if (*p != NUL)
1392 *p++ = NUL;
1393 while (arg[0] == '0' && arg[1] != NUL)
1394 ++arg;
1395 name = vim_strsave(arg);
1396
1397 if (idx == SIGNCMD_DEFINE)
1398 sign_define_cmd(name, p);
1399 else if (idx == SIGNCMD_LIST)
1400 // ":sign list {name}"
1401 sign_list_by_name(name);
1402 else
1403 // ":sign undefine {name}"
1404 sign_undefine_by_name(name);
1405
1406 vim_free(name);
1407 return;
1408 }
1409 }
1410 else
1411 {
1412 int id = -1;
1413 linenr_T lnum = -1;
1414 char_u *sign_name = NULL;
1415 char_u *group = NULL;
1416 int prio = SIGN_DEF_PRIO;
1417
1418 // Parse command line arguments
1419 if (parse_sign_cmd_args(idx, arg, &sign_name, &id, &group, &prio,
1420 &buf, &lnum) == FAIL)
1421 return;
1422
1423 if (idx == SIGNCMD_PLACE)
1424 sign_place_cmd(buf, lnum, sign_name, id, group, prio);
1425 else if (idx == SIGNCMD_UNPLACE)
1426 sign_unplace_cmd(buf, lnum, sign_name, id, group);
1427 else if (idx == SIGNCMD_JUMP)
1428 sign_jump_cmd(buf, lnum, sign_name, id, group);
1429 }
1430}
1431
1432/*
1433 * Return information about a specified sign
1434 */
1435 static void
1436sign_getinfo(sign_T *sp, dict_T *retdict)
1437{
1438 char_u *p;
1439
1440 dict_add_string(retdict, "name", (char_u *)sp->sn_name);
1441 if (sp->sn_icon != NULL)
1442 dict_add_string(retdict, "icon", (char_u *)sp->sn_icon);
1443 if (sp->sn_text != NULL)
1444 dict_add_string(retdict, "text", (char_u *)sp->sn_text);
1445 if (sp->sn_line_hl > 0)
1446 {
1447 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1448 if (p == NULL)
1449 p = (char_u *)"NONE";
1450 dict_add_string(retdict, "linehl", (char_u *)p);
1451 }
1452 if (sp->sn_text_hl > 0)
1453 {
1454 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1455 if (p == NULL)
1456 p = (char_u *)"NONE";
1457 dict_add_string(retdict, "texthl", (char_u *)p);
1458 }
1459}
1460
1461/*
1462 * If 'name' is NULL, return a list of all the defined signs.
1463 * Otherwise, return information about the specified sign.
1464 */
1465 void
1466sign_getlist(char_u *name, list_T *retlist)
1467{
1468 sign_T *sp = first_sign;
1469 dict_T *dict;
1470
1471 if (name != NULL)
1472 {
1473 sp = sign_find(name, NULL);
1474 if (sp == NULL)
1475 return;
1476 }
1477
1478 for (; sp != NULL && !got_int; sp = sp->sn_next)
1479 {
1480 if ((dict = dict_alloc_id(aid_sign_getlist)) == NULL)
1481 return;
1482 if (list_append_dict(retlist, dict) == FAIL)
1483 return;
1484 sign_getinfo(sp, dict);
1485
1486 if (name != NULL) // handle only the specified sign
1487 break;
1488 }
1489}
1490
1491/*
1492 * Returns information about signs placed in a buffer as list of dicts.
1493 */
1494 void
1495get_buffer_signs(buf_T *buf, list_T *l)
1496{
1497 signlist_T *sign;
1498 dict_T *d;
1499
1500 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1501 {
1502 if ((d = sign_get_info(sign)) != NULL)
1503 list_append_dict(l, d);
1504 }
1505}
1506
1507/*
1508 * Return information about all the signs placed in a buffer
1509 */
1510 static void
1511sign_get_placed_in_buf(
1512 buf_T *buf,
1513 linenr_T lnum,
1514 int sign_id,
1515 char_u *sign_group,
1516 list_T *retlist)
1517{
1518 dict_T *d;
1519 list_T *l;
1520 signlist_T *sign;
1521 dict_T *sdict;
1522
1523 if ((d = dict_alloc_id(aid_sign_getplaced_dict)) == NULL)
1524 return;
1525 list_append_dict(retlist, d);
1526
1527 dict_add_number(d, "bufnr", (long)buf->b_fnum);
1528
1529 if ((l = list_alloc_id(aid_sign_getplaced_list)) == NULL)
1530 return;
1531 dict_add_list(d, "signs", l);
1532
1533 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1534 {
1535 if (!sign_in_group(sign, sign_group))
1536 continue;
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001537 if ((lnum == 0 && sign_id == 0)
1538 || (sign_id == 0 && lnum == sign->lnum)
1539 || (lnum == 0 && sign_id == sign->id)
1540 || (lnum == sign->lnum && sign_id == sign->id))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001541 {
1542 if ((sdict = sign_get_info(sign)) != NULL)
1543 list_append_dict(l, sdict);
1544 }
1545 }
1546}
1547
1548/*
1549 * Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
1550 * sign placed at the line number. If 'lnum' is zero, return all the signs
1551 * placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
1552 */
1553 void
1554sign_get_placed(
1555 buf_T *buf,
1556 linenr_T lnum,
1557 int sign_id,
1558 char_u *sign_group,
1559 list_T *retlist)
1560{
1561 if (buf != NULL)
1562 sign_get_placed_in_buf(buf, lnum, sign_id, sign_group, retlist);
1563 else
1564 {
1565 FOR_ALL_BUFFERS(buf)
1566 {
1567 if (buf->b_signlist != NULL)
1568 sign_get_placed_in_buf(buf, 0, sign_id, sign_group, retlist);
1569 }
1570 }
1571}
1572
1573# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1574/*
1575 * Allocate the icons. Called when the GUI has started. Allows defining
1576 * signs before it starts.
1577 */
1578 void
1579sign_gui_started(void)
1580{
1581 sign_T *sp;
1582
1583 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1584 if (sp->sn_icon != NULL)
1585 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
1586}
1587# endif
1588
1589/*
1590 * List one sign.
1591 */
1592 static void
1593sign_list_defined(sign_T *sp)
1594{
1595 char_u *p;
1596
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001597 smsg("sign %s", sp->sn_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001598 if (sp->sn_icon != NULL)
1599 {
1600 MSG_PUTS(" icon=");
1601 msg_outtrans(sp->sn_icon);
1602# ifdef FEAT_SIGN_ICONS
1603 if (sp->sn_image == NULL)
1604 MSG_PUTS(_(" (NOT FOUND)"));
1605# else
1606 MSG_PUTS(_(" (not supported)"));
1607# endif
1608 }
1609 if (sp->sn_text != NULL)
1610 {
1611 MSG_PUTS(" text=");
1612 msg_outtrans(sp->sn_text);
1613 }
1614 if (sp->sn_line_hl > 0)
1615 {
1616 MSG_PUTS(" linehl=");
1617 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1618 if (p == NULL)
1619 MSG_PUTS("NONE");
1620 else
1621 msg_puts(p);
1622 }
1623 if (sp->sn_text_hl > 0)
1624 {
1625 MSG_PUTS(" texthl=");
1626 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1627 if (p == NULL)
1628 MSG_PUTS("NONE");
1629 else
1630 msg_puts(p);
1631 }
1632}
1633
1634/*
1635 * Undefine a sign and free its memory.
1636 */
1637 static void
1638sign_undefine(sign_T *sp, sign_T *sp_prev)
1639{
1640 vim_free(sp->sn_name);
1641 vim_free(sp->sn_icon);
1642# ifdef FEAT_SIGN_ICONS
1643 if (sp->sn_image != NULL)
1644 {
1645 out_flush();
1646 gui_mch_destroy_sign(sp->sn_image);
1647 }
1648# endif
1649 vim_free(sp->sn_text);
1650 if (sp_prev == NULL)
1651 first_sign = sp->sn_next;
1652 else
1653 sp_prev->sn_next = sp->sn_next;
1654 vim_free(sp);
1655}
1656
1657/*
1658 * Get highlighting attribute for sign "typenr".
1659 * If "line" is TRUE: line highl, if FALSE: text highl.
1660 */
1661 int
1662sign_get_attr(int typenr, int line)
1663{
1664 sign_T *sp;
1665
1666 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1667 if (sp->sn_typenr == typenr)
1668 {
1669 if (line)
1670 {
1671 if (sp->sn_line_hl > 0)
1672 return syn_id2attr(sp->sn_line_hl);
1673 }
1674 else
1675 {
1676 if (sp->sn_text_hl > 0)
1677 return syn_id2attr(sp->sn_text_hl);
1678 }
1679 break;
1680 }
1681 return 0;
1682}
1683
1684/*
1685 * Get text mark for sign "typenr".
1686 * Returns NULL if there isn't one.
1687 */
1688 char_u *
1689sign_get_text(int typenr)
1690{
1691 sign_T *sp;
1692
1693 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1694 if (sp->sn_typenr == typenr)
1695 return sp->sn_text;
1696 return NULL;
1697}
1698
1699# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1700 void *
1701sign_get_image(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001702 int typenr) // the attribute which may have a sign
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001703{
1704 sign_T *sp;
1705
1706 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1707 if (sp->sn_typenr == typenr)
1708 return sp->sn_image;
1709 return NULL;
1710}
1711# endif
1712
1713/*
1714 * Undefine/free all signs.
1715 */
1716 void
1717free_signs(void)
1718{
1719 while (first_sign != NULL)
1720 sign_undefine(first_sign, NULL);
1721}
1722
1723# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1724static enum
1725{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001726 EXP_SUBCMD, // expand :sign sub-commands
1727 EXP_DEFINE, // expand :sign define {name} args
1728 EXP_PLACE, // expand :sign place {id} args
1729 EXP_UNPLACE, // expand :sign unplace"
1730 EXP_SIGN_NAMES // expand with name of placed signs
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001731} expand_what;
1732
1733/*
1734 * Function given to ExpandGeneric() to obtain the sign command
1735 * expansion.
1736 */
1737 char_u *
1738get_sign_name(expand_T *xp UNUSED, int idx)
1739{
1740 sign_T *sp;
1741 int current_idx;
1742
1743 switch (expand_what)
1744 {
1745 case EXP_SUBCMD:
1746 return (char_u *)cmds[idx];
1747 case EXP_DEFINE:
1748 {
1749 char *define_arg[] =
1750 {
1751 "icon=", "linehl=", "text=", "texthl=", NULL
1752 };
1753 return (char_u *)define_arg[idx];
1754 }
1755 case EXP_PLACE:
1756 {
1757 char *place_arg[] =
1758 {
1759 "line=", "name=", "group=", "priority=", "file=",
1760 "buffer=", NULL
1761 };
1762 return (char_u *)place_arg[idx];
1763 }
1764 case EXP_UNPLACE:
1765 {
1766 char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1767 return (char_u *)unplace_arg[idx];
1768 }
1769 case EXP_SIGN_NAMES:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001770 // Complete with name of signs already defined
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001771 current_idx = 0;
1772 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1773 if (current_idx++ == idx)
1774 return sp->sn_name;
1775 return NULL;
1776 default:
1777 return NULL;
1778 }
1779}
1780
1781/*
1782 * Handle command line completion for :sign command.
1783 */
1784 void
1785set_context_in_sign_cmd(expand_T *xp, char_u *arg)
1786{
1787 char_u *p;
1788 char_u *end_subcmd;
1789 char_u *last;
1790 int cmd_idx;
1791 char_u *begin_subcmd_args;
1792
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001793 // Default: expand subcommands.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001794 xp->xp_context = EXPAND_SIGN;
1795 expand_what = EXP_SUBCMD;
1796 xp->xp_pattern = arg;
1797
1798 end_subcmd = skiptowhite(arg);
1799 if (*end_subcmd == NUL)
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001800 // expand subcmd name
1801 // :sign {subcmd}<CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001802 return;
1803
1804 cmd_idx = sign_cmd_idx(arg, end_subcmd);
1805
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001806 // :sign {subcmd} {subcmd_args}
1807 // |
1808 // begin_subcmd_args
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001809 begin_subcmd_args = skipwhite(end_subcmd);
1810 p = skiptowhite(begin_subcmd_args);
1811 if (*p == NUL)
1812 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001813 //
1814 // Expand first argument of subcmd when possible.
1815 // For ":jump {id}" and ":unplace {id}", we could
1816 // possibly expand the ids of all signs already placed.
1817 //
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001818 xp->xp_pattern = begin_subcmd_args;
1819 switch (cmd_idx)
1820 {
1821 case SIGNCMD_LIST:
1822 case SIGNCMD_UNDEFINE:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001823 // :sign list <CTRL-D>
1824 // :sign undefine <CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001825 expand_what = EXP_SIGN_NAMES;
1826 break;
1827 default:
1828 xp->xp_context = EXPAND_NOTHING;
1829 }
1830 return;
1831 }
1832
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001833 // expand last argument of subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001834
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001835 // :sign define {name} {args}...
1836 // |
1837 // p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001838
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001839 // Loop until reaching last argument.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001840 do
1841 {
1842 p = skipwhite(p);
1843 last = p;
1844 p = skiptowhite(p);
1845 } while (*p != NUL);
1846
1847 p = vim_strchr(last, '=');
1848
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001849 // :sign define {name} {args}... {last}=
1850 // | |
1851 // last p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001852 if (p == NULL)
1853 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001854 // Expand last argument name (before equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001855 xp->xp_pattern = last;
1856 switch (cmd_idx)
1857 {
1858 case SIGNCMD_DEFINE:
1859 expand_what = EXP_DEFINE;
1860 break;
1861 case SIGNCMD_PLACE:
1862 expand_what = EXP_PLACE;
1863 break;
1864 case SIGNCMD_JUMP:
1865 case SIGNCMD_UNPLACE:
1866 expand_what = EXP_UNPLACE;
1867 break;
1868 default:
1869 xp->xp_context = EXPAND_NOTHING;
1870 }
1871 }
1872 else
1873 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001874 // Expand last argument value (after equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001875 xp->xp_pattern = p + 1;
1876 switch (cmd_idx)
1877 {
1878 case SIGNCMD_DEFINE:
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001879 if (STRNCMP(last, "texthl", p - last) == 0
1880 || STRNCMP(last, "linehl", p - last) == 0)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001881 xp->xp_context = EXPAND_HIGHLIGHT;
1882 else if (STRNCMP(last, "icon", p - last) == 0)
1883 xp->xp_context = EXPAND_FILES;
1884 else
1885 xp->xp_context = EXPAND_NOTHING;
1886 break;
1887 case SIGNCMD_PLACE:
1888 if (STRNCMP(last, "name", p - last) == 0)
1889 expand_what = EXP_SIGN_NAMES;
1890 else
1891 xp->xp_context = EXPAND_NOTHING;
1892 break;
1893 default:
1894 xp->xp_context = EXPAND_NOTHING;
1895 }
1896 }
1897}
1898# endif
1899
1900#endif /* FEAT_SIGNS */