From 7b5b378efadaa9152fec764f224630a90fdbad2f Mon Sep 17 00:00:00 2001
From: jsancho <devnull@localhost>
Date: Wed, 24 Aug 2011 08:07:09 +0000
Subject: [PATCH]

---
 gacela_tetris.scm |  2 +-
 src/gacela.c      | 60 +++++++++++++++++++++++++++++++++++------------
 src/gacela.scm    |  4 ++--
 3 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/gacela_tetris.scm b/gacela_tetris.scm
index e8a97bd..63e0fc7 100644
--- a/gacela_tetris.scm
+++ b/gacela_tetris.scm
@@ -1,4 +1,4 @@
-(set-game-properties #:title "Gacela Tetris" #:fps 15)
+(set-game-properties! #:title "Gacela Tetris" #:fps 15)
 
 (define (tetramine-i)
   (let ((color '(1 0 0)))
diff --git a/src/gacela.c b/src/gacela.c
index 6891d4b..489d8c6 100644
--- a/src/gacela.c
+++ b/src/gacela.c
@@ -133,13 +133,36 @@ init_gacela_client ()
   sigaction (SIGINT, &new_action, NULL);
 }
 
+int
+opened_parens (char *line, int k)
+{
+  int i;
+  int opened = 0;
+  char c = 0;
+
+  // Choose the corresponding opening bracket
+  if (k == ')') c = '(';
+  else if (k == ']') c = '[';
+  else if (k == '}') c = '{';
+
+  for (i = 0; i < strlen (line); i++) {
+    if (line[i] == c)
+      opened++;
+    else if (line[i] == k)
+      opened--;
+  }
+
+  return opened;
+}
+
 void
 gacela_client (SCM rec_channel, SCM send_channel)
 {
   int n;
   SCM buffer;
-  char *line;
+  char *line, *line_for_sending;
   char *history_path;
+  int opened = 0;
 
   // Command line
   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
@@ -148,25 +171,32 @@ gacela_client (SCM rec_channel, SCM send_channel)
   read_history (history_path);
 
   while (1) {
-    line = readline ("gacela> ");
+    if (opened > 0)
+      line = readline ("... ");
+    else
+      line = readline ("gacela> ");
+
+    opened += opened_parens (line, ')');
     ctrl_c = 0;
     if (!line) break;
     if (line && *line)
       {
 	add_history (line);
-	scm_write (scm_from_locale_string (line), send_channel);
-	scm_force_output (send_channel);
-
-	while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
-	  if (ctrl_c) break;
-	  sleep (0.5);
-	}
-	if (ctrl_c)
-	  ctrl_c = 0;
-	else {
-	  buffer = scm_read (rec_channel);
-	  if (strlen (scm_to_locale_string (buffer)) > 0)
-	    printf ("%s\n", scm_to_locale_string (buffer));
+	if (opened == 0) {
+	  scm_write (scm_from_locale_string (line), send_channel);
+	  scm_force_output (send_channel);
+
+	  while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
+	    if (ctrl_c) break;
+	    sleep (0.5);
+	  }
+	  if (ctrl_c)
+	    ctrl_c = 0;
+	  else {
+	    buffer = scm_read (rec_channel);
+	    if (strlen (scm_to_locale_string (buffer)) > 0)
+	      printf ("%s\n", scm_to_locale_string (buffer));
+	  }
 	}
       }
     free (line);
diff --git a/src/gacela.scm b/src/gacela.scm
index fe31b8f..f5a751a 100644
--- a/src/gacela.scm
+++ b/src/gacela.scm
@@ -189,11 +189,11 @@
 		   (SDL_Delay (- time-per-frame frame-time))))))))
 
 
-(define set-game-properties #f)
+(define set-game-properties! #f)
 (define get-game-properties #f)
 
 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
-  (set! set-game-properties
+  (set! set-game-properties!
 	(lambda* (#:key title width height bpp fps mode)
 ;	  (init-video-mode)
 	  (if title
-- 
2.39.5