blob: bab93ff080b6eff7510a25f187d31bf78bf9ff12 [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/*
Bram Moolenaar03142362019-01-18 22:01:42 +0100730 * Allocate a new sign
731 */
732 static sign_T *
733alloc_new_sign(char_u *name)
734{
735 sign_T *sp;
736 sign_T *lp;
737 int start = next_sign_typenr;
738
739 // Allocate a new sign.
740 sp = (sign_T *)alloc_clear_id((unsigned)sizeof(sign_T),
741 aid_sign_define_by_name);
742 if (sp == NULL)
743 return NULL;
744
745 // Check that next_sign_typenr is not already being used.
746 // This only happens after wrapping around. Hopefully
747 // another one got deleted and we can use its number.
748 for (lp = first_sign; lp != NULL; )
749 {
750 if (lp->sn_typenr == next_sign_typenr)
751 {
752 ++next_sign_typenr;
753 if (next_sign_typenr == MAX_TYPENR)
754 next_sign_typenr = 1;
755 if (next_sign_typenr == start)
756 {
757 vim_free(sp);
758 emsg(_("E612: Too many signs defined"));
759 return NULL;
760 }
761 lp = first_sign; // start all over
762 continue;
763 }
764 lp = lp->sn_next;
765 }
766
767 sp->sn_typenr = next_sign_typenr;
768 if (++next_sign_typenr == MAX_TYPENR)
769 next_sign_typenr = 1; // wrap around
770
771 sp->sn_name = vim_strsave(name);
772 if (sp->sn_name == NULL) // out of memory
773 {
774 vim_free(sp);
775 return NULL;
776 }
777
778 return sp;
779}
780
781/*
782 * Initialize the icon information for a new sign
783 */
784 static void
785sign_define_init_icon(sign_T *sp, char_u *icon)
786{
787 vim_free(sp->sn_icon);
788 sp->sn_icon = vim_strsave(icon);
789 backslash_halve(sp->sn_icon);
790# ifdef FEAT_SIGN_ICONS
791 if (gui.in_use)
792 {
793 out_flush();
794 if (sp->sn_image != NULL)
795 gui_mch_destroy_sign(sp->sn_image);
796 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
797 }
798# endif
799}
800
801/*
802 * Initialize the text for a new sign
803 */
804 static int
805sign_define_init_text(sign_T *sp, char_u *text)
806{
807 char_u *s;
808 char_u *endp;
809 int cells;
810 int len;
811
812 endp = text + (int)STRLEN(text);
813
814 // Remove backslashes so that it is possible to use a space.
815 for (s = text; s + 1 < endp; ++s)
816 if (*s == '\\')
817 {
818 STRMOVE(s, s + 1);
819 --endp;
820 }
821
822 // Count cells and check for non-printable chars
823# ifdef FEAT_MBYTE
824 if (has_mbyte)
825 {
826 cells = 0;
827 for (s = text; s < endp; s += (*mb_ptr2len)(s))
828 {
829 if (!vim_isprintc((*mb_ptr2char)(s)))
830 break;
831 cells += (*mb_ptr2cells)(s);
832 }
833 }
834 else
835# endif
836 {
837 for (s = text; s < endp; ++s)
838 if (!vim_isprintc(*s))
839 break;
840 cells = (int)(s - text);
841 }
842
843 // Currently sign text must be one or two display cells
844 if (s != endp || cells < 1 || cells > 2)
845 {
846 semsg(_("E239: Invalid sign text: %s"), text);
847 return FAIL;
848 }
849
850 vim_free(sp->sn_text);
851 // Allocate one byte more if we need to pad up
852 // with a space.
853 len = (int)(endp - text + ((cells == 1) ? 1 : 0));
854 sp->sn_text = vim_strnsave(text, len);
855
856 // For single character sign text, pad with a space.
857 if (sp->sn_text != NULL && cells == 1)
858 STRCPY(sp->sn_text + len - 1, " ");
859
860 return OK;
861}
862
863/*
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100864 * Define a new sign or update an existing sign
865 */
866 int
867sign_define_by_name(
868 char_u *name,
869 char_u *icon,
870 char_u *linehl,
871 char_u *text,
872 char_u *texthl)
873{
874 sign_T *sp_prev;
875 sign_T *sp;
876
877 sp = sign_find(name, &sp_prev);
878 if (sp == NULL)
879 {
Bram Moolenaar03142362019-01-18 22:01:42 +0100880 sp = alloc_new_sign(name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100881 if (sp == NULL)
882 return FAIL;
883
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100884 // add the new sign to the list of signs
885 if (sp_prev == NULL)
886 first_sign = sp;
887 else
888 sp_prev->sn_next = sp;
889 }
890
891 // set values for a defined sign.
892 if (icon != NULL)
Bram Moolenaar03142362019-01-18 22:01:42 +0100893 sign_define_init_icon(sp, icon);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100894
Bram Moolenaar03142362019-01-18 22:01:42 +0100895 if (text != NULL && (sign_define_init_text(sp, text) == FAIL))
896 return FAIL;
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100897
898 if (linehl != NULL)
899 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl));
900
901 if (texthl != NULL)
902 sp->sn_text_hl = syn_check_group(texthl, (int)STRLEN(texthl));
903
904 return OK;
905}
906
907/*
908 * Free the sign specified by 'name'.
909 */
910 int
911sign_undefine_by_name(char_u *name)
912{
913 sign_T *sp_prev;
914 sign_T *sp;
915
916 sp = sign_find(name, &sp_prev);
917 if (sp == NULL)
918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100920 return FAIL;
921 }
922 sign_undefine(sp, sp_prev);
923
924 return OK;
925}
926
927/*
928 * List the signs matching 'name'
929 */
930 static void
931sign_list_by_name(char_u *name)
932{
933 sign_T *sp;
934
935 sp = sign_find(name, NULL);
936 if (sp != NULL)
937 sign_list_defined(sp);
938 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 semsg(_("E155: Unknown sign: %s"), name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100940}
941
942/*
Bram Moolenaar8144acb2019-01-14 23:08:18 +0100943 * Place a sign at the specified file location or update a sign.
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100944 */
945 int
946sign_place(
947 int *sign_id,
948 char_u *sign_group,
949 char_u *sign_name,
950 buf_T *buf,
951 linenr_T lnum,
952 int prio)
953{
954 sign_T *sp;
955
956 // Check for reserved character '*' in group name
957 if (sign_group != NULL && (*sign_group == '*' || *sign_group == '\0'))
958 return FAIL;
959
960 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
961 if (STRCMP(sp->sn_name, sign_name) == 0)
962 break;
963 if (sp == NULL)
964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100965 semsg(_("E155: Unknown sign: %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100966 return FAIL;
967 }
968 if (*sign_id == 0)
969 *sign_id = sign_group_get_next_signid(buf, sign_group);
970
971 if (lnum > 0)
972 // ":sign place {id} line={lnum} name={name} file={fname}":
973 // place a sign
974 buf_addsign(buf, *sign_id, sign_group, prio, lnum, sp->sn_typenr);
975 else
976 // ":sign place {id} file={fname}": change sign type
977 lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr);
978 if (lnum > 0)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100979 redraw_buf_line_later(buf, lnum);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100980 else
981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100982 semsg(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +0100983 return FAIL;
984 }
985
986 return OK;
987}
988
989/*
990 * Unplace the specified sign
991 */
992 int
993sign_unplace(int sign_id, char_u *sign_group, buf_T *buf, linenr_T atlnum)
994{
995 if (buf->b_signlist == NULL) // No signs in the buffer
996 return OK;
997
998 if (sign_id == 0)
999 {
1000 // Delete all the signs in the specified buffer
1001 redraw_buf_later(buf, NOT_VALID);
1002 buf_delete_signs(buf, sign_group);
1003 }
1004 else
1005 {
1006 linenr_T lnum;
1007
1008 // Delete only the specified signs
1009 lnum = buf_delsign(buf, atlnum, sign_id, sign_group);
1010 if (lnum == 0)
1011 return FAIL;
1012 }
1013
1014 return OK;
1015}
1016
1017/*
1018 * Unplace the sign at the current cursor line.
1019 */
1020 static void
1021sign_unplace_at_cursor(char_u *groupname)
1022{
1023 int id = -1;
1024
1025 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum, groupname);
1026 if (id > 0)
1027 sign_unplace(id, groupname, curwin->w_buffer, curwin->w_cursor.lnum);
1028 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001029 emsg(_("E159: Missing sign number"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001030}
1031
1032/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001033 * Jump to a sign.
1034 */
1035 linenr_T
1036sign_jump(int sign_id, char_u *sign_group, buf_T *buf)
1037{
1038 linenr_T lnum;
1039
1040 if ((lnum = buf_findsign(buf, sign_id, sign_group)) <= 0)
1041 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001042 semsg(_("E157: Invalid sign ID: %d"), sign_id);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001043 return -1;
1044 }
1045
1046 // goto a sign ...
1047 if (buf_jump_open_win(buf) != NULL)
1048 { // ... in a current window
1049 curwin->w_cursor.lnum = lnum;
1050 check_cursor_lnum();
1051 beginline(BL_WHITE);
1052 }
1053 else
1054 { // ... not currently in a window
1055 char_u *cmd;
1056
1057 if (buf->b_fname == NULL)
1058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001059 emsg(_("E934: Cannot jump to a buffer that does not have a name"));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001060 return -1;
1061 }
1062 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
1063 if (cmd == NULL)
1064 return -1;
1065 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
1066 do_cmdline_cmd(cmd);
1067 vim_free(cmd);
1068 }
1069# ifdef FEAT_FOLDING
1070 foldOpenCursor();
1071# endif
1072
1073 return lnum;
1074}
1075
1076/*
1077 * ":sign define {name} ..." command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001078 */
1079 static void
1080sign_define_cmd(char_u *sign_name, char_u *cmdline)
1081{
1082 char_u *arg;
1083 char_u *p = cmdline;
1084 char_u *icon = NULL;
1085 char_u *text = NULL;
1086 char_u *linehl = NULL;
1087 char_u *texthl = NULL;
1088 int failed = FALSE;
1089
1090 // set values for a defined sign.
1091 for (;;)
1092 {
1093 arg = skipwhite(p);
1094 if (*arg == NUL)
1095 break;
1096 p = skiptowhite_esc(arg);
1097 if (STRNCMP(arg, "icon=", 5) == 0)
1098 {
1099 arg += 5;
1100 icon = vim_strnsave(arg, (int)(p - arg));
1101 }
1102 else if (STRNCMP(arg, "text=", 5) == 0)
1103 {
1104 arg += 5;
1105 text = vim_strnsave(arg, (int)(p - arg));
1106 }
1107 else if (STRNCMP(arg, "linehl=", 7) == 0)
1108 {
1109 arg += 7;
1110 linehl = vim_strnsave(arg, (int)(p - arg));
1111 }
1112 else if (STRNCMP(arg, "texthl=", 7) == 0)
1113 {
1114 arg += 7;
1115 texthl = vim_strnsave(arg, (int)(p - arg));
1116 }
1117 else
1118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001119 semsg(_(e_invarg2), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001120 failed = TRUE;
1121 break;
1122 }
1123 }
1124
1125 if (!failed)
1126 sign_define_by_name(sign_name, icon, linehl, text, texthl);
1127
1128 vim_free(icon);
1129 vim_free(text);
1130 vim_free(linehl);
1131 vim_free(texthl);
1132}
1133
1134/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001135 * ":sign place" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001136 */
1137 static void
1138sign_place_cmd(
1139 buf_T *buf,
1140 linenr_T lnum,
1141 char_u *sign_name,
1142 int id,
1143 char_u *group,
1144 int prio)
1145{
1146 if (id <= 0)
1147 {
1148 // List signs placed in a file/buffer
1149 // :sign place file={fname}
1150 // :sign place group={group} file={fname}
1151 // :sign place group=* file={fname}
1152 // :sign place buffer={nr}
1153 // :sign place group={group} buffer={nr}
1154 // :sign place group=* buffer={nr}
1155 // :sign place
1156 // :sign place group={group}
1157 // :sign place group=*
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001158 if (lnum >= 0 || sign_name != NULL
1159 || (group != NULL && *group == '\0'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001161 else
1162 sign_list_placed(buf, group);
1163 }
1164 else
1165 {
1166 // Place a new sign
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001167 if (sign_name == NULL || buf == NULL
1168 || (group != NULL && *group == '\0'))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001170 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001171 return;
1172 }
1173
1174 sign_place(&id, group, sign_name, buf, lnum, prio);
1175 }
1176}
1177
1178/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001179 * ":sign unplace" command
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001180 */
1181 static void
1182sign_unplace_cmd(
1183 buf_T *buf,
1184 linenr_T lnum,
1185 char_u *sign_name,
1186 int id,
1187 char_u *group)
1188{
1189 if (lnum >= 0 || sign_name != NULL || (group != NULL && *group == '\0'))
1190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001192 return;
1193 }
1194
1195 if (id == -2)
1196 {
1197 if (buf != NULL)
1198 // :sign unplace * file={fname}
1199 // :sign unplace * group={group} file={fname}
1200 // :sign unplace * group=* file={fname}
1201 // :sign unplace * buffer={nr}
1202 // :sign unplace * group={group} buffer={nr}
1203 // :sign unplace * group=* buffer={nr}
1204 sign_unplace(0, group, buf, 0);
1205 else
1206 // :sign unplace *
1207 // :sign unplace * group={group}
1208 // :sign unplace * group=*
1209 FOR_ALL_BUFFERS(buf)
1210 if (buf->b_signlist != NULL)
1211 buf_delete_signs(buf, group);
1212 }
1213 else
1214 {
1215 if (buf != NULL)
1216 // :sign unplace {id} file={fname}
1217 // :sign unplace {id} group={group} file={fname}
1218 // :sign unplace {id} group=* file={fname}
1219 // :sign unplace {id} buffer={nr}
1220 // :sign unplace {id} group={group} buffer={nr}
1221 // :sign unplace {id} group=* buffer={nr}
1222 sign_unplace(id, group, buf, 0);
1223 else
1224 {
1225 if (id == -1)
1226 {
1227 // :sign unplace group={group}
1228 // :sign unplace group=*
1229 sign_unplace_at_cursor(group);
1230 }
1231 else
1232 {
1233 // :sign unplace {id}
1234 // :sign unplace {id} group={group}
1235 // :sign unplace {id} group=*
1236 FOR_ALL_BUFFERS(buf)
1237 sign_unplace(id, group, buf, 0);
1238 }
1239 }
1240 }
1241}
1242
1243/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001244 * Jump to a placed sign commands:
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001245 * :sign jump {id} file={fname}
1246 * :sign jump {id} buffer={nr}
1247 * :sign jump {id} group={group} file={fname}
1248 * :sign jump {id} group={group} buffer={nr}
1249 */
1250 static void
1251sign_jump_cmd(
1252 buf_T *buf,
1253 linenr_T lnum,
1254 char_u *sign_name,
1255 int id,
1256 char_u *group)
1257{
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001258 if (sign_name == NULL && group == NULL && id == -1)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 emsg(_(e_argreq));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001261 return;
1262 }
1263
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001264 if (buf == NULL || (group != NULL && *group == '\0')
1265 || lnum >= 0 || sign_name != NULL)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001266 {
1267 // File or buffer is not specified or an empty group is used
1268 // or a line number or a sign name is specified.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001269 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001270 return;
1271 }
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001272 (void)sign_jump(id, group, buf);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001273}
1274
1275/*
1276 * Parse the command line arguments for the ":sign place", ":sign unplace" and
1277 * ":sign jump" commands.
1278 * The supported arguments are: line={lnum} name={name} group={group}
1279 * priority={prio} and file={fname} or buffer={nr}.
1280 */
1281 static int
1282parse_sign_cmd_args(
1283 int cmd,
1284 char_u *arg,
1285 char_u **sign_name,
1286 int *signid,
1287 char_u **group,
1288 int *prio,
1289 buf_T **buf,
1290 linenr_T *lnum)
1291{
1292 char_u *arg1;
1293 char_u *name;
1294 char_u *filename = NULL;
Bram Moolenaarb589f952019-01-07 22:10:00 +01001295 int lnum_arg = FALSE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001296
1297 // first arg could be placed sign id
1298 arg1 = arg;
1299 if (VIM_ISDIGIT(*arg))
1300 {
1301 *signid = getdigits(&arg);
1302 if (!VIM_ISWHITE(*arg) && *arg != NUL)
1303 {
1304 *signid = -1;
1305 arg = arg1;
1306 }
1307 else
1308 arg = skipwhite(arg);
1309 }
1310
1311 while (*arg != NUL)
1312 {
1313 if (STRNCMP(arg, "line=", 5) == 0)
1314 {
1315 arg += 5;
1316 *lnum = atoi((char *)arg);
1317 arg = skiptowhite(arg);
Bram Moolenaarb589f952019-01-07 22:10:00 +01001318 lnum_arg = TRUE;
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001319 }
1320 else if (STRNCMP(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE)
1321 {
1322 if (*signid != -1)
1323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001324 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001325 return FAIL;
1326 }
1327 *signid = -2;
1328 arg = skiptowhite(arg + 1);
1329 }
1330 else if (STRNCMP(arg, "name=", 5) == 0)
1331 {
1332 arg += 5;
1333 name = arg;
1334 arg = skiptowhite(arg);
1335 if (*arg != NUL)
1336 *arg++ = NUL;
1337 while (name[0] == '0' && name[1] != NUL)
1338 ++name;
1339 *sign_name = name;
1340 }
1341 else if (STRNCMP(arg, "group=", 6) == 0)
1342 {
1343 arg += 6;
1344 *group = arg;
1345 arg = skiptowhite(arg);
1346 if (*arg != NUL)
1347 *arg++ = NUL;
1348 }
1349 else if (STRNCMP(arg, "priority=", 9) == 0)
1350 {
1351 arg += 9;
1352 *prio = atoi((char *)arg);
1353 arg = skiptowhite(arg);
1354 }
1355 else if (STRNCMP(arg, "file=", 5) == 0)
1356 {
1357 arg += 5;
1358 filename = arg;
1359 *buf = buflist_findname_exp(arg);
1360 break;
1361 }
1362 else if (STRNCMP(arg, "buffer=", 7) == 0)
1363 {
1364 arg += 7;
1365 filename = arg;
1366 *buf = buflist_findnr((int)getdigits(&arg));
1367 if (*skipwhite(arg) != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_(e_trailing));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001369 break;
1370 }
1371 else
1372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001373 emsg(_(e_invarg));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001374 return FAIL;
1375 }
1376 arg = skipwhite(arg);
1377 }
1378
1379 if (filename != NULL && *buf == NULL)
1380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001381 semsg(_("E158: Invalid buffer name: %s"), filename);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001382 return FAIL;
1383 }
1384
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001385 // If the filename is not supplied for the sign place or the sign jump
1386 // command, then use the current buffer.
Bram Moolenaarb589f952019-01-07 22:10:00 +01001387 if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg)
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001388 || cmd == SIGNCMD_JUMP))
Bram Moolenaarb328cca2019-01-06 16:24:01 +01001389 *buf = curwin->w_buffer;
1390
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001391 return OK;
1392}
1393
1394/*
1395 * ":sign" command
1396 */
1397 void
1398ex_sign(exarg_T *eap)
1399{
1400 char_u *arg = eap->arg;
1401 char_u *p;
1402 int idx;
1403 sign_T *sp;
1404 buf_T *buf = NULL;
1405
1406 // Parse the subcommand.
1407 p = skiptowhite(arg);
1408 idx = sign_cmd_idx(arg, p);
1409 if (idx == SIGNCMD_LAST)
1410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001411 semsg(_("E160: Unknown sign command: %s"), arg);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001412 return;
1413 }
1414 arg = skipwhite(p);
1415
1416 if (idx <= SIGNCMD_LIST)
1417 {
1418 // Define, undefine or list signs.
1419 if (idx == SIGNCMD_LIST && *arg == NUL)
1420 {
1421 // ":sign list": list all defined signs
1422 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
1423 sign_list_defined(sp);
1424 }
1425 else if (*arg == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001426 emsg(_("E156: Missing sign name"));
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001427 else
1428 {
1429 char_u *name;
1430
1431 // Isolate the sign name. If it's a number skip leading zeroes,
1432 // so that "099" and "99" are the same sign. But keep "0".
1433 p = skiptowhite(arg);
1434 if (*p != NUL)
1435 *p++ = NUL;
1436 while (arg[0] == '0' && arg[1] != NUL)
1437 ++arg;
1438 name = vim_strsave(arg);
1439
1440 if (idx == SIGNCMD_DEFINE)
1441 sign_define_cmd(name, p);
1442 else if (idx == SIGNCMD_LIST)
1443 // ":sign list {name}"
1444 sign_list_by_name(name);
1445 else
1446 // ":sign undefine {name}"
1447 sign_undefine_by_name(name);
1448
1449 vim_free(name);
1450 return;
1451 }
1452 }
1453 else
1454 {
1455 int id = -1;
1456 linenr_T lnum = -1;
1457 char_u *sign_name = NULL;
1458 char_u *group = NULL;
1459 int prio = SIGN_DEF_PRIO;
1460
1461 // Parse command line arguments
1462 if (parse_sign_cmd_args(idx, arg, &sign_name, &id, &group, &prio,
1463 &buf, &lnum) == FAIL)
1464 return;
1465
1466 if (idx == SIGNCMD_PLACE)
1467 sign_place_cmd(buf, lnum, sign_name, id, group, prio);
1468 else if (idx == SIGNCMD_UNPLACE)
1469 sign_unplace_cmd(buf, lnum, sign_name, id, group);
1470 else if (idx == SIGNCMD_JUMP)
1471 sign_jump_cmd(buf, lnum, sign_name, id, group);
1472 }
1473}
1474
1475/*
1476 * Return information about a specified sign
1477 */
1478 static void
1479sign_getinfo(sign_T *sp, dict_T *retdict)
1480{
1481 char_u *p;
1482
1483 dict_add_string(retdict, "name", (char_u *)sp->sn_name);
1484 if (sp->sn_icon != NULL)
1485 dict_add_string(retdict, "icon", (char_u *)sp->sn_icon);
1486 if (sp->sn_text != NULL)
1487 dict_add_string(retdict, "text", (char_u *)sp->sn_text);
1488 if (sp->sn_line_hl > 0)
1489 {
1490 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1491 if (p == NULL)
1492 p = (char_u *)"NONE";
1493 dict_add_string(retdict, "linehl", (char_u *)p);
1494 }
1495 if (sp->sn_text_hl > 0)
1496 {
1497 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1498 if (p == NULL)
1499 p = (char_u *)"NONE";
1500 dict_add_string(retdict, "texthl", (char_u *)p);
1501 }
1502}
1503
1504/*
1505 * If 'name' is NULL, return a list of all the defined signs.
1506 * Otherwise, return information about the specified sign.
1507 */
1508 void
1509sign_getlist(char_u *name, list_T *retlist)
1510{
1511 sign_T *sp = first_sign;
1512 dict_T *dict;
1513
1514 if (name != NULL)
1515 {
1516 sp = sign_find(name, NULL);
1517 if (sp == NULL)
1518 return;
1519 }
1520
1521 for (; sp != NULL && !got_int; sp = sp->sn_next)
1522 {
1523 if ((dict = dict_alloc_id(aid_sign_getlist)) == NULL)
1524 return;
1525 if (list_append_dict(retlist, dict) == FAIL)
1526 return;
1527 sign_getinfo(sp, dict);
1528
1529 if (name != NULL) // handle only the specified sign
1530 break;
1531 }
1532}
1533
1534/*
1535 * Returns information about signs placed in a buffer as list of dicts.
1536 */
1537 void
1538get_buffer_signs(buf_T *buf, list_T *l)
1539{
1540 signlist_T *sign;
1541 dict_T *d;
1542
1543 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1544 {
1545 if ((d = sign_get_info(sign)) != NULL)
1546 list_append_dict(l, d);
1547 }
1548}
1549
1550/*
1551 * Return information about all the signs placed in a buffer
1552 */
1553 static void
1554sign_get_placed_in_buf(
1555 buf_T *buf,
1556 linenr_T lnum,
1557 int sign_id,
1558 char_u *sign_group,
1559 list_T *retlist)
1560{
1561 dict_T *d;
1562 list_T *l;
1563 signlist_T *sign;
1564 dict_T *sdict;
1565
1566 if ((d = dict_alloc_id(aid_sign_getplaced_dict)) == NULL)
1567 return;
1568 list_append_dict(retlist, d);
1569
1570 dict_add_number(d, "bufnr", (long)buf->b_fnum);
1571
1572 if ((l = list_alloc_id(aid_sign_getplaced_list)) == NULL)
1573 return;
1574 dict_add_list(d, "signs", l);
1575
1576 FOR_ALL_SIGNS_IN_BUF(buf, sign)
1577 {
1578 if (!sign_in_group(sign, sign_group))
1579 continue;
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001580 if ((lnum == 0 && sign_id == 0)
1581 || (sign_id == 0 && lnum == sign->lnum)
1582 || (lnum == 0 && sign_id == sign->id)
1583 || (lnum == sign->lnum && sign_id == sign->id))
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001584 {
1585 if ((sdict = sign_get_info(sign)) != NULL)
1586 list_append_dict(l, sdict);
1587 }
1588 }
1589}
1590
1591/*
1592 * Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
1593 * sign placed at the line number. If 'lnum' is zero, return all the signs
1594 * placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
1595 */
1596 void
1597sign_get_placed(
1598 buf_T *buf,
1599 linenr_T lnum,
1600 int sign_id,
1601 char_u *sign_group,
1602 list_T *retlist)
1603{
1604 if (buf != NULL)
1605 sign_get_placed_in_buf(buf, lnum, sign_id, sign_group, retlist);
1606 else
1607 {
1608 FOR_ALL_BUFFERS(buf)
1609 {
1610 if (buf->b_signlist != NULL)
1611 sign_get_placed_in_buf(buf, 0, sign_id, sign_group, retlist);
1612 }
1613 }
1614}
1615
1616# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1617/*
1618 * Allocate the icons. Called when the GUI has started. Allows defining
1619 * signs before it starts.
1620 */
1621 void
1622sign_gui_started(void)
1623{
1624 sign_T *sp;
1625
1626 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1627 if (sp->sn_icon != NULL)
1628 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
1629}
1630# endif
1631
1632/*
1633 * List one sign.
1634 */
1635 static void
1636sign_list_defined(sign_T *sp)
1637{
1638 char_u *p;
1639
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001640 smsg("sign %s", sp->sn_name);
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001641 if (sp->sn_icon != NULL)
1642 {
1643 MSG_PUTS(" icon=");
1644 msg_outtrans(sp->sn_icon);
1645# ifdef FEAT_SIGN_ICONS
1646 if (sp->sn_image == NULL)
1647 MSG_PUTS(_(" (NOT FOUND)"));
1648# else
1649 MSG_PUTS(_(" (not supported)"));
1650# endif
1651 }
1652 if (sp->sn_text != NULL)
1653 {
1654 MSG_PUTS(" text=");
1655 msg_outtrans(sp->sn_text);
1656 }
1657 if (sp->sn_line_hl > 0)
1658 {
1659 MSG_PUTS(" linehl=");
1660 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
1661 if (p == NULL)
1662 MSG_PUTS("NONE");
1663 else
1664 msg_puts(p);
1665 }
1666 if (sp->sn_text_hl > 0)
1667 {
1668 MSG_PUTS(" texthl=");
1669 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
1670 if (p == NULL)
1671 MSG_PUTS("NONE");
1672 else
1673 msg_puts(p);
1674 }
1675}
1676
1677/*
1678 * Undefine a sign and free its memory.
1679 */
1680 static void
1681sign_undefine(sign_T *sp, sign_T *sp_prev)
1682{
1683 vim_free(sp->sn_name);
1684 vim_free(sp->sn_icon);
1685# ifdef FEAT_SIGN_ICONS
1686 if (sp->sn_image != NULL)
1687 {
1688 out_flush();
1689 gui_mch_destroy_sign(sp->sn_image);
1690 }
1691# endif
1692 vim_free(sp->sn_text);
1693 if (sp_prev == NULL)
1694 first_sign = sp->sn_next;
1695 else
1696 sp_prev->sn_next = sp->sn_next;
1697 vim_free(sp);
1698}
1699
1700/*
1701 * Get highlighting attribute for sign "typenr".
1702 * If "line" is TRUE: line highl, if FALSE: text highl.
1703 */
1704 int
1705sign_get_attr(int typenr, int line)
1706{
1707 sign_T *sp;
1708
1709 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1710 if (sp->sn_typenr == typenr)
1711 {
1712 if (line)
1713 {
1714 if (sp->sn_line_hl > 0)
1715 return syn_id2attr(sp->sn_line_hl);
1716 }
1717 else
1718 {
1719 if (sp->sn_text_hl > 0)
1720 return syn_id2attr(sp->sn_text_hl);
1721 }
1722 break;
1723 }
1724 return 0;
1725}
1726
1727/*
1728 * Get text mark for sign "typenr".
1729 * Returns NULL if there isn't one.
1730 */
1731 char_u *
1732sign_get_text(int typenr)
1733{
1734 sign_T *sp;
1735
1736 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1737 if (sp->sn_typenr == typenr)
1738 return sp->sn_text;
1739 return NULL;
1740}
1741
1742# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
1743 void *
1744sign_get_image(
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001745 int typenr) // the attribute which may have a sign
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001746{
1747 sign_T *sp;
1748
1749 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1750 if (sp->sn_typenr == typenr)
1751 return sp->sn_image;
1752 return NULL;
1753}
1754# endif
1755
1756/*
1757 * Undefine/free all signs.
1758 */
1759 void
1760free_signs(void)
1761{
1762 while (first_sign != NULL)
1763 sign_undefine(first_sign, NULL);
1764}
1765
1766# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1767static enum
1768{
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001769 EXP_SUBCMD, // expand :sign sub-commands
1770 EXP_DEFINE, // expand :sign define {name} args
1771 EXP_PLACE, // expand :sign place {id} args
1772 EXP_UNPLACE, // expand :sign unplace"
1773 EXP_SIGN_NAMES // expand with name of placed signs
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001774} expand_what;
1775
1776/*
1777 * Function given to ExpandGeneric() to obtain the sign command
1778 * expansion.
1779 */
1780 char_u *
1781get_sign_name(expand_T *xp UNUSED, int idx)
1782{
1783 sign_T *sp;
1784 int current_idx;
1785
1786 switch (expand_what)
1787 {
1788 case EXP_SUBCMD:
1789 return (char_u *)cmds[idx];
1790 case EXP_DEFINE:
1791 {
1792 char *define_arg[] =
1793 {
1794 "icon=", "linehl=", "text=", "texthl=", NULL
1795 };
1796 return (char_u *)define_arg[idx];
1797 }
1798 case EXP_PLACE:
1799 {
1800 char *place_arg[] =
1801 {
1802 "line=", "name=", "group=", "priority=", "file=",
1803 "buffer=", NULL
1804 };
1805 return (char_u *)place_arg[idx];
1806 }
1807 case EXP_UNPLACE:
1808 {
1809 char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
1810 return (char_u *)unplace_arg[idx];
1811 }
1812 case EXP_SIGN_NAMES:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001813 // Complete with name of signs already defined
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001814 current_idx = 0;
1815 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
1816 if (current_idx++ == idx)
1817 return sp->sn_name;
1818 return NULL;
1819 default:
1820 return NULL;
1821 }
1822}
1823
1824/*
1825 * Handle command line completion for :sign command.
1826 */
1827 void
1828set_context_in_sign_cmd(expand_T *xp, char_u *arg)
1829{
1830 char_u *p;
1831 char_u *end_subcmd;
1832 char_u *last;
1833 int cmd_idx;
1834 char_u *begin_subcmd_args;
1835
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001836 // Default: expand subcommands.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001837 xp->xp_context = EXPAND_SIGN;
1838 expand_what = EXP_SUBCMD;
1839 xp->xp_pattern = arg;
1840
1841 end_subcmd = skiptowhite(arg);
1842 if (*end_subcmd == NUL)
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001843 // expand subcmd name
1844 // :sign {subcmd}<CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001845 return;
1846
1847 cmd_idx = sign_cmd_idx(arg, end_subcmd);
1848
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001849 // :sign {subcmd} {subcmd_args}
1850 // |
1851 // begin_subcmd_args
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001852 begin_subcmd_args = skipwhite(end_subcmd);
1853 p = skiptowhite(begin_subcmd_args);
1854 if (*p == NUL)
1855 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001856 //
1857 // Expand first argument of subcmd when possible.
1858 // For ":jump {id}" and ":unplace {id}", we could
1859 // possibly expand the ids of all signs already placed.
1860 //
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001861 xp->xp_pattern = begin_subcmd_args;
1862 switch (cmd_idx)
1863 {
1864 case SIGNCMD_LIST:
1865 case SIGNCMD_UNDEFINE:
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001866 // :sign list <CTRL-D>
1867 // :sign undefine <CTRL-D>
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001868 expand_what = EXP_SIGN_NAMES;
1869 break;
1870 default:
1871 xp->xp_context = EXPAND_NOTHING;
1872 }
1873 return;
1874 }
1875
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001876 // expand last argument of subcmd
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001877
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001878 // :sign define {name} {args}...
1879 // |
1880 // p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001881
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001882 // Loop until reaching last argument.
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001883 do
1884 {
1885 p = skipwhite(p);
1886 last = p;
1887 p = skiptowhite(p);
1888 } while (*p != NUL);
1889
1890 p = vim_strchr(last, '=');
1891
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001892 // :sign define {name} {args}... {last}=
1893 // | |
1894 // last p
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001895 if (p == NULL)
1896 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001897 // Expand last argument name (before equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001898 xp->xp_pattern = last;
1899 switch (cmd_idx)
1900 {
1901 case SIGNCMD_DEFINE:
1902 expand_what = EXP_DEFINE;
1903 break;
1904 case SIGNCMD_PLACE:
1905 expand_what = EXP_PLACE;
1906 break;
1907 case SIGNCMD_JUMP:
1908 case SIGNCMD_UNPLACE:
1909 expand_what = EXP_UNPLACE;
1910 break;
1911 default:
1912 xp->xp_context = EXPAND_NOTHING;
1913 }
1914 }
1915 else
1916 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001917 // Expand last argument value (after equal sign).
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001918 xp->xp_pattern = p + 1;
1919 switch (cmd_idx)
1920 {
1921 case SIGNCMD_DEFINE:
Bram Moolenaar27a472c2019-01-09 21:47:30 +01001922 if (STRNCMP(last, "texthl", p - last) == 0
1923 || STRNCMP(last, "linehl", p - last) == 0)
Bram Moolenaarbbea4702019-01-01 13:20:31 +01001924 xp->xp_context = EXPAND_HIGHLIGHT;
1925 else if (STRNCMP(last, "icon", p - last) == 0)
1926 xp->xp_context = EXPAND_FILES;
1927 else
1928 xp->xp_context = EXPAND_NOTHING;
1929 break;
1930 case SIGNCMD_PLACE:
1931 if (STRNCMP(last, "name", p - last) == 0)
1932 expand_what = EXP_SIGN_NAMES;
1933 else
1934 xp->xp_context = EXPAND_NOTHING;
1935 break;
1936 default:
1937 xp->xp_context = EXPAND_NOTHING;
1938 }
1939 }
1940}
1941# endif
1942
1943#endif /* FEAT_SIGNS */