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.
17 #include "general.h" /* must always come first */
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
)
61 while ((*ref
!= '\0') && (*p
!= '\0') && (*ref
== *p
))
68 if (*p
== '*') /* to allow something like \section*{foobar} */
75 static void createTag(int flags
, TeXKind kind
, const char * l
)
77 vString
*name
= vStringNew ();
81 if (flags
& (TEX_BRACES
| TEX_LABEL
))
91 l
++; /* skip the closing square bracket */
97 if (flags
& TEX_BSLASH
)
102 if (flags
& TEX_LABEL
)
106 vStringPut(name
, (int) *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
);
119 } while (isalpha((int) *l
) || *l
== '@');
120 vStringTerminate(name
);
121 makeSimpleTag(name
, TeXKinds
, kind
);
125 vStringPut(name
, (int) *l
);
126 vStringTerminate(name
);
127 makeSimpleTag(name
, TeXKinds
, kind
);
134 static void findTeXTags(void)
138 while ((line
= (const char*)fileReadLine()) != NULL
)
140 const char *cp
= line
;
143 for (; *cp
!= '\0'; 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
);
161 /* \DeclareMathOperator{\command} */
162 else if (getWord("DeclareMathOperator", &cp
))
166 createTag(TEX_BRACES
|TEX_BSLASH
, K_COMMAND
, cp
);
171 else if (getWord("def", &cp
))
173 createTag(TEX_BSLASH
, K_COMMAND
, cp
);
177 /* \newenvironment{name} */
178 else if ( getWord("newenvironment", &cp
)
179 || getWord("newtheorem", &cp
)
180 || getWord("begin", &cp
)
183 createTag(TEX_BRACES
, K_ENVIRONMENT
, cp
);
187 /* \bibitem[label]{key} */
188 else if (getWord("bibitem", &cp
))
194 while ((*cp
!= '\0') && (*cp
!= ']'))
198 createTag(TEX_LABEL
, K_LABEL
, cp
);
203 else if (getWord("label", &cp
))
205 createTag(TEX_LABEL
, K_LABEL
, cp
);
209 else if (getWord("section", &cp
))
211 createTag(TEX_LABEL
, K_SECTION
, cp
);
214 /* \subsection{key} */
215 else if (getWord("subsection", &cp
))
217 createTag(TEX_LABEL
, K_SUBSECTION
, cp
);
220 /* \subsubsection{key} */
221 else if (getWord("subsubsection", &cp
))
223 createTag(TEX_LABEL
, K_SUBSUBSECTION
, cp
);
227 else if (getWord("chapter", &cp
))
229 createTag(TEX_LABEL
, K_CHAPTER
, cp
);
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
;