Very old versions for history.
[opsoft_archive.git] / silentbob / silentbob-1.1 / src / plugins / plugin_python.cpp
blob57b5f666011c69646247e9a0e6d7f5267622e331
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <dlib.h>
8 #include "../mod.h"
9 #include "../head.h"
10 #include "../dbg.h"
11 #include "../the_tt.h"
12 #include "../sblib/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 if (*stail (ptr) == ' ')
31 *stail (S) = '\0';
33 S = strchr (ptr, ' ');
34 return ++S;
37 void python_tag (tt_state_t * tt, char *ptr, char ch, int type, FILE *of)
39 int line = tt->d_attachment[ENV->t_op_no].pair_line+1;
41 while (*ptr == ' ' || *ptr == '\t')
42 ++ptr;
44 if (type & GLOBAL_TYPE_CLASS) {
45 if (! strncmp (ptr, "def ", 4)) {
46 ptr = py_prepare (ptr);
47 goto ttry;
51 if (type & GLOBAL_TYPE_FUNCTION) {
52 if (! strncmp (ptr, "class ", 6)) {
53 ptr = py_prepare (ptr);
54 goto ttry;
58 return;
59 ttry:
60 fprintf (of, "%s\t%s\t%i\n", ptr, tt->d_file_name, line);
64 void python_lookup ()
66 int i;
67 DArray * d_array;
69 unlink (ENV->tmp_files);
70 sblib_find ("./", "*.py", ENV->tmp_files);
71 d_array = ENV->d_files;
72 d_array->from_file (ENV->tmp_files);
73 unlink (ENV->tmp_files);
74 d_array->foreach ((Dfunc_t)chomp);
77 void python_ctags_foreach (FILE * of)
79 int i;
80 DArray * d_array = ENV->d_files;
81 for (i = 0; i < d_array->get_size (); ++i) {
82 if (! d_array->get (i))
83 continue;
85 python_ctags (d_array->get (i), of);
86 free (d_array->get (i));
90 int python_ctags (char * f_name, FILE * of)
92 char ch;
93 char *d_ptr,*d_out; // for t_op2
94 struct tt_state_t *tt;
96 if (! f_name) {
97 python_lookup ();
98 python_ctags_foreach (of);
99 return 0;
102 ENV->t_op_no = 0;
104 tt = CNEW (tt_state_t, 1);
105 bzero (tt, sizeof (struct tt_state_t));
106 tt->d_file_name = strdup (f_name);
107 PYTHON::py_tt_file (tt);
109 d_out = tt->d_output;
110 d_ptr = d_out;
111 while (true) {
112 ch = PYTHON::t_op (&d_ptr, &d_out);
113 ++ENV->t_op_no;
115 if (ch == '\0')
116 break;
118 python_tag (tt, d_out, ch, GLOBAL_TYPE_FUNCTION | GLOBAL_TYPE_CLASS, of);
121 free_tt_state (tt);
122 return 0;
125 void pyc_one (char * f_name)
127 char *S;
128 char ch;
129 char *d_ptr,*d_out; // for t_op2
130 struct tt_state_t *tt;
132 tt = CNEW (tt_state_t, 1);
133 bzero (tt, sizeof (struct tt_state_t));
134 tt->d_file_name = strdup (f_name);
135 PYTHON::py_tt_file (tt);
137 d_out = tt->d_output;
138 d_ptr = d_out;
139 while (true) {
140 ch = PYTHON::t_op (&d_ptr, &d_out);
142 if (ch == '\0')
143 break;
145 S = d_out;
146 while (*S == ' ' || *S == '\t')
147 ++S;
149 if (! strncmp (S, "class ", 6))
150 printf ("%s\n", py_prepare (S));
153 free_tt_state (tt);
156 void py_class ()
158 int i;
160 if (! ENV->d_files->get_size ())
161 python_lookup ();
163 for (i = 0; i < ENV->d_files->get_size (); ++i)
164 pyc_one (ENV->d_files->get (i));
166 exit (0);
169 char py_opt (DArray * d_opts, int * i)
171 if (! d_opts || ! i)
172 return 0;
174 if (EQ (d_opts->get (*i), "--python")) {
175 ENV->language = strdup ("Python");
176 return 1;
179 if (EQ (d_opts->get (*i), "--files") && EQ (ENV->language, "Python")) {
180 sblib_find ("./", "*.py", "./python_files");
181 exit (0);
184 return 0;
187 char py_opt2 (DArray * d_opts, int * i)
189 if (EQ (ENV->language, "Python") && EQ (d_opts->get (*i), "--class")) {
190 py_class ();
191 return 1;
193 return 0;
196 void py_info ()
198 printf ("Python plugin.\n");
199 printf ("Version: 1.0\n");
200 printf ("options: --python [--files | --make-ctags | --class ]\n");
203 DArray * plugin_init (struct env_t *env)
205 struct mod_feature * pm;
206 struct mod_language * pm_lang;
207 DArray * Ret;
209 pm = CNEW (mod_feature, 1);
210 pm_lang = CNEW (mod_language, 1);
212 bzero (pm, sizeof (struct mod_feature));
213 bzero (pm_lang, sizeof (struct mod_language));
214 pm_lang->mod.Version = strdup ("1.0");
215 pm_lang->mod.Type = TYPE_LANGUAGE;
216 pm_lang->the = PYTHON::py_tt_file;
217 pm_lang->make_ctags = python_ctags;
218 pm_lang->language = strdup ("Python");
220 pm->mod.Version = strdup ("1.0");
221 pm->opt = py_opt;
222 pm->opt2 = py_opt2;
223 pm->mod.Type = TYPE_FEATURE;
224 pm->mod.info = py_info;
226 Ret = new DArray (2);
227 Ret->add (LPCHAR (pm));
228 Ret->add (LPCHAR (pm_lang));
230 return Ret;