patch 9.1.1243: diff mode is lacking for changes within lines

Problem:  Diff mode's inline highlighting is lackluster. It only
          performs a line-by-line comparison, and calculates a single
          shortest range within a line that could encompass all the
          changes. In lines with multiple changes, or those that span
          multiple lines, this approach tends to end up highlighting
          much more than necessary.

Solution: Implement new inline highlighting modes by doing per-character
          or per-word diff within the diff block, and highlight only the
          relevant parts, add "inline:simple" to the defaults (which is
          the old behaviour)

This change introduces a new diffopt option "inline:<type>". Setting to
"none" will disable all inline highlighting, "simple" (the default) will
use the old behavior, "char" / "word" will perform a character/word-wise
diff of the texts within each diff block and only highlight the
differences.

The new char/word inline diff only use the internal xdiff, and will
respect diff options such as algorithm choice, icase, and misc iwhite
options. indent-heuristics is always on to perform better sliding.

For character highlight, a post-process of the diff results is first
applied before we show the highlight. This is because a naive diff will
create a result with a lot of small diff chunks and gaps, due to the
repetitive nature of individual characters. The post-process is a
heuristic-based refinement that attempts to merge adjacent diff blocks
if they are separated by a short gap (1-3 characters), and can be
further tuned in the future for better results. This process results in
more characters than necessary being highlighted but overall less visual
noise.

For word highlight, always use first buffer's iskeyword definition.
Otherwise if each buffer has different iskeyword settings we would not
be able to group words properly.

The char/word diffing is always per-diff block, not per line, meaning
that changes that span multiple lines will show up correctly.
Added/removed newlines are not shown by default, but if the user has
'list' set (with "eol" listchar defined), the eol character will be be
highlighted correctly for the specific newline characters.

Also, add a new "DiffTextAdd" highlight group linked to "DiffText" by
default. It allows color schemes to use different colors for texts that
have been added within a line versus modified.

This doesn't interact with linematch perfectly currently. The linematch
feature splits up diff blocks into multiple smaller blocks for better
visual matching, which makes inline highlight less useful especially for
multi-line change (e.g. a line is broken into two lines). This could be
addressed in the future.

As a side change, this also removes the bounds checking introduced to
diff_read() as they were added to mask existing logic bugs that were
properly fixed in #16768.

closes: #16881

Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/runtime/doc/diff.txt b/runtime/doc/diff.txt
index e3abbde..0dbc7f8 100644
--- a/runtime/doc/diff.txt
+++ b/runtime/doc/diff.txt
@@ -1,4 +1,4 @@
-*diff.txt*      For Vim version 9.1.  Last change: 2024 Feb 01
+*diff.txt*      For Vim version 9.1.  Last change: 2024 Mar 26
 
 
 		  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -226,14 +226,29 @@
 |hl-DiffAdd|	DiffAdd		Added (inserted) lines.  These lines exist in
 				this buffer but not in another.
 |hl-DiffChange|	DiffChange	Changed lines.
-|hl-DiffText|	DiffText	Changed text inside a Changed line.  Vim
-				finds the first character that is different,
-				and the last character that is different
-				(searching from the end of the line).  The
-				text in between is highlighted.  This means
-				that parts in the middle that are still the
-				same are highlighted anyway.  The 'diffopt'
-				flags "iwhite" and "icase" are used here.
+|hl-DiffText|	DiffText	Changed text inside a Changed line.  Exact
+				behavior depends on the `inline:` setting in
+				'diffopt'.
+				With `inline:` set to "simple", Vim finds the
+				first character that is different, and the
+				last character that is different (searching
+				from the end of the line).  The text in
+				between is highlighted.  This means that parts
+				in the middle that are still the same are
+				highlighted anyway.  The 'diffopt' flags
+				"iwhite" and "icase" are used here.
+				With `inline:` set to "char" or "word", Vim
+				uses the internal diff library to perform a
+				detailed diff between the changed blocks and
+				highlight the exact difference between the
+				two.  Will respect any 'diffopt' flag that
+				affects internal diff.
+				Not used when `inline:` set to "none".
+|hl-DiffTextAdd|  DiffTextAdd	Added text inside a Changed line. Similar to
+				DiffText, but used when there is no
+				corresponding text in other buffers.  Will not
+				be used when `inline:` is set to "simple" or
+				"none".
 |hl-DiffDelete|	DiffDelete	Deleted lines.  Also called filler lines,
 				because they don't really exist in this
 				buffer.
diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt
index a824536..035f6f7 100644
--- a/runtime/doc/options.txt
+++ b/runtime/doc/options.txt
@@ -1,4 +1,4 @@
-*options.txt*	For Vim version 9.1.  Last change: 2025 Mar 14
+*options.txt*	For Vim version 9.1.  Last change: 2025 Mar 26
 
 
 		  VIM REFERENCE MANUAL	  by Bram Moolenaar
@@ -2910,7 +2910,8 @@
 	security reasons.
 
 						*'dip'* *'diffopt'*
-'diffopt' 'dip'		string	(default "internal,filler,closeoff")
+'diffopt' 'dip'		string	(default
+				 "internal,filler,closeoff,inline:simple")
 			global
 			{not available when compiled without the |+diff|
 			feature}
@@ -2975,6 +2976,21 @@
 				Use the indent heuristic for the internal
 				diff library.
 
+		inline:{text}	Highlight inline differences within a change.
+				See |view-diffs|.  Supported values are:
+
+				none    Do not perform inline highlighting.
+				simple  Highlight from first different
+					character to the last one in each
+					line. This is the default if nothing
+					is set.
+				char    Use internal diff to perform a
+					character-wise diff and highlight the
+					difference.
+				word    Use internal diff to perform a
+					|word|-wise diff and highlight the
+					difference.
+
 		internal	Use the internal diff library.  This is
 				ignored when 'diffexpr' is set.  *E960*
 				When running out of memory when writing a
@@ -4392,10 +4408,10 @@
 				     v:Visual,V:VisualNOS,w:WarningMsg,
 				     W:WildMenu,f:Folded,F:FoldColumn,
 				     A:DiffAdd,C:DiffChange,D:DiffDelete,
-				     T:DiffText,>:SignColumn,-:Conceal,
-				     B:SpellBad,P:SpellCap,R:SpellRare,
-				     L:SpellLocal,+:Pmenu,=:PmenuSel,
-				     k:PmenuMatch,<:PmenuMatchSel,
+				     T:DiffText,E:DiffTextAdd,>:SignColumn,
+				     -:Conceal,B:SpellBad,P:SpellCap,
+				     R:SpellRare, L:SpellLocal,+:Pmenu,
+				     =:PmenuSel, k:PmenuMatch,<:PmenuMatchSel,
 				     [:PmenuKind,]:PmenuKindSel,
 				     {:PmenuExtra,}:PmenuExtraSel,
 				     x:PmenuSbar,X:PmenuThumb,*:TabLine,
@@ -4447,7 +4463,8 @@
 	|hl-DiffAdd|	 A  added line in diff mode
 	|hl-DiffChange|	 C  changed line in diff mode
 	|hl-DiffDelete|	 D  deleted line in diff mode
-	|hl-DiffText|	 T  inserted text in diff mode
+	|hl-DiffText|	 T  changed text in diff mode
+	|hl-DiffTextAdd|   E  inserted text in diff mode
 	|hl-SignColumn|	 >  column used for |signs|
 	|hl-Conceal|	 -  the placeholders used for concealed characters
 			    (see 'conceallevel')
diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt
index fe6865e..1a7452f 100644
--- a/runtime/doc/syntax.txt
+++ b/runtime/doc/syntax.txt
@@ -1,4 +1,4 @@
-*syntax.txt*	For Vim version 9.1.  Last change: 2025 Mar 21
+*syntax.txt*	For Vim version 9.1.  Last change: 2025 Mar 26
 
 
 		  VIM REFERENCE MANUAL	  by Bram Moolenaar
@@ -5832,6 +5832,9 @@
 DiffDelete	Diff mode: Deleted line. |diff.txt|
 							*hl-DiffText*
 DiffText	Diff mode: Changed text within a changed line. |diff.txt|
