]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
Strip out redundant includes (193 of 'em).
[lyx.git] / src / insets / insetcommandparams.C
1 /**
2  * \file insetcommandparams.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetcommandparams.h"
14
15 #include "debug.h"
16 #include "lyxlex.h"
17
18 using std::endl;
19 using std::ostream;
20
21
22 InsetCommandParams::InsetCommandParams()
23 {}
24
25
26 InsetCommandParams::InsetCommandParams(string const & n,
27                                         string const & c,
28                                         string const & o)
29         : cmdname(n), contents(c), options(o), preview_(false)
30 {}
31
32
33 void InsetCommandParams::scanCommand(string const & cmd)
34 {
35         string tcmdname, toptions, tcontents;
36
37         if (cmd.empty()) return;
38
39         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
40
41         // Used to handle things like \command[foo[bar]]{foo{bar}}
42         int nestdepth = 0;
43
44         for (string::size_type i = 0; i < cmd.length(); ++i) {
45                 char c = cmd[i];
46                 if ((state == CMDNAME && c == ' ') ||
47                     (state == CMDNAME && c == '[') ||
48                     (state == CMDNAME && c == '{')) {
49                         state = WS;
50                 }
51                 if ((state == OPTION  && c == ']') ||
52                     (state == CONTENT && c == '}')) {
53                         if (nestdepth == 0) {
54                                 state = WS;
55                         } else {
56                                 --nestdepth;
57                         }
58                 }
59                 if ((state == OPTION  && c == '[') ||
60                     (state == CONTENT && c == '{')) {
61                         ++nestdepth;
62                 }
63                 switch (state) {
64                 case CMDNAME:   tcmdname += c; break;
65                 case OPTION:    toptions += c; break;
66                 case CONTENT:   tcontents += c; break;
67                 case WS:
68                         if (c == '\\') {
69                                 state = CMDNAME;
70                         } else if (c == '[') {
71                                 state = OPTION;
72                                 nestdepth = 0; // Just to be sure
73                         } else if (c == '{') {
74                                 state = CONTENT;
75                                 nestdepth = 0; // Just to be sure
76                         }
77                         break;
78                 }
79         }
80
81         // Don't mess with this.
82         if (!tcmdname.empty())  setCmdName(tcmdname);
83         if (!toptions.empty())  setOptions(toptions);
84         if (!tcontents.empty()) setContents(tcontents);
85
86         if (lyxerr.debugging(Debug::PARSER))
87                 lyxerr << "Command <" <<  cmd
88                        << "> == <" << getCommand()
89                        << "> == <" << getCmdName()
90                        << '|' << getContents()
91                        << '|' << getOptions() << '>' << endl;
92 }
93
94
95 void InsetCommandParams::read(LyXLex & lex)
96 {
97         string token;
98
99         if (lex.eatLine()) {
100                 token = lex.getString();
101                 scanCommand(token);
102         } else {
103                 lex.printError("InsetCommand: Parse error: `$$Token'");
104         }
105
106         while (lex.isOK()) {
107                 lex.next();
108                 token = lex.getString();
109                 if (token == "\\end_inset")
110                         break;
111                 if (token == "preview") {
112                         lex.next();
113                         preview_ = lex.getBool();
114                 }
115         }
116         if (token != "\\end_inset") {
117                 lex.printError("Missing \\end_inset at this point. "
118                                "Read: `$$Token'");
119         }
120 }
121
122
123 void InsetCommandParams::write(ostream & os) const
124 {
125         os << "LatexCommand " << getCommand() << "\n";
126 }
127
128
129 string const InsetCommandParams::getCommand() const
130 {
131         string s;
132         if (!getCmdName().empty()) s += '\\' + getCmdName();
133         if (!getOptions().empty()) s += '[' + getOptions() + ']';
134         s += '{' + getContents() + '}';
135         return s;
136 }
137
138
139 bool operator==(InsetCommandParams const & o1,
140                 InsetCommandParams const & o2)
141 {
142         return o1.getCmdName() == o2.getCmdName()
143                 && o1.getContents() == o2.getContents()
144                 && o1.getOptions() == o2.getOptions()
145                 && o1.preview() == o2.preview();
146 }
147
148
149 bool operator!=(InsetCommandParams const & o1,
150                 InsetCommandParams const & o2)
151 {
152         return !(o1 == o2);
153 }