]> git.jsancho.org Git - gacela.git/blob - src/gacela.c
(no commit message)
[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_paren(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 void
96 init_gacela_client ()
97 {
98   /* init bouncing parens */
99   rl_bind_key(')', match_paren);
100   rl_bind_key(']', match_paren);
101   rl_bind_key('}', match_paren);
102 }
103
104 void
105 gacela_client (void)
106 {
107   char *line;
108
109   init_gacela_client ();
110
111   while (1) {
112     line = readline ("gacela>");
113     printf ("%s\n", line);
114     if (line && *line)
115       add_history (line);
116     free (line);
117   }
118 }
119
120 static void*
121 init_gacela (void *data, int argc, char **argv)
122 {
123   // Guile configuration
124   scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
125   scm_c_eval_string ("(use-modules (ice-9 readline))");
126   scm_c_eval_string ("(activate-readline)");
127   scm_c_eval_string ("(use-modules (ice-9 optargs))");
128   scm_c_eval_string ("(use-modules (ice-9 receive))");
129   scm_c_eval_string ("(use-modules (ice-9 threads))");
130   scm_c_eval_string ("(read-enable 'case-insensitive)");
131
132   // Bindings for C functions and structs
133   SDL_register_functions (NULL);
134   GL_register_functions (NULL);
135   FTGL_register_functions (NULL);
136
137   return NULL;
138 }
139
140 void
141 load_scheme_files (char *path)
142 {
143   char load_path[strlen (path) + 1024];
144
145   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
146   scm_c_eval_string (load_path);
147   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153   /*
154   scm_with_guile (&init_gacela, NULL);
155   load_scheme_files (dirname (argv[0]));
156   scm_shell (argc, argv);
157   */
158   gacela_client ();
159 }