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