Skip to content

Commit fa37835

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.0071: Need a diff() Vim script function
Problem: Need a diff() Vim script function Solution: Add the diff() Vim script function using the xdiff internal diff library, add support for "unified" and "indices" mode. (Yegappan Lakshmanan) fixes: #4241 closes: #12321 Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 1226b05 commit fa37835

File tree

13 files changed

+538
-12
lines changed

13 files changed

+538
-12
lines changed

runtime/doc/builtin.txt

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Jan 29
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Feb 01
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -147,6 +147,8 @@ delete({fname} [, {flags}]) Number delete the file or directory {fname}
147147
deletebufline({buf}, {first} [, {last}])
148148
Number delete lines from buffer {buf}
149149
did_filetype() Number |TRUE| if FileType autocmd event used
150+
diff({fromlist}, {tolist} [, {options}])
151+
List diff two Lists of strings
150152
diff_filler({lnum}) Number diff filler lines about {lnum}
151153
diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col}
152154
digraph_get({chars}) String get the |digraph| of {chars}
@@ -2046,6 +2048,67 @@ did_filetype() Returns |TRUE| when autocommands are being executed and the
20462048
editing another buffer to set 'filetype' and load a syntax
20472049
file.
20482050

2051+
diff({fromlist}, {tolist} [, {options}]) *diff()*
2052+
Returns a String or a List containing the diff between the
2053+
strings in {fromlist} and {tolist}. Uses the Vim internal
2054+
diff library to compute the diff.
2055+
2056+
*E106*
2057+
The optional "output" item in {options} specifies the returned
2058+
diff format. The following values are supported:
2059+
indices Return a List of the starting and ending
2060+
indices and a count of the strings in each
2061+
diff hunk.
2062+
unified Return the unified diff output as a String.
2063+
This is the default.
2064+
2065+
If the "output" item in {options} is "indices", then a List is
2066+
returned. Each List item contains a Dict with the following
2067+
items for each diff hunk:
2068+
from_idx start index in {fromlist} for this diff hunk.
2069+
from_count number of strings in {fromlist} that are
2070+
added/removed/modified in this diff hunk.
2071+
to_idx start index in {tolist} for this diff hunk.
2072+
to_count number of strings in {tolist} that are
2073+
added/removed/modified in this diff hunk.
2074+
2075+
The {options} Dict argument also specifies diff options
2076+
(similar to 'diffopt') and supports the following items:
2077+
iblank ignore changes where lines are all
2078+
blank.
2079+
icase ignore changes in case of text.
2080+
iwhite ignore changes in amount of white
2081+
space.
2082+
iwhiteall ignore all white space changes.
2083+
iwhiteeol ignore white space changes at end of
2084+
line.
2085+
indent-heuristic use the indent heuristic for the
2086+
internal diff library.
2087+
algorithm Dict specifying the diff algorithm to
2088+
use. Supported boolean items are
2089+
"myers", "minimal", "patience" and
2090+
"histogram".
2091+
For more information about these options, refer to 'diffopt'.
2092+
2093+
Returns an empty List or String if {fromlist} and {tolist} are
2094+
identical.
2095+
2096+
Examples:
2097+
:echo diff(['abc'], ['xxx'])
2098+
@@ -1 +1 @@
2099+
-abc
2100+
+xxx
2101+
2102+
:echo diff(['abc'], ['xxx'], {'output': 'indices'})
2103+
[{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1}]
2104+
:echo diff(readfile('oldfile'), readfile('newfile'))
2105+
:echo diff(getbufline(5, 1, '$'), getbufline(6, 1, '$'))
2106+
2107+
For more examples, refer to |diff-func-examples|
2108+
2109+
Can also be used as a |method|: >
2110+
GetFromList->diff(to_list)
2111+
<
20492112
diff_filler({lnum}) *diff_filler()*
20502113
Returns the number of filler lines above line {lnum}.
20512114
These are the lines that were inserted at this point in

runtime/doc/diff.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*diff.txt* For Vim version 9.1. Last change: 2023 Apr 04
1+
*diff.txt* For Vim version 9.1. Last change: 2024 Feb 01
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -476,4 +476,43 @@ Otherwise, the expression is evaluated in the context of the script where the
476476
option was set, thus script-local items are available.
477477

478478

