blob: 75f41235e854cebf080759f62b2ab99b68b1bf64 [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
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100165 // Search for the next usable sign identifier
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100166 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 Moolenaarc771bf92019-01-17 17:36:45 +0100663 linenr_T new_lnum;
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100664
665 FOR_ALL_SIGNS_IN_BUF(curbuf, sign)
666 {
Bram Moolenaarc771bf92019-01-17 17:36:45 +0100667 // Ignore changes to lines after the sign
668 if (sign->lnum < line1)
669 continue;
670 new_lnum = sign->lnum;
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100671 if (sign->lnum >= line1 && sign->lnum <= line2)
672 {
Bram Moolenaarc771bf92019-01-17 17:36:45 +0100673 if (amount != MAXLNUM)
674 new_lnum += amount;
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100675 }
676 else if (sign->lnum > line2)
Bram Moolenaarc771bf92019-01-17 17:36:45 +0100677 // Lines inserted or deleted before the sign
678 new_lnum += amount_after;
679
680 // If the new sign line number is past the last line in the buffer,
681 // then don't adjust the line number. Otherwise, it will always be past
682 // the last line and will not be visible.
683 if (new_lnum <= curbuf->b_ml.ml_line_count)
684 sign->lnum = new_lnum;
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100685 }
686}
687
688/*
689 * Find index of a ":sign" subcmd from its name.
690 * "*end_cmd" must be writable.
691 */
692 static int
693sign_cmd_idx(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100694 char_u *begin_cmd, // begin of sign subcmd
695 char_u *end_cmd) // just after sign subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100696{
697 int idx;
698 char save = *end_cmd;
699
700 *end_cmd = NUL;
701 for (idx = 0; ; ++idx)
702 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
703 break;
704 *end_cmd = save;
705 return idx;
706}
707
708/*
709 * Find a sign by name. Also returns pointer to the previous sign.
710 */
711 static sign_T *
712sign_find(char_u *name, sign_T **sp_prev)
713{
714 sign_T *sp;
715
716 if (sp_prev != NULL)
717 *sp_prev = NULL;
718 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
719 {
720 if (STRCMP(sp->sn_name, name) == 0)
721 break;
722 if (sp_prev != NULL)
723 *sp_prev = sp;
724 }
725
726 return sp;
727}
728
729/*
730 * Define a new sign or update an existing sign
731 */
732 int
733sign_define_by_name(
734 char_u *name,
735 char_u *icon,
736 char_u *linehl,
737 char_u *text,
738 char_u *texthl)
739{
740 sign_T *sp_prev;
741 sign_T *sp;
742
743 sp = sign_find(name, &sp_prev);
744 if (sp == NULL)
745 {
746 sign_T *lp;
747 int start = next_sign_typenr;
748
749 // Allocate a new sign.
750 sp = (sign_T *)alloc_clear_id((unsigned)sizeof(sign_T),
751 aid_sign_define_by_name);
752 if (sp == NULL)
753 return FAIL;
754
755 // Check that next_sign_typenr is not already being used.
756 // This only happens after wrapping around. Hopefully
757 // another one got deleted and we can use its number.
758 for (lp = first_sign; lp != NULL; )
759 {
760 if (lp->sn_typenr == next_sign_typenr)
761 {
762 ++next_sign_typenr;
763 if (next_sign_typenr == MAX_TYPENR)
764 next_sign_typenr = 1;
765 if (next_sign_typenr == start)
766 {
767 vim_free(sp);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100768 emsg(_("E612: Too many signs defined"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100769 return FAIL;
770 }
771 lp = first_sign; // start all over
772 continue;
773 }
774 lp = lp->sn_next;
775 }
776
777 sp->sn_typenr = next_sign_typenr;
778 if (++next_sign_typenr == MAX_TYPENR)
779 next_sign_typenr = 1; // wrap around
780
781 sp->sn_name = vim_strsave(name);
782 if (sp->sn_name == NULL) // out of memory
783 {
784 vim_free(sp);
785 return FAIL;
786 }
787
788 // add the new sign to the list of signs
789 if (sp_prev == NULL)
790 first_sign = sp;
791 else
792 sp_prev->sn_next = sp;
793 }
794
795 // set values for a defined sign.
796 if (icon != NULL)
797 {
798 vim_free(sp->sn_icon);
799 sp->sn_icon = vim_strsave(icon);
800 backslash_halve(sp->sn_icon);
801# ifdef FEAT_SIGN_ICONS
802 if (gui.in_use)
803 {
804 out_flush();
805 if (sp->sn_image != NULL)
806 gui_mch_destroy_sign(sp->sn_image);
807 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
808 }
809# endif
810 }
811
812 if (text != NULL)
813 {
814 char_u *s;
815 char_u *endp;
816 int cells;
817 int len;
818
819 endp = text + (int)STRLEN(text);
820 for (s = text; s + 1 < endp; ++s)
821 if (*s == '\\')
822 {
823 // Remove a backslash, so that it is possible
824 // to use a space.
825 STRMOVE(s, s + 1);
826 --endp;
827 }
828# ifdef FEAT_MBYTE
829 // Count cells and check for non-printable chars
830 if (has_mbyte)
831 {
832 cells = 0;
833 for (s = text; s < endp; s += (*mb_ptr2len)(s))
834 {
835 if (!vim_isprintc((*mb_ptr2char)(s)))
836 break;
837 cells += (*mb_ptr2cells)(s);
838 }
839 }
840 else
841# endif
842 {
843 for (s = text; s < endp; ++s)
844 if (!vim_isprintc(*s))
845 break;
846 cells = (int)(s - text);
847 }
848 // Currently must be one or two display cells
849 if (s != endp || cells < 1 || cells > 2)
850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 semsg(_("E239: Invalid sign text: %s"), text);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100852 return FAIL;
853 }
854
855 vim_free(sp->sn_text);
856 // Allocate one byte more if we need to pad up
857 // with a space.
858 len = (int)(endp - text + ((cells == 1) ? 1 : 0));
859 sp->sn_text = vim_strnsave(text, len);
860
861 if (sp->sn_text != NULL && cells == 1)
862 STRCPY(sp->sn_text + len - 1, " ");
863 }
864
865 if (linehl != NULL)
866 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl));
867
868 if (texthl != NULL)
869 sp->sn_text_hl = syn_check_group(texthl, (int)STRLEN(texthl));
870
871 return OK;
872}
873
874/*
875 * Free the sign specified by 'name'.
876 */
877 int
878sign_undefine_by_name(char_u *name)
879{
880 sign_T *sp_prev;
881 sign_T *sp;
882
883 sp = sign_find(name, &sp_prev);
884 if (sp == NULL)
885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100887 return FAIL;
888 }
889 sign_undefine(sp, sp_prev);
890
891 return OK;
892}
893
894/*
895 * List the signs matching 'name'
896 */
897 static void
898sign_list_by_name(char_u *name)
899{
900 sign_T *sp;
901
902 sp = sign_find(name, NULL);
903 if (sp != NULL)
904 sign_list_defined(sp);
905 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100907}
908
909/*
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100910 * Place a sign at the specified file location or update a sign.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100911 */
912 int
913sign_place(
914 int *sign_id,
915 char_u *sign_group,
916 char_u *sign_name,
917 buf_T *buf,
918 linenr_T lnum,
919 int prio)
920{
921 sign_T *sp;
922
923 // Check for reserved character '*' in group name
924 if (sign_group != NULL && (*sign_group == '*' || *sign_group == '\0'))
925 return FAIL;
926
927 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
928 if (STRCMP(sp->sn_name, sign_name) == 0)
929 break;
930 if (sp == NULL)
931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100932 semsg(_("E155: Unknown sign: %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100933 return FAIL;
934 }
935 if (*sign_id == 0)
936 *sign_id = sign_group_get_next_signid(buf, sign_group);
937
938 if (lnum > 0)
939 // ":sign place {id} line={lnum} name={name} file={fname}":
940 // place a sign
941 buf_addsign(buf, *sign_id, sign_group, prio, lnum, sp->sn_typenr);
942 else
943 // ":sign place {id} file={fname}": change sign type
944 lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr);
945 if (lnum > 0)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100946 redraw_buf_line_later(buf, lnum);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100947 else
948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 semsg(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100950 return FAIL;
951 }
952
953 return OK;
954}
955
956/*
957 * Unplace the specified sign
958 */
959 int
960sign_unplace(int sign_id, char_u *sign_group, buf_T *buf, linenr_T atlnum)
961{
962 if (buf->b_signlist == NULL) // No signs in the buffer
963 return OK;
964
965 if (sign_id == 0)
966 {
967 // Delete all the signs in the specified buffer
968 redraw_buf_later(buf, NOT_VALID);
969 buf_delete_signs(buf, sign_group);
970 }
971 else
972 {
973 linenr_T lnum;
974
975 // Delete only the specified signs
976 lnum = buf_delsign(buf, atlnum, sign_id, sign_group);
977 if (lnum == 0)
978 return FAIL;
979 }
980
981 return OK;
982}
983
984/*
985 * Unplace the sign at the current cursor line.
986 */
987 static void
988sign_unplace_at_cursor(char_u *groupname)
989{
990 int id = -1;
991
992 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum, groupname);
993 if (id > 0)
994 sign_unplace(id, groupname, curwin->w_buffer, curwin->w_cursor.lnum);
995 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 emsg(_("E159: Missing sign number"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100997}
998
999/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001000 * Jump to a sign.
1001 */
1002 linenr_T
1003sign_jump(int sign_id, char_u *sign_group, buf_T *buf)
1004{
1005 linenr_T lnum;
1006
1007 if ((lnum = buf_findsign(buf, sign_id, sign_group)) <= 0)
1008 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001009 semsg(_("E157: Invalid sign ID: %d"), sign_id);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001010 return -1;
1011 }
1012
1013 // goto a sign ...
1014 if (buf_jump_open_win(buf) != NULL)
1015 { // ... in a current window
1016 curwin->w_cursor.lnum = lnum;
1017 check_cursor_lnum();
1018 beginline(BL_WHITE);
1019 }
1020 else
1021 { // ... not currently in a window
1022 char_u *cmd;
1023
1024 if (buf->b_fname == NULL)
1025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 emsg(_("E934: Cannot jump to a buffer that does not have a name"));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001027 return -1;
1028 }
1029 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
1030 if (cmd == NULL)
1031 return -1;
1032 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
1033 do_cmdline_cmd(cmd);
1034 vim_free(cmd);
1035 }
1036# ifdef FEAT_FOLDING
1037 foldOpenCursor();
1038# endif
1039
1040 return lnum;
1041}
1042
1043/*
1044 * ":sign define {name} ..." command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001045 */
1046 static void
1047sign_define_cmd(char_u *sign_name, char_u *cmdline)
1048{
1049 char_u *arg;
1050 char_u *p = cmdline;
1051 char_u *icon = NULL;
1052 char_u *text = NULL;
1053 char_u *linehl = NULL;
1054 char_u *texthl = NULL;
1055 int failed = FALSE;
1056
1057 // set values for a defined sign.
1058 for (;;)
1059 {
1060 arg = skipwhite(p);
1061 if (*arg == NUL)
1062 break;
1063 p = skiptowhite_esc(arg);
1064 if (STRNCMP(arg, "icon=", 5) == 0)
1065 {
1066 arg += 5;
1067 icon = vim_strnsave(arg, (int)(p - arg));
1068 }
1069 else if (STRNCMP(arg, "text=", 5) == 0)
1070 {
1071 arg += 5;
1072 text = vim_strnsave(arg, (int)(p - arg));
1073 }
1074 else if (STRNCMP(arg, "linehl=", 7) == 0)
1075 {
1076 arg += 7;
1077 linehl = vim_strnsave(arg, (int)(p - arg));
1078 }
1079 else if (STRNCMP(arg, "texthl=", 7) == 0)
1080 {
1081 arg += 7;
1082 texthl = vim_strnsave(arg, (int)(p - arg));
1083 }
1084 else
1085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001086 semsg(_(e_invarg2), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001087 failed = TRUE;
1088 break;
1089 }
1090 }
1091
1092 if (!failed)
1093 sign_define_by_name(sign_name, icon, linehl, text, texthl);
1094
1095 vim_free(icon);
1096 vim_free(text);
1097 vim_free(linehl);
1098 vim_free(texthl);
1099}
1100
1101/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001102 * ":sign place" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001103 */
1104 static void
1105sign_place_cmd(
1106 buf_T *buf,
1107 linenr_T lnum,
1108 char_u *sign_name,
1109 int id,
1110 char_u *group,
1111 int prio)
1112{
1113 if (id <= 0)
1114 {
1115 // List signs placed in a file/buffer
1116 // :sign place file={fname}
1117 // :sign place group={group} file={fname}
1118 // :sign place group=* file={fname}
1119 // :sign place buffer={nr}
1120 // :sign place group={group} buffer={nr}
1121 // :sign place group=* buffer={nr}
1122 // :sign place
1123 // :sign place group={group}
1124 // :sign place group=*
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001125 if (lnum >= 0 || sign_name != NULL
1126 || (group != NULL && *group == '\0'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001128 else
1129 sign_list_placed(buf, group);
1130 }
1131 else
1132 {
1133 // Place a new sign
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001134 if (sign_name == NULL || buf == NULL
1135 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001138 return;
1139 }
1140
1141 sign_place(&id, group, sign_name, buf, lnum, prio);
1142 }
1143}
1144
1145/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001146 * ":sign unplace" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001147 */
1148 static void
1149sign_unplace_cmd(
1150 buf_T *buf,
1151 linenr_T lnum,
1152 char_u *sign_name,
1153 int id,
1154 char_u *group)
1155{
1156 if (lnum >= 0 || sign_name != NULL || (group != NULL && *group == '\0'))
1157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001158 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001159 return;
1160 }
1161
1162 if (id == -2)
1163 {
1164 if (buf != NULL)
1165 // :sign unplace * file={fname}
1166 // :sign unplace * group={group} file={fname}
1167 // :sign unplace * group=* file={fname}
1168 // :sign unplace * buffer={nr}
1169 // :sign unplace * group={group} buffer={nr}
1170 // :sign unplace * group=* buffer={nr}
1171 sign_unplace(0, group, buf, 0);
1172 else
1173 // :sign unplace *
1174 // :sign unplace * group={group}
1175 // :sign unplace * group=*
1176 FOR_ALL_BUFFERS(buf)
1177 if (buf->b_signlist != NULL)
1178 buf_delete_signs(buf, group);
1179 }
1180 else
1181 {
1182 if (buf != NULL)
1183 // :sign unplace {id} file={fname}
1184 // :sign unplace {id} group={group} file={fname}
1185 // :sign unplace {id} group=* file={fname}
1186 // :sign unplace {id} buffer={nr}
1187 // :sign unplace {id} group={group} buffer={nr}
1188 // :sign unplace {id} group=* buffer={nr}
1189 sign_unplace(id, group, buf, 0);
1190 else
1191 {
1192 if (id == -1)
1193 {
1194 // :sign unplace group={group}
1195 // :sign unplace group=*
1196 sign_unplace_at_cursor(group);
1197 }
1198 else
1199 {
1200 // :sign unplace {id}
1201 // :sign unplace {id} group={group}
1202 // :sign unplace {id} group=*
1203 FOR_ALL_BUFFERS(buf)
1204 sign_unplace(id, group, buf, 0);
1205 }
1206 }
1207 }
1208}
1209
1210/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001211 * Jump to a placed sign commands:
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001212 * :sign jump {id} file={fname}
1213 * :sign jump {id} buffer={nr}
1214 * :sign jump {id} group={group} file={fname}
1215 * :sign jump {id} group={group} buffer={nr}
1216 */
1217 static void
1218sign_jump_cmd(
1219 buf_T *buf,
1220 linenr_T lnum,
1221 char_u *sign_name,
1222 int id,
1223 char_u *group)
1224{
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001225 if (sign_name == NULL && group == NULL && id == -1)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_argreq));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001228 return;
1229 }
1230
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001231 if (buf == NULL || (group != NULL && *group == '\0')
1232 || lnum >= 0 || sign_name != NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001233 {
1234 // File or buffer is not specified or an empty group is used
1235 // or a line number or a sign name is specified.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001237 return;
1238 }
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001239 (void)sign_jump(id, group, buf);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001240}
1241
1242/*
1243 * Parse the command line arguments for the ":sign place", ":sign unplace" and
1244 * ":sign jump" commands.
1245 * The supported arguments are: line={lnum} name={name} group={group}
1246 * priority={prio} and file={fname} or buffer={nr}.
1247 */
1248 static int
1249parse_sign_cmd_args(
1250 int cmd,
1251 char_u *arg,
1252 char_u **sign_name,
1253 int *signid,
1254 char_u **group,
1255 int *prio,
1256 buf_T **buf,
1257 linenr_T *lnum)
1258{
1259 char_u *arg1;
1260 char_u *name;
1261 char_u *filename = NULL;
Bram Moolenaarb589f952019-01-07 22:10:00 +01001262 int lnum_arg = FALSE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001263
1264 // first arg could be placed sign id
1265 arg1 = arg;
1266 if (VIM_ISDIGIT(*arg))
1267 {
1268 *signid = getdigits(&arg);
1269 if (!VIM_ISWHITE(*arg) && *arg != NUL)
1270 {
1271 *signid = -1;
1272 arg = arg1;
1273 }
1274 else
1275 arg = skipwhite(arg);
1276 }
1277
1278 while (*arg != NUL)
1279 {
1280 if (STRNCMP(arg, "line=", 5) == 0)
1281 {
1282 arg += 5;
1283 *lnum = atoi((char *)arg);
1284 arg = skiptowhite(arg);
Bram Moolenaarb589f952019-01-07 22:10:00 +01001285 lnum_arg = TRUE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001286 }
1287 else if (STRNCMP(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE)
1288 {
1289 if (*signid != -1)
1290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001291 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001292 return FAIL;
1293 }
1294 *signid = -2;
1295 arg = skiptowhite(arg + 1);
1296 }
1297 else if (STRNCMP(arg, "name=", 5) == 0)
1298 {
1299 arg += 5;
1300 name = arg;
1301 arg = skiptowhite(arg);
1302 if (*arg != NUL)
1303 *arg++ = NUL;
1304 while (name[0] == '0' && name[1] != NUL)
1305 ++name;
1306 *sign_name = name;
1307 }
1308 else if (STRNCMP(arg, "group=", 6) == 0)
1309 {
1310 arg += 6;
1311 *group = arg;
1312 arg = skiptowhite(arg);
1313 if (*arg != NUL)
1314 *arg++ = NUL;
1315 }
1316 else if (STRNCMP(arg, "priority=", 9) == 0)
1317 {
1318 arg += 9;
1319 *prio = atoi((char *)arg);
1320 arg = skiptowhite(arg);
1321 }
1322 else if (STRNCMP(arg, "file=", 5) == 0)
1323 {
1324 arg += 5;
1325 filename = arg;
1326 *buf = buflist_findname_exp(arg);
1327 break;
1328 }
1329 else if (STRNCMP(arg, "buffer=", 7) == 0)
1330 {
1331 arg += 7;
1332 filename = arg;
1333 *buf = buflist_findnr((int)getdigits(&arg));
1334 if (*skipwhite(arg) != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 emsg(_(e_trailing));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001336 break;
1337 }
1338 else
1339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001340 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001341 return FAIL;
1342 }
1343 arg = skipwhite(arg);
1344 }
1345
1346 if (filename != NULL && *buf == NULL)
1347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 semsg(_("E158: Invalid buffer name: %s"), filename);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001349 return FAIL;
1350 }
1351
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001352 // If the filename is not supplied for the sign place or the sign jump
1353 // command, then use the current buffer.
Bram Moolenaarb589f952019-01-07 22:10:00 +01001354 if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg)
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001355 || cmd == SIGNCMD_JUMP))
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001356 *buf = curwin->w_buffer;
1357
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001358 return OK;
1359}
1360
1361/*
1362 * ":sign" command
1363 */
1364 void
1365ex_sign(exarg_T *eap)
1366{
1367 char_u *arg = eap->arg;
1368 char_u *p;
1369 int idx;
1370 sign_T *sp;
1371 buf_T *buf = NULL;
1372
1373 // Parse the subcommand.
1374 p = skiptowhite(arg);
1375 idx = sign_cmd_idx(arg, p);
1376 if (idx == SIGNCMD_LAST)
1377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001378 semsg(_("E160: Unknown sign command: %s"), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001379 return;
1380 }
1381 arg = skipwhite(p);
1382
1383 if (idx <= SIGNCMD_LIST)
1384 {
1385 // Define, undefine or list signs.
1386 if (idx == SIGNCMD_LIST && *arg == NUL)
1387 {
1388 // ":sign list": list all defined signs
1389 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
1390 sign_list_defined(sp);
1391 }
1392 else if (*arg == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001393 emsg(_("E156: Missing sign name"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001394 else
1395 {
1396 char_u *name;
1397
1398 // Isolate the sign name. If it's a number skip leading zeroes,
1399 // so that "099" and "99" are the same sign. But keep "0".
1400 p = skiptowhite(arg);
1401 if (*p != NUL)
1402 *p++ = NUL;
1403 while (arg[0] == '0' && arg[1] != NUL)
1404 ++arg;
1405 name = vim_strsave(arg);
1406
1407 if (idx == SIGNCMD_DEFINE)
1408 sign_define_cmd(name, p);
1409 else if (idx == SIGNCMD_LIST)
1410 // ":sign list {name}"
1411 sign_list_by_name(name);
1412 else
1413 // ":sign undefine {name}"
1414 sign_undefine_by_name(name);
1415
1416 vim_free(name);
1417 return;
1418 }
1419 }
1420 else
1421 {
1422 int id = -1;
1423 linenr_T lnum = -1;
1424 char_u *sign_name = NULL;
1425 char_u *group = NULL;
1426 int prio = SIGN_DEF_PRIO;
1427
1428 // Parse command line arguments
1429 if (parse_sign_cmd_args(idx, arg, &sign_name, &id, &group, &prio,
1430 &buf, &lnum) == FAIL)
1431 return;
1432
1433 if (idx == SIGNCMD_PLACE)
1434 sign_place_cmd(buf, lnum, sign_name, id, group, prio);
1435 else if (idx == SIGNCMD_UNPLACE)
1436 sign_unplace_cmd(buf, lnum, sign_name, id, group);
1437 else if (idx == SIGNCMD_JUMP)
1438 sign_jump_cmd(buf, lnum, sign_name, id, group);
1439 }
1440}
1441
1442/*
1443 * Return information about a specified sign
1444 */
1445 static void
1446sign_getinfo(sign_T *sp, dict_T *retdict)
1447{
1448 char_u *p;
1449
1450 dict_add_string(retdict, "name", (char_u *)sp->sn_name);
1451 if (sp->sn_icon != NULL)
1452 dict_add_string(retdict, "icon", (char_u *)sp->sn_icon);
1453 if (sp->sn_text != NULL)
1454 dict_add_string(retdict, "text", (char_u *)sp->sn_text);
1455 if (sp->sn_line_hl > 0)
1456 {
1457 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1458 if (p == NULL)
1459 p = (char_u *)"NONE";
1460 dict_add_string(retdict, "linehl", (char_u *)p);
1461 }
1462 if (sp->sn_text_hl > 0)
1463 {
1464 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1465 if (p == NULL)
1466 p = (char_u *)"NONE";
1467 dict_add_string(retdict, "texthl", (char_u *)p);
1468 }
1469}
1470
1471/*
1472 * If 'name' is NULL, return a list of all the defined signs.
1473 * Otherwise, return information about the specified sign.
1474 */
1475 void
1476sign_getlist(char_u *name, list_T *retlist)
1477{
1478 sign_T *sp = first_sign;
1479 dict_T *dict;
1480
1481 if (name != NULL)
1482 {
1483 sp = sign_find(name, NULL);
1484 if (sp == NULL)
1485 return;
1486 }
1487
1488 for (; sp != NULL && !got_int; sp = sp->sn_next)
1489 {
1490 if ((dict = dict_alloc_id(aid_sign_getlist)) == NULL)
1491 return;
1492 if (list_append_dict(retlist, dict) == FAIL)
1493 return;
1494 sign_getinfo(sp, dict);
1495
1496 if (name != NULL) // handle only the specified sign
1497 break;
1498 }
1499}
1500
1501/*
1502 * Returns information about signs placed in a buffer as list of dicts.
1503 */
1504 void
1505get_buffer_signs(buf_T *buf, list_T *l)
1506{
1507 signlist_T *sign;
1508 dict_T *d;
1509
1510 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1511 {
1512 if ((d = sign_get_info(sign)) != NULL)
1513 list_append_dict(l, d);
1514 }
1515}
1516
1517/*
1518 * Return information about all the signs placed in a buffer
1519 */
1520 static void
1521sign_get_placed_in_buf(
1522 buf_T *buf,
1523 linenr_T lnum,
1524 int sign_id,
1525 char_u *sign_group,
1526 list_T *retlist)
1527{
1528 dict_T *d;
1529 list_T *l;
1530 signlist_T *sign;
1531 dict_T *sdict;
1532
1533 if ((d = dict_alloc_id(aid_sign_getplaced_dict)) == NULL)
1534 return;
1535 list_append_dict(retlist, d);
1536
1537 dict_add_number(d, "bufnr", (long)buf->b_fnum);
1538
1539 if ((l = list_alloc_id(aid_sign_getplaced_list)) == NULL)
1540 return;
1541 dict_add_list(d, "signs", l);
1542
1543 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1544 {
1545 if (!sign_in_group(sign, sign_group))
1546 continue;
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001547 if ((lnum == 0 && sign_id == 0)
1548 || (sign_id == 0 && lnum == sign->lnum)
1549 || (lnum == 0 && sign_id == sign->id)
1550 || (lnum == sign->lnum && sign_id == sign->id))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001551 {
1552 if ((sdict = sign_get_info(sign)) != NULL)
1553 list_append_dict(l, sdict);
1554 }
1555 }
1556}
1557
1558/*
1559 * Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
1560 * sign placed at the line number. If 'lnum' is zero, return all the signs
1561 * placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
1562 */
1563 void
1564sign_get_placed(
1565 buf_T *buf,
1566 linenr_T lnum,
1567 int sign_id,
1568 char_u *sign_group,
1569 list_T *retlist)
1570{
1571 if (buf != NULL)
1572 sign_get_placed_in_buf(buf, lnum, sign_id, sign_group, retlist);
1573 else
1574 {
1575 FOR_ALL_BUFFERS(buf)
1576 {
1577 if (buf->b_signlist != NULL)
1578 sign_get_placed_in_buf(buf, 0, sign_id, sign_group, retlist);
1579 }
1580 }
1581}
1582
1583# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1584/*
1585 * Allocate the icons. Called when the GUI has started. Allows defining
1586 * signs before it starts.
1587 */
1588 void
1589sign_gui_started(void)
1590{
1591 sign_T *sp;
1592
1593 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1594 if (sp->sn_icon != NULL)
1595 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
1596}
1597# endif
1598
1599/*
1600 * List one sign.
1601 */
1602 static void
1603sign_list_defined(sign_T *sp)
1604{
1605 char_u *p;
1606
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001607 smsg("sign %s", sp->sn_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001608 if (sp->sn_icon != NULL)
1609 {
1610 MSG_PUTS(" icon=");
1611 msg_outtrans(sp->sn_icon);
1612# ifdef FEAT_SIGN_ICONS
1613 if (sp->sn_image == NULL)
1614 MSG_PUTS(_(" (NOT FOUND)"));
1615# else
1616 MSG_PUTS(_(" (not supported)"));
1617# endif
1618 }
1619 if (sp->sn_text != NULL)
1620 {
1621 MSG_PUTS(" text=");
1622 msg_outtrans(sp->sn_text);
1623 }
1624 if (sp->sn_line_hl > 0)
1625 {
1626 MSG_PUTS(" linehl=");
1627 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1628 if (p == NULL)
1629 MSG_PUTS("NONE");
1630 else
1631 msg_puts(p);
1632 }
1633 if (sp->sn_text_hl > 0)
1634 {
1635 MSG_PUTS(" texthl=");
1636 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1637 if (p == NULL)
1638 MSG_PUTS("NONE");
1639 else
1640 msg_puts(p);
1641 }
1642}
1643
1644/*
1645 * Undefine a sign and free its memory.
1646 */
1647 static void
1648sign_undefine(sign_T *sp, sign_T *sp_prev)
1649{
1650 vim_free(sp->sn_name);
1651 vim_free(sp->sn_icon);
1652# ifdef FEAT_SIGN_ICONS
1653 if (sp->sn_image != NULL)
1654 {
1655 out_flush();
1656 gui_mch_destroy_sign(sp->sn_image);
1657 }
1658# endif
1659 vim_free(sp->sn_text);
1660 if (sp_prev == NULL)
1661 first_sign = sp->sn_next;
1662 else
1663 sp_prev->sn_next = sp->sn_next;
1664 vim_free(sp);
1665}
1666
1667/*
1668 * Get highlighting attribute for sign "typenr".
1669 * If "line" is TRUE: line highl, if FALSE: text highl.
1670 */
1671 int
1672sign_get_attr(int typenr, int line)
1673{
1674 sign_T *sp;
1675
1676 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1677 if (sp->sn_typenr == typenr)
1678 {
1679 if (line)
1680 {
1681 if (sp->sn_line_hl > 0)
1682 return syn_id2attr(sp->sn_line_hl);
1683 }
1684 else
1685 {
1686 if (sp->sn_text_hl > 0)
1687 return syn_id2attr(sp->sn_text_hl);
1688 }
1689 break;
1690 }
1691 return 0;
1692}
1693
1694/*
1695 * Get text mark for sign "typenr".
1696 * Returns NULL if there isn't one.
1697 */
1698 char_u *
1699sign_get_text(int typenr)
1700{
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_text;
1706 return NULL;
1707}
1708
1709# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1710 void *
1711sign_get_image(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001712 int typenr) // the attribute which may have a sign
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001713{
1714 sign_T *sp;
1715
1716 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1717 if (sp->sn_typenr == typenr)
1718 return sp->sn_image;
1719 return NULL;
1720}
1721# endif
1722
1723/*
1724 * Undefine/free all signs.
1725 */
1726 void
1727free_signs(void)
1728{
1729 while (first_sign != NULL)
1730 sign_undefine(first_sign, NULL);
1731}
1732
1733# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1734static enum
1735{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001736 EXP_SUBCMD, // expand :sign sub-commands
1737 EXP_DEFINE, // expand :sign define {name} args
1738 EXP_PLACE, // expand :sign place {id} args
1739 EXP_UNPLACE, // expand :sign unplace"
1740 EXP_SIGN_NAMES // expand with name of placed signs
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001741} expand_what;
1742
1743/*
1744 * Function given to ExpandGeneric() to obtain the sign command
1745 * expansion.
1746 */
1747 char_u *
1748get_sign_name(expand_T *xp UNUSED, int idx)
1749{
1750 sign_T *sp;
1751 int current_idx;
1752
1753 switch (expand_what)
1754 {
1755 case EXP_SUBCMD:
1756 return (char_u *)cmds[idx];
1757 case EXP_DEFINE:
1758 {
1759 char *define_arg[] =
1760 {
1761 "icon=", "linehl=", "text=", "texthl=", NULL
1762 };
1763 return (char_u *)define_arg[idx];
1764 }
1765 case EXP_PLACE:
1766 {
1767 char *place_arg[] =
1768 {
1769 "line=", "name=", "group=", "priority=", "file=",
1770 "buffer=", NULL
1771 };
1772 return (char_u *)place_arg[idx];
1773 }
1774 case EXP_UNPLACE:
1775 {
1776 char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1777 return (char_u *)unplace_arg[idx];
1778 }
1779 case EXP_SIGN_NAMES:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001780 // Complete with name of signs already defined
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001781 current_idx = 0;
1782 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1783 if (current_idx++ == idx)
1784 return sp->sn_name;
1785 return NULL;
1786 default:
1787 return NULL;
1788 }
1789}
1790
1791/*
1792 * Handle command line completion for :sign command.
1793 */
1794 void
1795set_context_in_sign_cmd(expand_T *xp, char_u *arg)
1796{
1797 char_u *p;
1798 char_u *end_subcmd;
1799 char_u *last;
1800 int cmd_idx;
1801 char_u *begin_subcmd_args;
1802
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001803 // Default: expand subcommands.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001804 xp->xp_context = EXPAND_SIGN;
1805 expand_what = EXP_SUBCMD;
1806 xp->xp_pattern = arg;
1807
1808 end_subcmd = skiptowhite(arg);
1809 if (*end_subcmd == NUL)
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001810 // expand subcmd name
1811 // :sign {subcmd}<CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001812 return;
1813
1814 cmd_idx = sign_cmd_idx(arg, end_subcmd);
1815
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001816 // :sign {subcmd} {subcmd_args}
1817 // |
1818 // begin_subcmd_args
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001819 begin_subcmd_args = skipwhite(end_subcmd);
1820 p = skiptowhite(begin_subcmd_args);
1821 if (*p == NUL)
1822 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001823 //
1824 // Expand first argument of subcmd when possible.
1825 // For ":jump {id}" and ":unplace {id}", we could
1826 // possibly expand the ids of all signs already placed.
1827 //
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001828 xp->xp_pattern = begin_subcmd_args;
1829 switch (cmd_idx)
1830 {
1831 case SIGNCMD_LIST:
1832 case SIGNCMD_UNDEFINE:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001833 // :sign list <CTRL-D>
1834 // :sign undefine <CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001835 expand_what = EXP_SIGN_NAMES;
1836 break;
1837 default:
1838 xp->xp_context = EXPAND_NOTHING;
1839 }
1840 return;
1841 }
1842
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001843 // expand last argument of subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001844
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001845 // :sign define {name} {args}...
1846 // |
1847 // p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001848
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001849 // Loop until reaching last argument.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001850 do
1851 {
1852 p = skipwhite(p);
1853 last = p;
1854 p = skiptowhite(p);
1855 } while (*p != NUL);
1856
1857 p = vim_strchr(last, '=');
1858
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001859 // :sign define {name} {args}... {last}=
1860 // | |
1861 // last p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001862 if (p == NULL)
1863 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001864 // Expand last argument name (before equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001865 xp->xp_pattern = last;
1866 switch (cmd_idx)
1867 {
1868 case SIGNCMD_DEFINE:
1869 expand_what = EXP_DEFINE;
1870 break;
1871 case SIGNCMD_PLACE:
1872 expand_what = EXP_PLACE;
1873 break;
1874 case SIGNCMD_JUMP:
1875 case SIGNCMD_UNPLACE:
1876 expand_what = EXP_UNPLACE;
1877 break;
1878 default:
1879 xp->xp_context = EXPAND_NOTHING;
1880 }
1881 }
1882 else
1883 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001884 // Expand last argument value (after equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001885 xp->xp_pattern = p + 1;
1886 switch (cmd_idx)
1887 {
1888 case SIGNCMD_DEFINE:
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001889 if (STRNCMP(last, "texthl", p - last) == 0
1890 || STRNCMP(last, "linehl", p - last) == 0)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001891 xp->xp_context = EXPAND_HIGHLIGHT;
1892 else if (STRNCMP(last, "icon", p - last) == 0)
1893 xp->xp_context = EXPAND_FILES;
1894 else
1895 xp->xp_context = EXPAND_NOTHING;
1896 break;
1897 case SIGNCMD_PLACE:
1898 if (STRNCMP(last, "name", p - last) == 0)
1899 expand_what = EXP_SIGN_NAMES;
1900 else
1901 xp->xp_context = EXPAND_NOTHING;
1902 break;
1903 default:
1904 xp->xp_context = EXPAND_NOTHING;
1905 }
1906 }
1907}
1908# endif
1909
1910#endif /* FEAT_SIGNS */