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