479+
DIFF FUNCTION EXAMPLES *diff-func-examples*
480+
481+
Some examples for using the |diff()| function to compute the diff indices
482+
between two Lists of strings are below.
483+
>
484+
" some lines are changed
485+
:echo diff(['abc', 'def', 'ghi'], ['abx', 'rrr', 'xhi'], {'output': 'indices'})
486+
[{'from_idx': 0, 'from_count': 3, 'to_idx': 0, 'to_count': 3}]
487+
488+
" few lines added at the beginning
489+
:echo diff(['ghi'], ['abc', 'def', 'ghi'], {'output': 'indices'})
490+
[{'from_idx': 0, 'from_count': 0, 'to_idx': 0, 'to_count': 2}]
491+
492+
" few lines removed from the beginning
493+
:echo diff(['abc', 'def', 'ghi'], ['ghi'], {'output': 'indices'})
494+
[{'from_idx': 0, 'from_count': 2, 'to_idx': 0, 'to_count': 0}]
495+
496+
" few lines added in the middle
497+
:echo diff(['abc', 'jkl'], ['abc', 'def', 'ghi', 'jkl'], {'output': 'indices'})
498+
[{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}]
499+
500+
" few lines removed in the middle
501+
:echo diff(['abc', 'def', 'ghi', 'jkl'], ['abc', 'jkl'], {'output': 'indices'})
502+
[{'from_idx': 1, 'from_count': 2, 'to_idx': 1, 'to_count': 0}]
503+
504+
" few lines added at the end
505+
:echo diff(['abc'], ['abc', 'def', 'ghi'], {'output': 'indices'})
506+
[{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}]
507+
508+
" few lines removed from the end
509+
:echo diff(['abc', 'def', 'ghi'], ['abc'], {'output': 'indices'})
510+
[{'from_idx': 1, 'from_count': 2, 'to_idx': 1, 'to_count': 0}]
511+
512+
" disjointed changes
513+
:echo diff(['ab', 'def', 'ghi', 'jkl'], ['abc', 'def', 'ghi', 'jk'], {'output': 'indices'})
514+
[{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1},
515+
{'from_idx': 3, 'from_count': 1, 'to_idx': 3, 'to_count': 1}]
516+
<
517+
479518
vim:tw=78:ts=8:noet:ft=help:norl:

runtime/doc/tags

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,7 @@ E1056 vim9.txt /*E1056*
41374137
E1057 vim9.txt /*E1057*
41384138
E1058 vim9.txt /*E1058*
41394139
E1059 vim9.txt /*E1059*
4140+
E106 builtin.txt /*E106*
41404141
E1060 vim9.txt /*E1060*
41414142
E1061 vim9.txt /*E1061*
41424143
E1062 eval.txt /*E1062*
@@ -6759,7 +6760,9 @@ dict-identity eval.txt /*dict-identity*
67596760
dict-modification eval.txt /*dict-modification*
67606761
did_filetype() builtin.txt /*did_filetype()*
67616762
diff diff.txt /*diff*
6763+
diff() builtin.txt /*diff()*
67626764
diff-diffexpr diff.txt /*diff-diffexpr*
6765+
diff-func-examples diff.txt /*diff-func-examples*
67636766
diff-mode diff.txt /*diff-mode*
67646767
diff-options diff.txt /*diff-options*
67656768
diff-original-file diff.txt /*diff-original-file*

runtime/doc/todo.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*todo.txt* For Vim version 9.1. Last change: 2024 Jan 14
1+
*todo.txt* For Vim version 9.1. Last change: 2024 Feb 01
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -956,9 +956,6 @@ When 'sidescrolloff' is set, using "zl" to go to the end of the line, suddenly
956956
scrolls back. Should allow for this scrolling, like 'scrolloff' does when
957957
using CTRL-E. (Yee Cheng Chin, #3721)
958958

959-
Add function to make use of internal diff, working on two lists and returning
960-
unified diff (list of lines).
961-
962959
When splitting a window with few text lines, the relative cursor position is
963960
kept, which means part of the text isn't displayed. Better show all the text
964961
when possible. (Dylan Lloyd, #3973)

runtime/doc/usr_41.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*usr_41.txt* For Vim version 9.1. Last change: 2024 Jan 13
1+
*usr_41.txt* For Vim version 9.1. Last change: 2024 Feb 01
22

33
VIM USER MANUAL - by Bram Moolenaar
44

@@ -1368,6 +1368,7 @@ Various: *various-functions*
13681368
changenr() return number of most recent change
13691369
cscope_connection() check if a cscope connection exists
13701370
did_filetype() check if a FileType autocommand was used
1371+
diff() diff two Lists of strings
13711372
eventhandler() check if invoked by an event handler
13721373
getpid() get process ID of Vim
13731374
getscriptinfo() get list of sourced vim scripts

0 commit comments

Comments
 (0)