blob: af7771ead6d2e14c12c10174200245d58230cffb [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'
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,
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
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(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100529 buf_T *buf, // buffer whose sign we are searching for
530 linenr_T lnum, // line number of sign
531 int typenr) // sign type number
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100532{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100533 signlist_T *sign; // a sign in the signlist
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100534
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{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100659 signlist_T *sign; // a sign in a b_signlist
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100660
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(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100681 char_u *begin_cmd, // begin of sign subcmd
682 char_u *end_cmd) // just after sign subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100683{
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);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 emsg(_("E612: Too many signs defined"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100756 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 semsg(_("E239: Invalid sign text: %s"), text);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100839 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100873 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100874 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
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100894}
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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 semsg(_("E155: Unknown sign: %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100920 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100936 semsg(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100937 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
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_("E159: Missing sign number"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100984}
985
986/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100987 * Jump to a sign.
988 */
989 linenr_T
990sign_jump(int sign_id, char_u *sign_group, buf_T *buf)
991{
992 linenr_T lnum;
993
994 if ((lnum = buf_findsign(buf, sign_id, sign_group)) <= 0)
995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 semsg(_("E157: Invalid sign ID: %ld"), sign_id);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100997 return -1;
998 }
999
1000 // goto a sign ...
1001 if (buf_jump_open_win(buf) != NULL)
1002 { // ... in a current window
1003 curwin->w_cursor.lnum = lnum;
1004 check_cursor_lnum();
1005 beginline(BL_WHITE);
1006 }
1007 else
1008 { // ... not currently in a window
1009 char_u *cmd;
1010
1011 if (buf->b_fname == NULL)
1012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001013 emsg(_("E934: Cannot jump to a buffer that does not have a name"));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001014 return -1;
1015 }
1016 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
1017 if (cmd == NULL)
1018 return -1;
1019 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
1020 do_cmdline_cmd(cmd);
1021 vim_free(cmd);
1022 }
1023# ifdef FEAT_FOLDING
1024 foldOpenCursor();
1025# endif
1026
1027 return lnum;
1028}
1029
1030/*
1031 * ":sign define {name} ..." command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001032 */
1033 static void
1034sign_define_cmd(char_u *sign_name, char_u *cmdline)
1035{
1036 char_u *arg;
1037 char_u *p = cmdline;
1038 char_u *icon = NULL;
1039 char_u *text = NULL;
1040 char_u *linehl = NULL;
1041 char_u *texthl = NULL;
1042 int failed = FALSE;
1043
1044 // set values for a defined sign.
1045 for (;;)
1046 {
1047 arg = skipwhite(p);
1048 if (*arg == NUL)
1049 break;
1050 p = skiptowhite_esc(arg);
1051 if (STRNCMP(arg, "icon=", 5) == 0)
1052 {
1053 arg += 5;
1054 icon = vim_strnsave(arg, (int)(p - arg));
1055 }
1056 else if (STRNCMP(arg, "text=", 5) == 0)
1057 {
1058 arg += 5;
1059 text = vim_strnsave(arg, (int)(p - arg));
1060 }
1061 else if (STRNCMP(arg, "linehl=", 7) == 0)
1062 {
1063 arg += 7;
1064 linehl = vim_strnsave(arg, (int)(p - arg));
1065 }
1066 else if (STRNCMP(arg, "texthl=", 7) == 0)
1067 {
1068 arg += 7;
1069 texthl = vim_strnsave(arg, (int)(p - arg));
1070 }
1071 else
1072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 semsg(_(e_invarg2), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001074 failed = TRUE;
1075 break;
1076 }
1077 }
1078
1079 if (!failed)
1080 sign_define_by_name(sign_name, icon, linehl, text, texthl);
1081
1082 vim_free(icon);
1083 vim_free(text);
1084 vim_free(linehl);
1085 vim_free(texthl);
1086}
1087
1088/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001089 * ":sign place" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001090 */
1091 static void
1092sign_place_cmd(
1093 buf_T *buf,
1094 linenr_T lnum,
1095 char_u *sign_name,
1096 int id,
1097 char_u *group,
1098 int prio)
1099{
1100 if (id <= 0)
1101 {
1102 // List signs placed in a file/buffer
1103 // :sign place file={fname}
1104 // :sign place group={group} file={fname}
1105 // :sign place group=* file={fname}
1106 // :sign place buffer={nr}
1107 // :sign place group={group} buffer={nr}
1108 // :sign place group=* buffer={nr}
1109 // :sign place
1110 // :sign place group={group}
1111 // :sign place group=*
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001112 if (lnum >= 0 || sign_name != NULL
1113 || (group != NULL && *group == '\0'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001114 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001115 else
1116 sign_list_placed(buf, group);
1117 }
1118 else
1119 {
1120 // Place a new sign
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001121 if (sign_name == NULL || buf == NULL
1122 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001124 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001125 return;
1126 }
1127
1128 sign_place(&id, group, sign_name, buf, lnum, prio);
1129 }
1130}
1131
1132/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001133 * ":sign unplace" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001134 */
1135 static void
1136sign_unplace_cmd(
1137 buf_T *buf,
1138 linenr_T lnum,
1139 char_u *sign_name,
1140 int id,
1141 char_u *group)
1142{
1143 if (lnum >= 0 || sign_name != NULL || (group != NULL && *group == '\0'))
1144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001145 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001146 return;
1147 }
1148
1149 if (id == -2)
1150 {
1151 if (buf != NULL)
1152 // :sign unplace * file={fname}
1153 // :sign unplace * group={group} file={fname}
1154 // :sign unplace * group=* file={fname}
1155 // :sign unplace * buffer={nr}
1156 // :sign unplace * group={group} buffer={nr}
1157 // :sign unplace * group=* buffer={nr}
1158 sign_unplace(0, group, buf, 0);
1159 else
1160 // :sign unplace *
1161 // :sign unplace * group={group}
1162 // :sign unplace * group=*
1163 FOR_ALL_BUFFERS(buf)
1164 if (buf->b_signlist != NULL)
1165 buf_delete_signs(buf, group);
1166 }
1167 else
1168 {
1169 if (buf != NULL)
1170 // :sign unplace {id} file={fname}
1171 // :sign unplace {id} group={group} file={fname}
1172 // :sign unplace {id} group=* file={fname}
1173 // :sign unplace {id} buffer={nr}
1174 // :sign unplace {id} group={group} buffer={nr}
1175 // :sign unplace {id} group=* buffer={nr}
1176 sign_unplace(id, group, buf, 0);
1177 else
1178 {
1179 if (id == -1)
1180 {
1181 // :sign unplace group={group}
1182 // :sign unplace group=*
1183 sign_unplace_at_cursor(group);
1184 }
1185 else
1186 {
1187 // :sign unplace {id}
1188 // :sign unplace {id} group={group}
1189 // :sign unplace {id} group=*
1190 FOR_ALL_BUFFERS(buf)
1191 sign_unplace(id, group, buf, 0);
1192 }
1193 }
1194 }
1195}
1196
1197/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001198 * Jump to a placed sign commands:
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001199 * :sign jump {id} file={fname}
1200 * :sign jump {id} buffer={nr}
1201 * :sign jump {id} group={group} file={fname}
1202 * :sign jump {id} group={group} buffer={nr}
1203 */
1204 static void
1205sign_jump_cmd(
1206 buf_T *buf,
1207 linenr_T lnum,
1208 char_u *sign_name,
1209 int id,
1210 char_u *group)
1211{
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001212 if (sign_name == NULL && group == NULL && id == -1)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_argreq));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001215 return;
1216 }
1217
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001218 if (buf == NULL || (group != NULL && *group == '\0')
1219 || lnum >= 0 || sign_name != NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001220 {
1221 // File or buffer is not specified or an empty group is used
1222 // or a line number or a sign name is specified.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001224 return;
1225 }
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001226 (void)sign_jump(id, group, buf);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001227}
1228
1229/*
1230 * Parse the command line arguments for the ":sign place", ":sign unplace" and
1231 * ":sign jump" commands.
1232 * The supported arguments are: line={lnum} name={name} group={group}
1233 * priority={prio} and file={fname} or buffer={nr}.
1234 */
1235 static int
1236parse_sign_cmd_args(
1237 int cmd,
1238 char_u *arg,
1239 char_u **sign_name,
1240 int *signid,
1241 char_u **group,
1242 int *prio,
1243 buf_T **buf,
1244 linenr_T *lnum)
1245{
1246 char_u *arg1;
1247 char_u *name;
1248 char_u *filename = NULL;
Bram Moolenaarb589f952019-01-07 22:10:00 +01001249 int lnum_arg = FALSE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001250
1251 // first arg could be placed sign id
1252 arg1 = arg;
1253 if (VIM_ISDIGIT(*arg))
1254 {
1255 *signid = getdigits(&arg);
1256 if (!VIM_ISWHITE(*arg) && *arg != NUL)
1257 {
1258 *signid = -1;
1259 arg = arg1;
1260 }
1261 else
1262 arg = skipwhite(arg);
1263 }
1264
1265 while (*arg != NUL)
1266 {
1267 if (STRNCMP(arg, "line=", 5) == 0)
1268 {
1269 arg += 5;
1270 *lnum = atoi((char *)arg);
1271 arg = skiptowhite(arg);
Bram Moolenaarb589f952019-01-07 22:10:00 +01001272 lnum_arg = TRUE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001273 }
1274 else if (STRNCMP(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE)
1275 {
1276 if (*signid != -1)
1277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001278 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001279 return FAIL;
1280 }
1281 *signid = -2;
1282 arg = skiptowhite(arg + 1);
1283 }
1284 else if (STRNCMP(arg, "name=", 5) == 0)
1285 {
1286 arg += 5;
1287 name = arg;
1288 arg = skiptowhite(arg);
1289 if (*arg != NUL)
1290 *arg++ = NUL;
1291 while (name[0] == '0' && name[1] != NUL)
1292 ++name;
1293 *sign_name = name;
1294 }
1295 else if (STRNCMP(arg, "group=", 6) == 0)
1296 {
1297 arg += 6;
1298 *group = arg;
1299 arg = skiptowhite(arg);
1300 if (*arg != NUL)
1301 *arg++ = NUL;
1302 }
1303 else if (STRNCMP(arg, "priority=", 9) == 0)
1304 {
1305 arg += 9;
1306 *prio = atoi((char *)arg);
1307 arg = skiptowhite(arg);
1308 }
1309 else if (STRNCMP(arg, "file=", 5) == 0)
1310 {
1311 arg += 5;
1312 filename = arg;
1313 *buf = buflist_findname_exp(arg);
1314 break;
1315 }
1316 else if (STRNCMP(arg, "buffer=", 7) == 0)
1317 {
1318 arg += 7;
1319 filename = arg;
1320 *buf = buflist_findnr((int)getdigits(&arg));
1321 if (*skipwhite(arg) != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001322 emsg(_(e_trailing));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001323 break;
1324 }
1325 else
1326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001327 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001328 return FAIL;
1329 }
1330 arg = skipwhite(arg);
1331 }
1332
1333 if (filename != NULL && *buf == NULL)
1334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 semsg(_("E158: Invalid buffer name: %s"), filename);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001336 return FAIL;
1337 }
1338
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001339 // If the filename is not supplied for the sign place or the sign jump
1340 // command, then use the current buffer.
Bram Moolenaarb589f952019-01-07 22:10:00 +01001341 if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg)
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001342 || cmd == SIGNCMD_JUMP))
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001343 *buf = curwin->w_buffer;
1344
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001345 return OK;
1346}
1347
1348/*
1349 * ":sign" command
1350 */
1351 void
1352ex_sign(exarg_T *eap)
1353{
1354 char_u *arg = eap->arg;
1355 char_u *p;
1356 int idx;
1357 sign_T *sp;
1358 buf_T *buf = NULL;
1359
1360 // Parse the subcommand.
1361 p = skiptowhite(arg);
1362 idx = sign_cmd_idx(arg, p);
1363 if (idx == SIGNCMD_LAST)
1364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001365 semsg(_("E160: Unknown sign command: %s"), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001366 return;
1367 }
1368 arg = skipwhite(p);
1369
1370 if (idx <= SIGNCMD_LIST)
1371 {
1372 // Define, undefine or list signs.
1373 if (idx == SIGNCMD_LIST && *arg == NUL)
1374 {
1375 // ":sign list": list all defined signs
1376 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
1377 sign_list_defined(sp);
1378 }
1379 else if (*arg == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001380 emsg(_("E156: Missing sign name"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001381 else
1382 {
1383 char_u *name;
1384
1385 // Isolate the sign name. If it's a number skip leading zeroes,
1386 // so that "099" and "99" are the same sign. But keep "0".
1387 p = skiptowhite(arg);
1388 if (*p != NUL)
1389 *p++ = NUL;
1390 while (arg[0] == '0' && arg[1] != NUL)
1391 ++arg;
1392 name = vim_strsave(arg);
1393
1394 if (idx == SIGNCMD_DEFINE)
1395 sign_define_cmd(name, p);
1396 else if (idx == SIGNCMD_LIST)
1397 // ":sign list {name}"
1398 sign_list_by_name(name);
1399 else
1400 // ":sign undefine {name}"
1401 sign_undefine_by_name(name);
1402
1403 vim_free(name);
1404 return;
1405 }
1406 }
1407 else
1408 {
1409 int id = -1;
1410 linenr_T lnum = -1;
1411 char_u *sign_name = NULL;
1412 char_u *group = NULL;
1413 int prio = SIGN_DEF_PRIO;
1414
1415 // Parse command line arguments
1416 if (parse_sign_cmd_args(idx, arg, &sign_name, &id, &group, &prio,
1417 &buf, &lnum) == FAIL)
1418 return;
1419
1420 if (idx == SIGNCMD_PLACE)
1421 sign_place_cmd(buf, lnum, sign_name, id, group, prio);
1422 else if (idx == SIGNCMD_UNPLACE)
1423 sign_unplace_cmd(buf, lnum, sign_name, id, group);
1424 else if (idx == SIGNCMD_JUMP)
1425 sign_jump_cmd(buf, lnum, sign_name, id, group);
1426 }
1427}
1428
1429/*
1430 * Return information about a specified sign
1431 */
1432 static void
1433sign_getinfo(sign_T *sp, dict_T *retdict)
1434{
1435 char_u *p;
1436
1437 dict_add_string(retdict, "name", (char_u *)sp->sn_name);
1438 if (sp->sn_icon != NULL)
1439 dict_add_string(retdict, "icon", (char_u *)sp->sn_icon);
1440 if (sp->sn_text != NULL)
1441 dict_add_string(retdict, "text", (char_u *)sp->sn_text);
1442 if (sp->sn_line_hl > 0)
1443 {
1444 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1445 if (p == NULL)
1446 p = (char_u *)"NONE";
1447 dict_add_string(retdict, "linehl", (char_u *)p);
1448 }
1449 if (sp->sn_text_hl > 0)
1450 {
1451 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1452 if (p == NULL)
1453 p = (char_u *)"NONE";
1454 dict_add_string(retdict, "texthl", (char_u *)p);
1455 }
1456}
1457
1458/*
1459 * If 'name' is NULL, return a list of all the defined signs.
1460 * Otherwise, return information about the specified sign.
1461 */
1462 void
1463sign_getlist(char_u *name, list_T *retlist)
1464{
1465 sign_T *sp = first_sign;
1466 dict_T *dict;
1467
1468 if (name != NULL)
1469 {
1470 sp = sign_find(name, NULL);
1471 if (sp == NULL)
1472 return;
1473 }
1474
1475 for (; sp != NULL && !got_int; sp = sp->sn_next)
1476 {
1477 if ((dict = dict_alloc_id(aid_sign_getlist)) == NULL)
1478 return;
1479 if (list_append_dict(retlist, dict) == FAIL)
1480 return;
1481 sign_getinfo(sp, dict);
1482
1483 if (name != NULL) // handle only the specified sign
1484 break;
1485 }
1486}
1487
1488/*
1489 * Returns information about signs placed in a buffer as list of dicts.
1490 */
1491 void
1492get_buffer_signs(buf_T *buf, list_T *l)
1493{
1494 signlist_T *sign;
1495 dict_T *d;
1496
1497 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1498 {
1499 if ((d = sign_get_info(sign)) != NULL)
1500 list_append_dict(l, d);
1501 }
1502}
1503
1504/*
1505 * Return information about all the signs placed in a buffer
1506 */
1507 static void
1508sign_get_placed_in_buf(
1509 buf_T *buf,
1510 linenr_T lnum,
1511 int sign_id,
1512 char_u *sign_group,
1513 list_T *retlist)
1514{
1515 dict_T *d;
1516 list_T *l;
1517 signlist_T *sign;
1518 dict_T *sdict;
1519
1520 if ((d = dict_alloc_id(aid_sign_getplaced_dict)) == NULL)
1521 return;
1522 list_append_dict(retlist, d);
1523
1524 dict_add_number(d, "bufnr", (long)buf->b_fnum);
1525
1526 if ((l = list_alloc_id(aid_sign_getplaced_list)) == NULL)
1527 return;
1528 dict_add_list(d, "signs", l);
1529
1530 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1531 {
1532 if (!sign_in_group(sign, sign_group))
1533 continue;
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001534 if ((lnum == 0 && sign_id == 0)
1535 || (sign_id == 0 && lnum == sign->lnum)
1536 || (lnum == 0 && sign_id == sign->id)
1537 || (lnum == sign->lnum && sign_id == sign->id))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001538 {
1539 if ((sdict = sign_get_info(sign)) != NULL)
1540 list_append_dict(l, sdict);
1541 }
1542 }
1543}
1544
1545/*
1546 * Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
1547 * sign placed at the line number. If 'lnum' is zero, return all the signs
1548 * placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
1549 */
1550 void
1551sign_get_placed(
1552 buf_T *buf,
1553 linenr_T lnum,
1554 int sign_id,
1555 char_u *sign_group,
1556 list_T *retlist)
1557{
1558 if (buf != NULL)
1559 sign_get_placed_in_buf(buf, lnum, sign_id, sign_group, retlist);
1560 else
1561 {
1562 FOR_ALL_BUFFERS(buf)
1563 {
1564 if (buf->b_signlist != NULL)
1565 sign_get_placed_in_buf(buf, 0, sign_id, sign_group, retlist);
1566 }
1567 }
1568}
1569
1570# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1571/*
1572 * Allocate the icons. Called when the GUI has started. Allows defining
1573 * signs before it starts.
1574 */
1575 void
1576sign_gui_started(void)
1577{
1578 sign_T *sp;
1579
1580 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1581 if (sp->sn_icon != NULL)
1582 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
1583}
1584# endif
1585
1586/*
1587 * List one sign.
1588 */
1589 static void
1590sign_list_defined(sign_T *sp)
1591{
1592 char_u *p;
1593
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 smsg("sign %s", sp->sn_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001595 if (sp->sn_icon != NULL)
1596 {
1597 MSG_PUTS(" icon=");
1598 msg_outtrans(sp->sn_icon);
1599# ifdef FEAT_SIGN_ICONS
1600 if (sp->sn_image == NULL)
1601 MSG_PUTS(_(" (NOT FOUND)"));
1602# else
1603 MSG_PUTS(_(" (not supported)"));
1604# endif
1605 }
1606 if (sp->sn_text != NULL)
1607 {
1608 MSG_PUTS(" text=");
1609 msg_outtrans(sp->sn_text);
1610 }
1611 if (sp->sn_line_hl > 0)
1612 {
1613 MSG_PUTS(" linehl=");
1614 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1615 if (p == NULL)
1616 MSG_PUTS("NONE");
1617 else
1618 msg_puts(p);
1619 }
1620 if (sp->sn_text_hl > 0)
1621 {
1622 MSG_PUTS(" texthl=");
1623 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1624 if (p == NULL)
1625 MSG_PUTS("NONE");
1626 else
1627 msg_puts(p);
1628 }
1629}
1630
1631/*
1632 * Undefine a sign and free its memory.
1633 */
1634 static void
1635sign_undefine(sign_T *sp, sign_T *sp_prev)
1636{
1637 vim_free(sp->sn_name);
1638 vim_free(sp->sn_icon);
1639# ifdef FEAT_SIGN_ICONS
1640 if (sp->sn_image != NULL)
1641 {
1642 out_flush();
1643 gui_mch_destroy_sign(sp->sn_image);
1644 }
1645# endif
1646 vim_free(sp->sn_text);
1647 if (sp_prev == NULL)
1648 first_sign = sp->sn_next;
1649 else
1650 sp_prev->sn_next = sp->sn_next;
1651 vim_free(sp);
1652}
1653
1654/*
1655 * Get highlighting attribute for sign "typenr".
1656 * If "line" is TRUE: line highl, if FALSE: text highl.
1657 */
1658 int
1659sign_get_attr(int typenr, int line)
1660{
1661 sign_T *sp;
1662
1663 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1664 if (sp->sn_typenr == typenr)
1665 {
1666 if (line)
1667 {
1668 if (sp->sn_line_hl > 0)
1669 return syn_id2attr(sp->sn_line_hl);
1670 }
1671 else
1672 {
1673 if (sp->sn_text_hl > 0)
1674 return syn_id2attr(sp->sn_text_hl);
1675 }
1676 break;
1677 }
1678 return 0;
1679}
1680
1681/*
1682 * Get text mark for sign "typenr".
1683 * Returns NULL if there isn't one.
1684 */
1685 char_u *
1686sign_get_text(int typenr)
1687{
1688 sign_T *sp;
1689
1690 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1691 if (sp->sn_typenr == typenr)
1692 return sp->sn_text;
1693 return NULL;
1694}
1695
1696# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1697 void *
1698sign_get_image(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001699 int typenr) // the attribute which may have a sign
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001700{
1701 sign_T *sp;
1702
1703 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1704 if (sp->sn_typenr == typenr)
1705 return sp->sn_image;
1706 return NULL;
1707}
1708# endif
1709
1710/*
1711 * Undefine/free all signs.
1712 */
1713 void
1714free_signs(void)
1715{
1716 while (first_sign != NULL)
1717 sign_undefine(first_sign, NULL);
1718}
1719
1720# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1721static enum
1722{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001723 EXP_SUBCMD, // expand :sign sub-commands
1724 EXP_DEFINE, // expand :sign define {name} args
1725 EXP_PLACE, // expand :sign place {id} args
1726 EXP_UNPLACE, // expand :sign unplace"
1727 EXP_SIGN_NAMES // expand with name of placed signs
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001728} expand_what;
1729
1730/*
1731 * Function given to ExpandGeneric() to obtain the sign command
1732 * expansion.
1733 */
1734 char_u *
1735get_sign_name(expand_T *xp UNUSED, int idx)
1736{
1737 sign_T *sp;
1738 int current_idx;
1739
1740 switch (expand_what)
1741 {
1742 case EXP_SUBCMD:
1743 return (char_u *)cmds[idx];
1744 case EXP_DEFINE:
1745 {
1746 char *define_arg[] =
1747 {
1748 "icon=", "linehl=", "text=", "texthl=", NULL
1749 };
1750 return (char_u *)define_arg[idx];
1751 }
1752 case EXP_PLACE:
1753 {
1754 char *place_arg[] =
1755 {
1756 "line=", "name=", "group=", "priority=", "file=",
1757 "buffer=", NULL
1758 };
1759 return (char_u *)place_arg[idx];
1760 }
1761 case EXP_UNPLACE:
1762 {
1763 char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1764 return (char_u *)unplace_arg[idx];
1765 }
1766 case EXP_SIGN_NAMES:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001767 // Complete with name of signs already defined
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001768 current_idx = 0;
1769 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1770 if (current_idx++ == idx)
1771 return sp->sn_name;
1772 return NULL;
1773 default:
1774 return NULL;
1775 }
1776}
1777
1778/*
1779 * Handle command line completion for :sign command.
1780 */
1781 void
1782set_context_in_sign_cmd(expand_T *xp, char_u *arg)
1783{
1784 char_u *p;
1785 char_u *end_subcmd;
1786 char_u *last;
1787 int cmd_idx;
1788 char_u *begin_subcmd_args;
1789
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001790 // Default: expand subcommands.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001791 xp->xp_context = EXPAND_SIGN;
1792 expand_what = EXP_SUBCMD;
1793 xp->xp_pattern = arg;
1794
1795 end_subcmd = skiptowhite(arg);
1796 if (*end_subcmd == NUL)
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001797 // expand subcmd name
1798 // :sign {subcmd}<CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001799 return;
1800
1801 cmd_idx = sign_cmd_idx(arg, end_subcmd);
1802
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001803 // :sign {subcmd} {subcmd_args}
1804 // |
1805 // begin_subcmd_args
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001806 begin_subcmd_args = skipwhite(end_subcmd);
1807 p = skiptowhite(begin_subcmd_args);
1808 if (*p == NUL)
1809 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001810 //
1811 // Expand first argument of subcmd when possible.
1812 // For ":jump {id}" and ":unplace {id}", we could
1813 // possibly expand the ids of all signs already placed.
1814 //
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001815 xp->xp_pattern = begin_subcmd_args;
1816 switch (cmd_idx)
1817 {
1818 case SIGNCMD_LIST:
1819 case SIGNCMD_UNDEFINE:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001820 // :sign list <CTRL-D>
1821 // :sign undefine <CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001822 expand_what = EXP_SIGN_NAMES;
1823 break;
1824 default:
1825 xp->xp_context = EXPAND_NOTHING;
1826 }
1827 return;
1828 }
1829
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001830 // expand last argument of subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001831
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001832 // :sign define {name} {args}...
1833 // |
1834 // p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001835
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001836 // Loop until reaching last argument.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001837 do
1838 {
1839 p = skipwhite(p);
1840 last = p;
1841 p = skiptowhite(p);
1842 } while (*p != NUL);
1843
1844 p = vim_strchr(last, '=');
1845
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001846 // :sign define {name} {args}... {last}=
1847 // | |
1848 // last p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001849 if (p == NULL)
1850 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001851 // Expand last argument name (before equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001852 xp->xp_pattern = last;
1853 switch (cmd_idx)
1854 {
1855 case SIGNCMD_DEFINE:
1856 expand_what = EXP_DEFINE;
1857 break;
1858 case SIGNCMD_PLACE:
1859 expand_what = EXP_PLACE;
1860 break;
1861 case SIGNCMD_JUMP:
1862 case SIGNCMD_UNPLACE:
1863 expand_what = EXP_UNPLACE;
1864 break;
1865 default:
1866 xp->xp_context = EXPAND_NOTHING;
1867 }
1868 }
1869 else
1870 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001871 // Expand last argument value (after equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001872 xp->xp_pattern = p + 1;
1873 switch (cmd_idx)
1874 {
1875 case SIGNCMD_DEFINE:
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001876 if (STRNCMP(last, "texthl", p - last) == 0
1877 || STRNCMP(last, "linehl", p - last) == 0)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001878 xp->xp_context = EXPAND_HIGHLIGHT;
1879 else if (STRNCMP(last, "icon", p - last) == 0)
1880 xp->xp_context = EXPAND_FILES;
1881 else
1882 xp->xp_context = EXPAND_NOTHING;
1883 break;
1884 case SIGNCMD_PLACE:
1885 if (STRNCMP(last, "name", p - last) == 0)
1886 expand_what = EXP_SIGN_NAMES;
1887 else
1888 xp->xp_context = EXPAND_NOTHING;
1889 break;
1890 default:
1891 xp->xp_context = EXPAND_NOTHING;
1892 }
1893 }
1894}
1895# endif
1896
1897#endif /* FEAT_SIGNS */