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
))
87 l
++; /* skip the closing square bracket */
93 if (flags
& TEX_BSLASH
)
98 if (flags
& TEX_LABEL
)
102 vStringPut(name
, (int) *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
);
115 } while (isalpha((int) *l
) || *l
== '@');
116 vStringTerminate(name
);
117 makeSimpleTag(name
, TeXKinds
, kind
);
121 vStringPut(name
, (int) *l
);
122 vStringTerminate(name
);
123 makeSimpleTag(name
, TeXKinds
, kind
);
130 static void findTeXTags(void)
134 while ((line
= (const char*)fileReadLine()) != NULL
)
136 const char *cp
= line
;
139 for (; *cp
!= '\0'; 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
);
157 /* \DeclareMathOperator{\command} */
158 else if (getWord("DeclareMathOperator", &cp
))
162 createTag(TEX_BRACES
|TEX_BSLASH
, K_COMMAND
, cp
);
167 else if (getWord("def", &cp
))
169 createTag(TEX_BSLASH
, K_COMMAND
, cp
);
173 /* \newenvironment{name} */
174 else if ( getWord("newenvironment", &cp
)
175 || getWord("newtheorem", &cp
)
176 || getWord("begin", &cp
)
179 createTag(TEX_BRACES
, K_ENVIRONMENT
, cp
);
183 /* \bibitem[label]{key} */
184 else if (getWord("bibitem", &cp
))
190 while ((*cp
!= '\0') && (*cp
!= ']'))
194 createTag(TEX_LABEL
, K_LABEL
, cp
);
199 else if (getWord("label", &cp
))
201 createTag(TEX_LABEL
, K_LABEL
, cp
);
205 else if (getWord("section", &cp
))
207 createTag(TEX_LABEL
, K_SECTION
, cp
);
210 /* \subsection{key} */
211 else if (getWord("subsection", &cp
))
213 createTag(TEX_LABEL
, K_SUBSECTION
, cp
);
216 /* \subsubsection{key} */
217 else if (getWord("subsubsection", &cp
))
219 createTag(TEX_LABEL
, K_SUBSUBSECTION
, cp
);
223 else if (getWord("chapter", &cp
))
225 createTag(TEX_LABEL
, K_CHAPTER
, cp
);
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
;