changeset 18124:2a806e3c39f6 v8.1.2057

patch 8.1.2057: the screen.c file is much too big Commit: https://github.com/vim/vim/commit/7528d1f6b5422750eb778dfb550cfd0b0e540964 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Sep 19 23:06:20 2019 +0200 patch 8.1.2057: the screen.c file is much too big Problem: The screen.c file is much too big. Solution: Split it in three parts. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/4943)
author Bram Moolenaar <Bram@vim.org>
date Thu, 19 Sep 2019 23:15:05 +0200
parents ceb4be0b23c7
children b5ef528e2121
files Filelist src/Make_cyg_ming.mak src/Make_morph.mak src/Make_mvc.mak src/Make_vms.mms src/Makefile src/README.md src/drawline.c src/drawscreen.c src/globals.h src/proto.h src/proto/drawline.pro src/proto/drawscreen.pro src/proto/screen.pro src/screen.c src/version.c src/vim.h
diffstat 17 files changed, 6446 insertions(+), 6515 deletions(-) [+]
line wrap: on
line diff
--- a/Filelist
+++ b/Filelist
@@ -32,6 +32,8 @@ SRC_ALL =	\
 		src/dict.c \
 		src/diff.c \
 		src/digraph.c \
+		src/drawline.c \
+		src/drawscreen.c \
 		src/edit.c \
 		src/eval.c \
 		src/evalbuffer.c \
@@ -192,6 +194,8 @@ SRC_ALL =	\
 		src/proto/dict.pro \
 		src/proto/diff.pro \
 		src/proto/digraph.pro \
+		src/proto/drawline.pro \
+		src/proto/drawscreen.pro \
 		src/proto/edit.pro \
 		src/proto/eval.pro \
 		src/proto/evalbuffer.pro \
--- a/src/Make_cyg_ming.mak
+++ b/src/Make_cyg_ming.mak
@@ -719,6 +719,8 @@ OBJ = \
 	$(OUTDIR)/dict.o \
 	$(OUTDIR)/diff.o \
 	$(OUTDIR)/digraph.o \
+	$(OUTDIR)/drawline.o \
+	$(OUTDIR)/drawscreen.o \
 	$(OUTDIR)/edit.o \
 	$(OUTDIR)/eval.o \
 	$(OUTDIR)/evalbuffer.o \
--- a/src/Make_morph.mak
+++ b/src/Make_morph.mak
@@ -39,6 +39,8 @@ SRC =	arabic.c						\
 	dict.c							\
 	diff.c							\
 	digraph.c						\
+	drawline.c						\
+	drawscreen.c						\
 	edit.c							\
 	eval.c							\
 	evalbuffer.c						\
--- a/src/Make_mvc.mak
+++ b/src/Make_mvc.mak
@@ -726,6 +726,8 @@ OBJ = \
 	$(OUTDIR)\dict.obj \
 	$(OUTDIR)\diff.obj \
 	$(OUTDIR)\digraph.obj \
+	$(OUTDIR)\drawline.obj \
+	$(OUTDIR)\drawscreen.obj \
 	$(OUTDIR)\edit.obj \
 	$(OUTDIR)\eval.obj \
 	$(OUTDIR)\evalbuffer.obj \
@@ -1484,6 +1486,10 @@ testclean:
 
 $(OUTDIR)/digraph.obj:	$(OUTDIR) digraph.c  $(INCL)
 
+$(OUTDIR)/drawline.obj:	$(OUTDIR) drawline.c  $(INCL)
+
+$(OUTDIR)/drawscreen.obj:	$(OUTDIR) drawscreen.c  $(INCL)
+
 $(OUTDIR)/edit.obj:	$(OUTDIR) edit.c  $(INCL)
 
 $(OUTDIR)/eval.obj:	$(OUTDIR) eval.c  $(INCL)
@@ -1783,6 +1789,8 @@ proto.h: \
 	proto/dict.pro \
 	proto/diff.pro \
 	proto/digraph.pro \
+	proto/drawline.pro \
+	proto/drawscreen.pro \
 	proto/edit.pro \
 	proto/eval.pro \
 	proto/evalbuffer.pro \
--- a/src/Make_vms.mms
+++ b/src/Make_vms.mms
@@ -309,8 +309,8 @@ ALL_LIBS = $(LIBS) $(GUI_LIB_DIR) $(GUI_
 
 SRC =	arabic.c arglist.c autocmd.c beval.c blob.c blowfish.c buffer.c \
 	change.c charset.c cmdexpand.c cmdhist.c crypt.c crypt_zip.c \
-	debugger.c dict.c diff.c digraph.c edit.c eval.c evalbuffer.c \
-	evalfunc.c \
+	debugger.c dict.c diff.c digraph.c drawline.c drawscreen.c edit.c \
+	eval.c evalbuffer.c evalfunc.c \
 	evalvars.c evalwindow.c ex_cmds.c ex_cmds2.c ex_docmd.c ex_eval.c \
 	ex_getln.c \
 	if_cscope.c if_xcmdsrv.c fileio.c filepath.c, findfile.c fold.c \
@@ -329,8 +329,8 @@ SRC =	arabic.c arglist.c autocmd.c beval
 OBJ = 	arabic.obj arglist.obj autocmd.obj beval.obj blob.obj blowfish.obj \
 	buffer.obj change.obj charset.obj cmdexpand.obj cmdhist.obj \
 	crypt.obj crypt_zip.obj debugger.obj dict.obj diff.obj digraph.obj \
-	edit.obj eval.obj evalbuffer.obj evalfunc.obj evalvars.obj \
-	evalwindow.obj ex_cmds.obj ex_cmds2.obj \
+	drawline.obj drawscreen.obj edit.obj eval.obj evalbuffer.obj \
+	evalfunc.obj evalvars.obj evalwindow.obj ex_cmds.obj ex_cmds2.obj \
 	ex_docmd.obj ex_eval.obj ex_getln.obj if_cscope.obj if_xcmdsrv.obj \
 	fileio.obj filepath.obj \
 	findfile.obj fold.obj getchar.obj hardcopy.obj hashtab.obj \
@@ -556,16 +556,22 @@ dict.obj : dict.c vim.h [.auto]config.h 
  globals.h
 diff.obj : diff.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 digraph.obj : digraph.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
  globals.h
+drawline.obj : drawline.c vim.h [.auto]config.h feature.h os_unix.h \
+ ascii.h keymap.h term.h macros.h structs.h regexp.h \
+ gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
+ globals.h
+drawscreen.obj : drawscreen.c vim.h [.auto]config.h feature.h os_unix.h \
+ ascii.h keymap.h term.h macros.h structs.h regexp.h \
+ gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
+ globals.h
 edit.obj : edit.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 eval.obj : eval.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
  [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
@@ -620,8 +626,7 @@ findfile.obj : findfile.c vim.h [.auto]c
  globals.h
 fold.obj : fold.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 getchar.obj : getchar.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
@@ -669,8 +674,7 @@ map.obj : map.c vim.h [.auto]config.h fe
  [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
 mark.obj : mark.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 memfile.obj : memfile.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
@@ -681,8 +685,7 @@ memline.obj : memline.c vim.h [.auto]con
  globals.h
 menu.obj : menu.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 message.obj : message.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
@@ -693,24 +696,20 @@ misc1.obj : misc1.c vim.h [.auto]config.
  version.h
 misc2.obj : misc2.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 move.obj : move.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 mbyte.obj : mbyte.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 normal.obj : normal.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
  globals.h
 ops.obj : ops.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 option.obj : option.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
@@ -789,42 +788,33 @@ syntax.obj : syntax.c vim.h [.auto]confi
  globals.h
 tag.obj : tag.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 term.obj : term.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 termlib.obj : termlib.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 testing.obj : testing.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 textprop.obj : textprop.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 ui.obj : ui.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 undo.obj : undo.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 usercmd.obj : usercmd.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h option.h structs.h \
  regexp.h gui.h beval.h [.proto]gui_beval.pro alloc.h ex_cmds.h spell.h \
  proto.h globals.h
-
 userfunc.obj : userfunc.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h option.h structs.h \
  regexp.h gui.h beval.h [.proto]gui_beval.pro alloc.h ex_cmds.h spell.h \
  proto.h globals.h
-
 version.obj : version.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
@@ -839,8 +829,7 @@ window.obj : window.c vim.h [.auto]confi
  globals.h
 gui.obj : gui.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 gui_gtk.obj : gui_gtk.c gui_gtk_f.h vim.h [.auto]config.h feature.h \
  os_unix.h   ascii.h keymap.h term.h macros.h structs.h \
  regexp.h gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h \
@@ -893,8 +882,7 @@ gui_at_fs.obj : gui_at_fs.c vim.h [.auto
  globals.h gui_at_sb.h
 pty.obj : pty.c vim.h [.auto]config.h feature.h os_unix.h   \
  ascii.h keymap.h term.h macros.h structs.h regexp.h gui.h beval.h \
- [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h \
-
+ [.proto]gui_beval.pro option.h ex_cmds.h proto.h globals.h
 hangulin.obj : hangulin.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
--- a/src/Makefile
+++ b/src/Makefile
@@ -1593,6 +1593,8 @@ BASIC_SRC = \
 	dict.c \
 	diff.c \
 	digraph.c \
+	drawline.c \
+	drawscreen.c \
 	edit.c \
 	eval.c \
 	evalbuffer.c \
@@ -1727,6 +1729,8 @@ OBJ_COMMON = \
 	objects/dict.o \
 	objects/diff.o \
 	objects/digraph.o \
+	objects/drawline.o \
+	objects/drawscreen.o \
 	objects/edit.o \
 	objects/eval.o \
 	objects/evalbuffer.o \
@@ -1874,6 +1878,8 @@ PRO_AUTO = \
 	dict.pro \
 	diff.pro \
 	digraph.pro \
+	drawline.pro \
+	drawscreen.pro \
 	edit.pro \
 	eval.pro \
 	evalbuffer.pro \
@@ -3083,6 +3089,12 @@ objects/diff.o: diff.c $(XDIFF_INCL)
 objects/digraph.o: digraph.c
 	$(CCC) -o $@ digraph.c
 
+objects/drawline.o: drawline.c
+	$(CCC) -o $@ drawline.c
+
+objects/drawscreen.o: drawscreen.c
+	$(CCC) -o $@ drawscreen.c
+
 objects/edit.o: edit.c
 	$(CCC) -o $@ edit.c
 
@@ -3613,6 +3625,14 @@ objects/digraph.o: digraph.c vim.h proto
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
  proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
  proto.h globals.h
+objects/drawline.o: drawline.c vim.h protodef.h auto/config.h feature.h os_unix.h \
+ auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
+ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
+ proto.h globals.h
+objects/drawscreen.o: drawscreen.c vim.h protodef.h auto/config.h feature.h os_unix.h \
+ auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
+ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
+ proto.h globals.h
 objects/edit.o: edit.c vim.h protodef.h auto/config.h feature.h os_unix.h \
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
  proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
--- a/src/README.md
+++ b/src/README.md
@@ -32,6 +32,8 @@ cmdexpand.c	| command-line completion
 cmdhist.c	| command-line history
 debugger.c	| vim script debugger
 diff.c		| diff mode (vimdiff)
+drawline.c	| drawing a window line
+drawscreen.c	| drawing the windows
 eval.c		| expression evaluation
 evalbuffer.c	| buffer related built-in functions
 evalfunc.c	| built-in functions
new file mode 100644
--- /dev/null
+++ b/src/drawline.c
@@ -0,0 +1,3131 @@
+/* vi:set ts=8 sts=4 sw=4 noet:
+ *
+ * VIM - Vi IMproved	by Bram Moolenaar
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * drawline.c: Functions for drawing window lines on the screen.
+ * This is the middle level, drawscreen. is the higher level and screen.c the
+ * lower level.
+ */
+
+#include "vim.h"
+
+#ifdef FEAT_SYN_HL
+/*
+ * Advance **color_cols and return TRUE when there are columns to draw.
+ */
+    static int
+advance_color_col(int vcol, int **color_cols)
+{
+    while (**color_cols >= 0 && vcol > **color_cols)
+	++*color_cols;
+    return (**color_cols >= 0);
+}
+#endif
+
+#ifdef FEAT_SYN_HL
+/*
+ * Used when 'cursorlineopt' contains "screenline": compute the margins between
+ * which the highlighting is used.
+ */
+    static void
+margin_columns_win(win_T *wp, int *left_col, int *right_col)
+{
+    // cache previous calculations depending on w_virtcol
+    static int saved_w_virtcol;
+    static win_T *prev_wp;
+    static int prev_left_col;
+    static int prev_right_col;
+    static int prev_col_off;
+
+    int cur_col_off = win_col_off(wp);
+    int	width1;
+    int	width2;
+
+    if (saved_w_virtcol == wp->w_virtcol
+	    && prev_wp == wp && prev_col_off == cur_col_off)
+    {
+	*right_col = prev_right_col;
+	*left_col = prev_left_col;
+	return;
+    }
+
+    width1 = wp->w_width - cur_col_off;
+    width2 = width1 + win_col_off2(wp);
+
+    *left_col = 0;
+    *right_col = width1;
+
+    if (wp->w_virtcol >= (colnr_T)width1)
+	*right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
+    if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
+	*left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
+
+    // cache values
+    prev_left_col = *left_col;
+    prev_right_col = *right_col;
+    prev_wp = wp;
+    saved_w_virtcol = wp->w_virtcol;
+    prev_col_off = cur_col_off;
+}
+#endif
+
+#ifdef FEAT_SIGNS
+/*
+ * Get information needed to display the sign in line 'lnum' in window 'wp'.
+ * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
+ * Otherwise the sign is going to be displayed in the sign column.
+ */
+    static void
+get_sign_display_info(
+	int		nrcol,
+	win_T		*wp,
+	linenr_T	lnum UNUSED,
+	sign_attrs_T	*sattr,
+	int		wcr_attr,
+	int		row,
+	int		startrow,
+	int		filler_lines UNUSED,
+	int		filler_todo UNUSED,
+	int		*c_extrap,
+	int		*c_finalp,
+	char_u		*extra,
+	char_u		**pp_extra,
+	int		*n_extrap,
+	int		*char_attrp)
+{
+    int	text_sign;
+# ifdef FEAT_SIGN_ICONS
+    int	icon_sign;
+# endif
+
+    // Draw two cells with the sign value or blank.
+    *c_extrap = ' ';
+    *c_finalp = NUL;
+    if (nrcol)
+	*n_extrap = number_width(wp) + 1;
+    else
+    {
+	*char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
+	*n_extrap = 2;
+    }
+
+    if (row == startrow
+#ifdef FEAT_DIFF
+	    + filler_lines && filler_todo <= 0
+#endif
+       )
+    {
+	text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
+# ifdef FEAT_SIGN_ICONS
+	icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
+	if (gui.in_use && icon_sign != 0)
+	{
+	    // Use the image in this position.
+	    if (nrcol)
+	    {
+		*c_extrap = NUL;
+		sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
+		*pp_extra = extra;
+		*n_extrap = (int)STRLEN(*pp_extra);
+	    }
+	    else
+		*c_extrap = SIGN_BYTE;
+#  ifdef FEAT_NETBEANS_INTG
+	    if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
+	    {
+		if (nrcol)
+		{
+		    *c_extrap = NUL;
+		    sprintf((char *)extra, "%-*c ", number_width(wp),
+							MULTISIGN_BYTE);
+		    *pp_extra = extra;
+		    *n_extrap = (int)STRLEN(*pp_extra);
+		}
+		else
+		    *c_extrap = MULTISIGN_BYTE;
+	    }
+#  endif
+	    *c_finalp = NUL;
+	    *char_attrp = icon_sign;
+	}
+	else
+# endif
+	    if (text_sign != 0)
+	    {
+		*pp_extra = sattr->text;
+		if (*pp_extra != NULL)
+		{
+		    if (nrcol)
+		    {
+			int n, width = number_width(wp) - 2;
+
+			for (n = 0; n < width; n++)
+			    extra[n] = ' ';
+			extra[n] = 0;
+			STRCAT(extra, *pp_extra);
+			STRCAT(extra, " ");
+			*pp_extra = extra;
+		    }
+		    *c_extrap = NUL;
+		    *c_finalp = NUL;
+		    *n_extrap = (int)STRLEN(*pp_extra);
+		}
+		*char_attrp = sattr->texthl;
+	    }
+    }
+}
+#endif
+
+#ifdef FEAT_TEXT_PROP
+static textprop_T	*current_text_props = NULL;
+static buf_T		*current_buf = NULL;
+
+    static int
+text_prop_compare(const void *s1, const void *s2)
+{
+    int  idx1, idx2;
+    proptype_T  *pt1, *pt2;
+    colnr_T col1, col2;
+
+    idx1 = *(int *)s1;
+    idx2 = *(int *)s2;
+    pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
+    pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
+    if (pt1 == pt2)
+	return 0;
+    if (pt1 == NULL)
+	return -1;
+    if (pt2 == NULL)
+	return 1;
+    if (pt1->pt_priority != pt2->pt_priority)
+	return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
+    col1 = current_text_props[idx1].tp_col;
+    col2 = current_text_props[idx2].tp_col;
+    return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
+}
+#endif
+
+/*
+ * Display line "lnum" of window 'wp' on the screen.
+ * Start at row "startrow", stop when "endrow" is reached.
+ * wp->w_virtcol needs to be valid.
+ *
+ * Return the number of last row the line occupies.
+ */
+    int
+win_line(
+    win_T	*wp,
+    linenr_T	lnum,
+    int		startrow,
+    int		endrow,
+    int		nochange UNUSED,	// not updating for changed text
+    int		number_only)		// only update the number column
+{
+    int		col = 0;		// visual column on screen
+    unsigned	off;			// offset in ScreenLines/ScreenAttrs
+    int		c = 0;			// init for GCC
+    long	vcol = 0;		// virtual column (for tabs)
+#ifdef FEAT_LINEBREAK
+    long	vcol_sbr = -1;		// virtual column after showbreak
+#endif
+    long	vcol_prev = -1;		// "vcol" of previous character
+    char_u	*line;			// current line
+    char_u	*ptr;			// current position in "line"
+    int		row;			// row in the window, excl w_winrow
+    int		screen_row;		// row on the screen, incl w_winrow
+
+    char_u	extra[21];		// "%ld " and 'fdc' must fit in here
+    int		n_extra = 0;		// number of extra chars
+    char_u	*p_extra = NULL;	// string of extra chars, plus NUL
+    char_u	*p_extra_free = NULL;   // p_extra needs to be freed
+    int		c_extra = NUL;		// extra chars, all the same
+    int		c_final = NUL;		// final char, mandatory if set
+    int		extra_attr = 0;		// attributes when n_extra != 0
+    static char_u *at_end_str = (char_u *)""; // used for p_extra when
+					   // displaying lcs_eol at end-of-line
+    int		lcs_eol_one = lcs_eol;	// lcs_eol until it's been used
+    int		lcs_prec_todo = lcs_prec;   // lcs_prec until it's been used
+
+    // saved "extra" items for when draw_state becomes WL_LINE (again)
+    int		saved_n_extra = 0;
+    char_u	*saved_p_extra = NULL;
+    int		saved_c_extra = 0;
+    int		saved_c_final = 0;
+    int		saved_char_attr = 0;
+
+    int		n_attr = 0;		// chars with special attr
+    int		saved_attr2 = 0;	// char_attr saved for n_attr
+    int		n_attr3 = 0;		// chars with overruling special attr
+    int		saved_attr3 = 0;	// char_attr saved for n_attr3
+
+    int		n_skip = 0;		// nr of chars to skip for 'nowrap'
+
+    int		fromcol = -10;		// start of inverting
+    int		tocol = MAXCOL;		// end of inverting
+    int		fromcol_prev = -2;	// start of inverting after cursor
+    int		noinvcur = FALSE;	// don't invert the cursor
+    pos_T	*top, *bot;
+    int		lnum_in_visual_area = FALSE;
+    pos_T	pos;
+    long	v;
+
+    int		char_attr = 0;		// attributes for next character
+    int		attr_pri = FALSE;	// char_attr has priority
+    int		area_highlighting = FALSE; // Visual or incsearch highlighting
+					   // in this line
+    int		vi_attr = 0;		// attributes for Visual and incsearch
+					// highlighting
+    int		wcr_attr = 0;		// attributes from 'wincolor'
+    int		win_attr = 0;		// background for whole window, except
+					// margins and "~" lines.
+    int		area_attr = 0;		// attributes desired by highlighting
+    int		search_attr = 0;	// attributes desired by 'hlsearch'
+#ifdef FEAT_SYN_HL
+    int		vcol_save_attr = 0;	// saved attr for 'cursorcolumn'
+    int		syntax_attr = 0;	// attributes desired by syntax
+    int		has_syntax = FALSE;	// this buffer has syntax highl.
+    int		save_did_emsg;
+    int		draw_color_col = FALSE;	// highlight colorcolumn
+    int		*color_cols = NULL;	// pointer to according columns array
+#endif
+    int		eol_hl_off = 0;		// 1 if highlighted char after EOL
+#ifdef FEAT_TEXT_PROP
+    int		text_prop_count;
+    int		text_prop_next = 0;	// next text property to use
+    textprop_T	*text_props = NULL;
+    int		*text_prop_idxs = NULL;
+    int		text_props_active = 0;
+    proptype_T  *text_prop_type = NULL;
+    int		text_prop_attr = 0;
+    int		text_prop_combine = FALSE;
+#endif
+#ifdef FEAT_SPELL
+    int		has_spell = FALSE;	// this buffer has spell checking
+# define SPWORDLEN 150
+    char_u	nextline[SPWORDLEN * 2];// text with start of the next line
+    int		nextlinecol = 0;	// column where nextline[] starts
+    int		nextline_idx = 0;	// index in nextline[] where next line
+					// starts
+    int		spell_attr = 0;		// attributes desired by spelling
+    int		word_end = 0;		// last byte with same spell_attr
+    static linenr_T  checked_lnum = 0;	// line number for "checked_col"
+    static int	checked_col = 0;	// column in "checked_lnum" up to which
+					// there are no spell errors
+    static int	cap_col = -1;		// column to check for Cap word
+    static linenr_T capcol_lnum = 0;	// line number where "cap_col" used
+    int		cur_checked_col = 0;	// checked column for current line
+#endif
+    int		extra_check = 0;	// has extra highlighting
+    int		multi_attr = 0;		// attributes desired by multibyte
+    int		mb_l = 1;		// multi-byte byte length
+    int		mb_c = 0;		// decoded multi-byte character
+    int		mb_utf8 = FALSE;	// screen char is UTF-8 char
+    int		u8cc[MAX_MCO];		// composing UTF-8 chars
+#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
+    int		filler_lines = 0;	// nr of filler lines to be drawn
+    int		filler_todo = 0;	// nr of filler lines still to do + 1
+#endif
+#ifdef FEAT_DIFF
+    hlf_T	diff_hlf = (hlf_T)0;	// type of diff highlighting
+    int		change_start = MAXCOL;	// first col of changed area
+    int		change_end = -1;	// last col of changed area
+#endif
+    colnr_T	trailcol = MAXCOL;	// start of trailing spaces
+#ifdef FEAT_LINEBREAK
+    int		need_showbreak = FALSE; // overlong line, skipping first x
+					// chars
+#endif
+#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
+	|| defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
+# define LINE_ATTR
+    int		line_attr = 0;		// attribute for the whole line
+    int		line_attr_save;
+#endif
+#ifdef FEAT_SIGNS
+    int		sign_present = FALSE;
+    sign_attrs_T sattr;
+#endif
+#ifdef FEAT_ARABIC
+    int		prev_c = 0;		// previous Arabic character
+    int		prev_c1 = 0;		// first composing char for prev_c
+#endif
+#if defined(LINE_ATTR)
+    int		did_line_attr = 0;
+#endif
+#ifdef FEAT_TERMINAL
+    int		get_term_attr = FALSE;
+#endif
+#ifdef FEAT_SYN_HL
+    int		cul_attr = 0;		// set when 'cursorline' active
+
+    // 'cursorlineopt' has "screenline" and cursor is in this line
+    int		cul_screenline = FALSE;
+
+    // margin columns for the screen line, needed for when 'cursorlineopt'
+    // contains "screenline"
+    int		left_curline_col = 0;
+    int		right_curline_col = 0;
+#endif
+
+    // draw_state: items that are drawn in sequence:
+#define WL_START	0		// nothing done yet
+#ifdef FEAT_CMDWIN
+# define WL_CMDLINE	WL_START + 1	// cmdline window column
+#else
+# define WL_CMDLINE	WL_START
+#endif
+#ifdef FEAT_FOLDING
+# define WL_FOLD	WL_CMDLINE + 1	// 'foldcolumn'
+#else
+# define WL_FOLD	WL_CMDLINE
+#endif
+#ifdef FEAT_SIGNS
+# define WL_SIGN	WL_FOLD + 1	// column for signs
+#else
+# define WL_SIGN	WL_FOLD		// column for signs
+#endif
+#define WL_NR		WL_SIGN + 1	// line number
+#ifdef FEAT_LINEBREAK
+# define WL_BRI		WL_NR + 1	// 'breakindent'
+#else
+# define WL_BRI		WL_NR
+#endif
+#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
+# define WL_SBR		WL_BRI + 1	// 'showbreak' or 'diff'
+#else
+# define WL_SBR		WL_BRI
+#endif
+#define WL_LINE		WL_SBR + 1	// text in the line
+    int		draw_state = WL_START;	// what to draw next
+#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
+    int		feedback_col = 0;
+    int		feedback_old_attr = -1;
+#endif
+    int		screen_line_flags = 0;
+
+#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
+    int		match_conc	= 0;	// cchar for match functions
+#endif
+#ifdef FEAT_CONCEAL
+    int		syntax_flags	= 0;
+    int		syntax_seqnr	= 0;
+    int		prev_syntax_id	= 0;
+    int		conceal_attr	= HL_ATTR(HLF_CONCEAL);
+    int		is_concealing	= FALSE;
+    int		boguscols	= 0;	// nonexistent columns added to force
+					// wrapping
+    int		vcol_off	= 0;	// offset for concealed characters
+    int		did_wcol	= FALSE;
+    int		old_boguscols   = 0;
+# define VCOL_HLC (vcol - vcol_off)
+# define FIX_FOR_BOGUSCOLS \
+    { \
+	n_extra += vcol_off; \
+	vcol -= vcol_off; \
+	vcol_off = 0; \
+	col -= boguscols; \
+	old_boguscols = boguscols; \
+	boguscols = 0; \
+    }
+#else
+# define VCOL_HLC (vcol)
+#endif
+
+    if (startrow > endrow)		// past the end already!
+	return startrow;
+
+    row = startrow;
+    screen_row = row + W_WINROW(wp);
+
+    if (!number_only)
+    {
+	// To speed up the loop below, set extra_check when there is linebreak,
+	// trailing white space and/or syntax processing to be done.
+#ifdef FEAT_LINEBREAK
+	extra_check = wp->w_p_lbr;
+#endif
+#ifdef FEAT_SYN_HL
+	if (syntax_present(wp) && !wp->w_s->b_syn_error
+# ifdef SYN_TIME_LIMIT
+		&& !wp->w_s->b_syn_slow
+# endif
+	   )
+	{
+	    // Prepare for syntax highlighting in this line.  When there is an
+	    // error, stop syntax highlighting.
+	    save_did_emsg = did_emsg;
+	    did_emsg = FALSE;
+	    syntax_start(wp, lnum);
+	    if (did_emsg)
+		wp->w_s->b_syn_error = TRUE;
+	    else
+	    {
+		did_emsg = save_did_emsg;
+#ifdef SYN_TIME_LIMIT
+		if (!wp->w_s->b_syn_slow)
+#endif
+		{
+		    has_syntax = TRUE;
+		    extra_check = TRUE;
+		}
+	    }
+	}
+
+	// Check for columns to display for 'colorcolumn'.
+	color_cols = wp->w_p_cc_cols;
+	if (color_cols != NULL)
+	    draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
+#endif
+
+#ifdef FEAT_TERMINAL
+	if (term_show_buffer(wp->w_buffer))
+	{
+	    extra_check = TRUE;
+	    get_term_attr = TRUE;
+	    win_attr = term_get_attr(wp->w_buffer, lnum, -1);
+	}
+#endif
+
+#ifdef FEAT_SPELL
+	if (wp->w_p_spell
+		&& *wp->w_s->b_p_spl != NUL
+		&& wp->w_s->b_langp.ga_len > 0
+		&& *(char **)(wp->w_s->b_langp.ga_data) != NULL)
+	{
+	    // Prepare for spell checking.
+	    has_spell = TRUE;
+	    extra_check = TRUE;
+
+	    // Get the start of the next line, so that words that wrap to the
+	    // next line are found too: "et<line-break>al.".
+	    // Trick: skip a few chars for C/shell/Vim comments
+	    nextline[SPWORDLEN] = NUL;
+	    if (lnum < wp->w_buffer->b_ml.ml_line_count)
+	    {
+		line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
+		spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
+	    }
+
+	    // When a word wrapped from the previous line the start of the
+	    // current line is valid.
+	    if (lnum == checked_lnum)
+		cur_checked_col = checked_col;
+	    checked_lnum = 0;
+
+	    // When there was a sentence end in the previous line may require a
+	    // word starting with capital in this line.  In line 1 always check
+	    // the first word.
+	    if (lnum != capcol_lnum)
+		cap_col = -1;
+	    if (lnum == 1)
+		cap_col = 0;
+	    capcol_lnum = 0;
+	}
+#endif
+
+	// handle Visual active in this window
+	if (VIsual_active && wp->w_buffer == curwin->w_buffer)
+	{
+	    if (LTOREQ_POS(curwin->w_cursor, VIsual))
+	    {
+		// Visual is after curwin->w_cursor
+		top = &curwin->w_cursor;
+		bot = &VIsual;
+	    }
+	    else
+	    {
+		// Visual is before curwin->w_cursor
+		top = &VIsual;
+		bot = &curwin->w_cursor;
+	    }
+	    lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
+	    if (VIsual_mode == Ctrl_V)
+	    {
+		// block mode
+		if (lnum_in_visual_area)
+		{
+		    fromcol = wp->w_old_cursor_fcol;
+		    tocol = wp->w_old_cursor_lcol;
+		}
+	    }
+	    else
+	    {
+		// non-block mode
+		if (lnum > top->lnum && lnum <= bot->lnum)
+		    fromcol = 0;
+		else if (lnum == top->lnum)
+		{
+		    if (VIsual_mode == 'V')	// linewise
+			fromcol = 0;
+		    else
+		    {
+			getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
+			if (gchar_pos(top) == NUL)
+			    tocol = fromcol + 1;
+		    }
+		}
+		if (VIsual_mode != 'V' && lnum == bot->lnum)
+		{
+		    if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
+		    {
+			fromcol = -10;
+			tocol = MAXCOL;
+		    }
+		    else if (bot->col == MAXCOL)
+			tocol = MAXCOL;
+		    else
+		    {
+			pos = *bot;
+			if (*p_sel == 'e')
+			    getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
+			else
+			{
+			    getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
+			    ++tocol;
+			}
+		    }
+		}
+	    }
+
+	    // Check if the character under the cursor should not be inverted
+	    if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
+#ifdef FEAT_GUI
+		    && !gui.in_use
+#endif
+		    )
+		noinvcur = TRUE;
+
+	    // if inverting in this line set area_highlighting
+	    if (fromcol >= 0)
+	    {
+		area_highlighting = TRUE;
+		vi_attr = HL_ATTR(HLF_V);
+#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
+		if ((clip_star.available && !clip_star.owned
+						      && clip_isautosel_star())
+			|| (clip_plus.available && !clip_plus.owned
+						     && clip_isautosel_plus()))
+		    vi_attr = HL_ATTR(HLF_VNC);
+#endif
+	    }
+	}
+
+	// handle 'incsearch' and ":s///c" highlighting
+	else if (highlight_match
+		&& wp == curwin
+		&& lnum >= curwin->w_cursor.lnum
+		&& lnum <= curwin->w_cursor.lnum + search_match_lines)
+	{
+	    if (lnum == curwin->w_cursor.lnum)
+		getvcol(curwin, &(curwin->w_cursor),
+					      (colnr_T *)&fromcol, NULL, NULL);
+	    else
+		fromcol = 0;
+	    if (lnum == curwin->w_cursor.lnum + search_match_lines)
+	    {
+		pos.lnum = lnum;
+		pos.col = search_match_endcol;
+		getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
+	    }
+	    else
+		tocol = MAXCOL;
+	    // do at least one character; happens when past end of line
+	    if (fromcol == tocol)
+		tocol = fromcol + 1;
+	    area_highlighting = TRUE;
+	    vi_attr = HL_ATTR(HLF_I);
+	}
+    }
+
+#ifdef FEAT_DIFF
+    filler_lines = diff_check(wp, lnum);
+    if (filler_lines < 0)
+    {
+	if (filler_lines == -1)
+	{
+	    if (diff_find_change(wp, lnum, &change_start, &change_end))
+		diff_hlf = HLF_ADD;	// added line
+	    else if (change_start == 0)
+		diff_hlf = HLF_TXD;	// changed text
+	    else
+		diff_hlf = HLF_CHD;	// changed line
+	}
+	else
+	    diff_hlf = HLF_ADD;		// added line
+	filler_lines = 0;
+	area_highlighting = TRUE;
+    }
+    if (lnum == wp->w_topline)
+	filler_lines = wp->w_topfill;
+    filler_todo = filler_lines;
+#endif
+
+#ifdef FEAT_SIGNS
+    sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
+#endif
+
+#ifdef LINE_ATTR
+# ifdef FEAT_SIGNS
+    // If this line has a sign with line highlighting set line_attr.
+    if (sign_present)
+	line_attr = sattr.linehl;
+# endif
+# if defined(FEAT_QUICKFIX)
+    // Highlight the current line in the quickfix window.
+    if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
+	line_attr = HL_ATTR(HLF_QFL);
+# endif
+    if (line_attr != 0)
+	area_highlighting = TRUE;
+#endif
+
+    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
+    ptr = line;
+
+#ifdef FEAT_SPELL
+    if (has_spell && !number_only)
+    {
+	// For checking first word with a capital skip white space.
+	if (cap_col == 0)
+	    cap_col = getwhitecols(line);
+
+	// To be able to spell-check over line boundaries copy the end of the
+	// current line into nextline[].  Above the start of the next line was
+	// copied to nextline[SPWORDLEN].
+	if (nextline[SPWORDLEN] == NUL)
+	{
+	    // No next line or it is empty.
+	    nextlinecol = MAXCOL;
+	    nextline_idx = 0;
+	}
+	else
+	{
+	    v = (long)STRLEN(line);
+	    if (v < SPWORDLEN)
+	    {
+		// Short line, use it completely and append the start of the
+		// next line.
+		nextlinecol = 0;
+		mch_memmove(nextline, line, (size_t)v);
+		STRMOVE(nextline + v, nextline + SPWORDLEN);
+		nextline_idx = v + 1;
+	    }
+	    else
+	    {
+		// Long line, use only the last SPWORDLEN bytes.
+		nextlinecol = v - SPWORDLEN;
+		mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
+		nextline_idx = SPWORDLEN + 1;
+	    }
+	}
+    }
+#endif
+
+    if (wp->w_p_list)
+    {
+	if (lcs_space || lcs_trail || lcs_nbsp)
+	    extra_check = TRUE;
+	// find start of trailing whitespace
+	if (lcs_trail)
+	{
+	    trailcol = (colnr_T)STRLEN(ptr);
+	    while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
+		--trailcol;
+	    trailcol += (colnr_T) (ptr - line);
+	}
+    }
+
+    wcr_attr = get_wcr_attr(wp);
+    if (wcr_attr != 0)
+    {
+	win_attr = wcr_attr;
+	area_highlighting = TRUE;
+    }
+#ifdef FEAT_TEXT_PROP
+    if (WIN_IS_POPUP(wp))
+	screen_line_flags |= SLF_POPUP;
+#endif
+
+    // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
+    // first character to be displayed.
+    if (wp->w_p_wrap)
+	v = wp->w_skipcol;
+    else
+	v = wp->w_leftcol;
+    if (v > 0 && !number_only)
+    {
+	char_u	*prev_ptr = ptr;
+
+	while (vcol < v && *ptr != NUL)
+	{
+	    c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
+	    vcol += c;
+	    prev_ptr = ptr;
+	    MB_PTR_ADV(ptr);
+	}
+
+	// When:
+	// - 'cuc' is set, or
+	// - 'colorcolumn' is set, or
+	// - 'virtualedit' is set, or
+	// - the visual mode is active,
+	// the end of the line may be before the start of the displayed part.
+	if (vcol < v && (
+#ifdef FEAT_SYN_HL
+	     wp->w_p_cuc || draw_color_col ||
+#endif
+	     virtual_active() ||
+	     (VIsual_active && wp->w_buffer == curwin->w_buffer)))
+	    vcol = v;
+
+	// Handle a character that's not completely on the screen: Put ptr at
+	// that character but skip the first few screen characters.
+	if (vcol > v)
+	{
+	    vcol -= c;
+	    ptr = prev_ptr;
+	    // If the character fits on the screen, don't need to skip it.
+	    // Except for a TAB.
+	    if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
+	       n_skip = v - vcol;
+	}
+
+	// Adjust for when the inverted text is before the screen,
+	// and when the start of the inverted text is before the screen.
+	if (tocol <= vcol)
+	    fromcol = 0;
+	else if (fromcol >= 0 && fromcol < vcol)
+	    fromcol = vcol;
+
+#ifdef FEAT_LINEBREAK
+	// When w_skipcol is non-zero, first line needs 'showbreak'
+	if (wp->w_p_wrap)
+	    need_showbreak = TRUE;
+#endif
+#ifdef FEAT_SPELL
+	// When spell checking a word we need to figure out the start of the
+	// word and if it's badly spelled or not.
+	if (has_spell)
+	{
+	    int		len;
+	    colnr_T	linecol = (colnr_T)(ptr - line);
+	    hlf_T	spell_hlf = HLF_COUNT;
+
+	    pos = wp->w_cursor;
+	    wp->w_cursor.lnum = lnum;
+	    wp->w_cursor.col = linecol;
+	    len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
+
+	    // spell_move_to() may call ml_get() and make "line" invalid
+	    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
+	    ptr = line + linecol;
+
+	    if (len == 0 || (int)wp->w_cursor.col > ptr - line)
+	    {
+		// no bad word found at line start, don't check until end of a
+		// word
+		spell_hlf = HLF_COUNT;
+		word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
+	    }
+	    else
+	    {
+		// bad word found, use attributes until end of word
+		word_end = wp->w_cursor.col + len + 1;
+
+		// Turn index into actual attributes.
+		if (spell_hlf != HLF_COUNT)
+		    spell_attr = highlight_attr[spell_hlf];
+	    }
+	    wp->w_cursor = pos;
+
+# ifdef FEAT_SYN_HL
+	    // Need to restart syntax highlighting for this line.
+	    if (has_syntax)
+		syntax_start(wp, lnum);
+# endif
+	}
+#endif
+    }
+
+    // Correct highlighting for cursor that can't be disabled.
+    // Avoids having to check this for each character.
+    if (fromcol >= 0)
+    {
+	if (noinvcur)
+	{
+	    if ((colnr_T)fromcol == wp->w_virtcol)
+	    {
+		// highlighting starts at cursor, let it start just after the
+		// cursor
+		fromcol_prev = fromcol;
+		fromcol = -1;
+	    }
+	    else if ((colnr_T)fromcol < wp->w_virtcol)
+		// restart highlighting after the cursor
+		fromcol_prev = wp->w_virtcol;
+	}
+	if (fromcol >= tocol)
+	    fromcol = -1;
+    }
+
+#ifdef FEAT_SEARCH_EXTRA
+    if (!number_only)
+    {
+	v = (long)(ptr - line);
+	area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
+					      &line, &screen_search_hl,
+					      &search_attr);
+	ptr = line + v; // "line" may have been updated
+    }
+#endif
+
+#ifdef FEAT_SYN_HL
+    // Cursor line highlighting for 'cursorline' in the current window.
+    if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
+    {
+	// Do not show the cursor line in the text when Visual mode is active,
+	// because it's not clear what is selected then.  Do update
+	// w_last_cursorline.
+	if (!(wp == curwin && VIsual_active)
+					 && wp->w_p_culopt_flags != CULOPT_NBR)
+	{
+	    cul_screenline = (wp->w_p_wrap
+				   && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
+
+	    // Only set line_attr here when "screenline" is not present in
+	    // 'cursorlineopt'.  Otherwise it's done later.
+	    if (!cul_screenline)
+	    {
+		cul_attr = HL_ATTR(HLF_CUL);
+		line_attr = cul_attr;
+		wp->w_last_cursorline = wp->w_cursor.lnum;
+	    }
+	    else
+	    {
+		line_attr_save = line_attr;
+		wp->w_last_cursorline = 0;
+		margin_columns_win(wp, &left_curline_col, &right_curline_col);
+	    }
+	    area_highlighting = TRUE;
+	}
+	else
+	    wp->w_last_cursorline = wp->w_cursor.lnum;
+    }
+#endif
+
+#ifdef FEAT_TEXT_PROP
+    {
+	char_u *prop_start;
+
+	text_prop_count = get_text_props(wp->w_buffer, lnum,
+							   &prop_start, FALSE);
+	if (text_prop_count > 0)
+	{
+	    // Make a copy of the properties, so that they are properly
+	    // aligned.
+	    text_props = ALLOC_MULT(textprop_T, text_prop_count);
+	    if (text_props != NULL)
+		mch_memmove(text_props, prop_start,
+					 text_prop_count * sizeof(textprop_T));
+
+	    // Allocate an array for the indexes.
+	    text_prop_idxs = ALLOC_MULT(int, text_prop_count);
+	    area_highlighting = TRUE;
+	    extra_check = TRUE;
+	}
+    }
+#endif
+
+    off = (unsigned)(current_ScreenLine - ScreenLines);
+    col = 0;
+
+#ifdef FEAT_RIGHTLEFT
+    if (wp->w_p_rl)
+    {
+	// Rightleft window: process the text in the normal direction, but put
+	// it in current_ScreenLine[] from right to left.  Start at the
+	// rightmost column of the window.
+	col = wp->w_width - 1;
+	off += col;
+	screen_line_flags |= SLF_RIGHTLEFT;
+    }
+#endif
+
+    // Repeat for the whole displayed line.
+    for (;;)
+    {
+#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
+	int has_match_conc  = 0;	// match wants to conceal
+#endif
+#ifdef FEAT_CONCEAL
+	int did_decrement_ptr = FALSE;
+#endif
+	// Skip this quickly when working on the text.
+	if (draw_state != WL_LINE)
+	{
+#ifdef FEAT_CMDWIN
+	    if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
+	    {
+		draw_state = WL_CMDLINE;
+		if (cmdwin_type != 0 && wp == curwin)
+		{
+		    // Draw the cmdline character.
+		    n_extra = 1;
+		    c_extra = cmdwin_type;
+		    c_final = NUL;
+		    char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
+		}
+	    }
+#endif
+
+#ifdef FEAT_FOLDING
+	    if (draw_state == WL_FOLD - 1 && n_extra == 0)
+	    {
+		int fdc = compute_foldcolumn(wp, 0);
+
+		draw_state = WL_FOLD;
+		if (fdc > 0)
+		{
+		    // Draw the 'foldcolumn'.  Allocate a buffer, "extra" may
+		    // already be in use.
+		    vim_free(p_extra_free);
+		    p_extra_free = alloc(12 + 1);
+
+		    if (p_extra_free != NULL)
+		    {
+			fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
+			n_extra = fdc;
+			p_extra_free[n_extra] = NUL;
+			p_extra = p_extra_free;
+			c_extra = NUL;
+			c_final = NUL;
+			char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
+		    }
+		}
+	    }
+#endif
+
+#ifdef FEAT_SIGNS
+	    if (draw_state == WL_SIGN - 1 && n_extra == 0)
+	    {
+		draw_state = WL_SIGN;
+		// Show the sign column when there are any signs in this
+		// buffer or when using Netbeans.
+		if (signcolumn_on(wp))
+		    get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
+			    row, startrow, filler_lines, filler_todo, &c_extra,
+			    &c_final, extra, &p_extra, &n_extra, &char_attr);
+	    }
+#endif
+
+	    if (draw_state == WL_NR - 1 && n_extra == 0)
+	    {
+		draw_state = WL_NR;
+		// Display the absolute or relative line number. After the
+		// first fill with blanks when the 'n' flag isn't in 'cpo'
+		if ((wp->w_p_nu || wp->w_p_rnu)
+			&& (row == startrow
+#ifdef FEAT_DIFF
+			    + filler_lines
+#endif
+			    || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
+		{
+#ifdef FEAT_SIGNS
+		    // If 'signcolumn' is set to 'number' and a sign is present
+		    // in 'lnum', then display the sign instead of the line
+		    // number.
+		    if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
+			    && sign_present)
+			get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
+				row, startrow, filler_lines, filler_todo,
+				&c_extra, &c_final, extra, &p_extra, &n_extra,
+				&char_attr);
+		    else
+#endif
+		    {
+		      // Draw the line number (empty space after wrapping).
+		      if (row == startrow
+#ifdef FEAT_DIFF
+			    + filler_lines
+#endif
+			    )
+		      {
+			long num;
+			char *fmt = "%*ld ";
+
+			if (wp->w_p_nu && !wp->w_p_rnu)
+			    // 'number' + 'norelativenumber'
+			    num = (long)lnum;
+			else
+			{
+			    // 'relativenumber', don't use negative numbers
+			    num = labs((long)get_cursor_rel_lnum(wp, lnum));
+			    if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
+			    {
+				// 'number' + 'relativenumber'
+				num = lnum;
+				fmt = "%-*ld ";
+			    }
+			}
+
+			sprintf((char *)extra, fmt,
+						number_width(wp), num);
+			if (wp->w_skipcol > 0)
+			    for (p_extra = extra; *p_extra == ' '; ++p_extra)
+				*p_extra = '-';
+#ifdef FEAT_RIGHTLEFT
+			if (wp->w_p_rl)		    // reverse line numbers
+			{
+			    char_u	*p1, *p2;
+			    int		t;
+
+			    // like rl_mirror(), but keep the space at the end
+			    p2 = skiptowhite(extra) - 1;
+			    for (p1 = extra; p1 < p2; ++p1, --p2)
+			    {
+				t = *p1;
+				*p1 = *p2;
+				*p2 = t;
+			    }
+			}
+#endif
+			p_extra = extra;
+			c_extra = NUL;
+			c_final = NUL;
+		      }
+		      else
+		      {
+			c_extra = ' ';
+			c_final = NUL;
+		      }
+		      n_extra = number_width(wp) + 1;
+		      char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
+#ifdef FEAT_SYN_HL
+		      // When 'cursorline' is set highlight the line number of
+		      // the current line differently.
+		      // When 'cursorlineopt' has "screenline" only highlight
+		      // the line number itself.
+		      // TODO: Can we use CursorLine instead of CursorLineNr
+		      // when CursorLineNr isn't set?
+		      if ((wp->w_p_cul || wp->w_p_rnu)
+			      && (wp->w_p_culopt_flags & CULOPT_NBR)
+			      && (row == startrow
+				  || wp->w_p_culopt_flags & CULOPT_LINE)
+			      && lnum == wp->w_cursor.lnum)
+			char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
+#endif
+		    }
+		}
+	    }
+
+#ifdef FEAT_LINEBREAK
+	    if (wp->w_p_brisbr && draw_state == WL_BRI - 1
+					     && n_extra == 0 && *p_sbr != NUL)
+		// draw indent after showbreak value
+		draw_state = WL_BRI;
+	    else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
+		// After the showbreak, draw the breakindent
+		draw_state = WL_BRI - 1;
+
+	    // draw 'breakindent': indent wrapped text accordingly
+	    if (draw_state == WL_BRI - 1 && n_extra == 0)
+	    {
+		draw_state = WL_BRI;
+		// if need_showbreak is set, breakindent also applies
+		if (wp->w_p_bri && n_extra == 0
+					 && (row != startrow || need_showbreak)
+# ifdef FEAT_DIFF
+			&& filler_lines == 0
+# endif
+		   )
+		{
+		    char_attr = 0;
+# ifdef FEAT_DIFF
+		    if (diff_hlf != (hlf_T)0)
+		    {
+			char_attr = HL_ATTR(diff_hlf);
+#  ifdef FEAT_SYN_HL
+			if (cul_attr != 0)
+			    char_attr = hl_combine_attr(char_attr, cul_attr);
+#  endif
+		    }
+# endif
+		    p_extra = NULL;
+		    c_extra = ' ';
+		    n_extra = get_breakindent_win(wp,
+				       ml_get_buf(wp->w_buffer, lnum, FALSE));
+		    // Correct end of highlighted area for 'breakindent',
+		    // required when 'linebreak' is also set.
+		    if (tocol == vcol)
+			tocol += n_extra;
+		}
+	    }
+#endif
+
+#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
+	    if (draw_state == WL_SBR - 1 && n_extra == 0)
+	    {
+		draw_state = WL_SBR;
+# ifdef FEAT_DIFF
+		if (filler_todo > 0)
+		{
+		    // Draw "deleted" diff line(s).
+		    if (char2cells(fill_diff) > 1)
+		    {
+			c_extra = '-';
+			c_final = NUL;
+		    }
+		    else
+		    {
+			c_extra = fill_diff;
+			c_final = NUL;
+		    }
+#  ifdef FEAT_RIGHTLEFT
+		    if (wp->w_p_rl)
+			n_extra = col + 1;
+		    else
+#  endif
+			n_extra = wp->w_width - col;
+		    char_attr = HL_ATTR(HLF_DED);
+		}
+# endif
+# ifdef FEAT_LINEBREAK
+		if (*p_sbr != NUL && need_showbreak)
+		{
+		    // Draw 'showbreak' at the start of each broken line.
+		    p_extra = p_sbr;
+		    c_extra = NUL;
+		    c_final = NUL;
+		    n_extra = (int)STRLEN(p_sbr);
+		    char_attr = HL_ATTR(HLF_AT);
+		    need_showbreak = FALSE;
+		    vcol_sbr = vcol + MB_CHARLEN(p_sbr);
+		    // Correct end of highlighted area for 'showbreak',
+		    // required when 'linebreak' is also set.
+		    if (tocol == vcol)
+			tocol += n_extra;
+		    // combine 'showbreak' with 'wincolor'
+		    if (win_attr != 0)
+			char_attr = hl_combine_attr(win_attr, char_attr);
+#  ifdef FEAT_SYN_HL
+		    // combine 'showbreak' with 'cursorline'
+		    if (cul_attr != 0)
+			char_attr = hl_combine_attr(char_attr, cul_attr);
+#  endif
+		}
+# endif
+	    }
+#endif
+
+	    if (draw_state == WL_LINE - 1 && n_extra == 0)
+	    {
+		draw_state = WL_LINE;
+		if (saved_n_extra)
+		{
+		    // Continue item from end of wrapped line.
+		    n_extra = saved_n_extra;
+		    c_extra = saved_c_extra;
+		    c_final = saved_c_final;
+		    p_extra = saved_p_extra;
+		    char_attr = saved_char_attr;
+		}
+		else
+		    char_attr = win_attr;
+	    }
+	}
+#ifdef FEAT_SYN_HL
+	if (cul_screenline)
+	{
+	    if (draw_state == WL_LINE
+		    && vcol >= left_curline_col
+		    && vcol < right_curline_col)
+	    {
+		cul_attr = HL_ATTR(HLF_CUL);
+		line_attr = cul_attr;
+	    }
+	    else
+	    {
+		cul_attr = 0;
+		line_attr = line_attr_save;
+	    }
+	}
+#endif
+
+	// When still displaying '$' of change command, stop at cursor.
+	// When only displaying the (relative) line number and that's done,
+	// stop here.
+	if ((dollar_vcol >= 0 && wp == curwin
+		   && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
+#ifdef FEAT_DIFF
+				   && filler_todo <= 0
+#endif
+		)
+		|| (number_only && draw_state > WL_NR))
+	{
+	    screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
+							    screen_line_flags);
+	    // Pretend we have finished updating the window.  Except when
+	    // 'cursorcolumn' is set.
+#ifdef FEAT_SYN_HL
+	    if (wp->w_p_cuc)
+		row = wp->w_cline_row + wp->w_cline_height;
+	    else
+#endif
+		row = wp->w_height;
+	    break;
+	}
+
+	if (draw_state == WL_LINE && (area_highlighting
+#ifdef FEAT_SPELL
+		|| has_spell
+#endif
+	   ))
+	{
+	    // handle Visual or match highlighting in this line
+	    if (vcol == fromcol
+		    || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
+			&& (*mb_ptr2cells)(ptr) > 1)
+		    || ((int)vcol_prev == fromcol_prev
+			&& vcol_prev < vcol	// not at margin
+			&& vcol < tocol))
+		area_attr = vi_attr;		// start highlighting
+	    else if (area_attr != 0
+		    && (vcol == tocol
+			|| (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
+		area_attr = 0;			// stop highlighting
+
+#ifdef FEAT_SEARCH_EXTRA
+	    if (!n_extra)
+	    {
+		// Check for start/end of 'hlsearch' and other matches.
+		// After end, check for start/end of next match.
+		// When another match, have to check for start again.
+		v = (long)(ptr - line);
+		search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
+				      &screen_search_hl, &has_match_conc,
+				      &match_conc, did_line_attr, lcs_eol_one);
+		ptr = line + v;  // "line" may have been changed
+	    }
+#endif
+
+#ifdef FEAT_DIFF
+	    if (diff_hlf != (hlf_T)0)
+	    {
+		if (diff_hlf == HLF_CHD && ptr - line >= change_start
+							      && n_extra == 0)
+		    diff_hlf = HLF_TXD;		// changed text
+		if (diff_hlf == HLF_TXD && ptr - line > change_end
+							      && n_extra == 0)
+		    diff_hlf = HLF_CHD;		// changed line
+		line_attr = HL_ATTR(diff_hlf);
+		if (wp->w_p_cul && lnum == wp->w_cursor.lnum
+			&& wp->w_p_culopt_flags != CULOPT_NBR
+			&& (!cul_screenline || (vcol >= left_curline_col
+						&& vcol <= right_curline_col)))
+		    line_attr = hl_combine_attr(
+					  line_attr, HL_ATTR(HLF_CUL));
+	    }
+#endif
+
+#ifdef FEAT_TEXT_PROP
+	    if (text_props != NULL)
+	    {
+		int pi;
+		int bcol = (int)(ptr - line);
+
+		if (n_extra > 0)
+		    --bcol;  // still working on the previous char, e.g. Tab
+
+		// Check if any active property ends.
+		for (pi = 0; pi < text_props_active; ++pi)
+		{
+		    int tpi = text_prop_idxs[pi];
+
+		    if (bcol >= text_props[tpi].tp_col - 1
+						  + text_props[tpi].tp_len)
+		    {
+			if (pi + 1 < text_props_active)
+			    mch_memmove(text_prop_idxs + pi,
+					text_prop_idxs + pi + 1,
+					sizeof(int)
+					 * (text_props_active - (pi + 1)));
+			--text_props_active;
+			--pi;
+		    }
+		}
+
+		// Add any text property that starts in this column.
+		while (text_prop_next < text_prop_count
+			   && bcol >= text_props[text_prop_next].tp_col - 1)
+		    text_prop_idxs[text_props_active++] = text_prop_next++;
+
+		text_prop_attr = 0;
+		text_prop_combine = FALSE;
+		if (text_props_active > 0)
+		{
+		    // Sort the properties on priority and/or starting last.
+		    // Then combine the attributes, highest priority last.
+		    current_text_props = text_props;
+		    current_buf = wp->w_buffer;
+		    qsort((void *)text_prop_idxs, (size_t)text_props_active,
+					       sizeof(int), text_prop_compare);
+
+		    for (pi = 0; pi < text_props_active; ++pi)
+		    {
+			int	    tpi = text_prop_idxs[pi];
+			proptype_T  *pt = text_prop_type_by_id(
+					wp->w_buffer, text_props[tpi].tp_type);
+
+			if (pt != NULL && pt->pt_hl_id > 0)
+			{
+			    int pt_attr = syn_id2attr(pt->pt_hl_id);
+
+			    text_prop_type = pt;
+			    text_prop_attr =
+				      hl_combine_attr(text_prop_attr, pt_attr);
+			    text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
+			}
+		    }
+		}
+	    }
+#endif
+
+	    // Decide which of the highlight attributes to use.
+	    attr_pri = TRUE;
+#ifdef LINE_ATTR
+	    if (area_attr != 0)
+		char_attr = hl_combine_attr(line_attr, area_attr);
+	    else if (search_attr != 0)
+		char_attr = hl_combine_attr(line_attr, search_attr);
+# ifdef FEAT_TEXT_PROP
+	    else if (text_prop_type != NULL)
+	    {
+		char_attr = hl_combine_attr(
+			line_attr != 0 ? line_attr : win_attr, text_prop_attr);
+	    }
+# endif
+	    else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
+				|| vcol < fromcol || vcol_prev < fromcol_prev
+				|| vcol >= tocol))
+	    {
+		// Use line_attr when not in the Visual or 'incsearch' area
+		// (area_attr may be 0 when "noinvcur" is set).
+		char_attr = line_attr;
+		attr_pri = FALSE;
+	    }
+#else
+	    if (area_attr != 0)
+		char_attr = area_attr;
+	    else if (search_attr != 0)
+		char_attr = search_attr;
+#endif
+	    else
+	    {
+		attr_pri = FALSE;
+#ifdef FEAT_TEXT_PROP
+		if (text_prop_type != NULL)
+		{
+		    if (text_prop_combine)
+			char_attr = hl_combine_attr(
+						  syntax_attr, text_prop_attr);
+		    else
+			char_attr = hl_combine_attr(
+						  win_attr, text_prop_attr);
+		}
+		else
+#endif
+#ifdef FEAT_SYN_HL
+		if (has_syntax)
+		    char_attr = syntax_attr;
+		else
+#endif
+		    char_attr = 0;
+	    }
+	}
+	if (char_attr == 0)
+	    char_attr = win_attr;
+
+	// Get the next character to put on the screen.
+
+	// The "p_extra" points to the extra stuff that is inserted to
+	// represent special characters (non-printable stuff) and other
+	// things.  When all characters are the same, c_extra is used.
+	// If c_final is set, it will compulsorily be used at the end.
+	// "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
+	// "p_extra[n_extra]".
+	// For the '$' of the 'list' option, n_extra == 1, p_extra == "".
+	if (n_extra > 0)
+	{
+	    if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
+	    {
+		c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
+		mb_c = c;	// doesn't handle non-utf-8 multi-byte!
+		if (enc_utf8 && utf_char2len(c) > 1)
+		{
+		    mb_utf8 = TRUE;
+		    u8cc[0] = 0;
+		    c = 0xc0;
+		}
+		else
+		    mb_utf8 = FALSE;
+	    }
+	    else
+	    {
+		c = *p_extra;
+		if (has_mbyte)
+		{
+		    mb_c = c;
+		    if (enc_utf8)
+		    {
+			// If the UTF-8 character is more than one byte:
+			// Decode it into "mb_c".
+			mb_l = utfc_ptr2len(p_extra);
+			mb_utf8 = FALSE;
+			if (mb_l > n_extra)
+			    mb_l = 1;
+			else if (mb_l > 1)
+			{
+			    mb_c = utfc_ptr2char(p_extra, u8cc);
+			    mb_utf8 = TRUE;
+			    c = 0xc0;
+			}
+		    }
+		    else
+		    {
+			// if this is a DBCS character, put it in "mb_c"
+			mb_l = MB_BYTE2LEN(c);
+			if (mb_l >= n_extra)
+			    mb_l = 1;
+			else if (mb_l > 1)
+			    mb_c = (c << 8) + p_extra[1];
+		    }
+		    if (mb_l == 0)  // at the NUL at end-of-line
+			mb_l = 1;
+
+		    // If a double-width char doesn't fit display a '>' in the
+		    // last column.
+		    if ((
+# ifdef FEAT_RIGHTLEFT
+			    wp->w_p_rl ? (col <= 0) :
+# endif
+				    (col >= wp->w_width - 1))
+			    && (*mb_char2cells)(mb_c) == 2)
+		    {
+			c = '>';
+			mb_c = c;
+			mb_l = 1;
+			mb_utf8 = FALSE;
+			multi_attr = HL_ATTR(HLF_AT);
+#ifdef FEAT_SYN_HL
+			if (cul_attr)
+			    multi_attr = hl_combine_attr(multi_attr, cul_attr);
+#endif
+			// put the pointer back to output the double-width
+			// character at the start of the next line.
+			++n_extra;
+			--p_extra;
+		    }
+		    else
+		    {
+			n_extra -= mb_l - 1;
+			p_extra += mb_l - 1;
+		    }
+		}
+		++p_extra;
+	    }
+	    --n_extra;
+	}
+	else
+	{
+#ifdef FEAT_LINEBREAK
+	    int c0;
+#endif
+
+	    if (p_extra_free != NULL)
+		VIM_CLEAR(p_extra_free);
+	    // Get a character from the line itself.
+	    c = *ptr;
+#ifdef FEAT_LINEBREAK
+	    c0 = *ptr;
+#endif
+	    if (has_mbyte)
+	    {
+		mb_c = c;
+		if (enc_utf8)
+		{
+		    // If the UTF-8 character is more than one byte: Decode it
+		    // into "mb_c".
+		    mb_l = utfc_ptr2len(ptr);
+		    mb_utf8 = FALSE;
+		    if (mb_l > 1)
+		    {
+			mb_c = utfc_ptr2char(ptr, u8cc);
+			// Overlong encoded ASCII or ASCII with composing char
+			// is displayed normally, except a NUL.
+			if (mb_c < 0x80)
+			{
+			    c = mb_c;
+#ifdef FEAT_LINEBREAK
+			    c0 = mb_c;
+#endif
+			}
+			mb_utf8 = TRUE;
+
+			// At start of the line we can have a composing char.
+			// Draw it as a space with a composing char.
+			if (utf_iscomposing(mb_c))
+			{
+			    int i;
+
+			    for (i = Screen_mco - 1; i > 0; --i)
+				u8cc[i] = u8cc[i - 1];
+			    u8cc[0] = mb_c;
+			    mb_c = ' ';
+			}
+		    }
+
+		    if ((mb_l == 1 && c >= 0x80)
+			    || (mb_l >= 1 && mb_c == 0)
+			    || (mb_l > 1 && (!vim_isprintc(mb_c))))
+		    {
+			// Illegal UTF-8 byte: display as <xx>.
+			// Non-BMP character : display as ? or fullwidth ?.
+			transchar_hex(extra, mb_c);
+# ifdef FEAT_RIGHTLEFT
+			if (wp->w_p_rl)		// reverse
+			    rl_mirror(extra);
+# endif
+			p_extra = extra;
+			c = *p_extra;
+			mb_c = mb_ptr2char_adv(&p_extra);
+			mb_utf8 = (c >= 0x80);
+			n_extra = (int)STRLEN(p_extra);
+			c_extra = NUL;
+			c_final = NUL;
+			if (area_attr == 0 && search_attr == 0)
+			{
+			    n_attr = n_extra + 1;
+			    extra_attr = HL_ATTR(HLF_8);
+			    saved_attr2 = char_attr; // save current attr
+			}
+		    }
+		    else if (mb_l == 0)  // at the NUL at end-of-line
+			mb_l = 1;
+#ifdef FEAT_ARABIC
+		    else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
+		    {
+			// Do Arabic shaping.
+			int	pc, pc1, nc;
+			int	pcc[MAX_MCO];
+
+			// The idea of what is the previous and next
+			// character depends on 'rightleft'.
+			if (wp->w_p_rl)
+			{
+			    pc = prev_c;
+			    pc1 = prev_c1;
+			    nc = utf_ptr2char(ptr + mb_l);
+			    prev_c1 = u8cc[0];
+			}
+			else
+			{
+			    pc = utfc_ptr2char(ptr + mb_l, pcc);
+			    nc = prev_c;
+			    pc1 = pcc[0];
+			}
+			prev_c = mb_c;
+
+			mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
+		    }
+		    else
+			prev_c = mb_c;
+#endif
+		}
+		else	// enc_dbcs
+		{
+		    mb_l = MB_BYTE2LEN(c);
+		    if (mb_l == 0)  // at the NUL at end-of-line
+			mb_l = 1;
+		    else if (mb_l > 1)
+		    {
+			// We assume a second byte below 32 is illegal.
+			// Hopefully this is OK for all double-byte encodings!
+			if (ptr[1] >= 32)
+			    mb_c = (c << 8) + ptr[1];
+			else
+			{
+			    if (ptr[1] == NUL)
+			    {
+				// head byte at end of line
+				mb_l = 1;
+				transchar_nonprint(extra, c);
+			    }
+			    else
+			    {
+				// illegal tail byte
+				mb_l = 2;
+				STRCPY(extra, "XX");
+			    }
+			    p_extra = extra;
+			    n_extra = (int)STRLEN(extra) - 1;
+			    c_extra = NUL;
+			    c_final = NUL;
+			    c = *p_extra++;
+			    if (area_attr == 0 && search_attr == 0)
+			    {
+				n_attr = n_extra + 1;
+				extra_attr = HL_ATTR(HLF_8);
+				saved_attr2 = char_attr; // save current attr
+			    }
+			    mb_c = c;
+			}
+		    }
+		}
+		// If a double-width char doesn't fit display a '>' in the
+		// last column; the character is displayed at the start of the
+		// next line.
+		if ((
+# ifdef FEAT_RIGHTLEFT
+			    wp->w_p_rl ? (col <= 0) :
+# endif
+				(col >= wp->w_width - 1))
+			&& (*mb_char2cells)(mb_c) == 2)
+		{
+		    c = '>';
+		    mb_c = c;
+		    mb_utf8 = FALSE;
+		    mb_l = 1;
+		    multi_attr = HL_ATTR(HLF_AT);
+		    // Put pointer back so that the character will be
+		    // displayed at the start of the next line.
+		    --ptr;
+#ifdef FEAT_CONCEAL
+		    did_decrement_ptr = TRUE;
+#endif
+		}
+		else if (*ptr != NUL)
+		    ptr += mb_l - 1;
+
+		// If a double-width char doesn't fit at the left side display
+		// a '<' in the first column.  Don't do this for unprintable
+		// characters.
+		if (n_skip > 0 && mb_l > 1 && n_extra == 0)
+		{
+		    n_extra = 1;
+		    c_extra = MB_FILLER_CHAR;
+		    c_final = NUL;
+		    c = ' ';
+		    if (area_attr == 0 && search_attr == 0)
+		    {
+			n_attr = n_extra + 1;
+			extra_attr = HL_ATTR(HLF_AT);
+			saved_attr2 = char_attr; // save current attr
+		    }
+		    mb_c = c;
+		    mb_utf8 = FALSE;
+		    mb_l = 1;
+		}
+
+	    }
+	    ++ptr;
+
+	    if (extra_check)
+	    {
+#ifdef FEAT_SPELL
+		int	can_spell = TRUE;
+#endif
+
+#ifdef FEAT_TERMINAL
+		if (get_term_attr)
+		{
+		    syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
+
+		    if (!attr_pri)
+			char_attr = syntax_attr;
+		    else
+			char_attr = hl_combine_attr(syntax_attr, char_attr);
+		}
+#endif
+
+#ifdef FEAT_SYN_HL
+		// Get syntax attribute, unless still at the start of the line
+		// (double-wide char that doesn't fit).
+		v = (long)(ptr - line);
+		if (has_syntax && v > 0)
+		{
+		    // Get the syntax attribute for the character.  If there
+		    // is an error, disable syntax highlighting.
+		    save_did_emsg = did_emsg;
+		    did_emsg = FALSE;
+
+		    syntax_attr = get_syntax_attr((colnr_T)v - 1,
+# ifdef FEAT_SPELL
+						has_spell ? &can_spell :
+# endif
+						NULL, FALSE);
+
+		    if (did_emsg)
+		    {
+			wp->w_s->b_syn_error = TRUE;
+			has_syntax = FALSE;
+			syntax_attr = 0;
+		    }
+		    else
+			did_emsg = save_did_emsg;
+
+		    // combine syntax attribute with 'wincolor'
+		    if (win_attr != 0)
+			syntax_attr = hl_combine_attr(win_attr, syntax_attr);
+
+# ifdef SYN_TIME_LIMIT
+		    if (wp->w_s->b_syn_slow)
+			has_syntax = FALSE;
+# endif
+
+		    // Need to get the line again, a multi-line regexp may
+		    // have made it invalid.
+		    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
+		    ptr = line + v;
+
+# ifdef FEAT_TEXT_PROP
+		    // Text properties overrule syntax highlighting or combine.
+		    if (text_prop_attr == 0 || text_prop_combine)
+# endif
+		    {
+			int comb_attr = syntax_attr;
+# ifdef FEAT_TEXT_PROP
+			comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
+# endif
+			if (!attr_pri)
+			{
+#ifdef FEAT_SYN_HL
+			    if (cul_attr)
+				char_attr = hl_combine_attr(
+							  comb_attr, cul_attr);
+			    else
+#endif
+				if (line_attr)
+				char_attr = hl_combine_attr(
+							 comb_attr, line_attr);
+			    else
+				char_attr = comb_attr;
+			}
+			else
+			    char_attr = hl_combine_attr(comb_attr, char_attr);
+		    }
+# ifdef FEAT_CONCEAL
+		    // no concealing past the end of the line, it interferes
+		    // with line highlighting
+		    if (c == NUL)
+			syntax_flags = 0;
+		    else
+			syntax_flags = get_syntax_info(&syntax_seqnr);
+# endif
+		}
+#endif
+
+#ifdef FEAT_SPELL
+		// Check spelling (unless at the end of the line).
+		// Only do this when there is no syntax highlighting, the
+		// @Spell cluster is not used or the current syntax item
+		// contains the @Spell cluster.
+		if (has_spell && v >= word_end && v > cur_checked_col)
+		{
+		    spell_attr = 0;
+		    if (c != 0 && (
+# ifdef FEAT_SYN_HL
+				!has_syntax ||
+# endif
+				can_spell))
+		    {
+			char_u	*prev_ptr, *p;
+			int	len;
+			hlf_T	spell_hlf = HLF_COUNT;
+			if (has_mbyte)
+			{
+			    prev_ptr = ptr - mb_l;
+			    v -= mb_l - 1;
+			}
+			else
+			    prev_ptr = ptr - 1;
+
+			// Use nextline[] if possible, it has the start of the
+			// next line concatenated.
+			if ((prev_ptr - line) - nextlinecol >= 0)
+			    p = nextline + (prev_ptr - line) - nextlinecol;
+			else
+			    p = prev_ptr;
+			cap_col -= (int)(prev_ptr - line);
+			len = spell_check(wp, p, &spell_hlf, &cap_col,
+								    nochange);
+			word_end = v + len;
+
+			// In Insert mode only highlight a word that
+			// doesn't touch the cursor.
+			if (spell_hlf != HLF_COUNT
+				&& (State & INSERT) != 0
+				&& wp->w_cursor.lnum == lnum
+				&& wp->w_cursor.col >=
+						    (colnr_T)(prev_ptr - line)
+				&& wp->w_cursor.col < (colnr_T)word_end)
+			{
+			    spell_hlf = HLF_COUNT;
+			    spell_redraw_lnum = lnum;
+			}
+
+			if (spell_hlf == HLF_COUNT && p != prev_ptr
+				       && (p - nextline) + len > nextline_idx)
+			{
+			    // Remember that the good word continues at the
+			    // start of the next line.
+			    checked_lnum = lnum + 1;
+			    checked_col = (int)((p - nextline) + len - nextline_idx);
+			}
+
+			// Turn index into actual attributes.
+			if (spell_hlf != HLF_COUNT)
+			    spell_attr = highlight_attr[spell_hlf];
+
+			if (cap_col > 0)
+			{
+			    if (p != prev_ptr
+				   && (p - nextline) + cap_col >= nextline_idx)
+			    {
+				// Remember that the word in the next line
+				// must start with a capital.
+				capcol_lnum = lnum + 1;
+				cap_col = (int)((p - nextline) + cap_col
+							       - nextline_idx);
+			    }
+			    else
+				// Compute the actual column.
+				cap_col += (int)(prev_ptr - line);
+			}
+		    }
+		}
+		if (spell_attr != 0)
+		{
+		    if (!attr_pri)
+			char_attr = hl_combine_attr(char_attr, spell_attr);
+		    else
+			char_attr = hl_combine_attr(spell_attr, char_attr);
+		}
+#endif
+#ifdef FEAT_LINEBREAK
+		// Found last space before word: check for line break.
+		if (wp->w_p_lbr && c0 == c
+				  && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
+		{
+		    int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
+		    char_u *p = ptr - (mb_off + 1);
+
+		    // TODO: is passing p for start of the line OK?
+		    n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
+								    NULL) - 1;
+		    if (c == TAB && n_extra + col > wp->w_width)
+# ifdef FEAT_VARTABS
+			n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
+					      wp->w_buffer->b_p_vts_array) - 1;
+# else
+			n_extra = (int)wp->w_buffer->b_p_ts
+				       - vcol % (int)wp->w_buffer->b_p_ts - 1;
+# endif
+
+		    c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
+		    c_final = NUL;
+		    if (VIM_ISWHITE(c))
+		    {
+#ifdef FEAT_CONCEAL
+			if (c == TAB)
+			    // See "Tab alignment" below.
+			    FIX_FOR_BOGUSCOLS;
+#endif
+			if (!wp->w_p_list)
+			    c = ' ';
+		    }
+		}
+#endif
+
+		// 'list': Change char 160 to lcs_nbsp and space to lcs_space.
+		// But not when the character is followed by a composing
+		// character (use mb_l to check that).
+		if (wp->w_p_list
+			&& ((((c == 160 && mb_l == 1)
+			      || (mb_utf8
+				  && ((mb_c == 160 && mb_l == 2)
+				      || (mb_c == 0x202f && mb_l == 3))))
+			     && lcs_nbsp)
+			    || (c == ' '
+				&& mb_l == 1
+				&& lcs_space
+				&& ptr - line <= trailcol)))
+		{
+		    c = (c == ' ') ? lcs_space : lcs_nbsp;
+		    if (area_attr == 0 && search_attr == 0)
+		    {
+			n_attr = 1;
+			extra_attr = HL_ATTR(HLF_8);
+			saved_attr2 = char_attr; // save current attr
+		    }
+		    mb_c = c;
+		    if (enc_utf8 && utf_char2len(c) > 1)
+		    {
+			mb_utf8 = TRUE;
+			u8cc[0] = 0;
+			c = 0xc0;
+		    }
+		    else
+			mb_utf8 = FALSE;
+		}
+
+		if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
+		{
+		    c = lcs_trail;
+		    if (!attr_pri)
+		    {
+			n_attr = 1;
+			extra_attr = HL_ATTR(HLF_8);
+			saved_attr2 = char_attr; // save current attr
+		    }
+		    mb_c = c;
+		    if (enc_utf8 && utf_char2len(c) > 1)
+		    {
+			mb_utf8 = TRUE;
+			u8cc[0] = 0;
+			c = 0xc0;
+		    }
+		    else
+			mb_utf8 = FALSE;
+		}
+	    }
+
+	    // Handling of non-printable characters.
+	    if (!vim_isprintc(c))
+	    {
+		// when getting a character from the file, we may have to
+		// turn it into something else on the way to putting it
+		// into "ScreenLines".
+		if (c == TAB && (!wp->w_p_list || lcs_tab1))
+		{
+		    int tab_len = 0;
+		    long vcol_adjusted = vcol; // removed showbreak length
+#ifdef FEAT_LINEBREAK
+		    // only adjust the tab_len, when at the first column
+		    // after the showbreak value was drawn
+		    if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
+			vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
+#endif
+		    // tab amount depends on current column
+#ifdef FEAT_VARTABS
+		    tab_len = tabstop_padding(vcol_adjusted,
+					      wp->w_buffer->b_p_ts,
+					      wp->w_buffer->b_p_vts_array) - 1;
+#else
+		    tab_len = (int)wp->w_buffer->b_p_ts
+			       - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
+#endif
+
+#ifdef FEAT_LINEBREAK
+		    if (!wp->w_p_lbr || !wp->w_p_list)
+#endif
+			// tab amount depends on current column
+			n_extra = tab_len;
+#ifdef FEAT_LINEBREAK
+		    else
+		    {
+			char_u	*p;
+			int	len;
+			int	i;
+			int	saved_nextra = n_extra;
+
+#ifdef FEAT_CONCEAL
+			if (vcol_off > 0)
+			    // there are characters to conceal
+			    tab_len += vcol_off;
+			// boguscols before FIX_FOR_BOGUSCOLS macro from above
+			if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
+							 && n_extra > tab_len)
+			    tab_len += n_extra - tab_len;
+#endif
+
+			// if n_extra > 0, it gives the number of chars, to
+			// use for a tab, else we need to calculate the width
+			// for a tab
+			len = (tab_len * mb_char2len(lcs_tab2));
+			if (n_extra > 0)
+			    len += n_extra - tab_len;
+			c = lcs_tab1;
+			p = alloc(len + 1);
+			vim_memset(p, ' ', len);
+			p[len] = NUL;
+			vim_free(p_extra_free);
+			p_extra_free = p;
+			for (i = 0; i < tab_len; i++)
+			{
+			    int lcs = lcs_tab2;
+
+			    if (*p == NUL)
+			    {
+				tab_len = i;
+				break;
+			    }
+
+			    // if lcs_tab3 is given, need to change the char
+			    // for tab
+			    if (lcs_tab3 && i == tab_len - 1)
+				lcs = lcs_tab3;
+			    mb_char2bytes(lcs, p);
+			    p += mb_char2len(lcs);
+			    n_extra += mb_char2len(lcs)
+						  - (saved_nextra > 0 ? 1 : 0);
+			}
+			p_extra = p_extra_free;
+#ifdef FEAT_CONCEAL
+			// n_extra will be increased by FIX_FOX_BOGUSCOLS
+			// macro below, so need to adjust for that here
+			if (vcol_off > 0)
+			    n_extra -= vcol_off;
+#endif
+		    }
+#endif
+#ifdef FEAT_CONCEAL
+		    {
+			int vc_saved = vcol_off;
+
+			// Tab alignment should be identical regardless of
+			// 'conceallevel' value. So tab compensates of all
+			// previous concealed characters, and thus resets
+			// vcol_off and boguscols accumulated so far in the
+			// line. Note that the tab can be longer than
+			// 'tabstop' when there are concealed characters.
+			FIX_FOR_BOGUSCOLS;
+
+			// Make sure, the highlighting for the tab char will be
+			// correctly set further below (effectively reverts the
+			// FIX_FOR_BOGSUCOLS macro
+			if (n_extra == tab_len + vc_saved && wp->w_p_list
+								  && lcs_tab1)
+			    tab_len += vc_saved;
+		    }
+#endif
+		    mb_utf8 = FALSE;	// don't draw as UTF-8
+		    if (wp->w_p_list)
+		    {
+			c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
+#ifdef FEAT_LINEBREAK
+			if (wp->w_p_lbr)
+			    c_extra = NUL; // using p_extra from above
+			else
+#endif
+			    c_extra = lcs_tab2;
+			c_final = lcs_tab3;
+			n_attr = tab_len + 1;
+			extra_attr = HL_ATTR(HLF_8);
+			saved_attr2 = char_attr; // save current attr
+			mb_c = c;
+			if (enc_utf8 && utf_char2len(c) > 1)
+			{
+			    mb_utf8 = TRUE;
+			    u8cc[0] = 0;
+			    c = 0xc0;
+			}
+		    }
+		    else
+		    {
+			c_final = NUL;
+			c_extra = ' ';
+			c = ' ';
+		    }
+		}
+		else if (c == NUL
+			&& (wp->w_p_list
+			    || ((fromcol >= 0 || fromcol_prev >= 0)
+				&& tocol > vcol
+				&& VIsual_mode != Ctrl_V
+				&& (
+# ifdef FEAT_RIGHTLEFT
+				    wp->w_p_rl ? (col >= 0) :
+# endif
+				    (col < wp->w_width))
+				&& !(noinvcur
+				    && lnum == wp->w_cursor.lnum
+				    && (colnr_T)vcol == wp->w_virtcol)))
+			&& lcs_eol_one > 0)
+		{
+		    // Display a '$' after the line or highlight an extra
+		    // character if the line break is included.
+#if defined(FEAT_DIFF) || defined(LINE_ATTR)
+		    // For a diff line the highlighting continues after the
+		    // "$".
+		    if (
+# ifdef FEAT_DIFF
+			    diff_hlf == (hlf_T)0
+#  ifdef LINE_ATTR
+			    &&
+#  endif
+# endif
+# ifdef LINE_ATTR
+			    line_attr == 0
+# endif
+		       )
+#endif
+		    {
+			// In virtualedit, visual selections may extend
+			// beyond end of line.
+			if (area_highlighting && virtual_active()
+				&& tocol != MAXCOL && vcol < tocol)
+			    n_extra = 0;
+			else
+			{
+			    p_extra = at_end_str;
+			    n_extra = 1;
+			    c_extra = NUL;
+			    c_final = NUL;
+			}
+		    }
+		    if (wp->w_p_list && lcs_eol > 0)
+			c = lcs_eol;
+		    else
+			c = ' ';
+		    lcs_eol_one = -1;
+		    --ptr;	    // put it back at the NUL
+		    if (!attr_pri)
+		    {
+			extra_attr = HL_ATTR(HLF_AT);
+			n_attr = 1;
+		    }
+		    mb_c = c;
+		    if (enc_utf8 && utf_char2len(c) > 1)
+		    {
+			mb_utf8 = TRUE;
+			u8cc[0] = 0;
+			c = 0xc0;
+		    }
+		    else
+			mb_utf8 = FALSE;	// don't draw as UTF-8
+		}
+		else if (c != NUL)
+		{
+		    p_extra = transchar(c);
+		    if (n_extra == 0)
+			n_extra = byte2cells(c) - 1;
+#ifdef FEAT_RIGHTLEFT
+		    if ((dy_flags & DY_UHEX) && wp->w_p_rl)
+			rl_mirror(p_extra);	// reverse "<12>"
+#endif
+		    c_extra = NUL;
+		    c_final = NUL;
+#ifdef FEAT_LINEBREAK
+		    if (wp->w_p_lbr)
+		    {
+			char_u *p;
+
+			c = *p_extra;
+			p = alloc(n_extra + 1);
+			vim_memset(p, ' ', n_extra);
+			STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
+			p[n_extra] = NUL;
+			vim_free(p_extra_free);
+			p_extra_free = p_extra = p;
+		    }
+		    else
+#endif
+		    {
+			n_extra = byte2cells(c) - 1;
+			c = *p_extra++;
+		    }
+		    if (!attr_pri)
+		    {
+			n_attr = n_extra + 1;
+			extra_attr = HL_ATTR(HLF_8);
+			saved_attr2 = char_attr; // save current attr
+		    }
+		    mb_utf8 = FALSE;	// don't draw as UTF-8
+		}
+		else if (VIsual_active
+			 && (VIsual_mode == Ctrl_V
+			     || VIsual_mode == 'v')
+			 && virtual_active()
+			 && tocol != MAXCOL
+			 && vcol < tocol
+			 && (
+#ifdef FEAT_RIGHTLEFT
+			    wp->w_p_rl ? (col >= 0) :
+#endif
+			    (col < wp->w_width)))
+		{
+		    c = ' ';
+		    --ptr;	    // put it back at the NUL
+		}
+#if defined(LINE_ATTR)
+		else if ((
+# ifdef FEAT_DIFF
+			    diff_hlf != (hlf_T)0 ||
+# endif
+# ifdef FEAT_TERMINAL
+			    win_attr != 0 ||
+# endif
+			    line_attr != 0
+			) && (
+# ifdef FEAT_RIGHTLEFT
+			    wp->w_p_rl ? (col >= 0) :
+# endif
+			    (col
+# ifdef FEAT_CONCEAL
+				- boguscols
+# endif
+					    < wp->w_width)))
+		{
+		    // Highlight until the right side of the window
+		    c = ' ';
+		    --ptr;	    // put it back at the NUL
+
+		    // Remember we do the char for line highlighting.
+		    ++did_line_attr;
+
+		    // don't do search HL for the rest of the line
+		    if (line_attr != 0 && char_attr == search_attr
+					&& (did_line_attr > 1
+					    || (wp->w_p_list && lcs_eol > 0)))
+			char_attr = line_attr;
+# ifdef FEAT_DIFF
+		    if (diff_hlf == HLF_TXD)
+		    {
+			diff_hlf = HLF_CHD;
+			if (vi_attr == 0 || char_attr != vi_attr)
+			{
+			    char_attr = HL_ATTR(diff_hlf);
+			    if (wp->w_p_cul && lnum == wp->w_cursor.lnum
+				    && wp->w_p_culopt_flags != CULOPT_NBR
+				    && (!cul_screenline
+					|| (vcol >= left_curline_col
+						 && vcol <= right_curline_col)))
+				char_attr = hl_combine_attr(
+					  char_attr, HL_ATTR(HLF_CUL));
+			}
+		    }
+# endif
+# ifdef FEAT_TERMINAL
+		    if (win_attr != 0)
+		    {
+			char_attr = win_attr;
+			if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
+			{
+			    if (!cul_screenline || (vcol >= left_curline_col
+						  && vcol <= right_curline_col))
+				char_attr = hl_combine_attr(
+					      char_attr, HL_ATTR(HLF_CUL));
+			}
+			else if (line_attr)
+			    char_attr = hl_combine_attr(char_attr, line_attr);
+		    }
+# endif
+		}
+#endif
+	    }
+
+#ifdef FEAT_CONCEAL
+	    if (   wp->w_p_cole > 0
+		&& (wp != curwin || lnum != wp->w_cursor.lnum ||
+						       conceal_cursor_line(wp))
+		&& ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
+		&& !(lnum_in_visual_area
+				    && vim_strchr(wp->w_p_cocu, 'v') == NULL))
+	    {
+		char_attr = conceal_attr;
+		if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
+			&& (syn_get_sub_char() != NUL || match_conc
+							 || wp->w_p_cole == 1)
+			&& wp->w_p_cole != 3)
+		{
+		    // First time at this concealed item: display one
+		    // character.
+		    if (match_conc)
+			c = match_conc;
+		    else if (syn_get_sub_char() != NUL)
+			c = syn_get_sub_char();
+		    else if (lcs_conceal != NUL)
+			c = lcs_conceal;
+		    else
+			c = ' ';
+
+		    prev_syntax_id = syntax_seqnr;
+
+		    if (n_extra > 0)
+			vcol_off += n_extra;
+		    vcol += n_extra;
+		    if (wp->w_p_wrap && n_extra > 0)
+		    {
+# ifdef FEAT_RIGHTLEFT
+			if (wp->w_p_rl)
+			{
+			    col -= n_extra;
+			    boguscols -= n_extra;
+			}
+			else
+# endif
+			{
+			    boguscols += n_extra;
+			    col += n_extra;
+			}
+		    }
+		    n_extra = 0;
+		    n_attr = 0;
+		}
+		else if (n_skip == 0)
+		{
+		    is_concealing = TRUE;
+		    n_skip = 1;
+		}
+		mb_c = c;
+		if (enc_utf8 && utf_char2len(c) > 1)
+		{
+		    mb_utf8 = TRUE;
+		    u8cc[0] = 0;
+		    c = 0xc0;
+		}
+		else
+		    mb_utf8 = FALSE;	// don't draw as UTF-8
+	    }
+	    else
+	    {
+		prev_syntax_id = 0;
+		is_concealing = FALSE;
+	    }
+
+	    if (n_skip > 0 && did_decrement_ptr)
+		// not showing the '>', put pointer back to avoid getting stuck
+		++ptr;
+
+#endif // FEAT_CONCEAL
+	}
+
+#ifdef FEAT_CONCEAL
+	// In the cursor line and we may be concealing characters: correct
+	// the cursor column when we reach its position.
+	if (!did_wcol && draw_state == WL_LINE
+		&& wp == curwin && lnum == wp->w_cursor.lnum
+		&& conceal_cursor_line(wp)
+		&& (int)wp->w_virtcol <= vcol + n_skip)
+	{
+#  ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+		wp->w_wcol = wp->w_width - col + boguscols - 1;
+	    else
+#  endif
+		wp->w_wcol = col - boguscols;
+	    wp->w_wrow = row;
+	    did_wcol = TRUE;
+	    curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
+	}
+#endif
+
+	// Don't override visual selection highlighting.
+	if (n_attr > 0
+		&& draw_state == WL_LINE
+		&& !attr_pri)
+	{
+#ifdef LINE_ATTR
+	    if (line_attr)
+		char_attr = hl_combine_attr(extra_attr, line_attr);
+	    else
+#endif
+		char_attr = extra_attr;
+	}
+
+#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
+	// XIM don't send preedit_start and preedit_end, but they send
+	// preedit_changed and commit.  Thus Vim can't set "im_is_active", use
+	// im_is_preediting() here.
+	if (p_imst == IM_ON_THE_SPOT
+		&& xic != NULL
+		&& lnum == wp->w_cursor.lnum
+		&& (State & INSERT)
+		&& !p_imdisable
+		&& im_is_preediting()
+		&& draw_state == WL_LINE)
+	{
+	    colnr_T tcol;
+
+	    if (preedit_end_col == MAXCOL)
+		getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
+	    else
+		tcol = preedit_end_col;
+	    if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
+	    {
+		if (feedback_old_attr < 0)
+		{
+		    feedback_col = 0;
+		    feedback_old_attr = char_attr;
+		}
+		char_attr = im_get_feedback_attr(feedback_col);
+		if (char_attr < 0)
+		    char_attr = feedback_old_attr;
+		feedback_col++;
+	    }
+	    else if (feedback_old_attr >= 0)
+	    {
+		char_attr = feedback_old_attr;
+		feedback_old_attr = -1;
+		feedback_col = 0;
+	    }
+	}
+#endif
+	// Handle the case where we are in column 0 but not on the first
+	// character of the line and the user wants us to show us a
+	// special character (via 'listchars' option "precedes:<char>".
+	if (lcs_prec_todo != NUL
+		&& wp->w_p_list
+		&& (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
+#ifdef FEAT_DIFF
+		&& filler_todo <= 0
+#endif
+		&& draw_state > WL_NR
+		&& c != NUL)
+	{
+	    c = lcs_prec;
+	    lcs_prec_todo = NUL;
+	    if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
+	    {
+		// Double-width character being overwritten by the "precedes"
+		// character, need to fill up half the character.
+		c_extra = MB_FILLER_CHAR;
+		c_final = NUL;
+		n_extra = 1;
+		n_attr = 2;
+		extra_attr = HL_ATTR(HLF_AT);
+	    }
+	    mb_c = c;
+	    if (enc_utf8 && utf_char2len(c) > 1)
+	    {
+		mb_utf8 = TRUE;
+		u8cc[0] = 0;
+		c = 0xc0;
+	    }
+	    else
+		mb_utf8 = FALSE;	// don't draw as UTF-8
+	    if (!attr_pri)
+	    {
+		saved_attr3 = char_attr; // save current attr
+		char_attr = HL_ATTR(HLF_AT); // later copied to char_attr
+		n_attr3 = 1;
+	    }
+	}
+
+	// At end of the text line or just after the last character.
+	if ((c == NUL
+#if defined(LINE_ATTR)
+		|| did_line_attr == 1
+#endif
+		) && eol_hl_off == 0)
+	{
+#ifdef FEAT_SEARCH_EXTRA
+	    // flag to indicate whether prevcol equals startcol of search_hl or
+	    // one of the matches
+	    int prevcol_hl_flag = get_prevcol_hl_flag(wp, &screen_search_hl,
+					      (long)(ptr - line) - (c == NUL));
+#endif
+	    // Invert at least one char, used for Visual and empty line or
+	    // highlight match at end of line. If it's beyond the last
+	    // char on the screen, just overwrite that one (tricky!)  Not
+	    // needed when a '$' was displayed for 'list'.
+	    if (lcs_eol == lcs_eol_one
+		    && ((area_attr != 0 && vcol == fromcol
+			    && (VIsual_mode != Ctrl_V
+				|| lnum == VIsual.lnum
+				|| lnum == curwin->w_cursor.lnum)
+			    && c == NUL)
+#ifdef FEAT_SEARCH_EXTRA
+			// highlight 'hlsearch' match at end of line
+			|| (prevcol_hl_flag
+# ifdef FEAT_SYN_HL
+			    && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
+				    && !(wp == curwin && VIsual_active))
+# endif
+# ifdef FEAT_DIFF
+			    && diff_hlf == (hlf_T)0
+# endif
+# if defined(LINE_ATTR)
+			    && did_line_attr <= 1
+# endif
+			   )
+#endif
+		       ))
+	    {
+		int n = 0;
+
+#ifdef FEAT_RIGHTLEFT
+		if (wp->w_p_rl)
+		{
+		    if (col < 0)
+			n = 1;
+		}
+		else
+#endif
+		{
+		    if (col >= wp->w_width)
+			n = -1;
+		}
+		if (n != 0)
+		{
+		    // At the window boundary, highlight the last character
+		    // instead (better than nothing).
+		    off += n;
+		    col += n;
+		}
+		else
+		{
+		    // Add a blank character to highlight.
+		    ScreenLines[off] = ' ';
+		    if (enc_utf8)
+			ScreenLinesUC[off] = 0;
+		}
+#ifdef FEAT_SEARCH_EXTRA
+		if (area_attr == 0)
+		{
+		    // Use attributes from match with highest priority among
+		    // 'search_hl' and the match list.
+		    get_search_match_hl(wp, &screen_search_hl,
+					       (long)(ptr - line), &char_attr);
+		}
+#endif
+		ScreenAttrs[off] = char_attr;
+#ifdef FEAT_RIGHTLEFT
+		if (wp->w_p_rl)
+		{
+		    --col;
+		    --off;
+		}
+		else
+#endif
+		{
+		    ++col;
+		    ++off;
+		}
+		++vcol;
+		eol_hl_off = 1;
+	    }
+	}
+
+	// At end of the text line.
+	if (c == NUL)
+	{
+#ifdef FEAT_SYN_HL
+	    // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
+	    if (wp->w_p_wrap)
+		v = wp->w_skipcol;
+	    else
+		v = wp->w_leftcol;
+
+	    // check if line ends before left margin
+	    if (vcol < v + col - win_col_off(wp))
+		vcol = v + col - win_col_off(wp);
+#ifdef FEAT_CONCEAL
+	    // Get rid of the boguscols now, we want to draw until the right
+	    // edge for 'cursorcolumn'.
+	    col -= boguscols;
+	    boguscols = 0;
+#endif
+
+	    if (draw_color_col)
+		draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
+
+	    if (((wp->w_p_cuc
+		      && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
+		      && (int)wp->w_virtcol <
+					wp->w_width * (row - startrow + 1) + v
+		      && lnum != wp->w_cursor.lnum)
+		    || draw_color_col
+		    || win_attr != 0)
+# ifdef FEAT_RIGHTLEFT
+		    && !wp->w_p_rl
+# endif
+		    )
+	    {
+		int	rightmost_vcol = 0;
+		int	i;
+
+		if (wp->w_p_cuc)
+		    rightmost_vcol = wp->w_virtcol;
+		if (draw_color_col)
+		    // determine rightmost colorcolumn to possibly draw
+		    for (i = 0; color_cols[i] >= 0; ++i)
+			if (rightmost_vcol < color_cols[i])
+			    rightmost_vcol = color_cols[i];
+
+		while (col < wp->w_width)
+		{
+		    ScreenLines[off] = ' ';
+		    if (enc_utf8)
+			ScreenLinesUC[off] = 0;
+		    ++col;
+		    if (draw_color_col)
+			draw_color_col = advance_color_col(VCOL_HLC,
+								 &color_cols);
+
+		    if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
+			ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
+		    else if (draw_color_col && VCOL_HLC == *color_cols)
+			ScreenAttrs[off++] = HL_ATTR(HLF_MC);
+		    else
+			ScreenAttrs[off++] = win_attr;
+
+		    if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
+			break;
+
+		    ++vcol;
+		}
+	    }
+#endif
+
+	    screen_line(screen_row, wp->w_wincol, col,
+					  (int)wp->w_width, screen_line_flags);
+	    row++;
+
+	    // Update w_cline_height and w_cline_folded if the cursor line was
+	    // updated (saves a call to plines() later).
+	    if (wp == curwin && lnum == curwin->w_cursor.lnum)
+	    {
+		curwin->w_cline_row = startrow;
+		curwin->w_cline_height = row - startrow;
+#ifdef FEAT_FOLDING
+		curwin->w_cline_folded = FALSE;
+#endif
+		curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
+	    }
+
+	    break;
+	}
+
+	// Show "extends" character from 'listchars' if beyond the line end and
+	// 'list' is set.
+	if (lcs_ext != NUL
+		&& wp->w_p_list
+		&& !wp->w_p_wrap
+#ifdef FEAT_DIFF
+		&& filler_todo <= 0
+#endif
+		&& (
+#ifdef FEAT_RIGHTLEFT
+		    wp->w_p_rl ? col == 0 :
+#endif
+		    col == wp->w_width - 1)
+		&& (*ptr != NUL
+		    || (wp->w_p_list && lcs_eol_one > 0)
+		    || (n_extra && (c_extra != NUL || *p_extra != NUL))))
+	{
+	    c = lcs_ext;
+	    char_attr = HL_ATTR(HLF_AT);
+	    mb_c = c;
+	    if (enc_utf8 && utf_char2len(c) > 1)
+	    {
+		mb_utf8 = TRUE;
+		u8cc[0] = 0;
+		c = 0xc0;
+	    }
+	    else
+		mb_utf8 = FALSE;
+	}
+
+#ifdef FEAT_SYN_HL
+	// advance to the next 'colorcolumn'
+	if (draw_color_col)
+	    draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
+
+	// Highlight the cursor column if 'cursorcolumn' is set.  But don't
+	// highlight the cursor position itself.
+	// Also highlight the 'colorcolumn' if it is different than
+	// 'cursorcolumn'
+	vcol_save_attr = -1;
+	if (draw_state == WL_LINE && !lnum_in_visual_area
+		&& search_attr == 0 && area_attr == 0)
+	{
+	    if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
+						 && lnum != wp->w_cursor.lnum)
+	    {
+		vcol_save_attr = char_attr;
+		char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
+	    }
+	    else if (draw_color_col && VCOL_HLC == *color_cols)
+	    {
+		vcol_save_attr = char_attr;
+		char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
+	    }
+	}
+#endif
+
+	// Store character to be displayed.
+	// Skip characters that are left of the screen for 'nowrap'.
+	vcol_prev = vcol;
+	if (draw_state < WL_LINE || n_skip <= 0)
+	{
+	    // Store the character.
+#if defined(FEAT_RIGHTLEFT)
+	    if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
+	    {
+		// A double-wide character is: put first halve in left cell.
+		--off;
+		--col;
+	    }
+#endif
+	    ScreenLines[off] = c;
+	    if (enc_dbcs == DBCS_JPNU)
+	    {
+		if ((mb_c & 0xff00) == 0x8e00)
+		    ScreenLines[off] = 0x8e;
+		ScreenLines2[off] = mb_c & 0xff;
+	    }
+	    else if (enc_utf8)
+	    {
+		if (mb_utf8)
+		{
+		    int i;
+
+		    ScreenLinesUC[off] = mb_c;
+		    if ((c & 0xff) == 0)
+			ScreenLines[off] = 0x80;   // avoid storing zero
+		    for (i = 0; i < Screen_mco; ++i)
+		    {
+			ScreenLinesC[i][off] = u8cc[i];
+			if (u8cc[i] == 0)
+			    break;
+		    }
+		}
+		else
+		    ScreenLinesUC[off] = 0;
+	    }
+	    if (multi_attr)
+	    {
+		ScreenAttrs[off] = multi_attr;
+		multi_attr = 0;
+	    }
+	    else
+		ScreenAttrs[off] = char_attr;
+
+	    if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
+	    {
+		// Need to fill two screen columns.
+		++off;
+		++col;
+		if (enc_utf8)
+		    // UTF-8: Put a 0 in the second screen char.
+		    ScreenLines[off] = 0;
+		else
+		    // DBCS: Put second byte in the second screen char.
+		    ScreenLines[off] = mb_c & 0xff;
+		if (draw_state > WL_NR
+#ifdef FEAT_DIFF
+			&& filler_todo <= 0
+#endif
+			)
+		    ++vcol;
+		// When "tocol" is halfway a character, set it to the end of
+		// the character, otherwise highlighting won't stop.
+		if (tocol == vcol)
+		    ++tocol;
+#ifdef FEAT_RIGHTLEFT
+		if (wp->w_p_rl)
+		{
+		    // now it's time to backup one cell
+		    --off;
+		    --col;
+		}
+#endif
+	    }
+#ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+	    {
+		--off;
+		--col;
+	    }
+	    else
+#endif
+	    {
+		++off;
+		++col;
+	    }
+	}
+#ifdef FEAT_CONCEAL
+	else if (wp->w_p_cole > 0 && is_concealing)
+	{
+	    --n_skip;
+	    ++vcol_off;
+	    if (n_extra > 0)
+		vcol_off += n_extra;
+	    if (wp->w_p_wrap)
+	    {
+		// Special voodoo required if 'wrap' is on.
+		//
+		// Advance the column indicator to force the line
+		// drawing to wrap early. This will make the line
+		// take up the same screen space when parts are concealed,
+		// so that cursor line computations aren't messed up.
+		//
+		// To avoid the fictitious advance of 'col' causing
+		// trailing junk to be written out of the screen line
+		// we are building, 'boguscols' keeps track of the number
+		// of bad columns we have advanced.
+		if (n_extra > 0)
+		{
+		    vcol += n_extra;
+# ifdef FEAT_RIGHTLEFT
+		    if (wp->w_p_rl)
+		    {
+			col -= n_extra;
+			boguscols -= n_extra;
+		    }
+		    else
+# endif
+		    {
+			col += n_extra;
+			boguscols += n_extra;
+		    }
+		    n_extra = 0;
+		    n_attr = 0;
+		}
+
+
+		if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
+		{
+		    // Need to fill two screen columns.
+# ifdef FEAT_RIGHTLEFT
+		    if (wp->w_p_rl)
+		    {
+			--boguscols;
+			--col;
+		    }
+		    else
+# endif
+		    {
+			++boguscols;
+			++col;
+		    }
+		}
+
+# ifdef FEAT_RIGHTLEFT
+		if (wp->w_p_rl)
+		{
+		    --boguscols;
+		    --col;
+		}
+		else
+# endif
+		{
+		    ++boguscols;
+		    ++col;
+		}
+	    }
+	    else
+	    {
+		if (n_extra > 0)
+		{
+		    vcol += n_extra;
+		    n_extra = 0;
+		    n_attr = 0;
+		}
+	    }
+
+	}
+#endif // FEAT_CONCEAL
+	else
+	    --n_skip;
+
+	// Only advance the "vcol" when after the 'number' or 'relativenumber'
+	// column.
+	if (draw_state > WL_NR
+#ifdef FEAT_DIFF
+		&& filler_todo <= 0
+#endif
+		)
+	    ++vcol;
+
+#ifdef FEAT_SYN_HL
+	if (vcol_save_attr >= 0)
+	    char_attr = vcol_save_attr;
+#endif
+
+	// restore attributes after "predeces" in 'listchars'
+	if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
+	    char_attr = saved_attr3;
+
+	// restore attributes after last 'listchars' or 'number' char
+	if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
+	    char_attr = saved_attr2;
+
+	// At end of screen line and there is more to come: Display the line
+	// so far.  If there is no more to display it is caught above.
+	if ((
+#ifdef FEAT_RIGHTLEFT
+	    wp->w_p_rl ? (col < 0) :
+#endif
+				    (col >= wp->w_width))
+		&& (*ptr != NUL
+#ifdef FEAT_DIFF
+		    || filler_todo > 0
+#endif
+		    || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
+		    || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
+		)
+	{
+#ifdef FEAT_CONCEAL
+	    screen_line(screen_row, wp->w_wincol, col - boguscols,
+					  (int)wp->w_width, screen_line_flags);
+	    boguscols = 0;
+#else
+	    screen_line(screen_row, wp->w_wincol, col,
+					  (int)wp->w_width, screen_line_flags);
+#endif
+	    ++row;
+	    ++screen_row;
+
+	    // When not wrapping and finished diff lines, or when displayed
+	    // '$' and highlighting until last column, break here.
+	    if ((!wp->w_p_wrap
+#ifdef FEAT_DIFF
+		    && filler_todo <= 0
+#endif
+		    ) || lcs_eol_one == -1)
+		break;
+
+	    // When the window is too narrow draw all "@" lines.
+	    if (draw_state != WL_LINE
+#ifdef FEAT_DIFF
+		    && filler_todo <= 0
+#endif
+		    )
+	    {
+		win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
+		draw_vsep_win(wp, row);
+		row = endrow;
+	    }
+
+	    // When line got too long for screen break here.
+	    if (row == endrow)
+	    {
+		++row;
+		break;
+	    }
+
+	    if (screen_cur_row == screen_row - 1
+#ifdef FEAT_DIFF
+		     && filler_todo <= 0
+#endif
+		     && wp->w_width == Columns)
+	    {
+		// Remember that the line wraps, used for modeless copy.
+		LineWraps[screen_row - 1] = TRUE;
+
+		// Special trick to make copy/paste of wrapped lines work with
+		// xterm/screen: write an extra character beyond the end of
+		// the line. This will work with all terminal types
+		// (regardless of the xn,am settings).
+		// Only do this on a fast tty.
+		// Only do this if the cursor is on the current line
+		// (something has been written in it).
+		// Don't do this for the GUI.
+		// Don't do this for double-width characters.
+		// Don't do this for a window not at the right screen border.
+		if (p_tf
+#ifdef FEAT_GUI
+			 && !gui.in_use
+#endif
+			 && !(has_mbyte
+			     && ((*mb_off2cells)(LineOffset[screen_row],
+				     LineOffset[screen_row] + screen_Columns)
+									  == 2
+				 || (*mb_off2cells)(LineOffset[screen_row - 1]
+							+ (int)Columns - 2,
+				     LineOffset[screen_row] + screen_Columns)
+									== 2)))
+		{
+		    // First make sure we are at the end of the screen line,
+		    // then output the same character again to let the
+		    // terminal know about the wrap.  If the terminal doesn't
+		    // auto-wrap, we overwrite the character.
+		    if (screen_cur_col != wp->w_width)
+			screen_char(LineOffset[screen_row - 1]
+						      + (unsigned)Columns - 1,
+					  screen_row - 1, (int)(Columns - 1));
+
+		    // When there is a multi-byte character, just output a
+		    // space to keep it simple.
+		    if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
+					screen_row - 1] + (Columns - 1)]) > 1)
+			out_char(' ');
+		    else
+			out_char(ScreenLines[LineOffset[screen_row - 1]
+							    + (Columns - 1)]);
+		    // force a redraw of the first char on the next line
+		    ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
+		    screen_start();	// don't know where cursor is now
+		}
+	    }
+
+	    col = 0;
+	    off = (unsigned)(current_ScreenLine - ScreenLines);
+#ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+	    {
+		col = wp->w_width - 1;	// col is not used if breaking!
+		off += col;
+	    }
+#endif
+
+	    // reset the drawing state for the start of a wrapped line
+	    draw_state = WL_START;
+	    saved_n_extra = n_extra;
+	    saved_p_extra = p_extra;
+	    saved_c_extra = c_extra;
+	    saved_c_final = c_final;
+#ifdef FEAT_SYN_HL
+	    if (!(cul_screenline
+# ifdef FEAT_DIFF
+			&& diff_hlf == (hlf_T)0)
+# endif
+		    )
+		saved_char_attr = char_attr;
+	    else
+#endif
+		saved_char_attr = 0;
+	    n_extra = 0;
+	    lcs_prec_todo = lcs_prec;
+#ifdef FEAT_LINEBREAK
+# ifdef FEAT_DIFF
+	    if (filler_todo <= 0)
+# endif
+		need_showbreak = TRUE;
+#endif
+#ifdef FEAT_DIFF
+	    --filler_todo;
+	    // When the filler lines are actually below the last line of the
+	    // file, don't draw the line itself, break here.
+	    if (filler_todo == 0 && wp->w_botfill)
+		break;
+#endif
+	}
+
+    }	// for every character in the line
+
+#ifdef FEAT_SPELL
+    // After an empty line check first word for capital.
+    if (*skipwhite(line) == NUL)
+    {
+	capcol_lnum = lnum + 1;
+	cap_col = 0;
+    }
+#endif
+#ifdef FEAT_TEXT_PROP
+    vim_free(text_props);
+    vim_free(text_prop_idxs);
+#endif
+
+    vim_free(p_extra_free);
+    return row;
+}
+
new file mode 100644
--- /dev/null
+++ b/src/drawscreen.c
@@ -0,0 +1,3112 @@
+/* vi:set ts=8 sts=4 sw=4 noet:
+ *
+ * VIM - Vi IMproved	by Bram Moolenaar
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * drawscreen.c: Code for updating all the windows on the screen.
+ * This is the top level, drawline.c is the middle and screen.c the lower
+ * level.
+ *
+ * update_screen() is the function that updates all windows and status lines.
+ * It is called form the main loop when must_redraw is non-zero.  It may be
+ * called from other places when an immediate screen update is needed.
+ *
+ * The part of the buffer that is displayed in a window is set with:
+ * - w_topline (first buffer line in window)
+ * - w_topfill (filler lines above the first line)
+ * - w_leftcol (leftmost window cell in window),
+ * - w_skipcol (skipped window cells of first line)
+ *
+ * Commands that only move the cursor around in a window, do not need to take
+ * action to update the display.  The main loop will check if w_topline is
+ * valid and update it (scroll the window) when needed.
+ *
+ * Commands that scroll a window change w_topline and must call
+ * check_cursor() to move the cursor into the visible part of the window, and
+ * call redraw_later(VALID) to have the window displayed by update_screen()
+ * later.
+ *
+ * Commands that change text in the buffer must call changed_bytes() or
+ * changed_lines() to mark the area that changed and will require updating
+ * later.  The main loop will call update_screen(), which will update each
+ * window that shows the changed buffer.  This assumes text above the change
+ * can remain displayed as it is.  Text after the change may need updating for
+ * scrolling, folding and syntax highlighting.
+ *
+ * Commands that change how a window is displayed (e.g., setting 'list') or
+ * invalidate the contents of a window in another way (e.g., change fold
+ * settings), must call redraw_later(NOT_VALID) to have the whole window
+ * redisplayed by update_screen() later.
+ *
+ * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
+ * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
+ * buffer redisplayed by update_screen() later.
+ *
+ * Commands that change highlighting and possibly cause a scroll too must call
+ * redraw_later(SOME_VALID) to update the whole window but still use scrolling
+ * to avoid redrawing everything.  But the length of displayed lines must not
+ * change, use NOT_VALID then.
+ *
+ * Commands that move the window position must call redraw_later(NOT_VALID).
+ * TODO: should minimize redrawing by scrolling when possible.
+ *
+ * Commands that change everything (e.g., resizing the screen) must call
+ * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
+ *
+ * Things that are handled indirectly:
+ * - When messages scroll the screen up, msg_scrolled will be set and
+ *   update_screen() called to redraw.
+ */
+
+#include "vim.h"
+
+static void win_update(win_T *wp);
+#ifdef FEAT_STL_OPT
+static void redraw_custom_statusline(win_T *wp);
+#endif
+
+/*
+ * Based on the current value of curwin->w_topline, transfer a screenfull
+ * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
+ * Return OK when the screen was updated, FAIL if it was not done.
+ */
+    int
+update_screen(int type_arg)
+{
+    int		type = type_arg;
+    win_T	*wp;
+    static int	did_intro = FALSE;
+#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
+    int		did_one;
+#endif
+#ifdef FEAT_GUI
+    int		did_undraw = FALSE;
+    int		gui_cursor_col = 0;
+    int		gui_cursor_row = 0;
+#endif
+    int		no_update = FALSE;
+
+    // Don't do anything if the screen structures are (not yet) valid.
+    if (!screen_valid(TRUE))
+	return FAIL;
+
+    if (type == VALID_NO_UPDATE)
+    {
+	no_update = TRUE;
+	type = 0;
+    }
+
+#ifdef FEAT_EVAL
+    {
+	buf_T *buf;
+
+	// Before updating the screen, notify any listeners of changed text.
+	FOR_ALL_BUFFERS(buf)
+	    invoke_listeners(buf);
+    }
+#endif
+
+#ifdef FEAT_DIFF
+    // May have postponed updating diffs.
+    if (need_diff_redraw)
+	diff_redraw(TRUE);
+#endif
+
+    if (must_redraw)
+    {
+	if (type < must_redraw)	    // use maximal type
+	    type = must_redraw;
+
+	// must_redraw is reset here, so that when we run into some weird
+	// reason to redraw while busy redrawing (e.g., asynchronous
+	// scrolling), or update_topline() in win_update() will cause a
+	// scroll, the screen will be redrawn later or in win_update().
+	must_redraw = 0;
+    }
+
+    // May need to update w_lines[].
+    if (curwin->w_lines_valid == 0 && type < NOT_VALID
+#ifdef FEAT_TERMINAL
+	    && !term_do_update_window(curwin)
+#endif
+		)
+	type = NOT_VALID;
+
+    // Postpone the redrawing when it's not needed and when being called
+    // recursively.
+    if (!redrawing() || updating_screen)
+    {
+	redraw_later(type);		// remember type for next time
+	must_redraw = type;
+	if (type > INVERTED_ALL)
+	    curwin->w_lines_valid = 0;	// don't use w_lines[].wl_size now
+	return FAIL;
+    }
+    updating_screen = TRUE;
+
+#ifdef FEAT_TEXT_PROP
+    // Update popup_mask if needed.  This may set w_redraw_top and w_redraw_bot
+    // in some windows.
+    may_update_popup_mask(type);
+#endif
+
+#ifdef FEAT_SYN_HL
+    ++display_tick;	    // let syntax code know we're in a next round of
+			    // display updating
+#endif
+    if (no_update)
+	++no_win_do_lines_ins;
+
+    // if the screen was scrolled up when displaying a message, scroll it down
+    if (msg_scrolled)
+    {
+	clear_cmdline = TRUE;
+	if (msg_scrolled > Rows - 5)	    // clearing is faster
+	    type = CLEAR;
+	else if (type != CLEAR)
+	{
+	    check_for_delay(FALSE);
+	    if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
+								       == FAIL)
+		type = CLEAR;
+	    FOR_ALL_WINDOWS(wp)
+	    {
+		if (wp->w_winrow < msg_scrolled)
+		{
+		    if (W_WINROW(wp) + wp->w_height > msg_scrolled
+			    && wp->w_redr_type < REDRAW_TOP
+			    && wp->w_lines_valid > 0
+			    && wp->w_topline == wp->w_lines[0].wl_lnum)
+		    {
+			wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
+			wp->w_redr_type = REDRAW_TOP;
+		    }
+		    else
+		    {
+			wp->w_redr_type = NOT_VALID;
+			if (W_WINROW(wp) + wp->w_height + wp->w_status_height
+							       <= msg_scrolled)
+			    wp->w_redr_status = TRUE;
+		    }
+		}
+	    }
+	    if (!no_update)
+		redraw_cmdline = TRUE;
+	    redraw_tabline = TRUE;
+	}
+	msg_scrolled = 0;
+	need_wait_return = FALSE;
+    }
+
+    // reset cmdline_row now (may have been changed temporarily)
+    compute_cmdrow();
+
+    // Check for changed highlighting
+    if (need_highlight_changed)
+	highlight_changed();
+
+    if (type == CLEAR)		// first clear screen
+    {
+	screenclear();		// will reset clear_cmdline
+	type = NOT_VALID;
+	// must_redraw may be set indirectly, avoid another redraw later
+	must_redraw = 0;
+    }
+
+    if (clear_cmdline)		// going to clear cmdline (done below)
+	check_for_delay(FALSE);
+
+#ifdef FEAT_LINEBREAK
+    // Force redraw when width of 'number' or 'relativenumber' column
+    // changes.
+    if (curwin->w_redr_type < NOT_VALID
+	   && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
+				    ? number_width(curwin) : 0))
+	curwin->w_redr_type = NOT_VALID;
+#endif
+
+    // Only start redrawing if there is really something to do.
+    if (type == INVERTED)
+	update_curswant();
+    if (curwin->w_redr_type < type
+	    && !((type == VALID
+		    && curwin->w_lines[0].wl_valid
+#ifdef FEAT_DIFF
+		    && curwin->w_topfill == curwin->w_old_topfill
+		    && curwin->w_botfill == curwin->w_old_botfill
+#endif
+		    && curwin->w_topline == curwin->w_lines[0].wl_lnum)
+		|| (type == INVERTED
+		    && VIsual_active
+		    && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
+		    && curwin->w_old_visual_mode == VIsual_mode
+		    && (curwin->w_valid & VALID_VIRTCOL)
+		    && curwin->w_old_curswant == curwin->w_curswant)
+		))
+	curwin->w_redr_type = type;
+
+    // Redraw the tab pages line if needed.
+    if (redraw_tabline || type >= NOT_VALID)
+	draw_tabline();
+
+#ifdef FEAT_SYN_HL
+    // Correct stored syntax highlighting info for changes in each displayed
+    // buffer.  Each buffer must only be done once.
+    FOR_ALL_WINDOWS(wp)
+    {
+	if (wp->w_buffer->b_mod_set)
+	{
+	    win_T	*wwp;
+
+	    // Check if we already did this buffer.
+	    for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
+		if (wwp->w_buffer == wp->w_buffer)
+		    break;
+	    if (wwp == wp && syntax_present(wp))
+		syn_stack_apply_changes(wp->w_buffer);
+	}
+    }
+#endif
+
+    // Go from top to bottom through the windows, redrawing the ones that need
+    // it.
+#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
+    did_one = FALSE;
+#endif
+#ifdef FEAT_SEARCH_EXTRA
+    screen_search_hl.rm.regprog = NULL;
+#endif
+    FOR_ALL_WINDOWS(wp)
+    {
+	if (wp->w_redr_type != 0)
+	{
+	    cursor_off();
+#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
+	    if (!did_one)
+	    {
+		did_one = TRUE;
+# ifdef FEAT_SEARCH_EXTRA
+		start_search_hl();
+# endif
+# ifdef FEAT_CLIPBOARD
+		// When Visual area changed, may have to update selection.
+		if (clip_star.available && clip_isautosel_star())
+		    clip_update_selection(&clip_star);
+		if (clip_plus.available && clip_isautosel_plus())
+		    clip_update_selection(&clip_plus);
+# endif
+#ifdef FEAT_GUI
+		// Remove the cursor before starting to do anything, because
+		// scrolling may make it difficult to redraw the text under
+		// it.
+		if (gui.in_use && wp == curwin)
+		{
+		    gui_cursor_col = gui.cursor_col;
+		    gui_cursor_row = gui.cursor_row;
+		    gui_undraw_cursor();
+		    did_undraw = TRUE;
+		}
+#endif
+	    }
+#endif
+	    win_update(wp);
+	}
+
+	// redraw status line after the window to minimize cursor movement
+	if (wp->w_redr_status)
+	{
+	    cursor_off();
+	    win_redr_status(wp, TRUE); // any popup menu will be redrawn below
+	}
+    }
+#if defined(FEAT_SEARCH_EXTRA)
+    end_search_hl();
+#endif
+    // May need to redraw the popup menu.
+    pum_may_redraw();
+
+    // Reset b_mod_set flags.  Going through all windows is probably faster
+    // than going through all buffers (there could be many buffers).
+    FOR_ALL_WINDOWS(wp)
+	wp->w_buffer->b_mod_set = FALSE;
+
+#ifdef FEAT_TEXT_PROP
+    // Display popup windows on top of the windows and command line.
+    update_popups(win_update);
+#endif
+
+    after_updating_screen(TRUE);
+
+    // Clear or redraw the command line.  Done last, because scrolling may
+    // mess up the command line.
+    if (clear_cmdline || redraw_cmdline || redraw_mode)
+	showmode();
+
+    if (no_update)
+	--no_win_do_lines_ins;
+
+    // May put up an introductory message when not editing a file
+    if (!did_intro)
+	maybe_intro_message();
+    did_intro = TRUE;
+
+#ifdef FEAT_GUI
+    // Redraw the cursor and update the scrollbars when all screen updating is
+    // done.
+    if (gui.in_use)
+    {
+	if (did_undraw && !gui_mch_is_blink_off())
+	{
+	    mch_disable_flush();
+	    out_flush();	// required before updating the cursor
+	    mch_enable_flush();
+
+	    // Put the GUI position where the cursor was, gui_update_cursor()
+	    // uses that.
+	    gui.col = gui_cursor_col;
+	    gui.row = gui_cursor_row;
+	    gui.col = mb_fix_col(gui.col, gui.row);
+	    gui_update_cursor(FALSE, FALSE);
+	    gui_may_flush();
+	    screen_cur_col = gui.col;
+	    screen_cur_row = gui.row;
+	}
+	else
+	    out_flush();
+	gui_update_scrollbars(FALSE);
+    }
+#endif
+    return OK;
+}
+
+/*
+ * Redraw the status line of window wp.
+ *
+ * If inversion is possible we use it. Else '=' characters are used.
+ * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
+ * displayed.
+ */
+    void
+win_redr_status(win_T *wp, int ignore_pum UNUSED)
+{
+    int		row;
+    char_u	*p;
+    int		len;
+    int		fillchar;
+    int		attr;
+    int		this_ru_col;
+    static int  busy = FALSE;
+
+    // It's possible to get here recursively when 'statusline' (indirectly)
+    // invokes ":redrawstatus".  Simply ignore the call then.
+    if (busy)
+	return;
+    busy = TRUE;
+
+    wp->w_redr_status = FALSE;
+    if (wp->w_status_height == 0)
+    {
+	// no status line, can only be last window
+	redraw_cmdline = TRUE;
+    }
+    else if (!redrawing()
+	    // don't update status line when popup menu is visible and may be
+	    // drawn over it, unless it will be redrawn later
+	    || (!ignore_pum && pum_visible()))
+    {
+	// Don't redraw right now, do it later.
+	wp->w_redr_status = TRUE;
+    }
+#ifdef FEAT_STL_OPT
+    else if (*p_stl != NUL || *wp->w_p_stl != NUL)
+    {
+	// redraw custom status line
+	redraw_custom_statusline(wp);
+    }
+#endif
+    else
+    {
+	fillchar = fillchar_status(&attr, wp);
+
+	get_trans_bufname(wp->w_buffer);
+	p = NameBuff;
+	len = (int)STRLEN(p);
+
+	if (bt_help(wp->w_buffer)
+#ifdef FEAT_QUICKFIX
+		|| wp->w_p_pvw
+#endif
+		|| bufIsChanged(wp->w_buffer)
+		|| wp->w_buffer->b_p_ro)
+	    *(p + len++) = ' ';
+	if (bt_help(wp->w_buffer))
+	{
+	    STRCPY(p + len, _("[Help]"));
+	    len += (int)STRLEN(p + len);
+	}
+#ifdef FEAT_QUICKFIX
+	if (wp->w_p_pvw)
+	{
+	    STRCPY(p + len, _("[Preview]"));
+	    len += (int)STRLEN(p + len);
+	}
+#endif
+	if (bufIsChanged(wp->w_buffer)
+#ifdef FEAT_TERMINAL
+		&& !bt_terminal(wp->w_buffer)
+#endif
+		)
+	{
+	    STRCPY(p + len, "[+]");
+	    len += 3;
+	}
+	if (wp->w_buffer->b_p_ro)
+	{
+	    STRCPY(p + len, _("[RO]"));
+	    len += (int)STRLEN(p + len);
+	}
+
+	this_ru_col = ru_col - (Columns - wp->w_width);
+	if (this_ru_col < (wp->w_width + 1) / 2)
+	    this_ru_col = (wp->w_width + 1) / 2;
+	if (this_ru_col <= 1)
+	{
+	    p = (char_u *)"<";		// No room for file name!
+	    len = 1;
+	}
+	else if (has_mbyte)
+	{
+	    int	clen = 0, i;
+
+	    // Count total number of display cells.
+	    clen = mb_string2cells(p, -1);
+
+	    // Find first character that will fit.
+	    // Going from start to end is much faster for DBCS.
+	    for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
+		    i += (*mb_ptr2len)(p + i))
+		clen -= (*mb_ptr2cells)(p + i);
+	    len = clen;
+	    if (i > 0)
+	    {
+		p = p + i - 1;
+		*p = '<';
+		++len;
+	    }
+
+	}
+	else if (len > this_ru_col - 1)
+	{
+	    p += len - (this_ru_col - 1);
+	    *p = '<';
+	    len = this_ru_col - 1;
+	}
+
+	row = W_WINROW(wp) + wp->w_height;
+	screen_puts(p, row, wp->w_wincol, attr);
+	screen_fill(row, row + 1, len + wp->w_wincol,
+			this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
+
+	if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
+		&& (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
+	    screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
+						   - 1 + wp->w_wincol), attr);
+
+#ifdef FEAT_CMDL_INFO
+	win_redr_ruler(wp, TRUE, ignore_pum);
+#endif
+    }
+
+    /*
+     * May need to draw the character below the vertical separator.
+     */
+    if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
+    {
+	if (stl_connected(wp))
+	    fillchar = fillchar_status(&attr, wp);
+	else
+	    fillchar = fillchar_vsep(&attr);
+	screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
+									attr);
+    }
+    busy = FALSE;
+}
+
+#ifdef FEAT_STL_OPT
+/*
+ * Redraw the status line according to 'statusline' and take care of any
+ * errors encountered.
+ */
+    static void
+redraw_custom_statusline(win_T *wp)
+{
+    static int	    entered = FALSE;
+    int		    saved_did_emsg = did_emsg;
+
+    // When called recursively return.  This can happen when the statusline
+    // contains an expression that triggers a redraw.
+    if (entered)
+	return;
+    entered = TRUE;
+
+    did_emsg = FALSE;
+    win_redr_custom(wp, FALSE);
+    if (did_emsg)
+    {
+	// When there is an error disable the statusline, otherwise the
+	// display is messed up with errors and a redraw triggers the problem
+	// again and again.
+	set_string_option_direct((char_u *)"statusline", -1,
+		(char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
+					? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
+    }
+    did_emsg |= saved_did_emsg;
+    entered = FALSE;
+}
+#endif
+
+/*
+ * Show current status info in ruler and various other places
+ * If always is FALSE, only show ruler if position has changed.
+ */
+    void
+showruler(int always)
+{
+    if (!always && !redrawing())
+	return;
+    if (pum_visible())
+    {
+	// Don't redraw right now, do it later.
+	curwin->w_redr_status = TRUE;
+	return;
+    }
+#if defined(FEAT_STL_OPT)
+    if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
+	redraw_custom_statusline(curwin);
+    else
+#endif
+#ifdef FEAT_CMDL_INFO
+	win_redr_ruler(curwin, always, FALSE);
+#endif
+
+#ifdef FEAT_TITLE
+    if (need_maketitle
+# ifdef FEAT_STL_OPT
+	    || (p_icon && (stl_syntax & STL_IN_ICON))
+	    || (p_title && (stl_syntax & STL_IN_TITLE))
+# endif
+       )
+	maketitle();
+#endif
+    // Redraw the tab pages line if needed.
+    if (redraw_tabline)
+	draw_tabline();
+}
+
+#if defined(FEAT_CMDL_INFO) || defined(PROTO)
+    void
+win_redr_ruler(win_T *wp, int always, int ignore_pum)
+{
+#define RULER_BUF_LEN 70
+    char_u	buffer[RULER_BUF_LEN];
+    int		row;
+    int		fillchar;
+    int		attr;
+    int		empty_line = FALSE;
+    colnr_T	virtcol;
+    int		i;
+    size_t	len;
+    int		o;
+    int		this_ru_col;
+    int		off = 0;
+    int		width;
+
+    // If 'ruler' off or redrawing disabled, don't do anything
+    if (!p_ru)
+	return;
+
+    /*
+     * Check if cursor.lnum is valid, since win_redr_ruler() may be called
+     * after deleting lines, before cursor.lnum is corrected.
+     */
+    if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
+	return;
+
+    // Don't draw the ruler while doing insert-completion, it might overwrite
+    // the (long) mode message.
+    if (wp == lastwin && lastwin->w_status_height == 0)
+	if (edit_submode != NULL)
+	    return;
+    // Don't draw the ruler when the popup menu is visible, it may overlap.
+    // Except when the popup menu will be redrawn anyway.
+    if (!ignore_pum && pum_visible())
+	return;
+
+#ifdef FEAT_STL_OPT
+    if (*p_ruf)
+    {
+	int	save_called_emsg = called_emsg;
+
+	called_emsg = FALSE;
+	win_redr_custom(wp, TRUE);
+	if (called_emsg)
+	    set_string_option_direct((char_u *)"rulerformat", -1,
+					   (char_u *)"", OPT_FREE, SID_ERROR);
+	called_emsg |= save_called_emsg;
+	return;
+    }
+#endif
+
+    /*
+     * Check if not in Insert mode and the line is empty (will show "0-1").
+     */
+    if (!(State & INSERT)
+		&& *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
+	empty_line = TRUE;
+
+    /*
+     * Only draw the ruler when something changed.
+     */
+    validate_virtcol_win(wp);
+    if (       redraw_cmdline
+	    || always
+	    || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
+	    || wp->w_cursor.col != wp->w_ru_cursor.col
+	    || wp->w_virtcol != wp->w_ru_virtcol
+	    || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
+	    || wp->w_topline != wp->w_ru_topline
+	    || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
+#ifdef FEAT_DIFF
+	    || wp->w_topfill != wp->w_ru_topfill
+#endif
+	    || empty_line != wp->w_ru_empty)
+    {
+	cursor_off();
+	if (wp->w_status_height)
+	{
+	    row = W_WINROW(wp) + wp->w_height;
+	    fillchar = fillchar_status(&attr, wp);
+	    off = wp->w_wincol;
+	    width = wp->w_width;
+	}
+	else
+	{
+	    row = Rows - 1;
+	    fillchar = ' ';
+	    attr = 0;
+	    width = Columns;
+	    off = 0;
+	}
+
+	// In list mode virtcol needs to be recomputed
+	virtcol = wp->w_virtcol;
+	if (wp->w_p_list && lcs_tab1 == NUL)
+	{
+	    wp->w_p_list = FALSE;
+	    getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
+	    wp->w_p_list = TRUE;
+	}
+
+	/*
+	 * Some sprintfs return the length, some return a pointer.
+	 * To avoid portability problems we use strlen() here.
+	 */
+	vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
+		(wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
+		    ? 0L
+		    : (long)(wp->w_cursor.lnum));
+	len = STRLEN(buffer);
+	col_print(buffer + len, RULER_BUF_LEN - len,
+			empty_line ? 0 : (int)wp->w_cursor.col + 1,
+			(int)virtcol + 1);
+
+	/*
+	 * Add a "50%" if there is room for it.
+	 * On the last line, don't print in the last column (scrolls the
+	 * screen up on some terminals).
+	 */
+	i = (int)STRLEN(buffer);
+	get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
+	o = i + vim_strsize(buffer + i + 1);
+	if (wp->w_status_height == 0)	// can't use last char of screen
+	    ++o;
+	this_ru_col = ru_col - (Columns - width);
+	if (this_ru_col < 0)
+	    this_ru_col = 0;
+	// Never use more than half the window/screen width, leave the other
+	// half for the filename.
+	if (this_ru_col < (width + 1) / 2)
+	    this_ru_col = (width + 1) / 2;
+	if (this_ru_col + o < width)
+	{
+	    // need at least 3 chars left for get_rel_pos() + NUL
+	    while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
+	    {
+		if (has_mbyte)
+		    i += (*mb_char2bytes)(fillchar, buffer + i);
+		else
+		    buffer[i++] = fillchar;
+		++o;
+	    }
+	    get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
+	}
+	// Truncate at window boundary.
+	if (has_mbyte)
+	{
+	    o = 0;
+	    for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
+	    {
+		o += (*mb_ptr2cells)(buffer + i);
+		if (this_ru_col + o > width)
+		{
+		    buffer[i] = NUL;
+		    break;
+		}
+	    }
+	}
+	else if (this_ru_col + (int)STRLEN(buffer) > width)
+	    buffer[width - this_ru_col] = NUL;
+
+	screen_puts(buffer, row, this_ru_col + off, attr);
+	i = redraw_cmdline;
+	screen_fill(row, row + 1,
+		this_ru_col + off + (int)STRLEN(buffer),
+		(int)(off + width),
+		fillchar, fillchar, attr);
+	// don't redraw the cmdline because of showing the ruler
+	redraw_cmdline = i;
+	wp->w_ru_cursor = wp->w_cursor;
+	wp->w_ru_virtcol = wp->w_virtcol;
+	wp->w_ru_empty = empty_line;
+	wp->w_ru_topline = wp->w_topline;
+	wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
+#ifdef FEAT_DIFF
+	wp->w_ru_topfill = wp->w_topfill;
+#endif
+    }
+}
+#endif
+
+/*
+ * To be called when "updating_screen" was set before and now the postponed
+ * side effects may take place.
+ */
+    void
+after_updating_screen(int may_resize_shell UNUSED)
+{
+    updating_screen = FALSE;
+#ifdef FEAT_GUI
+    if (may_resize_shell)
+	gui_may_resize_shell();
+#endif
+#ifdef FEAT_TERMINAL
+    term_check_channel_closed_recently();
+#endif
+
+#ifdef HAVE_DROP_FILE
+    // If handle_drop() was called while updating_screen was TRUE need to
+    // handle the drop now.
+    handle_any_postponed_drop();
+#endif
+}
+
+/*
+ * Update all windows that are editing the current buffer.
+ */
+    void
+update_curbuf(int type)
+{
+    redraw_curbuf_later(type);
+    update_screen(type);
+}
+
+#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
+/*
+ * Copy "text" to ScreenLines using "attr".
+ * Returns the next screen column.
+ */
+    static int
+text_to_screenline(win_T *wp, char_u *text, int col)
+{
+    int		off = (int)(current_ScreenLine - ScreenLines);
+
+    if (has_mbyte)
+    {
+	int	cells;
+	int	u8c, u8cc[MAX_MCO];
+	int	i;
+	int	idx;
+	int	c_len;
+	char_u	*p;
+# ifdef FEAT_ARABIC
+	int	prev_c = 0;		// previous Arabic character
+	int	prev_c1 = 0;		// first composing char for prev_c
+# endif
+
+# ifdef FEAT_RIGHTLEFT
+	if (wp->w_p_rl)
+	    idx = off;
+	else
+# endif
+	    idx = off + col;
+
+	// Store multibyte characters in ScreenLines[] et al. correctly.
+	for (p = text; *p != NUL; )
+	{
+	    cells = (*mb_ptr2cells)(p);
+	    c_len = (*mb_ptr2len)(p);
+	    if (col + cells > wp->w_width
+# ifdef FEAT_RIGHTLEFT
+		    - (wp->w_p_rl ? col : 0)
+# endif
+		    )
+		break;
+	    ScreenLines[idx] = *p;
+	    if (enc_utf8)
+	    {
+		u8c = utfc_ptr2char(p, u8cc);
+		if (*p < 0x80 && u8cc[0] == 0)
+		{
+		    ScreenLinesUC[idx] = 0;
+#ifdef FEAT_ARABIC
+		    prev_c = u8c;
+#endif
+		}
+		else
+		{
+#ifdef FEAT_ARABIC
+		    if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
+		    {
+			// Do Arabic shaping.
+			int	pc, pc1, nc;
+			int	pcc[MAX_MCO];
+			int	firstbyte = *p;
+
+			// The idea of what is the previous and next
+			// character depends on 'rightleft'.
+			if (wp->w_p_rl)
+			{
+			    pc = prev_c;
+			    pc1 = prev_c1;
+			    nc = utf_ptr2char(p + c_len);
+			    prev_c1 = u8cc[0];
+			}
+			else
+			{
+			    pc = utfc_ptr2char(p + c_len, pcc);
+			    nc = prev_c;
+			    pc1 = pcc[0];
+			}
+			prev_c = u8c;
+
+			u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
+								 pc, pc1, nc);
+			ScreenLines[idx] = firstbyte;
+		    }
+		    else
+			prev_c = u8c;
+#endif
+		    // Non-BMP character: display as ? or fullwidth ?.
+		    ScreenLinesUC[idx] = u8c;
+		    for (i = 0; i < Screen_mco; ++i)
+		    {
+			ScreenLinesC[i][idx] = u8cc[i];
+			if (u8cc[i] == 0)
+			    break;
+		    }
+		}
+		if (cells > 1)
+		    ScreenLines[idx + 1] = 0;
+	    }
+	    else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
+		// double-byte single width character
+		ScreenLines2[idx] = p[1];
+	    else if (cells > 1)
+		// double-width character
+		ScreenLines[idx + 1] = p[1];
+	    col += cells;
+	    idx += cells;
+	    p += c_len;
+	}
+    }
+    else
+    {
+	int len = (int)STRLEN(text);
+
+	if (len > wp->w_width - col)
+	    len = wp->w_width - col;
+	if (len > 0)
+	{
+#ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+		mch_memmove(current_ScreenLine, text, len);
+	    else
+#endif
+		mch_memmove(current_ScreenLine + col, text, len);
+	    col += len;
+	}
+    }
+    return col;
+}
+#endif
+
+#ifdef FEAT_MENU
+/*
+ * Draw the window toolbar.
+ */
+    static void
+redraw_win_toolbar(win_T *wp)
+{
+    vimmenu_T	*menu;
+    int		item_idx = 0;
+    int		item_count = 0;
+    int		col = 0;
+    int		next_col;
+    int		off = (int)(current_ScreenLine - ScreenLines);
+    int		fill_attr = syn_name2attr((char_u *)"ToolbarLine");
+    int		button_attr = syn_name2attr((char_u *)"ToolbarButton");
+
+    vim_free(wp->w_winbar_items);
+    for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
+	++item_count;
+    wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
+
+    // TODO: use fewer spaces if there is not enough room
+    for (menu = wp->w_winbar->children;
+			  menu != NULL && col < wp->w_width; menu = menu->next)
+    {
+	space_to_screenline(off + col, fill_attr);
+	if (++col >= wp->w_width)
+	    break;
+	if (col > 1)
+	{
+	    space_to_screenline(off + col, fill_attr);
+	    if (++col >= wp->w_width)
+		break;
+	}
+
+	wp->w_winbar_items[item_idx].wb_startcol = col;
+	space_to_screenline(off + col, button_attr);
+	if (++col >= wp->w_width)
+	    break;
+
+	next_col = text_to_screenline(wp, menu->name, col);
+	while (col < next_col)
+	{
+	    ScreenAttrs[off + col] = button_attr;
+	    ++col;
+	}
+	wp->w_winbar_items[item_idx].wb_endcol = col;
+	wp->w_winbar_items[item_idx].wb_menu = menu;
+	++item_idx;
+
+	if (col >= wp->w_width)
+	    break;
+	space_to_screenline(off + col, button_attr);
+	++col;
+    }
+    while (col < wp->w_width)
+    {
+	space_to_screenline(off + col, fill_attr);
+	++col;
+    }
+    wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
+
+    screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
+							  (int)wp->w_width, 0);
+}
+#endif
+
+#if defined(FEAT_FOLDING) || defined(PROTO)
+/*
+ * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
+ */
+    static void
+copy_text_attr(
+    int		off,
+    char_u	*buf,
+    int		len,
+    int		attr)
+{
+    int		i;
+
+    mch_memmove(ScreenLines + off, buf, (size_t)len);
+    if (enc_utf8)
+	vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
+    for (i = 0; i < len; ++i)
+	ScreenAttrs[off + i] = attr;
+}
+
+/*
+ * Display one folded line.
+ */
+    static void
+fold_line(
+    win_T	*wp,
+    long	fold_count,
+    foldinfo_T	*foldinfo,
+    linenr_T	lnum,
+    int		row)
+{
+    char_u	buf[FOLD_TEXT_LEN];
+    pos_T	*top, *bot;
+    linenr_T	lnume = lnum + fold_count - 1;
+    int		len;
+    char_u	*text;
+    int		fdc;
+    int		col;
+    int		txtcol;
+    int		off = (int)(current_ScreenLine - ScreenLines);
+    int		ri;
+
+    // Build the fold line:
+    // 1. Add the cmdwin_type for the command-line window
+    // 2. Add the 'foldcolumn'
+    // 3. Add the 'number' or 'relativenumber' column
+    // 4. Compose the text
+    // 5. Add the text
+    // 6. set highlighting for the Visual area an other text
+    col = 0;
+
+    // 1. Add the cmdwin_type for the command-line window
+    // Ignores 'rightleft', this window is never right-left.
+#ifdef FEAT_CMDWIN
+    if (cmdwin_type != 0 && wp == curwin)
+    {
+	ScreenLines[off] = cmdwin_type;
+	ScreenAttrs[off] = HL_ATTR(HLF_AT);
+	if (enc_utf8)
+	    ScreenLinesUC[off] = 0;
+	++col;
+    }
+#endif
+
+    // 2. Add the 'foldcolumn'
+    //    Reduce the width when there is not enough space.
+    fdc = compute_foldcolumn(wp, col);
+    if (fdc > 0)
+    {
+	fill_foldcolumn(buf, wp, TRUE, lnum);
+#ifdef FEAT_RIGHTLEFT
+	if (wp->w_p_rl)
+	{
+	    int		i;
+
+	    copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
+							     HL_ATTR(HLF_FC));
+	    // reverse the fold column
+	    for (i = 0; i < fdc; ++i)
+		ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
+	}
+	else
+#endif
+	    copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
+	col += fdc;
+    }
+
+#ifdef FEAT_RIGHTLEFT
+# define RL_MEMSET(p, v, l) \
+    do { \
+	if (wp->w_p_rl) \
+	    for (ri = 0; ri < l; ++ri) \
+	       ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
+	 else \
+	    for (ri = 0; ri < l; ++ri) \
+	       ScreenAttrs[off + (p) + ri] = v; \
+    } while (0)
+#else
+# define RL_MEMSET(p, v, l) \
+    do { \
+	for (ri = 0; ri < l; ++ri) \
+	    ScreenAttrs[off + (p) + ri] = v; \
+    } while (0)
+#endif
+
+    // Set all attributes of the 'number' or 'relativenumber' column and the
+    // text
+    RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
+
+#ifdef FEAT_SIGNS
+    // If signs are being displayed, add two spaces.
+    if (signcolumn_on(wp))
+    {
+	len = wp->w_width - col;
+	if (len > 0)
+	{
+	    if (len > 2)
+		len = 2;
+# ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+		// the line number isn't reversed
+		copy_text_attr(off + wp->w_width - len - col,
+					(char_u *)"  ", len, HL_ATTR(HLF_FL));
+	    else
+# endif
+		copy_text_attr(off + col, (char_u *)"  ", len, HL_ATTR(HLF_FL));
+	    col += len;
+	}
+    }
+#endif
+
+    // 3. Add the 'number' or 'relativenumber' column
+    if (wp->w_p_nu || wp->w_p_rnu)
+    {
+	len = wp->w_width - col;
+	if (len > 0)
+	{
+	    int	    w = number_width(wp);
+	    long    num;
+	    char    *fmt = "%*ld ";
+
+	    if (len > w + 1)
+		len = w + 1;
+
+	    if (wp->w_p_nu && !wp->w_p_rnu)
+		// 'number' + 'norelativenumber'
+		num = (long)lnum;
+	    else
+	    {
+		// 'relativenumber', don't use negative numbers
+		num = labs((long)get_cursor_rel_lnum(wp, lnum));
+		if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
+		{
+		    // 'number' + 'relativenumber': cursor line shows absolute
+		    // line number
+		    num = lnum;
+		    fmt = "%-*ld ";
+		}
+	    }
+
+	    sprintf((char *)buf, fmt, w, num);
+#ifdef FEAT_RIGHTLEFT
+	    if (wp->w_p_rl)
+		// the line number isn't reversed
+		copy_text_attr(off + wp->w_width - len - col, buf, len,
+							     HL_ATTR(HLF_FL));
+	    else
+#endif
+		copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
+	    col += len;
+	}
+    }
+
+    // 4. Compose the folded-line string with 'foldtext', if set.
+    text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
+
+    txtcol = col;	// remember where text starts
+
+    // 5. move the text to current_ScreenLine.  Fill up with "fill_fold".
+    //    Right-left text is put in columns 0 - number-col, normal text is put
+    //    in columns number-col - window-width.
+    col = text_to_screenline(wp, text, col);
+
+    // Fill the rest of the line with the fold filler
+#ifdef FEAT_RIGHTLEFT
+    if (wp->w_p_rl)
+	col -= txtcol;
+#endif
+    while (col < wp->w_width
+#ifdef FEAT_RIGHTLEFT
+		    - (wp->w_p_rl ? txtcol : 0)
+#endif
+	    )
+    {
+	if (enc_utf8)
+	{
+	    if (fill_fold >= 0x80)
+	    {
+		ScreenLinesUC[off + col] = fill_fold;
+		ScreenLinesC[0][off + col] = 0;
+		ScreenLines[off + col] = 0x80; // avoid storing zero
+	    }
+	    else
+	    {
+		ScreenLinesUC[off + col] = 0;
+		ScreenLines[off + col] = fill_fold;
+	    }
+	    col++;
+	}
+	else
+	    ScreenLines[off + col++] = fill_fold;
+    }
+
+    if (text != buf)
+	vim_free(text);
+
+    // 6. set highlighting for the Visual area an other text.
+    // If all folded lines are in the Visual area, highlight the line.
+    if (VIsual_active && wp->w_buffer == curwin->w_buffer)
+    {
+	if (LTOREQ_POS(curwin->w_cursor, VIsual))
+	{
+	    // Visual is after curwin->w_cursor
+	    top = &curwin->w_cursor;
+	    bot = &VIsual;
+	}
+	else
+	{
+	    // Visual is before curwin->w_cursor
+	    top = &VIsual;
+	    bot = &curwin->w_cursor;
+	}
+	if (lnum >= top->lnum
+		&& lnume <= bot->lnum
+		&& (VIsual_mode != 'v'
+		    || ((lnum > top->lnum
+			    || (lnum == top->lnum
+				&& top->col == 0))
+			&& (lnume < bot->lnum
+			    || (lnume == bot->lnum
+				&& (bot->col - (*p_sel == 'e'))
+		>= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
+	{
+	    if (VIsual_mode == Ctrl_V)
+	    {
+		// Visual block mode: highlight the chars part of the block
+		if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
+		{
+		    if (wp->w_old_cursor_lcol != MAXCOL
+			     && wp->w_old_cursor_lcol + txtcol
+						       < (colnr_T)wp->w_width)
+			len = wp->w_old_cursor_lcol;
+		    else
+			len = wp->w_width - txtcol;
+		    RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
+					    len - (int)wp->w_old_cursor_fcol);
+		}
+	    }
+	    else
+	    {
+		// Set all attributes of the text
+		RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
+	    }
+	}
+    }
+
+#ifdef FEAT_SYN_HL
+    // Show colorcolumn in the fold line, but let cursorcolumn override it.
+    if (wp->w_p_cc_cols)
+    {
+	int i = 0;
+	int j = wp->w_p_cc_cols[i];
+	int old_txtcol = txtcol;
+
+	while (j > -1)
+	{
+	    txtcol += j;
+	    if (wp->w_p_wrap)
+		txtcol -= wp->w_skipcol;
+	    else
+		txtcol -= wp->w_leftcol;
+	    if (txtcol >= 0 && txtcol < wp->w_width)
+		ScreenAttrs[off + txtcol] = hl_combine_attr(
+				    ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
+	    txtcol = old_txtcol;
+	    j = wp->w_p_cc_cols[++i];
+	}
+    }
+
+    // Show 'cursorcolumn' in the fold line.
+    if (wp->w_p_cuc)
+    {
+	txtcol += wp->w_virtcol;
+	if (wp->w_p_wrap)
+	    txtcol -= wp->w_skipcol;
+	else
+	    txtcol -= wp->w_leftcol;
+	if (txtcol >= 0 && txtcol < wp->w_width)
+	    ScreenAttrs[off + txtcol] = hl_combine_attr(
+				 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
+    }
+#endif
+
+    screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
+						     (int)wp->w_width, 0);
+
+    // Update w_cline_height and w_cline_folded if the cursor line was
+    // updated (saves a call to plines() later).
+    if (wp == curwin
+	    && lnum <= curwin->w_cursor.lnum
+	    && lnume >= curwin->w_cursor.lnum)
+    {
+	curwin->w_cline_row = row;
+	curwin->w_cline_height = 1;
+	curwin->w_cline_folded = TRUE;
+	curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
+    }
+}
+#endif
+
+/*
+ * Update a single window.
+ *
+ * This may cause the windows below it also to be redrawn (when clearing the
+ * screen or scrolling lines).
+ *
+ * How the window is redrawn depends on wp->w_redr_type.  Each type also
+ * implies the one below it.
+ * NOT_VALID	redraw the whole window
+ * SOME_VALID	redraw the whole window but do scroll when possible
+ * REDRAW_TOP	redraw the top w_upd_rows window lines, otherwise like VALID
+ * INVERTED	redraw the changed part of the Visual area
+ * INVERTED_ALL	redraw the whole Visual area
+ * VALID	1. scroll up/down to adjust for a changed w_topline
+ *		2. update lines at the top when scrolled down
+ *		3. redraw changed text:
+ *		   - if wp->w_buffer->b_mod_set set, update lines between
+ *		     b_mod_top and b_mod_bot.
+ *		   - if wp->w_redraw_top non-zero, redraw lines between
+ *		     wp->w_redraw_top and wp->w_redr_bot.
+ *		   - continue redrawing when syntax status is invalid.
+ *		4. if scrolled up, update lines at the bottom.
+ * This results in three areas that may need updating:
+ * top:	from first row to top_end (when scrolled down)
+ * mid: from mid_start to mid_end (update inversion or changed text)
+ * bot: from bot_start to last row (when scrolled up)
+ */
+    static void
+win_update(win_T *wp)
+{
+    buf_T	*buf = wp->w_buffer;
+    int		type;
+    int		top_end = 0;	// Below last row of the top area that needs
+				// updating.  0 when no top area updating.
+    int		mid_start = 999;// first row of the mid area that needs
+				// updating.  999 when no mid area updating.
+    int		mid_end = 0;	// Below last row of the mid area that needs
+				// updating.  0 when no mid area updating.
+    int		bot_start = 999;// first row of the bot area that needs
+				// updating.  999 when no bot area updating
+    int		scrolled_down = FALSE;	// TRUE when scrolled down when
+					// w_topline got smaller a bit
+#ifdef FEAT_SEARCH_EXTRA
+    int		top_to_mod = FALSE;    // redraw above mod_top
+#endif
+
+    int		row;		// current window row to display
+    linenr_T	lnum;		// current buffer lnum to display
+    int		idx;		// current index in w_lines[]
+    int		srow;		// starting row of the current line
+
+    int		eof = FALSE;	// if TRUE, we hit the end of the file
+    int		didline = FALSE; // if TRUE, we finished the last line
+    int		i;
+    long	j;
+    static int	recursive = FALSE;	// being called recursively
+    int		old_botline = wp->w_botline;
+#ifdef FEAT_FOLDING
+    long	fold_count;
+#endif
+#ifdef FEAT_SYN_HL
+    // remember what happened to the previous line, to know if
+    // check_visual_highlight() can be used
+#define DID_NONE 1	// didn't update a line
+#define DID_LINE 2	// updated a normal line
+#define DID_FOLD 3	// updated a folded line
+    int		did_update = DID_NONE;
+    linenr_T	syntax_last_parsed = 0;		// last parsed text line
+#endif
+    linenr_T	mod_top = 0;
+    linenr_T	mod_bot = 0;
+#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
+    int		save_got_int;
+#endif
+#ifdef SYN_TIME_LIMIT
+    proftime_T	syntax_tm;
+#endif
+
+    type = wp->w_redr_type;
+
+    if (type == NOT_VALID)
+    {
+	wp->w_redr_status = TRUE;
+	wp->w_lines_valid = 0;
+    }
+
+    // Window is zero-height: nothing to draw.
+    if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
+    {
+	wp->w_redr_type = 0;
+	return;
+    }
+
+    // Window is zero-width: Only need to draw the separator.
+    if (wp->w_width == 0)
+    {
+	// draw the vertical separator right of this window
+	draw_vsep_win(wp, 0);
+	wp->w_redr_type = 0;
+	return;
+    }
+
+#ifdef FEAT_TERMINAL
+    // If this window contains a terminal, redraw works completely differently.
+    if (term_do_update_window(wp))
+    {
+	term_update_window(wp);
+# ifdef FEAT_MENU
+	// Draw the window toolbar, if there is one.
+	if (winbar_height(wp) > 0)
+	    redraw_win_toolbar(wp);
+# endif
+	wp->w_redr_type = 0;
+	return;
+    }
+#endif
+
+#ifdef FEAT_SEARCH_EXTRA
+    init_search_hl(wp, &screen_search_hl);
+#endif
+
+#ifdef FEAT_LINEBREAK
+    // Force redraw when width of 'number' or 'relativenumber' column
+    // changes.
+    i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
+    if (wp->w_nrwidth != i)
+    {
+	type = NOT_VALID;
+	wp->w_nrwidth = i;
+    }
+    else
+#endif
+
+    if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
+    {
+	// When there are both inserted/deleted lines and specific lines to be
+	// redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
+	// everything (only happens when redrawing is off for while).
+	type = NOT_VALID;
+    }
+    else
+    {
+	// Set mod_top to the first line that needs displaying because of
+	// changes.  Set mod_bot to the first line after the changes.
+	mod_top = wp->w_redraw_top;
+	if (wp->w_redraw_bot != 0)
+	    mod_bot = wp->w_redraw_bot + 1;
+	else
+	    mod_bot = 0;
+	if (buf->b_mod_set)
+	{
+	    if (mod_top == 0 || mod_top > buf->b_mod_top)
+	    {
+		mod_top = buf->b_mod_top;
+#ifdef FEAT_SYN_HL
+		// Need to redraw lines above the change that may be included
+		// in a pattern match.
+		if (syntax_present(wp))
+		{
+		    mod_top -= buf->b_s.b_syn_sync_linebreaks;
+		    if (mod_top < 1)
+			mod_top = 1;
+		}
+#endif
+	    }
+	    if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
+		mod_bot = buf->b_mod_bot;
+
+#ifdef FEAT_SEARCH_EXTRA
+	    // When 'hlsearch' is on and using a multi-line search pattern, a
+	    // change in one line may make the Search highlighting in a
+	    // previous line invalid.  Simple solution: redraw all visible
+	    // lines above the change.
+	    // Same for a match pattern.
+	    if (screen_search_hl.rm.regprog != NULL
+		    && re_multiline(screen_search_hl.rm.regprog))
+		top_to_mod = TRUE;
+	    else
+	    {
+		matchitem_T *cur = wp->w_match_head;
+
+		while (cur != NULL)
+		{
+		    if (cur->match.regprog != NULL
+					   && re_multiline(cur->match.regprog))
+		    {
+			top_to_mod = TRUE;
+			break;
+		    }
+		    cur = cur->next;
+		}
+	    }
+#endif
+	}
+#ifdef FEAT_FOLDING
+	if (mod_top != 0 && hasAnyFolding(wp))
+	{
+	    linenr_T	lnumt, lnumb;
+
+	    // A change in a line can cause lines above it to become folded or
+	    // unfolded.  Find the top most buffer line that may be affected.
+	    // If the line was previously folded and displayed, get the first
+	    // line of that fold.  If the line is folded now, get the first
+	    // folded line.  Use the minimum of these two.
+
+	    // Find last valid w_lines[] entry above mod_top.  Set lnumt to
+	    // the line below it.  If there is no valid entry, use w_topline.
+	    // Find the first valid w_lines[] entry below mod_bot.  Set lnumb
+	    // to this line.  If there is no valid entry, use MAXLNUM.
+	    lnumt = wp->w_topline;
+	    lnumb = MAXLNUM;
+	    for (i = 0; i < wp->w_lines_valid; ++i)
+		if (wp->w_lines[i].wl_valid)
+		{
+		    if (wp->w_lines[i].wl_lastlnum < mod_top)
+			lnumt = wp->w_lines[i].wl_lastlnum + 1;
+		    if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
+		    {
+			lnumb = wp->w_lines[i].wl_lnum;
+			// When there is a fold column it might need updating
+			// in the next line ("J" just above an open fold).
+			if (compute_foldcolumn(wp, 0) > 0)
+			    ++lnumb;
+		    }
+		}
+
+	    (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
+	    if (mod_top > lnumt)
+		mod_top = lnumt;
+
+	    // Now do the same for the bottom line (one above mod_bot).
+	    --mod_bot;
+	    (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
+	    ++mod_bot;
+	    if (mod_bot < lnumb)
+		mod_bot = lnumb;
+	}
+#endif
+
+	// When a change starts above w_topline and the end is below
+	// w_topline, start redrawing at w_topline.
+	// If the end of the change is above w_topline: do like no change was
+	// made, but redraw the first line to find changes in syntax.
+	if (mod_top != 0 && mod_top < wp->w_topline)
+	{
+	    if (mod_bot > wp->w_topline)
+		mod_top = wp->w_topline;
+#ifdef FEAT_SYN_HL
+	    else if (syntax_present(wp))
+		top_end = 1;
+#endif
+	}
+
+	// When line numbers are displayed need to redraw all lines below
+	// inserted/deleted lines.
+	if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
+	    mod_bot = MAXLNUM;
+    }
+    wp->w_redraw_top = 0;	// reset for next time
+    wp->w_redraw_bot = 0;
+
+    // When only displaying the lines at the top, set top_end.  Used when
+    // window has scrolled down for msg_scrolled.
+    if (type == REDRAW_TOP)
+    {
+	j = 0;
+	for (i = 0; i < wp->w_lines_valid; ++i)
+	{
+	    j += wp->w_lines[i].wl_size;
+	    if (j >= wp->w_upd_rows)
+	    {
+		top_end = j;
+		break;
+	    }
+	}
+	if (top_end == 0)
+	    // not found (cannot happen?): redraw everything
+	    type = NOT_VALID;
+	else
+	    // top area defined, the rest is VALID
+	    type = VALID;
+    }
+
+    // Trick: we want to avoid clearing the screen twice.  screenclear() will
+    // set "screen_cleared" to TRUE.  The special value MAYBE (which is still
+    // non-zero and thus not FALSE) will indicate that screenclear() was not
+    // called.
+    if (screen_cleared)
+	screen_cleared = MAYBE;
+
+    // If there are no changes on the screen that require a complete redraw,
+    // handle three cases:
+    // 1: we are off the top of the screen by a few lines: scroll down
+    // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
+    // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
+    //    w_lines[] that needs updating.
+    if ((type == VALID || type == SOME_VALID
+				  || type == INVERTED || type == INVERTED_ALL)
+#ifdef FEAT_DIFF
+	    && !wp->w_botfill && !wp->w_old_botfill
+#endif
+	    )
+    {
+	if (mod_top != 0 && wp->w_topline == mod_top)
+	{
+	    // w_topline is the first changed line, the scrolling will be done
+	    // further down.
+	}
+	else if (wp->w_lines[0].wl_valid
+		&& (wp->w_topline < wp->w_lines[0].wl_lnum
+#ifdef FEAT_DIFF
+		    || (wp->w_topline == wp->w_lines[0].wl_lnum
+			&& wp->w_topfill > wp->w_old_topfill)
+#endif
+		   ))
+	{
+	    // New topline is above old topline: May scroll down.
+#ifdef FEAT_FOLDING
+	    if (hasAnyFolding(wp))
+	    {
+		linenr_T ln;
+
+		// count the number of lines we are off, counting a sequence
+		// of folded lines as one
+		j = 0;
+		for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
+		{
+		    ++j;
+		    if (j >= wp->w_height - 2)
+			break;
+		    (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
+		}
+	    }
+	    else
+#endif
+		j = wp->w_lines[0].wl_lnum - wp->w_topline;
+	    if (j < wp->w_height - 2)		// not too far off
+	    {
+		i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
+#ifdef FEAT_DIFF
+		// insert extra lines for previously invisible filler lines
+		if (wp->w_lines[0].wl_lnum != wp->w_topline)
+		    i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
+							  - wp->w_old_topfill;
+#endif
+		if (i < wp->w_height - 2)	// less than a screen off
+		{
+		    // Try to insert the correct number of lines.
+		    // If not the last window, delete the lines at the bottom.
+		    // win_ins_lines may fail when the terminal can't do it.
+		    if (i > 0)
+			check_for_delay(FALSE);
+		    if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
+		    {
+			if (wp->w_lines_valid != 0)
+			{
+			    // Need to update rows that are new, stop at the
+			    // first one that scrolled down.
+			    top_end = i;
+			    scrolled_down = TRUE;
+
+			    // Move the entries that were scrolled, disable
+			    // the entries for the lines to be redrawn.
+			    if ((wp->w_lines_valid += j) > wp->w_height)
+				wp->w_lines_valid = wp->w_height;
+			    for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
+				wp->w_lines[idx] = wp->w_lines[idx - j];
+			    while (idx >= 0)
+				wp->w_lines[idx--].wl_valid = FALSE;
+			}
+		    }
+		    else
+			mid_start = 0;		// redraw all lines
+		}
+		else
+		    mid_start = 0;		// redraw all lines
+	    }
+	    else
+		mid_start = 0;		// redraw all lines
+	}
+	else
+	{
+	    // New topline is at or below old topline: May scroll up.
+	    // When topline didn't change, find first entry in w_lines[] that
+	    // needs updating.
+
+	    // try to find wp->w_topline in wp->w_lines[].wl_lnum
+	    j = -1;
+	    row = 0;
+	    for (i = 0; i < wp->w_lines_valid; i++)
+	    {
+		if (wp->w_lines[i].wl_valid
+			&& wp->w_lines[i].wl_lnum == wp->w_topline)
+		{
+		    j = i;
+		    break;
+		}
+		row += wp->w_lines[i].wl_size;
+	    }
+	    if (j == -1)
+	    {
+		// if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
+		// lines
+		mid_start = 0;
+	    }
+	    else
+	    {
+		// Try to delete the correct number of lines.
+		// wp->w_topline is at wp->w_lines[i].wl_lnum.
+#ifdef FEAT_DIFF
+		// If the topline didn't change, delete old filler lines,
+		// otherwise delete filler lines of the new topline...
+		if (wp->w_lines[0].wl_lnum == wp->w_topline)
+		    row += wp->w_old_topfill;
+		else
+		    row += diff_check_fill(wp, wp->w_topline);
+		// ... but don't delete new filler lines.
+		row -= wp->w_topfill;
+#endif
+		if (row > 0)
+		{
+		    check_for_delay(FALSE);
+		    if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
+									 == OK)
+			bot_start = wp->w_height - row;
+		    else
+			mid_start = 0;		// redraw all lines
+		}
+		if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
+		{
+		    // Skip the lines (below the deleted lines) that are still
+		    // valid and don't need redrawing.	Copy their info
+		    // upwards, to compensate for the deleted lines.  Set
+		    // bot_start to the first row that needs redrawing.
+		    bot_start = 0;
+		    idx = 0;
+		    for (;;)
+		    {
+			wp->w_lines[idx] = wp->w_lines[j];
+			// stop at line that didn't fit, unless it is still
+			// valid (no lines deleted)
+			if (row > 0 && bot_start + row
+				 + (int)wp->w_lines[j].wl_size > wp->w_height)
+			{
+			    wp->w_lines_valid = idx + 1;
+			    break;
+			}
+			bot_start += wp->w_lines[idx++].wl_size;
+
+			// stop at the last valid entry in w_lines[].wl_size
+			if (++j >= wp->w_lines_valid)
+			{
+			    wp->w_lines_valid = idx;
+			    break;
+			}
+		    }
+#ifdef FEAT_DIFF
+		    // Correct the first entry for filler lines at the top
+		    // when it won't get updated below.
+		    if (wp->w_p_diff && bot_start > 0)
+			wp->w_lines[0].wl_size =
+			    plines_win_nofill(wp, wp->w_topline, TRUE)
+							      + wp->w_topfill;
+#endif
+		}
+	    }
+	}
+
+	// When starting redraw in the first line, redraw all lines.  When
+	// there is only one window it's probably faster to clear the screen
+	// first.
+	if (mid_start == 0)
+	{
+	    mid_end = wp->w_height;
+	    if (ONE_WINDOW && !WIN_IS_POPUP(wp))
+	    {
+		// Clear the screen when it was not done by win_del_lines() or
+		// win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
+		// then.
+		if (screen_cleared != TRUE)
+		    screenclear();
+		// The screen was cleared, redraw the tab pages line.
+		if (redraw_tabline)
+		    draw_tabline();
+	    }
+	}
+
+	// When win_del_lines() or win_ins_lines() caused the screen to be
+	// cleared (only happens for the first window) or when screenclear()
+	// was called directly above, "must_redraw" will have been set to
+	// NOT_VALID, need to reset it here to avoid redrawing twice.
+	if (screen_cleared == TRUE)
+	    must_redraw = 0;
+    }
+    else
+    {
+	// Not VALID or INVERTED: redraw all lines.
+	mid_start = 0;
+	mid_end = wp->w_height;
+    }
+
+    if (type == SOME_VALID)
+    {
+	// SOME_VALID: redraw all lines.
+	mid_start = 0;
+	mid_end = wp->w_height;
+	type = NOT_VALID;
+    }
+
+    // check if we are updating or removing the inverted part
+    if ((VIsual_active && buf == curwin->w_buffer)
+	    || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
+    {
+	linenr_T    from, to;
+
+	if (VIsual_active)
+	{
+	    if (VIsual_active
+		    && (VIsual_mode != wp->w_old_visual_mode
+			|| type == INVERTED_ALL))
+	    {
+		// If the type of Visual selection changed, redraw the whole
+		// selection.  Also when the ownership of the X selection is
+		// gained or lost.
+		if (curwin->w_cursor.lnum < VIsual.lnum)
+		{
+		    from = curwin->w_cursor.lnum;
+		    to = VIsual.lnum;
+		}
+		else
+		{
+		    from = VIsual.lnum;
+		    to = curwin->w_cursor.lnum;
+		}
+		// redraw more when the cursor moved as well
+		if (wp->w_old_cursor_lnum < from)
+		    from = wp->w_old_cursor_lnum;
+		if (wp->w_old_cursor_lnum > to)
+		    to = wp->w_old_cursor_lnum;
+		if (wp->w_old_visual_lnum < from)
+		    from = wp->w_old_visual_lnum;
+		if (wp->w_old_visual_lnum > to)
+		    to = wp->w_old_visual_lnum;
+	    }
+	    else
+	    {
+		// Find the line numbers that need to be updated: The lines
+		// between the old cursor position and the current cursor
+		// position.  Also check if the Visual position changed.
+		if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
+		{
+		    from = curwin->w_cursor.lnum;
+		    to = wp->w_old_cursor_lnum;
+		}
+		else
+		{
+		    from = wp->w_old_cursor_lnum;
+		    to = curwin->w_cursor.lnum;
+		    if (from == 0)	// Visual mode just started
+			from = to;
+		}
+
+		if (VIsual.lnum != wp->w_old_visual_lnum
+					|| VIsual.col != wp->w_old_visual_col)
+		{
+		    if (wp->w_old_visual_lnum < from
+						&& wp->w_old_visual_lnum != 0)
+			from = wp->w_old_visual_lnum;
+		    if (wp->w_old_visual_lnum > to)
+			to = wp->w_old_visual_lnum;
+		    if (VIsual.lnum < from)
+			from = VIsual.lnum;
+		    if (VIsual.lnum > to)
+			to = VIsual.lnum;
+		}
+	    }
+
+	    // If in block mode and changed column or curwin->w_curswant:
+	    // update all lines.
+	    // First compute the actual start and end column.
+	    if (VIsual_mode == Ctrl_V)
+	    {
+		colnr_T	    fromc, toc;
+#if defined(FEAT_LINEBREAK)
+		int	    save_ve_flags = ve_flags;
+
+		if (curwin->w_p_lbr)
+		    ve_flags = VE_ALL;
+#endif
+		getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
+#if defined(FEAT_LINEBREAK)
+		ve_flags = save_ve_flags;
+#endif
+		++toc;
+		if (curwin->w_curswant == MAXCOL)
+		    toc = MAXCOL;
+
+		if (fromc != wp->w_old_cursor_fcol
+			|| toc != wp->w_old_cursor_lcol)
+		{
+		    if (from > VIsual.lnum)
+			from = VIsual.lnum;
+		    if (to < VIsual.lnum)
+			to = VIsual.lnum;
+		}
+		wp->w_old_cursor_fcol = fromc;
+		wp->w_old_cursor_lcol = toc;
+	    }
+	}
+	else
+	{
+	    // Use the line numbers of the old Visual area.
+	    if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
+	    {
+		from = wp->w_old_cursor_lnum;
+		to = wp->w_old_visual_lnum;
+	    }
+	    else
+	    {
+		from = wp->w_old_visual_lnum;
+		to = wp->w_old_cursor_lnum;
+	    }
+	}
+
+	// There is no need to update lines above the top of the window.
+	if (from < wp->w_topline)
+	    from = wp->w_topline;
+
+	// If we know the value of w_botline, use it to restrict the update to
+	// the lines that are visible in the window.
+	if (wp->w_valid & VALID_BOTLINE)
+	{
+	    if (from >= wp->w_botline)
+		from = wp->w_botline - 1;
+	    if (to >= wp->w_botline)
+		to = wp->w_botline - 1;
+	}
+
+	// Find the minimal part to be updated.
+	// Watch out for scrolling that made entries in w_lines[] invalid.
+	// E.g., CTRL-U makes the first half of w_lines[] invalid and sets
+	// top_end; need to redraw from top_end to the "to" line.
+	// A middle mouse click with a Visual selection may change the text
+	// above the Visual area and reset wl_valid, do count these for
+	// mid_end (in srow).
+	if (mid_start > 0)
+	{
+	    lnum = wp->w_topline;
+	    idx = 0;
+	    srow = 0;
+	    if (scrolled_down)
+		mid_start = top_end;
+	    else
+		mid_start = 0;
+	    while (lnum < from && idx < wp->w_lines_valid)	// find start
+	    {
+		if (wp->w_lines[idx].wl_valid)
+		    mid_start += wp->w_lines[idx].wl_size;
+		else if (!scrolled_down)
+		    srow += wp->w_lines[idx].wl_size;
+		++idx;
+# ifdef FEAT_FOLDING
+		if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
+		    lnum = wp->w_lines[idx].wl_lnum;
+		else
+# endif
+		    ++lnum;
+	    }
+	    srow += mid_start;
+	    mid_end = wp->w_height;
+	    for ( ; idx < wp->w_lines_valid; ++idx)		// find end
+	    {
+		if (wp->w_lines[idx].wl_valid
+			&& wp->w_lines[idx].wl_lnum >= to + 1)
+		{
+		    // Only update until first row of this line
+		    mid_end = srow;
+		    break;
+		}
+		srow += wp->w_lines[idx].wl_size;
+	    }
+	}
+    }
+
+    if (VIsual_active && buf == curwin->w_buffer)
+    {
+	wp->w_old_visual_mode = VIsual_mode;
+	wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
+	wp->w_old_visual_lnum = VIsual.lnum;
+	wp->w_old_visual_col = VIsual.col;
+	wp->w_old_curswant = curwin->w_curswant;
+    }
+    else
+    {
+	wp->w_old_visual_mode = 0;
+	wp->w_old_cursor_lnum = 0;
+	wp->w_old_visual_lnum = 0;
+	wp->w_old_visual_col = 0;
+    }
+
+#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
+    // reset got_int, otherwise regexp won't work
+    save_got_int = got_int;
+    got_int = 0;
+#endif
+#ifdef SYN_TIME_LIMIT
+    // Set the time limit to 'redrawtime'.
+    profile_setlimit(p_rdt, &syntax_tm);
+    syn_set_timeout(&syntax_tm);
+#endif
+#ifdef FEAT_FOLDING
+    win_foldinfo.fi_level = 0;
+#endif
+
+#ifdef FEAT_MENU
+    // Draw the window toolbar, if there is one.
+    // TODO: only when needed.
+    if (winbar_height(wp) > 0)
+	redraw_win_toolbar(wp);
+#endif
+
+    // Update all the window rows.
+    idx = 0;		// first entry in w_lines[].wl_size
+    row = 0;
+    srow = 0;
+    lnum = wp->w_topline;	// first line shown in window
+    for (;;)
+    {
+	// stop updating when reached the end of the window (check for _past_
+	// the end of the window is at the end of the loop)
+	if (row == wp->w_height)
+	{
+	    didline = TRUE;
+	    break;
+	}
+
+	// stop updating when hit the end of the file
+	if (lnum > buf->b_ml.ml_line_count)
+	{
+	    eof = TRUE;
+	    break;
+	}
+
+	// Remember the starting row of the line that is going to be dealt
+	// with.  It is used further down when the line doesn't fit.
+	srow = row;
+
+	// Update a line when it is in an area that needs updating, when it
+	// has changes or w_lines[idx] is invalid.
+	// "bot_start" may be halfway a wrapped line after using
+	// win_del_lines(), check if the current line includes it.
+	// When syntax folding is being used, the saved syntax states will
+	// already have been updated, we can't see where the syntax state is
+	// the same again, just update until the end of the window.
+	if (row < top_end
+		|| (row >= mid_start && row < mid_end)
+#ifdef FEAT_SEARCH_EXTRA
+		|| top_to_mod
+#endif
+		|| idx >= wp->w_lines_valid
+		|| (row + wp->w_lines[idx].wl_size > bot_start)
+		|| (mod_top != 0
+		    && (lnum == mod_top
+			|| (lnum >= mod_top
+			    && (lnum < mod_bot
+#ifdef FEAT_SYN_HL
+				|| did_update == DID_FOLD
+				|| (did_update == DID_LINE
+				    && syntax_present(wp)
+				    && (
+# ifdef FEAT_FOLDING
+					(foldmethodIsSyntax(wp)
+						      && hasAnyFolding(wp)) ||
+# endif
+					syntax_check_changed(lnum)))
+#endif
+#ifdef FEAT_SEARCH_EXTRA
+				// match in fixed position might need redraw
+				// if lines were inserted or deleted
+				|| (wp->w_match_head != NULL
+						    && buf->b_mod_xlines != 0)
+#endif
+				)))))
+	{
+#ifdef FEAT_SEARCH_EXTRA
+	    if (lnum == mod_top)
+		top_to_mod = FALSE;
+#endif
+
+	    // When at start of changed lines: May scroll following lines
+	    // up or down to minimize redrawing.
+	    // Don't do this when the change continues until the end.
+	    // Don't scroll when dollar_vcol >= 0, keep the "$".
+	    if (lnum == mod_top
+		    && mod_bot != MAXLNUM
+		    && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
+	    {
+		int		old_rows = 0;
+		int		new_rows = 0;
+		int		xtra_rows;
+		linenr_T	l;
+
+		// Count the old number of window rows, using w_lines[], which
+		// should still contain the sizes for the lines as they are
+		// currently displayed.
+		for (i = idx; i < wp->w_lines_valid; ++i)
+		{
+		    // Only valid lines have a meaningful wl_lnum.  Invalid
+		    // lines are part of the changed area.
+		    if (wp->w_lines[i].wl_valid
+			    && wp->w_lines[i].wl_lnum == mod_bot)
+			break;
+		    old_rows += wp->w_lines[i].wl_size;
+#ifdef FEAT_FOLDING
+		    if (wp->w_lines[i].wl_valid
+			    && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
+		    {
+			// Must have found the last valid entry above mod_bot.
+			// Add following invalid entries.
+			++i;
+			while (i < wp->w_lines_valid
+						  && !wp->w_lines[i].wl_valid)
+			    old_rows += wp->w_lines[i++].wl_size;
+			break;
+		    }
+#endif
+		}
+
+		if (i >= wp->w_lines_valid)
+		{
+		    // We can't find a valid line below the changed lines,
+		    // need to redraw until the end of the window.
+		    // Inserting/deleting lines has no use.
+		    bot_start = 0;
+		}
+		else
+		{
+		    // Able to count old number of rows: Count new window
+		    // rows, and may insert/delete lines
+		    j = idx;
+		    for (l = lnum; l < mod_bot; ++l)
+		    {
+#ifdef FEAT_FOLDING
+			if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
+			    ++new_rows;
+			else
+#endif
+#ifdef FEAT_DIFF
+			    if (l == wp->w_topline)
+			    new_rows += plines_win_nofill(wp, l, TRUE)
+							      + wp->w_topfill;
+			else
+#endif
+			    new_rows += plines_win(wp, l, TRUE);
+			++j;
+			if (new_rows > wp->w_height - row - 2)
+			{
+			    // it's getting too much, must redraw the rest
+			    new_rows = 9999;
+			    break;
+			}
+		    }
+		    xtra_rows = new_rows - old_rows;
+		    if (xtra_rows < 0)
+		    {
+			// May scroll text up.  If there is not enough
+			// remaining text or scrolling fails, must redraw the
+			// rest.  If scrolling works, must redraw the text
+			// below the scrolled text.
+			if (row - xtra_rows >= wp->w_height - 2)
+			    mod_bot = MAXLNUM;
+			else
+			{
+			    check_for_delay(FALSE);
+			    if (win_del_lines(wp, row,
+					  -xtra_rows, FALSE, FALSE, 0) == FAIL)
+				mod_bot = MAXLNUM;
+			    else
+				bot_start = wp->w_height + xtra_rows;
+			}
+		    }
+		    else if (xtra_rows > 0)
+		    {
+			// May scroll text down.  If there is not enough
+			// remaining text of scrolling fails, must redraw the
+			// rest.
+			if (row + xtra_rows >= wp->w_height - 2)
+			    mod_bot = MAXLNUM;
+			else
+			{
+			    check_for_delay(FALSE);
+			    if (win_ins_lines(wp, row + old_rows,
+					     xtra_rows, FALSE, FALSE) == FAIL)
+				mod_bot = MAXLNUM;
+			    else if (top_end > row + old_rows)
+				// Scrolled the part at the top that requires
+				// updating down.
+				top_end += xtra_rows;
+			}
+		    }
+
+		    // When not updating the rest, may need to move w_lines[]
+		    // entries.
+		    if (mod_bot != MAXLNUM && i != j)
+		    {
+			if (j < i)
+			{
+			    int x = row + new_rows;
+
+			    // move entries in w_lines[] upwards
+			    for (;;)
+			    {
+				// stop at last valid entry in w_lines[]
+				if (i >= wp->w_lines_valid)
+				{
+				    wp->w_lines_valid = j;
+				    break;
+				}
+				wp->w_lines[j] = wp->w_lines[i];
+				// stop at a line that won't fit
+				if (x + (int)wp->w_lines[j].wl_size
+							   > wp->w_height)
+				{
+				    wp->w_lines_valid = j + 1;
+				    break;
+				}
+				x += wp->w_lines[j++].wl_size;
+				++i;
+			    }
+			    if (bot_start > x)
+				bot_start = x;
+			}
+			else // j > i
+			{
+			    // move entries in w_lines[] downwards
+			    j -= i;
+			    wp->w_lines_valid += j;
+			    if (wp->w_lines_valid > wp->w_height)
+				wp->w_lines_valid = wp->w_height;
+			    for (i = wp->w_lines_valid; i - j >= idx; --i)
+				wp->w_lines[i] = wp->w_lines[i - j];
+
+			    // The w_lines[] entries for inserted lines are
+			    // now invalid, but wl_size may be used above.
+			    // Reset to zero.
+			    while (i >= idx)
+			    {
+				wp->w_lines[i].wl_size = 0;
+				wp->w_lines[i--].wl_valid = FALSE;
+			    }
+			}
+		    }
+		}
+	    }
+
+#ifdef FEAT_FOLDING
+	    // When lines are folded, display one line for all of them.
+	    // Otherwise, display normally (can be several display lines when
+	    // 'wrap' is on).
+	    fold_count = foldedCount(wp, lnum, &win_foldinfo);
+	    if (fold_count != 0)
+	    {
+		fold_line(wp, fold_count, &win_foldinfo, lnum, row);
+		++row;
+		--fold_count;
+		wp->w_lines[idx].wl_folded = TRUE;
+		wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
+# ifdef FEAT_SYN_HL
+		did_update = DID_FOLD;
+# endif
+	    }
+	    else
+#endif
+	    if (idx < wp->w_lines_valid
+		    && wp->w_lines[idx].wl_valid
+		    && wp->w_lines[idx].wl_lnum == lnum
+		    && lnum > wp->w_topline
+		    && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
+		    && !WIN_IS_POPUP(wp)
+		    && srow + wp->w_lines[idx].wl_size > wp->w_height
+#ifdef FEAT_DIFF
+		    && diff_check_fill(wp, lnum) == 0
+#endif
+		    )
+	    {
+		// This line is not going to fit.  Don't draw anything here,
+		// will draw "@  " lines below.
+		row = wp->w_height + 1;
+	    }
+	    else
+	    {
+#ifdef FEAT_SEARCH_EXTRA
+		prepare_search_hl(wp, &screen_search_hl, lnum);
+#endif
+#ifdef FEAT_SYN_HL
+		// Let the syntax stuff know we skipped a few lines.
+		if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
+						       && syntax_present(wp))
+		    syntax_end_parsing(syntax_last_parsed + 1);
+#endif
+
+		// Display one line.
+		row = win_line(wp, lnum, srow, wp->w_height,
+							  mod_top == 0, FALSE);
+
+#ifdef FEAT_FOLDING
+		wp->w_lines[idx].wl_folded = FALSE;
+		wp->w_lines[idx].wl_lastlnum = lnum;
+#endif
+#ifdef FEAT_SYN_HL
+		did_update = DID_LINE;
+		syntax_last_parsed = lnum;
+#endif
+	    }
+
+	    wp->w_lines[idx].wl_lnum = lnum;
+	    wp->w_lines[idx].wl_valid = TRUE;
+
+	    // Past end of the window or end of the screen. Note that after
+	    // resizing wp->w_height may be end up too big. That's a problem
+	    // elsewhere, but prevent a crash here.
+	    if (row > wp->w_height || row + wp->w_winrow >= Rows)
+	    {
+		// we may need the size of that too long line later on
+		if (dollar_vcol == -1)
+		    wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
+		++idx;
+		break;
+	    }
+	    if (dollar_vcol == -1)
+		wp->w_lines[idx].wl_size = row - srow;
+	    ++idx;
+#ifdef FEAT_FOLDING
+	    lnum += fold_count + 1;
+#else
+	    ++lnum;
+#endif
+	}
+	else
+	{
+	    if (wp->w_p_rnu)
+	    {
+#ifdef FEAT_FOLDING
+		// 'relativenumber' set: The text doesn't need to be drawn, but
+		// the number column nearly always does.
+		fold_count = foldedCount(wp, lnum, &win_foldinfo);
+		if (fold_count != 0)
+		    fold_line(wp, fold_count, &win_foldinfo, lnum, row);
+		else
+#endif
+		    (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
+	    }
+
+	    // This line does not need to be drawn, advance to the next one.
+	    row += wp->w_lines[idx++].wl_size;
+	    if (row > wp->w_height)	// past end of screen
+		break;
+#ifdef FEAT_FOLDING
+	    lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
+#else
+	    ++lnum;
+#endif
+#ifdef FEAT_SYN_HL
+	    did_update = DID_NONE;
+#endif
+	}
+
+	if (lnum > buf->b_ml.ml_line_count)
+	{
+	    eof = TRUE;
+	    break;
+	}
+    }
+
+    // End of loop over all window lines.
+
+#ifdef FEAT_VTP
+    // Rewrite the character at the end of the screen line.
+    if (use_vtp())
+    {
+	int i;
+
+	for (i = 0; i < Rows; ++i)
+	    if (enc_utf8)
+		if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
+					   LineOffset[i] + screen_Columns) > 1)
+		    screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
+		else
+		    screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
+	    else
+		screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
+    }
+#endif
+
+    if (idx > wp->w_lines_valid)
+	wp->w_lines_valid = idx;
+
+#ifdef FEAT_SYN_HL
+    // Let the syntax stuff know we stop parsing here.
+    if (syntax_last_parsed != 0 && syntax_present(wp))
+	syntax_end_parsing(syntax_last_parsed + 1);
+#endif
+
+    // If we didn't hit the end of the file, and we didn't finish the last
+    // line we were working on, then the line didn't fit.
+    wp->w_empty_rows = 0;
+#ifdef FEAT_DIFF
+    wp->w_filler_rows = 0;
+#endif
+    if (!eof && !didline)
+    {
+	if (lnum == wp->w_topline)
+	{
+	    // Single line that does not fit!
+	    // Don't overwrite it, it can be edited.
+	    wp->w_botline = lnum + 1;
+	}
+#ifdef FEAT_DIFF
+	else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
+	{
+	    // Window ends in filler lines.
+	    wp->w_botline = lnum;
+	    wp->w_filler_rows = wp->w_height - srow;
+	}
+#endif
+#ifdef FEAT_TEXT_PROP
+	else if (WIN_IS_POPUP(wp))
+	{
+	    // popup line that doesn't fit is left as-is
+	    wp->w_botline = lnum;
+	}
+#endif
+	else if (dy_flags & DY_TRUNCATE)	// 'display' has "truncate"
+	{
+	    int scr_row = W_WINROW(wp) + wp->w_height - 1;
+
+	    // Last line isn't finished: Display "@@@" in the last screen line.
+	    screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
+							      HL_ATTR(HLF_AT));
+	    screen_fill(scr_row, scr_row + 1,
+		    (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
+		    '@', ' ', HL_ATTR(HLF_AT));
+	    set_empty_rows(wp, srow);
+	    wp->w_botline = lnum;
+	}
+	else if (dy_flags & DY_LASTLINE)	// 'display' has "lastline"
+	{
+	    // Last line isn't finished: Display "@@@" at the end.
+	    screen_fill(W_WINROW(wp) + wp->w_height - 1,
+		    W_WINROW(wp) + wp->w_height,
+		    (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
+		    '@', '@', HL_ATTR(HLF_AT));
+	    set_empty_rows(wp, srow);
+	    wp->w_botline = lnum;
+	}
+	else
+	{
+	    win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
+	    wp->w_botline = lnum;
+	}
+    }
+    else
+    {
+	draw_vsep_win(wp, row);
+	if (eof)		// we hit the end of the file
+	{
+	    wp->w_botline = buf->b_ml.ml_line_count + 1;
+#ifdef FEAT_DIFF
+	    j = diff_check_fill(wp, wp->w_botline);
+	    if (j > 0 && !wp->w_botfill)
+	    {
+		// Display filler lines at the end of the file.
+		if (char2cells(fill_diff) > 1)
+		    i = '-';
+		else
+		    i = fill_diff;
+		if (row + j > wp->w_height)
+		    j = wp->w_height - row;
+		win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
+		row += j;
+	    }
+#endif
+	}
+	else if (dollar_vcol == -1)
+	    wp->w_botline = lnum;
+
+	// Make sure the rest of the screen is blank
+	// put '~'s on rows that aren't part of the file.
+	win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
+				       ' ', FALSE, row, wp->w_height, HLF_EOB);
+    }
+
+#ifdef SYN_TIME_LIMIT
+    syn_set_timeout(NULL);
+#endif
+
+    // Reset the type of redrawing required, the window has been updated.
+    wp->w_redr_type = 0;
+#ifdef FEAT_DIFF
+    wp->w_old_topfill = wp->w_topfill;
+    wp->w_old_botfill = wp->w_botfill;
+#endif
+
+    if (dollar_vcol == -1)
+    {
+	// There is a trick with w_botline.  If we invalidate it on each
+	// change that might modify it, this will cause a lot of expensive
+	// calls to plines() in update_topline() each time.  Therefore the
+	// value of w_botline is often approximated, and this value is used to
+	// compute the value of w_topline.  If the value of w_botline was
+	// wrong, check that the value of w_topline is correct (cursor is on
+	// the visible part of the text).  If it's not, we need to redraw
+	// again.  Mostly this just means scrolling up a few lines, so it
+	// doesn't look too bad.  Only do this for the current window (where
+	// changes are relevant).
+	wp->w_valid |= VALID_BOTLINE;
+	if (wp == curwin && wp->w_botline != old_botline && !recursive)
+	{
+	    recursive = TRUE;
+	    curwin->w_valid &= ~VALID_TOPLINE;
+	    update_topline();	// may invalidate w_botline again
+	    if (must_redraw != 0)
+	    {
+		// Don't update for changes in buffer again.
+		i = curbuf->b_mod_set;
+		curbuf->b_mod_set = FALSE;
+		win_update(curwin);
+		must_redraw = 0;
+		curbuf->b_mod_set = i;
+	    }
+	    recursive = FALSE;
+	}
+    }
+
+#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
+    // restore got_int, unless CTRL-C was hit while redrawing
+    if (!got_int)
+	got_int = save_got_int;
+#endif
+}
+
+#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
+/*
+ * Prepare for updating one or more windows.
+ * Caller must check for "updating_screen" already set to avoid recursiveness.
+ */
+    static void
+update_prepare(void)
+{
+    cursor_off();
+    updating_screen = TRUE;
+#ifdef FEAT_GUI
+    // Remove the cursor before starting to do anything, because scrolling may
+    // make it difficult to redraw the text under it.
+    if (gui.in_use)
+	gui_undraw_cursor();
+#endif
+#ifdef FEAT_SEARCH_EXTRA
+    start_search_hl();
+#endif
+#ifdef FEAT_TEXT_PROP
+    // Update popup_mask if needed.
+    may_update_popup_mask(must_redraw);
+#endif
+}
+
+/*
+ * Finish updating one or more windows.
+ */
+    static void
+update_finish(void)
+{
+    if (redraw_cmdline || redraw_mode)
+	showmode();
+
+# ifdef FEAT_SEARCH_EXTRA
+    end_search_hl();
+# endif
+
+    after_updating_screen(TRUE);
+
+# ifdef FEAT_GUI
+    // Redraw the cursor and update the scrollbars when all screen updating is
+    // done.
+    if (gui.in_use)
+    {
+	out_flush_cursor(FALSE, FALSE);
+	gui_update_scrollbars(FALSE);
+    }
+# endif
+}
+#endif
+
+#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
+    void
+update_debug_sign(buf_T *buf, linenr_T lnum)
+{
+    win_T	*wp;
+    int		doit = FALSE;
+
+# ifdef FEAT_FOLDING
+    win_foldinfo.fi_level = 0;
+# endif
+
+    // update/delete a specific sign
+    redraw_buf_line_later(buf, lnum);
+
+    // check if it resulted in the need to redraw a window
+    FOR_ALL_WINDOWS(wp)
+	if (wp->w_redr_type != 0)
+	    doit = TRUE;
+
+    // Return when there is nothing to do, screen updating is already
+    // happening (recursive call), messages on the screen or still starting up.
+    if (!doit || updating_screen
+	    || State == ASKMORE || State == HITRETURN
+	    || msg_scrolled
+#ifdef FEAT_GUI
+	    || gui.starting
+#endif
+	    || starting)
+	return;
+
+    // update all windows that need updating
+    update_prepare();
+
+    FOR_ALL_WINDOWS(wp)
+    {
+	if (wp->w_redr_type != 0)
+	    win_update(wp);
+	if (wp->w_redr_status)
+	    win_redr_status(wp, FALSE);
+    }
+
+    update_finish();
+}
+#endif
+
+#if defined(FEAT_GUI) || defined(PROTO)
+/*
+ * Update a single window, its status line and maybe the command line msg.
+ * Used for the GUI scrollbar.
+ */
+    void
+updateWindow(win_T *wp)
+{
+    // return if already busy updating
+    if (updating_screen)
+	return;
+
+    update_prepare();
+
+#ifdef FEAT_CLIPBOARD
+    // When Visual area changed, may have to update selection.
+    if (clip_star.available && clip_isautosel_star())
+	clip_update_selection(&clip_star);
+    if (clip_plus.available && clip_isautosel_plus())
+	clip_update_selection(&clip_plus);
+#endif
+
+    win_update(wp);
+
+    // When the screen was cleared redraw the tab pages line.
+    if (redraw_tabline)
+	draw_tabline();
+
+    if (wp->w_redr_status
+# ifdef FEAT_CMDL_INFO
+	    || p_ru
+# endif
+# ifdef FEAT_STL_OPT
+	    || *p_stl != NUL || *wp->w_p_stl != NUL
+# endif
+	    )
+	win_redr_status(wp, FALSE);
+
+#ifdef FEAT_TEXT_PROP
+    // Display popup windows on top of everything.
+    update_popups(win_update);
+#endif
+
+    update_finish();
+}
+#endif
+
+#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
+/*
+ * Redraw as soon as possible.  When the command line is not scrolled redraw
+ * right away and restore what was on the command line.
+ * Return a code indicating what happened.
+ */
+    int
+redraw_asap(int type)
+{
+    int		rows;
+    int		cols = screen_Columns;
+    int		r;
+    int		ret = 0;
+    schar_T	*screenline;	// copy from ScreenLines[]
+    sattr_T	*screenattr;	// copy from ScreenAttrs[]
+    int		i;
+    u8char_T	*screenlineUC = NULL;	// copy from ScreenLinesUC[]
+    u8char_T	*screenlineC[MAX_MCO];	// copy from ScreenLinesC[][]
+    schar_T	*screenline2 = NULL;	// copy from ScreenLines2[]
+
+    redraw_later(type);
+    if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
+	return ret;
+
+    // Allocate space to save the text displayed in the command line area.
+    rows = screen_Rows - cmdline_row;
+    screenline = LALLOC_MULT(schar_T, rows * cols);
+    screenattr = LALLOC_MULT(sattr_T, rows * cols);
+    if (screenline == NULL || screenattr == NULL)
+	ret = 2;
+    if (enc_utf8)
+    {
+	screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
+	if (screenlineUC == NULL)
+	    ret = 2;
+	for (i = 0; i < p_mco; ++i)
+	{
+	    screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
+	    if (screenlineC[i] == NULL)
+		ret = 2;
+	}
+    }
+    if (enc_dbcs == DBCS_JPNU)
+    {
+	screenline2 = LALLOC_MULT(schar_T, rows * cols);
+	if (screenline2 == NULL)
+	    ret = 2;
+    }
+
+    if (ret != 2)
+    {
+	// Save the text displayed in the command line area.
+	for (r = 0; r < rows; ++r)
+	{
+	    mch_memmove(screenline + r * cols,
+			ScreenLines + LineOffset[cmdline_row + r],
+			(size_t)cols * sizeof(schar_T));
+	    mch_memmove(screenattr + r * cols,
+			ScreenAttrs + LineOffset[cmdline_row + r],
+			(size_t)cols * sizeof(sattr_T));
+	    if (enc_utf8)
+	    {
+		mch_memmove(screenlineUC + r * cols,
+			    ScreenLinesUC + LineOffset[cmdline_row + r],
+			    (size_t)cols * sizeof(u8char_T));
+		for (i = 0; i < p_mco; ++i)
+		    mch_memmove(screenlineC[i] + r * cols,
+				ScreenLinesC[i] + LineOffset[cmdline_row + r],
+				(size_t)cols * sizeof(u8char_T));
+	    }
+	    if (enc_dbcs == DBCS_JPNU)
+		mch_memmove(screenline2 + r * cols,
+			    ScreenLines2 + LineOffset[cmdline_row + r],
+			    (size_t)cols * sizeof(schar_T));
+	}
+
+	update_screen(0);
+	ret = 3;
+
+	if (must_redraw == 0)
+	{
+	    int	off = (int)(current_ScreenLine - ScreenLines);
+
+	    // Restore the text displayed in the command line area.
+	    for (r = 0; r < rows; ++r)
+	    {
+		mch_memmove(current_ScreenLine,
+			    screenline + r * cols,
+			    (size_t)cols * sizeof(schar_T));
+		mch_memmove(ScreenAttrs + off,
+			    screenattr + r * cols,
+			    (size_t)cols * sizeof(sattr_T));
+		if (enc_utf8)
+		{
+		    mch_memmove(ScreenLinesUC + off,
+				screenlineUC + r * cols,
+				(size_t)cols * sizeof(u8char_T));
+		    for (i = 0; i < p_mco; ++i)
+			mch_memmove(ScreenLinesC[i] + off,
+				    screenlineC[i] + r * cols,
+				    (size_t)cols * sizeof(u8char_T));
+		}
+		if (enc_dbcs == DBCS_JPNU)
+		    mch_memmove(ScreenLines2 + off,
+				screenline2 + r * cols,
+				(size_t)cols * sizeof(schar_T));
+		screen_line(cmdline_row + r, 0, cols, cols, 0);
+	    }
+	    ret = 4;
+	}
+    }
+
+    vim_free(screenline);
+    vim_free(screenattr);
+    if (enc_utf8)
+    {
+	vim_free(screenlineUC);
+	for (i = 0; i < p_mco; ++i)
+	    vim_free(screenlineC[i]);
+    }
+    if (enc_dbcs == DBCS_JPNU)
+	vim_free(screenline2);
+
+    // Show the intro message when appropriate.
+    maybe_intro_message();
+
+    setcursor();
+
+    return ret;
+}
+#endif
+
+/*
+ * Invoked after an asynchronous callback is called.
+ * If an echo command was used the cursor needs to be put back where
+ * it belongs. If highlighting was changed a redraw is needed.
+ * If "call_update_screen" is FALSE don't call update_screen() when at the
+ * command line.
+ */
+    void
+redraw_after_callback(int call_update_screen)
+{
+    ++redrawing_for_callback;
+
+    if (State == HITRETURN || State == ASKMORE)
+	; // do nothing
+    else if (State & CMDLINE)
+    {
+	// Don't redraw when in prompt_for_number().
+	if (cmdline_row > 0)
+	{
+	    // Redrawing only works when the screen didn't scroll. Don't clear
+	    // wildmenu entries.
+	    if (msg_scrolled == 0
+#ifdef FEAT_WILDMENU
+		    && wild_menu_showing == 0
+#endif
+		    && call_update_screen)
+		update_screen(0);
+
+	    // Redraw in the same position, so that the user can continue
+	    // editing the command.
+	    redrawcmdline_ex(FALSE);
+	}
+    }
+    else if (State & (NORMAL | INSERT | TERMINAL))
+    {
+	// keep the command line if possible
+	update_screen(VALID_NO_UPDATE);
+	setcursor();
+    }
+    cursor_on();
+#ifdef FEAT_GUI
+    if (gui.in_use && !gui_mch_is_blink_off())
+	// Don't update the cursor when it is blinking and off to avoid
+	// flicker.
+	out_flush_cursor(FALSE, FALSE);
+    else
+#endif
+	out_flush();
+
+    --redrawing_for_callback;
+}
+
+/*
+ * Redraw the current window later, with update_screen(type).
+ * Set must_redraw only if not already set to a higher value.
+ * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
+ */
+    void
+redraw_later(int type)
+{
+    redraw_win_later(curwin, type);
+}
+
+    void
+redraw_win_later(
+    win_T	*wp,
+    int		type)
+{
+    if (!exiting && wp->w_redr_type < type)
+    {
+	wp->w_redr_type = type;
+	if (type >= NOT_VALID)
+	    wp->w_lines_valid = 0;
+	if (must_redraw < type)	// must_redraw is the maximum of all windows
+	    must_redraw = type;
+    }
+}
+
+/*
+ * Force a complete redraw later.  Also resets the highlighting.  To be used
+ * after executing a shell command that messes up the screen.
+ */
+    void
+redraw_later_clear(void)
+{
+    redraw_all_later(CLEAR);
+    reset_screen_attr();
+}
+
+/*
+ * Mark all windows to be redrawn later.
+ */
+    void
+redraw_all_later(int type)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+	redraw_win_later(wp, type);
+    // This may be needed when switching tabs.
+    if (must_redraw < type)
+	must_redraw = type;
+}
+
+/*
+ * Mark all windows that are editing the current buffer to be updated later.
+ */
+    void
+redraw_curbuf_later(int type)
+{
+    redraw_buf_later(curbuf, type);
+}
+
+    void
+redraw_buf_later(buf_T *buf, int type)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+    {
+	if (wp->w_buffer == buf)
+	    redraw_win_later(wp, type);
+    }
+}
+
+#if defined(FEAT_SIGNS) || defined(PROTO)
+    void
+redraw_buf_line_later(buf_T *buf, linenr_T lnum)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+	if (wp->w_buffer == buf && lnum >= wp->w_topline
+						  && lnum < wp->w_botline)
+	    redrawWinline(wp, lnum);
+}
+#endif
+
+#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
+    void
+redraw_buf_and_status_later(buf_T *buf, int type)
+{
+    win_T	*wp;
+
+#ifdef FEAT_WILDMENU
+    if (wild_menu_showing != 0)
+	// Don't redraw while the command line completion is displayed, it
+	// would disappear.
+	return;
+#endif
+    FOR_ALL_WINDOWS(wp)
+    {
+	if (wp->w_buffer == buf)
+	{
+	    redraw_win_later(wp, type);
+	    wp->w_redr_status = TRUE;
+	}
+    }
+}
+#endif
+
+/*
+ * mark all status lines for redraw; used after first :cd
+ */
+    void
+status_redraw_all(void)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+	if (wp->w_status_height)
+	{
+	    wp->w_redr_status = TRUE;
+	    redraw_later(VALID);
+	}
+}
+
+/*
+ * mark all status lines of the current buffer for redraw
+ */
+    void
+status_redraw_curbuf(void)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+	if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
+	{
+	    wp->w_redr_status = TRUE;
+	    redraw_later(VALID);
+	}
+}
+
+/*
+ * Redraw all status lines that need to be redrawn.
+ */
+    void
+redraw_statuslines(void)
+{
+    win_T	*wp;
+
+    FOR_ALL_WINDOWS(wp)
+	if (wp->w_redr_status)
+	    win_redr_status(wp, FALSE);
+    if (redraw_tabline)
+	draw_tabline();
+}
+
+#if defined(FEAT_WILDMENU) || defined(PROTO)
+/*
+ * Redraw all status lines at the bottom of frame "frp".
+ */
+    void
+win_redraw_last_status(frame_T *frp)
+{
+    if (frp->fr_layout == FR_LEAF)
+	frp->fr_win->w_redr_status = TRUE;
+    else if (frp->fr_layout == FR_ROW)
+    {
+	FOR_ALL_FRAMES(frp, frp->fr_child)
+	    win_redraw_last_status(frp);
+    }
+    else // frp->fr_layout == FR_COL
+    {
+	frp = frp->fr_child;
+	while (frp->fr_next != NULL)
+	    frp = frp->fr_next;
+	win_redraw_last_status(frp);
+    }
+}
+#endif
+
+/*
+ * Changed something in the current window, at buffer line "lnum", that
+ * requires that line and possibly other lines to be redrawn.
+ * Used when entering/leaving Insert mode with the cursor on a folded line.
+ * Used to remove the "$" from a change command.
+ * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
+ * may become invalid and the whole window will have to be redrawn.
+ */
+    void
+redrawWinline(
+    win_T	*wp,
+    linenr_T	lnum)
+{
+    if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
+	wp->w_redraw_top = lnum;
+    if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
+	wp->w_redraw_bot = lnum;
+    redraw_win_later(wp, VALID);
+}
--- a/src/globals.h
+++ b/src/globals.h
@@ -62,6 +62,31 @@ EXTERN int	Screen_mco INIT(= 0);		// val
 EXTERN schar_T	*ScreenLines2 INIT(= NULL);
 
 /*
+ * Buffer for one screen line (characters and attributes).
+ */
+EXTERN schar_T	*current_ScreenLine INIT(= NULL);
+
+/*
+ * Last known cursor position.
+ * Positioning the cursor is reduced by remembering the last position.
+ * Mostly used by windgoto() and screen_char().
+ */
+EXTERN int	screen_cur_row INIT(= 0);
+EXTERN int	screen_cur_col INIT(= 0);
+
+#ifdef FEAT_SEARCH_EXTRA
+EXTERN match_T	screen_search_hl; // used for 'hlsearch' highlight matching
+#endif
+
+#ifdef FEAT_FOLDING
+EXTERN foldinfo_T win_foldinfo;	// info for 'foldcolumn'
+#endif
+
+// Flag that is set when drawing for a callback, not from the main command
+// loop.
+EXTERN int redrawing_for_callback INIT(= 0);
+
+/*
  * Indexes for tab page line:
  *	N > 0 for label of tab page N
  *	N == 0 for no label
--- a/src/proto.h
+++ b/src/proto.h
@@ -73,6 +73,8 @@ extern int _stricoll(char *a, char *b);
 # include "dict.pro"
 # include "diff.pro"
 # include "digraph.pro"
+# include "drawline.pro"
+# include "drawscreen.pro"
 # include "edit.pro"
 # include "eval.pro"
 # include "evalbuffer.pro"
new file mode 100644
--- /dev/null
+++ b/src/proto/drawline.pro
@@ -0,0 +1,3 @@
+/* drawline.c */
+int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int nochange, int number_only);
+/* vim: set ft=c : */
new file mode 100644
--- /dev/null
+++ b/src/proto/drawscreen.pro
@@ -0,0 +1,25 @@
+/* drawscreen.c */
+int update_screen(int type_arg);
+void win_redr_status(win_T *wp, int ignore_pum);
+void showruler(int always);
+void win_redr_ruler(win_T *wp, int always, int ignore_pum);
+void after_updating_screen(int may_resize_shell);
+void update_curbuf(int type);
+void update_debug_sign(buf_T *buf, linenr_T lnum);
+void updateWindow(win_T *wp);
+int redraw_asap(int type);
+void redraw_after_callback(int call_update_screen);
+void redraw_later(int type);
+void redraw_win_later(win_T *wp, int type);
+void redraw_later_clear(void);
+void redraw_all_later(int type);
+void redraw_curbuf_later(int type);
+void redraw_buf_later(buf_T *buf, int type);
+void redraw_buf_line_later(buf_T *buf, linenr_T lnum);
+void redraw_buf_and_status_later(buf_T *buf, int type);
+void status_redraw_all(void);
+void status_redraw_curbuf(void);
+void redraw_statuslines(void);
+void win_redraw_last_status(frame_T *frp);
+void redrawWinline(win_T *wp, linenr_T lnum);
+/* vim: set ft=c : */
--- a/src/proto/screen.pro
+++ b/src/proto/screen.pro
@@ -1,40 +1,30 @@
 /* screen.c */
-void redraw_later(int type);
-void redraw_win_later(win_T *wp, int type);
-void redraw_later_clear(void);
-void redraw_all_later(int type);
-void redraw_curbuf_later(int type);
-void redraw_buf_later(buf_T *buf, int type);
-void redraw_buf_line_later(buf_T *buf, linenr_T lnum);
-void redraw_buf_and_status_later(buf_T *buf, int type);
-int redraw_asap(int type);
-void redraw_after_callback(int call_update_screen);
-void redrawWinline(win_T *wp, linenr_T lnum);
-void after_updating_screen(int may_resize_shell);
-void update_curbuf(int type);
-int update_screen(int type_arg);
 int conceal_cursor_line(win_T *wp);
 void conceal_check_cursor_line(void);
-void update_debug_sign(buf_T *buf, linenr_T lnum);
 int get_wcr_attr(win_T *wp);
-void updateWindow(win_T *wp);
+void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
+int compute_foldcolumn(win_T *wp, int col);
+void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
 int screen_get_current_line_off(void);
+void reset_screen_attr(void);
 void screen_line(int row, int coloff, int endcol, int clear_width, int flags);
 void rl_mirror(char_u *str);
-void status_redraw_all(void);
-void status_redraw_curbuf(void);
-void redraw_statuslines(void);
-void win_redraw_last_status(frame_T *frp);
+void draw_vsep_win(win_T *wp, int row);
 void win_redr_status_matches(expand_T *xp, int num_matches, char_u **matches, int match, int showtail);
 int stl_connected(win_T *wp);
 int get_keymap_str(win_T *wp, char_u *fmt, char_u *buf, int len);
+void win_redr_custom(win_T *wp, int draw_ruler);
 void screen_putchar(int c, int row, int col, int attr);
 void screen_getbytes(int row, int col, char_u *bytes, int *attrp);
 void screen_puts(char_u *text, int row, int col, int attr);
 void screen_puts_len(char_u *text, int textlen, int row, int col, int attr);
+void start_search_hl(void);
+void end_search_hl(void);
 void screen_stop_highlight(void);
 void reset_cterm_colors(void);
+void screen_char(unsigned off, int row, int col);
 void screen_draw_rectangle(int row, int col, int height, int width, int invert);
+void space_to_screenline(int off, int attr);
 void screen_fill(int start_row, int end_row, int start_col, int end_col, int c1, int c2, int attr);
 void check_for_delay(int check_msg_scroll);
 int screen_valid(int doclear);
@@ -56,9 +46,10 @@ void unshowmode(int force);
 void clearmode(void);
 void draw_tabline(void);
 void get_trans_bufname(buf_T *buf);
+int fillchar_status(int *attr, win_T *wp);
+int fillchar_vsep(int *attr);
 int redrawing(void);
 int messaging(void);
-void showruler(int always);
 void comp_col(void);
 int number_width(win_T *wp);
 int screen_screencol(void);
--- a/src/screen.c
+++ b/src/screen.c
@@ -8,7 +8,7 @@
  */
 
 /*
- * screen.c: code for displaying on the screen
+ * screen.c: Lower level code for displaying on the screen.
  *
  * Output to the screen (console, terminal emulator or GUI window) is minimized
  * by remembering what is already on the screen, and only updating the parts
@@ -35,110 +35,15 @@
  *
  * The screen_*() functions write to the screen and handle updating
  * ScreenLines[].
- *
- * update_screen() is the function that updates all windows and status lines.
- * It is called form the main loop when must_redraw is non-zero.  It may be
- * called from other places when an immediate screen update is needed.
- *
- * The part of the buffer that is displayed in a window is set with:
- * - w_topline (first buffer line in window)
- * - w_topfill (filler lines above the first line)
- * - w_leftcol (leftmost window cell in window),
- * - w_skipcol (skipped window cells of first line)
- *
- * Commands that only move the cursor around in a window, do not need to take
- * action to update the display.  The main loop will check if w_topline is
- * valid and update it (scroll the window) when needed.
- *
- * Commands that scroll a window change w_topline and must call
- * check_cursor() to move the cursor into the visible part of the window, and
- * call redraw_later(VALID) to have the window displayed by update_screen()
- * later.
- *
- * Commands that change text in the buffer must call changed_bytes() or
- * changed_lines() to mark the area that changed and will require updating
- * later.  The main loop will call update_screen(), which will update each
- * window that shows the changed buffer.  This assumes text above the change
- * can remain displayed as it is.  Text after the change may need updating for
- * scrolling, folding and syntax highlighting.
- *
- * Commands that change how a window is displayed (e.g., setting 'list') or
- * invalidate the contents of a window in another way (e.g., change fold
- * settings), must call redraw_later(NOT_VALID) to have the whole window
- * redisplayed by update_screen() later.
- *
- * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
- * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
- * buffer redisplayed by update_screen() later.
- *
- * Commands that change highlighting and possibly cause a scroll too must call
- * redraw_later(SOME_VALID) to update the whole window but still use scrolling
- * to avoid redrawing everything.  But the length of displayed lines must not
- * change, use NOT_VALID then.
- *
- * Commands that move the window position must call redraw_later(NOT_VALID).
- * TODO: should minimize redrawing by scrolling when possible.
- *
- * Commands that change everything (e.g., resizing the screen) must call
- * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
- *
- * Things that are handled indirectly:
- * - When messages scroll the screen up, msg_scrolled will be set and
- *   update_screen() called to redraw.
  */
 
 #include "vim.h"
 
-#define MB_FILLER_CHAR '<'  /* character used when a double-width character
-			     * doesn't fit. */
-
 /*
  * The attributes that are actually active for writing to the screen.
  */
 static int	screen_attr = 0;
 
-/*
- * Positioning the cursor is reduced by remembering the last position.
- * Mostly used by windgoto() and screen_char().
- */
-static int	screen_cur_row, screen_cur_col;	/* last known cursor position */
-
-#ifdef FEAT_SEARCH_EXTRA
-static match_T search_hl;	// used for 'hlsearch' highlight matching
-#endif
-
-#ifdef FEAT_FOLDING
-static foldinfo_T win_foldinfo;	/* info for 'foldcolumn' */
-static int compute_foldcolumn(win_T *wp, int col);
-#endif
-
-/* Flag that is set when drawing for a callback, not from the main command
- * loop. */
-static int redrawing_for_callback = 0;
-
-/*
- * Buffer for one screen line (characters and attributes).
- */
-static schar_T	*current_ScreenLine;
-
-static void win_update(win_T *wp);
-static void win_redr_status(win_T *wp, int ignore_pum);
-static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
-#ifdef FEAT_FOLDING
-static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
-static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
-static void copy_text_attr(int off, char_u *buf, int len, int attr);
-#endif
-static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
-static void draw_vsep_win(win_T *wp, int row);
-#ifdef FEAT_STL_OPT
-static void redraw_custom_statusline(win_T *wp);
-#endif
-#ifdef FEAT_SEARCH_EXTRA
-static void start_search_hl(void);
-static void end_search_hl(void);
-#endif
-static void screen_char(unsigned off, int row, int col);
 static void screen_char_2(unsigned off, int row, int col);
 static void screenclear2(void);
 static void lineclear(unsigned off, int width, int attr);
@@ -147,762 +52,10 @@ static int win_do_lines(win_T *wp, int r
 static void win_rest_invalid(win_T *wp);
 static void msg_pos_mode(void);
 static void recording_mode(int attr);
-static int fillchar_status(int *attr, win_T *wp);
-static int fillchar_vsep(int *attr);
-#ifdef FEAT_MENU
-static void redraw_win_toolbar(win_T *wp);
-#endif
-#ifdef FEAT_STL_OPT
-static void win_redr_custom(win_T *wp, int draw_ruler);
-#endif
-#ifdef FEAT_CMDL_INFO
-static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
-#endif
-#ifdef FEAT_SYN_HL
-static void margin_columns_win(win_T *wp, int *left_col, int *right_col);
-#endif
 
 /* Ugly global: overrule attribute used by screen_char() */
 static int screen_char_attr = 0;
 
-#ifdef FEAT_RIGHTLEFT
-# define HAS_RIGHTLEFT(x) x
-#else
-# define HAS_RIGHTLEFT(x) FALSE
-#endif
-
-// flags for screen_line()
-#define SLF_RIGHTLEFT	1
-#define SLF_POPUP	2
-
-/*
- * Redraw the current window later, with update_screen(type).
- * Set must_redraw only if not already set to a higher value.
- * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
- */
-    void
-redraw_later(int type)
-{
-    redraw_win_later(curwin, type);
-}
-
-    void
-redraw_win_later(
-    win_T	*wp,
-    int		type)
-{
-    if (!exiting && wp->w_redr_type < type)
-    {
-	wp->w_redr_type = type;
-	if (type >= NOT_VALID)
-	    wp->w_lines_valid = 0;
-	if (must_redraw < type)	/* must_redraw is the maximum of all windows */
-	    must_redraw = type;
-    }
-}
-
-/*
- * Force a complete redraw later.  Also resets the highlighting.  To be used
- * after executing a shell command that messes up the screen.
- */
-    void
-redraw_later_clear(void)
-{
-    redraw_all_later(CLEAR);
-#ifdef FEAT_GUI
-    if (gui.in_use)
-	/* Use a code that will reset gui.highlight_mask in
-	 * gui_stop_highlight(). */
-	screen_attr = HL_ALL + 1;
-    else
-#endif
-	/* Use attributes that is very unlikely to appear in text. */
-	screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
-}
-
-/*
- * Mark all windows to be redrawn later.
- */
-    void
-redraw_all_later(int type)
-{
-    win_T	*wp;
-
-    FOR_ALL_WINDOWS(wp)
-	redraw_win_later(wp, type);
-    // This may be needed when switching tabs.
-    if (must_redraw < type)
-	must_redraw = type;
-}
-
-/*
- * Mark all windows that are editing the current buffer to be updated later.
- */
-    void
-redraw_curbuf_later(int type)
-{
-    redraw_buf_later(curbuf, type);
-}
-
-    void
-redraw_buf_later(buf_T *buf, int type)
-{
-    win_T	*wp;
-
-    FOR_ALL_WINDOWS(wp)
-    {
-	if (wp->w_buffer == buf)
-	    redraw_win_later(wp, type);
-    }
-}
-
-#if defined(FEAT_SIGNS) || defined(PROTO)
-    void
-redraw_buf_line_later(buf_T *buf, linenr_T lnum)
-{
-    win_T	*wp;
-
-    FOR_ALL_WINDOWS(wp)
-	if (wp->w_buffer == buf && lnum >= wp->w_topline
-						  && lnum < wp->w_botline)
-	    redrawWinline(wp, lnum);
-}
-#endif
-
-#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
-    void
-redraw_buf_and_status_later(buf_T *buf, int type)
-{
-    win_T	*wp;
-
-#ifdef FEAT_WILDMENU
-    if (wild_menu_showing != 0)
-	/* Don't redraw while the command line completion is displayed, it
-	 * would disappear. */
-	return;
-#endif
-    FOR_ALL_WINDOWS(wp)
-    {
-	if (wp->w_buffer == buf)
-	{
-	    redraw_win_later(wp, type);
-	    wp->w_redr_status = TRUE;
-	}
-    }
-}
-#endif
-
-#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
-/*
- * Redraw as soon as possible.  When the command line is not scrolled redraw
- * right away and restore what was on the command line.
- * Return a code indicating what happened.
- */
-    int
-redraw_asap(int type)
-{
-    int		rows;
-    int		cols = screen_Columns;
-    int		r;
-    int		ret = 0;
-    schar_T	*screenline;	/* copy from ScreenLines[] */
-    sattr_T	*screenattr;	/* copy from ScreenAttrs[] */
-    int		i;
-    u8char_T	*screenlineUC = NULL;	/* copy from ScreenLinesUC[] */
-    u8char_T	*screenlineC[MAX_MCO];	/* copy from ScreenLinesC[][] */
-    schar_T	*screenline2 = NULL;	/* copy from ScreenLines2[] */
-
-    redraw_later(type);
-    if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
-	return ret;
-
-    /* Allocate space to save the text displayed in the command line area. */
-    rows = screen_Rows - cmdline_row;
-    screenline = LALLOC_MULT(schar_T, rows * cols);
-    screenattr = LALLOC_MULT(sattr_T, rows * cols);
-    if (screenline == NULL || screenattr == NULL)
-	ret = 2;
-    if (enc_utf8)
-    {
-	screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
-	if (screenlineUC == NULL)
-	    ret = 2;
-	for (i = 0; i < p_mco; ++i)
-	{
-	    screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
-	    if (screenlineC[i] == NULL)
-		ret = 2;
-	}
-    }
-    if (enc_dbcs == DBCS_JPNU)
-    {
-	screenline2 = LALLOC_MULT(schar_T, rows * cols);
-	if (screenline2 == NULL)
-	    ret = 2;
-    }
-
-    if (ret != 2)
-    {
-	/* Save the text displayed in the command line area. */
-	for (r = 0; r < rows; ++r)
-	{
-	    mch_memmove(screenline + r * cols,
-			ScreenLines + LineOffset[cmdline_row + r],
-			(size_t)cols * sizeof(schar_T));
-	    mch_memmove(screenattr + r * cols,
-			ScreenAttrs + LineOffset[cmdline_row + r],
-			(size_t)cols * sizeof(sattr_T));
-	    if (enc_utf8)
-	    {
-		mch_memmove(screenlineUC + r * cols,
-			    ScreenLinesUC + LineOffset[cmdline_row + r],
-			    (size_t)cols * sizeof(u8char_T));
-		for (i = 0; i < p_mco; ++i)
-		    mch_memmove(screenlineC[i] + r * cols,
-				ScreenLinesC[i] + LineOffset[cmdline_row + r],
-				(size_t)cols * sizeof(u8char_T));
-	    }
-	    if (enc_dbcs == DBCS_JPNU)
-		mch_memmove(screenline2 + r * cols,
-			    ScreenLines2 + LineOffset[cmdline_row + r],
-			    (size_t)cols * sizeof(schar_T));
-	}
-
-	update_screen(0);
-	ret = 3;
-
-	if (must_redraw == 0)
-	{
-	    int	off = (int)(current_ScreenLine - ScreenLines);
-
-	    /* Restore the text displayed in the command line area. */
-	    for (r = 0; r < rows; ++r)
-	    {
-		mch_memmove(current_ScreenLine,
-			    screenline + r * cols,
-			    (size_t)cols * sizeof(schar_T));
-		mch_memmove(ScreenAttrs + off,
-			    screenattr + r * cols,
-			    (size_t)cols * sizeof(sattr_T));
-		if (enc_utf8)
-		{
-		    mch_memmove(ScreenLinesUC + off,
-				screenlineUC + r * cols,
-				(size_t)cols * sizeof(u8char_T));
-		    for (i = 0; i < p_mco; ++i)
-			mch_memmove(ScreenLinesC[i] + off,
-				    screenlineC[i] + r * cols,
-				    (size_t)cols * sizeof(u8char_T));
-		}
-		if (enc_dbcs == DBCS_JPNU)
-		    mch_memmove(ScreenLines2 + off,
-				screenline2 + r * cols,
-				(size_t)cols * sizeof(schar_T));
-		screen_line(cmdline_row + r, 0, cols, cols, 0);
-	    }
-	    ret = 4;
-	}
-    }
-
-    vim_free(screenline);
-    vim_free(screenattr);
-    if (enc_utf8)
-    {
-	vim_free(screenlineUC);
-	for (i = 0; i < p_mco; ++i)
-	    vim_free(screenlineC[i]);
-    }
-    if (enc_dbcs == DBCS_JPNU)
-	vim_free(screenline2);
-
-    /* Show the intro message when appropriate. */
-    maybe_intro_message();
-
-    setcursor();
-
-    return ret;
-}
-#endif
-
-/*
- * Invoked after an asynchronous callback is called.
- * If an echo command was used the cursor needs to be put back where
- * it belongs. If highlighting was changed a redraw is needed.
- * If "call_update_screen" is FALSE don't call update_screen() when at the
- * command line.
- */
-    void
-redraw_after_callback(int call_update_screen)
-{
-    ++redrawing_for_callback;
-
-    if (State == HITRETURN || State == ASKMORE)
-	; // do nothing
-    else if (State & CMDLINE)
-    {
-	// Don't redraw when in prompt_for_number().
-	if (cmdline_row > 0)
-	{
-	    // Redrawing only works when the screen didn't scroll. Don't clear
-	    // wildmenu entries.
-	    if (msg_scrolled == 0
-#ifdef FEAT_WILDMENU
-		    && wild_menu_showing == 0
-#endif
-		    && call_update_screen)
-		update_screen(0);
-
-	    // Redraw in the same position, so that the user can continue
-	    // editing the command.
-	    redrawcmdline_ex(FALSE);
-	}
-    }
-    else if (State & (NORMAL | INSERT | TERMINAL))
-    {
-	// keep the command line if possible
-	update_screen(VALID_NO_UPDATE);
-	setcursor();
-    }
-    cursor_on();
-#ifdef FEAT_GUI
-    if (gui.in_use && !gui_mch_is_blink_off())
-	// Don't update the cursor when it is blinking and off to avoid
-	// flicker.
-	out_flush_cursor(FALSE, FALSE);
-    else
-#endif
-	out_flush();
-
-    --redrawing_for_callback;
-}
-
-/*
- * Changed something in the current window, at buffer line "lnum", that
- * requires that line and possibly other lines to be redrawn.
- * Used when entering/leaving Insert mode with the cursor on a folded line.
- * Used to remove the "$" from a change command.
- * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
- * may become invalid and the whole window will have to be redrawn.
- */
-    void
-redrawWinline(
-    win_T	*wp,
-    linenr_T	lnum)
-{
-    if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
-	wp->w_redraw_top = lnum;
-    if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
-	wp->w_redraw_bot = lnum;
-    redraw_win_later(wp, VALID);
-}
-
-/*
- * To be called when "updating_screen" was set before and now the postponed
- * side effects may take place.
- */
-    void
-after_updating_screen(int may_resize_shell UNUSED)
-{
-    updating_screen = FALSE;
-#ifdef FEAT_GUI
-    if (may_resize_shell)
-	gui_may_resize_shell();
-#endif
-#ifdef FEAT_TERMINAL
-    term_check_channel_closed_recently();
-#endif
-
-#ifdef HAVE_DROP_FILE
-    // If handle_drop() was called while updating_screen was TRUE need to
-    // handle the drop now.
-    handle_any_postponed_drop();
-#endif
-}
-
-/*
- * Update all windows that are editing the current buffer.
- */
-    void
-update_curbuf(int type)
-{
-    redraw_curbuf_later(type);
-    update_screen(type);
-}
-
-/*
- * Based on the current value of curwin->w_topline, transfer a screenfull
- * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
- * Return OK when the screen was updated, FAIL if it was not done.
- */
-    int
-update_screen(int type_arg)
-{
-    int		type = type_arg;
-    win_T	*wp;
-    static int	did_intro = FALSE;
-#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
-    int		did_one;
-#endif
-#ifdef FEAT_GUI
-    int		did_undraw = FALSE;
-    int		gui_cursor_col = 0;
-    int		gui_cursor_row = 0;
-#endif
-    int		no_update = FALSE;
-
-    /* Don't do anything if the screen structures are (not yet) valid. */
-    if (!screen_valid(TRUE))
-	return FAIL;
-
-    if (type == VALID_NO_UPDATE)
-    {
-	no_update = TRUE;
-	type = 0;
-    }
-
-#ifdef FEAT_EVAL
-    {
-	buf_T *buf;
-
-	// Before updating the screen, notify any listeners of changed text.
-	FOR_ALL_BUFFERS(buf)
-	    invoke_listeners(buf);
-    }
-#endif
-
-#ifdef FEAT_DIFF
-    // May have postponed updating diffs.
-    if (need_diff_redraw)
-	diff_redraw(TRUE);
-#endif
-
-    if (must_redraw)
-    {
-	if (type < must_redraw)	    /* use maximal type */
-	    type = must_redraw;
-
-	/* must_redraw is reset here, so that when we run into some weird
-	 * reason to redraw while busy redrawing (e.g., asynchronous
-	 * scrolling), or update_topline() in win_update() will cause a
-	 * scroll, the screen will be redrawn later or in win_update(). */
-	must_redraw = 0;
-    }
-
-    /* May need to update w_lines[]. */
-    if (curwin->w_lines_valid == 0 && type < NOT_VALID
-#ifdef FEAT_TERMINAL
-	    && !term_do_update_window(curwin)
-#endif
-		)
-	type = NOT_VALID;
-
-    /* Postpone the redrawing when it's not needed and when being called
-     * recursively. */
-    if (!redrawing() || updating_screen)
-    {
-	redraw_later(type);		/* remember type for next time */
-	must_redraw = type;
-	if (type > INVERTED_ALL)
-	    curwin->w_lines_valid = 0;	/* don't use w_lines[].wl_size now */
-	return FAIL;
-    }
-    updating_screen = TRUE;
-
-#ifdef FEAT_TEXT_PROP
-    // Update popup_mask if needed.  This may set w_redraw_top and w_redraw_bot
-    // in some windows.
-    may_update_popup_mask(type);
-#endif
-
-#ifdef FEAT_SYN_HL
-    ++display_tick;	    /* let syntax code know we're in a next round of
-			     * display updating */
-#endif
-    if (no_update)
-	++no_win_do_lines_ins;
-
-    /*
-     * if the screen was scrolled up when displaying a message, scroll it down
-     */
-    if (msg_scrolled)
-    {
-	clear_cmdline = TRUE;
-	if (msg_scrolled > Rows - 5)	    /* clearing is faster */
-	    type = CLEAR;
-	else if (type != CLEAR)
-	{
-	    check_for_delay(FALSE);
-	    if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
-								       == FAIL)
-		type = CLEAR;
-	    FOR_ALL_WINDOWS(wp)
-	    {
-		if (wp->w_winrow < msg_scrolled)
-		{
-		    if (W_WINROW(wp) + wp->w_height > msg_scrolled
-			    && wp->w_redr_type < REDRAW_TOP
-			    && wp->w_lines_valid > 0
-			    && wp->w_topline == wp->w_lines[0].wl_lnum)
-		    {
-			wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
-			wp->w_redr_type = REDRAW_TOP;
-		    }
-		    else
-		    {
-			wp->w_redr_type = NOT_VALID;
-			if (W_WINROW(wp) + wp->w_height + wp->w_status_height
-							       <= msg_scrolled)
-			    wp->w_redr_status = TRUE;
-		    }
-		}
-	    }
-	    if (!no_update)
-		redraw_cmdline = TRUE;
-	    redraw_tabline = TRUE;
-	}
-	msg_scrolled = 0;
-	need_wait_return = FALSE;
-    }
-
-    /* reset cmdline_row now (may have been changed temporarily) */
-    compute_cmdrow();
-
-    /* Check for changed highlighting */
-    if (need_highlight_changed)
-	highlight_changed();
-
-    if (type == CLEAR)		/* first clear screen */
-    {
-	screenclear();		/* will reset clear_cmdline */
-	type = NOT_VALID;
-	/* must_redraw may be set indirectly, avoid another redraw later */
-	must_redraw = 0;
-    }
-
-    if (clear_cmdline)		/* going to clear cmdline (done below) */
-	check_for_delay(FALSE);
-
-#ifdef FEAT_LINEBREAK
-    /* Force redraw when width of 'number' or 'relativenumber' column
-     * changes. */
-    if (curwin->w_redr_type < NOT_VALID
-	   && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
-				    ? number_width(curwin) : 0))
-	curwin->w_redr_type = NOT_VALID;
-#endif
-
-    /*
-     * Only start redrawing if there is really something to do.
-     */
-    if (type == INVERTED)
-	update_curswant();
-    if (curwin->w_redr_type < type
-	    && !((type == VALID
-		    && curwin->w_lines[0].wl_valid
-#ifdef FEAT_DIFF
-		    && curwin->w_topfill == curwin->w_old_topfill
-		    && curwin->w_botfill == curwin->w_old_botfill
-#endif
-		    && curwin->w_topline == curwin->w_lines[0].wl_lnum)
-		|| (type == INVERTED
-		    && VIsual_active
-		    && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
-		    && curwin->w_old_visual_mode == VIsual_mode
-		    && (curwin->w_valid & VALID_VIRTCOL)
-		    && curwin->w_old_curswant == curwin->w_curswant)
-		))
-	curwin->w_redr_type = type;
-
-    /* Redraw the tab pages line if needed. */
-    if (redraw_tabline || type >= NOT_VALID)
-	draw_tabline();
-
-#ifdef FEAT_SYN_HL
-    /*
-     * Correct stored syntax highlighting info for changes in each displayed
-     * buffer.  Each buffer must only be done once.
-     */
-    FOR_ALL_WINDOWS(wp)
-    {
-	if (wp->w_buffer->b_mod_set)
-	{
-	    win_T	*wwp;
-
-	    /* Check if we already did this buffer. */
-	    for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
-		if (wwp->w_buffer == wp->w_buffer)
-		    break;
-	    if (wwp == wp && syntax_present(wp))
-		syn_stack_apply_changes(wp->w_buffer);
-	}
-    }
-#endif
-
-    /*
-     * Go from top to bottom through the windows, redrawing the ones that need
-     * it.
-     */
-#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
-    did_one = FALSE;
-#endif
-#ifdef FEAT_SEARCH_EXTRA
-    search_hl.rm.regprog = NULL;
-#endif
-    FOR_ALL_WINDOWS(wp)
-    {
-	if (wp->w_redr_type != 0)
-	{
-	    cursor_off();
-#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
-	    if (!did_one)
-	    {
-		did_one = TRUE;
-# ifdef FEAT_SEARCH_EXTRA
-		start_search_hl();
-# endif
-# ifdef FEAT_CLIPBOARD
-		/* When Visual area changed, may have to update selection. */
-		if (clip_star.available && clip_isautosel_star())
-		    clip_update_selection(&clip_star);
-		if (clip_plus.available && clip_isautosel_plus())
-		    clip_update_selection(&clip_plus);
-# endif
-#ifdef FEAT_GUI
-		/* Remove the cursor before starting to do anything, because
-		 * scrolling may make it difficult to redraw the text under
-		 * it. */
-		if (gui.in_use && wp == curwin)
-		{
-		    gui_cursor_col = gui.cursor_col;
-		    gui_cursor_row = gui.cursor_row;
-		    gui_undraw_cursor();
-		    did_undraw = TRUE;
-		}
-#endif
-	    }
-#endif
-	    win_update(wp);
-	}
-
-	/* redraw status line after the window to minimize cursor movement */
-	if (wp->w_redr_status)
-	{
-	    cursor_off();
-	    win_redr_status(wp, TRUE); // any popup menu will be redrawn below
-	}
-    }
-#if defined(FEAT_SEARCH_EXTRA)
-    end_search_hl();
-#endif
-    /* May need to redraw the popup menu. */
-    pum_may_redraw();
-
-    /* Reset b_mod_set flags.  Going through all windows is probably faster
-     * than going through all buffers (there could be many buffers). */
-    FOR_ALL_WINDOWS(wp)
-	wp->w_buffer->b_mod_set = FALSE;
-
-#ifdef FEAT_TEXT_PROP
-    // Display popup windows on top of the windows and command line.
-    update_popups(win_update);
-#endif
-
-    after_updating_screen(TRUE);
-
-    /* Clear or redraw the command line.  Done last, because scrolling may
-     * mess up the command line. */
-    if (clear_cmdline || redraw_cmdline || redraw_mode)
-	showmode();
-
-    if (no_update)
-	--no_win_do_lines_ins;
-
-    /* May put up an introductory message when not editing a file */
-    if (!did_intro)
-	maybe_intro_message();
-    did_intro = TRUE;
-
-#ifdef FEAT_GUI
-    /* Redraw the cursor and update the scrollbars when all screen updating is
-     * done. */
-    if (gui.in_use)
-    {
-	if (did_undraw && !gui_mch_is_blink_off())
-	{
-	    mch_disable_flush();
-	    out_flush();	/* required before updating the cursor */
-	    mch_enable_flush();
-
-	    /* Put the GUI position where the cursor was, gui_update_cursor()
-	     * uses that. */
-	    gui.col = gui_cursor_col;
-	    gui.row = gui_cursor_row;
-	    gui.col = mb_fix_col(gui.col, gui.row);
-	    gui_update_cursor(FALSE, FALSE);
-	    gui_may_flush();
-	    screen_cur_col = gui.col;
-	    screen_cur_row = gui.row;
-	}
-	else
-	    out_flush();
-	gui_update_scrollbars(FALSE);
-    }
-#endif
-    return OK;
-}
-
-#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
-/*
- * Prepare for updating one or more windows.
- * Caller must check for "updating_screen" already set to avoid recursiveness.
- */
-    static void
-update_prepare(void)
-{
-    cursor_off();
-    updating_screen = TRUE;
-#ifdef FEAT_GUI
-    /* Remove the cursor before starting to do anything, because scrolling may
-     * make it difficult to redraw the text under it. */
-    if (gui.in_use)
-	gui_undraw_cursor();
-#endif
-#ifdef FEAT_SEARCH_EXTRA
-    start_search_hl();
-#endif
-#ifdef FEAT_TEXT_PROP
-    // Update popup_mask if needed.
-    may_update_popup_mask(must_redraw);
-#endif
-}
-
-/*
- * Finish updating one or more windows.
- */
-    static void
-update_finish(void)
-{
-    if (redraw_cmdline || redraw_mode)
-	showmode();
-
-# ifdef FEAT_SEARCH_EXTRA
-    end_search_hl();
-# endif
-
-    after_updating_screen(TRUE);
-
-# ifdef FEAT_GUI
-    /* Redraw the cursor and update the scrollbars when all screen updating is
-     * done. */
-    if (gui.in_use)
-    {
-	out_flush_cursor(FALSE, FALSE);
-	gui_update_scrollbars(FALSE);
-    }
-# endif
-}
-#endif
-
 #if defined(FEAT_CONCEAL) || defined(PROTO)
 /*
  * Return TRUE if the cursor line in window "wp" may be concealed, according
@@ -944,52 +97,6 @@ conceal_check_cursor_line(void)
 }
 #endif
 
-#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
-    void
-update_debug_sign(buf_T *buf, linenr_T lnum)
-{
-    win_T	*wp;
-    int		doit = FALSE;
-
-# ifdef FEAT_FOLDING
-    win_foldinfo.fi_level = 0;
-# endif
-
-    // update/delete a specific sign
-    redraw_buf_line_later(buf, lnum);
-
-    // check if it resulted in the need to redraw a window
-    FOR_ALL_WINDOWS(wp)
-	if (wp->w_redr_type != 0)
-	    doit = TRUE;
-
-    /* Return when there is nothing to do, screen updating is already
-     * happening (recursive call), messages on the screen or still starting up.
-     */
-    if (!doit || updating_screen
-	    || State == ASKMORE || State == HITRETURN
-	    || msg_scrolled
-#ifdef FEAT_GUI
-	    || gui.starting
-#endif
-	    || starting)
-	return;
-
-    /* update all windows that need updating */
-    update_prepare();
-
-    FOR_ALL_WINDOWS(wp)
-    {
-	if (wp->w_redr_type != 0)
-	    win_update(wp);
-	if (wp->w_redr_status)
-	    win_redr_status(wp, FALSE);
-    }
-
-    update_finish();
-}
-#endif
-
 /*
  * Get 'wincolor' attribute for window "wp".  If not set and "wp" is a popup
  * window then get the "Pmenu" highlight attribute.
@@ -1013,1353 +120,6 @@ get_wcr_attr(win_T *wp)
     return wcr_attr;
 }
 
-#if defined(FEAT_GUI) || defined(PROTO)
-/*
- * Update a single window, its status line and maybe the command line msg.
- * Used for the GUI scrollbar.
- */
-    void
-updateWindow(win_T *wp)
-{
-    /* return if already busy updating */
-    if (updating_screen)
-	return;
-
-    update_prepare();
-
-#ifdef FEAT_CLIPBOARD
-    /* When Visual area changed, may have to update selection. */
-    if (clip_star.available && clip_isautosel_star())
-	clip_update_selection(&clip_star);
-    if (clip_plus.available && clip_isautosel_plus())
-	clip_update_selection(&clip_plus);
-#endif
-
-    win_update(wp);
-
-    /* When the screen was cleared redraw the tab pages line. */
-    if (redraw_tabline)
-	draw_tabline();
-
-    if (wp->w_redr_status
-# ifdef FEAT_CMDL_INFO
-	    || p_ru
-# endif
-# ifdef FEAT_STL_OPT
-	    || *p_stl != NUL || *wp->w_p_stl != NUL
-# endif
-	    )
-	win_redr_status(wp, FALSE);
-
-#ifdef FEAT_TEXT_PROP
-    // Display popup windows on top of everything.
-    update_popups(win_update);
-#endif
-
-    update_finish();
-}
-#endif
-
-/*
- * Update a single window.
- *
- * This may cause the windows below it also to be redrawn (when clearing the
- * screen or scrolling lines).
- *
- * How the window is redrawn depends on wp->w_redr_type.  Each type also
- * implies the one below it.
- * NOT_VALID	redraw the whole window
- * SOME_VALID	redraw the whole window but do scroll when possible
- * REDRAW_TOP	redraw the top w_upd_rows window lines, otherwise like VALID
- * INVERTED	redraw the changed part of the Visual area
- * INVERTED_ALL	redraw the whole Visual area
- * VALID	1. scroll up/down to adjust for a changed w_topline
- *		2. update lines at the top when scrolled down
- *		3. redraw changed text:
- *		   - if wp->w_buffer->b_mod_set set, update lines between
- *		     b_mod_top and b_mod_bot.
- *		   - if wp->w_redraw_top non-zero, redraw lines between
- *		     wp->w_redraw_top and wp->w_redr_bot.
- *		   - continue redrawing when syntax status is invalid.
- *		4. if scrolled up, update lines at the bottom.
- * This results in three areas that may need updating:
- * top:	from first row to top_end (when scrolled down)
- * mid: from mid_start to mid_end (update inversion or changed text)
- * bot: from bot_start to last row (when scrolled up)
- */
-    static void
-win_update(win_T *wp)
-{
-    buf_T	*buf = wp->w_buffer;
-    int		type;
-    int		top_end = 0;	/* Below last row of the top area that needs
-				   updating.  0 when no top area updating. */
-    int		mid_start = 999;/* first row of the mid area that needs
-				   updating.  999 when no mid area updating. */
-    int		mid_end = 0;	/* Below last row of the mid area that needs
-				   updating.  0 when no mid area updating. */
-    int		bot_start = 999;/* first row of the bot area that needs
-				   updating.  999 when no bot area updating */
-    int		scrolled_down = FALSE;	/* TRUE when scrolled down when
-					   w_topline got smaller a bit */
-#ifdef FEAT_SEARCH_EXTRA
-    int		top_to_mod = FALSE;    // redraw above mod_top
-#endif
-
-    int		row;		/* current window row to display */
-    linenr_T	lnum;		/* current buffer lnum to display */
-    int		idx;		/* current index in w_lines[] */
-    int		srow;		/* starting row of the current line */
-
-    int		eof = FALSE;	/* if TRUE, we hit the end of the file */
-    int		didline = FALSE; /* if TRUE, we finished the last line */
-    int		i;
-    long	j;
-    static int	recursive = FALSE;	/* being called recursively */
-    int		old_botline = wp->w_botline;
-#ifdef FEAT_FOLDING
-    long	fold_count;
-#endif
-#ifdef FEAT_SYN_HL
-    /* remember what happened to the previous line, to know if
-     * check_visual_highlight() can be used */
-#define DID_NONE 1	/* didn't update a line */
-#define DID_LINE 2	/* updated a normal line */
-#define DID_FOLD 3	/* updated a folded line */
-    int		did_update = DID_NONE;
-    linenr_T	syntax_last_parsed = 0;		/* last parsed text line */
-#endif
-    linenr_T	mod_top = 0;
-    linenr_T	mod_bot = 0;
-#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
-    int		save_got_int;
-#endif
-#ifdef SYN_TIME_LIMIT
-    proftime_T	syntax_tm;
-#endif
-
-    type = wp->w_redr_type;
-
-    if (type == NOT_VALID)
-    {
-	wp->w_redr_status = TRUE;
-	wp->w_lines_valid = 0;
-    }
-
-    /* Window is zero-height: nothing to draw. */
-    if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
-    {
-	wp->w_redr_type = 0;
-	return;
-    }
-
-    /* Window is zero-width: Only need to draw the separator. */
-    if (wp->w_width == 0)
-    {
-	/* draw the vertical separator right of this window */
-	draw_vsep_win(wp, 0);
-	wp->w_redr_type = 0;
-	return;
-    }
-
-#ifdef FEAT_TERMINAL
-    // If this window contains a terminal, redraw works completely differently.
-    if (term_do_update_window(wp))
-    {
-	term_update_window(wp);
-# ifdef FEAT_MENU
-	/* Draw the window toolbar, if there is one. */
-	if (winbar_height(wp) > 0)
-	    redraw_win_toolbar(wp);
-# endif
-	wp->w_redr_type = 0;
-	return;
-    }
-#endif
-
-#ifdef FEAT_SEARCH_EXTRA
-    init_search_hl(wp, &search_hl);
-#endif
-
-#ifdef FEAT_LINEBREAK
-    /* Force redraw when width of 'number' or 'relativenumber' column
-     * changes. */
-    i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
-    if (wp->w_nrwidth != i)
-    {
-	type = NOT_VALID;
-	wp->w_nrwidth = i;
-    }
-    else
-#endif
-
-    if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
-    {
-	/*
-	 * When there are both inserted/deleted lines and specific lines to be
-	 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
-	 * everything (only happens when redrawing is off for while).
-	 */
-	type = NOT_VALID;
-    }
-    else
-    {
-	/*
-	 * Set mod_top to the first line that needs displaying because of
-	 * changes.  Set mod_bot to the first line after the changes.
-	 */
-	mod_top = wp->w_redraw_top;
-	if (wp->w_redraw_bot != 0)
-	    mod_bot = wp->w_redraw_bot + 1;
-	else
-	    mod_bot = 0;
-	if (buf->b_mod_set)
-	{
-	    if (mod_top == 0 || mod_top > buf->b_mod_top)
-	    {
-		mod_top = buf->b_mod_top;
-#ifdef FEAT_SYN_HL
-		/* Need to redraw lines above the change that may be included
-		 * in a pattern match. */
-		if (syntax_present(wp))
-		{
-		    mod_top -= buf->b_s.b_syn_sync_linebreaks;
-		    if (mod_top < 1)
-			mod_top = 1;
-		}
-#endif
-	    }
-	    if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
-		mod_bot = buf->b_mod_bot;
-
-#ifdef FEAT_SEARCH_EXTRA
-	    // When 'hlsearch' is on and using a multi-line search pattern, a
-	    // change in one line may make the Search highlighting in a
-	    // previous line invalid.  Simple solution: redraw all visible
-	    // lines above the change.
-	    // Same for a match pattern.
-	    if (search_hl.rm.regprog != NULL
-					&& re_multiline(search_hl.rm.regprog))
-		top_to_mod = TRUE;
-	    else
-	    {
-		matchitem_T *cur = wp->w_match_head;
-
-		while (cur != NULL)
-		{
-		    if (cur->match.regprog != NULL
-					   && re_multiline(cur->match.regprog))
-		    {
-			top_to_mod = TRUE;
-			break;
-		    }
-		    cur = cur->next;
-		}
-	    }
-#endif
-	}
-#ifdef FEAT_FOLDING
-	if (mod_top != 0 && hasAnyFolding(wp))
-	{
-	    linenr_T	lnumt, lnumb;
-
-	    /*
-	     * A change in a line can cause lines above it to become folded or
-	     * unfolded.  Find the top most buffer line that may be affected.
-	     * If the line was previously folded and displayed, get the first
-	     * line of that fold.  If the line is folded now, get the first
-	     * folded line.  Use the minimum of these two.
-	     */
-
-	    /* Find last valid w_lines[] entry above mod_top.  Set lnumt to
-	     * the line below it.  If there is no valid entry, use w_topline.
-	     * Find the first valid w_lines[] entry below mod_bot.  Set lnumb
-	     * to this line.  If there is no valid entry, use MAXLNUM. */
-	    lnumt = wp->w_topline;
-	    lnumb = MAXLNUM;
-	    for (i = 0; i < wp->w_lines_valid; ++i)
-		if (wp->w_lines[i].wl_valid)
-		{
-		    if (wp->w_lines[i].wl_lastlnum < mod_top)
-			lnumt = wp->w_lines[i].wl_lastlnum + 1;
-		    if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
-		    {
-			lnumb = wp->w_lines[i].wl_lnum;
-			/* When there is a fold column it might need updating
-			 * in the next line ("J" just above an open fold). */
-			if (compute_foldcolumn(wp, 0) > 0)
-			    ++lnumb;
-		    }
-		}
-
-	    (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
-	    if (mod_top > lnumt)
-		mod_top = lnumt;
-
-	    /* Now do the same for the bottom line (one above mod_bot). */
-	    --mod_bot;
-	    (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
-	    ++mod_bot;
-	    if (mod_bot < lnumb)
-		mod_bot = lnumb;
-	}
-#endif
-
-	/* When a change starts above w_topline and the end is below
-	 * w_topline, start redrawing at w_topline.
-	 * If the end of the change is above w_topline: do like no change was
-	 * made, but redraw the first line to find changes in syntax. */
-	if (mod_top != 0 && mod_top < wp->w_topline)
-	{
-	    if (mod_bot > wp->w_topline)
-		mod_top = wp->w_topline;
-#ifdef FEAT_SYN_HL
-	    else if (syntax_present(wp))
-		top_end = 1;
-#endif
-	}
-
-	/* When line numbers are displayed need to redraw all lines below
-	 * inserted/deleted lines. */
-	if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
-	    mod_bot = MAXLNUM;
-    }
-    wp->w_redraw_top = 0;	// reset for next time
-    wp->w_redraw_bot = 0;
-
-    /*
-     * When only displaying the lines at the top, set top_end.  Used when
-     * window has scrolled down for msg_scrolled.
-     */
-    if (type == REDRAW_TOP)
-    {
-	j = 0;
-	for (i = 0; i < wp->w_lines_valid; ++i)
-	{
-	    j += wp->w_lines[i].wl_size;
-	    if (j >= wp->w_upd_rows)
-	    {
-		top_end = j;
-		break;
-	    }
-	}
-	if (top_end == 0)
-	    /* not found (cannot happen?): redraw everything */
-	    type = NOT_VALID;
-	else
-	    /* top area defined, the rest is VALID */
-	    type = VALID;
-    }
-
-    /* Trick: we want to avoid clearing the screen twice.  screenclear() will
-     * set "screen_cleared" to TRUE.  The special value MAYBE (which is still
-     * non-zero and thus not FALSE) will indicate that screenclear() was not
-     * called. */
-    if (screen_cleared)
-	screen_cleared = MAYBE;
-
-    /*
-     * If there are no changes on the screen that require a complete redraw,
-     * handle three cases:
-     * 1: we are off the top of the screen by a few lines: scroll down
-     * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
-     * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
-     *    w_lines[] that needs updating.
-     */
-    if ((type == VALID || type == SOME_VALID
-				  || type == INVERTED || type == INVERTED_ALL)
-#ifdef FEAT_DIFF
-	    && !wp->w_botfill && !wp->w_old_botfill
-#endif
-	    )
-    {
-	if (mod_top != 0 && wp->w_topline == mod_top)
-	{
-	    /*
-	     * w_topline is the first changed line, the scrolling will be done
-	     * further down.
-	     */
-	}
-	else if (wp->w_lines[0].wl_valid
-		&& (wp->w_topline < wp->w_lines[0].wl_lnum
-#ifdef FEAT_DIFF
-		    || (wp->w_topline == wp->w_lines[0].wl_lnum
-			&& wp->w_topfill > wp->w_old_topfill)
-#endif
-		   ))
-	{
-	    /*
-	     * New topline is above old topline: May scroll down.
-	     */
-#ifdef FEAT_FOLDING
-	    if (hasAnyFolding(wp))
-	    {
-		linenr_T ln;
-
-		/* count the number of lines we are off, counting a sequence
-		 * of folded lines as one */
-		j = 0;
-		for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
-		{
-		    ++j;
-		    if (j >= wp->w_height - 2)
-			break;
-		    (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
-		}
-	    }
-	    else
-#endif
-		j = wp->w_lines[0].wl_lnum - wp->w_topline;
-	    if (j < wp->w_height - 2)		/* not too far off */
-	    {
-		i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
-#ifdef FEAT_DIFF
-		/* insert extra lines for previously invisible filler lines */
-		if (wp->w_lines[0].wl_lnum != wp->w_topline)
-		    i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
-							  - wp->w_old_topfill;
-#endif
-		if (i < wp->w_height - 2)	/* less than a screen off */
-		{
-		    /*
-		     * Try to insert the correct number of lines.
-		     * If not the last window, delete the lines at the bottom.
-		     * win_ins_lines may fail when the terminal can't do it.
-		     */
-		    if (i > 0)
-			check_for_delay(FALSE);
-		    if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
-		    {
-			if (wp->w_lines_valid != 0)
-			{
-			    /* Need to update rows that are new, stop at the
-			     * first one that scrolled down. */
-			    top_end = i;
-			    scrolled_down = TRUE;
-
-			    /* Move the entries that were scrolled, disable
-			     * the entries for the lines to be redrawn. */
-			    if ((wp->w_lines_valid += j) > wp->w_height)
-				wp->w_lines_valid = wp->w_height;
-			    for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
-				wp->w_lines[idx] = wp->w_lines[idx - j];
-			    while (idx >= 0)
-				wp->w_lines[idx--].wl_valid = FALSE;
-			}
-		    }
-		    else
-			mid_start = 0;		/* redraw all lines */
-		}
-		else
-		    mid_start = 0;		/* redraw all lines */
-	    }
-	    else
-		mid_start = 0;		/* redraw all lines */
-	}
-	else
-	{
-	    /*
-	     * New topline is at or below old topline: May scroll up.
-	     * When topline didn't change, find first entry in w_lines[] that
-	     * needs updating.
-	     */
-
-	    /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
-	    j = -1;
-	    row = 0;
-	    for (i = 0; i < wp->w_lines_valid; i++)
-	    {
-		if (wp->w_lines[i].wl_valid
-			&& wp->w_lines[i].wl_lnum == wp->w_topline)
-		{
-		    j = i;
-		    break;
-		}
-		row += wp->w_lines[i].wl_size;
-	    }
-	    if (j == -1)
-	    {
-		/* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
-		 * lines */
-		mid_start = 0;
-	    }
-	    else
-	    {
-		/*
-		 * Try to delete the correct number of lines.
-		 * wp->w_topline is at wp->w_lines[i].wl_lnum.
-		 */
-#ifdef FEAT_DIFF
-		/* If the topline didn't change, delete old filler lines,
-		 * otherwise delete filler lines of the new topline... */
-		if (wp->w_lines[0].wl_lnum == wp->w_topline)
-		    row += wp->w_old_topfill;
-		else
-		    row += diff_check_fill(wp, wp->w_topline);
-		/* ... but don't delete new filler lines. */
-		row -= wp->w_topfill;
-#endif
-		if (row > 0)
-		{
-		    check_for_delay(FALSE);
-		    if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
-									 == OK)
-			bot_start = wp->w_height - row;
-		    else
-			mid_start = 0;		/* redraw all lines */
-		}
-		if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
-		{
-		    /*
-		     * Skip the lines (below the deleted lines) that are still
-		     * valid and don't need redrawing.	Copy their info
-		     * upwards, to compensate for the deleted lines.  Set
-		     * bot_start to the first row that needs redrawing.
-		     */
-		    bot_start = 0;
-		    idx = 0;
-		    for (;;)
-		    {
-			wp->w_lines[idx] = wp->w_lines[j];
-			/* stop at line that didn't fit, unless it is still
-			 * valid (no lines deleted) */
-			if (row > 0 && bot_start + row
-				 + (int)wp->w_lines[j].wl_size > wp->w_height)
-			{
-			    wp->w_lines_valid = idx + 1;
-			    break;
-			}
-			bot_start += wp->w_lines[idx++].wl_size;
-
-			/* stop at the last valid entry in w_lines[].wl_size */
-			if (++j >= wp->w_lines_valid)
-			{
-			    wp->w_lines_valid = idx;
-			    break;
-			}
-		    }
-#ifdef FEAT_DIFF
-		    /* Correct the first entry for filler lines at the top
-		     * when it won't get updated below. */
-		    if (wp->w_p_diff && bot_start > 0)
-			wp->w_lines[0].wl_size =
-			    plines_win_nofill(wp, wp->w_topline, TRUE)
-							      + wp->w_topfill;
-#endif
-		}
-	    }
-	}
-
-	/* When starting redraw in the first line, redraw all lines.  When
-	 * there is only one window it's probably faster to clear the screen
-	 * first. */
-	if (mid_start == 0)
-	{
-	    mid_end = wp->w_height;
-	    if (ONE_WINDOW && !WIN_IS_POPUP(wp))
-	    {
-		/* Clear the screen when it was not done by win_del_lines() or
-		 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
-		 * then. */
-		if (screen_cleared != TRUE)
-		    screenclear();
-		/* The screen was cleared, redraw the tab pages line. */
-		if (redraw_tabline)
-		    draw_tabline();
-	    }
-	}
-
-	/* When win_del_lines() or win_ins_lines() caused the screen to be
-	 * cleared (only happens for the first window) or when screenclear()
-	 * was called directly above, "must_redraw" will have been set to
-	 * NOT_VALID, need to reset it here to avoid redrawing twice. */
-	if (screen_cleared == TRUE)
-	    must_redraw = 0;
-    }
-    else
-    {
-	/* Not VALID or INVERTED: redraw all lines. */
-	mid_start = 0;
-	mid_end = wp->w_height;
-    }
-
-    if (type == SOME_VALID)
-    {
-	/* SOME_VALID: redraw all lines. */
-	mid_start = 0;
-	mid_end = wp->w_height;
-	type = NOT_VALID;
-    }
-
-    /* check if we are updating or removing the inverted part */
-    if ((VIsual_active && buf == curwin->w_buffer)
-	    || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
-    {
-	linenr_T    from, to;
-
-	if (VIsual_active)
-	{
-	    if (VIsual_active
-		    && (VIsual_mode != wp->w_old_visual_mode
-			|| type == INVERTED_ALL))
-	    {
-		/*
-		 * If the type of Visual selection changed, redraw the whole
-		 * selection.  Also when the ownership of the X selection is
-		 * gained or lost.
-		 */
-		if (curwin->w_cursor.lnum < VIsual.lnum)
-		{
-		    from = curwin->w_cursor.lnum;
-		    to = VIsual.lnum;
-		}
-		else
-		{
-		    from = VIsual.lnum;
-		    to = curwin->w_cursor.lnum;
-		}
-		/* redraw more when the cursor moved as well */
-		if (wp->w_old_cursor_lnum < from)
-		    from = wp->w_old_cursor_lnum;
-		if (wp->w_old_cursor_lnum > to)
-		    to = wp->w_old_cursor_lnum;
-		if (wp->w_old_visual_lnum < from)
-		    from = wp->w_old_visual_lnum;
-		if (wp->w_old_visual_lnum > to)
-		    to = wp->w_old_visual_lnum;
-	    }
-	    else
-	    {
-		/*
-		 * Find the line numbers that need to be updated: The lines
-		 * between the old cursor position and the current cursor
-		 * position.  Also check if the Visual position changed.
-		 */
-		if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
-		{
-		    from = curwin->w_cursor.lnum;
-		    to = wp->w_old_cursor_lnum;
-		}
-		else
-		{
-		    from = wp->w_old_cursor_lnum;
-		    to = curwin->w_cursor.lnum;
-		    if (from == 0)	/* Visual mode just started */
-			from = to;
-		}
-
-		if (VIsual.lnum != wp->w_old_visual_lnum
-					|| VIsual.col != wp->w_old_visual_col)
-		{
-		    if (wp->w_old_visual_lnum < from
-						&& wp->w_old_visual_lnum != 0)
-			from = wp->w_old_visual_lnum;
-		    if (wp->w_old_visual_lnum > to)
-			to = wp->w_old_visual_lnum;
-		    if (VIsual.lnum < from)
-			from = VIsual.lnum;
-		    if (VIsual.lnum > to)
-			to = VIsual.lnum;
-		}
-	    }
-
-	    /*
-	     * If in block mode and changed column or curwin->w_curswant:
-	     * update all lines.
-	     * First compute the actual start and end column.
-	     */
-	    if (VIsual_mode == Ctrl_V)
-	    {
-		colnr_T	    fromc, toc;
-#if defined(FEAT_LINEBREAK)
-		int	    save_ve_flags = ve_flags;
-
-		if (curwin->w_p_lbr)
-		    ve_flags = VE_ALL;
-#endif
-		getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
-#if defined(FEAT_LINEBREAK)
-		ve_flags = save_ve_flags;
-#endif
-		++toc;
-		if (curwin->w_curswant == MAXCOL)
-		    toc = MAXCOL;
-
-		if (fromc != wp->w_old_cursor_fcol
-			|| toc != wp->w_old_cursor_lcol)
-		{
-		    if (from > VIsual.lnum)
-			from = VIsual.lnum;
-		    if (to < VIsual.lnum)
-			to = VIsual.lnum;
-		}
-		wp->w_old_cursor_fcol = fromc;
-		wp->w_old_cursor_lcol = toc;
-	    }
-	}
-	else
-	{
-	    /* Use the line numbers of the old Visual area. */
-	    if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
-	    {
-		from = wp->w_old_cursor_lnum;
-		to = wp->w_old_visual_lnum;
-	    }
-	    else
-	    {
-		from = wp->w_old_visual_lnum;
-		to = wp->w_old_cursor_lnum;
-	    }
-	}
-
-	/*
-	 * There is no need to update lines above the top of the window.
-	 */
-	if (from < wp->w_topline)
-	    from = wp->w_topline;
-
-	/*
-	 * If we know the value of w_botline, use it to restrict the update to
-	 * the lines that are visible in the window.
-	 */
-	if (wp->w_valid & VALID_BOTLINE)
-	{
-	    if (from >= wp->w_botline)
-		from = wp->w_botline - 1;
-	    if (to >= wp->w_botline)
-		to = wp->w_botline - 1;
-	}
-
-	/*
-	 * Find the minimal part to be updated.
-	 * Watch out for scrolling that made entries in w_lines[] invalid.
-	 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
-	 * top_end; need to redraw from top_end to the "to" line.
-	 * A middle mouse click with a Visual selection may change the text
-	 * above the Visual area and reset wl_valid, do count these for
-	 * mid_end (in srow).
-	 */
-	if (mid_start > 0)
-	{
-	    lnum = wp->w_topline;
-	    idx = 0;
-	    srow = 0;
-	    if (scrolled_down)
-		mid_start = top_end;
-	    else
-		mid_start = 0;
-	    while (lnum < from && idx < wp->w_lines_valid)	/* find start */
-	    {
-		if (wp->w_lines[idx].wl_valid)
-		    mid_start += wp->w_lines[idx].wl_size;
-		else if (!scrolled_down)
-		    srow += wp->w_lines[idx].wl_size;
-		++idx;
-# ifdef FEAT_FOLDING
-		if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
-		    lnum = wp->w_lines[idx].wl_lnum;
-		else
-# endif
-		    ++lnum;
-	    }
-	    srow += mid_start;
-	    mid_end = wp->w_height;
-	    for ( ; idx < wp->w_lines_valid; ++idx)		/* find end */
-	    {
-		if (wp->w_lines[idx].wl_valid
-			&& wp->w_lines[idx].wl_lnum >= to + 1)
-		{
-		    /* Only update until first row of this line */
-		    mid_end = srow;
-		    break;
-		}
-		srow += wp->w_lines[idx].wl_size;
-	    }
-	}
-    }
-
-    if (VIsual_active && buf == curwin->w_buffer)
-    {
-	wp->w_old_visual_mode = VIsual_mode;
-	wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
-	wp->w_old_visual_lnum = VIsual.lnum;
-	wp->w_old_visual_col = VIsual.col;
-	wp->w_old_curswant = curwin->w_curswant;
-    }
-    else
-    {
-	wp->w_old_visual_mode = 0;
-	wp->w_old_cursor_lnum = 0;
-	wp->w_old_visual_lnum = 0;
-	wp->w_old_visual_col = 0;
-    }
-
-#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
-    /* reset got_int, otherwise regexp won't work */
-    save_got_int = got_int;
-    got_int = 0;
-#endif
-#ifdef SYN_TIME_LIMIT
-    /* Set the time limit to 'redrawtime'. */
-    profile_setlimit(p_rdt, &syntax_tm);
-    syn_set_timeout(&syntax_tm);
-#endif
-#ifdef FEAT_FOLDING
-    win_foldinfo.fi_level = 0;
-#endif
-
-#ifdef FEAT_MENU
-    /*
-     * Draw the window toolbar, if there is one.
-     * TODO: only when needed.
-     */
-    if (winbar_height(wp) > 0)
-	redraw_win_toolbar(wp);
-#endif
-
-    /*
-     * Update all the window rows.
-     */
-    idx = 0;		/* first entry in w_lines[].wl_size */
-    row = 0;
-    srow = 0;
-    lnum = wp->w_topline;	/* first line shown in window */
-    for (;;)
-    {
-	/* stop updating when reached the end of the window (check for _past_
-	 * the end of the window is at the end of the loop) */
-	if (row == wp->w_height)
-	{
-	    didline = TRUE;
-	    break;
-	}
-
-	/* stop updating when hit the end of the file */
-	if (lnum > buf->b_ml.ml_line_count)
-	{
-	    eof = TRUE;
-	    break;
-	}
-
-	/* Remember the starting row of the line that is going to be dealt
-	 * with.  It is used further down when the line doesn't fit. */
-	srow = row;
-
-	/*
-	 * Update a line when it is in an area that needs updating, when it
-	 * has changes or w_lines[idx] is invalid.
-	 * "bot_start" may be halfway a wrapped line after using
-	 * win_del_lines(), check if the current line includes it.
-	 * When syntax folding is being used, the saved syntax states will
-	 * already have been updated, we can't see where the syntax state is
-	 * the same again, just update until the end of the window.
-	 */
-	if (row < top_end
-		|| (row >= mid_start && row < mid_end)
-#ifdef FEAT_SEARCH_EXTRA
-		|| top_to_mod
-#endif
-		|| idx >= wp->w_lines_valid
-		|| (row + wp->w_lines[idx].wl_size > bot_start)
-		|| (mod_top != 0
-		    && (lnum == mod_top
-			|| (lnum >= mod_top
-			    && (lnum < mod_bot
-#ifdef FEAT_SYN_HL
-				|| did_update == DID_FOLD
-				|| (did_update == DID_LINE
-				    && syntax_present(wp)
-				    && (
-# ifdef FEAT_FOLDING
-					(foldmethodIsSyntax(wp)
-						      && hasAnyFolding(wp)) ||
-# endif
-					syntax_check_changed(lnum)))
-#endif
-#ifdef FEAT_SEARCH_EXTRA
-				/* match in fixed position might need redraw
-				 * if lines were inserted or deleted */
-				|| (wp->w_match_head != NULL
-						    && buf->b_mod_xlines != 0)
-#endif
-				)))))
-	{
-#ifdef FEAT_SEARCH_EXTRA
-	    if (lnum == mod_top)
-		top_to_mod = FALSE;
-#endif
-
-	    /*
-	     * When at start of changed lines: May scroll following lines
-	     * up or down to minimize redrawing.
-	     * Don't do this when the change continues until the end.
-	     * Don't scroll when dollar_vcol >= 0, keep the "$".
-	     */
-	    if (lnum == mod_top
-		    && mod_bot != MAXLNUM
-		    && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
-	    {
-		int		old_rows = 0;
-		int		new_rows = 0;
-		int		xtra_rows;
-		linenr_T	l;
-
-		/* Count the old number of window rows, using w_lines[], which
-		 * should still contain the sizes for the lines as they are
-		 * currently displayed. */
-		for (i = idx; i < wp->w_lines_valid; ++i)
-		{
-		    /* Only valid lines have a meaningful wl_lnum.  Invalid
-		     * lines are part of the changed area. */
-		    if (wp->w_lines[i].wl_valid
-			    && wp->w_lines[i].wl_lnum == mod_bot)
-			break;
-		    old_rows += wp->w_lines[i].wl_size;
-#ifdef FEAT_FOLDING
-		    if (wp->w_lines[i].wl_valid
-			    && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
-		    {
-			/* Must have found the last valid entry above mod_bot.
-			 * Add following invalid entries. */
-			++i;
-			while (i < wp->w_lines_valid
-						  && !wp->w_lines[i].wl_valid)
-			    old_rows += wp->w_lines[i++].wl_size;
-			break;
-		    }
-#endif
-		}
-
-		if (i >= wp->w_lines_valid)
-		{
-		    /* We can't find a valid line below the changed lines,
-		     * need to redraw until the end of the window.
-		     * Inserting/deleting lines has no use. */
-		    bot_start = 0;
-		}
-		else
-		{
-		    /* Able to count old number of rows: Count new window
-		     * rows, and may insert/delete lines */
-		    j = idx;
-		    for (l = lnum; l < mod_bot; ++l)
-		    {
-#ifdef FEAT_FOLDING
-			if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
-			    ++new_rows;
-			else
-#endif
-#ifdef FEAT_DIFF
-			    if (l == wp->w_topline)
-			    new_rows += plines_win_nofill(wp, l, TRUE)
-							      + wp->w_topfill;
-			else
-#endif
-			    new_rows += plines_win(wp, l, TRUE);
-			++j;
-			if (new_rows > wp->w_height - row - 2)
-			{
-			    /* it's getting too much, must redraw the rest */
-			    new_rows = 9999;
-			    break;
-			}
-		    }
-		    xtra_rows = new_rows - old_rows;
-		    if (xtra_rows < 0)
-		    {
-			/* May scroll text up.  If there is not enough
-			 * remaining text or scrolling fails, must redraw the
-			 * rest.  If scrolling works, must redraw the text
-			 * below the scrolled text. */
-			if (row - xtra_rows >= wp->w_height - 2)
-			    mod_bot = MAXLNUM;
-			else
-			{
-			    check_for_delay(FALSE);
-			    if (win_del_lines(wp, row,
-					  -xtra_rows, FALSE, FALSE, 0) == FAIL)
-				mod_bot = MAXLNUM;
-			    else
-				bot_start = wp->w_height + xtra_rows;
-			}
-		    }
-		    else if (xtra_rows > 0)
-		    {
-			/* May scroll text down.  If there is not enough
-			 * remaining text of scrolling fails, must redraw the
-			 * rest. */
-			if (row + xtra_rows >= wp->w_height - 2)
-			    mod_bot = MAXLNUM;
-			else
-			{
-			    check_for_delay(FALSE);
-			    if (win_ins_lines(wp, row + old_rows,
-					     xtra_rows, FALSE, FALSE) == FAIL)
-				mod_bot = MAXLNUM;
-			    else if (top_end > row + old_rows)
-				/* Scrolled the part at the top that requires
-				 * updating down. */
-				top_end += xtra_rows;
-			}
-		    }
-
-		    /* When not updating the rest, may need to move w_lines[]
-		     * entries. */
-		    if (mod_bot != MAXLNUM && i != j)
-		    {
-			if (j < i)
-			{
-			    int x = row + new_rows;
-
-			    /* move entries in w_lines[] upwards */
-			    for (;;)
-			    {
-				/* stop at last valid entry in w_lines[] */
-				if (i >= wp->w_lines_valid)
-				{
-				    wp->w_lines_valid = j;
-				    break;
-				}
-				wp->w_lines[j] = wp->w_lines[i];
-				/* stop at a line that won't fit */
-				if (x + (int)wp->w_lines[j].wl_size
-							   > wp->w_height)
-				{
-				    wp->w_lines_valid = j + 1;
-				    break;
-				}
-				x += wp->w_lines[j++].wl_size;
-				++i;
-			    }
-			    if (bot_start > x)
-				bot_start = x;
-			}
-			else /* j > i */
-			{
-			    /* move entries in w_lines[] downwards */
-			    j -= i;
-			    wp->w_lines_valid += j;
-			    if (wp->w_lines_valid > wp->w_height)
-				wp->w_lines_valid = wp->w_height;
-			    for (i = wp->w_lines_valid; i - j >= idx; --i)
-				wp->w_lines[i] = wp->w_lines[i - j];
-
-			    /* The w_lines[] entries for inserted lines are
-			     * now invalid, but wl_size may be used above.
-			     * Reset to zero. */
-			    while (i >= idx)
-			    {
-				wp->w_lines[i].wl_size = 0;
-				wp->w_lines[i--].wl_valid = FALSE;
-			    }
-			}
-		    }
-		}
-	    }
-
-#ifdef FEAT_FOLDING
-	    /*
-	     * When lines are folded, display one line for all of them.
-	     * Otherwise, display normally (can be several display lines when
-	     * 'wrap' is on).
-	     */
-	    fold_count = foldedCount(wp, lnum, &win_foldinfo);
-	    if (fold_count != 0)
-	    {
-		fold_line(wp, fold_count, &win_foldinfo, lnum, row);
-		++row;
-		--fold_count;
-		wp->w_lines[idx].wl_folded = TRUE;
-		wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
-# ifdef FEAT_SYN_HL
-		did_update = DID_FOLD;
-# endif
-	    }
-	    else
-#endif
-	    if (idx < wp->w_lines_valid
-		    && wp->w_lines[idx].wl_valid
-		    && wp->w_lines[idx].wl_lnum == lnum
-		    && lnum > wp->w_topline
-		    && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
-		    && !WIN_IS_POPUP(wp)
-		    && srow + wp->w_lines[idx].wl_size > wp->w_height
-#ifdef FEAT_DIFF
-		    && diff_check_fill(wp, lnum) == 0
-#endif
-		    )
-	    {
-		/* This line is not going to fit.  Don't draw anything here,
-		 * will draw "@  " lines below. */
-		row = wp->w_height + 1;
-	    }
-	    else
-	    {
-#ifdef FEAT_SEARCH_EXTRA
-		prepare_search_hl(wp, &search_hl, lnum);
-#endif
-#ifdef FEAT_SYN_HL
-		/* Let the syntax stuff know we skipped a few lines. */
-		if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
-						       && syntax_present(wp))
-		    syntax_end_parsing(syntax_last_parsed + 1);
-#endif
-
-		/*
-		 * Display one line.
-		 */
-		row = win_line(wp, lnum, srow, wp->w_height,
-							  mod_top == 0, FALSE);
-
-#ifdef FEAT_FOLDING
-		wp->w_lines[idx].wl_folded = FALSE;
-		wp->w_lines[idx].wl_lastlnum = lnum;
-#endif
-#ifdef FEAT_SYN_HL
-		did_update = DID_LINE;
-		syntax_last_parsed = lnum;
-#endif
-	    }
-
-	    wp->w_lines[idx].wl_lnum = lnum;
-	    wp->w_lines[idx].wl_valid = TRUE;
-
-	    /* Past end of the window or end of the screen. Note that after
-	     * resizing wp->w_height may be end up too big. That's a problem
-	     * elsewhere, but prevent a crash here. */
-	    if (row > wp->w_height || row + wp->w_winrow >= Rows)
-	    {
-		/* we may need the size of that too long line later on */
-		if (dollar_vcol == -1)
-		    wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
-		++idx;
-		break;
-	    }
-	    if (dollar_vcol == -1)
-		wp->w_lines[idx].wl_size = row - srow;
-	    ++idx;
-#ifdef FEAT_FOLDING
-	    lnum += fold_count + 1;
-#else
-	    ++lnum;
-#endif
-	}
-	else
-	{
-	    if (wp->w_p_rnu)
-	    {
-#ifdef FEAT_FOLDING
-		// 'relativenumber' set: The text doesn't need to be drawn, but
-		// the number column nearly always does.
-		fold_count = foldedCount(wp, lnum, &win_foldinfo);
-		if (fold_count != 0)
-		    fold_line(wp, fold_count, &win_foldinfo, lnum, row);
-		else
-#endif
-		    (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
-	    }
-
-	    // This line does not need to be drawn, advance to the next one.
-	    row += wp->w_lines[idx++].wl_size;
-	    if (row > wp->w_height)	/* past end of screen */
-		break;
-#ifdef FEAT_FOLDING
-	    lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
-#else
-	    ++lnum;
-#endif
-#ifdef FEAT_SYN_HL
-	    did_update = DID_NONE;
-#endif
-	}
-
-	if (lnum > buf->b_ml.ml_line_count)
-	{
-	    eof = TRUE;
-	    break;
-	}
-    }
-    /*
-     * End of loop over all window lines.
-     */
-
-#ifdef FEAT_VTP
-    /* Rewrite the character at the end of the screen line. */
-    if (use_vtp())
-    {
-	int i;
-
-	for (i = 0; i < Rows; ++i)
-	    if (enc_utf8)
-		if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
-					   LineOffset[i] + screen_Columns) > 1)
-		    screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
-		else
-		    screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
-	    else
-		screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
-    }
-#endif
-
-    if (idx > wp->w_lines_valid)
-	wp->w_lines_valid = idx;
-
-#ifdef FEAT_SYN_HL
-    /*
-     * Let the syntax stuff know we stop parsing here.
-     */
-    if (syntax_last_parsed != 0 && syntax_present(wp))
-	syntax_end_parsing(syntax_last_parsed + 1);
-#endif
-
-    /*
-     * If we didn't hit the end of the file, and we didn't finish the last
-     * line we were working on, then the line didn't fit.
-     */
-    wp->w_empty_rows = 0;
-#ifdef FEAT_DIFF
-    wp->w_filler_rows = 0;
-#endif
-    if (!eof && !didline)
-    {
-	if (lnum == wp->w_topline)
-	{
-	    /*
-	     * Single line that does not fit!
-	     * Don't overwrite it, it can be edited.
-	     */
-	    wp->w_botline = lnum + 1;
-	}
-#ifdef FEAT_DIFF
-	else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
-	{
-	    /* Window ends in filler lines. */
-	    wp->w_botline = lnum;
-	    wp->w_filler_rows = wp->w_height - srow;
-	}
-#endif
-#ifdef FEAT_TEXT_PROP
-	else if (WIN_IS_POPUP(wp))
-	{
-	    // popup line that doesn't fit is left as-is
-	    wp->w_botline = lnum;
-	}
-#endif
-	else if (dy_flags & DY_TRUNCATE)	/* 'display' has "truncate" */
-	{
-	    int scr_row = W_WINROW(wp) + wp->w_height - 1;
-
-	    /*
-	     * Last line isn't finished: Display "@@@" in the last screen line.
-	     */
-	    screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
-							      HL_ATTR(HLF_AT));
-	    screen_fill(scr_row, scr_row + 1,
-		    (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
-		    '@', ' ', HL_ATTR(HLF_AT));
-	    set_empty_rows(wp, srow);
-	    wp->w_botline = lnum;
-	}
-	else if (dy_flags & DY_LASTLINE)	/* 'display' has "lastline" */
-	{
-	    /*
-	     * Last line isn't finished: Display "@@@" at the end.
-	     */
-	    screen_fill(W_WINROW(wp) + wp->w_height - 1,
-		    W_WINROW(wp) + wp->w_height,
-		    (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
-		    '@', '@', HL_ATTR(HLF_AT));
-	    set_empty_rows(wp, srow);
-	    wp->w_botline = lnum;
-	}
-	else
-	{
-	    win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
-	    wp->w_botline = lnum;
-	}
-    }
-    else
-    {
-	draw_vsep_win(wp, row);
-	if (eof)		/* we hit the end of the file */
-	{
-	    wp->w_botline = buf->b_ml.ml_line_count + 1;
-#ifdef FEAT_DIFF
-	    j = diff_check_fill(wp, wp->w_botline);
-	    if (j > 0 && !wp->w_botfill)
-	    {
-		// Display filler lines at the end of the file.
-		if (char2cells(fill_diff) > 1)
-		    i = '-';
-		else
-		    i = fill_diff;
-		if (row + j > wp->w_height)
-		    j = wp->w_height - row;
-		win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
-		row += j;
-	    }
-#endif
-	}
-	else if (dollar_vcol == -1)
-	    wp->w_botline = lnum;
-
-	// Make sure the rest of the screen is blank
-	// put '~'s on rows that aren't part of the file.
-	win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
-				       ' ', FALSE, row, wp->w_height, HLF_EOB);
-    }
-
-#ifdef SYN_TIME_LIMIT
-    syn_set_timeout(NULL);
-#endif
-
-    /* Reset the type of redrawing required, the window has been updated. */
-    wp->w_redr_type = 0;
-#ifdef FEAT_DIFF
-    wp->w_old_topfill = wp->w_topfill;
-    wp->w_old_botfill = wp->w_botfill;
-#endif
-
-    if (dollar_vcol == -1)
-    {
-	/*
-	 * There is a trick with w_botline.  If we invalidate it on each
-	 * change that might modify it, this will cause a lot of expensive
-	 * calls to plines() in update_topline() each time.  Therefore the
-	 * value of w_botline is often approximated, and this value is used to
-	 * compute the value of w_topline.  If the value of w_botline was
-	 * wrong, check that the value of w_topline is correct (cursor is on
-	 * the visible part of the text).  If it's not, we need to redraw
-	 * again.  Mostly this just means scrolling up a few lines, so it
-	 * doesn't look too bad.  Only do this for the current window (where
-	 * changes are relevant).
-	 */
-	wp->w_valid |= VALID_BOTLINE;
-	if (wp == curwin && wp->w_botline != old_botline && !recursive)
-	{
-	    recursive = TRUE;
-	    curwin->w_valid &= ~VALID_TOPLINE;
-	    update_topline();	/* may invalidate w_botline again */
-	    if (must_redraw != 0)
-	    {
-		/* Don't update for changes in buffer again. */
-		i = curbuf->b_mod_set;
-		curbuf->b_mod_set = FALSE;
-		win_update(curwin);
-		must_redraw = 0;
-		curbuf->b_mod_set = i;
-	    }
-	    recursive = FALSE;
-	}
-    }
-
-#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
-    /* restore got_int, unless CTRL-C was hit while redrawing */
-    if (!got_int)
-	got_int = save_got_int;
-#endif
-}
-
 /*
  * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
  * Return the new offset.
@@ -2399,7 +159,7 @@ screen_fill_end(
  * use "c2" as the filler character.
  * When "draw_margin" is TRUE then draw the sign, fold and number columns.
  */
-    static void
+    void
 win_draw_end(
     win_T	*wp,
     int		c1,
@@ -2459,155 +219,12 @@ win_draw_end(
     set_empty_rows(wp, row);
 }
 
-#ifdef FEAT_SYN_HL
-/*
- * Advance **color_cols and return TRUE when there are columns to draw.
- */
-    static int
-advance_color_col(int vcol, int **color_cols)
-{
-    while (**color_cols >= 0 && vcol > **color_cols)
-	++*color_cols;
-    return (**color_cols >= 0);
-}
-#endif
-
-#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
-/*
- * Copy "text" to ScreenLines using "attr".
- * Returns the next screen column.
- */
-    static int
-text_to_screenline(win_T *wp, char_u *text, int col)
-{
-    int		off = (int)(current_ScreenLine - ScreenLines);
-
-    if (has_mbyte)
-    {
-	int	cells;
-	int	u8c, u8cc[MAX_MCO];
-	int	i;
-	int	idx;
-	int	c_len;
-	char_u	*p;
-# ifdef FEAT_ARABIC
-	int	prev_c = 0;		/* previous Arabic character */
-	int	prev_c1 = 0;		/* first composing char for prev_c */
-# endif
-
-# ifdef FEAT_RIGHTLEFT
-	if (wp->w_p_rl)
-	    idx = off;
-	else
-# endif
-	    idx = off + col;
-
-	/* Store multibyte characters in ScreenLines[] et al. correctly. */
-	for (p = text; *p != NUL; )
-	{
-	    cells = (*mb_ptr2cells)(p);
-	    c_len = (*mb_ptr2len)(p);
-	    if (col + cells > wp->w_width
-# ifdef FEAT_RIGHTLEFT
-		    - (wp->w_p_rl ? col : 0)
-# endif
-		    )
-		break;
-	    ScreenLines[idx] = *p;
-	    if (enc_utf8)
-	    {
-		u8c = utfc_ptr2char(p, u8cc);
-		if (*p < 0x80 && u8cc[0] == 0)
-		{
-		    ScreenLinesUC[idx] = 0;
-#ifdef FEAT_ARABIC
-		    prev_c = u8c;
-#endif
-		}
-		else
-		{
-#ifdef FEAT_ARABIC
-		    if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
-		    {
-			/* Do Arabic shaping. */
-			int	pc, pc1, nc;
-			int	pcc[MAX_MCO];
-			int	firstbyte = *p;
-
-			/* The idea of what is the previous and next
-			 * character depends on 'rightleft'. */
-			if (wp->w_p_rl)
-			{
-			    pc = prev_c;
-			    pc1 = prev_c1;
-			    nc = utf_ptr2char(p + c_len);
-			    prev_c1 = u8cc[0];
-			}
-			else
-			{
-			    pc = utfc_ptr2char(p + c_len, pcc);
-			    nc = prev_c;
-			    pc1 = pcc[0];
-			}
-			prev_c = u8c;
-
-			u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
-								 pc, pc1, nc);
-			ScreenLines[idx] = firstbyte;
-		    }
-		    else
-			prev_c = u8c;
-#endif
-		    /* Non-BMP character: display as ? or fullwidth ?. */
-		    ScreenLinesUC[idx] = u8c;
-		    for (i = 0; i < Screen_mco; ++i)
-		    {
-			ScreenLinesC[i][idx] = u8cc[i];
-			if (u8cc[i] == 0)
-			    break;
-		    }
-		}
-		if (cells > 1)
-		    ScreenLines[idx + 1] = 0;
-	    }
-	    else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
-		/* double-byte single width character */
-		ScreenLines2[idx] = p[1];
-	    else if (cells > 1)
-		/* double-width character */
-		ScreenLines[idx + 1] = p[1];
-	    col += cells;
-	    idx += cells;
-	    p += c_len;
-	}
-    }
-    else
-    {
-	int len = (int)STRLEN(text);
-
-	if (len > wp->w_width - col)
-	    len = wp->w_width - col;
-	if (len > 0)
-	{
-#ifdef FEAT_RIGHTLEFT
-	    if (wp->w_p_rl)
-		mch_memmove(current_ScreenLine, text, len);
-	    else
-#endif
-		mch_memmove(current_ScreenLine + col, text, len);
-	    col += len;
-	}
-    }
-    return col;
-}
-#endif
-
-#ifdef FEAT_FOLDING
+#if defined(FEAT_FOLDING) || defined(PROTO)
 /*
  * Compute the width of the foldcolumn.  Based on 'foldcolumn' and how much
  * space is available for window "wp", minus "col".
  */
-    static int
+    int
 compute_foldcolumn(win_T *wp, int col)
 {
     int fdc = wp->w_p_fdc;
@@ -2620,343 +237,10 @@ compute_foldcolumn(win_T *wp, int col)
 }
 
 /*
- * Display one folded line.
- */
-    static void
-fold_line(
-    win_T	*wp,
-    long	fold_count,
-    foldinfo_T	*foldinfo,
-    linenr_T	lnum,
-    int		row)
-{
-    char_u	buf[FOLD_TEXT_LEN];
-    pos_T	*top, *bot;
-    linenr_T	lnume = lnum + fold_count - 1;
-    int		len;
-    char_u	*text;
-    int		fdc;
-    int		col;
-    int		txtcol;
-    int		off = (int)(current_ScreenLine - ScreenLines);
-    int		ri;
-
-    /* Build the fold line:
-     * 1. Add the cmdwin_type for the command-line window
-     * 2. Add the 'foldcolumn'
-     * 3. Add the 'number' or 'relativenumber' column
-     * 4. Compose the text
-     * 5. Add the text
-     * 6. set highlighting for the Visual area an other text
-     */
-    col = 0;
-
-    /*
-     * 1. Add the cmdwin_type for the command-line window
-     * Ignores 'rightleft', this window is never right-left.
-     */
-#ifdef FEAT_CMDWIN
-    if (cmdwin_type != 0 && wp == curwin)
-    {
-	ScreenLines[off] = cmdwin_type;
-	ScreenAttrs[off] = HL_ATTR(HLF_AT);
-	if (enc_utf8)
-	    ScreenLinesUC[off] = 0;
-	++col;
-    }
-#endif
-
-    /*
-     * 2. Add the 'foldcolumn'
-     *    Reduce the width when there is not enough space.
-     */
-    fdc = compute_foldcolumn(wp, col);
-    if (fdc > 0)
-    {
-	fill_foldcolumn(buf, wp, TRUE, lnum);
-#ifdef FEAT_RIGHTLEFT
-	if (wp->w_p_rl)
-	{
-	    int		i;
-
-	    copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
-							     HL_ATTR(HLF_FC));
-	    /* reverse the fold column */
-	    for (i = 0; i < fdc; ++i)
-		ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
-	}
-	else
-#endif
-	    copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
-	col += fdc;
-    }
-
-#ifdef FEAT_RIGHTLEFT
-# define RL_MEMSET(p, v, l) \
-    do { \
-	if (wp->w_p_rl) \
-	    for (ri = 0; ri < l; ++ri) \
-	       ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
-	 else \
-	    for (ri = 0; ri < l; ++ri) \
-	       ScreenAttrs[off + (p) + ri] = v; \
-    } while (0)
-#else
-# define RL_MEMSET(p, v, l) \
-    do { \
-	for (ri = 0; ri < l; ++ri) \
-	    ScreenAttrs[off + (p) + ri] = v; \
-    } while (0)
-#endif
-
-    /* Set all attributes of the 'number' or 'relativenumber' column and the
-     * text */
-    RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
-
-#ifdef FEAT_SIGNS
-    /* If signs are being displayed, add two spaces. */
-    if (signcolumn_on(wp))
-    {
-	len = wp->w_width - col;
-	if (len > 0)
-	{
-	    if (len > 2)
-		len = 2;
-# ifdef FEAT_RIGHTLEFT
-	    if (wp->w_p_rl)
-		/* the line number isn't reversed */
-		copy_text_attr(off + wp->w_width - len - col,
-					(char_u *)"  ", len, HL_ATTR(HLF_FL));
-	    else
-# endif
-		copy_text_attr(off + col, (char_u *)"  ", len, HL_ATTR(HLF_FL));
-	    col += len;
-	}
-    }
-#endif
-
-    /*
-     * 3. Add the 'number' or 'relativenumber' column
-     */
-    if (wp->w_p_nu || wp->w_p_rnu)
-    {
-	len = wp->w_width - col;
-	if (len > 0)
-	{
-	    int	    w = number_width(wp);
-	    long    num;
-	    char    *fmt = "%*ld ";
-
-	    if (len > w + 1)
-		len = w + 1;
-
-	    if (wp->w_p_nu && !wp->w_p_rnu)
-		/* 'number' + 'norelativenumber' */
-		num = (long)lnum;
-	    else
-	    {
-		/* 'relativenumber', don't use negative numbers */
-		num = labs((long)get_cursor_rel_lnum(wp, lnum));
-		if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
-		{
-		    /* 'number' + 'relativenumber': cursor line shows absolute
-		     * line number */
-		    num = lnum;
-		    fmt = "%-*ld ";
-		}
-	    }
-
-	    sprintf((char *)buf, fmt, w, num);
-#ifdef FEAT_RIGHTLEFT
-	    if (wp->w_p_rl)
-		/* the line number isn't reversed */
-		copy_text_attr(off + wp->w_width - len - col, buf, len,
-							     HL_ATTR(HLF_FL));
-	    else
-#endif
-		copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
-	    col += len;
-	}
-    }
-
-    /*
-     * 4. Compose the folded-line string with 'foldtext', if set.
-     */
-    text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
-
-    txtcol = col;	/* remember where text starts */
-
-    /*
-     * 5. move the text to current_ScreenLine.  Fill up with "fill_fold".
-     *    Right-left text is put in columns 0 - number-col, normal text is put
-     *    in columns number-col - window-width.
-     */
-    col = text_to_screenline(wp, text, col);
-
-    /* Fill the rest of the line with the fold filler */
-#ifdef FEAT_RIGHTLEFT
-    if (wp->w_p_rl)
-	col -= txtcol;
-#endif
-    while (col < wp->w_width
-#ifdef FEAT_RIGHTLEFT
-		    - (wp->w_p_rl ? txtcol : 0)
-#endif
-	    )
-    {
-	if (enc_utf8)
-	{
-	    if (fill_fold >= 0x80)
-	    {
-		ScreenLinesUC[off + col] = fill_fold;
-		ScreenLinesC[0][off + col] = 0;
-		ScreenLines[off + col] = 0x80; /* avoid storing zero */
-	    }
-	    else
-	    {
-		ScreenLinesUC[off + col] = 0;
-		ScreenLines[off + col] = fill_fold;
-	    }
-	    col++;
-	}
-	else
-	    ScreenLines[off + col++] = fill_fold;
-    }
-
-    if (text != buf)
-	vim_free(text);
-
-    /*
-     * 6. set highlighting for the Visual area an other text.
-     * If all folded lines are in the Visual area, highlight the line.
-     */
-    if (VIsual_active && wp->w_buffer == curwin->w_buffer)
-    {
-	if (LTOREQ_POS(curwin->w_cursor, VIsual))
-	{
-	    /* Visual is after curwin->w_cursor */
-	    top = &curwin->w_cursor;
-	    bot = &VIsual;
-	}
-	else
-	{
-	    /* Visual is before curwin->w_cursor */
-	    top = &VIsual;
-	    bot = &curwin->w_cursor;
-	}
-	if (lnum >= top->lnum
-		&& lnume <= bot->lnum
-		&& (VIsual_mode != 'v'
-		    || ((lnum > top->lnum
-			    || (lnum == top->lnum
-				&& top->col == 0))
-			&& (lnume < bot->lnum
-			    || (lnume == bot->lnum
-				&& (bot->col - (*p_sel == 'e'))
-		>= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
-	{
-	    if (VIsual_mode == Ctrl_V)
-	    {
-		/* Visual block mode: highlight the chars part of the block */
-		if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
-		{
-		    if (wp->w_old_cursor_lcol != MAXCOL
-			     && wp->w_old_cursor_lcol + txtcol
-						       < (colnr_T)wp->w_width)
-			len = wp->w_old_cursor_lcol;
-		    else
-			len = wp->w_width - txtcol;
-		    RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
-					    len - (int)wp->w_old_cursor_fcol);
-		}
-	    }
-	    else
-	    {
-		/* Set all attributes of the text */
-		RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
-	    }
-	}
-    }
-
-#ifdef FEAT_SYN_HL
-    /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
-    if (wp->w_p_cc_cols)
-    {
-	int i = 0;
-	int j = wp->w_p_cc_cols[i];
-	int old_txtcol = txtcol;
-
-	while (j > -1)
-	{
-	    txtcol += j;
-	    if (wp->w_p_wrap)
-		txtcol -= wp->w_skipcol;
-	    else
-		txtcol -= wp->w_leftcol;
-	    if (txtcol >= 0 && txtcol < wp->w_width)
-		ScreenAttrs[off + txtcol] = hl_combine_attr(
-				    ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
-	    txtcol = old_txtcol;
-	    j = wp->w_p_cc_cols[++i];
-	}
-    }
-
-    /* Show 'cursorcolumn' in the fold line. */
-    if (wp->w_p_cuc)
-    {
-	txtcol += wp->w_virtcol;
-	if (wp->w_p_wrap)
-	    txtcol -= wp->w_skipcol;
-	else
-	    txtcol -= wp->w_leftcol;
-	if (txtcol >= 0 && txtcol < wp->w_width)
-	    ScreenAttrs[off + txtcol] = hl_combine_attr(
-				 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
-    }
-#endif
-
-    screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
-						     (int)wp->w_width, 0);
-
-    /*
-     * Update w_cline_height and w_cline_folded if the cursor line was
-     * updated (saves a call to plines() later).
-     */
-    if (wp == curwin
-	    && lnum <= curwin->w_cursor.lnum
-	    && lnume >= curwin->w_cursor.lnum)
-    {
-	curwin->w_cline_row = row;
-	curwin->w_cline_height = 1;
-	curwin->w_cline_folded = TRUE;
-	curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
-    }
-}
-
-/*
- * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
- */
-    static void
-copy_text_attr(
-    int		off,
-    char_u	*buf,
-    int		len,
-    int		attr)
-{
-    int		i;
-
-    mch_memmove(ScreenLines + off, buf, (size_t)len);
-    if (enc_utf8)
-	vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
-    for (i = 0; i < len; ++i)
-	ScreenAttrs[off + i] = attr;
-}
-
-/*
  * Fill the foldcolumn at "p" for window "wp".
  * Only to be called when 'foldcolumn' > 0.
  */
-    static void
+    void
 fill_foldcolumn(
     char_u	*p,
     win_T	*wp,
@@ -3004,3108 +288,6 @@ fill_foldcolumn(
 }
 #endif /* FEAT_FOLDING */
 
-#ifdef FEAT_TEXT_PROP
-static textprop_T	*current_text_props = NULL;
-static buf_T		*current_buf = NULL;
-
-    static int
-text_prop_compare(const void *s1, const void *s2)
-{
-    int  idx1, idx2;
-    proptype_T  *pt1, *pt2;
-    colnr_T col1, col2;
-
-    idx1 = *(int *)s1;
-    idx2 = *(int *)s2;
-    pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
-    pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
-    if (pt1 == pt2)
-	return 0;
-    if (pt1 == NULL)
-	return -1;
-    if (pt2 == NULL)
-	return 1;
-    if (pt1->pt_priority != pt2->pt_priority)
-	return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
-    col1 = current_text_props[idx1].tp_col;
-    col2 = current_text_props[idx2].tp_col;
-    return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
-}
-#endif
-
-#ifdef FEAT_SIGNS
-/*
- * Get information needed to display the sign in line 'lnum' in window 'wp'.
- * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
- * Otherwise the sign is going to be displayed in the sign column.
- */
-    static void
-get_sign_display_info(
-	int		nrcol,
-	win_T		*wp,
-	linenr_T	lnum UNUSED,
-	sign_attrs_T	*sattr,
-	int		wcr_attr,
-	int		row,
-	int		startrow,
-	int		filler_lines UNUSED,
-	int		filler_todo UNUSED,
-	int		*c_extrap,
-	int		*c_finalp,
-	char_u		*extra,
-	char_u		**pp_extra,
-	int		*n_extrap,
-	int		*char_attrp)
-{
-    int	text_sign;
-# ifdef FEAT_SIGN_ICONS
-    int	icon_sign;
-# endif
-
-    // Draw two cells with the sign value or blank.
-    *c_extrap = ' ';
-    *c_finalp = NUL;
-    if (nrcol)
-	*n_extrap = number_width(wp) + 1;
-    else
-    {
-	*char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
-	*n_extrap = 2;
-    }
-
-    if (row == startrow
-#ifdef FEAT_DIFF
-	    + filler_lines && filler_todo <= 0
-#endif
-       )
-    {
-	text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
-# ifdef FEAT_SIGN_ICONS
-	icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
-	if (gui.in_use && icon_sign != 0)
-	{
-	    // Use the image in this position.
-	    if (nrcol)
-	    {
-		*c_extrap = NUL;
-		sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
-		*pp_extra = extra;
-		*n_extrap = (int)STRLEN(*pp_extra);
-	    }
-	    else
-		*c_extrap = SIGN_BYTE;
-#  ifdef FEAT_NETBEANS_INTG
-	    if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
-	    {
-		if (nrcol)
-		{
-		    *c_extrap = NUL;
-		    sprintf((char *)extra, "%-*c ", number_width(wp),
-							MULTISIGN_BYTE);
-		    *pp_extra = extra;
-		    *n_extrap = (int)STRLEN(*pp_extra);
-		}
-		else
-		    *c_extrap = MULTISIGN_BYTE;
-	    }
-#  endif
-	    *c_finalp = NUL;
-	    *char_attrp = icon_sign;
-	}
-	else
-# endif
-	    if (text_sign != 0)
-	    {
-		*pp_extra = sattr->text;
-		if (*pp_extra != NULL)
-		{
-		    if (nrcol)
-		    {
-			int n, width = number_width(wp) - 2;
-
-			for (n = 0; n < width; n++)
-			    extra[n] = ' ';
-			extra[n] = 0;
-			STRCAT(extra, *pp_extra);
-			STRCAT(extra, " ");
-			*pp_extra = extra;
-		    }
-		    *c_extrap = NUL;
-		    *c_finalp = NUL;
-		    *n_extrap = (int)STRLEN(*pp_extra);
-		}
-		*char_attrp = sattr->texthl;
-	    }
-    }
-}
-#endif
-
-/*
- * Display line "lnum" of window 'wp' on the screen.
- * Start at row "startrow", stop when "endrow" is reached.
- * wp->w_virtcol needs to be valid.
- *
- * Return the number of last row the line occupies.
- */
-    static int
-win_line(
-    win_T	*wp,
-    linenr_T	lnum,
-    int		startrow,
-    int		endrow,
-    int		nochange UNUSED,	// not updating for changed text
-    int		number_only)		// only update the number column
-{
-    int		col = 0;		// visual column on screen
-    unsigned	off;			// offset in ScreenLines/ScreenAttrs
-    int		c = 0;			// init for GCC
-    long	vcol = 0;		// virtual column (for tabs)
-#ifdef FEAT_LINEBREAK
-    long	vcol_sbr = -1;		// virtual column after showbreak
-#endif
-    long	vcol_prev = -1;		// "vcol" of previous character
-    char_u	*line;			// current line
-    char_u	*ptr;			// current position in "line"
-    int		row;			// row in the window, excl w_winrow
-    int		screen_row;		// row on the screen, incl w_winrow
-
-    char_u	extra[21];		// "%ld " and 'fdc' must fit in here
-    int		n_extra = 0;		// number of extra chars
-    char_u	*p_extra = NULL;	// string of extra chars, plus NUL
-    char_u	*p_extra_free = NULL;   // p_extra needs to be freed
-    int		c_extra = NUL;		// extra chars, all the same
-    int		c_final = NUL;		// final char, mandatory if set
-    int		extra_attr = 0;		// attributes when n_extra != 0
-    static char_u *at_end_str = (char_u *)""; // used for p_extra when
-					   // displaying lcs_eol at end-of-line
-    int		lcs_eol_one = lcs_eol;	// lcs_eol until it's been used
-    int		lcs_prec_todo = lcs_prec;   // lcs_prec until it's been used
-
-    // saved "extra" items for when draw_state becomes WL_LINE (again)
-    int		saved_n_extra = 0;
-    char_u	*saved_p_extra = NULL;
-    int		saved_c_extra = 0;
-    int		saved_c_final = 0;
-    int		saved_char_attr = 0;
-
-    int		n_attr = 0;		/* chars with special attr */
-    int		saved_attr2 = 0;	/* char_attr saved for n_attr */
-    int		n_attr3 = 0;		/* chars with overruling special attr */
-    int		saved_attr3 = 0;	/* char_attr saved for n_attr3 */
-
-    int		n_skip = 0;		/* nr of chars to skip for 'nowrap' */
-
-    int		fromcol = -10;		// start of inverting
-    int		tocol = MAXCOL;		// end of inverting
-    int		fromcol_prev = -2;	// start of inverting after cursor
-    int		noinvcur = FALSE;	// don't invert the cursor
-    pos_T	*top, *bot;
-    int		lnum_in_visual_area = FALSE;
-    pos_T	pos;
-    long	v;
-
-    int		char_attr = 0;		// attributes for next character
-    int		attr_pri = FALSE;	// char_attr has priority
-    int		area_highlighting = FALSE; // Visual or incsearch highlighting
-					   // in this line
-    int		vi_attr = 0;		// attributes for Visual and incsearch
-					// highlighting
-    int		wcr_attr = 0;		// attributes from 'wincolor'
-    int		win_attr = 0;		// background for whole window, except
-					// margins and "~" lines.
-    int		area_attr = 0;		// attributes desired by highlighting
-    int		search_attr = 0;	// attributes desired by 'hlsearch'
-#ifdef FEAT_SYN_HL
-    int		vcol_save_attr = 0;	/* saved attr for 'cursorcolumn' */
-    int		syntax_attr = 0;	/* attributes desired by syntax */
-    int		has_syntax = FALSE;	/* this buffer has syntax highl. */
-    int		save_did_emsg;
-    int		draw_color_col = FALSE;	/* highlight colorcolumn */
-    int		*color_cols = NULL;	/* pointer to according columns array */
-#endif
-    int		eol_hl_off = 0;		/* 1 if highlighted char after EOL */
-#ifdef FEAT_TEXT_PROP
-    int		text_prop_count;
-    int		text_prop_next = 0;	// next text property to use
-    textprop_T	*text_props = NULL;
-    int		*text_prop_idxs = NULL;
-    int		text_props_active = 0;
-    proptype_T  *text_prop_type = NULL;
-    int		text_prop_attr = 0;
-    int		text_prop_combine = FALSE;
-#endif
-#ifdef FEAT_SPELL
-    int		has_spell = FALSE;	/* this buffer has spell checking */
-# define SPWORDLEN 150
-    char_u	nextline[SPWORDLEN * 2];/* text with start of the next line */
-    int		nextlinecol = 0;	/* column where nextline[] starts */
-    int		nextline_idx = 0;	/* index in nextline[] where next line
-					   starts */
-    int		spell_attr = 0;		/* attributes desired by spelling */
-    int		word_end = 0;		/* last byte with same spell_attr */
-    static linenr_T  checked_lnum = 0;	/* line number for "checked_col" */
-    static int	checked_col = 0;	/* column in "checked_lnum" up to which
-					 * there are no spell errors */
-    static int	cap_col = -1;		/* column to check for Cap word */
-    static linenr_T capcol_lnum = 0;	/* line number where "cap_col" used */
-    int		cur_checked_col = 0;	/* checked column for current line */
-#endif
-    int		extra_check = 0;	// has extra highlighting
-    int		multi_attr = 0;		/* attributes desired by multibyte */
-    int		mb_l = 1;		/* multi-byte byte length */
-    int		mb_c = 0;		/* decoded multi-byte character */
-    int		mb_utf8 = FALSE;	/* screen char is UTF-8 char */
-    int		u8cc[MAX_MCO];		/* composing UTF-8 chars */
-#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
-    int		filler_lines = 0;	/* nr of filler lines to be drawn */
-    int		filler_todo = 0;	/* nr of filler lines still to do + 1 */
-#endif
-#ifdef FEAT_DIFF
-    hlf_T	diff_hlf = (hlf_T)0;	/* type of diff highlighting */
-    int		change_start = MAXCOL;	/* first col of changed area */
-    int		change_end = -1;	/* last col of changed area */
-#endif
-    colnr_T	trailcol = MAXCOL;	/* start of trailing spaces */
-#ifdef FEAT_LINEBREAK
-    int		need_showbreak = FALSE; /* overlong line, skipping first x
-					   chars */
-#endif
-#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
-	|| defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
-# define LINE_ATTR
-    int		line_attr = 0;		// attribute for the whole line
-    int		line_attr_save;
-#endif
-#ifdef FEAT_SIGNS
-    int		sign_present = FALSE;
-    sign_attrs_T sattr;
-#endif
-#ifdef FEAT_ARABIC
-    int		prev_c = 0;		/* previous Arabic character */
-    int		prev_c1 = 0;		/* first composing char for prev_c */
-#endif
-#if defined(LINE_ATTR)
-    int		did_line_attr = 0;
-#endif
-#ifdef FEAT_TERMINAL
-    int		get_term_attr = FALSE;
-#endif
-#ifdef FEAT_SYN_HL
-    int		cul_attr = 0;		// set when 'cursorline' active
-
-    // 'cursorlineopt' has "screenline" and cursor is in this line
-    int		cul_screenline = FALSE;
-
-    // margin columns for the screen line, needed for when 'cursorlineopt'
-    // contains "screenline"
-    int		left_curline_col = 0;
-    int		right_curline_col = 0;
-#endif
-
-    /* draw_state: items that are drawn in sequence: */
-#define WL_START	0		/* nothing done yet */
-#ifdef FEAT_CMDWIN
-# define WL_CMDLINE	WL_START + 1	/* cmdline window column */
-#else
-# define WL_CMDLINE	WL_START
-#endif
-#ifdef FEAT_FOLDING
-# define WL_FOLD	WL_CMDLINE + 1	/* 'foldcolumn' */
-#else
-# define WL_FOLD	WL_CMDLINE
-#endif
-#ifdef FEAT_SIGNS
-# define WL_SIGN	WL_FOLD + 1	/* column for signs */
-#else
-# define WL_SIGN	WL_FOLD		/* column for signs */
-#endif
-#define WL_NR		WL_SIGN + 1	/* line number */
-#ifdef FEAT_LINEBREAK
-# define WL_BRI		WL_NR + 1	/* 'breakindent' */
-#else
-# define WL_BRI		WL_NR
-#endif
-#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
-# define WL_SBR		WL_BRI + 1	/* 'showbreak' or 'diff' */
-#else
-# define WL_SBR		WL_BRI
-#endif
-#define WL_LINE		WL_SBR + 1	/* text in the line */
-    int		draw_state = WL_START;	/* what to draw next */
-#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
-    int		feedback_col = 0;
-    int		feedback_old_attr = -1;
-#endif
-    int		screen_line_flags = 0;
-
-#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
-    int		match_conc	= 0;	// cchar for match functions
-#endif
-#ifdef FEAT_CONCEAL
-    int		syntax_flags	= 0;
-    int		syntax_seqnr	= 0;
-    int		prev_syntax_id	= 0;
-    int		conceal_attr	= HL_ATTR(HLF_CONCEAL);
-    int		is_concealing	= FALSE;
-    int		boguscols	= 0;	// nonexistent columns added to force
-					// wrapping
-    int		vcol_off	= 0;	// offset for concealed characters
-    int		did_wcol	= FALSE;
-    int		old_boguscols   = 0;
-# define VCOL_HLC (vcol - vcol_off)
-# define FIX_FOR_BOGUSCOLS \
-    { \
-	n_extra += vcol_off; \
-	vcol -= vcol_off; \
-	vcol_off = 0; \
-	col -= boguscols; \
-	old_boguscols = boguscols; \
-	boguscols = 0; \
-    }
-#else
-# define VCOL_HLC (vcol)
-#endif
-
-    if (startrow > endrow)		/* past the end already! */
-	return startrow;
-
-    row = startrow;
-    screen_row = row + W_WINROW(wp);
-
-    if (!number_only)
-    {
-	/*
-	 * To speed up the loop below, set extra_check when there is linebreak,
-	 * trailing white space and/or syntax processing to be done.
-	 */
-#ifdef FEAT_LINEBREAK
-	extra_check = wp->w_p_lbr;
-#endif
-#ifdef FEAT_SYN_HL
-	if (syntax_present(wp) && !wp->w_s->b_syn_error
-# ifdef SYN_TIME_LIMIT
-		&& !wp->w_s->b_syn_slow
-# endif
-	   )
-	{
-	    /* Prepare for syntax highlighting in this line.  When there is an
-	     * error, stop syntax highlighting. */
-	    save_did_emsg = did_emsg;
-	    did_emsg = FALSE;
-	    syntax_start(wp, lnum);
-	    if (did_emsg)
-		wp->w_s->b_syn_error = TRUE;
-	    else
-	    {
-		did_emsg = save_did_emsg;
-#ifdef SYN_TIME_LIMIT
-		if (!wp->w_s->b_syn_slow)
-#endif
-		{
-		    has_syntax = TRUE;
-		    extra_check = TRUE;
-		}
-	    }
-	}
-
-	/* Check for columns to display for 'colorcolumn'. */
-	color_cols = wp->w_p_cc_cols;
-	if (color_cols != NULL)
-	    draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
-#endif
-
-#ifdef FEAT_TERMINAL
-	if (term_show_buffer(wp->w_buffer))
-	{
-	    extra_check = TRUE;
-	    get_term_attr = TRUE;
-	    win_attr = term_get_attr(wp->w_buffer, lnum, -1);
-	}
-#endif
-
-#ifdef FEAT_SPELL
-	if (wp->w_p_spell
-		&& *wp->w_s->b_p_spl != NUL
-		&& wp->w_s->b_langp.ga_len > 0
-		&& *(char **)(wp->w_s->b_langp.ga_data) != NULL)
-	{
-	    /* Prepare for spell checking. */
-	    has_spell = TRUE;
-	    extra_check = TRUE;
-
-	    /* Get the start of the next line, so that words that wrap to the
-	     * next line are found too: "et<line-break>al.".
-	     * Trick: skip a few chars for C/shell/Vim comments */
-	    nextline[SPWORDLEN] = NUL;
-	    if (lnum < wp->w_buffer->b_ml.ml_line_count)
-	    {
-		line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
-		spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
-	    }
-
-	    /* When a word wrapped from the previous line the start of the
-	     * current line is valid. */
-	    if (lnum == checked_lnum)
-		cur_checked_col = checked_col;
-	    checked_lnum = 0;
-
-	    /* When there was a sentence end in the previous line may require a
-	     * word starting with capital in this line.  In line 1 always check
-	     * the first word. */
-	    if (lnum != capcol_lnum)
-		cap_col = -1;
-	    if (lnum == 1)
-		cap_col = 0;
-	    capcol_lnum = 0;
-	}
-#endif
-
-	/*
-	 * handle Visual active in this window
-	 */
-	if (VIsual_active && wp->w_buffer == curwin->w_buffer)
-	{
-	    if (LTOREQ_POS(curwin->w_cursor, VIsual))
-	    {
-		// Visual is after curwin->w_cursor
-		top = &curwin->w_cursor;
-		bot = &VIsual;
-	    }
-	    else
-	    {
-		// Visual is before curwin->w_cursor
-		top = &VIsual;
-		bot = &curwin->w_cursor;
-	    }
-	    lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
-	    if (VIsual_mode == Ctrl_V)
-	    {
-		// block mode
-		if (lnum_in_visual_area)
-		{
-		    fromcol = wp->w_old_cursor_fcol;
-		    tocol = wp->w_old_cursor_lcol;
-		}
-	    }
-	    else
-	    {
-		// non-block mode
-		if (lnum > top->lnum && lnum <= bot->lnum)
-		    fromcol = 0;
-		else if (lnum == top->lnum)
-		{
-		    if (VIsual_mode == 'V')	// linewise
-			fromcol = 0;
-		    else
-		    {
-			getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
-			if (gchar_pos(top) == NUL)
-			    tocol = fromcol + 1;
-		    }
-		}
-		if (VIsual_mode != 'V' && lnum == bot->lnum)
-		{
-		    if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
-		    {
-			fromcol = -10;
-			tocol = MAXCOL;
-		    }
-		    else if (bot->col == MAXCOL)
-			tocol = MAXCOL;
-		    else
-		    {
-			pos = *bot;
-			if (*p_sel == 'e')
-			    getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
-			else
-			{
-			    getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
-			    ++tocol;
-			}
-		    }
-		}
-	    }
-
-	    /* Check if the character under the cursor should not be inverted */
-	    if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
-#ifdef FEAT_GUI
-		    && !gui.in_use
-#endif
-		    )
-		noinvcur = TRUE;
-
-	    /* if inverting in this line set area_highlighting */
-	    if (fromcol >= 0)
-	    {
-		area_highlighting = TRUE;
-		vi_attr = HL_ATTR(HLF_V);
-#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
-		if ((clip_star.available && !clip_star.owned
-						      && clip_isautosel_star())
-			|| (clip_plus.available && !clip_plus.owned
-						     && clip_isautosel_plus()))
-		    vi_attr = HL_ATTR(HLF_VNC);
-#endif
-	    }
-	}
-
-	/*
-	 * handle 'incsearch' and ":s///c" highlighting
-	 */
-	else if (highlight_match
-		&& wp == curwin
-		&& lnum >= curwin->w_cursor.lnum
-		&& lnum <= curwin->w_cursor.lnum + search_match_lines)
-	{
-	    if (lnum == curwin->w_cursor.lnum)
-		getvcol(curwin, &(curwin->w_cursor),
-					      (colnr_T *)&fromcol, NULL, NULL);
-	    else
-		fromcol = 0;
-	    if (lnum == curwin->w_cursor.lnum + search_match_lines)
-	    {
-		pos.lnum = lnum;
-		pos.col = search_match_endcol;
-		getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
-	    }
-	    else
-		tocol = MAXCOL;
-	    /* do at least one character; happens when past end of line */
-	    if (fromcol == tocol)
-		tocol = fromcol + 1;
-	    area_highlighting = TRUE;
-	    vi_attr = HL_ATTR(HLF_I);
-	}
-    }
-
-#ifdef FEAT_DIFF
-    filler_lines = diff_check(wp, lnum);
-    if (filler_lines < 0)
-    {
-	if (filler_lines == -1)
-	{
-	    if (diff_find_change(wp, lnum, &change_start, &change_end))
-		diff_hlf = HLF_ADD;	/* added line */
-	    else if (change_start == 0)
-		diff_hlf = HLF_TXD;	/* changed text */
-	    else
-		diff_hlf = HLF_CHD;	/* changed line */
-	}
-	else
-	    diff_hlf = HLF_ADD;		/* added line */
-	filler_lines = 0;
-	area_highlighting = TRUE;
-    }
-    if (lnum == wp->w_topline)
-	filler_lines = wp->w_topfill;
-    filler_todo = filler_lines;
-#endif
-
-#ifdef FEAT_SIGNS
-    sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
-#endif
-
-#ifdef LINE_ATTR
-# ifdef FEAT_SIGNS
-    /* If this line has a sign with line highlighting set line_attr. */
-    if (sign_present)
-	line_attr = sattr.linehl;
-# endif
-# if defined(FEAT_QUICKFIX)
-    /* Highlight the current line in the quickfix window. */
-    if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
-	line_attr = HL_ATTR(HLF_QFL);
-# endif
-    if (line_attr != 0)
-	area_highlighting = TRUE;
-#endif
-
-    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
-    ptr = line;
-
-#ifdef FEAT_SPELL
-    if (has_spell && !number_only)
-    {
-	/* For checking first word with a capital skip white space. */
-	if (cap_col == 0)
-	    cap_col = getwhitecols(line);
-
-	/* To be able to spell-check over line boundaries copy the end of the
-	 * current line into nextline[].  Above the start of the next line was
-	 * copied to nextline[SPWORDLEN]. */
-	if (nextline[SPWORDLEN] == NUL)
-	{
-	    /* No next line or it is empty. */
-	    nextlinecol = MAXCOL;
-	    nextline_idx = 0;
-	}
-	else
-	{
-	    v = (long)STRLEN(line);
-	    if (v < SPWORDLEN)
-	    {
-		/* Short line, use it completely and append the start of the
-		 * next line. */
-		nextlinecol = 0;
-		mch_memmove(nextline, line, (size_t)v);
-		STRMOVE(nextline + v, nextline + SPWORDLEN);
-		nextline_idx = v + 1;
-	    }
-	    else
-	    {
-		/* Long line, use only the last SPWORDLEN bytes. */
-		nextlinecol = v - SPWORDLEN;
-		mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
-		nextline_idx = SPWORDLEN + 1;
-	    }
-	}
-    }
-#endif
-
-    if (wp->w_p_list)
-    {
-	if (lcs_space || lcs_trail || lcs_nbsp)
-	    extra_check = TRUE;
-	/* find start of trailing whitespace */
-	if (lcs_trail)
-	{
-	    trailcol = (colnr_T)STRLEN(ptr);
-	    while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
-		--trailcol;
-	    trailcol += (colnr_T) (ptr - line);
-	}
-    }
-
-    wcr_attr = get_wcr_attr(wp);
-    if (wcr_attr != 0)
-    {
-	win_attr = wcr_attr;
-	area_highlighting = TRUE;
-    }
-#ifdef FEAT_TEXT_PROP
-    if (WIN_IS_POPUP(wp))
-	screen_line_flags |= SLF_POPUP;
-#endif
-
-    /*
-     * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
-     * first character to be displayed.
-     */
-    if (wp->w_p_wrap)
-	v = wp->w_skipcol;
-    else
-	v = wp->w_leftcol;
-    if (v > 0 && !number_only)
-    {
-	char_u	*prev_ptr = ptr;
-
-	while (vcol < v && *ptr != NUL)
-	{
-	    c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
-	    vcol += c;
-	    prev_ptr = ptr;
-	    MB_PTR_ADV(ptr);
-	}
-
-	/* When:
-	 * - 'cuc' is set, or
-	 * - 'colorcolumn' is set, or
-	 * - 'virtualedit' is set, or
-	 * - the visual mode is active,
-	 * the end of the line may be before the start of the displayed part.
-	 */
-	if (vcol < v && (
-#ifdef FEAT_SYN_HL
-	     wp->w_p_cuc || draw_color_col ||
-#endif
-	     virtual_active() ||
-	     (VIsual_active && wp->w_buffer == curwin->w_buffer)))
-	    vcol = v;
-
-	/* Handle a character that's not completely on the screen: Put ptr at
-	 * that character but skip the first few screen characters. */
-	if (vcol > v)
-	{
-	    vcol -= c;
-	    ptr = prev_ptr;
-	    /* If the character fits on the screen, don't need to skip it.
-	     * Except for a TAB. */
-	    if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
-	       n_skip = v - vcol;
-	}
-
-	/*
-	 * Adjust for when the inverted text is before the screen,
-	 * and when the start of the inverted text is before the screen.
-	 */
-	if (tocol <= vcol)
-	    fromcol = 0;
-	else if (fromcol >= 0 && fromcol < vcol)
-	    fromcol = vcol;
-
-#ifdef FEAT_LINEBREAK
-	/* When w_skipcol is non-zero, first line needs 'showbreak' */
-	if (wp->w_p_wrap)
-	    need_showbreak = TRUE;
-#endif
-#ifdef FEAT_SPELL
-	/* When spell checking a word we need to figure out the start of the
-	 * word and if it's badly spelled or not. */
-	if (has_spell)
-	{
-	    int		len;
-	    colnr_T	linecol = (colnr_T)(ptr - line);
-	    hlf_T	spell_hlf = HLF_COUNT;
-
-	    pos = wp->w_cursor;
-	    wp->w_cursor.lnum = lnum;
-	    wp->w_cursor.col = linecol;
-	    len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
-
-	    /* spell_move_to() may call ml_get() and make "line" invalid */
-	    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
-	    ptr = line + linecol;
-
-	    if (len == 0 || (int)wp->w_cursor.col > ptr - line)
-	    {
-		/* no bad word found at line start, don't check until end of a
-		 * word */
-		spell_hlf = HLF_COUNT;
-		word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
-	    }
-	    else
-	    {
-		/* bad word found, use attributes until end of word */
-		word_end = wp->w_cursor.col + len + 1;
-
-		/* Turn index into actual attributes. */
-		if (spell_hlf != HLF_COUNT)
-		    spell_attr = highlight_attr[spell_hlf];
-	    }
-	    wp->w_cursor = pos;
-
-# ifdef FEAT_SYN_HL
-	    /* Need to restart syntax highlighting for this line. */
-	    if (has_syntax)
-		syntax_start(wp, lnum);
-# endif
-	}
-#endif
-    }
-
-    /*
-     * Correct highlighting for cursor that can't be disabled.
-     * Avoids having to check this for each character.
-     */
-    if (fromcol >= 0)
-    {
-	if (noinvcur)
-	{
-	    if ((colnr_T)fromcol == wp->w_virtcol)
-	    {
-		/* highlighting starts at cursor, let it start just after the
-		 * cursor */
-		fromcol_prev = fromcol;
-		fromcol = -1;
-	    }
-	    else if ((colnr_T)fromcol < wp->w_virtcol)
-		/* restart highlighting after the cursor */
-		fromcol_prev = wp->w_virtcol;
-	}
-	if (fromcol >= tocol)
-	    fromcol = -1;
-    }
-
-#ifdef FEAT_SEARCH_EXTRA
-    if (!number_only)
-    {
-	v = (long)(ptr - line);
-	area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
-					      &line, &search_hl, &search_attr);
-	ptr = line + v; // "line" may have been updated
-    }
-#endif
-
-#ifdef FEAT_SYN_HL
-    // Cursor line highlighting for 'cursorline' in the current window.
-    if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
-    {
-	// Do not show the cursor line in the text when Visual mode is active,
-	// because it's not clear what is selected then.  Do update
-	// w_last_cursorline.
-	if (!(wp == curwin && VIsual_active)
-					 && wp->w_p_culopt_flags != CULOPT_NBR)
-	{
-	    cul_screenline = (wp->w_p_wrap
-				   && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
-
-	    // Only set line_attr here when "screenline" is not present in
-	    // 'cursorlineopt'.  Otherwise it's done later.
-	    if (!cul_screenline)
-	    {
-		cul_attr = HL_ATTR(HLF_CUL);
-		line_attr = cul_attr;
-		wp->w_last_cursorline = wp->w_cursor.lnum;
-	    }
-	    else
-	    {
-		line_attr_save = line_attr;
-		wp->w_last_cursorline = 0;
-		margin_columns_win(wp, &left_curline_col, &right_curline_col);
-	    }
-	    area_highlighting = TRUE;
-	}
-	else
-	    wp->w_last_cursorline = wp->w_cursor.lnum;
-    }
-#endif
-
-#ifdef FEAT_TEXT_PROP
-    {
-	char_u *prop_start;
-
-	text_prop_count = get_text_props(wp->w_buffer, lnum,
-							   &prop_start, FALSE);
-	if (text_prop_count > 0)
-	{
-	    // Make a copy of the properties, so that they are properly
-	    // aligned.
-	    text_props = ALLOC_MULT(textprop_T, text_prop_count);
-	    if (text_props != NULL)
-		mch_memmove(text_props, prop_start,
-					 text_prop_count * sizeof(textprop_T));
-
-	    // Allocate an array for the indexes.
-	    text_prop_idxs = ALLOC_MULT(int, text_prop_count);
-	    area_highlighting = TRUE;
-	    extra_check = TRUE;
-	}
-    }
-#endif
-
-    off = (unsigned)(current_ScreenLine - ScreenLines);
-    col = 0;
-
-#ifdef FEAT_RIGHTLEFT
-    if (wp->w_p_rl)
-    {
-	/* Rightleft window: process the text in the normal direction, but put
-	 * it in current_ScreenLine[] from right to left.  Start at the
-	 * rightmost column of the window. */
-	col = wp->w_width - 1;
-	off += col;
-	screen_line_flags |= SLF_RIGHTLEFT;
-    }
-#endif
-
-    /*
-     * Repeat for the whole displayed line.
-     */
-    for (;;)
-    {
-#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
-	int has_match_conc  = 0;	// match wants to conceal
-#endif
-#ifdef FEAT_CONCEAL
-	int did_decrement_ptr = FALSE;
-#endif
-	/* Skip this quickly when working on the text. */
-	if (draw_state != WL_LINE)
-	{
-#ifdef FEAT_CMDWIN
-	    if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
-	    {
-		draw_state = WL_CMDLINE;
-		if (cmdwin_type != 0 && wp == curwin)
-		{
-		    /* Draw the cmdline character. */
-		    n_extra = 1;
-		    c_extra = cmdwin_type;
-		    c_final = NUL;
-		    char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
-		}
-	    }
-#endif
-
-#ifdef FEAT_FOLDING
-	    if (draw_state == WL_FOLD - 1 && n_extra == 0)
-	    {
-		int fdc = compute_foldcolumn(wp, 0);
-
-		draw_state = WL_FOLD;
-		if (fdc > 0)
-		{
-		    /* Draw the 'foldcolumn'.  Allocate a buffer, "extra" may
-		     * already be in use. */
-		    vim_free(p_extra_free);
-		    p_extra_free = alloc(12 + 1);
-
-		    if (p_extra_free != NULL)
-		    {
-			fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
-			n_extra = fdc;
-			p_extra_free[n_extra] = NUL;
-			p_extra = p_extra_free;
-			c_extra = NUL;
-			c_final = NUL;
-			char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
-		    }
-		}
-	    }
-#endif
-
-#ifdef FEAT_SIGNS
-	    if (draw_state == WL_SIGN - 1 && n_extra == 0)
-	    {
-		draw_state = WL_SIGN;
-		/* Show the sign column when there are any signs in this
-		 * buffer or when using Netbeans. */
-		if (signcolumn_on(wp))
-		    get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
-			    row, startrow, filler_lines, filler_todo, &c_extra,
-			    &c_final, extra, &p_extra, &n_extra, &char_attr);
-	    }
-#endif
-
-	    if (draw_state == WL_NR - 1 && n_extra == 0)
-	    {
-		draw_state = WL_NR;
-		/* Display the absolute or relative line number. After the
-		 * first fill with blanks when the 'n' flag isn't in 'cpo' */
-		if ((wp->w_p_nu || wp->w_p_rnu)
-			&& (row == startrow
-#ifdef FEAT_DIFF
-			    + filler_lines
-#endif
-			    || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
-		{
-#ifdef FEAT_SIGNS
-		    // If 'signcolumn' is set to 'number' and a sign is present
-		    // in 'lnum', then display the sign instead of the line
-		    // number.
-		    if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
-			    && sign_present)
-			get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
-				row, startrow, filler_lines, filler_todo,
-				&c_extra, &c_final, extra, &p_extra, &n_extra,
-				&char_attr);
-		    else
-#endif
-		    {
-		      /* Draw the line number (empty space after wrapping). */
-		      if (row == startrow
-#ifdef FEAT_DIFF
-			    + filler_lines
-#endif
-			    )
-		      {
-			long num;
-			char *fmt = "%*ld ";
-
-			if (wp->w_p_nu && !wp->w_p_rnu)
-			    /* 'number' + 'norelativenumber' */
-			    num = (long)lnum;
-			else
-			{
-			    /* 'relativenumber', don't use negative numbers */
-			    num = labs((long)get_cursor_rel_lnum(wp, lnum));
-			    if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
-			    {
-				/* 'number' + 'relativenumber' */
-				num = lnum;
-				fmt = "%-*ld ";
-			    }
-			}
-
-			sprintf((char *)extra, fmt,
-						number_width(wp), num);
-			if (wp->w_skipcol > 0)
-			    for (p_extra = extra; *p_extra == ' '; ++p_extra)
-				*p_extra = '-';
-#ifdef FEAT_RIGHTLEFT
-			if (wp->w_p_rl)		    /* reverse line numbers */
-			{
-			    char_u	*p1, *p2;
-			    int		t;
-
-			    // like rl_mirror(), but keep the space at the end
-			    p2 = skiptowhite(extra) - 1;
-			    for (p1 = extra; p1 < p2; ++p1, --p2)
-			    {
-				t = *p1;
-				*p1 = *p2;
-				*p2 = t;
-			    }
-			}
-#endif
-			p_extra = extra;
-			c_extra = NUL;
-			c_final = NUL;
-		      }
-		      else
-		      {
-			c_extra = ' ';
-			c_final = NUL;
-		      }
-		      n_extra = number_width(wp) + 1;
-		      char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
-#ifdef FEAT_SYN_HL
-		      // When 'cursorline' is set highlight the line number of
-		      // the current line differently.
-		      // When 'cursorlineopt' has "screenline" only highlight
-		      // the line number itself.
-		      // TODO: Can we use CursorLine instead of CursorLineNr
-		      // when CursorLineNr isn't set?
-		      if ((wp->w_p_cul || wp->w_p_rnu)
-			      && (wp->w_p_culopt_flags & CULOPT_NBR)
-			      && (row == startrow
-				  || wp->w_p_culopt_flags & CULOPT_LINE)
-			      && lnum == wp->w_cursor.lnum)
-			char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
-#endif
-		    }
-		}
-	    }
-
-#ifdef FEAT_LINEBREAK
-	    if (wp->w_p_brisbr && draw_state == WL_BRI - 1
-					     && n_extra == 0 && *p_sbr != NUL)
-		/* draw indent after showbreak value */
-		draw_state = WL_BRI;
-	    else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
-		/* After the showbreak, draw the breakindent */
-		draw_state = WL_BRI - 1;
-
-	    /* draw 'breakindent': indent wrapped text accordingly */
-	    if (draw_state == WL_BRI - 1 && n_extra == 0)
-	    {
-		draw_state = WL_BRI;
-		/* if need_showbreak is set, breakindent also applies */
-		if (wp->w_p_bri && n_extra == 0
-					 && (row != startrow || need_showbreak)
-# ifdef FEAT_DIFF
-			&& filler_lines == 0
-# endif
-		   )
-		{
-		    char_attr = 0;
-# ifdef FEAT_DIFF
-		    if (diff_hlf != (hlf_T)0)
-		    {
-			char_attr = HL_ATTR(diff_hlf);
-#  ifdef FEAT_SYN_HL
-			if (cul_attr != 0)
-			    char_attr = hl_combine_attr(char_attr, cul_attr);
-#  endif
-		    }
-# endif
-		    p_extra = NULL;
-		    c_extra = ' ';
-		    n_extra = get_breakindent_win(wp,
-				       ml_get_buf(wp->w_buffer, lnum, FALSE));
-		    /* Correct end of highlighted area for 'breakindent',
-		     * required when 'linebreak' is also set. */
-		    if (tocol == vcol)
-			tocol += n_extra;
-		}
-	    }
-#endif
-
-#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
-	    if (draw_state == WL_SBR - 1 && n_extra == 0)
-	    {
-		draw_state = WL_SBR;
-# ifdef FEAT_DIFF
-		if (filler_todo > 0)
-		{
-		    /* Draw "deleted" diff line(s). */
-		    if (char2cells(fill_diff) > 1)
-		    {
-			c_extra = '-';
-			c_final = NUL;
-		    }
-		    else
-		    {
-			c_extra = fill_diff;
-			c_final = NUL;
-		    }
-#  ifdef FEAT_RIGHTLEFT
-		    if (wp->w_p_rl)
-			n_extra = col + 1;
-		    else
-#  endif
-			n_extra = wp->w_width - col;
-		    char_attr = HL_ATTR(HLF_DED);
-		}
-# endif
-# ifdef FEAT_LINEBREAK
-		if (*p_sbr != NUL && need_showbreak)
-		{
-		    /* Draw 'showbreak' at the start of each broken line. */
-		    p_extra = p_sbr;
-		    c_extra = NUL;
-		    c_final = NUL;
-		    n_extra = (int)STRLEN(p_sbr);
-		    char_attr = HL_ATTR(HLF_AT);
-		    need_showbreak = FALSE;
-		    vcol_sbr = vcol + MB_CHARLEN(p_sbr);
-		    /* Correct end of highlighted area for 'showbreak',
-		     * required when 'linebreak' is also set. */
-		    if (tocol == vcol)
-			tocol += n_extra;
-		    // combine 'showbreak' with 'wincolor'
-		    if (win_attr != 0)
-			char_attr = hl_combine_attr(win_attr, char_attr);
-#  ifdef FEAT_SYN_HL
-		    // combine 'showbreak' with 'cursorline'
-		    if (cul_attr != 0)
-			char_attr = hl_combine_attr(char_attr, cul_attr);
-#  endif
-		}
-# endif
-	    }
-#endif
-
-	    if (draw_state == WL_LINE - 1 && n_extra == 0)
-	    {
-		draw_state = WL_LINE;
-		if (saved_n_extra)
-		{
-		    /* Continue item from end of wrapped line. */
-		    n_extra = saved_n_extra;
-		    c_extra = saved_c_extra;
-		    c_final = saved_c_final;
-		    p_extra = saved_p_extra;
-		    char_attr = saved_char_attr;
-		}
-		else
-		    char_attr = win_attr;
-	    }
-	}
-#ifdef FEAT_SYN_HL
-	if (cul_screenline)
-	{
-	    if (draw_state == WL_LINE
-		    && vcol >= left_curline_col
-		    && vcol < right_curline_col)
-	    {
-		cul_attr = HL_ATTR(HLF_CUL);
-		line_attr = cul_attr;
-	    }
-	    else
-	    {
-		cul_attr = 0;
-		line_attr = line_attr_save;
-	    }
-	}
-#endif
-
-	// When still displaying '$' of change command, stop at cursor.
-	// When only displaying the (relative) line number and that's done,
-	// stop here.
-	if ((dollar_vcol >= 0 && wp == curwin
-		   && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
-#ifdef FEAT_DIFF
-				   && filler_todo <= 0
-#endif
-		)
-		|| (number_only && draw_state > WL_NR))
-	{
-	    screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
-							    screen_line_flags);
-	    /* Pretend we have finished updating the window.  Except when
-	     * 'cursorcolumn' is set. */
-#ifdef FEAT_SYN_HL
-	    if (wp->w_p_cuc)
-		row = wp->w_cline_row + wp->w_cline_height;
-	    else
-#endif
-		row = wp->w_height;
-	    break;
-	}
-
-	if (draw_state == WL_LINE && (area_highlighting
-#ifdef FEAT_SPELL
-		|| has_spell
-#endif
-	   ))
-	{
-	    /* handle Visual or match highlighting in this line */
-	    if (vcol == fromcol
-		    || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
-			&& (*mb_ptr2cells)(ptr) > 1)
-		    || ((int)vcol_prev == fromcol_prev
-			&& vcol_prev < vcol	/* not at margin */
-			&& vcol < tocol))
-		area_attr = vi_attr;		/* start highlighting */
-	    else if (area_attr != 0
-		    && (vcol == tocol
-			|| (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
-		area_attr = 0;			/* stop highlighting */
-
-#ifdef FEAT_SEARCH_EXTRA
-	    if (!n_extra)
-	    {
-		/*
-		 * Check for start/end of 'hlsearch' and other matches.
-		 * After end, check for start/end of next match.
-		 * When another match, have to check for start again.
-		 */
-		v = (long)(ptr - line);
-		search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
-				      &search_hl, &has_match_conc, &match_conc,
-				      did_line_attr, lcs_eol_one);
-		ptr = line + v;  // "line" may have been changed
-	    }
-#endif
-
-#ifdef FEAT_DIFF
-	    if (diff_hlf != (hlf_T)0)
-	    {
-		if (diff_hlf == HLF_CHD && ptr - line >= change_start
-							      && n_extra == 0)
-		    diff_hlf = HLF_TXD;		/* changed text */
-		if (diff_hlf == HLF_TXD && ptr - line > change_end
-							      && n_extra == 0)
-		    diff_hlf = HLF_CHD;		/* changed line */
-		line_attr = HL_ATTR(diff_hlf);
-		if (wp->w_p_cul && lnum == wp->w_cursor.lnum
-			&& wp->w_p_culopt_flags != CULOPT_NBR
-			&& (!cul_screenline || (vcol >= left_curline_col
-						&& vcol <= right_curline_col)))
-		    line_attr = hl_combine_attr(
-					  line_attr, HL_ATTR(HLF_CUL));
-	    }
-#endif
-
-#ifdef FEAT_TEXT_PROP
-	    if (text_props != NULL)
-	    {
-		int pi;
-		int bcol = (int)(ptr - line);
-
-		if (n_extra > 0)
-		    --bcol;  // still working on the previous char, e.g. Tab
-
-		// Check if any active property ends.
-		for (pi = 0; pi < text_props_active; ++pi)
-		{
-		    int tpi = text_prop_idxs[pi];
-
-		    if (bcol >= text_props[tpi].tp_col - 1
-						  + text_props[tpi].tp_len)
-		    {
-			if (pi + 1 < text_props_active)
-			    mch_memmove(text_prop_idxs + pi,
-					text_prop_idxs + pi + 1,
-					sizeof(int)
-					 * (text_props_active - (pi + 1)));
-			--text_props_active;
-			--pi;
-		    }
-		}
-
-		// Add any text property that starts in this column.
-		while (text_prop_next < text_prop_count
-			   && bcol >= text_props[text_prop_next].tp_col - 1)
-		    text_prop_idxs[text_props_active++] = text_prop_next++;
-
-		text_prop_attr = 0;
-		text_prop_combine = FALSE;
-		if (text_props_active > 0)
-		{
-		    // Sort the properties on priority and/or starting last.
-		    // Then combine the attributes, highest priority last.
-		    current_text_props = text_props;
-		    current_buf = wp->w_buffer;
-		    qsort((void *)text_prop_idxs, (size_t)text_props_active,
-					       sizeof(int), text_prop_compare);
-
-		    for (pi = 0; pi < text_props_active; ++pi)
-		    {
-			int	    tpi = text_prop_idxs[pi];
-			proptype_T  *pt = text_prop_type_by_id(
-					wp->w_buffer, text_props[tpi].tp_type);
-
-			if (pt != NULL && pt->pt_hl_id > 0)
-			{
-			    int pt_attr = syn_id2attr(pt->pt_hl_id);
-
-			    text_prop_type = pt;
-			    text_prop_attr =
-				      hl_combine_attr(text_prop_attr, pt_attr);
-			    text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
-			}
-		    }
-		}
-	    }
-#endif
-
-	    /* Decide which of the highlight attributes to use. */
-	    attr_pri = TRUE;
-#ifdef LINE_ATTR
-	    if (area_attr != 0)
-		char_attr = hl_combine_attr(line_attr, area_attr);
-	    else if (search_attr != 0)
-		char_attr = hl_combine_attr(line_attr, search_attr);
-# ifdef FEAT_TEXT_PROP
-	    else if (text_prop_type != NULL)
-	    {
-		char_attr = hl_combine_attr(
-			line_attr != 0 ? line_attr : win_attr, text_prop_attr);
-	    }
-# endif
-	    else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
-				|| vcol < fromcol || vcol_prev < fromcol_prev
-				|| vcol >= tocol))
-	    {
-		// Use line_attr when not in the Visual or 'incsearch' area
-		// (area_attr may be 0 when "noinvcur" is set).
-		char_attr = line_attr;
-		attr_pri = FALSE;
-	    }
-#else
-	    if (area_attr != 0)
-		char_attr = area_attr;
-	    else if (search_attr != 0)
-		char_attr = search_attr;
-#endif
-	    else
-	    {
-		attr_pri = FALSE;
-#ifdef FEAT_TEXT_PROP
-		if (text_prop_type != NULL)
-		{
-		    if (text_prop_combine)
-			char_attr = hl_combine_attr(
-						  syntax_attr, text_prop_attr);
-		    else
-			char_attr = hl_combine_attr(
-						  win_attr, text_prop_attr);
-		}
-		else
-#endif
-#ifdef FEAT_SYN_HL
-		if (has_syntax)
-		    char_attr = syntax_attr;
-		else
-#endif
-		    char_attr = 0;
-	    }
-	}
-	if (char_attr == 0)
-	    char_attr = win_attr;
-
-	/*
-	 * Get the next character to put on the screen.
-	 */
-	/*
-	 * The "p_extra" points to the extra stuff that is inserted to
-	 * represent special characters (non-printable stuff) and other
-	 * things.  When all characters are the same, c_extra is used.
-	 * If c_final is set, it will compulsorily be used at the end.
-	 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
-	 * "p_extra[n_extra]".
-	 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
-	 */
-	if (n_extra > 0)
-	{
-	    if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
-	    {
-		c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
-		mb_c = c;	/* doesn't handle non-utf-8 multi-byte! */
-		if (enc_utf8 && utf_char2len(c) > 1)
-		{
-		    mb_utf8 = TRUE;
-		    u8cc[0] = 0;
-		    c = 0xc0;
-		}
-		else
-		    mb_utf8 = FALSE;
-	    }
-	    else
-	    {
-		c = *p_extra;
-		if (has_mbyte)
-		{
-		    mb_c = c;
-		    if (enc_utf8)
-		    {
-			/* If the UTF-8 character is more than one byte:
-			 * Decode it into "mb_c". */
-			mb_l = utfc_ptr2len(p_extra);
-			mb_utf8 = FALSE;
-			if (mb_l &g