Mercurial > vim
comparison src/sign.c @ 15529:233e2c585e03 v8.1.0772
patch 8.1.0772: the sign_define_by_name() function is too long
commit https://github.com/vim/vim/commit/0314236aabcb2ca9d0b74074dadecf68d7c7ed5f
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Jan 18 22:01:42 2019 +0100
patch 8.1.0772: the sign_define_by_name() function is too long
Problem: The sign_define_by_name() function is too long.
Solution: Split it into smaller functions. (Yegappan Lakshmanan,
closes #3819)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 18 Jan 2019 22:15:04 +0100 |
parents | 89e76a598b30 |
children | dd725a8ab112 |
comparison
equal
deleted
inserted
replaced
15528:a8f924ee004b | 15529:233e2c585e03 |
---|---|
725 | 725 |
726 return sp; | 726 return sp; |
727 } | 727 } |
728 | 728 |
729 /* | 729 /* |
730 * Allocate a new sign | |
731 */ | |
732 static sign_T * | |
733 alloc_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 | |
785 sign_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 | |
805 sign_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 /* | |
730 * Define a new sign or update an existing sign | 864 * Define a new sign or update an existing sign |
731 */ | 865 */ |
732 int | 866 int |
733 sign_define_by_name( | 867 sign_define_by_name( |
734 char_u *name, | 868 char_u *name, |
741 sign_T *sp; | 875 sign_T *sp; |
742 | 876 |
743 sp = sign_find(name, &sp_prev); | 877 sp = sign_find(name, &sp_prev); |
744 if (sp == NULL) | 878 if (sp == NULL) |
745 { | 879 { |
746 sign_T *lp; | 880 sp = alloc_new_sign(name); |
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) | 881 if (sp == NULL) |
753 return FAIL; | 882 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); | |
768 emsg(_("E612: Too many signs defined")); | |
769 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 | 883 |
788 // add the new sign to the list of signs | 884 // add the new sign to the list of signs |
789 if (sp_prev == NULL) | 885 if (sp_prev == NULL) |
790 first_sign = sp; | 886 first_sign = sp; |
791 else | 887 else |
792 sp_prev->sn_next = sp; | 888 sp_prev->sn_next = sp; |
793 } | 889 } |
794 | 890 |
795 // set values for a defined sign. | 891 // set values for a defined sign. |
796 if (icon != NULL) | 892 if (icon != NULL) |
797 { | 893 sign_define_init_icon(sp, icon); |
798 vim_free(sp->sn_icon); | 894 |
799 sp->sn_icon = vim_strsave(icon); | 895 if (text != NULL && (sign_define_init_text(sp, text) == FAIL)) |
800 backslash_halve(sp->sn_icon); | 896 return FAIL; |
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 { | |
851 semsg(_("E239: Invalid sign text: %s"), text); | |
852 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 | 897 |
865 if (linehl != NULL) | 898 if (linehl != NULL) |
866 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl)); | 899 sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl)); |
867 | 900 |
868 if (texthl != NULL) | 901 if (texthl != NULL) |