# [PATCH] Add built-in diff patterns for MATLAB code

5 messages from 2011-11-13 to 2011-11-15. Participants: Gustaf Hendeby, Thomas Rast.
Thread: https://gitlist.dev/t/28924

## Gustaf Hendeby, 2011-11-13 13:42

Subject: [PATCH] Add built-in diff patterns for MATLAB code
Message-ID: <1321191764-11972-1-git-send-email-hendeby@isy.liu.se>
URL: https://gitlist.dev/e/1321191764-11972-1-git-send-email-hendeby%40isy.liu.se

```
MATLAB is often used in industry and academia for scientific
computations motivating it being included as a build in pattern.

Signed-off-by: Gustaf Hendeby <hendeby@isy.liu.se>
---
 Documentation/gitattributes.txt |    2 ++
 t/t4018-diff-funcname.sh        |    2 +-
 t/t4034-diff-words.sh           |    1 +
 t/t4034/matlab/expect           |   14 ++++++++++++++
 t/t4034/matlab/post             |    9 +++++++++
 t/t4034/matlab/pre              |    9 +++++++++
 userdiff.c                      |    3 +++
 7 files changed, 39 insertions(+), 1 deletions(-)
 create mode 100644 t/t4034/matlab/expect
 create mode 100644 t/t4034/matlab/post
 create mode 100644 t/t4034/matlab/pre

diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index 25e46ae..a85b187 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -500,6 +500,8 @@ patterns are available:
 
 - `java` suitable for source code in the Java language.
 
+- `matlab` suitable for source code in the MATLAB language.
+
 - `objc` suitable for source code in the Objective-C language.
 
 - `pascal` suitable for source code in the Pascal/Delphi language.
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
index b68c56b..4bd2a1c 100755
--- a/t/t4018-diff-funcname.sh
+++ b/t/t4018-diff-funcname.sh
@@ -105,7 +105,7 @@ test_expect_funcname () {
 	grep "^@@.*@@ $1" diff
 }
 
-for p in bibtex cpp csharp fortran html java objc pascal perl php python ruby tex
+for p in bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
 do
 	test_expect_success "builtin $p pattern compiles" '
 		echo "*.java diff=$p" >.gitattributes &&
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index c374aa4..6f1e5a2 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -299,6 +299,7 @@ test_language_driver csharp
 test_language_driver fortran
 test_language_driver html
 test_language_driver java
+test_language_driver matlab
 test_language_driver objc
 test_language_driver pascal
 test_language_driver perl
diff --git a/t/t4034/matlab/expect b/t/t4034/matlab/expect
new file mode 100644
index 0000000..d846ce9
--- /dev/null
+++ b/t/t4034/matlab/expect
@@ -0,0 +1,14 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index ac2af08..0f07497 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,9 +1,9 @@<RESET>
+(<RED>1<RESET><GREEN>0<RESET>) (<RED>-1e10<RESET><GREEN>-0e10<RESET>) '<RED>b<RESET><GREEN>y<RESET>';
+[<RED>a<RESET><GREEN>x<RESET>] {<RED>a<RESET><GREEN>x<RESET>} <RED>a<RESET><GREEN>x<RESET>.<RED>b<RESET><GREEN>y<RESET>;
+~<RED>a<RESET><GREEN>x<RESET>;
+<RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>.*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>./<RED>b a<RESET><GREEN>y x<RESET>^<RED>b a<RESET><GREEN>y x<RESET>.^<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>&<RED>b a<RESET><GREEN>y x<RESET>&&<RED>b a<RESET><GREEN>y x<RESET>|<RED>b a<RESET><GREEN>y x<RESET>||<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET><GREEN>y<RESET>;
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>~=<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>,<RED>b<RESET><GREEN>y<RESET>;
diff --git a/t/t4034/matlab/post b/t/t4034/matlab/post
new file mode 100644
index 0000000..0f07497
--- /dev/null
+++ b/t/t4034/matlab/post
@@ -0,0 +1,9 @@
+(0) (-0e10) 'y';
+[x] {x} x.y;
+~x;
+x*y x.*y x/y x./y x^y x.^y;
+x+y x-y;
+x&y x&&y x|y x||y;
+x<y x<=y x>y x>=y;
+x==y x~=y;
+x,y;
diff --git a/t/t4034/matlab/pre b/t/t4034/matlab/pre
new file mode 100644
index 0000000..ac2af08
--- /dev/null
+++ b/t/t4034/matlab/pre
@@ -0,0 +1,9 @@
+(1) (-1e10) 'b';
+[a] {a} a.b;
+~a;
+a*b a.*b a/b a./b a^b a.^b;
+a+b a-b;
+a&b a&&b a|b a||b;
+a<b a<=b a>b a>=b;
+==b a~=b;
+a,b;
diff --git a/userdiff.c b/userdiff.c
index bf553ad..e0948e6 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -37,6 +37,9 @@ PATTERNS("java",
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]="
 	 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
+PATTERNS("matlab",
+	 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
+	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\|\\||&&"),
 PATTERNS("objc",
 	 /* Negate C statements that can look like functions */
 	 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
-- 
1.7.8.rc1.222.g29897

```

