]> git.ipfire.org Git - thirdparty/git.git/commitdiff
userdiff: extend Scheme support to cover other Lisp dialects
authorScott L. Burson <Scott@sympoiesis.com>
Wed, 15 Apr 2026 02:27:43 +0000 (02:27 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Apr 2026 15:43:33 +0000 (08:43 -0700)
Common Lisp has top-level forms, such as 'defun' and 'defmacro', that
are not matched by the current Scheme pattern.  Also, it is more
common in CL, when defining user macros intended as top-level forms,
to prefix their names with "def" instead of "define"; such forms are
also not matched.  And some top-level forms don't even begin with
"def".

On the other hand, it is an established formatting convention in the
Lisp community that only top-level forms start at the left margin.  So
matching any unindented line starting with an open parenthesis is an
acceptable heuristic; false positives will be rare.

However, there are also cases where notionally top-level forms are
grouped together within some containing form.  At least in the Common
Lisp community, it is conventional to indent these by two spaces, or
sometimes one.  But matching just an open parenthesis indented by two
spaces would be too broad; so the pattern added by this commit
requires an indented form to start with "(def".  It is believed that
this strikes a good balance between potential false positives and
false negatives.

Signed-off-by: Scott L. Burson <Scott@sympoiesis.com>
Acked-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/gitattributes.adoc
t/t4018/scheme-lisp-defun-a [new file with mode: 0644]
t/t4018/scheme-lisp-defun-b [new file with mode: 0644]
t/t4018/scheme-lisp-eval-when [new file with mode: 0644]
t/t4018/scheme-module-a [moved from t/t4018/scheme-module with 100% similarity]
t/t4018/scheme-module-b [new file with mode: 0644]
t/t4034/scheme/expect
t/t4034/scheme/post
t/t4034/scheme/pre
userdiff.c

index f20041a323d174504a6358d2f499e49c9df38c9d..bd76167a45eb716cbeecefb030ec307ccf7ae01a 100644 (file)
@@ -911,7 +911,8 @@ patterns are available:
 
 - `rust` suitable for source code in the Rust language.
 
-- `scheme` suitable for source code in the Scheme language.
+- `scheme` suitable for source code in most Lisp dialects,
+  including Scheme, Emacs Lisp, Common Lisp, and Clojure.
 
 - `tex` suitable for source code for LaTeX documents.
 
diff --git a/t/t4018/scheme-lisp-defun-a b/t/t4018/scheme-lisp-defun-a
new file mode 100644 (file)
index 0000000..c3c750f
--- /dev/null
@@ -0,0 +1,4 @@
+(defun some-func (x y z) RIGHT
+  (let ((a x)
+        (b y))
+        (ChangeMe a b)))
diff --git a/t/t4018/scheme-lisp-defun-b b/t/t4018/scheme-lisp-defun-b
new file mode 100644 (file)
index 0000000..21be305
--- /dev/null
@@ -0,0 +1,4 @@
+(macrolet ((foo (x) `(bar ,x)))
+  (defun mumble (x) ; RIGHT
+    (when (> x 0)
+      (foo x)))) ; ChangeMe
diff --git a/t/t4018/scheme-lisp-eval-when b/t/t4018/scheme-lisp-eval-when
new file mode 100644 (file)
index 0000000..5d941d7
--- /dev/null
@@ -0,0 +1,4 @@
+(eval-when (:compile-toplevel :load-toplevel :execute)  ; RIGHT
+  (set-macro-character #\?
+                      (lambda (stream char)
+                        `(make-pattern-variable ,(read stream)))))  ; ChangeMe
diff --git a/t/t4018/scheme-module-b b/t/t4018/scheme-module-b
new file mode 100644 (file)
index 0000000..77bc0c5
--- /dev/null
@@ -0,0 +1,6 @@
+(module A
+  (export with-display-exception)
+  (extern (display-exception display-exception))
+  (def (with-display-exception thunk) RIGHT
+    (with-catch (lambda (e) (display-exception e (current-error-port)) e)
+      thunk ChangeMe)))
index 138abe9f56b38f84bd8b3a1b8aa081ffbeb5e215..fb7f2616fea547d971929ed8ba99b7473ebb1dad 100644 (file)
@@ -6,7 +6,7 @@
 (define (<RED>myfunc a b<RESET><GREEN>my-func first second<RESET>)
   ; This is a <RED>really<RESET><GREEN>(moderately)<RESET> cool function.
   (<RED>this\place<RESET><GREEN>that\place<RESET> (+ 3 4))
-  (define <RED>|the greeting|<RESET><GREEN>|a greeting|<RESET> "hello")
+  (define <RED>|the \| \greeting|<RESET><GREEN>|a \greeting|<RESET> |hello there|)
   ({<RED>}<RESET>(([<RED>]<RESET>(func-n)<RED>[<RESET>]))<RED>{<RESET>})
   (let ((c (<RED>+ a b<RESET><GREEN>add1 first<RESET>)))
     (format "one more than the total is %d" (<RED>add1<RESET><GREEN>+<RESET> c <GREEN>second<RESET>))))
index 0e3bab101da03e1cb567e057a65b36bb98e0d6ee..450cc234f75aeafc06a344530167c3b799b56433 100644 (file)
@@ -1,7 +1,7 @@
 (define (my-func first second)
   ; This is a (moderately) cool function.
   (that\place (+ 3 4))
-  (define |a greeting| "hello")
+  (define |a \greeting| |hello there|)
   ({(([(func-n)]))})
   (let ((c (add1 first)))
     (format "one more than the total is %d" (+ c second))))
index 03d77c7c430e0763a36526693db917da87eaba56..e16ee7584946e4f7cbe6c1c9d365562101e21b60 100644 (file)
@@ -1,7 +1,7 @@
 (define (myfunc a b)
   ; This is a really cool function.
   (this\place (+ 3 4))
-  (define |the greeting| "hello")
+  (define |the \| \greeting| |hello there|)
   ({}(([](func-n)[])){})
   (let ((c (+ a b)))
     (format "one more than the total is %d" (add1 c))))
index fe710a68bfdfa64f490873335bd40adc5db8cd78..b5412e6bc3ecd390d71a31e4d884d66d2af6c910 100644 (file)
@@ -344,14 +344,24 @@ PATTERNS("rust",
         "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
         "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
 PATTERNS("scheme",
-        "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
         /*
-         * R7RS valid identifiers include any sequence enclosed
-         * within vertical lines having no backslashes
+         * An unindented opening parenthesis identifies a top-level
+         * expression in all Lisp dialects.
          */
-        "\\|([^\\\\]*)\\|"
-        /* All other words should be delimited by spaces or parentheses */
-        "|([^][)(}{[ \t])+"),
+        "^(\\(.*)$\n"
+        /* For Scheme: a possibly indented left paren followed by a keyword. */
+        "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$\n"
+        /*
+         * For all Lisp dialects: a slightly indented line starting with "(def".
+         */
+        "^  ?(\\([Dd][Ee][Ff].*)$",
+        /*
+         * The union of R7RS and Common Lisp symbol syntax: allows arbitrary
+         * strings between vertical bars, including any escaped characters.
+         */
+        "\\|([^|\\\\]|\\\\.)*\\|"
+        /* All other words should be delimited by spaces or parentheses. */
+        "|([^][)(}{ \t])+"),
 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
         "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
 { .name = "default", .binary = -1 },