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