comparison src/ex_cmds.c @ 18265:fe5afdc03bd2 v8.1.2127

patch 8.1.2127: the indent.c file is a bit big Commit: https://github.com/vim/vim/commit/14c01f83487d5c53192297a710eda2b8a4ab17c9 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Oct 9 22:53:08 2019 +0200 patch 8.1.2127: the indent.c file is a bit big Problem: The indent.c file is a bit big. Solution: Move C-indent code a a new cindent.c file. Move other indent-related code to indent.c. (Yegappan Lakshmanan, closes #5031)
author Bram Moolenaar <Bram@vim.org>
date Wed, 09 Oct 2019 23:00:04 +0200
parents d0dfb3b0fe31
children ba5d8c5d77d7
comparison
equal deleted inserted replaced
18264:5202d9b99bee 18265:fe5afdc03bd2
656 vim_free(sortbuf1); 656 vim_free(sortbuf1);
657 vim_free(sortbuf2); 657 vim_free(sortbuf2);
658 vim_regfree(regmatch.regprog); 658 vim_regfree(regmatch.regprog);
659 if (got_int) 659 if (got_int)
660 emsg(_(e_interr)); 660 emsg(_(e_interr));
661 }
662
663 /*
664 * ":retab".
665 */
666 void
667 ex_retab(exarg_T *eap)
668 {
669 linenr_T lnum;
670 int got_tab = FALSE;
671 long num_spaces = 0;
672 long num_tabs;
673 long len;
674 long col;
675 long vcol;
676 long start_col = 0; /* For start of white-space string */
677 long start_vcol = 0; /* For start of white-space string */
678 long old_len;
679 char_u *ptr;
680 char_u *new_line = (char_u *)1; /* init to non-NULL */
681 int did_undo; /* called u_save for current line */
682 #ifdef FEAT_VARTABS
683 int *new_vts_array = NULL;
684 char_u *new_ts_str; /* string value of tab argument */
685 #else
686 int temp;
687 int new_ts;
688 #endif
689 int save_list;
690 linenr_T first_line = 0; /* first changed line */
691 linenr_T last_line = 0; /* last changed line */
692
693 save_list = curwin->w_p_list;
694 curwin->w_p_list = 0; /* don't want list mode here */
695
696 #ifdef FEAT_VARTABS
697 new_ts_str = eap->arg;
698 if (!tabstop_set(eap->arg, &new_vts_array))
699 return;
700 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
701 ++(eap->arg);
702
703 // This ensures that either new_vts_array and new_ts_str are freshly
704 // allocated, or new_vts_array points to an existing array and new_ts_str
705 // is null.
706 if (new_vts_array == NULL)
707 {
708 new_vts_array = curbuf->b_p_vts_array;
709 new_ts_str = NULL;
710 }
711 else
712 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
713 #else
714 new_ts = getdigits(&(eap->arg));
715 if (new_ts < 0)
716 {
717 emsg(_(e_positive));
718 return;
719 }
720 if (new_ts == 0)
721 new_ts = curbuf->b_p_ts;
722 #endif
723 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
724 {
725 ptr = ml_get(lnum);
726 col = 0;
727 vcol = 0;
728 did_undo = FALSE;
729 for (;;)
730 {
731 if (VIM_ISWHITE(ptr[col]))
732 {
733 if (!got_tab && num_spaces == 0)
734 {
735 /* First consecutive white-space */
736 start_vcol = vcol;
737 start_col = col;
738 }
739 if (ptr[col] == ' ')
740 num_spaces++;
741 else
742 got_tab = TRUE;
743 }
744 else
745 {
746 if (got_tab || (eap->forceit && num_spaces > 1))
747 {
748 /* Retabulate this string of white-space */
749
750 /* len is virtual length of white string */
751 len = num_spaces = vcol - start_vcol;
752 num_tabs = 0;
753 if (!curbuf->b_p_et)
754 {
755 #ifdef FEAT_VARTABS
756 int t, s;
757
758 tabstop_fromto(start_vcol, vcol,
759 curbuf->b_p_ts, new_vts_array, &t, &s);
760 num_tabs = t;
761 num_spaces = s;
762 #else
763 temp = new_ts - (start_vcol % new_ts);
764 if (num_spaces >= temp)
765 {
766 num_spaces -= temp;
767 num_tabs++;
768 }
769 num_tabs += num_spaces / new_ts;
770 num_spaces -= (num_spaces / new_ts) * new_ts;
771 #endif
772 }
773 if (curbuf->b_p_et || got_tab ||
774 (num_spaces + num_tabs < len))
775 {
776 if (did_undo == FALSE)
777 {
778 did_undo = TRUE;
779 if (u_save((linenr_T)(lnum - 1),
780 (linenr_T)(lnum + 1)) == FAIL)
781 {
782 new_line = NULL; /* flag out-of-memory */
783 break;
784 }
785 }
786
787 /* len is actual number of white characters used */
788 len = num_spaces + num_tabs;
789 old_len = (long)STRLEN(ptr);
790 new_line = alloc(old_len - col + start_col + len + 1);
791 if (new_line == NULL)
792 break;
793 if (start_col > 0)
794 mch_memmove(new_line, ptr, (size_t)start_col);
795 mch_memmove(new_line + start_col + len,
796 ptr + col, (size_t)(old_len - col + 1));
797 ptr = new_line + start_col;
798 for (col = 0; col < len; col++)
799 ptr[col] = (col < num_tabs) ? '\t' : ' ';
800 ml_replace(lnum, new_line, FALSE);
801 if (first_line == 0)
802 first_line = lnum;
803 last_line = lnum;
804 ptr = new_line;
805 col = start_col + len;
806 }
807 }
808 got_tab = FALSE;
809 num_spaces = 0;
810 }
811 if (ptr[col] == NUL)
812 break;
813 vcol += chartabsize(ptr + col, (colnr_T)vcol);
814 if (has_mbyte)
815 col += (*mb_ptr2len)(ptr + col);
816 else
817 ++col;
818 }
819 if (new_line == NULL) /* out of memory */
820 break;
821 line_breakcheck();
822 }
823 if (got_int)
824 emsg(_(e_interr));
825
826 #ifdef FEAT_VARTABS
827 // If a single value was given then it can be considered equal to
828 // either the value of 'tabstop' or the value of 'vartabstop'.
829 if (tabstop_count(curbuf->b_p_vts_array) == 0
830 && tabstop_count(new_vts_array) == 1
831 && curbuf->b_p_ts == tabstop_first(new_vts_array))
832 ; /* not changed */
833 else if (tabstop_count(curbuf->b_p_vts_array) > 0
834 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
835 ; /* not changed */
836 else
837 redraw_curbuf_later(NOT_VALID);
838 #else
839 if (curbuf->b_p_ts != new_ts)
840 redraw_curbuf_later(NOT_VALID);
841 #endif
842 if (first_line != 0)
843 changed_lines(first_line, 0, last_line + 1, 0L);
844
845 curwin->w_p_list = save_list; /* restore 'list' */
846
847 #ifdef FEAT_VARTABS
848 if (new_ts_str != NULL) /* set the new tabstop */
849 {
850 // If 'vartabstop' is in use or if the value given to retab has more
851 // than one tabstop then update 'vartabstop'.
852 int *old_vts_ary = curbuf->b_p_vts_array;
853
854 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
855 {
856 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
857 OPT_FREE|OPT_LOCAL, 0);
858 curbuf->b_p_vts_array = new_vts_array;
859 vim_free(old_vts_ary);
860 }
861 else
862 {
863 // 'vartabstop' wasn't in use and a single value was given to
864 // retab then update 'tabstop'.
865 curbuf->b_p_ts = tabstop_first(new_vts_array);
866 vim_free(new_vts_array);
867 }
868 vim_free(new_ts_str);
869 }
870 #else
871 curbuf->b_p_ts = new_ts;
872 #endif
873 coladvance(curwin->w_curswant);
874
875 u_clearline();
876 } 661 }
877 662
878 /* 663 /*
879 * :move command - move lines line1-line2 to line dest 664 * :move command - move lines line1-line2 to line dest
880 * 665 *