## Thomas Rast, 2011-11-15 12:37

Subject: Re: [PATCH] Add built-in diff patterns for MATLAB code
Message-ID: <201111151337.07013.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201111151337.07013.trast%40student.ethz.ch
In-Reply-To: <1321191764-11972-1-git-send-email-hendeby@isy.liu.se>

```
Gustaf Hendeby wrote:
> +PATTERNS("matlab",
> +	 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
> +	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\|\\||&&"),

Shouldn't you, for matlab, ensure that ./ "sticks" as a single word
since it is an operator?  At least we used the same logic for the C ||
and && operators, which you copied.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

```

## Gustaf Hendeby, 2011-11-15 12:47

Subject: Re: [PATCH] Add built-in diff patterns for MATLAB code
Message-ID: <df238a1919b7c7d05749b4aa637fe2c7@isy.liu.se>
URL: https://gitlist.dev/e/df238a1919b7c7d05749b4aa637fe2c7%40isy.liu.se
In-Reply-To: <201111151337.07013.trast@student.ethz.ch>

```
 On Tue, 15 Nov 2011 13:37:06 +0100, Thomas Rast wrote:
> Gustaf Hendeby wrote:
>> +PATTERNS("matlab",
>> +	 
>> "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
>> +	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\|\\||&&"),
>
> Shouldn't you, for matlab, ensure that ./ "sticks" as a single word
> since it is an operator?  At least we used the same logic for the C 
> ||
> and && operators, which you copied.

 Good point Thomas, I forgot all about the .-operators.  I will add 
 \.[*/^'] as words, are there any other ones to consider?  Thanks for 
 paying attention!

 /Gustaf

```

## Thomas Rast, 2011-11-15 13:14

Subject: Re: [PATCH] Add built-in diff patterns for MATLAB code
Message-ID: <201111151414.34141.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201111151414.34141.trast%40student.ethz.ch
In-Reply-To: <df238a1919b7c7d05749b4aa637fe2c7@isy.liu.se>

```
Gustaf Hendeby wrote:
>  On Tue, 15 Nov 2011 13:37:06 +0100, Thomas Rast wrote:
> > Gustaf Hendeby wrote:
> >> +PATTERNS("matlab",
> >> +	 
> >> "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
> >> +	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\|\\||&&"),
> >
> > Shouldn't you, for matlab, ensure that ./ "sticks" as a single word
> > since it is an operator?  At least we used the same logic for the C 
> > ||
> > and && operators, which you copied.
> 
>  Good point Thomas, I forgot all about the .-operators.  I will add 
>  \.[*/^'] as words, are there any other ones to consider?  Thanks for 
>  paying attention!

Uh, I'm afraid it's been years since I did matlab.  But

  http://www.mathworks.ch/help/techdoc/matlab_prog/f0-40063.html

seems to indicate you forgot '.\'.

Please also update the testcases to whether this splits correctly,
e.g., by changing './' to '/'.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

```

## Gustaf Hendeby, 2011-11-15 20:15

Subject: [PATCH v2] Add built-in diff patterns for MATLAB code
Message-ID: <1321388103-32225-1-git-send-email-hendeby@isy.liu.se>
URL: https://gitlist.dev/e/1321388103-32225-1-git-send-email-hendeby%40isy.liu.se
In-Reply-To: <201111151414.34141.trast@student.ethz.ch>

