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.