r5098
[geany-mirror.git] / tagmanager / latex.c
blobb93a9adc9adac0b2554707b35da6d0bec07d7ca1
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 != ']')
86 l++;
87 l++; /* skip the closing square bracket */
89 if (*l != '{')
90 goto no_tag;
91 l++;
93 if (flags & TEX_BSLASH)
95 if ((*(l++)) != '\\')
96 goto no_tag;
98 if (flags & TEX_LABEL)
102 vStringPut(name, (int) *l);
103 ++l;
104 } while ((*l != '\0') && (*l != '}'));
105 vStringTerminate(name);
106 if (name->buffer[0] != '}')
107 makeSimpleTag(name, TeXKinds, kind);
109 else if (isalpha((int) *l) || *l == '@')
113 vStringPut (name, (int) *l);
114 ++l;
115 } while (isalpha((int) *l) || *l == '@');
116 vStringTerminate(name);
117 makeSimpleTag(name, TeXKinds, kind);
119 else
121 vStringPut(name, (int) *l);
122 vStringTerminate(name);
123 makeSimpleTag(name, TeXKinds, kind);
126 no_tag:
127 vStringDelete(name);
130 static void findTeXTags(void)
132 const char *line;
134 while ((line = (const char*)fileReadLine()) != NULL)
136 const char *cp = line;
137 /*int escaped = 0;*/
139 for (; *cp != '\0'; cp++)
141 if (*cp == '%')
142 break;
143 if (*cp == '\\')
145 cp++;
147 /* \newcommand{\command} */
148 if (getWord("newcommand", &cp)
149 || getWord("providecommand", &cp)
150 || getWord("renewcommand", &cp)
153 createTag (TEX_BRACES|TEX_BSLASH, K_COMMAND, cp);
154 continue;
157 /* \DeclareMathOperator{\command} */
158 else if (getWord("DeclareMathOperator", &cp))
160 if (*cp == '*')
161 cp++;
162 createTag(TEX_BRACES|TEX_BSLASH, K_COMMAND, cp);
163 continue;
166 /* \def\command */
167 else if (getWord("def", &cp))
169 createTag(TEX_BSLASH, K_COMMAND, cp);
170 continue;
173 /* \newenvironment{name} */
174 else if ( getWord("newenvironment", &cp)
175 || getWord("newtheorem", &cp)
176 || getWord("begin", &cp)
179 createTag(TEX_BRACES, K_ENVIRONMENT, cp);
180 continue;
183 /* \bibitem[label]{key} */
184 else if (getWord("bibitem", &cp))
186 while (*cp == ' ')
187 cp++;
188 if (*(cp++) != '[')
189 break;
190 while ((*cp != '\0') && (*cp != ']'))
191 cp++;
192 if (*(cp++) != ']')
193 break;
194 createTag(TEX_LABEL, K_LABEL, cp);
195 continue;
198 /* \label{key} */
199 else if (getWord("label", &cp))
201 createTag(TEX_LABEL, K_LABEL, cp);
202 continue;
204 /* \section{key} */
205 else if (getWord("section", &cp))
207 createTag(TEX_LABEL, K_SECTION, cp);
208 continue;
210 /* \subsection{key} */
211 else if (getWord("subsection", &cp))
213 createTag(TEX_LABEL, K_SUBSECTION, cp);
214 continue;
216 /* \subsubsection{key} */
217 else if (getWord("subsubsection", &cp))
219 createTag(TEX_LABEL, K_SUBSUBSECTION, cp);
220 continue;
222 /* \chapter{key} */
223 else if (getWord("chapter", &cp))
225 createTag(TEX_LABEL, K_CHAPTER, cp);
226 continue;
233 extern parserDefinition* LaTeXParser (void)
235 static const char *const extensions [] = { "tex", "sty", "idx", NULL };
236 parserDefinition * def = parserNew ("LaTeX");
237 def->kinds = TeXKinds;
238 def->kindCount = KIND_COUNT (TeXKinds);
239 def->extensions = extensions;
240 def->parser = findTeXTags;
241 return def;