aboutsummaryrefslogtreecommitdiff
path: root/module/c/line-fold.scm
diff options
context:
space:
mode:
Diffstat (limited to 'module/c/line-fold.scm')
-rw-r--r--module/c/line-fold.scm29
1 files changed, 29 insertions, 0 deletions
diff --git a/module/c/line-fold.scm b/module/c/line-fold.scm
new file mode 100644
index 00000000..c61c2c70
--- /dev/null
+++ b/module/c/line-fold.scm
@@ -0,0 +1,29 @@
+(define-module (c line-fold)
+ :use-module (srfi srfi-1)
+ :use-module (srfi srfi-71)
+ :export (fold-lines))
+
+(define (line-continued? line)
+ (and (not (string-null? line))
+ (char=? #\\ (string-ref line (1- (string-length line))))))
+
+(define (strip-backslash line)
+ (string-drop-right line 1))
+
+(define (fold-lines string)
+ (with-output-to-string
+ (lambda ()
+ (let loop ((lines (string-split string #\newline)))
+ (cond ((null? lines) 'NOOP)
+ ((null? (cdr lines))
+ ;; TODO error message if last character is a backslash
+ (display (car lines))
+ (newline))
+ (else
+ (let ((to-merge remaining (span line-continued? lines)))
+ (for-each display (map strip-backslash to-merge))
+ (display (car remaining))
+ (newline)
+ (for-each (lambda _ (newline))
+ (iota (length to-merge)))
+ (loop (cdr remaining)))))))))