Skip to content

Commit 4a5a712

Browse files
committed
userdiff: extend Scheme support to cover other Lisp dialects
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 <[email protected]>
1 parent ed541bb commit 4a5a712

File tree

10 files changed

+38
-9
lines changed

10 files changed

+38
-9
lines changed

Documentation/gitattributes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ patterns are available:
912912
- `rust` suitable for source code in the Rust language.
913913

914914
- `scheme` suitable for source code in the Scheme language.
915+
Also handles Emacs Lisp, Common Lisp, and most other dialects.
915916

916917
- `tex` suitable for source code for LaTeX documents.
917918

t/t4018/scheme-lisp-defun-a

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defun some-func (x y z) RIGHT
2+
(let ((a x)
3+
(b y))
4+
(ChangeMe a b)))

t/t4018/scheme-lisp-defun-b

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(macrolet ((foo (x) `(bar ,x)))
2+
(defun mumble (x) ; RIGHT
3+
(when (> x 0)
4+
(foo x)))) ; ChangeMe

t/t4018/scheme-lisp-eval-when

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(eval-when (:compile-toplevel :load-toplevel :execute) ; RIGHT
2+
(set-macro-character #\?
3+
(lambda (stream char)
4+
`(make-pattern-variable ,(read stream))))) ; ChangeMe

t/t4018/scheme-module-b

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(module A
2+
(export with-display-exception)
3+
(extern (display-exception display-exception))
4+
(def (with-display-exception thunk) RIGHT
5+
(with-catch (lambda (e) (display-exception e (current-error-port)) e)
6+
thunk ChangeMe)))

t/t4034/scheme/expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(define (<RED>myfunc a b<RESET><GREEN>my-func first second<RESET>)
77
; This is a <RED>really<RESET><GREEN>(moderately)<RESET> cool function.
88
(<RED>this\place<RESET><GREEN>that\place<RESET> (+ 3 4))
9-
(define <RED>|the greeting|<RESET><GREEN>|a greeting|<RESET> "hello")
9+
(define <RED>|the \greeting|<RESET><GREEN>|a \greeting|<RESET> |hello there|)
1010
({<RED>}<RESET>(([<RED>]<RESET>(func-n)<RED>[<RESET>]))<RED>{<RESET>})
1111
(let ((c (<RED>+ a b<RESET><GREEN>add1 first<RESET>)))
1212
(format "one more than the total is %d" (<RED>add1<RESET><GREEN>+<RESET> c <GREEN>second<RESET>))))

t/t4034/scheme/post

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(define (my-func first second)
22
; This is a (moderately) cool function.
33
(that\place (+ 3 4))
4-
(define |a greeting| "hello")
4+
(define |a \greeting| |hello there|)
55
({(([(func-n)]))})
66
(let ((c (add1 first)))
77
(format "one more than the total is %d" (+ c second))))

t/t4034/scheme/pre

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(define (myfunc a b)
22
; This is a really cool function.
33
(this\place (+ 3 4))
4-
(define |the greeting| "hello")
4+
(define |the \greeting| |hello there|)
55
({}(([](func-n)[])){})
66
(let ((c (+ a b)))
77
(format "one more than the total is %d" (add1 c))))

userdiff.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,24 @@ PATTERNS("rust",
344344
"|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
345345
"|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
346346
PATTERNS("scheme",
347-
"^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
348347
/*
349-
* R7RS valid identifiers include any sequence enclosed
350-
* within vertical lines having no backslashes
348+
* An unindented opening parenthesis identifies a top-level
349+
* expression in all Lisp dialects.
351350
*/
352-
"\\|([^\\\\]*)\\|"
353-
/* All other words should be delimited by spaces or parentheses */
354-
"|([^][)(}{[ \t])+"),
351+
"^(\\(.*)$\n"
352+
/* For Scheme: a possibly indented left paren followed by a keyword. */
353+
"^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$\n"
354+
/*
355+
* For all Lisp dialects: a slightly indented line starting with "(def".
356+
*/
357+
"^ ?(\\([Dd][Ee][Ff].*)$",
358+
/*
359+
* The union of R7RS and Common Lisp symbol syntax: allows arbitrary
360+
* strings between vertical bars, including any escaped characters.
361+
*/
362+
"\\|([^|\\\\]|\\\\.)*\\|"
363+
/* All other words should be delimited by spaces or parentheses. */
364+
"|([^][)(}{ \t])+"),
355365
PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
356366
"\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
357367
{ .name = "default", .binary = -1 },

0 commit comments

Comments
 (0)