```
MATLAB is often used in industry and academia for scientific
computations motivating it being included as a build in pattern.

Signed-off-by: Gustaf Hendeby <hendeby@isy.liu.se>
---

This version of this commit adds the missing patterns to make the
.-operators words and adds these to the test.  Lots of thanks Thomas
for keeping your eyes open and catching this.

/Gustaf

 Documentation/gitattributes.txt |    2 ++
 t/t4018-diff-funcname.sh        |    2 +-
 t/t4034-diff-words.sh           |    1 +
 t/t4034/matlab/expect           |   14 ++++++++++++++
 t/t4034/matlab/post             |    9 +++++++++
 t/t4034/matlab/pre              |    9 +++++++++
 userdiff.c                      |    3 +++
 7 files changed, 39 insertions(+), 1 deletions(-)
 create mode 100644 t/t4034/matlab/expect
 create mode 100644 t/t4034/matlab/post
 create mode 100644 t/t4034/matlab/pre

diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index 25e46ae..a85b187 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -500,6 +500,8 @@ patterns are available:
 
 - `java` suitable for source code in the Java language.
 
+- `matlab` suitable for source code in the MATLAB language.
+
 - `objc` suitable for source code in the Objective-C language.
 
 - `pascal` suitable for source code in the Pascal/Delphi language.
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
index b68c56b..4bd2a1c 100755
--- a/t/t4018-diff-funcname.sh
+++ b/t/t4018-diff-funcname.sh
@@ -105,7 +105,7 @@ test_expect_funcname () {
 	grep "^@@.*@@ $1" diff
 }
 
-for p in bibtex cpp csharp fortran html java objc pascal perl php python ruby tex
+for p in bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
 do
 	test_expect_success "builtin $p pattern compiles" '
 		echo "*.java diff=$p" >.gitattributes &&
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index c374aa4..6f1e5a2 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -299,6 +299,7 @@ test_language_driver csharp
 test_language_driver fortran
 test_language_driver html
 test_language_driver java
+test_language_driver matlab
 test_language_driver objc
 test_language_driver pascal
 test_language_driver perl
diff --git a/t/t4034/matlab/expect b/t/t4034/matlab/expect
new file mode 100644
index 0000000..72cf3e9
--- /dev/null
+++ b/t/t4034/matlab/expect
@@ -0,0 +1,14 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index dc204db..70e05f0 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,9 +1,9 @@<RESET>
+(<RED>1<RESET><GREEN>0<RESET>) (<RED>-1e10<RESET><GREEN>-0e10<RESET>) '<RED>b<RESET><GREEN>y<RESET>';
+[<RED>a<RESET><GREEN>x<RESET>] {<RED>a<RESET><GREEN>x<RESET>} <RED>a<RESET><GREEN>x<RESET>.<RED>b<RESET><GREEN>y<RESET>;
+~<RED>a<RESET><GREEN>x<RESET>;
+<RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>.*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>./<RED>b a<RESET><GREEN>y x<RESET>^<RED>b a<RESET><GREEN>y x<RESET>.^<RED>b a<RESET><GREEN>y x<RESET>.\<RED>b a<RESET><GREEN>y x<RESET>.';
+<RED>a<RESET><GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>&<RED>b a<RESET><GREEN>y x<RESET>&&<RED>b a<RESET><GREEN>y x<RESET>|<RED>b a<RESET><GREEN>y x<RESET>||<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>~=<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>,<RED>b<RESET><GREEN>y<RESET>;
diff --git a/t/t4034/matlab/post b/t/t4034/matlab/post
new file mode 100644
index 0000000..70e05f0
--- /dev/null
+++ b/t/t4034/matlab/post
@@ -0,0 +1,9 @@
+(0) (-0e10) 'y';
+[x] {x} x.y;
+~x;
+x*y x.*y x/y x./y x^y x.^y x.\y x.';
+x+y x-y;
+x&y x&&y x|y x||y;
+x<y x<=y x>y x>=y;
+x==y x~=y;
+x,y;
diff --git a/t/t4034/matlab/pre b/t/t4034/matlab/pre
new file mode 100644
index 0000000..dc204db
--- /dev/null
+++ b/t/t4034/matlab/pre
@@ -0,0 +1,9 @@
+(1) (-1e10) 'b';
+[a] {a} a.b;
+~a;
+a*b a.*b a/b a./b a^b a.^b a.\b a.';
+a+b a-b;
+a&b a&&b a|b a||b;
+a<b a<=b a>b a>=b;
+a==b a~=b;
+a,b;
diff --git a/userdiff.c b/userdiff.c
index bf553ad..7c983c1 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -37,6 +37,9 @@ PATTERNS("java",
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]="
 	 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
+PATTERNS("matlab",
+	 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
+	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
 PATTERNS("objc",
 	 /* Negate C statements that can look like functions */
 	 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
-- 
1.7.8.rc2.209.geddc4

```
