]> git.jsancho.org Git - gacela.git/blob - src/gacela.c
d30ec0de4ad82cb598bb65eb7f68cb37134cfb20
[gacela.git] / src / gacela.c
1 /* Gacela, a GNU Guile extension for fast games development
2    Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <libguile.h>
19 #include <libgen.h>
20 #include <readline/readline.h>
21 #include <readline/history.h>
22 #include "gacela_SDL.h"
23 #include "gacela_GL.h"
24 #include "gacela_FTGL.h"
25
26 static int
27 find_matching_paren (int k)
28 {
29   register int i;
30   register char c = 0;
31   int end_parens_found = 0;
32
33   /* Choose the corresponding opening bracket.  */
34   if (k == ')') c = '(';
35   else if (k == ']') c = '[';
36   else if (k == '}') c = '{';
37
38   for (i = rl_point-2; i >= 0; i--)
39     {
40       /* Is the current character part of a character literal?  */
41       if (i - 2 >= 0
42           && rl_line_buffer[i - 1] == '\\'
43           && rl_line_buffer[i - 2] == '#')
44         ;
45       else if (rl_line_buffer[i] == k)
46         end_parens_found++;
47       else if (rl_line_buffer[i] == '"')
48         {
49           /* Skip over a string literal.  */
50           for (i--; i >= 0; i--)
51             if (rl_line_buffer[i] == '"'
52                 && ! (i - 1 >= 0
53                       && rl_line_buffer[i - 1] == '\\'))
54               break;
55         }
56       else if (rl_line_buffer[i] == c)
57         {
58           if (end_parens_found==0) return i;
59           else --end_parens_found;
60         }
61     }
62   return -1;
63 }
64
65 static void
66 match_paren2 (int x, int k)
67 {
68   int tmp;
69   fd_set readset;
70   struct timeval timeout;
71
72   rl_insert (x, k);
73  
74   /* Did we just insert a quoted paren?  If so, then don't bounce.  */
75   if (rl_point - 1 >= 1
76       && rl_line_buffer[rl_point - 2] == '\\')
77     return;
78
79   timeout.tv_sec = 0;
80   timeout.tv_usec = 500000;
81   FD_ZERO (&readset);
82   FD_SET (fileno(rl_instream), &readset);
83
84   if (rl_point > 1) {
85     tmp = rl_point;
86     rl_point = find_matching_paren(k);
87     if (rl_point > -1) {
88       rl_redisplay();
89       //scm_internal_select(1, &readset, NULL, NULL, &timeout);
90     }
91     //rl_point = tmp;
92   }
93 }
94
95 static int
96 match_paren (int x, int k)
97 {
98   int tmp;
99   int fno;
100   SELECT_TYPE readset;
101   struct timeval timeout;
102
103   rl_insert (x, k);
104
105   /* Did we just insert a quoted paren?  If so, then don't bounce.  */
106   if (rl_point - 1 >= 1
107       && rl_line_buffer[rl_point - 2] == '\\')
108     return 0;
109
110   tmp = 1000 * 1000;
111   timeout.tv_sec = tmp / 1000000;
112   timeout.tv_usec = tmp % 1000000;
113   FD_ZERO (&readset);
114   fno = fileno (rl_instream);
115   FD_SET (fno, &readset);
116
117   if (rl_point > 1)
118     {
119       tmp = rl_point;
120       rl_point = find_matching_paren (k);
121       if (rl_point > -1)
122         {
123           rl_redisplay ();
124           scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
125         }
126       rl_point = tmp;
127     }
128   return 0;
129 }
130
131 static void
132 init_gacela_client ()
133 {
134   /* init bouncing parens */
135   rl_bind_key (')', match_paren);
136   rl_bind_key (']', match_paren);
137   rl_bind_key ('}', match_paren);
138 }
139
140 void
141 gacela_client (void)
142 {
143   char *line;
144
145   init_gacela_client ();
146
147   while (1) {
148     line = readline ("gacela>");
149     printf ("%s\n", line);
150     if (line && *line)
151       add_history (line);
152     free (line);
153   }
154 }
155
156 static void*
157 init_gacela (void *data, int argc, char **argv)
158 {
159   // Guile configuration
160   scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
161   scm_c_eval_string ("(use-modules (ice-9 readline))");
162   scm_c_eval_string ("(activate-readline)");
163   scm_c_eval_string ("(use-modules (ice-9 optargs))");
164   scm_c_eval_string ("(use-modules (ice-9 receive))");
165   scm_c_eval_string ("(use-modules (ice-9 threads))");
166   scm_c_eval_string ("(read-enable 'case-insensitive)");
167
168   // Bindings for C functions and structs
169   SDL_register_functions (NULL);
170   GL_register_functions (NULL);
171   FTGL_register_functions (NULL);
172
173   return NULL;
174 }
175
176 void
177 load_scheme_files (char *path)
178 {
179   char load_path[strlen (path) + 1024];
180
181   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
182   scm_c_eval_string (load_path);
183   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
184 }
185
186 int
187 main (int argc, char *argv[])
188 {
189   /*
190   scm_with_guile (&init_gacela, NULL);
191   load_scheme_files (dirname (argv[0]));
192   scm_shell (argc, argv);
193   */
194   gacela_client ();
195 }