Enable SplitWindow build on Windows again
[geany-mirror.git] / tagmanager / latex.c
blob6eafd7bf4ed1e845fadfd90b90841002591a627a
1 /*
2 * $Id$
4 * Copyright (c) 2000-2001, Jérôme Plût
5 * Copyright (c) 2006, Enrico Tröger
7 * This source code is released for free distribution under the terms of the
8 * GNU General Public License.
10 * This module contains functions for generating tags for source files
11 * for the TeX formatting system.
15 * INCLUDE FILES
17 #include "general.h" /* must always come first */
19 #include <ctype.h>
20 #include <string.h>
22 #include "parse.h"
23 #include "read.h"
24 #include "vstring.h"
27 * DATA DEFINITIONS
29 typedef enum {
30 K_COMMAND,
31 K_ENVIRONMENT,
32 K_SECTION,
33 K_SUBSECTION,
34 K_SUBSUBSECTION,
35 K_CHAPTER,
36 K_LABEL
37 } TeXKind;
39 static kindOption TeXKinds[] = {
40 { TRUE, 'f', "function", "command definitions" },
41 { TRUE, 'c', "class", "environment definitions" },
42 { TRUE, 'm', "member", "labels, sections and bibliography" },
43 { TRUE, 'd', "macro", "subsections" },
44 { TRUE, 'v', "variable", "subsubsections" },
45 { TRUE, 'n', "namespace", "chapters"},
46 { TRUE, 's', "struct", "labels and bibliography" }
49 #define TEX_BRACES (1<<0)
50 #define TEX_BSLASH (1<<1)
51 #define TEX_LABEL (1<<2)
54 * FUNCTION DEFINITIONS
57 static int getWord(const char * ref, const char **ptr)
59 const char *p = *ptr;
61 while ((*ref != '\0') && (*p != '\0') && (*ref == *p))
62 ref++, p++;
65 if (*ref)
66 return FALSE;
68 if (*p == '*') /* to allow something like \section*{foobar} */
69 p++;
71 *ptr = p;
72 return TRUE;
75 static void createTag(int flags, TeXKind kind, const char * l)
77 vString *name = vStringNew ();
79 while ((*l == ' '))
80 l++;
81 if (flags & (TEX_BRACES | TEX_LABEL))
83 if (*l == '[')
85 while (*l != ']')
87 if (*l == '\0')
88 goto no_tag;
89 l++;
91 l++; /* skip the closing square bracket */
93 if (*l != '{')
94 goto no_tag;
95 l++;
97 if (flags & TEX_BSLASH)
99 if ((*(l++)) != '\\')
100 goto no_tag;
102 if (flags & TEX_LABEL)
106 vStringPut(name, (int) *l);
107 ++l;
108 } while ((*l != '\0') && (*l != '}'));
109 vStringTerminate(name);
110 if (name->buffer[0] != '}')
111 makeSimpleTag(name, TeXKinds, kind);
113 else if (isalpha((int) *l) || *l == '@')
117 vStringPut (name, (int) *l);
118 ++l;
119 } while (isalpha((int) *l) || *l == '@');
120 vStringTerminate(name);
121 makeSimpleTag(name, TeXKinds, kind);
123 else
125 vStringPut(name, (int) *l);
126 vStringTerminate(name);
127 makeSimpleTag(name, TeXKinds, kind);
130 no_tag:
131 vStringDelete(name);
134 static void findTeXTags(void)
136 const char *line;
138 while ((line = (const char*)fileReadLine()) != NULL)
140 const char *cp = line;
141 /*int escaped = 0;*/
143 for (; *cp != '\0'; cp++)
145 if (*cp == '%')
146 break;
147 if (*cp == '\\')
149 cp++;
151 /* \newcommand{\command} */
152 if (getWord("newcommand", &cp)
153 || getWord("providecommand", &cp)
154 || getWord("renewcommand", &cp)
157 createTag (TEX_BRACES|TEX_BSLASH, K_COMMAND, cp);
158 continue;
161 /* \DeclareMathOperator{\command} */
162 else if (getWord("DeclareMathOperator", &cp))
164 if (*cp == '*')
165 cp++;
166 createTag(TEX_BRACES|TEX_BSLASH, K_COMMAND, cp);
167 continue;
170 /* \def\command */
171 else if (getWord("def", &cp))
173 createTag(TEX_BSLASH, K_COMMAND, cp);
174 continue;
177 /* \newenvironment{name} */
178 else if ( getWord("newenvironment", &cp)
179 || getWord("newtheorem", &cp)
180 || getWord("begin", &cp)
183 createTag(TEX_BRACES, K_ENVIRONMENT, cp);
184 continue;
187 /* \bibitem[label]{key} */
188 else if (getWord("bibitem", &cp))
190 while (*cp == ' ')
191 cp++;
192 if (*(cp++) != '[')
193 break;
194 while ((*cp != '\0') && (*cp != ']'))
195 cp++;
196 if (*(cp++) != ']')
197 break;
198 createTag(TEX_LABEL, K_LABEL, cp);
199 continue;
202 /* \label{key} */
203 else if (getWord("label", &cp))
205 createTag(TEX_LABEL, K_LABEL, cp);
206 continue;
208 /* \section{key} */
209 else if (getWord("section", &cp))
211 createTag(TEX_LABEL, K_SECTION, cp);
212 continue;
214 /* \subsection{key} */
215 else if (getWord("subsection", &cp))
217 createTag(TEX_LABEL, K_SUBSECTION, cp);
218 continue;
220 /* \subsubsection{key} */
221 else if (getWord("subsubsection", &cp))
223 createTag(TEX_LABEL, K_SUBSUBSECTION, cp);
224 continue;
226 /* \chapter{key} */
227 else if (getWord("chapter", &cp))
229 createTag(TEX_LABEL, K_CHAPTER, cp);
230 continue;
237 extern parserDefinition* LaTeXParser (void)
239 static const char *const extensions [] = { "tex", "sty", "idx", NULL };
240 parserDefinition * def = parserNew ("LaTeX");
241 def->kinds = TeXKinds;
242 def->kindCount = KIND_COUNT (TeXKinds);
243 def->extensions = extensions;
244 def->parser = findTeXTags;
245 return def;