+ Fixes
[opsoft.git] / silentbob / plugins / plugin_python.cxx
blobd11dfdcc4326b3c2069c6555322bac4836d431fc
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib.h>
8 #include <mod.h>
9 #include <head.h>
10 #include <dbg.h>
11 #include <the_tt.h>
12 #include <wit.h>
14 extern "C" DArray * plugin_init (struct env_t *env);
15 int python_ctags (char * f_name, FILE * of);
17 namespace PYTHON {
18 char * py_tt_file (struct tt_state_t * tt);
19 char t_op (char ** d_in, char ** d_prev);
22 char * py_prepare (char *ptr)
24 char *S;
26 S = strchr (ptr, '(');
27 if (S)
28 *S = '\0';
30 strip2 (ptr);
31 S = strchr (ptr, ' ');
32 return ++S;
35 void python_tag (tt_state_t * tt, char *ptr, char ch, int type, FILE *of)
37 int line = tt->attachment[ENV->t_op_no].pair_line+1;
39 while (*ptr == ' ' || *ptr == '\t')
40 ++ptr;
42 if (type & OT::Class) {
43 if (! strncmp (ptr, "def ", 4)) {
44 ptr = py_prepare (ptr);
45 goto ttry;
49 if (type & OT::Function) {
50 if (! strncmp (ptr, "class ", 6)) {
51 ptr = py_prepare (ptr);
52 goto ttry;
56 return;
57 ttry:
58 fprintf (of, "%s\t%s\t%i\n", ptr, tt->fileName, line);
61 void python_lookup ()
63 DArray * d_array;
65 unlink (ENV->tmp_files);
66 sblib_find ("./", "*.py", ENV->tmp_files);
67 d_array = ENV->d_files;
68 d_array->from_file (ENV->tmp_files);
69 unlink (ENV->tmp_files);
70 d_array->foreach ((Dfunc_t)chomp);
73 void python_ctags_foreach (FILE * of)
75 int i;
76 DArray * d_array = ENV->d_files;
77 for (i = 0; i < d_array->get_size (); ++i) {
78 if (! d_array->get (i))
79 continue;
81 python_ctags (d_array->get (i), of);
82 free (d_array->get (i));
86 int python_ctags (char * f_name, FILE * of)
88 char ch;
89 char *d_ptr,*d_out; // for t_op2
90 struct tt_state_t *tt;
92 if (! f_name) {
93 python_lookup ();
94 python_ctags_foreach (of);
95 return 0;
98 ENV->t_op_no = 0;
100 tt = CNEW (tt_state_t, 1);
101 memset (tt, 0, sizeof (struct tt_state_t));
102 tt->fileName = strdup (f_name);
103 PYTHON::py_tt_file (tt);
105 d_out = tt->result;
106 d_ptr = d_out;
107 while (true) {
108 ch = PYTHON::t_op (&d_ptr, &d_out);
109 ++ENV->t_op_no;
111 if (ch == '\0')
112 break;
114 python_tag (tt, d_out, ch, OT::Function | OT::Class, of);
117 free_tt_state (tt);
118 return 0;
121 void pyc_one (char * f_name)
123 char *S;
124 char ch;
125 char *d_ptr,*d_out; // for t_op2
126 struct tt_state_t *tt;
128 tt = CNEW (tt_state_t, 1);
129 memset (tt, 0, sizeof (struct tt_state_t));
130 tt->fileName = strdup (f_name);
131 PYTHON::py_tt_file (tt);
133 d_out = tt->result;
134 d_ptr = d_out;
135 while (true) {
136 ch = PYTHON::t_op (&d_ptr, &d_out);
138 if (ch == '\0')
139 break;
141 S = d_out;
142 while (*S == ' ' || *S == '\t')
143 ++S;
145 if (! strncmp (S, "class ", 6))
146 printf ("%s\n", py_prepare (S));
149 free_tt_state (tt);
152 void py_class ()
154 int i;
156 if (! ENV->d_files->get_size ())
157 python_lookup ();
159 for (i = 0; i < ENV->d_files->get_size (); ++i)
160 pyc_one (ENV->d_files->get (i));
162 exit (0);
165 char py_opt (DArray * d_opts, int * i)
167 if (! d_opts || ! i)
168 return 0;
170 if (EQ (d_opts->get (*i), "--python")) {
171 ENV->language = strdup ("Python");
172 return 1;
175 if (EQ (d_opts->get (*i), "--files") && EQ (ENV->language, "Python")) {
176 unlink ("./python_files");
177 sblib_find ("./", "*.py", "./python_files");
178 exit (0);
181 return 0;
184 char py_opt2 (DArray * d_opts, int * i)
186 if (EQ (ENV->language, "Python") && EQ (d_opts->get (*i), "--class")) {
187 py_class ();
188 return 1;
190 return 0;
193 void py_short_info ()
195 printf ("Python plugin.");
198 void py_long_info ()
200 printf ("Python plugin.\n");
201 printf ("Version: 1.0\n");
202 printf ("options: --python [--files | --make-ctags | --class ]\n");
205 DArray * plugin_init (struct env_t *env)
207 struct mod_t * pm;
208 struct mod_t * pm_lang;
209 DArray * Ret;
211 pm = CNEW (mod_t, 1);
212 pm_lang = CNEW (mod_t, 1);
214 memset (pm, 0, sizeof (struct mod_t));
215 memset (pm_lang, 0, sizeof (struct mod_t));
217 pm_lang->Version = strdup ("1.0");
218 pm_lang->the = PYTHON::py_tt_file;
219 pm_lang->make_ctags = python_ctags;
220 pm_lang->language = strdup ("Python");
222 pm->Version = strdup ("1.0");
223 pm->opt = py_opt;
224 pm->opt2 = py_opt2;
225 pm->short_info = py_short_info;
226 pm->long_info = py_long_info;
228 ENV->listOptions->add ( "--python");
229 ENV->listOptions->add ( "--files");
230 ENV->listOptions->add ( "--class");
232 Ret = new DArray (2);
233 Ret->add (LPCHAR (pm));
234 Ret->add (LPCHAR (pm_lang));
236 return Ret;