+							*hl-DiffTextAdd*
+DiffTextAdd	Diff mode: Added text within a changed line.  Linked to
+		|hl-DiffText| by default. |diff.txt|
 							*hl-EndOfBuffer*
 EndOfBuffer	Filler lines (~) after the last line in the buffer.
 		By default, this is highlighted like |hl-NonText|.
diff --git a/runtime/doc/tags b/runtime/doc/tags
index a02d07e..5ff06d3 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -8205,6 +8205,7 @@
 hl-DiffChange	syntax.txt	/*hl-DiffChange*
 hl-DiffDelete	syntax.txt	/*hl-DiffDelete*
 hl-DiffText	syntax.txt	/*hl-DiffText*
+hl-DiffTextAdd	syntax.txt	/*hl-DiffTextAdd*
 hl-Directory	syntax.txt	/*hl-Directory*
 hl-EndOfBuffer	syntax.txt	/*hl-EndOfBuffer*
 hl-ErrorMsg	syntax.txt	/*hl-ErrorMsg*
diff --git a/runtime/doc/version9.txt b/runtime/doc/version9.txt
index 4f0da43..5868598 100644
--- a/runtime/doc/version9.txt
+++ b/runtime/doc/version9.txt
@@ -1,4 +1,4 @@
-*version9.txt*  For Vim version 9.1.  Last change: 2025 Mar 23
+*version9.txt*  For Vim version 9.1.  Last change: 2025 Mar 26
 
 
 		  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -41553,6 +41553,16 @@
 
 Support for protected _new() method
 
+Diff mode ~
+---------
+Include the "linematch" algorithm for the 'diffopt' setting.  This aligns
+changes between buffers on similar lines improving the diff highlighting in
+Vim
+
+Improve the diff highlighting for changes within a line.  Configurable using
+the "inline" sub option value for the 'diffopt' setting, with "inline:simple"
+being added to the default "diffopt" value (but this does not change how diff
+mode works).
 							*new-other-9.2*
 Other new features ~
 ------------------
@@ -41570,10 +41580,6 @@
 completion and command-line completion in |ins-completion-menu|, see
 |complete-items|
 
-Include the "linematch" algorithm for the 'diffopt' setting.  This aligns
-changes between buffers on similar lines improving the diff highlighting in
-Vim
-
 Support for the |Tuple| data type in Vim script and Vim9 script.
 
 							*changed-9.2*
@@ -41590,7 +41596,6 @@
 - the default value of the 'keyprotocol' option has been updated and support
   for the ghostty terminal emulator (using kitty protocol) has been added
 
-
 Completion: ~
 - allow to complete directories from 'cdpath' for |:cd| and similar commands,
   add the "cd_in_path" completion type for e.g. |:command-complete| and
@@ -41702,7 +41707,8 @@
 
 Highlighting: ~
 
-|hl-ComplMatchIns|	matched text of the currently inserted completion.
+|hl-ComplMatchIns|	matched text of the currently inserted completion
+|hl-DiffTextAdd|	added text within a changed line
 |hl-MsgArea|		highlighting of the Command-line and messages area
 |hl-PmenuMatch|		Popup menu: highlighting of matched text
 |hl-PmenuMatchSel|	Popup menu: highlighting of matched text in selected
diff --git a/runtime/syntax/vim.vim b/runtime/syntax/vim.vim
index 720038e..5027138 100644
--- a/runtime/syntax/vim.vim
+++ b/runtime/syntax/vim.vim
@@ -123,7 +123,7 @@
 
 " Default highlighting groups {{{2
 " GEN_SYN_VIM: vimHLGroup, START_STR='syn keyword vimHLGroup contained', END_STR=''
-syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC ToolbarLine ToolbarButton Menu Tooltip Scrollbar CursorIM LineNrAbove LineNrBelow
+syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText DiffTextAdd PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC ToolbarLine ToolbarButton Menu Tooltip Scrollbar CursorIM LineNrAbove LineNrBelow
 syn match vimHLGroup contained "\<Conceal\>"
 syn case match