1 /* Gacela, a GNU Guile extension for fast games development
2 Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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"
27 find_matching_paren (int k)
31 int end_parens_found = 0;
33 /* Choose the corresponding opening bracket. */
34 if (k == ')') c = '(';
35 else if (k == ']') c = '[';
36 else if (k == '}') c = '{';
38 for (i = rl_point-2; i >= 0; i--)
40 /* Is the current character part of a character literal? */
42 && rl_line_buffer[i - 1] == '\\'
43 && rl_line_buffer[i - 2] == '#')
45 else if (rl_line_buffer[i] == k)
47 else if (rl_line_buffer[i] == '"')
49 /* Skip over a string literal. */
50 for (i--; i >= 0; i--)
51 if (rl_line_buffer[i] == '"'
53 && rl_line_buffer[i - 1] == '\\'))
56 else if (rl_line_buffer[i] == c)
58 if (end_parens_found==0) return i;
59 else --end_parens_found;
66 match_paren2 (int x, int k)
70 struct timeval timeout;
74 /* Did we just insert a quoted paren? If so, then don't bounce. */
76 && rl_line_buffer[rl_point - 2] == '\\')
80 timeout.tv_usec = 500000;
82 FD_SET (fileno(rl_instream), &readset);
86 rl_point = find_matching_paren(k);
89 //scm_internal_select(1, &readset, NULL, NULL, &timeout);
96 match_paren (int x, int k)
101 struct timeval timeout;
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] == '\\')
111 timeout.tv_sec = tmp / 1000000;
112 timeout.tv_usec = tmp % 1000000;
114 fno = fileno (rl_instream);
115 FD_SET (fno, &readset);
120 rl_point = find_matching_paren (k);
124 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
132 init_gacela_client ()
134 /* init bouncing parens */
135 rl_bind_key (')', match_paren);
136 rl_bind_key (']', match_paren);
137 rl_bind_key ('}', match_paren);
145 init_gacela_client ();
148 line = readline ("gacela>");
149 printf ("%s\n", line);
157 init_gacela (void *data, int argc, char **argv)
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)");
168 // Bindings for C functions and structs
169 SDL_register_functions (NULL);
170 GL_register_functions (NULL);
171 FTGL_register_functions (NULL);
177 load_scheme_files (char *path)
179 char load_path[strlen (path) + 1024];
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"));
187 main (int argc, char *argv[])
190 scm_with_guile (&init_gacela, NULL);
191 load_scheme_files (dirname (argv[0]));
192 scm_shell (argc, argv);