]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
dont use pragma impementation and interface anymore
[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
14 #include "insetcommandparams.h"
15 #include "lyxlex.h"
16 #include "debug.h"
17
18 #include "support/LOstream.h"
19
20 using std::ostream;
21 using std::endl;
22
23
24 InsetCommandParams::InsetCommandParams()
25 {}
26
27
28 InsetCommandParams::InsetCommandParams(string const & n,
29                                         string const & c,
30                                         string const & o)
31         : cmdname(n), contents(c), options(o), preview_(false)
32 {}
33
34
35 string const InsetCommandParams::getAsString() const
36 {
37         return cmdname + "|++|" + contents + "|++|" + options;
38 }
39
40
41 void InsetCommandParams::setFromString(string const & b)
42 {
43         string::size_type idx = b.find("|++|");
44         if (idx == string::npos) {
45                 cmdname = b;
46                 contents = "";
47                 options = "";
48                 return;
49         }
50
51         cmdname = b.substr(0, idx);
52         string tmp = b.substr(idx+4);
53
54         idx = tmp.find("|++|");
55         if (idx == string::npos) {
56                 contents = tmp;
57                 options = "";
58         } else {
59                 contents  = tmp.substr(0, idx);
60                 options = tmp.substr(idx+4);
61         }
62 }
63
64
65 void InsetCommandParams::scanCommand(string const & cmd)
66 {
67         string tcmdname, toptions, tcontents;
68
69         if (cmd.empty()) return;
70
71         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
72
73         // Used to handle things like \command[foo[bar]]{foo{bar}}
74         int nestdepth = 0;
75
76         for (string::size_type i = 0; i < cmd.length(); ++i) {
77                 char c = cmd[i];
78                 if ((state == CMDNAME && c == ' ') ||
79                     (state == CMDNAME && c == '[') ||
80                     (state == CMDNAME && c == '{')) {
81                         state = WS;
82                 }
83                 if ((state == OPTION  && c == ']') ||
84                     (state == CONTENT && c == '}')) {
85                         if (nestdepth == 0) {
86                                 state = WS;
87                         } else {
88                                 --nestdepth;
89                         }
90                 }
91                 if ((state == OPTION  && c == '[') ||
92                     (state == CONTENT && c == '{')) {
93                         ++nestdepth;
94                 }
95                 switch (state) {
96                 case CMDNAME:   tcmdname += c; break;
97                 case OPTION:    toptions += c; break;
98                 case CONTENT:   tcontents += c; break;
99                 case WS:
100                         if (c == '\\') {
101                                 state = CMDNAME;
102                         } else if (c == '[') {
103                                 state = OPTION;
104                                 nestdepth = 0; // Just to be sure
105                         } else if (c == '{') {
106                                 state = CONTENT;
107                                 nestdepth = 0; // Just to be sure
108                         }
109                         break;
110                 }
111         }
112
113         // Don't mess with this.
114         if (!tcmdname.empty())  setCmdName(tcmdname);
115         if (!toptions.empty())  setOptions(toptions);
116         if (!tcontents.empty()) setContents(tcontents);
117
118         if (lyxerr.debugging(Debug::PARSER))
119                 lyxerr << "Command <" <<  cmd
120                        << "> == <" << getCommand()
121                        << "> == <" << getCmdName()
122                        << '|' << getContents()
123                        << '|' << getOptions() << '>' << endl;
124 }
125
126
127 void InsetCommandParams::read(LyXLex & lex)
128 {
129         string token;
130
131         if (lex.eatLine()) {
132                 token = lex.getString();
133                 scanCommand(token);
134         } else {
135                 lex.printError("InsetCommand: Parse error: `$$Token'");
136         }
137
138         while (lex.isOK()) {
139                 lex.next();
140                 token = lex.getString();
141                 if (token == "\\end_inset")
142                         break;
143                 if (token == "preview") {
144                         lex.next();
145                         preview_ = lex.getBool();
146                 }
147         }
148         if (token != "\\end_inset") {
149                 lex.printError("Missing \\end_inset at this point. "
150                                "Read: `$$Token'");
151         }
152 }
153
154
155 void InsetCommandParams::write(ostream & os) const
156 {
157         os << "LatexCommand " << getCommand() << "\n";
158 }
159
160
161 string const InsetCommandParams::getCommand() const
162 {
163         string s;
164         if (!getCmdName().empty()) s += '\\' + getCmdName();
165         if (!getOptions().empty()) s += '[' + getOptions() + ']';
166         s += '{' + getContents() + '}';
167         return s;
168 }
169
170
171 bool operator==(InsetCommandParams const & o1,
172                 InsetCommandParams const & o2)
173 {
174         return o1.getCmdName() == o2.getCmdName()
175                 && o1.getContents() == o2.getContents()
176                 && o1.getOptions() == o2.getOptions()
177                 && o1.preview() == o2.preview();
178 }
179
180
181 bool operator!=(InsetCommandParams const & o1,
182                 InsetCommandParams const & o2)
183 {
184         return !(o1 == o2);
185 }